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 38 2e 20 20 ersion 3.6.18.
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 30 39 2d 31 31 20 31 34 3a 31 36 3a 009-09-11 14:16:
0510: 31 33 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 69 13 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 2f 0a 23 69 triggers..*/.#i
2460: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
2470: 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 0a 23 _TRIGGER_DEPTH.#
2480: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
2490: 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a 23 E_SMALL_STACK).#
24a0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
24b0: 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 AX_TRIGGER_DEPTH
24c0: 20 31 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 10.#else.# defi
24d0: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 ne SQLITE_MAX_TR
24e0: 49 47 47 45 52 5f 44 45 50 54 48 20 31 30 30 30 IGGER_DEPTH 1000
24f0: 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a .#endif.#endif..
2500: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
2510: 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 6d End of sqliteLim
2520: 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a it.h ***********
2530: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2540: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
2550: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
2560: 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
2570: 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
2580: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
2590: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
25a0: 0a 2f 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 73 ./* Disable nuis
25b0: 61 6e 63 65 20 77 61 72 6e 69 6e 67 73 20 6f 6e ance warnings on
25c0: 20 42 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c 65 Borland compile
25d0: 72 73 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 rs */.#if define
25e0: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a d(__BORLANDC__).
25f0: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 63 #pragma warn -rc
2600: 68 20 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c 65 h /* unreachable
2610: 20 63 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d 61 code */.#pragma
2620: 20 77 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 6f warn -ccc /* Co
2630: 6e 64 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 79 ndition is alway
2640: 73 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20 s true or false
2650: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 */.#pragma warn
2660: 2d 61 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 64 -aus /* Assigned
2670: 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 value is never
2680: 75 73 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 used */.#pragma
2690: 77 61 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f 6d warn -csu /* Com
26a0: 70 61 72 69 6e 67 20 73 69 67 6e 65 64 20 61 6e paring signed an
26b0: 64 20 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 70 d unsigned */.#p
26c0: 72 61 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 20 ragma warn -spa
26d0: 2f 2a 20 53 75 73 70 69 63 69 6f 75 73 20 70 6f /* Suspicious po
26e0: 69 6e 74 65 72 20 61 72 69 74 68 6d 65 74 69 63 inter arithmetic
26f0: 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4e */.#endif../* N
2700: 65 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75 eeded for variou
2710: 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e s definitions...
2720: 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e 55 */.#ifndef _GNU
2730: 5f 53 4f 55 52 43 45 0a 23 20 64 65 66 69 6e 65 _SOURCE.# define
2740: 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 6e _GNU_SOURCE.#en
2750: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 dif../*.** Inclu
2760: 64 65 20 73 74 61 6e 64 61 72 64 20 68 65 61 64 de standard head
2770: 65 72 20 66 69 6c 65 73 20 61 73 20 6e 65 63 65 er files as nece
2780: 73 73 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 20 ssary.*/.#ifdef
2790: 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 69 HAVE_STDINT_H.#i
27a0: 6e 63 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68 nclude <stdint.h
27b0: 3e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 >.#endif.#ifdef
27c0: 48 41 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 0a HAVE_INTTYPES_H.
27d0: 23 69 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 70 #include <inttyp
27e0: 65 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 23 64 es.h>.#endif..#d
27f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 efine SQLITE_IND
2800: 45 58 5f 53 41 4d 50 4c 45 53 20 31 30 0a 0a 2f EX_SAMPLES 10../
2810: 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 *.** This macro
2820: 69 73 20 75 73 65 64 20 74 6f 20 22 68 69 64 65 is used to "hide
2830: 22 20 73 6f 6d 65 20 75 67 6c 69 6e 65 73 73 20 " some ugliness
2840: 69 6e 20 63 61 73 74 69 6e 67 20 61 6e 20 69 6e in casting an in
2850: 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 61 20 t.** value to a
2860: 70 74 72 20 76 61 6c 75 65 20 75 6e 64 65 72 20 ptr value under
2870: 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20 the MSVC 64-bit
2880: 63 6f 6d 70 69 6c 65 72 2e 20 20 20 43 61 73 74 compiler. Cast
2890: 69 6e 67 0a 2a 2a 20 6e 6f 6e 20 36 34 2d 62 69 ing.** non 64-bi
28a0: 74 20 76 61 6c 75 65 73 20 74 6f 20 70 74 72 20 t values to ptr
28b0: 74 79 70 65 73 20 72 65 73 75 6c 74 73 20 69 6e types results in
28c0: 20 61 20 22 68 61 72 64 22 20 65 72 72 6f 72 20 a "hard" error
28d0: 77 69 74 68 20 0a 2a 2a 20 74 68 65 20 4d 53 56 with .** the MSV
28e0: 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 C 64-bit compile
28f0: 72 20 77 68 69 63 68 20 74 68 69 73 20 61 74 74 r which this att
2900: 65 6d 70 74 73 20 74 6f 20 61 76 6f 69 64 2e 20 empts to avoid.
2910: 20 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6d 70 6c 65 .**.** A simple
2920: 20 63 6f 6d 70 69 6c 65 72 20 70 72 61 67 6d 61 compiler pragma
2930: 20 6f 72 20 63 61 73 74 69 6e 67 20 73 65 71 75 or casting sequ
2940: 65 6e 63 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 ence could not b
2950: 65 20 66 6f 75 6e 64 0a 2a 2a 20 74 6f 20 63 6f e found.** to co
2960: 72 72 65 63 74 20 74 68 69 73 20 69 6e 20 61 6c rrect this in al
2970: 6c 20 73 69 74 75 61 74 69 6f 6e 73 2c 20 73 6f l situations, so
2980: 20 74 68 69 73 20 6d 61 63 72 6f 20 77 61 73 20 this macro was
2990: 69 6e 74 72 6f 64 75 63 65 64 2e 0a 2a 2a 0a 2a introduced..**.*
29a0: 2a 20 49 74 20 63 6f 75 6c 64 20 62 65 20 61 72 * It could be ar
29b0: 67 75 65 64 20 74 68 61 74 20 74 68 65 20 69 6e gued that the in
29c0: 74 70 74 72 5f 74 20 74 79 70 65 20 63 6f 75 6c tptr_t type coul
29d0: 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 69 d be used in thi
29e0: 73 0a 2a 2a 20 63 61 73 65 2c 20 62 75 74 20 74 s.** case, but t
29f0: 68 61 74 20 74 79 70 65 20 69 73 20 6e 6f 74 20 hat type is not
2a00: 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c 6c available on all
2a10: 20 63 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 20 0a compilers, or .
2a20: 2a 2a 20 72 65 71 75 69 72 65 73 20 74 68 65 20 ** requires the
2a30: 23 69 6e 63 6c 75 64 65 20 6f 66 20 73 70 65 63 #include of spec
2a40: 69 66 69 63 20 68 65 61 64 65 72 73 20 77 68 69 ific headers whi
2a50: 63 68 20 64 69 66 66 65 72 73 20 62 65 74 77 65 ch differs betwe
2a60: 65 6e 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e en.** platforms.
2a70: 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 33 .**.** Ticket #3
2a80: 38 36 30 3a 20 20 54 68 65 20 6c 6c 76 6d 2d 67 860: The llvm-g
2a90: 63 63 2d 34 2e 32 20 63 6f 6d 70 69 6c 65 72 20 cc-4.2 compiler
2aa0: 66 72 6f 6d 20 41 70 70 6c 65 20 63 68 6f 6b 65 from Apple choke
2ab0: 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 28 28 76 6f s on.** the ((vo
2ac0: 69 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b id*)&((char*)0)[
2ad0: 58 5d 29 20 63 6f 6e 73 74 72 75 63 74 2e 20 20 X]) construct.
2ae0: 42 75 74 20 4d 53 56 43 20 63 68 6f 6b 65 73 20 But MSVC chokes
2af0: 6f 6e 20 28 28 76 6f 69 64 2a 29 28 58 29 29 2e on ((void*)(X)).
2b00: 0a 2a 2a 20 53 6f 20 77 65 20 68 61 76 65 20 74 .** So we have t
2b10: 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6d 61 63 o define the mac
2b20: 72 6f 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 ros in different
2b30: 20 77 61 79 73 20 64 65 70 65 6e 64 69 6e 67 20 ways depending
2b40: 6f 6e 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c on the.** compil
2b50: 65 72 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e er..*/.#if defin
2b60: 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 23 20 69 ed(__GNUC__).# i
2b70: 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 53 f defined(HAVE_S
2b80: 54 44 49 4e 54 5f 48 29 0a 23 20 20 20 64 65 66 TDINT_H).# def
2b90: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 ine SQLITE_INT_T
2ba0: 4f 5f 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 O_PTR(X) ((void
2bb0: 2a 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29 *)(intptr_t)(X))
2bc0: 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 .# define SQLI
2bd0: 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29 TE_PTR_TO_INT(X)
2be0: 20 20 28 28 69 6e 74 29 28 69 6e 74 70 74 72 5f ((int)(intptr_
2bf0: 74 29 28 58 29 29 0a 23 20 65 6c 73 65 0a 23 20 t)(X)).# else.#
2c00: 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
2c10: 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 28 INT_TO_PTR(X) (
2c20: 28 76 6f 69 64 2a 29 28 58 29 29 0a 23 20 20 20 (void*)(X)).#
2c30: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 define SQLITE_PT
2c40: 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 R_TO_INT(X) ((i
2c50: 6e 74 29 28 58 29 29 0a 23 20 65 6e 64 69 66 0a nt)(X)).# endif.
2c60: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
2c70: 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 QLITE_INT_TO_PTR
2c80: 28 58 29 20 20 20 28 28 76 6f 69 64 2a 29 26 28 (X) ((void*)&(
2c90: 28 63 68 61 72 2a 29 30 29 5b 58 5d 29 0a 23 20 (char*)0)[X]).#
2ca0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 define SQLITE_PT
2cb0: 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20 20 28 28 R_TO_INT(X) ((
2cc0: 69 6e 74 29 28 28 28 63 68 61 72 2a 29 58 29 2d int)(((char*)X)-
2cd0: 28 63 68 61 72 2a 29 30 29 29 0a 23 65 6e 64 69 (char*)0)).#endi
2ce0: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 f.../*.** The SQ
2cf0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
2d00: 6d 61 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65 macro must be de
2d10: 66 69 6e 65 64 20 61 73 20 65 69 74 68 65 72 20 fined as either
2d20: 30 20 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72 0 or 1..** Older
2d30: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
2d40: 69 74 65 20 75 73 65 64 20 61 6e 20 6f 70 74 69 ite used an opti
2d50: 6f 6e 61 6c 20 54 48 52 45 41 44 53 41 46 45 20 onal THREADSAFE
2d60: 6d 61 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75 70 macro..** We sup
2d70: 70 6f 72 74 20 74 68 61 74 20 66 6f 72 20 6c 65 port that for le
2d80: 67 61 63 79 0a 2a 2f 0a 23 69 66 20 21 64 65 66 gacy.*/.#if !def
2d90: 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 ined(SQLITE_THRE
2da0: 41 44 53 41 46 45 29 0a 23 69 66 20 64 65 66 69 ADSAFE).#if defi
2db0: 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29 0a ned(THREADSAFE).
2dc0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
2dd0: 54 48 52 45 41 44 53 41 46 45 20 54 48 52 45 41 THREADSAFE THREA
2de0: 44 53 41 46 45 0a 23 65 6c 73 65 0a 23 20 64 65 DSAFE.#else.# de
2df0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 fine SQLITE_THRE
2e00: 41 44 53 41 46 45 20 31 0a 23 65 6e 64 69 66 0a ADSAFE 1.#endif.
2e10: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
2e20: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
2e30: 5f 4d 45 4d 53 54 41 54 55 53 20 6d 61 63 72 6f _MEMSTATUS macro
2e40: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 must be defined
2e50: 20 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 as either 0 or
2e60: 31 2e 0a 2a 2a 20 49 74 20 64 65 74 65 72 6d 69 1..** It determi
2e70: 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e nes whether or n
2e80: 6f 74 20 74 68 65 20 66 65 61 74 75 72 65 73 20 ot the features
2e90: 72 65 6c 61 74 65 64 20 74 6f 20 0a 2a 2a 20 53 related to .** S
2ea0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d QLITE_CONFIG_MEM
2eb0: 53 54 41 54 55 53 20 61 72 65 20 61 76 61 69 6c STATUS are avail
2ec0: 61 62 6c 65 20 62 79 20 64 65 66 61 75 6c 74 20 able by default
2ed0: 6f 72 20 6e 6f 74 2e 20 54 68 69 73 20 76 61 6c or not. This val
2ee0: 75 65 20 63 61 6e 0a 2a 2a 20 62 65 20 6f 76 65 ue can.** be ove
2ef0: 72 72 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69 rridden at runti
2f00: 6d 65 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c me using the sql
2f10: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 41 50 ite3_config() AP
2f20: 49 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e I..*/.#if !defin
2f30: 65 64 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ed(SQLITE_DEFAUL
2f40: 54 5f 4d 45 4d 53 54 41 54 55 53 29 0a 23 20 64 T_MEMSTATUS).# d
2f50: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 efine SQLITE_DEF
2f60: 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 31 AULT_MEMSTATUS 1
2f70: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 .#endif../*.** E
2f80: 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 xactly one of th
2f90: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
2fa0: 6f 73 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e os must be defin
2fb0: 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a ed in order to.*
2fc0: 2a 20 73 70 65 63 69 66 79 20 77 68 69 63 68 20 * specify which
2fd0: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
2fe0: 6e 20 73 75 62 73 79 73 74 65 6d 20 74 6f 20 75 n subsystem to u
2ff0: 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 se..**.** SQ
3000: 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c LITE_SYSTEM_MALL
3010: 4f 43 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55 OC // U
3020: 73 65 20 6e 6f 72 6d 61 6c 20 73 79 73 74 65 6d se normal system
3030: 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 malloc().**
3040: 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 SQLITE_MEMDEBUG
3050: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
3060: 2f 20 44 65 62 75 67 67 69 6e 67 20 76 65 72 73 / Debugging vers
3070: 69 6f 6e 20 6f 66 20 73 79 73 74 65 6d 20 6d 61 ion of system ma
3080: 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 lloc().** SQ
3090: 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 LITE_MEMORY_SIZE
30a0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 69 // i
30b0: 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 6f nternal allocato
30c0: 72 20 23 31 0a 2a 2a 20 20 20 20 20 53 51 4c 49 r #1.** SQLI
30d0: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a TE_MMAP_HEAP_SIZ
30e0: 45 20 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74 E // int
30f0: 65 72 6e 61 6c 20 6d 6d 61 70 28 29 20 61 6c 6c ernal mmap() all
3100: 6f 63 61 74 6f 72 0a 2a 2a 20 20 20 20 20 53 51 ocator.** SQ
3110: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 LITE_POW2_MEMORY
3120: 5f 53 49 5a 45 20 20 20 20 20 20 20 2f 2f 20 69 _SIZE // i
3130: 6e 74 65 72 6e 61 6c 20 70 6f 77 65 72 2d 6f 66 nternal power-of
3140: 2d 74 77 6f 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a -two allocator.*
3150: 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 *.** If none of
3160: 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 64 65 the above are de
3170: 66 69 6e 65 64 2c 20 74 68 65 6e 20 73 65 74 20 fined, then set
3180: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
3190: 4c 4c 4f 43 20 61 73 0a 2a 2a 20 74 68 65 20 64 LLOC as.** the d
31a0: 65 66 61 75 6c 74 2e 0a 2a 2f 0a 23 69 66 20 64 efault..*/.#if d
31b0: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 efined(SQLITE_SY
31c0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 STEM_MALLOC)+def
31d0: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 ined(SQLITE_MEMD
31e0: 45 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 EBUG)+\. defi
31f0: 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 ned(SQLITE_MEMOR
3200: 59 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 Y_SIZE)+defined(
3210: 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 SQLITE_MMAP_HEAP
3220: 5f 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 _SIZE)+\. def
3230: 69 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 ined(SQLITE_POW2
3240: 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3e 31 0a _MEMORY_SIZE)>1.
3250: 23 20 65 72 72 6f 72 20 22 41 74 20 6d 6f 73 74 # error "At most
3260: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
3270: 6f 77 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 owing compile-ti
3280: 6d 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e me configuration
3290: 20 6f 70 74 69 6f 6e 73 5c 0a 20 69 73 20 61 6c options\. is al
32a0: 6c 6f 77 73 3a 20 53 51 4c 49 54 45 5f 53 59 53 lows: SQLITE_SYS
32b0: 54 45 4d 5f 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49 TEM_MALLOC, SQLI
32c0: 54 45 5f 4d 45 4d 44 45 42 55 47 2c 20 53 51 4c TE_MEMDEBUG, SQL
32d0: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2c ITE_MEMORY_SIZE,
32e0: 5c 0a 20 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 \. SQLITE_MMAP_H
32f0: 45 41 50 5f 53 49 5a 45 2c 20 53 51 4c 49 54 45 EAP_SIZE, SQLITE
3300: 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a _POW2_MEMORY_SIZ
3310: 45 22 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 E".#endif.#if de
3320: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53 fined(SQLITE_SYS
3330: 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69 TEM_MALLOC)+defi
3340: 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45 ned(SQLITE_MEMDE
3350: 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e BUG)+\. defin
3360: 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59 ed(SQLITE_MEMORY
3370: 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53 _SIZE)+defined(S
3380: 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f QLITE_MMAP_HEAP_
3390: 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69 SIZE)+\. defi
33a0: 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f ned(SQLITE_POW2_
33b0: 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3d 3d 30 0a MEMORY_SIZE)==0.
33c0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
33d0: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 31 0a SYSTEM_MALLOC 1.
33e0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 #endif../*.** If
33f0: 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 SQLITE_MALLOC_S
3400: 4f 46 54 5f 4c 49 4d 49 54 20 69 73 20 6e 6f 74 OFT_LIMIT is not
3410: 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 72 79 20 zero, then try
3420: 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 to keep the.** s
3430: 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 izes of memory a
3440: 6c 6c 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f 77 llocations below
3450: 20 74 68 69 73 20 76 61 6c 75 65 20 77 68 65 72 this value wher
3460: 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 e possible..*/.#
3470: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
3480: 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c TE_MALLOC_SOFT_L
3490: 49 4d 49 54 29 0a 23 20 64 65 66 69 6e 65 20 53 IMIT).# define S
34a0: 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 QLITE_MALLOC_SOF
34b0: 54 5f 4c 49 4d 49 54 20 31 30 32 34 0a 23 65 6e T_LIMIT 1024.#en
34c0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65 dif../*.** We ne
34d0: 65 64 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f ed to define _XO
34e0: 50 45 4e 5f 53 4f 55 52 43 45 20 61 73 20 66 6f PEN_SOURCE as fo
34f0: 6c 6c 6f 77 73 20 69 6e 20 6f 72 64 65 72 20 74 llows in order t
3500: 6f 20 65 6e 61 62 6c 65 0a 2a 2a 20 72 65 63 75 o enable.** recu
3510: 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 6f 6e rsive mutexes on
3520: 20 6d 6f 73 74 20 55 6e 69 78 20 73 79 73 74 65 most Unix syste
3530: 6d 73 2e 20 20 42 75 74 20 4d 61 63 20 4f 53 20 ms. But Mac OS
3540: 58 20 69 73 20 64 69 66 66 65 72 65 6e 74 2e 0a X is different..
3550: 2a 2a 20 54 68 65 20 5f 58 4f 50 45 4e 5f 53 4f ** The _XOPEN_SO
3560: 55 52 43 45 20 64 65 66 69 6e 65 20 63 61 75 73 URCE define caus
3570: 65 73 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 es problems for
3580: 4d 61 63 20 4f 53 20 58 20 77 65 20 61 72 65 20 Mac OS X we are
3590: 74 6f 6c 64 2c 0a 2a 2a 20 73 6f 20 69 74 20 69 told,.** so it i
35a0: 73 20 6f 6d 69 74 74 65 64 20 74 68 65 72 65 2e s omitted there.
35b0: 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32 36 See ticket #26
35c0: 37 33 2e 0a 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20 73..**.** Later
35d0: 77 65 20 6c 65 61 72 6e 20 74 68 61 74 20 5f 58 we learn that _X
35e0: 4f 50 45 4e 5f 53 4f 55 52 43 45 20 69 73 20 70 OPEN_SOURCE is p
35f0: 6f 6f 72 6c 79 20 6f 72 20 69 6e 63 6f 72 72 65 oorly or incorre
3600: 63 74 6c 79 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e ctly.** implemen
3610: 74 65 64 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74 ted on some syst
3620: 65 6d 73 2e 20 20 53 6f 20 77 65 20 61 76 6f 69 ems. So we avoi
3630: 64 20 64 65 66 69 6e 69 6e 67 20 69 74 20 61 74 d defining it at
3640: 20 61 6c 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73 all.** if it is
3650: 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e 65 64 already defined
3660: 20 6f 72 20 69 66 20 69 74 20 69 73 20 75 6e 6e or if it is unn
3670: 65 65 64 65 64 20 62 65 63 61 75 73 65 20 77 65 eeded because we
3680: 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e are.** not doin
3690: 67 20 61 20 74 68 72 65 61 64 73 61 66 65 20 62 g a threadsafe b
36a0: 75 69 6c 64 2e 20 20 54 69 63 6b 65 74 20 23 32 uild. Ticket #2
36b0: 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 681..**.** See a
36c0: 6c 73 6f 20 74 69 63 6b 65 74 20 23 32 37 34 31 lso ticket #2741
36d0: 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 ..*/.#if !define
36e0: 64 28 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 29 d(_XOPEN_SOURCE)
36f0: 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 44 && !defined(__D
3700: 41 52 57 49 4e 5f 5f 29 20 26 26 20 21 64 65 66 ARWIN__) && !def
3710: 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
3720: 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 && SQLITE_THREAD
3730: 53 41 46 45 0a 23 20 20 64 65 66 69 6e 65 20 5f SAFE.# define _
3740: 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 35 30 30 XOPEN_SOURCE 500
3750: 20 20 2f 2a 20 4e 65 65 64 65 64 20 74 6f 20 65 /* Needed to e
3760: 6e 61 62 6c 65 20 70 74 68 72 65 61 64 20 72 65 nable pthread re
3770: 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 cursive mutexes
3780: 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a */.#endif../*.**
3790: 20 54 68 65 20 54 43 4c 20 68 65 61 64 65 72 73 The TCL headers
37a0: 20 61 72 65 20 6f 6e 6c 79 20 6e 65 65 64 65 64 are only needed
37b0: 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 when compiling
37c0: 74 68 65 20 54 43 4c 20 62 69 6e 64 69 6e 67 73 the TCL bindings
37d0: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
37e0: 28 53 51 4c 49 54 45 5f 54 43 4c 29 20 7c 7c 20 (SQLITE_TCL) ||
37f0: 64 65 66 69 6e 65 64 28 54 43 4c 53 48 29 0a 23 defined(TCLSH).#
3800: 20 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68 3e include <tcl.h>
3810: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d .#endif../*.** M
3820: 61 6e 79 20 70 65 6f 70 6c 65 20 61 72 65 20 66 any people are f
3830: 61 69 6c 69 6e 67 20 74 6f 20 73 65 74 20 2d 44 ailing to set -D
3840: 4e 44 45 42 55 47 3d 31 20 77 68 65 6e 20 63 6f NDEBUG=1 when co
3850: 6d 70 69 6c 69 6e 67 20 53 51 4c 69 74 65 2e 0a mpiling SQLite..
3860: 2a 2a 20 53 65 74 74 69 6e 67 20 4e 44 45 42 55 ** Setting NDEBU
3870: 47 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 G makes the code
3880: 20 73 6d 61 6c 6c 65 72 20 61 6e 64 20 72 75 6e smaller and run
3890: 20 66 61 73 74 65 72 2e 20 20 53 6f 20 74 68 65 faster. So the
38a0: 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 6c 69 following.** li
38b0: 6e 65 73 20 61 72 65 20 61 64 64 65 64 20 74 6f nes are added to
38c0: 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 automatically s
38d0: 65 74 20 4e 44 45 42 55 47 20 75 6e 6c 65 73 73 et NDEBUG unless
38e0: 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 45 the -DSQLITE_DE
38f0: 42 55 47 3d 31 0a 2a 2a 20 6f 70 74 69 6f 6e 20 BUG=1.** option
3900: 69 73 20 73 65 74 2e 20 20 54 68 75 73 20 4e 44 is set. Thus ND
3910: 45 42 55 47 20 62 65 63 6f 6d 65 73 20 61 6e 20 EBUG becomes an
3920: 6f 70 74 2d 69 6e 20 72 61 74 68 65 72 20 74 68 opt-in rather th
3930: 61 6e 20 61 6e 20 6f 70 74 2d 6f 75 74 0a 2a 2a an an opt-out.**
3940: 20 66 65 61 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 feature..*/.#if
3950: 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
3960: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
3970: 4c 49 54 45 5f 44 45 42 55 47 29 20 0a 23 20 64 LITE_DEBUG) .# d
3980: 65 66 69 6e 65 20 4e 44 45 42 55 47 20 31 0a 23 efine NDEBUG 1.#
3990: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
39a0: 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 testcase() macr
39b0: 6f 20 69 73 20 75 73 65 64 20 74 6f 20 61 69 64 o is used to aid
39c0: 20 69 6e 20 63 6f 76 65 72 61 67 65 20 74 65 73 in coverage tes
39d0: 74 69 6e 67 2e 20 20 57 68 65 6e 20 0a 2a 2a 20 ting. When .**
39e0: 64 6f 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 doing coverage t
39f0: 65 73 74 69 6e 67 2c 20 74 68 65 20 63 6f 6e 64 esting, the cond
3a00: 69 74 69 6f 6e 20 69 6e 73 69 64 65 20 74 68 65 ition inside the
3a10: 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 argument to.**
3a20: 74 65 73 74 63 61 73 65 28 29 20 6d 75 73 74 20 testcase() must
3a30: 62 65 20 65 76 61 6c 75 61 74 65 64 20 62 6f 74 be evaluated bot
3a40: 68 20 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 h true and false
3a50: 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 in order to.**
3a60: 67 65 74 20 66 75 6c 6c 20 62 72 61 6e 63 68 20 get full branch
3a70: 63 6f 76 65 72 61 67 65 2e 20 20 54 68 65 20 74 coverage. The t
3a80: 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 estcase() macro
3a90: 69 73 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74 is inserted.** t
3aa0: 6f 20 68 65 6c 70 20 65 6e 73 75 72 65 20 61 64 o help ensure ad
3ab0: 65 71 75 61 74 65 20 74 65 73 74 20 63 6f 76 65 equate test cove
3ac0: 72 61 67 65 20 69 6e 20 70 6c 61 63 65 73 20 77 rage in places w
3ad0: 68 65 72 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 here simple.** c
3ae0: 6f 6e 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f ondition/decisio
3af0: 6e 20 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e n coverage is in
3b00: 61 64 65 71 75 61 74 65 2e 20 20 46 6f 72 20 65 adequate. For e
3b10: 78 61 6d 70 6c 65 2c 20 74 65 73 74 63 61 73 65 xample, testcase
3b20: 28 29 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 ().** can be use
3b30: 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 62 d to make sure b
3b40: 6f 75 6e 64 61 72 79 20 76 61 6c 75 65 73 20 61 oundary values a
3b50: 72 65 20 74 65 73 74 65 64 2e 20 20 46 6f 72 0a re tested. For.
3b60: 2a 2a 20 62 69 74 6d 61 73 6b 20 74 65 73 74 73 ** bitmask tests
3b70: 2c 20 74 65 73 74 63 61 73 65 28 29 20 63 61 6e , testcase() can
3b80: 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 be used to make
3b90: 20 73 75 72 65 20 65 61 63 68 20 62 69 74 0a 2a sure each bit.*
3ba0: 2a 20 69 73 20 73 69 67 6e 69 66 69 63 61 6e 74 * is significant
3bb0: 20 61 6e 64 20 75 73 65 64 20 61 74 20 6c 65 61 and used at lea
3bc0: 73 74 20 6f 6e 63 65 2e 20 20 4f 6e 20 73 77 69 st once. On swi
3bd0: 74 63 68 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a tch statements.*
3be0: 2a 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 * where multiple
3bf0: 20 63 61 73 65 73 20 67 6f 20 74 6f 20 74 68 65 cases go to the
3c00: 20 73 61 6d 65 20 62 6c 6f 63 6b 20 6f 66 20 63 same block of c
3c10: 6f 64 65 2c 20 74 65 73 74 63 61 73 65 28 29 0a ode, testcase().
3c20: 2a 2a 20 63 61 6e 20 69 6e 73 75 72 65 20 74 68 ** can insure th
3c30: 61 74 20 61 6c 6c 20 63 61 73 65 73 20 61 72 65 at all cases are
3c40: 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a evaluated..**.*
3c50: 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
3c60: 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 COVERAGE_TEST.SQ
3c70: 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
3c80: 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 oid sqlite3Cover
3c90: 61 67 65 28 69 6e 74 29 3b 0a 23 20 64 65 66 69 age(int);.# defi
3ca0: 6e 65 20 74 65 73 74 63 61 73 65 28 58 29 20 20 ne testcase(X)
3cb0: 69 66 28 20 58 20 29 7b 20 73 71 6c 69 74 65 33 if( X ){ sqlite3
3cc0: 43 6f 76 65 72 61 67 65 28 5f 5f 4c 49 4e 45 5f Coverage(__LINE_
3cd0: 5f 29 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 _); }.#else.# de
3ce0: 66 69 6e 65 20 74 65 73 74 63 61 73 65 28 58 29 fine testcase(X)
3cf0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
3d00: 68 65 20 54 45 53 54 4f 4e 4c 59 20 6d 61 63 72 he TESTONLY macr
3d10: 6f 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e 63 o is used to enc
3d20: 6c 6f 73 65 20 76 61 72 69 61 62 6c 65 20 64 65 lose variable de
3d30: 63 6c 61 72 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a clarations or.**
3d40: 20 6f 74 68 65 72 20 62 69 74 73 20 6f 66 20 63 other bits of c
3d50: 6f 64 65 20 74 68 61 74 20 61 72 65 20 6e 65 65 ode that are nee
3d60: 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 ded to support t
3d70: 68 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 he arguments.**
3d80: 77 69 74 68 69 6e 20 74 65 73 74 63 61 73 65 28 within testcase(
3d90: 29 20 61 6e 64 20 61 73 73 65 72 74 28 29 20 6d ) and assert() m
3da0: 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 acros..*/.#if !d
3db0: 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c efined(NDEBUG) |
3dc0: 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
3dd0: 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a _COVERAGE_TEST).
3de0: 23 20 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c # define TESTONL
3df0: 59 28 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20 Y(X) X.#else.#
3e00: 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28 define TESTONLY(
3e10: 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a X).#endif../*.**
3e20: 20 53 6f 6d 65 74 69 6d 65 73 20 77 65 20 6e 65 Sometimes we ne
3e30: 65 64 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e ed a small amoun
3e40: 74 20 6f 66 20 63 6f 64 65 20 73 75 63 68 20 61 t of code such a
3e50: 73 20 61 20 76 61 72 69 61 62 6c 65 20 69 6e 69 s a variable ini
3e60: 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 tialization.** t
3e70: 6f 20 73 65 74 75 70 20 66 6f 72 20 61 20 6c 61 o setup for a la
3e80: 74 65 72 20 61 73 73 65 72 74 28 29 20 73 74 61 ter assert() sta
3e90: 74 65 6d 65 6e 74 2e 20 20 57 65 20 64 6f 20 6e tement. We do n
3ea0: 6f 74 20 77 61 6e 74 20 74 68 69 73 20 63 6f 64 ot want this cod
3eb0: 65 20 74 6f 0a 2a 2a 20 61 70 70 65 61 72 20 77 e to.** appear w
3ec0: 68 65 6e 20 61 73 73 65 72 74 28 29 20 69 73 20 hen assert() is
3ed0: 64 69 73 61 62 6c 65 64 2e 20 20 54 68 65 20 66 disabled. The f
3ee0: 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 ollowing macro i
3ef0: 73 20 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 75 s therefore.** u
3f00: 73 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 sed to contain t
3f10: 68 61 74 20 73 65 74 75 70 20 63 6f 64 65 2e 20 hat setup code.
3f20: 20 54 68 65 20 22 56 56 41 22 20 61 63 72 6f 6e The "VVA" acron
3f30: 79 6d 20 73 74 61 6e 64 73 20 66 6f 72 0a 2a 2a ym stands for.**
3f40: 20 22 56 65 72 69 66 69 63 61 74 69 6f 6e 2c 20 "Verification,
3f50: 56 61 6c 69 64 61 74 69 6f 6e 2c 20 61 6e 64 20 Validation, and
3f60: 41 63 63 72 65 64 69 74 61 74 69 6f 6e 22 2e 20 Accreditation".
3f70: 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
3f80: 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 74 the.** code wit
3f90: 68 69 6e 20 56 56 41 5f 4f 4e 4c 59 28 29 20 77 hin VVA_ONLY() w
3fa0: 69 6c 6c 20 6f 6e 6c 79 20 72 75 6e 20 64 75 72 ill only run dur
3fb0: 69 6e 67 20 76 65 72 69 66 69 63 61 74 69 6f 6e ing verification
3fc0: 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2f 0a 23 processes..*/.#
3fd0: 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 20 ifndef NDEBUG.#
3fe0: 64 65 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 define VVA_ONLY(
3ff0: 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 X) X.#else.# de
4000: 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29 fine VVA_ONLY(X)
4010: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
4020: 68 65 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 he ALWAYS and NE
4030: 56 45 52 20 6d 61 63 72 6f 73 20 73 75 72 72 6f VER macros surro
4040: 75 6e 64 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 und boolean expr
4050: 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 0a 2a essions which .*
4060: 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 * are intended t
4070: 6f 20 61 6c 77 61 79 73 20 62 65 20 74 72 75 65 o always be true
4080: 20 6f 72 20 66 61 6c 73 65 2c 20 72 65 73 70 65 or false, respe
4090: 63 74 69 76 65 6c 79 2e 20 20 53 75 63 68 0a 2a ctively. Such.*
40a0: 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 6f * expressions co
40b0: 75 6c 64 20 62 65 20 6f 6d 69 74 74 65 64 20 66 uld be omitted f
40c0: 72 6f 6d 20 74 68 65 20 63 6f 64 65 20 63 6f 6d rom the code com
40d0: 70 6c 65 74 65 6c 79 2e 20 20 42 75 74 20 74 68 pletely. But th
40e0: 65 79 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 ey.** are includ
40f0: 65 64 20 69 6e 20 61 20 66 65 77 20 63 61 73 65 ed in a few case
4100: 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e s in order to en
4110: 68 61 6e 63 65 20 74 68 65 20 72 65 73 69 6c 69 hance the resili
4120: 65 6e 63 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 ence.** of SQLit
4130: 65 20 74 6f 20 75 6e 65 78 70 65 63 74 65 64 20 e to unexpected
4140: 62 65 68 61 76 69 6f 72 20 2d 20 74 6f 20 6d 61 behavior - to ma
4150: 6b 65 20 74 68 65 20 63 6f 64 65 20 22 73 65 6c ke the code "sel
4160: 66 2d 68 65 61 6c 69 6e 67 22 0a 2a 2a 20 6f 72 f-healing".** or
4170: 20 22 64 75 63 74 69 6c 65 22 20 72 61 74 68 65 "ductile" rathe
4180: 72 20 74 68 61 6e 20 62 65 69 6e 67 20 22 62 72 r than being "br
4190: 69 74 74 6c 65 22 20 61 6e 64 20 63 72 61 73 68 ittle" and crash
41a0: 69 6e 67 20 61 74 20 74 68 65 20 66 69 72 73 74 ing at the first
41b0: 0a 2a 2a 20 68 69 6e 74 20 6f 66 20 75 6e 70 6c .** hint of unpl
41c0: 61 6e 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a anned behavior..
41d0: 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 **.** In other w
41e0: 6f 72 64 73 2c 20 41 4c 57 41 59 53 20 61 6e 64 ords, ALWAYS and
41f0: 20 4e 45 56 45 52 20 61 72 65 20 61 64 64 65 64 NEVER are added
4200: 20 66 6f 72 20 64 65 66 65 6e 73 69 76 65 20 63 for defensive c
4210: 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ode..**.** When
4220: 64 6f 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 doing coverage t
4230: 65 73 74 69 6e 67 20 41 4c 57 41 59 53 20 61 6e esting ALWAYS an
4240: 64 20 4e 45 56 45 52 20 61 72 65 20 68 61 72 64 d NEVER are hard
4250: 2d 63 6f 64 65 64 20 74 6f 0a 2a 2a 20 62 65 20 -coded to.** be
4260: 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 73 true and false s
4270: 6f 20 74 68 61 74 20 74 68 65 20 75 6e 72 65 61 o that the unrea
4280: 63 68 61 62 6c 65 20 63 6f 64 65 20 74 68 65 6e chable code then
4290: 20 73 70 65 63 69 66 79 20 77 69 6c 6c 0a 2a 2a specify will.**
42a0: 20 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20 not be counted
42b0: 61 73 20 75 6e 74 65 73 74 65 64 20 63 6f 64 65 as untested code
42c0: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
42d0: 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 (SQLITE_COVERAGE
42e0: 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20 _TEST).# define
42f0: 41 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 ALWAYS(X) (
4300: 31 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45 1).# define NEVE
4310: 52 28 58 29 20 20 20 20 20 20 20 28 30 29 0a 23 R(X) (0).#
4320: 65 6c 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 elif !defined(ND
4330: 45 42 55 47 29 0a 23 20 64 65 66 69 6e 65 20 41 EBUG).# define A
4340: 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 28 LWAYS(X) ((
4350: 58 29 3f 31 3a 28 61 73 73 65 72 74 28 30 29 2c X)?1:(assert(0),
4360: 30 29 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 0)).# define NEV
4370: 45 52 28 58 29 20 20 20 20 20 20 20 28 28 58 29 ER(X) ((X)
4380: 3f 28 61 73 73 65 72 74 28 30 29 2c 31 29 3a 30 ?(assert(0),1):0
4390: 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
43a0: 20 41 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 ALWAYS(X)
43b0: 28 58 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 (X).# define NEV
43c0: 45 52 28 58 29 20 20 20 20 20 20 20 28 58 29 0a ER(X) (X).
43d0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
43e0: 65 20 6d 61 63 72 6f 20 75 6e 6c 69 6b 65 6c 79 e macro unlikely
43f0: 28 29 20 69 73 20 61 20 68 69 6e 74 20 74 68 61 () is a hint tha
4400: 74 20 73 75 72 72 6f 75 6e 64 73 20 61 20 62 6f t surrounds a bo
4410: 6f 6c 65 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 olean.** express
4420: 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75 61 ion that is usua
4430: 6c 6c 79 20 66 61 6c 73 65 2e 20 20 4d 61 63 72 lly false. Macr
4440: 6f 20 6c 69 6b 65 6c 79 28 29 20 73 75 72 72 6f o likely() surro
4450: 75 6e 64 73 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61 unds.** a boolea
4460: 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 n expression tha
4470: 74 20 69 73 20 75 73 75 61 6c 6c 79 20 74 72 75 t is usually tru
4480: 65 2e 20 20 47 43 43 20 69 73 20 61 62 6c 65 20 e. GCC is able
4490: 74 6f 0a 2a 2a 20 75 73 65 20 74 68 65 73 65 20 to.** use these
44a0: 68 69 6e 74 73 20 74 6f 20 67 65 6e 65 72 61 74 hints to generat
44b0: 65 20 62 65 74 74 65 72 20 63 6f 64 65 2c 20 73 e better code, s
44c0: 6f 6d 65 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66 ometimes..*/.#if
44d0: 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f defined(__GNUC_
44e0: 5f 29 20 26 26 20 30 0a 23 20 64 65 66 69 6e 65 _) && 0.# define
44f0: 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20 5f 5f likely(X) __
4500: 62 75 69 6c 74 69 6e 5f 65 78 70 65 63 74 28 28 builtin_expect((
4510: 58 29 2c 31 29 0a 23 20 64 65 66 69 6e 65 20 75 X),1).# define u
4520: 6e 6c 69 6b 65 6c 79 28 58 29 20 20 5f 5f 62 75 nlikely(X) __bu
4530: 69 6c 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29 iltin_expect((X)
4540: 2c 30 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 ,0).#else.# defi
4550: 6e 65 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20 ne likely(X)
4560: 21 21 28 58 29 0a 23 20 64 65 66 69 6e 65 20 75 !!(X).# define u
4570: 6e 6c 69 6b 65 6c 79 28 58 29 20 20 21 21 28 58 nlikely(X) !!(X
4580: 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a ).#endif../*****
4590: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
45a0: 65 20 73 71 6c 69 74 65 33 2e 68 20 69 6e 20 74 e sqlite3.h in t
45b0: 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
45c0: 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
45d0: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
45e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
45f0: 66 69 6c 65 20 73 71 6c 69 74 65 33 2e 68 20 2a file sqlite3.h *
4600: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4610: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4620: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
4630: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
4640: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
4650: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
4660: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
4670: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
4680: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
4690: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
46a0: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
46b0: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
46c0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
46d0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
46e0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
46f0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
4700: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
4710: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
4720: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
4730: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
4740: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
4750: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
4760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4770: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4780: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4790: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47a0: 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 .** This header
47b0: 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 file defines the
47c0: 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 interface that
47d0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 the SQLite libra
47e0: 72 79 0a 2a 2a 20 70 72 65 73 65 6e 74 73 20 74 ry.** presents t
47f0: 6f 20 63 6c 69 65 6e 74 20 70 72 6f 67 72 61 6d o client program
4800: 73 2e 20 20 49 66 20 61 20 43 2d 66 75 6e 63 74 s. If a C-funct
4810: 69 6f 6e 2c 20 73 74 72 75 63 74 75 72 65 2c 20 ion, structure,
4820: 64 61 74 61 74 79 70 65 2c 0a 2a 2a 20 6f 72 20 datatype,.** or
4830: 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 69 74 constant definit
4840: 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 ion does not app
4850: 65 61 72 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ear in this file
4860: 2c 20 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 , then it is.**
4870: 6e 6f 74 20 61 20 70 75 62 6c 69 73 68 65 64 20 not a published
4880: 41 50 49 20 6f 66 20 53 51 4c 69 74 65 2c 20 69 API of SQLite, i
4890: 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 s subject to cha
48a0: 6e 67 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6e nge without.** n
48b0: 6f 74 69 63 65 2c 20 61 6e 64 20 73 68 6f 75 6c otice, and shoul
48c0: 64 20 6e 6f 74 20 62 65 20 72 65 66 65 72 65 6e d not be referen
48d0: 63 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20 ced by programs
48e0: 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 2e that use SQLite.
48f0: 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 .**.** Some of t
4900: 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 74 he definitions t
4910: 68 61 74 20 61 72 65 20 69 6e 20 74 68 69 73 20 hat are in this
4920: 66 69 6c 65 20 61 72 65 20 6d 61 72 6b 65 64 20 file are marked
4930: 61 73 0a 2a 2a 20 22 65 78 70 65 72 69 6d 65 6e as.** "experimen
4940: 74 61 6c 22 2e 20 20 45 78 70 65 72 69 6d 65 6e tal". Experimen
4950: 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 tal interfaces a
4960: 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a re normally new.
4970: 2a 2a 20 66 65 61 74 75 72 65 73 20 72 65 63 65 ** features rece
4980: 6e 74 6c 79 20 61 64 64 65 64 20 74 6f 20 53 51 ntly added to SQ
4990: 4c 69 74 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 Lite. We do not
49a0: 20 61 6e 74 69 63 69 70 61 74 65 20 63 68 61 6e anticipate chan
49b0: 67 65 73 0a 2a 2a 20 74 6f 20 65 78 70 65 72 69 ges.** to experi
49c0: 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65 mental interface
49d0: 73 20 62 75 74 20 72 65 73 65 72 76 65 20 74 68 s but reserve th
49e0: 65 20 72 69 67 68 74 20 74 6f 20 6d 61 6b 65 20 e right to make
49f0: 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 0a 2a 2a minor changes.**
4a00: 20 69 66 20 65 78 70 65 72 69 65 6e 63 65 20 66 if experience f
4a10: 72 6f 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20 rom use "in the
4a20: 77 69 6c 64 22 20 73 75 67 67 65 73 74 20 73 75 wild" suggest su
4a30: 63 68 20 63 68 61 6e 67 65 73 20 61 72 65 20 70 ch changes are p
4a40: 72 75 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 rudent..**.** Th
4a50: 65 20 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e e official C-lan
4a60: 67 75 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65 guage API docume
4a70: 6e 74 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69 ntation for SQLi
4a80: 74 65 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a te is derived.**
4a90: 20 66 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 from comments i
4aa0: 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68 n this file. Th
4ab0: 69 73 20 66 69 6c 65 20 69 73 20 74 68 65 20 61 is file is the a
4ac0: 75 74 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75 uthoritative sou
4ad0: 72 63 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 rce.** on how SQ
4ae0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 Lite interfaces
4af0: 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f are suppose to o
4b00: 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 perate..**.** Th
4b10: 65 20 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 e name of this f
4b20: 69 6c 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67 ile under config
4b30: 75 72 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 uration manageme
4b40: 6e 74 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e nt is "sqlite.h.
4b50: 69 6e 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 in"..** The make
4b60: 66 69 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20 file makes some
4b70: 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f minor changes to
4b80: 20 74 68 69 73 20 66 69 6c 65 20 28 73 75 63 68 this file (such
4b90: 20 61 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a as inserting.**
4ba0: 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d the version num
4bb0: 62 65 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73 ber) and changes
4bc0: 20 69 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71 its name to "sq
4bd0: 6c 69 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70 lite3.h" as.** p
4be0: 61 72 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64 art of the build
4bf0: 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 23 69 66 process..*/.#if
4c00: 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 5f 48 5f ndef _SQLITE3_H_
4c10: 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 .#define _SQLITE
4c20: 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 3_H_.#include <s
4c30: 74 64 61 72 67 2e 68 3e 20 20 20 20 20 2f 2a 20 tdarg.h> /*
4c40: 4e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64 Needed for the d
4c50: 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 76 61 5f efinition of va_
4c60: 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d list */../*.** M
4c70: 61 6b 65 20 73 75 72 65 20 77 65 20 63 61 6e 20 ake sure we can
4c80: 63 61 6c 6c 20 74 68 69 73 20 73 74 75 66 66 20 call this stuff
4c90: 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 66 from C++..*/.#if
4ca0: 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 0.extern "C" {.
4cb0: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 #endif.../*.** A
4cc0: 64 64 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 dd the ability t
4cd0: 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78 74 65 o override 'exte
4ce0: 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 rn'.*/.#ifndef S
4cf0: 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a 23 20 64 QLITE_EXTERN.# d
4d00: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 efine SQLITE_EXT
4d10: 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 6e 64 69 ERN extern.#endi
4d20: 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
4d30: 45 5f 41 50 49 0a 23 20 64 65 66 69 6e 65 20 53 E_API.# define S
4d40: 51 4c 49 54 45 5f 41 50 49 0a 23 65 6e 64 69 66 QLITE_API.#endif
4d50: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6e .../*.** These n
4d60: 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 72 65 20 o-op macros are
4d70: 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f 66 used in front of
4d80: 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f 20 6d interfaces to m
4d90: 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 6e 74 ark those.** int
4da0: 65 72 66 61 63 65 73 20 61 73 20 65 69 74 68 65 erfaces as eithe
4db0: 72 20 64 65 70 72 65 63 61 74 65 64 20 6f 72 20 r deprecated or
4dc0: 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 4e experimental. N
4dd0: 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a ew applications.
4de0: 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 ** should not us
4df0: 65 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74 e deprecated int
4e00: 65 72 66 61 63 65 73 20 2d 20 74 68 65 79 20 61 erfaces - they a
4e10: 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62 re support for b
4e20: 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 ackwards.** comp
4e30: 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 20 atibility only.
4e40: 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 69 Application wri
4e50: 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 61 ters should be a
4e60: 77 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 70 ware that.** exp
4e70: 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 erimental interf
4e80: 61 63 65 73 20 61 72 65 20 73 75 62 6a 65 63 74 aces are subject
4e90: 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 6f to change in po
4ea0: 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a int releases..**
4eb0: 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 .** These macros
4ec0: 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 used to resolve
4ed0: 20 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e 64 to various kind
4ee0: 73 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d 61 s of compiler ma
4ef0: 67 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 6c gic that.** woul
4f00: 64 20 67 65 6e 65 72 61 74 65 20 77 61 72 6e 69 d generate warni
4f10: 6e 67 20 6d 65 73 73 61 67 65 73 20 77 68 65 6e ng messages when
4f20: 20 74 68 65 79 20 77 65 72 65 20 75 73 65 64 2e they were used.
4f30: 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 6f But that.** co
4f40: 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 65 6e 64 mpiler magic end
4f50: 65 64 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67 ed up generating
4f60: 20 73 75 63 68 20 61 20 66 6c 75 72 72 79 20 6f such a flurry o
4f70: 66 20 62 75 67 20 72 65 70 6f 72 74 73 0a 2a 2a f bug reports.**
4f80: 20 74 68 61 74 20 77 65 20 68 61 76 65 20 74 61 that we have ta
4f90: 6b 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 61 ken it all out a
4fa0: 6e 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f 20 nd gone back to
4fb0: 75 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a 20 using simple.**
4fc0: 6e 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a noop macros..*/.
4fd0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
4fe0: 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e EPRECATED.#defin
4ff0: 65 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d e SQLITE_EXPERIM
5000: 45 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73 ENTAL../*.** Ens
5010: 75 72 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c ure these symbol
5020: 73 20 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e s were not defin
5030: 65 64 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69 ed by some previ
5040: 6f 75 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e ous header file.
5050: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
5060: 45 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 65 E_VERSION.# unde
5070: 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e f SQLITE_VERSION
5080: 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
5090: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 QLITE_VERSION_NU
50a0: 4d 42 45 52 0a 23 20 75 6e 64 65 66 20 53 51 4c MBER.# undef SQL
50b0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 ITE_VERSION_NUMB
50c0: 45 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a ER.#endif../*.**
50d0: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 CAPI3REF: Compi
50e0: 6c 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 le-Time Library
50f0: 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 Version Numbers
5100: 7b 48 31 30 30 31 30 7d 20 3c 53 36 30 31 30 30 {H10010} <S60100
5110: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 >.**.** The SQLI
5120: 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 53 TE_VERSION and S
5130: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 QLITE_VERSION_NU
5140: 4d 42 45 52 20 23 64 65 66 69 6e 65 73 20 69 6e MBER #defines in
5150: 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 2e .** the sqlite3.
5160: 68 20 66 69 6c 65 20 73 70 65 63 69 66 79 20 74 h file specify t
5170: 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 he version of SQ
5180: 4c 69 74 65 20 77 69 74 68 20 77 68 69 63 68 0a Lite with which.
5190: 2a 2a 20 74 68 61 74 20 68 65 61 64 65 72 20 66 ** that header f
51a0: 69 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 ile is associate
51b0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 65 d..**.** The "ve
51c0: 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 65 rsion" of SQLite
51d0: 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 66 20 is a string of
51e0: 74 68 65 20 66 6f 72 6d 20 22 57 2e 58 2e 59 22 the form "W.X.Y"
51f0: 20 6f 72 20 22 57 2e 58 2e 59 2e 5a 22 2e 0a 2a or "W.X.Y.Z"..*
5200: 2a 20 54 68 65 20 57 20 76 61 6c 75 65 20 69 73 * The W value is
5210: 20 6d 61 6a 6f 72 20 76 65 72 73 69 6f 6e 20 6e major version n
5220: 75 6d 62 65 72 20 61 6e 64 20 69 73 20 61 6c 77 umber and is alw
5230: 61 79 73 20 33 20 69 6e 20 53 51 4c 69 74 65 33 ays 3 in SQLite3
5240: 2e 0a 2a 2a 20 54 68 65 20 57 20 76 61 6c 75 65 ..** The W value
5250: 20 6f 6e 6c 79 20 63 68 61 6e 67 65 73 20 77 68 only changes wh
5260: 65 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d en backwards com
5270: 70 61 74 69 62 69 6c 69 74 79 20 69 73 0a 2a 2a patibility is.**
5280: 20 62 72 6f 6b 65 6e 20 61 6e 64 20 77 65 20 69 broken and we i
5290: 6e 74 65 6e 64 20 74 6f 20 6e 65 76 65 72 20 62 ntend to never b
52a0: 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20 63 reak backwards c
52b0: 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a ompatibility..**
52c0: 20 54 68 65 20 58 20 76 61 6c 75 65 20 69 73 20 The X value is
52d0: 74 68 65 20 6d 69 6e 6f 72 20 76 65 72 73 69 6f the minor versio
52e0: 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 6f 6e 6c n number and onl
52f0: 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 0a 2a y changes when.*
5300: 2a 20 74 68 65 72 65 20 61 72 65 20 6d 61 6a 6f * there are majo
5310: 72 20 66 65 61 74 75 72 65 20 65 6e 68 61 6e 63 r feature enhanc
5320: 65 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 20 ements that are
5330: 66 6f 72 77 61 72 64 73 20 63 6f 6d 70 61 74 69 forwards compati
5340: 62 6c 65 0a 2a 2a 20 62 75 74 20 6e 6f 74 20 62 ble.** but not b
5350: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 ackwards compati
5360: 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 59 20 76 61 ble..** The Y va
5370: 6c 75 65 20 69 73 20 74 68 65 20 72 65 6c 65 61 lue is the relea
5380: 73 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 69 73 se number and is
5390: 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74 incremented wit
53a0: 68 0a 2a 2a 20 65 61 63 68 20 72 65 6c 65 61 73 h.** each releas
53b0: 65 20 62 75 74 20 72 65 73 65 74 73 20 62 61 63 e but resets bac
53c0: 6b 20 74 6f 20 30 20 77 68 65 6e 65 76 65 72 20 k to 0 whenever
53d0: 58 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 X is incremented
53e0: 2e 0a 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 ..** The Z value
53f0: 20 6f 6e 6c 79 20 61 70 70 65 61 72 73 20 6f 6e only appears on
5400: 20 62 72 61 6e 63 68 20 72 65 6c 65 61 73 65 73 branch releases
5410: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
5420: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 TE_VERSION_NUMBE
5430: 52 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 R is an integer
5440: 74 68 61 74 20 69 73 20 63 6f 6d 70 75 74 65 64 that is computed
5450: 20 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a as.** follows:.
5460: 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
5470: 65 3e 3c 70 72 65 3e 0a 2a 2a 20 53 51 4c 49 54 e><pre>.** SQLIT
5480: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 E_VERSION_NUMBER
5490: 20 3d 20 57 2a 31 30 30 30 30 30 30 20 2b 20 58 = W*1000000 + X
54a0: 2a 31 30 30 30 20 2b 20 59 0a 2a 2a 20 3c 2f 70 *1000 + Y.** </p
54b0: 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
54c0: 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 .**.** Since ver
54d0: 73 69 6f 6e 20 33 2e 36 2e 31 38 2c 20 53 51 4c sion 3.6.18, SQL
54e0: 69 74 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 ite source code
54f0: 68 61 73 20 62 65 65 6e 20 73 74 6f 72 65 64 20 has been stored
5500: 69 6e 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 in the.** <a hre
5510: 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 66 6f f="http://www.fo
5520: 73 73 69 6c 2d 73 63 6d 2e 6f 72 67 2f 22 3e 66 ssil-scm.org/">f
5530: 6f 73 73 69 6c 20 63 6f 6e 66 69 67 75 72 61 74 ossil configurat
5540: 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a ion management.*
5550: 2a 20 73 79 73 74 65 6d 3c 2f 61 3e 2e 20 20 54 * system</a>. T
5560: 68 65 20 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 he SQLITE_SOURCE
5570: 5f 49 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 _ID.** macro is
5580: 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 a string which i
5590: 64 65 6e 74 69 66 69 65 73 20 61 20 70 61 72 74 dentifies a part
55a0: 69 63 75 6c 61 72 20 63 68 65 63 6b 2d 69 6e 20 icular check-in
55b0: 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 74 of SQLite.** wit
55c0: 68 69 6e 20 69 74 73 20 63 6f 6e 66 69 67 75 72 hin its configur
55d0: 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 ation management
55e0: 20 73 79 73 74 65 6d 2e 20 20 54 68 65 20 73 74 system. The st
55f0: 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 74 68 ring contains th
5600: 65 0a 2a 2a 20 64 61 74 65 20 61 6e 64 20 74 69 e.** date and ti
5610: 6d 65 20 6f 66 20 74 68 65 20 63 68 65 63 6b 2d me of the check-
5620: 69 6e 20 28 55 54 43 29 20 61 6e 64 20 61 6e 20 in (UTC) and an
5630: 53 48 41 31 20 68 61 73 68 20 6f 66 20 74 68 65 SHA1 hash of the
5640: 20 65 6e 74 69 72 65 0a 2a 2a 20 73 6f 75 72 63 entire.** sourc
5650: 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 e tree..**.** Se
5660: 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
5670: 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 5d 2c 0a _libversion()],.
5680: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 ** [sqlite3_libv
5690: 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d ersion_number()]
56a0: 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 , [sqlite3_sourc
56b0: 65 69 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 eid()],.** [sqli
56c0: 74 65 5f 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e te_version()] an
56d0: 64 20 5b 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 d [sqlite_source
56e0: 5f 69 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 _id()]..**.** Re
56f0: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 quirements: [H10
5700: 30 31 31 5d 20 5b 48 31 30 30 31 34 5d 0a 2a 2f 011] [H10014].*/
5710: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5720: 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20 22 VERSION "
5730: 33 2e 36 2e 31 38 22 0a 23 64 65 66 69 6e 65 20 3.6.18".#define
5740: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e SQLITE_VERSION_N
5750: 55 4d 42 45 52 20 33 30 30 36 30 31 38 0a 23 64 UMBER 3006018.#d
5760: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 55 efine SQLITE_SOU
5770: 52 43 45 5f 49 44 20 20 20 20 20 20 22 32 30 30 RCE_ID "200
5780: 39 2d 30 39 2d 31 31 20 31 34 3a 30 35 3a 30 37 9-09-11 14:05:07
5790: 20 62 30 38 34 38 32 38 61 37 37 31 65 63 34 30 b084828a771ec40
57a0: 62 65 38 35 66 30 37 63 35 39 30 63 61 39 39 64 be85f07c590ca99d
57b0: 65 34 66 36 63 32 34 65 65 22 0a 0a 2f 2a 0a 2a e4f6c24ee"../*.*
57c0: 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d * CAPI3REF: Run-
57d0: 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72 Time Library Ver
57e0: 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 sion Numbers {H1
57f0: 30 30 32 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 0020} <S60100>.*
5800: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 * KEYWORDS: sqli
5810: 74 65 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a te3_version.**.*
5820: 2a 20 54 68 65 73 65 20 69 6e 74 65 72 66 61 63 * These interfac
5830: 65 73 20 70 72 6f 76 69 64 65 20 74 68 65 20 73 es provide the s
5840: 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ame information
5850: 61 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 56 as the [SQLITE_V
5860: 45 52 53 49 4f 4e 5d 2c 0a 2a 2a 20 5b 53 51 4c ERSION],.** [SQL
5870: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 ITE_VERSION_NUMB
5880: 45 52 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 54 45 ER], and [SQLITE
5890: 5f 53 4f 55 52 43 45 5f 49 44 5d 20 23 64 65 66 _SOURCE_ID] #def
58a0: 69 6e 65 73 20 69 6e 20 74 68 65 20 68 65 61 64 ines in the head
58b0: 65 72 2c 0a 2a 2a 20 62 75 74 20 61 72 65 20 61 er,.** but are a
58c0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
58d0: 68 65 20 6c 69 62 72 61 72 79 20 69 6e 73 74 65 he library inste
58e0: 61 64 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 ad of the header
58f0: 20 66 69 6c 65 2e 20 20 43 61 75 74 69 6f 75 73 file. Cautious
5900: 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 65 72 73 20 .** programmers
5910: 6d 69 67 68 74 20 69 6e 63 6c 75 64 65 20 61 73 might include as
5920: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
5930: 73 20 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69 s in their appli
5940: 63 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 76 65 72 cation to.** ver
5950: 69 66 79 20 74 68 61 74 20 76 61 6c 75 65 73 20 ify that values
5960: 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 returned by thes
5970: 65 20 69 6e 74 65 72 66 61 63 65 73 20 6d 61 74 e interfaces mat
5980: 63 68 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e ch the macros in
5990: 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20 .** the header,
59a0: 61 6e 64 20 74 68 75 73 20 69 6e 73 75 72 65 20 and thus insure
59b0: 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 61 that the applica
59c0: 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d 70 69 tion is.** compi
59d0: 6c 65 64 20 77 69 74 68 20 6d 61 74 63 68 69 6e led with matchin
59e0: 67 20 6c 69 62 72 61 72 79 20 61 6e 64 20 68 65 g library and he
59f0: 61 64 65 72 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a ader files..**.*
5a00: 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
5a10: 72 65 3e 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 re>.** assert( s
5a20: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f qlite3_libversio
5a30: 6e 5f 6e 75 6d 62 65 72 28 29 3d 3d 53 51 4c 49 n_number()==SQLI
5a40: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 TE_VERSION_NUMBE
5a50: 52 20 29 3b 0a 2a 2a 20 61 73 73 65 72 74 28 20 R );.** assert(
5a60: 73 74 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 73 strcmp(sqlite3_s
5a70: 6f 75 72 63 65 69 64 28 29 2c 53 51 4c 49 54 45 ourceid(),SQLITE
5a80: 5f 53 4f 55 52 43 45 5f 49 44 29 3d 3d 30 20 29 _SOURCE_ID)==0 )
5a90: 3b 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 74 72 ;.** assert( str
5aa0: 63 6d 70 28 73 71 6c 69 74 65 33 5f 6c 69 62 76 cmp(sqlite3_libv
5ab0: 65 72 73 69 6f 6e 2c 53 51 4c 49 54 45 5f 56 45 ersion,SQLITE_VE
5ac0: 52 53 49 4f 4e 29 3d 3d 30 20 29 3b 0a 2a 2a 20 RSION)==0 );.**
5ad0: 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f </pre></blockquo
5ae0: 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 te>.**.** The sq
5af0: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e lite3_libversion
5b00: 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 () function retu
5b10: 72 6e 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 rns the same inf
5b20: 6f 72 6d 61 74 69 6f 6e 20 61 73 20 69 73 0a 2a ormation as is.*
5b30: 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 * in the sqlite3
5b40: 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69 6e _version[] strin
5b50: 67 20 63 6f 6e 73 74 61 6e 74 2e 20 20 54 68 65 g constant. The
5b60: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 72 6f function is pro
5b70: 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20 75 73 65 vided.** for use
5b80: 20 69 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20 44 in DLLs since D
5b90: 4c 4c 20 75 73 65 72 73 20 75 73 75 61 6c 6c 79 LL users usually
5ba0: 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 64 69 72 do not have dir
5bb0: 65 63 74 20 61 63 63 65 73 73 20 74 6f 20 73 74 ect access to st
5bc0: 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74 ring.** constant
5bd0: 73 20 77 69 74 68 69 6e 20 74 68 65 20 44 4c 4c s within the DLL
5be0: 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 74 68 . Similarly, th
5bf0: 65 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 e sqlite3_source
5c00: 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a id() function.**
5c10: 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d returns the sam
5c20: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 e information as
5c30: 20 69 73 20 69 6e 20 74 68 65 20 5b 53 51 4c 49 is in the [SQLI
5c40: 54 45 5f 53 4f 55 52 43 45 5f 49 44 5d 20 23 64 TE_SOURCE_ID] #d
5c50: 65 66 69 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 efine of.** the
5c60: 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a header file..**.
5c70: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
5c80: 6c 69 74 65 5f 76 65 72 73 69 6f 6e 28 29 5d 20 lite_version()]
5c90: 61 6e 64 20 5b 73 71 6c 69 74 65 5f 73 6f 75 72 and [sqlite_sour
5ca0: 63 65 5f 69 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 ce_id()]..**.**
5cb0: 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 Requirements: [H
5cc0: 31 30 30 32 31 5d 20 5b 48 31 30 30 32 32 5d 20 10021] [H10022]
5cd0: 5b 48 31 30 30 32 33 5d 0a 2a 2f 0a 53 51 4c 49 [H10023].*/.SQLI
5ce0: 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 TE_API const cha
5cf0: 72 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f r sqlite3_versio
5d00: 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56 45 52 n[] = SQLITE_VER
5d10: 53 49 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41 50 49 SION;.SQLITE_API
5d20: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
5d30: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 ite3_libversion(
5d40: 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 void);.SQLITE_AP
5d50: 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
5d60: 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 76 lite3_sourceid(v
5d70: 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 oid);.SQLITE_API
5d80: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 int sqlite3_lib
5d90: 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 version_number(v
5da0: 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 oid);../*.** CAP
5db0: 49 33 52 45 46 3a 20 54 65 73 74 20 54 6f 20 53 I3REF: Test To S
5dc0: 65 65 20 49 66 20 54 68 65 20 4c 69 62 72 61 72 ee If The Librar
5dd0: 79 20 49 73 20 54 68 72 65 61 64 73 61 66 65 20 y Is Threadsafe
5de0: 7b 48 31 30 31 30 30 7d 20 3c 53 36 30 31 30 30 {H10100} <S60100
5df0: 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 >.**.** SQLite c
5e00: 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 an be compiled w
5e10: 69 74 68 20 6f 72 20 77 69 74 68 6f 75 74 20 6d ith or without m
5e20: 75 74 65 78 65 73 2e 20 20 57 68 65 6e 0a 2a 2a utexes. When.**
5e30: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52 the [SQLITE_THR
5e40: 45 41 44 53 41 46 45 5d 20 43 20 70 72 65 70 72 EADSAFE] C prepr
5e50: 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 69 73 ocessor macro is
5e60: 20 31 20 6f 72 20 32 2c 20 6d 75 74 65 78 65 73 1 or 2, mutexes
5e70: 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 .** are enabled
5e80: 61 6e 64 20 53 51 4c 69 74 65 20 69 73 20 74 68 and SQLite is th
5e90: 72 65 61 64 73 61 66 65 2e 20 20 57 68 65 6e 20 readsafe. When
5ea0: 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 the.** [SQLITE_T
5eb0: 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f HREADSAFE] macro
5ec0: 20 69 73 20 30 2c 20 0a 2a 2a 20 74 68 65 20 6d is 0, .** the m
5ed0: 75 74 65 78 65 73 20 61 72 65 20 6f 6d 69 74 74 utexes are omitt
5ee0: 65 64 2e 20 20 57 69 74 68 6f 75 74 20 74 68 65 ed. Without the
5ef0: 20 6d 75 74 65 78 65 73 2c 20 69 74 20 69 73 20 mutexes, it is
5f00: 6e 6f 74 20 73 61 66 65 0a 2a 2a 20 74 6f 20 75 not safe.** to u
5f10: 73 65 20 53 51 4c 69 74 65 20 63 6f 6e 63 75 72 se SQLite concur
5f20: 72 65 6e 74 6c 79 20 66 72 6f 6d 20 6d 6f 72 65 rently from more
5f30: 20 74 68 61 6e 20 6f 6e 65 20 74 68 72 65 61 64 than one thread
5f40: 2e 0a 2a 2a 0a 2a 2a 20 45 6e 61 62 6c 69 6e 67 ..**.** Enabling
5f50: 20 6d 75 74 65 78 65 73 20 69 6e 63 75 72 73 20 mutexes incurs
5f60: 61 20 6d 65 61 73 75 72 61 62 6c 65 20 70 65 72 a measurable per
5f70: 66 6f 72 6d 61 6e 63 65 20 70 65 6e 61 6c 74 79 formance penalty
5f80: 2e 0a 2a 2a 20 53 6f 20 69 66 20 73 70 65 65 64 ..** So if speed
5f90: 20 69 73 20 6f 66 20 75 74 6d 6f 73 74 20 69 6d is of utmost im
5fa0: 70 6f 72 74 61 6e 63 65 2c 20 69 74 20 6d 61 6b portance, it mak
5fb0: 65 73 20 73 65 6e 73 65 20 74 6f 20 64 69 73 61 es sense to disa
5fc0: 62 6c 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 ble.** the mutex
5fd0: 65 73 2e 20 20 42 75 74 20 66 6f 72 20 6d 61 78 es. But for max
5fe0: 69 6d 75 6d 20 73 61 66 65 74 79 2c 20 6d 75 74 imum safety, mut
5ff0: 65 78 65 73 20 73 68 6f 75 6c 64 20 62 65 20 65 exes should be e
6000: 6e 61 62 6c 65 64 2e 0a 2a 2a 20 54 68 65 20 64 nabled..** The d
6010: 65 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20 efault behavior
6020: 69 73 20 66 6f 72 20 6d 75 74 65 78 65 73 20 74 is for mutexes t
6030: 6f 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a o be enabled..**
6040: 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 .** This interfa
6050: 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 62 ce can be used b
6060: 79 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e y an application
6070: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
6080: 61 74 20 74 68 65 0a 2a 2a 20 76 65 72 73 69 6f at the.** versio
6090: 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68 61 74 n of SQLite that
60a0: 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67 20 61 it is linking a
60b0: 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d 70 69 gainst was compi
60c0: 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 led with.** the
60d0: 64 65 73 69 72 65 64 20 73 65 74 74 69 6e 67 20 desired setting
60e0: 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 of the [SQLITE_T
60f0: 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f HREADSAFE] macro
6100: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 ..**.** This int
6110: 65 72 66 61 63 65 20 6f 6e 6c 79 20 72 65 70 6f erface only repo
6120: 72 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 rts on the compi
6130: 6c 65 2d 74 69 6d 65 20 6d 75 74 65 78 20 73 65 le-time mutex se
6140: 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 tting.** of the
6150: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
6160: 46 45 5d 20 66 6c 61 67 2e 20 20 49 66 20 53 51 FE] flag. If SQ
6170: 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
6180: 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f with.** SQLITE_
6190: 54 48 52 45 41 44 53 41 46 45 3d 31 20 74 68 65 THREADSAFE=1 the
61a0: 6e 20 6d 75 74 65 78 65 73 20 61 72 65 20 65 6e n mutexes are en
61b0: 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
61c0: 20 62 75 74 0a 2a 2a 20 63 61 6e 20 62 65 20 66 but.** can be f
61d0: 75 6c 6c 79 20 6f 72 20 70 61 72 74 69 61 6c 6c ully or partiall
61e0: 79 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 y disabled using
61f0: 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 a call to [sqli
6200: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 0a 2a 2a te3_config()].**
6210: 20 77 69 74 68 20 74 68 65 20 76 65 72 62 73 20 with the verbs
6220: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
6230: 49 4e 47 4c 45 54 48 52 45 41 44 5d 2c 20 5b 53 INGLETHREAD], [S
6240: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c QLITE_CONFIG_MUL
6250: 54 49 54 48 52 45 41 44 5d 2c 0a 2a 2a 20 6f 72 TITHREAD],.** or
6260: 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
6270: 4d 55 54 45 58 5d 2e 20 20 54 68 65 20 72 65 74 MUTEX]. The ret
6280: 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 urn value of thi
6290: 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 77 73 s function shows
62a0: 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 20 64 65 66 .** only the def
62b0: 61 75 6c 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d ault compile-tim
62c0: 65 20 73 65 74 74 69 6e 67 2c 20 6e 6f 74 20 61 e setting, not a
62d0: 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63 68 61 6e ny run-time chan
62e0: 67 65 73 0a 2a 2a 20 74 6f 20 74 68 61 74 20 73 ges.** to that s
62f0: 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 65 etting..**.** Se
6300: 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 e the [threading
6310: 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 61 mode] documenta
6320: 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f tion for additio
6330: 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
6340: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
6350: 6e 74 73 3a 20 5b 48 31 30 31 30 31 5d 20 5b 48 nts: [H10101] [H
6360: 31 30 31 30 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 10102].*/.SQLITE
6370: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
6380: 5f 74 68 72 65 61 64 73 61 66 65 28 76 6f 69 64 _threadsafe(void
6390: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
63a0: 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e EF: Database Con
63b0: 6e 65 63 74 69 6f 6e 20 48 61 6e 64 6c 65 20 7b nection Handle {
63c0: 48 31 32 30 30 30 7d 20 3c 53 34 30 32 30 30 3e H12000} <S40200>
63d0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 64 .** KEYWORDS: {d
63e0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
63f0: 6f 6e 7d 20 7b 64 61 74 61 62 61 73 65 20 63 6f on} {database co
6400: 6e 6e 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a nnections}.**.**
6410: 20 45 61 63 68 20 6f 70 65 6e 20 53 51 4c 69 74 Each open SQLit
6420: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 e database is re
6430: 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 20 70 presented by a p
6440: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 ointer to an ins
6450: 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 tance of.** the
6460: 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 opaque structure
6470: 20 6e 61 6d 65 64 20 22 73 71 6c 69 74 65 33 22 named "sqlite3"
6480: 2e 20 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 . It is useful
6490: 74 6f 20 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73 to think of an s
64a0: 71 6c 69 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65 qlite3.** pointe
64b0: 72 20 61 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20 r as an object.
64c0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 The [sqlite3_op
64d0: 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f en()], [sqlite3_
64e0: 6f 70 65 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a open16()], and.*
64f0: 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f * [sqlite3_open_
6500: 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 v2()] interfaces
6510: 20 61 72 65 20 69 74 73 20 63 6f 6e 73 74 72 75 are its constru
6520: 63 74 6f 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69 ctors, and [sqli
6530: 74 65 33 5f 63 6c 6f 73 65 28 29 5d 0a 2a 2a 20 te3_close()].**
6540: 69 73 20 69 74 73 20 64 65 73 74 72 75 63 74 6f is its destructo
6550: 72 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 r. There are ma
6560: 6e 79 20 6f 74 68 65 72 20 69 6e 74 65 72 66 61 ny other interfa
6570: 63 65 73 20 28 73 75 63 68 20 61 73 0a 2a 2a 20 ces (such as.**
6580: 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
6590: 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 _v2()], [sqlite3
65a0: 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
65b0: 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c ()], and.** [sql
65c0: 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 ite3_busy_timeou
65d0: 74 28 29 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74 t()] to name but
65e0: 20 74 68 72 65 65 29 20 74 68 61 74 20 61 72 65 three) that are
65f0: 20 6d 65 74 68 6f 64 73 20 6f 6e 20 61 6e 0a 2a methods on an.*
6600: 2a 20 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 * sqlite3 object
6610: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
6620: 75 63 74 20 73 71 6c 69 74 65 33 20 73 71 6c 69 uct sqlite3 sqli
6630: 74 65 33 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 te3;../*.** CAPI
6640: 33 52 45 46 3a 20 36 34 2d 42 69 74 20 49 6e 74 3REF: 64-Bit Int
6650: 65 67 65 72 20 54 79 70 65 73 20 7b 48 31 30 32 eger Types {H102
6660: 30 30 7d 20 3c 53 31 30 31 31 30 3e 0a 2a 2a 20 00} <S10110>.**
6670: 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 KEYWORDS: sqlite
6680: 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 69 _int64 sqlite_ui
6690: 6e 74 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 nt64.**.** Becau
66a0: 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 se there is no c
66b0: 72 6f 73 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61 ross-platform wa
66c0: 79 20 74 6f 20 73 70 65 63 69 66 79 20 36 34 2d y to specify 64-
66d0: 62 69 74 20 69 6e 74 65 67 65 72 20 74 79 70 65 bit integer type
66e0: 73 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c s.** SQLite incl
66f0: 75 64 65 73 20 74 79 70 65 64 65 66 73 20 66 6f udes typedefs fo
6700: 72 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 r 64-bit signed
6710: 61 6e 64 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 and unsigned int
6720: 65 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 egers..**.** The
6730: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 sqlite3_int64 a
6740: 6e 64 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 nd sqlite3_uint6
6750: 34 20 61 72 65 20 74 68 65 20 70 72 65 66 65 72 4 are the prefer
6760: 72 65 64 20 74 79 70 65 20 64 65 66 69 6e 69 74 red type definit
6770: 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c ions..** The sql
6780: 69 74 65 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 ite_int64 and sq
6790: 6c 69 74 65 5f 75 69 6e 74 36 34 20 74 79 70 65 lite_uint64 type
67a0: 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 20 s are supported
67b0: 66 6f 72 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a for backwards.**
67c0: 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 6f compatibility o
67d0: 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 nly..**.** Requi
67e0: 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 32 30 31 rements: [H10201
67f0: 5d 20 5b 48 31 30 32 30 32 5d 0a 2a 2f 0a 23 69 ] [H10202].*/.#i
6800: 66 64 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 36 fdef SQLITE_INT6
6810: 34 5f 54 59 50 45 0a 20 20 74 79 70 65 64 65 66 4_TYPE. typedef
6820: 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 SQLITE_INT64_TY
6830: 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b PE sqlite_int64;
6840: 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 . typedef unsig
6850: 6e 65 64 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 ned SQLITE_INT64
6860: 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 75 69 6e _TYPE sqlite_uin
6870: 74 36 34 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e t64;.#elif defin
6880: 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 7c 7c 20 ed(_MSC_VER) ||
6890: 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e defined(__BORLAN
68a0: 44 43 5f 5f 29 0a 20 20 74 79 70 65 64 65 66 20 DC__). typedef
68b0: 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 69 __int64 sqlite_i
68c0: 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 nt64;. typedef
68d0: 75 6e 73 69 67 6e 65 64 20 5f 5f 69 6e 74 36 34 unsigned __int64
68e0: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a sqlite_uint64;.
68f0: 23 65 6c 73 65 0a 20 20 74 79 70 65 64 65 66 20 #else. typedef
6900: 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 long long int sq
6910: 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 lite_int64;. ty
6920: 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 6c pedef unsigned l
6930: 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 6c ong long int sql
6940: 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6e 64 ite_uint64;.#end
6950: 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 if.typedef sqlit
6960: 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f e_int64 sqlite3_
6970: 69 6e 74 36 34 3b 0a 74 79 70 65 64 65 66 20 73 int64;.typedef s
6980: 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
6990: 69 74 65 33 5f 75 69 6e 74 36 34 3b 0a 0a 2f 2a ite3_uint64;../*
69a0: 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 .** If compiling
69b0: 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 for a processor
69c0: 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 that lacks floa
69d0: 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f ting point suppo
69e0: 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 rt,.** substitut
69f0: 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c e integer for fl
6a00: 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 2e 0a 2a 2f oating-point..*/
6a10: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
6a20: 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
6a30: 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 NT.# define doub
6a40: 6c 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 le sqlite3_int64
6a50: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
6a60: 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 69 6e 67 API3REF: Closing
6a70: 20 41 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e A Database Conn
6a80: 65 63 74 69 6f 6e 20 7b 48 31 32 30 31 30 7d 20 ection {H12010}
6a90: 3c 53 33 30 31 30 30 3e 3c 53 34 30 32 30 30 3e <S30100><S40200>
6aa0: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
6ab0: 69 6e 65 20 69 73 20 74 68 65 20 64 65 73 74 72 ine is the destr
6ac0: 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 5b 73 uctor for the [s
6ad0: 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2e 0a qlite3] object..
6ae0: 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f **.** Applicatio
6af0: 6e 73 20 73 68 6f 75 6c 64 20 5b 73 71 6c 69 74 ns should [sqlit
6b00: 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 e3_finalize | fi
6b10: 6e 61 6c 69 7a 65 5d 20 61 6c 6c 20 5b 70 72 65 nalize] all [pre
6b20: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
6b30: 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 ].** and [sqlite
6b40: 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 7c 20 63 3_blob_close | c
6b50: 6c 6f 73 65 5d 20 61 6c 6c 20 5b 42 4c 4f 42 20 lose] all [BLOB
6b60: 68 61 6e 64 6c 65 73 5d 20 61 73 73 6f 63 69 61 handles] associa
6b70: 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 ted with.** the
6b80: 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 [sqlite3] object
6b90: 20 70 72 69 6f 72 20 74 6f 20 61 74 74 65 6d 70 prior to attemp
6ba0: 74 69 6e 67 20 74 6f 20 63 6c 6f 73 65 20 74 68 ting to close th
6bb0: 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 65 e object..** The
6bc0: 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 [sqlite3_next_s
6bd0: 74 6d 74 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tmt()] interface
6be0: 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
6bf0: 6c 6f 63 61 74 65 20 61 6c 6c 0a 2a 2a 20 5b 70 locate all.** [p
6c00: 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
6c10: 74 73 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 ts] associated w
6c20: 69 74 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 ith a [database
6c30: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 66 20 64 connection] if d
6c40: 65 73 69 72 65 64 2e 0a 2a 2a 20 54 79 70 69 63 esired..** Typic
6c50: 61 6c 20 63 6f 64 65 20 6d 69 67 68 74 20 6c 6f al code might lo
6c60: 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a ok like this:.**
6c70: 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
6c80: 3c 70 72 65 3e 0a 2a 2a 20 73 71 6c 69 74 65 33 <pre>.** sqlite3
6c90: 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 2a 2a _stmt *pStmt;.**
6ca0: 20 77 68 69 6c 65 28 20 28 70 53 74 6d 74 20 3d while( (pStmt =
6cb0: 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 sqlite3_next_st
6cc0: 6d 74 28 64 62 2c 20 30 29 29 21 3d 30 20 29 7b mt(db, 0))!=0 ){
6cd0: 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 73 71 6c .** sql
6ce0: 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 ite3_finalize(pS
6cf0: 74 6d 74 29 3b 0a 2a 2a 20 7d 0a 2a 2a 20 3c 2f tmt);.** }.** </
6d00: 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 pre></blockquote
6d10: 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 >.**.** If [sqli
6d20: 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20 te3_close()] is
6d30: 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20 invoked while a
6d40: 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f transaction is o
6d50: 70 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e pen,.** the tran
6d60: 73 61 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d saction is autom
6d70: 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 atically rolled
6d80: 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 back..**.** The
6d90: 43 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b C parameter to [
6da0: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 sqlite3_close(C)
6db0: 5d 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 ] must be either
6dc0: 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 a NULL.** point
6dd0: 65 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 er or an [sqlite
6de0: 33 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65 3] object pointe
6df0: 72 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 r obtained.** fr
6e00: 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e om [sqlite3_open
6e10: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 ()], [sqlite3_op
6e20: 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b en16()], or.** [
6e30: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
6e40: 29 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76 )], and not prev
6e50: 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a iously closed..*
6e60: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
6e70: 73 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b s:.** [H12011] [
6e80: 48 31 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d H12012] [H12013]
6e90: 20 5b 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31 [H12014] [H1201
6ea0: 35 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53 5] [H12019].*/.S
6eb0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
6ec0: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 lite3_close(sqli
6ed0: 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 te3 *);../*.** T
6ee0: 68 65 20 74 79 70 65 20 66 6f 72 20 61 20 63 61 he type for a ca
6ef0: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e llback function.
6f00: 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61 .** This is lega
6f10: 63 79 20 61 6e 64 20 64 65 70 72 65 63 61 74 65 cy and deprecate
6f20: 64 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 d. It is includ
6f30: 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 ed for historica
6f40: 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 l.** compatibili
6f50: 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f ty and is not do
6f60: 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 cumented..*/.typ
6f70: 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 edef int (*sqlit
6f80: 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 e3_callback)(voi
6f90: 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 d*,int,char**, c
6fa0: 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 har**);../*.** C
6fb0: 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 API3REF: One-Ste
6fc0: 70 20 51 75 65 72 79 20 45 78 65 63 75 74 69 6f p Query Executio
6fd0: 6e 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32 n Interface {H12
6fe0: 31 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 100} <S10000>.**
6ff0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
7000: 65 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 exec() interface
7010: 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 is a convenient
7020: 20 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 way of running
7030: 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 one or more.** S
7040: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69 QL statements wi
7050: 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
7060: 77 72 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 write a lot of C
7070: 20 63 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d code. The UTF-
7080: 38 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 8 encoded.** SQL
7090: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 statements are
70a0: 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 passed in as the
70b0: 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
70c0: 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 r to sqlite3_exe
70d0: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 c()..** The stat
70e0: 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 ements are evalu
70f0: 61 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 ated one by one
7100: 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20 until either an
7110: 65 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 error or.** an i
7120: 6e 74 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f nterrupt is enco
7130: 75 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 untered, or unti
7140: 6c 20 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64 l they are all d
7150: 6f 6e 65 2e 20 20 54 68 65 20 33 72 64 20 70 61 one. The 3rd pa
7160: 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e rameter.** is an
7170: 20 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 optional callba
7180: 63 6b 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b ck that is invok
7190: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ed once for each
71a0: 20 72 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72 row of any quer
71b0: 79 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f y.** results pro
71c0: 64 75 63 65 64 20 62 79 20 74 68 65 20 53 51 4c duced by the SQL
71d0: 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 statements. Th
71e0: 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 5th parameter
71f0: 74 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 tells where.** t
7200: 6f 20 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f o write any erro
7210: 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a r messages..**.*
7220: 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 * The error mess
7230: 61 67 65 20 70 61 73 73 65 64 20 62 61 63 6b 20 age passed back
7240: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 through the 5th
7250: 70 61 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c parameter is hel
7260: 64 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f d.** in memory o
7270: 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 btained from [sq
7280: 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e lite3_malloc()].
7290: 20 20 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d To avoid a mem
72a0: 6f 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 ory leak,.** the
72b0: 20 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 calling applica
72c0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c tion should call
72d0: 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
72e0: 5d 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a ] on any error.*
72f0: 2a 20 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e * message return
7300: 65 64 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 ed through the 5
7310: 74 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 65 th parameter whe
7320: 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 n it has finishe
7330: 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 d using.** the e
7340: 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a rror message..**
7350: 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 .** If the SQL s
7360: 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 tatement in the
7370: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 2nd parameter is
7380: 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 NULL or an empt
7390: 79 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 y string.** or a
73a0: 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 string containi
73b0: 6e 67 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 ng only whitespa
73c0: 63 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c ce and comments,
73d0: 20 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 then no SQL.**
73e0: 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 statements are e
73f0: 76 61 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65 valuated and the
7400: 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 database is not
7410: 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 changed..**.**
7420: 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 The sqlite3_exec
7430: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 () interface is
7440: 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 implemented in t
7450: 65 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 erms of.** [sqli
7460: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
7470: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 ], [sqlite3_step
7480: 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
7490: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 3_finalize()]..*
74a0: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 * The sqlite3_ex
74b0: 65 63 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 ec() routine doe
74c0: 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 s nothing to the
74d0: 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63 database that c
74e0: 61 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a annot be done.**
74f0: 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 by [sqlite3_pre
7500: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c pare_v2()], [sql
7510: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e ite3_step()], an
7520: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c d [sqlite3_final
7530: 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 ize()]..**.** Th
7540: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
7550: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 r to [sqlite3_ex
7560: 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e ec()] must be an
7570: 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a valid and open.
7580: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
7590: 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 nection]..**.**
75a0: 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
75b0: 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 nection must not
75c0: 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 be closed while
75d0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 .** [sqlite3_exe
75e0: 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e c()] is running.
75f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 .**.** The calli
7600: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 ng function shou
7610: 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f ld use [sqlite3_
7620: 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a free()] to free.
7630: 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 ** the memory th
7640: 61 74 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 at *errmsg is le
7650: 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f ft pointing at o
7660: 6e 63 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a nce the error.**
7670: 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c message is no l
7680: 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a onger needed..**
7690: 0a 2a 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74 .** The SQL stat
76a0: 65 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68 ement text in th
76b0: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 e 2nd parameter
76c0: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 to [sqlite3_exec
76d0: 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61 ()].** must rema
76e0: 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69 in unchanged whi
76f0: 6c 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 le [sqlite3_exec
7700: 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a ()] is running..
7710: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
7720: 74 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20 ts:.** [H12101]
7730: 5b 48 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34 [H12102] [H12104
7740: 5d 20 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31 ] [H12105] [H121
7750: 30 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31 07] [H12110] [H1
7760: 32 31 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a 2113] [H12116].*
7770: 2a 20 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31 * [H12119] [H121
7780: 32 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31 22] [H12125] [H1
7790: 32 31 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b 2131] [H12134] [
77a0: 48 31 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d H12137] [H12138]
77b0: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
77c0: 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 nt sqlite3_exec(
77d0: 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 . sqlite3*,
77e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
7800: 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 An open databas
7810: 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
7820: 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20 r *sql,
7830: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7840: 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 /* SQL to be e
7850: 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e valuated */. in
7860: 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f t (*callback)(vo
7870: 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 id*,int,char**,c
7880: 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c har**), /* Call
7890: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f back function */
78a0: 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 . void *,
78b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
78d0: 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 1st argument to
78e0: 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 callback */. c
78f0: 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20 har **errmsg
7900: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7910: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
7920: 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 or msg written h
7930: 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ere */.);../*.**
7940: 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c CAPI3REF: Resul
7950: 74 20 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d t Codes {H10210}
7960: 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 <S10700>.** KEY
7970: 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b WORDS: SQLITE_OK
7980: 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 {error code} {e
7990: 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b rror codes}.** K
79a0: 45 59 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74 EYWORDS: {result
79b0: 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63 code} {result c
79c0: 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 odes}.**.** Many
79d0: 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e SQLite function
79e0: 73 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 s return an inte
79f0: 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 ger result code
7a00: 66 72 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f from the set sho
7a10: 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 wn.** here in or
7a20: 64 65 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73 der to indicates
7a30: 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c success or fail
7a40: 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65 ure..**.** New e
7a50: 72 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62 rror codes may b
7a60: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 e added in futur
7a70: 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 e versions of SQ
7a80: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 Lite..**.** See
7a90: 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f also: [SQLITE_IO
7aa0: 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e ERR_READ | exten
7ab0: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 ded result codes
7ac0: 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ].*/.#define SQL
7ad0: 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 ITE_OK
7ae0: 20 30 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66 0 /* Successf
7af0: 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 ul result */./*
7b00: 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 beginning-of-err
7b10: 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 or-codes */.#def
7b20: 69 6e 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 ine SQLITE_ERROR
7b30: 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53 1 /* S
7b40: 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 QL error or miss
7b50: 69 6e 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a ing database */.
7b60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
7b70: 4e 54 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20 NTERNAL 2
7b80: 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 /* Internal logi
7b90: 63 20 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74 c error in SQLit
7ba0: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
7bb0: 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 ITE_PERM
7bc0: 20 33 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70 3 /* Access p
7bd0: 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 ermission denied
7be0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
7bf0: 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 TE_ABORT
7c00: 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 4 /* Callback
7c10: 72 6f 75 74 69 6e 65 20 72 65 71 75 65 73 74 65 routine requeste
7c20: 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64 d an abort */.#d
7c30: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 efine SQLITE_BUS
7c40: 59 20 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a Y 5 /*
7c50: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
7c60: 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a le is locked */.
7c70: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
7c80: 4f 43 4b 45 44 20 20 20 20 20 20 20 36 20 20 20 OCKED 6
7c90: 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 /* A table in th
7ca0: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f e database is lo
7cb0: 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cked */.#define
7cc0: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 SQLITE_NOMEM
7cd0: 20 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 7 /* A mal
7ce0: 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a loc() failed */.
7cf0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
7d00: 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20 EADONLY 8
7d10: 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72 /* Attempt to wr
7d20: 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 ite a readonly d
7d30: 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 atabase */.#defi
7d40: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 ne SQLITE_INTERR
7d50: 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70 UPT 9 /* Op
7d60: 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74 eration terminat
7d70: 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e ed by sqlite3_in
7d80: 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66 terrupt()*/.#def
7d90: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
7da0: 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53 10 /* S
7db0: 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b ome kind of disk
7dc0: 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 I/O error occur
7dd0: 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 red */.#define S
7de0: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 QLITE_CORRUPT
7df0: 20 20 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61 11 /* The da
7e00: 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67 tabase disk imag
7e10: 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a e is malformed *
7e20: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
7e30: 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20 _NOTFOUND 12
7e40: 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54 /* NOT USED. T
7e50: 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e able or record n
7e60: 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66 ot found */.#def
7e70: 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 ine SQLITE_FULL
7e80: 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49 13 /* I
7e90: 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20 nsertion failed
7ea0: 62 65 63 61 75 73 65 20 64 61 74 61 62 61 73 65 because database
7eb0: 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66 is full */.#def
7ec0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f ine SQLITE_CANTO
7ed0: 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55 PEN 14 /* U
7ee0: 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 nable to open th
7ef0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
7f00: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
7f10: 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35 E_PROTOCOL 15
7f20: 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 /* NOT USED.
7f30: 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72 Database lock pr
7f40: 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a otocol error */.
7f50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 #define SQLITE_E
7f60: 4d 50 54 59 20 20 20 20 20 20 20 31 36 20 20 20 MPTY 16
7f70: 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 65 /* Database is e
7f80: 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mpty */.#define
7f90: 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20 SQLITE_SCHEMA
7fa0: 20 20 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64 17 /* The d
7fb0: 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 atabase schema c
7fc0: 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e hanged */.#defin
7fd0: 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 e SQLITE_TOOBIG
7fe0: 20 20 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72 18 /* Str
7ff0: 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65 ing or BLOB exce
8000: 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a eds size limit *
8010: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
8020: 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20 _CONSTRAINT 19
8030: 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74 /* Abort due t
8040: 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f o constraint vio
8050: 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e lation */.#defin
8060: 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 e SQLITE_MISMATC
8070: 48 20 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74 H 20 /* Dat
8080: 61 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20 a type mismatch
8090: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
80a0: 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20 32 31 E_MISUSE 21
80b0: 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73 /* Library us
80c0: 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a ed incorrectly *
80d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
80e0: 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20 _NOLFS 22
80f0: 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61 /* Uses OS fea
8100: 74 75 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 tures not suppor
8110: 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23 ted on host */.#
8120: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55 define SQLITE_AU
8130: 54 48 20 20 20 20 20 20 20 20 32 33 20 20 20 2f TH 23 /
8140: 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 * Authorization
8150: 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e denied */.#defin
8160: 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 e SQLITE_FORMAT
8170: 20 20 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78 24 /* Aux
8180: 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 iliary database
8190: 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a format error */.
81a0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
81b0: 41 4e 47 45 20 20 20 20 20 20 20 32 35 20 20 20 ANGE 25
81c0: 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 /* 2nd parameter
81d0: 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 to sqlite3_bind
81e0: 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f out of range */
81f0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8200: 4e 4f 54 41 44 42 20 20 20 20 20 20 32 36 20 20 NOTADB 26
8210: 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 /* File opened
8220: 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61 that is not a da
8230: 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 tabase file */.#
8240: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f define SQLITE_RO
8250: 57 20 20 20 20 20 20 20 20 20 31 30 30 20 20 2f W 100 /
8260: 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 * sqlite3_step()
8270: 20 68 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 has another row
8280: 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e ready */.#defin
8290: 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 e SQLITE_DONE
82a0: 20 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 101 /* sql
82b0: 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 ite3_step() has
82c0: 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 finished executi
82d0: 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d ng */./* end-of-
82e0: 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a error-codes */..
82f0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
8300: 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 Extended Result
8310: 43 6f 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c Codes {H10220} <
8320: 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S10700>.** KEYWO
8330: 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 RDS: {extended e
8340: 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 rror code} {exte
8350: 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 nded error codes
8360: 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b }.** KEYWORDS: {
8370: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
8380: 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 code} {extended
8390: 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a result codes}.**
83a0: 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75 .** In its defau
83b0: 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e lt configuration
83c0: 2c 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75 , SQLite API rou
83d0: 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 tines return one
83e0: 20 6f 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a of 26 integer.*
83f0: 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 * [SQLITE_OK | r
8400: 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48 esult codes]. H
8410: 6f 77 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e owever, experien
8420: 63 65 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61 ce has shown tha
8430: 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 t many of.** the
8440: 73 65 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 se result codes
8450: 61 72 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67 are too coarse-g
8460: 72 61 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f rained. They do
8470: 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a not provide as.
8480: 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 ** much informat
8490: 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65 ion about proble
84a0: 6d 73 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72 ms as programmer
84b0: 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 s might like. I
84c0: 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a n an effort to.*
84d0: 2a 20 61 64 64 72 65 73 73 20 74 68 69 73 2c 20 * address this,
84e0: 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f newer versions o
84f0: 66 20 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f f SQLite (versio
8500: 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 n 3.3.8 and late
8510: 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 r) include.** su
8520: 70 70 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69 pport for additi
8530: 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 onal result code
8540: 73 20 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d s that provide m
8550: 6f 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66 ore detailed inf
8560: 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 ormation.** abou
8570: 74 20 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78 t errors. The ex
8580: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f tended result co
8590: 64 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 des are enabled
85a0: 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f or disabled.** o
85b0: 6e 20 61 20 70 65 72 20 64 61 74 61 62 61 73 65 n a per database
85c0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 connection basi
85d0: 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b s using the.** [
85e0: 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
85f0: 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d _result_codes()]
8600: 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 API..**.** Some
8610: 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c of the availabl
8620: 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c e extended resul
8630: 74 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74 t codes are list
8640: 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20 ed here..** One
8650: 6d 61 79 20 65 78 70 65 63 74 20 74 68 65 20 6e may expect the n
8660: 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65 umber of extende
8670: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 d result codes w
8680: 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a ill be expand.**
8690: 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66 over time. Sof
86a0: 74 77 61 72 65 20 74 68 61 74 20 75 73 65 73 20 tware that uses
86b0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
86c0: 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70 codes should exp
86d0: 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65 ect.** to see ne
86e0: 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69 w result codes i
86f0: 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
8700: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a s of SQLite..**.
8710: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b ** The SQLITE_OK
8720: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c result code wil
8730: 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e l never be exten
8740: 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c ded. It will al
8750: 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74 ways.** be exact
8760: 6c 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66 ly zero..*/.#def
8770: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8780: 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20 _READ
8790: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
87a0: 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 | (1<<8)).#defi
87b0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f ne SQLITE_IOERR_
87c0: 53 48 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20 SHORT_READ
87d0: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 (SQLITE_IOERR
87e0: 7c 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e | (2<<8)).#defin
87f0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 e SQLITE_IOERR_W
8800: 52 49 54 45 20 20 20 20 20 20 20 20 20 20 20 20 RITE
8810: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c (SQLITE_IOERR |
8820: 20 28 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 (3<<8)).#define
8830: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 SQLITE_IOERR_FS
8840: 59 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20 YNC
8850: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 (SQLITE_IOERR |
8860: 28 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 (4<<8)).#define
8870: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 SQLITE_IOERR_DIR
8880: 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 28 _FSYNC (
8890: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 SQLITE_IOERR | (
88a0: 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 5<<8)).#define S
88b0: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e QLITE_IOERR_TRUN
88c0: 43 41 54 45 20 20 20 20 20 20 20 20 20 20 28 53 CATE (S
88d0: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 QLITE_IOERR | (6
88e0: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 <<8)).#define SQ
88f0: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 LITE_IOERR_FSTAT
8900: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 (SQ
8910: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c LITE_IOERR | (7<
8920: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c <8)).#define SQL
8930: 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
8940: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
8950: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c ITE_IOERR | (8<<
8960: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
8970: 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 TE_IOERR_RDLOCK
8980: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 (SQLI
8990: 54 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 TE_IOERR | (9<<8
89a0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
89b0: 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 E_IOERR_DELETE
89c0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
89d0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 E_IOERR | (10<<8
89e0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
89f0: 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 E_IOERR_BLOCKED
8a00: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8a10: 45 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 E_IOERR | (11<<8
8a20: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8a30: 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 E_IOERR_NOMEM
8a40: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8a50: 45 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 E_IOERR | (12<<8
8a60: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8a70: 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20 E_IOERR_ACCESS
8a80: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8a90: 45 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 E_IOERR | (13<<8
8aa0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8ab0: 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 E_IOERR_CHECKRES
8ac0: 45 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 ERVEDLOCK (SQLIT
8ad0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 E_IOERR | (14<<8
8ae0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8af0: 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 E_IOERR_LOCK
8b00: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8b10: 45 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 E_IOERR | (15<<8
8b20: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8b30: 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20 E_IOERR_CLOSE
8b40: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
8b50: 45 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38 E_IOERR | (16<<8
8b60: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8b70: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 E_IOERR_DIR_CLOS
8b80: 45 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 E (SQLIT
8b90: 45 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38 E_IOERR | (17<<8
8ba0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
8bb0: 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
8bc0: 41 43 48 45 20 20 20 20 20 20 28 53 51 4c 49 54 ACHE (SQLIT
8bd0: 45 5f 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38 E_LOCKED | (1<<8
8be0: 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 ) )../*.** CAPI3
8bf0: 52 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 REF: Flags For F
8c00: 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 ile Open Operati
8c10: 6f 6e 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31 ons {H10230} <H1
8c20: 31 31 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 1120> <H12700>.*
8c30: 2a 0a 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76 *.** These bit v
8c40: 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64 alues are intend
8c50: 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68 ed for use in th
8c60: 65 0a 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74 e.** 3rd paramet
8c70: 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 er to the [sqlit
8c80: 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e e3_open_v2()] in
8c90: 74 65 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69 terface and.** i
8ca0: 6e 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 n the 4th parame
8cb0: 74 65 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e ter to the xOpen
8cc0: 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a method of the.*
8cd0: 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 * [sqlite3_vfs]
8ce0: 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 object..*/.#defi
8cf0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ne SQLITE_OPEN_R
8d00: 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 EADONLY
8d10: 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f 0x00000001 /* O
8d20: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8d30: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8d40: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ne SQLITE_OPEN_R
8d50: 45 41 44 57 52 49 54 45 20 20 20 20 20 20 20 20 EADWRITE
8d60: 30 78 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f 0x00000002 /* O
8d70: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8d80: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8d90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 ne SQLITE_OPEN_C
8da0: 52 45 41 54 45 20 20 20 20 20 20 20 20 20 20 20 REATE
8db0: 30 78 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f 0x00000004 /* O
8dc0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
8dd0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
8de0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 ne SQLITE_OPEN_D
8df0: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20 ELETEONCLOSE
8e00: 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56 0x00000008 /* V
8e10: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8e20: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 ne SQLITE_OPEN_E
8e30: 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 XCLUSIVE
8e40: 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56 0x00000010 /* V
8e50: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8e60: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d ne SQLITE_OPEN_M
8e70: 41 49 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20 AIN_DB
8e80: 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56 0x00000100 /* V
8e90: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8ea0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 ne SQLITE_OPEN_T
8eb0: 45 4d 50 5f 44 42 20 20 20 20 20 20 20 20 20 20 EMP_DB
8ec0: 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56 0x00000200 /* V
8ed0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8ee0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 ne SQLITE_OPEN_T
8ef0: 52 41 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20 RANSIENT_DB
8f00: 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56 0x00000400 /* V
8f10: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8f20: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d ne SQLITE_OPEN_M
8f30: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 AIN_JOURNAL
8f40: 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56 0x00000800 /* V
8f50: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8f60: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 ne SQLITE_OPEN_T
8f70: 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 EMP_JOURNAL
8f80: 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56 0x00001000 /* V
8f90: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8fa0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 ne SQLITE_OPEN_S
8fb0: 55 42 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20 UBJOURNAL
8fc0: 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56 0x00002000 /* V
8fd0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
8fe0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d ne SQLITE_OPEN_M
8ff0: 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 ASTER_JOURNAL
9000: 30 78 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56 0x00004000 /* V
9010: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 FS only */.#defi
9020: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e ne SQLITE_OPEN_N
9030: 4f 4d 55 54 45 58 20 20 20 20 20 20 20 20 20 20 OMUTEX
9040: 30 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f 0x00008000 /* O
9050: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
9060: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
9070: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 ne SQLITE_OPEN_F
9080: 55 4c 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20 ULLMUTEX
9090: 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f 0x00010000 /* O
90a0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
90b0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
90c0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 ne SQLITE_OPEN_S
90d0: 48 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20 HAREDCACHE
90e0: 30 78 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f 0x00020000 /* O
90f0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
9100: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 en_v2() */.#defi
9110: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 ne SQLITE_OPEN_P
9120: 52 49 56 41 54 45 43 41 43 48 45 20 20 20 20 20 RIVATECACHE
9130: 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f 0x00040000 /* O
9140: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 k for sqlite3_op
9150: 65 6e 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a en_v2() */../*.*
9160: 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 76 69 * CAPI3REF: Devi
9170: 63 65 20 43 68 61 72 61 63 74 65 72 69 73 74 69 ce Characteristi
9180: 63 73 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31 cs {H10240} <H11
9190: 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 120>.**.** The x
91a0: 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 DeviceCapabiliti
91b0: 65 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 es method of the
91c0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 [sqlite3_io_met
91d0: 68 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 hods].** object
91e0: 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 returns an integ
91f0: 65 72 20 77 68 69 63 68 20 69 73 20 61 20 76 65 er which is a ve
9200: 63 74 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73 ctor of the thes
9210: 65 0a 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20 e.** bit values
9220: 65 78 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63 expressing I/O c
9230: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f haracteristics o
9240: 66 20 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61 f the mass stora
9250: 67 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 ge.** device tha
9260: 74 20 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65 t holds the file
9270: 20 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 that the [sqlit
9280: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a e3_io_methods].*
9290: 2a 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a * refers to..**.
92a0: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f ** The SQLITE_IO
92b0: 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 CAP_ATOMIC prope
92c0: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 rty means that a
92d0: 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 ll writes of.**
92e0: 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f any size are ato
92f0: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 mic. The SQLITE
9300: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e _IOCAP_ATOMICnnn
9310: 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 values.** mean
9320: 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 that writes of b
9330: 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e locks that are n
9340: 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 nn bytes in size
9350: 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 and.** are alig
9360: 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 ned to an addres
9370: 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e s which is an in
9380: 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f teger multiple o
9390: 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f f.** nnn are ato
93a0: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 mic. The SQLITE
93b0: 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 _IOCAP_SAFE_APPE
93c0: 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a ND value means.*
93d0: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 * that when data
93e0: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 is appended to
93f0: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 a file, the data
9400: 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 is appended.**
9410: 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 first then the s
9420: 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ize of the file
9430: 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 is extended, nev
9440: 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 er the other.**
9450: 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 way around. The
9460: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 SQLITE_IOCAP_SE
9470: 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 QUENTIAL propert
9480: 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 y means that.**
9490: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 information is w
94a0: 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 ritten to disk i
94b0: 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 n the same order
94c0: 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 as calls.** to
94d0: 78 57 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 xWrite()..*/.#de
94e0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
94f0: 50 5f 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20 P_ATOMIC
9500: 20 20 30 78 30 30 30 30 30 30 30 31 0a 23 64 65 0x00000001.#de
9510: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9520: 50 5f 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20 P_ATOMIC512
9530: 20 20 30 78 30 30 30 30 30 30 30 32 0a 23 64 65 0x00000002.#de
9540: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9550: 50 5f 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20 P_ATOMIC1K
9560: 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65 0x00000004.#de
9570: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9580: 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 P_ATOMIC2K
9590: 20 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 65 0x00000008.#de
95a0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
95b0: 50 5f 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20 P_ATOMIC4K
95c0: 20 20 30 78 30 30 30 30 30 30 31 30 0a 23 64 65 0x00000010.#de
95d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
95e0: 50 5f 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 P_ATOMIC8K
95f0: 20 20 30 78 30 30 30 30 30 30 32 30 0a 23 64 65 0x00000020.#de
9600: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9610: 50 5f 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20 P_ATOMIC16K
9620: 20 20 30 78 30 30 30 30 30 30 34 30 0a 23 64 65 0x00000040.#de
9630: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9640: 50 5f 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20 P_ATOMIC32K
9650: 20 20 30 78 30 30 30 30 30 30 38 30 0a 23 64 65 0x00000080.#de
9660: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
9670: 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20 P_ATOMIC64K
9680: 20 20 30 78 30 30 30 30 30 31 30 30 0a 23 64 65 0x00000100.#de
9690: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
96a0: 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20 P_SAFE_APPEND
96b0: 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 65 0x00000200.#de
96c0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 fine SQLITE_IOCA
96d0: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20 P_SEQUENTIAL
96e0: 20 20 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0x00000400../*
96f0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 .** CAPI3REF: Fi
9700: 6c 65 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c le Locking Level
9710: 73 20 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31 s {H10250} <H111
9720: 32 30 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 20> <H11310>.**.
9730: 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f ** SQLite uses o
9740: 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 ne of these inte
9750: 67 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 ger values as th
9760: 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 e second.** argu
9770: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74 ment to calls it
9780: 20 6d 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c makes to the xL
9790: 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 ock() and xUnloc
97a0: 6b 28 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f k() methods.** o
97b0: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f f an [sqlite3_io
97c0: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 _methods] object
97d0: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
97e0: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 ITE_LOCK_NONE
97f0: 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 0.#define
9800: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 SQLITE_LOCK_SHA
9810: 52 45 44 20 20 20 20 20 20 20 20 31 0a 23 64 65 RED 1.#de
9820: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b fine SQLITE_LOCK
9830: 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 32 _RESERVED 2
9840: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
9850: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20 LOCK_PENDING
9860: 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 3.#define SQL
9870: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 ITE_LOCK_EXCLUSI
9880: 56 45 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 VE 4../*.**
9890: 43 41 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72 CAPI3REF: Synchr
98a0: 6f 6e 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46 onization Type F
98b0: 6c 61 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48 lags {H10260} <H
98c0: 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 11120>.**.** Whe
98d0: 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 n SQLite invokes
98e0: 20 74 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74 the xSync() met
98f0: 68 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 hod of an.** [sq
9900: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
9910: 5d 20 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73 ] object it uses
9920: 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f a combination o
9930: 66 0a 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67 f.** these integ
9940: 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 er values as the
9950: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
9960: 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 ..**.** When the
9970: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 SQLITE_SYNC_DAT
9980: 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73 AONLY flag is us
9990: 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 ed, it means tha
99a0: 74 20 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 t the.** sync op
99b0: 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 eration only nee
99c0: 64 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61 ds to flush data
99d0: 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65 to mass storage
99e0: 2e 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f . Inode.** info
99f0: 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 rmation need not
9a00: 20 62 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20 be flushed. If
9a10: 74 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 the lower four b
9a20: 69 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a its of the flag.
9a30: 2a 2a 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f ** equal SQLITE_
9a40: 53 59 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61 SYNC_NORMAL, tha
9a50: 74 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e t means to use n
9a60: 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65 ormal fsync() se
9a70: 6d 61 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74 mantics..** If t
9a80: 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 he lower four bi
9a90: 74 73 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f ts equal SQLITE_
9aa0: 53 59 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20 SYNC_FULL, that
9ab0: 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 means.** to use
9ac0: 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 Mac OS X style f
9ad0: 75 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20 ullsync instead
9ae0: 6f 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 of fsync()..*/.#
9af0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 define SQLITE_SY
9b00: 4e 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 NC_NORMAL
9b10: 20 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65 0x00002.#define
9b20: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c SQLITE_SYNC_FUL
9b30: 4c 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 L 0x000
9b40: 30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 03.#define SQLIT
9b50: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 E_SYNC_DATAONLY
9b60: 20 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0x00010../*
9b70: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 .** CAPI3REF: OS
9b80: 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20 Interface Open
9b90: 46 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 File Handle {H11
9ba0: 31 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 110} <S20110>.**
9bb0: 0a 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f .** An [sqlite3_
9bc0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 file] object rep
9bd0: 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 resents an open
9be0: 66 69 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 file in the .**
9bf0: 5b 73 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f [sqlite3_vfs | O
9c00: 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 S interface laye
9c10: 72 5d 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 r]. Individual
9c20: 4f 53 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 OS interface.**
9c30: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
9c40: 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 will.** want to
9c50: 73 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62 subclass this ob
9c60: 6a 65 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e ject by appendin
9c70: 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 g additional fie
9c80: 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 lds.** for their
9c90: 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70 own use. The p
9ca0: 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73 Methods entry is
9cb0: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
9cc0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f .** [sqlite3_io_
9cd0: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 methods] object
9ce0: 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74 that defines met
9cf0: 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d hods for perform
9d00: 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 ing.** I/O opera
9d10: 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65 tions on the ope
9d20: 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 n file..*/.typed
9d30: 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
9d40: 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 3_file sqlite3_f
9d50: 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 ile;.struct sqli
9d60: 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e te3_file {. con
9d70: 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 st struct sqlite
9d80: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 3_io_methods *pM
9d90: 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68 ethods; /* Meth
9da0: 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 ods for an open
9db0: 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a file */.};../*.*
9dc0: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 * CAPI3REF: OS I
9dd0: 6e 74 65 72 66 61 63 65 20 46 69 6c 65 20 56 69 nterface File Vi
9de0: 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62 rtual Methods Ob
9df0: 6a 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53 ject {H11120} <S
9e00: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 20110>.**.** Eve
9e10: 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 ry file opened b
9e20: 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 y the [sqlite3_v
9e30: 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 fs] xOpen method
9e40: 20 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a populates an.**
9e50: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 [sqlite3_file]
9e60: 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65 object (or, more
9e70: 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 commonly, a sub
9e80: 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 class of the.**
9e90: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f [sqlite3_file] o
9ea0: 62 6a 65 63 74 29 20 77 69 74 68 20 61 20 70 6f bject) with a po
9eb0: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 inter to an inst
9ec0: 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a ance of this obj
9ed0: 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a ect..** This obj
9ee0: 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 ect defines the
9ef0: 6d 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20 methods used to
9f00: 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 perform various
9f10: 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 operations.** ag
9f20: 61 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66 ainst the open f
9f30: 69 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 ile represented
9f40: 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f by the [sqlite3_
9f50: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a file] object..**
9f60: 0a 2a 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e .** If the xOpen
9f70: 20 6d 65 74 68 6f 64 20 73 65 74 73 20 74 68 65 method sets the
9f80: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d sqlite3_file.pM
9f90: 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a ethods element .
9fa0: 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c ** to a non-NULL
9fb0: 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 pointer, then t
9fc0: 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
9fd0: 74 68 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 thods.xClose met
9fe0: 68 6f 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e hod.** may be in
9ff0: 76 6f 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68 voked even if th
a000: 65 20 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64 e xOpen reported
a010: 20 74 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e that it failed.
a020: 20 20 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61 The.** only wa
a030: 79 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63 y to prevent a c
a040: 61 6c 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f all to xClose fo
a050: 6c 6c 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64 llowing a failed
a060: 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72 xOpen.** is for
a070: 20 74 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65 the xOpen to se
a080: 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 t the sqlite3_fi
a090: 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d le.pMethods elem
a0a0: 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a ent to NULL..**.
a0b0: 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 ** The flags arg
a0c0: 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d ument to xSync m
a0d0: 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 ay be one of [SQ
a0e0: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
a0f0: 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f ] or.** [SQLITE_
a100: 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 SYNC_FULL]. The
a110: 20 66 69 72 73 74 20 63 68 6f 69 63 65 20 69 73 first choice is
a120: 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e the normal fsyn
a130: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f c()..** The seco
a140: 6e 64 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d nd choice is a M
a150: 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 ac OS X style fu
a160: 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 llsync. The [SQ
a170: 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e LITE_SYNC_DATAON
a180: 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 LY].** flag may
a190: 62 65 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e be ORed in to in
a1a0: 64 69 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 dicate that only
a1b0: 20 74 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 the data of the
a1c0: 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 file.** and not
a1d0: 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 its inode needs
a1e0: 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a to be synced..*
a1f0: 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72 *.** The integer
a200: 20 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b values to xLock
a210: 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 () and xUnlock()
a220: 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c are one of.** <
a230: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c ul>.** <li> [SQL
a240: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a ITE_LOCK_NONE],.
a250: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
a260: 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a LOCK_SHARED],.**
a270: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f <li> [SQLITE_LO
a280: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a CK_RESERVED],.**
a290: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f <li> [SQLITE_LO
a2a0: 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a CK_PENDING], or.
a2b0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
a2c0: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e LOCK_EXCLUSIVE].
a2d0: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f .** </ul>.** xLo
a2e0: 63 6b 28 29 20 69 6e 63 72 65 61 73 65 73 20 74 ck() increases t
a2f0: 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b he lock. xUnlock
a300: 28 29 20 64 65 63 72 65 61 73 65 73 20 74 68 65 () decreases the
a310: 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 lock..** The xC
a320: 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
a330: 28 29 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 () method checks
a340: 20 77 68 65 74 68 65 72 20 61 6e 79 20 64 61 74 whether any dat
a350: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
a360: 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74 ,.** either in t
a370: 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69 his process or i
a380: 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f n some other pro
a390: 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 cess, is holding
a3a0: 20 61 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 a RESERVED,.**
a3b0: 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c PENDING, or EXCL
a3c0: 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 USIVE lock on th
a3d0: 65 20 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75 e file. It retu
a3e0: 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 rns true.** if s
a3f0: 75 63 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 uch a lock exist
a400: 73 20 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 s and false othe
a410: 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rwise..**.** The
a420: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 xFileControl()
a430: 6d 65 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65 method is a gene
a440: 72 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 68 ric interface th
a450: 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d at allows custom
a460: 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e .** VFS implemen
a470: 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 tations to direc
a480: 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f tly control an o
a490: 70 65 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74 pen file using t
a4a0: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 he.** [sqlite3_f
a4b0: 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 ile_control()] i
a4c0: 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65 20 73 nterface. The s
a4d0: 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d econd "op" argum
a4e0: 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 ent is an.** int
a4f0: 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 eger opcode. Th
a500: 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
a510: 20 69 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f is a generic po
a520: 69 6e 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74 inter intended t
a530: 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 o.** point to a
a540: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d structure that m
a550: 61 79 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d ay contain argum
a560: 65 6e 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e ents or space in
a570: 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 which to.** wri
a580: 74 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 te return values
a590: 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 . Potential use
a5a0: 73 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 s for xFileContr
a5b0: 6f 6c 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a ol() might be.**
a5c0: 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e functions to en
a5d0: 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f able blocking lo
a5e0: 63 6b 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74 cks with timeout
a5f0: 73 2c 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 s, to change the
a600: 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 .** locking stra
a610: 74 65 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c tegy (for exampl
a620: 65 20 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c e to use dot-fil
a630: 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 e locks), to inq
a640: 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 uire.** about th
a650: 65 20 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f e status of a lo
a660: 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 ck, or to break
a670: 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 stale locks. Th
a680: 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 e SQLite.** core
a690: 20 72 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 reserves all op
a6a0: 63 6f 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20 codes less than
a6b0: 31 30 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 100 for its own
a6c0: 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 use..** A [SQLIT
a6d0: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 E_FCNTL_LOCKSTAT
a6e0: 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f E | list of opco
a6f0: 64 65 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 des] less than 1
a700: 30 30 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 00 is available.
a710: 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 .** Applications
a720: 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 20 63 that define a c
a730: 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 ustom xFileContr
a740: 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 ol method should
a750: 20 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 use opcodes.**
a760: 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 30 30 greater than 100
a770: 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 to avoid confli
a780: 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 cts..**.** The x
a790: 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 SectorSize() met
a7a0: 68 6f 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 hod returns the
a7b0: 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 sector size of t
a7c0: 68 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 he.** device tha
a7d0: 74 20 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20 t underlies the
a7e0: 66 69 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f file. The secto
a7f0: 72 20 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a r size is the.**
a800: 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 minimum write t
a810: 68 61 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f hat can be perfo
a820: 72 6d 65 64 20 77 69 74 68 6f 75 74 20 64 69 73 rmed without dis
a830: 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 turbing.** other
a840: 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 69 bytes in the fi
a850: 6c 65 2e 20 20 54 68 65 20 78 44 65 76 69 63 65 le. The xDevice
a860: 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
a870: 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 ).** method retu
a880: 72 6e 73 20 61 20 62 69 74 20 76 65 63 74 6f 72 rns a bit vector
a890: 20 64 65 73 63 72 69 62 69 6e 67 20 62 65 68 61 describing beha
a8a0: 76 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 viors of the.**
a8b0: 75 6e 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63 underlying devic
a8c0: 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a e:.**.** <ul>.**
a8d0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f <li> [SQLITE_IO
a8e0: 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c CAP_ATOMIC].** <
a8f0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
a900: 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 P_ATOMIC512].**
a910: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a920: 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 AP_ATOMIC1K].**
a930: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a940: 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20 AP_ATOMIC2K].**
a950: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a960: 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 AP_ATOMIC4K].**
a970: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a980: 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 AP_ATOMIC8K].**
a990: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 <li> [SQLITE_IOC
a9a0: 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a AP_ATOMIC16K].**
a9b0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f <li> [SQLITE_IO
a9c0: 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a CAP_ATOMIC32K].*
a9d0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 * <li> [SQLITE_I
a9e0: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a OCAP_ATOMIC64K].
a9f0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
aa00: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e IOCAP_SAFE_APPEN
aa10: 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 D].** <li> [SQLI
aa20: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 TE_IOCAP_SEQUENT
aa30: 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a IAL].** </ul>.**
aa40: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 .** The SQLITE_I
aa50: 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 OCAP_ATOMIC prop
aa60: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 erty means that
aa70: 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a all writes of.**
aa80: 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 any size are at
aa90: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 omic. The SQLIT
aaa0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e E_IOCAP_ATOMICnn
aab0: 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e n values.** mean
aac0: 20 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 that writes of
aad0: 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 blocks that are
aae0: 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a nnn bytes in siz
aaf0: 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 e and.** are ali
ab00: 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 gned to an addre
ab10: 73 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 ss which is an i
ab20: 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 nteger multiple
ab30: 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 of.** nnn are at
ab40: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 omic. The SQLIT
ab50: 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 E_IOCAP_SAFE_APP
ab60: 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a END value means.
ab70: 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 ** that when dat
ab80: 61 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f a is appended to
ab90: 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 a file, the dat
aba0: 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a a is appended.**
abb0: 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 first then the
abc0: 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
abd0: 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 is extended, ne
abe0: 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a ver the other.**
abf0: 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 way around. Th
ac00: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 e SQLITE_IOCAP_S
ac10: 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 EQUENTIAL proper
ac20: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a ty means that.**
ac30: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 information is
ac40: 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 written to disk
ac50: 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 in the same orde
ac60: 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f r as calls.** to
ac70: 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a xWrite()..**.**
ac80: 20 49 66 20 78 52 65 61 64 28 29 20 72 65 74 75 If xRead() retu
ac90: 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 rns SQLITE_IOERR
aca0: 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d _SHORT_READ it m
acb0: 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a ust also fill.**
acc0: 20 69 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70 in the unread p
acd0: 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 ortions of the b
ace0: 75 66 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73 uffer with zeros
acf0: 2e 20 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a . A VFS that.**
ad00: 20 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 fails to zero-f
ad10: 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 ill short reads
ad20: 6d 69 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f might seem to wo
ad30: 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a rk. However,.**
ad40: 20 66 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f failure to zero
ad50: 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 -fill short read
ad60: 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c s will eventuall
ad70: 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 y lead to.** dat
ad80: 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
ad90: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
ada0: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d uct sqlite3_io_m
adb0: 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 ethods sqlite3_i
adc0: 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 o_methods;.struc
add0: 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
ade0: 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 hods {. int iVe
adf0: 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 rsion;. int (*x
ae00: 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66 Close)(sqlite3_f
ae10: 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 ile*);. int (*x
ae20: 52 65 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69 Read)(sqlite3_fi
ae30: 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 le*, void*, int
ae40: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e iAmt, sqlite3_in
ae50: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e t64 iOfst);. in
ae60: 74 20 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69 t (*xWrite)(sqli
ae70: 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 te3_file*, const
ae80: 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 void*, int iAmt
ae90: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
aea0: 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a iOfst);. int (*
aeb0: 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 xTruncate)(sqlit
aec0: 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 e3_file*, sqlite
aed0: 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 3_int64 size);.
aee0: 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 int (*xSync)(sq
aef0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 lite3_file*, int
af00: 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 flags);. int (
af10: 2a 78 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 *xFileSize)(sqli
af20: 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 te3_file*, sqlit
af30: 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 e3_int64 *pSize)
af40: 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 ;. int (*xLock)
af50: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
af60: 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 int);. int (*xU
af70: 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 nlock)(sqlite3_f
af80: 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e ile*, int);. in
af90: 74 20 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76 t (*xCheckReserv
afa0: 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f edLock)(sqlite3_
afb0: 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 file*, int *pRes
afc0: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 Out);. int (*xF
afd0: 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 ileControl)(sqli
afe0: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f te3_file*, int o
aff0: 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a p, void *pArg);.
b000: 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 int (*xSectorS
b010: 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ize)(sqlite3_fil
b020: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 e*);. int (*xDe
b030: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
b040: 69 63 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ics)(sqlite3_fil
b050: 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 e*);. /* Additi
b060: 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 onal methods may
b070: 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 be added in fut
b080: 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a ure releases */.
b090: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 };../*.** CAPI3R
b0a0: 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c EF: Standard Fil
b0b0: 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 e Control Opcode
b0c0: 73 20 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38 s {H11310} <S308
b0d0: 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
b0e0: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
b0f0: 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f s are opcodes fo
b100: 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 r the xFileContr
b110: 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 ol method.** of
b120: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f the [sqlite3_io_
b130: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 methods] object
b140: 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c and for the [sql
b150: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f ite3_file_contro
b160: 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 l()].** interfac
b170: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 e..**.** The [SQ
b180: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 LITE_FCNTL_LOCKS
b190: 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 TATE] opcode is
b1a0: 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 used for debuggi
b1b0: 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 ng. This.** opc
b1c0: 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78 ode causes the x
b1d0: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
b1e0: 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 od to write the
b1f0: 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 current state of
b200: 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e .** the lock (on
b210: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 e of [SQLITE_LOC
b220: 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 K_NONE], [SQLITE
b230: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a _LOCK_SHARED],.*
b240: 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 * [SQLITE_LOCK_R
b250: 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 ESERVED], [SQLIT
b260: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c E_LOCK_PENDING],
b270: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b or [SQLITE_LOCK
b280: 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 _EXCLUSIVE]).**
b290: 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 into an integer
b2a0: 74 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72 that the pArg ar
b2b0: 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f gument points to
b2c0: 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74 . This capabilit
b2d0: 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72 y.** is used dur
b2e0: 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20 ing testing and
b2f0: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65 only needs to be
b300: 20 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20 supported when
b310: 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 SQLITE_TEST.** i
b320: 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 s defined..*/.#d
b330: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e efine SQLITE_FCN
b340: 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 TL_LOCKSTATE
b350: 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
b360: 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f LITE_GET_LOCKPRO
b370: 58 59 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64 XYFILE 2.#d
b380: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 efine SQLITE_SET
b390: 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 _LOCKPROXYFILE
b3a0: 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 3.#define SQ
b3b0: 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 LITE_LAST_ERRNO
b3c0: 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 4../
b3d0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d *.** CAPI3REF: M
b3e0: 75 74 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 utex Handle {H17
b3f0: 31 31 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 110} <S20130>.**
b400: 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f .** The mutex mo
b410: 64 75 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 dule within SQLi
b420: 74 65 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 te defines [sqli
b430: 74 65 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 te3_mutex] to be
b440: 20 61 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 an.** abstract
b450: 74 79 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 type for a mutex
b460: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 object. The SQ
b470: 4c 69 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 Lite core never
b480: 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 looks.** at the
b490: 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 internal represe
b4a0: 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 ntation of an [s
b4b0: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 qlite3_mutex].
b4c0: 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 It only.** deals
b4d0: 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
b4e0: 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d o the [sqlite3_m
b4f0: 75 74 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a utex] object..**
b500: 0a 2a 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20 .** Mutexes are
b510: 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 created using [s
b520: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
b530: 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 oc()]..*/.typede
b540: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
b550: 5f 6d 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d _mutex sqlite3_m
b560: 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 utex;../*.** CAP
b570: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 I3REF: OS Interf
b580: 61 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 ace Object {H111
b590: 34 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 40} <S20100>.**.
b5a0: 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
b5b0: 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 f the sqlite3_vf
b5c0: 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 s object defines
b5d0: 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 the interface b
b5e0: 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 etween.** the SQ
b5f0: 4c 69 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68 Lite core and th
b600: 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 e underlying ope
b610: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 rating system.
b620: 54 68 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 The "vfs".** in
b630: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
b640: 6f 62 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f object stands fo
b650: 72 20 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20 r "virtual file
b660: 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 system"..**.** T
b670: 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
b680: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 iVersion field i
b690: 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 s initially 1 bu
b6a0: 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20 t may be larger
b6b0: 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72 in.** future ver
b6c0: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e sions of SQLite.
b6d0: 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 Additional fie
b6e0: 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e lds may be appen
b6f0: 64 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f ded to this.** o
b700: 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 bject when the i
b710: 56 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 Version value is
b720: 20 69 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 increased. Not
b730: 65 20 74 68 61 74 20 74 68 65 20 73 74 72 75 63 e that the struc
b740: 74 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 ture.** of the s
b750: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
b760: 74 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 t changes in the
b770: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 transaction bet
b780: 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 ween.** SQLite v
b790: 65 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 ersion 3.5.9 and
b7a0: 20 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74 3.6.0 and yet t
b7b0: 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c he iVersion fiel
b7c0: 64 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 d was not.** mod
b7d0: 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ified..**.** The
b7e0: 20 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 szOsFile field
b7f0: 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 is the size of t
b800: 68 65 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73 he subclassed [s
b810: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 qlite3_file].**
b820: 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 62 structure used b
b830: 79 20 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50 y this VFS. mxP
b840: 61 74 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d athname is the m
b850: 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 aximum length of
b860: 0a 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69 .** a pathname i
b870: 6e 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a n this VFS..**.*
b880: 2a 20 52 65 67 69 73 74 65 72 65 64 20 73 71 6c * Registered sql
b890: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 ite3_vfs objects
b8a0: 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c are kept on a l
b8b0: 69 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 inked list forme
b8c0: 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 d by.** the pNex
b8d0: 74 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 t pointer. The
b8e0: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 [sqlite3_vfs_reg
b8f0: 69 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 ister()].** and
b900: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 [sqlite3_vfs_unr
b910: 65 67 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 egister()] inter
b920: 66 61 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 faces manage thi
b930: 73 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 s list.** in a t
b940: 68 72 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 hread-safe way.
b950: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 The [sqlite3_vf
b960: 73 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 s_find()] interf
b970: 61 63 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 ace.** searches
b980: 74 68 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 the list. Neith
b990: 65 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 er the applicati
b9a0: 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 on code nor the
b9b0: 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 VFS.** implement
b9c0: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 ation should use
b9d0: 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 the pNext point
b9e0: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e er..**.** The pN
b9f0: 65 78 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 ext field is the
ba00: 20 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 only field in t
ba10: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a he sqlite3_vfs.*
ba20: 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 * structure that
ba30: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 SQLite will eve
ba40: 72 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 r modify. SQLit
ba50: 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 e will only acce
ba60: 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 ss.** or modify
ba70: 74 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65 this field while
ba80: 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69 holding a parti
ba90: 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74 cular static mut
baa0: 65 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 ex..** The appli
bab0: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 cation should ne
bac0: 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 ver modify anyth
bad0: 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 73 ing within the s
bae0: 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 qlite3_vfs.** ob
baf0: 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 ject once the ob
bb00: 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20 72 65 ject has been re
bb10: 67 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 gistered..**.**
bb20: 54 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 The zName field
bb30: 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f holds the name o
bb40: 66 20 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 f the VFS module
bb50: 2e 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 . The name must
bb60: 0a 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63 .** be unique ac
bb70: 72 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 ross all VFS mod
bb80: 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 ules..**.** SQLi
bb90: 74 65 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 te will guarante
bba0: 65 20 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65 e that the zFile
bbb0: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74 name parameter t
bbc0: 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 o xOpen.** is ei
bbd0: 74 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e ther a NULL poin
bbe0: 74 65 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 ter or string ob
bbf0: 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 tained.** from x
bc00: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 FullPathname().
bc10: 20 53 51 4c 69 74 65 20 66 75 72 74 68 65 72 20 SQLite further
bc20: 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a guarantees that.
bc30: 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 ** the string wi
bc40: 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20 ll be valid and
bc50: 75 6e 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 unchanged until
bc60: 78 43 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 xClose() is.** c
bc70: 61 6c 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f alled. Because o
bc80: 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 f the previous s
bc90: 65 6e 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 entence,.** the
bca0: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 [sqlite3_file] c
bcb0: 61 6e 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 an safely store
bcc0: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
bcd0: 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 .** filename if
bce0: 69 74 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 it needs to reme
bcf0: 6d 62 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d mber the filenam
bd00: 65 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f e for some reaso
bd10: 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 n..** If the zFi
bd20: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 lename parameter
bd30: 20 69 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e is xOpen is a N
bd40: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e ULL pointer then
bd50: 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 xOpen.** must i
bd60: 6e 76 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 nvent its own te
bd70: 6d 70 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 mporary name for
bd80: 20 74 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e the file. When
bd90: 65 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 ever the .** xFi
bda0: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 lename parameter
bdb0: 20 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c is NULL it will
bdc0: 20 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 also be the cas
bdd0: 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c e that the.** fl
bde0: 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 ags parameter wi
bdf0: 6c 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 ll include [SQLI
be00: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
be10: 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 CLOSE]..**.** Th
be20: 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 e flags argument
be30: 20 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c to xOpen() incl
be40: 75 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65 udes all bits se
be50: 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 t in.** the flag
be60: 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 s argument to [s
be70: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
be80: 5d 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 ]. Or if [sqlit
be90: 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 e3_open()].** or
bea0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 [sqlite3_open16
beb0: 28 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65 ()] is used, the
bec0: 6e 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 n flags includes
bed0: 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 at least.** [SQ
bee0: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
bef0: 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f ITE] | [SQLITE_O
bf00: 50 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a PEN_CREATE]. .**
bf10: 20 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e If xOpen() open
bf20: 73 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e s a file read-on
bf30: 6c 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20 ly then it sets
bf40: 2a 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a *pOutFlags to.**
bf50: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 include [SQLITE
bf60: 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e _OPEN_READONLY].
bf70: 20 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20 Other bits in
bf80: 2a 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62 *pOutFlags may b
bf90: 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c e set..**.** SQL
bfa0: 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 ite will also ad
bfb0: 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c d one of the fol
bfc0: 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 lowing flags to
bfd0: 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 the xOpen().** c
bfe0: 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f all, depending o
bff0: 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69 n the object bei
c000: 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a ng opened:.**.**
c010: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b <ul>.** <li> [
c020: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
c030: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 _DB].** <li> [S
c040: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
c050: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e JOURNAL].** <li>
c060: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 [SQLITE_OPEN_T
c070: 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 EMP_DB].** <li>
c080: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 [SQLITE_OPEN_TE
c090: 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c MP_JOURNAL].** <
c0a0: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 li> [SQLITE_OPE
c0b0: 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a N_TRANSIENT_DB].
c0c0: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 ** <li> [SQLITE
c0d0: 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c _OPEN_SUBJOURNAL
c0e0: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 ].** <li> [SQLI
c0f0: 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a TE_OPEN_MASTER_J
c100: 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e OURNAL].** </ul>
c110: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 .**.** The file
c120: 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 I/O implementati
c130: 6f 6e 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f on can use the o
c140: 62 6a 65 63 74 20 74 79 70 65 20 66 6c 61 67 73 bject type flags
c150: 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 to.** change th
c160: 65 20 77 61 79 20 69 74 20 64 65 61 6c 73 20 77 e way it deals w
c170: 69 74 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 ith files. For
c180: 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c example, an appl
c190: 69 63 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 ication.** that
c1a0: 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 does not care ab
c1b0: 6f 75 74 20 63 72 61 73 68 20 72 65 63 6f 76 65 out crash recove
c1c0: 72 79 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d ry or rollback m
c1d0: 69 67 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 ight make.** the
c1e0: 20 6f 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e open of a journ
c1f0: 61 6c 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e al file a no-op.
c200: 20 20 57 72 69 74 65 73 20 74 6f 20 74 68 69 73 Writes to this
c210: 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a journal would.*
c220: 2a 20 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 * also be no-ops
c230: 2c 20 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 , and any attemp
c240: 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f t to read the jo
c250: 75 72 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 urnal would retu
c260: 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 rn.** SQLITE_IOE
c270: 52 52 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c RR. Or the impl
c280: 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 ementation might
c290: 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 recognize that
c2a0: 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 a database.** fi
c2b0: 6c 65 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 le will be doing
c2c0: 20 70 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 page-aligned se
c2d0: 63 74 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 ctor reads and w
c2e0: 72 69 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f rites in a rando
c2f0: 6d 0a 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 m.** order and s
c300: 65 74 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75 et up its I/O su
c310: 62 73 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e bsystem accordin
c320: 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 gly..**.** SQLit
c330: 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64 e might also add
c340: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
c350: 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 owing flags to t
c360: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a he xOpen method:
c370: 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
c380: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e li> [SQLITE_OPEN
c390: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a _DELETEONCLOSE].
c3a0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f ** <li> [SQLITE_
c3b0: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a OPEN_EXCLUSIVE].
c3c0: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
c3d0: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f he [SQLITE_OPEN_
c3e0: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 DELETEONCLOSE] f
c3f0: 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 lag means the fi
c400: 6c 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 le should be.**
c410: 64 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 deleted when it
c420: 69 73 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 is closed. The
c430: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c [SQLITE_OPEN_DEL
c440: 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 ETEONCLOSE].** w
c450: 69 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54 ill be set for T
c460: 45 4d 50 20 20 64 61 74 61 62 61 73 65 73 2c 20 EMP databases,
c470: 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 journals and for
c480: 20 73 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a subjournals..**
c490: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f .** The [SQLITE_
c4a0: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 OPEN_EXCLUSIVE]
c4b0: 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 75 flag is always u
c4c0: 73 65 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 sed in conjuncti
c4d0: 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b on.** with the [
c4e0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
c4f0: 54 45 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20 TE] flag, which
c500: 61 72 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c are both directl
c510: 79 0a 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 y.** analogous t
c520: 6f 20 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 o the O_EXCL and
c530: 20 4f 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f O_CREAT flags o
c540: 66 20 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e f the POSIX open
c550: 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 ().** API. The
c560: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c SQLITE_OPEN_EXCL
c570: 55 53 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e USIVE flag, when
c580: 20 70 61 69 72 65 64 20 77 69 74 68 20 74 68 65 paired with the
c590: 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e .** SQLITE_OPEN
c5a0: 5f 43 52 45 41 54 45 2c 20 69 73 20 75 73 65 64 _CREATE, is used
c5b0: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 to indicate tha
c5c0: 74 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c t file should al
c5d0: 77 61 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 ways.** be creat
c5e0: 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 ed, and that it
c5f0: 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69 is an error if i
c600: 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 t already exists
c610: 2e 0a 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f ..** It is <i>no
c620: 74 3c 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e t</i> used to in
c630: 64 69 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 dicate the file
c640: 73 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 should be opened
c650: 20 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 .** for exclusi
c660: 76 65 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a ve access..**.**
c670: 20 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 At least szOsFi
c680: 6c 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f le bytes of memo
c690: 72 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 ry are allocated
c6a0: 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f by SQLite.** to
c6b0: 20 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 hold the [sqli
c6c0: 74 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 te3_file] struct
c6d0: 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68 ure passed as th
c6e0: 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d e third.** argum
c6f0: 65 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 ent to xOpen. T
c700: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 he xOpen method
c710: 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f does not have to
c720: 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 .** allocate the
c730: 20 73 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 structure; it s
c740: 68 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 hould just fill
c750: 69 74 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 it in. Note tha
c760: 74 0a 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d t.** the xOpen m
c770: 65 74 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74 ethod must set t
c780: 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e he sqlite3_file.
c790: 70 4d 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68 pMethods to eith
c7a0: 65 72 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 er.** a valid [s
c7b0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
c7c0: 73 5d 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 s] object or to
c7d0: 4e 55 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 NULL. xOpen mus
c7e0: 74 20 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 t do.** this eve
c7f0: 6e 20 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61 n if the open fa
c800: 69 6c 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70 ils. SQLite exp
c810: 65 63 74 73 20 74 68 61 74 20 74 68 65 20 73 71 ects that the sq
c820: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 lite3_file.pMeth
c830: 6f 64 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 ods.** element w
c840: 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 ill be valid aft
c850: 65 72 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 er xOpen returns
c860: 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 regardless of t
c870: 68 65 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 he success.** or
c880: 20 66 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 failure of the
c890: 78 4f 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a xOpen call..**.*
c8a0: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 * The flags argu
c8b0: 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 ment to xAccess(
c8c0: 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 ) may be [SQLITE
c8d0: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a _ACCESS_EXISTS].
c8e0: 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 ** to test for t
c8f0: 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 he existence of
c900: 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 a file, or [SQLI
c910: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
c920: 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 ITE] to.** test
c930: 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 whether a file i
c940: 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 s readable and w
c950: 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c ritable, or [SQL
c960: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d ITE_ACCESS_READ]
c970: 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74 .** to test whet
c980: 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 61 74 her a file is at
c990: 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e least readable.
c9a0: 20 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20 The file can
c9b0: 62 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72 be a.** director
c9c0: 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 y..**.** SQLite
c9d0: 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f will always allo
c9e0: 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78 cate at least mx
c9f0: 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 Pathname+1 bytes
ca00: 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 for the.** outp
ca10: 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50 ut buffer xFullP
ca20: 61 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78 athname. The ex
ca30: 61 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 act size of the
ca40: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a output buffer.**
ca50: 20 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20 is also passed
ca60: 61 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 74 as a parameter t
ca70: 6f 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e o both methods.
ca80: 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 If the output b
ca90: 75 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 uffer.** is not
caa0: 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 large enough, [S
cab0: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 QLITE_CANTOPEN]
cac0: 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e should be return
cad0: 65 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69 ed. Since this i
cae0: 73 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 s.** handled as
caf0: 61 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79 a fatal error by
cb00: 20 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 SQLite, vfs imp
cb10: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f lementations sho
cb20: 75 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 uld endeavor.**
cb30: 74 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 to prevent this
cb40: 62 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74 by setting mxPat
cb50: 68 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69 hname to a suffi
cb60: 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 ciently large va
cb70: 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 lue..**.** The x
cb80: 52 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 Randomness(), xS
cb90: 6c 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72 leep(), and xCur
cba0: 72 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72 rentTime() inter
cbb0: 66 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 faces.** are not
cbc0: 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 strictly a part
cbd0: 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 of the filesyst
cbe0: 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 em, but they are
cbf0: 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 .** included in
cc00: 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72 the VFS structur
cc10: 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 e for completene
cc20: 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 ss..** The xRand
cc30: 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f omness() functio
cc40: 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 n attempts to re
cc50: 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 turn nBytes byte
cc60: 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 s.** of good-qua
cc70: 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 lity randomness
cc80: 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 into zOut. The
cc90: 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a return value is.
cca0: 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 ** the actual nu
ccb0: 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
ccc0: 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 randomness obta
ccd0: 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c ined..** The xSl
cce0: 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 eep() method cau
ccf0: 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 ses the calling
cd00: 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 thread to sleep
cd10: 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 for at.** least
cd20: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 the number of mi
cd30: 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e croseconds given
cd40: 2e 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54 . The xCurrentT
cd50: 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 ime().** method
cd60: 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e returns a Julian
cd70: 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 Day Number for
cd80: 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 the current date
cd90: 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f and time..**.*/
cda0: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
cdb0: 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69 sqlite3_vfs sqli
cdc0: 74 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20 te3_vfs;.struct
cdd0: 73 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20 sqlite3_vfs {.
cde0: 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 int iVersion;
cdf0: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75 /* Stru
ce00: 63 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 cture version nu
ce10: 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a mber */. int sz
ce20: 4f 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 OsFile;
ce30: 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 /* Size of su
ce40: 62 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33 bclassed sqlite3
ce50: 5f 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d _file */. int m
ce60: 78 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 xPathname;
ce70: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 /* Maximum f
ce80: 69 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e ile pathname len
ce90: 67 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 gth */. sqlite3
cea0: 5f 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 _vfs *pNext;
ceb0: 20 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74 /* Next regist
cec0: 65 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f ered VFS */. co
ced0: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b nst char *zName;
cee0: 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
cef0: 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 66 f this virtual f
cf00: 69 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 ile system */.
cf10: 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 void *pAppData;
cf20: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
cf30: 74 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69 ter to applicati
cf40: 6f 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 on-specific data
cf50: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 */. int (*xOpe
cf60: 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c n)(sqlite3_vfs*,
cf70: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
cf80: 6d 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 me, sqlite3_file
cf90: 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 *,.
cfa0: 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 int flags, int
cfb0: 20 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 *pOutFlags);.
cfc0: 69 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73 int (*xDelete)(s
cfd0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e qlite3_vfs*, con
cfe0: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
cff0: 69 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20 int syncDir);.
d000: 69 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28 73 int (*xAccess)(s
d010: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e qlite3_vfs*, con
d020: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
d030: 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a int flags, int *
d040: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 pResOut);. int
d050: 28 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 (*xFullPathname)
d060: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 (sqlite3_vfs*, c
d070: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
d080: 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72 , int nOut, char
d090: 20 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20 *zOut);. void
d0a0: 2a 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69 *(*xDlOpen)(sqli
d0b0: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 te3_vfs*, const
d0c0: 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
d0d0: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72 ;. void (*xDlEr
d0e0: 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 ror)(sqlite3_vfs
d0f0: 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 *, int nByte, ch
d100: 61 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20 ar *zErrMsg);.
d110: 76 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29 void (*(*xDlSym)
d120: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f (sqlite3_vfs*,vo
d130: 69 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 id*, const char
d140: 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 *zSymbol))(void)
d150: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c ;. void (*xDlCl
d160: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 ose)(sqlite3_vfs
d170: 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 *, void*);. int
d180: 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 (*xRandomness)(
d190: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e sqlite3_vfs*, in
d1a0: 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a t nByte, char *z
d1b0: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 Out);. int (*xS
d1c0: 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66 leep)(sqlite3_vf
d1d0: 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 s*, int microsec
d1e0: 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 onds);. int (*x
d1f0: 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c CurrentTime)(sql
d200: 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c ite3_vfs*, doubl
d210: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 e*);. int (*xGe
d220: 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69 tLastError)(sqli
d230: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 te3_vfs*, int, c
d240: 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 har *);. /* New
d250: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 fields may be a
d260: 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72 ppended in figur
d270: 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 e versions. The
d280: 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 iVersion. ** v
d290: 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d alue will increm
d2a0: 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69 ent whenever thi
d2b0: 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b s happens. */.};
d2c0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
d2d0: 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 : Flags for the
d2e0: 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68 xAccess VFS meth
d2f0: 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 od {H11190} <H11
d300: 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 140>.**.** These
d310: 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
d320: 74 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 ts can be used a
d330: 73 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 s the third para
d340: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 meter to.** the
d350: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f xAccess method o
d360: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 f an [sqlite3_vf
d370: 73 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d s] object. {END}
d380: 20 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 They determine
d390: 0a 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 .** what kind of
d3a0: 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 permissions the
d3b0: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 xAccess method
d3c0: 69 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a is looking for..
d3d0: 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 ** With SQLITE_A
d3e0: 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68 CCESS_EXISTS, th
d3f0: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 e xAccess method
d400: 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b .** simply check
d410: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 s whether the fi
d420: 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 le exists..** Wi
d430: 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 th SQLITE_ACCESS
d440: 5f 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 _READWRITE, the
d450: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a xAccess method.*
d460: 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 * checks whether
d470: 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74 the file is bot
d480: 68 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 h readable and w
d490: 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 ritable..** With
d4a0: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
d4b0: 45 41 44 2c 20 74 68 65 20 78 41 63 63 65 73 73 EAD, the xAccess
d4c0: 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b method.** check
d4d0: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 s whether the fi
d4e0: 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a le is readable..
d4f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
d500: 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 E_ACCESS_EXISTS
d510: 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 0.#define SQL
d520: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 ITE_ACCESS_READW
d530: 52 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53 RITE 1.#define S
d540: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
d550: 44 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 D 2../*.**
d560: 43 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61 CAPI3REF: Initia
d570: 6c 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20 lize The SQLite
d580: 4c 69 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d Library {H10130}
d590: 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30 <S20000><S30100
d5a0: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
d5b0: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
d5c0: 20 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c routine initial
d5d0: 69 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 izes the.** SQLi
d5e0: 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 te library. The
d5f0: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
d600: 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 n() routine.** d
d610: 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 eallocates any r
d620: 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65 esources that we
d630: 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 re allocated by
d640: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
d650: 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 ze()..**.** A ca
d660: 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e ll to sqlite3_in
d670: 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 61 6e itialize() is an
d680: 20 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c "effective" cal
d690: 6c 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 74 68 l if it is.** th
d6a0: 65 20 66 69 72 73 74 20 74 69 6d 65 20 73 71 6c e first time sql
d6b0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
d6c0: 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 64 75 72 ) is invoked dur
d6d0: 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 ing the lifetime
d6e0: 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 63 65 of.** the proce
d6f0: 73 73 2c 20 6f 72 20 69 66 20 69 74 20 69 73 20 ss, or if it is
d700: 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 73 the first time 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 69 6e 76 6f 6b 65 64 0a 2a e() is invoked.*
d730: 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 * following a ca
d740: 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 ll to sqlite3_sh
d750: 75 74 64 6f 77 6e 28 29 2e 20 20 4f 6e 6c 79 20 utdown(). Only
d760: 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 6c an effective cal
d770: 6c 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f l.** of sqlite3_
d780: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 6f 65 initialize() doe
d790: 73 20 61 6e 79 20 69 6e 69 74 69 61 6c 69 7a 61 s any initializa
d7a0: 74 69 6f 6e 2e 20 20 41 6c 6c 20 6f 74 68 65 72 tion. All other
d7b0: 20 63 61 6c 6c 73 0a 2a 2a 20 61 72 65 20 68 61 calls.** are ha
d7c0: 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 0a 2a rmless no-ops..*
d7d0: 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 *.** A call to s
d7e0: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
d7f0: 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 69 ) is an "effecti
d800: 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 69 ve" call if it i
d810: 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 s the first.** c
d820: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 all to sqlite3_s
d830: 68 75 74 64 6f 77 6e 28 29 20 73 69 6e 63 65 20 hutdown() since
d840: 74 68 65 20 6c 61 73 74 20 73 71 6c 69 74 65 33 the last sqlite3
d850: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 _initialize().
d860: 4f 6e 6c 79 0a 2a 2a 20 61 6e 20 65 66 66 65 63 Only.** an effec
d870: 74 69 76 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c tive call to sql
d880: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 ite3_shutdown()
d890: 64 6f 65 73 20 61 6e 79 20 64 65 69 6e 69 74 69 does any deiniti
d8a0: 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20 41 6c alization..** Al
d8b0: 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 20 74 6f l other calls to
d8c0: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
d8d0: 6e 28 29 20 61 72 65 20 68 61 72 6d 6c 65 73 73 n() are harmless
d8e0: 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 no-ops..**.** A
d8f0: 6d 6f 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67 mong other thing
d900: 73 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 s, sqlite3_initi
d910: 61 6c 69 7a 65 28 29 20 73 68 61 6c 6c 20 69 6e alize() shall in
d920: 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f voke.** sqlite3_
d930: 6f 73 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69 os_init(). Simi
d940: 6c 61 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f 73 larly, sqlite3_s
d950: 68 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 73 68 61 hutdown().** sha
d960: 6c 6c 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 ll invoke sqlite
d970: 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a 3_os_end()..**.*
d980: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e * The sqlite3_in
d990: 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 itialize() routi
d9a0: 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 ne returns [SQLI
d9b0: 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 TE_OK] on succes
d9c0: 73 2e 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d s..** If for som
d9d0: 65 20 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65 e reason, sqlite
d9e0: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 3_initialize() i
d9f0: 73 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 s unable to init
da00: 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69 ialize.** the li
da10: 62 72 61 72 79 20 28 70 65 72 68 61 70 73 20 69 brary (perhaps i
da20: 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 t is unable to a
da30: 6c 6c 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64 llocate a needed
da40: 20 72 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a resource such.*
da50: 2a 20 61 73 20 61 20 6d 75 74 65 78 29 20 69 74 * as a mutex) it
da60: 20 72 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72 returns an [err
da70: 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74 or code] other t
da80: 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e han [SQLITE_OK].
da90: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
daa0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
dab0: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
dac0: 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 d internally by
dad0: 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51 many other.** SQ
dae0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 Lite interfaces
daf0: 73 6f 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 so that an appli
db00: 63 61 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64 cation usually d
db10: 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a oes not need to.
db20: 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 ** invoke sqlite
db30: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 3_initialize() d
db40: 69 72 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78 irectly. For ex
db50: 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f ample, [sqlite3_
db60: 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73 open()].** calls
db70: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
db80: 69 7a 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c ize() so the SQL
db90: 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c ite library will
dba0: 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c be automaticall
dbb0: 79 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 y.** initialized
dbc0: 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f when [sqlite3_o
dbd0: 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 pen()] is called
dbe0: 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 if it has not b
dbf0: 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a e initialized.**
dc00: 20 61 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76 already. Howev
dc10: 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 er, if SQLite is
dc20: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
dc30: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f he [SQLITE_OMIT_
dc40: 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d AUTOINIT].** com
dc50: 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e pile-time option
dc60: 2c 20 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d , then the autom
dc70: 61 74 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71 atic calls to sq
dc80: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
dc90: 28 29 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65 ().** are omitte
dca0: 64 20 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63 d and the applic
dcb0: 61 74 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20 ation must call
dcc0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
dcd0: 7a 65 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a ze() directly.**
dce0: 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 prior to using
dcf0: 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 any other SQLite
dd00: 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72 interface. For
dd10: 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69 maximum portabi
dd20: 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72 lity,.** it is r
dd30: 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 ecommended that
dd40: 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77 applications alw
dd50: 61 79 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 ays invoke sqlit
dd60: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a e3_initialize().
dd70: 2a 2a 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f ** directly prio
dd80: 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f r to using any o
dd90: 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 ther SQLite inte
dda0: 72 66 61 63 65 2e 20 20 46 75 74 75 72 65 20 72 rface. Future r
ddb0: 65 6c 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51 eleases.** of SQ
ddc0: 4c 69 74 65 20 6d 61 79 20 72 65 71 75 69 72 65 Lite may require
ddd0: 20 74 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72 this. In other
dde0: 20 77 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61 words, the beha
ddf0: 76 69 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a vior exhibited.*
de00: 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 * when SQLite is
de10: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b compiled with [
de20: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
de30: 49 4e 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f INIT] might beco
de40: 6d 65 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c me the.** defaul
de50: 74 20 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f t behavior in so
de60: 6d 65 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 me future releas
de70: 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a e of SQLite..**.
de80: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f ** The sqlite3_o
de90: 73 5f 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65 s_init() routine
dea0: 20 64 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d does operating-
deb0: 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a system specific.
dec0: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ** initializatio
ded0: 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 n of the SQLite
dee0: 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 71 library. The sq
def0: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a lite3_os_end().*
df00: 2a 20 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73 * routine undoes
df10: 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 73 the effect of s
df20: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 qlite3_os_init()
df30: 2e 20 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73 . Typical tasks
df40: 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79 .** performed by
df50: 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
df60: 69 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69 include allocati
df70: 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 on or deallocati
df80: 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20 on.** of static
df90: 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69 resources, initi
dfa0: 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f alization of glo
dfb0: 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a bal variables,.*
dfc0: 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61 20 64 * setting up a d
dfd0: 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f efault [sqlite3_
dfe0: 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 vfs] module, or
dff0: 73 65 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20 setting up.** a
e000: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 default configur
e010: 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c ation using [sql
e020: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a ite3_config()]..
e030: 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 **.** The applic
e040: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76 ation should nev
e050: 65 72 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72 er invoke either
e060: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
e070: 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 ().** or sqlite3
e080: 5f 6f 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74 _os_end() direct
e090: 6c 79 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61 ly. The applica
e0a0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 tion should only
e0b0: 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 invoke.** sqlit
e0c0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 e3_initialize()
e0d0: 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74 and sqlite3_shut
e0e0: 64 6f 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c down(). The sql
e0f0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a ite3_os_init().*
e100: 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 63 * interface is c
e110: 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 alled automatica
e120: 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69 lly by sqlite3_i
e130: 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a nitialize() and.
e140: 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e ** sqlite3_os_en
e150: 64 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 d() is called by
e160: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 sqlite3_shutdow
e170: 6e 28 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74 n(). Appropriat
e180: 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
e190: 69 6f 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33 ions for sqlite3
e1a0: 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 _os_init() and s
e1b0: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a qlite3_os_end().
e1c0: 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74 ** are built int
e1d0: 6f 20 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74 o SQLite when it
e1e0: 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72 is compiled for
e1f0: 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 Unix, Windows,
e200: 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68 65 6e or OS/2..** When
e210: 20 5b 63 75 73 74 6f 6d 20 62 75 69 6c 64 73 20 [custom builds
e220: 7c 20 62 75 69 6c 74 20 66 6f 72 20 6f 74 68 65 | built for othe
e230: 72 20 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a 2a 20 r platforms].**
e240: 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 (using the [SQLI
e250: 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d 20 63 TE_OS_OTHER=1] c
e260: 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6f ompile-time.** o
e270: 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70 6c 69 ption) the appli
e280: 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 cation must supp
e290: 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20 69 6d ly a suitable im
e2a0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 plementation for
e2b0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 .** sqlite3_os_i
e2c0: 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 nit() and sqlite
e2d0: 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41 6e 20 3_os_end(). An
e2e0: 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 70 application-supp
e2f0: 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e lied.** implemen
e300: 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 tation of sqlite
e310: 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72 20 73 3_os_init() or s
e320: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a qlite3_os_end().
e330: 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e 20 5b ** must return [
e340: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 SQLITE_OK] on su
e350: 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65 20 6f ccess and some o
e360: 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f 64 65 ther [error code
e370: 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72 ] upon.** failur
e380: 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
e390: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 int sqlite3_ini
e3a0: 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a 53 tialize(void);.S
e3b0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e3c0: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76 lite3_shutdown(v
e3d0: 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 oid);.SQLITE_API
e3e0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f int sqlite3_os_
e3f0: 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 init(void);.SQLI
e400: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
e410: 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 3b e3_os_end(void);
e420: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
e430: 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20 54 68 : Configuring Th
e440: 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 e SQLite Library
e450: 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 30 30 30 {H14100} <S2000
e460: 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 20 45 58 0><S30200>.** EX
e470: 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
e480: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e The sqlite3_con
e490: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 fig() interface
e4a0: 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 is used to make
e4b0: 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 global configura
e4c0: 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 tion.** changes
e4d0: 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f 72 64 to SQLite in ord
e4e0: 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c 69 74 er to tune SQLit
e4f0: 65 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 e to the specifi
e500: 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a 20 74 68 c needs of.** th
e510: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 e application.
e520: 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 The default conf
e530: 69 67 75 72 61 74 69 6f 6e 20 69 73 20 72 65 63 iguration is rec
e540: 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6d 6f 73 ommended for mos
e550: 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e t.** application
e560: 73 20 61 6e 64 20 73 6f 20 74 68 69 73 20 72 6f s and so this ro
e570: 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 utine is usually
e580: 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 not necessary.
e590: 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 It is.** provid
e5a0: 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 72 61 ed to support ra
e5b0: 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 re applications
e5c0: 77 69 74 68 20 75 6e 75 73 75 61 6c 20 6e 65 65 with unusual nee
e5d0: 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ds..**.** The sq
e5e0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 lite3_config() i
e5f0: 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20 nterface is not
e600: 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 threadsafe. The
e610: 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 application.**
e620: 6d 75 73 74 20 69 6e 73 75 72 65 20 74 68 61 74 must insure that
e630: 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65 no other SQLite
e640: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 interfaces are
e650: 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 68 65 72 invoked by other
e660: 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 68 69 6c .** threads whil
e670: 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 e sqlite3_config
e680: 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 () is running.
e690: 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 73 71 6c Furthermore, sql
e6a0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 0a 2a 2a ite3_config().**
e6b0: 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 69 6e 76 may only be inv
e6c0: 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 6c 69 oked prior to li
e6d0: 62 72 61 72 79 20 69 6e 69 74 69 61 6c 69 7a 61 brary initializa
e6e0: 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 tion using.** [s
e6f0: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
e700: 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 73 68 e()] or after sh
e710: 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 6c 69 74 utdown by [sqlit
e720: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a e3_shutdown()]..
e730: 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 ** Note, however
e740: 2c 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 , that sqlite3_c
e750: 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 65 20 63 onfig() can be c
e760: 61 6c 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66 alled as part of
e770: 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e the.** implemen
e780: 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 61 70 70 tation of an app
e790: 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
e7a0: 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 [sqlite3_os_ini
e7b0: 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 t()]..**.** The
e7c0: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
e7d0: 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 o sqlite3_config
e7e0: 28 29 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 () is an integer
e7f0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 .** [SQLITE_CONF
e800: 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 IG_SINGLETHREAD
e810: 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 | configuration
e820: 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64 65 74 option] that det
e830: 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 74 20 ermines.** what
e840: 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51 4c 69 property of SQLi
e850: 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f 6e 66 te is to be conf
e860: 69 67 75 72 65 64 2e 20 20 53 75 62 73 65 71 75 igured. Subsequ
e870: 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a ent arguments.**
e880: 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e 67 20 vary depending
e890: 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 on the [SQLITE_C
e8a0: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 ONFIG_SINGLETHRE
e8b0: 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 AD | configurati
e8c0: 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20 69 6e on option].** in
e8d0: 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
e8e0: 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ent..**.** When
e8f0: 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 a configuration
e900: 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2c 20 73 option is set, s
e910: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
e920: 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
e930: 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f OK]..** If the o
e940: 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f 77 6e ption is unknown
e950: 20 6f 72 20 53 51 4c 69 74 65 20 69 73 20 75 6e or SQLite is un
e960: 61 62 6c 65 20 74 6f 20 73 65 74 20 74 68 65 20 able to set the
e970: 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 74 option.** then t
e980: 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
e990: 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 5b rns a non-zero [
e9a0: 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a error code]..**.
e9b0: 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
e9c0: 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 5b 48 31 .** [H14103] [H1
e9d0: 34 31 30 36 5d 20 5b 48 31 34 31 32 30 5d 20 5b 4106] [H14120] [
e9e0: 48 31 34 31 32 33 5d 20 5b 48 31 34 31 32 36 5d H14123] [H14126]
e9f0: 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 34 31 33 [H14129] [H1413
ea00: 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 2a 20 5b 2] [H14135].** [
ea10: 48 31 34 31 33 38 5d 20 5b 48 31 34 31 34 31 5d H14138] [H14141]
ea20: 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 34 31 34 [H14144] [H1414
ea30: 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 48 31 34 7] [H14150] [H14
ea40: 31 35 33 5d 20 5b 48 31 34 31 35 36 5d 20 5b 48 153] [H14156] [H
ea50: 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 34 31 36 14159].** [H1416
ea60: 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 48 31 34 2] [H14165] [H14
ea70: 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 168].*/.SQLITE_A
ea80: 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 PI SQLITE_EXPERI
ea90: 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 MENTAL int sqlit
eaa0: 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e e3_config(int, .
eab0: 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ..);../*.** CAPI
eac0: 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20 3REF: Configure
ead0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
eae0: 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 7d 20 3c ions {H14200} <
eaf0: 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S20000>.** EXPER
eb00: 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
eb10: 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e e sqlite3_db_con
eb20: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 fig() interface
eb30: 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 is used to make
eb40: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a configuration.**
eb50: 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64 changes to a [d
eb60: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
eb70: 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66 on]. The interf
eb80: 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 ace is similar t
eb90: 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f o.** [sqlite3_co
eba0: 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74 20 74 nfig()] except t
ebb0: 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 hat the changes
ebc0: 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e 67 6c apply to a singl
ebd0: 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 e.** [database c
ebe0: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70 65 63 onnection] (spec
ebf0: 69 66 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 ified in the fir
ec00: 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 20 20 54 st argument). T
ec10: 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 64 62 he.** sqlite3_db
ec20: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 _config() interf
ec30: 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 ace can only be
ec40: 75 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 used immediately
ec50: 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 64 61 after.** the da
ec60: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
ec70: 6e 20 69 73 20 63 72 65 61 74 65 64 20 75 73 69 n is created usi
ec80: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e ng [sqlite3_open
ec90: 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ()],.** [sqlite3
eca0: 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72 20 5b _open16()], or [
ecb0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
ecc0: 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 )]. .**.** The
ecd0: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
ece0: 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f to sqlite3_db_co
ecf0: 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69 nfig(D,V,...) i
ed00: 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 67 75 s the.** configu
ed10: 72 61 74 69 6f 6e 20 76 65 72 62 20 2d 20 61 6e ration verb - an
ed20: 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 integer code th
ed30: 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 68 61 at indicates wha
ed40: 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f 66 20 74 t.** aspect of t
ed50: 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
ed60: 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e nection] is bein
ed70: 67 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a g configured..**
ed80: 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f 69 63 65 The only choice
ed90: 20 66 6f 72 20 74 68 69 73 20 76 61 6c 75 65 20 for this value
eda0: 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e is [SQLITE_DBCON
edb0: 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a FIG_LOOKASIDE]..
edc0: 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 61 72 65 ** New verbs are
edd0: 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 64 likely to be ad
ede0: 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 ded in future re
edf0: 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
ee00: 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 ..** Additional
ee10: 61 72 67 75 6d 65 6e 74 73 20 64 65 70 65 6e 64 arguments depend
ee20: 20 6f 6e 20 74 68 65 20 76 65 72 62 2e 0a 2a 2a on the verb..**
ee30: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
ee40: 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d 20 5b 48 :.** [H14203] [H
ee50: 31 34 32 30 36 5d 20 5b 48 31 34 32 30 39 5d 20 14206] [H14209]
ee60: 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 32 31 35 [H14212] [H14215
ee70: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
ee80: 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
ee90: 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TAL int sqlite3_
eea0: 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 db_config(sqlite
eeb0: 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3*, int op, ...)
eec0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
eed0: 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 F: Memory Alloca
eee0: 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 tion Routines {H
eef0: 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30 3e 0a 10155} <S20120>.
ef00: 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
ef10: 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 **.** An instanc
ef20: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
ef30: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
ef40: 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 53 erface between S
ef50: 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77 QLite.** and low
ef60: 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
ef70: 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 location routine
ef80: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 s..**.** This ob
ef90: 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 6e 20 ject is used in
efa0: 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69 only one place i
efb0: 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74 n the SQLite int
efc0: 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69 erface..** A poi
efd0: 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
efe0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
eff0: 63 74 20 69 73 20 74 68 65 20 61 72 67 75 6d 65 ct is the argume
f000: 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 nt to.** [sqlite
f010: 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e 3_config()] when
f020: 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 the configurati
f030: 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 on option is.**
f040: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
f050: 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54 ALLOC] or [SQLIT
f060: 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c E_CONFIG_GETMALL
f070: 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 72 65 OC]. .** By cre
f080: 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63 ating an instanc
f090: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
f0a0: 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20 .** and passing
f0b0: 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 it to [sqlite3_c
f0c0: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 onfig]([SQLITE_C
f0d0: 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 0a 2a ONFIG_MALLOC]).*
f0e0: 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66 69 67 75 * during configu
f0f0: 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70 70 6c 69 ration, an appli
f100: 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69 cation can speci
f110: 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 fy an alternativ
f120: 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f e.** memory allo
f130: 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
f140: 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75 for SQLite to u
f150: 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 se for all of it
f160: 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 65 6d s.** dynamic mem
f170: 6f 72 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a ory needs..**.**
f180: 20 4e 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74 Note that SQLit
f190: 65 20 63 6f 6d 65 73 20 77 69 74 68 20 73 65 76 e comes with sev
f1a0: 65 72 61 6c 20 5b 62 75 69 6c 74 2d 69 6e 20 6d eral [built-in m
f1b0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73 emory allocators
f1c0: 5d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 70 65 ].** that are pe
f1d0: 72 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65 rfectly adequate
f1e0: 20 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65 for the overwhe
f1f0: 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f lming majority o
f200: 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a f applications.*
f210: 2a 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 * and that this
f220: 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75 object is only u
f230: 73 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20 seful to a tiny
f240: 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c minority of appl
f250: 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68 ications.** with
f260: 20 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d specialized mem
f270: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
f280: 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68 equirements. Th
f290: 69 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 is object is.**
f2a0: 61 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67 also used during
f2b0: 20 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69 testing of SQLi
f2c0: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 te in order to s
f2d0: 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e pecify an altern
f2e0: 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 ative.** memory
f2f0: 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73 allocator that s
f300: 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20 imulates memory
f310: 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f out-of-memory co
f320: 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f nditions in.** o
f330: 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 rder to verify t
f340: 68 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76 hat SQLite recov
f350: 65 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66 ers gracefully f
f360: 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64 rom such.** cond
f370: 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 itions..**.** Th
f380: 65 20 78 4d 61 6c 6c 6f 63 20 61 6e 64 20 78 46 e xMalloc and xF
f390: 72 65 65 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 ree methods must
f3a0: 20 77 6f 72 6b 20 6c 69 6b 65 20 74 68 65 0a 2a work like the.*
f3b0: 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 * malloc() and f
f3c0: 72 65 65 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 ree() functions
f3d0: 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 from the standar
f3e0: 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 d C library..**
f3f0: 54 68 65 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 The xRealloc met
f400: 68 6f 64 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69 hod must work li
f410: 6b 65 20 72 65 61 6c 6c 6f 63 28 29 20 66 72 6f ke realloc() fro
f420: 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 m the standard C
f430: 20 6c 69 62 72 61 72 79 0a 2a 2a 20 77 69 74 68 library.** with
f440: 20 74 68 65 20 65 78 63 65 70 74 69 6f 6e 20 74 the exception t
f450: 68 61 74 20 69 66 20 74 68 65 20 73 65 63 6f 6e hat if the secon
f460: 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 d argument to xR
f470: 65 61 6c 6c 6f 63 20 69 73 20 7a 65 72 6f 2c 0a ealloc is zero,.
f480: 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 6d 75 73 74 ** xRealloc must
f490: 20 62 65 20 61 20 6e 6f 2d 6f 70 20 2d 20 69 74 be a no-op - it
f4a0: 20 6d 75 73 74 20 6e 6f 74 20 70 65 72 66 6f 72 must not perfor
f4b0: 6d 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e m any allocation
f4c0: 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 or.** deallocat
f4d0: 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 67 75 61 ion. SQLite gua
f4e0: 72 61 6e 74 65 65 64 73 20 74 68 61 74 20 74 68 ranteeds that th
f4f0: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
f500: 74 20 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 t to.** xRealloc
f510: 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c is always a val
f520: 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 ue returned by a
f530: 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78 prior call to x
f540: 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20 41 6e 64 20 Roundup..** And
f550: 73 6f 20 69 6e 20 63 61 73 65 73 20 77 68 65 72 so in cases wher
f560: 65 20 78 52 6f 75 6e 64 75 70 20 61 6c 77 61 79 e xRoundup alway
f570: 73 20 72 65 74 75 72 6e 73 20 61 20 70 6f 73 69 s returns a posi
f580: 74 69 76 65 20 6e 75 6d 62 65 72 2c 0a 2a 2a 20 tive number,.**
f590: 78 52 65 61 6c 6c 6f 63 20 63 61 6e 20 70 65 72 xRealloc can per
f5a0: 66 6f 72 6d 20 65 78 61 63 74 6c 79 20 61 73 20 form exactly as
f5b0: 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 the standard lib
f5c0: 72 61 72 79 20 72 65 61 6c 6c 6f 63 28 29 20 61 rary realloc() a
f5d0: 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20 62 65 20 69 nd.** still be i
f5e0: 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 20 77 69 74 n compliance wit
f5f0: 68 20 74 68 69 73 20 73 70 65 63 69 66 69 63 61 h this specifica
f600: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 78 53 69 7a tion..**.** xSiz
f610: 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 e should return
f620: 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 the allocated si
f630: 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 ze of a memory a
f640: 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 65 llocation.** pre
f650: 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 viously obtained
f660: 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 6f 72 from xMalloc or
f670: 20 78 52 65 61 6c 6c 6f 63 2e 20 20 54 68 65 20 xRealloc. The
f680: 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 0a 2a allocated size.*
f690: 2a 20 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c * is always at l
f6a0: 65 61 73 74 20 61 73 20 62 69 67 20 61 73 20 74 east as big as t
f6b0: 68 65 20 72 65 71 75 65 73 74 65 64 20 73 69 7a he requested siz
f6c0: 65 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72 e but may be lar
f6d0: 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ger..**.** The x
f6e0: 52 6f 75 6e 64 75 70 20 6d 65 74 68 6f 64 20 72 Roundup method r
f6f0: 65 74 75 72 6e 73 20 77 68 61 74 20 77 6f 75 6c eturns what woul
f700: 64 20 62 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 d be the allocat
f710: 65 64 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 61 20 ed size of.** a
f720: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
f730: 6e 20 67 69 76 65 6e 20 61 20 70 61 72 74 69 63 n given a partic
f740: 75 6c 61 72 20 72 65 71 75 65 73 74 65 64 20 73 ular requested s
f750: 69 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72 ize. Most memor
f760: 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20 y.** allocators
f770: 72 6f 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20 round up memory
f780: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c allocations at l
f790: 65 61 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74 east to the next
f7a0: 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f 66 20 multiple.** of
f7b0: 38 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74 8. Some allocat
f7c0: 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20 ors round up to
f7d0: 61 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c a larger multipl
f7e0: 65 20 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20 e or to a power
f7f0: 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 72 79 20 6d of 2..** Every m
f800: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
f810: 20 72 65 71 75 65 73 74 20 63 6f 6d 69 6e 67 20 request coming
f820: 69 6e 20 74 68 72 6f 75 67 68 20 5b 73 71 6c 69 in through [sqli
f830: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a te3_malloc()].**
f840: 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 or [sqlite3_rea
f850: 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74 20 63 61 lloc()] first ca
f860: 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e 20 20 49 lls xRoundup. I
f870: 66 20 78 52 6f 75 6e 64 75 70 20 72 65 74 75 72 f xRoundup retur
f880: 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 61 74 20 63 ns 0, .** that c
f890: 61 75 73 65 73 20 74 68 65 20 63 6f 72 72 65 73 auses the corres
f8a0: 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 ponding memory a
f8b0: 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 66 61 69 llocation to fai
f8c0: 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e l..**.** The xIn
f8d0: 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61 it method initia
f8e0: 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 lizes the memory
f8f0: 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f allocator. (Fo
f900: 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74 r example,.** it
f910: 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 might allocate
f920: 61 6e 79 20 72 65 71 75 69 72 65 20 6d 75 74 65 any require mute
f930: 78 65 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a xes or initializ
f940: 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a e internal data.
f950: 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 20 ** structures.
f960: 54 68 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 The xShutdown me
f970: 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20 thod is invoked
f980: 28 69 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a (indirectly) by.
f990: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 ** [sqlite3_shut
f9a0: 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75 down()] and shou
f9b0: 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e ld deallocate an
f9c0: 79 20 72 65 73 6f 75 72 63 65 73 20 61 63 71 75 y resources acqu
f9d0: 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74 ired.** by xInit
f9e0: 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 20 . The pAppData
f9f0: 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 pointer is used
fa00: 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61 as the only para
fa10: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69 meter to.** xIni
fa20: 74 20 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e t and xShutdown.
fa30: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f .**.** SQLite ho
fa40: 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f lds the [SQLITE_
fa50: 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
fa60: 54 45 52 5d 20 6d 75 74 65 78 20 77 68 65 6e 20 TER] mutex when
fa70: 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68 it invokes.** th
fa80: 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 2c 20 e xInit method,
fa90: 73 6f 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 so the xInit met
faa0: 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 hod need not be
fab0: 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 threadsafe. The
fac0: 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 .** xShutdown me
fad0: 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c thod is only cal
fae0: 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 led from [sqlite
faf0: 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f 3_shutdown()] so
fb00: 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
fb10: 6e 65 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 need to be threa
fb20: 64 73 61 66 65 20 65 69 74 68 65 72 2e 20 20 46 dsafe either. F
fb30: 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 or all other met
fb40: 68 6f 64 73 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 hods, SQLite.**
fb50: 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 holds the [SQLIT
fb60: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
fb70: 45 4d 5d 20 6d 75 74 65 78 20 61 73 20 6c 6f 6e EM] mutex as lon
fb80: 67 20 61 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c g as the.** [SQL
fb90: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 ITE_CONFIG_MEMST
fba0: 41 54 55 53 5d 20 63 6f 6e 66 69 67 75 72 61 74 ATUS] configurat
fbb0: 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 74 75 ion option is tu
fbc0: 72 6e 65 64 20 6f 6e 20 28 77 68 69 63 68 0a 2a rned on (which.*
fbd0: 2a 20 69 74 20 69 73 20 62 79 20 64 65 66 61 75 * it is by defau
fbe0: 6c 74 29 20 61 6e 64 20 73 6f 20 74 68 65 20 6d lt) and so the m
fbf0: 65 74 68 6f 64 73 20 61 72 65 20 61 75 74 6f 6d ethods are autom
fc00: 61 74 69 63 61 6c 6c 79 20 73 65 72 69 61 6c 69 atically seriali
fc10: 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c zed..** However,
fc20: 20 69 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 if [SQLITE_CONF
fc30: 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20 69 73 IG_MEMSTATUS] is
fc40: 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 disabled, then
fc50: 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 74 the other.** met
fc60: 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74 68 72 hods must be thr
fc70: 65 61 64 73 61 66 65 20 6f 72 20 65 6c 73 65 20 eadsafe or else
fc80: 6d 61 6b 65 20 74 68 65 69 72 20 6f 77 6e 20 61 make their own a
fc90: 72 72 61 6e 67 65 6d 65 6e 74 73 20 66 6f 72 0a rrangements for.
fca0: 2a 2a 20 73 65 72 69 61 6c 69 7a 61 74 69 6f 6e ** serialization
fcb0: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 ..**.** SQLite w
fcc0: 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 ill never invoke
fcd0: 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 xInit() more th
fce0: 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 an once without
fcf0: 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a an intervening.*
fd00: 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 * call to xShutd
fd10: 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 own()..*/.typede
fd20: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
fd30: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 71 6c _mem_methods sql
fd40: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
fd50: 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 ;.struct sqlite3
fd60: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 _mem_methods {.
fd70: 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c 6f 63 void *(*xMalloc
fd80: 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20 20 20 )(int);
fd90: 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 /* Memory alloca
fda0: 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f tion function */
fdb0: 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 29 . void (*xFree)
fdc0: 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 (void*);
fdd0: 20 20 2f 2a 20 46 72 65 65 20 61 20 70 72 69 6f /* Free a prio
fde0: 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a r allocation */.
fdf0: 20 20 76 6f 69 64 20 2a 28 2a 78 52 65 61 6c 6c void *(*xReall
fe00: 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 oc)(void*,int);
fe10: 20 2f 2a 20 52 65 73 69 7a 65 20 61 6e 20 61 6c /* Resize an al
fe20: 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e location */. in
fe30: 74 20 28 2a 78 53 69 7a 65 29 28 76 6f 69 64 2a t (*xSize)(void*
fe40: 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ); /*
fe50: 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
fe60: 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e of an allocation
fe70: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 52 6f 75 */. int (*xRou
fe80: 6e 64 75 70 29 28 69 6e 74 29 3b 20 20 20 20 20 ndup)(int);
fe90: 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 20 75 70 /* Round up
fea0: 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f request size to
feb0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
fec0: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 */. int (*xIni
fed0: 74 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 t)(void*);
fee0: 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 /* Initiali
fef0: 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
ff00: 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 locator */. voi
ff10: 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 d (*xShutdown)(v
ff20: 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 oid*); /* D
ff30: 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 einitialize the
ff40: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
ff50: 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 */. void *pApp
ff60: 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
ff70: 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 /* Argument
ff80: 20 74 6f 20 78 49 6e 69 74 28 29 20 61 6e 64 20 to xInit() and
ff90: 78 53 68 75 74 64 6f 77 6e 28 29 20 2a 2f 0a 7d xShutdown() */.}
ffa0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
ffb0: 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e F: Configuration
ffc0: 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 36 30 Options {H10160
ffd0: 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 } <S20000>.** EX
ffe0: 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
fff0: 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 These constants
10000 20 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 are the availab
10010 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 le integer confi
10020 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 guration options
10030 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 that.** can be
10040 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
10050 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
10060 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e the [sqlite3_con
10070 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 fig()] interface
10080 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 ..**.** New conf
10090 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
100a0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 s may be added i
100b0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
100c0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 s of SQLite..**
100d0 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 Existing configu
100e0 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d ration options m
100f0 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 ight be disconti
10100 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 nued. Applicati
10110 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 ons.** should ch
10120 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 eck the return c
10130 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 ode from [sqlite
10140 33 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d 3_config()] to m
10150 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a ake sure that.**
10160 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 the call worked
10170 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f . The [sqlite3_
10180 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 config()] interf
10190 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ace will return
101a0 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 a.** non-zero [e
101b0 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20 rror code] if a
101c0 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 discontinued or
101d0 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 unsupported conf
101e0 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
101f0 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a .** is invoked..
10200 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 **.** <dl>.** <d
10210 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
10220 53 49 4e 47 4c 45 54 48 52 45 41 44 3c 2f 64 74 SINGLETHREAD</dt
10230 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 >.** <dd>There a
10240 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 re no arguments
10250 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 to this option.
10260 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 This option dis
10270 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 ables.** all mut
10280 65 78 69 6e 67 20 61 6e 64 20 70 75 74 73 20 53 exing and puts S
10290 51 4c 69 74 65 20 69 6e 74 6f 20 61 20 6d 6f 64 QLite into a mod
102a0 65 20 77 68 65 72 65 20 69 74 20 63 61 6e 20 6f e where it can o
102b0 6e 6c 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 62 nly be used.** b
102c0 79 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 y a single threa
102d0 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 d.</dd>.**.** <d
102e0 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t>SQLITE_CONFIG_
102f0 4d 55 4c 54 49 54 48 52 45 41 44 3c 2f 64 74 3e MULTITHREAD</dt>
10300 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 .** <dd>There ar
10310 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 e no arguments t
10320 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 o this option.
10330 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 61 This option disa
10340 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 78 69 6e 67 bles.** mutexing
10350 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f on [database co
10360 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 nnection] and [p
10370 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
10380 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 t] objects..** T
10390 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 he application i
103a0 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
103b0 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 63 r serializing ac
103c0 63 65 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 74 61 cess to.** [data
103d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
103e0 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 ] and [prepared
103f0 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20 42 75 statements]. Bu
10400 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65 73 0a t other mutexes.
10410 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 73 ** are enabled s
10420 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 o that SQLite wi
10430 6c 6c 20 62 65 20 73 61 66 65 20 74 6f 20 75 73 ll be safe to us
10440 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72 e in a multi-thr
10450 65 61 64 65 64 0a 2a 2a 20 65 6e 76 69 72 6f 6e eaded.** environ
10460 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 ment as long as
10470 6e 6f 20 74 77 6f 20 74 68 72 65 61 64 73 20 61 no two threads a
10480 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 ttempt to use th
10490 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 61 62 e same.** [datab
104a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
104b0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
104c0 2e 20 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 . See the [thre
104d0 61 64 69 6e 67 20 6d 6f 64 65 5d 0a 2a 2a 20 64 ading mode].** d
104e0 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 ocumentation for
104f0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
10500 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a rmation.</dd>.**
10510 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
10520 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 ONFIG_SERIALIZED
10530 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 </dt>.** <dd>The
10540 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 re are no argume
10550 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 nts to this opti
10560 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e on. This option
10570 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 enables.** all
10580 6d 75 74 65 78 65 73 20 69 6e 63 6c 75 64 69 6e mutexes includin
10590 67 20 74 68 65 20 72 65 63 75 72 73 69 76 65 0a g the recursive.
105a0 2a 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b 64 ** mutexes on [d
105b0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
105c0 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 on] and [prepare
105d0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a d statement] obj
105e0 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 ects..** In this
105f0 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69 73 20 mode (which is
10600 74 68 65 20 64 65 66 61 75 6c 74 20 77 68 65 6e the default when
10610 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
10620 6c 65 64 20 77 69 74 68 0a 2a 2a 20 5b 53 51 4c led with.** [SQL
10630 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31 ITE_THREADSAFE=1
10640 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 ]) the SQLite li
10650 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73 65 6c brary will itsel
10660 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65 f serialize acce
10670 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 62 61 ss.** to [databa
10680 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 se connections]
10690 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 and [prepared st
106a0 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74 68 61 atements] so tha
106b0 74 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 t the.** applica
106c0 74 69 6f 6e 20 69 73 20 66 72 65 65 20 74 6f 20 tion is free to
106d0 75 73 65 20 74 68 65 20 73 61 6d 65 20 5b 64 61 use the same [da
106e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
106f0 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d n] or the.** sam
10700 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 e [prepared stat
10710 65 6d 65 6e 74 5d 20 69 6e 20 64 69 66 66 65 72 ement] in differ
10720 65 6e 74 20 74 68 72 65 61 64 73 20 61 74 20 74 ent threads at t
10730 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a he same time..**
10740 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64 See the [thread
10750 69 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 ing mode] docume
10760 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 ntation for addi
10770 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
10780 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c on.</dd>.**.** <
10790 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
107a0 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 _MALLOC</dt>.**
107b0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
107c0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
107d0 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
107e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
107f0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
10800 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d the [sqlite3_mem
10810 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 _methods] struct
10820 75 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65 ure. The argume
10830 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 nt specifies.**
10840 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d alternative low-
10850 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
10860 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 ocation routines
10870 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 to be used in p
10880 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6d lace of.** the m
10890 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
108a0 20 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 routines built
108b0 69 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 into SQLite.</dd
108c0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
108d0 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c TE_CONFIG_GETMAL
108e0 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e LOC</dt>.** <dd>
108f0 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 This option take
10900 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d s a single argum
10910 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70 ent which is a p
10920 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 ointer to an.**
10930 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
10940 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 [sqlite3_mem_met
10950 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e hods] structure.
10960 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d The [sqlite3_m
10970 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73 em_methods].** s
10980 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c tructure is fill
10990 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72 72 ed with the curr
109a0 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 65 ently defined me
109b0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
109c0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 routines..** Thi
109d0 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 s option can be
109e0 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 used to overload
109f0 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d the default mem
10a00 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a ory allocation.*
10a10 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20 * routines with
10a20 61 20 77 72 61 70 70 65 72 20 74 68 61 74 20 73 a wrapper that s
10a30 69 6d 75 6c 61 74 69 6f 6e 73 20 6d 65 6d 6f 72 imulations memor
10a40 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 y allocation fai
10a50 6c 75 72 65 20 6f 72 0a 2a 2a 20 74 72 61 63 6b lure or.** track
10a60 73 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 2c 20 s memory usage,
10a70 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64 for example.</dd
10a80 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
10a90 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 TE_CONFIG_MEMSTA
10aa0 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TUS</dt>.** <dd>
10ab0 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 This option take
10ac0 73 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e s single argumen
10ad0 74 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69 t of type int, i
10ae0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20 nterpreted as a
10af0 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77 68 69 .** boolean, whi
10b00 63 68 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 ch enables or di
10b10 73 61 62 6c 65 73 20 74 68 65 20 63 6f 6c 6c 65 sables the colle
10b20 63 74 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72 79 20 ction of memory
10b30 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 73 allocation .** s
10b40 74 61 74 69 73 74 69 63 73 2e 20 57 68 65 6e 20 tatistics. When
10b50 64 69 73 61 62 6c 65 64 2c 20 74 68 65 20 66 6f disabled, the fo
10b60 6c 6c 6f 77 69 6e 67 20 53 51 4c 69 74 65 20 69 llowing SQLite i
10b70 6e 74 65 72 66 61 63 65 73 20 62 65 63 6f 6d 65 nterfaces become
10b80 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65 72 61 74 69 .** non-operati
10b90 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c 75 6c 3e 0a onal:.** <ul>.
10ba0 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 ** <li> [sqlit
10bb0 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 e3_memory_used()
10bc0 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c ].** <li> [sql
10bd0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 ite3_memory_high
10be0 77 61 74 65 72 28 29 5d 0a 2a 2a 20 20 20 3c 6c water()].** <l
10bf0 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74 i> [sqlite3_soft
10c00 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 5d 0a 2a _heap_limit()].*
10c10 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 * <li> [sqlite
10c20 33 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2a 20 20 3_status()].**
10c30 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f 64 64 3e 0a </ul>.** </dd>.
10c40 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
10c50 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3c _CONFIG_SCRATCH<
10c60 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
10c70 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65 option specifie
10c80 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72 s a static memor
10c90 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51 y buffer that SQ
10ca0 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72 Lite can use for
10cb0 0a 2a 2a 20 73 63 72 61 74 63 68 20 6d 65 6d 6f .** scratch memo
10cc0 72 79 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 ry. There are t
10cd0 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 hree arguments:
10ce0 20 41 20 70 6f 69 6e 74 65 72 20 61 6e 20 38 2d A pointer an 8-
10cf0 62 79 74 65 0a 2a 2a 20 61 6c 69 67 6e 65 64 20 byte.** aligned
10d00 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 66 72 memory buffer fr
10d10 6f 6d 20 77 68 69 63 68 20 74 68 65 20 73 63 72 om which the scr
10d20 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 ach allocations
10d30 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 72 61 77 6e will be.** drawn
10d40 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 , the size of ea
10d50 63 68 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 ch scratch alloc
10d60 61 74 69 6f 6e 20 28 73 7a 29 2c 0a 2a 2a 20 61 ation (sz),.** a
10d70 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e nd the maximum n
10d80 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 umber of scratch
10d90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 28 4e 29 allocations (N)
10da0 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 61 72 67 . The sz.** arg
10db0 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 61 20 ument must be a
10dc0 6d 75 6c 74 69 70 6c 65 20 6f 66 20 31 36 2e 20 multiple of 16.
10dd0 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72 The sz parameter
10de0 20 73 68 6f 75 6c 64 20 62 65 20 61 20 66 65 77 should be a few
10df0 20 62 79 74 65 73 0a 2a 2a 20 6c 61 72 67 65 72 bytes.** larger
10e00 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c than the actual
10e10 20 73 63 72 61 74 63 68 20 73 70 61 63 65 20 72 scratch space r
10e20 65 71 75 69 72 65 64 20 64 75 65 20 74 6f 20 69 equired due to i
10e30 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 65 61 64 nternal overhead
10e40 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 ..** The first a
10e50 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 rgument should p
10e60 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 38 2d 62 ointer to an 8-b
10e70 79 74 65 20 61 6c 69 67 6e 65 64 20 62 75 66 66 yte aligned buff
10e80 65 72 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 73 er.** of at leas
10e90 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 t sz*N bytes of
10ea0 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 memory..** SQLit
10eb0 65 20 77 69 6c 6c 20 75 73 65 20 6e 6f 20 6d 6f e will use no mo
10ec0 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 re than one scra
10ed0 74 63 68 20 62 75 66 66 65 72 20 61 74 20 6f 6e tch buffer at on
10ee0 63 65 20 70 65 72 20 74 68 72 65 61 64 2c 20 73 ce per thread, s
10ef0 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 62 65 o.** N should be
10f00 20 73 65 74 20 74 6f 20 74 68 65 20 65 78 70 65 set to the expe
10f10 63 74 65 64 20 6d 61 78 69 6d 75 6d 20 6e 75 6d cted maximum num
10f20 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 2e 20 ber of threads.
10f30 20 54 68 65 20 73 7a 0a 2a 2a 20 70 61 72 61 6d The sz.** param
10f40 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 36 eter should be 6
10f50 20 74 69 6d 65 73 20 74 68 65 20 73 69 7a 65 20 times the size
10f60 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 of the largest d
10f70 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a atabase page siz
10f80 65 2e 0a 2a 2a 20 53 63 72 61 74 63 68 20 62 75 e..** Scratch bu
10f90 66 66 65 72 73 20 61 72 65 20 75 73 65 64 20 61 ffers are used a
10fa0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 62 74 s part of the bt
10fb0 72 65 65 20 62 61 6c 61 6e 63 65 20 6f 70 65 72 ree balance oper
10fc0 61 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 54 68 ation. If.** Th
10fd0 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 72 e btree balancer
10fe0 20 6e 65 65 64 73 20 61 64 64 69 74 69 6f 6e 61 needs additiona
10ff0 6c 20 6d 65 6d 6f 72 79 20 62 65 79 6f 6e 64 20 l memory beyond
11000 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 what is provided
11010 20 62 79 0a 2a 2a 20 73 63 72 61 74 63 68 20 62 by.** scratch b
11020 75 66 66 65 72 73 20 6f 72 20 69 66 20 6e 6f 20 uffers or if no
11030 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 73 scratch buffer s
11040 70 61 63 65 20 69 73 20 73 70 65 63 69 66 69 65 pace is specifie
11050 64 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a d, then SQLite.*
11060 2a 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 * goes to [sqlit
11070 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 74 6f 20 e3_malloc()] to
11080 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 obtain the memor
11090 79 20 69 74 20 6e 65 65 64 73 2e 3c 2f 64 64 3e y it needs.</dd>
110a0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
110b0 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
110c0 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 HE</dt>.** <dd>T
110d0 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 his option speci
110e0 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 fies a static me
110f0 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61 74 mory buffer that
11100 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20 SQLite can use
11110 66 6f 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 for.** the datab
11120 61 73 65 20 70 61 67 65 20 63 61 63 68 65 20 77 ase page cache w
11130 69 74 68 20 74 68 65 20 64 65 66 61 75 6c 74 20 ith the default
11140 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 page cache imple
11150 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54 menation. .** T
11160 68 69 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f his configuratio
11170 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 n should not be
11180 75 73 65 64 20 69 66 20 61 6e 20 61 70 70 6c 69 used if an appli
11190 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 20 70 61 cation-define pa
111a0 67 65 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c ge.** cache impl
111b0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f ementation is lo
111c0 61 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 53 aded using the S
111d0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 QLITE_CONFIG_PCA
111e0 43 48 45 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54 CHE option..** T
111f0 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61 here are three a
11200 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 rguments to this
11210 20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74 option: A point
11220 65 72 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 er to 8-byte ali
11230 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 gned.** memory,
11240 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68 the size of each
11250 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a page buffer (sz
11260 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 ), and the numbe
11270 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a r of pages (N)..
11280 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65 ** The sz argume
11290 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 nt should be the
112a0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 size of the lar
112b0 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 61 gest database pa
112c0 67 65 0a 2a 2a 20 28 61 20 70 6f 77 65 72 20 6f ge.** (a power o
112d0 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 f two between 51
112e0 32 20 61 6e 64 20 33 32 37 36 38 29 20 70 6c 75 2 and 32768) plu
112f0 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61 s a little extra
11300 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 70 61 67 for each.** pag
11310 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 70 e header. The p
11320 61 67 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 age header size
11330 69 73 20 32 30 20 74 6f 20 34 30 20 62 79 74 65 is 20 to 40 byte
11340 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 0a 2a s depending on.*
11350 2a 20 74 68 65 20 68 6f 73 74 20 61 72 63 68 69 * the host archi
11360 74 65 63 74 75 72 65 2e 20 20 49 74 20 69 73 20 tecture. It is
11370 68 61 72 6d 6c 65 73 73 2c 20 61 70 61 72 74 20 harmless, apart
11380 66 72 6f 6d 20 74 68 65 20 77 61 73 74 65 64 20 from the wasted
11390 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20 6d 61 memory,.** to ma
113a0 6b 65 20 73 7a 20 61 20 6c 69 74 74 6c 65 20 74 ke sz a little t
113b0 6f 6f 20 6c 61 72 67 65 2e 20 20 54 68 65 20 66 oo large. The f
113c0 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 irst.** argument
113d0 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f should point to
113e0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f an allocation o
113f0 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 f at least sz*N
11400 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e bytes of memory.
11410 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
11420 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 use the memory p
11430 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 66 rovided by the f
11440 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
11450 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a 2a 20 satisfy its.**
11460 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66 6f 72 memory needs for
11470 20 74 68 65 20 66 69 72 73 74 20 4e 20 70 61 67 the first N pag
11480 65 73 20 74 68 61 74 20 69 74 20 61 64 64 73 20 es that it adds
11490 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20 61 64 to cache. If ad
114a0 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 67 65 ditional.** page
114b0 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 69 73 cache memory is
114c0 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64 20 77 needed beyond w
114d0 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20 hat is provided
114e0 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 by this option,
114f0 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 67 then.** SQLite g
11500 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f oes to [sqlite3_
11510 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68 malloc()] for th
11520 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f e additional sto
11530 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a 20 54 rage space..** T
11540 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
11550 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e 65 20 n might use one
11560 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 4e or more of the N
11570 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f 6c 64 buffers to hold
11580 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 63 6f .** memory acco
11590 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 unting informati
115a0 6f 6e 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 on. The pointer
115b0 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 in the first arg
115c0 75 6d 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 62 65 ument must.** be
115d0 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 aligned to an 8
115e0 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f -byte boundary o
115f0 72 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 r subsequent beh
11600 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a avior of SQLite.
11610 2a 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 66 ** will be undef
11620 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ined.</dd>.**.**
11630 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
11640 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a 20 IG_HEAP</dt>.**
11650 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
11660 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 specifies a stat
11670 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 ic memory buffer
11680 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c that SQLite wil
11690 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c l use.** for all
116a0 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 20 of its dynamic
116b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
116c0 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 74 n needs beyond t
116d0 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a hose provided.**
116e0 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 5f for by [SQLITE_
116f0 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 CONFIG_SCRATCH]
11700 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 and [SQLITE_CONF
11710 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a 2a IG_PAGECACHE]..*
11720 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 * There are thre
11730 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 6e 20 e arguments: An
11740 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 70 8-byte aligned p
11750 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 ointer to the me
11760 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65 20 6e 75 6d mory,.** the num
11770 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
11780 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 the memory buffe
11790 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 6d r, and the minim
117a0 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 um allocation si
117b0 7a 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 ze..** If the fi
117c0 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 rst pointer (the
117d0 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 memory pointer)
117e0 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 is NULL, then S
117f0 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a 2a 2a QLite reverts.**
11800 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65 to using its de
11810 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c fault memory all
11820 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79 73 74 ocator (the syst
11830 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c em malloc() impl
11840 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a 2a 20 ementation),.**
11850 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f undoing any prio
11860 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 r invocation of
11870 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
11880 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68 65 0a ALLOC]. If the.
11890 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 ** memory pointe
118a0 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e r is not NULL an
118b0 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 d either [SQLITE
118c0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 5d _ENABLE_MEMSYS3]
118d0 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 or.** [SQLITE_E
118e0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d 20 61 NABLE_MEMSYS5] a
118f0 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e re defined, then
11900 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 the alternative
11910 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 memory.** alloc
11920 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65 64 20 ator is engaged
11930 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20 6f 66 to handle all of
11940 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72 79 20 SQLites memory
11950 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 allocation needs
11960 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 ..** The first p
11970 6f 69 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f ointer (the memo
11980 72 79 20 70 6f 69 6e 74 65 72 29 20 6d 75 73 74 ry pointer) must
11990 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 be aligned to a
119a0 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 62 6f 75 6e n 8-byte.** boun
119b0 64 61 72 79 20 6f 72 20 73 75 62 73 65 71 75 65 dary or subseque
119c0 6e 74 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53 nt behavior of S
119d0 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 75 6e QLite will be un
119e0 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a defined.</dd>.**
119f0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
11a00 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e ONFIG_MUTEX</dt>
11a10 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
11a20 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
11a30 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 le argument whic
11a40 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 h is a pointer t
11a50 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 o an.** instance
11a60 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
11a70 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 _mutex_methods]
11a80 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
11a90 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 argument specifi
11aa0 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 es.** alternativ
11ab0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 e low-level mute
11ac0 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 x routines to be
11ad0 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a used in place.*
11ae0 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f 75 74 * the mutex rout
11af0 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 ines built into
11b00 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a SQLite.</dd>.**.
11b10 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f ** <dt>SQLITE_CO
11b20 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64 NFIG_GETMUTEX</d
11b30 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
11b40 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ption takes a si
11b50 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 ngle argument wh
11b60 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ich is a pointer
11b70 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e to an.** instan
11b80 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ce of the [sqlit
11b90 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
11ba0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 ] structure. Th
11bb0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 e.** [sqlite3_mu
11bc0 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 tex_methods].**
11bd0 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c structure is fil
11be0 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72 led with the cur
11bf0 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d rently defined m
11c00 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a utex routines..*
11c10 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 * This option ca
11c20 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 n be used to ove
11c30 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c rload the defaul
11c40 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 69 t mutex allocati
11c50 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 on.** routines w
11c60 69 74 68 20 61 20 77 72 61 70 70 65 72 20 75 73 ith a wrapper us
11c70 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 65 ed to track mute
11c80 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 72 66 x usage for perf
11c90 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69 ormance.** profi
11ca0 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 2c ling or testing,
11cb0 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 for example.</d
11cc0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
11cd0 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 ITE_CONFIG_LOOKA
11ce0 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SIDE</dt>.** <dd
11cf0 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b >This option tak
11d00 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 es two arguments
11d10 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20 that determine
11d20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d the default.** m
11d30 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
11d40 20 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d lookaside optim
11d50 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 ization. The fi
11d60 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
11d70 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 the.** size of e
11d80 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 ach lookaside bu
11d90 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68 ffer slot and th
11da0 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 e second is the
11db0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f number of.** slo
11dc0 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 ts allocated to
11dd0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f each database co
11de0 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 nnection. This
11df0 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 68 65 0a option sets the.
11e00 2a 2a 20 3c 69 3e 64 65 66 61 75 6c 74 3c 2f 69 ** <i>default</i
11e10 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 73 69 7a 65 > lookaside size
11e20 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 44 . The [SQLITE_D
11e30 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 BCONFIG_LOOKASID
11e40 45 5d 0a 2a 2a 20 76 65 72 62 20 74 6f 20 5b 73 E].** verb to [s
11e50 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 qlite3_db_config
11e60 28 29 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20 ()] can be used
11e70 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6c 6f to change the lo
11e80 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 6f 6e 66 69 okaside.** confi
11e90 67 75 72 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 guration on indi
11ea0 76 69 64 75 61 6c 20 63 6f 6e 6e 65 63 74 69 6f vidual connectio
11eb0 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ns.</dd>.**.** <
11ec0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
11ed0 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 _PCACHE</dt>.**
11ee0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 <dd>This option
11ef0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 takes a single a
11f00 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 rgument which is
11f10 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a a pointer to.**
11f20 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 an [sqlite3_pca
11f30 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a che_methods] obj
11f40 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 ect. This objec
11f50 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 t specifies the
11f60 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 interface.** to
11f70 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 a custom page ca
11f80 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 che implementati
11f90 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65 on. SQLite make
11fa0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a s a copy of the.
11fb0 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73 ** object and us
11fc0 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 20 63 es it for page c
11fd0 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ache memory allo
11fe0 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a cations.</dd>.**
11ff0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 .** <dt>SQLITE_C
12000 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c ONFIG_GETPCACHE<
12010 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
12020 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 option takes a
12030 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 single argument
12040 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 which is a point
12050 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c er to an.** [sql
12060 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
12070 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51 ods] object. SQ
12080 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74 Lite copies of t
12090 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61 he current.** pa
120a0 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 ge cache impleme
120b0 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61 ntation into tha
120c0 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a t object.</dd>.*
120d0 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 *.** </dl>.*/.#d
120e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
120f0 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
12100 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 1 /* nil */.#
12110 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
12120 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 NFIG_MULTITHREAD
12130 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 2 /* nil */.
12140 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
12150 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 ONFIG_SERIALIZED
12160 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 3 /* nil */
12170 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
12180 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 CONFIG_MALLOC
12190 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 4 /* sqlit
121a0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 e3_mem_methods*
121b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
121c0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c E_CONFIG_GETMALL
121d0 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c OC 5 /* sql
121e0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
121f0 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c * */.#define SQL
12200 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
12210 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 CH 6 /* v
12220 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e oid*, int sz, in
12230 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 t N */.#define S
12240 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 QLITE_CONFIG_PAG
12250 45 43 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a ECACHE 7 /*
12260 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 void*, int sz,
12270 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 int N */.#define
12280 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 SQLITE_CONFIG_H
12290 45 41 50 20 20 20 20 20 20 20 20 20 20 38 20 20 EAP 8
122a0 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 /* void*, int nB
122b0 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a yte, int min */.
122c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
122d0 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 ONFIG_MEMSTATUS
122e0 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 9 /* boolea
122f0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
12300 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 ITE_CONFIG_MUTEX
12310 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 10 /* s
12320 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
12330 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 hods* */.#define
12340 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 SQLITE_CONFIG_G
12350 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 ETMUTEX 11
12360 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 /* sqlite3_mutex
12370 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 _methods* */./*
12380 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 previously SQLIT
12390 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c E_CONFIG_CHUNKAL
123a0 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73 20 LOC 12 which is
123b0 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a now unused. */ .
123c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
123d0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 ONFIG_LOOKASIDE
123e0 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 13 /* int in
123f0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c t */.#define SQL
12400 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 ITE_CONFIG_PCACH
12410 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73 E 14 /* s
12420 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
12430 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e thods* */.#defin
12440 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
12450 47 45 54 50 43 41 43 48 45 20 20 20 20 31 35 20 GETPCACHE 15
12460 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 /* sqlite3_pcac
12470 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a he_methods* */..
12480 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
12490 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 Configuration Op
124a0 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c tions {H10170} <
124b0 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S20000>.** EXPER
124c0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
124d0 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 ese constants ar
124e0 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 e the available
124f0 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 integer configur
12500 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 ation options th
12510 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 at.** can be pas
12520 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
12530 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 d argument to th
12540 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f e [sqlite3_db_co
12550 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 nfig()] interfac
12560 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e e..**.** New con
12570 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f figuration optio
12580 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 ns may be added
12590 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
125a0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a es of SQLite..**
125b0 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 Existing config
125c0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 uration options
125d0 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 might be discont
125e0 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 inued. Applicat
125f0 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 ions.** should c
12600 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
12610 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 code from [sqlit
12620 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 e3_db_config()]
12630 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
12640 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f t.** the call wo
12650 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 rked. The [sqli
12660 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d te3_db_config()]
12670 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 interface will
12680 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d return a.** non-
12690 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 zero [error code
126a0 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e ] if a discontin
126b0 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 ued or unsupport
126c0 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ed configuration
126d0 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e option.** is in
126e0 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c voked..**.** <dl
126f0 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f >.** <dt>SQLITE_
12700 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 DBCONFIG_LOOKASI
12710 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 DE</dt>.** <dd>T
12720 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 his option takes
12730 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61 three additiona
12740 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 l arguments that
12750 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a determine the .
12760 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 ** [lookaside me
12770 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 mory allocator]
12780 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f configuration fo
12790 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 r the [database
127a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 connection]..**
127b0 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
127c0 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 70 61 nt (the third pa
127d0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
127e0 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d te3_db_config()]
127f0 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 is a.** pointer
12800 20 74 6f 20 61 6e 20 6d 65 6d 6f 72 79 20 62 75 to an memory bu
12810 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20 ffer to use for
12820 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 lookaside memory
12830 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 ..** The first a
12840 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e rgument may be N
12850 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73 ULL in which cas
12860 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c e SQLite will al
12870 6c 6f 63 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f locate the.** lo
12880 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 69 okaside buffer i
12890 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c tself using [sql
128a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 ite3_malloc()].
128b0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
128c0 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 ment is the.** s
128d0 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b ize of each look
128e0 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f aside buffer slo
128f0 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 t and the third
12900 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
12910 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f number of.** slo
12920 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 ts. The size of
12930 20 74 68 65 20 62 75 66 66 65 72 20 69 6e 20 74 the buffer in t
12940 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
12950 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 t must be greate
12960 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 r than.** or equ
12970 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63 al to the produc
12980 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 t of the second
12990 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d 65 and third argume
129a0 6e 74 73 2e 20 20 54 68 65 20 62 75 66 66 65 72 nts. The buffer
129b0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 6c 69 67 .** must be alig
129c0 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 ned to an 8-byte
129d0 20 62 6f 75 6e 64 61 72 79 2e 20 20 49 66 20 74 boundary. If t
129e0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
129f0 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 6d nt is not.** a m
12a00 75 6c 74 69 70 6c 65 20 6f 66 20 38 2c 20 69 74 ultiple of 8, it
12a10 20 69 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 72 is internally r
12a20 6f 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 74 ounded down to t
12a30 68 65 20 6e 65 78 74 20 73 6d 61 6c 6c 65 72 0a he next smaller.
12a40 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 ** multiple of 8
12a50 2e 20 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 . See also: [SQ
12a60 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b LITE_CONFIG_LOOK
12a70 41 53 49 44 45 5d 3c 2f 64 64 3e 0a 2a 2a 0a 2a ASIDE]</dd>.**.*
12a80 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 * </dl>.*/.#defi
12a90 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 ne SQLITE_DBCONF
12aa0 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20 IG_LOOKASIDE
12ab0 31 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69 1001 /* void* i
12ac0 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a nt int */.../*.*
12ad0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 * CAPI3REF: Enab
12ae0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 le Or Disable Ex
12af0 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f tended Result Co
12b00 64 65 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31 des {H12200} <S1
12b10 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0700>.**.** The
12b20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 sqlite3_extended
12b30 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20 _result_codes()
12b40 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 routine enables
12b50 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a or disables the.
12b60 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 ** [extended res
12b70 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75 ult codes] featu
12b80 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68 re of SQLite. Th
12b90 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c e extended resul
12ba0 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64 t.** codes are d
12bb0 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 isabled by defau
12bc0 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 lt for historica
12bd0 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 l compatibility
12be0 63 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a considerations..
12bf0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
12c00 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20 ts:.** [H12201]
12c10 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49 [H12202].*/.SQLI
12c20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
12c30 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 e3_extended_resu
12c40 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 lt_codes(sqlite3
12c50 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a *, int onoff);..
12c60 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
12c70 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 Last Insert Rowi
12c80 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 d {H12220} <S107
12c90 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 00>.**.** Each e
12ca0 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 ntry in an SQLit
12cb0 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e e table has a un
12cc0 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e ique 64-bit sign
12cd0 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 ed.** integer ke
12ce0 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f y called the [RO
12cf0 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20 WID | "rowid"].
12d00 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77 The rowid is alw
12d10 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a ays available.**
12d20 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65 as an undeclare
12d30 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52 d column named R
12d40 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52 OWID, OID, or _R
12d50 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73 OWID_ as long as
12d60 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20 those.** names
12d70 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65 are not also use
12d80 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20 d by explicitly
12d90 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73 declared columns
12da0 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c . If.** the tabl
12db0 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f e has a column o
12dc0 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20 f type [INTEGER
12dd0 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65 PRIMARY KEY] the
12de0 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a n that column.**
12df0 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61 is another alia
12e00 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e s for the rowid.
12e10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
12e20 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
12e30 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d [rowid] of the m
12e40 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75 ost recent.** su
12e50 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 ccessful [INSERT
12e60 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 ] into the datab
12e70 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61 ase from the [da
12e80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
12e90 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 n].** in the fir
12ea0 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 st argument. If
12eb0 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b no successful [
12ec0 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65 INSERT]s.** have
12ed0 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f ever occurred o
12ee0 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20 n that database
12ef0 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f connection, zero
12f00 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
12f10 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52 .** If an [INSER
12f20 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e T] occurs within
12f30 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e a trigger, then
12f40 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 the [rowid] of
12f50 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 the inserted.**
12f60 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 row is returned
12f70 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 by this routine
12f80 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 as long as the t
12f90 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e rigger is runnin
12fa0 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 g..** But once t
12fb0 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 he trigger termi
12fc0 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 nates, the value
12fd0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 returned by thi
12fe0 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 s routine.** rev
12ff0 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 erts to the last
13000 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 value inserted
13010 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 before the trigg
13020 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 er fired..**.**
13030 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 An [INSERT] that
13040 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 fails due to a
13050 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 constraint viola
13060 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a tion is not a.**
13070 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 successful [INS
13080 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f ERT] and does no
13090 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c t change the val
130a0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ue returned by t
130b0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 his.** routine.
130c0 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 Thus INSERT OR
130d0 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 FAIL, INSERT OR
130e0 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f IGNORE, INSERT O
130f0 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 R ROLLBACK,.** a
13100 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f nd INSERT OR ABO
13110 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 RT make no chang
13120 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e es to the return
13130 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a value of this.*
13140 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 * routine when t
13150 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 heir insertion f
13160 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 ails. When INSE
13170 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a RT OR REPLACE.**
13180 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f encounters a co
13190 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
131a0 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 on, it does not
131b0 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e fail. The.** IN
131c0 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 SERT continues t
131d0 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 o completion aft
131e0 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 er deleting rows
131f0 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 that caused.**
13200 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 the constraint p
13210 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 roblem so INSERT
13220 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c OR REPLACE will
13230 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a always change.*
13240 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c * the return val
13250 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 ue of this inter
13260 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 face..**.** For
13270 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
13280 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e this routine, an
13290 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e [INSERT] is con
132a0 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 sidered to.** be
132b0 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e successful even
132c0 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71 if it is subseq
132d0 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 uently rolled ba
132e0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ck..**.** Requir
132f0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 ements:.** [H122
13300 32 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a 21] [H12223].**.
13310 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65 ** If a separate
13320 20 74 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73 thread performs
13330 20 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 a new [INSERT]
13340 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64 on the same.** d
13350 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
13360 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71 on while the [sq
13370 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
13380 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75 t_rowid()].** fu
13390 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
133a0 67 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 g and thus chang
133b0 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 es the last inse
133c0 72 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74 rt [rowid],.** t
133d0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
133e0 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 turned by [sqlit
133f0 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 e3_last_insert_r
13400 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e owid()] is.** un
13410 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 predictable and
13420 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 might not equal
13430 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f either the old o
13440 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73 r the new.** las
13450 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d t insert [rowid]
13460 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
13470 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
13480 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
13490 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a t_rowid(sqlite3*
134a0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
134b0 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75 EF: Count The Nu
134c0 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 mber Of Rows Mod
134d0 69 66 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c ified {H12240} <
134e0 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10600>.**.** Th
134f0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
13500 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
13510 66 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 f database rows
13520 74 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65 that were change
13530 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64 d.** or inserted
13540 20 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74 or deleted by t
13550 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 he most recently
13560 20 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73 completed SQL s
13570 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74 tatement.** on t
13580 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
13590 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69 nection] specifi
135a0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 ed by the first
135b0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e parameter..** On
135c0 6c 79 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 ly changes that
135d0 61 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65 are directly spe
135e0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 5b 49 cified by the [I
135f0 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d NSERT], [UPDATE]
13600 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d ,.** or [DELETE]
13610 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 statement are c
13620 6f 75 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61 ounted. Auxilia
13630 72 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 ry changes cause
13640 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73 d by.** triggers
13650 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 are not counted
13660 2e 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 . Use the [sqlit
13670 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
13680 28 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 ()] function.**
13690 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61 to find the tota
136a0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e l number of chan
136b0 67 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 ges including ch
136c0 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20 anges caused by
136d0 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 triggers..**.**
136e0 43 68 61 6e 67 65 73 20 74 6f 20 61 20 76 69 65 Changes to a vie
136f0 77 20 74 68 61 74 20 61 72 65 20 73 69 6d 75 6c w that are simul
13700 61 74 65 64 20 62 79 20 61 6e 20 5b 49 4e 53 54 ated by an [INST
13710 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 0a EAD OF trigger].
13720 2a 2a 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 ** are not count
13730 65 64 2e 20 20 4f 6e 6c 79 20 72 65 61 6c 20 74 ed. Only real t
13740 61 62 6c 65 20 63 68 61 6e 67 65 73 20 61 72 65 able changes are
13750 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 counted..**.**
13760 41 20 22 72 6f 77 20 63 68 61 6e 67 65 22 20 69 A "row change" i
13770 73 20 61 20 63 68 61 6e 67 65 20 74 6f 20 61 20 s a change to a
13780 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 single row of a
13790 73 69 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 single table.**
137a0 63 61 75 73 65 64 20 62 79 20 61 6e 20 49 4e 53 caused by an INS
137b0 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 ERT, DELETE, or
137c0 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 UPDATE statement
137d0 2e 20 20 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20 . Rows that.**
137e0 61 72 65 20 63 68 61 6e 67 65 64 20 61 73 20 73 are changed as s
137f0 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 20 5b ide effects of [
13800 52 45 50 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 REPLACE] constra
13810 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a int resolution,.
13820 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f ** rollback, ABO
13830 52 54 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 5b RT processing, [
13840 44 52 4f 50 20 54 41 42 4c 45 5d 2c 20 6f 72 20 DROP TABLE], or
13850 62 79 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 by any other.**
13860 6d 65 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f mechanisms do no
13870 74 20 63 6f 75 6e 74 20 61 73 20 64 69 72 65 63 t count as direc
13880 74 20 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a t row changes..*
13890 2a 0a 2a 2a 20 41 20 22 74 72 69 67 67 65 72 20 *.** A "trigger
138a0 63 6f 6e 74 65 78 74 22 20 69 73 20 61 20 73 63 context" is a sc
138b0 6f 70 65 20 6f 66 20 65 78 65 63 75 74 69 6f 6e ope of execution
138c0 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 6e 64 that begins and
138d0 0a 2a 2a 20 65 6e 64 73 20 77 69 74 68 20 74 68 .** ends with th
138e0 65 20 73 63 72 69 70 74 20 6f 66 20 61 20 5b 43 e script of a [C
138f0 52 45 41 54 45 20 54 52 49 47 47 45 52 20 7c 20 REATE TRIGGER |
13900 74 72 69 67 67 65 72 5d 2e 20 0a 2a 2a 20 4d 6f trigger]. .** Mo
13910 73 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 st SQL statement
13920 73 20 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74 s are.** evaluat
13930 65 64 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e ed outside of an
13940 79 20 74 72 69 67 67 65 72 2e 20 20 54 68 69 73 y trigger. This
13950 20 69 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76 is the "top lev
13960 65 6c 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 el".** trigger c
13970 6f 6e 74 65 78 74 2e 20 20 49 66 20 61 20 74 72 ontext. If a tr
13980 69 67 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d igger fires from
13990 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 the top level,
139a0 61 0a 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72 a.** new trigger
139b0 20 63 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65 context is ente
139c0 72 65 64 20 66 6f 72 20 74 68 65 20 64 75 72 61 red for the dura
139d0 74 69 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65 tion of that one
139e0 0a 2a 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75 .** trigger. Su
139f0 62 74 72 69 67 67 65 72 73 20 63 72 65 61 74 65 btriggers create
13a00 20 73 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72 subcontexts for
13a10 20 74 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e their duration.
13a20 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b .**.** Calling [
13a30 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 sqlite3_exec()]
13a40 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 or [sqlite3_step
13a50 28 29 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20 ()] recursively
13a60 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 does.** not crea
13a70 74 65 20 61 20 6e 65 77 20 74 72 69 67 67 65 72 te a new trigger
13a80 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 context..**.**
13a90 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
13aa0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
13ab0 20 6f 66 20 64 69 72 65 63 74 20 72 6f 77 20 63 of direct row c
13ac0 68 61 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a hanges in the.**
13ad0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 most recent INS
13ae0 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 ERT, UPDATE, or
13af0 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 DELETE statement
13b00 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 within the same
13b10 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 .** trigger cont
13b20 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c ext..**.** Thus,
13b30 20 77 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f when called fro
13b40 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c m the top level,
13b50 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
13b60 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 eturns the.** nu
13b70 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 mber of changes
13b80 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 in the most rece
13b90 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 nt INSERT, UPDAT
13ba0 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 E, or DELETE.**
13bb0 74 68 61 74 20 61 6c 73 6f 20 6f 63 63 75 72 72 that also occurr
13bc0 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65 ed at the top le
13bd0 76 65 6c 2e 20 20 57 69 74 68 69 6e 20 74 68 65 vel. Within the
13be0 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67 body of a trigg
13bf0 65 72 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 er,.** the sqlit
13c00 65 33 5f 63 68 61 6e 67 65 73 28 29 20 69 6e 74 e3_changes() int
13c10 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 63 61 erface can be ca
13c20 6c 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 lled to find the
13c30 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 number of.** ch
13c40 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 anges in the mos
13c50 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c t recently compl
13c60 65 74 65 64 20 49 4e 53 45 52 54 2c 20 55 50 44 eted INSERT, UPD
13c70 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a ATE, or DELETE.*
13c80 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 * statement with
13c90 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 in the body of t
13ca0 68 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 2e he same trigger.
13cb0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 .** However, the
13cc0 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 number returned
13cd0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 does not includ
13ce0 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75 e changes.** cau
13cf0 73 65 64 20 62 79 20 73 75 62 74 72 69 67 67 65 sed by subtrigge
13d00 72 73 20 73 69 6e 63 65 20 74 68 6f 73 65 20 68 rs since those h
13d10 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f ave their own co
13d20 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 ntext..**.** See
13d30 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 also the [sqlit
13d40 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
13d50 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e ()] interface an
13d60 64 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f d the.** [count_
13d70 63 68 61 6e 67 65 73 20 70 72 61 67 6d 61 5d 2e changes pragma].
13d80 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
13d90 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d nts:.** [H12241]
13da0 20 5b 48 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20 [H12243].**.**
13db0 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 If a separate th
13dc0 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 read makes chang
13dd0 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 es on the same d
13de0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
13df0 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c on.** while [sql
13e00 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 ite3_changes()]
13e10 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 is running then
13e20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
13e30 65 64 0a 2a 2a 20 69 73 20 75 6e 70 72 65 64 69 ed.** is unpredi
13e40 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d ctable and not m
13e50 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 eaningful..*/.SQ
13e60 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
13e70 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c ite3_changes(sql
13e80 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ite3*);../*.** C
13e90 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e API3REF: Total N
13ea0 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f umber Of Rows Mo
13eb0 64 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20 dified {H12260}
13ec0 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S10600>.**.** T
13ed0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
13ee0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
13ef0 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63 of row changes c
13f00 61 75 73 65 64 20 62 79 20 5b 49 4e 53 45 52 54 aused by [INSERT
13f10 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f ],.** [UPDATE] o
13f20 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65 r [DELETE] state
13f30 6d 65 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20 ments since the
13f40 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
13f50 74 69 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 tion] was opened
13f60 2e 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 20 69 ..** The count i
13f70 6e 63 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e ncludes all chan
13f80 67 65 73 20 66 72 6f 6d 20 61 6c 6c 20 0a 2a 2a ges from all .**
13f90 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 52 [CREATE TRIGGER
13fa0 20 7c 20 74 72 69 67 67 65 72 5d 20 63 6f 6e 74 | trigger] cont
13fb0 65 78 74 73 2e 20 20 48 6f 77 65 76 65 72 2c 0a exts. However,.
13fc0 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 6f 65 ** the count doe
13fd0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 s not include ch
13fe0 61 6e 67 65 73 20 75 73 65 64 20 74 6f 20 69 6d anges used to im
13ff0 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45 plement [REPLACE
14000 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a ] constraints,.*
14010 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f * do rollbacks o
14020 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 r ABORT processi
14030 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 42 ng, or [DROP TAB
14040 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 LE] processing.
14050 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f The.** count do
14060 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 72 es not include r
14070 6f 77 73 20 6f 66 20 76 69 65 77 73 20 74 68 61 ows of views tha
14080 74 20 66 69 72 65 20 61 6e 20 5b 49 4e 53 54 45 t fire an [INSTE
14090 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 2c 0a AD OF trigger],.
140a0 2a 2a 20 74 68 6f 75 67 68 20 69 66 20 74 68 65 ** though if the
140b0 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 INSTEAD OF trig
140c0 67 65 72 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 ger makes change
140d0 73 20 6f 66 20 69 74 73 20 6f 77 6e 2c 20 74 68 s of its own, th
140e0 6f 73 65 20 63 68 61 6e 67 65 73 20 0a 2a 2a 20 ose changes .**
140f0 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 20 are counted..**
14100 54 68 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 The changes are
14110 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e 20 counted as soon
14120 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 as the statement
14130 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 6d that makes them
14140 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 is.** completed
14150 20 28 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 (when the state
14160 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 20 70 ment handle is p
14170 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 assed to [sqlite
14180 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 3_reset()] or.**
14190 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
141a0 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 ze()])..**.** Se
141b0 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 e also the [sqli
141c0 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 te3_changes()] i
141d0 6e 74 65 72 66 61 63 65 20 61 6e 64 20 74 68 65 nterface and the
141e0 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 .** [count_chang
141f0 65 73 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a es pragma]..**.*
14200 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
14210 2a 2a 20 5b 48 31 32 32 36 31 5d 20 5b 48 31 32 ** [H12261] [H12
14220 32 36 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 263].**.** If a
14230 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 separate thread
14240 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e makes changes on
14250 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
14260 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a se connection.**
14270 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f while [sqlite3_
14280 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d total_changes()]
14290 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e is running then
142a0 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 the value.** re
142b0 74 75 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64 turned is unpred
142c0 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 ictable and not
142d0 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 meaningful..*/.S
142e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
142f0 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e lite3_total_chan
14300 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a ges(sqlite3*);..
14310 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
14320 49 6e 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 Interrupt A Long
14330 2d 52 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b -Running Query {
14340 48 31 32 32 37 30 7d 20 3c 53 33 30 35 30 30 3e H12270} <S30500>
14350 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
14360 74 69 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 20 tion causes any
14370 70 65 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 pending database
14380 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 operation to ab
14390 6f 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 ort and.** retur
143a0 6e 20 61 74 20 69 74 73 20 65 61 72 6c 69 65 73 n at its earlies
143b0 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 t opportunity. T
143c0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 his routine is t
143d0 79 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c ypically.** call
143e0 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 ed in response t
143f0 6f 20 61 20 75 73 65 72 20 61 63 74 69 6f 6e 20 o a user action
14400 73 75 63 68 20 61 73 20 70 72 65 73 73 69 6e 67 such as pressing
14410 20 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 "Cancel".** or
14420 43 74 72 6c 2d 43 20 77 68 65 72 65 20 74 68 65 Ctrl-C where the
14430 20 75 73 65 72 20 77 61 6e 74 73 20 61 20 6c 6f user wants a lo
14440 6e 67 20 71 75 65 72 79 20 6f 70 65 72 61 74 69 ng query operati
14450 6f 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d on to halt.** im
14460 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a mediately..**.**
14470 20 49 74 20 69 73 20 73 61 66 65 20 74 6f 20 63 It is safe to c
14480 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 all this routine
14490 20 66 72 6f 6d 20 61 20 74 68 72 65 61 64 20 64 from a thread d
144a0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 ifferent from th
144b0 65 0a 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74 e.** thread that
144c0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 is currently ru
144d0 6e 6e 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 nning the databa
144e0 73 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 se operation. B
144f0 75 74 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 ut it.** is not
14500 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 safe to call thi
14510 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 s routine with a
14520 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
14530 63 74 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 ction] that.** i
14540 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 s closed or migh
14550 74 20 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 t close before s
14560 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
14570 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a () returns..**.*
14580 2a 20 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 * If an SQL oper
14590 61 74 69 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 ation is very ne
145a0 61 72 6c 79 20 66 69 6e 69 73 68 65 64 20 61 74 arly finished at
145b0 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a the time when.*
145c0 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 * sqlite3_interr
145d0 75 70 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2c upt() is called,
145e0 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e then it might n
145f0 6f 74 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 ot have an oppor
14600 74 75 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 tunity.** to be
14610 69 6e 74 65 72 72 75 70 74 65 64 20 61 6e 64 20 interrupted and
14620 6d 69 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 might continue t
14630 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a o completion..**
14640 0a 2a 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 .** An SQL opera
14650 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 74 tion that is int
14660 65 72 72 75 70 74 65 64 20 77 69 6c 6c 20 72 65 errupted will re
14670 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 turn [SQLITE_INT
14680 45 52 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 ERRUPT]..** If t
14690 68 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 53 he interrupted S
146a0 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 QL operation is
146b0 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 an INSERT, UPDAT
146c0 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 E, or DELETE.**
146d0 74 68 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 that is inside a
146e0 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 n explicit trans
146f0 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 action, then the
14700 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 entire transact
14710 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 ion.** will be r
14720 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d olled back autom
14730 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 atically..**.**
14740 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 The sqlite3_inte
14750 72 72 75 70 74 28 44 29 20 63 61 6c 6c 20 69 73 rrupt(D) call is
14760 20 69 6e 20 65 66 66 65 63 74 20 75 6e 74 69 6c in effect until
14770 20 61 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 72 all currently r
14780 75 6e 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 unning.** SQL st
14790 61 74 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74 atements on [dat
147a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
147b0 5d 20 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20 41 ] D complete. A
147c0 6e 79 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65 ny new SQL state
147d0 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 ments.** that ar
147e0 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20 e started after
147f0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 the sqlite3_inte
14800 72 72 75 70 74 28 29 20 63 61 6c 6c 20 61 6e 64 rrupt() call and
14810 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 before the .**
14820 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e running statemen
14830 74 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 ts reaches zero
14840 61 72 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 are interrupted
14850 61 73 20 69 66 20 74 68 65 79 20 68 61 64 20 62 as if they had b
14860 65 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 70 een.** running p
14870 72 69 6f 72 20 74 6f 20 74 68 65 20 73 71 6c 69 rior to the sqli
14880 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 te3_interrupt()
14890 63 61 6c 6c 2e 20 20 4e 65 77 20 53 51 4c 20 73 call. New SQL s
148a0 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 tatements.** tha
148b0 74 20 61 72 65 20 73 74 61 72 74 65 64 20 61 66 t are started af
148c0 74 65 72 20 74 68 65 20 72 75 6e 6e 69 6e 67 20 ter the running
148d0 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 20 statement count
148e0 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65 reaches zero are
148f0 0a 2a 2a 20 6e 6f 74 20 65 66 66 65 63 74 65 64 .** not effected
14900 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f by the sqlite3_
14910 69 6e 74 65 72 72 75 70 74 28 29 2e 0a 2a 2a 20 interrupt()..**
14920 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 A call to sqlite
14930 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 74 3_interrupt(D) t
14940 68 61 74 20 6f 63 63 75 72 73 20 77 68 65 6e 20 hat occurs when
14950 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 75 6e there are no run
14960 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 ning.** SQL stat
14970 65 6d 65 6e 74 73 20 69 73 20 61 20 6e 6f 2d 6f ements is a no-o
14980 70 20 61 6e 64 20 68 61 73 20 6e 6f 20 65 66 66 p and has no eff
14990 65 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 65 ect on SQL state
149a0 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 ments.** that ar
149b0 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20 e started after
149c0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 the sqlite3_inte
149d0 72 72 75 70 74 28 29 20 63 61 6c 6c 20 72 65 74 rrupt() call ret
149e0 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 urns..**.** Requ
149f0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
14a00 32 32 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a 2a 2271] [H12272].*
14a10 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 *.** If the data
14a20 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
14a30 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 71 closes while [sq
14a40 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 lite3_interrupt(
14a50 29 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67 )].** is running
14a60 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 73 then bad things
14a70 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70 will likely hap
14a80 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 pen..*/.SQLITE_A
14a90 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
14aa0 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65 interrupt(sqlite
14ab0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 3*);../*.** CAPI
14ac0 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 3REF: Determine
14ad0 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d If An SQL Statem
14ae0 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 20 ent Is Complete
14af0 7b 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30 30 {H10510} <S70200
14b00 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f >.**.** These ro
14b10 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 75 utines are usefu
14b20 6c 20 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 l during command
14b30 2d 6c 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 -line input to d
14b40 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a etermine if the.
14b50 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 ** currently ent
14b60 65 72 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 ered text seems
14b70 74 6f 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 to form a comple
14b80 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 te SQL statement
14b90 20 6f 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 or.** if additi
14ba0 6f 6e 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 onal input is ne
14bb0 65 64 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 eded before send
14bc0 69 6e 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 ing the text int
14bd0 6f 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 o.** SQLite for
14be0 70 61 72 73 69 6e 67 2e 20 20 54 68 65 73 65 20 parsing. These
14bf0 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
14c00 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 1 if the input s
14c10 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 tring.** appears
14c20 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 to be a complet
14c30 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e e SQL statement.
14c40 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73 A statement is
14c50 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a judged to be.**
14c60 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 20 complete if it
14c70 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d 69 ends with a semi
14c80 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 colon token and
14c90 69 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78 20 is not a prefix
14ca0 6f 66 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 of a.** well-for
14cb0 6d 65 64 20 43 52 45 41 54 45 20 54 52 49 47 47 med CREATE TRIGG
14cc0 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 ER statement. S
14cd0 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20 61 emicolons that a
14ce0 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 74 68 re embedded with
14cf0 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 in.** string lit
14d00 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64 20 erals or quoted
14d10 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 identifier names
14d20 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 or comments are
14d30 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 not.** independ
14d40 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65 79 ent tokens (they
14d50 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 are part of the
14d60 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68 20 token in which
14d70 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62 65 they are.** embe
14d80 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 20 64 dded) and thus d
14d90 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 61 o not count as a
14da0 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d 69 statement termi
14db0 6e 61 74 6f 72 2e 20 20 57 68 69 74 65 73 70 61 nator. Whitespa
14dc0 63 65 0a 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 6e ce.** and commen
14dd0 74 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 ts that follow t
14de0 68 65 20 66 69 6e 61 6c 20 73 65 6d 69 63 6f 6c he final semicol
14df0 6f 6e 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a on are ignored..
14e00 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
14e10 69 6e 65 73 20 72 65 74 75 72 6e 20 30 20 69 66 ines return 0 if
14e20 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 the statement i
14e30 73 20 69 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 49 s incomplete. I
14e40 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c f a.** memory al
14e50 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 location fails,
14e60 74 68 65 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 then SQLITE_NOME
14e70 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a M is returned..*
14e80 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
14e90 6e 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 nes do not parse
14ea0 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
14eb0 6e 74 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c nts thus.** will
14ec0 20 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 not detect synt
14ed0 61 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 actically incorr
14ee0 65 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 ect SQL..**.** I
14ef0 66 20 53 51 4c 69 74 65 20 68 61 73 20 6e 6f 74 f SQLite has not
14f00 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 been initialize
14f10 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 d using [sqlite3
14f20 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 70 _initialize()] p
14f30 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 6f rior .** to invo
14f40 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6d king sqlite3_com
14f50 70 6c 65 74 65 31 36 28 29 20 74 68 65 6e 20 73 plete16() then s
14f60 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
14f70 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a e() is invoked.*
14f80 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
14f90 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c by sqlite3_compl
14fa0 65 74 65 31 36 28 29 2e 20 20 49 66 20 74 68 61 ete16(). If tha
14fb0 74 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e t initialization
14fc0 20 66 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 fails,.** then
14fd0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
14fe0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f from sqlite3_co
14ff0 6d 70 6c 65 74 65 31 36 28 29 20 77 69 6c 6c 20 mplete16() will
15000 62 65 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 be non-zero.** r
15010 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 egardless of whe
15020 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
15030 69 6e 70 75 74 20 53 51 4c 20 69 73 20 63 6f 6d input SQL is com
15040 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 plete..**.** Req
15050 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 uirements: [H105
15060 31 31 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 0a 11] [H10512].**.
15070 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 ** The input to
15080 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 [sqlite3_complet
15090 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a e()] must be a z
150a0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a ero-terminated.*
150b0 2a 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a * UTF-8 string..
150c0 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 **.** The input
150d0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 to [sqlite3_comp
150e0 6c 65 74 65 31 36 28 29 5d 20 6d 75 73 74 20 62 lete16()] must b
150f0 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 e a zero-termina
15100 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 ted.** UTF-16 st
15110 72 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 62 ring in native b
15120 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 yte order..*/.SQ
15130 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
15140 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f ite3_complete(co
15150 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a nst char *sql);.
15160 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
15170 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 qlite3_complete1
15180 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6(const void *sq
15190 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 l);../*.** CAPI3
151a0 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 REF: Register A
151b0 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 Callback To Hand
151c0 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 45 le SQLITE_BUSY E
151d0 72 72 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 3c rrors {H12310} <
151e0 53 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S40400>.**.** Th
151f0 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
15200 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 a callback funct
15210 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20 62 ion that might b
15220 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 e invoked whenev
15230 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 er.** an attempt
15240 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e is made to open
15250 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c a database tabl
15260 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 e that another t
15270 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 hread.** or proc
15280 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a ess has locked..
15290 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 **.** If the bus
152a0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 y callback is NU
152b0 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 LL, then [SQLITE
152c0 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 _BUSY] or [SQLIT
152d0 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d E_IOERR_BLOCKED]
152e0 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 .** is returned
152f0 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 6f 6e immediately upon
15300 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 encountering th
15310 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 e lock. If the b
15320 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 usy callback.**
15330 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 is not NULL, the
15340 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 n the callback w
15350 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 ill be invoked w
15360 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 ith two argument
15370 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 s..**.** The fir
15380 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 st argument to t
15390 68 65 20 68 61 6e 64 6c 65 72 20 69 73 20 61 20 he handler is a
153a0 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 copy of the void
153b0 2a 20 70 6f 69 6e 74 65 72 20 77 68 69 63 68 0a * pointer which.
153c0 2a 2a 20 69 73 20 74 68 65 20 74 68 69 72 64 20 ** is the third
153d0 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 argument to sqli
153e0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 te3_busy_handler
153f0 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 (). The second
15400 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 argument to.** t
15410 68 65 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 he handler callb
15420 61 63 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ack is the numbe
15430 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 r of times that
15440 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 the busy handler
15450 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 has.** been inv
15460 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f oked for this lo
15470 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 cking event. If
15480 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c the.** busy cal
15490 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c lback returns 0,
154a0 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f then no additio
154b0 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65 nal attempts are
154c0 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 made to.** acce
154d0 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ss the database
154e0 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 and [SQLITE_BUSY
154f0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 ] or [SQLITE_IOE
15500 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 RR_BLOCKED] is r
15510 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 eturned..** If t
15520 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 he callback retu
15530 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 rns non-zero, th
15540 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65 6d en another attem
15550 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f pt.** is made to
15560 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 open the databa
15570 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 se for reading a
15580 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72 65 70 nd the cycle rep
15590 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eats..**.** The
155a0 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62 75 presence of a bu
155b0 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 sy handler does
155c0 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 not guarantee th
155d0 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e at it will be in
155e0 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 voked.** when th
155f0 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 ere is lock cont
15600 65 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 ention. If SQLit
15610 65 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 61 e determines tha
15620 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 t invoking the b
15630 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 usy.** handler c
15640 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 ould result in a
15650 20 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 deadlock, it wi
15660 6c 6c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 ll go ahead and
15670 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 return [SQLITE_B
15680 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 USY].** or [SQLI
15690 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 TE_IOERR_BLOCKED
156a0 5d 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 ] instead of inv
156b0 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 68 oking the busy h
156c0 61 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 andler..** Consi
156d0 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20 77 der a scenario w
156e0 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 here one process
156f0 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 is holding a re
15700 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 ad lock that.**
15710 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 it is trying to
15720 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 promote to a res
15730 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a erved lock and.*
15740 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 * a second proce
15750 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 ss is holding a
15760 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 reserved lock th
15770 61 74 20 69 74 20 69 73 20 74 72 79 69 6e 67 0a at it is trying.
15780 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f ** to promote to
15790 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
157a0 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 ck. The first p
157b0 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 rocess cannot pr
157c0 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 oceed.** because
157d0 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 it is blocked b
157e0 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 y the second and
157f0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 the second proc
15800 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 ess cannot.** pr
15810 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 69 74 oceed because it
15820 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 is blocked by t
15830 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f he first. If bo
15840 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 th processes.**
15850 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 invoke the busy
15860 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 handlers, neithe
15870 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 r will make any
15880 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65 progress. There
15890 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 fore,.** SQLite
158a0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
158b0 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66 69 BUSY] for the fi
158c0 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 rst process, hop
158d0 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a 2a ing that this.**
158e0 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 will induce the
158f0 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 74 first process t
15900 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 72 65 o release its re
15910 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f ad lock and allo
15920 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 w.** the second
15930 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 65 process to proce
15940 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 ed..**.** The de
15950 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 fault busy callb
15960 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a ack is NULL..**.
15970 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 ** The [SQLITE_B
15980 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63 6f USY] error is co
15990 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 nverted to [SQLI
159a0 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 TE_IOERR_BLOCKED
159b0 5d 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 ].** when SQLite
159c0 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c is in the middl
159d0 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72 61 e of a large tra
159e0 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20 61 nsaction where a
159f0 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 ll the.** change
15a00 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 s will not fit i
15a10 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 nto the in-memor
15a20 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74 65 y cache. SQLite
15a30 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 will.** already
15a40 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 hold a RESERVED
15a50 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
15a60 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 abase file, but
15a70 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 it needs.** to p
15a80 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b romote this lock
15a90 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73 6f to EXCLUSIVE so
15aa0 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 70 69 that it can spi
15ab0 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 ll cache.** page
15ac0 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 s into the datab
15ad0 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 ase file without
15ae0 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 harm to concurr
15af0 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 ent.** readers.
15b00 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 If it is unable
15b10 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 to promote the
15b20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 lock, then the i
15b30 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 n-memory.** cach
15b40 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 e will be left i
15b50 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e n an inconsisten
15b60 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20 74 t state and so t
15b70 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 he error.** code
15b80 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f is promoted fro
15b90 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c 79 m the relatively
15ba0 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f benign [SQLITE_
15bb0 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 BUSY] to.** the
15bc0 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51 4c more severe [SQL
15bd0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
15be0 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72 20 D]. This error
15bf0 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a code promotion.*
15c00 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74 6f * forces an auto
15c10 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f matic rollback o
15c20 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20 20 f the changes.
15c30 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 See the.** <a hr
15c40 65 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 6b ef="/cvstrac/wik
15c50 69 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f i?p=CorruptionFo
15c60 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 llowingBusyError
15c70 22 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e ">.** Corruption
15c80 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 FollowingBusyErr
15c90 6f 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 or</a> wiki page
15ca0 20 66 6f 72 20 61 20 64 69 73 63 75 73 73 69 6f for a discussio
15cb0 6e 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 n of why.** this
15cc0 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a is important..*
15cd0 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f *.** There can o
15ce0 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 nly be a single
15cf0 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 65 66 busy handler def
15d00 69 6e 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a ined for each.**
15d10 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
15d20 63 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 ction]. Setting
15d30 20 61 20 6e 65 77 20 62 75 73 79 20 68 61 6e 64 a new busy hand
15d40 6c 65 72 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a ler clears any.*
15d50 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 * previously set
15d60 20 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 handler. Note
15d70 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 that calling [sq
15d80 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f lite3_busy_timeo
15d90 75 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c ut()].** will al
15da0 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 so set or clear
15db0 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 the busy handler
15dc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 ..**.** The busy
15dd0 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 callback should
15de0 20 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 not take any ac
15df0 74 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 tions which modi
15e00 66 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 fy the.** databa
15e10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 se connection th
15e20 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 at invoked the b
15e30 75 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e usy handler. An
15e40 79 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a y such actions.*
15e50 2a 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 * result in unde
15e60 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a fined behavior..
15e70 2a 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 ** .** Requireme
15e80 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 5d nts:.** [H12311]
15e90 20 5b 48 31 32 33 31 32 5d 20 5b 48 31 32 33 31 [H12312] [H1231
15ea0 34 5d 20 5b 48 31 32 33 31 36 5d 20 5b 48 31 32 4] [H12316] [H12
15eb0 33 31 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 73 318].**.** A bus
15ec0 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e y handler must n
15ed0 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 ot close the dat
15ee0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
15ef0 0a 2a 2a 20 6f 72 20 5b 70 72 65 70 61 72 65 64 .** or [prepared
15f00 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 statement] that
15f10 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 invoked the bus
15f20 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 y handler..*/.SQ
15f30 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
15f40 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 ite3_busy_handle
15f50 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 r(sqlite3*, int(
15f60 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 *)(void*,int), v
15f70 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid*);../*.** CA
15f80 50 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 75 PI3REF: Set A Bu
15f90 73 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 sy Timeout {H123
15fa0 34 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 40} <S40410>.**.
15fb0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
15fc0 73 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33 5f sets a [sqlite3_
15fd0 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 busy_handler | b
15fe0 75 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 usy handler] tha
15ff0 74 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 t sleeps.** for
16000 61 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f 75 a specified amou
16010 6e 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 nt of time when
16020 61 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 a table is locke
16030 64 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72 0a d. The handler.
16040 2a 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 ** will sleep mu
16050 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 ltiple times unt
16060 69 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 il at least "ms"
16070 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 milliseconds of
16080 20 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 sleeping.** hav
16090 65 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b e accumulated. {
160a0 48 31 32 33 34 33 7d 20 41 66 74 65 72 20 22 6d H12343} After "m
160b0 73 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 s" milliseconds
160c0 6f 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 of sleeping,.**
160d0 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75 the handler retu
160e0 72 6e 73 20 30 20 77 68 69 63 68 20 63 61 75 73 rns 0 which caus
160f0 65 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 es [sqlite3_step
16100 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a ()] to return.**
16110 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f [SQLITE_BUSY] o
16120 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f r [SQLITE_IOERR_
16130 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 BLOCKED]..**.**
16140 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 Calling this rou
16150 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 tine with an arg
16160 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 ument less than
16170 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f or equal to zero
16180 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c .** turns off al
16190 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e l busy handlers.
161a0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e .**.** There can
161b0 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c only be a singl
161c0 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 e busy handler f
161d0 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a or a particular.
161e0 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
161f0 6e 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 nection] any any
16200 20 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 given moment.
16210 49 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20 If another busy
16220 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 handler.** was d
16230 65 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b efined (using [
16240 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e sqlite3_busy_han
16250 64 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74 dler()]) prior t
16260 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 o calling.** thi
16270 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 s routine, that
16280 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c other busy handl
16290 65 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a er is cleared..*
162a0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
162b0 73 3a 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20 5b s:.** [H12341] [
162c0 48 31 32 33 34 33 5d 20 5b 48 31 32 33 34 34 5d H12343] [H12344]
162d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
162e0 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f nt sqlite3_busy_
162f0 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a timeout(sqlite3*
16300 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a , int ms);../*.*
16310 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 * CAPI3REF: Conv
16320 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 enience Routines
16330 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 For Running Que
16340 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c 53 ries {H12370} <S
16350 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 10000>.**.** Def
16360 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65 inition: A <b>re
16370 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69 sult table</b> i
16380 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 s memory data st
16390 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64 20 ructure created
163a0 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 by the.** [sqlit
163b0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 e3_get_table()]
163c0 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 65 interface. A re
163d0 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f 72 sult table recor
163e0 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 ds the.** comple
163f0 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 te query results
16400 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72 from one or mor
16410 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a e queries..**.**
16420 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 65 The table conce
16430 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e 75 ptually has a nu
16440 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 mber of rows and
16450 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a columns. But.*
16460 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 20 * these numbers
16470 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 are not part of
16480 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 the result table
16490 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 0a itself. These.
164a0 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f ** numbers are o
164b0 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 65 btained separate
164c0 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 68 ly. Let N be th
164d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 e number of rows
164e0 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 65 .** and M be the
164f0 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
16500 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 ns..**.** A resu
16510 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 61 lt table is an a
16520 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 rray of pointers
16530 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 to zero-termina
16540 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 ted UTF-8 string
16550 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 s..** There are
16560 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 (N+1)*M elements
16570 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20 in the array.
16580 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 6e The first M poin
16590 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f ters point.** to
165a0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
165b0 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 63 strings that c
165c0 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 ontain the names
165d0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e of the columns.
165e0 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e .** The remainin
165f0 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f g entries all po
16600 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 73 int to query res
16610 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 ults. NULL valu
16620 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 es result.** in
16630 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20 NULL pointers.
16640 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73 All other values
16650 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 54 are in their UT
16660 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 F-8 zero-termina
16670 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65 ted.** string re
16680 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 20 presentation as
16690 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c returned by [sql
166a0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
166b0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 ()]..**.** A res
166c0 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20 ult table might
166d0 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f consist of one o
166e0 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c r more memory al
166f0 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 locations..** It
16700 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 is not safe to
16710 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 61 pass a result ta
16720 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 ble directly to
16730 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
16740 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 ..** A result ta
16750 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65 ble should be de
16760 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 allocated using
16770 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 [sqlite3_free_ta
16780 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 ble()]..**.** As
16790 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 an example of t
167a0 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 he result table
167b0 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 format, suppose
167c0 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a a query result.*
167d0 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a * is as follows:
167e0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f .**.** <blockquo
167f0 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 te><pre>.**
16800 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c Name |
16810 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d Age.** -
16820 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
16830 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 ------.**
16840 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34 Alice | 4
16850 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20 3.** Bob
16860 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 | 28.**
16870 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 20 Cindy
16880 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 | 21.** </pre
16890 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a ></blockquote>.*
168a0 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 *.** There are t
168b0 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 wo column (M==2)
168c0 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20 and three rows
168d0 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68 (N==3). Thus th
168e0 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c e.** result tabl
168f0 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e e has 8 entries.
16900 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 65 Suppose the re
16910 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74 sult table is st
16920 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 ored.** in an ar
16930 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 ray names azResu
16940 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75 lt. Then azResu
16950 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f lt holds this co
16960 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c ntent:.**.** <bl
16970 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
16980 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c * azResul
16990 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 t[0] = "Name
169a0 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
169b0 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 esult[1] = "
169c0 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 Age";.**
169d0 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 azResult[2]
169e0 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 = "Alice";.**
169f0 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 azResult	
16a00 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 1;3] = "43";.**
16a10 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 azResult&
16a20 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a #91;4] = "Bob";.
16a30 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
16a40 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 lt[5] = "28"
16a50 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 ;.** azRe
16a60 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 sult[6] = "C
16a70 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 indy";.**
16a80 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d azResult[7]
16a90 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 = "21";.** </pr
16aa0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
16ab0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
16ac0 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75 3_get_table() fu
16ad0 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 nction evaluates
16ae0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 one or more.**
16af0 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 semicolon-separa
16b00 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ted SQL statemen
16b10 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 ts in the zero-t
16b20 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a erminated UTF-8.
16b30 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73 ** string of its
16b40 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 2nd parameter.
16b50 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 65 It returns a re
16b60 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 sult table to th
16b70 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 e.** pointer giv
16b80 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 en in its 3rd pa
16b90 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 rameter..**.** A
16ba0 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 fter the calling
16bb0 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 function has fi
16bc0 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 nished using the
16bd0 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 result, it shou
16be0 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70 ld.** pass the p
16bf0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 ointer to the re
16c00 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71 sult table to sq
16c10 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 lite3_free_table
16c20 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a () in order to.*
16c30 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65 * release the me
16c40 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d 61 mory that was ma
16c50 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 65 lloced. Because
16c60 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 0a of the way the.
16c70 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ** [sqlite3_mall
16c80 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 69 oc()] happens wi
16c90 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 thin sqlite3_get
16ca0 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 61 _table(), the ca
16cb0 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f lling.** functio
16cc0 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 74 n must not try t
16cd0 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f o call [sqlite3_
16ce0 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79 free()] directly
16cf0 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69 . Only.** [sqli
16d00 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 te3_free_table()
16d10 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 6c ] is able to rel
16d20 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 ease the memory
16d30 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66 properly and saf
16d40 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ely..**.** The s
16d50 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 qlite3_get_table
16d60 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 () interface is
16d70 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 implemented as a
16d80 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a wrapper around.
16d90 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 ** [sqlite3_exec
16da0 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 65 ()]. The sqlite
16db0 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 6f 3_get_table() ro
16dc0 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68 utine does not h
16dd0 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f ave access.** to
16de0 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 any internal da
16df0 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 ta structures of
16e00 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 65 SQLite. It use
16e10 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 s only the publi
16e20 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64 c.** interface d
16e30 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73 efined here. As
16e40 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 a consequence,
16e50 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 errors that occu
16e60 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70 r in the.** wrap
16e70 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64 per layer outsid
16e80 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61 e of the interna
16e90 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 l [sqlite3_exec(
16ea0 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a )] call are not.
16eb0 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20 ** reflected in
16ec0 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
16ed0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 to [sqlite3_err
16ee0 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 code()] or [sqli
16ef0 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a te3_errmsg()]..*
16f00 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
16f10 73 3a 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20 5b s:.** [H12371] [
16f20 48 31 32 33 37 33 5d 20 5b 48 31 32 33 37 34 5d H12373] [H12374]
16f30 20 5b 48 31 32 33 37 36 5d 20 5b 48 31 32 33 37 [H12376] [H1237
16f40 39 5d 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 53 9] [H12382].*/.S
16f50 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
16f60 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 lite3_get_table(
16f70 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
16f80 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f /* An o
16f90 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a pen database */.
16fa0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
16fb0 71 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 ql, /* SQL t
16fc0 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a o be evaluated *
16fd0 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 /. char ***pazR
16fe0 65 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 esult, /* Res
16ff0 75 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65 72 ults of the quer
17000 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f y */. int *pnRo
17010 77 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 w, /*
17020 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 Number of result
17030 20 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68 65 rows written he
17040 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 re */. int *pnC
17050 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a olumn, /*
17060 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c Number of resul
17070 74 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 t columns writte
17080 6e 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 n here */. char
17090 20 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20 **pzErrmsg
170a0 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 /* Error msg w
170b0 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 ritten here */.)
170c0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
170d0 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 d sqlite3_free_t
170e0 61 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 75 able(char **resu
170f0 6c 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 lt);../*.** CAPI
17100 33 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 3REF: Formatted
17110 53 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 String Printing
17120 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34 30 Functions {H1740
17130 30 7d 20 3c 53 37 30 30 30 30 3e 3c 53 32 30 30 0} <S70000><S200
17140 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
17150 72 6f 75 74 69 6e 65 73 20 61 72 65 20 77 6f 72 routines are wor
17160 6b 2d 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 20 k-alikes of the
17170 22 70 72 69 6e 74 66 28 29 22 20 66 61 6d 69 6c "printf()" famil
17180 79 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a y of functions.*
17190 2a 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 * from the stand
171a0 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a ard C library..*
171b0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
171c0 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 _mprintf() and s
171d0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 qlite3_vmprintf(
171e0 29 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65 ) routines write
171f0 20 74 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74 their.** result
17200 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 s into memory ob
17210 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
17220 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a ite3_malloc()]..
17230 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 72 ** The strings r
17240 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 eturned by these
17250 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 68 two routines sh
17260 6f 75 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61 ould be.** relea
17270 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f sed by [sqlite3_
17280 66 72 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 72 free()]. Both r
17290 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 61 outines return a
172a0 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 .** NULL pointer
172b0 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c if [sqlite3_mal
172c0 6c 6f 63 28 29 5d 20 69 73 20 75 6e 61 62 6c 65 loc()] is unable
172d0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f to allocate eno
172e0 75 67 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f ugh.** memory to
172f0 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 hold the result
17300 69 6e 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a ing string..**.*
17310 2a 20 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70 * In sqlite3_snp
17320 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20 rintf() routine
17330 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 73 is similar to "s
17340 6e 70 72 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a nprintf()" from.
17350 2a 2a 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 ** the standard
17360 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 C library. The
17370 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 result is writte
17380 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 n into the.** bu
17390 66 66 65 72 20 73 75 70 70 6c 69 65 64 20 61 73 ffer supplied as
173a0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
173b0 6d 65 74 65 72 20 77 68 6f 73 65 20 73 69 7a 65 meter whose size
173c0 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 is given by.**
173d0 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 the first parame
173e0 74 65 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 ter. Note that t
173f0 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a he order of the.
17400 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61 72 ** first two par
17410 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 72 ameters is rever
17420 73 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 sed from snprint
17430 66 28 29 2e 20 20 54 68 69 73 20 69 73 20 61 6e f(). This is an
17440 0a 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 61 .** historical a
17450 63 63 69 64 65 6e 74 20 74 68 61 74 20 63 61 6e ccident that can
17460 6e 6f 74 20 62 65 20 66 69 78 65 64 20 77 69 74 not be fixed wit
17470 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a hout breaking.**
17480 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 backwards compa
17490 74 69 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 tibility. Note
174a0 61 6c 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 also that sqlite
174b0 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 3_snprintf().**
174c0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
174d0 72 20 74 6f 20 69 74 73 20 62 75 66 66 65 72 20 r to its buffer
174e0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e instead of the n
174f0 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 umber of.** char
17500 61 63 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20 acters actually
17510 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
17520 20 62 75 66 66 65 72 2e 20 20 57 65 20 61 64 6d buffer. We adm
17530 69 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e it that.** the n
17540 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
17550 65 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c ers written woul
17560 64 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66 d be a more usef
17570 75 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c ul return.** val
17580 75 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 ue but we cannot
17590 20 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c change the impl
175a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
175b0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
175c0 0a 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 .** now without
175d0 62 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 breaking compati
175e0 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 bility..**.** As
175f0 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66 long as the buf
17600 66 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 61 fer size is grea
17610 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 ter than zero, s
17620 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
17630 29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20 ).** guarantees
17640 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72 20 that the buffer
17650 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 is always zero-t
17660 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 erminated. The
17670 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 first.** paramet
17680 65 72 20 22 6e 22 20 69 73 20 74 68 65 20 74 6f er "n" is the to
17690 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 tal size of the
176a0 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e buffer, includin
176b0 67 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 g space for.** t
176c0 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 he zero terminat
176d0 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 or. So the long
176e0 65 73 74 20 73 74 72 69 6e 67 20 74 68 61 74 20 est string that
176f0 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c can be completel
17700 79 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 6c y.** written wil
17710 6c 20 62 65 20 6e 2d 31 20 63 68 61 72 61 63 74 l be n-1 charact
17720 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ers..**.** These
17730 20 72 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d routines all im
17740 70 6c 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 plement some add
17750 69 74 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 itional formatti
17760 6e 67 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 ng.** options th
17770 61 74 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f at are useful fo
17780 72 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 r constructing S
17790 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a QL statements..*
177a0 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 * All of the usu
177b0 61 6c 20 70 72 69 6e 74 66 28 29 20 66 6f 72 6d al printf() form
177c0 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 atting options a
177d0 70 70 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 69 pply. In additi
177e0 6f 6e 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 on, there.** is
177f0 61 72 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20 are "%q", "%Q",
17800 61 6e 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 and "%z" options
17810 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f ..**.** The %q o
17820 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 ption works like
17830 20 25 73 20 69 6e 20 74 68 61 74 20 69 74 20 73 %s in that it s
17840 75 62 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c ubstitutes a nul
17850 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 l-terminated.**
17860 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 string from the
17870 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 argument list.
17880 42 75 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 But %q also doub
17890 6c 65 73 20 65 76 65 72 79 20 27 5c 27 27 20 63 les every '\'' c
178a0 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 haracter..** %q
178b0 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 is designed for
178c0 75 73 65 20 69 6e 73 69 64 65 20 61 20 73 74 72 use inside a str
178d0 69 6e 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 ing literal. By
178e0 20 64 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27 doubling each '
178f0 5c 27 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 \''.** character
17900 20 69 74 20 65 73 63 61 70 65 73 20 74 68 61 74 it escapes that
17910 20 63 68 61 72 61 63 74 65 72 20 61 6e 64 20 61 character and a
17920 6c 6c 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69 llows it to be i
17930 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 nserted into.**
17940 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a the string..**.*
17950 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 * For example, a
17960 73 73 75 6d 65 20 74 68 65 20 73 74 72 69 6e 67 ssume the string
17970 20 76 61 72 69 61 62 6c 65 20 7a 54 65 78 74 20 variable zText
17980 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61 73 contains text as
17990 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
179a0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 <blockquote><pre
179b0 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 78 >.** char *zTex
179c0 74 20 3d 20 22 49 74 27 73 20 61 20 68 61 70 70 t = "It's a happ
179d0 79 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72 y day!";.** </pr
179e0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
179f0 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 **.** One can us
17a00 65 20 74 68 69 73 20 74 65 78 74 20 69 6e 20 61 e this text in a
17a10 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
17a20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
17a30 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
17a40 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 re>.** char *zS
17a50 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 QL = sqlite3_mpr
17a60 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 intf("INSERT INT
17a70 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 27 O table VALUES('
17a80 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a %q')", zText);.*
17a90 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 * sqlite3_exec(
17aa0 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 db, zSQL, 0, 0,
17ab0 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 0);.** sqlite3_
17ac0 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c free(zSQL);.** <
17ad0 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 /pre></blockquot
17ae0 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 e>.**.** Because
17af0 20 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20 73 the %q format s
17b00 74 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74 tring is used, t
17b10 68 65 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 he '\'' characte
17b20 72 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73 r in zText.** is
17b30 20 65 73 63 61 70 65 64 20 61 6e 64 20 74 68 65 escaped and the
17b40 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20 69 SQL generated i
17b50 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
17b60 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e .** <blockquote>
17b70 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 <pre>.** INSERT
17b80 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c INTO table1 VAL
17b90 55 45 53 28 27 49 74 27 27 73 20 61 20 68 61 70 UES('It''s a hap
17ba0 70 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70 py day!').** </p
17bb0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
17bc0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 .**.** This is c
17bd0 6f 72 72 65 63 74 2e 20 20 48 61 64 20 77 65 20 orrect. Had we
17be0 75 73 65 64 20 25 73 20 69 6e 73 74 65 61 64 20 used %s instead
17bf0 6f 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72 of %q, the gener
17c00 61 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c ated SQL.** woul
17c10 64 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 d have looked li
17c20 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c ke this:.**.** <
17c30 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
17c40 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f .** INSERT INTO
17c50 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 table1 VALUES('
17c60 49 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79 It's a happy day
17c70 21 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f !');.** </pre></
17c80 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a blockquote>.**.*
17c90 2a 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65 78 * This second ex
17ca0 61 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c 20 ample is an SQL
17cb0 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 41 syntax error. A
17cc0 73 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c 65 s a general rule
17cd0 20 79 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 you should.** a
17ce0 6c 77 61 79 73 20 75 73 65 20 25 71 20 69 6e 73 lways use %q ins
17cf0 74 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e 20 tead of %s when
17d00 69 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20 69 inserting text i
17d10 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74 nto a string lit
17d20 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 eral..**.** The
17d30 25 51 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 %Q option works
17d40 6c 69 6b 65 20 25 71 20 65 78 63 65 70 74 20 69 like %q except i
17d50 74 20 61 6c 73 6f 20 61 64 64 73 20 73 69 6e 67 t also adds sing
17d60 6c 65 20 71 75 6f 74 65 73 20 61 72 6f 75 6e 64 le quotes around
17d70 0a 2a 2a 20 74 68 65 20 6f 75 74 73 69 64 65 20 .** the outside
17d80 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 73 74 72 of the total str
17d90 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c ing. Additional
17da0 6c 79 2c 20 69 66 20 74 68 65 20 70 61 72 61 6d ly, if the param
17db0 65 74 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 eter in the.** a
17dc0 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 20 rgument list is
17dd0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
17de0 25 51 20 73 75 62 73 74 69 74 75 74 65 73 20 74 %Q substitutes t
17df0 68 65 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 he text "NULL" (
17e00 77 69 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c without.** singl
17e10 65 20 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 e quotes) in pla
17e20 63 65 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74 ce of the %Q opt
17e30 69 6f 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 ion. So, for ex
17e40 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 ample, one could
17e50 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f say:.**.** <blo
17e60 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a ckquote><pre>.**
17e70 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 char *zSQL = s
17e80 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 qlite3_mprintf("
17e90 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c INSERT INTO tabl
17ea0 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a e VALUES(%Q)", z
17eb0 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 Text);.** sqlit
17ec0 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c e3_exec(db, zSQL
17ed0 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 , 0, 0, 0);.**
17ee0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 sqlite3_free(zSQ
17ef0 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 L);.** </pre></b
17f00 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
17f10 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 The code above
17f20 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f will render a co
17f30 72 72 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d rrect SQL statem
17f40 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a ent in the zSQL.
17f50 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e ** variable even
17f60 20 69 66 20 74 68 65 20 7a 54 65 78 74 20 76 61 if the zText va
17f70 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c riable is a NULL
17f80 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
17f90 54 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74 The "%z" formatt
17fa0 69 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 ing option works
17fb0 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25 exactly like "%
17fc0 73 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 s" with the.** a
17fd0 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74 ddition that aft
17fe0 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61 er the string ha
17ff0 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20 s been read and
18000 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 copied into.** t
18010 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 he result, [sqli
18020 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63 te3_free()] is c
18030 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 alled on the inp
18040 75 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d ut string. {END}
18050 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
18060 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d nts:.** [H17403]
18070 20 5b 48 31 37 34 30 36 5d 20 5b 48 31 37 34 30 [H17406] [H1740
18080 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 7].*/.SQLITE_API
18090 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d char *sqlite3_m
180a0 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 printf(const cha
180b0 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f r*,...);.SQLITE_
180c0 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
180d0 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 3_vmprintf(const
180e0 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 char*, va_list)
180f0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 ;.SQLITE_API cha
18100 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 r *sqlite3_snpri
18110 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f ntf(int,char*,co
18120 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b nst char*, ...);
18130 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
18140 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 : Memory Allocat
18150 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48 ion Subsystem {H
18160 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 17300} <S20000>.
18170 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 **.** The SQLite
18180 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 73 core uses thes
18190 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 e three routines
181a0 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 for all of its
181b0 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 own.** internal
181c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
181d0 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20 n needs. "Core"
181e0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 in the previous
181f0 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 sentence.** does
18200 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 not include ope
18210 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 rating-system sp
18220 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65 ecific VFS imple
18230 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a mentation. The.
18240 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75 ** Windows VFS u
18250 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f ses native mallo
18260 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 c() and free() f
18270 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f or some operatio
18280 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 ns..**.** The sq
18290 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 lite3_malloc() r
182a0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 outine returns a
182b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c pointer to a bl
182c0 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 ock.** of memory
182d0 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 at least N byte
182e0 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 s in length, whe
182f0 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 61 re N is the para
18300 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c meter..** If sql
18310 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 ite3_malloc() is
18320 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 unable to obtai
18330 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 65 n sufficient fre
18340 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 e.** memory, it
18350 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 returns a NULL p
18360 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 ointer. If the
18370 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a parameter N to.*
18380 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 * sqlite3_malloc
18390 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 () is zero or ne
183a0 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 gative then sqli
183b0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 te3_malloc() ret
183c0 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 urns.** a NULL p
183d0 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 ointer..**.** Ca
183e0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 lling sqlite3_fr
183f0 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e ee() with a poin
18400 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 ter previously r
18410 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 eturned.** by sq
18420 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f lite3_malloc() o
18430 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f r sqlite3_reallo
18440 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 c() releases tha
18450 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 t memory so.** t
18460 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 hat it might be
18470 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c reused. The sql
18480 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74 ite3_free() rout
18490 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f ine is.** a no-o
184a0 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77 p if is called w
184b0 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ith a NULL point
184c0 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e er. Passing a N
184d0 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 ULL pointer.** t
184e0 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 o sqlite3_free()
184f0 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 is harmless. A
18500 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64 fter being freed
18510 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 , memory.** shou
18520 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65 ld neither be re
18530 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 ad nor written.
18540 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72 Even reading pr
18550 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a eviously freed.*
18560 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 * memory might r
18570 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 esult in a segme
18580 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 ntation fault or
18590 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72 other severe er
185a0 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 ror..** Memory c
185b0 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 orruption, a seg
185c0 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c mentation fault,
185d0 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 or other severe
185e0 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 error.** might
185f0 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 result if sqlite
18600 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 3_free() is call
18610 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 ed with a non-NU
18620 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a LL pointer that.
18630 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 ** was not obtai
18640 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
18650 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c _malloc() or sql
18660 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a ite3_realloc()..
18670 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
18680 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 3_realloc() inte
18690 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 rface attempts t
186a0 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 o resize a.** pr
186b0 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ior memory alloc
186c0 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c ation to be at l
186d0 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68 east N bytes, wh
186e0 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 ere N is the.**
186f0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
18700 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c . The memory al
18710 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 location to be r
18720 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 69 esized is the fi
18730 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 rst.** parameter
18740 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74 20 . If the first
18750 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
18760 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a ite3_realloc().*
18770 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e * is a NULL poin
18780 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 68 ter then its beh
18790 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63 avior is identic
187a0 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a al to calling.**
187b0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
187c0 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 N) where N is th
187d0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
187e0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 er to sqlite3_re
187f0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 alloc()..** If t
18800 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
18810 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
18820 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f ealloc() is zero
18830 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 or.** negative
18840 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f then the behavio
18850 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 r is exactly the
18860 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 same as calling
18870 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 .** sqlite3_free
18880 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74 (P) where P is t
18890 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
188a0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 er to sqlite3_re
188b0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 alloc()..** sqli
188c0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 te3_realloc() re
188d0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
188e0 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f to a memory allo
188f0 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 cation.** of at
18900 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e least N bytes in
18910 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 size or NULL if
18920 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f sufficient memo
18930 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c ry is unavailabl
18940 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 e..** If M is th
18950 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72 e size of the pr
18960 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 ior allocation,
18970 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 then min(N,M) by
18980 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 tes.** of the pr
18990 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 ior allocation a
189a0 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 re copied into t
189b0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
189c0 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a buffer returned.
189d0 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65 ** by sqlite3_re
189e0 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20 alloc() and the
189f0 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e prior allocation
18a00 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 is freed..** If
18a10 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 sqlite3_realloc
18a20 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c () returns NULL,
18a30 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 then the prior
18a40 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 allocation.** is
18a50 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a not freed..**.*
18a60 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 * The memory ret
18a70 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
18a80 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 _malloc() and sq
18a90 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a lite3_realloc().
18aa0 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69 ** is always ali
18ab0 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74 gned to at least
18ac0 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 an 8 byte bound
18ad0 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a ary. {END}.**.**
18ae0 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 The default imp
18af0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
18b00 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
18b10 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75 tion subsystem u
18b20 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f ses.** the mallo
18b30 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 c(), realloc() a
18b40 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 64 nd free() provid
18b50 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 ed by the standa
18b60 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a rd C library..**
18b70 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65 {H17382} Howeve
18b80 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 r, if SQLite is
18b90 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
18ba0 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f e.** SQLITE_MEMO
18bb0 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f RY_SIZE=<i>NNN</
18bc0 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f i> C preprocesso
18bd0 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c r macro (where <
18be0 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 i>NNN</i>.** is
18bf0 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65 an integer), the
18c00 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20 n SQLite create
18c10 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20 6f a static array o
18c20 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 f at least.** <i
18c30 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 >NNN</i> bytes i
18c40 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20 n size and uses
18c50 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 61 that array for a
18c60 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 ll of its dynami
18c70 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f c.** memory allo
18c80 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 cation needs. {E
18c90 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 ND} Additional
18ca0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
18cb0 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 options.** may
18cc0 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 be added in futu
18cd0 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a re releases..**.
18ce0 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72 ** In SQLite ver
18cf0 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 sion 3.5.0 and 3
18d00 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73 .5.1, it was pos
18d10 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a sible to define.
18d20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d ** the SQLITE_OM
18d30 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 IT_MEMORY_ALLOCA
18d40 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 TION which would
18d50 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 74 cause the built
18d60 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 -in.** implement
18d70 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 ation of these r
18d80 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d outines to be om
18d90 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 70 itted. That cap
18da0 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f ability.** is no
18db0 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64 longer provided
18dc0 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e . Only built-in
18dd0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
18de0 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a rs can be used..
18df0 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 **.** The Window
18e00 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c s OS interface l
18e10 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 ayer calls.** th
18e20 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 e system malloc(
18e30 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72 ) and free() dir
18e40 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 ectly when conve
18e50 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d rting.** filenam
18e60 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 55 es between the U
18e70 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 TF-8 encoding us
18e80 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 ed by SQLite.**
18e90 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 6c and whatever fil
18ea0 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 ename encoding i
18eb0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 s used by the pa
18ec0 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 rticular Windows
18ed0 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e .** installation
18ee0 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 . Memory alloca
18ef0 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20 tion errors are
18f00 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a detected, but.**
18f10 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 74 they are report
18f20 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 ed back as [SQLI
18f30 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a TE_CANTOPEN] or.
18f40 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 ** [SQLITE_IOERR
18f50 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53 ] rather than [S
18f60 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a QLITE_NOMEM]..**
18f70 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
18f80 3a 0a 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48 :.** [H17303] [H
18f90 31 37 33 30 34 5d 20 5b 48 31 37 33 30 35 5d 20 17304] [H17305]
18fa0 5b 48 31 37 33 30 36 5d 20 5b 48 31 37 33 31 30 [H17306] [H17310
18fb0 5d 20 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 33 ] [H17312] [H173
18fc0 31 35 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20 15] [H17318].**
18fd0 5b 48 31 37 33 32 31 5d 20 5b 48 31 37 33 32 32 [H17321] [H17322
18fe0 5d 20 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a ] [H17323].**.**
18ff0 20 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 The pointer arg
19000 75 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 uments to [sqlit
19010 65 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b e3_free()] and [
19020 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
19030 29 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 )].** must be ei
19040 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 ther NULL or els
19050 65 20 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 e pointers obtai
19060 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 ned from a prior
19070 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f .** invocation o
19080 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f f [sqlite3_mallo
19090 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 c()] or [sqlite3
190a0 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74 _realloc()] that
190b0 20 68 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74 have.** not yet
190c0 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a been released..
190d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 **.** The applic
190e0 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 ation must not r
190f0 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 ead or write any
19100 20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c part of.** a bl
19110 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66 ock of memory af
19120 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 ter it has been
19130 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a 2a released using.*
19140 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 * [sqlite3_free(
19150 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 )] or [sqlite3_r
19160 65 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 ealloc()]..*/.SQ
19170 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
19180 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e qlite3_malloc(in
19190 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 t);.SQLITE_API v
191a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 oid *sqlite3_rea
191b0 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 lloc(void*, int)
191c0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
191d0 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 d sqlite3_free(v
191e0 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid*);../*.** CA
191f0 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 PI3REF: Memory A
19200 6c 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74 llocator Statist
19210 69 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 ics {H17370} <S3
19220 30 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 0210>.**.** SQLi
19230 74 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73 te provides thes
19240 65 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73 e two interfaces
19250 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f for reporting o
19260 6e 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 n the status.**
19270 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f of the [sqlite3_
19280 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 malloc()], [sqli
19290 74 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 te3_free()], and
192a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
192b0 63 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 c()].** routines
192c0 2c 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65 , which form the
192d0 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 built-in memory
192e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
192f0 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 ystem..**.** Req
19300 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
19310 31 37 33 37 31 5d 20 5b 48 31 37 33 37 33 5d 20 17371] [H17373]
19320 5b 48 31 37 33 37 34 5d 20 5b 48 31 37 33 37 35 [H17374] [H17375
19330 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
19340 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
19350 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 lite3_memory_use
19360 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f d(void);.SQLITE_
19370 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 API sqlite3_int6
19380 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 4 sqlite3_memory
19390 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 _highwater(int r
193a0 65 73 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a esetFlag);../*.*
193b0 2a 20 43 41 50 49 33 52 45 46 3a 20 50 73 65 75 * CAPI3REF: Pseu
193c0 64 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 do-Random Number
193d0 20 47 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 33 Generator {H173
193e0 39 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 90} <S20000>.**.
193f0 2a 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69 ** SQLite contai
19400 6e 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74 ns a high-qualit
19410 79 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 y pseudo-random
19420 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
19430 20 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0a (PRNG) used to.
19440 2a 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d ** select random
19450 20 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 73 [ROWID | ROWIDs
19460 5d 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 ] when inserting
19470 20 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74 new records int
19480 6f 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a o a table that.*
19490 2a 20 61 6c 72 65 61 64 79 20 75 73 65 73 20 74 * already uses t
194a0 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 he largest possi
194b0 62 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68 ble [ROWID]. Th
194c0 65 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 e PRNG is also u
194d0 73 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 sed for.** the b
194e0 75 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 uild-in random()
194f0 20 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 and randomblob(
19500 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e ) SQL functions.
19510 20 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 This interface
19520 20 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 allows.** appli
19530 63 61 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 cations to acces
19540 73 20 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20 s the same PRNG
19550 66 6f 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73 for other purpos
19560 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c es..**.** A call
19570 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
19580 20 73 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20 stores N bytes
19590 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e of randomness in
195a0 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a to buffer P..**.
195b0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d ** The first tim
195c0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 e this routine i
195d0 73 20 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 s invoked (eithe
195e0 72 20 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 r internally or
195f0 62 79 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 by.** the applic
19600 61 74 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 ation) the PRNG
19610 69 73 20 73 65 65 64 65 64 20 75 73 69 6e 67 20 is seeded using
19620 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 randomness obtai
19630 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ned.** from the
19640 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 xRandomness meth
19650 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c od of the defaul
19660 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 t [sqlite3_vfs]
19670 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c object..** On al
19680 6c 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 l subsequent inv
19690 6f 63 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 ocations, the ps
196a0 65 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 eudo-randomness
196b0 69 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 is generated.**
196c0 69 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 internally and w
196d0 69 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20 ithout recourse
196e0 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f to the [sqlite3_
196f0 76 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 vfs] xRandomness
19700 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a .** method..**.*
19710 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
19720 2a 2a 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53 ** [H17392].*/.S
19730 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
19740 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
19750 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 s(int N, void *P
19760 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
19770 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 EF: Compile-Time
19780 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 Authorization C
19790 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30 allbacks {H12500
197a0 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S70100>.**.**
197b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
197c0 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f 72 gisters a author
197d0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 izer callback wi
197e0 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a th a particular.
197f0 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
19800 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 nection], suppli
19810 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 ed in the first
19820 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 argument..** The
19830 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
19840 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
19850 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 as SQL statement
19860 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 s are being comp
19870 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 iled.** by [sqli
19880 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f te3_prepare()] o
19890 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b r its variants [
198a0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
198b0 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 v2()],.** [sqlit
198c0 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 e3_prepare16()]
198d0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 and [sqlite3_pre
198e0 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 pare16_v2()]. A
198f0 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 t various.** poi
19900 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 63 nts during the c
19910 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 ompilation proce
19920 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20 ss, as logic is
19930 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a being created.**
19940 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 to perform vari
19950 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 ous actions, the
19960 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
19970 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
19980 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f to.** see if tho
19990 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61 se actions are a
199a0 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74 llowed. The aut
199b0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
199c0 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 should.** retur
199d0 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f n [SQLITE_OK] to
199e0 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f allow the actio
199f0 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 n, [SQLITE_IGNOR
19a00 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 E] to disallow t
19a10 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61 he.** specific a
19a20 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 ction but allow
19a30 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
19a40 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f t to continue to
19a50 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c be.** compiled,
19a60 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 or [SQLITE_DENY
19a70 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65 ] to cause the e
19a80 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d ntire SQL statem
19a90 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a ent to be.** rej
19aa0 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 ected with an er
19ab0 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 74 ror. If the aut
19ac0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
19ad0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 returns.** any
19ae0 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e value other than
19af0 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
19b00 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f , [SQLITE_OK], o
19b10 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a r [SQLITE_DENY].
19b20 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c ** then the [sql
19b30 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
19b40 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 )] or equivalent
19b50 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 call that trigg
19b60 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 ered.** the auth
19b70 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c orizer will fail
19b80 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d with an error m
19b90 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 essage..**.** Wh
19ba0 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 en the callback
19bb0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
19bc0 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 OK], that means
19bd0 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a the operation.**
19be0 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b requested is ok
19bf0 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c . When the call
19c00 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 back returns [SQ
19c10 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a LITE_DENY], the.
19c20 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ** [sqlite3_prep
19c30 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 are_v2()] or equ
19c40 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 ivalent call tha
19c50 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 0a t triggered the.
19c60 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 ** authorizer wi
19c70 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 ll fail with an
19c80 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78 error message ex
19c90 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a plaining that.**
19ca0 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65 access is denie
19cb0 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 d. .**.** The fi
19cc0 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f rst parameter to
19cd0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 the authorizer
19ce0 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f callback is a co
19cf0 70 79 20 6f 66 20 74 68 65 20 74 68 69 72 64 0a py of the third.
19d00 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 ** parameter to
19d10 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f the sqlite3_set_
19d20 61 75 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 authorizer() int
19d30 65 72 66 61 63 65 2e 20 54 68 65 20 73 65 63 6f erface. The seco
19d40 6e 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 nd parameter.**
19d50 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 to the callback
19d60 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 is an integer [S
19d70 51 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 QLITE_COPY | act
19d80 69 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73 ion code] that s
19d90 70 65 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 pecifies.** the
19da0 70 61 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f particular actio
19db0 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a n to be authoriz
19dc0 65 64 2e 20 54 68 65 20 74 68 69 72 64 20 74 68 ed. The third th
19dd0 72 6f 75 67 68 20 73 69 78 74 68 20 70 61 72 61 rough sixth para
19de0 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 meters.** to the
19df0 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65 callback are ze
19e00 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 ro-terminated st
19e10 72 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61 rings that conta
19e20 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a in additional.**
19e30 20 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74 details about t
19e40 68 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 he action to be
19e50 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a authorized..**.*
19e60 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 * If the action
19e70 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f code is [SQLITE_
19e80 52 45 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 READ].** and the
19e90 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
19ea0 73 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 s [SQLITE_IGNORE
19eb0 5d 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70 ] then the.** [p
19ec0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
19ed0 74 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 t] statement is
19ee0 63 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20 73 constructed to s
19ef0 75 62 73 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e ubstitute.** a N
19f00 55 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c 61 ULL value in pla
19f10 63 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 ce of the table
19f20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c column that woul
19f30 64 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72 d have.** been r
19f40 65 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f ead if [SQLITE_O
19f50 4b 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74 75 K] had been retu
19f60 72 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 rned. The [SQLI
19f70 54 45 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65 TE_IGNORE].** re
19f80 74 75 72 6e 20 63 61 6e 20 62 65 20 75 73 65 64 turn can be used
19f90 20 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74 72 to deny an untr
19fa0 75 73 74 65 64 20 75 73 65 72 20 61 63 63 65 73 usted user acces
19fb0 73 20 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a s to individual.
19fc0 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20 ** columns of a
19fd0 74 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 65 table..** If the
19fe0 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 action code is
19ff0 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d 20 [SQLITE_DELETE]
1a000 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b and the callback
1a010 20 72 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c returns.** [SQL
1a020 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e ITE_IGNORE] then
1a030 20 74 68 65 20 5b 44 45 4c 45 54 45 5d 20 6f 70 the [DELETE] op
1a040 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 eration proceeds
1a050 20 62 75 74 20 74 68 65 0a 2a 2a 20 5b 74 72 75 but the.** [tru
1a060 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 ncate optimizati
1a070 6f 6e 5d 20 69 73 20 64 69 73 61 62 6c 65 64 20 on] is disabled
1a080 61 6e 64 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 and all rows are
1a090 20 64 65 6c 65 74 65 64 20 69 6e 64 69 76 69 64 deleted individ
1a0a0 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 ually..**.** An
1a0b0 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 authorizer is us
1a0c0 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 ed when [sqlite3
1a0d0 5f 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 _prepare | prepa
1a0e0 72 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 ring].** SQL sta
1a0f0 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 tements from an
1a100 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 untrusted source
1a110 2c 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 , to ensure that
1a120 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
1a130 6e 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 nts.** do not tr
1a140 79 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 y to access data
1a150 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c they are not al
1a160 6c 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 lowed to see, or
1a170 20 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f that they do no
1a180 74 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 t.** try to exec
1a190 75 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 ute malicious st
1a1a0 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 atements that da
1a1b0 6d 61 67 65 20 74 68 65 20 64 61 74 61 62 61 73 mage the databas
1a1c0 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 e. For.** examp
1a1d0 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 le, an applicati
1a1e0 6f 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 on may allow a u
1a1f0 73 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 ser to enter arb
1a200 69 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 itrary.** SQL qu
1a210 65 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 eries for evalua
1a220 74 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 tion by a databa
1a230 73 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 se. But the app
1a240 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a lication does.**
1a250 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 not want the us
1a260 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f er to be able to
1a270 20 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 make arbitrary
1a280 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a changes to the.*
1a290 2a 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 * database. An
1a2a0 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 authorizer could
1a2b0 20 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 then be put in
1a2c0 70 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a place while the.
1a2d0 2a 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 ** user-entered
1a2e0 53 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 SQL is being [sq
1a2f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 lite3_prepare |
1a300 70 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a prepared] that.*
1a310 2a 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 * disallows ever
1a320 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 ything except [S
1a330 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 ELECT] statement
1a340 73 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 s..**.** Applica
1a350 74 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 tions that need
1a360 74 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 to process SQL f
1a370 72 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f rom untrusted so
1a380 75 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 urces.** might a
1a390 6c 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 lso consider low
1a3a0 65 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c ering resource l
1a3b0 69 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c imits using [sql
1a3c0 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a ite3_limit()].**
1a3d0 20 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 and limiting da
1a3e0 74 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e tabase size usin
1a3f0 67 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f g the [max_page_
1a400 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a count] [PRAGMA].
1a410 2a 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 ** in addition t
1a420 6f 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f o using an autho
1a430 72 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c rizer..**.** Onl
1a440 79 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f y a single autho
1a450 72 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 rizer can be in
1a460 70 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 place on a datab
1a470 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase connection.*
1a480 2a 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 * at a time. Ea
1a490 63 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ch call to sqlit
1a4a0 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 e3_set_authorize
1a4b0 72 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a r overrides the.
1a4c0 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c ** previous call
1a4d0 2e 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 . Disable the a
1a4e0 75 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 uthorizer by ins
1a4f0 74 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 talling a NULL c
1a500 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 allback..** The
1a510 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 authorizer is di
1a520 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c sabled by defaul
1a530 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 t..**.** The aut
1a540 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1a550 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 must not do any
1a560 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 thing that will
1a570 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 modify.** the da
1a580 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1a590 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 n that invoked t
1a5a0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
1a5b0 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 llback..** Note
1a5c0 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 that [sqlite3_pr
1a5d0 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 epare_v2()] and
1a5e0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1a5f0 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 both modify the
1a600 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 ir.** database c
1a610 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 onnections for t
1a620 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d he meaning of "m
1a630 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 odify" in this p
1a640 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 aragraph..**.**
1a650 57 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 When [sqlite3_pr
1a660 65 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 epare_v2()] is u
1a670 73 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 sed to prepare a
1a680 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a statement, the.
1a690 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 ** statement mig
1a6a0 68 74 20 62 65 20 72 65 2d 70 72 65 70 61 72 65 ht be re-prepare
1a6b0 64 20 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 d during [sqlite
1a6c0 33 5f 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 3_step()] due to
1a6d0 20 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 a .** schema ch
1a6e0 61 6e 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 ange. Hence, th
1a6f0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 e application sh
1a700 6f 75 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 ould ensure that
1a710 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 the.** correct
1a720 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
1a730 61 63 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 ack remains in p
1a740 6c 61 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 lace during the
1a750 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1a760 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
1a770 74 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 t the authorizer
1a780 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 callback is inv
1a790 6f 6b 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 oked only during
1a7a0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
1a7b0 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 pare()] or its v
1a7c0 61 72 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 ariants. Author
1a7d0 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a ization is not.*
1a7e0 2a 20 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 * performed duri
1a7f0 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 ng statement eva
1a800 6c 75 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 luation in [sqli
1a810 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e 6c te3_step()], unl
1a820 65 73 73 0a 2a 2a 20 61 73 20 73 74 61 74 65 64 ess.** as stated
1a830 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 in the previous
1a840 20 70 61 72 61 67 72 61 70 68 2c 20 73 71 6c 69 paragraph, sqli
1a850 74 65 33 5f 73 74 65 70 28 29 20 69 6e 76 6f 6b te3_step() invok
1a860 65 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 es.** sqlite3_pr
1a870 65 70 61 72 65 5f 76 32 28 29 20 74 6f 20 72 65 epare_v2() to re
1a880 70 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d prepare a statem
1a890 65 6e 74 20 61 66 74 65 72 20 61 20 73 63 68 65 ent after a sche
1a8a0 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a ma change..**.**
1a8b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
1a8c0 2a 20 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 35 * [H12501] [H125
1a8d0 30 32 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 02] [H12503] [H1
1a8e0 32 35 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 2504] [H12505] [
1a8f0 48 31 32 35 30 36 5d 20 5b 48 31 32 35 30 37 5d H12506] [H12507]
1a900 20 5b 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 [H12510].** [H1
1a910 32 35 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 2511] [H12512] [
1a920 48 31 32 35 32 30 5d 20 5b 48 31 32 35 32 31 5d H12520] [H12521]
1a930 20 5b 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c [H12522].*/.SQL
1a940 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1a950 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a te3_set_authoriz
1a960 65 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a er(. sqlite3*,.
1a970 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 int (*xAuth)(v
1a980 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 oid*,int,const c
1a990 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
1a9a0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
1a9b0 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 st char*),. voi
1a9c0 64 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a d *pUserData.);.
1a9d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
1a9e0 20 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75 Authorizer Retu
1a9f0 72 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 39 30 rn Codes {H12590
1aa00 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a } <H12500>.**.**
1aa10 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 The [sqlite3_se
1aa20 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 t_authorizer | a
1aa30 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 uthorizer callba
1aa40 63 6b 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 ck function] mus
1aa50 74 0a 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 t.** return eith
1aa60 65 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f er [SQLITE_OK] o
1aa70 72 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 r one of these t
1aa80 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 wo constants in
1aa90 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e order.** to sign
1aaa0 61 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 al SQLite whethe
1aab0 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 r or not the act
1aac0 69 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 ion is permitted
1aad0 2e 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 . See the.** [s
1aae0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f qlite3_set_autho
1aaf0 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a rizer | authoriz
1ab00 65 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e er documentation
1ab10 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c ] for additional
1ab20 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e .** information.
1ab30 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
1ab40 54 45 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a TE_DENY 1 /*
1ab50 20 41 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 Abort the SQL s
1ab60 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e tatement with an
1ab70 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e error */.#defin
1ab80 65 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 e SQLITE_IGNORE
1ab90 32 20 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 2 /* Don't all
1aba0 6f 77 20 61 63 63 65 73 73 2c 20 62 75 74 20 64 ow access, but d
1abb0 6f 6e 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e on't generate an
1abc0 20 65 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a error */../*.**
1abd0 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f CAPI3REF: Autho
1abe0 72 69 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 rizer Action Cod
1abf0 65 73 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 es {H12550} <H12
1ac00 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 500>.**.** The [
1ac10 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
1ac20 6f 72 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 orizer()] interf
1ac30 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 ace registers a
1ac40 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
1ac50 6e 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76 n.** that is inv
1ac60 6f 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a oked to authoriz
1ac70 65 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 e certain SQL st
1ac80 61 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e atement actions.
1ac90 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 The.** second
1aca0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
1acb0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 callback is an
1acc0 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 integer code tha
1acd0 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 t specifies.** w
1ace0 68 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65 hat action is be
1acf0 69 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 ing authorized.
1ad00 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20 69 These are the i
1ad10 6e 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f nteger action co
1ad20 64 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 des that.** the
1ad30 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
1ad40 61 63 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65 ack may be passe
1ad50 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 d..**.** These a
1ad60 63 74 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 ction code value
1ad70 73 20 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b s signify what k
1ad80 69 6e 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e ind of operation
1ad90 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 is to be.** aut
1ada0 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 horized. The 3r
1adb0 64 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 d and 4th parame
1adc0 74 65 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 ters to the auth
1add0 6f 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c orization.** cal
1ade0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 lback function w
1adf0 69 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 ill be parameter
1ae00 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 s or NULL depend
1ae10 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 ing on which of
1ae20 74 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 these.** codes i
1ae30 73 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65 s used as the se
1ae40 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 cond parameter.
1ae50 20 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 The 5th paramet
1ae60 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 er to the.** aut
1ae70 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b horizer callback
1ae80 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
1ae90 74 68 65 20 64 61 74 61 62 61 73 65 20 28 22 6d the database ("m
1aea0 61 69 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a ain", "temp",.**
1aeb0 20 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63 etc.) if applic
1aec0 61 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70 able. The 6th p
1aed0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
1aee0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
1aef0 61 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 ack.** is the na
1af00 6d 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d me of the inner-
1af10 6d 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 most trigger or
1af20 76 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 view that is res
1af30 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 ponsible for.**
1af40 74 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d the access attem
1af50 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 pt or NULL if th
1af60 69 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 is access attemp
1af70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 t is directly fr
1af80 6f 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 om.** top-level
1af90 53 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 SQL code..**.**
1afa0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
1afb0 20 5b 48 31 32 35 35 31 5d 20 5b 48 31 32 35 35 [H12551] [H1255
1afc0 32 5d 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 2] [H12553] [H12
1afd0 35 35 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 554].*/./*******
1afe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1aff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b000 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a **** 3rd *******
1b010 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a ***** 4th ******
1b020 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 *****/.#define S
1b030 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 QLITE_CREATE_IND
1b040 45 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 EX 1
1b050 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 /* Index Name
1b060 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
1b070 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b080 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c LITE_CREATE_TABL
1b090 45 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f E 2 /
1b0a0 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 * Table Name
1b0b0 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1b0c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b0d0 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
1b0e0 49 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a INDEX 3 /*
1b0f0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 Index Name
1b100 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b110 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b120 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 TE_CREATE_TEMP_T
1b130 41 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 ABLE 4 /*
1b140 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b150 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1b160 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b170 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 E_CREATE_TEMP_TR
1b180 49 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 IGGER 5 /* T
1b190 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 rigger Name T
1b1a0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a able Name *
1b1b0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b1c0 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 _CREATE_TEMP_VIE
1b1d0 57 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 W 6 /* Vi
1b1e0 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 ew Name NU
1b1f0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1b200 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b210 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 CREATE_TRIGGER
1b220 20 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 7 /* Tri
1b230 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 gger Name Tab
1b240 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a le Name */.
1b250 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
1b260 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 REATE_VIEW
1b270 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 8 /* View
1b280 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c Name NULL
1b290 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1b2a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
1b2b0 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 LETE
1b2c0 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 9 /* Table
1b2d0 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
1b2e0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1b2f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f efine SQLITE_DRO
1b300 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 P_INDEX
1b310 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 10 /* Index
1b320 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 Name Table
1b330 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1b340 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
1b350 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 _TABLE
1b360 20 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 11 /* Table N
1b370 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
1b380 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1b390 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
1b3a0 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 TEMP_INDEX
1b3b0 31 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 12 /* Index Na
1b3c0 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 me Table Na
1b3d0 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 me */.#defi
1b3e0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 ne SQLITE_DROP_T
1b3f0 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 EMP_TABLE 1
1b400 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 3 /* Table Nam
1b410 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 e NULL
1b420 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
1b430 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 e SQLITE_DROP_TE
1b440 4d 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 MP_TRIGGER 14
1b450 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 /* Trigger Na
1b460 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 me Table Name
1b470 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b480 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d SQLITE_DROP_TEM
1b490 50 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 P_VIEW 15
1b4a0 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 /* View Name
1b4b0 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
1b4c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b4d0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 SQLITE_DROP_TRIG
1b4e0 47 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 GER 16
1b4f0 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 /* Trigger Name
1b500 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 Table Name
1b510 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b520 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 QLITE_DROP_VIEW
1b530 20 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 17
1b540 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 /* View Name
1b550 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
1b560 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b570 4c 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 LITE_INSERT
1b580 20 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 18 /
1b590 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 * Table Name
1b5a0 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1b5b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b5c0 49 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 ITE_PRAGMA
1b5d0 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 19 /*
1b5e0 20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 Pragma Name
1b5f0 20 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 1st arg or NULL
1b600 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b610 54 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 TE_READ
1b620 20 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 20 /*
1b630 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b640 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 Column Name
1b650 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b660 45 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 E_SELECT
1b670 20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 21 /* N
1b680 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e ULL N
1b690 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b6a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b6b0 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 _TRANSACTION
1b6c0 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 22 /* Op
1b6d0 65 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 eration NU
1b6e0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1b6f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b700 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 UPDATE
1b710 20 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 23 /* Tab
1b720 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c le Name Col
1b730 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a umn Name */.
1b740 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
1b750 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 TTACH
1b760 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 24 /* File
1b770 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c name NULL
1b780 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1b790 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
1b7a0 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 TACH
1b7b0 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 25 /* Datab
1b7c0 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 ase Name NULL
1b7d0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1b7e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 efine SQLITE_ALT
1b7f0 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 ER_TABLE
1b800 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 26 /* Databa
1b810 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 se Name Table
1b820 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1b830 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e fine SQLITE_REIN
1b840 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 DEX
1b850 20 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 27 /* Index N
1b860 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
1b870 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1b880 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 ine SQLITE_ANALY
1b890 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ZE
1b8a0 32 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 28 /* Table Na
1b8b0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1b8c0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1b8d0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 ne SQLITE_CREATE
1b8e0 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 _VTABLE 2
1b8f0 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 9 /* Table Nam
1b900 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 e Module Na
1b910 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e me */.#defin
1b920 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 e SQLITE_DROP_VT
1b930 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 ABLE 30
1b940 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1b950 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d Module Nam
1b960 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 e */.#define
1b970 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e SQLITE_FUNCTION
1b980 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 31
1b990 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 /* NULL
1b9a0 20 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 Function Na
1b9b0 6d 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 me */.#define
1b9c0 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 SQLITE_SAVEPOINT
1b9d0 20 20 20 20 20 20 20 20 20 20 20 20 33 32 20 20 32
1b9e0 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 /* Operation
1b9f0 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 Savepoint Na
1ba00 6d 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 me */.#define S
1ba10 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 QLITE_COPY
1ba20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 0
1ba30 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 /* No longer use
1ba40 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 d */../*.** CAPI
1ba50 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 3REF: Tracing An
1ba60 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 d Profiling Func
1ba70 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c tions {H12280} <
1ba80 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S60400>.** EXPER
1ba90 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
1baa0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 ese routines reg
1bab0 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 ister callback f
1bac0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 unctions that ca
1bad0 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a n be used for.**
1bae0 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f tracing and pro
1baf0 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 filing the execu
1bb00 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 tion of SQL stat
1bb10 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ements..**.** Th
1bb20 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
1bb30 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 ion registered b
1bb40 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 y sqlite3_trace(
1bb50 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a ) is invoked at.
1bb60 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 ** various times
1bb70 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 when an SQL sta
1bb80 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 tement is being
1bb90 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f run by [sqlite3_
1bba0 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 step()]..** The
1bbb0 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
1bbc0 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 a UTF-8 renderi
1bbd0 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 ng of the SQL st
1bbe0 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 atement text.**
1bbf0 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 as the statement
1bc00 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 first begins ex
1bc10 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 ecuting. Additi
1bc20 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f onal callbacks o
1bc30 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 ccur.** as each
1bc40 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f triggered subpro
1bc50 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e gram is entered.
1bc60 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 The callbacks
1bc70 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 for triggers.**
1bc80 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 contain a UTF-8
1bc90 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 SQL comment that
1bca0 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
1bcb0 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 trigger..**.** T
1bcc0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
1bcd0 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 tion registered
1bce0 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 by sqlite3_profi
1bcf0 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a le() is invoked.
1bd00 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 ** as each SQL s
1bd10 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 tatement finishe
1bd20 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 s. The profile
1bd30 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e callback contain
1bd40 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 s.** the origina
1bd50 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 l statement text
1bd60 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 and an estimate
1bd70 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 of wall-clock t
1bd80 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f ime.** of how lo
1bd90 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e ng that statemen
1bda0 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a t took to run..*
1bdb0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
1bdc0 73 3a 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b s:.** [H12281] [
1bdd0 48 31 32 32 38 32 5d 20 5b 48 31 32 32 38 33 5d H12282] [H12283]
1bde0 20 5b 48 31 32 32 38 34 5d 20 5b 48 31 32 32 38 [H12284] [H1228
1bdf0 35 5d 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 5] [H12287] [H12
1be00 32 38 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 288] [H12289].**
1be10 20 5b 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c [H12290].*/.SQL
1be20 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 ITE_API SQLITE_E
1be30 58 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 XPERIMENTAL void
1be40 20 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 *sqlite3_trace(
1be50 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a sqlite3*, void(*
1be60 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f xTrace)(void*,co
1be70 6e 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 nst char*), void
1be80 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 *);.SQLITE_API S
1be90 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
1bea0 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 AL void *sqlite3
1beb0 5f 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 _profile(sqlite3
1bec0 2a 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f *,. void(*xPro
1bed0 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 file)(void*,cons
1bee0 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f t char*,sqlite3_
1bef0 75 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b uint64), void*);
1bf00 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
1bf10 3a 20 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 : Query Progress
1bf20 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 Callbacks {H129
1bf30 31 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 10} <S60400>.**.
1bf40 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1bf50 63 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c configures a cal
1bf60 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d lback function -
1bf70 20 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 the.** progress
1bf80 20 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 callback - that
1bf90 20 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 is invoked peri
1bfa0 6f 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 odically during
1bfb0 6c 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 long.** running
1bfc0 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
1bfd0 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 3_exec()], [sqli
1bfe0 74 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a te3_step()] and.
1bff0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f ** [sqlite3_get_
1c000 74 61 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 table()]. An ex
1c010 61 6d 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 ample use for th
1c020 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 is.** interface
1c030 69 73 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 is to keep a GUI
1c040 20 75 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 updated during
1c050 61 20 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a a large query..*
1c060 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 *.** If the prog
1c070 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 ress callback re
1c080 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 turns non-zero,
1c090 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 the operation is
1c0a0 0a 2a 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e .** interrupted.
1c0b0 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20 63 This feature c
1c0c0 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d an be used to im
1c0d0 70 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 plement a.** "Ca
1c0e0 6e 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 ncel" button on
1c0f0 61 20 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 a GUI progress d
1c100 69 61 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a ialog box..**.**
1c110 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 The progress ha
1c120 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 ndler must not d
1c130 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 o anything that
1c140 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 will modify.** t
1c150 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1c160 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f ection that invo
1c170 6b 65 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 ked the progress
1c180 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 handler..** Not
1c190 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f e that [sqlite3_
1c1a0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e prepare_v2()] an
1c1b0 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 d [sqlite3_step(
1c1c0 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 )] both modify t
1c1d0 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 heir.** database
1c1e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 connections for
1c1f0 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 the meaning of
1c200 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 "modify" in this
1c210 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a paragraph..**.*
1c220 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
1c230 2a 2a 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 ** [H12911] [H12
1c240 39 31 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 912] [H12913] [H
1c250 31 32 39 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 12914] [H12915]
1c260 5b 48 31 32 39 31 36 5d 20 5b 48 31 32 39 31 37 [H12916] [H12917
1c270 5d 20 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f ] [H12918].**.*/
1c280 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1c290 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 sqlite3_progres
1c2a0 73 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 s_handler(sqlite
1c2b0 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 3*, int, int(*)(
1c2c0 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a void*), void*);.
1c2d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
1c2e0 20 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 Opening A New D
1c2f0 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 atabase Connecti
1c300 6f 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 on {H12700} <S40
1c310 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 200>.**.** These
1c320 20 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 routines open a
1c330 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 n SQLite databas
1c340 65 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d e file whose nam
1c350 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 e is given by th
1c360 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 e.** filename ar
1c370 67 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 gument. The file
1c380 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 name argument is
1c390 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
1c3a0 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c UTF-8 for.** sql
1c3b0 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 ite3_open() and
1c3c0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 sqlite3_open_v2(
1c3d0 29 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 ) and as UTF-16
1c3e0 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 in the native by
1c3f0 74 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 te.** order for
1c400 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 sqlite3_open16()
1c410 2e 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f . A [database co
1c420 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 nnection] handle
1c430 20 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 is usually.** r
1c440 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 eturned in *ppDb
1c450 2c 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 , even if an err
1c460 6f 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 or occurs. The
1c470 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 only exception i
1c480 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c s that.** if SQL
1c490 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f ite is unable to
1c4a0 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 allocate memory
1c4b0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 to hold the [sq
1c4c0 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a lite3] object,.*
1c4d0 2a 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 * a NULL will be
1c4e0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 written into *p
1c4f0 70 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 pDb instead of a
1c500 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1c510 5b 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a [sqlite3].** obj
1c520 65 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 ect. If the data
1c530 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 base is opened (
1c540 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 and/or created)
1c550 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 successfully, th
1c560 65 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b en.** [SQLITE_OK
1c570 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 ] is returned.
1c580 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 Otherwise an [er
1c590 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
1c5a0 75 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b urned. The.** [
1c5b0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
1c5c0 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 ] or [sqlite3_er
1c5d0 72 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e rmsg16()] routin
1c5e0 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 es can be used t
1c5f0 6f 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 o obtain.** an E
1c600 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 nglish language
1c610 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 description of t
1c620 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 he error..**.**
1c630 54 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f The default enco
1c640 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 ding for the dat
1c650 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 abase will be UT
1c660 46 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 F-8 if.** sqlite
1c670 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 3_open() or sqli
1c680 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 te3_open_v2() is
1c690 20 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 called and.** U
1c6a0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 TF-16 in the nat
1c6b0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 ive byte order i
1c6c0 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 f sqlite3_open16
1c6d0 28 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a () is used..**.*
1c6e0 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 * Whether or not
1c6f0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1c700 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e when it is open
1c710 65 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a ed, resources.**
1c720 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
1c730 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
1c740 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c onnection] handl
1c750 65 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 e should be rele
1c760 61 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 ased by.** passi
1c770 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 ng it to [sqlite
1c780 33 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 3_close()] when
1c790 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 it is no longer
1c7a0 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 required..**.**
1c7b0 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e The sqlite3_open
1c7c0 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 _v2() interface
1c7d0 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 works like sqlit
1c7e0 65 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 e3_open().** exc
1c7f0 65 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 ept that it acce
1c800 70 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e pts two addition
1c810 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f al parameters fo
1c820 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e r additional con
1c830 74 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 trol.** over the
1c840 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f new database co
1c850 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 nnection. The f
1c860 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 lags parameter c
1c870 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a an take one of.*
1c880 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * the following
1c890 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 three values, op
1c8a0 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 tionally combine
1c8b0 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b d with the .** [
1c8c0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 SQLITE_OPEN_NOMU
1c8d0 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 TEX], [SQLITE_OP
1c8e0 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 5b EN_FULLMUTEX], [
1c8f0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 SQLITE_OPEN_SHAR
1c900 45 44 43 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e 64 EDCACHE],.** and
1c910 2f 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e /or [SQLITE_OPEN
1c920 5f 50 52 49 56 41 54 45 43 41 43 48 45 5d 20 66 _PRIVATECACHE] f
1c930 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e lags:.**.** <dl>
1c940 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f .** <dt>[SQLITE_
1c950 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f OPEN_READONLY]</
1c960 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 dt>.** <dd>The d
1c970 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 atabase is opene
1c980 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d d in read-only m
1c990 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 ode. If the dat
1c9a0 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a abase does not.*
1c9b0 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c * already exist,
1c9c0 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 an error is ret
1c9d0 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a urned.</dd>.**.*
1c9e0 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 * <dt>[SQLITE_OP
1c9f0 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 EN_READWRITE]</d
1ca00 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 t>.** <dd>The da
1ca10 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 tabase is opened
1ca20 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 for reading and
1ca30 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73 writing if poss
1ca40 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 ible, or reading
1ca50 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 .** only if the
1ca60 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 file is write pr
1ca70 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f otected by the o
1ca80 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e perating system.
1ca90 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 In either.** c
1caa0 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ase the database
1cab0 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 must already ex
1cac0 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61 ist, otherwise a
1cad0 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
1cae0 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
1caf0 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e <dt>[SQLITE_OPEN
1cb00 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 _READWRITE] | [S
1cb10 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
1cb20 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 E]</dt>.** <dd>T
1cb30 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f he database is o
1cb40 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e pened for readin
1cb50 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 g and writing, a
1cb60 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74 nd is creates it
1cb70 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e if.** it does n
1cb80 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ot already exist
1cb90 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65 . This is the be
1cba0 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 havior that is a
1cbb0 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a lways used for.*
1cbc0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 * sqlite3_open()
1cbd0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 and sqlite3_ope
1cbe0 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c n16().</dd>.** <
1cbf0 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 /dl>.**.** If th
1cc00 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 e 3rd parameter
1cc10 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f to sqlite3_open_
1cc20 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 v2() is not one
1cc30 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e of the.** combin
1cc40 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f ations shown abo
1cc50 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 ve or one of the
1cc60 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 combinations sh
1cc70 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e own above combin
1cc80 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b ed.** with the [
1cc90 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 SQLITE_OPEN_NOMU
1cca0 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 TEX], [SQLITE_OP
1ccb0 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a 2a EN_FULLMUTEX],.*
1ccc0 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 * [SQLITE_OPEN_S
1ccd0 48 41 52 45 44 43 41 43 48 45 5d 20 61 6e 64 2f HAREDCACHE] and/
1cce0 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f or [SQLITE_OPEN_
1ccf0 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61 SHAREDCACHE] fla
1cd00 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 gs,.** then the
1cd10 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
1cd20 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 fined..**.** If
1cd30 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e the [SQLITE_OPEN
1cd40 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 _NOMUTEX] flag i
1cd50 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 s set, then the
1cd60 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1cd70 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 ion.** opens in
1cd80 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 the multi-thread
1cd90 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 [threading mode
1cda0 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 ] as long as the
1cdb0 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a single-thread.*
1cdc0 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 * mode has not b
1cdd0 65 65 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 69 een set at compi
1cde0 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 le-time or start
1cdf0 2d 74 69 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a -time. If the.*
1ce00 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 * [SQLITE_OPEN_F
1ce10 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 ULLMUTEX] flag i
1ce20 73 20 73 65 74 20 74 68 65 6e 20 74 68 65 20 64 s set then the d
1ce30 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1ce40 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 on opens.** in t
1ce50 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 he serialized [t
1ce60 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 hreading mode] u
1ce70 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 nless single-thr
1ce80 65 61 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 ead was.** previ
1ce90 6f 75 73 6c 79 20 73 65 6c 65 63 74 65 64 20 61 ously selected a
1cea0 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f t compile-time o
1ceb0 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a r start-time..**
1cec0 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 The [SQLITE_OPE
1ced0 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 N_SHAREDCACHE] f
1cee0 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 20 64 lag causes the d
1cef0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1cf00 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67 on to be.** elig
1cf10 69 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61 ible to use [sha
1cf20 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c red cache mode],
1cf30 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 regardless of w
1cf40 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68 hether or not sh
1cf50 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 ared.** cache is
1cf60 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 5b enabled using [
1cf70 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 sqlite3_enable_s
1cf80 68 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e 20 hared_cache()].
1cf90 20 54 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f The.** [SQLITE_
1cfa0 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 OPEN_PRIVATECACH
1cfb0 45 5d 20 66 6c 61 67 20 63 61 75 73 65 73 20 74 E] flag causes t
1cfc0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
1cfd0 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a 2a ection to not.**
1cfe0 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 participate in
1cff0 5b 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f [shared cache mo
1d000 64 65 5d 20 65 76 65 6e 20 69 66 20 69 74 20 69 de] even if it i
1d010 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a s enabled..**.**
1d020 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 If the filename
1d030 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 is ":memory:",
1d040 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 20 then a private,
1d050 74 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d temporary in-mem
1d060 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ory database.**
1d070 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 is created for t
1d080 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 he connection.
1d090 54 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 This in-memory d
1d0a0 61 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e atabase will van
1d0b0 69 73 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 ish when.** the
1d0c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1d0d0 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 ion is closed.
1d0e0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
1d0f0 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a of SQLite might.
1d100 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 ** make use of a
1d110 64 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 dditional specia
1d120 6c 20 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 74 l filenames that
1d130 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 begin with the
1d140 22 3a 22 20 63 68 61 72 61 63 74 65 72 2e 0a 2a ":" character..*
1d150 2a 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e * It is recommen
1d160 64 65 64 20 74 68 61 74 20 77 68 65 6e 20 61 20 ded that when a
1d170 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d database filenam
1d180 65 20 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 20 e actually does
1d190 62 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 begin with.** a
1d1a0 22 3a 22 20 63 68 61 72 61 63 74 65 72 20 79 6f ":" character yo
1d1b0 75 20 73 68 6f 75 6c 64 20 70 72 65 66 69 78 20 u should prefix
1d1c0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 74 the filename wit
1d1d0 68 20 61 20 70 61 74 68 6e 61 6d 65 20 73 75 63 h a pathname suc
1d1e0 68 20 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 h as.** "./" to
1d1f0 61 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e avoid ambiguity.
1d200 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
1d210 6c 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 lename is an emp
1d220 74 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 ty string, then
1d230 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70 6f a private, tempo
1d240 72 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 rary.** on-disk
1d250 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 database will be
1d260 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20 created. This
1d270 70 72 69 76 61 74 65 20 64 61 74 61 62 61 73 65 private database
1d280 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f will be.** auto
1d290 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 matically delete
1d2a0 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 d as soon as the
1d2b0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1d2c0 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a tion is closed..
1d2d0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 **.** The fourth
1d2e0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
1d2f0 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 lite3_open_v2()
1d300 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
1d310 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 he.** [sqlite3_v
1d320 66 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 fs] object that
1d330 64 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72 defines the oper
1d340 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 ating system int
1d350 65 72 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 erface that.** t
1d360 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 he new database
1d370 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c connection shoul
1d380 64 20 75 73 65 2e 20 20 49 66 20 74 68 65 20 66 d use. If the f
1d390 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 ourth parameter
1d3a0 69 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 is.** a NULL poi
1d3b0 6e 74 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 nter then the de
1d3c0 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 fault [sqlite3_v
1d3d0 66 73 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73 fs] object is us
1d3e0 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 ed..**.** <b>Not
1d3f0 65 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 e to Windows use
1d400 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 rs:</b> The enc
1d410 6f 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 oding used for t
1d420 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 he filename argu
1d430 6d 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 ment.** of sqlit
1d440 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 e3_open() and sq
1d450 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 lite3_open_v2()
1d460 6d 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e must be UTF-8, n
1d470 6f 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 ot whatever.** c
1d480 6f 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65 odepage is curre
1d490 6e 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46 ntly defined. F
1d4a0 69 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e ilenames contain
1d4b0 69 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 ing internationa
1d4c0 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 l.** characters
1d4d0 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 must be converte
1d4e0 64 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 d to UTF-8 prior
1d4f0 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d to passing them
1d500 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 into.** sqlite3
1d510 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 _open() or sqlit
1d520 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a e3_open_v2()..**
1d530 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
1d540 3a 0a 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48 :.** [H12701] [H
1d550 31 32 37 30 32 5d 20 5b 48 31 32 37 30 33 5d 20 12702] [H12703]
1d560 5b 48 31 32 37 30 34 5d 20 5b 48 31 32 37 30 36 [H12704] [H12706
1d570 5d 20 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 37 ] [H12707] [H127
1d580 30 39 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 09] [H12711].**
1d590 5b 48 31 32 37 31 32 5d 20 5b 48 31 32 37 31 33 [H12712] [H12713
1d5a0 5d 20 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 37 ] [H12714] [H127
1d5b0 31 37 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 31 17] [H12719] [H1
1d5c0 32 37 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2721] [H12723].*
1d5d0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1d5e0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 sqlite3_open(.
1d5f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c const char *fil
1d600 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 ename, /* Data
1d610 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 base filename (U
1d620 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 TF-8) */. sqlit
1d630 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 e3 **ppDb
1d640 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 /* OUT: SQLit
1d650 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 e db handle */.)
1d660 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
1d670 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
1d680 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66 . const void *f
1d690 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 ilename, /* Da
1d6a0 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 tabase filename
1d6b0 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71 (UTF-16) */. sq
1d6c0 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 lite3 **ppDb
1d6d0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 /* OUT: SQ
1d6e0 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a Lite db handle *
1d6f0 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 /.);.SQLITE_API
1d700 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e int sqlite3_open
1d710 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 _v2(. const cha
1d720 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f r *filename, /
1d730 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e * Database filen
1d740 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 ame (UTF-8) */.
1d750 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c sqlite3 **ppDb,
1d760 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
1d770 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c SQLite db handl
1d780 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
1d790 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1d7a0 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e * Flags */. con
1d7b0 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 st char *zVfs
1d7c0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
1d7d0 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 VFS module to us
1d7e0 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 e */.);../*.** C
1d7f0 41 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20 43 API3REF: Error C
1d800 6f 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67 65 odes And Message
1d810 73 20 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 32 s {H12800} <S602
1d820 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 00>.**.** The sq
1d830 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20 lite3_errcode()
1d840 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
1d850 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 5b 72 s the numeric [r
1d860 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a esult code] or.*
1d870 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 * [extended resu
1d880 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 lt code] for the
1d890 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 66 61 69 most recent fai
1d8a0 6c 65 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 led sqlite3_* AP
1d8b0 49 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 I call.** associ
1d8c0 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 74 ated with a [dat
1d8d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1d8e0 5d 2e 20 49 66 20 61 20 70 72 69 6f 72 20 41 50 ]. If a prior AP
1d8f0 49 20 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a I call failed.**
1d900 20 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65 but the most re
1d910 63 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75 cent API call su
1d920 63 63 65 65 64 65 64 2c 20 74 68 65 20 72 65 74 cceeded, the ret
1d930 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a urn value from.*
1d940 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 * sqlite3_errcod
1d950 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 e() is undefined
1d960 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 . The sqlite3_e
1d970 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 xtended_errcode(
1d980 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 ).** interface i
1d990 73 20 74 68 65 20 73 61 6d 65 20 65 78 63 65 70 s the same excep
1d9a0 74 20 74 68 61 74 20 69 74 20 61 6c 77 61 79 73 t that it always
1d9b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a returns the .**
1d9c0 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c [extended resul
1d9d0 74 20 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 65 t code] even whe
1d9e0 6e 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c n extended resul
1d9f0 74 20 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64 t codes are.** d
1da00 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 isabled..**.** T
1da10 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 he sqlite3_errms
1da20 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f g() and sqlite3_
1da30 65 72 72 6d 73 67 31 36 28 29 20 72 65 74 75 72 errmsg16() retur
1da40 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61 n English-langua
1da50 67 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20 ge.** text that
1da60 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 describes the er
1da70 72 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20 55 ror, as either U
1da80 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 TF-8 or UTF-16 r
1da90 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 espectively..**
1daa0 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 Memory to hold t
1dab0 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
1dac0 20 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 string is manag
1dad0 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a ed internally..*
1dae0 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f * The applicatio
1daf0 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
1db00 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 66 to worry about f
1db10 72 65 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c reeing the resul
1db20 74 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 t..** However, t
1db30 68 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 he error string
1db40 6d 69 67 68 74 20 62 65 20 6f 76 65 72 77 72 69 might be overwri
1db50 74 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 tten or dealloca
1db60 74 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 ted by.** subseq
1db70 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 uent calls to ot
1db80 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 her SQLite inter
1db90 66 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a face functions..
1dba0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 **.** When the s
1dbb0 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 erialized [threa
1dbc0 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e ding mode] is in
1dbd0 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74 20 62 use, it might b
1dbe0 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 e the.** case th
1dbf0 61 74 20 61 20 73 65 63 6f 6e 64 20 65 72 72 6f at a second erro
1dc00 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 65 r occurs on a se
1dc10 70 61 72 61 74 65 20 74 68 72 65 61 64 20 69 6e parate thread in
1dc20 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 between.** the
1dc30 74 69 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73 time of the firs
1dc40 74 20 65 72 72 6f 72 20 61 6e 64 20 74 68 65 20 t error and the
1dc50 63 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 6e call to these in
1dc60 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65 terfaces..** Whe
1dc70 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 n that happens,
1dc80 74 68 65 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 the second error
1dc90 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 will be reporte
1dca0 64 20 73 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a d since these.**
1dcb0 20 69 6e 74 65 72 66 61 63 65 73 20 61 6c 77 61 interfaces alwa
1dcc0 79 73 20 72 65 70 6f 72 74 20 74 68 65 20 6d 6f ys report the mo
1dcd0 73 74 20 72 65 63 65 6e 74 20 72 65 73 75 6c 74 st recent result
1dce0 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 . To avoid.** t
1dcf0 68 69 73 2c 20 65 61 63 68 20 74 68 72 65 61 64 his, each thread
1dd00 20 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c can obtain excl
1dd10 75 73 69 76 65 20 75 73 65 20 6f 66 20 74 68 65 usive use of the
1dd20 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
1dd30 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 ction] D.** by i
1dd40 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 nvoking [sqlite3
1dd50 5f 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73 _mutex_enter]([s
1dd60 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d qlite3_db_mutex]
1dd70 28 44 29 29 20 62 65 66 6f 72 65 20 62 65 67 69 (D)) before begi
1dd80 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20 nning.** to use
1dd90 44 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b D and invoking [
1dda0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1ddb0 61 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 ave]([sqlite3_db
1ddc0 5f 6d 75 74 65 78 5d 28 44 29 29 20 61 66 74 65 _mutex](D)) afte
1ddd0 72 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74 r.** all calls t
1dde0 6f 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 73 o the interfaces
1ddf0 20 6c 69 73 74 65 64 20 68 65 72 65 20 61 72 65 listed here are
1de00 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a completed..**.*
1de10 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 66 61 63 * If an interfac
1de20 65 20 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c e fails with SQL
1de30 49 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 74 ITE_MISUSE, that
1de40 20 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 65 72 means the inter
1de50 66 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f face.** was invo
1de60 6b 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 ked incorrectly
1de70 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 by the applicati
1de80 6f 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 on. In that cas
1de90 65 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 e, the.** error
1dea0 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 code and message
1deb0 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 may or may not
1dec0 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 be set..**.** Re
1ded0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
1dee0 48 31 32 38 30 31 5d 20 5b 48 31 32 38 30 32 5d H12801] [H12802]
1def0 20 5b 48 31 32 38 30 33 5d 20 5b 48 31 32 38 30 [H12803] [H1280
1df00 37 5d 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 32 7] [H12808] [H12
1df10 38 30 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 809].*/.SQLITE_A
1df20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 PI int sqlite3_e
1df30 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a rrcode(sqlite3 *
1df40 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 db);.SQLITE_API
1df50 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 int sqlite3_exte
1df60 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c nded_errcode(sql
1df70 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 ite3 *db);.SQLIT
1df80 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
1df90 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 *sqlite3_errmsg
1dfa0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
1dfb0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1dfc0 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 d *sqlite3_errms
1dfd0 67 31 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a g16(sqlite3*);..
1dfe0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
1dff0 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 SQL Statement Ob
1e000 6a 65 63 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 ject {H13000} <H
1e010 31 33 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 13010>.** KEYWOR
1e020 44 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74 DS: {prepared st
1e030 61 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 atement} {prepar
1e040 65 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a ed statements}.*
1e050 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
1e060 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 of this object
1e070 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e represents a sin
1e080 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e gle SQL statemen
1e090 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 t..** This objec
1e0a0 74 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b t is variously k
1e0b0 6e 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 61 nown as a "prepa
1e0c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f red statement" o
1e0d0 72 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 r a.** "compiled
1e0e0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 SQL statement"
1e0f0 6f 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22 or simply as a "
1e100 73 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a statement"..**.*
1e110 2a 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 * The life of a
1e120 73 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 statement object
1e130 20 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 goes something
1e140 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a like this:.**.**
1e150 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 <ol>.** <li> Cr
1e160 65 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20 eate the object
1e170 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 using [sqlite3_p
1e180 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
1e190 61 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 a related.**
1e1a0 20 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c function..** <
1e1b0 6c 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20 li> Bind values
1e1c0 74 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 to [host paramet
1e1d0 65 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73 ers] using the s
1e1e0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a qlite3_bind_*().
1e1f0 2a 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 ** interfac
1e200 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 es..** <li> Run
1e210 74 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 the SQL by calli
1e220 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 ng [sqlite3_step
1e230 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ()] one or more
1e240 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 times..** <li> R
1e250 65 73 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 eset the stateme
1e260 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 nt using [sqlite
1e270 33 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20 3_reset()] then
1e280 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 go back.**
1e290 74 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74 to step 2. Do t
1e2a0 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 his zero or more
1e2b0 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 times..** <li>
1e2c0 44 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 Destroy the obje
1e2d0 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ct using [sqlite
1e2e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 3_finalize()]..*
1e2f0 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 * </ol>.**.** Re
1e300 66 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 fer to documenta
1e310 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 tion on individu
1e320 61 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 al methods above
1e330 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a for additional.
1e340 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a ** information..
1e350 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
1e360 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 t sqlite3_stmt s
1e370 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a qlite3_stmt;../*
1e380 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 .** CAPI3REF: Ru
1e390 6e 2d 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 n-time Limits {H
1e3a0 31 32 37 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 12760} <S20600>.
1e3b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
1e3c0 66 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 face allows the
1e3d0 73 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20 size of various
1e3e0 63 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 constructs to be
1e3f0 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 limited.** on a
1e400 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 connection by c
1e410 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e onnection basis.
1e420 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 The first para
1e430 6d 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 meter is the.**
1e440 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
1e450 74 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 tion] whose limi
1e460 74 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f t is to be set o
1e470 72 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a r queried. The.
1e480 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 ** second parame
1e490 74 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 ter is one of th
1e4a0 65 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 e [limit categor
1e4b0 69 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65 ies] that define
1e4c0 20 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 a.** class of c
1e4d0 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 onstructs to be
1e4e0 73 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 size limited. T
1e4f0 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 he third paramet
1e500 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 er is the.** new
1e510 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20 limit for that
1e520 63 6f 6e 73 74 72 75 63 74 2e 20 20 54 68 65 20 construct. The
1e530 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1e540 20 74 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a the old limit..
1e550 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 **.** If the new
1e560 20 6c 69 6d 69 74 20 69 73 20 61 20 6e 65 67 61 limit is a nega
1e570 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 tive number, the
1e580 20 6c 69 6d 69 74 20 69 73 20 75 6e 63 68 61 6e limit is unchan
1e590 67 65 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 ged..** For the
1e5a0 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 6f limit category o
1e5b0 66 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 f SQLITE_LIMIT_X
1e5c0 59 5a 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a YZ there is a .*
1e5d0 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 64 * [limits | hard
1e5e0 20 75 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a upper bound].**
1e5f0 20 73 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c set by a compil
1e600 65 2d 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63 e-time C preproc
1e610 65 73 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 essor macro name
1e620 64 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 d .** [limits |
1e630 53 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e SQLITE_MAX_XYZ].
1e640 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 .** (The "_LIMIT
1e650 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 _" in the name i
1e660 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d s changed to "_M
1e670 41 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 AX_".).** Attemp
1e680 74 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 ts to increase a
1e690 20 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 limit above its
1e6a0 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e hard upper boun
1e6b0 64 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c d are.** silentl
1e6c0 79 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 y truncated to t
1e6d0 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 he hard upper li
1e6e0 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 mit..**.** Run t
1e6f0 69 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 ime limits are i
1e700 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
1e710 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 in applications
1e720 74 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 that manage.** b
1e730 6f 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e oth their own in
1e740 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 ternal database
1e750 61 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 and also databas
1e760 65 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 es that are cont
1e770 72 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 rolled.** by unt
1e780 72 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 rusted external
1e790 73 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 sources. An exa
1e7a0 6d 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e mple application
1e7b0 20 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 might be a.** w
1e7c0 65 62 20 62 72 6f 77 73 65 72 20 74 68 61 74 20 eb browser that
1e7d0 68 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61 has its own data
1e7e0 62 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e bases for storin
1e7f0 67 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a g history and.**
1e800 20 73 65 70 61 72 61 74 65 20 64 61 74 61 62 61 separate databa
1e810 73 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 ses controlled b
1e820 79 20 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 y JavaScript app
1e830 6c 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f lications downlo
1e840 61 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 aded.** off the
1e850 49 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 Internet. The i
1e860 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 nternal database
1e870 73 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 s can be given t
1e880 68 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 he.** large, def
1e890 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 ault limits. Da
1e8a0 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20 tabases managed
1e8b0 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 by external sour
1e8c0 63 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 ces can.** be gi
1e8d0 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 ven much smaller
1e8e0 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64 limits designed
1e8f0 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65 to prevent a de
1e900 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a nial of service.
1e910 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65 ** attack. Deve
1e920 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73 lopers might als
1e930 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 o want to use th
1e940 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 e [sqlite3_set_a
1e950 75 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 uthorizer()].**
1e960 69 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75 72 interface to fur
1e970 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 ther control unt
1e980 72 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65 rusted SQL. The
1e990 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 size of the dat
1e9a0 61 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 abase.** created
1e9b0 20 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 by an untrusted
1e9c0 20 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 63 script can be c
1e9d0 6f 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 ontained using t
1e9e0 68 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f he.** [max_page_
1e9f0 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e count] [PRAGMA].
1ea00 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 .**.** New run-t
1ea10 69 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f ime limit catego
1ea20 72 69 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 ries may be adde
1ea30 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 d in future rele
1ea40 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ases..**.** Requ
1ea50 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
1ea60 32 37 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b 2762] [H12766] [
1ea70 48 31 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 H12769].*/.SQLIT
1ea80 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1ea90 33 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 3_limit(sqlite3*
1eaa0 2c 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 , int id, int ne
1eab0 77 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 wVal);../*.** CA
1eac0 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 PI3REF: Run-Time
1ead0 20 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65 Limit Categorie
1eae0 73 20 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 37 s {H12790} <H127
1eaf0 36 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 60>.** KEYWORDS:
1eb00 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 {limit category
1eb10 7d 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 } {limit categor
1eb20 69 65 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ies}.**.** These
1eb30 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e constants defin
1eb40 65 20 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 e various perfor
1eb50 6d 61 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 mance limits.**
1eb60 74 68 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 that can be lowe
1eb70 72 65 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 red at run-time
1eb80 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c using [sqlite3_l
1eb90 69 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 imit()]..** The
1eba0 73 79 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 synopsis of the
1ebb0 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 meanings of the
1ebc0 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 various limits i
1ebd0 73 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a s shown below..*
1ebe0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 * Additional inf
1ebf0 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 ormation is avai
1ec00 6c 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 lable at [limits
1ec10 20 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c | Limits in SQL
1ec20 69 74 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e ite]..**.** <dl>
1ec30 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
1ec40 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e IMIT_LENGTH</dt>
1ec50 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1ec60 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 mum size of any
1ec70 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f string or BLOB o
1ec80 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e r table row.<dd>
1ec90 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
1eca0 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 E_LIMIT_SQL_LENG
1ecb0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 TH</dt>.** <dd>T
1ecc0 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 he maximum lengt
1ecd0 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 h of an SQL stat
1ece0 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ement.</dd>.**.*
1ecf0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d * <dt>SQLITE_LIM
1ed00 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a IT_COLUMN</dt>.*
1ed10 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1ed20 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 m number of colu
1ed30 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 mns in a table d
1ed40 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 efinition or in
1ed50 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 the.** result se
1ed60 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 t of a [SELECT]
1ed70 6f 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e or the maximum n
1ed80 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
1ed90 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 in an index.**
1eda0 6f 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 or in an ORDER B
1edb0 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c Y or GROUP BY cl
1edc0 61 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ause.</dd>.**.**
1edd0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 <dt>SQLITE_LIMI
1ede0 54 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 T_EXPR_DEPTH</dt
1edf0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 >.** <dd>The max
1ee00 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 imum depth of th
1ee10 65 20 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 e parse tree on
1ee20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c any expression.<
1ee30 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
1ee40 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 QLITE_LIMIT_COMP
1ee50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e OUND_SELECT</dt>
1ee60 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1ee70 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 mum number of te
1ee80 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e rms in a compoun
1ee90 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 d SELECT stateme
1eea0 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c nt.</dd>.**.** <
1eeb0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
1eec0 56 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 VDBE_OP</dt>.**
1eed0 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 <dd>The maximum
1eee0 6e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 number of instru
1eef0 63 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 ctions in a virt
1ef00 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 ual machine prog
1ef10 72 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 ram.** used to i
1ef20 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 mplement an SQL
1ef30 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a statement.</dd>.
1ef40 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
1ef50 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f _LIMIT_FUNCTION_
1ef60 41 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e ARG</dt>.** <dd>
1ef70 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1ef80 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
1ef90 6f 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f on a function.</
1efa0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
1efb0 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 LITE_LIMIT_ATTAC
1efc0 48 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e HED</dt>.** <dd>
1efd0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1efe0 65 72 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20 er of [ATTACH |
1eff0 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
1f000 65 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 es].</dd>.**.**
1f010 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1f020 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 _LIKE_PATTERN_LE
1f030 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 NGTH</dt>.** <dd
1f040 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e >The maximum len
1f050 67 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 gth of the patte
1f060 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 rn argument to t
1f070 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 he [LIKE] or.**
1f080 5b 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 [GLOB] operators
1f090 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1f0a0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 >SQLITE_LIMIT_VA
1f0b0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 RIABLE_NUMBER</d
1f0c0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
1f0d0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
1f0e0 76 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 variables in an
1f0f0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 SQL statement th
1f100 61 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 at can.** be bou
1f110 6e 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c nd.</dd>.**.** <
1f120 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
1f130 54 52 49 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 TRIGGER_DEPTH</d
1f140 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
1f150 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 ximum depth of r
1f160 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 ecursion for tri
1f170 67 67 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c ggers.</dd>.** <
1f180 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 /dl>.*/.#define
1f190 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
1f1a0 47 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 GTH
1f1b0 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 0.#define
1f1c0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 SQLITE_LIMIT_SQ
1f1d0 4c 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 L_LENGTH
1f1e0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
1f1f0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 e SQLITE_LIMIT_C
1f200 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 OLUMN
1f210 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 2.#defi
1f220 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
1f230 45 58 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 EXPR_DEPTH
1f240 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 3.#def
1f250 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
1f260 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 _COMPOUND_SELECT
1f270 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 4.#de
1f280 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
1f290 54 5f 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 T_VDBE_OP
1f2a0 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 5.#d
1f2b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
1f2c0 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 IT_FUNCTION_ARG
1f2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 6.#
1f2e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 define SQLITE_LI
1f2f0 4d 49 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 MIT_ATTACHED
1f300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 7.
1f310 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
1f320 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 IMIT_LIKE_PATTER
1f330 4e 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 N_LENGTH 8
1f340 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1f350 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
1f360 55 4d 42 45 52 20 20 20 20 20 20 20 20 20 20 20 UMBER
1f370 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 9.#define SQLITE
1f380 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 _LIMIT_TRIGGER_D
1f390 45 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 EPTH
1f3a0 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 10../*.** CAPI3R
1f3b0 45 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e EF: Compiling An
1f3c0 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b SQL Statement {
1f3d0 48 31 33 30 31 30 7d 20 3c 53 31 30 30 30 30 3e H13010} <S10000>
1f3e0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 .** KEYWORDS: {S
1f3f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d QL statement com
1f400 70 69 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 piler}.**.** To
1f410 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20 71 execute an SQL q
1f420 75 65 72 79 2c 20 69 74 20 6d 75 73 74 20 66 69 uery, it must fi
1f430 72 73 74 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 rst be compiled
1f440 69 6e 74 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 into a byte-code
1f450 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73 69 6e .** program usin
1f460 67 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 72 g one of these r
1f470 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 outines..**.** T
1f480 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
1f490 74 2c 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64 t, "db", is a [d
1f4a0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1f4b0 6f 6e 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f on] obtained fro
1f4c0 6d 20 61 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 m a.** prior suc
1f4d0 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 cessful call to
1f4e0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
1f4f0 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f , [sqlite3_open_
1f500 76 32 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c v2()] or.** [sql
1f510 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 ite3_open16()].
1f520 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
1f530 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f nnection must no
1f540 74 20 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 t have been clos
1f550 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 ed..**.** The se
1f560 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 cond argument, "
1f570 7a 53 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74 zSql", is the st
1f580 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f atement to be co
1f590 6d 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a mpiled, encoded.
1f5a0 2a 2a 20 61 73 20 65 69 74 68 65 72 20 55 54 46 ** as either UTF
1f5b0 2d 38 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 -8 or UTF-16. T
1f5c0 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 he sqlite3_prepa
1f5d0 72 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 re() and sqlite3
1f5e0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a _prepare_v2().**
1f5f0 20 69 6e 74 65 72 66 61 63 65 73 20 75 73 65 20 interfaces use
1f600 55 54 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 UTF-8, and sqlit
1f610 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 20 61 e3_prepare16() a
1f620 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 nd sqlite3_prepa
1f630 72 65 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 re16_v2().** use
1f640 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 UTF-16..**.** I
1f650 66 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 f the nByte argu
1f660 6d 65 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 ment is less tha
1f670 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 n zero, then zSq
1f680 6c 20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 l is read up to
1f690 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 the.** first zer
1f6a0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66 o terminator. If
1f6b0 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 nByte is non-ne
1f6c0 67 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 gative, then it
1f6d0 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a is the maximum.*
1f6e0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 * number of byt
1f6f0 65 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 es read from zSq
1f700 6c 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 l. When nByte i
1f710 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 s non-negative,
1f720 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 the.** zSql stri
1f730 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 ng ends at eithe
1f740 72 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 r the first '\00
1f750 30 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 0' or '\u0000' c
1f760 68 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 haracter or.** t
1f770 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 he nByte-th byte
1f780 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 , whichever come
1f790 73 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 s first. If the
1f7a0 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 caller knows.**
1f7b0 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 that the supplie
1f7c0 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d d string is nul-
1f7d0 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e terminated, then
1f7e0 20 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c there is a smal
1f7f0 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 l.** performance
1f800 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 advantage to be
1f810 20 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 gained by passi
1f820 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 ng an nByte para
1f830 6d 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 meter that.** is
1f840 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 equal to the nu
1f850 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
1f860 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
1f870 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f g <i>including</
1f880 69 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 i>.** the nul-te
1f890 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a rminator bytes..
1f8a0 2a 2a 0a 2a 2a 20 49 66 20 70 7a 54 61 69 6c 20 **.** If pzTail
1f8b0 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e is not NULL then
1f8c0 20 2a 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 *pzTail is made
1f8d0 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 to point to the
1f8e0 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 70 first byte.** p
1f8f0 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 ast the end of t
1f900 68 65 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 he first SQL sta
1f910 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 tement in zSql.
1f920 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
1f930 6f 6e 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 only.** compile
1f940 74 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d the first statem
1f950 65 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 ent in zSql, so
1f960 2a 70 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 *pzTail is left
1f970 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 pointing to.** w
1f980 68 61 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f hat remains unco
1f990 6d 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 mpiled..**.** *p
1f9a0 70 53 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f pStmt is left po
1f9b0 69 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 inting to a comp
1f9c0 69 6c 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 iled [prepared s
1f9d0 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 tatement] that c
1f9e0 61 6e 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 an be.** execute
1f9f0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 d using [sqlite3
1fa00 5f 73 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 _step()]. If th
1fa10 65 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c ere is an error,
1fa20 20 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a *ppStmt is set.
1fa30 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 ** to NULL. If
1fa40 74 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 the input text c
1fa50 6f 6e 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 ontains no SQL (
1fa60 69 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 if the input is
1fa70 61 6e 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 an empty.** stri
1fa80 6e 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 ng or a comment)
1fa90 20 74 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 then *ppStmt is
1faa0 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a set to NULL..**
1fab0 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f The calling pro
1fac0 63 65 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e cedure is respon
1fad0 73 69 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 sible for deleti
1fae0 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a ng the compiled.
1faf0 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ** SQL statement
1fb00 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
1fb10 66 69 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 finalize()] afte
1fb20 72 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 r it has finishe
1fb30 64 20 77 69 74 68 20 69 74 2e 0a 2a 2a 20 70 70 d with it..** pp
1fb40 53 74 6d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 Stmt may not be
1fb50 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 NULL..**.** On s
1fb60 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f uccess, [SQLITE_
1fb70 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c OK] is returned,
1fb80 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 otherwise an [e
1fb90 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 rror code] is re
1fba0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 turned..**.** Th
1fbb0 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 e sqlite3_prepar
1fbc0 65 5f 76 32 28 29 20 61 6e 64 20 73 71 6c 69 74 e_v2() and sqlit
1fbd0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 e3_prepare16_v2(
1fbe0 29 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 ) interfaces are
1fbf0 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 .** recommended
1fc00 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 for all new prog
1fc10 72 61 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c rams. The two ol
1fc20 64 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 61 der interfaces a
1fc30 72 65 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 re retained.** f
1fc40 6f 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d or backwards com
1fc50 70 61 74 69 62 69 6c 69 74 79 2c 20 62 75 74 20 patibility, but
1fc60 74 68 65 69 72 20 75 73 65 20 69 73 20 64 69 73 their use is dis
1fc70 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 couraged..** In
1fc80 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 the "v2" interfa
1fc90 63 65 73 2c 20 74 68 65 20 70 72 65 70 61 72 65 ces, the prepare
1fca0 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 d statement.** t
1fcb0 68 61 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 hat is returned
1fcc0 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 (the [sqlite3_st
1fcd0 6d 74 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 mt] object) cont
1fce0 61 69 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 ains a copy of t
1fcf0 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 he.** original S
1fd00 51 4c 20 74 65 78 74 2e 20 54 68 69 73 20 63 61 QL text. This ca
1fd10 75 73 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65 uses the [sqlite
1fd20 33 5f 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66 3_step()] interf
1fd30 61 63 65 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 ace to.** behave
1fd40 20 61 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69 a differently i
1fd50 6e 20 74 77 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a n two ways:.**.*
1fd60 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a * <ol>.** <li>.*
1fd70 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
1fd80 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 e schema changes
1fd90 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 74 , instead of ret
1fda0 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45 5f 53 urning [SQLITE_S
1fdb0 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a 2a 20 CHEMA] as it.**
1fdc0 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f 20 64 always used to d
1fdd0 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 o, [sqlite3_step
1fde0 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 ()] will automat
1fdf0 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 ically recompile
1fe00 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74 61 74 the SQL.** stat
1fe10 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20 74 6f ement and try to
1fe20 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e 20 20 run it again.
1fe30 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 If the schema ha
1fe40 73 20 63 68 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 s changed in.**
1fe50 61 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73 a way that makes
1fe60 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6e the statement n
1fe70 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 2c 20 o longer valid,
1fe80 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
1fe90 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 will still.** r
1fea0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 53 43 eturn [SQLITE_SC
1feb0 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e 6c 69 HEMA]. But unli
1fec0 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20 62 65 ke the legacy be
1fed0 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54 45 5f havior, [SQLITE_
1fee0 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f SCHEMA] is.** no
1fef0 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e w a fatal error.
1ff00 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 Calling [sqlit
1ff10 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
1ff20 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 again will not
1ff30 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f make the.** erro
1ff40 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65 r go away. Note
1ff50 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 65 : use [sqlite3_e
1ff60 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e 64 rrmsg()] to find
1ff70 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20 the text.** of
1ff80 74 68 65 20 70 61 72 73 69 6e 67 20 65 72 72 6f the parsing erro
1ff90 72 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 69 r that results i
1ffa0 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 n an [SQLITE_SCH
1ffb0 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 EMA] return..**
1ffc0 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e </li>.**.** <li>
1ffd0 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72 72 6f .** When an erro
1ffe0 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 6c 69 74 r occurs, [sqlit
1fff0 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 e3_step()] will
20000 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 return one of th
20010 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 e detailed.** [e
20020 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b rror codes] or [
20030 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
20040 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c 65 67 61 odes]. The lega
20050 63 79 20 62 65 68 61 76 69 6f 72 20 77 61 73 20 cy behavior was
20060 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 that.** [sqlite3
20070 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f _step()] would o
20080 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67 65 6e nly return a gen
20090 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 52 eric [SQLITE_ERR
200a0 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64 65 0a OR] result code.
200b0 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64 ** and you would
200c0 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 have to make a
200d0 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b second call to [
200e0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
200f0 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 in order.** to
20100 66 69 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 find the underly
20110 69 6e 67 20 63 61 75 73 65 20 6f 66 20 74 68 65 ing cause of the
20120 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74 problem. With t
20130 68 65 20 22 76 32 22 20 70 72 65 70 61 72 65 0a he "v2" prepare.
20140 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 ** interfaces, t
20150 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 he underlying re
20160 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 72 ason for the err
20170 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 or is returned i
20180 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c mmediately..** <
20190 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a /li>.** </ol>.**
201a0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
201b0 3a 0a 2a 2a 20 5b 48 31 33 30 31 31 5d 20 5b 48 :.** [H13011] [H
201c0 31 33 30 31 32 5d 20 5b 48 31 33 30 31 33 5d 20 13012] [H13013]
201d0 5b 48 31 33 30 31 34 5d 20 5b 48 31 33 30 31 35 [H13014] [H13015
201e0 5d 20 5b 48 31 33 30 31 36 5d 20 5b 48 31 33 30 ] [H13016] [H130
201f0 31 39 5d 20 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a 19] [H13021].**.
20200 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
20210 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 t sqlite3_prepar
20220 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 e(. sqlite3 *db
20230 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
20240 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 Database handle
20250 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
20260 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 *zSql, /*
20270 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 SQL statement, U
20280 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a TF-8 encoded */.
20290 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
202a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
202b0 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a imum length of z
202c0 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f Sql in bytes. */
202d0 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 . sqlite3_stmt
202e0 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 **ppStmt, /* OU
202f0 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e T: Statement han
20300 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 dle */. const c
20310 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 har **pzTail
20320 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 /* OUT: Pointer
20330 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 to unused porti
20340 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b on of zSql */.);
20350 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
20360 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
20370 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 v2(. sqlite3 *d
20380 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b, /*
20390 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 Database handle
203a0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
203b0 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a *zSql, /*
203c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 SQL statement,
203d0 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f UTF-8 encoded */
203e0 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 . int nByte,
203f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
20400 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 ximum length of
20410 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a zSql in bytes. *
20420 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 /. sqlite3_stmt
20430 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f **ppStmt, /* O
20440 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 UT: Statement ha
20450 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
20460 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 char **pzTail
20470 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 /* OUT: Pointe
20480 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 r to unused port
20490 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 ion of zSql */.)
204a0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
204b0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
204c0 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 16(. sqlite3 *d
204d0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b, /*
204e0 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 Database handle
204f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
20500 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a *zSql, /*
20510 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 SQL statement,
20520 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a UTF-16 encoded *
20530 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 /. int nByte,
20540 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
20550 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 aximum length of
20560 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 zSql in bytes.
20570 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d */. sqlite3_stm
20580 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 t **ppStmt, /*
20590 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 OUT: Statement h
205a0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
205b0 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 void **pzTail
205c0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 /* OUT: Point
205d0 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 er to unused por
205e0 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a tion of zSql */.
205f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
20600 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 t sqlite3_prepar
20610 65 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 e16_v2(. sqlite
20620 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
20630 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 /* Database ha
20640 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
20650 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 void *zSql,
20660 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 /* SQL stateme
20670 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 nt, UTF-16 encod
20680 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 ed */. int nByt
20690 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
206a0 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 /* Maximum lengt
206b0 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 h of zSql in byt
206c0 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 es. */. sqlite3
206d0 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 _stmt **ppStmt,
206e0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 /* OUT: Stateme
206f0 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 nt handle */. c
20700 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 onst void **pzTa
20710 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 il /* OUT: P
20720 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 ointer to unused
20730 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c portion of zSql
20740 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 */.);../*.** CA
20750 50 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 69 PI3REF: Retrievi
20760 6e 67 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c ng Statement SQL
20770 20 7b 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30 {H13100} <H1300
20780 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 0>.**.** This in
20790 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 terface can be u
207a0 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 sed to retrieve
207b0 61 20 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20 a saved copy of
207c0 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 the original.**
207d0 53 51 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f SQL text used to
207e0 20 63 72 65 61 74 65 20 61 20 5b 70 72 65 70 61 create a [prepa
207f0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 red statement] i
20800 66 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 f that statement
20810 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 was.** compiled
20820 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 using either [s
20830 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
20840 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 2()] or [sqlite3
20850 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d _prepare16_v2()]
20860 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
20870 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 31 30 31 ents:.** [H13101
20880 5d 20 5b 48 31 33 31 30 32 5d 20 5b 48 31 33 31 ] [H13102] [H131
20890 30 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 03].*/.SQLITE_AP
208a0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
208b0 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 lite3_sql(sqlite
208c0 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 3_stmt *pStmt);.
208d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
208e0 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 54 79 70 Dynamically Typ
208f0 65 64 20 56 61 6c 75 65 20 4f 62 6a 65 63 74 20 ed Value Object
20900 7b 48 31 35 30 30 30 7d 20 3c 53 32 30 32 30 30 {H15000} <S20200
20910 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
20920 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
20930 33 5f 76 61 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 3_value} {unprot
20940 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
20950 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 lue}.**.** SQLit
20960 65 20 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 e uses the sqlit
20970 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 e3_value object
20980 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 6c 6c to represent all
20990 20 76 61 6c 75 65 73 0a 2a 2a 20 74 68 61 74 20 values.** that
209a0 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e can be stored in
209b0 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c a database tabl
209c0 65 2e 20 53 51 4c 69 74 65 20 75 73 65 73 20 64 e. SQLite uses d
209d0 79 6e 61 6d 69 63 20 74 79 70 69 6e 67 0a 2a 2a ynamic typing.**
209e0 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 73 20 for the values
209f0 69 74 20 73 74 6f 72 65 73 2e 20 56 61 6c 75 65 it stores. Value
20a00 73 20 73 74 6f 72 65 64 20 69 6e 20 73 71 6c 69 s stored in sqli
20a10 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
20a20 73 0a 2a 2a 20 63 61 6e 20 62 65 20 69 6e 74 65 s.** can be inte
20a30 67 65 72 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70 gers, floating p
20a40 6f 69 6e 74 20 76 61 6c 75 65 73 2c 20 73 74 72 oint values, str
20a50 69 6e 67 73 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 ings, BLOBs, or
20a60 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 NULL..**.** An s
20a70 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
20a80 65 63 74 20 6d 61 79 20 62 65 20 65 69 74 68 65 ect may be eithe
20a90 72 20 22 70 72 6f 74 65 63 74 65 64 22 20 6f 72 r "protected" or
20aa0 20 22 75 6e 70 72 6f 74 65 63 74 65 64 22 2e 0a "unprotected"..
20ab0 2a 2a 20 53 6f 6d 65 20 69 6e 74 65 72 66 61 63 ** Some interfac
20ac0 65 73 20 72 65 71 75 69 72 65 20 61 20 70 72 6f es require a pro
20ad0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
20ae0 61 6c 75 65 2e 20 20 4f 74 68 65 72 20 69 6e 74 alue. Other int
20af0 65 72 66 61 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 erfaces.** will
20b00 61 63 63 65 70 74 20 65 69 74 68 65 72 20 61 20 accept either a
20b10 70 72 6f 74 65 63 74 65 64 20 6f 72 20 61 6e 20 protected or an
20b20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 unprotected sqli
20b30 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 te3_value..** Ev
20b40 65 72 79 20 69 6e 74 65 72 66 61 63 65 20 74 68 ery interface th
20b50 61 74 20 61 63 63 65 70 74 73 20 73 71 6c 69 74 at accepts sqlit
20b60 65 33 5f 76 61 6c 75 65 20 61 72 67 75 6d 65 6e e3_value argumen
20b70 74 73 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 ts specifies.**
20b80 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69 whether or not i
20b90 74 20 72 65 71 75 69 72 65 73 20 61 20 70 72 6f t requires a pro
20ba0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
20bb0 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 alue..**.** The
20bc0 74 65 72 6d 73 20 22 70 72 6f 74 65 63 74 65 64 terms "protected
20bd0 22 20 61 6e 64 20 22 75 6e 70 72 6f 74 65 63 74 " and "unprotect
20be0 65 64 22 20 72 65 66 65 72 20 74 6f 20 77 68 65 ed" refer to whe
20bf0 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 ther or not.** a
20c00 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e 20 mutex is held.
20c10 20 41 20 69 6e 74 65 72 6e 61 6c 20 6d 75 74 65 A internal mute
20c20 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 20 x is held for a
20c30 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c protected.** sql
20c40 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
20c50 74 20 62 75 74 20 6e 6f 20 6d 75 74 65 78 20 69 t but no mutex i
20c60 73 20 68 65 6c 64 20 66 6f 72 20 61 6e 20 75 6e s held for an un
20c70 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c protected.** sql
20c80 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
20c90 74 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 t. If SQLite is
20ca0 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 62 65 20 compiled to be
20cb0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a single-threaded.
20cc0 2a 2a 20 28 77 69 74 68 20 5b 53 51 4c 49 54 45 ** (with [SQLITE
20cd0 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20 61 _THREADSAFE=0] a
20ce0 6e 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 nd with [sqlite3
20cf0 5f 74 68 72 65 61 64 73 61 66 65 28 29 5d 20 72 _threadsafe()] r
20d00 65 74 75 72 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f eturning 0).** o
20d10 72 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 72 r if SQLite is r
20d20 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 20 72 65 64 un in one of red
20d30 75 63 65 64 20 6d 75 74 65 78 20 6d 6f 64 65 73 uced mutex modes
20d40 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e .** [SQLITE_CON
20d50 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 FIG_SINGLETHREAD
20d60 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e ] or [SQLITE_CON
20d70 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d FIG_MULTITHREAD]
20d80 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 .** then there i
20d90 73 20 6e 6f 20 64 69 73 74 69 6e 63 74 69 6f 6e s no distinction
20da0 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63 74 between protect
20db0 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 ed and unprotect
20dc0 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 ed.** sqlite3_va
20dd0 6c 75 65 20 6f 62 6a 65 63 74 73 20 61 6e 64 20 lue objects and
20de0 74 68 65 79 20 63 61 6e 20 62 65 20 75 73 65 64 they can be used
20df0 20 69 6e 74 65 72 63 68 61 6e 67 65 61 62 6c 79 interchangeably
20e00 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 . However,.** f
20e10 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f 64 65 20 or maximum code
20e20 70 6f 72 74 61 62 69 6c 69 74 79 20 69 74 20 69 portability it i
20e30 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 s recommended th
20e40 61 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a at applications.
20e50 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b 65 20 74 68 ** still make th
20e60 65 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 e distinction be
20e70 74 77 65 65 6e 20 62 65 74 77 65 65 6e 20 70 72 tween between pr
20e80 6f 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 otected and unpr
20e90 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 otected.** sqlit
20ea0 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 e3_value objects
20eb0 20 65 76 65 6e 20 77 68 65 6e 20 6e 6f 74 20 73 even when not s
20ec0 74 72 69 63 74 6c 79 20 72 65 71 75 69 72 65 64 trictly required
20ed0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
20ee0 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
20ef0 73 20 74 68 61 74 20 61 72 65 20 70 61 73 73 65 s that are passe
20f00 64 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 d as parameters
20f10 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 6d 70 6c into the.** impl
20f20 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 61 ementation of [a
20f30 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
20f40 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 ed SQL functions
20f50 5d 20 61 72 65 20 70 72 6f 74 65 63 74 65 64 2e ] are protected.
20f60 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
20f70 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 72 65 74 value object ret
20f80 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c urned by.** [sql
20f90 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 ite3_column_valu
20fa0 65 28 29 5d 20 69 73 20 75 6e 70 72 6f 74 65 63 e()] is unprotec
20fb0 74 65 64 2e 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 ted..** Unprotec
20fc0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ted sqlite3_valu
20fd0 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 20 6f 6e e objects may on
20fe0 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68 0a ly be used with.
20ff0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 ** [sqlite3_resu
21000 6c 74 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20 lt_value()] and
21010 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 [sqlite3_bind_va
21020 6c 75 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b lue()]..** The [
21030 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c sqlite3_value_bl
21040 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c ob | sqlite3_val
21050 75 65 5f 74 79 70 65 28 29 5d 20 66 61 6d 69 6c ue_type()] famil
21060 79 20 6f 66 0a 2a 2a 20 69 6e 74 65 72 66 61 63 y of.** interfac
21070 65 73 20 72 65 71 75 69 72 65 20 70 72 6f 74 65 es require prote
21080 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
21090 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74 ue objects..*/.t
210a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 ypedef struct Me
210b0 6d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 3b m sqlite3_value;
210c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
210d0 3a 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 43 : SQL Function C
210e0 6f 6e 74 65 78 74 20 4f 62 6a 65 63 74 20 7b 48 ontext Object {H
210f0 31 36 30 30 31 7d 20 3c 53 32 30 32 30 30 3e 0a 16001} <S20200>.
21100 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 78 **.** The contex
21110 74 20 69 6e 20 77 68 69 63 68 20 61 6e 20 53 51 t in which an SQ
21120 4c 20 66 75 6e 63 74 69 6f 6e 20 65 78 65 63 75 L function execu
21130 74 65 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e tes is stored in
21140 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 an.** sqlite3_c
21150 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 2e 20 20 ontext object.
21160 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 A pointer to an
21170 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
21180 6f 62 6a 65 63 74 0a 2a 2a 20 69 73 20 61 6c 77 object.** is alw
21190 61 79 73 20 66 69 72 73 74 20 70 61 72 61 6d 65 ays first parame
211a0 74 65 72 20 74 6f 20 5b 61 70 70 6c 69 63 61 74 ter to [applicat
211b0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 ion-defined SQL
211c0 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54 functions]..** T
211d0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
211e0 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 efined SQL funct
211f0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ion implementati
21200 6f 6e 20 77 69 6c 6c 20 70 61 73 73 20 74 68 69 on will pass thi
21210 73 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 68 72 s.** pointer thr
21220 6f 75 67 68 20 69 6e 74 6f 20 63 61 6c 6c 73 20 ough into calls
21230 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 to [sqlite3_resu
21240 6c 74 5f 69 6e 74 20 7c 20 73 71 6c 69 74 65 33 lt_int | sqlite3
21250 5f 72 65 73 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b _result()],.** [
21260 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
21270 65 5f 63 6f 6e 74 65 78 74 28 29 5d 2c 20 5b 73 e_context()], [s
21280 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
21290 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ()],.** [sqlite3
212a0 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
212b0 6c 65 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f le()], [sqlite3_
212c0 67 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2c 0a get_auxdata()],.
212d0 2a 2a 20 61 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 ** and/or [sqlit
212e0 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 e3_set_auxdata()
212f0 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 ]..*/.typedef st
21300 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e ruct sqlite3_con
21310 74 65 78 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e text sqlite3_con
21320 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 text;../*.** CAP
21330 49 33 52 45 46 3a 20 42 69 6e 64 69 6e 67 20 56 I3REF: Binding V
21340 61 6c 75 65 73 20 54 6f 20 50 72 65 70 61 72 65 alues To Prepare
21350 64 20 53 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 d Statements {H1
21360 33 35 30 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 3500} <S70300>.*
21370 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 68 6f 73 * KEYWORDS: {hos
21380 74 20 70 61 72 61 6d 65 74 65 72 7d 20 7b 68 6f t parameter} {ho
21390 73 74 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b st parameters} {
213a0 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 6e host parameter n
213b0 61 6d 65 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 ame}.** KEYWORDS
213c0 3a 20 7b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 : {SQL parameter
213d0 7d 20 7b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 } {SQL parameter
213e0 73 7d 20 7b 70 61 72 61 6d 65 74 65 72 20 62 69 s} {parameter bi
213f0 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 nding}.**.** In
21400 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 73 20 the SQL strings
21410 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 input to [sqlite
21420 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
21430 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 and its variants
21440 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c 73 20 6d 61 ,.** literals ma
21450 79 20 62 65 20 72 65 70 6c 61 63 65 64 20 62 79 y be replaced by
21460 20 61 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 74 a [parameter] t
21470 68 61 74 20 6d 61 74 63 68 65 73 20 6f 6e 65 20 hat matches one
21480 6f 66 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 of following.**
21490 74 65 6d 70 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a templates:.**.**
214a0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f <ul>.** <li> ?
214b0 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a .** <li> ?NNN.*
214c0 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 * <li> :VVV.**
214d0 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c 6c <li> @VVV.** <l
214e0 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c i> $VVV.** </ul
214f0 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 74 >.**.** In the t
21500 65 6d 70 6c 61 74 65 73 20 61 62 6f 76 65 2c 20 emplates above,
21510 4e 4e 4e 20 72 65 70 72 65 73 65 6e 74 73 20 61 NNN represents a
21520 6e 20 69 6e 74 65 67 65 72 20 6c 69 74 65 72 61 n integer litera
21530 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 56 20 72 65 l,.** and VVV re
21540 70 72 65 73 65 6e 74 73 20 61 6e 20 61 6c 70 68 presents an alph
21550 61 6e 75 6d 65 72 69 63 20 69 64 65 6e 74 69 66 anumeric identif
21560 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 er. The values
21570 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 61 72 61 of these.** para
21580 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63 61 6c meters (also cal
21590 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61 6d 65 led "host parame
215a0 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 53 ter names" or "S
215b0 51 4c 20 70 61 72 61 6d 65 74 65 72 73 22 29 0a QL parameters").
215c0 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 75 73 ** can be set us
215d0 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f ing the sqlite3_
215e0 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 bind_*() routine
215f0 73 20 64 65 66 69 6e 65 64 20 68 65 72 65 2e 0a s defined here..
21600 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
21610 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
21620 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 sqlite3_bind_*()
21630 20 72 6f 75 74 69 6e 65 73 20 69 73 20 61 6c 77 routines is alw
21640 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 ays.** a pointer
21650 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 to the [sqlite3
21660 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72 65 _stmt] object re
21670 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b turned from.** [
21680 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
21690 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 v2()] or its var
216a0 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 iants..**.** The
216b0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
216c0 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
216d0 20 74 68 65 20 53 51 4c 20 70 61 72 61 6d 65 74 the SQL paramet
216e0 65 72 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a 2a er to be set..**
216f0 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 53 51 The leftmost SQ
21700 4c 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 L parameter has
21710 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 20 20 an index of 1.
21720 57 68 65 6e 20 74 68 65 20 73 61 6d 65 20 6e 61 When the same na
21730 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61 6d med.** SQL param
21740 65 74 65 72 20 69 73 20 75 73 65 64 20 6d 6f 72 eter is used mor
21750 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65 63 e than once, sec
21760 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 ond and subseque
21770 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63 65 nt.** occurrence
21780 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 s have the same
21790 69 6e 64 65 78 20 61 73 20 74 68 65 20 66 69 72 index as the fir
217a0 73 74 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a 2a st occurrence..*
217b0 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20 * The index for
217c0 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 73 named parameters
217d0 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 75 can be looked u
217e0 70 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b p using the.** [
217f0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
21800 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 20 ameter_index()]
21810 41 50 49 20 69 66 20 64 65 73 69 72 65 64 2e 20 API if desired.
21820 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66 6f The index.** fo
21830 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65 74 r "?NNN" paramet
21840 65 72 73 20 69 73 20 74 68 65 20 76 61 6c 75 65 ers is the value
21850 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20 of NNN..** The
21860 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 74 20 62 NNN value must b
21870 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 e between 1 and
21880 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d the [sqlite3_lim
21890 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65 74 it()].** paramet
218a0 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 er [SQLITE_LIMIT
218b0 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
218c0 5d 20 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 ] (default value
218d0 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 : 999)..**.** Th
218e0 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 e third argument
218f0 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f is the value to
21900 20 62 69 6e 64 20 74 6f 20 74 68 65 20 70 61 72 bind to the par
21910 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e ameter..**.** In
21920 20 74 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 those routines
21930 74 68 61 74 20 68 61 76 65 20 61 20 66 6f 75 72 that have a four
21940 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 73 th argument, its
21950 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a value is the.**
21960 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
21970 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 in the paramete
21980 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a r. To be clear:
21990 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 68 the value is th
219a0 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c e.** number of <
219b0 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74 u>bytes</u> in t
219c0 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68 he value, not th
219d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 e number of char
219e0 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74 68 acters..** If th
219f0 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 e fourth paramet
21a00 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 er is negative,
21a10 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 the length of th
21a20 65 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 74 e string is.** t
21a30 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
21a40 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 es up to the fir
21a50 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 st zero terminat
21a60 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 or..**.** The fi
21a70 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 fth argument to
21a80 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
21a90 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e b(), sqlite3_bin
21aa0 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a 2a d_text(), and.**
21ab0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 sqlite3_bind_te
21ac0 78 74 31 36 28 29 20 69 73 20 61 20 64 65 73 74 xt16() is a dest
21ad0 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64 ructor used to d
21ae0 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c ispose of the BL
21af0 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 20 OB or.** string
21b00 61 66 74 65 72 20 53 51 4c 69 74 65 20 68 61 73 after SQLite has
21b10 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 finished with i
21b20 74 2e 20 49 66 20 74 68 65 20 66 69 66 74 68 20 t. If the fifth
21b30 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 argument is.** t
21b40 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65 he special value
21b50 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d [SQLITE_STATIC]
21b60 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 73 , then SQLite as
21b70 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 0a 2a sumes that the.*
21b80 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 * information is
21b90 20 69 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d 61 in static, unma
21ba0 6e 61 67 65 64 20 73 70 61 63 65 20 61 6e 64 20 naged space and
21bb0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
21bc0 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 be freed..** If
21bd0 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75 6d the fifth argum
21be0 65 6e 74 20 68 61 73 20 74 68 65 20 76 61 6c 75 ent has the valu
21bf0 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 e [SQLITE_TRANSI
21c00 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 ENT], then.** SQ
21c10 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 73 20 6f Lite makes its o
21c20 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 wn private copy
21c30 6f 66 20 74 68 65 20 64 61 74 61 20 69 6d 6d 65 of the data imme
21c40 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 0a diately, before.
21c50 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 ** the sqlite3_b
21c60 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 20 ind_*() routine
21c70 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 returns..**.** T
21c80 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f he sqlite3_bind_
21c90 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74 69 zeroblob() routi
21ca0 6e 65 20 62 69 6e 64 73 20 61 20 42 4c 4f 42 20 ne binds a BLOB
21cb0 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 68 61 74 of length N that
21cc0 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 69 .** is filled wi
21cd0 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a 65 th zeroes. A ze
21ce0 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 20 66 69 roblob uses a fi
21cf0 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 xed amount of me
21d00 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61 6e mory.** (just an
21d10 20 69 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c 64 integer to hold
21d20 20 69 74 73 20 73 69 7a 65 29 20 77 68 69 6c 65 its size) while
21d30 20 69 74 20 69 73 20 62 65 69 6e 67 20 70 72 6f it is being pro
21d40 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62 cessed..** Zerob
21d50 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 6e 64 65 lobs are intende
21d60 64 20 74 6f 20 73 65 72 76 65 20 61 73 20 70 6c d to serve as pl
21d70 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 42 aceholders for B
21d80 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63 6f LOBs whose.** co
21d90 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 72 20 77 ntent is later w
21da0 72 69 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a 20 ritten using.**
21db0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 [sqlite3_blob_op
21dc0 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c en | incremental
21dd0 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74 69 BLOB I/O] routi
21de0 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74 69 nes..** A negati
21df0 76 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 ve value for the
21e00 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c 74 zeroblob result
21e10 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 s in a zero-leng
21e20 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 th BLOB..**.** T
21e30 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f he sqlite3_bind_
21e40 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 *() routines mus
21e50 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 t be called afte
21e60 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 r.** [sqlite3_pr
21e70 65 70 61 72 65 5f 76 32 28 29 5d 20 28 61 6e 64 epare_v2()] (and
21e80 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 6f its variants) o
21e90 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 r [sqlite3_reset
21ea0 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f 72 ()] and.** befor
21eb0 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 e [sqlite3_step(
21ec0 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73 20 )]..** Bindings
21ed0 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 65 64 20 are not cleared
21ee0 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f by the [sqlite3_
21ef0 72 65 73 65 74 28 29 5d 20 72 6f 75 74 69 6e 65 reset()] routine
21f00 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61 72 ..** Unbound par
21f10 61 6d 65 74 65 72 73 20 61 72 65 20 69 6e 74 65 ameters are inte
21f20 72 70 72 65 74 65 64 20 61 73 20 4e 55 4c 4c 2e rpreted as NULL.
21f30 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
21f40 74 69 6e 65 73 20 72 65 74 75 72 6e 20 5b 53 51 tines return [SQ
21f50 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 LITE_OK] on succ
21f60 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 ess or an error
21f70 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 code if.** anyth
21f80 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 ing goes wrong.
21f90 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20 [SQLITE_RANGE]
21fa0 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
21fb0 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 he parameter.**
21fc0 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20 index is out of
21fd0 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45 5f range. [SQLITE_
21fe0 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 6e NOMEM] is return
21ff0 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 ed if malloc() f
22000 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45 ails..** [SQLITE
22010 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 74 20 62 _MISUSE] might b
22020 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 e returned if th
22030 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
22040 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20 called on a.**
22050 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 virtual machine
22060 74 68 61 74 20 69 73 20 74 68 65 20 77 72 6f 6e that is the wron
22070 67 20 73 74 61 74 65 20 6f 72 20 77 68 69 63 68 g state or which
22080 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
22090 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 n finalized..**
220a0 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69 73 Detection of mis
220b0 75 73 65 20 69 73 20 75 6e 72 65 6c 69 61 62 6c use is unreliabl
220c0 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 e. Applications
220d0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70 65 should not depe
220e0 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45 5f nd.** on SQLITE_
220f0 4d 49 53 55 53 45 20 72 65 74 75 72 6e 73 2e 20 MISUSE returns.
22100 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 SQLITE_MISUSE i
22110 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 69 6e s intended to in
22120 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c 6f dicate a.** a lo
22130 67 69 63 20 65 72 72 6f 72 20 69 6e 20 74 68 65 gic error in the
22140 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 46 application. F
22150 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
22160 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a f SQLite might.*
22170 2a 20 70 61 6e 69 63 20 72 61 74 68 65 72 20 74 * panic rather t
22180 68 61 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 han return SQLIT
22190 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20 E_MISUSE..**.**
221a0 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 See also: [sqlit
221b0 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
221c0 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b r_count()],.** [
221d0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
221e0 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 ameter_name()],
221f0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e and [sqlite3_bin
22200 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 d_parameter_inde
22210 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 x()]..**.** Requ
22220 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
22230 33 35 30 36 5d 20 5b 48 31 33 35 30 39 5d 20 5b 3506] [H13509] [
22240 48 31 33 35 31 32 5d 20 5b 48 31 33 35 31 35 5d H13512] [H13515]
22250 20 5b 48 31 33 35 31 38 5d 20 5b 48 31 33 35 32 [H13518] [H1352
22260 31 5d 20 5b 48 31 33 35 32 34 5d 20 5b 48 31 33 1] [H13524] [H13
22270 35 32 37 5d 0a 2a 2a 20 5b 48 31 33 35 33 30 5d 527].** [H13530]
22280 20 5b 48 31 33 35 33 33 5d 20 5b 48 31 33 35 33 [H13533] [H1353
22290 36 5d 20 5b 48 31 33 35 33 39 5d 20 5b 48 31 33 6] [H13539] [H13
222a0 35 34 32 5d 20 5b 48 31 33 35 34 35 5d 20 5b 48 542] [H13545] [H
222b0 31 33 35 34 38 5d 20 5b 48 31 33 35 35 31 5d 0a 13548] [H13551].
222c0 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 **.*/.SQLITE_API
222d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
222e0 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 d_blob(sqlite3_s
222f0 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 tmt*, int, const
22300 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 void*, int n, v
22310 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
22320 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
22330 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 qlite3_bind_doub
22340 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a le(sqlite3_stmt*
22350 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a , int, double);.
22360 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
22370 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 qlite3_bind_int(
22380 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
22390 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
223a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
223b0 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 _bind_int64(sqli
223c0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
223d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a sqlite3_int64);.
223e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
223f0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c qlite3_bind_null
22400 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
22410 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
22420 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
22430 64 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 d_text(sqlite3_s
22440 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 tmt*, int, const
22450 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 char*, int n, v
22460 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
22470 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
22480 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 qlite3_bind_text
22490 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 16(sqlite3_stmt*
224a0 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 , int, const voi
224b0 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 d*, int, void(*)
224c0 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 (void*));.SQLITE
224d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
224e0 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 _bind_value(sqli
224f0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
22500 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 const sqlite3_va
22510 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
22520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
22530 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 nd_zeroblob(sqli
22540 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
22550 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 int n);../*.** C
22560 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 API3REF: Number
22570 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 74 65 72 Of SQL Parameter
22580 73 20 7b 48 31 33 36 30 30 7d 20 3c 53 37 30 33 s {H13600} <S703
22590 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 00>.**.** This r
225a0 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73 outine can be us
225b0 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e ed to find the n
225c0 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c 20 70 61 umber of [SQL pa
225d0 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 rameters].** in
225e0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
225f0 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20 70 61 72 ement]. SQL par
22600 61 6d 65 74 65 72 73 20 61 72 65 20 74 6f 6b 65 ameters are toke
22610 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 ns of the.** for
22620 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 m "?", "?NNN", "
22630 3a 41 41 41 22 2c 20 22 24 41 41 41 22 2c 20 6f :AAA", "$AAA", o
22640 72 20 22 40 41 41 41 22 20 74 68 61 74 20 73 65 r "@AAA" that se
22650 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61 63 65 68 rve as.** placeh
22660 6f 6c 64 65 72 73 20 66 6f 72 20 76 61 6c 75 65 olders for value
22670 73 20 74 68 61 74 20 61 72 65 20 5b 73 71 6c 69 s that are [sqli
22680 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 te3_bind_blob |
22690 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 bound].** to the
226a0 20 70 61 72 61 6d 65 74 65 72 73 20 61 74 20 61 parameters at a
226b0 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a later time..**.
226c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
226d0 61 63 74 75 61 6c 6c 79 20 72 65 74 75 72 6e 73 actually returns
226e0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
226f0 65 20 6c 61 72 67 65 73 74 20 28 72 69 67 68 74 e largest (right
22700 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61 6d 65 74 most).** paramet
22710 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66 6f 72 6d er. For all form
22720 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 s except ?NNN, t
22730 68 69 73 20 77 69 6c 6c 20 63 6f 72 72 65 73 70 his will corresp
22740 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75 ond to the.** nu
22750 6d 62 65 72 20 6f 66 20 75 6e 69 71 75 65 20 70 mber of unique p
22760 61 72 61 6d 65 74 65 72 73 2e 20 20 49 66 20 70 arameters. If p
22770 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 arameters of the
22780 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65 64 2c 0a ?NNN are used,.
22790 2a 2a 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 ** there may be
227a0 67 61 70 73 20 69 6e 20 74 68 65 20 6c 69 73 74 gaps in the list
227b0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
227c0 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f : [sqlite3_bind_
227d0 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e blob|sqlite3_bin
227e0 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 d()],.** [sqlite
227f0 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
22800 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a _name()], and.**
22810 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
22820 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 arameter_index()
22830 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
22840 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 30 ments:.** [H1360
22850 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 1].*/.SQLITE_API
22860 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
22870 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e d_parameter_coun
22880 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 t(sqlite3_stmt*)
22890 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
228a0 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f 73 F: Name Of A Hos
228b0 74 20 50 61 72 61 6d 65 74 65 72 20 7b 48 31 33 t Parameter {H13
228c0 36 32 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 620} <S70300>.**
228d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
228e0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
228f0 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f er to the name o
22900 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53 f the n-th.** [S
22910 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e QL parameter] in
22920 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
22930 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20 tement]..** SQL
22940 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 parameters of th
22950 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 e form "?NNN" or
22960 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 ":AAA" or "@AAA
22970 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 68 " or "$AAA".** h
22980 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69 63 68 ave a name which
22990 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 20 22 is the string "
229a0 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 ?NNN" or ":AAA"
229b0 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 or "@AAA" or "$A
229c0 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74 69 76 AA".** respectiv
229d0 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 ely..** In other
229e0 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74 words, the init
229f0 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f ial ":" or "$" o
22a00 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a 20 r "@" or "?".**
22a10 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 20 70 is included as p
22a20 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 2e art of the name.
22a30 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f .** Parameters o
22a40 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 20 77 f the form "?" w
22a50 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77 69 ithout a followi
22a60 6e 67 20 69 6e 74 65 67 65 72 20 68 61 76 65 20 ng integer have
22a70 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 61 no name.** and a
22a80 72 65 20 61 6c 73 6f 20 72 65 66 65 72 72 65 64 re also referred
22a90 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f 75 to as "anonymou
22aa0 73 20 70 61 72 61 6d 65 74 65 72 73 22 2e 0a 2a s parameters"..*
22ab0 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 68 *.** The first h
22ac0 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 61 ost parameter ha
22ad0 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2c s an index of 1,
22ae0 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 not 0..**.** If
22af0 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 73 20 the value n is
22b00 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 out of range or
22b10 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 72 61 if the n-th para
22b20 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 meter is.** name
22b30 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 less, then NULL
22b40 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 is returned. Th
22b50 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e e returned strin
22b60 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 69 g is.** always i
22b70 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 n UTF-8 encoding
22b80 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e 61 6d even if the nam
22b90 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 ed parameter was
22ba0 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 73 .** originally s
22bb0 70 65 63 69 66 69 65 64 20 61 73 20 55 54 46 2d pecified as UTF-
22bc0 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 70 16 in [sqlite3_p
22bd0 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 0a 2a repare16()] or.*
22be0 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
22bf0 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a re16_v2()]..**.*
22c00 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
22c10 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 ite3_bind_blob|s
22c20 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a qlite3_bind()],.
22c30 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 ** [sqlite3_bind
22c40 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 _parameter_count
22c50 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c ()], and.** [sql
22c60 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
22c70 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a ter_index()]..**
22c80 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
22c90 3a 0a 2a 2a 20 5b 48 31 33 36 32 31 5d 0a 2a 2f :.** [H13621].*/
22ca0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
22cb0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
22cc0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e bind_parameter_n
22cd0 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
22ce0 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *, int);../*.**
22cf0 43 41 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 CAPI3REF: Index
22d00 4f 66 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 Of A Parameter W
22d10 69 74 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 ith A Given Name
22d20 20 7b 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 {H13640} <S7030
22d30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 0>.**.** Return
22d40 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 the index of an
22d50 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 SQL parameter gi
22d60 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 ven its name. T
22d70 68 65 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 he.** index valu
22d80 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 e returned is su
22d90 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 itable for use a
22da0 73 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 s the second.**
22db0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 parameter to [sq
22dc0 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c lite3_bind_blob|
22dd0 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e sqlite3_bind()].
22de0 20 20 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 A zero.** is r
22df0 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 eturned if no ma
22e00 74 63 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 tching parameter
22e10 20 69 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 is found. The
22e20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d parameter.** nam
22e30 65 20 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 e must be given
22e40 69 6e 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 in UTF-8 even if
22e50 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 the original st
22e60 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 atement.** was p
22e70 72 65 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 repared from UTF
22e80 2d 31 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b -16 text using [
22e90 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
22ea0 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 6_v2()]..**.** S
22eb0 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 ee also: [sqlite
22ec0 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 3_bind_blob|sqli
22ed0 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 te3_bind()],.**
22ee0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
22ef0 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d rameter_count()]
22f00 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 , and.** [sqlite
22f10 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
22f20 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a _index()]..**.**
22f30 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
22f40 2a 20 5b 48 31 33 36 34 31 5d 0a 2a 2f 0a 53 51 * [H13641].*/.SQ
22f50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
22f60 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
22f70 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 ter_index(sqlite
22f80 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 3_stmt*, const c
22f90 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a har *zName);../*
22fa0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
22fb0 73 65 74 20 41 6c 6c 20 42 69 6e 64 69 6e 67 73 set All Bindings
22fc0 20 4f 6e 20 41 20 50 72 65 70 61 72 65 64 20 53 On A Prepared S
22fd0 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 36 36 30 tatement {H13660
22fe0 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a } <S70300>.**.**
22ff0 20 43 6f 6e 74 72 61 72 79 20 74 6f 20 74 68 65 Contrary to the
23000 20 69 6e 74 75 69 74 69 6f 6e 20 6f 66 20 6d 61 intuition of ma
23010 6e 79 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 ny, [sqlite3_res
23020 65 74 28 29 5d 20 64 6f 65 73 20 6e 6f 74 20 72 et()] does not r
23030 65 73 65 74 0a 2a 2a 20 74 68 65 20 5b 73 71 6c eset.** the [sql
23040 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c ite3_bind_blob |
23050 20 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 61 20 bindings] on a
23060 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
23070 65 6e 74 5d 2e 0a 2a 2a 20 55 73 65 20 74 68 69 ent]..** Use thi
23080 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 73 s routine to res
23090 65 74 20 61 6c 6c 20 68 6f 73 74 20 70 61 72 61 et all host para
230a0 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a meters to NULL..
230b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
230c0 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 36 31 5d 0a ts:.** [H13661].
230d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
230e0 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f t sqlite3_clear_
230f0 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 bindings(sqlite3
23100 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 _stmt*);../*.**
23110 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 CAPI3REF: Number
23120 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 Of Columns In A
23130 20 52 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 Result Set {H13
23140 37 31 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 710} <S10700>.**
23150 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
23160 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
23170 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
23180 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 et returned by t
23190 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 he.** [prepared
231a0 73 74 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 statement]. This
231b0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
231c0 20 30 20 69 66 20 70 53 74 6d 74 20 69 73 20 61 0 if pStmt is a
231d0 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 n SQL.** stateme
231e0 6e 74 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 nt that does not
231f0 20 72 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f return data (fo
23200 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 r example an [UP
23210 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 DATE])..**.** Re
23220 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
23230 48 31 33 37 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54 H13711].*/.SQLIT
23240 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
23250 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 3_column_count(s
23260 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
23270 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 mt);../*.** CAPI
23280 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 3REF: Column Nam
23290 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 es In A Result S
232a0 65 74 20 7b 48 31 33 37 32 30 7d 20 3c 53 31 30 et {H13720} <S10
232b0 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 700>.**.** These
232c0 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e routines return
232d0 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e the name assign
232e0 65 64 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c ed to a particul
232f0 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 ar column.** in
23300 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f the result set o
23310 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 f a [SELECT] sta
23320 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73 71 6c tement. The sql
23330 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
23340 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 ().** interface
23350 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
23360 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d r to a zero-term
23370 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 inated UTF-8 str
23380 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 ing.** and sqlit
23390 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 e3_column_name16
233a0 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 () returns a poi
233b0 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 nter to a zero-t
233c0 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 erminated.** UTF
233d0 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65 -16 string. The
233e0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
233f0 20 69 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 is the [prepare
23400 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 d statement].**
23410 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 that implements
23420 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 the [SELECT] sta
23430 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f tement. The seco
23440 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 nd parameter is
23450 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 the.** column nu
23460 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 66 74 6d mber. The leftm
23470 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 ost column is nu
23480 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 mber 0..**.** Th
23490 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e e returned strin
234a0 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c g pointer is val
234b0 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 id until either
234c0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
234d0 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 atement].** is d
234e0 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c estroyed by [sql
234f0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d ite3_finalize()]
23500 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 or until the ne
23510 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 xt call to.** sq
23520 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
23530 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 e() or sqlite3_c
23540 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 6f olumn_name16() o
23550 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d n the same colum
23560 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 n..**.** If sqli
23570 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 te3_malloc() fai
23580 6c 73 20 64 75 72 69 6e 67 20 74 68 65 20 70 72 ls during the pr
23590 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 69 74 68 ocessing of eith
235a0 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 er routine.** (f
235b0 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 72 69 6e or example durin
235c0 67 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 g a conversion f
235d0 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46 rom UTF-8 to UTF
235e0 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 4e -16) then a.** N
235f0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 ULL pointer is r
23600 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
23610 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 73 he name of a res
23620 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 ult column is th
23630 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 e value of the "
23640 41 53 22 20 63 6c 61 75 73 65 20 66 6f 72 0a 2a AS" clause for.*
23650 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 * that column, i
23660 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 41 53 f there is an AS
23670 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 clause. If the
23680 72 65 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 75 re is no AS clau
23690 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e se.** then the n
236a0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d ame of the colum
236b0 6e 20 69 73 20 75 6e 73 70 65 63 69 66 69 65 64 n is unspecified
236c0 20 61 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 20 and may change
236d0 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65 from.** one rele
236e0 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20 74 6f ase of SQLite to
236f0 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a the next..**.**
23700 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
23710 2a 20 5b 48 31 33 37 32 31 5d 20 5b 48 31 33 37 * [H13721] [H137
23720 32 33 5d 20 5b 48 31 33 37 32 34 5d 20 5b 48 31 23] [H13724] [H1
23730 33 37 32 35 5d 20 5b 48 31 33 37 32 36 5d 20 5b 3725] [H13726] [
23740 48 31 33 37 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54 H13727].*/.SQLIT
23750 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 E_API const char
23760 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
23770 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 _name(sqlite3_st
23780 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c mt*, int N);.SQL
23790 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
237a0 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
237b0 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 mn_name16(sqlite
237c0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 3_stmt*, int N);
237d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
237e0 3a 20 53 6f 75 72 63 65 20 4f 66 20 44 61 74 61 : Source Of Data
237f0 20 49 6e 20 41 20 51 75 65 72 79 20 52 65 73 75 In A Query Resu
23800 6c 74 20 7b 48 31 33 37 34 30 7d 20 3c 53 31 30 lt {H13740} <S10
23810 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 700>.**.** These
23820 20 72 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 routines provid
23830 65 20 61 20 6d 65 61 6e 73 20 74 6f 20 64 65 74 e a means to det
23840 65 72 6d 69 6e 65 20 77 68 61 74 20 63 6f 6c 75 ermine what colu
23850 6d 6e 20 6f 66 20 77 68 61 74 0a 2a 2a 20 74 61 mn of what.** ta
23860 62 6c 65 20 69 6e 20 77 68 69 63 68 20 64 61 74 ble in which dat
23870 61 62 61 73 65 20 61 20 72 65 73 75 6c 74 20 6f abase a result o
23880 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 f a [SELECT] sta
23890 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f tement comes fro
238a0 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f m..** The name o
238b0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f f the database o
238c0 72 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d r table or colum
238d0 6e 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 n can be returne
238e0 64 20 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61 d as.** either a
238f0 20 55 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 UTF-8 or UTF-16
23900 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 5f 64 string. The _d
23910 61 74 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65 atabase_ routine
23920 73 20 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 s return.** the
23930 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74 database name, t
23940 68 65 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69 he _table_ routi
23950 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 74 nes return the t
23960 61 62 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a able name, and.*
23970 2a 20 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f * the origin_ ro
23980 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 utines return th
23990 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a e column name..*
239a0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 * The returned s
239b0 74 72 69 6e 67 20 69 73 20 76 61 6c 69 64 20 75 tring is valid u
239c0 6e 74 69 6c 20 74 68 65 20 5b 70 72 65 70 61 72 ntil the [prepar
239d0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 73 ed statement] is
239e0 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 75 73 destroyed.** us
239f0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e ing [sqlite3_fin
23a00 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 alize()] or unti
23a10 6c 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 l the same infor
23a20 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 mation is reques
23a30 74 65 64 0a 2a 2a 20 61 67 61 69 6e 20 69 6e 20 ted.** again in
23a40 61 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f a different enco
23a50 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ding..**.** The
23a60 6e 61 6d 65 73 20 72 65 74 75 72 6e 65 64 20 61 names returned a
23a70 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 re the original
23a80 75 6e 2d 61 6c 69 61 73 65 64 20 6e 61 6d 65 73 un-aliased names
23a90 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 of the.** datab
23aa0 61 73 65 2c 20 74 61 62 6c 65 2c 20 61 6e 64 20 ase, table, and
23ab0 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 column..**.** Th
23ac0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
23ad0 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e to the followin
23ae0 67 20 63 61 6c 6c 73 20 69 73 20 61 20 5b 70 72 g calls is a [pr
23af0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
23b00 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 ]..** These func
23b10 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 69 6e 66 tions return inf
23b20 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
23b30 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 he Nth column re
23b40 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 74 68 65 turned by.** the
23b50 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 68 65 72 statement, wher
23b60 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e e N is the secon
23b70 64 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d d function argum
23b80 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ent..**.** If th
23b90 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 e Nth column ret
23ba0 75 72 6e 65 64 20 62 79 20 74 68 65 20 73 74 61 urned by the sta
23bb0 74 65 6d 65 6e 74 20 69 73 20 61 6e 20 65 78 70 tement is an exp
23bc0 72 65 73 73 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75 ression or.** su
23bd0 62 71 75 65 72 79 20 61 6e 64 20 69 73 20 6e 6f bquery and is no
23be0 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 t a column value
23bf0 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68 , then all of th
23c00 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 ese functions re
23c10 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54 turn.** NULL. T
23c20 68 65 73 65 20 72 6f 75 74 69 6e 65 20 6d 69 67 hese routine mig
23c30 68 74 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 4e ht also return N
23c40 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 ULL if a memory
23c50 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
23c60 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 4f 74 68 .** occurs. Oth
23c70 65 72 77 69 73 65 2c 20 74 68 65 79 20 72 65 74 erwise, they ret
23c80 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 urn the name of
23c90 74 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74 the attached dat
23ca0 61 62 61 73 65 2c 20 74 61 62 6c 65 0a 2a 2a 20 abase, table.**
23cb0 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 and column that
23cc0 71 75 65 72 79 20 72 65 73 75 6c 74 20 63 6f 6c query result col
23cd0 75 6d 6e 20 77 61 73 20 65 78 74 72 61 63 74 65 umn was extracte
23ce0 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 d from..**.** As
23cf0 20 77 69 74 68 20 61 6c 6c 20 6f 74 68 65 72 20 with all other
23d00 53 51 4c 69 74 65 20 41 50 49 73 2c 20 74 68 6f SQLite APIs, tho
23d10 73 65 20 70 6f 73 74 66 69 78 65 64 20 77 69 74 se postfixed wit
23d20 68 20 22 31 36 22 20 72 65 74 75 72 6e 0a 2a 2a h "16" return.**
23d30 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 UTF-16 encoded
23d40 73 74 72 69 6e 67 73 2c 20 74 68 65 20 6f 74 68 strings, the oth
23d50 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 er functions ret
23d60 75 72 6e 20 55 54 46 2d 38 2e 20 7b 45 4e 44 7d urn UTF-8. {END}
23d70 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 41 50 49 .**.** These API
23d80 73 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c s are only avail
23d90 61 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 able if the libr
23da0 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 ary was compiled
23db0 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 with the.** [SQ
23dc0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 LITE_ENABLE_COLU
23dd0 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 MN_METADATA] C-p
23de0 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 reprocessor symb
23df0 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a ol defined..**.*
23e00 2a 20 7b 41 31 33 37 35 31 7d 0a 2a 2a 20 49 66 * {A13751}.** If
23e10 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72 two or more thr
23e20 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 eads call one or
23e30 20 6d 6f 72 65 20 6f 66 20 74 68 65 73 65 20 72 more of these r
23e40 6f 75 74 69 6e 65 73 20 61 67 61 69 6e 73 74 20 outines against
23e50 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70 72 65 70 the same.** prep
23e60 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 ared statement a
23e70 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 20 74 68 65 nd column at the
23e80 20 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 same time then
23e90 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 0a the results are.
23ea0 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a ** undefined..**
23eb0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
23ec0 3a 0a 2a 2a 20 5b 48 31 33 37 34 31 5d 20 5b 48 :.** [H13741] [H
23ed0 31 33 37 34 32 5d 20 5b 48 31 33 37 34 33 5d 20 13742] [H13743]
23ee0 5b 48 31 33 37 34 34 5d 20 5b 48 31 33 37 34 35 [H13744] [H13745
23ef0 5d 20 5b 48 31 33 37 34 36 5d 20 5b 48 31 33 37 ] [H13746] [H137
23f00 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 48].**.** If two
23f10 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73 or more threads
23f20 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 call one or mor
23f30 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f e.** [sqlite3_co
23f40 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 lumn_database_na
23f50 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 me | column meta
23f60 64 61 74 61 20 69 6e 74 65 72 66 61 63 65 73 5d data interfaces]
23f70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 .** for the same
23f80 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
23f90 6d 65 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 ment] and result
23fa0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 20 74 68 column.** at th
23fb0 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e e same time then
23fc0 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 the results are
23fd0 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 undefined..*/.S
23fe0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
23ff0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
24000 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 lumn_database_na
24010 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a me(sqlite3_stmt*
24020 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ,int);.SQLITE_AP
24030 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
24040 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 lite3_column_dat
24050 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c abase_name16(sql
24060 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b ite3_stmt*,int);
24070 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
24080 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
24090 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d column_table_nam
240a0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
240b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
240c0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
240d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c ite3_column_tabl
240e0 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 e_name16(sqlite3
240f0 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c _stmt*,int);.SQL
24100 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
24110 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ar *sqlite3_colu
24120 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73 mn_origin_name(s
24130 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 qlite3_stmt*,int
24140 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
24150 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
24160 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 3_column_origin_
24170 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 name16(sqlite3_s
24180 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a tmt*,int);../*.*
24190 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c * CAPI3REF: Decl
241a0 61 72 65 64 20 44 61 74 61 74 79 70 65 20 4f 66 ared Datatype Of
241b0 20 41 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 A Query Result
241c0 7b 48 31 33 37 36 30 7d 20 3c 53 31 30 37 30 30 {H13760} <S10700
241d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 >.**.** The firs
241e0 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 t parameter is a
241f0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
24200 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 69 ment]..** If thi
24210 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 s statement is a
24220 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d [SELECT] statem
24230 65 6e 74 20 61 6e 64 20 74 68 65 20 4e 74 68 20 ent and the Nth
24240 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a column of the.**
24250 20 72 65 74 75 72 6e 65 64 20 72 65 73 75 6c 74 returned result
24260 20 73 65 74 20 6f 66 20 74 68 61 74 20 5b 53 45 set of that [SE
24270 4c 45 43 54 5d 20 69 73 20 61 20 74 61 62 6c 65 LECT] is a table
24280 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a column (not an.
24290 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 ** expression or
242a0 20 73 75 62 71 75 65 72 79 29 20 74 68 65 6e 20 subquery) then
242b0 74 68 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 the declared typ
242c0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a e of the table.*
242d0 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 * column is retu
242e0 72 6e 65 64 2e 20 20 49 66 20 74 68 65 20 4e 74 rned. If the Nt
242f0 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 h column of the
24300 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 61 6e result set is an
24310 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6f .** expression o
24320 72 20 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e r subquery, then
24330 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
24340 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
24350 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 The returned str
24360 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20 55 54 ing is always UT
24370 46 2d 38 20 65 6e 63 6f 64 65 64 2e 20 7b 45 4e F-8 encoded. {EN
24380 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 D}.**.** For exa
24390 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74 68 65 20 mple, given the
243a0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 3a database schema:
243b0 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 .**.** CREATE TA
243c0 42 4c 45 20 74 31 28 63 31 20 56 41 52 49 41 4e BLE t1(c1 VARIAN
243d0 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 T);.**.** and th
243e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 e following stat
243f0 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70 ement to be comp
24400 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 iled:.**.** SELE
24410 43 54 20 63 31 20 2b 20 31 2c 20 63 31 20 46 52 CT c1 + 1, c1 FR
24420 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69 OM t1;.**.** thi
24430 73 20 72 6f 75 74 69 6e 65 20 77 6f 75 6c 64 20 s routine would
24440 72 65 74 75 72 6e 20 74 68 65 20 73 74 72 69 6e return the strin
24450 67 20 22 56 41 52 49 41 4e 54 22 20 66 6f 72 20 g "VARIANT" for
24460 74 68 65 20 73 65 63 6f 6e 64 20 72 65 73 75 6c the second resul
24470 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d t.** column (i==
24480 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c 4c 20 70 1), and a NULL p
24490 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20 66 ointer for the f
244a0 69 72 73 74 20 72 65 73 75 6c 74 20 63 6f 6c 75 irst result colu
244b0 6d 6e 20 28 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a mn (i==0)..**.**
244c0 20 53 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e SQLite uses dyn
244d0 61 6d 69 63 20 72 75 6e 2d 74 69 6d 65 20 74 79 amic run-time ty
244e0 70 69 6e 67 2e 20 20 53 6f 20 6a 75 73 74 20 62 ping. So just b
244f0 65 63 61 75 73 65 20 61 20 63 6f 6c 75 6d 6e 0a ecause a column.
24500 2a 2a 20 69 73 20 64 65 63 6c 61 72 65 64 20 74 ** is declared t
24510 6f 20 63 6f 6e 74 61 69 6e 20 61 20 70 61 72 74 o contain a part
24520 69 63 75 6c 61 72 20 74 79 70 65 20 64 6f 65 73 icular type does
24530 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 not mean that t
24540 68 65 0a 2a 2a 20 64 61 74 61 20 73 74 6f 72 65 he.** data store
24550 64 20 69 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e d in that column
24560 20 69 73 20 6f 66 20 74 68 65 20 64 65 63 6c 61 is of the decla
24570 72 65 64 20 74 79 70 65 2e 20 20 53 51 4c 69 74 red type. SQLit
24580 65 20 69 73 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79 e is.** strongly
24590 20 74 79 70 65 64 2c 20 62 75 74 20 74 68 65 20 typed, but the
245a0 74 79 70 69 6e 67 20 69 73 20 64 79 6e 61 6d 69 typing is dynami
245b0 63 20 6e 6f 74 20 73 74 61 74 69 63 2e 20 20 54 c not static. T
245c0 79 70 65 0a 2a 2a 20 69 73 20 61 73 73 6f 63 69 ype.** is associ
245d0 61 74 65 64 20 77 69 74 68 20 69 6e 64 69 76 69 ated with indivi
245e0 64 75 61 6c 20 76 61 6c 75 65 73 2c 20 6e 6f 74 dual values, not
245f0 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74 61 69 with the contai
24600 6e 65 72 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 ners.** used to
24610 68 6f 6c 64 20 74 68 6f 73 65 20 76 61 6c 75 65 hold those value
24620 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 s..**.** Require
24630 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 36 ments:.** [H1376
24640 31 5d 20 5b 48 31 33 37 36 32 5d 20 5b 48 31 33 1] [H13762] [H13
24650 37 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 763].*/.SQLITE_A
24660 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
24670 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 qlite3_column_de
24680 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 cltype(sqlite3_s
24690 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 tmt*,int);.SQLIT
246a0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
246b0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
246c0 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69 _decltype16(sqli
246d0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a te3_stmt*,int);.
246e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
246f0 20 45 76 61 6c 75 61 74 65 20 41 6e 20 53 51 4c Evaluate An SQL
24700 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 32 Statement {H132
24710 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 00} <S10000>.**.
24720 2a 2a 20 41 66 74 65 72 20 61 20 5b 70 72 65 70 ** After a [prep
24730 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
24740 68 61 73 20 62 65 65 6e 20 70 72 65 70 61 72 65 has been prepare
24750 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 0a 2a d using either.*
24760 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 * [sqlite3_prepa
24770 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c re_v2()] or [sql
24780 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 ite3_prepare16_v
24790 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 2()] or one of t
247a0 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 he legacy.** int
247b0 65 72 66 61 63 65 73 20 5b 73 71 6c 69 74 65 33 erfaces [sqlite3
247c0 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20 5b _prepare()] or [
247d0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
247e0 36 28 29 5d 2c 20 74 68 69 73 20 66 75 6e 63 74 6()], this funct
247f0 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 63 ion.** must be c
24800 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 alled one or mor
24810 65 20 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75 e times to evalu
24820 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e ate the statemen
24830 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 t..**.** The det
24840 61 69 6c 73 20 6f 66 20 74 68 65 20 62 65 68 61 ails of the beha
24850 76 69 6f 72 20 6f 66 20 74 68 65 20 73 71 6c 69 vior of the sqli
24860 74 65 33 5f 73 74 65 70 28 29 20 69 6e 74 65 72 te3_step() inter
24870 66 61 63 65 20 64 65 70 65 6e 64 0a 2a 2a 20 6f face depend.** o
24880 6e 20 77 68 65 74 68 65 72 20 74 68 65 20 73 74 n whether the st
24890 61 74 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 atement was prep
248a0 61 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 6e ared using the n
248b0 65 77 65 72 20 22 76 32 22 20 69 6e 74 65 72 66 ewer "v2" interf
248c0 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ace.** [sqlite3_
248d0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e prepare_v2()] an
248e0 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 d [sqlite3_prepa
248f0 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 74 68 re16_v2()] or th
24900 65 20 6f 6c 64 65 72 20 6c 65 67 61 63 79 0a 2a e older legacy.*
24910 2a 20 69 6e 74 65 72 66 61 63 65 20 5b 73 71 6c * interface [sql
24920 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
24930 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 and [sqlite3_pre
24940 70 61 72 65 31 36 28 29 5d 2e 20 20 54 68 65 20 pare16()]. The
24950 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 use of the.** ne
24960 77 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 w "v2" interface
24970 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 is recommended
24980 66 6f 72 20 6e 65 77 20 61 70 70 6c 69 63 61 74 for new applicat
24990 69 6f 6e 73 20 62 75 74 20 74 68 65 20 6c 65 67 ions but the leg
249a0 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 acy.** interface
249b0 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 will continue t
249c0 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 2e 0a o be supported..
249d0 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 6c 65 67 **.** In the leg
249e0 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 acy interface, t
249f0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
24a00 77 69 6c 6c 20 62 65 20 65 69 74 68 65 72 20 5b will be either [
24a10 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a SQLITE_BUSY],.**
24a20 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 [SQLITE_DONE],
24a30 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 [SQLITE_ROW], [S
24a40 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 QLITE_ERROR], or
24a50 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d [SQLITE_MISUSE]
24a60 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 22 76 ..** With the "v
24a70 32 22 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 2" interface, an
24a80 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 5b y of the other [
24a90 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 6f 72 result codes] or
24aa0 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 .** [extended re
24ab0 73 75 6c 74 20 63 6f 64 65 73 5d 20 6d 69 67 68 sult codes] migh
24ac0 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 73 t be returned as
24ad0 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 well..**.** [SQ
24ae0 4c 49 54 45 5f 42 55 53 59 5d 20 6d 65 61 6e 73 LITE_BUSY] means
24af0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 that the databa
24b00 73 65 20 65 6e 67 69 6e 65 20 77 61 73 20 75 6e se engine was un
24b10 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 able to acquire
24b20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
24b30 6c 6f 63 6b 73 20 69 74 20 6e 65 65 64 73 20 74 locks it needs t
24b40 6f 20 64 6f 20 69 74 73 20 6a 6f 62 2e 20 20 49 o do its job. I
24b50 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
24b60 69 73 20 61 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a is a [COMMIT].**
24b70 20 6f 72 20 6f 63 63 75 72 73 20 6f 75 74 73 69 or occurs outsi
24b80 64 65 20 6f 66 20 61 6e 20 65 78 70 6c 69 63 69 de of an explici
24b90 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 t transaction, t
24ba0 68 65 6e 20 79 6f 75 20 63 61 6e 20 72 65 74 72 hen you can retr
24bb0 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 y the.** stateme
24bc0 6e 74 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 nt. If the stat
24bd0 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 61 20 5b ement is not a [
24be0 43 4f 4d 4d 49 54 5d 20 61 6e 64 20 6f 63 63 75 COMMIT] and occu
24bf0 72 73 20 77 69 74 68 69 6e 20 61 0a 2a 2a 20 65 rs within a.** e
24c00 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 xplicit transact
24c10 69 6f 6e 20 74 68 65 6e 20 79 6f 75 20 73 68 6f ion then you sho
24c20 75 6c 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 uld rollback the
24c30 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 transaction bef
24c40 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e ore.** continuin
24c50 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 g..**.** [SQLITE
24c60 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 20 74 68 61 _DONE] means tha
24c70 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 t the statement
24c80 68 61 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 has finished exe
24c90 63 75 74 69 6e 67 0a 2a 2a 20 73 75 63 63 65 73 cuting.** succes
24ca0 73 66 75 6c 6c 79 2e 20 20 73 71 6c 69 74 65 33 sfully. sqlite3
24cb0 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e _step() should n
24cc0 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 ot be called aga
24cd0 69 6e 20 6f 6e 20 74 68 69 73 20 76 69 72 74 75 in on this virtu
24ce0 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 77 69 al.** machine wi
24cf0 74 68 6f 75 74 20 66 69 72 73 74 20 63 61 6c 6c thout first call
24d00 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 ing [sqlite3_res
24d10 65 74 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 et()] to reset t
24d20 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 he virtual.** ma
24d30 63 68 69 6e 65 20 62 61 63 6b 20 74 6f 20 69 74 chine back to it
24d40 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e s initial state.
24d50 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 .**.** If the SQ
24d60 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e L statement bein
24d70 67 20 65 78 65 63 75 74 65 64 20 72 65 74 75 72 g executed retur
24d80 6e 73 20 61 6e 79 20 64 61 74 61 2c 20 74 68 65 ns any data, the
24d90 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a n [SQLITE_ROW].*
24da0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 65 61 * is returned ea
24db0 63 68 20 74 69 6d 65 20 61 20 6e 65 77 20 72 6f ch time a new ro
24dc0 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 w of data is rea
24dd0 64 79 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e dy for processin
24de0 67 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c g by the.** call
24df0 65 72 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6d er. The values m
24e00 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20 75 ay be accessed u
24e10 73 69 6e 67 20 74 68 65 20 5b 63 6f 6c 75 6d 6e sing the [column
24e20 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e access function
24e30 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 s]..** sqlite3_s
24e40 74 65 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 tep() is called
24e50 61 67 61 69 6e 20 74 6f 20 72 65 74 72 69 65 76 again to retriev
24e60 65 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 6f e the next row o
24e70 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 f data..**.** [S
24e80 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6d 65 61 QLITE_ERROR] mea
24e90 6e 73 20 74 68 61 74 20 61 20 72 75 6e 2d 74 69 ns that a run-ti
24ea0 6d 65 20 65 72 72 6f 72 20 28 73 75 63 68 20 61 me error (such a
24eb0 73 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a s a constraint.*
24ec0 2a 20 76 69 6f 6c 61 74 69 6f 6e 29 20 68 61 73 * violation) has
24ed0 20 6f 63 63 75 72 72 65 64 2e 20 20 73 71 6c 69 occurred. sqli
24ee0 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c te3_step() shoul
24ef0 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 d not be called
24f00 61 67 61 69 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 again on.** the
24f10 56 4d 2e 20 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 VM. More informa
24f20 74 69 6f 6e 20 6d 61 79 20 62 65 20 66 6f 75 6e tion may be foun
24f30 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 d by calling [sq
24f40 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e lite3_errmsg()].
24f50 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 6c 65 67 .** With the leg
24f60 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 61 acy interface, a
24f70 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20 65 more specific e
24f80 72 72 6f 72 20 63 6f 64 65 20 28 66 6f 72 20 65 rror code (for e
24f90 78 61 6d 70 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 xample,.** [SQLI
24fa0 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2c 20 5b TE_INTERRUPT], [
24fb0 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2c 20 SQLITE_SCHEMA],
24fc0 5b 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5d [SQLITE_CORRUPT]
24fd0 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a , and so forth).
24fe0 2a 2a 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e ** can be obtain
24ff0 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 ed by calling [s
25000 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
25010 6f 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 on the.** [prepa
25020 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 red statement].
25030 20 49 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 In the "v2" int
25040 65 72 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d erface,.** the m
25050 6f 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72 ore specific err
25060 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
25070 6e 65 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 ned directly by
25080 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a sqlite3_step()..
25090 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 **.** [SQLITE_MI
250a0 53 55 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 SUSE] means that
250b0 20 74 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e the this routin
250c0 65 20 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61 e was called ina
250d0 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a ppropriately..**
250e0 20 50 65 72 68 61 70 73 20 69 74 20 77 61 73 20 Perhaps it was
250f0 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b 70 72 65 called on a [pre
25100 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
25110 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 that has.** alr
25120 65 61 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74 eady been [sqlit
25130 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69 e3_finalize | fi
25140 6e 61 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f nalized] or on o
25150 6e 65 20 74 68 61 74 20 68 61 64 0a 2a 2a 20 70 ne that had.** p
25160 72 65 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e reviously return
25170 65 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 ed [SQLITE_ERROR
25180 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e ] or [SQLITE_DON
25190 45 5d 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64 E]. Or it could
251a0 0a 2a 2a 20 62 65 20 74 68 65 20 63 61 73 65 20 .** be the case
251b0 74 68 61 74 20 74 68 65 20 73 61 6d 65 20 64 61 that the same da
251c0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
251d0 6e 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 n is being used
251e0 62 79 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 by two or.** mor
251f0 65 20 74 68 72 65 61 64 73 20 61 74 20 74 68 65 e threads at the
25200 20 73 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 same moment in
25210 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 time..**.** <b>G
25220 6f 6f 66 79 20 49 6e 74 65 72 66 61 63 65 20 41 oofy Interface A
25230 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e 20 74 68 65 lert:</b> In the
25240 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63 legacy interfac
25250 65 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 e, the sqlite3_s
25260 74 65 70 28 29 0a 2a 2a 20 41 50 49 20 61 6c 77 tep().** API alw
25270 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 67 65 ays returns a ge
25280 6e 65 72 69 63 20 65 72 72 6f 72 20 63 6f 64 65 neric error code
25290 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d , [SQLITE_ERROR]
252a0 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a , following any.
252b0 2a 2a 20 65 72 72 6f 72 20 6f 74 68 65 72 20 74 ** error other t
252c0 68 61 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 han [SQLITE_BUSY
252d0 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 4d 49 ] and [SQLITE_MI
252e0 53 55 53 45 5d 2e 20 20 59 6f 75 20 6d 75 73 74 SUSE]. You must
252f0 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65 call.** [sqlite
25300 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 3_reset()] or [s
25310 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
25320 29 5d 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 )] in order to f
25330 69 6e 64 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a ind one of the.*
25340 2a 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f * specific [erro
25350 72 20 63 6f 64 65 73 5d 20 74 68 61 74 20 62 65 r codes] that be
25360 74 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74 tter describes t
25370 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 he error..** We
25380 61 64 6d 69 74 20 74 68 61 74 20 74 68 69 73 20 admit that this
25390 69 73 20 61 20 67 6f 6f 66 79 20 64 65 73 69 67 is a goofy desig
253a0 6e 2e 20 20 54 68 65 20 70 72 6f 62 6c 65 6d 20 n. The problem
253b0 68 61 73 20 62 65 65 6e 20 66 69 78 65 64 0a 2a has been fixed.*
253c0 2a 20 77 69 74 68 20 74 68 65 20 22 76 32 22 20 * with the "v2"
253d0 69 6e 74 65 72 66 61 63 65 2e 20 20 49 66 20 79 interface. If y
253e0 6f 75 20 70 72 65 70 61 72 65 20 61 6c 6c 20 6f ou prepare all o
253f0 66 20 79 6f 75 72 20 53 51 4c 20 73 74 61 74 65 f your SQL state
25400 6d 65 6e 74 73 0a 2a 2a 20 75 73 69 6e 67 20 65 ments.** using e
25410 69 74 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 ither [sqlite3_p
25420 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 repare_v2()] or
25430 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
25440 31 36 5f 76 32 28 29 5d 20 69 6e 73 74 65 61 64 16_v2()] instead
25450 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 65 67 61 63 .** of the legac
25460 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 y [sqlite3_prepa
25470 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 re()] and [sqlit
25480 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 e3_prepare16()]
25490 69 6e 74 65 72 66 61 63 65 73 2c 0a 2a 2a 20 74 interfaces,.** t
254a0 68 65 6e 20 74 68 65 20 6d 6f 72 65 20 73 70 65 hen the more spe
254b0 63 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64 cific [error cod
254c0 65 73 5d 20 61 72 65 20 72 65 74 75 72 6e 65 64 es] are returned
254d0 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 directly.** by
254e0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 sqlite3_step().
254f0 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 20 The use of the
25500 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 "v2" interface i
25510 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a s recommended..*
25520 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
25530 73 3a 0a 2a 2a 20 5b 48 31 33 32 30 32 5d 20 5b s:.** [H13202] [
25540 48 31 35 33 30 34 5d 20 5b 48 31 35 33 30 36 5d H15304] [H15306]
25550 20 5b 48 31 35 33 30 38 5d 20 5b 48 31 35 33 31 [H15308] [H1531
25560 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 0].*/.SQLITE_API
25570 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 int sqlite3_ste
25580 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 p(sqlite3_stmt*)
25590 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
255a0 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c F: Number of col
255b0 75 6d 6e 73 20 69 6e 20 61 20 72 65 73 75 6c 74 umns in a result
255c0 20 73 65 74 20 7b 48 31 33 37 37 30 7d 20 3c 53 set {H13770} <S
255d0 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 10700>.**.** Ret
255e0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
255f0 6f 66 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 of values in the
25600 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 current row of
25610 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a the result set..
25620 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
25630 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 37 31 5d 20 ts:.** [H13771]
25640 5b 48 31 33 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49 [H13772].*/.SQLI
25650 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
25660 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 e3_data_count(sq
25670 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
25680 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
25690 52 45 46 3a 20 46 75 6e 64 61 6d 65 6e 74 61 6c REF: Fundamental
256a0 20 44 61 74 61 74 79 70 65 73 20 7b 48 31 30 32 Datatypes {H102
256b0 36 35 7d 20 3c 53 31 30 31 31 30 3e 3c 53 31 30 65} <S10110><S10
256c0 31 32 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 120>.** KEYWORDS
256d0 3a 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 2a 2a : SQLITE_TEXT.**
256e0 0a 2a 2a 20 7b 48 31 30 32 36 36 7d 20 45 76 65 .** {H10266} Eve
256f0 72 79 20 76 61 6c 75 65 20 69 6e 20 53 51 4c 69 ry value in SQLi
25700 74 65 20 68 61 73 20 6f 6e 65 20 6f 66 20 66 69 te has one of fi
25710 76 65 20 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64 ve fundamental d
25720 61 74 61 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 atatypes:.**.**
25730 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d <ul>.** <li> 64-
25740 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
25750 65 72 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 er.** <li> 64-bi
25760 74 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 t IEEE floating
25770 70 6f 69 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 point number.**
25780 3c 6c 69 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c <li> string.** <
25790 6c 69 3e 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e li> BLOB.** <li>
257a0 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b NULL.** </ul> {
257b0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 END}.**.** These
257c0 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 63 constants are c
257d0 6f 64 65 73 20 66 6f 72 20 65 61 63 68 20 6f 66 odes for each of
257e0 20 74 68 6f 73 65 20 74 79 70 65 73 2e 0a 2a 2a those types..**
257f0 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 .** Note that th
25800 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20 63 6f e SQLITE_TEXT co
25810 6e 73 74 61 6e 74 20 77 61 73 20 61 6c 73 6f 20 nstant was also
25820 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 20 76 used in SQLite v
25830 65 72 73 69 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 ersion 2.** for
25840 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 66 a completely dif
25850 66 65 72 65 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 ferent meaning.
25860 20 53 6f 66 74 77 61 72 65 20 74 68 61 74 20 6c Software that l
25870 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 62 6f 74 inks against bot
25880 68 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73 h.** SQLite vers
25890 69 6f 6e 20 32 20 61 6e 64 20 53 51 4c 69 74 65 ion 2 and SQLite
258a0 20 76 65 72 73 69 6f 6e 20 33 20 73 68 6f 75 6c version 3 shoul
258b0 64 20 75 73 65 20 53 51 4c 49 54 45 33 5f 54 45 d use SQLITE3_TE
258c0 58 54 2c 20 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 XT, not.** SQLIT
258d0 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 E_TEXT..*/.#defi
258e0 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 ne SQLITE_INTEGE
258f0 52 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c R 1.#define SQL
25900 49 54 45 5f 46 4c 4f 41 54 20 20 20 20 32 0a 23 ITE_FLOAT 2.#
25910 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 4c define SQLITE_BL
25920 4f 42 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 OB 4.#define
25930 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 20 20 20 SQLITE_NULL
25940 20 35 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5.#ifdef SQLITE
25950 5f 54 45 58 54 0a 23 20 75 6e 64 65 66 20 53 51 _TEXT.# undef SQ
25960 4c 49 54 45 5f 54 45 58 54 0a 23 65 6c 73 65 0a LITE_TEXT.#else.
25970 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
25980 54 45 58 54 20 20 20 20 20 33 0a 23 65 6e 64 69 TEXT 3.#endi
25990 66 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 f.#define SQLITE
259a0 33 5f 54 45 58 54 20 20 20 20 20 33 0a 0a 2f 2a 3_TEXT 3../*
259b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
259c0 73 75 6c 74 20 56 61 6c 75 65 73 20 46 72 6f 6d sult Values From
259d0 20 41 20 51 75 65 72 79 20 7b 48 31 33 38 30 30 A Query {H13800
259e0 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 } <S10700>.** KE
259f0 59 57 4f 52 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 YWORDS: {column
25a00 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 access functions
25a10 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f }.**.** These ro
25a20 75 74 69 6e 65 73 20 66 6f 72 6d 20 74 68 65 20 utines form the
25a30 22 72 65 73 75 6c 74 20 73 65 74 20 71 75 65 72 "result set quer
25a40 79 22 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a y" interface..**
25a50 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
25a60 65 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d es return inform
25a70 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 69 ation about a si
25a80 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 ngle column of t
25a90 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65 he current.** re
25aa0 73 75 6c 74 20 72 6f 77 20 6f 66 20 61 20 71 75 sult row of a qu
25ab0 65 72 79 2e 20 20 49 6e 20 65 76 65 72 79 20 63 ery. In every c
25ac0 61 73 65 20 74 68 65 20 66 69 72 73 74 20 61 72 ase the first ar
25ad0 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e gument is a poin
25ae0 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 70 ter.** to the [p
25af0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
25b00 74 5d 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 t] that is being
25b10 20 65 76 61 6c 75 61 74 65 64 20 28 74 68 65 20 evaluated (the
25b20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 0a [sqlite3_stmt*].
25b30 2a 2a 20 74 68 61 74 20 77 61 73 20 72 65 74 75 ** that was retu
25b40 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 rned from [sqlit
25b50 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
25b60 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76 or one of its v
25b70 61 72 69 61 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 ariants).** and
25b80 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
25b90 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 ent is the index
25ba0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 of the column f
25bb0 6f 72 20 77 68 69 63 68 20 69 6e 66 6f 72 6d 61 or which informa
25bc0 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 tion.** should b
25bd0 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 e returned. The
25be0 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e leftmost column
25bf0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
25c00 65 74 20 68 61 73 20 74 68 65 20 69 6e 64 65 78 et has the index
25c10 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 0..**.** If the
25c20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64 SQL statement d
25c30 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c oes not currentl
25c40 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c y point to a val
25c50 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68 id row, or if th
25c60 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 e.** column inde
25c70 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 x is out of rang
25c80 65 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 e, the result is
25c90 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 undefined..** T
25ca0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61 hese routines ma
25cb0 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 y only be called
25cc0 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72 when the most r
25cd0 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a ecent call to.**
25ce0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
25cf0 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b ] has returned [
25d00 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20 SQLITE_ROW] and
25d10 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 neither.** [sqli
25d20 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72 te3_reset()] nor
25d30 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
25d40 7a 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20 ze()] have been
25d50 63 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e called subsequen
25d60 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f tly..** If any o
25d70 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 f these routines
25d80 20 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65 are called afte
25d90 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 r [sqlite3_reset
25da0 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 ()] or.** [sqlit
25db0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f e3_finalize()] o
25dc0 72 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 r after [sqlite3
25dd0 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 _step()] has ret
25de0 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 urned.** somethi
25df0 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 ng other than [S
25e00 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20 QLITE_ROW], the
25e10 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 results are unde
25e20 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 fined..** If [sq
25e30 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 lite3_step()] or
25e40 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
25e50 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 )] or [sqlite3_f
25e60 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 inalize()].** ar
25e70 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20 e called from a
25e80 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
25e90 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68 while any of th
25ea0 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 ese routines.**
25eb0 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65 are pending, the
25ec0 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 n the results ar
25ed0 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a e undefined..**.
25ee0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
25ef0 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75 olumn_type() rou
25f00 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
25f10 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 .** [SQLITE_INTE
25f20 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20 63 GER | datatype c
25f30 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69 ode] for the ini
25f40 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a tial data type.*
25f50 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 * of the result
25f60 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74 column. The ret
25f70 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f urned value is o
25f80 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e ne of [SQLITE_IN
25f90 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 TEGER],.** [SQLI
25fa0 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 TE_FLOAT], [SQLI
25fb0 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54 TE_TEXT], [SQLIT
25fc0 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c E_BLOB], or [SQL
25fd0 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 ITE_NULL]. The
25fe0 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 value.** returne
25ff0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c d by sqlite3_col
26000 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e umn_type() is on
26010 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 ly meaningful if
26020 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 no type.** conv
26030 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63 ersions have occ
26040 75 72 72 65 64 20 61 73 20 64 65 73 63 72 69 62 urred as describ
26050 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72 ed below. After
26060 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 a type conversi
26070 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 on,.** the value
26080 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
26090 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 ite3_column_type
260a0 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e () is undefined.
260b0 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73 Future.** vers
260c0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d ions of SQLite m
260d0 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 62 65 ay change the be
260e0 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 havior of sqlite
260f0 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 3_column_type().
26100 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 ** following a t
26110 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a ype conversion..
26120 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 **.** If the res
26130 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72 ult is a BLOB or
26140 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68 UTF-8 string th
26150 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 en the sqlite3_c
26160 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a olumn_bytes().**
26170 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
26180 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
26190 79 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f ytes in that BLO
261a0 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 B or string..**
261b0 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 If the result is
261c0 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 a UTF-16 string
261d0 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 , then sqlite3_c
261e0 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f olumn_bytes() co
261f0 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74 nverts.** the st
26200 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e ring to UTF-8 an
26210 64 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74 d then returns t
26220 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
26230 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 es..** If the re
26240 73 75 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69 sult is a numeri
26250 63 20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c c value then 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 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c s() uses.** [sql
26280 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d ite3_snprintf()]
26290 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74 to convert that
262a0 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d value to a UTF-
262b0 38 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 8 string and ret
262c0 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 urns.** the numb
262d0 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
262e0 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 hat string..** T
262f0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
26300 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 d does not inclu
26310 64 65 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d de the zero term
26320 69 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e inator at the en
26330 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69 d.** of the stri
26340 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79 ng. For clarity
26350 3a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 : the value retu
26360 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 rned is the numb
26370 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 er of.** bytes i
26380 6e 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f n the string, no
26390 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
263a0 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a characters..**.*
263b0 2a 20 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e * Strings return
263c0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f ed by sqlite3_co
263d0 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20 lumn_text() and
263e0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
263f0 65 78 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e ext16(),.** even
26400 20 65 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20 empty strings,
26410 61 72 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20 are always zero
26420 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 terminated. The
26430 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 return.** value
26440 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f from sqlite3_co
26450 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20 lumn_blob() for
26460 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c a zero-length BL
26470 4f 42 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 OB is an arbitra
26480 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70 ry.** pointer, p
26490 6f 73 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e ossibly even a N
264a0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a ULL pointer..**.
264b0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
264c0 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 olumn_bytes16()
264d0 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c routine is simil
264e0 61 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ar to sqlite3_co
264f0 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 lumn_bytes().**
26500 62 75 74 20 6c 65 61 76 65 73 20 74 68 65 20 72 but leaves the r
26510 65 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20 esult in UTF-16
26520 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f in native byte o
26530 72 64 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 rder instead of
26540 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 UTF-8..** The ze
26550 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73 ro terminator is
26560 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e not included in
26570 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a this count..**.
26580 2a 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65 ** The object re
26590 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 turned by [sqlit
265a0 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 e3_column_value(
265b0 29 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 )] is an.** [unp
265c0 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
265d0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20 _value] object.
265e0 20 41 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20 An unprotected
265f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
26600 6a 65 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 ject.** may only
26610 20 62 65 20 75 73 65 64 20 77 69 74 68 20 5b 73 be used with [s
26620 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 qlite3_bind_valu
26630 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 e()] and [sqlite
26640 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 3_result_value()
26650 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e ]..** If the [un
26660 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
26670 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 3_value] object
26680 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b returned by.** [
26690 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 sqlite3_column_v
266a0 61 6c 75 65 28 29 5d 20 69 73 20 75 73 65 64 20 alue()] is used
266b0 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79 in any other way
266c0 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c , including call
266d0 73 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73 s.** to routines
266e0 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76 like [sqlite3_v
266f0 61 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 alue_int()], [sq
26700 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
26710 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 ()],.** or [sqli
26720 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
26730 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68 )], then the beh
26740 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
26750 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ed..**.** These
26760 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 routines attempt
26770 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 to convert the
26780 76 61 6c 75 65 20 77 68 65 72 65 20 61 70 70 72 value where appr
26790 6f 70 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a opriate. For.**
267a0 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 example, if the
267b0 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 internal repres
267c0 65 6e 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41 entation is FLOA
267d0 54 20 61 6e 64 20 61 20 74 65 78 74 20 72 65 73 T and a text res
267e0 75 6c 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73 ult.** is reques
267f0 74 65 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e ted, [sqlite3_sn
26800 70 72 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65 printf()] is use
26810 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 d internally to
26820 70 65 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 perform the.** c
26830 6f 6e 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61 onversion automa
26840 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f tically. The fo
26850 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 llowing table de
26860 74 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72 tails the conver
26870 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72 sions.** that ar
26880 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a e applied:.**.**
26890 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a <blockquote>.**
268a0 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 <table border="
268b0 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 1">.** <tr><th>
268c0 49 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65 Internal<br>Type
268d0 20 3c 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c <th> Requested<
268e0 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f br>Type <th> Co
268f0 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c nversion.**.** <
26900 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 tr><td> NULL
26910 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 <td> INTEGER
26920 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30 <td> Result is 0
26930 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 .** <tr><td> NU
26940 4c 4c 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 LL <td> FLOA
26950 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 T <td> Result
26960 20 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c is 0.0.** <tr><
26970 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 td> NULL <td
26980 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e > TEXT <td>
26990 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 Result is NULL
269a0 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c pointer.** <tr><
269b0 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 td> NULL <td
269c0 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e > BLOB <td>
269d0 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 Result is NULL
269e0 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c pointer.** <tr><
269f0 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 td> INTEGER <td
26a00 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e > FLOAT <td>
26a10 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e Convert from in
26a20 74 65 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a teger to float.*
26a30 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 * <tr><td> INTEG
26a40 45 52 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 ER <td> TEXT
26a50 20 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 <td> ASCII re
26a60 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69 ndering of the i
26a70 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 nteger.** <tr><t
26a80 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e d> INTEGER <td>
26a90 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 BLOB <td>
26aa0 53 61 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d Same as INTEGER-
26ab0 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 >TEXT.** <tr><td
26ac0 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 > FLOAT <td>
26ad0 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43 INTEGER <td> C
26ae0 6f 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61 onvert from floa
26af0 74 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20 t to integer.**
26b00 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 <tr><td> FLOAT
26b10 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 <td> TEXT
26b20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 <td> ASCII rend
26b30 65 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f ering of the flo
26b40 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 at.** <tr><td>
26b50 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42 FLOAT <td> B
26b60 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65 LOB <td> Same
26b70 20 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a as FLOAT->TEXT.
26b80 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 ** <tr><td> TEX
26b90 54 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 T <td> INTEGE
26ba0 52 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f R <td> Use ato
26bb0 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 i().** <tr><td>
26bc0 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46 TEXT <td> F
26bd0 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65 LOAT <td> Use
26be0 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c atof().** <tr><
26bf0 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 td> TEXT <td
26c00 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e > BLOB <td>
26c10 20 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 No change.** <t
26c20 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 r><td> BLOB
26c30 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c <td> INTEGER <
26c40 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 td> Convert to T
26c50 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f EXT then use ato
26c60 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 i().** <tr><td>
26c70 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46 BLOB <td> F
26c80 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e LOAT <td> Con
26c90 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 vert to TEXT the
26ca0 6e 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20 n use atof().**
26cb0 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 <tr><td> BLOB
26cc0 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 <td> TEXT
26cd0 20 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f <td> Add a zero
26ce0 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e terminator if n
26cf0 65 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 eeded.** </table
26d00 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 >.** </blockquot
26d10 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 e>.**.** The tab
26d20 6c 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72 le above makes r
26d30 65 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e eference to stan
26d40 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 20 66 dard C library f
26d50 75 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a unctions atoi().
26d60 2a 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20 ** and atof().
26d70 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 SQLite does not
26d80 72 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65 really use these
26d90 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20 functions. It
26da0 68 61 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 has its.** own e
26db0 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e quivalent intern
26dc0 61 6c 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 al routines. Th
26dd0 65 20 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f e atoi() and ato
26de0 66 28 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a f() names are.**
26df0 20 75 73 65 64 20 69 6e 20 74 68 65 20 74 61 62 used in the tab
26e00 6c 65 20 66 6f 72 20 62 72 65 76 69 74 79 20 61 le for brevity a
26e10 6e 64 20 62 65 63 61 75 73 65 20 74 68 65 79 20 nd because they
26e20 61 72 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20 are familiar to
26e30 6d 6f 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61 most.** C progra
26e40 6d 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 mmers..**.** Not
26e50 65 20 74 68 61 74 20 77 68 65 6e 20 74 79 70 65 e that when type
26e60 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63 conversions occ
26e70 75 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74 ur, pointers ret
26e80 75 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a urned by prior.*
26e90 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 * calls to sqlit
26ea0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 e3_column_blob()
26eb0 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e , sqlite3_column
26ec0 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a _text(), and/or.
26ed0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
26ee0 6e 5f 74 65 78 74 31 36 28 29 20 6d 61 79 20 62 n_text16() may b
26ef0 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a e invalidated..*
26f00 2a 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f * Type conversio
26f10 6e 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69 ns and pointer i
26f20 6e 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67 nvalidations mig
26f30 68 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74 ht occur.** in t
26f40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 he following cas
26f50 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a es:.**.** <ul>.*
26f60 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 * <li> The initi
26f70 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20 al content is a
26f80 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33 BLOB and sqlite3
26f90 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f _column_text() o
26fa0 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 r.** sqlite
26fb0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
26fc0 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20 ) is called. A
26fd0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 zero-terminator
26fe0 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65 might.** ne
26ff0 65 64 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 ed to be added t
27000 6f 20 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c o the string.</l
27010 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 i>.** <li> The i
27020 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 nitial content i
27030 73 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64 s UTF-8 text and
27040 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27050 62 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20 bytes16() or.**
27060 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c sqlite3_col
27070 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20 umn_text16() is
27080 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e called. The con
27090 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e tent must be con
270a0 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 verted.** t
270b0 6f 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a o UTF-16.</li>.*
270c0 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69 * <li> The initi
270d0 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54 al content is UT
270e0 46 2d 31 36 20 74 65 78 74 20 61 6e 64 20 73 71 F-16 text and sq
270f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
27100 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 es() or.**
27110 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
27120 65 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e ext() is called.
27130 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 The content mu
27140 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a st be converted.
27150 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38 ** to UTF-8
27160 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a .</li>.** </ul>.
27170 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e **.** Conversion
27180 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 s between UTF-16
27190 62 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20 be and UTF-16le
271a0 61 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20 are always done
271b0 69 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a in place and do.
271c0 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 ** not invalidat
271d0 65 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 e a prior pointe
271e0 72 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75 r, though of cou
271f0 72 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 rse the content
27200 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a of the buffer.**
27210 20 74 68 61 74 20 74 68 65 20 70 72 69 6f 72 20 that the prior
27220 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 pointer points t
27230 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e o will have been
27240 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65 modified. Othe
27250 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f r kinds.** of co
27260 6e 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e nversion are don
27270 65 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20 e in place when
27280 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20 it is possible,
27290 62 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68 but sometimes th
272a0 65 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f ey.** are not po
272b0 73 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68 ssible and in th
272c0 6f 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20 ose cases prior
272d0 70 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76 pointers are inv
272e0 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 alidated..**.**
272f0 54 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 65 The safest and e
27300 61 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62 asiest to rememb
27310 65 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20 er policy is to
27320 69 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75 invoke these rou
27330 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 tines.** in one
27340 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
27350 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c ways:.**.** <ul
27360 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 >.** <li>sqlite
27370 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 3_column_text()
27380 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 followed by sqli
27390 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
273a0 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e ()</li>.** <li>
273b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
273c0 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 lob() followed b
273d0 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e y sqlite3_column
273e0 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a _bytes()</li>.**
273f0 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f <li>sqlite3_co
27400 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f lumn_text16() fo
27410 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 llowed by sqlite
27420 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 3_column_bytes16
27430 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e ()</li>.** </ul>
27440 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 .**.** In other
27450 77 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c words, you shoul
27460 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63 d call sqlite3_c
27470 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a olumn_text(),.**
27480 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27490 62 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74 blob(), or sqlit
274a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
274b0 28 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63 () first to forc
274c0 65 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 e the result.**
274d0 69 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 into the desired
274e0 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e format, then in
274f0 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c voke sqlite3_col
27500 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a umn_bytes() or.*
27510 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
27520 5f 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 69 _bytes16() to fi
27530 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 nd the size of t
27540 68 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e he result. Do n
27550 6f 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 ot mix calls.**
27560 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d to sqlite3_colum
27570 6e 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69 n_text() or sqli
27580 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
27590 29 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a ) with calls to.
275a0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
275b0 6e 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64 n_bytes16(), and
275c0 20 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c do not mix call
275d0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c s to sqlite3_col
275e0 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20 umn_text16().**
275f0 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71 with calls to sq
27600 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
27610 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 es()..**.** The
27620 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65 pointers returne
27630 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69 d are valid unti
27640 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 l a type convers
27650 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a ion occurs as.**
27660 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 described above
27670 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69 , or until [sqli
27680 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b te3_step()] or [
27690 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
276a0 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f or.** [sqlite3_
276b0 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63 finalize()] is c
276c0 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f alled. The memo
276d0 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f ry space used to
276e0 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a hold strings.**
276f0 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72 and BLOBs is fr
27700 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c eed automaticall
27710 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 y. Do <b>not</b
27720 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74 > pass the point
27730 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 ers returned.**
27740 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
27750 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 blob()], [sqlite
27760 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 3_column_text()]
27770 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b , etc. into.** [
27780 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e sqlite3_free()].
27790 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f .**.** If a memo
277a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
277b0 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e ror occurs durin
277c0 67 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e g the evaluation
277d0 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 of any.** of th
277e0 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20 ese routines, a
277f0 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 default value is
27800 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 returned. The
27810 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a default value.**
27820 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 69 is either the i
27830 6e 74 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c nteger 0, the fl
27840 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d oating point num
27850 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55 ber 0.0, or a NU
27860 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20 LL.** pointer.
27870 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 Subsequent calls
27880 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 to [sqlite3_err
27890 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74 code()] will ret
278a0 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e urn.** [SQLITE_N
278b0 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 OMEM]..**.** Req
278c0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
278d0 31 33 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20 13803] [H13806]
278e0 5b 48 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32 [H13809] [H13812
278f0 5d 20 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38 ] [H13815] [H138
27900 31 38 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31 18] [H13821] [H1
27910 33 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37 3824].** [H13827
27920 5d 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51 ] [H13830].*/.SQ
27930 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
27940 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
27950 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 umn_blob(sqlite3
27960 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
27970 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
27980 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
27990 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73 _bytes(sqlite3_s
279a0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
279b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
279c0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
279d0 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 ytes16(sqlite3_s
279e0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b tmt*, int iCol);
279f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 .SQLITE_API doub
27a00 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d le sqlite3_colum
27a10 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 n_double(sqlite3
27a20 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
27a30 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
27a40 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
27a50 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d _int(sqlite3_stm
27a60 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
27a70 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
27a80 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
27a90 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c column_int64(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 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
27ad0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
27ae0 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 olumn_text(sqlit
27af0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 e3_stmt*, int iC
27b00 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ol);.SQLITE_API
27b10 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
27b20 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
27b30 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 6(sqlite3_stmt*,
27b40 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 int iCol);.SQLI
27b50 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
27b60 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 e3_column_type(s
27b70 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
27b80 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f t iCol);.SQLITE_
27b90 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 API sqlite3_valu
27ba0 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e *sqlite3_colum
27bb0 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f n_value(sqlite3_
27bc0 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
27bd0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
27be0 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65 F: Destroy A Pre
27bf0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 pared Statement
27c00 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20 Object {H13300}
27c10 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e <S70300><S30100>
27c20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
27c30 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75 e3_finalize() fu
27c40 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
27c50 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72 to delete a [pr
27c60 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
27c70 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61 ]..** If the sta
27c80 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75 tement was execu
27c90 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ted successfully
27ca0 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64 or not executed
27cb0 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a at all, then.**
27cc0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
27cd0 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75 turned. If execu
27ce0 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74 tion of the stat
27cf0 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65 ement failed the
27d00 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 n an.** [error c
27d10 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 ode] or [extende
27d20 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 d error code] is
27d30 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
27d40 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
27d50 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 n be called at a
27d60 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20 ny point during
27d70 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 the execution of
27d80 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 the.** [prepare
27d90 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 d statement]. I
27da0 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 f the virtual ma
27db0 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a chine has not.**
27dc0 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75 completed execu
27dd0 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72 tion when this r
27de0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
27df0 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a , that is like.*
27e00 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61 * encountering a
27e10 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73 n error or an [s
27e20 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
27e30 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a | interrupt]..*
27e40 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64 * Incomplete upd
27e50 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c ates may be roll
27e60 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e ed back and tran
27e70 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65 sactions cancele
27e80 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 d,.** depending
27e90 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61 on the circumsta
27ea0 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a nces, and the.**
27eb0 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65 [error code] re
27ec0 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b turned will be [
27ed0 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a SQLITE_ABORT]..*
27ee0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
27ef0 73 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b s:.** [H11302] [
27f00 48 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H11304].*/.SQLIT
27f10 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
27f20 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 3_finalize(sqlit
27f30 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b e3_stmt *pStmt);
27f40 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
27f50 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 61 72 : Reset A Prepar
27f60 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a ed Statement Obj
27f70 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37 ect {H13330} <S7
27f80 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0300>.**.** The
27f90 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 sqlite3_reset()
27fa0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
27fb0 65 64 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70 ed to reset a [p
27fc0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
27fd0 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63 t].** object bac
27fe0 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c k to its initial
27ff0 20 73 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f state, ready to
28000 20 62 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e be re-executed.
28010 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74 .** Any SQL stat
28020 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20 ement variables
28030 74 68 61 74 20 68 61 64 20 76 61 6c 75 65 73 20 that had values
28040 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73 bound to them us
28050 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 ing.** the [sqli
28060 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 te3_bind_blob |
28070 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 sqlite3_bind_*()
28080 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65 API] retain the
28090 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73 ir values..** Us
280a0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 e [sqlite3_clear
280b0 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20 _bindings()] to
280c0 72 65 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e reset the bindin
280d0 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 gs..**.** {H1133
280e0 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 2} The [sqlite3_
280f0 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 reset(S)] interf
28100 61 63 65 20 72 65 73 65 74 73 20 74 68 65 20 5b ace resets the [
28110 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
28120 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20 nt] S.**
28130 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65 back to the be
28140 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70 ginning of its p
28150 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 rogram..**.** {H
28160 31 31 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f 11334} If the mo
28170 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 st recent call t
28180 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 o [sqlite3_step(
28190 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 S)] for the.**
281a0 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 [prepare
281b0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72 d statement] S r
281c0 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f eturned [SQLITE_
281d0 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f ROW] or [SQLITE_
281e0 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 DONE],.**
281f0 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65 or if [sqlite
28200 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e 3_step(S)] has n
28210 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e ever before been
28220 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a called on S,.**
28230 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b then [
28240 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 sqlite3_reset(S)
28250 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 ] returns [SQLIT
28260 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 E_OK]..**.** {H1
28270 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73 1336} If the mos
28280 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f t recent call to
28290 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 [sqlite3_step(S
282a0 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 )] for the.**
282b0 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 [prepared
282c0 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e statement] S in
282d0 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72 dicated an error
282e0 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 , then.**
282f0 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 [sqlite3_rese
28300 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e t(S)] returns an
28310 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72 appropriate [er
28320 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a ror code]..**.**
28330 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73 {H11338} The [s
28340 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d qlite3_reset(S)]
28350 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20 interface does
28360 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 not change the v
28370 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 alues.**
28380 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65 of any [sqlite
28390 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64 3_bind_blob|bind
283a0 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72 ings] on the [pr
283b0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
283c0 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ] S..*/.SQLITE_A
283d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 PI int sqlite3_r
283e0 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d eset(sqlite3_stm
283f0 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a t *pStmt);../*.*
28400 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 65 61 * CAPI3REF: Crea
28410 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53 te Or Redefine S
28420 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 QL Functions {H1
28430 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 6100} <S20200>.*
28440 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e * KEYWORDS: {fun
28450 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 ction creation r
28460 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57 outines}.** KEYW
28470 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 ORDS: {applicati
28480 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 on-defined SQL f
28490 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57 unction}.** KEYW
284a0 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69 ORDS: {applicati
284b0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 on-defined SQL f
284c0 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 unctions}.**.**
284d0 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69 These two functi
284e0 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c ons (collectivel
284f0 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63 y known as "func
28500 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f tion creation ro
28510 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20 utines").** are
28520 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20 used to add SQL
28530 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 functions or agg
28540 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65 regates or to re
28550 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 61 76 define the behav
28560 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69 ior.** of existi
28570 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 ng SQL functions
28580 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 2e 20 or aggregates.
28590 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 The only differ
285a0 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 ence between the
285b0 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20 .** two is that
285c0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
285d0 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f eter, the name o
285e0 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 20 66 f the (scalar) f
285f0 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 unction or.** ag
28600 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f gregate, is enco
28610 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72 ded in UTF-8 for
28620 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
28630 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55 function() and U
28640 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c TF-16.** for sql
28650 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
28660 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 tion16()..**.**
28670 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
28680 74 65 72 20 69 73 20 74 68 65 20 5b 64 61 74 61 ter is the [data
28690 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
286a0 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 53 51 to which the SQ
286b0 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 L.** function is
286c0 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49 to be added. I
286d0 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72 f a single progr
286e0 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61 am uses more tha
286f0 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a n one database.*
28700 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74 * connection int
28710 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51 ernally, then SQ
28720 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74 L functions must
28730 20 62 65 20 61 64 64 65 64 20 69 6e 64 69 76 69 be added indivi
28740 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63 dually to.** eac
28750 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 h database conne
28760 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
28770 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
28780 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 r is the name of
28790 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
287a0 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 n to be created
287b0 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e or.** redefined.
287c0 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 The length of
287d0 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69 the name is limi
287e0 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 73 ted to 255 bytes
287f0 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a , exclusive of.*
28800 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 * the zero-termi
28810 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61 nator. Note tha
28820 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74 t the name lengt
28830 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79 h limit is in by
28840 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72 tes, not.** char
28850 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74 acters. Any att
28860 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20 61 empt to create a
28870 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 function with a
28880 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20 longer name.**
28890 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b will result in [
288a0 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65 SQLITE_ERROR] be
288b0 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a ing returned..**
288c0 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70 61 .** The third pa
288d0 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a rameter (nArg).*
288e0 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 * is the number
288f0 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 of arguments tha
28900 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 t the SQL functi
28910 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 on or.** aggrega
28920 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69 te takes. If thi
28930 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d s parameter is -
28940 31 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 20 1, then the SQL
28950 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 function or.** a
28960 67 67 72 65 67 61 74 65 20 6d 61 79 20 74 61 6b ggregate may tak
28970 65 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 e any number of
28980 61 72 67 75 6d 65 6e 74 73 20 62 65 74 77 65 65 arguments betwee
28990 6e 20 30 20 61 6e 64 20 74 68 65 20 6c 69 6d 69 n 0 and the limi
289a0 74 0a 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 6c t.** set by [sql
289b0 69 74 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 4c ite3_limit]([SQL
289c0 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 ITE_LIMIT_FUNCTI
289d0 4f 4e 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 68 ON_ARG]). If th
289e0 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d e third.** param
289f0 65 74 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 eter is less tha
28a00 6e 20 2d 31 20 6f 72 20 67 72 65 61 74 65 72 20 n -1 or greater
28a10 74 68 61 6e 20 31 32 37 20 74 68 65 6e 20 74 68 than 127 then th
28a20 65 20 62 65 68 61 76 69 6f 72 20 69 73 0a 2a 2a e behavior is.**
28a30 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a undefined..**.*
28a40 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72 * The fourth par
28a50 61 6d 65 74 65 72 2c 20 65 54 65 78 74 52 65 70 ameter, eTextRep
28a60 2c 20 73 70 65 63 69 66 69 65 73 20 77 68 61 74 , specifies what
28a70 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 .** [SQLITE_UTF8
28a80 20 7c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 | text encoding
28a90 5d 20 74 68 69 73 20 53 51 4c 20 66 75 6e 63 74 ] this SQL funct
28aa0 69 6f 6e 20 70 72 65 66 65 72 73 20 66 6f 72 0a ion prefers for.
28ab0 2a 2a 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 ** its parameter
28ac0 73 2e 20 20 41 6e 79 20 53 51 4c 20 66 75 6e 63 s. Any SQL func
28ad0 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 tion implementat
28ae0 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 62 ion should be ab
28af0 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f le to work.** wo
28b00 72 6b 20 77 69 74 68 20 55 54 46 2d 38 2c 20 55 rk with UTF-8, U
28b10 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 2d TF-16le, or UTF-
28b20 31 36 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 20 16be. But some
28b30 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
28b40 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 65 may be.** more e
28b50 66 66 69 63 69 65 6e 74 20 77 69 74 68 20 6f 6e fficient with on
28b60 65 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e 20 e encoding than
28b70 61 6e 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 70 another. An app
28b80 6c 69 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 lication may.**
28b90 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 invoke sqlite3_c
28ba0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 reate_function()
28bb0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 or sqlite3_crea
28bc0 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 20 te_function16()
28bd0 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d 65 multiple.** time
28be0 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 s with the same
28bf0 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 74 function but wit
28c00 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 h different valu
28c10 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e 0a es of eTextRep..
28c20 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 ** When multiple
28c30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
28c40 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e of the same fun
28c50 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c 61 ction are availa
28c60 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 77 ble, SQLite.** w
28c70 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e 65 ill pick the one
28c80 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 74 that involves t
28c90 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20 he least amount
28ca0 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73 69 of data conversi
28cb0 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 on..** If there
28cc0 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 is only a single
28cd0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
28ce0 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63 which does not c
28cf0 61 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a 2a are what text.**
28d00 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65 encoding is use
28d10 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72 d, then the four
28d20 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 th argument shou
28d30 6c 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e ld be [SQLITE_AN
28d40 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 Y]..**.** The fi
28d50 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 fth parameter is
28d60 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f an arbitrary po
28d70 69 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70 6c inter. The impl
28d80 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
28d90 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 61 e.** function ca
28da0 6e 20 67 61 69 6e 20 61 63 63 65 73 73 20 74 6f n gain access to
28db0 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75 73 this pointer us
28dc0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 ing [sqlite3_use
28dd0 72 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a r_data()]..**.**
28de0 20 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65 69 The seventh, ei
28df0 67 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 70 ghth and ninth p
28e00 61 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63 arameters, xFunc
28e10 2c 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e , xStep and xFin
28e20 61 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74 al, are.** point
28e30 65 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67 ers to C-languag
28e40 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 e functions that
28e50 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 implement the S
28e60 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a QL function or.*
28e70 2a 20 61 67 67 72 65 67 61 74 65 2e 20 41 20 73 * aggregate. A s
28e80 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 calar SQL functi
28e90 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 on requires an i
28ea0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
28eb0 20 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61 the xFunc.** ca
28ec0 6c 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c llback only, NUL
28ed0 4c 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c L pointers shoul
28ee0 64 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 d be passed as t
28ef0 68 65 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 he xStep and xFi
28f00 6e 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 nal.** parameter
28f10 73 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65 20 s. An aggregate
28f20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 SQL function req
28f30 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 uires an impleme
28f40 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70 ntation of xStep
28f50 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61 .** and xFinal a
28f60 6e 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62 nd NULL should b
28f70 65 20 70 61 73 73 65 64 20 66 6f 72 20 78 46 75 e passed for xFu
28f80 6e 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 6e nc. To delete an
28f90 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 4c existing.** SQL
28fa0 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 function or agg
28fb0 72 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55 4c regate, pass NUL
28fc0 4c 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20 L for all three
28fd0 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63 function callbac
28fe0 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 ks..**.** It is
28ff0 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67 permitted to reg
29000 69 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69 ister multiple i
29010 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
29020 66 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75 f the same.** fu
29030 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65 nctions with the
29040 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77 same name but w
29050 69 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65 ith either diffe
29060 72 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a ring numbers of.
29070 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 ** arguments or
29080 64 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72 differing prefer
29090 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e red text encodin
290a0 67 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c gs. SQLite will
290b0 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c use.** the impl
290c0 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 ementation that
290d0 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74 most closely mat
290e0 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20 ches the way in
290f0 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c which the.** SQL
29100 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
29110 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69 d. A function i
29120 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 mplementation wi
29130 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 th a non-negativ
29140 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65 e.** nArg parame
29150 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20 ter is a better
29160 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e match than a fun
29170 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ction implementa
29180 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e tion with.** a n
29190 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41 egative nArg. A
291a0 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 function where
291b0 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65 the preferred te
291c0 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d xt encoding.** m
291d0 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62 atches the datab
291e0 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 ase encoding is
291f0 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63 a better.** matc
29200 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f h than a functio
29210 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f n where the enco
29220 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e ding is differen
29230 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69 t. .** A functi
29240 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 on where the enc
29250 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65 oding difference
29260 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31 is between UTF1
29270 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a 6le and UTF16be.
29280 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d ** is a closer m
29290 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 atch than a func
292a0 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 tion where the e
292b0 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e ncoding differen
292c0 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e ce is.** between
292d0 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e UTF8 and UTF16.
292e0 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20 .**.** Built-in
292f0 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 functions may be
29300 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e overloaded by n
29310 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 ew application-d
29320 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 efined functions
29330 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 ..** The first a
29340 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
29350 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 ed function with
29360 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76 a given name ov
29370 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62 errides all.** b
29380 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e uilt-in function
29390 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 s in the same [d
293a0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
293b0 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d on] with the sam
293c0 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65 e name..** Subse
293d0 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f quent applicatio
293e0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
293f0 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ons of the same
29400 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69 name only overri
29410 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70 de .** prior app
29420 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
29430 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
29440 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74 are an exact mat
29450 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75 ch for the.** nu
29460 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 mber of paramete
29470 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64 rs and preferred
29480 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a encoding..**.**
29490 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d An application-
294a0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
294b0 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f is permitted to
294c0 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53 call other.** S
294d0 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 QLite interfaces
294e0 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68 . However, such
294f0 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a calls must not.
29500 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 ** close the dat
29510 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
29520 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72 nor finalize or
29530 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61 reset the prepa
29540 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 red.** statement
29550 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75 in which the fu
29560 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e nction is runnin
29570 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 g..**.** Require
29580 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 30 ments:.** [H1610
29590 33 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 36 3] [H16106] [H16
295a0 31 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 48 109] [H16112] [H
295b0 31 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d 20 16118] [H16121]
295c0 5b 48 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36 [H16127].** [H16
295d0 31 33 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48 130] [H16133] [H
295e0 31 36 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20 16136] [H16139]
295f0 5b 48 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49 [H16142].*/.SQLI
29600 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
29610 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
29620 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 on(. sqlite3 *d
29630 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 b,. const char
29640 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a *zFunctionName,.
29650 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e int nArg,. in
29660 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f t eTextRep,. vo
29670 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 id *pApp,. void
29680 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 (*xFunc)(sqlite
29690 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
296a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c qlite3_value**),
296b0 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 . void (*xStep)
296c0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
296d0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
296e0 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 lue**),. void (
296f0 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 *xFinal)(sqlite3
29700 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51 _context*).);.SQ
29710 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
29720 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
29730 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 tion16(. sqlite
29740 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 3 *db,. const v
29750 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 oid *zFunctionNa
29760 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a me,. int nArg,.
29770 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a int eTextRep,.
29780 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 void *pApp,.
29790 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 void (*xFunc)(sq
297a0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 lite3_context*,i
297b0 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt,sqlite3_value
297c0 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 **),. void (*xS
297d0 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e tep)(sqlite3_con
297e0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 text*,int,sqlite
297f0 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 3_value**),. vo
29800 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c id (*xFinal)(sql
29810 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 ite3_context*).)
29820 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
29830 46 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67 F: Text Encoding
29840 73 20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32 s {H10267} <S502
29850 30 30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a 00> <H16100>.**.
29860 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e ** These constan
29870 74 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 t define integer
29880 20 63 6f 64 65 73 20 74 68 61 74 20 72 65 70 72 codes that repr
29890 65 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 esent the variou
298a0 73 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 s.** text encodi
298b0 6e 67 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 ngs supported by
298c0 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66 SQLite..*/.#def
298d0 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20 ine SQLITE_UTF8
298e0 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 1.#def
298f0 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 ine SQLITE_UTF16
29900 4c 45 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 LE 2.#def
29910 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 ine SQLITE_UTF16
29920 42 45 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 BE 3.#def
29930 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 ine SQLITE_UTF16
29940 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20 2f 4 /
29950 2a 20 55 73 65 20 6e 61 74 69 76 65 20 62 79 74 * Use native byt
29960 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 e order */.#defi
29970 6e 65 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20 ne SQLITE_ANY
29980 20 20 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a 5 /*
29990 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
299a0 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f function only */
299b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
299c0 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38 UTF16_ALIGNED 8
299d0 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 /* sqlite3_c
299e0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 reate_collation
299f0 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 only */../*.** C
29a00 41 50 49 33 52 45 46 3a 20 44 65 70 72 65 63 61 API3REF: Depreca
29a10 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a ted Functions.**
29a20 20 44 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a DEPRECATED.**.*
29a30 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e * These function
29a40 73 20 61 72 65 20 5b 64 65 70 72 65 63 61 74 65 s are [deprecate
29a50 64 5d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f d]. In order to
29a60 20 6d 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63 maintain.** bac
29a70 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 kwards compatibi
29a80 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 lity with older
29a90 63 6f 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63 code, these func
29aa0 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a tions continue .
29ab0 2a 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74 ** to be support
29ac0 65 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65 ed. However, ne
29ad0 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 w applications s
29ae0 68 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74 hould avoid.** t
29af0 68 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 20 he use of these
29b00 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68 functions. To h
29b10 65 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65 elp encourage pe
29b20 6f 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a ople to avoid.**
29b30 20 75 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e using these fun
29b40 63 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e ctions, we are n
29b50 6f 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c ot going to tell
29b60 20 79 6f 75 20 77 68 61 74 20 74 68 65 79 20 64 you what they d
29b70 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 o..*/.#ifndef SQ
29b80 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 LITE_OMIT_DEPREC
29b90 41 54 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20 ATED.SQLITE_API
29ba0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 SQLITE_DEPRECATE
29bb0 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67 D int sqlite3_ag
29bc0 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71 gregate_count(sq
29bd0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b lite3_context*);
29be0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
29bf0 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e TE_DEPRECATED in
29c00 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 t sqlite3_expire
29c10 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 d(sqlite3_stmt*)
29c20 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c ;.SQLITE_API SQL
29c30 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 ITE_DEPRECATED i
29c40 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 nt sqlite3_trans
29c50 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c fer_bindings(sql
29c60 69 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69 ite3_stmt*, sqli
29c70 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 te3_stmt*);.SQLI
29c80 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 TE_API SQLITE_DE
29c90 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c PRECATED int sql
29ca0 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f ite3_global_reco
29cb0 76 65 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ver(void);.SQLIT
29cc0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
29cd0 52 45 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c RECATED void sql
29ce0 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 ite3_thread_clea
29cf0 6e 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 nup(void);.SQLIT
29d00 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
29d10 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
29d20 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d te3_memory_alarm
29d30 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 (void(*)(void*,s
29d40 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 qlite3_int64,int
29d50 29 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f ),void*,sqlite3_
29d60 69 6e 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a int64);.#endif..
29d70 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
29d80 4f 62 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75 Obtaining SQL Fu
29d90 6e 63 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72 nction Parameter
29da0 20 56 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d Values {H15100}
29db0 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20200>.**.**
29dc0 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69 The C-language i
29dd0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
29de0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 SQL functions a
29df0 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75 73 nd aggregates us
29e00 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f es.** this set o
29e10 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 f interface rout
29e20 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 ines to access t
29e30 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c he parameter val
29e40 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75 ues on.** the fu
29e50 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 nction or aggreg
29e60 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ate..**.** The x
29e70 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72 Func (for scalar
29e80 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78 functions) or x
29e90 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 67 Step (for aggreg
29ea0 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 73 ates) parameters
29eb0 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f .** to [sqlite3_
29ec0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
29ed0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
29ee0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 create_function1
29ef0 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63 6()].** define c
29f00 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d allbacks that im
29f10 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20 plement the SQL
29f20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 functions and ag
29f30 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65 gregates..** The
29f40 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 4th parameter t
29f50 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b o these callback
29f60 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 s is an array of
29f70 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 pointers to.**
29f80 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
29f90 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
29fa0 73 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 s. There is one
29fb0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d [sqlite3_value]
29fc0 20 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 object for.** e
29fd0 61 63 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f ach parameter to
29fe0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
29ff0 6e 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e n. These routin
2a000 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a es are used to.*
2a010 2a 20 65 78 74 72 61 63 74 20 76 61 6c 75 65 73 * extract values
2a020 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 from the [sqlit
2a030 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
2a040 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 s..**.** These r
2a050 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c outines work onl
2a060 79 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65 y with [protecte
2a070 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
2a080 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79 objects..** Any
2a090 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
2a0a0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f these routines o
2a0b0 6e 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65 n an [unprotecte
2a0c0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
2a0d0 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c .** object resul
2a0e0 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 ts in undefined
2a0f0 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
2a100 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 These routines w
2a110 6f 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 ork just like th
2a120 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
2a130 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 [column access f
2a140 75 6e 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63 unctions].** exc
2a150 65 70 74 20 74 68 61 74 20 20 74 68 65 73 65 20 ept that these
2a160 72 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20 routines take a
2a170 73 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65 single [protecte
2a180 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d d sqlite3_value]
2a190 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 object.** point
2a1a0 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 er instead of a
2a1b0 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20 [sqlite3_stmt*]
2a1c0 70 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69 pointer and an i
2a1d0 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 nteger column nu
2a1e0 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 mber..**.** The
2a1f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2a200 78 74 31 36 28 29 20 69 6e 74 65 72 66 61 63 65 xt16() interface
2a210 20 65 78 74 72 61 63 74 73 20 61 20 55 54 46 2d extracts a UTF-
2a220 31 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 16 string.** in
2a230 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d the native byte-
2a240 6f 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73 order of the hos
2a250 74 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a t machine. The.
2a260 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
2a270 5f 74 65 78 74 31 36 62 65 28 29 20 61 6e 64 20 _text16be() and
2a280 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2a290 78 74 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61 xt16le() interfa
2a2a0 63 65 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55 ces.** extract U
2a2b0 54 46 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73 TF-16 strings as
2a2c0 20 62 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20 big-endian and
2a2d0 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65 little-endian re
2a2e0 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a spectively..**.*
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 6e 75 6d 65 72 69 63 5f 74 79 70 65 lue_numeric_type
2a310 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 () interface att
2a320 65 6d 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a empts to apply.*
2a330 2a 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 * numeric affini
2a340 74 79 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e ty to the value.
2a350 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 This means tha
2a360 74 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a t an attempt is.
2a370 2a 2a 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65 ** made to conve
2a380 72 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 rt the value to
2a390 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c an integer or fl
2a3a0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49 oating point. I
2a3b0 66 0a 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76 f.** such a conv
2a3c0 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 ersion is possib
2a3d0 6c 65 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 le without loss
2a3e0 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 of information (
2a3f0 69 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 in other.** word
2a400 73 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 s, if the value
2a410 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 is a string that
2a420 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 looks like a nu
2a430 6d 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68 mber).** then th
2a440 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 e conversion is
2a450 70 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65 performed. Othe
2a460 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73 rwise no convers
2a470 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54 ion occurs..** T
2a480 68 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 he [SQLITE_INTEG
2a490 45 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61 ER | datatype] a
2a4a0 66 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20 fter conversion
2a4b0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
2a4c0 2a 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 61 ** Please pay pa
2a4d0 72 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69 rticular attenti
2a4e0 6f 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 74 on to the fact t
2a4f0 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 hat the pointer
2a500 72 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d returned.** from
2a510 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
2a520 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65 blob()], [sqlite
2a530 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 3_value_text()],
2a540 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f or.** [sqlite3_
2a550 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20 value_text16()]
2a560 63 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74 can be invalidat
2a570 65 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65 ed by a subseque
2a580 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 nt call to.** [s
2a590 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
2a5a0 65 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f es()], [sqlite3_
2a5b0 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d value_bytes16()]
2a5c0 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 , [sqlite3_value
2a5d0 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 _text()],.** or
2a5e0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 [sqlite3_value_t
2a5f0 65 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 ext16()]..**.**
2a600 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d These routines m
2a610 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 ust be called fr
2a620 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 om the same thre
2a630 61 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c ad as.** the SQL
2a640 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73 function that s
2a650 75 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c upplied the [sql
2a660 69 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72 ite3_value*] par
2a670 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 ameters..**.** R
2a680 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
2a690 5b 48 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36 [H15103] [H15106
2a6a0 5d 20 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31 ] [H15109] [H151
2a6b0 31 32 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31 12] [H15115] [H1
2a6c0 35 31 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b 5118] [H15121] [
2a6d0 48 31 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31 H15124].** [H151
2a6e0 32 37 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31 27] [H15130] [H1
2a6f0 35 31 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a 5133] [H15136].*
2a700 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
2a710 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
2a720 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69 _value_blob(sqli
2a730 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
2a740 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
2a750 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
2a760 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2a770 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2a780 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
2a790 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61 tes16(sqlite3_va
2a7a0 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2a7b0 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 I double sqlite3
2a7c0 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71 _value_double(sq
2a7d0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2a7e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
2a7f0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 lite3_value_int(
2a800 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2a810 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
2a820 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 te3_int64 sqlite
2a830 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 3_value_int64(sq
2a840 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2a850 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2a860 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 unsigned char *s
2a870 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
2a880 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t(sqlite3_value*
2a890 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
2a8a0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
2a8b0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 3_value_text16(s
2a8c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
2a8d0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
2a8e0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 void *sqlite3_v
2a8f0 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71 alue_text16le(sq
2a900 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2a910 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
2a920 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 void *sqlite3_va
2a930 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c lue_text16be(sql
2a940 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
2a950 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2a960 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
2a970 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2a980 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
2a990 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 sqlite3_value_nu
2a9a0 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 meric_type(sqlit
2a9b0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a e3_value*);../*.
2a9c0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74 ** CAPI3REF: Obt
2a9d0 61 69 6e 20 41 67 67 72 65 67 61 74 65 20 46 75 ain Aggregate Fu
2a9e0 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b nction Context {
2a9f0 48 31 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e H16210} <S20200>
2aa00 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 .**.** The imple
2aa10 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67 mentation of agg
2aa20 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 regate SQL funct
2aa30 69 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f ions use this ro
2aa40 75 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74 utine to allocat
2aa50 65 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65 e.** a structure
2aa60 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 for storing the
2aa70 69 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 ir state..**.**
2aa80 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 The first time t
2aa90 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 he sqlite3_aggre
2aaa0 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72 gate_context() r
2aab0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
2aac0 20 66 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63 for a.** partic
2aad0 75 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c 20 ular aggregate,
2aae0 53 51 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73 SQLite allocates
2aaf0 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 nBytes of memor
2ab00 79 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68 y, zeroes out th
2ab10 61 74 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e at.** memory, an
2ab20 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e d returns a poin
2ab30 74 65 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65 ter to it. On se
2ab40 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 cond and subsequ
2ab50 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 ent calls to.**
2ab60 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
2ab70 65 5f 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20 e_context() for
2ab80 74 68 65 20 73 61 6d 65 20 61 67 67 72 65 67 61 the same aggrega
2ab90 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65 te function inde
2aba0 78 2c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62 x,.** the same b
2abb0 75 66 66 65 72 20 69 73 20 72 65 74 75 72 6e 65 uffer is returne
2abc0 64 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 d. The implement
2abd0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 ation of the agg
2abe0 72 65 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a regate can use.*
2abf0 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62 * the returned b
2ac00 75 66 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c uffer to accumul
2ac10 61 74 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 ate data..**.**
2ac20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 SQLite automatic
2ac30 61 6c 6c 79 20 66 72 65 65 73 20 74 68 65 20 61 ally frees the a
2ac40 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 llocated buffer
2ac50 77 68 65 6e 20 74 68 65 20 61 67 67 72 65 67 61 when the aggrega
2ac60 74 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63 te.** query conc
2ac70 6c 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ludes..**.** The
2ac80 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
2ac90 20 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70 should be a cop
2aca0 79 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c y of the.** [sql
2acb0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53 ite3_context | S
2acc0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 QL function cont
2acd0 65 78 74 5d 20 74 68 61 74 20 69 73 20 74 68 65 ext] that is the
2ace0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
2acf0 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 .** to the callb
2ad00 61 63 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74 ack routine that
2ad10 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
2ad20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
2ad30 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 on..**.** This r
2ad40 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 outine must be c
2ad50 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 alled from the s
2ad60 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 ame thread in wh
2ad70 69 63 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65 ich.** the aggre
2ad80 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f gate SQL functio
2ad90 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a n is running..**
2ada0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
2adb0 3a 0a 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48 :.** [H16211] [H
2adc0 31 36 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20 16213] [H16215]
2add0 5b 48 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49 [H16217].*/.SQLI
2ade0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
2adf0 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
2ae00 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63 ontext(sqlite3_c
2ae10 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79 ontext*, int nBy
2ae20 74 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 tes);../*.** CAP
2ae30 49 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 61 I3REF: User Data
2ae40 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b For Functions {
2ae50 48 31 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e H16240} <S20200>
2ae60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2ae70 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69 e3_user_data() i
2ae80 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
2ae90 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 a copy of.** th
2aea0 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77 e pointer that w
2aeb0 61 73 20 74 68 65 20 70 55 73 65 72 44 61 74 61 as the pUserData
2aec0 20 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20 parameter (the
2aed0 35 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 5th parameter).*
2aee0 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 * of the [sqlite
2aef0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
2af00 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c n()].** and [sql
2af10 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
2af20 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e tion16()] routin
2af30 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c es that original
2af40 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 ly.** registered
2af50 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2af60 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f defined functio
2af70 6e 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 n. {END}.**.** T
2af80 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 his routine must
2af90 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 be called from
2afa0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
2afb0 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 in which.** the
2afc0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
2afd0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ned function is
2afe0 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 running..**.** R
2aff0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
2b000 5b 48 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49 [H16243].*/.SQLI
2b010 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
2b020 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 ite3_user_data(s
2b030 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
2b040 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2b050 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e F: Database Conn
2b060 65 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74 ection For Funct
2b070 69 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53 ions {H16250} <S
2b080 36 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a 60600><S20200>.*
2b090 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2b0a0 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
2b0b0 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 le() interface r
2b0c0 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 eturns a copy of
2b0d0 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 .** the pointer
2b0e0 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 to the [database
2b0f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68 connection] (th
2b100 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29 e 1st parameter)
2b110 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 .** of the [sqli
2b120 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
2b130 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 ion()].** and [s
2b140 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
2b150 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 nction16()] rout
2b160 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e ines that origin
2b170 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 ally.** register
2b180 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ed the applicati
2b190 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on defined funct
2b1a0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ion..**.** Requi
2b1b0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 rements:.** [H16
2b1c0 32 35 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 253].*/.SQLITE_A
2b1d0 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 PI sqlite3 *sqli
2b1e0 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
2b1f0 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f andle(sqlite3_co
2b200 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 ntext*);../*.**
2b210 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69 CAPI3REF: Functi
2b220 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74 on Auxiliary Dat
2b230 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32 a {H16270} <S202
2b240 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 00>.**.** The fo
2b250 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63 llowing two func
2b260 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65 tions may be use
2b270 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20 d by scalar SQL
2b280 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 functions to.**
2b290 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61 associate metada
2b2a0 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 ta with argument
2b2b0 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20 values. If the
2b2c0 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61 same value is pa
2b2d0 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69 ssed to.** multi
2b2e0 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 ple invocations
2b2f0 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20 of the same SQL
2b300 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20 function during
2b310 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c query execution,
2b320 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63 under.** some c
2b330 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65 ircumstances the
2b340 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61 associated meta
2b350 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73 data may be pres
2b360 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a erved. This may.
2b370 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20 ** be used, for
2b380 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20 example, to add
2b390 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73 a regular-expres
2b3a0 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63 sion matching sc
2b3b0 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e alar.** function
2b3c0 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76 . The compiled v
2b3d0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 ersion of the re
2b3e0 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e gular expression
2b3f0 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a is stored as.**
2b400 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69 metadata associ
2b410 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51 ated with the SQ
2b420 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 L value passed a
2b430 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 s the regular ex
2b440 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74 pression.** patt
2b450 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c ern. The compil
2b460 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 ed regular expre
2b470 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75 ssion can be reu
2b480 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a sed on multiple.
2b490 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f ** invocations o
2b4a0 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 f the same funct
2b4b0 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20 ion so that the
2b4c0 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e original pattern
2b4d0 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20 string.** does
2b4e0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 not need to be r
2b4f0 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63 ecompiled on eac
2b500 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a h invocation..**
2b510 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2b520 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e get_auxdata() in
2b530 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
2b540 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
2b550 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73 metadata.** ass
2b560 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73 ociated by the s
2b570 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
2b580 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69 ta() function wi
2b590 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d th the Nth argum
2b5a0 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 ent.** value to
2b5b0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
2b5c0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2b5d0 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61 . If no metadata
2b5e0 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a has been ever.*
2b5f0 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74 * been set for t
2b600 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20 he Nth argument
2b610 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c of the function,
2b620 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65 or if the corre
2b630 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 sponding.** func
2b640 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68 tion parameter h
2b650 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65 as changed since
2b660 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77 the meta-data w
2b670 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 as set,.** then
2b680 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 sqlite3_get_auxd
2b690 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20 ata() returns a
2b6a0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
2b6b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2b6c0 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e set_auxdata() in
2b6d0 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68 terface saves th
2b6e0 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f e metadata.** po
2b6f0 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20 inted to by its
2b700 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 3rd parameter as
2b710 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f the metadata fo
2b720 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72 r the N-th.** ar
2b730 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70 gument of the ap
2b740 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2b750 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62 d function. Sub
2b760 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 sequent.** calls
2b770 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f to sqlite3_get_
2b780 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20 auxdata() might
2b790 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61 return this data
2b7a0 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e , if it has.** n
2b7b0 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65 ot been destroye
2b7c0 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e d..** If it is n
2b7d0 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20 ot NULL, SQLite
2b7e0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 will invoke the
2b7f0 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75 destructor.** fu
2b800 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20 nction given by
2b810 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 the 4th paramete
2b820 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 r to sqlite3_set
2b830 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a _auxdata() on.**
2b840 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68 the metadata wh
2b850 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e en the correspon
2b860 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61 ding function pa
2b870 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a rameter changes.
2b880 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53 ** or when the S
2b890 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d QL statement com
2b8a0 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65 pletes, whicheve
2b8b0 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a r comes first..*
2b8c0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66 *.** SQLite is f
2b8d0 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 ree to call the
2b8e0 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 destructor and d
2b8f0 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20 rop metadata on
2b900 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 any.** parameter
2b910 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e of any function
2b920 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54 at any time. T
2b930 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 he only guarante
2b940 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 e is that.** the
2b950 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c destructor will
2b960 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 be called befor
2b970 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69 e the metadata i
2b980 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a s dropped..**.**
2b990 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65 In practice, me
2b9a0 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72 tadata is preser
2b9b0 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63 ved between func
2b9c0 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a tion calls for.*
2b9d0 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 * expressions th
2b9e0 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 at are constant
2b9f0 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e at compile time.
2ba00 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c This includes l
2ba10 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73 iteral.** values
2ba20 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c and SQL variabl
2ba30 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 es..**.** These
2ba40 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 routines must be
2ba50 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 called from the
2ba60 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 same thread in
2ba70 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c which.** the SQL
2ba80 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e function is run
2ba90 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ning..**.** Requ
2baa0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
2bab0 36 32 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b 6272] [H16274] [
2bac0 48 31 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d H16276] [H16277]
2bad0 20 5b 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37 [H16278] [H1627
2bae0 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 9].*/.SQLITE_API
2baf0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67 void *sqlite3_g
2bb00 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 et_auxdata(sqlit
2bb10 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 e3_context*, int
2bb20 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 N);.SQLITE_API
2bb30 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 void sqlite3_set
2bb40 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33 _auxdata(sqlite3
2bb50 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e _context*, int N
2bb60 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a , void*, void (*
2bb70 29 28 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a )(void*));.../*.
2bb80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e ** CAPI3REF: Con
2bb90 73 74 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20 stants Defining
2bba0 53 70 65 63 69 61 6c 20 44 65 73 74 72 75 63 74 Special Destruct
2bbb0 6f 72 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30 or Behavior {H10
2bbc0 32 38 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a 280} <S30100>.**
2bbd0 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73 70 .** These are sp
2bbe0 65 63 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 ecial values for
2bbf0 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
2bc00 74 68 61 74 20 69 73 20 70 61 73 73 65 64 20 69 that is passed i
2bc10 6e 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61 n as the.** fina
2bc20 6c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f l argument to ro
2bc30 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c utines like [sql
2bc40 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 ite3_result_blob
2bc50 28 29 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73 ()]. If the des
2bc60 74 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d tructor.** argum
2bc70 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54 ent is SQLITE_ST
2bc80 41 54 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74 ATIC, it means t
2bc90 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 hat the content
2bca0 70 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74 pointer is const
2bcb0 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20 ant.** and will
2bcc0 6e 65 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49 never change. I
2bcd0 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 t does not need
2bce0 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e to be destroyed.
2bcf0 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f The.** SQLITE_
2bd00 54 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20 TRANSIENT value
2bd10 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 means that the c
2bd20 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65 ontent will like
2bd30 6c 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 ly change in.**
2bd40 74 68 65 20 6e 65 61 72 20 66 75 74 75 72 65 20 the near future
2bd50 61 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65 20 and that SQLite
2bd60 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 should make its
2bd70 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 own private copy
2bd80 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 of.** the conte
2bd90 6e 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e nt before return
2bda0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 ing..**.** The t
2bdb0 79 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 73 ypedef is necess
2bdc0 61 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 ary to work arou
2bdd0 6e 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63 nd problems in c
2bde0 65 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f ertain.** C++ co
2bdf0 6d 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69 mpilers. See ti
2be00 63 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74 cket #2191..*/.t
2be10 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71 ypedef void (*sq
2be20 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 lite3_destructor
2be30 5f 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23 _type)(void*);.#
2be40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
2be50 41 54 49 43 20 20 20 20 20 20 28 28 73 71 6c 69 ATIC ((sqli
2be60 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 te3_destructor_t
2be70 79 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53 ype)0).#define S
2be80 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 QLITE_TRANSIENT
2be90 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 ((sqlite3_dest
2bea0 72 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a ructor_type)-1).
2beb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2bec0 20 53 65 74 74 69 6e 67 20 54 68 65 20 52 65 73 Setting The Res
2bed0 75 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75 ult Of An SQL Fu
2bee0 6e 63 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20 nction {H16400}
2bef0 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S20200>.**.** T
2bf00 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 hese routines ar
2bf10 65 20 75 73 65 64 20 62 79 20 74 68 65 20 78 46 e used by the xF
2bf20 75 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61 unc or xFinal ca
2bf30 6c 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20 llbacks that.**
2bf40 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75 implement SQL fu
2bf50 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 nctions and aggr
2bf60 65 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20 egates. See.**
2bf70 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
2bf80 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 function()] and
2bf90 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
2bfa0 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a function16()].**
2bfb0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
2bfc0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a information..**.
2bfd0 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f ** These functio
2bfe0 6e 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63 ns work very muc
2bff0 68 20 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61 h like the [para
2c000 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66 meter binding] f
2c010 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63 amily of.** func
2c020 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69 tions used to bi
2c030 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73 nd values to hos
2c040 74 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 t parameters in
2c050 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
2c060 6e 74 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f nts..** Refer to
2c070 20 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65 the [SQL parame
2c080 74 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 ter] documentati
2c090 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 on for additiona
2c0a0 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
2c0b0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2c0c0 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69 _result_blob() i
2c0d0 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 nterface sets th
2c0e0 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a e result from.**
2c0f0 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d an application-
2c100 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2c110 20 74 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20 to be the BLOB
2c120 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73 whose content is
2c130 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 pointed.** to b
2c140 79 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 y the second par
2c150 61 6d 65 74 65 72 20 61 6e 64 20 77 68 69 63 68 ameter and which
2c160 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 is N bytes long
2c170 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a where N is the.
2c180 2a 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 ** third paramet
2c190 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 er..**.** The sq
2c1a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 lite3_result_zer
2c1b0 6f 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 oblob() interfac
2c1c0 65 73 20 73 65 74 20 74 68 65 20 72 65 73 75 6c es set the resul
2c1d0 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c t of.** the appl
2c1e0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
2c1f0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 function to be a
2c200 20 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67 BLOB containing
2c210 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74 all zero.** byt
2c220 65 73 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69 es and N bytes i
2c230 6e 20 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20 n size, where N
2c240 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 is the value of
2c250 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
2c260 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c r..**.** The sql
2c270 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 ite3_result_doub
2c280 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 le() interface s
2c290 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 ets the result f
2c2a0 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 rom.** an applic
2c2b0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2c2c0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66 nction to be a f
2c2d0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
2c2e0 6c 75 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a lue specified.**
2c2f0 20 62 79 20 69 74 73 20 32 6e 64 20 61 72 67 75 by its 2nd argu
2c300 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
2c310 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2c320 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 rror() and sqlit
2c330 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 e3_result_error1
2c340 36 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 6() functions.**
2c350 20 63 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65 cause the imple
2c360 6d 65 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74 mented SQL funct
2c370 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 ion to throw an
2c380 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51 exception..** SQ
2c390 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 74 Lite uses the st
2c3a0 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 ring pointed to
2c3b0 62 79 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61 by the.** 2nd pa
2c3c0 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74 rameter of sqlit
2c3d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
2c3e0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 ) or sqlite3_res
2c3f0 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a ult_error16().**
2c400 20 61 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 as the text of
2c410 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
2c420 2e 20 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70 . SQLite interp
2c430 72 65 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a rets the error.*
2c440 2a 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 * message string
2c450 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 from sqlite3_re
2c460 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20 sult_error() as
2c470 55 54 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a UTF-8. SQLite.**
2c480 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 interprets the
2c490 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 string from sqli
2c4a0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2c4b0 31 36 28 29 20 61 73 20 55 54 46 2d 31 36 20 69 16() as UTF-16 i
2c4c0 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65 n native.** byte
2c4d0 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65 20 order. If the
2c4e0 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
2c4f0 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c to sqlite3_resul
2c500 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 t_error().** or
2c510 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2c520 72 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61 rror16() is nega
2c530 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65 tive then SQLite
2c540 20 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 72 takes as the er
2c550 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61 ror.** message a
2c560 6c 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75 ll text up throu
2c570 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 gh the first zer
2c580 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 o character..**
2c590 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72 If the third par
2c5a0 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 ameter to sqlite
2c5b0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
2c5c0 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 or.** sqlite3_r
2c5d0 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 esult_error16()
2c5e0 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 is non-negative
2c5f0 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 then SQLite take
2c600 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62 s that many.** b
2c610 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 ytes (not charac
2c620 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32 ters) from the 2
2c630 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20 nd parameter as
2c640 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
2c650 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 e..** The sqlite
2c660 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 3_result_error()
2c670 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 and sqlite3_res
2c680 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a ult_error16().**
2c690 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61 routines make a
2c6a0 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 private copy of
2c6b0 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
2c6c0 67 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a ge text before.*
2c6d0 2a 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20 * they return.
2c6e0 48 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69 Hence, the calli
2c6f0 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 ng function can
2c700 64 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a deallocate or.**
2c710 20 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78 74 modify the text
2c720 20 61 66 74 65 72 20 74 68 65 79 20 72 65 74 75 after they retu
2c730 72 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e rn without harm.
2c740 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2c750 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 result_error_cod
2c760 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 e() function cha
2c770 6e 67 65 73 20 74 68 65 20 65 72 72 6f 72 20 63 nges the error c
2c780 6f 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 ode.** returned
2c790 62 79 20 53 51 4c 69 74 65 20 61 73 20 61 20 72 by SQLite as a r
2c7a0 65 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f esult of an erro
2c7b0 72 20 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e r in a function.
2c7c0 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a By default,.**
2c7d0 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
2c7e0 69 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e is SQLITE_ERROR.
2c7f0 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 A subsequent c
2c800 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 all to sqlite3_r
2c810 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a esult_error().**
2c820 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 or sqlite3_resu
2c830 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73 lt_error16() res
2c840 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f ets the error co
2c850 64 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 de to SQLITE_ERR
2c860 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 OR..**.** The sq
2c870 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f lite3_result_too
2c880 62 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 big() interface
2c890 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f causes SQLite to
2c8a0 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a throw an error.
2c8b0 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 ** indicating th
2c8c0 61 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42 at a string or B
2c8d0 4c 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74 LOB is to long t
2c8e0 6f 20 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a o represent..**.
2c8f0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2c900 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e esult_nomem() in
2c910 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53 terface causes S
2c920 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 QLite to throw a
2c930 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 n error.** indic
2c940 61 74 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d ating that a mem
2c950 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
2c960 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ailed..**.** The
2c970 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2c980 69 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 int() interface
2c990 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 sets the return
2c9a0 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 value.** of the
2c9b0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
2c9c0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 ned function to
2c9d0 62 65 20 74 68 65 20 33 32 2d 62 69 74 20 73 69 be the 32-bit si
2c9e0 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 gned integer.**
2c9f0 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 value given in t
2ca00 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e he 2nd argument.
2ca10 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2ca20 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69 result_int64() i
2ca30 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 nterface sets th
2ca40 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
2ca50 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 * of the applica
2ca60 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
2ca70 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 ction to be the
2ca80 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
2ca90 74 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 teger.** value g
2caa0 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 iven in the 2nd
2cab0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
2cac0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
2cad0 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66 lt_null() interf
2cae0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74 ace sets the ret
2caf0 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 urn value.** of
2cb00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
2cb10 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2cb20 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a to be NULL..**.
2cb30 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 ** The sqlite3_r
2cb40 65 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71 esult_text(), sq
2cb50 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2cb60 74 31 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 t16(),.** sqlite
2cb70 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 3_result_text16l
2cb80 65 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 e(), and sqlite3
2cb90 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 _result_text16be
2cba0 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a () interfaces.**
2cbb0 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 set the return
2cbc0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70 value of the app
2cbd0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
2cbe0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a function to be.
2cbf0 2a 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e 67 ** a text string
2cc00 20 77 68 69 63 68 20 69 73 20 72 65 70 72 65 73 which is repres
2cc10 65 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20 ented as UTF-8,
2cc20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 UTF-16 native by
2cc30 74 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46 te order,.** UTF
2cc40 2d 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 -16 little endia
2cc50 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 n, or UTF-16 big
2cc60 20 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 endian, respect
2cc70 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 ively..** SQLite
2cc80 20 74 61 6b 65 73 20 74 68 65 20 74 65 78 74 20 takes the text
2cc90 72 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20 result from the
2cca0 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d application from
2ccb0 0a 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61 .** the 2nd para
2ccc0 6d 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c meter of the sql
2ccd0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2cce0 2a 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a * interfaces..**
2ccf0 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 If the 3rd para
2cd00 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c meter to the sql
2cd10 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2cd20 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 * interfaces.**
2cd30 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 is negative, the
2cd40 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72 n SQLite takes r
2cd50 65 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20 esult text from
2cd60 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 the 2nd paramete
2cd70 72 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 r.** through the
2cd80 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 first zero char
2cd90 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 acter..** If the
2cda0 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 3rd parameter t
2cdb0 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 o the sqlite3_re
2cdc0 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 sult_text* inter
2cdd0 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d faces.** is non-
2cde0 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61 negative, then a
2cdf0 73 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f s many bytes (no
2ce00 74 20 63 68 61 72 61 63 74 65 72 73 29 20 6f 66 t characters) of
2ce10 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69 the text.** poi
2ce20 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 32 nted to by the 2
2ce30 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 72 65 nd parameter are
2ce40 20 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70 taken as the ap
2ce50 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2ce60 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 d.** function re
2ce70 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 sult..** If the
2ce80 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 4th parameter to
2ce90 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 the sqlite3_res
2cea0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 ult_text* interf
2ceb0 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 aces.** or sqlit
2cec0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 e3_result_blob i
2ced0 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 s a non-NULL poi
2cee0 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 nter, then SQLit
2cef0 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20 e calls that.**
2cf00 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20 function as the
2cf10 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 destructor on th
2cf20 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72 e text or BLOB r
2cf30 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61 esult when it ha
2cf40 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73 s.** finished us
2cf50 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e ing that result.
2cf60 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 .** If the 4th p
2cf70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 arameter to the
2cf80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2cf90 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20 ext* interfaces
2cfa0 6f 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 or to.** sqlite3
2cfb0 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20 _result_blob is
2cfc0 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 the special cons
2cfd0 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54 tant SQLITE_STAT
2cfe0 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a IC, then SQLite.
2cff0 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 ** assumes that
2d000 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 the text or BLOB
2d010 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 6f result is in co
2d020 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e 64 nstant space and
2d030 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f 70 does not.** cop
2d040 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 y the content of
2d050 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e the parameter n
2d060 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75 or call a destru
2d070 63 74 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74 ctor on the cont
2d080 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68 ent.** when it h
2d090 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e as finished usin
2d0a0 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a g that result..*
2d0b0 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 * If the 4th par
2d0c0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
2d0d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2d0e0 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a t* interfaces.**
2d0f0 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 or sqlite3_resu
2d100 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73 lt_blob is the s
2d110 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 pecial constant
2d120 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
2d130 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20 .** then SQLite
2d140 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 makes a copy of
2d150 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 the result into
2d160 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 space obtained f
2d170 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c rom.** from [sql
2d180 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 ite3_malloc()] b
2d190 65 66 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73 efore it returns
2d1a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2d1b0 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
2d1c0 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 () interface set
2d1d0 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a s the result of.
2d1e0 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 ** the applicati
2d1f0 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
2d200 69 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79 ion to be a copy
2d210 20 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 the.** [unprote
2d220 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2d230 75 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69 ue] object speci
2d240 66 69 65 64 20 62 79 20 74 68 65 20 32 6e 64 20 fied by the 2nd
2d250 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a parameter. The.
2d260 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c ** sqlite3_resul
2d270 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 t_value() interf
2d280 61 63 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 ace makes a copy
2d290 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
2d2a0 5f 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68 _value].** so th
2d2b0 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f at the [sqlite3_
2d2c0 76 61 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64 value] specified
2d2d0 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65 in the paramete
2d2e0 72 20 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a r may change or.
2d2f0 2a 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 ** be deallocate
2d300 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f d after sqlite3_
2d310 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72 result_value() r
2d320 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68 eturns without h
2d330 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65 arm..** A [prote
2d340 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c cted sqlite3_val
2d350 75 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61 ue] object may a
2d360 6c 77 61 79 73 20 62 65 20 75 73 65 64 20 77 68 lways be used wh
2d370 65 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f ere an.** [unpro
2d380 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 tected sqlite3_v
2d390 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20 alue] object is
2d3a0 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74 required, so eit
2d3b0 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b her.** kind of [
2d3c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
2d3d0 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73 65 bject can be use
2d3e0 64 20 77 69 74 68 20 74 68 69 73 20 69 6e 74 65 d with this inte
2d3f0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 rface..**.** If
2d400 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 these routines a
2d410 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 re called from w
2d420 69 74 68 69 6e 20 74 68 65 20 64 69 66 66 65 72 ithin the differ
2d430 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68 ent thread.** th
2d440 61 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61 an the one conta
2d450 69 6e 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63 ining the applic
2d460 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2d470 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65 nction that rece
2d480 69 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c ived.** the [sql
2d490 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f ite3_context] po
2d4a0 69 6e 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c inter, the resul
2d4b0 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 ts are undefined
2d4c0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
2d4d0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33 ents:.** [H16403
2d4e0 5d 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34 ] [H16406] [H164
2d4f0 30 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31 09] [H16412] [H1
2d500 36 34 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b 6415] [H16418] [
2d510 48 31 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d H16421] [H16424]
2d520 0a 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31 .** [H16427] [H1
2d530 36 34 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b 6430] [H16433] [
2d540 48 31 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d H16436] [H16439]
2d550 20 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34 [H16442] [H1644
2d560 35 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b 5] [H16448].** [
2d570 48 31 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d H16451] [H16454]
2d580 20 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36 [H16457] [H1646
2d590 30 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53 0] [H16463].*/.S
2d5a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2d5b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c qlite3_result_bl
2d5c0 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ob(sqlite3_conte
2d5d0 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a xt*, const void*
2d5e0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 , int, void(*)(v
2d5f0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 oid*));.SQLITE_A
2d600 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2d610 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 result_double(sq
2d620 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2d630 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f double);.SQLITE_
2d640 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2d650 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 _result_error(sq
2d660 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2d670 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
2d680 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
2d690 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
2d6a0 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 t_error16(sqlite
2d6b0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 3_context*, cons
2d6c0 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 t void*, int);.S
2d6d0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2d6e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2d6f0 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 ror_toobig(sqlit
2d700 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 e3_context*);.SQ
2d710 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2d720 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2d730 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 or_nomem(sqlite3
2d740 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 _context*);.SQLI
2d750 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2d760 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2d770 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f _code(sqlite3_co
2d780 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 ntext*, int);.SQ
2d790 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2d7a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
2d7b0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2d7c0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
2d7d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2d7e0 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 _result_int64(sq
2d7f0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2d800 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a sqlite3_int64);.
2d810 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2d820 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e sqlite3_result_n
2d830 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ull(sqlite3_cont
2d840 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ext*);.SQLITE_AP
2d850 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2d860 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 esult_text(sqlit
2d870 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e e3_context*, con
2d880 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 st char*, int, v
2d890 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
2d8a0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2d8b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2d8c0 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f ext16(sqlite3_co
2d8d0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f ntext*, const vo
2d8e0 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a id*, int, void(*
2d8f0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
2d900 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2d910 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 e3_result_text16
2d920 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 le(sqlite3_conte
2d930 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a xt*, const void*
2d940 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f , int,void(*)(vo
2d950 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 id*));.SQLITE_AP
2d960 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
2d970 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 esult_text16be(s
2d980 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2d990 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
2d9a0 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 t,void(*)(void*)
2d9b0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
2d9c0 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
2d9d0 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f t_value(sqlite3_
2d9e0 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 context*, sqlite
2d9f0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 3_value*);.SQLIT
2da00 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2da10 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c e3_result_zerobl
2da20 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 ob(sqlite3_conte
2da30 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a xt*, int n);../*
2da40 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 .** CAPI3REF: De
2da50 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 fine New Collati
2da60 6e 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 ng Sequences {H1
2da70 36 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 6600} <S20300>.*
2da80 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 *.** These funct
2da90 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f ions are used to
2daa0 20 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 add new collati
2dab0 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 on sequences to
2dac0 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 the.** [database
2dad0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 connection] spe
2dae0 63 69 66 69 65 64 20 61 73 20 74 68 65 20 66 69 cified as the fi
2daf0 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a rst argument..**
2db00 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 .** The name of
2db10 74 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f the new collatio
2db20 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 n sequence is sp
2db30 65 63 69 66 69 65 64 20 61 73 20 61 20 55 54 46 ecified as a UTF
2db40 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 -8 string.** for
2db50 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2db60 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 collation() and
2db70 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2db80 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a ollation_v2().**
2db90 20 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 and a UTF-16 st
2dba0 72 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 ring for sqlite3
2dbb0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2dbc0 6e 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 n16(). In all ca
2dbd0 73 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 ses.** the name
2dbe0 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 is passed as the
2dbf0 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e second function
2dc00 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
2dc10 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d The third argum
2dc20 65 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f ent may be one o
2dc30 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 f the constants
2dc40 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a [SQLITE_UTF8],.*
2dc50 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c * [SQLITE_UTF16L
2dc60 45 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 E], or [SQLITE_U
2dc70 54 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 TF16BE], indicat
2dc80 69 6e 67 20 74 68 61 74 20 74 68 65 20 75 73 65 ing that the use
2dc90 72 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f r-supplied.** ro
2dca0 75 74 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f utine expects to
2dcb0 20 62 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 be passed point
2dcc0 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 ers to strings e
2dcd0 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 ncoded using UTF
2dce0 2d 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 -8,.** UTF-16 li
2dcf0 74 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 ttle-endian, or
2dd00 55 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 UTF-16 big-endia
2dd10 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e n, respectively.
2dd20 20 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 The.** third ar
2dd30 67 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 gument might als
2dd40 6f 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 o be [SQLITE_UTF
2dd50 31 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20 16] to indicate
2dd60 74 68 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65 that the routine
2dd70 0a 2a 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e .** expects poin
2dd80 74 65 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31 ters to be UTF-1
2dd90 36 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 6 strings in the
2dda0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
2ddb0 65 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72 er, or the.** ar
2ddc0 67 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53 gument can be [S
2ddd0 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
2dde0 4e 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74 NED] if the.** t
2ddf0 68 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 he routine expec
2de00 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 ts pointers to 1
2de10 36 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 6-bit word align
2de20 65 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 ed strings.** of
2de30 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e UTF-16 in the n
2de40 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
2de50 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 ..**.** A pointe
2de60 72 20 74 6f 20 74 68 65 20 75 73 65 72 20 73 75 r to the user su
2de70 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d pplied routine m
2de80 75 73 74 20 62 65 20 70 61 73 73 65 64 20 61 73 ust be passed as
2de90 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72 the fifth.** ar
2dea0 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69 gument. If it i
2deb0 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20 s NULL, this is
2dec0 74 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65 the same as dele
2ded0 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 ting the collati
2dee0 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28 on.** sequence (
2def0 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 so that SQLite c
2df00 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e annot call it an
2df10 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20 ymore)..** Each
2df20 74 69 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61 time the applica
2df30 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75 tion supplied fu
2df40 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 nction is invoke
2df50 64 2c 20 69 74 20 69 73 20 70 61 73 73 65 64 0a d, it is passed.
2df60 2a 2a 20 61 73 20 69 74 73 20 66 69 72 73 74 20 ** as its first
2df70 70 61 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79 parameter a copy
2df80 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61 of the void* pa
2df90 73 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 ssed as the four
2dfa0 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 th argument.** t
2dfb0 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 o sqlite3_create
2dfc0 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20 _collation() or
2dfd0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
2dfe0 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a ollation16()..**
2dff0 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e .** The remainin
2e000 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 g arguments to t
2e010 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 he application-s
2e020 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 upplied routine
2e030 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c are two strings,
2e040 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 65 73 65 .** each represe
2e050 6e 74 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74 nted by a (lengt
2e060 68 2c 20 64 61 74 61 29 20 70 61 69 72 20 61 6e h, data) pair an
2e070 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 d encoded in the
2e080 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61 encoding.** tha
2e090 74 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 t was passed as
2e0a0 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 the third argume
2e0b0 6e 74 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c nt when the coll
2e0c0 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 ation sequence w
2e0d0 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64 as.** registered
2e0e0 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70 . {END} The app
2e0f0 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 lication defined
2e100 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69 collation routi
2e110 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 ne should.** ret
2e120 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 urn negative, ze
2e130 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 ro or positive i
2e140 66 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 f the first stri
2e150 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c ng is less than,
2e160 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 .** equal to, or
2e170 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
2e180 65 20 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e e second string.
2e190 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d i.e. (STRING1 -
2e1a0 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a STRING2)..**.**
2e1b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 The sqlite3_cre
2e1c0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 ate_collation_v2
2e1d0 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 () works like sq
2e1e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
2e1f0 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65 lation().** exce
2e200 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 pt that it takes
2e210 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 an extra argume
2e220 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 64 65 nt which is a de
2e230 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20 structor for.**
2e240 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20 the collation.
2e250 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69 The destructor i
2e260 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 s called when th
2e270 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a e collation is.*
2e280 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20 * destroyed and
2e290 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79 is passed a copy
2e2a0 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 of the fourth p
2e2b0 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70 arameter void* p
2e2c0 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 ointer.** of the
2e2d0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2e2e0 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a collation_v2()..
2e2f0 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72 ** Collations ar
2e300 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e e destroyed when
2e310 20 74 68 65 79 20 61 72 65 20 6f 76 65 72 72 69 they are overri
2e320 64 64 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61 dden by later ca
2e330 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f lls to the.** co
2e340 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e llation creation
2e350 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68 functions or wh
2e360 65 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 en the [database
2e370 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 connection] is
2e380 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20 closed.** using
2e390 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 [sqlite3_close()
2e3a0 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 ]..**.** See als
2e3b0 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c o: [sqlite3_col
2e3c0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d lation_needed()]
2e3d0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f and [sqlite3_co
2e3e0 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 llation_needed16
2e3f0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ()]..**.** Requi
2e400 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 rements:.** [H16
2e410 36 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48 603] [H16604] [H
2e420 31 36 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20 16606] [H16609]
2e430 5b 48 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35 [H16612] [H16615
2e440 5d 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36 ] [H16618] [H166
2e450 32 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20 21].** [H16624]
2e460 5b 48 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30 [H16627] [H16630
2e470 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
2e480 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
2e490 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 te_collation(.
2e4a0 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e sqlite3*, . con
2e4b0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
2e4c0 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c . int eTextRep,
2e4d0 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 . void*,. int
2e4e0 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 (*xCompare)(void
2e4f0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2e500 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2e510 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 *).);.SQLITE_API
2e520 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 int sqlite3_cre
2e530 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 ate_collation_v2
2e540 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 (. sqlite3*, .
2e550 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
2e560 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 me, . int eText
2e570 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 Rep, . void*,.
2e580 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 int(*xCompare)(
2e590 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 void*,int,const
2e5a0 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 void*,int,const
2e5b0 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a void*),. void(*
2e5c0 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 xDestroy)(void*)
2e5d0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
2e5e0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
2e5f0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 e_collation16(.
2e600 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f sqlite3*, . co
2e610 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c nst void *zName,
2e620 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c . int eTextRep,
2e630 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 . void*,. int
2e640 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 (*xCompare)(void
2e650 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2e660 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
2e670 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 *).);../*.** CAP
2e680 49 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e I3REF: Collation
2e690 20 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b Needed Callback
2e6a0 73 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 s {H16700} <S203
2e6b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 00>.**.** To avo
2e6c0 69 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 id having to reg
2e6d0 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 ister all collat
2e6e0 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 ion sequences be
2e6f0 66 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 0a fore a database.
2e700 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 ** can be used,
2e710 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 a single callbac
2e720 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 k function may b
2e730 65 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 e registered wit
2e740 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 h the.** [databa
2e750 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 se connection] t
2e760 6f 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e o be called when
2e770 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 ever an undefine
2e780 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 d collation.** s
2e790 65 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 equence is requi
2e7a0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 red..**.** If th
2e7b0 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 e function is re
2e7c0 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 gistered using t
2e7d0 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 he sqlite3_colla
2e7e0 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 tion_needed() AP
2e7f0 49 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 I,.** then it is
2e800 20 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 passed the name
2e810 73 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 s of undefined c
2e820 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
2e830 65 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a es as strings.**
2e840 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d encoded in UTF-
2e850 38 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 8. {H16703} If s
2e860 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e qlite3_collation
2e870 5f 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 75 _needed16() is u
2e880 73 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 sed,.** the name
2e890 73 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20 s are passed as
2e8a0 55 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e UTF-16 in machin
2e8b0 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 e native byte or
2e8c0 64 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 der..** A call t
2e8d0 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f o either functio
2e8e0 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 n replaces any e
2e8f0 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b xisting callback
2e900 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 ..**.** When the
2e910 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 callback is inv
2e920 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 oked, the first
2e930 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 argument passed
2e940 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 is a copy.** of
2e950 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
2e960 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 ent to sqlite3_c
2e970 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 ollation_needed(
2e980 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ) or.** sqlite3_
2e990 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
2e9a0 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 16(). The secon
2e9b0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 d argument is th
2e9c0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
2e9d0 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 nnection. The t
2e9e0 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 hird argument is
2e9f0 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f one of [SQLITE_
2ea00 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 UTF8], [SQLITE_U
2ea10 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b TF16BE],.** or [
2ea20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c SQLITE_UTF16LE],
2ea30 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 indicating the
2ea40 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 most desirable f
2ea50 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 orm of the colla
2ea60 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 tion.** sequence
2ea70 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 function requir
2ea80 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 ed. The fourth
2ea90 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 parameter is the
2eaa0 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 name of the.**
2eab0 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 required collati
2eac0 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a on sequence..**.
2ead0 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 ** The callback
2eae0 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
2eaf0 72 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73 register the des
2eb00 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 ired collation u
2eb10 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 sing.** [sqlite3
2eb20 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2eb30 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 n()], [sqlite3_c
2eb40 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 reate_collation1
2eb50 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 6()], or.** [sql
2eb60 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
2eb70 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a ation_v2()]..**.
2eb80 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
2eb90 0a 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31 .** [H16702] [H1
2eba0 36 37 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a 6704] [H16706].*
2ebb0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
2ebc0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 sqlite3_collati
2ebd0 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c on_needed(. sql
2ebe0 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c ite3*, . void*,
2ebf0 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 . void(*)(void
2ec00 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 *,sqlite3*,int e
2ec10 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 TextRep,const ch
2ec20 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 ar*).);.SQLITE_A
2ec30 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
2ec40 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 ollation_needed1
2ec50 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 6(. sqlite3*, .
2ec60 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28 void*,. void(
2ec70 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 *)(void*,sqlite3
2ec80 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 *,int eTextRep,c
2ec90 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a onst void*).);..
2eca0 2f 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68 /*.** Specify th
2ecb0 65 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63 e key for an enc
2ecc0 72 79 70 74 65 64 20 64 61 74 61 62 61 73 65 2e rypted database.
2ecd0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 This routine s
2ece0 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c hould be.** call
2ecf0 65 64 20 72 69 67 68 74 20 61 66 74 65 72 20 73 ed right after s
2ed00 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a qlite3_open()..*
2ed10 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f *.** The code to
2ed20 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 implement this
2ed30 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c API is not avail
2ed40 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c able in the publ
2ed50 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 ic release.** of
2ed60 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 SQLite..*/.SQLI
2ed70 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2ed80 65 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 e3_key(. sqlite
2ed90 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
2eda0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
2edb0 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 base to be rekey
2edc0 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f ed */. const vo
2edd0 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b id *pKey, int nK
2ede0 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 ey /* The ke
2edf0 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 y */.);../*.** C
2ee00 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e hange the key on
2ee10 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 an open databas
2ee20 65 2e 20 20 49 66 20 74 68 65 20 63 75 72 72 65 e. If the curre
2ee30 6e 74 20 64 61 74 61 62 61 73 65 20 69 73 20 6e nt database is n
2ee40 6f 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c ot.** encrypted,
2ee50 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 this routine wi
2ee60 6c 6c 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20 ll encrypt it.
2ee70 49 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e If pNew==0 or nN
2ee80 65 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61 ew==0, the.** da
2ee90 74 61 62 61 73 65 20 69 73 20 64 65 63 72 79 70 tabase is decryp
2eea0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ted..**.** The c
2eeb0 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 ode to implement
2eec0 20 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 this API is not
2eed0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 available in th
2eee0 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 e public release
2eef0 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a .** of SQLite..*
2ef00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
2ef10 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a sqlite3_rekey(.
2ef20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
2ef30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ef40 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 /* Database to
2ef50 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 be rekeyed */.
2ef60 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
2ef70 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f , int nKey /
2ef80 2a 20 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f * The new key */
2ef90 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .);../*.** CAPI3
2efa0 52 45 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65 REF: Suspend Exe
2efb0 63 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f cution For A Sho
2efc0 72 74 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d rt Time {H10530}
2efd0 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 <S40410>.**.**
2efe0 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 The sqlite3_slee
2eff0 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 p() function cau
2f000 73 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 ses the current
2f010 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e thread to suspen
2f020 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66 d execution.** f
2f030 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75 or at least a nu
2f040 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 mber of millisec
2f050 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69 onds specified i
2f060 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e n its parameter.
2f070 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 .**.** If the op
2f080 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 erating system d
2f090 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 oes not support
2f0a0 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 77 sleep requests w
2f0b0 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f ith.** milliseco
2f0c0 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69 nd time resoluti
2f0d0 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d on, then the tim
2f0e0 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65 e will be rounde
2f0f0 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e d up to.** the n
2f100 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54 earest second. T
2f110 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c he number of mil
2f120 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 liseconds of sle
2f130 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 ep actually.** r
2f140 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 equested from th
2f150 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
2f160 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a em is returned..
2f170 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70 **.** SQLite imp
2f180 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74 lements this int
2f190 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e erface by callin
2f1a0 67 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a g the xSleep().*
2f1b0 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 * method of the
2f1c0 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 default [sqlite3
2f1d0 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a _vfs] object..**
2f1e0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
2f1f0 3a 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35 : [H10533] [H105
2f200 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 36].*/.SQLITE_AP
2f210 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c I int sqlite3_sl
2f220 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a eep(int);../*.**
2f230 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 CAPI3REF: Name
2f240 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f Of The Folder Ho
2f250 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20 lding Temporary
2f260 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c Files {H10310} <
2f270 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66 S20000>.**.** If
2f280 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 this global var
2f290 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f iable is made to
2f2a0 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69 point to a stri
2f2b0 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 ng which is.** t
2f2c0 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c he name of a fol
2f2d0 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65 der (a.k.a. dire
2f2e0 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c ctory), then all
2f2f0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
2f300 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53 .** created by S
2f310 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c QLite will be pl
2f320 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72 aced in that dir
2f330 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73 ectory. If this
2f340 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20 variable.** is
2f350 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 a NULL pointer,
2f360 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66 then SQLite perf
2f370 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f orms a search fo
2f380 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 r an appropriate
2f390 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 .** temporary fi
2f3a0 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a le directory..**
2f3b0 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 .** It is not sa
2f3c0 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f fe to read or mo
2f3d0 64 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62 dify this variab
2f3e0 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 le in more than
2f3f0 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74 one.** thread at
2f400 20 61 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20 a time. It is
2f410 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 not safe to read
2f420 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 or modify this
2f430 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61 variable.** if a
2f440 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
2f450 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20 ction] is being
2f460 75 73 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 used at the same
2f470 20 74 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72 time in a separ
2f480 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a ate.** thread..*
2f490 2a 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 * It is intended
2f4a0 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61 that this varia
2f4b0 62 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a ble be set once.
2f4c0 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 70 72 ** as part of pr
2f4d0 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 ocess initializa
2f4e0 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20 tion and before
2f4f0 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72 any SQLite inter
2f500 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 face.** routines
2f510 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 have been calle
2f520 64 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20 d and that this
2f530 76 61 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20 variable remain
2f540 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65 unchanged.** the
2f550 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 reafter..**.** T
2f560 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 he [temp_store_d
2f570 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d irectory pragma]
2f580 20 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73 may modify this
2f590 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61 variable and ca
2f5a0 75 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69 use.** it to poi
2f5b0 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 nt to memory obt
2f5c0 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 ained from [sqli
2f5d0 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75 te3_malloc]. Fu
2f5e0 72 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68 rthermore,.** th
2f5f0 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 e [temp_store_di
2f600 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 rectory pragma]
2f610 61 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20 74 always assumes t
2f620 68 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a hat any string.*
2f630 2a 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 * that this vari
2f640 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 able points to i
2f650 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 s held in memory
2f660 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a obtained from .
2f670 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ** [sqlite3_mall
2f680 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67 oc] and the prag
2f690 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74 ma may attempt t
2f6a0 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f o free that memo
2f6b0 72 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c ry.** using [sql
2f6c0 69 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48 ite3_free]..** H
2f6d0 65 6e 63 65 2c 20 69 66 20 74 68 69 73 20 76 61 ence, if this va
2f6e0 72 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69 riable is modifi
2f6f0 65 64 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74 ed directly, eit
2f700 68 65 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65 her it should be
2f710 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72 .** made NULL or
2f720 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 made to point t
2f730 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 o memory obtaine
2f740 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f d from [sqlite3_
2f750 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c malloc].** or el
2f760 73 65 20 74 68 65 20 75 73 65 20 6f 66 20 74 68 se the use of th
2f770 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 e [temp_store_di
2f780 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20 rectory pragma]
2f790 73 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65 should be avoide
2f7a0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
2f7b0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 char *sqlite3_t
2f7c0 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a emp_directory;..
2f7d0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2f7e0 54 65 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f Test For Auto-Co
2f7f0 6d 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33 mmit Mode {H1293
2f800 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b 0} <S60200>.** K
2f810 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f EYWORDS: {autoco
2f820 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a mmit mode}.**.**
2f830 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 The sqlite3_get
2f840 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e _autocommit() in
2f850 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
2f860 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a non-zero or.** z
2f870 65 72 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e ero if the given
2f880 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
2f890 74 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f tion is or is no
2f8a0 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 t in autocommit
2f8b0 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74 mode,.** respect
2f8c0 69 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d ively. Autocomm
2f8d0 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79 it mode is on by
2f8e0 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74 default..** Aut
2f8f0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 ocommit mode is
2f900 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42 disabled by a [B
2f910 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e EGIN] statement.
2f920 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d .** Autocommit m
2f930 6f 64 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65 ode is re-enable
2f940 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 d by a [COMMIT]
2f950 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a or [ROLLBACK]..*
2f960 2a 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20 *.** If certain
2f970 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 kinds of errors
2f980 6f 63 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65 occur on a state
2f990 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75 ment within a mu
2f9a0 6c 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a lti-statement.**
2f9b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72 transaction (er
2f9c0 72 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b rors including [
2f9d0 53 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53 SQLITE_FULL], [S
2f9e0 51 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a QLITE_IOERR],.**
2f9f0 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c [SQLITE_NOMEM],
2fa00 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20 [SQLITE_BUSY],
2fa10 61 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 and [SQLITE_INTE
2fa20 52 52 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65 RRUPT]) then the
2fa30 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
2fa40 6d 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20 might be rolled
2fa50 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c back automatical
2fa60 6c 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 ly. The only wa
2fa70 79 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74 y to.** find out
2fa80 20 77 68 65 74 68 65 72 20 53 51 4c 69 74 65 20 whether SQLite
2fa90 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f automatically ro
2faa0 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 74 72 lled back the tr
2fab0 61 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a ansaction after.
2fac0 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74 ** an error is t
2fad0 6f 20 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 o use this funct
2fae0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ion..**.** If an
2faf0 6f 74 68 65 72 20 74 68 72 65 61 64 20 63 68 61 other thread cha
2fb00 6e 67 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d nges the autocom
2fb10 6d 69 74 20 73 74 61 74 75 73 20 6f 66 20 74 68 mit status of th
2fb20 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f e database.** co
2fb30 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 nnection while t
2fb40 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 his routine is r
2fb50 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 unning, then the
2fb60 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
2fb70 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
2fb80 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2fb90 73 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32 s: [H12931] [H12
2fba0 39 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48 932] [H12933] [H
2fbb0 31 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12934].*/.SQLITE
2fbc0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2fbd0 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 _get_autocommit(
2fbe0 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
2fbf0 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64 * CAPI3REF: Find
2fc00 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61 The Database Ha
2fc10 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72 ndle Of A Prepar
2fc20 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 ed Statement {H1
2fc30 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 3120} <S60600>.*
2fc40 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2fc50 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72 _db_handle inter
2fc60 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
2fc70 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
2fc80 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a ction] handle.**
2fc90 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65 to which a [pre
2fca0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
2fcb0 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b belongs. The [
2fcc0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2fcd0 69 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64 ion].** returned
2fce0 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 by sqlite3_db_h
2fcf0 61 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d andle is the sam
2fd00 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
2fd10 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 ection] that was
2fd20 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
2fd30 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73 ent.** to the [s
2fd40 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
2fd50 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74 2()] call (or it
2fd60 73 20 76 61 72 69 61 6e 74 73 29 20 74 68 61 74 s variants) that
2fd70 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 was used to.**
2fd80 63 72 65 61 74 65 20 74 68 65 20 73 74 61 74 65 create the state
2fd90 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 ment in the firs
2fda0 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 t place..**.** R
2fdb0 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 equirements: [H1
2fdc0 33 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3123].*/.SQLITE_
2fdd0 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c API sqlite3 *sql
2fde0 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 ite3_db_handle(s
2fdf0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a qlite3_stmt*);..
2fe00 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2fe10 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72 Find the next pr
2fe20 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2fe30 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30 {H13140} <S6060
2fe40 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 0>.**.** This in
2fe50 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
2fe60 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
2fe70 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20 next [prepared
2fe80 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72 statement] after
2fe90 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69 .** pStmt associ
2fea0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 ated with the [d
2feb0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
2fec0 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74 on] pDb. If pSt
2fed0 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 mt is NULL.** th
2fee0 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 en this interfac
2fef0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e e returns a poin
2ff00 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74 ter to the first
2ff10 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
2ff20 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 ent.** associate
2ff30 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 d with the datab
2ff40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ase connection p
2ff50 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61 Db. If no prepa
2ff60 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a red statement.**
2ff70 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63 satisfies the c
2ff80 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69 onditions of thi
2ff90 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65 s routine, it re
2ffa0 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a turns NULL..**.*
2ffb0 2a 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 * The [database
2ffc0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e connection] poin
2ffd0 74 65 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20 ter D in a call
2ffe0 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e to.** [sqlite3_n
2fff0 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d ext_stmt(D,S)] m
30000 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 ust refer to an
30010 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a open database.**
30020 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20 connection and
30030 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75 in particular mu
30040 73 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c st not be a NULL
30050 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
30060 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 Requirements: [H
30070 31 33 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20 13143] [H13146]
30080 5b 48 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32 [H13149] [H13152
30090 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
300a0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71 sqlite3_stmt *sq
300b0 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 lite3_next_stmt(
300c0 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71 sqlite3 *pDb, sq
300d0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
300e0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 t);../*.** CAPI3
300f0 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20 REF: Commit And
30100 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63 Rollback Notific
30110 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 ation Callbacks
30120 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30 {H12950} <S60400
30130 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
30140 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 te3_commit_hook(
30150 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 ) interface regi
30160 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b sters a callback
30170 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 .** function to
30180 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 be invoked whene
30190 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f ver a transactio
301a0 6e 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63 n is [COMMIT | c
301b0 6f 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 6e ommitted]..** An
301c0 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 y callback set b
301d0 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c y a previous cal
301e0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d l to sqlite3_com
301f0 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f mit_hook().** fo
30200 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 r the same datab
30210 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
30220 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a s overridden..**
30230 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c The sqlite3_rol
30240 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74 lback_hook() int
30250 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73 erface registers
30260 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 a callback.** f
30270 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e unction to be in
30280 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 voked whenever a
30290 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
302a0 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c 6c [ROLLBACK | roll
302b0 65 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e 79 ed back]..** Any
302c0 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79 callback set by
302d0 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c a previous call
302e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d to sqlite3_comm
302f0 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72 it_hook().** for
30300 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
30310 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 se connection is
30320 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20 overridden..**
30330 54 68 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e The pArg argumen
30340 74 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f t is passed thro
30350 75 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 ugh to the callb
30360 61 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 ack..** If the c
30370 61 6c 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f 6d allback on a com
30380 6d 69 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 6f mit hook functio
30390 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 n returns non-ze
303a0 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 ro,.** then the
303b0 63 6f 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 72 commit is conver
303c0 74 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 ted into a rollb
303d0 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ack..**.** If an
303e0 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 other function w
303f0 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 as previously re
30400 67 69 73 74 65 72 65 64 2c 20 69 74 73 0a 2a 2a gistered, its.**
30410 20 70 41 72 67 20 76 61 6c 75 65 20 69 73 20 72 pArg value is r
30420 65 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 eturned. Otherw
30430 69 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 ise NULL is retu
30440 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 rned..**.** The
30450 63 61 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d 65 callback impleme
30460 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 ntation must not
30470 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 do anything tha
30480 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a t will modify.**
30490 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
304a0 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e nnection that in
304b0 76 6f 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 61 voked the callba
304c0 63 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 ck. Any actions
304d0 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 .** to modify th
304e0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
304f0 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 ction must be de
30500 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 ferred until aft
30510 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 er the.** comple
30520 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c tion of the [sql
30530 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c ite3_step()] cal
30540 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64 l that triggered
30550 20 74 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 6f the commit.** o
30560 72 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 r rollback hook
30570 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 in the first pla
30580 63 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 ce..** Note that
30590 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
305a0 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c e_v2()] and [sql
305b0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 ite3_step()] bot
305c0 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a h modify their.*
305d0 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
305e0 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d ctions for the m
305f0 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 eaning of "modif
30600 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 y" in this parag
30610 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 raph..**.** Regi
30620 73 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66 stering a NULL f
30630 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 unction disables
30640 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a the callback..*
30650 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f *.** When the co
30660 6d 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 mmit hook callba
30670 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 ck routine retur
30680 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 4f ns zero, the [CO
30690 4d 4d 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 69 MMIT].** operati
306a0 6f 6e 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f on is allowed to
306b0 20 63 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 6c continue normal
306c0 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d ly. If the comm
306d0 69 74 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 72 it hook.** retur
306e0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 ns non-zero, the
306f0 6e 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 69 n the [COMMIT] i
30700 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f s converted into
30710 20 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a a [ROLLBACK]..*
30720 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68 * The rollback h
30730 6f 6f 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f ook is invoked o
30740 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 61 n a rollback tha
30750 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 61 t results from a
30760 20 63 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20 commit.** hook
30770 72 65 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65 returning non-ze
30780 72 6f 2c 20 6a 75 73 74 20 61 73 20 69 74 20 77 ro, just as it w
30790 6f 75 6c 64 20 62 65 20 77 69 74 68 20 61 6e 79 ould be with any
307a0 20 6f 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b 2e other rollback.
307b0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 .**.** For the p
307c0 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 urposes of this
307d0 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63 74 69 API, a transacti
307e0 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20 68 61 on is said to ha
307f0 76 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65 ve been.** rolle
30800 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 78 70 d back if an exp
30810 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22 licit "ROLLBACK"
30820 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78 statement is ex
30830 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e ecuted, or.** an
30840 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72 error or constr
30850 61 69 6e 74 20 63 61 75 73 65 73 20 61 6e 20 69 aint causes an i
30860 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b mplicit rollback
30870 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 68 to occur..** Th
30880 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 e rollback callb
30890 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b ack is not invok
308a0 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 ed if a transact
308b0 69 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61 ion is.** automa
308c0 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 tically rolled b
308d0 61 63 6b 20 62 65 63 61 75 73 65 20 74 68 65 20 ack because the
308e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
308f0 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a ion is closed..*
30900 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 * The rollback c
30910 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 allback is not i
30920 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e nvoked if a tran
30930 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 6f saction is.** ro
30940 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73 lled back becaus
30950 65 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c 62 e a commit callb
30960 61 63 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f 6e ack returned non
30970 2d 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f 3e -zero..** <todo>
30980 20 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 3c Check on this <
30990 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 65 /todo>.**.** See
309a0 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 also the [sqlit
309b0 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 e3_update_hook()
309c0 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a ] interface..**.
309d0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
309e0 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31 .** [H12951] [H1
309f0 32 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b 2952] [H12953] [
30a00 48 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d H12954] [H12955]
30a10 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31 .** [H12961] [H1
30a20 32 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b 2962] [H12963] [
30a30 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H12964].*/.SQLIT
30a40 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
30a50 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 te3_commit_hook(
30a60 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 sqlite3*, int(*)
30a70 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b (void*), void*);
30a80 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
30a90 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 *sqlite3_rollba
30aa0 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a ck_hook(sqlite3*
30ab0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a , void(*)(void *
30ac0 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a ), void*);../*.*
30ad0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 * CAPI3REF: Data
30ae0 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61 Change Notifica
30af0 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b tion Callbacks {
30b00 48 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e H12970} <S60400>
30b10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
30b20 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 e3_update_hook()
30b30 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 interface regis
30b40 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 ters a callback
30b50 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68 function.** with
30b60 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
30b70 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74 onnection] ident
30b80 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72 ified by the fir
30b90 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 st argument.** t
30ba0 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 o be invoked whe
30bb0 6e 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75 never a row is u
30bc0 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 pdated, inserted
30bd0 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 or deleted..**
30be0 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 Any callback set
30bf0 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 by a previous c
30c00 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 all to this func
30c10 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20 tion.** for the
30c20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f same database co
30c30 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 nnection is over
30c40 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ridden..**.** Th
30c50 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
30c60 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 t is a pointer t
30c70 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 o the function t
30c80 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a o invoke when a.
30c90 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 ** row is update
30ca0 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 d, inserted or d
30cb0 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66 eleted..** The f
30cc0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
30cd0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 the callback is
30ce0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74 a copy of the t
30cf0 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a hird argument.**
30d00 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61 to sqlite3_upda
30d10 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68 te_hook()..** Th
30d20 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63 e second callbac
30d30 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e k argument is on
30d40 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53 e of [SQLITE_INS
30d50 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45 ERT], [SQLITE_DE
30d60 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 LETE],.** or [SQ
30d70 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65 LITE_UPDATE], de
30d80 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f pending on the o
30d90 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61 peration that ca
30da0 75 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63 used the callbac
30db0 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b k.** to be invok
30dc0 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 ed..** The third
30dd0 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 and fourth argu
30de0 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c ments to the cal
30df0 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f lback contain po
30e00 69 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a inters to the.**
30e10 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61 database and ta
30e20 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e ble name contain
30e30 69 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64 ing the affected
30e40 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e row..** The fin
30e50 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61 al callback para
30e60 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f meter is the [ro
30e70 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e wid] of the row.
30e80 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20 .** In the case
30e90 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68 of an update, th
30ea0 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64 is is the [rowid
30eb0 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 ] after the upda
30ec0 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a te takes place..
30ed0 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 **.** The update
30ee0 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76 hook is not inv
30ef0 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e oked when intern
30f00 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73 al system tables
30f10 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 are.** modified
30f20 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 (i.e. sqlite_ma
30f30 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f ster and sqlite_
30f40 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a sequence)..**.**
30f50 20 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 In the current
30f60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
30f70 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a the update hook.
30f80 2a 2a 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 ** is not invoke
30f90 64 20 77 68 65 6e 20 64 75 70 6c 69 63 61 74 69 d when duplicati
30fa0 6f 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 on rows are dele
30fb0 74 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 61 ted because of a
30fc0 6e 0a 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 n.** [ON CONFLIC
30fd0 54 20 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 T | ON CONFLICT
30fe0 52 45 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 2e REPLACE] clause.
30ff0 20 20 4e 6f 72 20 69 73 20 74 68 65 20 75 70 64 Nor is the upd
31000 61 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 6f ate hook.** invo
31010 6b 65 64 20 77 68 65 6e 20 72 6f 77 73 20 61 72 ked when rows ar
31020 65 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 e deleted using
31030 74 68 65 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 the [truncate op
31040 74 69 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a 20 timization]..**
31050 54 68 65 20 65 78 63 65 70 74 69 6f 6e 73 20 64 The exceptions d
31060 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 70 efined in this p
31070 61 72 61 67 72 61 70 68 20 6d 69 67 68 74 20 63 aragraph might c
31080 68 61 6e 67 65 20 69 6e 20 61 20 66 75 74 75 72 hange in a futur
31090 65 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 20 e.** release of
310a0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 SQLite..**.** Th
310b0 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d e update hook im
310c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73 plementation mus
310d0 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e t not do anythin
310e0 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 g that will modi
310f0 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 fy.** the databa
31100 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 se connection th
31110 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75 at invoked the u
31120 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79 pdate hook. Any
31130 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d actions.** to m
31140 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61 odify the databa
31150 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 se connection mu
31160 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75 st be deferred u
31170 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a ntil after the.*
31180 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 * completion of
31190 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 the [sqlite3_ste
311a0 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 p()] call that t
311b0 72 69 67 67 65 72 65 64 20 74 68 65 20 75 70 64 riggered the upd
311c0 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74 ate hook..** Not
311d0 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f e that [sqlite3_
311e0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e prepare_v2()] an
311f0 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 d [sqlite3_step(
31200 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 )] both modify t
31210 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 heir.** database
31220 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 connections for
31230 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 the meaning of
31240 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 "modify" in this
31250 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a paragraph..**.*
31260 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e * If another fun
31270 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f ction was previo
31280 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c usly registered,
31290 20 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a its pArg value.
312a0 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 ** is returned.
312b0 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 Otherwise NULL
312c0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
312d0 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 ** See also the
312e0 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f [sqlite3_commit_
312f0 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 6c hook()] and [sql
31300 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f ite3_rollback_ho
31310 6f 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 ok()].** interfa
31320 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ces..**.** Requi
31330 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
31340 39 37 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48 971] [H12973] [H
31350 31 32 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20 12975] [H12977]
31360 5b 48 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31 [H12979] [H12981
31370 5d 20 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39 ] [H12983] [H129
31380 38 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 86].*/.SQLITE_AP
31390 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f I void *sqlite3_
313a0 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73 update_hook(. s
313b0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 qlite3*, . void
313c0 28 2a 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c (*)(void *,int ,
313d0 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 char const *,cha
313e0 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 r const *,sqlite
313f0 33 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 3_int64),. void
31400 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 *.);../*.** CAPI
31410 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 3REF: Enable Or
31420 44 69 73 61 62 6c 65 20 53 68 61 72 65 64 20 50 Disable Shared P
31430 61 67 65 72 20 43 61 63 68 65 20 7b 48 31 30 33 ager Cache {H103
31440 33 30 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20 30} <S30900>.**
31450 4b 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65 KEYWORDS: {share
31460 64 20 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 54 d cache}.**.** T
31470 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 his routine enab
31480 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 les or disables
31490 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74 the sharing of t
314a0 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68 he database cach
314b0 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20 e.** and schema
314c0 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 data structures
314d0 62 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73 between [databas
314e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63 e connection | c
314f0 6f 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74 onnections].** t
31500 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 o the same datab
31510 61 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20 ase. Sharing is
31520 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 enabled if the a
31530 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a rgument is true.
31540 2a 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 ** and disabled
31550 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 if the argument
31560 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 is false..**.**
31570 43 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 73 Cache sharing is
31580 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73 enabled and dis
31590 61 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74 abled for an ent
315a0 69 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20 ire process..**
315b0 54 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 This is a change
315c0 20 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 as of SQLite ve
315d0 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 rsion 3.5.0. In
315e0 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f prior versions o
315f0 66 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 f SQLite,.** sha
31600 72 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 ring was enabled
31610 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 or disabled for
31620 20 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70 each thread sep
31630 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 arately..**.** T
31640 68 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 he cache sharing
31650 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 mode set by thi
31660 73 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 s interface effe
31670 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 cts all subseque
31680 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b nt.** calls to [
31690 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c sqlite3_open()],
316a0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 [sqlite3_open_v
316b0 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 2()], and [sqlit
316c0 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a e3_open16()]..**
316d0 20 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 Existing databa
316e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 se connections c
316f0 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 ontinue use the
31700 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 sharing mode.**
31710 74 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 that was in effe
31720 63 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 ct at the time t
31730 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e hey were opened.
31740 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 .**.** Virtual t
31750 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 ables cannot be
31760 75 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72 used with a shar
31770 65 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 ed cache. When
31780 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 shared.** cache
31790 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 is enabled, the
317a0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
317b0 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 module()] API us
317c0 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a ed to register.*
317d0 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 * virtual tables
317e0 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 will always ret
317f0 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a urn an error..**
31800 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
31810 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
31820 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 _OK] if shared c
31830 61 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 ache was enabled
31840 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 or disabled.**
31850 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 successfully. A
31860 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 n [error code] i
31870 73 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 s returned other
31880 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 wise..**.** Shar
31890 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 ed cache is disa
318a0 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e bled by default.
318b0 20 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 But this might
318c0 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 change in.** fut
318d0 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 ure releases of
318e0 53 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 SQLite. Applica
318f0 74 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 tions that care
31900 61 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 about shared.**
31910 63 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 cache setting sh
31920 6f 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c ould set it expl
31930 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65 icitly..**.** Se
31940 65 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65 e Also: [SQLite
31950 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f Shared-Cache Mo
31960 64 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 de].**.** Requir
31970 65 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d ements: [H10331]
31980 20 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33 [H10336] [H1033
31990 37 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53 7] [H10339].*/.S
319a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
319b0 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 lite3_enable_sha
319c0 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a red_cache(int);.
319d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
319e0 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65 Attempt To Free
319f0 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31 Heap Memory {H1
31a00 37 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 7340} <S30220>.*
31a10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
31a20 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
31a30 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 ) interface atte
31a40 6d 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62 mpts to free N b
31a50 79 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20 ytes.** of heap
31a60 6d 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f memory by deallo
31a70 63 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e cating non-essen
31a80 74 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f tial memory allo
31a90 63 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20 cations.** held
31aa0 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 by the database
31ab0 6c 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20 library. {END}
31ac0 4d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63 Memory used to c
31ad0 61 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a ache database.**
31ae0 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76 pages to improv
31af0 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 e performance is
31b00 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e an example of n
31b10 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d on-essential mem
31b20 6f 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ory..** sqlite3_
31b30 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
31b40 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
31b50 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 ber of bytes act
31b60 75 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20 ually freed,.**
31b70 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d which might be m
31b80 6f 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e ore or less than
31b90 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75 the amount requ
31ba0 65 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 ested..**.** Req
31bb0 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33 uirements: [H173
31bc0 34 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a 41] [H17342].*/.
31bd0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
31be0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
31bf0 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a emory(int);../*.
31c00 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70 ** CAPI3REF: Imp
31c10 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48 ose A Limit On H
31c20 65 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30 eap Size {H17350
31c30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a } <S30220>.**.**
31c40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66 The sqlite3_sof
31c50 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69 t_heap_limit() i
31c60 6e 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 20 nterface places
31c70 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a a "soft" limit.*
31c80 2a 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 * on the amount
31c90 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74 of heap memory t
31ca0 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 hat may be alloc
31cb0 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a ated by SQLite..
31cc0 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61 ** If an interna
31cd0 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 l allocation is
31ce0 72 65 71 75 65 73 74 65 64 20 74 68 61 74 20 77 requested that w
31cf0 6f 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a ould exceed the.
31d00 2a 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d ** soft heap lim
31d10 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c it, [sqlite3_rel
31d20 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 ease_memory()] i
31d30 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72 s invoked one or
31d40 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 .** more times t
31d50 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73 o free up some s
31d60 70 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 20 pace before the
31d70 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65 allocation is pe
31d80 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 rformed..**.** T
31d90 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c he limit is call
31da0 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75 ed "soft", becau
31db0 73 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72 se if [sqlite3_r
31dc0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d elease_memory()]
31dd0 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 .** cannot free
31de0 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72 sufficient memor
31df0 79 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 y to prevent the
31e00 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e limit from bein
31e10 67 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74 g exceeded,.** t
31e20 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c he memory is all
31e30 6f 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e ocated anyway an
31e40 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 d the current op
31e50 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 eration proceeds
31e60 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69 ..**.** A negati
31e70 76 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65 ve or zero value
31e80 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61 for N means tha
31e90 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f t there is no so
31ea0 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e ft heap limit an
31eb0 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 d.** [sqlite3_re
31ec0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 lease_memory()]
31ed0 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c will only be cal
31ee0 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 led when memory
31ef0 69 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a is exhausted..**
31f00 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c The default val
31f10 75 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 ue for the soft
31f20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65 heap limit is ze
31f30 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 ro..**.** SQLite
31f40 20 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 66 makes a best ef
31f50 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68 fort to honor th
31f60 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 e soft heap limi
31f70 74 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65 t..** But if the
31f80 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
31f90 20 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72 cannot be honor
31fa0 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69 ed, execution wi
31fb0 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77 ll.** continue w
31fc0 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20 ithout error or
31fd0 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54 notification. T
31fe0 68 69 73 20 69 73 20 77 68 79 20 74 68 65 20 6c his is why the l
31ff0 69 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 imit is.** calle
32000 64 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 d a "soft" limit
32010 2e 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72 . It is advisor
32020 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72 y only..**.** Pr
32030 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65 ior to SQLite ve
32040 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69 rsion 3.5.0, thi
32050 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 s routine only c
32060 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d onstrained the m
32070 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 emory.** allocat
32080 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 ed by a single t
32090 68 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65 hread - the same
320a0 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 thread in which
320b0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
320c0 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e runs. Beginnin
320d0 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65 g with SQLite ve
320e0 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65 rsion 3.5.0, the
320f0 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
32100 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74 is.** applied t
32110 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54 o all threads. T
32120 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 he value specifi
32130 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20 ed for the soft
32140 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73 heap limit.** is
32150 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 an upper bound
32160 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d on the total mem
32170 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
32180 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 or all threads.
32190 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e In.** version 3.
321a0 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20 5.0 there is no
321b0 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69 mechanism for li
321c0 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 20 miting the heap
321d0 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64 usage for.** ind
321e0 69 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e ividual threads.
321f0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
32200 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d nts:.** [H16351]
32210 20 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35 [H16352] [H1635
32220 33 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36 3] [H16354] [H16
32230 33 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f 355] [H16358].*/
32240 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
32250 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 sqlite3_soft_he
32260 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a ap_limit(int);..
32270 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
32280 45 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 Extract Metadata
32290 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 About A Column
322a0 4f 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 Of A Table {H128
322b0 35 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 50} <S60300>.**.
322c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
322d0 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 returns metadata
322e0 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 about a specifi
322f0 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 c column of a sp
32300 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 ecific.** databa
32310 73 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 se table accessi
32320 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 ble using the [d
32330 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
32340 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 on] handle.** pa
32350 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 ssed as the firs
32360 74 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d t function argum
32370 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ent..**.** The c
32380 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 olumn is identif
32390 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e ied by the secon
323a0 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 d, third and fou
323b0 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 rth parameters t
323c0 6f 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 o.** this functi
323d0 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 on. The second p
323e0 61 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 arameter is eith
323f0 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 er the name of t
32400 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 he database.** (
32410 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 i.e. "main", "te
32420 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 mp" or an attach
32430 65 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e ed database) con
32440 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 taining the spec
32450 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f ified.** table o
32460 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 r NULL. If it is
32470 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 NULL, then all
32480 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
32490 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a es are searched.
324a0 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 ** for the table
324b0 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 using the same
324c0 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 algorithm used b
324d0 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 y the database e
324e0 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f ngine to.** reso
324f0 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 lve unqualified
32500 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 table references
32510 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 ..**.** The thir
32520 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 d and fourth par
32530 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 ameters to this
32540 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 function are the
32550 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d table and colum
32560 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 n.** name of the
32570 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c desired column,
32580 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e respectively. N
32590 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 either of these
325a0 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 parameters.** ma
325b0 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a y be NULL..**.**
325c0 20 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 Metadata is ret
325d0 75 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 urned by writing
325e0 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c to the memory l
325f0 6f 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 ocations passed
32600 61 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e as the 5th.** an
32610 64 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 d subsequent par
32620 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 ameters to this
32630 66 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 function. Any of
32640 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 these arguments
32650 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c may be.** NULL,
32660 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 in which case t
32670 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
32680 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 element of meta
32690 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e data is omitted.
326a0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f .**.** <blockquo
326b0 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f te>.** <table bo
326c0 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 rder="1">.** <tr
326d0 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 ><th> Parameter
326e0 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 <th> Output<br>T
326f0 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 ype <th> Descri
32700 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e ption.**.** <tr>
32710 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f <td> 5th <td> co
32720 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 nst char* <td> D
32730 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e ata type.** <tr>
32740 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f <td> 6th <td> co
32750 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e nst char* <td> N
32760 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 ame of default c
32770 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
32780 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 e.** <tr><td> 7t
32790 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 h <td> int
327a0 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 <td> True if
327b0 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 column has a NOT
327c0 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 NULL constraint
327d0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 .** <tr><td> 8th
327e0 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 <td> int
327f0 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 <td> True if c
32800 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 olumn is part of
32810 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 the PRIMARY KEY
32820 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 .** <tr><td> 9th
32830 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 <td> int
32840 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 <td> True if c
32850 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e olumn is [AUTOIN
32860 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61 CREMENT].** </ta
32870 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 ble>.** </blockq
32880 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 uote>.**.** The
32890 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 memory pointed t
328a0 6f 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74 o by the charact
328b0 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 er pointers retu
328c0 72 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 rned for the.**
328d0 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 declaration type
328e0 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 and collation s
328f0 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64 equence is valid
32900 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 only until the
32910 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 next.** call to
32920 61 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 66 any SQLite API f
32930 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 unction..**.** I
32940 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 f the specified
32950 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c table is actuall
32960 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 y a view, an [er
32970 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
32980 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
32990 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f the specified co
329a0 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c lumn is "rowid",
329b0 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 "oid" or "_rowi
329c0 64 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49 d_" and an.** [I
329d0 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
329e0 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62 EY] column has b
329f0 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64 een explicitly d
32a00 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68 eclared, then th
32a10 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 e output.** para
32a20 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66 meters are set f
32a30 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c or the explicitl
32a40 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d y declared colum
32a50 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e n. If there is n
32a60 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 o.** explicitly
32a70 64 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45 declared [INTEGE
32a80 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 R PRIMARY KEY] c
32a90 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20 olumn, then the
32aa0 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 output.** parame
32ab0 74 65 72 73 20 61 72 65 20 73 65 74 20 61 73 20 ters are set as
32ac0 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c follows:.**.** <
32ad0 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61 pre>.** data
32ae0 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22 type: "INTEGER"
32af0 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f .** collatio
32b00 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e n sequence: "BIN
32b10 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20 ARY".** not
32b20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70 null: 0.** p
32b30 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a rimary key: 1.**
32b40 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d auto increm
32b50 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e ent: 0.** </pre>
32b60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
32b70 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e tion may load on
32b80 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61 e or more schema
32b90 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 s from database
32ba0 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20 files. If an.**
32bb0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
32bc0 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73 ing this process
32bd0 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75 , or if the requ
32be0 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63 ested table or c
32bf0 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 olumn.** cannot
32c00 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72 be found, an [er
32c10 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 ror code] is ret
32c20 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 urned and an err
32c30 6f 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a or message left.
32c40 2a 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62 ** in the [datab
32c50 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
32c60 28 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 (to be retrieved
32c70 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65 using sqlite3_e
32c80 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 rrmsg())..**.**
32c90 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 This API is only
32ca0 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 available if th
32cb0 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f e library was co
32cc0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a mpiled with the.
32cd0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c ** [SQLITE_ENABL
32ce0 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 E_COLUMN_METADAT
32cf0 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f A] C-preprocesso
32d00 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 r symbol defined
32d10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
32d20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c int sqlite3_tabl
32d30 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 e_column_metadat
32d40 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 a(. sqlite3 *db
32d50 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
32d60 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 /* Connection h
32d70 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
32d80 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 char *zDbName,
32d90 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
32da0 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 se name or NULL
32db0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
32dc0 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 *zTableName,
32dd0 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a /* Table name *
32de0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
32df0 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 zColumnName,
32e00 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a /* Column name *
32e10 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a /. char const *
32e20 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 *pzDataType,
32e30 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 /* OUTPUT: Decla
32e40 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f red data type */
32e50 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a . char const **
32e60 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f pzCollSeq, /
32e70 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 * OUTPUT: Collat
32e80 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d ion sequence nam
32e90 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 e */. int *pNot
32ea0 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Null,
32eb0 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 /* OUTPUT: Tr
32ec0 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 ue if NOT NULL c
32ed0 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 onstraint exists
32ee0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d */. int *pPrim
32ef0 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 aryKey,
32f00 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 /* OUTPUT: Tru
32f10 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 e if column part
32f20 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 of PK */. int
32f30 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 *pAutoinc
32f40 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 /* OUTPU
32f50 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d T: True if colum
32f60 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d n is auto-increm
32f70 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ent */.);../*.**
32f80 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20 CAPI3REF: Load
32f90 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31 An Extension {H1
32fa0 32 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2600} <S20500>.*
32fb0 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
32fc0 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c ace loads an SQL
32fd0 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 ite extension li
32fe0 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e brary from the n
32ff0 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a amed file..**.**
33000 20 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 71 {H12601} The sq
33010 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e lite3_load_exten
33020 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 sion() interface
33030 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61 attempts to loa
33040 64 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 d an.**
33050 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f SQLite extensio
33060 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 n library contai
33070 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 ned in the file
33080 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 zFile..**.** {H1
33090 32 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20 2602} The entry
330a0 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a point is zProc..
330b0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a **.** {H12603} z
330c0 50 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69 Proc may be 0, i
330d0 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
330e0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74 name of the ent
330f0 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20 ry point.**
33100 20 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f defaults to
33110 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 "sqlite3_extens
33120 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a ion_init"..**.**
33130 20 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 71 {H12604} The sq
33140 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e lite3_load_exten
33150 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 sion() interface
33160 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a shall return.**
33170 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 [SQLIT
33180 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 E_OK] on success
33190 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 and [SQLITE_ERR
331a0 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 OR] if something
331b0 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a goes wrong..**.
331c0 2a 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61 ** {H12605} If a
331d0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 n error occurs a
331e0 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e nd pzErrMsg is n
331f0 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a ot 0, then the.*
33200 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 * [sqli
33210 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 te3_load_extensi
33220 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 on()] interface
33230 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f shall attempt to
33240 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c .** fil
33250 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68 l *pzErrMsg with
33260 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
33270 65 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 ext stored in me
33280 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 mory.**
33290 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
332a0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
332b0 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61 ]. {END} The ca
332c0 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a lling function.*
332d0 2a 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c * shoul
332e0 64 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f d free this memo
332f0 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 ry by calling [s
33300 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a qlite3_free()]..
33310 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45 **.** {H12606} E
33320 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 xtension loading
33330 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64 must be enabled
33340 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 using.**
33350 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 [sqlite3_enab
33360 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f le_load_extensio
33370 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61 n()] prior to ca
33380 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a lling this API,.
33390 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 65 ** othe
333a0 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77 rwise an error w
333b0 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e ill be returned.
333c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
333d0 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f nt sqlite3_load_
333e0 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c extension(. sql
333f0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
33400 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 /* Load the e
33410 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68 xtension into th
33420 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e is database conn
33430 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 ection */. cons
33440 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20 t char *zFile,
33450 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
33460 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 shared library
33470 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e containing exten
33480 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 sion */. const
33490 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20 char *zProc,
334a0 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20 /* Entry point.
334b0 20 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46 Derived from zF
334c0 69 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 ile if 0 */. ch
334d0 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 ar **pzErrMsg
334e0 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 /* Put error
334f0 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66 message here if
33500 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a not 0 */.);../*
33510 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e .** CAPI3REF: En
33520 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 able Or Disable
33530 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e Extension Loadin
33540 67 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35 g {H12620} <S205
33550 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 00>.**.** So as
33560 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 not to open secu
33570 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c rity holes in ol
33580 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 der applications
33590 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70 that are.** unp
335a0 72 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20 repared to deal
335b0 77 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c with extension l
335c0 6f 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61 oading, and as a
335d0 20 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c means of disabl
335e0 69 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e ing.** extension
335f0 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65 loading while e
33600 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65 valuating user-e
33610 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20 ntered SQL, the
33620 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a following API.**
33630 20 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20 is provided to
33640 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 turn the [sqlite
33650 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 3_load_extension
33660 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e ()] mechanism on
33670 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 and off..**.**
33680 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e Extension loadin
33690 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 g is off by defa
336a0 75 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20 ult. See ticket
336b0 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 #1863..**.** {H1
336c0 32 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73 2621} Call the s
336d0 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f qlite3_enable_lo
336e0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72 ad_extension() r
336f0 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66 outine with onof
33700 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 f==1.**
33710 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69 to turn extensi
33720 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e on loading on an
33730 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f d call it with o
33740 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a noff==0 to turn.
33750 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 62 ** it b
33760 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a ack off again..*
33770 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78 *.** {H12622} Ex
33780 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 tension loading
33790 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c is off by defaul
337a0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
337b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 int sqlite3_ena
337c0 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 ble_load_extensi
337d0 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 on(sqlite3 *db,
337e0 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a int onoff);../*.
337f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 ** CAPI3REF: Aut
33800 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20 omatically Load
33810 41 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48 An Extensions {H
33820 31 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a 12640} <S20500>.
33830 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63 **.** This API c
33840 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74 an be invoked at
33850 20 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70 program startup
33860 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67 in order to reg
33870 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 ister.** one or
33880 6d 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20 more statically
33890 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e linked extension
338a0 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61 s that will be a
338b0 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 vailable.** to a
338c0 6c 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 ll new [database
338d0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b connections]. {
338e0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 END}.**.** This
338f0 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 routine stores a
33900 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
33910 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 extension in an
33920 61 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a array that is.**
33930 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b obtained from [
33940 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
33950 5d 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61 ]. If you run a
33960 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65 memory leak che
33970 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 cker.** on your
33980 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72 program and it r
33990 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65 eports a leak be
339a0 63 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 72 cause of this ar
339b0 72 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b ray, invoke.** [
339c0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 sqlite3_reset_au
339d0 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 to_extension()]
339e0 70 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77 prior to shutdow
339f0 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65 n to free the me
33a00 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 mory..**.** {H12
33a10 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69 641} This functi
33a20 6f 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e 20 on registers an
33a30 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20 extension entry
33a40 70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a point that is.**
33a50 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61 automa
33a60 74 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 tically invoked
33a70 77 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b whenever a new [
33a80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
33a90 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 ion].**
33aa0 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 is opened using
33ab0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 [sqlite3_open()
33ac0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e ], [sqlite3_open
33ad0 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 16()],.**
33ae0 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f or [sqlite3_o
33af0 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a pen_v2()]..**.**
33b00 20 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63 {H12642} Duplic
33b10 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 ate extensions a
33b20 72 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 63 re detected so c
33b30 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
33b40 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ine.**
33b50 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 multiple times w
33b60 69 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74 ith the same ext
33b70 65 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65 ension is harmle
33b80 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 ss..**.** {H1264
33b90 33 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 3} This routine
33ba0 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 stores a pointer
33bb0 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f to the extensio
33bc0 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a n in an array.**
33bd0 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69 that i
33be0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 s obtained from
33bf0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
33c00 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 )]..**.** {H1264
33c10 34 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74 4} Automatic ext
33c20 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63 ensions apply ac
33c30 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73 ross all threads
33c40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
33c50 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f int sqlite3_auto
33c60 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20 _extension(void
33c70 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76 (*xEntryPoint)(v
33c80 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid));../*.** CA
33c90 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75 PI3REF: Reset Au
33ca0 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f tomatic Extensio
33cb0 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36 n Loading {H1266
33cc0 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 0} <S20500>.**.*
33cd0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
33ce0 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 disables all pre
33cf0 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 viously register
33d00 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 ed automatic.**
33d10 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 extensions. {END
33d20 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65 } It undoes the
33d30 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70 effect of all p
33d40 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 rior.** [sqlite3
33d50 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 _auto_extension(
33d60 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 )] calls..**.**
33d70 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75 {H12661} This fu
33d80 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 nction disables
33d90 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 all previously r
33da0 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20 egistered.**
33db0 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20 automatic
33dc0 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a extensions..**.*
33dd0 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20 * {H12662} This
33de0 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 function disable
33df0 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 s automatic exte
33e00 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68 nsions in all th
33e10 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 reads..*/.SQLITE
33e20 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
33e30 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 3_reset_auto_ext
33e40 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f ension(void);../
33e50 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d *.****** EXPERIM
33e60 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 ENTAL - subject
33e70 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 to change withou
33e80 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a t notice *******
33e90 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
33ea0 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 e interface to t
33eb0 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
33ec0 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75 mechanism is cu
33ed0 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 rrently consider
33ee0 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 ed.** to be expe
33ef0 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 rimental. The i
33f00 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 nterface might c
33f10 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 hange in incompa
33f20 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 tible ways..** I
33f30 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 f this is a prob
33f40 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 lem for you, do
33f50 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 not use the inte
33f60 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69 rface at this ti
33f70 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 me..**.** When t
33f80 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 he virtual-table
33f90 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 mechanism stabi
33fa0 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 lizes, we will d
33fb0 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e eclare the.** in
33fc0 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 terface fixed, s
33fd0 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 upport it indefi
33fe0 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f nitely, and remo
33ff0 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e ve this comment.
34000 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 .*/../*.** Struc
34010 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 tures used by th
34020 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
34030 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70 interface.*/.typ
34040 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
34050 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33 te3_vtab sqlite3
34060 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73 _vtab;.typedef s
34070 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
34080 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33 dex_info sqlite3
34090 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 _index_info;.typ
340a0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
340b0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
340c0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
340d0 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 sor;.typedef str
340e0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 uct sqlite3_modu
340f0 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c le sqlite3_modul
34100 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 e;../*.** CAPI3R
34110 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c EF: Virtual Tabl
34120 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30 e Object {H18000
34130 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 } <S20400>.** KE
34140 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f YWORDS: sqlite3_
34150 6d 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20 module {virtual
34160 74 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a table module}.**
34170 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
34180 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 .** This structu
34190 72 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 re, sometimes ca
341a0 6c 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 61 lled a a "virtua
341b0 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c l table module",
341c0 20 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 .** defines the
341d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
341e0 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61 of a [virtual ta
341f0 62 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 bles]. .** This
34200 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69 structure consi
34210 73 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 sts mostly of me
34220 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f thods for the mo
34230 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 dule..**.** A vi
34240 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 rtual table modu
34250 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 62 79 le is created by
34260 20 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65 filling in a pe
34270 72 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 rsistent.** inst
34280 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
34290 75 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 69 ucture and passi
342a0 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ng a pointer to
342b0 74 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a that instance.**
342c0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 to [sqlite3_cre
342d0 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 ate_module()] or
342e0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
342f0 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a _module_v2()]..*
34300 2a 20 54 68 65 20 72 65 67 69 73 74 72 61 74 69 * The registrati
34310 6f 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 on remains valid
34320 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70 until it is rep
34330 6c 61 63 65 64 20 62 79 20 61 20 64 69 66 66 65 laced by a diffe
34340 72 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f rent.** module o
34350 72 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74 r until the [dat
34360 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
34370 5d 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63 ] closes. The c
34380 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 ontent.** of thi
34390 73 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 74 s structure must
343a0 20 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c not change whil
343b0 65 20 69 74 20 69 73 20 72 65 67 69 73 74 65 72 e it is register
343c0 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64 ed with.** any d
343d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
343e0 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 on..*/.struct sq
343f0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 lite3_module {.
34400 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 int iVersion;.
34410 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 int (*xCreate)(
34420 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a sqlite3*, void *
34430 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 pAux,.
34440 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 int argc, c
34450 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 onst char *const
34460 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 *argv,.
34470 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
34480 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 ab **ppVTab, cha
34490 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 r**);. int (*xC
344a0 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a onnect)(sqlite3*
344b0 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 , void *pAux,.
344c0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
344d0 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 argc, const cha
344e0 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 r *const*argv,.
344f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
34500 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 lite3_vtab **ppV
34510 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 Tab, char**);.
34520 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 int (*xBestIndex
34530 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
34540 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 pVTab, sqlite3_i
34550 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 ndex_info*);. i
34560 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 nt (*xDisconnect
34570 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
34580 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a pVTab);. int (*
34590 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 xDestroy)(sqlite
345a0 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 3_vtab *pVTab);.
345b0 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 int (*xOpen)(s
345c0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 qlite3_vtab *pVT
345d0 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 ab, sqlite3_vtab
345e0 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 _cursor **ppCurs
345f0 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c or);. int (*xCl
34600 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 ose)(sqlite3_vta
34610 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e b_cursor*);. in
34620 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c t (*xFilter)(sql
34630 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
34640 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 *, int idxNum, c
34650 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 onst char *idxSt
34660 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 r,.
34670 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c int argc, sql
34680 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
34690 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 v);. int (*xNex
346a0 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f t)(sqlite3_vtab_
346b0 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 cursor*);. int
346c0 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f (*xEof)(sqlite3_
346d0 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 vtab_cursor*);.
346e0 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 int (*xColumn)(
346f0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
34700 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f sor*, sqlite3_co
34710 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 ntext*, int);.
34720 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 int (*xRowid)(sq
34730 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
34740 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 r*, sqlite3_int6
34750 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 4 *pRowid);. in
34760 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c t (*xUpdate)(sql
34770 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 ite3_vtab *, int
34780 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
34790 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 **, sqlite3_int6
347a0 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 4 *);. int (*xB
347b0 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 egin)(sqlite3_vt
347c0 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e ab *pVTab);. in
347d0 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 t (*xSync)(sqlit
347e0 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b e3_vtab *pVTab);
347f0 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 . int (*xCommit
34800 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a )(sqlite3_vtab *
34810 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a pVTab);. int (*
34820 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 xRollback)(sqlit
34830 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b e3_vtab *pVTab);
34840 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 . int (*xFindFu
34850 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f nction)(sqlite3_
34860 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 vtab *pVtab, int
34870 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 nArg, const cha
34880 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 r *zName,.
34890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
348a0 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 void (**pxFunc)
348b0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
348c0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 *,int,sqlite3_va
348d0 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 lue**),.
348e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 v
348f0 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 oid **ppArg);.
34900 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 int (*xRename)(s
34910 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
34920 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ab, const char *
34930 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a zNew);.};../*.**
34940 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 CAPI3REF: Virtu
34950 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e al Table Indexin
34960 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 g Information {H
34970 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18100} <S20400>.
34980 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c ** KEYWORDS: sql
34990 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a ite3_index_info.
349a0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
349b0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
349c0 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 3_index_info str
349d0 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 ucture and its s
349e0 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 ubstructures is
349f0 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 used to.** pass
34a00 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f information into
34a10 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 and receive the
34a20 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 reply from the
34a30 5b 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 [xBestIndex].**
34a40 6d 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72 method of a [vir
34a50 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c tual table modul
34a60 65 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20 e]. The fields
34a70 75 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a under **Inputs**
34a80 20 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75 are the.** inpu
34a90 74 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 ts to xBestIndex
34aa0 20 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e and are read-on
34ab0 6c 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20 ly. xBestIndex
34ac0 69 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72 inserts its.** r
34ad0 65 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20 esults into the
34ae0 2a 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c **Outputs** fiel
34af0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 ds..**.** The aC
34b00 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 onstraint[] arra
34b10 79 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 20 y records WHERE
34b20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e clause constrain
34b30 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a ts of the form:.
34b40 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d **.** <pre>colum
34b50 6e 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a n OP expr</pre>.
34b60 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69 **.** where OP i
34b70 73 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d s =, <, <=
34b80 2c 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d , >, or >=
34b90 2e 20 20 54 68 65 20 70 61 72 74 69 63 75 6c 61 . The particula
34ba0 72 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a r operator is.**
34bb0 20 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73 stored in aCons
34bc0 74 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 traint[].op. Th
34bd0 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 e index of the c
34be0 6f 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20 olumn is stored
34bf0 69 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e in.** aConstrain
34c00 74 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 t[].iColumn. aC
34c10 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 onstraint[].usab
34c20 6c 65 20 69 73 20 54 52 55 45 20 69 66 20 74 68 le is TRUE if th
34c30 65 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65 e.** expr on the
34c40 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 right-hand side
34c50 20 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65 can be evaluate
34c60 64 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 20 d (and thus the
34c70 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73 constraint.** is
34c80 20 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c usable) and fal
34c90 73 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e se if it cannot.
34ca0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d .**.** The optim
34cb0 69 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c izer automatical
34cc0 6c 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73 ly inverts terms
34cd0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78 of the form "ex
34ce0 70 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a pr OP column".**
34cf0 20 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72 and makes other
34d00 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73 simplifications
34d10 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c to the WHERE cl
34d20 61 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d ause in an attem
34d30 70 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20 pt to.** get as
34d40 6d 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73 many WHERE claus
34d50 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65 e terms into the
34d60 20 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76 form shown abov
34d70 65 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a e as possible..*
34d80 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e * The aConstrain
34d90 74 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72 t[] array only r
34da0 65 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61 eports WHERE cla
34db0 75 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 use terms in the
34dc0 20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d correct.** form
34dd0 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 that refer to t
34de0 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 69 he particular vi
34df0 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e rtual table bein
34e00 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a g queried..**.**
34e10 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
34e20 75 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 ut the ORDER BY
34e30 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 64 clause is stored
34e40 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a in aOrderBy[]..
34e50 2a 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20 ** Each term of
34e60 61 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73 aOrderBy records
34e70 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 a column of the
34e80 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
34e90 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65 ..**.** The [xBe
34ea0 73 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20 stIndex] method
34eb0 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74 must fill aConst
34ec0 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 raintUsage[] wit
34ed0 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a h information.**
34ee0 20 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61 about what para
34ef0 6d 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74 meters to pass t
34f00 6f 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61 o xFilter. If a
34f10 72 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a rgvIndex>0 then.
34f20 2a 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e ** the right-han
34f30 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f d side of the co
34f40 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e rresponding aCon
34f50 73 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61 straint[] is eva
34f60 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65 luated.** and be
34f70 63 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e comes the argvIn
34f80 64 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20 dex-th entry in
34f90 61 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74 argv. If aConst
34fa0 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 raintUsage[].omi
34fb0 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 t.** is true, th
34fc0 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e en the constrain
34fd0 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 t is assumed to
34fe0 62 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64 be fully handled
34ff0 20 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 by the.** virtu
35000 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20 al table and is
35010 6e 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69 not checked agai
35020 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a n by SQLite..**.
35030 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e ** The idxNum an
35040 64 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20 d idxPtr values
35050 61 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64 are recorded and
35060 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 passed into the
35070 0a 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65 .** [xFilter] me
35080 74 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 thod..** [sqlite
35090 33 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 65 3_free()] is use
350a0 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 72 d to free idxPtr
350b0 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66 if and only iff
350c0 0a 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64 .** needToFreeId
350d0 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a xPtr is true..**
350e0 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43 .** The orderByC
350f0 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68 onsumed means th
35100 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b at output from [
35110 78 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d xFilter]/[xNext]
35120 20 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a will occur in.*
35130 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 * the correct or
35140 64 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 74 der to satisfy t
35150 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
35160 73 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65 se so that no se
35170 70 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e parate.** sortin
35180 67 20 73 74 65 70 20 69 73 20 72 65 71 75 69 72 g step is requir
35190 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 ed..**.** The es
351a0 74 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75 timatedCost valu
351b0 65 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 e is an estimate
351c0 20 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20 of the cost of
351d0 64 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72 doing the.** par
351e0 74 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 ticular lookup.
351f0 20 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20 A full scan of
35200 61 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65 a table with N e
35210 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61 ntries should ha
35220 76 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20 ve.** a cost of
35230 4e 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 61 N. A binary sea
35240 72 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f rch of a table o
35250 66 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75 f N entries shou
35260 6c 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73 ld have a.** cos
35270 74 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65 t of approximate
35280 6c 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 ly log(N)..*/.st
35290 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
352a0 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 ex_info {. /* I
352b0 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e nputs */. int n
352c0 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 Constraint;
352d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
352e0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 of entries in aC
352f0 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73 onstraint */. s
35300 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
35310 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b dex_constraint {
35320 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d . int iColum
35330 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
35340 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 /* Column on lef
35350 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63 t-hand side of c
35360 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20 onstraint */.
35370 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
35380 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 op; /* C
35390 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74 onstraint operat
353a0 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 or */. unsig
353b0 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b ned char usable;
353c0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
353d0 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 this constraint
353e0 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 is usable */.
353f0 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65 int iTermOffse
35400 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 t; /* U
35410 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d sed internally -
35420 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75 xBestIndex shou
35430 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d ld ignore */. }
35440 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 *aConstraint;
35450 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 /* Tab
35460 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75 le of WHERE clau
35470 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a se constraints *
35480 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 /. int nOrderBy
35490 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
354a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d * Number of term
354b0 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 s in the ORDER B
354c0 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 Y clause */. st
354d0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
354e0 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20 ex_orderby {.
354f0 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 int iColumn;
35500 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
35510 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a olumn number */.
35520 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 unsigned ch
35530 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f ar desc; /
35540 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e * True for DESC.
35550 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e False for ASC.
35560 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42 */. } *aOrderB
35570 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
35580 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 /* The ORDER BY
35590 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 clause */. /*
355a0 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72 Outputs */. str
355b0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 uct sqlite3_inde
355c0 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 x_constraint_usa
355d0 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67 ge {. int arg
355e0 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20 vIndex;
355f0 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73 /* if >0, cons
35600 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f traint is part o
35610 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65 f argv to xFilte
35620 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 r */. unsigne
35630 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20 d char omit;
35640 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65 /* Do not code
35650 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73 a test for this
35660 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 constraint */.
35670 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55 } *aConstraintU
35680 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e sage;. int idxN
35690 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 um;
356a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65 /* Number use
356b0 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68 d to identify th
356c0 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61 e index */. cha
356d0 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20 r *idxStr;
356e0 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e /* Strin
356f0 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61 g, possibly obta
35700 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
35710 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 3_malloc */. in
35720 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 t needToFreeIdxS
35730 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65 tr; /* Free
35740 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71 idxStr using sq
35750 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20 lite3_free() if
35760 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 true */. int or
35770 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20 derByConsumed;
35780 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
35790 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64 output is alread
357a0 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64 y ordered */. d
357b0 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43 ouble estimatedC
357c0 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74 ost; /* Est
357d0 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75 imated cost of u
357e0 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 sing this index
357f0 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51 */.};.#define SQ
35800 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 LITE_INDEX_CONST
35810 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64 RAINT_EQ 2.#d
35820 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 efine SQLITE_IND
35830 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 EX_CONSTRAINT_GT
35840 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4.#define SQ
35850 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 LITE_INDEX_CONST
35860 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64 RAINT_LE 8.#d
35870 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 efine SQLITE_IND
35880 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 EX_CONSTRAINT_LT
35890 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53 16.#define S
358a0 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 QLITE_INDEX_CONS
358b0 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a TRAINT_GE 32.
358c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
358d0 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f NDEX_CONSTRAINT_
358e0 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 MATCH 64../*.**
358f0 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 CAPI3REF: Regist
35900 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 er A Virtual Tab
35910 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le Implementatio
35920 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34 n {H18200} <S204
35930 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
35940 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 TAL.**.** This r
35950 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
35960 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 77 o register a new
35970 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 [virtual table
35980 6d 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a module] name..**
35990 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75 Module names mu
359a0 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64 st be registered
359b0 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74 before.** creat
359c0 69 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75 ing a new [virtu
359d0 61 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20 al table] using
359e0 74 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 the module, or b
359f0 65 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a efore using a.**
35a00 20 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69 preexisting [vi
35a10 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72 rtual table] for
35a20 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a the module..**.
35a30 2a 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 ** The module na
35a40 6d 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 me is registered
35a50 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 on the [databas
35a60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 e connection] sp
35a70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 ecified.** by th
35a80 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
35a90 72 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 r. The name of
35aa0 74 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69 the module is gi
35ab0 76 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73 ven by the .** s
35ac0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e econd parameter.
35ad0 20 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61 The third para
35ae0 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 meter is a point
35af0 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 er to.** the imp
35b00 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
35b10 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c he [virtual tabl
35b20 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65 e module]. The
35b30 20 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d fourth.** param
35b40 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 eter is an arbit
35b50 72 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61 rary client data
35b60 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 pointer that is
35b70 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a passed through.
35b80 2a 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72 ** into the [xCr
35b90 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e eate] and [xConn
35ba0 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 ect] methods of
35bb0 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
35bc0 65 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e e module.** when
35bd0 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 a new virtual t
35be0 61 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67 able is be being
35bf0 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e created or rein
35c00 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a itialized..**.**
35c10 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
35c20 68 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 has exactly the
35c30 73 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 63 same effect as c
35c40 61 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 alling.** [sqlit
35c50 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
35c60 5f 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55 _v2()] with a NU
35c70 4c 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64 LL client data d
35c80 65 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 estructor..*/.SQ
35c90 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
35ca0 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 EXPERIMENTAL int
35cb0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
35cc0 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 module(. sqlite
35cd0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
35ce0 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 /* SQLite c
35cf0 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 onnection to reg
35d00 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 ister module wit
35d10 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 h */. const cha
35d20 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 r *zName,
35d30 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
35d40 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e module */. con
35d50 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
35d60 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f e *p, /* Metho
35d70 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c ds for the modul
35d80 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c e */. void *pCl
35d90 69 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 20 ientData
35da0 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 /* Client data
35db0 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f for xCreate/xCo
35dc0 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a nnect */.);../*.
35dd0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 ** CAPI3REF: Reg
35de0 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 ister A Virtual
35df0 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 Table Implementa
35e00 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53 tion {H18210} <S
35e10 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 20400>.** EXPERI
35e20 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 MENTAL.**.** Thi
35e30 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65 s routine is ide
35e40 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73 ntical to the [s
35e50 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f qlite3_create_mo
35e60 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a dule()] method,.
35e70 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ** except that i
35e80 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 70 t has an extra p
35e90 61 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63 arameter to spec
35ea0 69 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75 ify .** a destru
35eb0 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f ctor function fo
35ec0 72 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74 r the client dat
35ed0 61 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69 a pointer. SQLi
35ee0 74 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b te will.** invok
35ef0 65 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 e the destructor
35f00 20 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74 function (if it
35f10 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 is not NULL) wh
35f20 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 en SQLite.** no
35f30 6c 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65 longer needs the
35f40 20 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69 pClientData poi
35f50 6e 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 nter. .*/.SQLIT
35f60 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 E_API SQLITE_EXP
35f70 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 ERIMENTAL int sq
35f80 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
35f90 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 ule_v2(. sqlite
35fa0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
35fb0 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63 /* SQLite c
35fc0 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67 onnection to reg
35fd0 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74 ister module wit
35fe0 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 h */. const cha
35ff0 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 r *zName,
36000 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
36010 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e module */. con
36020 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
36030 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f e *p, /* Metho
36040 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c ds for the modul
36050 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c e */. void *pCl
36060 69 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20 ientData,
36070 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61 /* Client data
36080 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f for xCreate/xCo
36090 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28 nnect */. void(
360a0 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a *xDestroy)(void*
360b0 29 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 ) /* Module
360c0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 destructor funct
360d0 69 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ion */.);../*.**
360e0 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 CAPI3REF: Virtu
360f0 61 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63 al Table Instanc
36100 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30 e Object {H18010
36110 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 } <S20400>.** KE
36120 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f YWORDS: sqlite3_
36130 76 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45 vtab.** EXPERIME
36140 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 NTAL.**.** Every
36150 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 [virtual table
36160 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e module] implemen
36170 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 tation uses a su
36180 62 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65 bclass.** of the
36190 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
361a0 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 ture to describe
361b0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e a particular in
361c0 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 stance.** of the
361d0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d [virtual table]
361e0 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73 . Each subclass
361f0 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c will.** be tail
36200 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63 ored to the spec
36210 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68 ific needs of th
36220 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 e module impleme
36230 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 ntation..** The
36240 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20 purpose of this
36250 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f superclass is to
36260 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20 define certain
36270 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a fields that are.
36280 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c ** common to all
36290 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
362a0 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 tations..**.** V
362b0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 irtual tables me
362c0 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e thods can set an
362d0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 error message b
362e0 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a y assigning a.**
362f0 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 string obtained
36300 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
36310 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 printf()] to zEr
36320 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f rMsg. The metho
36330 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 d should.** take
36340 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70 care that any p
36350 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66 rior string is f
36360 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 reed by a call t
36370 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 o [sqlite3_free(
36380 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 )].** prior to a
36390 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 ssigning a new s
363a0 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 tring to zErrMsg
363b0 2e 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72 . After the err
363c0 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 or message.** is
363d0 20 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f delivered up to
363e0 20 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c the client appl
363f0 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 ication, the str
36400 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f ing will be auto
36410 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 matically.** fre
36420 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72 ed by sqlite3_fr
36430 65 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72 ee() and the zEr
36440 72 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 rMsg field will
36450 62 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 be zeroed..*/.st
36460 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 ruct sqlite3_vta
36470 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 b {. const sqli
36480 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
36490 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 ule; /* The mod
364a0 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 72 ule for this vir
364b0 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 tual table */.
364c0 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
364d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
364e0 2f 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 45 /* NO LONGER USE
364f0 44 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 D */. char *zEr
36500 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20 rMsg;
36510 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 /* Error
36520 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c message from sql
36530 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a ite3_mprintf() *
36540 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 /. /* Virtual t
36550 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 able implementat
36560 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61 ions will typica
36570 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e lly add addition
36580 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a al fields */.};.
36590 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
365a0 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43 Virtual Table C
365b0 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48 ursor Object {H
365c0 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a 18020} <S20400>.
365d0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c ** KEYWORDS: sql
365e0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
365f0 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 {virtual table
36600 63 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 cursor}.** EXPER
36610 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 IMENTAL.**.** Ev
36620 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 ery [virtual tab
36630 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 le module] imple
36640 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 mentation uses a
36650 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65 subclass of the
36660 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 .** following st
36670 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 ructure to descr
36680 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 ibe cursors that
36690 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a point into the.
366a0 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c ** [virtual tabl
366b0 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a e] and are used.
366c0 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 ** to loop throu
366d0 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 gh the virtual t
366e0 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61 able. Cursors a
366f0 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 re created using
36700 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
36710 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 _module.xOpen |
36720 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 xOpen] method of
36730 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20 the module and
36740 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a are destroyed.**
36750 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 by the [sqlite3
36760 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c _module.xClose |
36770 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e xClose] method.
36780 20 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 73 Cussors are us
36790 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46 ed.** by the [xF
367a0 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c ilter], [xNext],
367b0 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d [xEof], [xColum
367c0 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d n], and [xRowid]
367d0 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 methods.** of t
367e0 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68 he module. Each
367f0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
36800 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69 tation will defi
36810 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ne.** the conten
36820 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74 t of a cursor st
36830 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20 ructure to suit
36840 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a its own needs..*
36850 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63 *.** This superc
36860 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f lass exists in o
36870 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66 rder to define f
36880 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 ields of the cur
36890 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 sor that.** are
368a0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d common to all im
368b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
368c0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
368d0 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20 _vtab_cursor {.
368e0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
368f0 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69 Vtab; /* Vi
36900 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74 rtual table of t
36910 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 his cursor */.
36920 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 /* Virtual table
36930 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
36940 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 will typically
36950 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 add additional f
36960 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ields */.};../*.
36970 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 ** CAPI3REF: Dec
36980 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20 lare The Schema
36990 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 Of A Virtual Tab
369a0 6c 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30 le {H18280} <S20
369b0 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 400>.** EXPERIME
369c0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b NTAL.**.** The [
369d0 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 xCreate] and [xC
369e0 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 onnect] methods
369f0 6f 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c of a.** [virtual
36a00 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 table module] c
36a10 61 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61 all this interfa
36a20 63 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65 ce.** to declare
36a30 20 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65 the format (the
36a40 20 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74 names and datat
36a50 79 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 ypes of the colu
36a60 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76 mns) of.** the v
36a70 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68 irtual tables th
36a80 65 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f ey implement..*/
36a90 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
36aa0 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
36ab0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c int sqlite3_decl
36ac0 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 are_vtab(sqlite3
36ad0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
36ae0 53 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 SQL);../*.** CAP
36af0 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20 I3REF: Overload
36b00 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41 A Function For A
36b10 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b Virtual Table {
36b20 48 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e H18300} <S20400>
36b30 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
36b40 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 .**.** Virtual t
36b50 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64 ables can provid
36b60 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d e alternative im
36b70 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
36b80 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 functions.** us
36b90 69 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75 ing the [xFindFu
36ba0 6e 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f nction] method o
36bb0 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 f the [virtual t
36bc0 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a able module]. .
36bd0 2a 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65 ** But global ve
36be0 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20 rsions of those
36bf0 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 functions.** mus
36c00 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72 t exist in order
36c10 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 to be overloade
36c20 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 d..**.** This AP
36c30 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67 I makes sure a g
36c40 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66 lobal version of
36c50 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 a function with
36c60 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a a particular.**
36c70 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72 name and number
36c80 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65 of parameters e
36c90 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75 xists. If no su
36ca0 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 ch function exis
36cb0 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 ts.** before thi
36cc0 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c s API is called,
36cd0 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 a new function
36ce0 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 is created. The
36cf0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
36d00 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75 ** of the new fu
36d10 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61 nction always ca
36d20 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f uses an exceptio
36d30 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20 n to be thrown.
36d40 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66 So.** the new f
36d50 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 unction is not g
36d60 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67 ood for anything
36d70 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73 by itself. Its
36d80 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65 only.** purpose
36d90 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63 is to be a plac
36da0 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e eholder function
36db0 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65 that can be ove
36dc0 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20 rloaded.** by a
36dd0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e [virtual table].
36de0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
36df0 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
36e00 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f AL int sqlite3_o
36e10 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e verload_function
36e20 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 (sqlite3*, const
36e30 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 char *zFuncName
36e40 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a , int nArg);../*
36e50 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 .** The interfac
36e60 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c e to the virtual
36e70 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d -table mechanism
36e80 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28 defined above (
36e90 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20 back up.** to a
36ea0 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62 comment remarkab
36eb0 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 ly similar to th
36ec0 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65 is one) is curre
36ed0 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a ntly considered.
36ee0 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d ** to be experim
36ef0 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 ental. The inte
36f00 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e rface might chan
36f10 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 ge in incompatib
36f20 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 le ways..** If t
36f30 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d his is a problem
36f40 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 for you, do not
36f50 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 use the interfa
36f60 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e ce at this time.
36f70 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
36f80 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 virtual-table me
36f90 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a chanism stabiliz
36fa0 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c es, we will decl
36fb0 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 are the.** inter
36fc0 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 face fixed, supp
36fd0 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 ort it indefinit
36fe0 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 ely, and remove
36ff0 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a this comment..**
37000 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 .****** EXPERIME
37010 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 NTAL - subject t
37020 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 o change without
37030 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a notice ********
37040 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ******.*/../*.**
37050 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e CAPI3REF: A Han
37060 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42 dle To An Open B
37070 4c 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33 LOB {H17800} <S3
37080 30 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0230>.** KEYWORD
37090 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d S: {BLOB handle}
370a0 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a {BLOB handles}.
370b0 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 **.** An instanc
370c0 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
370d0 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f represents an o
370e0 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63 pen BLOB on whic
370f0 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c h.** [sqlite3_bl
37100 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d ob_open | increm
37110 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 ental BLOB I/O]
37120 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64 can be performed
37130 2e 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20 ..** Objects of
37140 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 72 this type are cr
37150 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65 eated by [sqlite
37160 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 3_blob_open()].*
37170 2a 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20 * and destroyed
37180 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 by [sqlite3_blob
37190 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 _close()]..** Th
371a0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
371b0 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c read()] and [sql
371c0 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 ite3_blob_write(
371d0 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a )] interfaces.**
371e0 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
371f0 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d read or write sm
37200 61 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20 all subsections
37210 6f 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 of the BLOB..**
37220 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f The [sqlite3_blo
37230 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 b_bytes()] inter
37240 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 face returns the
37250 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f size of the BLO
37260 42 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 B in bytes..*/.t
37270 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 ypedef struct sq
37280 6c 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74 lite3_blob sqlit
37290 65 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 e3_blob;../*.**
372a0 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41 CAPI3REF: Open A
372b0 20 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d BLOB For Increm
372c0 65 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31 ental I/O {H1781
372d0 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 0} <S30230>.**.*
372e0 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
372f0 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20 s opens a [BLOB
37300 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d handle | handle]
37310 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63 to the BLOB loc
37320 61 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 ated.** in row i
37330 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c Row, column zCol
37340 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c umn, table zTabl
37350 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44 e in database zD
37360 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 b;.** in other w
37370 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42 ords, the same B
37380 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62 LOB that would b
37390 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a e selected by:.*
373a0 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 *.** <pre>.**
373b0 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e SELECT zColumn
373c0 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 FROM zDb.zTable
373d0 20 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d WHERE [rowid] =
373e0 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e iRow;.** </pre>
373f0 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 {END}.**.** If
37400 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
37410 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ter is non-zero,
37420 20 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69 then the BLOB i
37430 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 s opened for rea
37440 64 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61 d.** and write a
37450 63 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20 ccess. If it is
37460 7a 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69 zero, the BLOB i
37470 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 s opened for rea
37480 64 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 d access..**.**
37490 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 64 61 Note that the da
374a0 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e tabase name is n
374b0 6f 74 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 ot the filename
374c0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a that contains.**
374d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75 the database bu
374e0 74 20 72 61 74 68 65 72 20 74 68 65 20 73 79 6d t rather the sym
374f0 62 6f 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 bolic name of th
37500 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 0a e database that.
37510 2a 2a 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 ** is assigned w
37520 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
37530 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 is connected us
37540 69 6e 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a ing [ATTACH]..**
37550 20 46 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 For the main da
37560 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
37570 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 database name i
37580 73 20 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 s "main"..** For
37590 20 54 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 TEMP tables, th
375a0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 e database name
375b0 69 73 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a is "temp"..**.**
375c0 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 On success, [SQ
375d0 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 LITE_OK] is retu
375e0 72 6e 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 rned and the new
375f0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 [BLOB handle] i
37600 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 s written.** to
37610 2a 70 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 *ppBlob. Otherwi
37620 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 se an [error cod
37630 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 e] is returned a
37640 6e 64 20 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 nd *ppBlob is se
37650 74 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c t.** to be a nul
37660 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 l pointer..** Th
37670 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 is function sets
37680 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
37690 6f 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 onnection] error
376a0 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 code and messag
376b0 65 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 e.** accessible
376c0 76 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 via [sqlite3_err
376d0 63 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c code()] and [sql
376e0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 ite3_errmsg()] a
376f0 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 nd related.** fu
37700 6e 63 74 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 nctions. Note t
37710 68 61 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 hat the *ppBlob
37720 76 61 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 variable is alwa
37730 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 ys initialized i
37740 6e 20 61 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 n a.** way that
37750 6d 61 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f makes it safe to
37760 20 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 invoke [sqlite3
37770 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f _blob_close()] o
37780 6e 20 2a 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 n *ppBlob.** reg
37790 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73 ardless of the s
377a0 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 uccess or failur
377b0 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e e of this routin
377c0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
377d0 72 6f 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 row that a BLOB
377e0 68 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f handle points to
377f0 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 is modified by
37800 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 an.** [UPDATE],
37810 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 [DELETE], or by
37820 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 [ON CONFLICT] si
37830 64 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 de-effects.** th
37840 65 6e 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 en the BLOB hand
37850 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 le is marked as
37860 22 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 "expired"..** Th
37870 69 73 20 69 73 20 74 72 75 65 20 69 66 20 61 6e is is true if an
37880 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 y column of the
37890 72 6f 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 row is changed,
378a0 65 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a even a column.**
378b0 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 other than the
378c0 6f 6e 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e one the BLOB han
378d0 64 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a dle is open on..
378e0 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c ** Calls to [sql
378f0 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 ite3_blob_read()
37900 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 ] and [sqlite3_b
37910 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 lob_write()] for
37920 0a 2a 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c .** a expired BL
37930 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 OB handle fail w
37940 69 74 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f ith an return co
37950 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 de of [SQLITE_AB
37960 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 ORT]..** Changes
37970 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 written into a
37980 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68 BLOB prior to th
37990 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 e BLOB expiring
379a0 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 are not.** rollb
379b0 61 63 6b 20 62 79 20 74 68 65 20 65 78 70 69 72 ack by the expir
379c0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f ation of the BLO
379d0 42 2e 20 20 53 75 63 68 20 63 68 61 6e 67 65 73 B. Such changes
379e0 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 will eventually
379f0 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 .** commit if th
37a00 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
37a10 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c ntinues to compl
37a20 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 etion..**.** Use
37a30 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c the [sqlite3_bl
37a40 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 ob_bytes()] inte
37a50 72 66 61 63 65 20 74 6f 20 64 65 74 65 72 6d 69 rface to determi
37a60 6e 65 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a ne the size of.*
37a70 2a 20 74 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f * the opened blo
37a80 62 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 b. The size of
37a90 61 20 62 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 a blob may not b
37aa0 65 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 e changed by thi
37ab0 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 s.** interface.
37ac0 20 55 73 65 20 74 68 65 20 5b 55 50 44 41 54 45 Use the [UPDATE
37ad0 5d 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f ] SQL command to
37ae0 20 63 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 change the size
37af0 20 6f 66 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a of a.** blob..*
37b00 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 *.** The [sqlite
37b10 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 3_bind_zeroblob(
37b20 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
37b30 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 result_zeroblob(
37b40 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a )] interfaces.**
37b50 20 61 6e 64 20 74 68 65 20 62 75 69 6c 74 2d 69 and the built-i
37b60 6e 20 5b 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c n [zeroblob] SQL
37b70 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 function can be
37b80 20 75 73 65 64 2c 20 69 66 20 64 65 73 69 72 65 used, if desire
37b90 64 2c 0a 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 d,.** to create
37ba0 61 6e 20 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 an empty, zero-f
37bb0 69 6c 6c 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 illed blob in wh
37bc0 69 63 68 20 74 6f 20 72 65 61 64 20 6f 72 20 77 ich to read or w
37bd0 72 69 74 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 rite using.** th
37be0 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a is interface..**
37bf0 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 61 20 72 .** To avoid a r
37c00 65 73 6f 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 esource leak, ev
37c10 65 72 79 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 ery open [BLOB h
37c20 61 6e 64 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 andle] should ev
37c30 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 entually.** be r
37c40 65 6c 65 61 73 65 64 20 62 79 20 61 20 63 61 6c eleased by a cal
37c50 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c l to [sqlite3_bl
37c60 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a ob_close()]..**.
37c70 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
37c80 0a 2a 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 .** [H17813] [H1
37c90 37 38 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 7814] [H17816] [
37ca0 48 31 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d H17819] [H17821]
37cb0 20 5b 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c [H17824].*/.SQL
37cc0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
37cd0 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 te3_blob_open(.
37ce0 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e sqlite3*,. con
37cf0 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 st char *zDb,.
37d00 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 const char *zTab
37d10 6c 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 le,. const char
37d20 20 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c *zColumn,. sql
37d30 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c ite3_int64 iRow,
37d40 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 . int flags,.
37d50 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 sqlite3_blob **p
37d60 70 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 pBlob.);../*.**
37d70 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 CAPI3REF: Close
37d80 41 20 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 A BLOB Handle {H
37d90 31 37 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 17830} <S30230>.
37da0 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 **.** Closes an
37db0 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c open [BLOB handl
37dc0 65 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e e]..**.** Closin
37dd0 67 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 g a BLOB shall c
37de0 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 ause the current
37df0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 transaction to
37e00 63 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 commit.** if the
37e10 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 re are no other
37e20 42 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e BLOBs, no pendin
37e30 67 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 g prepared state
37e40 6d 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a ments, and the.*
37e50 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
37e60 63 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 ction is in [aut
37e70 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a ocommit mode]..*
37e80 2a 20 49 66 20 61 6e 79 20 77 72 69 74 65 73 20 * If any writes
37e90 77 65 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 were made to the
37ea0 20 42 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 BLOB, they migh
37eb0 74 20 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63 t be held in cac
37ec0 68 65 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 he.** until the
37ed0 63 6c 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 close operation
37ee0 69 66 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74 if they will fit
37ef0 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 ..**.** Closing
37f00 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 the BLOB often f
37f10 6f 72 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 orces the change
37f20 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b s.** out to disk
37f30 20 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 and so if any I
37f40 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c /O errors occur,
37f50 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c they will likel
37f60 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 y occur.** at th
37f70 65 20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 e time when the
37f80 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 BLOB is closed.
37f90 20 41 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 Any errors that
37fa0 20 6f 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a occur during.**
37fb0 20 63 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 closing are rep
37fc0 6f 72 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a orted as a non-z
37fd0 65 72 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 ero return value
37fe0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 ..**.** The BLOB
37ff0 20 69 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e is closed uncon
38000 64 69 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 ditionally. Eve
38010 6e 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e n if this routin
38020 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 e returns.** an
38030 65 72 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 error code, the
38040 42 4c 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c BLOB is still cl
38050 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c osed..**.** Call
38060 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
38070 20 77 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 with a null poi
38080 6e 74 65 72 20 28 77 68 69 63 68 20 61 73 20 77 nter (which as w
38090 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 ould be returned
380a0 0a 2a 2a 20 62 79 20 66 61 69 6c 65 64 20 63 61 .** by failed ca
380b0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 ll to [sqlite3_b
380c0 6c 6f 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 lob_open()]) is
380d0 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 a harmless no-op
380e0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
380f0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 ents:.** [H17833
38100 5d 20 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 ] [H17836] [H178
38110 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 39].*/.SQLITE_AP
38120 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
38130 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 ob_close(sqlite3
38140 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _blob *);../*.**
38150 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 CAPI3REF: Retur
38160 6e 20 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e n The Size Of An
38170 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 Open BLOB {H178
38180 34 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 40} <S30230>.**.
38190 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 ** Returns the s
381a0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
381b0 74 68 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 the BLOB accessi
381c0 62 6c 65 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 ble via the .**
381d0 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 successfully ope
381e0 6e 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 ned [BLOB handle
381f0 5d 20 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 ] in its only ar
38200 67 75 6d 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 gument. The.**
38210 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 incremental blob
38220 20 49 2f 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 I/O routines ca
38230 6e 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f n only read or o
38240 76 65 72 77 72 69 74 69 6e 67 20 65 78 69 73 74 verwriting exist
38250 69 6e 67 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 ing.** blob cont
38260 65 6e 74 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 ent; they cannot
38270 20 63 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 change the size
38280 20 6f 66 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a of a blob..**.*
38290 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f * This routine o
382a0 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b nly works on a [
382b0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 BLOB handle] whi
382c0 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 ch has been crea
382d0 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f ted.** by a prio
382e0 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c r successful cal
382f0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c l to [sqlite3_bl
38300 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 ob_open()] and w
38310 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 hich has not.**
38320 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b been closed by [
38330 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f sqlite3_blob_clo
38340 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 se()]. Passing
38350 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 any other pointe
38360 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 r in.** to this
38370 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 routine results
38380 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 in undefined and
38390 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 probably undesi
383a0 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a rable behavior..
383b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
383c0 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a ts:.** [H17843].
383d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
383e0 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 t sqlite3_blob_b
383f0 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f ytes(sqlite3_blo
38400 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 b *);../*.** CAP
38410 49 33 52 45 46 3a 20 52 65 61 64 20 44 61 74 61 I3REF: Read Data
38420 20 46 72 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 From A BLOB Inc
38430 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 rementally {H178
38440 35 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 50} <S30230>.**.
38450 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
38460 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 is used to read
38470 20 64 61 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 data from an op
38480 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d en [BLOB handle]
38490 20 69 6e 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 into a.** calle
384a0 72 2d 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 r-supplied buffe
384b0 72 2e 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 r. N bytes of da
384c0 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e ta are copied in
384d0 74 6f 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 to buffer Z.** f
384e0 72 6f 6d 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f rom the open BLO
384f0 42 2c 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f B, starting at o
38500 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a ffset iOffset..*
38510 2a 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 *.** If offset i
38520 4f 66 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 Offset is less t
38530 68 61 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d han N bytes from
38540 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
38550 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 BLOB,.** [SQLITE
38560 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 _ERROR] is retur
38570 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 ned and no data
38580 69 73 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f is read. If N o
38590 72 20 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 r iOffset is.**
385a0 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 less than zero,
385b0 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 [SQLITE_ERROR] i
385c0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e s returned and n
385d0 6f 20 64 61 74 61 20 69 73 20 72 65 61 64 2e 0a o data is read..
385e0 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 ** The size of t
385f0 68 65 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e he blob (and hen
38600 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 ce the maximum v
38610 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 alue of N+iOffse
38620 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 t).** can be det
38630 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 ermined using th
38640 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f e [sqlite3_blob_
38650 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 bytes()] interfa
38660 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 ce..**.** An att
38670 65 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f empt to read fro
38680 6d 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c m an expired [BL
38690 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 OB handle] fails
386a0 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f with an.** erro
386b0 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 r code of [SQLIT
386c0 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 E_ABORT]..**.**
386d0 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 On success, SQLI
386e0 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
386f0 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c d..** Otherwise,
38700 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d an [error code]
38710 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 or an [extended
38720 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 error code] is
38730 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
38740 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c This routine onl
38750 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c y works on a [BL
38760 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 OB handle] which
38770 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 has been create
38780 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 d.** by a prior
38790 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 successful call
387a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 to [sqlite3_blob
387b0 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 _open()] and whi
387c0 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 ch has not.** be
387d0 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 en closed by [sq
387e0 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 lite3_blob_close
387f0 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e ()]. Passing an
38800 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 y other pointer
38810 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f in.** to this ro
38820 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e utine results in
38830 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 undefined and p
38840 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 robably undesira
38850 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a ble behavior..**
38860 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
38870 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 qlite3_blob_writ
38880 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 e()]..**.** Requ
38890 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
388a0 37 38 35 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 7853] [H17856] [
388b0 48 31 37 38 35 39 5d 20 5b 48 31 37 38 36 32 5d H17859] [H17862]
388c0 20 5b 48 31 37 38 36 33 5d 20 5b 48 31 37 38 36 [H17863] [H1786
388d0 35 5d 20 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 5] [H17868].*/.S
388e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
388f0 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 lite3_blob_read(
38900 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 sqlite3_blob *,
38910 76 6f 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 void *Z, int N,
38920 69 6e 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f int iOffset);../
38930 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 57 *.** CAPI3REF: W
38940 72 69 74 65 20 44 61 74 61 20 49 6e 74 6f 20 41 rite Data Into A
38950 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 BLOB Incrementa
38960 6c 6c 79 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 lly {H17870} <S3
38970 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0230>.**.** This
38980 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
38990 64 20 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 d to write data
389a0 69 6e 74 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c into an open [BL
389b0 4f 42 20 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 OB handle] from
389c0 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 a.** caller-supp
389d0 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 lied buffer. N b
389e0 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 ytes of data are
389f0 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 copied from the
38a00 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 buffer Z.** int
38a10 6f 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c o the open BLOB,
38a20 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 starting at off
38a30 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a set iOffset..**.
38a40 2a 2a 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 ** If the [BLOB
38a50 68 61 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 handle] passed a
38a60 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
38a70 6d 65 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 ment was not ope
38a80 6e 65 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 ned for.** writi
38a90 6e 67 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 ng (the flags pa
38aa0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
38ab0 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d te3_blob_open()]
38ac0 20 77 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 was zero),.** t
38ad0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
38ae0 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 urns [SQLITE_REA
38af0 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 DONLY]..**.** Th
38b00 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 is function may
38b10 6f 6e 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 only modify the
38b20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
38b30 42 4c 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e BLOB; it is.** n
38b40 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 ot possible to i
38b50 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 ncrease the size
38b60 20 6f 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 of a BLOB using
38b70 20 74 68 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 this API..** If
38b80 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 offset iOffset
38b90 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 is less than N b
38ba0 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e ytes from the en
38bb0 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a d of the BLOB,.*
38bc0 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d * [SQLITE_ERROR]
38bd0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
38be0 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 no data is writ
38bf0 74 65 6e 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a ten. If N is.**
38c00 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 less than zero
38c10 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 [SQLITE_ERROR] i
38c20 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e s returned and n
38c30 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 o data is writte
38c40 6e 2e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f n..** The size o
38c50 66 20 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 f the BLOB (and
38c60 68 65 6e 63 65 20 74 68 65 20 6d 61 78 69 6d 75 hence the maximu
38c70 6d 20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 m value of N+iOf
38c80 66 73 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 fset).** can be
38c90 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 determined using
38ca0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c the [sqlite3_bl
38cb0 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 ob_bytes()] inte
38cc0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 rface..**.** An
38cd0 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
38ce0 20 74 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b to an expired [
38cf0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 BLOB handle] fai
38d00 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 ls with an.** er
38d10 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c ror code of [SQL
38d20 49 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 ITE_ABORT]. Wri
38d30 74 65 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 tes to the BLOB
38d40 74 68 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a that occurred.**
38d50 20 62 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f before the [BLO
38d60 42 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 B handle] expire
38d70 64 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 d are not rolled
38d80 20 62 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 back by the.**
38d90 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 expiration of th
38da0 65 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 e handle, though
38db0 20 6f 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 of course those
38dc0 20 63 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a changes might.*
38dd0 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 * have been over
38de0 77 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 written by the s
38df0 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 tatement that ex
38e00 70 69 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 pired the BLOB h
38e10 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f andle.** or by o
38e20 74 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 ther independent
38e30 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
38e40 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 ** On success, S
38e50 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
38e60 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 rned..** Otherwi
38e70 73 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 se, an [error c
38e80 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 ode] or an [exte
38e90 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d nded error code]
38ea0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
38eb0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
38ec0 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 only works on a
38ed0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 [BLOB handle] w
38ee0 68 69 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 hich has been cr
38ef0 65 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 eated.** by a pr
38f00 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 ior successful c
38f10 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
38f20 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 blob_open()] and
38f30 20 77 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a which has not.*
38f40 2a 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 * been closed by
38f50 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 [sqlite3_blob_c
38f60 6c 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e lose()]. Passin
38f70 67 20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e g any other poin
38f80 74 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 ter in.** to thi
38f90 73 20 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 s routine result
38fa0 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 s in undefined a
38fb0 6e 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 nd probably unde
38fc0 73 69 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 sirable behavior
38fd0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
38fe0 3a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f : [sqlite3_blob_
38ff0 72 65 61 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 read()]..**.** R
39000 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
39010 5b 48 31 37 38 37 33 5d 20 5b 48 31 37 38 37 34 [H17873] [H17874
39020 5d 20 5b 48 31 37 38 37 35 5d 20 5b 48 31 37 38 ] [H17875] [H178
39030 37 36 5d 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 76] [H17877] [H1
39040 37 38 37 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 7879] [H17882] [
39050 48 31 37 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 H17885].** [H178
39060 38 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 88].*/.SQLITE_AP
39070 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
39080 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 ob_write(sqlite3
39090 5f 62 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 _blob *, const v
390a0 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 oid *z, int n, i
390b0 6e 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a nt iOffset);../*
390c0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 .** CAPI3REF: Vi
390d0 72 74 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 rtual File Syste
390e0 6d 20 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 m Objects {H1120
390f0 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S20100>.**.*
39100 2a 20 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 * A virtual file
39110 73 79 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 system (VFS) is
39120 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d an [sqlite3_vfs]
39130 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 object.** that
39140 53 51 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 SQLite uses to i
39150 6e 74 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 nteract.** with
39160 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f the underlying o
39170 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e perating system.
39180 20 20 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 Most SQLite bu
39190 69 6c 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 ilds come with a
391a0 0a 2a 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 .** single defau
391b0 6c 74 20 56 46 53 20 74 68 61 74 20 69 73 20 61 lt VFS that is a
391c0 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 ppropriate for t
391d0 68 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 he host computer
391e0 2e 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 ..** New VFSes c
391f0 61 6e 20 62 65 20 72 65 67 69 73 74 65 72 65 64 an be registered
39200 20 61 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 and existing VF
39210 53 65 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 Ses can be unreg
39220 69 73 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 istered..** The
39230 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 following interf
39240 61 63 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 aces are provide
39250 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c d..**.** The sql
39260 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 ite3_vfs_find()
39270 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
39280 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
39290 20 56 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e VFS given its n
392a0 61 6d 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 ame..** Names ar
392b0 65 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 e case sensitive
392c0 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a ..** Names are z
392d0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 ero-terminated U
392e0 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a TF-8 strings..**
392f0 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
39300 6d 61 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f match, a NULL po
39310 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 inter is returne
39320 64 2e 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d d..** If zVfsNam
39330 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 e is NULL then t
39340 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 he default VFS i
39350 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
39360 2a 20 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 * New VFSes are
39370 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 registered with
39380 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
39390 73 74 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 ster()..** Each
393a0 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 new VFS becomes
393b0 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 the default VFS
393c0 69 66 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 if the makeDflt
393d0 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 flag is set..**
393e0 54 68 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e The same VFS can
393f0 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 6d be registered m
39400 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 ultiple times wi
39410 74 68 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a thout injury..**
39420 20 54 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 To make an exis
39430 74 69 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 ting VFS into th
39440 65 20 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 e default VFS, r
39450 65 67 69 73 74 65 72 20 69 74 20 61 67 61 69 6e egister it again
39460 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b .** with the mak
39470 65 44 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 eDflt flag set.
39480 20 49 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e If two differen
39490 74 20 56 46 53 65 73 20 77 69 74 68 20 74 68 65 t VFSes with the
394a0 0a 2a 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 .** same name ar
394b0 65 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 e registered, th
394c0 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e e behavior is un
394d0 64 65 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a defined. If a.*
394e0 2a 20 56 46 53 20 69 73 20 72 65 67 69 73 74 65 * VFS is registe
394f0 72 65 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 red with a name
39500 74 68 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 that is NULL or
39510 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c an empty string,
39520 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 .** then the beh
39530 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
39540 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 ed..**.** Unregi
39550 73 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20 ster a VFS with
39560 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f the sqlite3_vfs_
39570 75 6e 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 unregister() int
39580 65 72 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 erface..** If th
39590 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 e default VFS is
395a0 20 75 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 unregistered, a
395b0 6e 6f 74 68 65 72 20 56 46 53 20 69 73 20 63 68 nother VFS is ch
395c0 6f 73 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 osen as.** the d
395d0 65 66 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f efault. The cho
395e0 69 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 ice for the new
395f0 56 46 53 20 69 73 20 61 72 62 69 74 72 61 72 79 VFS is arbitrary
39600 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
39610 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 ents:.** [H11203
39620 5d 20 5b 48 31 31 32 30 36 5d 20 5b 48 31 31 32 ] [H11206] [H112
39630 30 39 5d 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 09] [H11212] [H1
39640 31 32 31 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 1215] [H11218].*
39650 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
39660 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 ite3_vfs *sqlite
39670 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 3_vfs_find(const
39680 20 63 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 char *zVfsName)
39690 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
396a0 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 sqlite3_vfs_reg
396b0 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 ister(sqlite3_vf
396c0 73 2a 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 s*, int makeDflt
396d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
396e0 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e t sqlite3_vfs_un
396f0 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 register(sqlite3
39700 5f 76 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 _vfs*);../*.** C
39710 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73 API3REF: Mutexes
39720 20 7b 48 31 37 30 30 30 7d 20 3c 53 32 30 30 30 {H17000} <S2000
39730 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 0>.**.** The SQL
39740 69 74 65 20 63 6f 72 65 20 75 73 65 73 20 74 68 ite core uses th
39750 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 ese routines for
39760 20 74 68 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 thread.** synch
39770 72 6f 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 ronization. Thou
39780 67 68 20 74 68 65 79 20 61 72 65 20 69 6e 74 65 gh they are inte
39790 6e 64 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 nded for interna
397a0 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 l.** use by SQLi
397b0 74 65 2c 20 63 6f 64 65 20 74 68 61 74 20 6c 69 te, code that li
397c0 6e 6b 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69 nks against SQLi
397d0 74 65 20 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 te is.** permitt
397e0 65 64 20 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 ed to use any of
397f0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e these routines.
39800 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 .**.** The SQLit
39810 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f e source code co
39820 6e 74 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 ntains multiple
39830 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a implementations.
39840 2a 2a 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65 ** of these mute
39850 78 20 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 x routines. An
39860 61 70 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c appropriate impl
39870 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 ementation.** is
39880 20 73 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 selected automa
39890 74 69 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 tically at compi
398a0 6c 65 2d 74 69 6d 65 2e 20 20 54 68 65 20 66 6f le-time. The fo
398b0 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 llowing.** imple
398c0 6d 65 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 61 mentations are a
398d0 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 vailable in the
398e0 53 51 4c 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a SQLite core:.**.
398f0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 ** <ul>.** <li>
39900 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f SQLITE_MUTEX_O
39910 53 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c S2.** <li> SQL
39920 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
39930 44 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 D.** <li> SQLI
39940 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 TE_MUTEX_W32.**
39950 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
39960 54 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c TEX_NOOP.** </ul
39970 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 >.**.** The SQLI
39980 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d TE_MUTEX_NOOP im
39990 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
399a0 61 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 a set of routine
399b0 73 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e s.** that does n
399c0 6f 20 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 o real locking a
399d0 6e 64 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 nd is appropriat
399e0 65 20 66 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 e for use in.**
399f0 61 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 a single-threade
39a00 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 d application.
39a10 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 The SQLITE_MUTEX
39a20 5f 4f 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f _OS2,.** SQLITE_
39a30 4d 55 54 45 58 5f 50 54 48 52 45 41 44 2c 20 61 MUTEX_PTHREAD, a
39a40 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f nd SQLITE_MUTEX_
39a50 57 33 32 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 W32 implementati
39a60 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f ons.** are appro
39a70 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f priate for use o
39a80 6e 20 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e n OS/2, Unix, an
39a90 64 20 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a d Windows..**.**
39aa0 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f If SQLite is co
39ab0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 mpiled with the
39ac0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 SQLITE_MUTEX_APP
39ad0 44 45 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 DEF preprocessor
39ae0 0a 2a 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 .** macro define
39af0 64 20 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 d (with "-DSQLIT
39b00 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 E_MUTEX_APPDEF=1
39b10 22 29 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 "), then no mute
39b20 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 x.** implementat
39b30 69 6f 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 ion is included
39b40 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 with the library
39b50 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
39b60 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f he.** applicatio
39b70 6e 20 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 n must supply a
39b80 63 75 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 custom mutex imp
39b90 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e lementation usin
39ba0 67 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 g the.** [SQLITE
39bb0 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f _CONFIG_MUTEX] o
39bc0 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ption of the sql
39bd0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 ite3_config() fu
39be0 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 nction.** before
39bf0 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 calling sqlite3
39c00 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 _initialize() or
39c10 20 61 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 any other publi
39c20 63 20 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 c sqlite3_.** fu
39c30 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c nction that call
39c40 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 s sqlite3_initia
39c50 6c 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 lize()..**.** {H
39c60 31 37 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74 17011} The sqlit
39c70 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
39c80 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 routine allocat
39c90 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 es a new.** mute
39ca0 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 x and returns a
39cb0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b pointer to it. {
39cc0 48 31 37 30 31 32 7d 20 49 66 20 69 74 20 72 65 H17012} If it re
39cd0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 turns NULL.** th
39ce0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 at means that a
39cf0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 mutex could not
39d00 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 be allocated. {H
39d10 31 37 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 17013} SQLite.**
39d20 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 will unwind its
39d30 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 stack and retur
39d40 6e 20 61 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 n an error. {H17
39d50 30 31 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 014} The argumen
39d60 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f t.** to sqlite3_
39d70 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 mutex_alloc() is
39d80 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e one of these in
39d90 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a teger constants:
39da0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
39db0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
39dc0 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 X_FAST.** <li>
39dd0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
39de0 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 URSIVE.** <li>
39df0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
39e00 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c TIC_MASTER.** <l
39e10 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
39e20 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c _STATIC_MEM.** <
39e30 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
39e40 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a X_STATIC_MEM2.**
39e50 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
39e60 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a TEX_STATIC_PRNG.
39e70 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
39e80 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
39e90 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
39ea0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
39eb0 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a U2.** </ul>.**.*
39ec0 2a 20 7b 48 31 37 30 31 35 7d 20 54 68 65 20 66 * {H17015} The f
39ed0 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e irst two constan
39ee0 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 ts cause sqlite3
39ef0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 _mutex_alloc() t
39f00 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 o create.** a ne
39f10 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 w mutex. The ne
39f20 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 w mutex is recur
39f30 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 sive when SQLITE
39f40 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
39f50 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 .** is used but
39f60 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 not necessarily
39f70 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d so when SQLITE_M
39f80 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 UTEX_FAST is use
39f90 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 d. {END}.** The
39fa0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 mutex implementa
39fb0 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 tion does not ne
39fc0 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 ed to make a dis
39fd0 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 tinction.** betw
39fe0 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 een SQLITE_MUTEX
39ff0 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 _RECURSIVE and S
3a000 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
3a010 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e if it does.** n
3a020 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 ot want to. {H1
3a030 37 30 31 36 7d 20 42 75 74 20 53 51 4c 69 74 65 7016} But SQLite
3a040 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 will only reque
3a050 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d st a recursive m
3a060 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 utex in.** cases
3a070 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 where it really
3a080 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e needs one. {EN
3a090 44 7d 20 49 66 20 61 20 66 61 73 74 65 72 20 6e D} If a faster n
3a0a0 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 on-recursive mut
3a0b0 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ex.** implementa
3a0c0 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c tion is availabl
3a0d0 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c e on the host pl
3a0e0 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 atform, the mute
3a0f0 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d x subsystem.** m
3a100 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 ight return such
3a110 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 a mutex in resp
3a120 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d onse to SQLITE_M
3a130 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a UTEX_FAST..**.**
3a140 20 7b 48 31 37 30 31 37 7d 20 54 68 65 20 6f 74 {H17017} The ot
3a150 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 her allowed para
3a160 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 meters to sqlite
3a170 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
3a180 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 each return.** a
3a190 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 pointer to a st
3a1a0 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 atic preexisting
3a1b0 20 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 mutex. {END} S
3a1c0 69 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 ix static mutexe
3a1d0 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 s are.** used by
3a1e0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 the current ver
3a1f0 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 sion of SQLite.
3a200 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
3a210 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 of SQLite.** ma
3a220 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c y add additional
3a230 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e static mutexes.
3a240 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 Static mutexes
3a250 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 are for interna
3a260 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 l.** use by SQLi
3a270 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 te only. Applic
3a280 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 ations that use
3a290 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 SQLite mutexes s
3a2a0 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c hould.** use onl
3a2b0 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 y the dynamic mu
3a2c0 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 texes returned b
3a2d0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 y SQLITE_MUTEX_F
3a2e0 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 AST or.** SQLITE
3a2f0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
3a300 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d ..**.** {H17018}
3a310 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e Note that if on
3a320 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 e of the dynamic
3a330 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 mutex parameter
3a340 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f s (SQLITE_MUTEX_
3a350 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 FAST.** or SQLIT
3a360 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
3a370 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 E) is used then
3a380 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
3a390 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 loc().** returns
3a3a0 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 a different mut
3a3b0 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ex on every call
3a3c0 2e 20 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 . {H17034} But
3a3d0 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a for the static.*
3a3e0 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 * mutex types, t
3a3f0 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 he same mutex is
3a400 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 returned on eve
3a410 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 ry call that has
3a420 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 .** the same typ
3a430 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 e number..**.**
3a440 7b 48 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c {H17019} The sql
3a450 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 ite3_mutex_free(
3a460 29 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f ) routine deallo
3a470 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 cates a previous
3a480 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 ly.** allocated
3a490 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b dynamic mutex. {
3a4a0 48 31 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 H17020} SQLite i
3a4b0 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 s careful to dea
3a4c0 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a llocate every.**
3a4d0 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 dynamic mutex t
3a4e0 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 hat it allocates
3a4f0 2e 20 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 . {A17021} The d
3a500 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d ynamic mutexes m
3a510 75 73 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a ust not be in.**
3a520 20 75 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 use when they a
3a530 72 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 re deallocated.
3a540 7b 41 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 {A17022} Attempt
3a550 69 6e 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 ing to deallocat
3a560 65 20 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 e a static.** mu
3a570 74 65 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 tex results in u
3a580 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f ndefined behavio
3a590 72 2e 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 r. {H17023} SQLi
3a5a0 74 65 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 te never dealloc
3a5b0 61 74 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 ates.** a static
3a5c0 20 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a mutex. {END}.**
3a5d0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
3a5e0 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e mutex_enter() an
3a5f0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
3a600 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 try() routines a
3a610 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 ttempt.** to ent
3a620 65 72 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 er a mutex. {H17
3a630 30 32 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 024} If another
3a640 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 thread is alread
3a650 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 y within the mut
3a660 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ex,.** sqlite3_m
3a670 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c utex_enter() wil
3a680 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 l block and sqli
3a690 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
3a6a0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 will return.** S
3a6b0 51 4c 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 QLITE_BUSY. {H17
3a6c0 30 32 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 025} The sqlite
3a6d0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 3_mutex_try() in
3a6e0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
3a6f0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 [SQLITE_OK].** u
3a700 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 pon successful e
3a710 6e 74 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 ntry. {H17026}
3a720 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 Mutexes created
3a730 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f using.** SQLITE_
3a740 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
3a750 63 61 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d can be entered m
3a760 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 ultiple times by
3a770 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
3a780 2e 0a 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e ..** {H17027} In
3a790 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c such cases the,
3a7a0 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 .** mutex must b
3a7b0 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 e exited an equa
3a7c0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 l number of time
3a7d0 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 s before another
3a7e0 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 thread.** can e
3a7f0 6e 74 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 nter. {A17028}
3a800 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 If the same thre
3a810 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 ad tries to ente
3a820 72 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b r any other.** k
3a830 69 6e 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 ind of mutex mor
3a840 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 e than once, the
3a850 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
3a860 65 66 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 efined..** {H170
3a870 32 39 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 29} SQLite will
3a880 6e 65 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a never exhibit.**
3a890 20 73 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 such behavior i
3a8a0 6e 20 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 n its own use of
3a8b0 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 mutexes..**.**
3a8c0 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f Some systems (fo
3a8d0 72 20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f r example, Windo
3a8e0 77 73 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 ws 95) do not su
3a8f0 70 70 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 pport the operat
3a900 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 ion.** implement
3a910 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 ed by sqlite3_mu
3a920 74 65 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 tex_try(). On t
3a930 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 hose systems, sq
3a940 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
3a950 29 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 ).** will always
3a960 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
3a970 55 53 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 USY. {H17030} T
3a980 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f he SQLite core o
3a990 6e 6c 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a nly ever uses.**
3a9a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
3a9b0 72 79 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d ry() as an optim
3a9c0 69 7a 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 ization so this
3a9d0 69 73 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 is acceptable be
3a9e0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 havior..**.** {H
3a9f0 31 37 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 17031} The sqlit
3aa00 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
3aa10 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
3aa20 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a mutex that was.
3aa30 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e ** previously en
3aa40 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
3aa50 65 20 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 e thread. {A170
3aa60 33 32 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 32} The behavior
3aa70 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 .** is undefined
3aa80 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 if the mutex is
3aa90 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 not currently e
3aaa0 6e 74 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a ntered by the.**
3aab0 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 calling thread
3aac0 6f 72 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e or is not curren
3aad0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 tly allocated.
3aae0 7b 48 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 {H17033} SQLite
3aaf0 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f will.** never do
3ab00 20 65 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a either. {END}.*
3ab10 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 *.** If the argu
3ab20 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f ment to sqlite3_
3ab30 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 mutex_enter(), s
3ab40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
3ab50 28 29 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 (), or.** sqlite
3ab60 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 3_mutex_leave()
3ab70 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 is a NULL pointe
3ab80 72 2c 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 r, then all thre
3ab90 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 e routines.** be
3aba0 68 61 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a have as no-ops..
3abb0 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
3abc0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 [sqlite3_mutex_h
3abd0 65 6c 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 eld()] and [sqli
3abe0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
3abf0 64 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f d()]..*/.SQLITE_
3ac00 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 API sqlite3_mute
3ac10 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 x *sqlite3_mutex
3ac20 5f 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c _alloc(int);.SQL
3ac30 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
3ac40 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 ite3_mutex_free(
3ac50 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b sqlite3_mutex*);
3ac60 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
3ac70 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
3ac80 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 nter(sqlite3_mut
3ac90 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ex*);.SQLITE_API
3aca0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 int sqlite3_mut
3acb0 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d ex_try(sqlite3_m
3acc0 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 utex*);.SQLITE_A
3acd0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
3ace0 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 mutex_leave(sqli
3acf0 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a te3_mutex*);../*
3ad00 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 .** CAPI3REF: Mu
3ad10 74 65 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 tex Methods Obje
3ad20 63 74 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 ct {H17120} <S20
3ad30 31 33 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 130>.** EXPERIME
3ad40 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e NTAL.**.** An in
3ad50 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 stance of this s
3ad60 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 tructure defines
3ad70 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 the low-level r
3ad80 6f 75 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 outines.** used
3ad90 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 to allocate and
3ada0 75 73 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a use mutexes..**.
3adb0 2a 2a 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 ** Usually, the
3adc0 64 65 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d default mutex im
3add0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 plementations pr
3ade0 6f 76 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 ovided by SQLite
3adf0 20 61 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 are.** sufficie
3ae00 6e 74 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 nt, however the
3ae10 75 73 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 user has the opt
3ae20 69 6f 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 ion of substitut
3ae30 69 6e 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 ing a custom.**
3ae40 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 implementation f
3ae50 6f 72 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 or specialized d
3ae60 65 70 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 eployments or sy
3ae70 73 74 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 stems for which
3ae80 53 51 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e SQLite.** does n
3ae90 6f 74 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 ot provide a sui
3aea0 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 table implementa
3aeb0 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 tion. In this ca
3aec0 73 65 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 se, the user.**
3aed0 63 72 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 creates and popu
3aee0 6c 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 lates an instanc
3aef0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 e of this struct
3af00 75 72 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 ure to pass.** t
3af10 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 o sqlite3_config
3af20 28 29 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 () along with th
3af30 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 e [SQLITE_CONFIG
3af40 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a _MUTEX] option..
3af50 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c ** Additionally,
3af60 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
3af70 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 this structure c
3af80 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 6e an be used as an
3af90 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 .** output varia
3afa0 62 6c 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e ble when queryin
3afb0 67 20 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 g the system for
3afc0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 the current mut
3afd0 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ex.** implementa
3afe0 74 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 tion, using the
3aff0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 [SQLITE_CONFIG_G
3b000 45 54 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e ETMUTEX] option.
3b010 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 .**.** The xMute
3b020 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 xInit method def
3b030 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 ined by this str
3b040 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 ucture is invoke
3b050 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 d as.** part of
3b060 73 79 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a system initializ
3b070 61 74 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c ation by the sql
3b080 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
3b090 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b ) function..** {
3b0a0 48 31 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 H17001} The xMut
3b0b0 65 78 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 exInit routine s
3b0c0 68 61 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 hall be called b
3b0d0 79 20 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f y SQLite once fo
3b0e0 72 20 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 r each.** effect
3b0f0 69 76 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c ive call to [sql
3b100 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
3b110 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d )]..**.** The xM
3b120 75 74 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 utexEnd method d
3b130 65 66 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 efined by this s
3b140 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f tructure is invo
3b150 6b 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f ked as.** part o
3b160 66 20 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 f system shutdow
3b170 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 n by the sqlite3
3b180 5f 73 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 _shutdown() func
3b190 74 69 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 tion. The.** imp
3b1a0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
3b1b0 68 69 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 his method is ex
3b1c0 70 65 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 pected to releas
3b1d0 65 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e e all outstandin
3b1e0 67 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f g.** resources o
3b1f0 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d btained by the m
3b200 75 74 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 utex methods imp
3b210 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 lementation, esp
3b220 65 63 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 ecially.** those
3b230 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 obtained by the
3b240 20 78 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 xMutexInit meth
3b250 6f 64 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 od. {H17003} The
3b260 20 78 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 xMutexEnd().**
3b270 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 interface shall
3b280 62 65 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 be invoked once
3b290 66 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f for each call to
3b2a0 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f [sqlite3_shutdo
3b2b0 77 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 wn()]..**.** The
3b2c0 20 72 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e remaining seven
3b2d0 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 methods defined
3b2e0 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75 by this structu
3b2f0 72 65 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c re (xMutexAlloc,
3b300 0a 2a 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 .** xMutexFree,
3b310 78 4d 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 xMutexEnter, xMu
3b320 74 65 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 texTry, xMutexLe
3b330 61 76 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 ave, xMutexHeld
3b340 61 6e 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 and.** xMutexNot
3b350 68 65 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 held) implement
3b360 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e the following in
3b370 74 65 72 66 61 63 65 73 20 28 72 65 73 70 65 63 terfaces (respec
3b380 74 69 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c tively):.**.** <
3b390 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b ul>.** <li> [
3b3a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
3b3b0 6c 6f 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 loc()] </li>.**
3b3c0 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 <li> [sqlite3
3b3d0 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c _mutex_free()] <
3b3e0 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 /li>.** <li>
3b3f0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 [sqlite3_mutex_e
3b400 6e 74 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a nter()] </li>.**
3b410 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 <li> [sqlite
3b420 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 3_mutex_try()] <
3b430 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 /li>.** <li>
3b440 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c [sqlite3_mutex_l
3b450 65 61 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a eave()] </li>.**
3b460 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 <li> [sqlite
3b470 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3_mutex_held()]
3b480 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 </li>.** <li>
3b490 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
3b4a0 6e 6f 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e notheld()] </li>
3b4b0 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ul>.**.**
3b4c0 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 The only differe
3b4d0 6e 63 65 20 69 73 20 74 68 61 74 20 74 68 65 20 nce is that the
3b4e0 70 75 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 public sqlite3_X
3b4f0 58 58 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 XX functions enu
3b500 6d 65 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 merated.** above
3b510 20 73 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 silently ignore
3b520 20 61 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 any invocations
3b530 20 74 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c that pass a NUL
3b540 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 L pointer instea
3b550 64 0a 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 d.** of a valid
3b560 6d 75 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 mutex handle. Th
3b570 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
3b580 73 20 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 s of the methods
3b590 20 64 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 defined.** by t
3b5a0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 his structure ar
3b5b0 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 e not required t
3b5c0 6f 20 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 o handle this ca
3b5d0 73 65 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a se, the results.
3b5e0 2a 2a 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 ** of passing a
3b5f0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 NULL pointer ins
3b600 74 65 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 tead of a valid
3b610 6d 75 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 mutex handle are
3b620 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 undefined.** (i
3b630 2e 65 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 .e. it is accept
3b640 61 62 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 able to provide
3b650 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f an implementatio
3b660 6e 20 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 n that segfaults
3b670 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 if.** it is pas
3b680 73 65 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 sed a NULL point
3b690 65 72 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 er)..**.** The x
3b6a0 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 65 74 68 MutexInit() meth
3b6b0 6f 64 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 od must be threa
3b6c0 64 73 61 66 65 2e 20 20 49 74 20 6d 75 73 74 20 dsafe. It must
3b6d0 62 65 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a be harmless to.*
3b6e0 2a 20 69 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 * invoke xMutexI
3b6f0 6e 69 74 28 29 20 6d 75 74 69 70 6c 65 20 74 69 nit() mutiple ti
3b700 6d 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 mes within the s
3b710 61 6d 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20 ame process and
3b720 77 69 74 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 without.** inter
3b730 76 65 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 vening calls to
3b740 78 4d 75 74 65 78 45 6e 64 28 29 2e 20 20 53 65 xMutexEnd(). Se
3b750 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 cond and subsequ
3b760 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 ent calls to.**
3b770 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73 xMutexInit() mus
3b780 74 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a t be no-ops..**.
3b790 2a 2a 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 ** xMutexInit()
3b7a0 6d 75 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c must not use SQL
3b7b0 69 74 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ite memory alloc
3b7c0 61 74 69 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f ation ([sqlite3_
3b7d0 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 malloc()].** and
3b7e0 20 69 74 73 20 61 73 73 6f 63 69 61 74 65 73 29 its associates)
3b7f0 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d . Similarly, xM
3b800 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 utexAlloc() must
3b810 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 not use SQLite
3b820 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 memory.** alloca
3b830 74 69 6f 6e 20 66 6f 72 20 61 20 73 74 61 74 69 tion for a stati
3b840 63 20 6d 75 74 65 78 2e 20 20 48 6f 77 65 76 65 c mutex. Howeve
3b850 72 20 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 r xMutexAlloc()
3b860 6d 61 79 20 75 73 65 20 53 51 4c 69 74 65 0a 2a may use SQLite.*
3b870 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 * memory allocat
3b880 69 6f 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f ion for a fast o
3b890 72 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 r recursive mute
3b8a0 78 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 x..**.** SQLite
3b8b0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 will invoke the
3b8c0 78 4d 75 74 65 78 45 6e 64 28 29 20 6d 65 74 68 xMutexEnd() meth
3b8d0 6f 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 od when [sqlite3
3b8e0 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a _shutdown()] is.
3b8f0 2a 2a 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f ** called, but o
3b900 6e 6c 79 20 69 66 20 74 68 65 20 70 72 69 6f 72 nly if the prior
3b910 20 63 61 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 call to xMutexI
3b920 6e 69 74 20 72 65 74 75 72 6e 65 64 20 53 51 4c nit returned SQL
3b930 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d ITE_OK..** If xM
3b940 75 74 65 78 49 6e 69 74 20 66 61 69 6c 73 20 69 utexInit fails i
3b950 6e 20 61 6e 79 20 77 61 79 2c 20 69 74 20 69 73 n any way, it is
3b960 20 65 78 70 65 63 74 65 64 20 74 6f 20 63 6c 65 expected to cle
3b970 61 6e 20 75 70 20 61 66 74 65 72 20 69 74 73 65 an up after itse
3b980 6c 66 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 lf.** prior to r
3b990 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 eturning..*/.typ
3b9a0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
3b9b0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
3b9c0 73 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f s sqlite3_mutex_
3b9d0 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 methods;.struct
3b9e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
3b9f0 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a thods {. int (*
3ba00 78 4d 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 xMutexInit)(void
3ba10 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 );. int (*xMute
3ba20 78 45 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 xEnd)(void);. s
3ba30 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a qlite3_mutex *(*
3ba40 78 4d 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 xMutexAlloc)(int
3ba50 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 );. void (*xMut
3ba60 65 78 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f exFree)(sqlite3_
3ba70 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 mutex *);. void
3ba80 20 28 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 (*xMutexEnter)(
3ba90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 sqlite3_mutex *)
3baa0 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 ;. int (*xMutex
3bab0 54 72 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 Try)(sqlite3_mut
3bac0 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a ex *);. void (*
3bad0 78 4d 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c xMutexLeave)(sql
3bae0 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 ite3_mutex *);.
3baf0 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c int (*xMutexHel
3bb00 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 d)(sqlite3_mutex
3bb10 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 *);. int (*xMu
3bb20 74 65 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 texNotheld)(sqli
3bb30 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b te3_mutex *);.};
3bb40 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3bb50 3a 20 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 : Mutex Verifica
3bb60 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 tion Routines {H
3bb70 31 37 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 17080} <S20130>
3bb80 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S30800>.**.** T
3bb90 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
3bba0 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 _held() and sqli
3bbb0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
3bbc0 64 28 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 d() routines.**
3bbd0 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 are intended for
3bbe0 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 use inside asse
3bbf0 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
3bc00 20 7b 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 {H17081} The SQ
3bc10 4c 69 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 Lite core.** nev
3bc20 65 72 20 75 73 65 73 20 74 68 65 73 65 20 72 6f er uses these ro
3bc30 75 74 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e utines except in
3bc40 73 69 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 side an assert()
3bc50 20 61 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e and application
3bc60 73 0a 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 s.** are advised
3bc70 20 74 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c to follow the l
3bc80 65 61 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e ead of the core.
3bc90 20 20 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 {H17082} The c
3bca0 6f 72 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 ore only.** prov
3bcb0 69 64 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ides implementat
3bcc0 69 6f 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 ions for these r
3bcd0 6f 75 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 outines when it
3bce0 69 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 is compiled.** w
3bcf0 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 ith the SQLITE_D
3bd00 45 42 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 EBUG flag. {A17
3bd10 30 38 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 087} External mu
3bd20 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
3bd30 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 ons.** are only
3bd40 72 65 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 required to prov
3bd50 69 64 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e ide these routin
3bd60 65 73 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 es if SQLITE_DEB
3bd70 55 47 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 UG is.** defined
3bd80 20 61 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 and if NDEBUG i
3bd90 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a s not defined..*
3bda0 2a 0a 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 *.** {H17083} Th
3bdb0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f ese routines sho
3bdc0 75 6c 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 uld return true
3bdd0 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 if the mutex in
3bde0 74 68 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a their argument.*
3bdf0 2a 20 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 * is held or not
3be00 20 68 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 held, respectiv
3be10 65 6c 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c ely, by the call
3be20 69 6e 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a ing thread..**.*
3be30 2a 20 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 * {X17084} The i
3be40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 mplementation is
3be50 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f not required to
3be60 20 70 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f provided versio
3be70 6e 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 ns of these.** r
3be80 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 outines that act
3be90 75 61 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 ually work. If t
3bea0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
3beb0 6e 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 n does not provi
3bec0 64 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 de working.** ve
3bed0 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 rsions of these
3bee0 72 6f 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f routines, it sho
3bef0 75 6c 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f uld at least pro
3bf00 76 69 64 65 20 73 74 75 62 73 20 74 68 61 74 20 vide stubs that
3bf10 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e always.** return
3bf20 20 74 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e true so that on
3bf30 65 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 e does not get s
3bf40 70 75 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f purious assertio
3bf50 6e 20 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a n failures..**.*
3bf60 2a 20 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 * {H17085} If th
3bf70 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 e argument to sq
3bf80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
3bf90 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 () is a NULL poi
3bfa0 6e 74 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 nter then.** the
3bfb0 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 routine should
3bfc0 72 65 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d return 1. {END}
3bfd0 20 54 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e This seems coun
3bfe0 74 65 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 ter-intuitive si
3bff0 6e 63 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 nce.** clearly t
3c000 68 65 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 he mutex cannot
3c010 62 65 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f be held if it do
3c020 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 es not exist. B
3c030 75 74 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 ut the.** the re
3c040 61 73 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 ason the mutex d
3c050 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 oes not exist is
3c060 20 62 65 63 61 75 73 65 20 74 68 65 20 62 75 69 because the bui
3c070 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 ld is not.** usi
3c080 6e 67 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 ng mutexes. And
3c090 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 we do not want
3c0a0 74 68 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e the assert() con
3c0b0 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 taining the.** c
3c0c0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d all to sqlite3_m
3c0d0 75 74 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 utex_held() to f
3c0e0 61 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 ail, so a non-ze
3c0f0 72 6f 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 ro return is.**
3c100 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 the appropriate
3c110 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 thing to do. {H
3c120 31 37 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 17086} The sqlit
3c130 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
3c140 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 ().** interface
3c150 73 68 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 should also retu
3c160 72 6e 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 rn 1 when given
3c170 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
3c180 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
3c190 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f t sqlite3_mutex_
3c1a0 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
3c1b0 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ex*);.SQLITE_API
3c1c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 int sqlite3_mut
3c1d0 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 ex_notheld(sqlit
3c1e0 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a e3_mutex*);../*.
3c1f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 ** CAPI3REF: Mut
3c200 65 78 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 ex Types {H17001
3c210 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a } <H17000>.**.**
3c220 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 The [sqlite3_mu
3c230 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 tex_alloc()] int
3c240 65 72 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 erface takes a s
3c250 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a ingle argument.*
3c260 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f * which is one o
3c270 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 f these integer
3c280 63 6f 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a constants..**.**
3c290 20 54 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 The set of stat
3c2a0 69 63 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 ic mutexes may c
3c2b0 68 61 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 hange from one S
3c2c0 51 4c 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f QLite release to
3c2d0 20 74 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 the.** next. A
3c2e0 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
3c2f0 20 6f 76 65 72 72 69 64 65 20 74 68 65 20 62 75 override the bu
3c300 69 6c 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 ilt-in mutex log
3c310 69 63 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 ic must be.** pr
3c320 65 70 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d epared to accomm
3c330 6f 64 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c odate additional
3c340 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e static mutexes.
3c350 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
3c360 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 TE_MUTEX_FAST
3c370 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 0.#def
3c380 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
3c390 5f 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 _RECURSIVE
3c3a0 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 1.#define SQLI
3c3b0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
3c3c0 4d 41 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 MASTER 2.#def
3c3d0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
3c3e0 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 _STATIC_MEM
3c3f0 20 20 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 3 /* sqlite3_
3c400 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 malloc() */.#def
3c410 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ine SQLITE_MUTEX
3c420 5f 53 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 _STATIC_MEM2
3c430 20 20 34 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 4 /* NOT USED
3c440 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
3c450 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
3c460 4f 50 45 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 OPEN 4 /*
3c470 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e sqlite3BtreeOpen
3c480 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 () */.#define SQ
3c490 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
3c4a0 43 5f 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f C_PRNG 5 /
3c4b0 2a 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d * sqlite3_random
3c4c0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 () */.#define SQ
3c4d0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
3c4e0 43 5f 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f C_LRU 6 /
3c4f0 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 * lru page list
3c500 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
3c510 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
3c520 52 55 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c RU2 7 /* l
3c530 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a ru page list */.
3c540 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3c550 20 52 65 74 72 69 65 76 65 20 74 68 65 20 6d 75 Retrieve the mu
3c560 74 65 78 20 66 6f 72 20 61 20 64 61 74 61 62 61 tex for a databa
3c570 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 se connection {H
3c580 31 37 30 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 17002} <H17000>.
3c590 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
3c5a0 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 face returns a p
3c5b0 6f 69 6e 74 65 72 20 74 68 65 20 5b 73 71 6c 69 ointer the [sqli
3c5c0 74 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 te3_mutex] objec
3c5d0 74 20 74 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 t that .** seria
3c5e0 6c 69 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20 lizes access to
3c5f0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
3c600 6e 6e 65 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 nnection] given
3c610 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a in the argument.
3c620 2a 2a 20 77 68 65 6e 20 74 68 65 20 5b 74 68 72 ** when the [thr
3c630 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 eading mode] is
3c640 53 65 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 Serialized..** I
3c650 66 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 f the [threading
3c660 20 6d 6f 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 mode] is Single
3c670 2d 74 68 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 -thread or Multi
3c680 2d 74 68 72 65 61 64 20 74 68 65 6e 20 74 68 69 -thread then thi
3c690 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 s.** routine ret
3c6a0 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e urns a NULL poin
3c6b0 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ter..*/.SQLITE_A
3c6c0 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 PI sqlite3_mutex
3c6d0 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 *sqlite3_db_mut
3c6e0 65 78 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f ex(sqlite3*);../
3c6f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c *.** CAPI3REF: L
3c700 6f 77 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c ow-Level Control
3c710 20 4f 66 20 44 61 74 61 62 61 73 65 20 46 69 6c Of Database Fil
3c720 65 73 20 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 es {H11300} <S30
3c730 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 800>.**.** {H113
3c740 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 01} The [sqlite3
3c750 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d _file_control()]
3c760 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 interface makes
3c770 20 61 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 a direct call t
3c780 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f o the.** xFileCo
3c790 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 ntrol method for
3c7a0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f the [sqlite3_io
3c7b0 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 _methods] object
3c7c0 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
3c7d0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 ith a particular
3c7e0 20 64 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 database identi
3c7f0 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f fied by the seco
3c800 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 nd argument. {H1
3c810 31 33 30 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 1302} The.** nam
3c820 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
3c830 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 61 73 e is the name as
3c840 73 69 67 6e 65 64 20 74 6f 20 74 68 65 20 64 61 signed to the da
3c850 74 61 62 61 73 65 20 62 79 20 74 68 65 0a 2a 2a tabase by the.**
3c860 20 3c 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 <a href="lang_a
3c870 74 74 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 ttach.html">ATTA
3c880 43 48 3c 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 CH</a> SQL comma
3c890 6e 64 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 nd that opened t
3c8a0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 he.** database.
3c8b0 7b 48 31 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 {H11303} To cont
3c8c0 72 6f 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 rol the main dat
3c8d0 61 62 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20 abase file, use
3c8e0 74 68 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a the name "main".
3c8f0 2a 2a 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 ** or a NULL poi
3c900 6e 74 65 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 nter. {H11304} T
3c910 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 he third and fou
3c920 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 rth parameters t
3c930 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a o this routine.*
3c940 2a 20 61 72 65 20 70 61 73 73 65 64 20 64 69 72 * are passed dir
3c950 65 63 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f ectly through to
3c960 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 the second and
3c970 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 73 third parameters
3c980 20 6f 66 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 of.** the xFile
3c990 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 Control method.
3c9a0 20 7b 48 31 31 33 30 35 7d 20 54 68 65 20 72 65 {H11305} The re
3c9b0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 turn value of th
3c9c0 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a e xFileControl.*
3c9d0 2a 20 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 * method becomes
3c9e0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
3c9f0 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e e of this routin
3ca00 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 e..**.** {H11306
3ca10 7d 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 } If the second
3ca20 70 61 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 parameter (zDbNa
3ca30 6d 65 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 me) does not mat
3ca40 63 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 ch the name of a
3ca50 6e 79 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 ny.** open datab
3ca60 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 ase file, then S
3ca70 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 QLITE_ERROR is r
3ca80 65 74 75 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 eturned. {H11307
3ca90 7d 20 54 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 } This error.**
3caa0 63 6f 64 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 code is not reme
3cab0 6d 62 65 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 mbered and will
3cac0 6e 6f 74 20 62 65 20 72 65 63 61 6c 6c 65 64 20 not be recalled
3cad0 62 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 by [sqlite3_errc
3cae0 6f 64 65 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 ode()].** or [sq
3caf0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e lite3_errmsg()].
3cb00 20 7b 41 31 31 33 30 38 7d 20 54 68 65 20 75 6e {A11308} The un
3cb10 64 65 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f derlying xFileCo
3cb20 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 ntrol method mig
3cb30 68 74 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 ht.** also retur
3cb40 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 n SQLITE_ERROR.
3cb50 20 7b 41 31 31 33 30 39 7d 20 54 68 65 72 65 20 {A11309} There
3cb60 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 is no way to dis
3cb70 74 69 6e 67 75 69 73 68 20 62 65 74 77 65 65 6e tinguish between
3cb80 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 .** an incorrect
3cb90 20 7a 44 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 zDbName and an
3cba0 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 72 65 74 SQLITE_ERROR ret
3cbb0 75 72 6e 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 urn from the und
3cbc0 65 72 6c 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 erlying.** xFile
3cbd0 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 Control method.
3cbe0 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 {END}.**.** See
3cbf0 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 also: [SQLITE_FC
3cc00 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a NTL_LOCKSTATE].*
3cc10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
3cc20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f sqlite3_file_co
3cc30 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 ntrol(sqlite3*,
3cc40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e const char *zDbN
3cc50 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 ame, int op, voi
3cc60 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 d*);../*.** CAPI
3cc70 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 3REF: Testing In
3cc80 74 65 72 66 61 63 65 20 7b 48 31 31 34 30 30 7d terface {H11400}
3cc90 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S30800>.**.**
3cca0 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 The sqlite3_test
3ccb0 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 _control() inter
3ccc0 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 face is used to
3ccd0 72 65 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 read out interna
3cce0 6c 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 l.** state of SQ
3ccf0 4c 69 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 Lite and to inje
3cd00 63 74 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 ct faults into S
3cd10 51 4c 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e QLite for testin
3cd20 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 g.** purposes.
3cd30 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
3cd40 74 65 72 20 69 73 20 61 6e 20 6f 70 65 72 61 74 ter is an operat
3cd50 69 6f 6e 20 63 6f 64 65 20 74 68 61 74 20 64 65 ion code that de
3cd60 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 termines.** the
3cd70 6e 75 6d 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c number, meaning,
3cd80 20 61 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f and operation o
3cd90 66 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 f all subsequent
3cda0 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a parameters..**.
3cdb0 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 ** This interfac
3cdc0 65 20 69 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 e is not for use
3cdd0 20 62 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 by applications
3cde0 2e 20 20 49 74 20 65 78 69 73 74 73 20 73 6f 6c . It exists sol
3cdf0 65 6c 79 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 ely.** for verif
3ce00 79 69 6e 67 20 74 68 65 20 63 6f 72 72 65 63 74 ying the correct
3ce10 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 operation of th
3ce20 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
3ce30 2e 20 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 . Depending.**
3ce40 6f 6e 20 68 6f 77 20 74 68 65 20 53 51 4c 69 74 on how the SQLit
3ce50 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d e library is com
3ce60 70 69 6c 65 64 2c 20 74 68 69 73 20 69 6e 74 65 piled, this inte
3ce70 72 66 61 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 rface might not
3ce80 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 exist..**.** The
3ce90 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 details of the
3cea0 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c operation codes,
3ceb0 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c their meanings,
3cec0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 0a the parameters.
3ced0 2a 2a 20 74 68 65 79 20 74 61 6b 65 2c 20 61 6e ** they take, an
3cee0 64 20 77 68 61 74 20 74 68 65 79 20 64 6f 20 61 d what they do a
3cef0 72 65 20 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 re all subject t
3cf00 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 o change without
3cf10 20 6e 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 notice..** Unli
3cf20 6b 65 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 ke most of the S
3cf30 51 4c 69 74 65 20 41 50 49 2c 20 74 68 69 73 20 QLite API, this
3cf40 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 function is not
3cf50 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a guaranteed to.**
3cf60 20 6f 70 65 72 61 74 65 20 63 6f 6e 73 69 73 74 operate consist
3cf70 65 6e 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 ently from one r
3cf80 65 6c 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 elease to the ne
3cf90 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 xt..*/.SQLITE_AP
3cfa0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 I int sqlite3_te
3cfb0 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f st_control(int o
3cfc0 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 p, ...);../*.**
3cfd0 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e CAPI3REF: Testin
3cfe0 67 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72 g Interface Oper
3cff0 61 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 ation Codes {H11
3d000 34 31 30 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 410} <H11400>.**
3d010 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 .** These consta
3d020 6e 74 73 20 61 72 65 20 74 68 65 20 76 61 6c 69 nts are the vali
3d030 64 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 d operation code
3d040 20 70 61 72 61 6d 65 74 65 72 73 20 75 73 65 64 parameters used
3d050 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 .** as the first
3d060 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 argument to [sq
3d070 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 lite3_test_contr
3d080 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ol()]..**.** The
3d090 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e se parameters an
3d0a0 64 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 d their meanings
3d0b0 20 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 are subject to
3d0c0 63 68 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 change.** withou
3d0d0 74 20 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65 t notice. These
3d0e0 20 76 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20 values are for
3d0f0 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 testing purposes
3d100 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 only..** Applic
3d110 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f ations should no
3d120 74 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 t use any of the
3d130 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 se parameters or
3d140 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
3d150 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d _test_control()]
3d160 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 interface..*/.#
3d170 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
3d180 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 STCTRL_PRNG_SAVE
3d190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3d1a0 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5.#define SQLITE
3d1b0 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 _TESTCTRL_PRNG_R
3d1c0 45 53 54 4f 52 45 20 20 20 20 20 20 20 20 20 20 ESTORE
3d1d0 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 6.#define SQL
3d1e0 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e ITE_TESTCTRL_PRN
3d1f0 47 5f 52 45 53 45 54 20 20 20 20 20 20 20 20 20 G_RESET
3d200 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 7.#define
3d210 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
3d220 42 49 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 BITVEC_TEST
3d230 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 8.#defi
3d240 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
3d250 52 4c 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c RL_FAULT_INSTALL
3d260 20 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 9.#d
3d270 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
3d280 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c TCTRL_BENIGN_MAL
3d290 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 LOC_HOOKS 10
3d2a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3d2b0 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 TESTCTRL_PENDING
3d2c0 5f 42 59 54 45 20 20 20 20 20 20 20 20 20 20 20 _BYTE
3d2d0 20 31 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 11.#define SQLI
3d2e0 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 TE_TESTCTRL_ASSE
3d2f0 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 RT
3d300 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 53 12.#define S
3d310 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 QLITE_TESTCTRL_A
3d320 4c 57 41 59 53 20 20 20 20 20 20 20 20 20 20 20 LWAYS
3d330 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 13.#defin
3d340 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 e SQLITE_TESTCTR
3d350 4c 5f 52 45 53 45 52 56 45 20 20 20 20 20 20 20 L_RESERVE
3d360 20 20 20 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 14../*
3d370 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 .** CAPI3REF: SQ
3d380 4c 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 Lite Runtime Sta
3d390 74 75 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 tus {H17200} <S6
3d3a0 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0200>.** EXPERIM
3d3b0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ENTAL.**.** This
3d3c0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 interface is us
3d3d0 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 ed to retrieve r
3d3e0 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e untime status in
3d3f0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f formation.** abo
3d400 75 74 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e ut the preforman
3d410 63 65 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e ce of SQLite, an
3d420 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 d optionally to
3d430 72 65 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a reset various.**
3d440 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 highwater marks
3d450 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 . The first arg
3d460 75 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 ument is an inte
3d470 67 65 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 ger code for.**
3d480 74 68 65 20 73 70 65 63 69 66 69 63 20 70 61 72 the specific par
3d490 61 6d 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 ameter to measur
3d4a0 65 2e 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 e. Recognized i
3d4b0 6e 74 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 nteger codes.**
3d4c0 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 are of the form
3d4d0 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d [SQLITE_STATUS_M
3d4e0 45 4d 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c EMORY_USED | SQL
3d4f0 49 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e ITE_STATUS_...].
3d500 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 .** The current
3d510 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 value of the par
3d520 61 6d 65 74 65 72 20 69 73 20 72 65 74 75 72 6e ameter is return
3d530 65 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e ed into *pCurren
3d540 74 2e 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 t..** The highes
3d550 74 20 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 t recorded value
3d560 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 is returned in
3d570 2a 70 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 *pHighwater. If
3d580 20 74 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 the.** resetFla
3d590 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 g is true, then
3d5a0 74 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f the highest reco
3d5b0 72 64 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 rd value is rese
3d5c0 74 20 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 t after.** *pHig
3d5d0 68 77 61 74 65 72 20 69 73 20 77 72 69 74 74 65 hwater is writte
3d5e0 6e 2e 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 n. Some paramete
3d5f0 72 73 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 rs do not record
3d600 20 74 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 the highest.**
3d610 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 value. For thos
3d620 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 e parameters.**
3d630 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 nothing is writt
3d640 65 6e 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 en into *pHighwa
3d650 74 65 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 ter and the rese
3d660 74 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 tFlag is ignored
3d670 2e 0a 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d ..** Other param
3d680 65 74 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c eters record onl
3d690 79 20 74 68 65 20 68 69 67 68 77 61 74 65 72 20 y the highwater
3d6a0 6d 61 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 mark and not the
3d6b0 20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 current.** valu
3d6c0 65 2e 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 e. For these la
3d6d0 74 74 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 tter parameters
3d6e0 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 nothing is writt
3d6f0 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e en into *pCurren
3d700 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f t..**.** This ro
3d710 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 utine returns SQ
3d720 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
3d730 73 73 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 ss and a non-zer
3d740 6f 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 o.** [error code
3d750 5d 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a ] on failure..**
3d760 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
3d770 20 69 73 20 74 68 72 65 61 64 73 61 66 65 20 62 is threadsafe b
3d780 75 74 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 ut is not atomic
3d790 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
3d7a0 63 61 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 can be.** called
3d7b0 20 77 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 while other thr
3d7c0 65 61 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 eads are running
3d7d0 20 74 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 the same or dif
3d7e0 66 65 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a ferent SQLite.**
3d7f0 20 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f interfaces. Ho
3d800 77 65 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 wever the values
3d810 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 returned in *pC
3d820 75 72 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 urrent and.** *p
3d830 48 69 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 Highwater reflec
3d840 74 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 t the status of
3d850 53 51 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 SQLite at differ
3d860 65 6e 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 ent points in ti
3d870 6d 65 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 me.** and it is
3d880 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e possible that an
3d890 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 other thread mig
3d8a0 68 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 ht change the pa
3d8b0 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 rameter.** in be
3d8c0 74 77 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 tween the times
3d8d0 77 68 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 when *pCurrent a
3d8e0 6e 64 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 nd *pHighwater a
3d8f0 72 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a re written..**.*
3d900 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c * See also: [sql
3d910 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 ite3_db_status()
3d920 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
3d930 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
3d940 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TAL int sqlite3_
3d950 73 74 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 status(int op, i
3d960 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e nt *pCurrent, in
3d970 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 t *pHighwater, i
3d980 6e 74 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a nt resetFlag);..
3d990 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3d9a0 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 Status Paramete
3d9b0 72 73 20 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 rs {H17250} <H17
3d9c0 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 200>.** EXPERIME
3d9d0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 NTAL.**.** These
3d9e0 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e integer constan
3d9f0 74 73 20 64 65 73 69 67 6e 61 74 65 20 76 61 72 ts designate var
3da00 69 6f 75 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 ious run-time st
3da10 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 73 0a atus parameters.
3da20 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 72 ** that can be r
3da30 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 eturned by [sqli
3da40 74 65 33 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a te3_status()]..*
3da50 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 *.** <dl>.** <dt
3da60 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d >SQLITE_STATUS_M
3da70 45 4d 4f 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a EMORY_USED</dt>.
3da80 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
3da90 6d 65 74 65 72 20 69 73 20 74 68 65 20 63 75 72 meter is the cur
3daa0 72 65 6e 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d rent amount of m
3dab0 65 6d 6f 72 79 20 63 68 65 63 6b 65 64 20 6f 75 emory checked ou
3dac0 74 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 t.** using [sqli
3dad0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 te3_malloc()], e
3dae0 69 74 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f ither directly o
3daf0 72 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 r indirectly. T
3db00 68 65 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 he.** figure inc
3db10 6c 75 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 ludes calls made
3db20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c to [sqlite3_mal
3db30 6c 6f 63 28 29 5d 20 62 79 20 74 68 65 20 61 70 loc()] by the ap
3db40 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 plication.** and
3db50 20 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 internal memory
3db60 20 75 73 61 67 65 20 62 79 20 74 68 65 20 53 51 usage by the SQ
3db70 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53 Lite library. S
3db80 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a cratch memory.**
3db90 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b controlled by [
3dba0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 SQLITE_CONFIG_SC
3dbb0 52 41 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c RATCH] and auxil
3dbc0 69 61 72 79 20 70 61 67 65 2d 63 61 63 68 65 0a iary page-cache.
3dbd0 2a 2a 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f ** memory contro
3dbe0 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f lled by [SQLITE_
3dbf0 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
3dc00 5d 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 ] is not include
3dc10 64 20 69 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 d in.** this par
3dc20 61 6d 65 74 65 72 2e 20 20 54 68 65 20 61 6d 6f ameter. The amo
3dc30 75 6e 74 20 72 65 74 75 72 6e 65 64 20 69 73 20 unt returned is
3dc40 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 61 the sum of the a
3dc50 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a llocation.** siz
3dc60 65 73 20 61 73 20 72 65 70 6f 72 74 65 64 20 62 es as reported b
3dc70 79 20 74 68 65 20 78 53 69 7a 65 20 6d 65 74 68 y the xSize meth
3dc80 6f 64 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d od in [sqlite3_m
3dc90 65 6d 5f 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 em_methods].</dd
3dca0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
3dcb0 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 TE_STATUS_MALLOC
3dcc0 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 _SIZE</dt>.** <d
3dcd0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
3dce0 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 records the lar
3dcf0 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f gest memory allo
3dd00 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a cation request.*
3dd10 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c * handed to [sql
3dd20 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f ite3_malloc()] o
3dd30 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c r [sqlite3_reall
3dd40 6f 63 28 29 5d 20 28 6f 72 20 74 68 65 69 72 0a oc()] (or their.
3dd50 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 ** internal equi
3dd60 76 61 6c 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 valents). Only
3dd70 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
3dd80 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 ed in the.** *pH
3dd90 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 ighwater paramet
3dda0 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 er to [sqlite3_s
3ddb0 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 tatus()] is of i
3ddc0 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 nterest. .** Th
3ddd0 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 e value written
3dde0 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 into the *pCurre
3ddf0 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 nt parameter is
3de00 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a undefined.</dd>.
3de10 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
3de20 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
3de30 45 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c E_USED</dt>.** <
3de40 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
3de50 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 r returns the nu
3de60 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 75 73 mber of pages us
3de70 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a ed out of the.**
3de80 20 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f [pagecache memo
3de90 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 ry allocator] th
3dea0 61 74 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 at was configure
3deb0 64 20 75 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c d using .** [SQL
3dec0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
3ded0 41 43 48 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 ACHE]. The.** v
3dee0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
3def0 20 69 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 in pages, not i
3df00 6e 20 62 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a n bytes.</dd>.**
3df10 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3df20 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
3df30 4f 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a OVERFLOW</dt>.**
3df40 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3df50 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 ter returns the
3df60 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
3df70 6f 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a of page cache.**
3df80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 allocation whic
3df90 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 h could not be s
3dfa0 74 61 74 69 73 66 69 65 64 20 62 79 20 74 68 65 tatisfied by the
3dfb0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
3dfc0 50 41 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 PAGECACHE].** bu
3dfd0 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 ffer and where f
3dfe0 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f orced to overflo
3dff0 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 w to [sqlite3_ma
3e000 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a lloc()]. The.**
3e010 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 returned value
3e020 69 6e 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 includes allocat
3e030 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c ions that overfl
3e040 6f 77 65 64 20 62 65 63 61 75 73 65 20 74 68 65 owed because the
3e050 79 0a 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c y.** where too l
3e060 61 72 67 65 20 28 74 68 65 79 20 77 65 72 65 20 arge (they were
3e070 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
3e080 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 "sz" parameter t
3e090 6f 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e o.** [SQLITE_CON
3e0a0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20 FIG_PAGECACHE])
3e0b0 61 6e 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 and allocations
3e0c0 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 that overflowed
3e0d0 62 65 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 because.** no sp
3e0e0 61 63 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20 ace was left in
3e0f0 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c the page cache.<
3e100 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
3e110 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
3e120 45 43 41 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e ECACHE_SIZE</dt>
3e130 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 .** <dd>This par
3e140 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 ameter records t
3e150 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 he largest memor
3e160 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
3e170 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 uest.** handed t
3e180 6f 20 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d o [pagecache mem
3e190 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 ory allocator].
3e1a0 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 Only the value
3e1b0 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a returned in the.
3e1c0 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 ** *pHighwater p
3e1d0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
3e1e0 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 ite3_status()] i
3e1f0 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 s of interest.
3e200 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 .** The value wr
3e210 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a itten into the *
3e220 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 pCurrent paramet
3e230 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e er is undefined.
3e240 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
3e250 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 SQLITE_STATUS_SC
3e260 52 41 54 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a RATCH_USED</dt>.
3e270 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
3e280 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 meter returns th
3e290 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f e number of allo
3e2a0 63 61 74 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 cations used out
3e2b0 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 of the.** [scra
3e2c0 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 tch memory alloc
3e2d0 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 ator] configured
3e2e0 20 75 73 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 using.** [SQLIT
3e2f0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 E_CONFIG_SCRATCH
3e300 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 ]. The value re
3e310 74 75 72 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c turned is in all
3e320 6f 63 61 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a ocations, not.**
3e330 20 69 6e 20 62 79 74 65 73 2e 20 20 53 69 6e 63 in bytes. Sinc
3e340 65 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 e a single threa
3e350 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 d may only have
3e360 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f one scratch allo
3e370 63 61 74 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 cation.** outsta
3e380 6e 64 69 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 nding at time, t
3e390 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 61 6c his parameter al
3e3a0 73 6f 20 72 65 70 6f 72 74 73 20 74 68 65 20 6e so reports the n
3e3b0 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 umber of threads
3e3c0 0a 2a 2a 20 75 73 69 6e 67 20 73 63 72 61 74 63 .** using scratc
3e3d0 68 20 6d 65 6d 6f 72 79 20 61 74 20 74 68 65 20 h memory at the
3e3e0 73 61 6d 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a same time.</dd>.
3e3f0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
3e400 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
3e410 4f 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a OVERFLOW</dt>.**
3e420 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3e430 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 ter returns the
3e440 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
3e450 6f 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 of scratch memor
3e460 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y.** allocation
3e470 77 68 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 which could not
3e480 62 65 20 73 74 61 74 69 73 66 69 65 64 20 62 79 be statisfied by
3e490 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e the [SQLITE_CON
3e4a0 46 49 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 FIG_SCRATCH].**
3e4b0 62 75 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 buffer and where
3e4c0 20 66 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 forced to overf
3e4d0 6c 6f 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f low to [sqlite3_
3e4e0 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 malloc()]. The
3e4f0 76 61 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e values.** return
3e500 65 64 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 ed include overf
3e510 6c 6f 77 73 20 62 65 63 61 75 73 65 20 74 68 65 lows because the
3e520 20 72 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 requested alloc
3e530 61 74 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a ation was too.**
3e540 20 6c 61 72 67 65 72 20 28 74 68 61 74 20 69 73 larger (that is
3e550 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65 , because the re
3e560 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 quested allocati
3e570 6f 6e 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 on was larger th
3e580 61 6e 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 an the.** "sz" p
3e590 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c arameter to [SQL
3e5a0 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 ITE_CONFIG_SCRAT
3e5b0 43 48 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 CH]) and because
3e5c0 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 no scratch buff
3e5d0 65 72 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 er.** slots were
3e5e0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c available..** <
3e5f0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
3e600 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
3e610 41 54 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a ATCH_SIZE</dt>.*
3e620 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
3e630 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 eter records the
3e640 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 largest memory
3e650 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 allocation reque
3e660 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 st.** handed to
3e670 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 [scratch memory
3e680 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c allocator]. Onl
3e690 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 y the value retu
3e6a0 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a rned in the.** *
3e6b0 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61 6d pHighwater param
3e6c0 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 eter to [sqlite3
3e6d0 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 _status()] is of
3e6e0 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 interest. .**
3e6f0 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 The value writte
3e700 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 n into the *pCur
3e710 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 rent parameter i
3e720 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 s undefined.</dd
3e730 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
3e740 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 TE_STATUS_PARSER
3e750 5f 53 54 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c _STACK</dt>.** <
3e760 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 dd>This paramete
3e770 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 64 65 r records the de
3e780 65 70 65 73 74 20 70 61 72 73 65 72 20 73 74 61 epest parser sta
3e790 63 6b 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a ck. It is only.
3e7a0 2a 2a 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 ** meaningful if
3e7b0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
3e7c0 6c 65 64 20 77 69 74 68 20 5b 59 59 54 52 41 43 led with [YYTRAC
3e7d0 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 5d 2e KMAXSTACKDEPTH].
3e7e0 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a </dd>.** </dl>.*
3e7f0 2a 0a 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20 *.** New status
3e800 70 61 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62 parameters may b
3e810 65 20 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d e added from tim
3e820 65 20 74 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 e to time..*/.#d
3e830 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 efine SQLITE_STA
3e840 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 TUS_MEMORY_USED
3e850 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 0.#defi
3e860 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ne SQLITE_STATUS
3e870 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 20 _PAGECACHE_USED
3e880 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
3e890 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
3e8a0 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 GECACHE_OVERFLOW
3e8b0 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 2.#define SQL
3e8c0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
3e8d0 43 48 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 CH_USED
3e8e0 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 3.#define SQLITE
3e8f0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
3e900 4f 56 45 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 OVERFLOW 4.#
3e910 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3e920 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 ATUS_MALLOC_SIZE
3e930 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 5.#def
3e940 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
3e950 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 20 20 S_PARSER_STACK
3e960 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 6.#define
3e970 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 SQLITE_STATUS_P
3e980 41 47 45 43 41 43 48 45 5f 53 49 5a 45 20 20 20 AGECACHE_SIZE
3e990 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 7.#define SQ
3e9a0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
3e9b0 54 43 48 5f 53 49 5a 45 20 20 20 20 20 20 20 20 TCH_SIZE
3e9c0 20 38 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 8../*.** CAPI3R
3e9d0 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e EF: Database Con
3e9e0 6e 65 63 74 69 6f 6e 20 53 74 61 74 75 73 20 7b nection Status {
3e9f0 48 31 37 35 30 30 7d 20 3c 53 36 30 32 30 30 3e H17500} <S60200>
3ea00 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
3ea10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
3ea20 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f rface is used to
3ea30 20 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d retrieve runtim
3ea40 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 e status informa
3ea50 74 69 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 tion .** about a
3ea60 20 73 69 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 single [databas
3ea70 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 e connection].
3ea80 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
3ea90 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 nt is the.** dat
3eaa0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3eab0 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e object to be in
3eac0 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54 68 65 terrogated. The
3ead0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
3eae0 0a 2a 2a 20 69 73 20 74 68 65 20 70 61 72 61 6d .** is the param
3eaf0 65 74 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 eter to interrog
3eb00 61 74 65 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c ate. Currently,
3eb10 20 74 68 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 the only allowe
3eb20 64 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 d value.** for t
3eb30 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
3eb40 74 65 72 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 ter is [SQLITE_D
3eb50 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 BSTATUS_LOOKASID
3eb60 45 5f 55 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 E_USED]..** Addi
3eb70 74 69 6f 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 tional options w
3eb80 69 6c 6c 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 ill likely appea
3eb90 72 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 r in future rele
3eba0 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a ases of SQLite..
3ebb0 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e **.** The curren
3ebc0 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 t value of the r
3ebd0 65 71 75 65 73 74 65 64 20 70 61 72 61 6d 65 74 equested paramet
3ebe0 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e er is written in
3ebf0 74 6f 20 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 to *pCur.** and
3ec00 74 68 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 the highest inst
3ec10 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 antaneous value
3ec20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
3ec30 2a 70 48 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 *pHiwtr. If.**
3ec40 74 68 65 20 72 65 73 65 74 46 6c 67 20 69 73 20 the resetFlg is
3ec50 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 true, then the h
3ec60 69 67 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e ighest instantan
3ec70 65 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a eous value is.**
3ec80 20 72 65 73 65 74 20 62 61 63 6b 20 64 6f 77 6e reset back down
3ec90 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 to the current
3eca0 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 value..**.** See
3ecb0 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
3ecc0 73 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 status()] and [s
3ecd0 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 qlite3_stmt_stat
3ece0 75 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 us()]..*/.SQLITE
3ecf0 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
3ed00 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c RIMENTAL int sql
3ed10 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 ite3_db_status(s
3ed20 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c qlite3*, int op,
3ed30 20 69 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 int *pCur, int
3ed40 2a 70 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 *pHiwtr, int res
3ed50 65 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 etFlg);../*.** C
3ed60 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 API3REF: Status
3ed70 50 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 Parameters for d
3ed80 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
3ed90 6f 6e 73 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 ons {H17520} <H1
3eda0 37 35 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 7500>.** EXPERIM
3edb0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ENTAL.**.** Thes
3edc0 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 e constants are
3edd0 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e the available in
3ede0 74 65 67 65 72 20 22 76 65 72 62 73 22 20 74 68 teger "verbs" th
3edf0 61 74 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 at can be passed
3ee00 20 61 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e as.** the secon
3ee10 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 d argument to th
3ee20 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 e [sqlite3_db_st
3ee30 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63 atus()] interfac
3ee40 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 e..**.** New ver
3ee50 62 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 bs may be added
3ee60 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 in future releas
3ee70 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 es of SQLite. Ex
3ee80 69 73 74 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 isting verbs.**
3ee90 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 might be discont
3eea0 69 6e 75 65 64 2e 20 41 70 70 6c 69 63 61 74 69 inued. Applicati
3eeb0 6f 6e 73 20 73 68 6f 75 6c 64 20 63 68 65 63 6b ons should check
3eec0 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 the return code
3eed0 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 from.** [sqlite
3eee0 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20 74 3_db_status()] t
3eef0 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
3ef00 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 the call worked
3ef10 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 ..** The [sqlite
3ef20 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20 69 3_db_status()] i
3ef30 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 nterface will re
3ef40 74 75 72 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 turn a non-zero
3ef50 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 error code.** if
3ef60 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 a discontinued
3ef70 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76 or unsupported v
3ef80 65 72 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a erb is invoked..
3ef90 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 **.** <dl>.** <d
3efa0 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 t>SQLITE_DBSTATU
3efb0 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 S_LOOKASIDE_USED
3efc0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
3efd0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 s parameter retu
3efe0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
3eff0 66 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f f lookaside memo
3f000 72 79 20 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 ry slots current
3f010 6c 79 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 ly.** checked ou
3f020 74 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e t.</dd>.** </dl>
3f030 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
3f040 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b TE_DBSTATUS_LOOK
3f050 41 53 49 44 45 5f 55 53 45 44 20 20 20 20 20 30 ASIDE_USED 0
3f060 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 .../*.** CAPI3RE
3f070 46 3a 20 50 72 65 70 61 72 65 64 20 53 74 61 74 F: Prepared Stat
3f080 65 6d 65 6e 74 20 53 74 61 74 75 73 20 7b 48 31 ement Status {H1
3f090 37 35 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 7550} <S60200>.*
3f0a0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
3f0b0 2a 0a 2a 2a 20 45 61 63 68 20 70 72 65 70 61 72 *.** Each prepar
3f0c0 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 ed statement mai
3f0d0 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a ntains various.*
3f0e0 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 * [SQLITE_STMTST
3f0f0 41 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e ATUS_SORT | coun
3f100 74 65 72 73 5d 20 74 68 61 74 20 6d 65 61 73 75 ters] that measu
3f110 72 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a re the number.**
3f120 20 6f 66 20 74 69 6d 65 73 20 69 74 20 68 61 73 of times it has
3f130 20 70 65 72 66 6f 72 6d 65 64 20 73 70 65 63 69 performed speci
3f140 66 69 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 fic operations.
3f150 20 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20 These counters
3f160 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 can.** be used t
3f170 6f 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 o monitor the pe
3f180 72 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63 rformance charac
3f190 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 teristics of the
3f1a0 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 prepared.** sta
3f1b0 74 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 tements. For ex
3f1c0 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 ample, if the nu
3f1d0 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73 74 mber of table st
3f1e0 65 70 73 20 67 72 65 61 74 6c 79 20 65 78 63 65 eps greatly exce
3f1f0 65 64 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 eds.** the numbe
3f200 72 20 6f 66 20 74 61 62 6c 65 20 73 65 61 72 63 r of table searc
3f210 68 65 73 20 6f 72 20 72 65 73 75 6c 74 20 72 6f hes or result ro
3f220 77 73 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 74 ws, that would t
3f230 65 6e 64 20 74 6f 20 69 6e 64 69 63 61 74 65 0a end to indicate.
3f240 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 65 70 ** that the prep
3f250 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 ared statement i
3f260 73 20 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 s using a full t
3f270 61 62 6c 65 20 73 63 61 6e 20 72 61 74 68 65 72 able scan rather
3f280 20 74 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 than.** an inde
3f290 78 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 x. .**.** This
3f2a0 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 interface is use
3f2b0 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e d to retrieve an
3f2c0 64 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 d reset counter
3f2d0 76 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 values from.** a
3f2e0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
3f2f0 6d 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 ment]. The firs
3f300 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 t argument is th
3f310 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 e prepared state
3f320 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 ment.** object t
3f330 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 o be interrogate
3f340 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 d. The second a
3f350 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e rgument.** is an
3f360 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f integer code fo
3f370 72 20 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 r a specific [SQ
3f380 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f LITE_STMTSTATUS_
3f390 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a SORT | counter].
3f3a0 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f ** to be interro
3f3b0 67 61 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 gated. .** The c
3f3c0 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 urrent value of
3f3d0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f the requested co
3f3e0 75 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 unter is returne
3f3f0 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 d..** If the res
3f400 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 etFlg is true, t
3f410 68 65 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 20 hen the counter
3f420 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f is reset to zero
3f430 20 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 after this.** i
3f440 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20 72 65 nterface call re
3f450 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 turns..**.** See
3f460 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
3f470 73 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 status()] and [s
3f480 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 qlite3_db_status
3f490 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ()]..*/.SQLITE_A
3f4a0 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 PI SQLITE_EXPERI
3f4b0 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 MENTAL int sqlit
3f4c0 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 e3_stmt_status(s
3f4d0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
3f4e0 74 20 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 6c t op,int resetFl
3f4f0 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 g);../*.** CAPI3
3f500 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 REF: Status Para
3f510 6d 65 74 65 72 73 20 66 6f 72 20 70 72 65 70 61 meters for prepa
3f520 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 7b red statements {
3f530 48 31 37 35 37 30 7d 20 3c 48 31 37 35 35 30 3e H17570} <H17550>
3f540 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
3f550 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 .**.** These pre
3f560 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 processor macros
3f570 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 define integer
3f580 63 6f 64 65 73 20 74 68 61 74 20 6e 61 6d 65 20 codes that name
3f590 63 6f 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 counter.** value
3f5a0 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
3f5b0 68 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 h the [sqlite3_s
3f5c0 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 20 69 6e tmt_status()] in
3f5d0 74 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 terface..** The
3f5e0 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 meanings of the
3f5f0 76 61 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 various counters
3f600 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a are as follows:
3f610 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c .**.** <dl>.** <
3f620 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 dt>SQLITE_STMTST
3f630 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 ATUS_FULLSCAN_ST
3f640 45 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 EP</dt>.** <dd>T
3f650 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 his is the numbe
3f660 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 r of times that
3f670 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 70 SQLite has stepp
3f680 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a ed forward in.**
3f690 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 a table as part
3f6a0 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 of a full table
3f6b0 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 scan. Large nu
3f6c0 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20 63 mbers for this c
3f6d0 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e ounter.** may in
3f6e0 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 dicate opportuni
3f6f0 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d ties for perform
3f700 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 ance improvement
3f710 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 through .** car
3f720 65 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 eful use of indi
3f730 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ces.</dd>.**.**
3f740 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 <dt>SQLITE_STMTS
3f750 54 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a TATUS_SORT</dt>.
3f760 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 20 74 ** <dd>This is t
3f770 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 he number of sor
3f780 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 t operations tha
3f790 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e t have occurred.
3f7a0 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 .** A non-zero v
3f7b0 61 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75 alue in this cou
3f7c0 6e 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74 nter may indicat
3f7d0 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 e an opportunity
3f7e0 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 to.** improveme
3f7f0 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 nt performance t
3f800 68 72 6f 75 67 68 20 63 61 72 65 66 75 6c 20 75 hrough careful u
3f810 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f se of indices.</
3f820 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a dd>.**.** </dl>.
3f830 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
3f840 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c E_STMTSTATUS_FUL
3f850 4c 53 43 41 4e 5f 53 54 45 50 20 20 20 20 20 31 LSCAN_STEP 1
3f860 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3f870 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 STMTSTATUS_SORT
3f880 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2..
3f890 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3f8a0 43 75 73 74 6f 6d 20 50 61 67 65 20 43 61 63 68 Custom Page Cach
3f8b0 65 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 e Object.** EXPE
3f8c0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
3f8d0 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
3f8e0 65 20 74 79 70 65 20 69 73 20 6f 70 61 71 75 65 e type is opaque
3f8f0 2e 20 20 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 . It is impleme
3f900 6e 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 nted by.** the p
3f910 6c 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e luggable module.
3f920 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 The SQLite cor
3f930 65 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 e has no knowled
3f940 67 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a ge of.** its siz
3f950 65 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 e or internal st
3f960 72 75 63 74 75 72 65 20 61 6e 64 20 6e 65 76 65 ructure and neve
3f970 72 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 r deals with the
3f980 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 .** sqlite3_pcac
3f990 68 65 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74 he object except
3f9a0 20 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 by holding and
3f9b0 70 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 passing pointers
3f9c0 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 .** to the objec
3f9d0 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 t..**.** See [sq
3f9e0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
3f9f0 68 6f 64 73 5d 20 66 6f 72 20 61 64 64 69 74 69 hods] for additi
3fa00 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
3fa10 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
3fa20 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 uct sqlite3_pcac
3fa30 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
3fa40 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 e;../*.** CAPI3R
3fa50 45 46 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 EF: Application
3fa60 44 65 66 69 6e 65 64 20 50 61 67 65 20 43 61 63 Defined Page Cac
3fa70 68 65 2e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a he..** KEYWORDS:
3fa80 20 7b 70 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a {page cache}.**
3fa90 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
3faa0 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
3fab0 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 _config]([SQLITE
3fac0 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c _CONFIG_PCACHE],
3fad0 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 20 ...) interface
3fae0 63 61 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 can.** register
3faf0 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 an alternative p
3fb00 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d age cache implem
3fb10 65 6e 74 61 74 69 6f 6e 20 62 79 20 70 61 73 73 entation by pass
3fb20 69 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e ing in an .** in
3fb30 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 stance of the sq
3fb40 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
3fb50 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 2e 20 hods structure.
3fb60 54 68 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 The majority of
3fb70 74 68 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d the .** heap mem
3fb80 6f 72 79 20 75 73 65 64 20 62 79 20 53 51 4c 69 ory used by SQLi
3fb90 74 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 te is used by th
3fba0 65 20 70 61 67 65 20 63 61 63 68 65 20 74 6f 20 e page cache to
3fbb0 63 61 63 68 65 20 64 61 74 61 20 72 65 61 64 20 cache data read
3fbc0 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 .** from, or rea
3fbd0 64 79 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e dy to be written
3fbe0 20 74 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73 to, the databas
3fbf0 65 20 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 e file. By imple
3fc00 6d 65 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 menting a .** cu
3fc10 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20 stom page cache
3fc20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 2c 20 using this API,
3fc30 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 an application c
3fc40 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 an control more
3fc50 0a 2a 2a 20 70 72 65 63 69 73 65 6c 79 20 74 68 .** precisely th
3fc60 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f e amount of memo
3fc70 72 79 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 ry consumed by S
3fc80 51 4c 69 74 65 2c 20 74 68 65 20 77 61 79 20 69 QLite, the way i
3fc90 6e 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 n which .** that
3fca0 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 memory is alloc
3fcb0 61 74 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 ated and release
3fcc0 64 2c 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 d, and the polic
3fcd0 69 65 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 ies used to .**
3fce0 64 65 74 65 72 6d 69 6e 65 20 65 78 61 63 74 6c determine exactl
3fcf0 79 20 77 68 69 63 68 20 70 61 72 74 73 20 6f 66 y which parts of
3fd00 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 a database file
3fd10 20 61 72 65 20 63 61 63 68 65 64 20 61 6e 64 20 are cached and
3fd20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 for .** how long
3fd30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 ..**.** The cont
3fd40 65 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 ents of the sqli
3fd50 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
3fd60 64 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 ds structure are
3fd70 20 63 6f 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a copied to an.**
3fd80 20 69 6e 74 65 72 6e 61 6c 20 62 75 66 66 65 72 internal buffer
3fd90 20 62 79 20 53 51 4c 69 74 65 20 77 69 74 68 69 by SQLite withi
3fda0 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 n the call to [s
3fdb0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 qlite3_config].
3fdc0 20 48 65 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 Hence.** the ap
3fdd0 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 plication may di
3fde0 73 63 61 72 64 20 74 68 65 20 70 61 72 61 6d 65 scard the parame
3fdf0 74 65 72 20 61 66 74 65 72 20 74 68 65 20 63 61 ter after the ca
3fe00 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 ll to.** [sqlite
3fe10 33 5f 63 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 3_config()] retu
3fe20 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 rns..**.** The x
3fe30 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73 Init() method is
3fe40 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 called once for
3fe50 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 each call to [s
3fe60 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
3fe70 65 28 29 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 e()].** (usually
3fe80 20 6f 6e 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e only once durin
3fe90 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f g the lifetime o
3fea0 66 20 74 68 65 20 70 72 6f 63 65 73 73 29 2e 20 f the process).
3feb0 49 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 It is passed.**
3fec0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 71 a copy of the sq
3fed0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
3fee0 68 6f 64 73 2e 70 41 72 67 20 76 61 6c 75 65 2e hods.pArg value.
3fef0 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 It can be used
3ff00 74 6f 20 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f to set.** up glo
3ff10 62 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 61 bal structures a
3ff20 6e 64 20 6d 75 74 65 78 65 73 20 72 65 71 75 69 nd mutexes requi
3ff30 72 65 64 20 62 79 20 74 68 65 20 63 75 73 74 6f red by the custo
3ff40 6d 20 70 61 67 65 20 63 61 63 68 65 20 0a 2a 2a m page cache .**
3ff50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e implementation.
3ff60 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 .**.** The xShu
3ff70 74 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 tdown() method i
3ff80 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 s called from wi
3ff90 74 68 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 thin [sqlite3_sh
3ffa0 75 74 64 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 utdown()], .** i
3ffb0 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f f the applicatio
3ffc0 6e 20 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 n invokes this A
3ffd0 50 49 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 PI. It can be us
3ffe0 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a ed to clean up .
3fff0 2a 2a 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 ** any outstandi
40000 6e 67 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 ng resources bef
40010 6f 72 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 ore process shut
40020 64 6f 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 down, if require
40030 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 d..**.** SQLite
40040 68 6f 6c 64 73 20 61 20 5b 53 51 4c 49 54 45 5f holds a [SQLITE_
40050 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 5d MUTEX_RECURSIVE]
40060 20 6d 75 74 65 78 20 77 68 65 6e 20 69 74 20 69 mutex when it i
40070 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 nvokes.** the xI
40080 6e 69 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 nit method, so t
40090 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 he xInit method
400a0 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 need not be thre
400b0 61 64 73 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 adsafe. The.**
400c0 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 xShutdown method
400d0 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 is only called
400e0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 from [sqlite3_sh
400f0 75 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 utdown()] so it
40100 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 does.** not need
40110 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 66 to be threadsaf
40120 65 20 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f e either. All o
40130 74 68 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 ther methods mus
40140 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65 0a t be threadsafe.
40150 2a 2a 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 ** in multithrea
40160 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 ded applications
40170 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 ..**.** SQLite w
40180 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 ill never invoke
40190 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 xInit() more th
401a0 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 an once without
401b0 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a an intervening.*
401c0 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 * call to xShutd
401d0 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 own()..**.** The
401e0 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f xCreate() metho
401f0 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e d is used to con
40200 73 74 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 struct a new cac
40210 68 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 he instance. SQ
40220 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 Lite.** will typ
40230 69 63 61 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e ically create on
40240 65 20 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 e cache instance
40250 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 64 for each open d
40260 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a atabase file,.**
40270 20 74 68 6f 75 67 68 20 74 68 69 73 20 69 73 20 though this is
40280 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 not guaranteed.
40290 54 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 The.** first par
402a0 61 6d 65 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 ameter, szPage,
402b0 69 73 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 is the size in b
402c0 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 ytes of the page
402d0 73 20 74 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 s that must.** b
402e0 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 e allocated by t
402f0 68 65 20 63 61 63 68 65 2e 20 20 73 7a 50 61 67 he cache. szPag
40300 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 e will not be a
40310 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 power of two. s
40320 7a 50 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 zPage.** will th
40330 65 20 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74 e page size of t
40340 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
40350 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 63 that is to be c
40360 61 63 68 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a ached plus an.**
40370 20 69 6e 63 72 65 6d 65 6e 74 20 28 68 65 72 65 increment (here
40380 20 63 61 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 called "R") of
40390 61 62 6f 75 74 20 31 30 30 20 6f 72 20 32 30 30 about 100 or 200
403a0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 . SQLite will u
403b0 73 65 20 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 se the.** extra
403c0 52 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 R bytes on each
403d0 70 61 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 page to store me
403e0 74 61 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 tadata about the
403f0 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 underlying.** d
40400 61 74 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20 atabase page on
40410 64 69 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 disk. The value
40420 20 6f 66 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a of R depends.**
40430 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 on the SQLite v
40440 65 72 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 ersion, the targ
40450 65 74 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 et platform, and
40460 20 68 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 how SQLite was
40470 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 compiled..** R i
40480 73 20 63 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 s constant for a
40490 20 70 61 72 74 69 63 75 6c 61 72 20 62 75 69 6c particular buil
404a0 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68 d of SQLite. Th
404b0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
404c0 74 20 74 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 t to.** xCreate(
404d0 29 2c 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 ), bPurgeable, i
404e0 73 20 74 72 75 65 20 69 66 20 74 68 65 20 63 61 s true if the ca
404f0 63 68 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 che being create
40500 64 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 d will.** be use
40510 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 d to cache datab
40520 61 73 65 20 70 61 67 65 73 20 6f 66 20 61 20 66 ase pages of a f
40530 69 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 ile stored on di
40540 73 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 sk, or.** false
40550 69 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f if it is used fo
40560 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 r an in-memory d
40570 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 atabase. The cac
40580 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
40590 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 n.** does not ha
405a0 76 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e ve to do anythin
405b0 67 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 g special based
405c0 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f with the value o
405d0 66 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a f bPurgeable;.**
405e0 20 69 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 it is purely ad
405f0 76 69 73 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 visory. On a ca
40600 63 68 65 20 77 68 65 72 65 20 62 50 75 72 67 65 che where bPurge
40610 61 62 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 53 able is false, S
40620 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 QLite will.** ne
40630 76 65 72 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 ver invoke xUnpi
40640 6e 28 29 20 65 78 63 65 70 74 20 74 6f 20 64 65 n() except to de
40650 6c 69 62 65 72 61 74 65 6c 79 20 64 65 6c 65 74 liberately delet
40660 65 20 61 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 e a page..** In
40670 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 61 20 63 other words, a c
40680 61 63 68 65 20 63 72 65 61 74 65 64 20 77 69 74 ache created wit
40690 68 20 62 50 75 72 67 65 61 62 6c 65 20 73 65 74 h bPurgeable set
406a0 20 74 6f 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a to false will.*
406b0 2a 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 * never contain
406c0 61 6e 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 any unpinned pag
406d0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 es..**.** The xC
406e0 61 63 68 65 73 69 7a 65 28 29 20 6d 65 74 68 6f achesize() metho
406f0 64 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 d may be called
40700 61 74 20 61 6e 79 20 74 69 6d 65 20 62 79 20 53 at any time by S
40710 51 4c 69 74 65 20 74 6f 20 73 65 74 20 74 68 65 QLite to set the
40720 0a 2a 2a 20 73 75 67 67 65 73 74 65 64 20 6d 61 .** suggested ma
40730 78 69 6d 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 ximum cache-size
40740 20 28 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 (number of page
40750 73 20 73 74 6f 72 65 64 20 62 79 29 20 74 68 65 s stored by) the
40760 20 63 61 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e cache.** instan
40770 63 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ce passed as the
40780 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
40790 20 54 68 69 73 20 69 73 20 74 68 65 20 76 61 6c This is the val
407a0 75 65 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 ue configured us
407b0 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 ing.** the SQLit
407c0 65 20 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65 e "[PRAGMA cache
407d0 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e _size]" command.
407e0 20 41 73 20 77 69 74 68 20 74 68 65 20 62 50 75 As with the bPu
407f0 72 67 65 61 62 6c 65 20 70 61 72 61 6d 65 74 65 rgeable paramete
40800 72 2c 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d r,.** the implem
40810 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 entation is not
40820 72 65 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61 required to do a
40830 6e 79 74 68 69 6e 67 20 77 69 74 68 20 74 68 69 nything with thi
40840 73 0a 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 s.** value; it i
40850 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e s advisory only.
40860 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 .**.** The xPage
40870 63 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 count() method s
40880 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 hould return the
40890 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
408a0 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 currently.** st
408b0 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 ored in the cach
408c0 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 e..** .** The xF
408d0 65 74 63 68 28 29 20 6d 65 74 68 6f 64 20 69 73 etch() method is
408e0 20 75 73 65 64 20 74 6f 20 66 65 74 63 68 20 61 used to fetch a
408f0 20 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e page and return
40900 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
40910 2e 20 0a 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 . .** A 'page',
40920 69 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 2c in this context,
40930 20 69 73 20 61 20 62 75 66 66 65 72 20 6f 66 20 is a buffer of
40940 73 7a 50 61 67 65 20 62 79 74 65 73 20 61 6c 69 szPage bytes ali
40950 67 6e 65 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d gned at an.** 8-
40960 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 byte boundary. T
40970 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 66 65 he page to be fe
40980 74 63 68 65 64 20 69 73 20 64 65 74 65 72 6d 69 tched is determi
40990 6e 65 64 20 62 79 20 74 68 65 20 6b 65 79 2e 20 ned by the key.
409a0 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b The.** mimimum k
409b0 65 79 20 76 61 6c 75 65 20 69 73 20 31 2e 20 41 ey value is 1. A
409c0 66 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e fter it has been
409d0 20 72 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 retrieved using
409e0 20 78 46 65 74 63 68 2c 20 74 68 65 20 70 61 67 xFetch, the pag
409f0 65 20 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 e .** is conside
40a00 72 65 64 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 red to be "pinne
40a10 64 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 d"..**.** If the
40a20 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
40a30 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 is already in th
40a40 65 20 70 61 67 65 20 63 61 63 68 65 2c 20 74 68 e page cache, th
40a50 65 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 en the page cach
40a60 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e.** implementat
40a70 69 6f 6e 20 6d 75 73 74 20 72 65 74 75 72 6e 20 ion must return
40a80 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
40a90 20 70 61 67 65 20 62 75 66 66 65 72 20 77 69 74 page buffer wit
40aa0 68 20 69 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a h its content.**
40ab0 20 69 6e 74 61 63 74 2e 20 20 49 66 20 74 68 65 intact. If the
40ac0 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
40ad0 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 is not already i
40ae0 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 n the cache, the
40af0 6e 20 74 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f n the.** behavio
40b00 72 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 69 r of the cache i
40b10 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 mplementation is
40b20 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 determined by t
40b30 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a he value of the.
40b40 2a 2a 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 ** createFlag pa
40b50 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 rameter passed t
40b60 6f 20 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 o xFetch, accord
40b70 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f ing to the follo
40b80 77 69 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a wing table:.**.*
40b90 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d * <table border=
40ba0 31 20 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 1 width=85% alig
40bb0 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 n=center>.** <tr
40bc0 3e 3c 74 68 3e 20 63 72 65 61 74 65 46 6c 61 67 ><th> createFlag
40bd0 20 3c 74 68 3e 20 42 65 68 61 76 69 6f 75 72 20 <th> Behaviour
40be0 77 68 65 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 when page is not
40bf0 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 already in cach
40c00 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 e.** <tr><td> 0
40c10 3c 74 64 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f <td> Do not allo
40c20 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e cate a new page.
40c30 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a Return NULL..*
40c40 2a 20 3c 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 * <tr><td> 1 <td
40c50 3e 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 > Allocate a new
40c60 20 70 61 67 65 20 69 66 20 69 74 20 65 61 73 79 page if it easy
40c70 20 61 6e 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 and convenient
40c80 74 6f 20 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 to do so..**
40c90 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 Oth
40ca0 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 erwise return NU
40cb0 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 LL..** <tr><td>
40cc0 32 20 3c 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 2 <td> Make ever
40cd0 79 20 65 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f y effort to allo
40ce0 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e cate a new page.
40cf0 20 20 4f 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a Only return.**
40d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
40d10 20 4e 55 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 NULL if allocat
40d20 69 6e 67 20 61 20 6e 65 77 20 70 61 67 65 20 69 ing a new page i
40d30 73 20 65 66 66 65 63 74 69 76 65 6c 79 20 69 6d s effectively im
40d40 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 possible..** </t
40d50 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 able>.**.** SQLi
40d60 74 65 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 te will normally
40d70 20 69 6e 76 6f 6b 65 20 78 46 65 74 63 68 28 29 invoke xFetch()
40d80 20 77 69 74 68 20 61 20 63 72 65 61 74 65 46 6c with a createFl
40d90 61 67 20 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 ag of 0 or 1. I
40da0 66 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 f.** a call to x
40db0 46 65 74 63 68 28 29 20 77 69 74 68 20 63 72 65 Fetch() with cre
40dc0 61 74 65 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 ateFlag==1 retur
40dd0 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 ns NULL, then SQ
40de0 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 Lite will.** att
40df0 65 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e empt to unpin on
40e00 65 20 6f 72 20 6d 6f 72 65 20 63 61 63 68 65 20 e or more cache
40e10 70 61 67 65 73 20 62 79 20 73 70 69 6c 6c 69 6e pages by spillin
40e20 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 g the content of
40e30 0a 2a 2a 20 70 69 6e 6e 65 64 20 70 61 67 65 73 .** pinned pages
40e40 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 73 79 6e to disk and syn
40e50 63 68 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74 ching the operat
40e60 69 6e 67 20 73 79 73 74 65 6d 20 64 69 73 6b 20 ing system disk
40e70 63 61 63 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 cache. After.**
40e80 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e attempting to un
40e90 70 69 6e 20 70 61 67 65 73 2c 20 74 68 65 20 78 pin pages, the x
40ea0 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64 20 77 Fetch() method w
40eb0 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 ill be invoked a
40ec0 67 61 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 gain with.** a c
40ed0 72 65 61 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a reateFlag of 2..
40ee0 2a 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 **.** xUnpin() i
40ef0 73 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 s called by SQLi
40f00 74 65 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 te with a pointe
40f10 72 20 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 r to a currently
40f20 20 70 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 pinned page.**
40f30 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 as its second ar
40f40 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 gument. If the t
40f50 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 hird parameter,
40f60 64 69 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d discard, is non-
40f70 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 zero,.** then th
40f80 65 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 e page should be
40f90 20 65 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 evicted from th
40fa0 65 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 e cache. In this
40fb0 20 63 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a case SQLite .**
40fc0 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 assumes that th
40fd0 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 e next time the
40fe0 70 61 67 65 20 69 73 20 72 65 74 72 69 65 76 65 page is retrieve
40ff0 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 d from the cache
41000 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 using.** the xF
41010 65 74 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 etch() method, i
41020 74 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 t will be zeroed
41030 2e 20 49 66 20 74 68 65 20 64 69 73 63 61 72 64 . If the discard
41040 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a parameter is.**
41050 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 zero, then the
41060 70 61 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 page is consider
41070 65 64 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 ed to be unpinne
41080 64 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 d. The cache imp
41090 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d lementation.** m
410a0 61 79 20 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 ay choose to evi
410b0 63 74 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 ct unpinned page
410c0 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a s at any time..*
410d0 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 *.** The cache i
410e0 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 s not required t
410f0 6f 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 o perform any re
41100 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 ference counting
41110 2e 20 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 . A single .** c
41120 61 6c 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 all to xUnpin()
41130 75 6e 70 69 6e 73 20 74 68 65 20 70 61 67 65 20 unpins the page
41140 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 regardless of th
41150 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f e number of prio
41160 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 r calls .** to x
41170 46 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 Fetch()..**.** T
41180 68 65 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68 he xRekey() meth
41190 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 od is used to ch
411a0 61 6e 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c ange the key val
411b0 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ue associated wi
411c0 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 th the.** page p
411d0 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 assed as the sec
411e0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 66 72 6f ond argument fro
411f0 6d 20 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b m oldKey to newK
41200 65 79 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 ey. If the cache
41210 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 .** previously c
41220 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 ontains an entry
41230 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
41240 20 6e 65 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 newKey, it shou
41250 6c 64 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 ld be.** discard
41260 65 64 2e 20 41 6e 79 20 70 72 69 6f 72 20 63 61 ed. Any prior ca
41270 63 68 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 che entry associ
41280 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 ated with newKey
41290 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e is guaranteed n
412a0 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e ot.** to be pinn
412b0 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 ed..**.** When S
412c0 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 QLite calls the
412d0 78 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 xTruncate() meth
412e0 6f 64 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 od, the cache mu
412f0 73 74 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a st discard all.*
41300 2a 20 65 78 69 73 74 69 6e 67 20 63 61 63 68 65 * existing cache
41310 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 70 61 entries with pa
41320 67 65 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 ge numbers (keys
41330 29 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f ) greater than o
41340 72 20 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 r equal.** to th
41350 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 e value of the i
41360 4c 69 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 Limit parameter
41370 70 61 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63 passed to xTrunc
41380 61 74 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a ate(). If any.**
41390 20 6f 66 20 74 68 65 73 65 20 70 61 67 65 73 20 of these pages
413a0 61 72 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 are pinned, they
413b0 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 are implicitly
413c0 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e unpinned, meanin
413d0 67 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 g that.** they c
413e0 61 6e 20 62 65 20 73 61 66 65 6c 79 20 64 69 73 an be safely dis
413f0 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 carded..**.** Th
41400 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 e xDestroy() met
41410 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 64 hod is used to d
41420 65 6c 65 74 65 20 61 20 63 61 63 68 65 20 61 6c elete a cache al
41430 6c 6f 63 61 74 65 64 20 62 79 20 78 43 72 65 61 located by xCrea
41440 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 te()..** All res
41450 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 ources associate
41460 64 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 d with the speci
41470 66 69 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c fied cache shoul
41480 64 20 62 65 20 66 72 65 65 64 2e 20 41 66 74 65 d be freed. Afte
41490 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 r.** calling the
414a0 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 xDestroy() meth
414b0 6f 64 2c 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 od, SQLite consi
414c0 64 65 72 73 20 74 68 65 20 5b 73 71 6c 69 74 65 ders the [sqlite
414d0 33 5f 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 3_pcache*].** ha
414e0 6e 64 6c 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e ndle invalid, an
414f0 64 20 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 d will not use i
41500 74 20 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 t with any other
41510 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f sqlite3_pcache_
41520 6d 65 74 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 methods.** funct
41530 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ions..*/.typedef
41540 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
41550 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 pcache_methods s
41560 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 qlite3_pcache_me
41570 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 thods;.struct sq
41580 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
41590 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 hods {. void *p
415a0 41 72 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e Arg;. int (*xIn
415b0 69 74 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f it)(void*);. vo
415c0 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 id (*xShutdown)(
415d0 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 void*);. sqlite
415e0 33 5f 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65 3_pcache *(*xCre
415f0 61 74 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c ate)(int szPage,
41600 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 int bPurgeable)
41610 3b 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 ;. void (*xCach
41620 65 73 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 esize)(sqlite3_p
41630 63 61 63 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 cache*, int nCac
41640 68 65 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 hesize);. int (
41650 2a 78 50 61 67 65 63 6f 75 6e 74 29 28 73 71 6c *xPagecount)(sql
41660 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 ite3_pcache*);.
41670 20 76 6f 69 64 20 2a 28 2a 78 46 65 74 63 68 29 void *(*xFetch)
41680 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
41690 2c 20 75 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 , unsigned key,
416a0 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 3b int createFlag);
416b0 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e . void (*xUnpin
416c0 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 )(sqlite3_pcache
416d0 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 *, void*, int di
416e0 73 63 61 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 scard);. void (
416f0 2a 78 52 65 6b 65 79 29 28 73 71 6c 69 74 65 33 *xRekey)(sqlite3
41700 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c _pcache*, void*,
41710 20 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 unsigned oldKey
41720 2c 20 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 , unsigned newKe
41730 79 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 y);. void (*xTr
41740 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f uncate)(sqlite3_
41750 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 pcache*, unsigne
41760 64 20 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 d iLimit);. voi
41770 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 d (*xDestroy)(sq
41780 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a lite3_pcache*);.
41790 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 };../*.** CAPI3R
417a0 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 EF: Online Backu
417b0 70 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 p Object.** EXPE
417c0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
417d0 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 he sqlite3_backu
417e0 70 20 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 p object records
417f0 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 state informati
41800 6f 6e 20 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f on about an ongo
41810 69 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 ing.** online ba
41820 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 ckup operation.
41830 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 The sqlite3_bac
41840 6b 75 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72 kup object is cr
41850 65 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 eated by.** a ca
41860 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 ll to [sqlite3_b
41870 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e ackup_init()] an
41880 64 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 d is destroyed b
41890 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b y a call to.** [
418a0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
418b0 69 6e 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 inish()]..**.**
418c0 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 See Also: [Using
418d0 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 the SQLite Onli
418e0 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a ne Backup API].*
418f0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
41900 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
41910 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a sqlite3_backup;.
41920 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
41930 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 Online Backup A
41940 50 49 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e PI..** EXPERIMEN
41950 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 TAL.**.** This A
41960 50 49 20 69 73 20 75 73 65 64 20 74 6f 20 6f 76 PI is used to ov
41970 65 72 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 erwrite the cont
41980 65 6e 74 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 ents of one data
41990 62 61 73 65 20 77 69 74 68 20 74 68 61 74 0a 2a base with that.*
419a0 2a 20 6f 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 * of another. It
419b0 20 69 73 20 75 73 65 66 75 6c 20 65 69 74 68 65 is useful eithe
419c0 72 20 66 6f 72 20 63 72 65 61 74 69 6e 67 20 62 r for creating b
419d0 61 63 6b 75 70 73 20 6f 66 20 64 61 74 61 62 61 ackups of databa
419e0 73 65 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f ses or.** for co
419f0 70 79 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 pying in-memory
41a00 64 61 74 61 62 61 73 65 73 20 74 6f 20 6f 72 20 databases to or
41a10 66 72 6f 6d 20 70 65 72 73 69 73 74 65 6e 74 20 from persistent
41a20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 files. .**.** Se
41a30 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 e Also: [Using t
41a40 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 he SQLite Online
41a50 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a Backup API].**.
41a60 2a 2a 20 45 78 63 6c 75 73 69 76 65 20 61 63 63 ** Exclusive acc
41a70 65 73 73 20 69 73 20 72 65 71 75 69 72 65 64 20 ess is required
41a80 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 to the destinati
41a90 6f 6e 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 on database for
41aa0 74 68 65 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e the .** duration
41ab0 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f of the operatio
41ac0 6e 2e 20 48 6f 77 65 76 65 72 20 74 68 65 20 73 n. However the s
41ad0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 ource database i
41ae0 73 20 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c s only.** read-l
41af0 6f 63 6b 65 64 20 77 68 69 6c 65 20 69 74 20 69 ocked while it i
41b00 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 s actually being
41b10 20 72 65 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 read, it is not
41b20 20 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 locked.** conti
41b30 6e 75 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20 nuously for the
41b40 65 6e 74 69 72 65 20 6f 70 65 72 61 74 69 6f 6e entire operation
41b50 2e 20 54 68 75 73 2c 20 74 68 65 20 62 61 63 6b . Thus, the back
41b60 75 70 20 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 up may be.** per
41b70 66 6f 72 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 formed on a live
41b80 20 64 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 database withou
41b90 74 20 70 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 t preventing oth
41ba0 65 72 20 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a er users from.**
41bb0 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 writing to the
41bc0 64 61 74 61 62 61 73 65 20 66 6f 72 20 61 6e 20 database for an
41bd0 65 78 74 65 6e 64 65 64 20 70 65 72 69 6f 64 20 extended period
41be0 6f 66 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 of time..** .**
41bf0 54 6f 20 70 65 72 66 6f 72 6d 20 61 20 62 61 63 To perform a bac
41c00 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a kup operation: .
41c10 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 ** <ol>.**
41c20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f <li><b>sqlite3_
41c30 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 backup_init()</b
41c40 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 > is called once
41c50 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 to initialize t
41c60 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 62 61 he.** ba
41c70 63 6b 75 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c ckup, .** <l
41c80 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 i><b>sqlite3_bac
41c90 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 kup_step()</b> i
41ca0 73 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 s called one or
41cb0 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 74 72 more times to tr
41cc0 61 6e 73 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 ansfer .**
41cd0 20 20 20 74 68 65 20 64 61 74 61 20 62 65 74 77 the data betw
41ce0 65 65 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61 een the two data
41cf0 62 61 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c bases, and final
41d00 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 ly.** <li><b
41d10 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f >sqlite3_backup_
41d20 66 69 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 finish()</b> is
41d30 63 61 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 called to releas
41d40 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 e all resources
41d50 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f .** asso
41d60 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
41d70 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
41d80 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a . .** </ol>.**
41d90 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 There should be
41da0 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c exactly one cal
41db0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
41dc0 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 kup_finish() for
41dd0 20 65 61 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 each.** success
41de0 66 75 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ful call to sqli
41df0 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
41e00 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 )..**.** <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 0a 2a 2a 0a 2a 2a 20 54 68 65 20 )</b>.**.** The
41e30 66 69 72 73 74 20 74 77 6f 20 61 72 67 75 6d 65 first two argume
41e40 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 nts passed to [s
41e50 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e qlite3_backup_in
41e60 69 74 28 29 5d 20 61 72 65 20 74 68 65 20 64 61 it()] are the da
41e70 74 61 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 tabase.** handle
41e80 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
41e90 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
41ea0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 database and th
41eb0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 e database name
41ec0 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 74 74 61 .** used to atta
41ed0 63 68 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 ch the destinati
41ee0 6f 6e 20 64 61 74 61 62 61 73 65 20 74 6f 20 74 on database to t
41ef0 68 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 he handle. The d
41f00 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 atabase name.**
41f10 69 73 20 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 is "main" for th
41f20 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c e main database,
41f30 20 22 74 65 6d 70 22 20 66 6f 72 20 74 68 65 20 "temp" for the
41f40 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 temporary databa
41f50 73 65 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 se, or.** the na
41f60 6d 65 20 73 70 65 63 69 66 69 65 64 20 61 73 20 me specified as
41f70 70 61 72 74 20 6f 66 20 74 68 65 20 5b 41 54 54 part of the [ATT
41f80 41 43 48 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 ACH] statement i
41f90 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f f the destinatio
41fa0 6e 20 69 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 n is.** an attac
41fb0 68 65 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 hed database. Th
41fc0 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 e third and four
41fd0 74 68 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 th arguments pas
41fe0 73 65 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 sed to .** sqlit
41ff0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
42000 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 5b 64 identify the [d
42010 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
42020 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 on].** and datab
42030 61 73 65 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a ase name used.**
42040 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 to access the s
42050 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 ource database.
42060 54 68 65 20 76 61 6c 75 65 73 20 70 61 73 73 65 The values passe
42070 64 20 66 6f 72 20 74 68 65 20 73 6f 75 72 63 65 d for the source
42080 20 61 6e 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 and .** destina
42090 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 tion [database c
420a0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d onnection] param
420b0 65 74 65 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 eters must not b
420c0 65 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a e the same..**.*
420d0 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
420e0 63 75 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69 curs within sqli
420f0 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
42100 29 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 ), then NULL is
42110 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 returned.** and
42120 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e an error code an
42130 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 d error message
42140 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
42150 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
42160 63 74 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 ction] .** passe
42170 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
42180 72 67 75 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 rgument. They ma
42190 79 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 y be retrieved u
421a0 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c sing the.** [sql
421b0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c ite3_errcode()],
421c0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
421d0 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
421e0 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 66 75 3_errmsg16()] fu
421f0 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 nctions..** Othe
42200 72 77 69 73 65 2c 20 69 66 20 73 75 63 63 65 73 rwise, if succes
42210 73 66 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 sful, a pointer
42220 74 6f 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 to an [sqlite3_b
42230 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 ackup] object is
42240 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 .** returned. Th
42250 69 73 20 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 is pointer may b
42260 65 20 75 73 65 64 20 77 69 74 68 20 74 68 65 20 e used with the
42270 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
42280 74 65 70 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c tep() and.** sql
42290 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
422a0 73 68 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 sh() functions t
422b0 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20 73 70 o perform the sp
422c0 65 63 69 66 69 65 64 20 62 61 63 6b 75 70 20 0a ecified backup .
422d0 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a ** operation..**
422e0 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 .** <b>sqlite3_b
422f0 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e ackup_step()</b>
42300 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 .**.** Function
42310 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f [sqlite3_backup_
42320 73 74 65 70 28 29 5d 20 69 73 20 75 73 65 64 20 step()] is used
42330 74 6f 20 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 to copy up to nP
42340 61 67 65 20 70 61 67 65 73 20 62 65 74 77 65 65 age pages betwee
42350 6e 20 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 n .** the source
42360 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e and destination
42370 20 64 61 74 61 62 61 73 65 73 2c 20 77 68 65 72 databases, wher
42380 65 20 6e 50 61 67 65 20 69 73 20 74 68 65 20 76 e nPage is the v
42390 61 6c 75 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 alue of the .**
423a0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
423b0 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 passed to sqlit
423c0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
423d0 2e 20 49 66 20 6e 50 61 67 65 20 69 73 20 61 20 . If nPage is a
423e0 6e 65 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 negative.** valu
423f0 65 2c 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 e, all remaining
42400 20 73 6f 75 72 63 65 20 70 61 67 65 73 20 61 72 source pages ar
42410 65 20 63 6f 70 69 65 64 2e 20 49 66 20 74 68 65 e copied. If the
42420 20 72 65 71 75 69 72 65 64 20 70 61 67 65 73 20 required pages
42430 61 72 65 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 are .** succesfu
42440 6c 6c 79 20 63 6f 70 69 65 64 2c 20 62 75 74 20 lly copied, but
42450 74 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 there are still
42460 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f more pages to co
42470 70 79 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a py before the .*
42480 2a 20 62 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 * backup is comp
42490 6c 65 74 65 2c 20 69 74 20 72 65 74 75 72 6e 73 lete, it returns
424a0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 [SQLITE_OK]. If
424b0 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 no error occure
424c0 64 20 61 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 d and there .**
424d0 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 are no more page
424e0 73 20 74 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 s to copy, then
424f0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 [SQLITE_DONE] is
42500 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e returned. If an
42510 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 error .** occur
42520 73 2c 20 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 s, then an SQLit
42530 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
42540 72 65 74 75 72 6e 65 64 2e 20 41 73 20 77 65 6c returned. As wel
42550 6c 20 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d l as [SQLITE_OK]
42560 20 61 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f and.** [SQLITE_
42570 44 4f 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f DONE], a call to
42580 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
42590 73 74 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 step() may retur
425a0 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e n [SQLITE_READON
425b0 4c 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f LY],.** [SQLITE_
425c0 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f NOMEM], [SQLITE_
425d0 42 55 53 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c BUSY], [SQLITE_L
425e0 4f 43 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a OCKED], or an.**
425f0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 [SQLITE_IOERR_A
42600 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 CCESS | SQLITE_I
42610 4f 45 52 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 OERR_XXX] extend
42620 65 64 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a ed error code..*
42630 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 *.** As well as
42640 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 the case where t
42650 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
42660 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 61 73 atabase file was
42670 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 opened for.** r
42680 65 61 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c ead-only access,
42690 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
426a0 73 74 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 step() may retur
426b0 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e n [SQLITE_READON
426c0 4c 59 5d 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 LY] if.** the de
426d0 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 stination is an
426e0 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
426f0 73 65 20 77 69 74 68 20 61 20 64 69 66 66 65 72 se with a differ
42700 65 6e 74 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a ent page size.**
42710 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 from the source
42720 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
42730 20 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b If sqlite3_back
42740 75 70 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 up_step() cannot
42750 20 6f 62 74 61 69 6e 20 61 20 72 65 71 75 69 72 obtain a requir
42760 65 64 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c ed file-system l
42770 6f 63 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 ock, then.** the
42780 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 [sqlite3_busy_h
42790 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 andler | busy-ha
427a0 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a ndler function].
427b0 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 ** is invoked (i
427c0 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 f one is specifi
427d0 65 64 29 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 ed). If the .**
427e0 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 busy-handler ret
427f0 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 urns non-zero be
42800 66 6f 72 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 fore the lock is
42810 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e available, then
42820 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 .** [SQLITE_BUS
42830 59 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 Y] is returned t
42840 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e o the caller. In
42850 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 this case the c
42860 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 all to.** sqlite
42870 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
42880 63 61 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c can be retried l
42890 61 74 65 72 2e 20 49 66 20 74 68 65 20 73 6f 75 ater. If the sou
428a0 72 63 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 rce.** [database
428b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 connection].**
428c0 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f is being used to
428d0 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f write to the so
428e0 75 72 63 65 20 64 61 74 61 62 61 73 65 20 77 68 urce database wh
428f0 65 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 en sqlite3_backu
42900 70 5f 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 p_step().** is c
42910 61 6c 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c alled, then [SQL
42920 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 ITE_LOCKED] is r
42930 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 eturned immediat
42940 65 6c 79 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 ely. Again, in t
42950 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 his.** case the
42960 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
42970 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 backup_step() ca
42980 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 n be retried lat
42990 65 72 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 er on. If.** [SQ
429a0 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 LITE_IOERR_ACCES
429b0 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 S | SQLITE_IOERR
429c0 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e _XXX], [SQLITE_N
429d0 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 OMEM], or.** [SQ
429e0 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 LITE_READONLY] i
429f0 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e s returned, then
42a00 20 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f .** there is no
42a10 20 70 6f 69 6e 74 20 69 6e 20 72 65 74 72 79 69 point in retryi
42a20 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 ng the call to s
42a30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42a40 65 70 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 ep(). These .**
42a50 65 72 72 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 errors are consi
42a60 64 65 72 65 64 20 66 61 74 61 6c 2e 20 41 74 20 dered fatal. At
42a70 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 61 this point the a
42a80 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 pplication must
42a90 61 63 63 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 accept .** that
42aa0 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
42ab0 74 69 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 20 tion has failed
42ac0 61 6e 64 20 70 61 73 73 20 74 68 65 20 62 61 63 and pass the bac
42ad0 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 kup operation ha
42ae0 6e 64 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 ndle .** to the
42af0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
42b00 69 6e 69 73 68 28 29 20 74 6f 20 72 65 6c 65 61 inish() to relea
42b10 73 65 20 61 73 73 6f 63 69 61 74 65 64 20 72 65 se associated re
42b20 73 6f 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 sources..**.** F
42b30 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 72 ollowing the fir
42b40 73 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 st call to sqlit
42b50 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
42b60 2c 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c , an exclusive l
42b70 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e ock is.** obtain
42b80 65 64 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e ed on the destin
42b90 61 74 69 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 ation file. It i
42ba0 73 20 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 75 s not released u
42bb0 6e 74 69 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 ntil either .**
42bc0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
42bd0 69 6e 69 73 68 28 29 20 69 73 20 63 61 6c 6c 65 inish() is calle
42be0 64 20 6f 72 20 74 68 65 20 62 61 63 6b 75 70 20 d or the backup
42bf0 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d operation is com
42c00 70 6c 65 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 plete .** and sq
42c10 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
42c20 70 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c p() returns [SQL
42c30 49 54 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 ITE_DONE]. Addit
42c40 69 6f 6e 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 ionally, each ti
42c50 6d 65 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f me .** a call to
42c60 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
42c70 73 74 65 70 28 29 20 69 73 20 6d 61 64 65 20 61 step() is made a
42c80 20 5b 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 [shared lock] i
42c90 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a s obtained on.**
42ca0 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
42cb0 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 base file. This
42cc0 6c 6f 63 6b 20 69 73 20 72 65 6c 65 61 73 65 64 lock is released
42cd0 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 before the.** s
42ce0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42cf0 65 70 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e ep() call return
42d00 73 2e 20 42 65 63 61 75 73 65 20 74 68 65 20 73 s. Because the s
42d10 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 ource database i
42d20 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 s not.** locked
42d30 62 65 74 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f between calls to
42d40 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
42d50 73 74 65 70 28 29 2c 20 69 74 20 6d 61 79 20 62 step(), it may b
42d60 65 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 e modified mid-w
42d70 61 79 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 ay.** through th
42d80 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 64 75 e backup procedu
42d90 72 65 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 re. If the sourc
42da0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f e database is mo
42db0 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 dified by an.**
42dc0 65 78 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 external process
42dd0 20 6f 72 20 76 69 61 20 61 20 64 61 74 61 62 61 or via a databa
42de0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 se connection ot
42df0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 her than the one
42e00 20 62 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 being.** used b
42e10 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 y the backup ope
42e20 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 ration, then the
42e30 20 62 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 backup will be
42e40 74 72 61 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a transparently.**
42e50 20 72 65 73 74 61 72 74 65 64 20 62 79 20 74 68 restarted by th
42e60 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 e next call to s
42e70 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42e80 65 70 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 ep(). If the sou
42e90 72 63 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 rce .** database
42ea0 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 is modified by
42eb0 74 68 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 the using the sa
42ec0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
42ed0 65 63 74 69 6f 6e 20 61 73 20 69 73 20 75 73 65 ection as is use
42ee0 64 0a 2a 2a 20 62 79 20 74 68 65 20 62 61 63 6b d.** by the back
42ef0 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 up operation, th
42f00 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 en the backup da
42f10 74 61 62 61 73 65 20 69 73 20 74 72 61 6e 73 70 tabase is transp
42f20 61 72 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 arently .** upda
42f30 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 ted at the same
42f40 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 time..**.** <b>s
42f50 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
42f60 6e 69 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a nish()</b>.**.**
42f70 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 62 61 Once sqlite3_ba
42f80 63 6b 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 ckup_step() has
42f90 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 returned [SQLITE
42fa0 5f 44 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 _DONE], or when
42fb0 74 68 65 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 the .** applicat
42fc0 69 6f 6e 20 77 69 73 68 65 73 20 74 6f 20 61 62 ion wishes to ab
42fd0 61 6e 64 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 andon the backup
42fe0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 operation, the
42ff0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d [sqlite3_backup]
43000 0a 2a 2a 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c .** object shoul
43010 64 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 d be passed to s
43020 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
43030 6e 69 73 68 28 29 2e 20 54 68 69 73 20 72 65 6c nish(). This rel
43040 65 61 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 eases all.** res
43050 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 ources associate
43060 64 20 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 d with the backu
43070 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 p operation. If
43080 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
43090 74 65 70 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 tep().** has not
430a0 20 79 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53 yet returned [S
430b0 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 QLITE_DONE], the
430c0 6e 20 61 6e 79 20 61 63 74 69 76 65 20 77 72 69 n any active wri
430d0 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te-transaction o
430e0 6e 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 n the.** destina
430f0 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 tion database is
43100 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 rolled back. Th
43110 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 e [sqlite3_backu
43120 70 5d 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 p] object is inv
43130 61 6c 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 alid.** and may
43140 6e 6f 74 20 62 65 20 75 73 65 64 20 66 6f 6c 6c not be used foll
43150 6f 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 owing a call to
43160 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
43170 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 inish()..**.** T
43180 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
43190 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 d by sqlite3_bac
431a0 6b 75 70 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 kup_finish is [S
431b0 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 QLITE_OK] if no
431c0 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 error.** occurre
431d0 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72 d, regardless or
431e0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
431f0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
43200 74 65 70 28 29 20 77 61 73 20 63 61 6c 6c 65 64 tep() was called
43210 0a 2a 2a 20 61 20 73 75 66 66 69 63 69 65 6e 74 .** a sufficient
43220 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
43230 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 to complete the
43240 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
43250 6e 2e 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 n. Or, if.** an
43260 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f out-of-memory co
43270 6e 64 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 ndition or IO er
43280 72 6f 72 20 6f 63 63 75 72 65 64 20 64 75 72 69 ror occured duri
43290 6e 67 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 ng a call to.**
432a0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
432b0 74 65 70 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 tep() then [SQLI
432c0 54 45 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a TE_NOMEM] or an.
432d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 ** [SQLITE_IOERR
432e0 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 _ACCESS | SQLITE
432f0 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 72 72 6f _IOERR_XXX] erro
43300 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 r code.** is ret
43310 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 urned. In this c
43320 61 73 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f ase the error co
43330 64 65 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 de and an error
43340 6d 65 73 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 message are.** w
43350 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 65 ritten to the de
43360 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 stination [datab
43370 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e ase connection].
43380 0a 2a 2a 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 .**.** A return
43390 6f 66 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d of [SQLITE_BUSY]
433a0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b or [SQLITE_LOCK
433b0 45 44 5d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ED] from sqlite3
433c0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 _backup_step() i
433d0 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 s.** not a perma
433e0 6e 65 6e 74 20 65 72 72 6f 72 20 61 6e 64 20 64 nent error and d
433f0 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74 20 74 oes not affect t
43400 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
43410 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 of.** sqlite3_ba
43420 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a ckup_finish()..*
43430 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f *.** <b>sqlite3_
43440 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 backup_remaining
43450 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b (), sqlite3_back
43460 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f up_pagecount()</
43470 62 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 b>.**.** Each ca
43480 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 ll to sqlite3_ba
43490 63 6b 75 70 5f 73 74 65 70 28 29 20 73 65 74 73 ckup_step() sets
434a0 20 74 77 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 two values stor
434b0 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a ed internally.**
434c0 20 62 79 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f by an [sqlite3_
434d0 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 backup] object.
434e0 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 The number of pa
434f0 67 65 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 ges still to be
43500 62 61 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 backed.** up, wh
43510 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 ich may be queri
43520 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 ed by sqlite3_ba
43530 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 ckup_remaining()
43540 2c 20 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a , and the total.
43550 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 ** number of pag
43560 65 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 es in the source
43570 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
43580 77 68 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 which may be que
43590 72 69 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 ried by.** sqlit
435a0 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f e3_backup_pageco
435b0 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 unt()..**.** The
435c0 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 values returned
435d0 20 62 79 20 74 68 65 73 65 20 66 75 6e 63 74 69 by these functi
435e0 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 ons are only upd
435f0 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 ated by.** sqlit
43600 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
43610 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 . If the source
43620 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 database is modi
43630 66 69 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 fied during a ba
43640 63 6b 75 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f ckup.** operatio
43650 6e 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 n, then the valu
43660 65 73 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74 es are not updat
43670 65 64 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f ed to account fo
43680 72 20 61 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 r any extra.** p
43690 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 ages that need t
436a0 6f 20 62 65 20 75 70 64 61 74 65 64 20 6f 72 20 o be updated or
436b0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
436c0 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 source database
436d0 66 69 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 file.** changing
436e0 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 ..**.** <b>Concu
436f0 72 72 65 6e 74 20 55 73 61 67 65 20 6f 66 20 44 rrent Usage of D
43700 61 74 61 62 61 73 65 20 48 61 6e 64 6c 65 73 3c atabase Handles<
43710 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f /b>.**.** The so
43720 75 72 63 65 20 5b 64 61 74 61 62 61 73 65 20 63 urce [database c
43730 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 onnection] may b
43740 65 20 75 73 65 64 20 62 79 20 74 68 65 20 61 70 e used by the ap
43750 70 6c 69 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 plication for ot
43760 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 her.** purposes
43770 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 6f while a backup o
43780 70 65 72 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 peration is unde
43790 72 77 61 79 20 6f 72 20 62 65 69 6e 67 20 69 6e rway or being in
437a0 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 itialized..** If
437b0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 SQLite is compi
437c0 6c 65 64 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 led and configur
437d0 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 ed to support th
437e0 72 65 61 64 73 61 66 65 20 64 61 74 61 62 61 73 readsafe databas
437f0 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 e.** connections
43800 2c 20 74 68 65 6e 20 74 68 65 20 73 6f 75 72 63 , then the sourc
43810 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
43820 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 ction may be use
43830 64 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a d concurrently.*
43840 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 * from within ot
43850 68 65 72 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a her threads..**.
43860 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 ** However, the
43870 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 application must
43880 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
43890 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
438a0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
438b0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 ection handle is
438c0 20 6e 6f 74 20 70 61 73 73 65 64 20 74 6f 20 61 not passed to a
438d0 6e 79 20 6f 74 68 65 72 20 41 50 49 20 28 62 79 ny other API (by
438e0 20 61 6e 79 20 74 68 72 65 61 64 29 20 61 66 74 any thread) aft
438f0 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 er .** sqlite3_b
43900 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 73 20 ackup_init() is
43910 63 61 6c 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 called and befor
43920 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 e the correspond
43930 69 6e 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 ing call to.** s
43940 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
43950 6e 69 73 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e nish(). Unfortun
43960 61 74 65 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 ately SQLite doe
43970 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
43980 63 68 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 check.** for thi
43990 73 2c 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 s, if the applic
439a0 61 74 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 ation does use t
439b0 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b he destination [
439c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
439d0 69 6f 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 ion].** for some
439e0 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 20 64 other purpose d
439f0 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f uring a backup o
43a00 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 peration, things
43a10 20 6d 61 79 20 61 70 70 65 61 72 20 74 6f 0a 2a may appear to.*
43a20 2a 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 * work correctly
43a30 20 62 75 74 20 69 6e 20 66 61 63 74 20 62 65 20 but in fact be
43a40 73 75 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 subtly malfuncti
43a50 6f 6e 69 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 oning. Use of t
43a60 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f he.** destinatio
43a70 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 n database conne
43a80 63 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 ction while a ba
43a90 63 6b 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 ckup is in progr
43aa0 65 73 73 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 ess might.** als
43ab0 6f 20 63 61 75 73 65 20 61 20 6d 75 74 65 78 20 o cause a mutex
43ac0 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 deadlock..**.**
43ad0 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 Furthermore, if
43ae0 72 75 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 running in [shar
43af0 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 ed cache mode],
43b00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
43b10 6d 75 73 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 must.** guarante
43b20 65 20 74 68 61 74 20 74 68 65 20 73 68 61 72 65 e that the share
43b30 64 20 63 61 63 68 65 20 75 73 65 64 20 62 79 20 d cache used by
43b40 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
43b50 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e database.** is n
43b60 6f 74 20 61 63 63 65 73 73 65 64 20 77 68 69 6c ot accessed whil
43b70 65 20 74 68 65 20 62 61 63 6b 75 70 20 69 73 20 e the backup is
43b80 72 75 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 running. In prac
43b90 74 69 63 65 20 74 68 69 73 20 6d 65 61 6e 73 0a tice this means.
43ba0 2a 2a 20 74 68 61 74 20 74 68 65 20 61 70 70 6c ** that the appl
43bb0 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 ication must gua
43bc0 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 rantee that the
43bd0 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 file-system file
43be0 20 62 65 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 being .** backe
43bf0 64 20 75 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 d up to is not a
43c00 63 63 65 73 73 65 64 20 62 79 20 61 6e 79 20 63 ccessed by any c
43c10 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e onnection within
43c20 20 74 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a the process,.**
43c30 20 6e 6f 74 20 6a 75 73 74 20 74 68 65 20 73 70 not just the sp
43c40 65 63 69 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f ecific connectio
43c50 6e 20 74 68 61 74 20 77 61 73 20 70 61 73 73 65 n that was passe
43c60 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 d to sqlite3_bac
43c70 6b 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a kup_init()..**.*
43c80 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 * The [sqlite3_b
43c90 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 ackup] object it
43ca0 73 65 6c 66 20 69 73 20 70 61 72 74 69 61 6c 6c self is partiall
43cb0 79 20 74 68 72 65 61 64 73 61 66 65 2e 20 4d 75 y threadsafe. Mu
43cc0 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 ltiple .** threa
43cd0 64 73 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 ds may safely ma
43ce0 6b 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 ke multiple conc
43cf0 75 72 72 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 urrent calls to
43d00 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
43d10 74 65 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 tep()..** Howeve
43d20 72 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 r, the sqlite3_b
43d30 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 ackup_remaining(
43d40 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 ) and sqlite3_ba
43d50 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 ckup_pagecount()
43d60 0a 2a 2a 20 41 50 49 73 20 61 72 65 20 6e 6f 74 .** APIs are not
43d70 20 73 74 72 69 63 74 6c 79 20 73 70 65 61 6b 69 strictly speaki
43d80 6e 67 20 74 68 72 65 61 64 73 61 66 65 2e 20 49 ng threadsafe. I
43d90 66 20 74 68 65 79 20 61 72 65 20 69 6e 76 6f 6b f they are invok
43da0 65 64 20 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d ed at the.** sam
43db0 65 20 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 e time as anothe
43dc0 72 20 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f r thread is invo
43dd0 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 king sqlite3_bac
43de0 6b 75 70 5f 73 74 65 70 28 29 20 69 74 20 69 73 kup_step() it is
43df0 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61 .** possible tha
43e00 74 20 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e t they return in
43e10 76 61 6c 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f valid values..*/
43e20 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
43e30 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 te3_backup *sqli
43e40 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
43e50 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 44 65 73 . sqlite3 *pDes
43e60 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
43e70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 /* Des
43e80 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
43e90 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f e handle */. co
43ea0 6e 73 74 20 63 68 61 72 20 2a 7a 44 65 73 74 4e nst char *zDestN
43eb0 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ame,
43ec0 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 /* Destinat
43ed0 69 6f 6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d ion database nam
43ee0 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a e */. sqlite3 *
43ef0 70 53 6f 75 72 63 65 2c 20 20 20 20 20 20 20 20 pSource,
43f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
43f10 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 Source database
43f20 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
43f30 73 74 20 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 st char *zSource
43f40 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 Name
43f50 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 /* Source da
43f60 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 tabase name */.)
43f70 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
43f80 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
43f90 73 74 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 step(sqlite3_bac
43fa0 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 kup *p, int nPag
43fb0 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 e);.SQLITE_API i
43fc0 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 nt sqlite3_backu
43fd0 70 5f 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 p_finish(sqlite3
43fe0 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c _backup *p);.SQL
43ff0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
44000 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 te3_backup_remai
44010 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 ning(sqlite3_bac
44020 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f kup *p);.SQLITE_
44030 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
44040 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 backup_pagecount
44050 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
44060 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 *p);../*.** CAPI
44070 33 52 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 3REF: Unlock Not
44080 69 66 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 ification.** EXP
44090 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
440a0 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 When running in
440b0 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 shared-cache mod
440c0 65 2c 20 61 20 64 61 74 61 62 61 73 65 20 6f 70 e, a database op
440d0 65 72 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c eration may fail
440e0 20 77 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c with.** an [SQL
440f0 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f ITE_LOCKED] erro
44100 72 20 69 66 20 74 68 65 20 72 65 71 75 69 72 65 r if the require
44110 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 d locks on the s
44120 68 61 72 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a hared-cache or.*
44130 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 61 62 * individual tab
44140 6c 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 les within the s
44150 68 61 72 65 64 2d 63 61 63 68 65 20 63 61 6e 6e hared-cache cann
44160 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 ot be obtained.
44170 53 65 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 See.** [SQLite S
44180 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 hared-Cache Mode
44190 5d 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 ] for a descript
441a0 69 6f 6e 20 6f 66 20 73 68 61 72 65 64 2d 63 61 ion of shared-ca
441b0 63 68 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a che locking. .**
441c0 20 54 68 69 73 20 41 50 49 20 6d 61 79 20 62 65 This API may be
441d0 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 used to registe
441e0 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 r a callback tha
441f0 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e t SQLite will in
44200 76 6f 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 voke .** when th
44210 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 e connection cur
44220 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 rently holding t
44230 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b he required lock
44240 20 72 65 6c 69 6e 71 75 69 73 68 65 73 20 69 74 relinquishes it
44250 2e 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 ..** This API is
44260 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
44270 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 if the library w
44280 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 as compiled with
44290 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f the.** [SQLITE_
442a0 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f ENABLE_UNLOCK_NO
442b0 54 49 46 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 TIFY] C-preproce
442c0 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 ssor symbol defi
442d0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 ned..**.** See A
442e0 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 lso: [Using the
442f0 53 51 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f SQLite Unlock No
44300 74 69 66 69 63 61 74 69 6f 6e 20 46 65 61 74 75 tification Featu
44310 72 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 re]..**.** Share
44320 64 2d 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 d-cache locks ar
44330 65 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 e released when
44340 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 a database conne
44350 63 74 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a ction concludes.
44360 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 ** its current t
44370 72 61 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 ransaction, eith
44380 65 72 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 er by committing
44390 20 69 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 it or rolling i
443a0 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 t back. .**.** W
443b0 68 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e hen a connection
443c0 20 28 6b 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 (known as the b
443d0 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f locked connectio
443e0 6e 29 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 n) fails to obta
443f0 69 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 in a.** shared-c
44400 61 63 68 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 ache lock and SQ
44410 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 LITE_LOCKED is r
44420 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 eturned to the c
44430 61 6c 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 aller, the.** id
44440 65 6e 74 69 74 79 20 6f 66 20 74 68 65 20 64 61 entity of the da
44450 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
44460 6e 20 28 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 n (the blocking
44470 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 connection) that
44480 0a 2a 2a 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 .** has locked t
44490 68 65 20 72 65 71 75 69 72 65 64 20 72 65 73 6f he required reso
444a0 75 72 63 65 20 69 73 20 73 74 6f 72 65 64 20 69 urce is stored i
444b0 6e 74 65 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 nternally. After
444c0 20 61 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 an .** applicat
444d0 69 6f 6e 20 72 65 63 65 69 76 65 73 20 61 6e 20 ion receives an
444e0 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 SQLITE_LOCKED er
444f0 72 6f 72 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c ror, it may call
44500 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
44510 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 unlock_notify()
44520 6d 65 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 method with the
44530 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 blocked connecti
44540 6f 6e 20 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a on handle as .**
44550 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
44560 65 6e 74 20 74 6f 20 72 65 67 69 73 74 65 72 20 ent to register
44570 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 for a callback t
44580 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f hat will be invo
44590 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ked.** when the
445a0 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 blocking connect
445b0 69 6f 6e 73 20 63 75 72 72 65 6e 74 20 74 72 61 ions current tra
445c0 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 nsaction is conc
445d0 6c 75 64 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 luded. The.** ca
445e0 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 llback is invoke
445f0 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 d from within th
44600 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d e [sqlite3_step]
44610 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f or [sqlite3_clo
44620 73 65 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 se].** call that
44630 20 63 6f 6e 63 6c 75 64 65 73 20 74 68 65 20 62 concludes the b
44640 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 locking connecti
44650 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ons transaction.
44660 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 .**.** If sqlite
44670 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 3_unlock_notify(
44680 29 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 ) is called in a
44690 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 multi-threaded
446a0 61 70 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 application,.**
446b0 74 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 there is a chanc
446c0 65 20 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b e that the block
446d0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 ing connection w
446e0 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 ill have already
446f0 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 .** concluded it
44700 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 s transaction by
44710 20 74 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 the time sqlite
44720 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 3_unlock_notify(
44730 29 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a ) is invoked..**
44740 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 If this happens
44750 2c 20 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 , then the speci
44760 66 69 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 fied callback is
44770 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 invoked immedia
44780 74 65 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 tely,.** from wi
44790 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f thin the call to
447a0 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f sqlite3_unlock_
447b0 6e 6f 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 notify()..**.**
447c0 49 66 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 If the blocked c
447d0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 74 74 onnection is att
447e0 65 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 empting to obtai
447f0 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f n a write-lock o
44800 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 n a.** shared-ca
44810 63 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d che table, and m
44820 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 ore than one oth
44830 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 er connection cu
44840 72 72 65 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a rrently holds.**
44850 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 a read-lock on
44860 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 the same table,
44870 74 68 65 6e 20 53 51 4c 69 74 65 20 61 72 62 69 then SQLite arbi
44880 74 72 61 72 69 6c 79 20 73 65 6c 65 63 74 73 20 trarily selects
44890 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f one of .** the o
448a0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 ther connections
448b0 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 62 to use as the b
448c0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 locking connecti
448d0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 on..**.** There
448e0 6d 61 79 20 62 65 20 61 74 20 6d 6f 73 74 20 6f may be at most o
448f0 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 ne unlock-notify
44900 20 63 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 callback regist
44910 65 72 65 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c ered by a .** bl
44920 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e ocked connection
44930 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c . If sqlite3_unl
44940 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 ock_notify() is
44950 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 0a called when the.
44960 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 ** blocked conne
44970 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 ction already ha
44980 73 20 61 20 72 65 67 69 73 74 65 72 65 64 20 75 s a registered u
44990 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c nlock-notify cal
449a0 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 lback,.** then t
449b0 68 65 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 he new callback
449c0 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 replaces the old
449d0 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c . If sqlite3_unl
449e0 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a ock_notify() is.
449f0 2a 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 ** called with a
44a00 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 NULL pointer as
44a10 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 its second argu
44a20 6d 65 6e 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 ment, then any e
44a30 78 69 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 xisting.** unloc
44a40 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
44a50 6b 20 69 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 k is cancelled.
44a60 54 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e The blocked conn
44a70 65 63 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f ections .** unlo
44a80 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 ck-notify callba
44a90 63 6b 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 ck may also be c
44aa0 61 6e 63 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 anceled by closi
44ab0 6e 67 20 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a ng the blocked.*
44ac0 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 * connection usi
44ad0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 ng [sqlite3_clos
44ae0 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 e()]..**.** The
44af0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
44b00 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 llback is not re
44b10 65 6e 74 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 entrant. If an a
44b20 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b pplication invok
44b30 65 73 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 es.** any sqlite
44b40 33 5f 78 78 78 20 41 50 49 20 66 75 6e 63 74 69 3_xxx API functi
44b50 6f 6e 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 ons from within
44b60 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 an unlock-notify
44b70 20 63 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 callback, a.**
44b80 63 72 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 crash or deadloc
44b90 6b 20 6d 61 79 20 62 65 20 74 68 65 20 72 65 73 k may be the res
44ba0 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 ult..**.** Unles
44bb0 73 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 s deadlock is de
44bc0 74 65 63 74 65 64 20 28 73 65 65 20 62 65 6c 6f tected (see belo
44bd0 77 29 2c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f w), sqlite3_unlo
44be0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 ck_notify() alwa
44bf0 79 73 0a 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 ys.** returns SQ
44c00 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c LITE_OK..**.** <
44c10 62 3e 43 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 b>Callback Invoc
44c20 61 74 69 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 ation Details</b
44c30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 >.**.** When an
44c40 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
44c50 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 llback is regist
44c60 65 72 65 64 2c 20 74 68 65 20 61 70 70 6c 69 63 ered, the applic
44c70 61 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 ation provides a
44c80 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 .** single void
44c90 2a 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 * pointer that i
44ca0 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 s passed to the
44cb0 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 callback when it
44cc0 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 is invoked..**
44cd0 48 6f 77 65 76 65 72 2c 20 74 68 65 20 73 69 67 However, the sig
44ce0 6e 61 74 75 72 65 20 6f 66 20 74 68 65 20 63 61 nature of the ca
44cf0 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 llback function
44d00 61 6c 6c 6f 77 73 20 53 51 4c 69 74 65 20 74 6f allows SQLite to
44d10 20 70 61 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 pass.** it an a
44d20 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f rray of void* co
44d30 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 ntext pointers.
44d40 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
44d50 6e 74 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 nt passed to.**
44d60 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 an unlock-notify
44d70 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 callback is a p
44d80 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 ointer to an arr
44d90 61 79 20 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e ay of void* poin
44da0 74 65 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 ters,.** and the
44db0 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e second is the n
44dc0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
44dd0 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 0a 2a in the array..*
44de0 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 *.** When a bloc
44df0 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 king connections
44e00 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
44e10 63 6f 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65 concluded, there
44e20 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 may be.** more
44e30 74 68 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 than one blocked
44e40 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 connection that
44e50 20 68 61 73 20 72 65 67 69 73 74 65 72 65 64 20 has registered
44e60 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f for an unlock-no
44e70 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b tify.** callback
44e80 2e 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 . If two or more
44e90 20 73 75 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f such blocked co
44ea0 6e 6e 65 63 74 69 6f 6e 73 20 68 61 76 65 20 73 nnections have s
44eb0 70 65 63 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 pecified the.**
44ec0 73 61 6d 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 same callback fu
44ed0 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 nction, then ins
44ee0 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 tead of invoking
44ef0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 the callback fu
44f00 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 nction.** multip
44f10 6c 65 20 74 69 6d 65 73 2c 20 69 74 20 69 73 20 le times, it is
44f20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 invoked once wit
44f30 68 20 74 68 65 20 73 65 74 20 6f 66 20 76 6f 69 h the set of voi
44f40 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 d* context point
44f50 65 72 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 ers.** specified
44f60 20 62 79 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 by the blocked
44f70 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 connections bund
44f80 6c 65 64 20 74 6f 67 65 74 68 65 72 20 69 6e 74 led together int
44f90 6f 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 o an array..** T
44fa0 68 69 73 20 67 69 76 65 73 20 74 68 65 20 61 70 his gives the ap
44fb0 70 6c 69 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 plication an opp
44fc0 6f 72 74 75 6e 69 74 79 20 74 6f 20 70 72 69 6f ortunity to prio
44fd0 72 69 74 69 7a 65 20 61 6e 79 20 61 63 74 69 6f ritize any actio
44fe0 6e 73 20 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 ns .** related t
44ff0 6f 20 74 68 65 20 73 65 74 20 6f 66 20 75 6e 62 o the set of unb
45000 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73 65 20 locked database
45010 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a connections..**.
45020 2a 2a 20 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 ** <b>Deadlock D
45030 65 74 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a etection</b>.**.
45040 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 61 74 ** Assuming that
45050 20 61 66 74 65 72 20 72 65 67 69 73 74 65 72 69 after registeri
45060 6e 67 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b ng for an unlock
45070 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
45080 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 a .** database
45090 77 61 69 74 73 20 66 6f 72 20 74 68 65 20 63 61 waits for the ca
450a0 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 73 73 llback to be iss
450b0 75 65 64 20 62 65 66 6f 72 65 20 74 61 6b 69 6e ued before takin
450c0 67 20 61 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a g any further.**
450d0 20 61 63 74 69 6f 6e 20 28 61 20 72 65 61 73 6f action (a reaso
450e0 6e 61 62 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e nable assumption
450f0 29 2c 20 74 68 65 6e 20 75 73 69 6e 67 20 74 68 ), then using th
45100 69 73 20 41 50 49 20 6d 61 79 20 63 61 75 73 65 is API may cause
45110 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 the.** applicat
45120 69 6f 6e 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e ion to deadlock.
45130 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 For example, if
45140 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 connection X is
45150 20 77 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 waiting for.**
45160 63 6f 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 connection Y's t
45170 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 ransaction to be
45180 20 63 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 concluded, and
45190 73 69 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 similarly connec
451a0 74 69 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 tion.** Y is wai
451b0 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 ting on connecti
451c0 6f 6e 20 58 27 73 20 74 72 61 6e 73 61 63 74 69 on X's transacti
451d0 6f 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 on, then neither
451e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 connection.** w
451f0 69 6c 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20 ill proceed and
45200 74 68 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72 the system may r
45210 65 6d 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 emain deadlocked
45220 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a indefinitely..*
45230 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 *.** To avoid th
45240 69 73 20 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 is scenario, the
45250 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f sqlite3_unlock_
45260 6e 6f 74 69 66 79 28 29 20 70 65 72 66 6f 72 6d notify() perform
45270 73 20 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 s deadlock.** de
45280 74 65 63 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 tection. If a gi
45290 76 65 6e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ven call to sqli
452a0 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
452b0 79 28 29 20 77 6f 75 6c 64 20 70 75 74 20 74 68 y() would put th
452c0 65 0a 2a 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 e.** system in a
452d0 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 deadlocked stat
452e0 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c e, then SQLITE_L
452f0 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 OCKED is returne
45300 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f d and no.** unlo
45310 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 ck-notify callba
45320 63 6b 20 69 73 20 72 65 67 69 73 74 65 72 65 64 ck is registered
45330 2e 20 54 68 65 20 73 79 73 74 65 6d 20 69 73 20 . The system is
45340 73 61 69 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a said to be in.**
45350 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 a deadlocked st
45360 61 74 65 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f ate if connectio
45370 6e 20 41 20 68 61 73 20 72 65 67 69 73 74 65 72 n A has register
45380 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b ed for an unlock
45390 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 -notify.** callb
453a0 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c ack on the concl
453b0 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 usion of connect
453c0 69 6f 6e 20 42 27 73 20 74 72 61 6e 73 61 63 74 ion B's transact
453d0 69 6f 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 ion, and connect
453e0 69 6f 6e 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 ion.** B has its
453f0 65 6c 66 20 72 65 67 69 73 74 65 72 65 64 20 66 elf registered f
45400 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 or an unlock-not
45410 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 ify callback whe
45420 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 n connection.**
45430 41 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 A's transaction
45440 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e is concluded. In
45450 64 69 72 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 direct deadlock
45460 69 73 20 61 6c 73 6f 20 64 65 74 65 63 74 65 64 is also detected
45470 2c 20 73 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 , so.** the syst
45480 65 6d 20 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 em is also consi
45490 64 65 72 65 64 20 74 6f 20 62 65 20 64 65 61 64 dered to be dead
454a0 6c 6f 63 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 locked if connec
454b0 74 69 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 tion B has.** re
454c0 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 gistered for an
454d0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
454e0 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f llback on the co
454f0 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e nclusion of conn
45500 65 63 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 ection.** C's tr
45510 61 6e 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 ansaction, where
45520 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 connection C is
45530 20 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e waiting on conn
45540 65 63 74 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a ection A. Any.**
45550 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c number of level
45560 73 20 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e s of indirection
45570 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a are allowed..**
45580 0a 2a 2a 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 .** <b>The "DROP
45590 20 54 41 42 4c 45 22 20 45 78 63 65 70 74 69 6f TABLE" Exceptio
455a0 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e n</b>.**.** When
455b0 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 a call to [sqli
455c0 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65 74 75 te3_step()] retu
455d0 72 6e 73 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 rns SQLITE_LOCKE
455e0 44 2c 20 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 D, it is almost
455f0 0a 2a 2a 20 61 6c 77 61 79 73 20 61 70 70 72 6f .** always appro
45600 70 72 69 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 priate to call s
45610 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
45620 74 69 66 79 28 29 2e 20 54 68 65 72 65 20 69 73 tify(). There is
45630 20 68 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 however,.** one
45640 20 65 78 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e exception. When
45650 20 65 78 65 63 75 74 69 6e 67 20 61 20 22 44 52 executing a "DR
45660 4f 50 20 54 41 42 4c 45 22 20 6f 72 20 22 44 52 OP TABLE" or "DR
45670 4f 50 20 49 4e 44 45 58 22 20 73 74 61 74 65 6d OP INDEX" statem
45680 65 6e 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 ent,.** SQLite c
45690 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 61 hecks if there a
456a0 72 65 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 re any currently
456b0 20 65 78 65 63 75 74 69 6e 67 20 53 45 4c 45 43 executing SELEC
456c0 54 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 T statements.**
456d0 74 68 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 that belong to t
456e0 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 he same connecti
456f0 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 on. If there are
45700 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 , SQLITE_LOCKED
45710 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 is.** returned.
45720 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
45730 72 65 20 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 re is no "blocki
45740 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 ng connection",
45750 73 6f 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 so invoking.** s
45760 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
45770 74 69 66 79 28 29 20 72 65 73 75 6c 74 73 20 69 tify() results i
45780 6e 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 n the unlock-not
45790 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 ify callback bei
457a0 6e 67 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d ng.** invoked im
457b0 6d 65 64 69 61 74 65 6c 79 2e 20 49 66 20 74 68 mediately. If th
457c0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 e application th
457d0 65 6e 20 72 65 2d 61 74 74 65 6d 70 74 73 20 74 en re-attempts t
457e0 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45 22 0a he "DROP TABLE".
457f0 2a 2a 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 ** or "DROP INDE
45800 58 22 20 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 X" query, an inf
45810 69 6e 69 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 inite loop might
45820 20 62 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a be the result..
45830 2a 2a 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 **.** One way ar
45840 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 ound this proble
45850 6d 20 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68 m is to check th
45860 65 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 e extended error
45870 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 0a 2a code returned.*
45880 2a 20 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f * by an sqlite3_
45890 73 74 65 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 step() call. If
458a0 74 68 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b there is a block
458b0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 ing connection,
458c0 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 then the.** exte
458d0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 nded error code
458e0 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 is set to SQLITE
458f0 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 _LOCKED_SHAREDCA
45900 43 48 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 CHE. Otherwise,
45910 69 6e 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 in.** the specia
45920 6c 20 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e l "DROP TABLE/IN
45930 44 45 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 DEX" case, the e
45940 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f xtended error co
45950 64 65 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 de is just .** S
45960 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f QLITE_LOCKED..*/
45970 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
45980 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e sqlite3_unlock_n
45990 6f 74 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33 otify(. sqlite3
459a0 20 2a 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 *pBlocked,
459b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
459c0 20 20 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 /* Waiting
459d0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
459e0 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 void (*xNotify)(
459f0 76 6f 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e void **apArg, in
45a00 74 20 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 t nArg), /* C
45a10 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
45a20 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 to invoke */.
45a30 76 6f 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 void *pNotifyArg
45a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45a50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
45a60 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 rgument to pass
45a70 74 6f 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b to xNotify */.);
45a80 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 .../*.** CAPI3RE
45a90 46 3a 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 F: String Compar
45aa0 69 73 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 ison.** EXPERIME
45ab0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b NTAL.**.** The [
45ac0 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 sqlite3_strnicmp
45ad0 28 29 5d 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 ()] API allows a
45ae0 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 pplications and
45af0 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a extensions to.**
45b00 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e compare the con
45b10 74 65 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66 tents of two buf
45b20 66 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 fers containing
45b30 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 69 6e UTF-8 strings in
45b40 20 61 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e a.** case-inden
45b50 64 65 6e 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 dent fashion, us
45b60 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 65 66 ing the same def
45b70 69 6e 69 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 inition of case
45b80 69 6e 64 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a independence .**
45b90 20 74 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 that SQLite use
45ba0 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 s internally whe
45bb0 6e 20 63 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e n comparing iden
45bc0 74 69 66 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 tifiers..*/.SQLI
45bd0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
45be0 65 33 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 e3_strnicmp(cons
45bf0 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 t char *, const
45c00 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f char *, int);../
45c10 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 *.** Undo the ha
45c20 63 6b 20 74 68 61 74 20 63 6f 6e 76 65 72 74 73 ck that converts
45c30 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
45c40 74 79 70 65 73 20 74 6f 20 69 6e 74 65 67 65 72 types to integer
45c50 20 66 6f 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f for.** builds o
45c60 6e 20 70 72 6f 63 65 73 73 6f 72 73 20 77 69 74 n processors wit
45c70 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f hout floating po
45c80 69 6e 74 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a int support..*/.
45c90 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
45ca0 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
45cb0 54 0a 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 T.# undef double
45cc0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d .#endif..#if 0.}
45cd0 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 /* End of the
45ce0 27 65 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 'extern "C"' blo
45cf0 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e ck */.#endif.#en
45d00 64 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a dif.../*********
45d10 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c ***** End of sql
45d20 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ite3.h *********
45d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45d50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
45d60 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
45d70 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
45d80 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
45d90 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
45da0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
45db0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 ***** Include ha
45dc0 73 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 sh.h in the midd
45dd0 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e le of sqliteInt.
45de0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
45df0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
45e00 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
45e10 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a hash.h ********
45e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45e40 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
45e50 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a September 22.**
45e60 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
45e70 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
45e80 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
45e90 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
45ea0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
45eb0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
45ec0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
45ed0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
45ee0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
45ef0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
45f00 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
45f10 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
45f20 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
45f30 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
45f40 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
45f50 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
45f60 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
45f70 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
45f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
45fc0 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 This is the head
45fd0 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 er file for the
45fe0 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 generic hash-tab
45ff0 6c 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e le implemenation
46000 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 .** used in SQLi
46010 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 te..**.** $Id: h
46020 61 73 68 2e 68 2c 76 20 31 2e 31 35 20 32 30 30 ash.h,v 1.15 200
46030 39 2f 30 35 2f 30 32 20 31 33 3a 32 39 3a 33 38 9/05/02 13:29:38
46040 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 drh Exp $.*/.#i
46050 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 48 41 fndef _SQLITE_HA
46060 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 SH_H_.#define _S
46070 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f QLITE_HASH_H_../
46080 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 * Forward declar
46090 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 ations of struct
460a0 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 ures. */.typedef
460b0 20 73 74 72 75 63 74 20 48 61 73 68 20 48 61 73 struct Hash Has
460c0 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 h;.typedef struc
460d0 74 20 48 61 73 68 45 6c 65 6d 20 48 61 73 68 45 t HashElem HashE
460e0 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c lem;../* A compl
460f0 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 ete hash table i
46100 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
46110 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
46120 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 tructure..** The
46130 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 internals of th
46140 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 is structure are
46150 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 intended to be
46160 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 opaque -- client
46170 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 .** code should
46180 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61 not attempt to a
46190 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 ccess or modify
461a0 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 the fields of th
461b0 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 is structure.**
461c0 64 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 directly. Chang
461d0 65 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 e this structure
461e0 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74 only by using t
461f0 68 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f he routines belo
46200 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 73 w..** However, s
46210 6f 6d 65 20 6f 66 20 74 68 65 20 22 70 72 6f 63 ome of the "proc
46220 65 64 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e edures" and "fun
46230 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 ctions" for modi
46240 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 fying and.** acc
46250 65 73 73 69 6e 67 20 74 68 69 73 20 73 74 72 75 essing this stru
46260 63 74 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79 cture are really
46270 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 macros, so we c
46280 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 an't really make
46290 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 .** this structu
462a0 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2a 0a 2a 2a re opaque..**.**
462b0 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 All elements of
462c0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
462d0 61 72 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 are on a single
462e0 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 doubly-linked li
462f0 73 74 2e 0a 2a 2a 20 48 61 73 68 2e 66 69 72 73 st..** Hash.firs
46300 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 t points to the
46310 68 65 61 64 20 6f 66 20 74 68 69 73 20 6c 69 73 head of this lis
46320 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 t..**.** There a
46330 72 65 20 48 61 73 68 2e 68 74 73 69 7a 65 20 62 re Hash.htsize b
46340 75 63 6b 65 74 73 2e 20 20 45 61 63 68 20 62 75 uckets. Each bu
46350 63 6b 65 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 cket points to a
46360 20 73 70 6f 74 20 69 6e 0a 2a 2a 20 74 68 65 20 spot in.** the
46370 67 6c 6f 62 61 6c 20 64 6f 75 62 6c 79 2d 6c 69 global doubly-li
46380 6e 6b 65 64 20 6c 69 73 74 2e 20 20 54 68 65 20 nked list. The
46390 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
463a0 62 75 63 6b 65 74 20 61 72 65 20 74 68 65 0a 2a bucket are the.*
463b0 2a 20 65 6c 65 6d 65 6e 74 20 70 6f 69 6e 74 65 * element pointe
463c0 64 20 74 6f 20 70 6c 75 73 20 74 68 65 20 6e 65 d to plus the ne
463d0 78 74 20 5f 68 74 2e 63 6f 75 6e 74 2d 31 20 65 xt _ht.count-1 e
463e0 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 6c lements in the l
463f0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 2e ist..**.** Hash.
46400 68 74 73 69 7a 65 20 61 6e 64 20 48 61 73 68 2e htsize and Hash.
46410 68 74 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20 ht may be zero.
46420 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 6c 6f In that case lo
46430 6f 6b 75 70 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 okup is done.**
46440 62 79 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 by a linear sear
46450 63 68 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c ch of the global
46460 20 6c 69 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c list. For smal
46470 6c 20 74 61 62 6c 65 73 2c 20 74 68 65 20 0a 2a l tables, the .*
46480 2a 20 48 61 73 68 2e 68 74 20 74 61 62 6c 65 20 * Hash.ht table
46490 69 73 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 is never allocat
464a0 65 64 20 62 65 63 61 75 73 65 20 69 66 20 74 68 ed because if th
464b0 65 72 65 20 61 72 65 20 66 65 77 20 65 6c 65 6d ere are few elem
464c0 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74 ents.** in the t
464d0 61 62 6c 65 2c 20 69 74 20 69 73 20 66 61 73 74 able, it is fast
464e0 65 72 20 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61 er to do a linea
464f0 72 20 73 65 61 72 63 68 20 74 68 61 6e 20 74 6f r search than to
46500 20 6d 61 6e 61 67 65 0a 2a 2a 20 74 68 65 20 68 manage.** the h
46510 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 ash table..*/.st
46520 72 75 63 74 20 48 61 73 68 20 7b 0a 20 20 75 6e ruct Hash {. un
46530 73 69 67 6e 65 64 20 69 6e 74 20 68 74 73 69 7a signed int htsiz
46540 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 e; /* Numbe
46550 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 r of buckets in
46560 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a the hash table *
46570 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
46580 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a count; /*
46590 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
465a0 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 es in this table
465b0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
465c0 66 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 first;
465d0 2f 2a 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 /* The first ele
465e0 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 ment of the arra
465f0 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 y */. struct _h
46600 74 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 t {
46610 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 /* the hash tab
46620 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f le */. int co
46630 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 unt;
46640 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
46650 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 f entries with t
46660 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 his hash */.
46670 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b HashElem *chain;
46680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f /* Po
46690 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 65 inter to first e
466a0 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20 68 ntry with this h
466b0 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a ash */. } *ht;.
466c0 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d };../* Each elem
466d0 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68 20 ent in the hash
466e0 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 table is an inst
466f0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
46700 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 owing .** struct
46710 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e ure. All elemen
46720 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e ts are stored on
46730 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 a single doubly
46740 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a -linked list..**
46750 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20 .** Again, this
46760 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 74 structure is int
46770 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 ended to be opaq
46780 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74 ue, but it can't
46790 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 really.** be op
467a0 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74 20 aque because it
467b0 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f is used by macro
467c0 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 s..*/.struct Has
467d0 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c hElem {. HashEl
467e0 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b em *next, *prev;
467f0 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 /* Next a
46800 6e 64 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d nd previous elem
46810 65 6e 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c ents in the tabl
46820 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 e */. void *dat
46830 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a;
46840 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f /* Data asso
46850 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 ciated with this
46860 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f element */. co
46870 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 3b 20 nst char *pKey;
46880 69 6e 74 20 6e 4b 65 79 3b 20 20 2f 2a 20 4b 65 int nKey; /* Ke
46890 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 y associated wit
468a0 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a h this element *
468b0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 /.};../*.** Acce
468c0 73 73 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f ss routines. To
468d0 20 64 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 delete, insert
468e0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
468f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
46900 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 E void sqlite3Ha
46910 73 68 49 6e 69 74 28 48 61 73 68 2a 29 3b 0a 53 shInit(Hash*);.S
46920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
46930 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 id *sqlite3HashI
46940 6e 73 65 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e nsert(Hash*, con
46950 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 st char *pKey, i
46960 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 nt nKey, void *p
46970 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Data);.SQLITE_PR
46980 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
46990 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 te3HashFind(cons
469a0 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 t Hash*, const c
469b0 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e har *pKey, int n
469c0 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Key);.SQLITE_PRI
469d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
469e0 33 48 61 73 68 43 6c 65 61 72 28 48 61 73 68 2a 3HashClear(Hash*
469f0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 );../*.** Macros
46a00 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 for looping ove
46a10 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f r all elements o
46a20 66 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 f a hash table.
46a30 20 54 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a The idiom is.**
46a40 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
46a50 2a 20 20 20 48 61 73 68 20 68 3b 0a 2a 2a 20 20 * Hash h;.**
46a60 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a HashElem *p;.**
46a70 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 ....** for(
46a80 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 p=sqliteHashFirs
46a90 74 28 26 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 t(&h); p; p=sqli
46aa0 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a teHashNext(p)){.
46ab0 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63 ** SomeStruc
46ac0 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20 73 71 ture *pData = sq
46ad0 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b liteHashData(p);
46ae0 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f .** // do so
46af0 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 44 61 mething with pDa
46b00 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 ta.** }.*/.#de
46b10 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 46 fine sqliteHashF
46b20 69 72 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 irst(H) ((H)->f
46b30 69 72 73 74 29 0a 23 64 65 66 69 6e 65 20 73 71 irst).#define sq
46b40 6c 69 74 65 48 61 73 68 4e 65 78 74 28 45 29 20 liteHashNext(E)
46b50 20 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 ((E)->next).#d
46b60 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 efine sqliteHash
46b70 44 61 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e Data(E) ((E)->
46b80 64 61 74 61 29 0a 2f 2a 20 23 64 65 66 69 6e 65 data)./* #define
46b90 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45 sqliteHashKey(E
46ba0 29 20 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 ) ((E)->pKey)
46bb0 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a // NOT USED */.
46bc0 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 /* #define sqlit
46bd0 65 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 eHashKeysize(E)
46be0 28 28 45 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f 20 ((E)->nKey) //
46bf0 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 0a 2f 2a 0a NOT USED */../*.
46c00 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 ** Number of ent
46c10 72 69 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 ries in a hash t
46c20 61 62 6c 65 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 able.*/./* #defi
46c30 6e 65 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75 ne sqliteHashCou
46c40 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 nt(H) ((H)->cou
46c50 6e 74 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 nt) // NOT USED
46c60 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 */..#endif /* _S
46c70 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f QLITE_HASH_H_ */
46c80 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
46c90 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 * End of hash.h
46ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46cd0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
46ce0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
46cf0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
46d00 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
46d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46d20 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
46d30 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 73 65 2e * Include parse.
46d40 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
46d50 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a of sqliteInt.h *
46d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46d70 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
46d80 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 72 * Begin file par
46d90 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a se.h ***********
46da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46dc0 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d /.#define TK_SEM
46dd0 49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 I
46de0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 1.#
46df0 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 define TK_EXPLAI
46e00 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
46e10 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 2.#def
46e20 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20 20 20 ine TK_QUERY
46e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46e40 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 3.#define
46e50 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 TK_PLAN
46e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46e70 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 4.#define TK
46e80 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20 20 20 _BEGIN
46e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46ea0 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 5.#define TK_TR
46eb0 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20 ANSACTION
46ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 6.
46ed0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 #define TK_DEFER
46ee0 52 45 44 20 20 20 20 20 20 20 20 20 20 20 20 20 RED
46ef0 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 7.#de
46f00 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 fine TK_IMMEDIAT
46f10 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
46f20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 8.#defin
46f30 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 e TK_EXCLUSIVE
46f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46f50 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 54 9.#define T
46f60 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 K_COMMIT
46f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46f80 20 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 10.#define TK_E
46f90 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ND
46fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
46fb0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c .#define TK_ROLL
46fc0 42 41 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 BACK
46fd0 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 12.#d
46fe0 65 66 69 6e 65 20 54 4b 5f 53 41 56 45 50 4f 49 efine TK_SAVEPOI
46ff0 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NT
47000 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 13.#defi
47010 6e 65 20 54 4b 5f 52 45 4c 45 41 53 45 20 20 20 ne TK_RELEASE
47020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47030 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 14.#define
47040 54 4b 5f 54 4f 20 20 20 20 20 20 20 20 20 20 20 TK_TO
47050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47060 20 20 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 15.#define TK_
47070 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 TABLE
47080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
47090 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 6.#define TK_CRE
470a0 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ATE
470b0 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 23 17.#
470c0 64 65 66 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 define TK_IF
470d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
470e0 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 18.#def
470f0 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 ine TK_NOT
47100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47110 20 20 20 20 20 20 31 39 0a 23 64 65 66 69 6e 65 19.#define
47120 20 54 4b 5f 45 58 49 53 54 53 20 20 20 20 20 20 TK_EXISTS
47130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47140 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 20.#define TK
47150 5f 54 45 4d 50 20 20 20 20 20 20 20 20 20 20 20 _TEMP
47160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47170 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50 21.#define TK_LP
47180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47190 20 20 20 20 20 20 20 20 20 20 20 20 20 32 32 0a 22.
471a0 23 64 65 66 69 6e 65 20 54 4b 5f 52 50 20 20 20 #define TK_RP
471b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
471c0 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 23.#de
471d0 66 69 6e 65 20 54 4b 5f 41 53 20 20 20 20 20 20 fine TK_AS
471e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
471f0 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 24.#defin
47200 65 20 54 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20 e TK_COMMA
47210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47220 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 54 25.#define T
47230 4b 5f 49 44 20 20 20 20 20 20 20 20 20 20 20 20 K_ID
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47250 20 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 26.#define TK_I
47260 4e 44 45 58 45 44 20 20 20 20 20 20 20 20 20 20 NDEXED
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 37 27
47280 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 .#define TK_ABOR
47290 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
472a0 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 28.#d
472b0 65 66 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 20 efine TK_AFTER
472c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472d0 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 29.#defi
472e0 6e 65 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20 ne TK_ANALYZE
472f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47300 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 30.#define
47310 54 4b 5f 41 53 43 20 20 20 20 20 20 20 20 20 20 TK_ASC
47320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47330 20 20 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 31.#define TK_
47340 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 ATTACH
47350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
47360 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 46 2.#define TK_BEF
47370 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ORE
47380 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a 23 33.#
47390 64 65 66 69 6e 65 20 54 4b 5f 42 59 20 20 20 20 define TK_BY
473a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473b0 20 20 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 34.#def
473c0 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20 ine TK_CASCADE
473d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473e0 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 35.#define
473f0 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20 TK_CAST
47400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47410 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 36.#define TK
47420 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20 _COLUMNKW
47430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47440 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 37.#define TK_CO
47450 4e 46 4c 49 43 54 20 20 20 20 20 20 20 20 20 20 NFLICT
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 38.
47470 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42 #define TK_DATAB
47480 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ASE
47490 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 39.#de
474a0 66 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20 fine TK_DESC
474b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474c0 20 20 20 20 20 20 20 34 30 0a 23 64 65 66 69 6e 40.#defin
474d0 65 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20 e TK_DETACH
474e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474f0 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 54 41.#define T
47500 4b 5f 45 41 43 48 20 20 20 20 20 20 20 20 20 20 K_EACH
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47520 20 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 42.#define TK_F
47530 41 49 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 AIL
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 33 43
47550 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20 .#define TK_FOR
47560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47570 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 44.#d
47580 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20 efine TK_IGNORE
47590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475a0 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 45.#defi
475b0 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20 ne TK_INITIALLY
475c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475d0 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20 46.#define
475e0 54 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20 TK_INSTEAD
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47600 20 20 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47.#define TK_
47610 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20 LIKE_KW
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
47630 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54 8.#define TK_MAT
47640 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CH
47650 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 49.#
47660 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 define TK_KEY
47670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47680 20 20 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 50.#def
47690 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 ine TK_OF
476a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
476b0 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 51.#define
476c0 20 54 4b 5f 4f 46 46 53 45 54 20 20 20 20 20 20 TK_OFFSET
476d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
476e0 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 52.#define TK
476f0 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 _PRAGMA
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47710 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 53.#define TK_RA
47720 49 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ISE
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 54.
47740 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 #define TK_REPLA
47750 43 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CE
47760 20 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 55.#de
47770 66 69 6e 65 20 54 4b 5f 52 45 53 54 52 49 43 54 fine TK_RESTRICT
47780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47790 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 56.#defin
477a0 65 20 54 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 e TK_ROW
477b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477c0 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20 54 57.#define T
477d0 4b 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20 K_TRIGGER
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477f0 20 35 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 58.#define TK_V
47800 41 43 55 55 4d 20 20 20 20 20 20 20 20 20 20 20 ACUUM
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 39 59
47820 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 .#define TK_VIEW
47830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47840 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23 64 60.#d
47850 65 66 69 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c efine TK_VIRTUAL
47860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47870 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 66 69 61.#defi
47880 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 ne TK_REINDEX
47890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478a0 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 62.#define
478b0 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20 20 20 20 TK_RENAME
478c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478d0 20 20 36 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 63.#define TK_
478e0 43 54 49 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 CTIME_KW
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
47900 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 4.#define TK_ANY
47910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47920 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a 23 65.#
47930 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 define TK_OR
47940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47950 20 20 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 66.#def
47960 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 ine TK_AND
47970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47980 20 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65 67.#define
47990 20 54 4b 5f 49 53 20 20 20 20 20 20 20 20 20 20 TK_IS
479a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479b0 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 68.#define TK
479c0 5f 42 45 54 57 45 45 4e 20 20 20 20 20 20 20 20 _BETWEEN
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479e0 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 69.#define TK_IN
479f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 70.
47a10 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c #define TK_ISNUL
47a20 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 L
47a30 20 20 20 20 20 20 20 20 20 20 37 31 0a 23 64 65 71.#de
47a40 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 fine TK_NOTNULL
47a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a60 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e 72.#defin
47a70 65 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 e TK_NE
47a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a90 20 20 20 20 37 33 0a 23 64 65 66 69 6e 65 20 54 73.#define T
47aa0 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20 20 20 K_EQ
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ac0 20 37 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 74.#define TK_G
47ad0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35 75
47af0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 .#define TK_LE
47b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b10 20 20 20 20 20 20 20 20 20 20 20 37 36 0a 23 64 76.#d
47b20 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 efine TK_LT
47b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b40 20 20 20 20 20 20 20 20 37 37 0a 23 64 65 66 69 77.#defi
47b50 6e 65 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 ne TK_GE
47b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b70 20 20 20 20 20 37 38 0a 23 64 65 66 69 6e 65 20 78.#define
47b80 54 4b 5f 45 53 43 41 50 45 20 20 20 20 20 20 20 TK_ESCAPE
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ba0 20 20 37 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 79.#define TK_
47bb0 42 49 54 41 4e 44 20 20 20 20 20 20 20 20 20 20 BITAND
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
47bd0 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 0.#define TK_BIT
47be0 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OR
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 38 31 0a 23 81.#
47c00 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 define TK_LSHIFT
47c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c20 20 20 20 20 20 20 20 20 20 38 32 0a 23 64 65 66 82.#def
47c30 69 6e 65 20 54 4b 5f 52 53 48 49 46 54 20 20 20 ine TK_RSHIFT
47c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c50 20 20 20 20 20 20 38 33 0a 23 64 65 66 69 6e 65 83.#define
47c60 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 20 20 20 TK_PLUS
47c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c80 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 84.#define TK
47c90 5f 4d 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 _MINUS
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47cb0 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 85.#define TK_ST
47cc0 41 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AR
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36 0a 86.
47ce0 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 #define TK_SLASH
47cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d00 20 20 20 20 20 20 20 20 20 20 38 37 0a 23 64 65 87.#de
47d10 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 fine TK_REM
47d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d30 20 20 20 20 20 20 20 38 38 0a 23 64 65 66 69 6e 88.#defin
47d40 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 e TK_CONCAT
47d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d60 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 20 54 89.#define T
47d70 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 K_COLLATE
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d90 20 39 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 90.#define TK_U
47da0 4d 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 MINUS
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 31 91
47dc0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 .#define TK_UPLU
47dd0 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 S
47de0 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 92.#d
47df0 65 66 69 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 efine TK_BITNOT
47e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e10 20 20 20 20 20 20 20 20 39 33 0a 23 64 65 66 69 93.#defi
47e20 6e 65 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 ne TK_STRING
47e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e40 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e 65 20 94.#define
47e50 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 TK_JOIN_KW
47e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e70 20 20 39 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 95.#define TK_
47e80 43 4f 4e 53 54 52 41 49 4e 54 20 20 20 20 20 20 CONSTRAINT
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
47ea0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 6.#define TK_DEF
47eb0 41 55 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 AULT
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a 23 97.#
47ed0 64 65 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 define TK_NULL
47ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ef0 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 98.#def
47f00 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 ine TK_PRIMARY
47f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 99.#define
47f30 20 54 4b 5f 55 4e 49 51 55 45 20 20 20 20 20 20 TK_UNIQUE
47f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f50 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20 54 100.#define T
47f60 4b 5f 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 K_CHECK
47f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f80 20 31 30 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 101.#define TK_
47f90 52 45 46 45 52 45 4e 43 45 53 20 20 20 20 20 20 REFERENCES
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
47fb0 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55 02.#define TK_AU
47fc0 54 4f 49 4e 43 52 20 20 20 20 20 20 20 20 20 20 TOINCR
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 33 103
47fe0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 .#define TK_ON
47ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48000 20 20 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 104.#
48010 64 65 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 define TK_DELETE
48020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48030 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 105.#de
48040 66 69 6e 65 20 54 4b 5f 55 50 44 41 54 45 20 20 fine TK_UPDATE
48050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48060 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 106.#defi
48070 6e 65 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 ne TK_INSERT
48080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48090 20 20 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 107.#define
480a0 20 54 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20 TK_SET
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480c0 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20 54 108.#define T
480d0 4b 5f 44 45 46 45 52 52 41 42 4c 45 20 20 20 20 K_DEFERRABLE
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480f0 20 31 30 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 109.#define TK_
48100 46 4f 52 45 49 47 4e 20 20 20 20 20 20 20 20 20 FOREIGN
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48120 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52 10.#define TK_DR
48130 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OP
48140 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 31 111
48150 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f .#define TK_UNIO
48160 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
48170 20 20 20 20 20 20 20 20 20 20 20 31 31 32 0a 23 112.#
48180 64 65 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20 define TK_ALL
48190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481a0 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 113.#de
481b0 66 69 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20 fine TK_EXCEPT
481c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481d0 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 114.#defi
481e0 6e 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 ne TK_INTERSECT
481f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48200 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 115.#define
48210 20 54 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20 TK_SELECT
48220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48230 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65 20 54 116.#define T
48240 4b 5f 44 49 53 54 49 4e 43 54 20 20 20 20 20 20 K_DISTINCT
48250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48260 20 31 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 117.#define TK_
48270 44 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 DOT
48280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48290 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52 18.#define TK_FR
482a0 4f 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OM
482b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39 119
482c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e .#define TK_JOIN
482d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
482e0 20 20 20 20 20 20 20 20 20 20 20 31 32 30 0a 23 120.#
482f0 64 65 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20 define TK_USING
48300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48310 20 20 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 121.#de
48320 66 69 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20 fine TK_ORDER
48330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48340 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 122.#defi
48350 6e 65 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20 ne TK_GROUP
48360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48370 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 123.#define
48380 20 54 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20 TK_HAVING
48390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
483a0 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20 54 124.#define T
483b0 4b 5f 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20 K_LIMIT
483c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
483d0 20 31 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 125.#define TK_
483e0 57 48 45 52 45 20 20 20 20 20 20 20 20 20 20 20 WHERE
483f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48400 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 26.#define TK_IN
48410 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TO
48420 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 37 127
48430 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55 .#define TK_VALU
48440 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ES
48450 20 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 128.#
48460 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 define TK_INTEGE
48470 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R
48480 20 20 20 20 20 20 20 20 20 31 32 39 0a 23 64 65 129.#de
48490 66 69 6e 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 fine TK_FLOAT
484a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484b0 20 20 20 20 20 20 20 31 33 30 0a 23 64 65 66 69 130.#defi
484c0 6e 65 20 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20 ne TK_BLOB
484d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484e0 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 131.#define
484f0 20 54 4b 5f 52 45 47 49 53 54 45 52 20 20 20 20 TK_REGISTER
48500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48510 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 54 132.#define T
48520 4b 5f 56 41 52 49 41 42 4c 45 20 20 20 20 20 20 K_VARIABLE
48530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48540 20 31 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 133.#define TK_
48550 43 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 CASE
48560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48570 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 34.#define TK_WH
48580 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EN
48590 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 35 135
485a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e .#define TK_THEN
485b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485c0 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 23 136.#
485d0 64 65 66 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20 define TK_ELSE
485e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485f0 20 20 20 20 20 20 20 20 20 31 33 37 0a 23 64 65 137.#de
48600 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58 20 20 20 fine TK_INDEX
48610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48620 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 138.#defi
48630 6e 65 20 54 4b 5f 41 4c 54 45 52 20 20 20 20 20 ne TK_ALTER
48640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48650 20 20 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 139.#define
48660 20 54 4b 5f 41 44 44 20 20 20 20 20 20 20 20 20 TK_ADD
48670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48680 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65 20 54 140.#define T
48690 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20 20 20 20 K_TO_TEXT
486a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
486b0 20 31 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 141.#define TK_
486c0 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20 TO_BLOB
486d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
486e0 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 42.#define TK_TO
486f0 5f 4e 55 4d 45 52 49 43 20 20 20 20 20 20 20 20 _NUMERIC
48700 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 33 143
48710 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49 .#define TK_TO_I
48720 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NT
48730 20 20 20 20 20 20 20 20 20 20 20 31 34 34 0a 23 144.#
48740 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41 define TK_TO_REA
48750 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 L
48760 20 20 20 20 20 20 20 20 20 31 34 35 0a 23 64 65 145.#de
48770 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f 46 fine TK_END_OF_F
48780 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ILE
48790 20 20 20 20 20 20 20 31 34 36 0a 23 64 65 66 69 146.#defi
487a0 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20 20 ne TK_ILLEGAL
487b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487c0 20 20 20 20 20 31 34 37 0a 23 64 65 66 69 6e 65 147.#define
487d0 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 20 20 TK_SPACE
487e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487f0 20 20 20 31 34 38 0a 23 64 65 66 69 6e 65 20 54 148.#define T
48800 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49 4e K_UNCLOSED_STRIN
48810 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 G
48820 20 31 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 149.#define TK_
48830 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 FUNCTION
48840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48850 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 50.#define TK_CO
48860 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 LUMN
48870 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 31 151
48880 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f .#define TK_AGG_
48890 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 FUNCTION
488a0 20 20 20 20 20 20 20 20 20 20 20 31 35 32 0a 23 152.#
488b0 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43 4f define TK_AGG_CO
488c0 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 LUMN
488d0 20 20 20 20 20 20 20 20 20 31 35 33 0a 23 64 65 153.#de
488e0 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 fine TK_CONST_FU
488f0 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NC
48900 20 20 20 20 20 20 20 31 35 34 0a 0a 2f 2a 2a 2a 154../***
48910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
48920 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a of parse.h *****
48930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
48960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
48970 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
48980 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
48990 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
489a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63 **********/.#inc
489b0 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 lude <stdio.h>.#
489c0 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e include <stdlib.
489d0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 h>.#include <str
489e0 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 ing.h>.#include
489f0 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c <assert.h>.#incl
48a00 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a ude <stddef.h>..
48a10 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 /*.** If compili
48a20 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 ng for a process
48a30 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c or that lacks fl
48a40 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 oating point sup
48a50 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 port,.** substit
48a60 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 ute integer for
48a70 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a floating-point.*
48a80 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
48a90 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
48aa0 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 INT.# define dou
48ab0 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 ble sqlite_int64
48ac0 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f .# define LONGDO
48ad0 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 65 UBLE_TYPE sqlite
48ae0 5f 69 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 20 _int64.# ifndef
48af0 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 SQLITE_BIG_DBL.#
48b00 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
48b10 5f 42 49 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 _BIG_DBL (((sqli
48b20 74 65 33 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 te3_int64)1)<<50
48b30 29 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 ).# endif.# defi
48b40 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ne SQLITE_OMIT_D
48b50 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a ATETIME_FUNCS 1.
48b60 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
48b70 4f 4d 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 OMIT_TRACE 1.# u
48b80 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 ndef SQLITE_MIXE
48b90 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 D_ENDIAN_64BIT_F
48ba0 4c 4f 41 54 0a 23 20 75 6e 64 65 66 20 53 51 4c LOAT.# undef SQL
48bb0 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 ITE_HAVE_ISNAN.#
48bc0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 endif.#ifndef SQ
48bd0 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 LITE_BIG_DBL.# d
48be0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 efine SQLITE_BIG
48bf0 5f 44 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 _DBL (1e99).#end
48c00 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 if../*.** OMIT_T
48c10 45 4d 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 EMPDB is set to
48c20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 1 if SQLITE_OMIT
48c30 5f 54 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e _TEMPDB is defin
48c40 65 64 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 ed, or 0.** afte
48c50 72 77 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68 rward. Having th
48c60 69 73 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 is macro allows
48c70 75 73 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 us to cause the
48c80 43 20 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 C compiler .** t
48c90 6f 20 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64 o omit code used
48ca0 20 62 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20 by TEMP tables
48cb0 77 69 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69 without messy #i
48cc0 66 6e 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73 fndef statements
48cd0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
48ce0 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 TE_OMIT_TEMPDB.#
48cf0 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 define OMIT_TEMP
48d00 44 42 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 DB 1.#else.#defi
48d10 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 ne OMIT_TEMPDB 0
48d20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 .#endif../*.** I
48d30 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
48d40 6d 61 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 macro is set to
48d50 31 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 1, then NULL val
48d60 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 ues are consider
48d70 65 64 0a 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 ed.** distinct w
48d80 68 65 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 hen determining
48d90 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
48da0 77 6f 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 wo entries are t
48db0 68 65 20 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 he same.** in a
48dc0 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 20 20 54 UNIQUE index. T
48dd0 68 69 73 20 69 73 20 74 68 65 20 77 61 79 20 50 his is the way P
48de0 6f 73 74 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c ostgreSQL, Oracl
48df0 65 2c 20 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a e, DB2, MySQL,.*
48e00 2a 20 4f 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 * OCELOT, and Fi
48e10 72 65 62 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e rebird all work.
48e20 20 20 54 68 65 20 53 51 4c 39 32 20 73 70 65 63 The SQL92 spec
48e30 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79 73 explicitly says
48e40 20 74 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 this.** is the
48e50 77 61 79 20 74 68 69 6e 67 73 20 61 72 65 20 73 way things are s
48e60 75 70 70 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a uppose to work..
48e70 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c **.** If the fol
48e80 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 lowing macro is
48e90 73 65 74 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 set to 0, the NU
48ea0 4c 4c 73 20 61 72 65 20 69 6e 64 69 73 74 69 6e LLs are indistin
48eb0 63 74 20 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 ct for.** a UNIQ
48ec0 55 45 20 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 UE index. In th
48ed0 69 73 20 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e is mode, you can
48ee0 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e only have a sin
48ef0 67 6c 65 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a gle NULL entry.*
48f00 2a 20 66 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 * for a column d
48f10 65 63 6c 61 72 65 64 20 55 4e 49 51 55 45 2e 20 eclared UNIQUE.
48f20 20 54 68 69 73 20 69 73 20 74 68 65 20 77 61 79 This is the way
48f30 20 49 6e 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 Informix and SQ
48f40 4c 20 53 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b L Server.** work
48f50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c ..*/.#define NUL
48f60 4c 5f 44 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 L_DISTINCT_FOR_U
48f70 4e 49 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 NIQUE 1../*.** T
48f80 68 65 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 he "file format"
48f90 20 6e 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e number is an in
48fa0 74 65 67 65 72 20 74 68 61 74 20 69 73 20 69 6e teger that is in
48fb0 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 cremented whenev
48fc0 65 72 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c er.** the VDBE-l
48fd0 65 76 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 evel file format
48fe0 20 63 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66 changes. The f
48ff0 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 ollowing macros
49000 64 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 define the.** th
49010 65 20 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66 e default file f
49020 6f 72 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61 ormat for new da
49030 74 61 62 61 73 65 73 20 61 6e 64 20 74 68 65 20 tabases and the
49040 6d 61 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 maximum file for
49050 6d 61 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 mat.** that the
49060 6c 69 62 72 61 72 79 20 63 61 6e 20 72 65 61 64 library can read
49070 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
49080 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 ITE_MAX_FILE_FOR
49090 4d 41 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51 MAT 4.#ifndef SQ
490a0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c LITE_DEFAULT_FIL
490b0 45 5f 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e E_FORMAT.# defin
490c0 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
490d0 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 _FILE_FORMAT 1.#
490e0 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
490f0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 QLITE_DEFAULT_RE
49100 43 55 52 53 49 56 45 5f 54 52 49 47 47 45 52 53 CURSIVE_TRIGGERS
49110 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
49120 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49 _DEFAULT_RECURSI
49130 56 45 5f 54 52 49 47 47 45 52 53 20 30 0a 23 65 VE_TRIGGERS 0.#e
49140 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 ndif../*.** Prov
49150 69 64 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 ide a default va
49160 6c 75 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 lue for SQLITE_T
49170 45 4d 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 EMP_STORE in cas
49180 65 20 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63 e it is not spec
49190 69 66 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 ified.** on the
491a0 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a command-line.*/.
491b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 #ifndef SQLITE_T
491c0 45 4d 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 EMP_STORE.# defi
491d0 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 ne SQLITE_TEMP_S
491e0 54 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f TORE 1.#endif../
491f0 2a 0a 2a 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f *.** GCC does no
49200 74 20 64 65 66 69 6e 65 20 74 68 65 20 6f 66 66 t define the off
49210 73 65 74 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f setof() macro so
49220 20 77 65 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 we'll have to d
49230 6f 20 69 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 o it.** ourselve
49240 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 s..*/.#ifndef of
49250 66 73 65 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f fsetof.#define o
49260 66 66 73 65 74 6f 66 28 53 54 52 55 43 54 55 52 ffsetof(STRUCTUR
49270 45 2c 46 49 45 4c 44 29 20 28 28 69 6e 74 29 28 E,FIELD) ((int)(
49280 28 63 68 61 72 2a 29 26 28 28 53 54 52 55 43 54 (char*)&((STRUCT
49290 55 52 45 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 URE*)0)->FIELD))
492a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
492b0 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
492c0 68 69 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73 his machine uses
492d0 20 45 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20 EBCDIC. (Yes,
492e0 62 65 6c 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a believe it or.**
492f0 20 6e 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20 not, there are
49300 73 74 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f still machines o
49310 75 74 20 74 68 65 72 65 20 74 68 61 74 20 75 73 ut there that us
49320 65 20 45 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 e EBCDIC.).*/.#i
49330 66 20 27 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a f 'A' == '\301'.
49340 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
49350 45 42 43 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 EBCDIC 1.#else.#
49360 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
49370 53 43 49 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f SCII 1.#endif../
49380 2a 0a 2a 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 *.** Integers of
49390 20 6b 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 known sizes. T
493a0 68 65 73 65 20 74 79 70 65 64 65 66 73 20 6d 69 hese typedefs mi
493b0 67 68 74 20 63 68 61 6e 67 65 20 66 6f 72 20 61 ght change for a
493c0 72 63 68 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 rchitectures.**
493d0 77 68 65 72 65 20 74 68 65 20 73 69 7a 65 73 20 where the sizes
493e0 76 65 72 79 2e 20 20 50 72 65 70 72 6f 63 65 73 very. Preproces
493f0 73 6f 72 20 6d 61 63 72 6f 73 20 61 72 65 20 61 sor macros are a
49400 76 61 69 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 vailable so that
49410 20 74 68 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 the.** types ca
49420 6e 20 62 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c n be convenientl
49430 79 20 72 65 64 65 66 69 6e 65 64 20 61 74 20 63 y redefined at c
49440 6f 6d 70 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 ompile-type. Li
49450 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
49460 20 20 20 20 20 20 20 63 63 20 27 2d 44 55 49 4e cc '-DUIN
49470 54 50 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c TPTR_TYPE=long l
49480 6f 6e 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a ong int' ....*/.
49490 23 69 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 #ifndef UINT32_T
494a0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
494b0 5f 55 49 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 _UINT32_T.# def
494c0 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 ine UINT32_TYPE
494d0 75 69 6e 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a uint32_t.# else.
494e0 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 # define UINT32
494f0 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 69 _TYPE unsigned i
49500 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 nt.# endif.#endi
49510 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 31 36 f.#ifndef UINT16
49520 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 _TYPE.# ifdef HA
49530 56 45 5f 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 VE_UINT16_T.# d
49540 65 66 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 efine UINT16_TYP
49550 45 20 75 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 E uint16_t.# els
49560 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 e.# define UINT
49570 31 36 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 16_TYPE unsigned
49580 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 short int.# end
49590 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 if.#endif.#ifnde
495a0 66 20 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 f INT16_TYPE.# i
495b0 66 64 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f fdef HAVE_INT16_
495c0 54 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31 T.# define INT1
495d0 36 5f 54 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 6_TYPE int16_t.#
495e0 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 else.# define
495f0 49 4e 54 31 36 5f 54 59 50 45 20 73 68 6f 72 74 INT16_TYPE short
49600 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e int.# endif.#en
49610 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 dif.#ifndef UINT
49620 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 8_TYPE.# ifdef H
49630 41 56 45 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 AVE_UINT8_T.# d
49640 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 efine UINT8_TYPE
49650 20 75 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a uint8_t.# else.
49660 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f # define UINT8_
49670 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 TYPE unsigned ch
49680 61 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 ar.# endif.#endi
49690 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 f.#ifndef INT8_T
496a0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
496b0 5f 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 6e _INT8_T.# defin
496c0 65 20 49 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 e INT8_TYPE int8
496d0 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 _t.# else.# def
496e0 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69 ine INT8_TYPE si
496f0 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 gned char.# endi
49700 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 f.#endif.#ifndef
49710 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 LONGDOUBLE_TYPE
49720 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f .# define LONGDO
49730 55 42 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 UBLE_TYPE long d
49740 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 ouble.#endif.typ
49750 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 edef sqlite_int6
49760 34 20 69 36 34 3b 20 20 20 20 20 20 20 20 20 20 4 i64;
49770 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 8-byte signed
49780 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
49790 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 def sqlite_uint6
497a0 34 20 75 36 34 3b 20 20 20 20 20 20 20 20 20 2f 4 u64; /
497b0 2a 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 * 8-byte unsigne
497c0 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 d integer */.typ
497d0 65 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 edef UINT32_TYPE
497e0 20 75 33 32 3b 20 20 20 20 20 20 20 20 20 20 20 u32;
497f0 2f 2a 20 34 2d 62 79 74 65 20 75 6e 73 69 67 6e /* 4-byte unsign
49800 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 ed integer */.ty
49810 70 65 64 65 66 20 55 49 4e 54 31 36 5f 54 59 50 pedef UINT16_TYP
49820 45 20 75 31 36 3b 20 20 20 20 20 20 20 20 20 20 E u16;
49830 20 2f 2a 20 32 2d 62 79 74 65 20 75 6e 73 69 67 /* 2-byte unsig
49840 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 ned integer */.t
49850 79 70 65 64 65 66 20 49 4e 54 31 36 5f 54 59 50 ypedef INT16_TYP
49860 45 20 69 31 36 3b 20 20 20 20 20 20 20 20 20 20 E i16;
49870 20 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e /* 2-byte sign
49880 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 ed integer */.ty
49890 70 65 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 pedef UINT8_TYPE
498a0 20 75 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 u8;
498b0 20 2f 2a 20 31 2d 62 79 74 65 20 75 6e 73 69 67 /* 1-byte unsig
498c0 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 ned integer */.t
498d0 79 70 65 64 65 66 20 49 4e 54 38 5f 54 59 50 45 ypedef INT8_TYPE
498e0 20 69 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 i8;
498f0 20 20 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e /* 1-byte sign
49900 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f ed integer */../
49910 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f *.** SQLITE_MAX_
49920 55 33 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e U32 is a u64 con
49930 73 74 61 6e 74 20 74 68 61 74 20 69 73 20 74 68 stant that is th
49940 65 20 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61 e maximum u64 va
49950 6c 75 65 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 lue.** that can
49960 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75 be stored in a u
49970 33 32 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 32 without loss
49980 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 76 61 of data. The va
49990 6c 75 65 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 lue.** is 0x0000
499a0 30 30 30 30 66 66 66 66 66 66 66 66 2e 20 20 42 0000ffffffff. B
499b0 75 74 20 62 65 63 61 75 73 65 20 6f 66 20 71 75 ut because of qu
499c0 69 72 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d irks of some com
499d0 70 69 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 pilers, we.** ha
499e0 76 65 20 74 6f 20 73 70 65 63 69 66 79 20 74 68 ve to specify th
499f0 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 6c e value in the l
49a00 65 73 73 20 69 6e 74 75 69 74 69 76 65 20 6d 61 ess intuitive ma
49a10 6e 6e 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 nner shown:.*/.#
49a20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
49a30 58 5f 55 33 32 20 20 28 28 28 28 75 36 34 29 31 X_U32 ((((u64)1
49a40 29 3c 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a )<<32)-1)../*.**
49a50 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 Macros to deter
49a60 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68 65 mine whether the
49a70 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67 20 machine is big
49a80 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e or little endian
49a90 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 61 ,.** evaluated a
49aa0 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 t runtime..*/.#i
49ab0 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c fdef SQLITE_AMAL
49ac0 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f GAMATION.SQLITE_
49ad0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e PRIVATE const in
49ae0 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 t sqlite3one = 1
49af0 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 ;.#else.SQLITE_P
49b00 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 RIVATE const int
49b10 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e sqlite3one;.#en
49b20 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 dif.#if defined(
49b30 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 i386) || defined
49b40 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 (__i386__) || de
49b50 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a fined(_M_IX86)\.
49b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49b70 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
49b80 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 defined(__x86_64
49b90 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 ) || defined(__x
49ba0 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 86_64__).# defin
49bb0 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 e SQLITE_BIGENDI
49bc0 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 AN 0.# define
49bd0 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e SQLITE_LITTLEEN
49be0 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 DIAN 1.# define
49bf0 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 SQLITE_UTF16NATI
49c00 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 VE SQLITE_UTF16
49c10 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e LE.#else.# defin
49c20 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 e SQLITE_BIGENDI
49c30 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a 29 AN (*(char *)
49c40 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 (&sqlite3one)==0
49c50 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
49c60 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 E_LITTLEENDIAN (
49c70 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 *(char *)(&sqlit
49c80 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 e3one)==1).# def
49c90 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 ine SQLITE_UTF16
49ca0 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 5f 42 NATIVE (SQLITE_B
49cb0 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f IGENDIAN?SQLITE_
49cc0 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 UTF16BE:SQLITE_U
49cd0 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a TF16LE).#endif..
49ce0 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 /*.** Constants
49cf0 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 for the largest
49d00 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 and smallest pos
49d10 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 69 67 sible 64-bit sig
49d20 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a ned integers..**
49d30 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 61 72 These macros ar
49d40 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77 6f e designed to wo
49d50 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 rk correctly on
49d60 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e 64 20 both 32-bit and
49d70 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 64-bit.** compil
49d80 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ers..*/.#define
49d90 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 20 28 LARGEST_INT64 (
49da0 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 69 36 0xffffffff|(((i6
49db0 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 33 4)0x7fffffff)<<3
49dc0 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 2)).#define SMAL
49dd0 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 69 36 LEST_INT64 (((i6
49de0 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 4)-1) - LARGEST_
49df0 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 INT64)../* .** R
49e00 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 72 ound up a number
49e10 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 72 to the next lar
49e20 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 ger multiple of
49e30 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 8. This is used
49e40 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 .** to force 8-b
49e50 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e yte alignment on
49e60 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65 63 64-bit architec
49e70 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e tures..*/.#defin
49e80 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 20 20 e ROUND8(x)
49e90 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a (((x)+7)&~7)../*
49ea0 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 .** Round down t
49eb0 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 6d 75 o the nearest mu
49ec0 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 ltiple of 8.*/.#
49ed0 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e define ROUNDDOWN
49ee0 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 8(x) ((x)&~7)../
49ef0 2a 0a 2a 2a 20 41 73 73 65 72 74 20 74 68 61 74 *.** Assert that
49f00 20 74 68 65 20 70 6f 69 6e 74 65 72 20 58 20 69 the pointer X i
49f10 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 s aligned to an
49f20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 8-byte boundary.
49f30 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 49 47 48 .*/.#define EIGH
49f40 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
49f50 28 58 29 20 20 20 28 28 28 28 63 68 61 72 2a 29 (X) ((((char*)
49f60 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30 29 26 (X) - (char*)0)&
49f70 37 29 3d 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 7)==0).../*.** A
49f80 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
49f90 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
49fa0 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f cture is used to
49fb0 20 73 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d store the busy-
49fc0 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 handler.** callb
49fd0 61 63 6b 20 66 6f 72 20 61 20 67 69 76 65 6e 20 ack for a given
49fe0 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a sqlite handle. .
49ff0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
4a000 2e 62 75 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d .busyHandler mem
4a010 62 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ber of the sqlit
4a020 65 20 73 74 72 75 63 74 20 63 6f 6e 74 61 69 6e e struct contain
4a030 73 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 63 61 s the busy.** ca
4a040 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65 20 64 llback for the d
4a050 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 atabase handle.
4a060 45 61 63 68 20 70 61 67 65 72 20 6f 70 65 6e 65 Each pager opene
4a070 64 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 d via the sqlite
4a080 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 70 61 .** handle is pa
4a090 73 73 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74 ssed a pointer t
4a0a0 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e o sqlite.busyHan
4a0b0 64 6c 65 72 2e 20 54 68 65 20 62 75 73 79 2d 68 dler. The busy-h
4a0c0 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 andler.** callba
4a0d0 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 ck is currently
4a0e0 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f invoked only fro
4a0f0 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 2e 63 m within pager.c
4a100 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
4a110 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 uct BusyHandler
4a120 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72 BusyHandler;.str
4a130 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 uct BusyHandler
4a140 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 {. int (*xFunc)
4a150 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f (void *,int); /
4a160 2a 20 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 * The busy callb
4a170 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ack */. void *p
4a180 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Arg;
4a190 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 /* First arg
4a1a0 20 74 6f 20 62 75 73 79 20 63 61 6c 6c 62 61 63 to busy callbac
4a1b0 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79 k */. int nBusy
4a1c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4a1d0 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64 /* Incremented
4a1e0 20 77 69 74 68 20 65 61 63 68 20 62 75 73 79 20 with each busy
4a1f0 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a call */.};../*.*
4a200 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 * Name of the ma
4a210 73 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 ster database ta
4a220 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72 ble. The master
4a230 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a database table.
4a240 2a 2a 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 ** is a special
4a250 74 61 62 6c 65 20 74 68 61 74 20 68 6f 6c 64 73 table that holds
4a260 20 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 61 the names and a
4a270 74 74 72 69 62 75 74 65 73 20 6f 66 20 61 6c 6c ttributes of all
4a280 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c 65 73 20 .** user tables
4a290 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a and indices..*/.
4a2a0 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e #define MASTER_N
4a2b0 41 4d 45 20 20 20 20 20 20 20 22 73 71 6c 69 74 AME "sqlit
4a2c0 65 5f 6d 61 73 74 65 72 22 0a 23 64 65 66 69 6e e_master".#defin
4a2d0 65 20 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 e TEMP_MASTER_NA
4a2e0 4d 45 20 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 ME "sqlite_temp
4a2f0 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 _master"../*.**
4a300 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 The root-page of
4a310 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 the master data
4a320 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 base table..*/.#
4a330 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f define MASTER_RO
4a340 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a OT 1../*.*
4a350 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 * The name of th
4a360 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a e schema table..
4a370 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d */.#define SCHEM
4a380 41 5f 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f A_TABLE(x) ((!O
4a390 4d 49 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d MIT_TEMPDB)&&(x=
4a3a0 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f =1)?TEMP_MASTER_
4a3b0 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 NAME:MASTER_NAME
4a3c0 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 )../*.** A conve
4a3d0 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 74 68 61 nience macro tha
4a3e0 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 t returns the nu
4a3f0 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 mber of elements
4a400 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e in.** an array.
4a410 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41 72 72 61 .*/.#define Arra
4a420 79 53 69 7a 65 28 58 29 20 20 20 20 28 28 69 6e ySize(X) ((in
4a430 74 29 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a t)(sizeof(X)/siz
4a440 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a eof(X[0])))../*.
4a450 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
4a460 20 76 61 6c 75 65 20 61 73 20 61 20 64 65 73 74 value as a dest
4a470 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 ructor means to
4a480 75 73 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 use sqlite3DbFre
4a490 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 e()..** This is
4a4a0 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 an internal exte
4a4b0 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f nsion to SQLITE_
4a4c0 53 54 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54 STATIC and SQLIT
4a4d0 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a E_TRANSIENT..*/.
4a4e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
4a4f0 59 4e 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74 YNAMIC ((sqlit
4a500 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 e3_destructor_ty
4a510 70 65 29 73 71 6c 69 74 65 33 44 62 46 72 65 65 pe)sqlite3DbFree
4a520 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 )../*.** When SQ
4a530 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 LITE_OMIT_WSD is
4a540 20 64 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 defined, it mea
4a550 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 72 67 ns that the targ
4a560 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 et platform does
4a570 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 .** not support
4a580 57 72 69 74 61 62 6c 65 20 53 74 61 74 69 63 20 Writable Static
4a590 44 61 74 61 20 28 57 53 44 29 20 73 75 63 68 20 Data (WSD) such
4a5a0 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 as global and st
4a5b0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a atic variables..
4a5c0 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 ** All variables
4a5d0 20 6d 75 73 74 20 65 69 74 68 65 72 20 62 65 20 must either be
4a5e0 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20 on the stack or
4a5f0 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f dynamically allo
4a600 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 cated from.** th
4a610 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53 e heap. When WS
4a620 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 D is unsupported
4a630 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 64 , the variable d
4a640 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74 eclarations scat
4a650 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 tered.** through
4a660 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65 20 63 out the SQLite c
4a670 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 ode must become
4a680 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61 constants instea
4a690 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57 d. The SQLITE_W
4a6a0 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 SD.** macro is u
4a6b0 73 65 64 20 66 6f 72 20 74 68 69 73 20 70 75 72 sed for this pur
4a6c0 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 pose. And inste
4a6d0 61 64 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e ad of referencin
4a6e0 67 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a g the variable.*
4a6f0 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75 * directly, we u
4a700 73 65 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 se its constant
4a710 61 73 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b as a key to look
4a720 75 70 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 up the run-time
4a730 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 allocated.** buf
4a740 66 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72 fer that holds r
4a750 65 61 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54 eal variable. T
4a760 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 he constant is a
4a770 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69 lso the initiali
4a780 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 zer.** for the r
4a790 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 un-time allocate
4a7a0 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 d buffer..**.**
4a7b0 49 6e 20 74 68 65 20 75 73 75 61 6c 20 63 61 73 In the usual cas
4a7c0 65 20 77 68 65 72 65 20 57 53 44 20 69 73 20 73 e where WSD is s
4a7d0 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 53 51 upported, the SQ
4a7e0 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f LITE_WSD and GLO
4a7f0 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65 BAL.** macros be
4a800 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 come no-ops and
4a810 68 61 76 65 20 7a 65 72 6f 20 70 65 72 66 6f 72 have zero perfor
4a820 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f mance impact..*/
4a830 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
4a840 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 66 69 6e MIT_WSD. #defin
4a850 65 20 53 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e e SQLITE_WSD con
4a860 73 74 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f st. #define GLO
4a870 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 BAL(t,v) (*(t*)s
4a880 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 qlite3_wsd_find(
4a890 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 73 69 7a (void*)&(v), siz
4a8a0 65 6f 66 28 76 29 29 29 0a 20 20 23 64 65 66 69 eof(v))). #defi
4a8b0 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ne sqlite3Global
4a8c0 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 Config GLOBAL(st
4a8d0 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 ruct Sqlite3Conf
4a8e0 69 67 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 ig, sqlite3Confi
4a8f0 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 g).SQLITE_API
4a900 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f int sqlite3_wsd_
4a910 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 init(int N, int
4a920 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 J);.SQLITE_API
4a930 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 void *sqlite3_w
4a940 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c sd_find(void *K,
4a950 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 int L);.#else.
4a960 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f #define SQLITE_
4a970 57 53 44 20 0a 20 20 23 64 65 66 69 6e 65 20 47 WSD . #define G
4a980 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a 20 20 23 LOBAL(t,v) v. #
4a990 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c define sqlite3Gl
4a9a0 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74 obalConfig sqlit
4a9b0 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a e3Config.#endif.
4a9c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
4a9d0 77 69 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20 wing macros are
4a9e0 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 used to suppress
4a9f0 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e compiler warnin
4aa00 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b gs and to.** mak
4aa10 65 20 69 74 20 63 6c 65 61 72 20 74 6f 20 68 75 e it clear to hu
4aa20 6d 61 6e 20 72 65 61 64 65 72 73 20 77 68 65 6e man readers when
4aa30 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 a function para
4aa40 6d 65 74 65 72 20 69 73 20 64 65 6c 69 62 65 72 meter is deliber
4aa50 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75 ately .** left u
4aa60 6e 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 nused within the
4aa70 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 74 body of a funct
4aa80 69 6f 6e 2e 20 54 68 69 73 20 75 73 75 61 6c 6c ion. This usuall
4aa90 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a y happens when.*
4aaa0 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 * a function is
4aab0 63 61 6c 6c 65 64 20 76 69 61 20 61 20 66 75 6e called via a fun
4aac0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46 ction pointer. F
4aad0 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 20 0a or example the .
4aae0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
4aaf0 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61 67 67 72 n of an SQL aggr
4ab00 65 67 61 74 65 20 73 74 65 70 20 63 61 6c 6c 62 egate step callb
4ab10 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75 73 65 20 ack may not use
4ab20 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 the.** parameter
4ab30 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 indicating the
4ab40 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 number of argume
4ab50 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 nts passed to th
4ab60 65 20 61 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20 e aggregate,.**
4ab70 69 66 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 if it knows that
4ab80 20 74 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65 this is enforce
4ab90 64 20 65 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a d elsewhere..**.
4aba0 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69 ** When a functi
4abb0 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 on parameter is
4abc0 6e 6f 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20 not used at all
4abd0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 within the body
4abe0 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a of a function,.*
4abf0 2a 20 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c * it is generall
4ac00 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65 64 y named "NotUsed
4ac10 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 32 22 20 " or "NotUsed2"
4ac20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 73 20 65 to make things e
4ac30 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 ven clearer..**
4ac40 48 6f 77 65 76 65 72 2c 20 74 68 65 73 65 20 6d However, these m
4ac50 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62 acros may also b
4ac60 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 e used to suppre
4ac70 73 73 20 77 61 72 6e 69 6e 67 73 20 72 65 6c 61 ss warnings rela
4ac80 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 ted to.** parame
4ac90 74 65 72 73 20 74 68 61 74 20 6d 61 79 20 6f 72 ters that may or
4aca0 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 may not be used
4acb0 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f depending on co
4acc0 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e mpilation option
4acd0 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c s..** For exampl
4ace0 65 20 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65 e those paramete
4acf0 72 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 rs only used in
4ad00 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
4ad10 6e 74 73 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a nts. In these.**
4ad20 20 63 61 73 65 73 20 74 68 65 20 70 61 72 61 6d cases the param
4ad30 65 74 65 72 73 20 61 72 65 20 6e 61 6d 65 64 20 eters are named
4ad40 61 73 20 70 65 72 20 74 68 65 20 75 73 75 61 6c as per the usual
4ad50 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f conventions..*/
4ad60 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f .#define UNUSED_
4ad70 50 41 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f PARAMETER(x) (vo
4ad80 69 64 29 28 78 29 0a 23 64 65 66 69 6e 65 20 55 id)(x).#define U
4ad90 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
4ada0 28 78 2c 79 29 20 55 4e 55 53 45 44 5f 50 41 52 (x,y) UNUSED_PAR
4adb0 41 4d 45 54 45 52 28 78 29 2c 55 4e 55 53 45 44 AMETER(x),UNUSED
4adc0 5f 50 41 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f _PARAMETER(y)../
4add0 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 *.** Forward ref
4ade0 65 72 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63 erences to struc
4adf0 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 tures.*/.typedef
4ae00 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 struct AggInfo
4ae10 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 AggInfo;.typedef
4ae20 20 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 struct AuthCont
4ae30 65 78 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b ext AuthContext;
4ae40 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4ae50 41 75 74 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f AutoincInfo Auto
4ae60 69 6e 63 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 incInfo;.typedef
4ae70 20 73 74 72 75 63 74 20 42 69 74 76 65 63 20 42 struct Bitvec B
4ae80 69 74 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73 itvec;.typedef s
4ae90 74 72 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 truct RowSet Row
4aea0 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Set;.typedef str
4aeb0 75 63 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c uct CollSeq Coll
4aec0 53 65 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Seq;.typedef str
4aed0 75 63 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d uct Column Colum
4aee0 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 n;.typedef struc
4aef0 74 20 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 t Db Db;.typedef
4af00 20 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 struct Schema S
4af10 63 68 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 chema;.typedef s
4af20 74 72 75 63 74 20 45 78 70 72 20 45 78 70 72 3b truct Expr Expr;
4af30 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4af40 45 78 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 ExprList ExprLis
4af50 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4af60 74 20 45 78 70 72 53 70 61 6e 20 45 78 70 72 53 t ExprSpan ExprS
4af70 70 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 pan;.typedef str
4af80 75 63 74 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 uct FKey FKey;.t
4af90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 75 ypedef struct Fu
4afa0 6e 63 44 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 ncDef FuncDef;.t
4afb0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 75 ypedef struct Fu
4afc0 6e 63 44 65 66 48 61 73 68 20 46 75 6e 63 44 65 ncDefHash FuncDe
4afd0 66 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 fHash;.typedef s
4afe0 74 72 75 63 74 20 49 64 4c 69 73 74 20 49 64 4c truct IdList IdL
4aff0 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ist;.typedef str
4b000 75 63 74 20 49 6e 64 65 78 20 49 6e 64 65 78 3b uct Index Index;
4b010 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b020 49 6e 64 65 78 53 61 6d 70 6c 65 20 49 6e 64 65 IndexSample Inde
4b030 78 53 61 6d 70 6c 65 3b 0a 74 79 70 65 64 65 66 xSample;.typedef
4b040 20 73 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73 struct KeyClass
4b050 20 4b 65 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 KeyClass;.typed
4b060 65 66 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 ef struct KeyInf
4b070 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 o KeyInfo;.typed
4b080 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 ef struct Lookas
4b090 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 ide Lookaside;.t
4b0a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f ypedef struct Lo
4b0b0 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b okasideSlot Look
4b0c0 61 73 69 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 asideSlot;.typed
4b0d0 65 66 20 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 ef struct Module
4b0e0 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 Module;.typedef
4b0f0 20 73 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 struct NameCont
4b100 65 78 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b ext NameContext;
4b110 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b120 50 61 72 73 65 20 50 61 72 73 65 3b 0a 74 79 70 Parse Parse;.typ
4b130 65 64 65 66 20 73 74 72 75 63 74 20 53 61 76 65 edef struct Save
4b140 70 6f 69 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b point Savepoint;
4b150 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b160 53 65 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 Select Select;.t
4b170 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 72 ypedef struct Sr
4b180 63 4c 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 cList SrcList;.t
4b190 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74 ypedef struct St
4b1a0 72 41 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b rAccum StrAccum;
4b1b0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b1c0 54 61 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 Table Table;.typ
4b1d0 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62 6c edef struct Tabl
4b1e0 65 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b eLock TableLock;
4b1f0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b200 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 Token Token;.typ
4b210 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 edef struct Trig
4b220 67 65 72 50 72 67 20 54 72 69 67 67 65 72 50 72 gerPrg TriggerPr
4b230 67 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 g;.typedef struc
4b240 74 20 54 72 69 67 67 65 72 53 74 65 70 20 54 72 t TriggerStep Tr
4b250 69 67 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 iggerStep;.typed
4b260 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 ef struct Trigge
4b270 72 20 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64 r Trigger;.typed
4b280 65 66 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b ef struct Unpack
4b290 65 64 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 edRecord Unpacke
4b2a0 64 52 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 dRecord;.typedef
4b2b0 20 73 74 72 75 63 74 20 56 54 61 62 6c 65 20 56 struct VTable V
4b2c0 54 61 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 Table;.typedef s
4b2d0 74 72 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c truct Walker Wal
4b2e0 6b 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ker;.typedef str
4b2f0 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68 uct WherePlan Wh
4b300 65 72 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 erePlan;.typedef
4b310 20 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 struct WhereInf
4b320 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 o WhereInfo;.typ
4b330 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 edef struct Wher
4b340 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 eLevel WhereLeve
4b350 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 l;../*.** Defer
4b360 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20 sourcing vdbe.h
4b370 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69 and btree.h unti
4b380 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 38 22 l after the "u8"
4b390 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 and .** "BusyHa
4b3a0 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e ndler" typedefs.
4b3b0 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 vdbe.h also req
4b3c0 75 69 72 65 73 20 61 20 66 65 77 20 6f 66 20 74 uires a few of t
4b3d0 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 he opaque.** poi
4b3e0 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e nter types (i.e.
4b3f0 20 46 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65 FuncDef) define
4b400 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a d above..*/./***
4b410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
4b420 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74 ude btree.h in t
4b430 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
4b440 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
4b450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
4b460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
4b470 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a n file btree.h *
4b480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
4b4b0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 * 2001 September
4b4c0 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 15.**.** The au
4b4d0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
4b4e0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
4b4f0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
4b500 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
4b510 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
4b520 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
4b530 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
4b540 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
4b550 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
4b560 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
4b570 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
4b580 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
4b590 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
4b5a0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
4b5b0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
4b5c0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
4b5d0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
4b5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b620 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 **.** This heade
4b630 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 r file defines t
4b640 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 he interface tha
4b650 74 20 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54 t the sqlite B-T
4b660 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 ree file.** subs
4b670 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d ystem. See comm
4b680 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72 ents in the sour
4b690 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 ce code for a de
4b6a0 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 tailed descripti
4b6b0 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 on.** of what ea
4b6c0 63 68 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 ch interface rou
4b6d0 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a tine does..**.**
4b6e0 20 40 28 23 29 20 24 49 64 3a 20 62 74 72 65 65 @(#) $Id: btree
4b6f0 2e 68 2c 76 20 31 2e 31 32 30 20 32 30 30 39 2f .h,v 1.120 2009/
4b700 30 37 2f 32 32 20 30 30 3a 33 35 3a 32 34 20 64 07/22 00:35:24 d
4b710 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e rh Exp $.*/.#ifn
4b720 64 65 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64 def _BTREE_H_.#d
4b730 65 66 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a efine _BTREE_H_.
4b740 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64 ./* TODO: This d
4b750 65 66 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73 efinition is jus
4b760 74 20 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74 t included so ot
4b770 68 65 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 her modules comp
4b780 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73 ile. It.** needs
4b790 20 74 6f 20 62 65 20 72 65 76 69 73 69 74 65 64 to be revisited
4b7a0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
4b7b0 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 ITE_N_BTREE_META
4b7c0 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 10../*.** If de
4b7d0 66 69 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 fined as non-zer
4b7e0 6f 2c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 o, auto-vacuum i
4b7f0 73 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 s enabled by def
4b800 61 75 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a ault. Otherwise.
4b810 2a 2a 20 69 74 20 6d 75 73 74 20 62 65 20 74 75 ** it must be tu
4b820 72 6e 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68 rned on for each
4b830 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 database using
4b840 22 50 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 "PRAGMA auto_vac
4b850 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 uum = 1"..*/.#if
4b860 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
4b870 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 ULT_AUTOVACUUM.
4b880 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f #define SQLITE_
4b890 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 DEFAULT_AUTOVACU
4b8a0 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 UM 0.#endif..#de
4b8b0 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 fine BTREE_AUTOV
4b8c0 41 43 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 ACUUM_NONE 0
4b8d0 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f /* Do not do
4b8e0 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a auto-vacuum */.
4b8f0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 #define BTREE_AU
4b900 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20 TOVACUUM_FULL 1
4b910 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c /* Do ful
4b920 6c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f l auto-vacuum */
4b930 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 .#define BTREE_A
4b940 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32 UTOVACUUM_INCR 2
4b950 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 /* Incre
4b960 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f mental vacuum */
4b970 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 ../*.** Forward
4b980 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 declarations of
4b990 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 structure.*/.typ
4b9a0 65 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65 edef struct Btre
4b9b0 65 20 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66 e Btree;.typedef
4b9c0 20 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 struct BtCursor
4b9d0 20 42 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 BtCursor;.typed
4b9e0 65 66 20 73 74 72 75 63 74 20 42 74 53 68 61 72 ef struct BtShar
4b9f0 65 64 20 42 74 53 68 61 72 65 64 3b 0a 74 79 70 ed BtShared;.typ
4ba00 65 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65 edef struct Btre
4ba10 65 4d 75 74 65 78 41 72 72 61 79 20 42 74 72 65 eMutexArray Btre
4ba20 65 4d 75 74 65 78 41 72 72 61 79 3b 0a 0a 2f 2a eMutexArray;../*
4ba30 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 .** This structu
4ba40 72 65 20 72 65 63 6f 72 64 73 20 61 6c 6c 20 6f re records all o
4ba50 66 20 74 68 65 20 42 74 72 65 65 73 20 74 68 61 f the Btrees tha
4ba60 74 20 6e 65 65 64 20 74 6f 20 68 6f 6c 64 0a 2a t need to hold.*
4ba70 2a 20 61 20 6d 75 74 65 78 20 62 65 66 6f 72 65 * a mutex before
4ba80 20 77 65 20 65 6e 74 65 72 20 73 71 6c 69 74 65 we enter sqlite
4ba90 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 54 68 3VdbeExec(). Th
4baa0 65 20 42 74 72 65 65 73 20 61 72 65 0a 2a 2a 20 e Btrees are.**
4bab0 61 72 65 20 70 6c 61 63 65 64 20 69 6e 20 61 42 are placed in aB
4bac0 74 72 65 65 5b 5d 20 69 6e 20 6f 72 64 65 72 20 tree[] in order
4bad0 6f 66 20 61 42 74 72 65 65 5b 5d 2d 3e 70 42 74 of aBtree[]->pBt
4bae0 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 . That way,.**
4baf0 77 65 20 63 61 6e 20 61 6c 77 61 79 73 20 6c 6f we can always lo
4bb00 63 6b 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 ck and unlock th
4bb10 65 6d 20 61 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a em all quickly..
4bb20 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 65 65 4d */.struct BtreeM
4bb30 75 74 65 78 41 72 72 61 79 20 7b 0a 20 20 69 6e utexArray {. in
4bb40 74 20 6e 4d 75 74 65 78 3b 0a 20 20 42 74 72 65 t nMutex;. Btre
4bb50 65 20 2a 61 42 74 72 65 65 5b 53 51 4c 49 54 45 e *aBtree[SQLITE
4bb60 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 31 5d _MAX_ATTACHED+1]
4bb70 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 ;.};...SQLITE_PR
4bb80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4bb90 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 3BtreeOpen(. co
4bba0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
4bbb0 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f ame, /* Name o
4bbc0 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 f database file
4bbd0 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c to open */. sql
4bbe0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 ite3 *db,
4bbf0 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 /* Associa
4bc00 74 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e ted database con
4bc10 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 nection */. Btr
4bc20 65 65 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 ee **ppBtree,
4bc30 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
4bc40 6f 70 65 6e 20 42 74 72 65 65 2a 20 68 65 72 65 open Btree* here
4bc50 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c */. int flags,
4bc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4bc70 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 * Flags */. int
4bc80 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 vfsFlags
4bc90 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 /* Flags p
4bca0 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f assed through to
4bcb0 20 56 46 53 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a VFS open */.);.
4bcc0 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 ./* The flags pa
4bcd0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
4bce0 65 33 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20 e3BtreeOpen can
4bcf0 62 65 20 74 68 65 20 62 69 74 77 69 73 65 20 6f be the bitwise o
4bd00 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c r of the.** foll
4bd10 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a owing values..**
4bd20 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65 .** NOTE: These
4bd30 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 values must mat
4bd40 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e ch the correspon
4bd50 64 69 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75 ding PAGER_ valu
4bd60 65 73 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 es in.** pager.h
4bd70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 ..*/.#define BTR
4bd80 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 EE_OMIT_JOURNAL
4bd90 20 31 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 1 /* Do not us
4bda0 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 e journal. No a
4bdb0 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 rgument */.#defi
4bdc0 6e 65 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 ne BTREE_NO_READ
4bdd0 4c 4f 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69 LOCK 2 /* Omi
4bde0 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 t readlocks on r
4bdf0 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f eadonly files */
4be00 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d .#define BTREE_M
4be10 45 4d 4f 52 59 20 20 20 20 20 20 20 20 34 20 20 EMORY 4
4be20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e /* In-memory DB.
4be30 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f No argument */
4be40 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 .#define BTREE_R
4be50 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 38 20 20 EADONLY 8
4be60 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 /* Open the data
4be70 62 61 73 65 20 69 6e 20 72 65 61 64 2d 6f 6e 6c base in read-onl
4be80 79 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e y mode */.#defin
4be90 65 20 42 54 52 45 45 5f 52 45 41 44 57 52 49 54 e BTREE_READWRIT
4bea0 45 20 20 20 20 31 36 20 20 2f 2a 20 4f 70 65 6e E 16 /* Open
4beb0 20 66 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e for both readin
4bec0 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f g and writing */
4bed0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 43 .#define BTREE_C
4bee0 52 45 41 54 45 20 20 20 20 20 20 20 33 32 20 20 REATE 32
4bef0 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 64 61 /* Create the da
4bf00 74 61 62 61 73 65 20 69 66 20 69 74 20 64 6f 65 tabase if it doe
4bf10 73 20 6e 6f 74 20 65 78 69 73 74 20 2a 2f 0a 0a s not exist */..
4bf20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4bf30 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
4bf40 6c 6f 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 lose(Btree*);.SQ
4bf50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4bf60 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
4bf70 43 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 2a CacheSize(Btree*
4bf80 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
4bf90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4bfa0 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 3BtreeSetSafetyL
4bfb0 65 76 65 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c evel(Btree*,int,
4bfc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4bfd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4bfe0 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 BtreeSyncDisable
4bff0 64 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 d(Btree*);.SQLIT
4c000 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c010 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67 lite3BtreeSetPag
4c020 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 eSize(Btree *p,
4c030 69 6e 74 20 6e 50 61 67 65 73 69 7a 65 2c 20 69 int nPagesize, i
4c040 6e 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 nt nReserve, int
4c050 20 65 46 69 78 29 3b 0a 53 51 4c 49 54 45 5f 50 eFix);.SQLITE_P
4c060 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4c070 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 e3BtreeGetPageSi
4c080 7a 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 ze(Btree*);.SQLI
4c090 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c0a0 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 qlite3BtreeMaxPa
4c0b0 67 65 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 geCount(Btree*,i
4c0c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
4c0d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c0e0 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 treeGetReserve(B
4c0f0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
4c100 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4c110 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 e3BtreeSetAutoVa
4c120 63 75 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e cuum(Btree *, in
4c130 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4c140 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c150 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d reeGetAutoVacuum
4c160 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 (Btree *);.SQLIT
4c170 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c180 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
4c190 72 61 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 rans(Btree*,int)
4c1a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c1b0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c1c0 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 eCommitPhaseOne(
4c1d0 42 74 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 Btree*, const ch
4c1e0 61 72 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 ar *zMaster);.SQ
4c1f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4c200 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
4c210 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 mitPhaseTwo(Btre
4c220 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
4c230 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c240 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 treeCommit(Btree
4c250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4c260 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c270 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 reeRollback(Btre
4c280 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
4c290 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c2a0 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 treeBeginStmt(Bt
4c2b0 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ree*,int);.SQLIT
4c2c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c2d0 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 lite3BtreeCreate
4c2e0 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e Table(Btree*, in
4c2f0 74 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a t*, int flags);.
4c300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c310 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
4c320 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 sInTrans(Btree*)
4c330 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c340 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c350 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 eIsInReadTrans(B
4c360 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
4c370 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4c380 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 e3BtreeIsInBacku
4c390 70 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 p(Btree*);.SQLIT
4c3a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
4c3b0 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 sqlite3BtreeSche
4c3c0 6d 61 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c ma(Btree *, int,
4c3d0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 void(*)(void *)
4c3e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c3f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c400 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 eeSchemaLocked(B
4c410 74 72 65 65 20 2a 70 42 74 72 65 65 29 3b 0a 53 tree *pBtree);.S
4c420 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c430 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f t sqlite3BtreeLo
4c440 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 ckTable(Btree *p
4c450 42 74 72 65 65 2c 20 69 6e 74 20 69 54 61 62 2c Btree, int iTab,
4c460 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 u8 isWriteLock)
4c470 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c480 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c490 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 eSavepoint(Btree
4c4a0 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a *, int, int);..
4c4b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
4c4c0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
4c4d0 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 e3BtreeGetFilena
4c4e0 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c me(Btree *);.SQL
4c4f0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
4c500 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 t char *sqlite3B
4c510 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 treeGetJournalna
4c520 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c me(Btree *);.SQL
4c530 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c540 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 sqlite3BtreeCopy
4c550 46 69 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 File(Btree *, Bt
4c560 72 65 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f ree *);..SQLITE_
4c570 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c580 74 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 te3BtreeIncrVacu
4c590 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a um(Btree *);../*
4c5a0 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d The flags param
4c5b0 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 eter to sqlite3B
4c5c0 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 treeCreateTable
4c5d0 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 can be the bitwi
4c5e0 73 65 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 se OR.** of the
4c5f0 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a following flags:
4c600 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 .*/.#define BTRE
4c610 45 5f 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 E_INTKEY 1
4c620 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f /* Table has o
4c630 6e 6c 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 nly 64-bit signe
4c640 64 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a d integer keys *
4c650 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
4c660 5a 45 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 ZERODATA 2
4c670 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 /* Table has key
4c680 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 s only - no data
4c690 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
4c6a0 45 5f 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 E_LEAFDATA 4
4c6b0 20 20 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 /* Data stored
4c6c0 20 69 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e in leaves only.
4c6d0 20 20 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 Implies INTKEY
4c6e0 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 */..SQLITE_PRIV
4c6f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c700 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 treeDropTable(Bt
4c710 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 ree*, int, int*)
4c720 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c730 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c740 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 eClearTable(Btre
4c750 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a e*, int, int*);.
4c760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
4c770 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4c780 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 TripAllCursors(B
4c790 74 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 tree*, int);..SQ
4c7a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4c7b0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 d sqlite3BtreeGe
4c7c0 74 4d 65 74 61 28 42 74 72 65 65 20 2a 70 42 74 tMeta(Btree *pBt
4c7d0 72 65 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 ree, int idx, u3
4c7e0 32 20 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 2 *pValue);.SQLI
4c7f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c800 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 qlite3BtreeUpdat
4c810 65 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e eMeta(Btree*, in
4c820 74 20 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 t idx, u32 value
4c830 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 );../*.** The se
4c840 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 cond parameter t
4c850 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 o sqlite3BtreeGe
4c860 74 4d 65 74 61 20 6f 72 20 73 71 6c 69 74 65 33 tMeta or sqlite3
4c870 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 0a BtreeUpdateMeta.
4c880 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 ** should be one
4c890 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
4c8a0 67 20 76 61 6c 75 65 73 2e 20 54 68 65 20 69 6e g values. The in
4c8b0 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 72 65 teger values are
4c8c0 20 61 73 73 69 67 6e 65 64 20 0a 2a 2a 20 74 6f assigned .** to
4c8d0 20 63 6f 6e 73 74 61 6e 74 73 20 73 6f 20 74 68 constants so th
4c8e0 61 74 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 at the offset of
4c8f0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
4c900 6e 67 20 66 69 65 6c 64 20 69 6e 20 61 6e 0a 2a ng field in an.*
4c910 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 * SQLite databas
4c920 65 20 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 e header may be
4c930 66 6f 75 6e 64 20 75 73 69 6e 67 20 74 68 65 20 found using the
4c940 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 75 6c following formul
4c950 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 6f 66 66 73 65 a:.**.** offse
4c960 74 20 3d 20 33 36 20 2b 20 28 69 64 78 20 2a 20 t = 36 + (idx *
4c970 34 29 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 4).**.** For exa
4c980 6d 70 6c 65 2c 20 74 68 65 20 66 72 65 65 2d 70 mple, the free-p
4c990 61 67 65 2d 63 6f 75 6e 74 20 66 69 65 6c 64 20 age-count field
4c9a0 69 73 20 6c 6f 63 61 74 65 64 20 61 74 20 62 79 is located at by
4c9b0 74 65 20 6f 66 66 73 65 74 20 33 36 20 6f 66 0a te offset 36 of.
4c9c0 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
4c9d0 66 69 6c 65 20 68 65 61 64 65 72 2e 20 54 68 65 file header. The
4c9e0 20 69 6e 63 72 2d 76 61 63 75 75 6d 2d 66 6c 61 incr-vacuum-fla
4c9f0 67 20 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 g field is locat
4ca00 65 64 20 61 74 0a 2a 2a 20 62 79 74 65 20 6f 66 ed at.** byte of
4ca10 66 73 65 74 20 36 34 20 28 3d 3d 20 33 36 2b 34 fset 64 (== 36+4
4ca20 2a 37 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 *7)..*/.#define
4ca30 42 54 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f BTREE_FREE_PAGE_
4ca40 43 4f 55 4e 54 20 20 20 20 20 30 0a 23 64 65 66 COUNT 0.#def
4ca50 69 6e 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41 ine BTREE_SCHEMA
4ca60 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0a _VERSION 1.
4ca70 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49 #define BTREE_FI
4ca80 4c 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20 LE_FORMAT
4ca90 20 20 32 0a 23 64 65 66 69 6e 65 20 42 54 52 45 2.#define BTRE
4caa0 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f E_DEFAULT_CACHE_
4cab0 53 49 5a 45 20 20 33 0a 23 64 65 66 69 6e 65 20 SIZE 3.#define
4cac0 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f BTREE_LARGEST_RO
4cad0 4f 54 5f 50 41 47 45 20 20 20 34 0a 23 64 65 66 OT_PAGE 4.#def
4cae0 69 6e 65 20 42 54 52 45 45 5f 54 45 58 54 5f 45 ine BTREE_TEXT_E
4caf0 4e 43 4f 44 49 4e 47 20 20 20 20 20 20 20 35 0a NCODING 5.
4cb00 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 55 53 #define BTREE_US
4cb10 45 52 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 ER_VERSION
4cb20 20 20 36 0a 23 64 65 66 69 6e 65 20 42 54 52 45 6.#define BTRE
4cb30 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 20 20 E_INCR_VACUUM
4cb40 20 20 20 20 20 20 37 0a 0a 53 51 4c 49 54 45 5f 7..SQLITE_
4cb50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4cb60 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a te3BtreeCursor(.
4cb70 20 20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 Btree*,
4cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cb90 20 20 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 /* BTree
4cba0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 containing table
4cbb0 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e to open */. in
4cbc0 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 t iTable,
4cbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cbe0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 /* Index of r
4cbf0 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e oot page */. in
4cc00 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 t wrFlag,
4cc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cc20 20 20 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 /* 1 for writ
4cc30 69 6e 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 ing. 0 for read
4cc40 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 -only */. struc
4cc50 74 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 t KeyInfo*,
4cc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cc70 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e /* First argumen
4cc80 74 20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e t to compare fun
4cc90 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 ction */. BtCur
4cca0 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 sor *pCursor
4ccb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ccc0 2f 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 /* Space to writ
4ccd0 65 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 e cursor structu
4cce0 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f re */.);.SQLITE_
4ccf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4cd00 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 te3BtreeCursorSi
4cd10 7a 65 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 ze(void);..SQLIT
4cd20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4cd30 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 lite3BtreeCloseC
4cd40 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 ursor(BtCursor*)
4cd50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4cd60 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4cd70 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
4cd80 0a 20 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 . BtCursor*,.
4cd90 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
4cda0 70 55 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e pUnKey,. i64 in
4cdb0 74 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 tKey,. int bias
4cdc0 2c 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b ,. int *pRes.);
4cdd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4cde0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4cdf0 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 CursorHasMoved(B
4ce00 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b tCursor*, int*);
4ce10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4ce20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4ce30 44 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a Delete(BtCursor*
4ce40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4ce50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4ce60 65 65 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f eeInsert(BtCurso
4ce70 72 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a r*, const void *
4ce80 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a pKey, i64 nKey,.
4ce90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ceb0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 const void *pD
4cec0 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a ata, int nData,.
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cef0 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 int nZero, int
4cf00 20 62 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 bias, int seekR
4cf10 65 73 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 esult);.SQLITE_P
4cf20 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4cf30 65 33 42 74 72 65 65 46 69 72 73 74 28 42 74 43 e3BtreeFirst(BtC
4cf40 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 ursor*, int *pRe
4cf50 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 s);.SQLITE_PRIVA
4cf60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4cf70 72 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 reeLast(BtCursor
4cf80 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 *, int *pRes);.S
4cf90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4cfa0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 t sqlite3BtreeNe
4cfb0 78 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e xt(BtCursor*, in
4cfc0 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 t *pRes);.SQLITE
4cfd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4cfe0 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 ite3BtreeEof(BtC
4cff0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f ursor*);.SQLITE_
4d000 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4d010 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 te3BtreePrevious
4d020 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 (BtCursor*, int
4d030 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 *pRes);.SQLITE_P
4d040 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d050 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 e3BtreeKeySize(B
4d060 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 tCursor*, i64 *p
4d070 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Size);.SQLITE_PR
4d080 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d090 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 3BtreeKey(BtCurs
4d0a0 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c or*, u32 offset,
4d0b0 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 u32 amt, void*)
4d0c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d0d0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
4d0e0 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 ite3BtreeKeyFetc
4d0f0 68 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 h(BtCursor*, int
4d100 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f *pAmt);.SQLITE_
4d110 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f PRIVATE const vo
4d120 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 id *sqlite3Btree
4d130 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73 DataFetch(BtCurs
4d140 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b or*, int *pAmt);
4d150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d160 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d170 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f DataSize(BtCurso
4d180 72 2a 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b r*, u32 *pSize);
4d190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d1a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d1b0 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 Data(BtCursor*,
4d1c0 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
4d1d0 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c amt, void*);.SQL
4d1e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4d1f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
4d200 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 CachedRowid(BtCu
4d210 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 rsor*, sqlite3_i
4d220 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt64);.SQLITE_PR
4d230 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e IVATE sqlite3_in
4d240 74 36 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 t64 sqlite3Btree
4d250 47 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 GetCachedRowid(B
4d260 74 43 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 tCursor*);..SQLI
4d270 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
4d280 2a 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 *sqlite3BtreeInt
4d290 65 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 egrityCheck(Btre
4d2a0 65 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 e*, int *aRoot,
4d2b0 69 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 int nRoot, int,
4d2c0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int*);.SQLITE_PR
4d2d0 49 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67 IVATE struct Pag
4d2e0 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 er *sqlite3Btree
4d2f0 50 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a Pager(Btree*);..
4d300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4d310 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 nt sqlite3BtreeP
4d320 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a utData(BtCursor*
4d330 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 , u32 offset, u3
4d340 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 2 amt, void*);.S
4d350 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4d360 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 id sqlite3BtreeC
4d370 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 acheOverflow(BtC
4d380 75 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 ursor *);.SQLITE
4d390 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
4d3a0 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 lite3BtreeClearC
4d3b0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a ursor(BtCursor *
4d3c0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 );..#ifndef NDEB
4d3d0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
4d3e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4d3f0 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 eeCursorIsValid(
4d400 42 74 43 75 72 73 6f 72 2a 29 3b 0a 23 65 6e 64 BtCursor*);.#end
4d410 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
4d420 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 TE_OMIT_BTREECOU
4d430 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 NT.SQLITE_PRIVAT
4d440 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4d450 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 eeCount(BtCursor
4d460 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64 *, i64 *);.#end
4d470 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
4d480 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
4d490 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d4a0 33 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 3BtreeCursorInfo
4d4b0 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a (BtCursor*, int*
4d4c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
4d4d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
4d4e0 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 te3BtreeCursorLi
4d4f0 73 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 st(Btree*);.#end
4d500 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 if../*.** If we
4d510 61 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 are not using sh
4d520 61 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e ared cache, then
4d530 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
4d540 64 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 d to.** use mute
4d550 78 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 xes to access th
4d560 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 e BtShared struc
4d570 74 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 tures. So make
4d580 74 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 the.** Enter and
4d590 20 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 Leave procedure
4d5a0 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 s no-ops..*/.#if
4d5b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
4d5c0 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 _SHARED_CACHE.SQ
4d5d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
4d5e0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4d5f0 45 6e 74 65 72 28 42 74 72 65 65 2a 29 3b 0a 53 Enter(Btree*);.S
4d600 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
4d610 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
4d620 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 eEnterAll(sqlite
4d630 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 3*);.#else.# def
4d640 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
4d650 45 6e 74 65 72 28 58 29 20 0a 23 20 64 65 66 69 Enter(X) .# defi
4d660 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 ne sqlite3BtreeE
4d670 6e 74 65 72 41 6c 6c 28 58 29 0a 23 65 6e 64 69 nterAll(X).#endi
4d680 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 f..#if !defined(
4d690 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
4d6a0 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c ED_CACHE) && SQL
4d6b0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53 ITE_THREADSAFE.S
4d6c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
4d6d0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
4d6e0 65 4c 65 61 76 65 28 42 74 72 65 65 2a 29 3b 0a eLeave(Btree*);.
4d6f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
4d700 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
4d710 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 eeEnterCursor(Bt
4d720 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 Cursor*);.SQLITE
4d730 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4d740 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
4d750 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 eCursor(BtCursor
4d760 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4d770 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
4d780 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 3BtreeLeaveAll(s
4d790 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
4d7a0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4d7b0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
4d7c0 78 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 xArrayEnter(Btre
4d7d0 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 eMutexArray*);.S
4d7e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
4d7f0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
4d800 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
4d810 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 (BtreeMutexArray
4d820 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4d830 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
4d840 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3BtreeMutexArray
4d850 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 Insert(BtreeMute
4d860 78 41 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29 xArray*, Btree*)
4d870 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ;.#ifndef NDEBUG
4d880 0a 20 20 2f 2a 20 54 68 65 73 65 20 72 6f 75 74 . /* These rout
4d890 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 69 6e ines are used in
4d8a0 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 side assert() st
4d8b0 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a atements only. *
4d8c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
4d8d0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 int sqlite3Bt
4d8e0 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 reeHoldsMutex(Bt
4d8f0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ree*);.SQLITE_PR
4d900 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
4d910 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c te3BtreeHoldsAll
4d920 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a Mutexes(sqlite3*
4d930 29 3b 0a 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a );.#endif.#else.
4d940 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
4d950 33 42 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 3BtreeLeave(X).#
4d960 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4d970 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 treeEnterCursor(
4d980 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 X).# define sqli
4d990 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 te3BtreeLeaveCur
4d9a0 73 6f 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 sor(X).# define
4d9b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
4d9c0 65 41 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 eAll(X).# define
4d9d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
4d9e0 65 78 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a exArrayEnter(X).
4d9f0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
4da00 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c BtreeMutexArrayL
4da10 65 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 eave(X).# define
4da20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
4da30 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c exArrayInsert(X,
4da40 59 29 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c Y)..# define sql
4da50 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
4da60 74 65 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e tex(X) 1.# defin
4da70 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f e sqlite3BtreeHo
4da80 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 ldsAllMutexes(X)
4da90 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 1.#endif...#end
4daa0 69 66 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 if /* _BTREE_H_
4dab0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
4dac0 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 *** End of btree
4dad0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
4dae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4daf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4db00 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
4db10 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
4db20 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
4db30 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
4db40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4db50 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
4db60 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 *** Include vdbe
4db70 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
4db80 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
4db90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dba0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
4dbb0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 *** Begin file v
4dbc0 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dbe.h **********
4dbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dbf0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 **/./*.** 2001 S
4dc00 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a eptember 15.**.*
4dc10 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
4dc20 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
4dc30 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
4dc40 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
4dc50 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
4dc60 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
4dc70 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
4dc80 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
4dc90 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
4dca0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
4dcb0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
4dcc0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
4dcd0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
4dce0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
4dcf0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
4dd00 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
4dd10 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
4dd20 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
4dd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dd40 2a 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 0a 2a 2a 20 48 65 **********.** He
4dd70 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 ader file for th
4dd80 65 20 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 e Virtual DataBa
4dd90 73 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 se Engine (VDBE)
4dda0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 .**.** This head
4ddb0 65 72 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 er defines the i
4ddc0 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 nterface to the
4ddd0 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 virtual database
4dde0 20 65 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 engine.** or VD
4ddf0 42 45 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d BE. The VDBE im
4de00 70 6c 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 plements an abst
4de10 72 61 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 ract machine tha
4de20 74 20 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 t runs a.** simp
4de30 6c 65 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 le program to ac
4de40 63 65 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 cess and modify
4de50 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 the underlying d
4de60 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 atabase..**.** $
4de70 49 64 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 Id: vdbe.h,v 1.1
4de80 34 32 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 42 2009/07/24 17
4de90 3a 35 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 :58:53 danielk19
4dea0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 77 Exp $.*/.#ifn
4deb0 64 65 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 def _SQLITE_VDBE
4dec0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c _H_.#define _SQL
4ded0 49 54 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a ITE_VDBE_H_../*.
4dee0 2a 2a 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 ** A single VDBE
4def0 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 is an opaque st
4df00 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 ructure named "V
4df10 64 62 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 dbe". Only rout
4df20 69 6e 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 ines.** in the s
4df30 6f 75 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 ource file sqlit
4df40 65 56 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f eVdbe.c are allo
4df50 77 65 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 wed to see the i
4df60 6e 73 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 nsides.** of thi
4df70 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a s structure..*/.
4df80 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 typedef struct V
4df90 64 62 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a dbe Vdbe;../*.**
4dfa0 20 54 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 The names of th
4dfb0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 e following type
4dfc0 73 20 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64 s declared in vd
4dfd0 62 65 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75 beInt.h are requ
4dfe0 69 72 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 ired.** for the
4dff0 56 64 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f VdbeOp definitio
4e000 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 n..*/.typedef st
4e010 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 56 64 ruct VdbeFunc Vd
4e020 62 65 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 beFunc;.typedef
4e030 73 74 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a struct Mem Mem;.
4e040 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
4e050 75 62 50 72 6f 67 72 61 6d 20 53 75 62 50 72 6f ubProgram SubPro
4e060 67 72 61 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 gram;../*.** A s
4e070 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f ingle instructio
4e080 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c n of the virtual
4e090 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20 machine has an
4e0a0 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73 opcode.** and as
4e0b0 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f many as three o
4e0c0 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e perands. The in
4e0d0 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63 struction is rec
4e0e0 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69 orded.** as an i
4e0f0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
4e100 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
4e110 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 re:.*/.struct Vd
4e120 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f beOp {. u8 opco
4e130 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 de; /*
4e140 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 What operation t
4e150 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 o perform */. s
4e160 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79 70 igned char p4typ
4e170 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 e; /* One of the
4e180 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74 P4_xxx constant
4e190 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38 s for p4 */. u8
4e1a0 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20 opflags;
4e1b0 20 20 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 /* Not current
4e1c0 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 20 ly used */. u8
4e1d0 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p5;
4e1e0 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 /* Fifth parame
4e1f0 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e ter is an unsign
4e200 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a ed character */.
4e210 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 int p1;
4e220 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f /* First o
4e230 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
4e240 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p2;
4e250 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 /* Second parame
4e260 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a ter (often the j
4e270 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 ump destination)
4e280 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 */. int p3;
4e290 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
4e2a0 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
4e2b0 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 */. union {
4e2c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75 /* fou
4e2d0 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f rth parameter */
4e2e0 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 . int i;
4e2f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
4e300 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 nteger value if
4e310 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 p4type==P4_INT32
4e320 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b */. void *p;
4e330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4e340 2a 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 * Generic pointe
4e350 72 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a r */. char *z
4e360 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4e370 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 /* Pointer to da
4e380 74 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 ta for string (c
4e390 68 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73 har array) types
4e3a0 20 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 */. i64 *pI6
4e3b0 34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 4; /
4e3c0 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 * Used when p4ty
4e3d0 70 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a pe is P4_INT64 *
4e3e0 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 /. double *pR
4e3f0 65 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 eal; /*
4e400 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 Used when p4type
4e410 20 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 is P4_REAL */.
4e420 20 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e FuncDef *pFun
4e430 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 c; /* Use
4e440 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4e450 20 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 P4_FUNCDEF */.
4e460 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 VdbeFunc *pVd
4e470 62 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 beFunc; /* Use
4e480 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4e490 20 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a P4_VDBEFUNC */.
4e4a0 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f CollSeq *pCo
4e4b0 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 ll; /* Us
4e4c0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4e4d0 73 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a s P4_COLLSEQ */.
4e4e0 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 Mem *pMem;
4e4f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
4e500 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4e510 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 s P4_MEM */.
4e520 56 54 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 VTable *pVtab;
4e530 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 /* Used w
4e540 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 hen p4type is P4
4e550 5f 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 _VTAB */. Key
4e560 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
4e570 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
4e580 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 p4type is P4_KE
4e590 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 YINFO */. int
4e5a0 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 *ai;
4e5b0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
4e5c0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e p4type is P4_IN
4e5d0 54 41 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75 TARRAY */. Su
4e5e0 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 bProgram *pProgr
4e5f0 61 6d 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 am; /* Used whe
4e600 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 53 n p4type is P4_S
4e610 55 42 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d UBPROGRAM */. }
4e620 20 70 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 p4;.#ifdef SQLI
4e630 54 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 TE_DEBUG. char
4e640 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 *zComment;
4e650 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 /* Comment t
4e660 6f 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 o improve readab
4e670 69 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a ility */.#endif.
4e680 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
4e690 49 4c 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 ILE. int cnt;
4e6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4e6b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 * Number of time
4e6c0 73 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 s this instructi
4e6d0 6f 6e 20 77 61 73 20 65 78 65 63 75 74 65 64 20 on was executed
4e6e0 2a 2f 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b */. u64 cycles;
4e6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4e700 20 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e Total time spen
4e710 74 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 t executing this
4e720 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a instruction */.
4e730 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 #endif.};.typede
4e740 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 f struct VdbeOp
4e750 56 64 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 VdbeOp;.../*.**
4e760 41 20 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 A sub-routine us
4e770 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
4e780 61 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 a trigger progra
4e790 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62 m..*/.struct Sub
4e7a0 50 72 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65 Program {. Vdbe
4e7b0 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 Op *aOp;
4e7c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 /* Arr
4e7d0 61 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f ay of opcodes fo
4e7e0 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f r sub-program */
4e7f0 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 . int nOp;
4e800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4e810 20 2f 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 /* Elements in
4e820 61 4f 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e aOp[] */. int n
4e830 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
4e840 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
4e850 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c er of memory cel
4e860 6c 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 ls required */.
4e870 20 69 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20 int nCsr;
4e880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4e890 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 * Number of curs
4e8a0 6f 72 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a ors required */.
4e8b0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
4e8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4e8d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 /* Number of poi
4e8e0 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 nters to this st
4e8f0 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69 ructure */. voi
4e900 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 d *token;
4e910 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 /* id
4e920 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 that may be use
4e930 64 20 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 d to recursive t
4e940 72 69 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f riggers */.};../
4e950 2a 0a 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 *.** A smaller v
4e960 65 72 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 ersion of VdbeOp
4e970 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 used for the Vd
4e980 62 65 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 beAddOpList() fu
4e990 6e 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a nction because.*
4e9a0 2a 20 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 * it takes up le
4e9b0 73 73 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 ss space..*/.str
4e9c0 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b uct VdbeOpList {
4e9d0 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 . u8 opcode;
4e9e0 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f /* What o
4e9f0 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 peration to perf
4ea00 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 orm */. signed
4ea10 63 68 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 char p1; /*
4ea20 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f First operand */
4ea30 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 . signed char p
4ea40 32 3b 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 2; /* Second
4ea50 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 parameter (ofte
4ea60 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 n the jump desti
4ea70 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 nation) */. sig
4ea80 6e 65 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 ned char p3;
4ea90 20 2f 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 /* Third parame
4eaa0 74 65 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 ter */.};.typede
4eab0 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c f struct VdbeOpL
4eac0 69 73 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a ist VdbeOpList;.
4ead0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
4eae0 61 6c 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e alues of VdbeOp.
4eaf0 70 34 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e p4type.*/.#defin
4eb00 65 20 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 e P4_NOTUSED
4eb10 30 20 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 0 /* The P4 pa
4eb20 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 rameter is not u
4eb30 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 sed */.#define P
4eb40 34 5f 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 4_DYNAMIC (-1)
4eb50 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
4eb60 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 string obtained
4eb70 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c from sqliteMall
4eb80 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 oc() */.#define
4eb90 50 34 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 P4_STATIC (-2)
4eba0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
4ebb0 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 a static string
4ebc0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f */.#define P4_CO
4ebd0 4c 4c 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 LLSEQ (-4) /*
4ebe0 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
4ebf0 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 to a CollSeq str
4ec00 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e ucture */.#defin
4ec10 65 20 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d e P4_FUNCDEF (-
4ec20 35 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 5) /* P4 is a p
4ec30 6f 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 ointer to a Func
4ec40 44 65 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f Def structure */
4ec50 0a 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 .#define P4_KEYI
4ec60 4e 46 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 NFO (-6) /* P4
4ec70 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
4ec80 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
4ec90 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4eca0 50 34 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 P4_VDBEFUNC (-7)
4ecb0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
4ecc0 6e 74 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 nter to a VdbeFu
4ecd0 6e 63 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a nc structure */.
4ece0 23 64 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 #define P4_MEM
4ecf0 20 20 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 (-8) /* P4
4ed00 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
4ed10 61 20 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 a Mem* struct
4ed20 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
4ed30 34 5f 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 4_TRANSIENT (-9)
4ed40 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
4ed50 74 65 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 ter to a transie
4ed60 6e 74 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 nt string */.#de
4ed70 66 69 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 fine P4_VTAB
4ed80 20 28 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 (-10) /* P4 is
4ed90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
4eda0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 sqlite3_vtab str
4edb0 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e ucture */.#defin
4edc0 65 20 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d e P4_MPRINTF (-
4edd0 31 31 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 11) /* P4 is a s
4ede0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 tring obtained f
4edf0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 rom sqlite3_mpri
4ee00 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 ntf() */.#define
4ee10 20 50 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 P4_REAL (-1
4ee20 32 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2) /* P4 is a 64
4ee30 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f -bit floating po
4ee40 69 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 int value */.#de
4ee50 66 69 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 fine P4_INT64
4ee60 20 28 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 (-13) /* P4 is
4ee70 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 a 64-bit signed
4ee80 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 integer */.#defi
4ee90 6e 65 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 ne P4_INT32 (
4eea0 2d 31 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -14) /* P4 is a
4eeb0 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 32-bit signed in
4eec0 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 teger */.#define
4eed0 20 50 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 P4_INTARRAY (-1
4eee0 35 29 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 5) /* P4 is a ve
4eef0 63 74 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 ctor of 32-bit i
4ef00 6e 74 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 ntegers */.#defi
4ef10 6e 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d ne P4_SUBPROGRAM
4ef20 20 20 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 (-18) /* P4 is
4ef30 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4ef40 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 SubProgram struc
4ef50 74 75 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e ture */../* When
4ef60 20 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 adding a P4 arg
4ef70 75 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b ument using P4_K
4ef80 45 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f EYINFO, a copy o
4ef90 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 f the KeyInfo st
4efa0 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 ructure.** is ma
4efb0 64 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 de. That copy i
4efc0 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 s freed when the
4efd0 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a Vdbe is finaliz
4efe0 65 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a ed. But if the.
4eff0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 ** argument is P
4f000 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 4_KEYINFO_HANDOF
4f010 46 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e F, the passed in
4f020 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 pointer is used
4f030 2e 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 . It still.** g
4f040 65 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 ets freed when t
4f050 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c he Vdbe is final
4f060 69 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c ized so it still
4f070 20 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 should be obtai
4f080 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 ned.** from a si
4f090 6e 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f ngle sqliteMallo
4f0a0 63 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 c(). But no cop
4f0b0 79 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 y is made and th
4f0c0 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e e calling.** fun
4f0d0 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f ction should *no
4f0e0 74 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 t* try to free t
4f0f0 68 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 he KeyInfo..*/.#
4f100 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 define P4_KEYINF
4f110 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a O_HANDOFF (-16).
4f120 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e #define P4_KEYIN
4f130 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 FO_STATIC (-17)
4f140 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 ../*.** The Vdbe
4f150 2e 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 .aColName array
4f160 63 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 contains 5n Mem
4f170 73 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 structures, wher
4f180 65 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e e n is the .** n
4f190 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
4f1a0 20 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 of data returne
4f1b0 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 d by the stateme
4f1c0 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 nt..*/.#define C
4f1d0 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 OLNAME_NAME
4f1e0 30 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 0.#define COLNAM
4f1f0 45 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 E_DECLTYPE 1.#de
4f200 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 fine COLNAME_DAT
4f210 41 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 ABASE 2.#define
4f220 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 COLNAME_TABLE
4f230 20 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 3.#define COLNA
4f240 4d 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 ME_COLUMN 4.#i
4f250 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
4f260 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 LE_COLUMN_METADA
4f270 54 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e TA.# define COLN
4f280 41 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 AME_N 5
4f290 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
4f2a0 20 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d COLNAME_xxx sym
4f2b0 62 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 bols */.#else.#
4f2c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
4f2d0 54 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 T_DECLTYPE.# d
4f2e0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
4f2f0 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 1 /* S
4f300 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 tore only the na
4f310 6d 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 me */.# else.#
4f320 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f define COLNAME_
4f330 4e 20 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a N 2 /*
4f340 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 Store the name
4f350 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a and decltype */.
4f360 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a # endif.#endif..
4f370 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
4f380 69 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 ing macro conver
4f390 74 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 ts a relative ad
4f3a0 64 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 dress in the p2
4f3b0 66 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 field.** of a Vd
4f3c0 62 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 beOp structure i
4f3d0 6e 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e nto a negative n
4f3e0 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a umber so that .*
4f3f0 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 * sqlite3VdbeAdd
4f400 4f 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 OpList() knows t
4f410 68 61 74 20 74 68 65 20 61 64 64 72 65 73 73 20 hat the address
4f420 69 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 is relative. Ca
4f430 6c 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 lling.** the mac
4f440 72 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 ro again restore
4f450 73 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a s the address..*
4f460 2f 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 /.#define ADDR(X
4f470 29 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a ) (-1-(X))../*.
4f480 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 ** The makefile
4f490 73 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 scans the vdbe.c
4f4a0 20 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 source file and
4f4b0 20 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 creates the "op
4f4c0 63 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 codes.h".** head
4f4d0 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 er file that def
4f4e0 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f ines a number fo
4f4f0 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 r each opcode us
4f500 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a ed by the VDBE..
4f510 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4f520 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 ** Include opcod
4f530 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 es.h in the midd
4f540 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a le of vdbe.h ***
4f550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f560 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4f570 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 ** Begin file op
4f580 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a codes.h ********
4f590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f5b0 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 */./* Automatica
4f5c0 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 lly generated.
4f5d0 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f Do not edit */./
4f5e0 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f * See the mkopco
4f5f0 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 deh.awk script f
4f600 6f 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 or details */.#d
4f610 65 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 efine OP_VNext
4f620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 1.
4f640 23 64 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e #define OP_Affin
4f650 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20 20 ity
4f660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f670 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 2.#define OP_Col
4f680 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 umn
4f690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f6a0 20 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 3.#define OP_S
4f6b0 65 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 etCookie
4f6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f6d0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 4.#define OP
4f6e0 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 _Seek
4f6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f700 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 5.#define
4f710 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 OP_Real
4f720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f730 20 20 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 130 /* s
4f740 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 ame as TK_FLOAT
4f750 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4f760 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20 20 20 _Sequence
4f770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f780 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
4f790 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 OP_Savepoint
4f7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f7b0 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 7.#defin
4f7c0 65 20 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 e OP_Ge
4f7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f7e0 20 20 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a 78 /*
4f7f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 same as TK_GE
4f800 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4f810 4f 50 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20 OP_RowKey
4f820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f830 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 8.#defin
4f840 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20 e OP_SCopy
4f850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f860 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 9.#def
4f870 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20 ine OP_Eq
4f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f890 20 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20 74
4f8a0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 /* same as TK_EQ
4f8b0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
4f8c0 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 e OP_OpenWrite
4f8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f8e0 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 10.#def
4f8f0 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 ine OP_NotNull
4f900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f910 20 20 20 20 20 20 20 20 20 20 20 37 32 20 20 20 72
4f920 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f /* same as TK_NO
4f930 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e TNULL */.#defin
4f940 65 20 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 e OP_If
4f950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f960 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 11.#def
4f970 69 6e 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 ine OP_ToInt
4f980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f990 20 20 20 20 20 20 20 20 20 20 31 34 34 20 20 20 144
4f9a0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f /* same as TK_TO
4f9b0 5f 49 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e _INT */.#defin
4f9c0 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 e OP_String8
4f9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f9e0 20 20 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 94 /*
4f9f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 same as TK_STRI
4fa00 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NG */.#define
4fa10 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 OP_CollSeq
4fa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa30 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 12.#defin
4fa40 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 e OP_OpenRead
4fa50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa60 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 13.#def
4fa70 69 6e 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 ine OP_Expire
4fa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa90 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 14.#d
4faa0 65 66 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d efine OP_AutoCom
4fab0 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 mit
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 15.
4fad0 23 64 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 #define OP_Gt
4fae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
4fb00 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 5 /* same as T
4fb10 4b 5f 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 K_GT */.#d
4fb20 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f 75 efine OP_Pagecou
4fb30 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nt
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 16.
4fb50 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 #define OP_Integ
4fb60 72 69 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 rityCk
4fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4fb80 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 7.#define OP_Sor
4fb90 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
4fba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbb0 20 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 18.#define OP_C
4fbc0 6f 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 opy
4fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbe0 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 20.#define OP
4fbf0 5f 54 72 61 63 65 20 20 20 20 20 20 20 20 20 20 _Trace
4fc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc10 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 21.#define
4fc20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 OP_Function
4fc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc40 20 20 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 22.#defin
4fc50 65 20 4f 50 5f 49 66 4e 65 67 20 20 20 20 20 20 e OP_IfNeg
4fc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc70 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 23.#def
4fc80 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 ine OP_And
4fc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fca0 20 20 20 20 20 20 20 20 20 20 20 36 37 20 20 20 67
4fcb0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e /* same as TK_AN
4fcc0 44 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e D */.#defin
4fcd0 65 20 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 e OP_Subtract
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcf0 20 20 20 20 20 20 20 20 20 38 35 20 20 20 2f 2a 85 /*
4fd00 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 same as TK_MINU
4fd10 53 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 S */.#define
4fd20 4f 50 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 OP_Noop
4fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd40 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 24.#defin
4fd50 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20 e OP_Program
4fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd70 20 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 25.#def
4fd80 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 ine OP_Return
4fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fda0 20 20 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 26.#d
4fdb0 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 efine OP_Remaind
4fdc0 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 er
4fdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 20 88
4fde0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4fdf0 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 REM */.#def
4fe00 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 ine OP_NewRowid
4fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe20 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 27.#d
4fe30 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c efine OP_Multipl
4fe40 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36 20 86
4fe60 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4fe70 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 STAR */.#def
4fe80 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 ine OP_Variable
4fe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fea0 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 28.#d
4feb0 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20 efine OP_String
4fec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fed0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 29.
4fee0 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 #define OP_RealA
4fef0 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 ffinity
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
4ff10 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 0.#define OP_VRe
4ff20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 name
4ff30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff40 20 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 31.#define OP_P
4ff50 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20 arseSchema
4ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff70 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 32.#define OP
4ff80 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20 _VOpen
4ff90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffa0 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 33.#define
4ffb0 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20 OP_Close
4ffc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffd0 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 34.#defin
4ffe0 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 e OP_CreateIndex
4fff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50000 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 35.#def
50010 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 ine OP_IsUnique
50020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50030 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 36.#d
50040 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e efine OP_NotFoun
50050 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
50060 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 37.
50070 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34 #define OP_Int64
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
500a0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73 8.#define OP_Mus
500b0 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 20 tBeInt
500c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500d0 20 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 39.#define OP_H
500e0 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 alt
500f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50100 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 40.#define OP
50110 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 _Rowid
50120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50130 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 41.#define
50140 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 OP_IdxLT
50150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50160 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 42.#defin
50170 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 e OP_AddImm
50180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50190 20 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 43.#def
501a0 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 ine OP_RowData
501b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501c0 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 44.#d
501d0 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 efine OP_MemMax
501e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501f0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 45.
50200 23 64 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 #define OP_Or
50210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
50230 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 6 /* same as T
50240 4b 5f 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 K_OR */.#d
50250 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 efine OP_NotExis
50260 74 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ts
50270 20 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 46.
50280 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 #define OP_Gosub
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 20 20 20 20 20 20 34 4
502b0 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 7.#define OP_Div
502c0 69 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ide
502d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
502e0 20 38 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 87 /* same as
502f0 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a TK_SLASH */.
50300 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 #define OP_Integ
50310 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 er
50320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
50330 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e 8.#define OP_ToN
50340 75 6d 65 72 69 63 20 20 20 20 20 20 20 20 20 20 umeric
50350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50360 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 143 /* same as
50370 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f TK_TO_NUMERIC*/
50380 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 76 .#define OP_Prev
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503b0 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 49.#define OP_Ro
503c0 77 53 65 74 52 65 61 64 20 20 20 20 20 20 20 20 wSetRead
503d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503e0 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50.#define OP_
503f0 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 Concat
50400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50410 20 20 20 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 89 /* same
50420 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 as TK_CONCAT
50430 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f */.#define OP_Ro
50440 77 53 65 74 41 64 64 20 20 20 20 20 20 20 20 20 wSetAdd
50450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50460 20 20 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 51.#define OP_
50470 42 69 74 41 6e 64 20 20 20 20 20 20 20 20 20 20 BitAnd
50480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50490 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65 80 /* same
504a0 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 as TK_BITAND
504b0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 */.#define OP_VC
504c0 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 olumn
504d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504e0 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52.#define OP_
504f0 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 20 20 CreateTable
50500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50510 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 53.#define O
50520 50 5f 4c 61 73 74 20 20 20 20 20 20 20 20 20 20 P_Last
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50540 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 54.#define
50550 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 OP_SeekLe
50560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50570 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 55.#defi
50580 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20 ne OP_IsNull
50590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505a0 20 20 20 20 20 20 20 20 20 20 37 31 20 20 20 2f 71 /
505b0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e * same as TK_ISN
505c0 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 ULL */.#define
505d0 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20 OP_IncrVacuum
505e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505f0 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 56.#defi
50600 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 ne OP_IdxRowid
50610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50620 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 57.#de
50630 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 fine OP_ShiftRig
50640 68 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ht
50650 20 20 20 20 20 20 20 20 20 20 20 20 38 33 20 20 83
50660 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 /* same as TK_R
50670 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 SHIFT */.#defi
50680 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 ne OP_ResetCount
50690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506a0 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 58.#de
506b0 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20 fine OP_Yield
506c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506d0 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 59.#
506e0 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 define OP_DropTr
506f0 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 igger
50700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30 60
50710 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 .#define OP_Drop
50720 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20 Index
50730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50740 36 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 61.#define OP_Pa
50750 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 ram
50760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50770 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 62.#define OP_
50780 49 64 78 47 45 20 20 20 20 20 20 20 20 20 20 20 IdxGE
50790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507a0 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f 63.#define O
507b0 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20 20 20 P_IdxDelete
507c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507d0 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 64.#define
507e0 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20 OP_Vacuum
507f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50800 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 65.#defi
50810 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 ne OP_IfNot
50820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50830 20 20 20 20 20 20 20 20 20 20 36 38 0a 23 64 65 68.#de
50840 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c fine OP_DropTabl
50850 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
50860 20 20 20 20 20 20 20 20 20 20 20 20 36 39 0a 23 69.#
50870 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 define OP_SeekLt
50880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30 70
508a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 .#define OP_Make
508b0 52 65 63 6f 72 64 20 20 20 20 20 20 20 20 20 20 Record
508c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508d0 37 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 79.#define OP_To
508e0 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 Blob
508f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50900 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 142 /* same a
50910 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f s TK_TO_BLOB */
50920 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 .#define OP_Resu
50930 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 ltRow
50940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50950 39 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 90.#define OP_De
50960 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 lete
50970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50980 20 20 39 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 91.#define OP_
50990 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 AggFinal
509a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509b0 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 92.#define O
509c0 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20 P_Compare
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509e0 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 95.#define
509f0 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 OP_ShiftLeft
50a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a10 20 20 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20 82 /*
50a20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 same as TK_LSHIF
50a30 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f T */.#define O
50a40 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20 P_Goto
50a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a60 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 96.#define
50a70 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20 OP_TableLock
50a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a90 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 97.#defi
50aa0 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20 ne OP_Clear
50ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ac0 20 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 98.#de
50ad0 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 fine OP_Le
50ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50af0 20 20 20 20 20 20 20 20 20 20 20 20 37 36 20 20 76
50b00 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
50b10 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 E */.#defi
50b20 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b ne OP_VerifyCook
50b30 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ie
50b40 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 99.#de
50b50 66 69 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 fine OP_AggStep
50b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b70 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 100.#
50b80 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74 define OP_ToText
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 31 141
50bb0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50bc0 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65 _TO_TEXT */.#de
50bd0 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 fine OP_Not
50be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bf0 20 20 20 20 20 20 20 20 20 20 20 20 31 39 20 20 19
50c00 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
50c10 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 OT */.#defi
50c20 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 ne OP_ToReal
50c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c40 20 20 20 20 20 20 20 20 20 31 34 35 20 20 20 2f 145 /
50c50 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
50c60 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 REAL */.#define
50c70 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 OP_Transaction
50c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c90 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 101.#defi
50ca0 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20 ne OP_VFilter
50cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cc0 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 102.#de
50cd0 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20 fine OP_Ne
50ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20 73
50d00 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
50d10 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 E */.#defi
50d20 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 ne OP_VDestroy
50d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d40 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 103.#de
50d50 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 fine OP_BitOr
50d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d70 20 20 20 20 20 20 20 20 20 20 20 20 38 31 20 20 81
50d80 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 /* same as TK_B
50d90 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 ITOR */.#defi
50da0 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 ne OP_Next
50db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50dc0 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 104.#de
50dd0 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20 fine OP_Count
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50df0 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 105.#
50e00 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 define OP_IdxIns
50e10 65 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ert
50e20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 36 106
50e30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 .#define OP_Lt
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e60 37 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 77 /* same as
50e70 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 TK_LT */.#
50e80 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 define OP_SeekGe
50e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37 107
50eb0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 .#define OP_Inse
50ec0 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 rt
50ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
50ee0 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 08.#define OP_De
50ef0 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 stroy
50f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f10 20 31 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 109.#define OP_
50f20 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20 ReadCookie
50f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f40 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 110.#define O
50f50 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20 20 20 P_RowSetTest
50f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f70 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 111.#define
50f80 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 OP_LoadAnalysis
50f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fa0 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 112.#defi
50fb0 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20 ne OP_Explain
50fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fd0 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 113.#de
50fe0 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 fine OP_HaltIfNu
50ff0 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ll
51000 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 114.#
51010 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 define OP_OpenPs
51020 65 75 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 eudo
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 35 115
51040 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e .#define OP_Open
51050 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 Ephemeral
51060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51070 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75 16.#define OP_Nu
51080 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ll
51090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510a0 20 31 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 117.#define OP_
510b0 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20 20 20 Move
510c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510d0 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f 118.#define O
510e0 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 P_Blob
510f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51100 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 119.#define
51110 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 OP_Add
51120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51130 20 20 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 84 /*
51140 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 same as TK_PLUS
51150 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f */.#define O
51160 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 20 20 P_Rewind
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51180 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 120.#define
51190 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 20 20 20 OP_SeekGt
511a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511b0 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 121.#defi
511c0 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 20 ne OP_VBegin
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511e0 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 122.#de
511f0 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20 fine OP_VUpdate
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51210 20 20 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 123.#
51220 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f define OP_IfZero
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 20 20 20 20 20 20 31 32 34 124
51250 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4e .#define OP_BitN
51260 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ot
51270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51280 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 93 /* same as
51290 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a 23 TK_BITNOT */.#
512a0 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 61 74 define OP_VCreat
512b0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
512c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35 125
512d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 6e .#define OP_Foun
512e0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
512f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51300 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 26.#define OP_If
51310 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 20 20 Pos
51320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51330 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 127.#define OP_
51340 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20 NullRow
51350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51360 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 128.#define O
51370 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20 P_Jump
51380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51390 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 129.#define
513a0 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 OP_Permutation
513b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513c0 20 20 20 20 20 20 20 31 33 31 0a 0a 2f 2a 20 54 131../* T
513d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 he following opc
513e0 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e ode values are n
513f0 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 ever used */.#de
51400 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
51410 31 33 32 20 20 20 20 20 20 20 20 20 20 20 20 20 132
51420 20 20 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 132.#
51430 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 define OP_NotUse
51440 64 5f 31 33 33 20 20 20 20 20 20 20 20 20 20 20 d_133
51450 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 33 133
51460 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
51470 73 65 64 5f 31 33 34 20 20 20 20 20 20 20 20 20 sed_134
51480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51490 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 34.#define OP_No
514a0 74 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 tUsed_135
514b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514c0 20 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 135.#define OP_
514d0 4e 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 NotUsed_136
514e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514f0 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 136.#define O
51500 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 P_NotUsed_137
51510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51520 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 137.#define
51530 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 OP_NotUsed_138
51540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51550 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 138.#defi
51560 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 ne OP_NotUsed_13
51570 39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 9
51580 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 139.#de
51590 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f fine OP_NotUsed_
515a0 31 34 30 20 20 20 20 20 20 20 20 20 20 20 20 20 140
515b0 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 140..
515c0 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 ./* Properties s
515d0 75 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 uch as "out2" or
515e0 20 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 "jump" that are
515f0 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a specified in.**
51600 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 comments follow
51610 69 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 66 ing the "case" f
51620 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 or each opcode i
51630 6e 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 n the vdbe.c.**
51640 61 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f are encoded into
51650 20 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 bitvectors as f
51660 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 ollows:.*/.#defi
51670 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 ne OPFLG_JUMP
51680 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 0x0001
51690 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f /* jump: P2 ho
516a0 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a lds jmp target *
516b0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
516c0 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 OUT2_PRERELEASE
516d0 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 0x0002 /* out2-
516e0 70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 prerelease: */.#
516f0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 define OPFLG_IN1
51700 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
51710 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 004 /* in1: P
51720 31 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 1 is an input */
51730 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 .#define OPFLG_I
51740 4e 32 20 20 20 20 20 20 20 20 20 20 20 20 20 30 N2 0
51750 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 x0008 /* in2:
51760 20 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 P2 is an input
51770 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 */.#define OPFLG
51780 5f 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 _IN3
51790 20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 0x0010 /* in3:
517a0 20 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 P3 is an inpu
517b0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 t */.#define OPF
517c0 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20 LG_OUT3
517d0 20 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 0x0020 /* ou
517e0 74 33 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75 t3: P3 is an ou
517f0 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tput */.#define
51800 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 OPFLG_INITIALIZE
51810 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 R {\./* 0 */ 0
51820 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x00, 0x01, 0x00,
51830 20 30 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 0x00, 0x10, 0x0
51840 38 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 8, 0x02, 0x00,\.
51850 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20 /* 8 */ 0x00,
51860 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35 0x04, 0x00, 0x05
51870 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
51880 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31 00, 0x00,\./* 1
51890 36 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 6 */ 0x02, 0x00,
518a0 20 30 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 0x01, 0x04, 0x0
518b0 34 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 4, 0x00, 0x00, 0
518c0 78 30 35 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 x05,\./* 24 */
518d0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 34 0x00, 0x01, 0x04
518e0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 , 0x02, 0x00, 0x
518f0 30 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 5c 02, 0x04, 0x00,\
51900 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c ./* 32 */ 0x00,
51910 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
51920 32 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 2, 0x11, 0x11, 0
51930 78 30 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 x02, 0x05,\./*
51940 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32 40 */ 0x00, 0x02
51950 2c 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78 , 0x11, 0x04, 0x
51960 30 30 2c 20 30 78 30 38 2c 20 30 78 31 31 2c 20 00, 0x08, 0x11,
51970 30 78 30 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 0x01,\./* 48 */
51980 20 30 78 30 32 2c 20 30 78 30 31 2c 20 30 78 32 0x02, 0x01, 0x2
51990 31 2c 20 30 78 30 38 2c 20 30 78 30 30 2c 20 30 1, 0x08, 0x00, 0
519a0 78 30 32 2c 20 30 78 30 31 2c 20 30 78 31 31 2c x02, 0x01, 0x11,
519b0 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 78 30 31 \./* 56 */ 0x01
519c0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 , 0x02, 0x00, 0x
519d0 30 34 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 04, 0x00, 0x00,
519e0 30 78 30 32 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 0x02, 0x11,\./*
519f0 20 36 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 64 */ 0x00, 0x0
51a00 30 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 0, 0x2c, 0x2c, 0
51a10 78 30 35 2c 20 30 78 30 30 2c 20 30 78 31 31 2c x05, 0x00, 0x11,
51a20 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 37 32 20 2a 0x05,\./* 72 *
51a30 2f 20 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 / 0x05, 0x15, 0x
51a40 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 15, 0x15, 0x15,
51a50 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 30 30 0x15, 0x15, 0x00
51a60 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 32 ,\./* 80 */ 0x2
51a70 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 c, 0x2c, 0x2c, 0
51a80 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c x2c, 0x2c, 0x2c,
51a90 20 30 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 0x2c, 0x2c,\./*
51aa0 20 20 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78 88 */ 0x2c, 0x
51ab0 32 63 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 2c, 0x00, 0x00,
51ac0 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 0x00, 0x04, 0x02
51ad0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 , 0x00,\./* 96
51ae0 2a 2f 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 */ 0x01, 0x00, 0
51af0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
51b00 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
51b10 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 0,\./* 104 */ 0x
51b20 30 31 2c 20 30 78 30 32 2c 20 30 78 30 38 2c 20 01, 0x02, 0x08,
51b30 30 78 31 31 2c 20 30 78 30 30 2c 20 30 78 30 32 0x11, 0x00, 0x02
51b40 2c 20 30 78 30 32 2c 20 30 78 31 35 2c 5c 0a 2f , 0x02, 0x15,\./
51b50 2a 20 31 31 32 20 2a 2f 20 30 78 30 30 2c 20 30 * 112 */ 0x00, 0
51b60 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 30 2c x00, 0x10, 0x00,
51b70 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 0x00, 0x02, 0x0
51b80 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30 0, 0x02,\./* 120
51b90 20 2a 2f 20 30 78 30 31 2c 20 30 78 31 31 2c 20 */ 0x01, 0x11,
51ba0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 0x00, 0x00, 0x05
51bb0 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 , 0x00, 0x11, 0x
51bc0 30 35 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 05,\./* 128 */ 0
51bd0 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c x00, 0x01, 0x02,
51be0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
51bf0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 0, 0x00, 0x00,\.
51c00 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 /* 136 */ 0x00,
51c10 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
51c20 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 , 0x00, 0x04, 0x
51c30 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 04, 0x04,\./* 14
51c40 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 4 */ 0x04, 0x04,
51c50 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
51c60 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 ** End of opcode
51c70 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.h ************
51c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51ca0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
51cb0 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
51cc0 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
51cd0 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a in vdbe.h ******
51ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51cf0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 */../*.** Protot
51d00 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 ypes for the VDB
51d10 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 E interface. Se
51d20 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 e comments on th
51d30 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
51d40 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 .** for a descri
51d50 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 ption of what ea
51d60 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 ch of these rout
51d70 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 ines does..*/.SQ
51d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 LITE_PRIVATE Vdb
51d90 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 e *sqlite3VdbeCr
51da0 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a eate(sqlite3*);.
51db0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
51dc0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
51dd0 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b dOp0(Vdbe*,int);
51de0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
51df0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
51e00 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c ddOp1(Vdbe*,int,
51e10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
51e20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
51e30 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a VdbeAddOp2(Vdbe*
51e40 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 ,int,int,int);.S
51e50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
51e60 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
51e70 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e Op3(Vdbe*,int,in
51e80 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 t,int,int);.SQLI
51e90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
51ea0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
51eb0 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 (Vdbe*,int,int,i
51ec0 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 nt,int,const cha
51ed0 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c r *zP4,int);.SQL
51ee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
51ef0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
51f00 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 List(Vdbe*, int
51f10 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 nOp, VdbeOpList
51f20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c const *aOp);.SQL
51f30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
51f40 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
51f50 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 geP1(Vdbe*, int
51f60 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 addr, int P1);.S
51f70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
51f80 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
51f90 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e angeP2(Vdbe*, in
51fa0 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b t addr, int P2);
51fb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
51fc0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
51fd0 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 ChangeP3(Vdbe*,
51fe0 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 int addr, int P3
51ff0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52000 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
52010 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a beChangeP5(Vdbe*
52020 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 , u8 P5);.SQLITE
52030 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
52040 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
52050 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 e(Vdbe*, int add
52060 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 r);.SQLITE_PRIVA
52070 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
52080 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
52090 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c Vdbe*, int addr,
520a0 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
520b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
520c0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 ite3VdbeChangeP4
520d0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 (Vdbe*, int addr
520e0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 , const char *zP
520f0 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 4, int N);.SQLIT
52100 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52110 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 qlite3VdbeUsesBt
52120 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b ree(Vdbe*, int);
52130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52140 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 VdbeOp *sqlite3V
52150 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 dbeGetOp(Vdbe*,
52160 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
52170 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
52180 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 VdbeMakeLabel(Vd
52190 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
521a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
521b0 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 3VdbeDelete(Vdbe
521c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
521d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
521e0 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 dbeMakeReady(Vdb
521f0 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 e*,int,int,int,i
52200 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c nt,int,int);.SQL
52210 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
52220 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c sqlite3VdbeFinal
52230 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 ize(Vdbe*);.SQLI
52240 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
52250 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c sqlite3VdbeResol
52260 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 69 veLabel(Vdbe*, i
52270 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
52280 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
52290 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 dbeCurrentAddr(V
522a0 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 dbe*);.#ifdef SQ
522b0 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 LITE_DEBUG.SQLIT
522c0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
522d0 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 sqlite3VdbeAsser
522e0 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20 2a tMayAbort(Vdbe *
522f0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
52300 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
52310 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 lite3VdbeTrace(V
52320 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e dbe*,FILE*);.#en
52330 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
52340 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
52350 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75 dbeResetStepResu
52360 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 lt(Vdbe*);.SQLIT
52370 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
52380 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56 lite3VdbeReset(V
52390 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
523a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
523b0 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 e3VdbeSetNumCols
523c0 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c (Vdbe*,int);.SQL
523d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
523e0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f sqlite3VdbeSetCo
523f0 6c 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e 74 lName(Vdbe*, int
52400 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 , int, const cha
52410 72 20 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 r *, void(*)(voi
52420 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 d*));.SQLITE_PRI
52430 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
52440 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 3VdbeCountChange
52450 73 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 s(Vdbe*);.SQLITE
52460 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
52470 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 *sqlite3VdbeDb(
52480 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Vdbe*);.SQLITE_P
52490 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
524a0 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 te3VdbeSetSql(Vd
524b0 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 be*, const char
524c0 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29 3b *z, int n, int);
524d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
524e0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
524f0 53 77 61 70 28 56 64 62 65 2a 2c 56 64 62 65 2a Swap(Vdbe*,Vdbe*
52500 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52510 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 E VdbeOp *sqlite
52520 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 3VdbeTakeOpArray
52530 28 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e (Vdbe*, int*, in
52540 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
52550 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
52560 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 VdbeProgramDelet
52570 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 53 75 62 e(sqlite3 *, Sub
52580 50 72 6f 67 72 61 6d 20 2a 2c 20 69 6e 74 29 3b Program *, int);
52590 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
525a0 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
525b0 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f NAGEMENT.SQLITE_
525c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
525d0 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 4d 65 te3VdbeReleaseMe
525e0 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 mory(int);.#endi
525f0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
52600 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
52610 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f *sqlite3VdbeReco
52620 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f rdUnpack(KeyInfo
52630 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
52640 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 *,char*,int);.SQ
52650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
52660 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c d sqlite3VdbeDel
52670 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
52680 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 d(UnpackedRecord
52690 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
526a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
526b0 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
526c0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
526d0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 UnpackedRecord*)
526e0 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 ;...#ifndef NDEB
526f0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
52700 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
52710 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 VdbeComment(Vdbe
52720 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
52730 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 ...);.# define V
52740 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 dbeComment(X) s
52750 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e qlite3VdbeCommen
52760 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t X.SQLITE_PRIVA
52770 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
52780 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 3VdbeNoopComment
52790 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 (Vdbe*, const ch
527a0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 ar*, ...);.# def
527b0 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ine VdbeNoopComm
527c0 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 33 56 ent(X) sqlite3V
527d0 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 dbeNoopComment X
527e0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
527f0 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 VdbeComment(X).#
52800 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 define VdbeNoop
52810 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 Comment(X).#endi
52820 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a f..#endif../****
52830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
52840 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a f vdbe.h *******
52850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
52880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
52890 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
528a0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 eft off in sqlit
528b0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
528c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
528d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
528e0 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 74 68 de pager.h in th
528f0 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
52900 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
52910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
52920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
52930 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a file pager.h **
52940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
52970 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
52980 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
52990 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
529a0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
529b0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
529c0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
529d0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
529e0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
529f0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
52a00 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
52a10 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
52a20 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
52a30 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
52a40 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
52a50 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
52a60 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
52a70 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
52a80 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
52a90 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
52aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 *.** This header
52af0 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 file defines th
52b00 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 e interface that
52b10 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 the sqlite page
52b20 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 cache.** subsys
52b30 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 20 63 tem. The page c
52b40 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 72 ache subsystem r
52b50 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 eads and writes
52b60 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a a file a page.**
52b70 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 70 at a time and p
52b80 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 rovides a journa
52b90 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a l for rollback..
52ba0 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
52bb0 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30 34 20 pager.h,v 1.104
52bc0 32 30 30 39 2f 30 37 2f 32 34 20 31 39 3a 30 31 2009/07/24 19:01
52bd0 3a 31 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :19 drh Exp $.*/
52be0 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 ..#ifndef _PAGER
52bf0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 _H_.#define _PAG
52c00 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 ER_H_../*.** Def
52c10 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a ault maximum siz
52c20 65 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 e for persistent
52c30 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 journal files.
52c40 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 A negative .** v
52c50 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 alue means no li
52c60 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 mit. This value
52c70 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 may be overridde
52c80 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 n using the .**
52c90 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 sqlite3PagerJour
52ca0 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 nalSizeLimit() A
52cb0 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 PI. See also "PR
52cc0 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a AGMA journal_siz
52cd0 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 e_limit"..*/.#if
52ce0 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 ndef SQLITE_DEFA
52cf0 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 ULT_JOURNAL_SIZE
52d00 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 _LIMIT. #define
52d10 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
52d20 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d JOURNAL_SIZE_LIM
52d30 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a IT -1.#endif../*
52d40 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 .** The type use
52d50 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 d to represent a
52d60 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 page number. T
52d70 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e he first page in
52d80 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 a file.** is ca
52d90 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 lled page 1. 0
52da0 69 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 is used to repre
52db0 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 sent "not a page
52dc0 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 "..*/.typedef u3
52dd0 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 2 Pgno;../*.** E
52de0 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 ach open file is
52df0 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 managed by a se
52e00 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 parate instance
52e10 6f 66 20 74 68 65 20 22 50 61 67 65 72 22 20 73 of the "Pager" s
52e20 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 tructure..*/.typ
52e30 65 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65 edef struct Page
52e40 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 r Pager;../*.**
52e50 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 Handle type for
52e60 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 pages..*/.typede
52e70 66 20 73 74 72 75 63 74 20 50 67 48 64 72 20 44 f struct PgHdr D
52e80 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 bPage;../*.** Pa
52e90 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f ge number PAGER_
52ea0 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 MJ_PGNO is never
52eb0 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 used in an SQLi
52ec0 74 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20 te database (it
52ed0 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 is.** reserved f
52ee0 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e or working aroun
52ef0 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 d a windows/posi
52f00 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 x incompatibilit
52f10 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 y). It is.** use
52f20 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c d in the journal
52f30 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 to signify that
52f40 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f the remainder o
52f50 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
52f60 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 le .** is devote
52f70 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d d to storing a m
52f80 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
52f90 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e me - there are n
52fa0 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a o more pages to.
52fb0 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 ** roll back. Se
52fc0 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 e comments for f
52fd0 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 unction writeMas
52fe0 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 terJournal() in
52ff0 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 pager.c .** for
53000 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 details..*/.#def
53010 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e ine PAGER_MJ_PGN
53020 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 O(x) ((Pgno)((PE
53030 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d NDING_BYTE/((x)-
53040 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a >pageSize))+1)).
53050 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 ./*.** Allowed v
53060 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c alues for the fl
53070 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f ags parameter to
53080 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
53090 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a n()..**.** NOTE:
530a0 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 These values mu
530b0 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 st match the cor
530c0 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 responding BTREE
530d0 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 _ values in btre
530e0 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 e.h..*/.#define
530f0 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e PAGER_OMIT_JOURN
53100 41 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a AL 0x0001 /*
53110 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f Do not use a ro
53120 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a llback journal *
53130 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f /.#define PAGER_
53140 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78 NO_READLOCK 0x
53150 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20 0002 /* Omit
53160 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 readlocks on rea
53170 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a donly files */..
53180 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 /*.** Valid valu
53190 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e es for the secon
531a0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 d argument to sq
531b0 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e lite3PagerLockin
531c0 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 gMode()..*/.#def
531d0 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e ine PAGER_LOCKIN
531e0 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 GMODE_QUERY
531f0 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 -1.#define PAGE
53200 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f R_LOCKINGMODE_NO
53210 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 RMAL 0.#def
53220 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e ine PAGER_LOCKIN
53230 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 GMODE_EXCLUSIVE
53240 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 1../*.** Valid
53250 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 values for the
53260 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
53270 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a to sqlite3PagerJ
53280 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a 2f ournalMode()..*/
53290 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a .#define PAGER_J
532a0 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 OURNALMODE_QUERY
532b0 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 -1.#define
532c0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
532d0 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 DE_DELETE 0
532e0 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 /* Commit by
532f0 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 6c deleting journal
53300 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 file */.#define
53310 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
53320 44 45 5f 50 45 52 53 49 53 54 20 20 20 20 20 31 DE_PERSIST 1
53330 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 /* Commit by
53340 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 zeroing journal
53350 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e header */.#defin
53360 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d e PAGER_JOURNALM
53370 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20 20 20 ODE_OFF
53380 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 6f 2 /* Journal o
53390 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 66 mitted. */.#def
533a0 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
533b0 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 20 LMODE_TRUNCATE
533c0 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 3 /* Commit
533d0 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f by truncating jo
533e0 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 urnal */.#define
533f0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f PAGER_JOURNALMO
53400 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 34 DE_MEMORY 4
53410 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 /* In-memory
53420 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
53430 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 ./*.** The remai
53440 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c nder of this fil
53450 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 e contains the d
53460 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 eclarations of t
53470 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 he functions.**
53480 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 that make up the
53490 20 50 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 Pager sub-syste
534a0 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 m API. See sourc
534b0 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 e code comments
534c0 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c for .** a detail
534d0 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f ed description o
534e0 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a f each routine..
534f0 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 */../* Open and
53500 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f close a Pager co
53510 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 nnection. */ .SQ
53520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
53530 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
53540 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 n(. sqlite3_vfs
53550 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 *,. Pager **ppP
53560 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68 ager,. const ch
53570 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 6e ar*,. int,. in
53580 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 64 t,. int,. void
53590 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 3b 0a (*)(DbPage*).);.
535a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
535b0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 nt sqlite3PagerC
535c0 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 lose(Pager *pPag
535d0 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 er);.SQLITE_PRIV
535e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
535f0 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 agerReadFilehead
53600 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 er(Pager*, int,
53610 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b unsigned char*);
53620 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 ../* Functions u
53630 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 sed to configure
53640 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e a Pager object.
53650 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
53660 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
53670 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c agerSetBusyhandl
53680 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a er(Pager*, int(*
53690 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20 )(void *), void
536a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
536b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
536c0 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 gerSetPagesize(P
536d0 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 6e 74 ager*, u16*, int
536e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
536f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
53700 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 erMaxPageCount(P
53710 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ager*, int);.SQL
53720 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
53730 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
53740 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a Cachesize(Pager*
53750 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
53760 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
53770 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 te3PagerSetSafet
53780 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e yLevel(Pager*,in
53790 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 t,int);.SQLITE_P
537a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
537b0 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f e3PagerLockingMo
537c0 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 de(Pager *, int)
537d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
537e0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
537f0 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 rJournalMode(Pag
53800 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 er *, int);.SQLI
53810 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 TE_PRIVATE i64 s
53820 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
53830 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 alSizeLimit(Page
53840 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 r *, i64);.SQLIT
53850 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
53860 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 3_backup **sqlit
53870 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 e3PagerBackupPtr
53880 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 (Pager*);../* Fu
53890 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 nctions used to
538a0 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 61 obtain and relea
538b0 73 65 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 se page referenc
538c0 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 es. */ .SQLITE_P
538d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
538e0 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 50 e3PagerAcquire(P
538f0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
53900 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 20 no pgno, DbPage
53910 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 63 6c **ppPage, int cl
53920 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 20 rFlag);.#define
53930 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 sqlite3PagerGet(
53940 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 50 61 A,B,C) sqlite3Pa
53950 67 65 72 41 63 71 75 69 72 65 28 41 2c 42 2c 43 gerAcquire(A,B,C
53960 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ,0).SQLITE_PRIVA
53970 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 TE DbPage *sqlit
53980 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 e3PagerLookup(Pa
53990 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e ger *pPager, Pgn
539a0 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f o pgno);.SQLITE_
539b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
539c0 69 74 65 33 50 61 67 65 72 52 65 66 28 44 62 50 ite3PagerRef(DbP
539d0 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 age*);.SQLITE_PR
539e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
539f0 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 e3PagerUnref(DbP
53a00 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 61 age*);../* Opera
53a10 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 65 tions on page re
53a20 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 4c ferences. */.SQL
53a30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53a40 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
53a50 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 e(DbPage*);.SQLI
53a60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
53a70 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 sqlite3PagerDont
53a80 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a Write(DbPage*);.
53a90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53aa0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d nt sqlite3PagerM
53ab0 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 ovepage(Pager*,D
53ac0 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 bPage*,Pgno,int)
53ad0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53ae0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53af0 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 rPageRefcount(Db
53b00 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Page*);.SQLITE_P
53b10 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
53b20 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
53b30 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c (DbPage *); .SQL
53b40 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
53b50 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *sqlite3PagerGe
53b60 74 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 tExtra(DbPage *)
53b70 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 ; ../* Functions
53b80 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20 used to manage
53b90 70 61 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f pager transactio
53ba0 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69 6e 74 ns and savepoint
53bb0 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s. */.SQLITE_PRI
53bc0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53bd0 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 PagerPagecount(P
53be0 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 ager*, int*);.SQ
53bf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
53c00 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 sqlite3PagerBeg
53c10 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 65 in(Pager*, int e
53c20 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 4c xFlag, int);.SQL
53c30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53c40 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
53c50 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 itPhaseOne(Pager
53c60 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d *,const char *zM
53c70 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 4c aster, int);.SQL
53c80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53c90 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 sqlite3PagerSync
53ca0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b (Pager *pPager);
53cb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53cc0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
53cd0 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 CommitPhaseTwo(P
53ce0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
53cf0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
53d00 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 e3PagerRollback(
53d10 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
53d20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
53d30 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 te3PagerOpenSave
53d40 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 point(Pager *pPa
53d50 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c ger, int n);.SQL
53d60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
53d70 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 sqlite3PagerSave
53d80 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 point(Pager *pPa
53d90 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 ger, int op, int
53da0 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53 51 iSavepoint);.SQ
53db0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
53dc0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 61 sqlite3PagerSha
53dd0 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 redLock(Pager *p
53de0 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 6e 63 Pager);../* Func
53df0 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 71 75 tions used to qu
53e00 65 72 79 20 70 61 67 65 72 20 73 74 61 74 65 20 ery pager state
53e10 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f and configuratio
53e20 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n. */.SQLITE_PRI
53e30 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 VATE u8 sqlite3P
53e40 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 agerIsreadonly(P
53e50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ager*);.SQLITE_P
53e60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
53e70 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 e3PagerRefcount(
53e80 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Pager*);.SQLITE_
53e90 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
53ea0 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 ar *sqlite3Pager
53eb0 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a 29 Filename(Pager*)
53ec0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53ed0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 const sqlite3_v
53ee0 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 fs *sqlite3Pager
53ef0 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c Vfs(Pager*);.SQL
53f00 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
53f10 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 te3_file *sqlite
53f20 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65 72 3PagerFile(Pager
53f30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
53f40 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
53f50 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e qlite3PagerJourn
53f60 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a alname(Pager*);.
53f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53f80 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e nt sqlite3PagerN
53f90 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a 53 osync(Pager*);.S
53fa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
53fb0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
53fc0 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a TempSpace(Pager*
53fd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
53fe0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
53ff0 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 2a erIsMemdb(Pager*
54000 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 );../* Functions
54010 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61 74 used to truncat
54020 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 e the database f
54030 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ile. */.SQLITE_P
54040 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
54050 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 te3PagerTruncate
54060 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 67 6e Image(Pager*,Pgn
54070 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e o);../* Function
54080 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 65 73 s to support tes
54090 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 ting and debuggi
540a0 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 ng. */.#if !defi
540b0 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 ned(NDEBUG) || d
540c0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 efined(SQLITE_TE
540d0 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ST).SQLITE_PRIVA
540e0 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 74 65 TE Pgno sqlite
540f0 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 3PagerPagenumber
54100 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 (DbPage*);.SQLIT
54110 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
54120 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
54130 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 2a 29 iteable(DbPage*)
54140 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 ;.#endif.#ifdef
54150 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
54160 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
54170 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 *sqlite3PagerSt
54180 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c ats(Pager*);.SQL
54190 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
541a0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 id sqlite3PagerR
541b0 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 3b 0a efdump(Pager*);.
541c0 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 void disable_s
541d0 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
541e0 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 rs(void);. void
541f0 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 enable_simulate
54200 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 d_io_errors(void
54210 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
54220 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 e disable_simula
54230 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a ted_io_errors().
54240 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f # define enable_
54250 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
54260 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65 ors().#endif..#e
54270 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48 ndif /* _PAGER_H
54280 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a _ */../*********
54290 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 ***** End of pag
542a0 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.h ***********
542b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
542c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
542d0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
542e0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
542f0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
54300 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
54310 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
54320 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
54330 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 63 ***** Include pc
54340 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 ache.h in the mi
54350 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e ddle of sqliteIn
54360 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
54370 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
54380 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
54390 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a pcache.h ******
543a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
543b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
543c0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
543d0 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a August 05.**.**
543e0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
543f0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
54400 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
54410 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
54420 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
54430 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
54440 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
54450 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
54460 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
54470 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
54480 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
54490 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
544a0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
544b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
544c0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
544d0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
544e0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
544f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 0a 2a 2a 20 54 68 69 *********.** Thi
54540 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 s header file de
54550 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 fines the interf
54560 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c ace that the sql
54570 69 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a ite page cache.*
54580 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a 2a * subsystem. .**
54590 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63 .** @(#) $Id: pc
545a0 61 63 68 65 2e 68 2c 76 20 31 2e 32 30 20 32 30 ache.h,v 1.20 20
545b0 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36 3a 34 09/07/25 11:46:4
545c0 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 9 danielk1977 Ex
545d0 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 p $.*/..#ifndef
545e0 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65 _PCACHE_H_..type
545f0 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 def struct PgHdr
54600 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20 PgHdr;.typedef
54610 73 74 72 75 63 74 20 50 43 61 63 68 65 20 50 43 struct PCache PC
54620 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 ache;../*.** Eve
54630 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 ry page in the c
54640 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c ache is controll
54650 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 ed by an instanc
54660 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
54670 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ng.** structure.
54680 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 .*/.struct PgHdr
54690 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 {. void *pData
546a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
546b0 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f /* Content o
546c0 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 f this page */.
546d0 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 20 void *pExtra;
546e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
546f0 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e 74 /* Extra content
54700 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 */. PgHdr *pDi
54710 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 rty;
54720 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 6e /* Transien
54730 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 t list of dirty
54740 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 pages */. Pgno
54750 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 pgno;
54760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
54770 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 e number for thi
54780 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 65 s page */. Page
54790 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 r *pPager;
547a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
547b0 65 20 70 61 67 65 72 20 74 68 69 73 20 70 61 67 e pager this pag
547c0 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a e is part of */.
547d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 #ifdef SQLITE_CH
547e0 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 20 ECK_PAGES. u32
547f0 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 20 20 pageHash;
54800 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
54810 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 65 sh of page conte
54820 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 nt */.#endif. u
54830 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 16 flags;
54840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
54850 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 65 66 PGHDR flags def
54860 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20 ined below */..
54870 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /**************
54880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
548a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
548b0 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c ********. ** El
548c0 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 72 65 ements above are
548d0 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 68 public. All th
548e0 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 72 at follows is pr
548f0 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 65 2e ivate to pcache.
54900 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 6c c. ** and shoul
54910 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65 d not be accesse
54920 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c d by other modul
54930 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 6e es.. */. i16 n
54940 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
54950 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
54960 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 ber of users of
54970 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 this page */. P
54980 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 Cache *pCache;
54990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
549a0 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73 Cache that owns
549b0 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a 20 this page */..
549c0 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e 65 PgHdr *pDirtyNe
549d0 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 xt;
549e0 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 20 /* Next element
549f0 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 in list of dirty
54a00 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 pages */. PgHd
54a10 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b 20 20 r *pDirtyPrev;
54a20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 /* Pr
54a30 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 69 evious element i
54a40 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 n list of dirty
54a50 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 pages */.};../*
54a60 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 20 50 Bit values for P
54a70 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 64 gHdr.flags */.#d
54a80 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54 efine PGHDR_DIRT
54a90 59 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 Y 0x
54aa0 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73 002 /* Page has
54ab0 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 changed */.#def
54ac0 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 ine PGHDR_NEED_S
54ad0 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 30 YNC 0x00
54ae0 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20 4 /* Fsync the
54af0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
54b00 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20 before.
54b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
54b30 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 70 * writing this p
54b40 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 age to the datab
54b50 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ase */.#define P
54b60 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20 GHDR_NEED_READ
54b70 20 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a 0x008 /*
54b80 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65 Content is unre
54b90 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 ad */.#define PG
54ba0 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 HDR_REUSE_UNLIKE
54bb0 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20 LY 0x010 /*
54bc0 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 73 A hint that reus
54bd0 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f e is unlikely */
54be0 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 .#define PGHDR_D
54bf0 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 20 ONT_WRITE
54c00 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 0x020 /* Do no
54c10 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 t write content
54c20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 to disk */../* I
54c30 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 nitialize and sh
54c40 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 utdown the page
54c50 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 cache subsystem
54c60 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
54c70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
54c80 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f cheInitialize(vo
54c90 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 id);.SQLITE_PRIV
54ca0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
54cb0 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 PcacheShutdown(v
54cc0 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 oid);../* Page c
54cd0 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61 ache buffer mana
54ce0 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 gement:.** These
54cf0 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d routines implem
54d00 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ent SQLITE_CONFI
54d10 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a G_PAGECACHE..*/.
54d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
54d30 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 oid sqlite3PCach
54d40 65 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 eBufferSetup(voi
54d50 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 d *, int sz, int
54d60 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20 n);../* Create
54d70 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 68 a new pager cach
54d80 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f e..** Under memo
54d90 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b ry stress, invok
54da0 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 79 e xStress to try
54db0 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 to make pages c
54dc0 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c lean..** Only cl
54dd0 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64 ean and unpinned
54de0 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 65 pages can be re
54df0 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 claimed..*/.SQLI
54e00 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
54e10 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 sqlite3PcacheOpe
54e20 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c n(. int szPage,
54e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54e40 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 /* Size of e
54e50 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 very page */. i
54e60 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20 nt szExtra,
54e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
54e80 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 Extra space ass
54e90 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 ociated with eac
54ea0 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 h page */. int
54eb0 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 bPurgeable,
54ec0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
54ed0 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20 ue if pages are
54ee0 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 on backing store
54ef0 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 */. int (*xStr
54f00 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64 ess)(void*, PgHd
54f10 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 r*), /* Call to
54f20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 try to make page
54f30 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 s clean */. voi
54f40 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 d *pStress,
54f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
54f60 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 rgument to xStre
54f70 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a ss */. PCache *
54f80 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20 pToInit
54f90 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c /* Preall
54fa0 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 ocated space for
54fb0 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 the PCache */.)
54fc0 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 ;../* Modify the
54fd0 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72 page-size after
54fe0 20 74 68 65 20 63 61 63 68 65 20 68 61 73 20 62 the cache has b
54ff0 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a een created. */.
55000 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
55010 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
55020 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 eSetPageSize(PCa
55030 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a che *, int);../*
55040 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
55050 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 in bytes of a P
55060 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55 Cache object. U
55070 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61 sed to prealloca
55080 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70 te.** storage sp
55090 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ace..*/.SQLITE_P
550a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
550b0 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 e3PcacheSize(voi
550c0 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 d);../* One rele
550d0 61 73 65 20 70 65 72 20 73 75 63 63 65 73 73 66 ase per successf
550e0 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 20 ul fetch. Page
550f0 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20 is pinned until
55100 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66 released..** Ref
55110 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20 erence counted.
55120 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
55130 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
55140 61 63 68 65 46 65 74 63 68 28 50 43 61 63 68 65 acheFetch(PCache
55150 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65 *, Pgno, int cre
55160 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a ateFlag, PgHdr**
55170 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
55180 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
55190 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 acheRelease(PgHd
551a0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 r*);..SQLITE_PRI
551b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
551c0 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 3PcacheDrop(PgHd
551d0 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 r*); /*
551e0 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d Remove page from
551f0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 cache */.SQLITE
55200 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
55210 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
55220 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20 irty(PgHdr*);
55230 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61 /* Make sure pa
55240 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72 ge is marked dir
55250 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ty */.SQLITE_PRI
55260 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
55270 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 3PcacheMakeClean
55280 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 (PgHdr*); /*
55290 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61 Mark a single pa
552a0 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 ge as clean */.S
552b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
552c0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
552d0 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a CleanAll(PCache*
552e0 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c ); /* Mark al
552f0 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 67 l dirty list pag
55300 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a es as clean */..
55310 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 65 /* Change a page
55320 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62 number. Used b
55330 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a y incr-vacuum. *
55340 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55350 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55360 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20 cheMove(PgHdr*,
55370 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 Pgno);../* Remov
55380 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68 e all pages with
55390 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20 pgno>x. Reset
553a0 74 68 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d the cache if x==
553b0 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 0 */.SQLITE_PRIV
553c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
553d0 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 PcacheTruncate(P
553e0 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b Cache*, Pgno x);
553f0 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20 ../* Get a list
55400 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 of all dirty pag
55410 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c es in the cache,
55420 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 sorted by page
55430 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45 number */.SQLITE
55440 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a _PRIVATE PgHdr *
55450 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 sqlite3PcacheDir
55460 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b tyList(PCache*);
55470 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63 ../* Reset and c
55480 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f lose the cache o
55490 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f bject */.SQLITE_
554a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
554b0 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 ite3PcacheClose(
554c0 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c PCache*);../* Cl
554d0 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70 ear flags from p
554e0 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 ages of the page
554f0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 cache */.SQLITE
55500 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
55510 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 lite3PcacheClear
55520 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 SyncFlags(PCache
55530 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 64 *);../* Discard
55540 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
55550 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 the cache */.SQ
55560 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
55570 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 d sqlite3PcacheC
55580 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b 0a 0a lear(PCache*);..
55590 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f /* Return the to
555a0 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 tal number of ou
555b0 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 tstanding page r
555c0 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 4c eferences */.SQL
555d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
555e0 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
555f0 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a Count(PCache*);.
55600 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 ./* Increment th
55610 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
55620 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 t of an existing
55630 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 5f page */.SQLITE_
55640 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
55650 69 74 65 33 50 63 61 63 68 65 52 65 66 28 50 67 ite3PcacheRef(Pg
55660 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 Hdr*);..SQLITE_P
55670 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
55680 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 e3PcachePageRefc
55690 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a 2f ount(PgHdr*);../
556a0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 * Return the tot
556b0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
556c0 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 es stored in the
556d0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 cache */.SQLITE
556e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
556f0 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f ite3PcachePageco
55700 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 23 unt(PCache*);..#
55710 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
55720 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c E_CHECK_PAGES) |
55730 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
55740 5f 44 45 42 55 47 29 0a 2f 2a 20 49 74 65 72 61 _DEBUG)./* Itera
55750 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 64 te through all d
55760 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 irty pages curre
55770 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74 ntly stored in t
55780 68 65 20 63 61 63 68 65 2e 20 54 68 69 73 0a 2a he cache. This.*
55790 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f * interface is o
557a0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 nly available if
557b0 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 SQLITE_CHECK_PA
557c0 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 77 GES is defined w
557d0 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 72 hen the .** libr
557e0 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a 2f ary is built..*/
557f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
55800 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
55810 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 heIterateDirty(P
55820 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 Cache *pCache, v
55830 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 oid (*xIter)(PgH
55840 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a 0a dr *));.#endif..
55850 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74 20 74 /* Set and get t
55860 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 he suggested cac
55870 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 20 he-size for the
55880 73 70 65 63 69 66 69 65 64 20 70 61 67 65 72 2d specified pager-
55890 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 cache..**.** If
558a0 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 no global maximu
558b0 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 2c m is configured,
558c0 20 74 68 65 6e 20 74 68 65 20 73 79 73 74 65 6d then the system
558d0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d attempts to lim
558e0 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 it.** the total
558f0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
55900 63 61 63 68 65 64 20 62 79 20 70 75 72 67 65 61 cached by purgea
55910 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68 65 73 ble pager-caches
55920 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f to the sum.** o
55930 66 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 f the suggested
55940 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a cache-sizes..*/.
55950 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
55960 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
55970 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 eSetCachesize(PC
55980 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 ache *, int);.#i
55990 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
559a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
559b0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
559c0 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 eGetCachesize(PC
559d0 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 0a ache *);.#endif.
559e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
559f0 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e NABLE_MEMORY_MAN
55a00 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74 AGEMENT./* Try t
55a10 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20 o return memory
55a20 75 73 65 64 20 62 79 20 74 68 65 20 70 63 61 63 used by the pcac
55a30 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65 he module to the
55a40 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61 main memory hea
55a50 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 p */.SQLITE_PRIV
55a60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
55a70 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f cacheReleaseMemo
55a80 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a ry(int);.#endif.
55a90 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
55aa0 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 EST.SQLITE_PRIVA
55ab0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
55ac0 63 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c cacheStats(int*,
55ad0 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b int*,int*,int*);
55ae0 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f .#endif..SQLITE_
55af0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
55b00 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 ite3PCacheSetDef
55b10 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 6e ault(void);..#en
55b20 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f 48 dif /* _PCACHE_H
55b30 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a _ */../*********
55b40 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 ***** End of pca
55b50 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a che.h **********
55b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55b80 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
55b90 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
55ba0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
55bb0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
55bc0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
55bd0 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
55be0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f ****** Include o
55bf0 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c s.h in the middl
55c00 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 e of sqliteInt.h
55c10 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
55c20 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
55c30 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
55c40 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a e os.h *********
55c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55c70 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
55c80 31 20 53 65 70 74 65 6d 62 65 72 20 31 36 0a 2a 1 September 16.*
55c90 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
55ca0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
55cb0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
55cc0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
55cd0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
55ce0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
55cf0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
55d00 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
55d10 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
55d20 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
55d30 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
55d40 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
55d50 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
55d60 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
55d70 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
55d80 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
55d90 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
55da0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
55db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 **.**.** This he
55e00 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67 65 74 ader file (toget
55e10 68 65 72 20 77 69 74 68 20 69 73 20 63 6f 6d 70 her with is comp
55e20 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d 63 anion C source-c
55e30 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e ode file.** "os.
55e40 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f 20 61 c") attempt to a
55e50 62 73 74 72 61 63 74 20 74 68 65 20 75 6e 64 65 bstract the unde
55e60 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 rlying operating
55e70 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a system so that.
55e80 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 ** the SQLite li
55e90 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b 20 brary will work
55ea0 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 61 6e on both POSIX an
55eb0 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d d windows system
55ec0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 s..**.** This he
55ed0 61 64 65 72 20 66 69 6c 65 20 69 73 20 23 69 6e ader file is #in
55ee0 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71 6c 69 clude-ed by sqli
55ef0 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 73 teInt.h and thus
55f00 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 6e ends up.** bein
55f10 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20 65 76 g included by ev
55f20 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c 65 2e ery source file.
55f30 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 68 .**.** $Id: os.h
55f40 2c 76 20 31 2e 31 30 38 20 32 30 30 39 2f 30 32 ,v 1.108 2009/02
55f50 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 64 72 68 /05 16:31:46 drh
55f60 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
55f70 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a f _SQLITE_OS_H_.
55f80 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f #define _SQLITE_
55f90 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 OS_H_../*.** Fig
55fa0 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 61 72 ure out if we ar
55fb0 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 55 e dealing with U
55fc0 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f 72 nix, Windows, or
55fd0 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 6f some other.** o
55fe0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e perating system.
55ff0 20 20 41 66 74 65 72 20 74 68 65 20 66 6f 6c 6c After the foll
56000 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 70 owing block of p
56010 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 6f 73 reprocess macros
56020 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c 49 ,.** all of SQLI
56030 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c 49 TE_OS_UNIX, SQLI
56040 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c 49 54 TE_OS_WIN, SQLIT
56050 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 53 51 E_OS_OS2, and SQ
56060 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0a 2a LITE_OS_OTHER .*
56070 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 20 74 * will defined t
56080 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 30 2e o either 1 or 0.
56090 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 75 One of the fou
560a0 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 54 68 r will be 1. Th
560b0 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 72 65 e other .** thre
560c0 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f 0a e will be 0..*/.
560d0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
560e0 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 69 TE_OS_OTHER).# i
560f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 f SQLITE_OS_OTHE
56100 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 20 53 R==1.# undef S
56110 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 QLITE_OS_UNIX.#
56120 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
56130 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 75 6e OS_UNIX 0.# un
56140 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 def SQLITE_OS_WI
56150 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c N.# define SQL
56160 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 ITE_OS_WIN 0.#
56170 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 undef SQLITE_OS
56180 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e 65 20 _OS2.# define
56190 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a SQLITE_OS_OS2 0.
561a0 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64 65 66 # else.# undef
561b0 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 SQLITE_OS_OTHER
561c0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
561d0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
561e0 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26 20 ITE_OS_UNIX) &&
561f0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
56200 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65 66 69 OS_OTHER).# defi
56210 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 ne SQLITE_OS_OTH
56220 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20 53 51 ER 0.# ifndef SQ
56230 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 LITE_OS_WIN.#
56240 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 if defined(_WIN3
56250 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 57 49 2) || defined(WI
56260 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 N32) || defined(
56270 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20 64 __CYGWIN__) || d
56280 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33 32 efined(__MINGW32
56290 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
562a0 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20 20 _BORLANDC__).#
562b0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
562c0 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20 20 20 _OS_WIN 1.#
562d0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
562e0 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65 _UNIX 0.# de
562f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f fine SQLITE_OS_O
56300 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20 64 65 S2 0.# elif de
56310 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 20 7c fined(__EMX__) |
56320 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 29 20 | defined(_OS2)
56330 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32 29 20 || defined(OS2)
56340 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 5f || defined(_OS2_
56350 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4f ) || defined(__O
56360 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 S2__).# defi
56370 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e ne SQLITE_OS_WIN
56380 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 0.# define
56390 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 SQLITE_OS_UNIX 0
563a0 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 .# define SQ
563b0 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a 23 20 LITE_OS_OS2 1.#
563c0 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64 65 66 else.# def
563d0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 ine SQLITE_OS_WI
563e0 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 N 0.# define
563f0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 SQLITE_OS_UNIX
56400 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 1.# define S
56410 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 QLITE_OS_OS2 0.#
56420 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65 0a 23 endif.# else.#
56430 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
56440 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64 65 66 OS_UNIX 0.# def
56450 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 ine SQLITE_OS_OS
56460 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 2 0.# endif.#els
56470 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 e.# ifndef SQLIT
56480 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66 69 E_OS_WIN.# defi
56490 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e ne SQLITE_OS_WIN
564a0 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 0.# endif.#endi
564b0 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 f../*.** Determi
564c0 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 65 61 ne if we are dea
564d0 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 6f 77 ling with Window
564e0 73 43 45 20 2d 20 77 68 69 63 68 20 68 61 73 20 sCE - which has
564f0 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75 63 65 a much.** reduce
56500 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 d API..*/.#if de
56510 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57 43 45 fined(_WIN32_WCE
56520 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 ).# define SQLIT
56530 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23 65 6c E_OS_WINCE 1.#el
56540 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
56550 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a 23 65 TE_OS_WINCE 0.#e
56560 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 ndif.../*.** Def
56570 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 ine the maximum
56580 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 size of a tempor
56590 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a 2f 0a ary filename.*/.
565a0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
565b0 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77 69 6e N.# include <win
565c0 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69 6e 65 dows.h>.# define
565d0 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 SQLITE_TEMPNAME
565e0 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54 48 2b _SIZE (MAX_PATH+
565f0 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49 54 45 50).#elif SQLITE
56600 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28 5f 5f _OS_OS2.# if (__
56610 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 5f 5f GNUC__ > 3 || __
56620 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26 20 5f GNUC__ == 3 && _
56630 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 3e 3d _GNUC_MINOR__ >=
56640 20 33 29 20 26 26 20 64 65 66 69 6e 65 64 28 4f 3) && defined(O
56650 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 29 0a S2_HIGH_MEMORY).
56660 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 73 # include <os2s
56670 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20 74 6f afe.h> /* has to
56680 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62 65 66 be included bef
56690 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20 6c 69 ore os2.h for li
566a0 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20 2a 2f nking to work */
566b0 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e .# endif.# defin
566c0 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45 54 49 e INCL_DOSDATETI
566d0 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c ME.# define INCL
566e0 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20 64 65 _DOSFILEMGR.# de
566f0 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 52 52 fine INCL_DOSERR
56700 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 ORS.# define INC
56710 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65 66 69 L_DOSMISC.# defi
56720 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f 43 45 ne INCL_DOSPROCE
56730 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c SS.# define INCL
56740 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a 23 20 _DOSMODULEMGR.#
56750 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 53 define INCL_DOSS
56760 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e 63 6c EMAPHORES.# incl
56770 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 69 6e ude <os2.h>.# in
56780 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68 3e 0a clude <uconv.h>.
56790 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
567a0 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 43 TEMPNAME_SIZE (C
567b0 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29 0a 23 CHMAXPATHCOMP).#
567c0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
567d0 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 LITE_TEMPNAME_SI
567e0 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a 0a 2f ZE 200.#endif../
567f0 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46 55 4c * If the SET_FUL
56800 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73 20 6e LSYNC macro is n
56810 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 ot defined above
56820 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74 0a 2a , then make it.*
56830 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69 66 * a no-op.*/.#if
56840 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53 59 4e ndef SET_FULLSYN
56850 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f 46 C.# define SET_F
56860 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23 65 6e ULLSYNC(x,y).#en
56870 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 dif../*.** The d
56880 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 efault size of a
56890 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a 2f 0a disk sector.*/.
568a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
568b0 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 EFAULT_SECTOR_SI
568c0 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 ZE.# define SQLI
568d0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f TE_DEFAULT_SECTO
568e0 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e 64 69 R_SIZE 512.#endi
568f0 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72 61 f../*.** Tempora
56900 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e 61 6d ry files are nam
56910 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 ed starting with
56920 20 74 68 69 73 20 70 72 65 66 69 78 20 66 6f 6c this prefix fol
56930 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61 6e 64 lowed by 16 rand
56940 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d 65 72 om.** alphanumer
56950 69 63 20 63 68 61 72 61 63 74 65 72 73 2c 20 61 ic characters, a
56960 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74 65 6e nd no file exten
56970 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65 20 73 sion. They are s
56980 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 tored in the.**
56990 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20 74 65 OS's standard te
569a0 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72 mporary file dir
569b0 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72 65 20 ectory, and are
569c0 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20 74 6f deleted prior to
569d0 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73 71 6c exit..** If sql
569e0 69 74 65 20 69 73 20 62 65 69 6e 67 20 65 6d 62 ite is being emb
569f0 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68 65 72 edded in another
56a00 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 6d 61 program, you ma
56a10 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e 67 65 y wish to change
56a20 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 20 74 the.** prefix t
56a30 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72 20 70 o reflect your p
56a40 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c 20 73 rogram's name, s
56a50 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72 20 70 o that if your p
56a60 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 rogram exits.**
56a70 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f 6c 64 prematurely, old
56a80 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 temporary files
56a90 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79 20 69 can be easily i
56aa0 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69 73 20 dentified. This
56ab0 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 75 can be done.** u
56ac0 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 54 45 sing -DSQLITE_TE
56ad0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 3d 6d MP_FILE_PREFIX=m
56ae0 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68 65 20 yprefix_ on the
56af0 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 compiler command
56b00 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 line..**.** 200
56b10 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20 64 65 6-10-31: The de
56b20 66 61 75 6c 74 20 70 72 65 66 69 78 20 75 73 65 fault prefix use
56b30 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65 5f d to be "sqlite_
56b40 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a 2a 20 ". But then.**
56b50 4d 63 61 66 65 65 20 73 74 61 72 74 65 64 20 75 Mcafee started u
56b60 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 20 74 sing SQLite in t
56b70 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75 73 20 heir anti-virus
56b80 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74 0a 2a product and it.*
56b90 2a 20 73 74 61 72 74 65 64 20 70 75 74 74 69 6e * started puttin
56ba0 67 20 66 69 6c 65 73 20 77 69 74 68 20 74 68 65 g files with the
56bb0 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65 20 69 "sqlite" name i
56bc0 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f n the c:/temp fo
56bd0 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20 61 6e lder..** This an
56be0 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e 64 6f noyed many windo
56bf0 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f 73 65 ws users. Those
56c00 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74 68 65 users would the
56c10 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 6c n do a .** Googl
56c20 65 20 73 65 61 72 63 68 20 66 6f 72 20 22 73 71 e search for "sq
56c30 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68 65 20 lite", find the
56c40 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 65 72 telephone number
56c50 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 65 s of the.** deve
56c60 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c 6c 20 lopers and call
56c70 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75 70 20 to wake them up
56c80 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63 6f 6d at night and com
56c90 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 68 plain..** For th
56ca0 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 is reason, the d
56cb0 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72 65 66 efault name pref
56cc0 69 78 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f ix is changed to
56cd0 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a 2a 2a be "sqlite" .**
56ce0 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77 61 72 spelled backwar
56cf0 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65 6d 70 ds. So the temp
56d00 20 66 69 6c 65 73 20 61 72 65 20 73 74 69 6c 6c files are still
56d10 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62 75 74 identified, but
56d20 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d 61 72 .** anybody smar
56d30 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69 67 75 t enough to figu
56d40 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64 65 20 re out the code
56d50 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 73 is also likely s
56d60 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 mart.** enough t
56d70 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61 6c 6c o know that call
56d80 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f 70 65 ing the develope
56d90 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 20 r will not help
56da0 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20 74 68 get rid.** of th
56db0 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 e file..*/.#ifnd
56dc0 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 ef SQLITE_TEMP_F
56dd0 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64 65 66 ILE_PREFIX.# def
56de0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f ine SQLITE_TEMP_
56df0 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65 74 69 FILE_PREFIX "eti
56e00 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a lqs_".#endif../*
56e10 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
56e20 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20 g values may be
56e30 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
56e40 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
56e50 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 .** sqlite3OsLoc
56e60 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f 75 73 k(). The various
56e70 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74 20 74 locks exhibit t
56e80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 6d he following sem
56e90 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 antics:.**.** SH
56ea0 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e 75 6d ARED: Any num
56eb0 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73 65 73 ber of processes
56ec0 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48 41 52 may hold a SHAR
56ed0 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 61 6e ED lock simultan
56ee0 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 45 52 eously..** RESER
56ef0 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65 20 70 VED: A single p
56f00 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 rocess may hold
56f10 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
56f20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 on a file at.**
56f30 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 74 any t
56f40 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f 63 65 ime. Other proce
56f50 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 6e sses may hold an
56f60 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53 48 41 d obtain new SHA
56f70 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50 45 RED locks..** PE
56f80 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e 67 6c NDING: A singl
56f90 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f e process may ho
56fa0 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 ld a PENDING loc
56fb0 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a k on a file at.*
56fc0 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 * any
56fd0 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69 73 74 one time. Exist
56fe0 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 ing SHARED locks
56ff0 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20 62 75 may persist, bu
57000 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 20 20 t no new.**
57010 20 20 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f SHARED lo
57020 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74 61 69 cks may be obtai
57030 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70 72 6f ned by other pro
57040 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43 4c 55 cesses..** EXCLU
57050 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55 53 49 SIVE: An EXCLUSI
57060 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75 64 65 VE lock preclude
57070 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b s all other lock
57080 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e 47 s..**.** PENDING
57090 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 62 65 _LOCK may not be
570a0 20 70 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 passed directly
570b0 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 to sqlite3OsLoc
570c0 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 61 0a k(). Instead, a.
570d0 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 ** process that
570e0 72 65 71 75 65 73 74 73 20 61 6e 20 45 58 43 4c requests an EXCL
570f0 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79 20 61 USIVE lock may a
57100 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e 20 61 ctually obtain a
57110 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63 6b PENDING.** lock
57120 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 70 . This can be up
57130 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43 graded to an EXC
57140 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79 20 61 LUSIVE lock by a
57150 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
57160 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 to.** sqlite3Os
57170 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 Lock()..*/.#defi
57180 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 ne NO_LOCK
57190 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 48 41 0.#define SHA
571a0 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 RED_LOCK 1.#
571b0 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f define RESERVED_
571c0 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69 6e 65 LOCK 2.#define
571d0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20 20 PENDING_LOCK
571e0 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43 4c 55 3.#define EXCLU
571f0 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f 2a SIVE_LOCK 4../*
57200 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 .** File Locking
57210 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74 6c 79 Notes: (Mostly
57220 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73 20 62 about windows b
57230 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69 6e 66 ut also some inf
57240 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a 2a o for Unix).**.*
57250 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 * We cannot use
57260 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 72 20 LockFileEx() or
57270 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f UnlockFileEx() o
57280 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 62 65 n Win95/98/ME be
57290 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65 20 66 cause.** those f
572a0 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 unctions are not
572b0 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53 6f 20 available. So
572c0 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f 63 6b we use only Lock
572d0 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 55 6e File() and.** Un
572e0 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a 2a lockFile()..**.*
572f0 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70 72 65 * LockFile() pre
57300 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74 20 77 vents not just w
57310 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73 6f 20 riting but also
57320 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68 65 72 reading by other
57330 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 41 processes..** A
57340 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69 73 20 SHARED_LOCK is
57350 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b obtained by lock
57360 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 61 6e ing a single ran
57370 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a 2a domly-chosen .**
57380 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61 20 73 byte out of a s
57390 70 65 63 69 66 69 63 20 72 61 6e 67 65 20 6f 66 pecific range of
573a0 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f 63 6b bytes. The lock
573b0 20 62 79 74 65 20 69 73 20 6f 62 74 61 69 6e 65 byte is obtaine
573c0 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 d at .** random
573d0 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 so two separate
573e0 72 65 61 64 65 72 73 20 63 61 6e 20 70 72 6f 62 readers can prob
573f0 61 62 6c 79 20 61 63 63 65 73 73 20 74 68 65 20 ably access the
57400 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a 2a 20 file at the .**
57410 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c 65 73 same time, unles
57420 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c 75 63 s they are unluc
57430 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20 74 68 ky and choose th
57440 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79 74 65 e same lock byte
57450 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 ..** An EXCLUSIV
57460 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e E_LOCK is obtain
57470 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 6c ed by locking al
57480 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 l bytes in the r
57490 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65 20 63 ange..** There c
574a0 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 20 77 an only be one w
574b0 72 69 74 65 72 2e 20 20 41 20 52 45 53 45 52 56 riter. A RESERV
574c0 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 ED_LOCK is obtai
574d0 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 0a 2a ned by locking.*
574e0 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 * a single byte
574f0 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 of the file that
57500 20 69 73 20 64 65 73 69 67 6e 61 74 65 64 20 61 is designated a
57510 73 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c s the reserved l
57520 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 20 50 ock byte..** A P
57530 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 20 6f ENDING_LOCK is o
57540 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 btained by locki
57550 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65 64 20 ng a designated
57560 62 79 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 byte different f
57570 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53 45 52 rom.** the RESER
57580 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 2a VED_LOCK byte..*
57590 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32 4b *.** On WinNT/2K
575a0 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c 6f 63 /XP systems, Loc
575b0 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20 55 6e kFileEx() and Un
575c0 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 72 65 lockFileEx() are
575d0 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20 77 available,.** w
575e0 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20 63 61 hich means we ca
575f0 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77 72 69 n use reader/wri
57600 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68 65 6e ter locks. When
57610 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c reader/writer l
57620 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73 65 64 ocks.** are used
57630 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 70 6c , the lock is pl
57640 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 aced on the same
57650 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 range of bytes
57660 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 that is used.**
57670 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73 74 69 for probabilisti
57680 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 69 6e c locking in Win
57690 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e 63 65 95/98/ME. Hence
576a0 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 , the locking sc
576b0 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 75 70 heme.** will sup
576c0 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 port two or more
576d0 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 20 6f Win95 readers o
576e0 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 r two or more Wi
576f0 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a 2a 20 nNT readers..**
57700 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57 69 6e But a single Win
57710 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 95 reader will l
57720 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69 6e 4e ock out all WinN
57730 54 20 72 65 61 64 65 72 73 20 61 6e 64 20 61 20 T readers and a
57740 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54 20 single.** WinNT
57750 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b reader will lock
57760 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72 20 57 out all other W
57770 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a 2a 2a in95 readers..**
57780 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
57790 67 20 23 64 65 66 69 6e 65 73 20 73 70 65 63 69 g #defines speci
577a0 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 fy the range of
577b0 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c bytes used for l
577c0 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 52 45 ocking..** SHARE
577d0 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 D_SIZE is the nu
577e0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 mber of bytes av
577f0 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 ailable in the p
57800 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68 0a 2a ool from which.*
57810 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 * a random byte
57820 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f 72 20 is selected for
57830 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 a shared lock.
57840 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79 74 65 The pool of byte
57850 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65 64 20 s for.** shared
57860 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61 74 20 locks begins at
57870 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20 0a 2a SHARED_FIRST. .*
57880 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 6c 6f *.** The same lo
57890 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 61 cking strategy a
578a0 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e 67 65 nd.** byte range
578b0 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 55 s are used for U
578c0 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61 76 65 nix. This leave
578d0 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73 73 69 s open the possi
578e0 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e 67 0a blity of having.
578f0 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20 77 69 ** clients on wi
57900 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e 64 20 n95, winNT, and
57910 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69 6e 67 unix all talking
57920 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 68 61 to the same sha
57930 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 red file.** and
57940 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 72 72 all locking corr
57950 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20 73 6f ectly. To do so
57960 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65 20 74 would require t
57970 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20 77 68 hat samba (or wh
57980 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20 69 atever.** tool i
57990 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72 s being used for
579a0 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29 20 69 file sharing) i
579b0 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b 73 20 mplements locks
579c0 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77 65 65 correctly betwee
579d0 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61 6e 64 n.** windows and
579e0 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75 65 73 unix. I'm gues
579f0 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27 74 20 sing that isn't
57a00 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70 65 6e likely to happen
57a10 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73 69 6e , but by.** usin
57a20 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 g the same locki
57a30 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72 65 20 ng range we are
57a40 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20 74 6f at least open to
57a50 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 the possibility
57a60 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 20 ..**.** Locking
57a70 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20 6d 61 in windows is ma
57a80 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20 74 68 nditory. For th
57a90 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20 63 61 is reason, we ca
57aa0 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 61 63 nnot store.** ac
57ab0 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74 68 65 tual data in the
57ac0 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 20 bytes used for
57ad0 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20 70 61 locking. The pa
57ae0 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 ger never alloca
57af0 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 tes.** the pages
57b00 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f 63 involved in loc
57b10 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65 2e 20 king therefore.
57b20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73 20 SHARED_SIZE is
57b30 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a 20 74 selected so.** t
57b40 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 hat all locks wi
57b50 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 ll fit on a sing
57b60 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61 74 20 le page even at
57b70 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67 65 the minimum page
57b80 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49 4e size..** PENDIN
57b90 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 20 74 G_BYTE defines t
57ba0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
57bb0 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20 64 the locks. By d
57bc0 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f 42 efault PENDING_B
57bd0 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20 68 69 YTE.** is set hi
57be0 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f gh so that we do
57bf0 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c 6c 6f n't have to allo
57c00 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 20 70 cate an unused p
57c10 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f age except.** fo
57c20 72 20 76 65 72 79 20 6c 61 72 67 65 20 64 61 74 r very large dat
57c30 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f 6e 65 abases. But one
57c40 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74 68 65 should test the
57c50 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67 20 6c page skipping l
57c60 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65 74 74 ogic .** by sett
57c70 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 ing PENDING_BYTE
57c80 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69 6e 67 low and running
57c90 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 67 72 the entire regr
57ca0 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a 2a 2a ession suite..**
57cb0 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65 .** Changing the
57cc0 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44 49 4e value of PENDIN
57cd0 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73 20 69 G_BYTE results i
57ce0 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63 6f 6d n a subtly incom
57cf0 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c 65 20 patible.** file
57d00 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e 64 69 format. Dependi
57d10 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69 73 20 ng on how it is
57d20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d 69 67 changed, you mig
57d30 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a 2a ht not notice.**
57d40 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69 62 69 the incompatibi
57d50 6c 69 74 79 20 72 69 67 68 74 20 61 77 61 79 2c lity right away,
57d60 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20 61 20 even running a
57d70 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f 6e 20 full regression
57d80 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64 65 66 test..** The def
57d90 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 ault location of
57da0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 69 73 PENDING_BYTE is
57db0 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 the first byte
57dc0 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47 42 20 past the.** 1GB
57dd0 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f 0a boundary..**.*/.
57de0 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f #define PENDING_
57df0 42 59 54 45 20 20 20 20 20 20 73 71 6c 69 74 65 BYTE sqlite
57e00 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23 64 65 3PendingByte.#de
57e10 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f 42 59 fine RESERVED_BY
57e20 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f TE (PENDING_
57e30 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e 65 20 BYTE+1).#define
57e40 53 48 41 52 45 44 5f 46 49 52 53 54 20 20 20 20 SHARED_FIRST
57e50 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b (PENDING_BYTE+
57e60 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 2).#define SHARE
57e70 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35 31 30 D_SIZE 510
57e80 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 ../*.** Wrapper
57e90 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63 69 66 around OS specif
57ea0 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e ic sqlite3_os_in
57eb0 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a it() function..*
57ec0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
57ed0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 6e int sqlite3OsIn
57ee0 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 0a 2a it(void);../* .*
57ef0 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 * Functions for
57f00 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 accessing sqlite
57f10 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 3_file methods .
57f20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
57f30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
57f40 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
57f50 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
57f60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
57f70 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 sRead(sqlite3_fi
57f80 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 le*, void*, int
57f90 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 amt, i64 offset)
57fa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
57fb0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 int sqlite3OsWr
57fc0 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ite(sqlite3_file
57fd0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
57fe0 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 int amt, i64 off
57ff0 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 set);.SQLITE_PRI
58000 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
58010 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 OsTruncate(sqlit
58020 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 e3_file*, i64 si
58030 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ze);.SQLITE_PRIV
58040 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
58050 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 sSync(sqlite3_fi
58060 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 le*, int);.SQLIT
58070 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
58080 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
58090 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
580a0 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 64 *pSize);.SQLI
580b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
580c0 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c qlite3OsLock(sql
580d0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 ite3_file*, int)
580e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
580f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e int sqlite3OsUn
58100 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
58110 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 e*, int);.SQLITE
58120 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
58130 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 ite3OsCheckReser
58140 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
58150 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
58160 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f ResOut);.SQLITE_
58170 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
58180 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c te3OsFileControl
58190 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 (sqlite3_file*,i
581a0 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 nt,void*);.#defi
581b0 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f ne SQLITE_FCNTL_
581c0 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63 DB_UNCHANGED 0xc
581d0 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50 a093fa0.SQLITE_P
581e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
581f0 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 e3OsSectorSize(s
58200 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
58210 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
58220 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 int sqlite3OsDe
58230 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
58240 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ics(sqlite3_file
58250 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 *id);../* .** F
58260 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 unctions for acc
58270 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76 essing sqlite3_v
58280 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 fs methods .*/.S
58290 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
582a0 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 t sqlite3OsOpen(
582b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 sqlite3_vfs *, c
582c0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c onst char *, sql
582d0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c ite3_file*, int,
582e0 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f int *);.SQLITE_
582f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
58300 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 te3OsDelete(sqli
58310 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 te3_vfs *, const
58320 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 char *, int);.S
58330 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
58340 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 t sqlite3OsAcces
58350 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c s(sqlite3_vfs *,
58360 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 const char *, i
58370 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 nt, int *pResOut
58380 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
58390 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 E int sqlite3OsF
583a0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 ullPathname(sqli
583b0 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 te3_vfs *, const
583c0 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 char *, int, ch
583d0 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 ar *);.#ifndef S
583e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
583f0 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 EXTENSION.SQLITE
58400 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
58410 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 qlite3OsDlOpen(s
58420 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f qlite3_vfs *, co
58430 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nst char *);.SQL
58440 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
58450 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f sqlite3OsDlErro
58460 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c r(sqlite3_vfs *,
58470 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 int, char *);.S
58480 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
58490 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c id (*sqlite3OsDl
584a0 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 Sym(sqlite3_vfs
584b0 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 *, void *, const
584c0 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b char *))(void);
584d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
584e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c void sqlite3OsDl
584f0 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 Close(sqlite3_vf
58500 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65 s *, void *);.#e
58510 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
58520 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
58530 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ON */.SQLITE_PRI
58540 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
58550 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c OsRandomness(sql
58560 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c ite3_vfs *, int,
58570 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 char *);.SQLITE
58580 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
58590 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 ite3OsSleep(sqli
585a0 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b te3_vfs *, int);
585b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
585c0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 int sqlite3OsCur
585d0 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 rentTime(sqlite3
585e0 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29 _vfs *, double*)
585f0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 ;../*.** Conveni
58600 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 66 ence functions f
58610 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63 or opening and c
58620 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75 73 69 losing files usi
58630 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ng .** sqlite3_m
58640 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 69 alloc() to obtai
58650 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 n space for the
58660 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 75 file-handle stru
58670 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cture..*/.SQLITE
58680 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
58690 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 ite3OsOpenMalloc
586a0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 (sqlite3_vfs *,
586b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 const char *, sq
586c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 lite3_file **, i
586d0 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 nt,int*);.SQLITE
586e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
586f0 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 ite3OsCloseFree(
58700 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b sqlite3_file *);
58710 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c ..#endif /* _SQL
58720 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a ITE_OS_H_ */../*
58730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
58740 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a d of os.h ******
58750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
58780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
58790 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
587a0 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 e left off in sq
587b0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
587c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
587d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
587e0 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20 69 6e clude mutex.h in
587f0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 the middle of s
58800 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
58810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
58820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
58830 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 68 gin file mutex.h
58840 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
58850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
58870 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 .** 2007 August
58880 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 28.**.** The aut
58890 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
588a0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
588b0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
588c0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
588d0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
588e0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
588f0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
58900 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
58910 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
58920 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
58930 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
58940 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
58950 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
58960 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
58970 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
58980 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
58990 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
589a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
589b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
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 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
589f0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 e contains the c
58a00 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f 72 ommon header for
58a10 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 all mutex imple
58a20 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 mentations..** T
58a30 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 68 he sqliteInt.h h
58a40 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65 73 20 eader #includes
58a50 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61 this file so tha
58a60 74 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c t it is availabl
58a70 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 72 e.** to all sour
58a80 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20 62 72 ce files. We br
58a90 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 6e eak it out in an
58aa0 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 effort to keep
58ab0 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 74 the code.** bett
58ac0 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a er organized..**
58ad0 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63 .** NOTE: sourc
58ae0 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a e files should *
58af0 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68 not* #include th
58b00 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 is header file d
58b10 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 irectly..** Sour
58b20 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 ce files should
58b30 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c #include the sql
58b40 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e iteInt.h file an
58b50 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0a d let that file.
58b60 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69 73 20 ** include this
58b70 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79 2e 0a one indirectly..
58b80 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 **.** $Id: mutex
58b90 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 30 .h,v 1.9 2008/10
58ba0 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 /07 15:25:48 drh
58bb0 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a Exp $.*/.../*.*
58bc0 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 * Figure out wha
58bd0 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 t version of the
58be0 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 code to use. T
58bf0 68 65 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a he choices are.*
58c00 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 *.** SQLITE_MU
58c10 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 TEX_OMIT
58c20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e No mutex logic.
58c30 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 Not even stubs
58c40 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 . The.**
58c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58c60 20 20 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d mutexes im
58c70 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f plemention canno
58c80 74 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a t be overridden.
58c90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
58ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
58cb0 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a t start-time..**
58cc0 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 .** SQLITE_MUT
58cd0 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 EX_NOOP
58ce0 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 For single-threa
58cf0 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 ded applications
58d00 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 . No.**
58d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58d20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c mutual excl
58d30 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 usion is provide
58d40 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 d. But this.**
58d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58d60 20 20 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c impl
58d70 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 ementation can b
58d80 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a e overridden at.
58d90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
58da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
58db0 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a tart-time..**.**
58dc0 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f SQLITE_MUTEX_
58dd0 50 54 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 PTHREADS For
58de0 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 multi-threaded
58df0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 applications on
58e00 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 Unix..**.** SQ
58e10 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 LITE_MUTEX_W32
58e20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 For mult
58e30 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 i-threaded appli
58e40 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 cations on Win32
58e50 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 ..**.** SQLITE
58e60 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 _MUTEX_OS2
58e70 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 For multi-th
58e80 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 readed applicati
58e90 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a ons on OS/2..*/.
58ea0 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 #if !SQLITE_THRE
58eb0 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 ADSAFE.# define
58ec0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 SQLITE_MUTEX_OMI
58ed0 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c T.#endif.#if SQL
58ee0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
58ef0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 & !defined(SQLIT
58f00 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 E_MUTEX_NOOP).#
58f10 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e if SQLITE_OS_UN
58f20 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 IX.# define S
58f30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 QLITE_MUTEX_PTHR
58f40 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c EADS.# elif SQL
58f50 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 ITE_OS_WIN.#
58f60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 define SQLITE_MU
58f70 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 TEX_W32.# elif
58f80 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 SQLITE_OS_OS2.#
58f90 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
58fa0 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c _MUTEX_OS2.# el
58fb0 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 se.# define S
58fc0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 QLITE_MUTEX_NOOP
58fd0 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 .# endif.#endif
58fe0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
58ff0 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a MUTEX_OMIT./*.**
59000 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f If this is a no
59010 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 -op implementati
59020 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 on, implement ev
59030 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 erything as macr
59040 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 os..*/.#define s
59050 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
59060 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 oc(X) ((sqlit
59070 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 e3_mutex*)8).#de
59080 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 fine sqlite3_mut
59090 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 ex_free(X).#defi
590a0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
590b0 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e _enter(X).#defin
590c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
590d0 74 72 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 try(X) SQLI
590e0 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 TE_OK.#define sq
590f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
59100 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c e(X).#define sql
59110 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
59120 58 29 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 X) 1.#define
59130 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
59140 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 otheld(X) 1.#de
59150 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 fine sqlite3Mute
59160 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 xAlloc(X) (
59170 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 (sqlite3_mutex*)
59180 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 8).#define sqlit
59190 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 e3MutexInit()
591a0 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 SQLITE_OK.#
591b0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 define sqlite3Mu
591c0 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 texEnd().#endif
591d0 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 /* defined(SQLIT
591e0 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 29 20 2a 2f E_OMIT_MUTEX) */
591f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
59200 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 * End of mutex.h
59210 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
59220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59240 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
59250 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
59260 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
59270 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
59280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59290 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 /.../*.** Each d
592a0 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
592b0 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 be accessed by t
592c0 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 he system is an
592d0 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 instance.** of t
592e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
592f0 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 ucture. There a
59300 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 re normally two
59310 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 of these structu
59320 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 res.** in the sq
59330 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 lite.aDb[] array
59340 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 . aDb[0] is the
59350 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
59360 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 ile and.** aDb[1
59370 5d 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 ] is the databas
59380 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 e file used to h
59390 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 old temporary ta
593a0 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 bles. Additiona
593b0 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d l.** databases m
593c0 61 79 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a ay be attached..
593d0 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 */.struct Db {.
593e0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
593f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
59400 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a this database *
59410 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 /. Btree *pBt;
59420 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
59430 42 2a 54 72 65 65 20 73 74 72 75 63 74 75 72 65 B*Tree structure
59440 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 for this databa
59450 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 se file */. u8
59460 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 inTrans;
59470 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 /* 0: not writ
59480 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 able. 1: Transa
59490 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b ction. 2: Check
594a0 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 point */. u8 sa
594b0 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 fety_level;
594c0 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 /* How aggressiv
594d0 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 e at syncing dat
594e0 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 53 a to disk */. S
594f0 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 chema *pSchema;
59500 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
59510 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d o database schem
59520 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61 72 a (possibly shar
59530 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ed) */.};../*.**
59540 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
59550 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
59560 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 20 61 ructure stores a
59570 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
59580 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ..**.** If there
59590 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c 20 are no virtual
595a0 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 65 tables configure
595b0 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d 61 d in this schema
595c0 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 2e , the.** Schema.
595d0 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 db variable is s
595e0 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 et to NULL. Afte
595f0 72 20 74 68 65 20 66 69 72 73 74 20 76 69 72 74 r the first virt
59600 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 ual table.** has
59610 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74 20 been added, it
59620 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 is set to point
59630 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
59640 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 connection .** u
59650 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 sed to create th
59660 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e e connection. On
59670 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 ce a virtual tab
59680 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 le has been.** a
59690 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 68 65 dded to the Sche
596a0 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 ma structure and
596b0 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20 76 the Schema.db v
596c0 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 65 ariable populate
596d0 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 d, .** only that
596e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
596f0 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 65 tion may use the
59700 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70 61 Schema to prepa
59710 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 re .** statement
59720 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 68 s..*/.struct Sch
59730 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 65 ema {. int sche
59740 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 ma_cookie; /*
59750 44 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 Database schema
59760 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 66 version number f
59770 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a or this file */.
59780 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b 20 Hash tblHash;
59790 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 /* All ta
597a0 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 bles indexed by
597b0 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 69 name */. Hash i
597c0 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f dxHash; /
597d0 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 6e * All (named) in
597e0 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 62 79 dices indexed by
597f0 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 name */. Hash
59800 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 20 20 trigHash;
59810 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 /* All triggers
59820 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 indexed by name
59830 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 65 71 */. Table *pSeq
59840 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 Tab; /* The
59850 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 sqlite_sequence
59860 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 41 table used by A
59870 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a UTOINCREMENT */.
59880 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 6d 61 74 u8 file_format
59890 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 ; /* Schema
598a0 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e 20 format version
598b0 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f for this file */
598c0 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20 . u8 enc;
598d0 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 /* Text
598e0 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 encoding used by
598f0 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a this database *
59900 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 /. u16 flags;
59910 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
59920 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
59930 68 20 74 68 69 73 20 73 63 68 65 6d 61 20 2a 2f h this schema */
59940 0a 20 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a . int cache_siz
59950 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 e; /* Numbe
59960 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 75 73 r of pages to us
59970 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a e in the cache *
59980 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
59990 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
599a0 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 LE. sqlite3 *db
599b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 22 4f 77 ; /* "Ow
599c0 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e ner" connection.
599d0 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f See comment abo
599e0 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a ve */.#endif.};.
599f0 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 ./*.** These mac
59a00 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 ros can be used
59a10 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f 72 to test, set, or
59a20 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 74 clear bits in t
59a30 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 67 73 20 he .** Db.flags
59a40 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e field..*/.#defin
59a50 65 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 e DbHasProperty(
59a60 44 2c 49 2c 50 29 20 20 20 20 20 28 28 28 44 29 D,I,P) (((D)
59a70 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 ->aDb[I].pSchema
59a80 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 50 ->flags&(P))==(P
59a90 29 29 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73 )).#define DbHas
59aa0 41 6e 79 50 72 6f 70 65 72 74 79 28 44 2c 49 2c AnyProperty(D,I,
59ab0 50 29 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 P) (((D)->aDb[I
59ac0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 ].pSchema->flags
59ad0 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69 6e &(P))!=0).#defin
59ae0 65 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28 e DbSetProperty(
59af0 44 2c 49 2c 50 29 20 20 20 20 20 28 44 29 2d 3e D,I,P) (D)->
59b00 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e aDb[I].pSchema->
59b10 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 69 flags|=(P).#defi
59b20 6e 65 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 ne DbClearProper
59b30 74 79 28 44 2c 49 2c 50 29 20 20 20 28 44 29 2d ty(D,I,P) (D)-
59b40 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d >aDb[I].pSchema-
59b50 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a >flags&=~(P)../*
59b60 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
59b70 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e 66 6c es for the DB.fl
59b80 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a ags field..**.**
59b90 20 54 68 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f The DB_SchemaLo
59ba0 61 64 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 aded flag is set
59bb0 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62 after the datab
59bc0 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 ase schema has b
59bd0 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 6e 74 6f een.** read into
59be0 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 internal hash t
59bf0 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 42 5f ables..**.** DB_
59c00 55 6e 72 65 73 65 74 56 69 65 77 73 20 6d 65 61 UnresetViews mea
59c10 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d ns that one or m
59c20 6f 72 65 20 76 69 65 77 73 20 68 61 76 65 20 63 ore views have c
59c30 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 61 74 olumn names that
59c40 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 66 69 .** have been fi
59c50 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20 74 68 lled out. If th
59c60 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 e schema changes
59c70 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 20 6e , these column n
59c80 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a 20 63 68 ames might.** ch
59c90 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74 68 65 anges and so the
59ca0 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65 64 20 view will need
59cb0 74 6f 20 62 65 20 72 65 73 65 74 2e 0a 2a 2f 0a to be reset..*/.
59cc0 23 64 65 66 69 6e 65 20 44 42 5f 53 63 68 65 6d #define DB_Schem
59cd0 61 4c 6f 61 64 65 64 20 20 20 20 30 78 30 30 30 aLoaded 0x000
59ce0 31 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61 1 /* The schema
59cf0 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64 has been loaded
59d00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 55 */.#define DB_U
59d10 6e 72 65 73 65 74 56 69 65 77 73 20 20 20 20 30 nresetViews 0
59d20 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 65 20 76 x0002 /* Some v
59d30 69 65 77 73 20 68 61 76 65 20 64 65 66 69 6e 65 iews have define
59d40 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a d column names *
59d50 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 45 6d 70 /.#define DB_Emp
59d60 74 79 20 20 20 20 20 20 20 20 20 20 20 30 78 30 ty 0x0
59d70 30 30 34 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 004 /* The file
59d80 20 69 73 20 65 6d 70 74 79 20 28 6c 65 6e 67 74 is empty (lengt
59d90 68 20 30 20 62 79 74 65 73 29 20 2a 2f 0a 0a 2f h 0 bytes) */../
59da0 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 *.** The number
59db0 6f 66 20 64 69 66 66 65 72 65 6e 74 20 6b 69 6e of different kin
59dc0 64 73 20 6f 66 20 74 68 69 6e 67 73 20 74 68 61 ds of things tha
59dd0 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74 65 64 t can be limited
59de0 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71 .** using the sq
59df0 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 20 69 6e lite3_limit() in
59e00 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 terface..*/.#def
59e10 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d ine SQLITE_N_LIM
59e20 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 4d 49 54 IT (SQLITE_LIMIT
59e30 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 2b 31 _TRIGGER_DEPTH+1
59e40 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 )../*.** Lookasi
59e50 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 61 20 73 de malloc is a s
59e60 65 74 20 6f 66 20 66 69 78 65 64 2d 73 69 7a 65 et of fixed-size
59e70 20 62 75 66 66 65 72 73 20 74 68 61 74 20 63 61 buffers that ca
59e80 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 n be used.** to
59e90 73 61 74 69 73 66 79 20 73 6d 61 6c 6c 20 74 72 satisfy small tr
59ea0 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 79 20 61 ansient memory a
59eb0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 llocation reques
59ec0 74 73 20 66 6f 72 20 6f 62 6a 65 63 74 73 0a 2a ts for objects.*
59ed0 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 * associated wit
59ee0 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 h a particular d
59ef0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
59f00 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 6f 66 0a on. The use of.
59f10 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c ** lookaside mal
59f20 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 61 20 73 loc provides a s
59f30 69 67 6e 69 66 69 63 61 6e 74 20 70 65 72 66 6f ignificant perfo
59f40 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 65 6d 65 rmance enhanceme
59f50 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78 20 31 30 nt.** (approx 10
59f60 25 29 20 62 79 20 61 76 6f 69 64 69 6e 67 20 6e %) by avoiding n
59f70 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f 63 2f 66 umerous malloc/f
59f80 72 65 65 20 72 65 71 75 65 73 74 73 20 77 68 69 ree requests whi
59f90 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a 20 53 51 le parsing.** SQ
59fa0 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a L statements..**
59fb0 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61 73 69 64 .** The Lookasid
59fc0 65 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 e structure hold
59fd0 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 s configuration
59fe0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
59ff0 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 t the.** lookasi
5a000 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 de malloc subsys
5a010 74 65 6d 2e 20 20 45 61 63 68 20 61 76 61 69 6c tem. Each avail
5a020 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f able memory allo
5a030 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20 74 68 65 cation in.** the
5a040 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75 62 73 79 lookaside subsy
5a050 73 74 65 6d 20 69 73 20 73 74 6f 72 65 64 20 6f stem is stored o
5a060 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 n a linked list
5a070 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 of LookasideSlot
5a080 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a .** objects..**.
5a090 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c ** Lookaside all
5a0a0 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c ocations are onl
5a0b0 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 6f 62 y allowed for ob
5a0c0 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 61 jects that are a
5a0d0 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
5a0e0 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 h a particular d
5a0f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
5a100 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73 63 68 65 on. Hence, sche
5a110 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 ma information c
5a120 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73 74 6f 72 annot.** be stor
5a130 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69 64 65 20 ed in lookaside
5a140 62 65 63 61 75 73 65 20 69 6e 20 73 68 61 72 65 because in share
5a150 64 20 63 61 63 68 65 20 6d 6f 64 65 20 74 68 65 d cache mode the
5a160 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 schema informat
5a170 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61 72 65 64 ion.** is shared
5a180 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 64 61 74 by multiple dat
5a190 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
5a1a0 73 2e 20 20 54 68 65 72 65 66 6f 72 65 2c 20 77 s. Therefore, w
5a1b0 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a 20 hile parsing.**
5a1c0 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 schema informati
5a1d0 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61 73 69 64 on, the Lookasid
5a1e0 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c 61 67 20 e.bEnabled flag
5a1f0 69 73 20 63 6c 65 61 72 65 64 20 73 6f 20 74 68 is cleared so th
5a200 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 at.** lookaside
5a210 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 allocations are
5a220 6e 6f 74 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 not used to cons
5a230 74 72 75 63 74 20 74 68 65 20 73 63 68 65 6d 61 truct the schema
5a240 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 73 74 72 objects..*/.str
5a250 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 7b 0a uct Lookaside {.
5a260 20 20 75 31 36 20 73 7a 3b 20 20 20 20 20 20 20 u16 sz;
5a270 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
5a280 65 20 6f 66 20 65 61 63 68 20 62 75 66 66 65 72 e of each buffer
5a290 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 in bytes */. u
5a2a0 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 8 bEnabled;
5a2b0 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 73 65 20 /* False
5a2c0 74 6f 20 64 69 73 61 62 6c 65 20 6e 65 77 20 6c to disable new l
5a2d0 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 ookaside allocat
5a2e0 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20 62 4d 61 ions */. u8 bMa
5a2f0 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 20 20 20 lloced;
5a300 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 53 74 /* True if pSt
5a310 61 72 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f art obtained fro
5a320 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
5a330 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 () */. int nOut
5a340 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5a350 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 66 /* Number of buf
5a360 66 65 72 73 20 63 75 72 72 65 6e 74 6c 79 20 63 fers currently c
5a370 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 hecked out */.
5a380 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 20 20 20 int mxOut;
5a390 20 20 20 20 20 20 20 20 2f 2a 20 48 69 67 68 77 /* Highw
5a3a0 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 4f ater mark for nO
5a3b0 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 69 64 ut */. Lookasid
5a3c0 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 eSlot *pFree;
5a3d0 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 61 69 6c /* List of avail
5a3e0 61 62 6c 65 20 62 75 66 66 65 72 73 20 2a 2f 0a able buffers */.
5a3f0 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74 3b 20 void *pStart;
5a400 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
5a410 73 74 20 62 79 74 65 20 6f 66 20 61 76 61 69 6c st byte of avail
5a420 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 able memory spac
5a430 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e e */. void *pEn
5a440 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f d; /
5a450 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 * First byte pas
5a460 74 20 65 6e 64 20 6f 66 20 61 76 61 69 6c 61 62 t end of availab
5a470 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 73 le space */.};.s
5a480 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53 truct LookasideS
5a490 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61 73 69 64 lot {. Lookasid
5a4a0 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20 eSlot *pNext;
5a4b0 20 2f 2a 20 4e 65 78 74 20 62 75 66 66 65 72 20 /* Next buffer
5a4c0 69 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 20 66 in the list of f
5a4d0 72 65 65 20 62 75 66 66 65 72 73 20 2a 2f 0a 7d ree buffers */.}
5a4e0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61 73 68 20 ;../*.** A hash
5a4f0 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 table for functi
5a500 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a on definitions..
5a510 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61 63 68 20 **.** Hash each
5a520 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 FuncDef structur
5a530 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68 e into one of th
5a540 65 20 46 75 6e 63 44 65 66 48 61 73 68 2e 61 5b e FuncDefHash.a[
5a550 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43 6f 6c 6c ] slots..** Coll
5a560 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68 isions are on th
5a570 65 20 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20 e FuncDef.pHash
5a580 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 chain..*/.struct
5a590 20 46 75 6e 63 44 65 66 48 61 73 68 20 7b 0a 20 FuncDefHash {.
5a5a0 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 33 5d 3b FuncDef *a[23];
5a5b0 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 /* Hash t
5a5c0 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 6f able for functio
5a5d0 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ns */.};../*.**
5a5e0 45 61 63 68 20 64 61 74 61 62 61 73 65 20 69 73 Each database is
5a5f0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
5a600 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
5a610 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
5a620 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 74 52 6f he sqlite.lastRo
5a630 77 69 64 20 72 65 63 6f 72 64 73 20 74 68 65 20 wid records the
5a640 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69 last insert rowi
5a650 64 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61 d generated by a
5a660 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73 74 61 74 n.** insert stat
5a670 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72 74 73 20 ement. Inserts
5a680 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 20 on views do not
5a690 61 66 66 65 63 74 20 69 74 73 20 76 61 6c 75 65 affect its value
5a6a0 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72 69 67 67 . Each.** trigg
5a6b0 65 72 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63 er has its own c
5a6c0 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68 61 74 20 ontext, so that
5a6d0 6c 61 73 74 52 6f 77 69 64 20 63 61 6e 20 62 65 lastRowid can be
5a6e0 20 75 70 64 61 74 65 64 20 69 6e 73 69 64 65 0a updated inside.
5a6f0 2a 2a 20 74 72 69 67 67 65 72 73 20 61 73 20 75 ** triggers as u
5a700 73 75 61 6c 2e 20 20 54 68 65 20 70 72 65 76 69 sual. The previ
5a710 6f 75 73 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 ous value will b
5a720 65 20 72 65 73 74 6f 72 65 64 20 6f 6e 63 65 20 e restored once
5a730 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 65 the trigger.** e
5a740 78 69 74 73 2e 20 20 55 70 6f 6e 20 65 6e 74 65 xits. Upon ente
5a750 72 69 6e 67 20 61 20 62 65 66 6f 72 65 20 6f 72 ring a before or
5a760 20 69 6e 73 74 65 61 64 20 6f 66 20 74 72 69 67 instead of trig
5a770 67 65 72 2c 20 6c 61 73 74 52 6f 77 69 64 20 69 ger, lastRowid i
5a780 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65 72 20 28 s no.** longer (
5a790 73 69 6e 63 65 20 61 66 74 65 72 20 76 65 72 73 since after vers
5a7a0 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72 65 73 65 ion 2.8.12) rese
5a7b0 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a 20 54 t to -1..**.** T
5a7c0 68 65 20 73 71 6c 69 74 65 2e 6e 43 68 61 6e 67 he sqlite.nChang
5a7d0 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 75 6e 74 e does not count
5a7e0 20 63 68 61 6e 67 65 73 20 77 69 74 68 69 6e 20 changes within
5a7f0 74 72 69 67 67 65 72 73 20 61 6e 64 20 6b 65 65 triggers and kee
5a800 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74 65 78 74 ps no.** context
5a810 2e 20 20 49 74 20 69 73 20 72 65 73 65 74 20 61 . It is reset a
5a820 74 20 73 74 61 72 74 20 6f 66 20 73 71 6c 69 74 t start of sqlit
5a830 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54 68 65 20 e3_exec..** The
5a840 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e 67 65 20 sqlite.lsChange
5a850 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6e represents the n
5a860 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 umber of changes
5a870 20 6d 61 64 65 20 62 79 20 74 68 65 20 6c 61 73 made by the las
5a880 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20 75 70 64 t.** insert, upd
5a890 61 74 65 2c 20 6f 72 20 64 65 6c 65 74 65 20 73 ate, or delete s
5a8a0 74 61 74 65 6d 65 6e 74 2e 20 20 49 74 20 72 65 tatement. It re
5a8b0 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e 74 20 74 mains constant t
5a8c0 68 72 6f 75 67 68 6f 75 74 20 74 68 65 0a 2a 2a hroughout the.**
5a8d0 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73 74 61 length of a sta
5a8e0 74 65 6d 65 6e 74 20 61 6e 64 20 69 73 20 74 68 tement and is th
5a8f0 65 6e 20 75 70 64 61 74 65 64 20 62 79 20 4f 50 en updated by OP
5a900 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20 49 74 20 _SetCounts. It
5a910 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f 6e 74 65 keeps a.** conte
5a920 78 74 20 73 74 61 63 6b 20 6a 75 73 74 20 6c 69 xt stack just li
5a930 6b 65 20 6c 61 73 74 52 6f 77 69 64 20 73 6f 20 ke lastRowid so
5a940 74 68 61 74 20 74 68 65 20 63 6f 75 6e 74 20 6f that the count o
5a950 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 77 69 74 f changes.** wit
5a960 68 69 6e 20 61 20 74 72 69 67 67 65 72 20 69 73 hin a trigger is
5a970 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74 73 69 64 not seen outsid
5a980 65 20 74 68 65 20 74 72 69 67 67 65 72 2e 20 20 e the trigger.
5a990 43 68 61 6e 67 65 73 20 74 6f 20 76 69 65 77 73 Changes to views
5a9a0 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66 66 65 63 do not.** affec
5a9b0 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6c t the value of l
5a9c0 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 20 sChange..** The
5a9d0 73 71 6c 69 74 65 2e 63 73 43 68 61 6e 67 65 20 sqlite.csChange
5a9e0 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74 keeps track of t
5a9f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75 72 he number of cur
5aa00 72 65 6e 74 20 63 68 61 6e 67 65 73 20 28 73 69 rent changes (si
5aa10 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 nce.** the last
5aa20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e 64 20 69 statement) and i
5aa30 73 20 75 73 65 64 20 74 6f 20 75 70 64 61 74 65 s used to update
5aa40 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 6e 67 65 sqlite_lsChange
5aa50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 62 ..**.** The memb
5aa60 65 72 20 76 61 72 69 61 62 6c 65 73 20 73 71 6c er variables sql
5aa70 69 74 65 2e 65 72 72 43 6f 64 65 2c 20 73 71 6c ite.errCode, sql
5aa80 69 74 65 2e 7a 45 72 72 4d 73 67 20 61 6e 64 20 ite.zErrMsg and
5aa90 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 31 36 sqlite.zErrMsg16
5aaa0 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 6d 6f .** store the mo
5aab0 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 st recent error
5aac0 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 61 70 70 code and, if app
5aad0 6c 69 63 61 62 6c 65 2c 20 73 74 72 69 6e 67 2e licable, string.
5aae0 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c The.** internal
5aaf0 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 function sqlite
5ab00 33 45 72 72 6f 72 28 29 20 69 73 20 75 73 65 64 3Error() is used
5ab10 20 74 6f 20 73 65 74 20 74 68 65 73 65 20 76 61 to set these va
5ab20 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f 6e 73 69 riables.** consi
5ab30 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73 74 72 75 stently..*/.stru
5ab40 63 74 20 73 71 6c 69 74 65 33 20 7b 0a 20 20 73 ct sqlite3 {. s
5ab50 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
5ab60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
5ab70 4f 53 20 49 6e 74 65 72 66 61 63 65 20 2a 2f 0a OS Interface */.
5ab80 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 int nDb;
5ab90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aba0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 /* Number of bac
5abb0 6b 65 6e 64 73 20 63 75 72 72 65 6e 74 6c 79 20 kends currently
5abc0 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44 62 20 2a in use */. Db *
5abd0 61 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 aDb;
5abe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c /* All
5abf0 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 69 backends */. i
5ac00 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 nt flags;
5ac10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5ac20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73 20 66 6c Miscellaneous fl
5ac30 61 67 73 2e 20 53 65 65 20 62 65 6c 6f 77 20 2a ags. See below *
5ac40 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 /. int openFlag
5ac50 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
5ac60 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 /* Flags passe
5ac70 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73 d to sqlite3_vfs
5ac80 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e .xOpen() */. in
5ac90 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20 t errCode;
5aca0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
5acb0 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 ost recent error
5acc0 20 63 6f 64 65 20 28 53 51 4c 49 54 45 5f 2a 29 code (SQLITE_*)
5acd0 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 4d 61 73 */. int errMas
5ace0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
5acf0 20 20 20 20 2f 2a 20 26 20 72 65 73 75 6c 74 20 /* & result
5ad00 63 6f 64 65 73 20 77 69 74 68 20 74 68 69 73 20 codes with this
5ad10 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
5ad20 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d */. u8 autoCom
5ad30 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 mit;
5ad40 20 20 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d /* The auto-
5ad50 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a commit flag. */.
5ad60 20 20 75 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b u8 temp_store;
5ad70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ad80 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65 /* 1: file 2: me
5ad90 6d 6f 72 79 20 30 3a 20 64 65 66 61 75 6c 74 20 mory 0: default
5ada0 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f 63 46 61 */. u8 mallocFa
5adb0 69 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 iled;
5adc0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 /* True if we
5add0 20 68 61 76 65 20 73 65 65 6e 20 61 20 6d 61 6c have seen a mal
5ade0 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20 loc failure */.
5adf0 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 u8 dfltLockMode
5ae00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
5ae10 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e * Default lockin
5ae20 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63 g-mode for attac
5ae30 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 75 38 20 hed dbs */. u8
5ae40 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b dfltJournalMode;
5ae50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
5ae60 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f fault journal mo
5ae70 64 65 20 66 6f 72 20 61 74 74 61 63 68 65 64 20 de for attached
5ae80 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 dbs */. signed
5ae90 63 68 61 72 20 6e 65 78 74 41 75 74 6f 76 61 63 char nextAutovac
5aea0 3b 20 20 20 20 20 20 2f 2a 20 41 75 74 6f 76 61 ; /* Autova
5aeb0 63 20 73 65 74 74 69 6e 67 20 61 66 74 65 72 20 c setting after
5aec0 56 41 43 55 55 4d 20 69 66 20 3e 3d 30 20 2a 2f VACUUM if >=0 */
5aed0 0a 20 20 69 6e 74 20 6e 65 78 74 50 61 67 65 73 . int nextPages
5aee0 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
5aef0 20 2f 2a 20 50 61 67 65 73 69 7a 65 20 61 66 74 /* Pagesize aft
5af00 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e 30 20 er VACUUM if >0
5af10 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 3b */. int nTable;
5af20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af30 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5af40 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 tables in the da
5af50 74 61 62 61 73 65 20 2a 2f 0a 20 20 43 6f 6c 6c tabase */. Coll
5af60 53 65 71 20 2a 70 44 66 6c 74 43 6f 6c 6c 3b 20 Seq *pDfltColl;
5af70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
5af80 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 default collati
5af90 6e 67 20 73 65 71 75 65 6e 63 65 20 28 42 49 4e ng sequence (BIN
5afa0 41 52 59 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61 ARY) */. i64 la
5afb0 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 stRowid;
5afc0 20 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49 44 /* ROWID
5afd0 20 6f 66 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 of most recent
5afe0 69 6e 73 65 72 74 20 28 73 65 65 20 61 62 6f 76 insert (see abov
5aff0 65 29 20 2a 2f 0a 20 20 75 33 32 20 6d 61 67 69 e) */. u32 magi
5b000 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
5b010 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e /* Magic n
5b020 75 6d 62 65 72 20 66 6f 72 20 64 65 74 65 63 74 umber for detect
5b030 20 6c 69 62 72 61 72 79 20 6d 69 73 75 73 65 20 library misuse
5b040 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 */. int nChange
5b050 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5b060 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 /* Value retu
5b070 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
5b080 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20 69 changes() */. i
5b090 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b nt nTotalChange;
5b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b0b0 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 Value returned b
5b0c0 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f y sqlite3_total_
5b0d0 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20 73 changes() */. s
5b0e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
5b0f0 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 tex; /*
5b100 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65 78 Connection mutex
5b110 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 6d 69 74 */. int aLimit
5b120 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 5d [SQLITE_N_LIMIT]
5b130 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73 20 2a 2f ; /* Limits */
5b140 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 . struct sqlite
5b150 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20 20 20 3InitInfo {
5b160 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
5b170 75 73 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 used during init
5b180 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 ialization */.
5b190 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 int iDb;
5b1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5b1b0 20 57 68 65 6e 20 62 61 63 6b 20 69 73 20 62 65 When back is be
5b1c0 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 ing initialized
5b1d0 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 77 54 6e */. int newTn
5b1e0 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 um;
5b1f0 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 65 20 6f /* Rootpage o
5b200 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 6e f table being in
5b210 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 itialized */.
5b220 20 75 38 20 62 75 73 79 3b 20 20 20 20 20 20 20 u8 busy;
5b230 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b240 54 52 55 45 20 69 66 20 63 75 72 72 65 6e 74 6c TRUE if currentl
5b250 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 2a y initializing *
5b260 2f 0a 20 20 20 20 75 38 20 6f 72 70 68 61 6e 54 /. u8 orphanT
5b270 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 20 rigger;
5b280 20 20 2f 2a 20 4c 61 73 74 20 73 74 61 74 65 6d /* Last statem
5b290 65 6e 74 20 69 73 20 6f 72 70 68 61 6e 65 64 20 ent is orphaned
5b2a0 54 45 4d 50 20 74 72 69 67 67 65 72 20 2a 2f 0a TEMP trigger */.
5b2b0 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69 6e 74 20 } init;. int
5b2c0 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 nExtension;
5b2d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
5b2e0 62 65 72 20 6f 66 20 6c 6f 61 64 65 64 20 65 78 ber of loaded ex
5b2f0 74 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 76 6f tensions */. vo
5b300 69 64 20 2a 2a 61 45 78 74 65 6e 73 69 6f 6e 3b id **aExtension;
5b310 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5b320 72 72 61 79 20 6f 66 20 73 68 61 72 65 64 20 6c rray of shared l
5b330 69 62 72 61 72 79 20 68 61 6e 64 6c 65 73 20 2a ibrary handles *
5b340 2f 0a 20 20 73 74 72 75 63 74 20 56 64 62 65 20 /. struct Vdbe
5b350 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20 *pVdbe;
5b360 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 /* List of act
5b370 69 76 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 ive virtual mach
5b380 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 61 63 ines */. int ac
5b390 74 69 76 65 56 64 62 65 43 6e 74 3b 20 20 20 20 tiveVdbeCnt;
5b3a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5b3b0 72 20 6f 66 20 56 44 42 45 73 20 63 75 72 72 65 r of VDBEs curre
5b3c0 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 2a ntly executing *
5b3d0 2f 0a 20 20 69 6e 74 20 77 72 69 74 65 56 64 62 /. int writeVdb
5b3e0 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 eCnt;
5b3f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
5b400 63 74 69 76 65 20 56 44 42 45 73 20 74 68 61 74 ctive VDBEs that
5b410 20 61 72 65 20 77 72 69 74 69 6e 67 20 2a 2f 0a are writing */.
5b420 20 20 76 6f 69 64 20 28 2a 78 54 72 61 63 65 29 void (*xTrace)
5b430 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 (void*,const cha
5b440 72 2a 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 r*); /* T
5b450 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f race function */
5b460 0a 20 20 76 6f 69 64 20 2a 70 54 72 61 63 65 41 . void *pTraceA
5b470 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
5b480 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b490 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 Argument to the
5b4a0 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 2a trace function *
5b4b0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 /. void (*xProf
5b4c0 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 ile)(void*,const
5b4d0 20 63 68 61 72 2a 2c 75 36 34 29 3b 20 20 2f 2a char*,u64); /*
5b4e0 20 50 72 6f 66 69 6c 69 6e 67 20 66 75 6e 63 74 Profiling funct
5b4f0 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ion */. void *p
5b500 50 72 6f 66 69 6c 65 41 72 67 3b 20 20 20 20 20 ProfileArg;
5b510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b520 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
5b530 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 o profile functi
5b540 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 on */. void *pC
5b550 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20 20 20 20 ommitArg;
5b560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
5b570 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d 6d 69 74 ument to xCommit
5b580 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20 Callback() */
5b590 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 . int (*xCommit
5b5a0 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 Callback)(void*)
5b5b0 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 ; /* Invoked
5b5c0 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e at every commit.
5b5d0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 52 6f 6c */. void *pRol
5b5e0 6c 62 61 63 6b 41 72 67 3b 20 20 20 20 20 20 20 lbackArg;
5b5f0 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
5b600 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62 61 63 6b ent to xRollback
5b610 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20 Callback() */
5b620 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f 6c 6c 62 . void (*xRollb
5b630 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 ackCallback)(voi
5b640 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 d*); /* Invoked
5b650 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e at every commit.
5b660 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 70 64 */. void *pUpd
5b670 61 74 65 41 72 67 3b 0a 20 20 76 6f 69 64 20 28 ateArg;. void (
5b680 2a 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b *xUpdateCallback
5b690 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e )(void*,int, con
5b6a0 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
5b6b0 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 har*,sqlite_int6
5b6c0 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 4);. void(*xCol
5b6d0 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a 2c 73 lNeeded)(void*,s
5b6e0 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 qlite3*,int eTex
5b6f0 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a tRep,const char*
5b700 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c );. void(*xColl
5b710 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64 2a 2c Needed16)(void*,
5b720 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 sqlite3*,int eTe
5b730 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 xtRep,const void
5b740 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c *);. void *pCol
5b750 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71 lNeededArg;. sq
5b760 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 45 72 lite3_value *pEr
5b770 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d r; /* M
5b780 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 ost recent error
5b790 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 63 68 message */. ch
5b7a0 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 ar *zErrMsg;
5b7b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
5b7c0 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 ost recent error
5b7d0 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d 38 20 message (UTF-8
5b7e0 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20 63 68 encoded) */. ch
5b7f0 61 72 20 2a 7a 45 72 72 4d 73 67 31 36 3b 20 20 ar *zErrMsg16;
5b800 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
5b810 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 ost recent error
5b820 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d 31 36 message (UTF-16
5b830 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20 75 encoded) */. u
5b840 6e 69 6f 6e 20 7b 0a 20 20 20 20 76 6f 6c 61 74 nion {. volat
5b850 69 6c 65 20 69 6e 74 20 69 73 49 6e 74 65 72 72 ile int isInterr
5b860 75 70 74 65 64 3b 20 2f 2a 20 54 72 75 65 20 69 upted; /* True i
5b870 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 f sqlite3_interr
5b880 75 70 74 20 68 61 73 20 62 65 65 6e 20 63 61 6c upt has been cal
5b890 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c led */. doubl
5b8a0 65 20 6e 6f 74 55 73 65 64 31 3b 20 20 20 20 20 e notUsed1;
5b8b0 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 72 /* Spacer
5b8c0 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 20 20 4c 6f */. } u1;. Lo
5b8d0 6f 6b 61 73 69 64 65 20 6c 6f 6f 6b 61 73 69 64 okaside lookasid
5b8e0 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c e; /* L
5b8f0 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 ookaside malloc
5b900 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 2a 2f configuration */
5b910 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
5b920 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 OMIT_AUTHORIZATI
5b930 4f 4e 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68 ON. int (*xAuth
5b940 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 )(void*,int,cons
5b950 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 t char*,const ch
5b960 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
5b970 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 const char*);.
5b980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5b9a0 20 41 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a Access authoriz
5b9b0 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a ation function *
5b9c0 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 74 68 41 /. void *pAuthA
5b9d0 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
5b9e0 20 20 2f 2a 20 31 73 74 20 61 72 67 75 6d 65 6e /* 1st argumen
5b9f0 74 20 74 6f 20 74 68 65 20 61 63 63 65 73 73 20 t to the access
5ba00 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f auth function */
5ba10 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
5ba20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 SQLITE_OMIT_PROG
5ba30 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 RESS_CALLBACK.
5ba40 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73 73 29 int (*xProgress)
5ba50 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 2f 2a (void *); /*
5ba60 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 The progress ca
5ba70 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 llback */. void
5ba80 20 2a 70 50 72 6f 67 72 65 73 73 41 72 67 3b 20 *pProgressArg;
5ba90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
5baa0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 72 6f ument to the pro
5bab0 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a gress callback *
5bac0 2f 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72 65 73 /. int nProgres
5bad0 73 4f 70 73 3b 20 20 20 20 20 20 20 20 20 20 20 sOps;
5bae0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f /* Number of o
5baf0 70 63 6f 64 65 73 20 66 6f 72 20 70 72 6f 67 72 pcodes for progr
5bb00 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a ess callback */.
5bb10 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
5bb20 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
5bb30 41 4c 54 41 42 4c 45 0a 20 20 48 61 73 68 20 61 ALTABLE. Hash a
5bb40 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 20 20 Module;
5bb50 20 20 20 20 20 20 20 20 2f 2a 20 70 6f 70 75 6c /* popul
5bb60 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f ated by sqlite3_
5bb70 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 create_module()
5bb80 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 56 54 61 */. Table *pVTa
5bb90 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
5bba0 20 20 20 2f 2a 20 76 74 61 62 20 77 69 74 68 20 /* vtab with
5bbb0 61 63 74 69 76 65 20 43 6f 6e 6e 65 63 74 2f 43 active Connect/C
5bbc0 72 65 61 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a reate method */.
5bbd0 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 54 72 61 VTable **aVTra
5bbe0 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ns;
5bbf0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 /* Virtual table
5bc00 73 20 77 69 74 68 20 6f 70 65 6e 20 74 72 61 6e s with open tran
5bc10 73 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e sactions */. in
5bc20 74 20 6e 56 54 72 61 6e 73 3b 20 20 20 20 20 20 t nVTrans;
5bc30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5bc40 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 llocated size of
5bc50 20 61 56 54 72 61 6e 73 20 2a 2f 0a 20 20 56 54 aVTrans */. VT
5bc60 61 62 6c 65 20 2a 70 44 69 73 63 6f 6e 6e 65 63 able *pDisconnec
5bc70 74 3b 20 20 20 20 2f 2a 20 44 69 73 63 6f 6e 6e t; /* Disconn
5bc80 65 63 74 20 74 68 65 73 65 20 69 6e 20 6e 65 78 ect these in nex
5bc90 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 t sqlite3_prepar
5bca0 65 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 e() */.#endif.
5bcb0 46 75 6e 63 44 65 66 48 61 73 68 20 61 46 75 6e FuncDefHash aFun
5bcc0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a c; /*
5bcd0 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 63 Hash table of c
5bce0 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69 onnection functi
5bcf0 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20 61 43 ons */. Hash aC
5bd00 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 20 20 ollSeq;
5bd10 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 6f /* All co
5bd20 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
5bd30 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c s */. BusyHandl
5bd40 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72 3b 20 er busyHandler;
5bd50 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63 61 6c /* Busy cal
5bd60 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 62 lback */. int b
5bd70 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 20 usyTimeout;
5bd80 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 73 79 /* Busy
5bd90 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 74 handler timeout
5bda0 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20 44 , in msec */. D
5bdb0 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b 20 b aDbStatic[2];
5bdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5bdd0 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f 72 Static space for
5bde0 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20 62 the 2 default b
5bdf0 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 53 61 76 ackends */. Sav
5be00 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 epoint *pSavepoi
5be10 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 nt; /* Li
5be20 73 74 20 6f 66 20 61 63 74 69 76 65 20 73 61 76 st of active sav
5be30 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 epoints */. int
5be40 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 nSavepoint;
5be50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5be60 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e mber of non-tran
5be70 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e saction savepoin
5be80 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 61 ts */. int nSta
5be90 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 tement;
5bea0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
5beb0 6f 66 20 6e 65 73 74 65 64 20 73 74 61 74 65 6d of nested statem
5bec0 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 ent-transactions
5bed0 20 20 2a 2f 0a 20 20 75 38 20 69 73 54 72 61 6e */. u8 isTran
5bee0 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 sactionSavepoint
5bef0 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 ; /* True if
5bf00 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 the outermost sa
5bf10 76 65 70 6f 69 6e 74 20 69 73 20 61 20 54 53 20 vepoint is a TS
5bf20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
5bf30 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f E_ENABLE_UNLOCK_
5bf40 4e 4f 54 49 46 59 0a 20 20 2f 2a 20 54 68 65 20 NOTIFY. /* The
5bf50 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 following variab
5bf60 6c 65 73 20 61 72 65 20 61 6c 6c 20 70 72 6f 74 les are all prot
5bf70 65 63 74 65 64 20 62 79 20 74 68 65 20 53 54 41 ected by the STA
5bf80 54 49 43 5f 4d 41 53 54 45 52 20 0a 20 20 2a 2a TIC_MASTER . **
5bf90 20 6d 75 74 65 78 2c 20 6e 6f 74 20 62 79 20 73 mutex, not by s
5bfa0 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 20 54 68 qlite3.mutex. Th
5bfb0 65 79 20 61 72 65 20 75 73 65 64 20 62 79 20 63 ey are used by c
5bfc0 6f 64 65 20 69 6e 20 6e 6f 74 69 66 79 2e 63 2e ode in notify.c.
5bfd0 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e . **. ** When
5bfe0 20 58 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 X.pUnlockConnec
5bff0 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 74 20 6d 65 tion==Y, that me
5c000 61 6e 73 20 74 68 61 74 20 58 20 69 73 20 77 61 ans that X is wa
5c010 69 74 69 6e 67 20 66 6f 72 20 59 20 74 6f 0a 20 iting for Y to.
5c020 20 2a 2a 20 75 6e 6c 6f 63 6b 20 73 6f 20 74 68 ** unlock so th
5c030 61 74 20 69 74 20 63 61 6e 20 70 72 6f 63 65 65 at it can procee
5c040 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 d.. **. ** Whe
5c050 6e 20 58 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e n X.pBlockingCon
5c060 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 74 nection==Y, that
5c070 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65 means that some
5c080 74 68 69 6e 67 20 74 68 61 74 20 58 20 74 72 69 thing that X tri
5c090 65 64 0a 20 20 2a 2a 20 74 72 69 65 64 20 74 6f ed. ** tried to
5c0a0 20 64 6f 20 72 65 63 65 6e 74 6c 79 20 66 61 69 do recently fai
5c0b0 6c 65 64 20 77 69 74 68 20 61 6e 20 53 51 4c 49 led with an SQLI
5c0c0 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 20 TE_LOCKED error
5c0d0 64 75 65 20 74 6f 20 6c 6f 63 6b 73 0a 20 20 2a due to locks. *
5c0e0 2a 20 68 65 6c 64 20 62 79 20 59 2e 0a 20 20 2a * held by Y.. *
5c0f0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c /. sqlite3 *pBl
5c100 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e ockingConnection
5c110 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 ; /* Connection
5c120 74 68 61 74 20 63 61 75 73 65 64 20 53 51 4c 49 that caused SQLI
5c130 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f 0a 20 20 73 TE_LOCKED */. s
5c140 71 6c 69 74 65 33 20 2a 70 55 6e 6c 6f 63 6b 43 qlite3 *pUnlockC
5c150 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 onnection;
5c160 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 /* Connecti
5c170 6f 6e 20 74 6f 20 77 61 74 63 68 20 66 6f 72 20 on to watch for
5c180 75 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 unlock */. void
5c190 20 2a 70 55 6e 6c 6f 63 6b 41 72 67 3b 20 20 20 *pUnlockArg;
5c1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c1b0 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
5c1c0 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 2a xUnlockNotify *
5c1d0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 6c 6f /. void (*xUnlo
5c1e0 63 6b 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a ckNotify)(void *
5c1f0 2a 2c 20 69 6e 74 29 3b 20 20 2f 2a 20 55 6e 6c *, int); /* Unl
5c200 6f 63 6b 20 6e 6f 74 69 66 79 20 63 61 6c 6c 62 ock notify callb
5c210 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ack */. sqlite3
5c220 20 2a 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 20 *pNextBlocked;
5c230 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 /* Next i
5c240 6e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 62 6c n list of all bl
5c250 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e ocked connection
5c260 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a s */.#endif.};..
5c270 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 74 6f /*.** A macro to
5c280 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 65 6e discover the en
5c290 63 6f 64 69 6e 67 20 6f 66 20 61 20 64 61 74 61 coding of a data
5c2a0 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 base..*/.#define
5c2b0 20 45 4e 43 28 64 62 29 20 28 28 64 62 29 2d 3e ENC(db) ((db)->
5c2c0 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 2d 3e aDb[0].pSchema->
5c2d0 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 enc)../*.** Poss
5c2e0 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 ible values for
5c2f0 74 68 65 20 73 71 6c 69 74 65 2e 66 6c 61 67 73 the sqlite.flags
5c300 20 61 6e 64 20 6f 72 20 44 62 2e 66 6c 61 67 73 and or Db.flags
5c310 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f fields..**.** O
5c320 6e 20 73 71 6c 69 74 65 2e 66 6c 61 67 73 2c 20 n sqlite.flags,
5c330 74 68 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 the SQLITE_InTra
5c340 6e 73 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74 ns value means t
5c350 68 61 74 20 77 65 20 68 61 76 65 0a 2a 2a 20 65 hat we have.** e
5c360 78 65 63 75 74 65 64 20 61 20 42 45 47 49 4e 2e xecuted a BEGIN.
5c370 20 20 4f 6e 20 44 62 2e 66 6c 61 67 73 2c 20 53 On Db.flags, S
5c380 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 6d 65 QLITE_InTrans me
5c390 61 6e 73 20 61 20 73 74 61 74 65 6d 65 6e 74 0a ans a statement.
5c3a0 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ** transaction i
5c3b0 73 20 61 63 74 69 76 65 20 6f 6e 20 74 68 61 74 s active on that
5c3c0 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 particular data
5c3d0 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64 base file..*/.#d
5c3e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 efine SQLITE_Vdb
5c3f0 65 54 72 61 63 65 20 20 20 20 20 20 30 78 30 30 eTrace 0x00
5c400 30 30 30 30 30 31 20 20 2f 2a 20 54 72 75 65 20 000001 /* True
5c410 74 6f 20 74 72 61 63 65 20 56 44 42 45 20 65 78 to trace VDBE ex
5c420 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 ecution */.#defi
5c430 6e 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e ne SQLITE_InTran
5c440 73 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 s 0x00000
5c450 30 30 38 20 20 2f 2a 20 54 72 75 65 20 69 66 20 008 /* True if
5c460 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e in a transaction
5c470 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5c480 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 TE_InternChanges
5c490 20 20 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 0x00000010 /*
5c4a0 20 55 6e 63 6f 6d 6d 69 74 74 65 64 20 48 61 73 Uncommitted Has
5c4b0 68 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20 h table changes
5c4c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5c4d0 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20 E_FullColNames
5c4e0 20 30 78 30 30 30 30 30 30 32 30 20 20 2f 2a 20 0x00000020 /*
5c4f0 53 68 6f 77 20 66 75 6c 6c 20 63 6f 6c 75 6d 6e Show full column
5c500 20 6e 61 6d 65 73 20 6f 6e 20 53 45 4c 45 43 54 names on SELECT
5c510 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5c520 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 TE_ShortColNames
5c530 20 20 30 78 30 30 30 30 30 30 34 30 20 20 2f 2a 0x00000040 /*
5c540 20 53 68 6f 77 20 73 68 6f 72 74 20 63 6f 6c 75 Show short colu
5c550 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 mns names */.#de
5c560 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e fine SQLITE_Coun
5c570 74 52 6f 77 73 20 20 20 20 20 20 30 78 30 30 30 tRows 0x000
5c580 30 30 30 38 30 20 20 2f 2a 20 43 6f 75 6e 74 20 00080 /* Count
5c590 72 6f 77 73 20 63 68 61 6e 67 65 64 20 62 79 20 rows changed by
5c5a0 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 20 20 20 20 INSERT, */.
5c5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c5d0 20 20 20 20 20 2f 2a 20 20 20 44 45 4c 45 54 45 /* DELETE
5c5e0 2c 20 6f 72 20 55 50 44 41 54 45 20 61 6e 64 20 , or UPDATE and
5c5f0 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 20 20 return */.
5c600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c620 20 20 20 20 2f 2a 20 20 20 74 68 65 20 63 6f 75 /* the cou
5c630 6e 74 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 62 nt using a callb
5c640 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ack. */.#define
5c650 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 SQLITE_NullCallb
5c660 61 63 6b 20 20 20 30 78 30 30 30 30 30 31 30 30 ack 0x00000100
5c670 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 /* Invoke the
5c680 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 69 66 callback once if
5c690 20 74 68 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 the */.
5c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c6c0 20 20 2f 2a 20 20 20 72 65 73 75 6c 74 20 73 65 /* result se
5c6d0 74 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64 t is empty */.#d
5c6e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 71 6c efine SQLITE_Sql
5c6f0 54 72 61 63 65 20 20 20 20 20 20 20 30 78 30 30 Trace 0x00
5c700 30 30 30 32 30 30 20 20 2f 2a 20 44 65 62 75 67 000200 /* Debug
5c710 20 70 72 69 6e 74 20 53 51 4c 20 61 73 20 69 74 print SQL as it
5c720 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 23 64 65 executes */.#de
5c730 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 65 fine SQLITE_Vdbe
5c740 4c 69 73 74 69 6e 67 20 20 20 20 30 78 30 30 30 Listing 0x000
5c750 30 30 34 30 30 20 20 2f 2a 20 44 65 62 75 67 20 00400 /* Debug
5c760 6c 69 73 74 69 6e 67 73 20 6f 66 20 56 44 42 45 listings of VDBE
5c770 20 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 23 64 65 programs */.#de
5c780 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 72 69 74 fine SQLITE_Writ
5c790 65 53 63 68 65 6d 61 20 20 20 20 30 78 30 30 30 eSchema 0x000
5c7a0 30 30 38 30 30 20 20 2f 2a 20 4f 4b 20 74 6f 20 00800 /* OK to
5c7b0 75 70 64 61 74 65 20 53 51 4c 49 54 45 5f 4d 41 update SQLITE_MA
5c7c0 53 54 45 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20 STER */.#define
5c7d0 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 SQLITE_NoReadloc
5c7e0 6b 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30 k 0x00001000
5c7f0 20 20 2f 2a 20 52 65 61 64 6c 6f 63 6b 73 20 61 /* Readlocks a
5c800 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65 6e 20 re omitted when
5c810 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
5c820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c830 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 63 ** ac
5c840 63 65 73 73 69 6e 67 20 72 65 61 64 2d 6f 6e 6c cessing read-onl
5c850 79 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 23 y databases */.#
5c860 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 67 define SQLITE_Ig
5c870 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30 78 30 noreChecks 0x0
5c880 30 30 30 32 30 30 30 20 20 2f 2a 20 44 6f 20 6e 0002000 /* Do n
5c890 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65 63 6b ot enforce check
5c8a0 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a constraints */.
5c8b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 #define SQLITE_R
5c8c0 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 30 eadUncommitted 0
5c8d0 78 30 30 30 30 34 30 30 30 20 2f 2a 20 46 6f 72 x00004000 /* For
5c8e0 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f shared-cache mo
5c8f0 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 de */.#define SQ
5c900 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 LITE_LegacyFileF
5c910 6d 74 20 20 30 78 30 30 30 30 38 30 30 30 20 20 mt 0x00008000
5c920 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 64 61 /* Create new da
5c930 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72 6d 61 tabases in forma
5c940 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 t 1 */.#define S
5c950 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 QLITE_FullFSync
5c960 20 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20 0x00010000
5c970 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 73 79 /* Use full fsy
5c980 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b 65 6e nc on the backen
5c990 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
5c9a0 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f ITE_LoadExtensio
5c9b0 6e 20 20 30 78 30 30 30 32 30 30 30 30 20 20 2f n 0x00020000 /
5c9c0 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f 65 78 * Enable load_ex
5c9d0 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a 23 64 65 66 tension */..#def
5c9e0 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 ine SQLITE_Recov
5c9f0 65 72 79 4d 6f 64 65 20 20 20 30 78 30 30 30 34 eryMode 0x0004
5ca00 30 30 30 30 20 20 2f 2a 20 49 67 6e 6f 72 65 20 0000 /* Ignore
5ca10 73 63 68 65 6d 61 20 65 72 72 6f 72 73 20 2a 2f schema errors */
5ca20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5ca30 52 65 76 65 72 73 65 4f 72 64 65 72 20 20 20 30 ReverseOrder 0
5ca40 78 30 30 31 30 30 30 30 30 20 20 2f 2a 20 52 65 x00100000 /* Re
5ca50 76 65 72 73 65 20 75 6e 6f 72 64 65 72 65 64 20 verse unordered
5ca60 53 45 4c 45 43 54 73 20 2a 2f 0a 23 64 65 66 69 SELECTs */.#defi
5ca70 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69 ne SQLITE_RecTri
5ca80 67 67 65 72 73 20 20 20 20 30 78 30 30 32 30 30 ggers 0x00200
5ca90 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 72 000 /* Enable r
5caa0 65 63 75 72 73 69 76 65 20 74 72 69 67 67 65 72 ecursive trigger
5cab0 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 s */../*.** Poss
5cac0 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 ible values for
5cad0 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 the sqlite.magic
5cae0 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65 20 6e field..** The n
5caf0 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69 umbers are obtai
5cb00 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e ned at random an
5cb10 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63 69 61 d have no specia
5cb20 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68 65 72 l meaning, other
5cb30 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67 20 64 .** than being d
5cb40 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f 6e 65 istinct from one
5cb50 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 another..*/.#de
5cb60 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 fine SQLITE_MAGI
5cb70 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61 30 32 C_OPEN 0xa02
5cb80 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61 62 61 9a697 /* Databa
5cb90 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a 23 64 se is open */.#d
5cba0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 efine SQLITE_MAG
5cbb0 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78 39 66 IC_CLOSED 0x9f
5cbc0 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74 61 62 3c2d33 /* Datab
5cbd0 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 2a 2f ase is closed */
5cbe0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5cbf0 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20 20 30 MAGIC_SICK 0
5cc00 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20 45 72 x4b771290 /* Er
5cc10 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69 6e 67 ror and awaiting
5cc20 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 69 6e close */.#defin
5cc30 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 e SQLITE_MAGIC_B
5cc40 55 53 59 20 20 20 20 20 30 78 66 30 33 62 37 39 USY 0xf03b79
5cc50 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 06 /* Database
5cc60 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 currently in use
5cc70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5cc80 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 20 20 TE_MAGIC_ERROR
5cc90 20 20 30 78 62 35 33 35 37 39 33 30 20 20 2f 2a 0xb5357930 /*
5cca0 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 An SQLITE_MISUS
5ccb0 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 E error occurred
5ccc0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 */../*.** Each
5ccd0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 SQL function is
5cce0 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e defined by an in
5ccf0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
5cd00 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 llowing.** struc
5cd10 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74 65 72 ture. A pointer
5cd20 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 to this structu
5cd30 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 re is stored in
5cd40 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75 6e 63 the sqlite.aFunc
5cd50 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20 .** hash table.
5cd60 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 66 When multiple f
5cd70 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 74 68 unctions have th
5cd80 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 68 65 e same name, the
5cd90 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 70 hash table.** p
5cda0 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e 6b 65 oints to a linke
5cdb0 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20 d list of these
5cdc0 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 structures..*/.s
5cdd0 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 7b 0a truct FuncDef {.
5cde0 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20 20 20 i16 nArg;
5cdf0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5ce00 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 of arguments.
5ce10 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d 69 74 -1 means unlimit
5ce20 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72 65 66 ed */. u8 iPref
5ce30 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Enc; /*
5ce40 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 Preferred text e
5ce50 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54 45 5f ncoding (SQLITE_
5ce60 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36 42 45 UTF8, 16LE, 16BE
5ce70 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 3b ) */. u8 flags;
5ce80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
5ce90 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 ome combination
5cea0 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 2a of SQLITE_FUNC_*
5ceb0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 */. void *pUse
5cec0 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 55 73 rData; /* Us
5ced0 65 72 20 64 61 74 61 20 70 61 72 61 6d 65 74 65 er data paramete
5cee0 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a r */. FuncDef *
5cef0 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e pNext; /* N
5cf00 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 ext function wit
5cf10 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 h same name */.
5cf20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 void (*xFunc)(s
5cf30 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
5cf40 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
5cf50 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c 61 72 e**); /* Regular
5cf60 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
5cf70 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c oid (*xStep)(sql
5cf80 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
5cf90 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
5cfa0 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61 74 65 *); /* Aggregate
5cfb0 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69 64 20 step */. void
5cfc0 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 71 6c (*xFinalize)(sql
5cfd0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 20 ite3_context*);
5cfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5cff0 2a 20 41 67 67 72 65 67 61 74 65 20 66 69 6e 61 * Aggregate fina
5d000 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 lizer */. char
5d010 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
5d020 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f 66 20 74 /* SQL name of t
5d030 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a he function. */.
5d040 20 20 46 75 6e 63 44 65 66 20 2a 70 48 61 73 68 FuncDef *pHash
5d050 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 77 ; /* Next w
5d060 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 ith a different
5d070 6e 61 6d 65 20 62 75 74 20 74 68 65 20 73 61 6d name but the sam
5d080 65 20 68 61 73 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a e hash */.};../*
5d090 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c .** Possible val
5d0a0 75 65 73 20 66 6f 72 20 46 75 6e 63 44 65 66 2e ues for FuncDef.
5d0b0 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 flags.*/.#define
5d0c0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b SQLITE_FUNC_LIK
5d0d0 45 20 20 20 20 20 30 78 30 31 20 2f 2a 20 43 61 E 0x01 /* Ca
5d0e0 6e 64 69 64 61 74 65 20 66 6f 72 20 74 68 65 20 ndidate for the
5d0f0 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f LIKE optimizatio
5d100 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
5d110 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 20 20 20 ITE_FUNC_CASE
5d120 20 20 30 78 30 32 20 2f 2a 20 43 61 73 65 2d 73 0x02 /* Case-s
5d130 65 6e 73 69 74 69 76 65 20 4c 49 4b 45 2d 74 79 ensitive LIKE-ty
5d140 70 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 pe function */.#
5d150 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
5d160 4e 43 5f 45 50 48 45 4d 20 20 20 20 30 78 30 34 NC_EPHEM 0x04
5d170 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c 2e 20 20 /* Ephemeral.
5d180 44 65 6c 65 74 65 20 77 69 74 68 20 56 44 42 45 Delete with VDBE
5d190 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5d1a0 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c TE_FUNC_NEEDCOLL
5d1b0 20 30 78 30 38 20 2f 2a 20 73 71 6c 69 74 65 33 0x08 /* sqlite3
5d1c0 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 GetFuncCollSeq()
5d1d0 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64 might be called
5d1e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5d1f0 54 45 5f 46 55 4e 43 5f 50 52 49 56 41 54 45 20 TE_FUNC_PRIVATE
5d200 20 30 78 31 30 20 2f 2a 20 41 6c 6c 6f 77 65 64 0x10 /* Allowed
5d210 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 for internal us
5d220 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e e only */.#defin
5d230 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f e SQLITE_FUNC_CO
5d240 55 4e 54 20 20 20 20 30 78 32 30 20 2f 2a 20 42 UNT 0x20 /* B
5d250 75 69 6c 74 2d 69 6e 20 63 6f 75 6e 74 28 2a 29 uilt-in count(*)
5d260 20 61 67 67 72 65 67 61 74 65 20 2a 2f 0a 0a 2f aggregate */../
5d270 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
5d280 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f 73 2c ng three macros,
5d290 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c 49 4b FUNCTION(), LIK
5d2a0 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 47 52 EFUNC() and AGGR
5d2b0 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a 20 75 EGATE() are.** u
5d2c0 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 sed to create th
5d2d0 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 20 66 e initializers f
5d2e0 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 20 73 or the FuncDef s
5d2f0 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a tructures..**.**
5d300 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d FUNCTION(zNam
5d310 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 e, nArg, iArg, b
5d320 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 20 20 NC, xFunc).**
5d330 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 Used to create
5d340 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 a scalar functi
5d350 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 on definition of
5d360 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d a function zNam
5d370 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c 65 6d e .** implem
5d380 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e 63 74 ented by C funct
5d390 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 20 61 ion xFunc that a
5d3a0 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 ccepts nArg argu
5d3b0 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 20 20 ments. The.**
5d3c0 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 value passed a
5d3d0 73 20 69 41 72 67 20 69 73 20 63 61 73 74 20 74 s iArg is cast t
5d3e0 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e 64 20 o a (void*) and
5d3f0 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a made available.*
5d400 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 73 65 * as the use
5d410 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f r-data (sqlite3_
5d420 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 6f 72 user_data()) for
5d430 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 49 the function. I
5d440 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 6d 65 f .** argume
5d450 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 2c 20 nt bNC is true,
5d460 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f then the SQLITE_
5d470 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 66 6c FUNC_NEEDCOLL fl
5d480 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a ag is set..**.**
5d490 20 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 AGGREGATE(zNa
5d4a0 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 me, nArg, iArg,
5d4b0 62 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e bNC, xStep, xFin
5d4c0 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 al).** Used
5d4d0 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67 to create an agg
5d4e0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 regate function
5d4f0 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65 definition imple
5d500 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 20 20 mented by.**
5d510 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
5d520 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 xStep and xFina
5d530 6c 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f 75 l. The first fou
5d540 72 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 r parameters.**
5d550 20 20 20 20 61 72 65 20 69 6e 74 65 72 70 72 65 are interpre
5d560 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ted in the same
5d570 77 61 79 20 61 73 20 74 68 65 20 66 69 72 73 74 way as the first
5d580 20 34 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 4 parameters to
5d590 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 4f 4e .** FUNCTION
5d5a0 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45 ()..**.** LIKE
5d5b0 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 FUNC(zName, nArg
5d5c0 2c 20 70 41 72 67 2c 20 66 6c 61 67 73 29 0a 2a , pArg, flags).*
5d5d0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 * Used to cr
5d5e0 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 eate a scalar fu
5d5f0 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f nction definitio
5d600 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 n of a function
5d610 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 74 68 zName .** th
5d620 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 at accepts nArg
5d630 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 69 73 arguments and is
5d640 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 implemented by
5d650 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20 a call to C .**
5d660 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b function lik
5d670 65 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e 74 20 eFunc. Argument
5d680 70 41 72 67 20 69 73 20 63 61 73 74 20 74 6f 20 pArg is cast to
5d690 61 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 20 6d a (void *) and m
5d6a0 61 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 69 6c ade.** avail
5d6b0 61 62 6c 65 20 61 73 20 74 68 65 20 66 75 6e 63 able as the func
5d6c0 74 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 20 28 tion user-data (
5d6d0 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
5d6e0 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 20 20 a()). The.**
5d6f0 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 20 76 FuncDef.flags v
5d700 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 ariable is set t
5d710 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 o the value pass
5d720 65 64 20 61 73 20 74 68 65 20 66 6c 61 67 73 0a ed as the flags.
5d730 2a 2a 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 ** parameter
5d740 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e ..*/.#define FUN
5d750 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 CTION(zName, nAr
5d760 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 g, iArg, bNC, xF
5d770 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 unc) \. {nArg,
5d780 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 SQLITE_UTF8, bNC
5d790 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 *SQLITE_FUNC_NEE
5d7a0 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 DCOLL, \. SQLI
5d7b0 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 69 41 TE_INT_TO_PTR(iA
5d7c0 72 67 29 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30 rg), 0, xFunc, 0
5d7d0 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a , 0, #zName, 0}.
5d7e0 23 64 65 66 69 6e 65 20 53 54 52 5f 46 55 4e 43 #define STR_FUNC
5d7f0 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 TION(zName, nArg
5d800 2c 20 70 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 , pArg, bNC, xFu
5d810 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 nc) \. {nArg, S
5d820 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a QLITE_UTF8, bNC*
5d830 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 SQLITE_FUNC_NEED
5d840 43 4f 4c 4c 2c 20 5c 0a 20 20 20 70 41 72 67 2c COLL, \. pArg,
5d850 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 0, xFunc, 0, 0,
5d860 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 #zName, 0}.#def
5d870 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 ine LIKEFUNC(zNa
5d880 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 66 me, nArg, arg, f
5d890 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 67 2c lags) \. {nArg,
5d8a0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 66 6c SQLITE_UTF8, fl
5d8b0 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 72 67 ags, (void *)arg
5d8c0 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 , 0, likeFunc, 0
5d8d0 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a , 0, #zName, 0}.
5d8e0 23 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 #define AGGREGAT
5d8f0 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 E(zName, nArg, a
5d900 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 rg, nc, xStep, x
5d910 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 Final) \. {nArg
5d920 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e , SQLITE_UTF8, n
5d930 63 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 c*SQLITE_FUNC_NE
5d940 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 51 4c EDCOLL, \. SQL
5d950 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 61 ITE_INT_TO_PTR(a
5d960 72 67 29 2c 20 30 2c 20 30 2c 20 78 53 74 65 70 rg), 0, 0, xStep
5d970 2c 78 46 69 6e 61 6c 2c 23 7a 4e 61 6d 65 2c 30 ,xFinal,#zName,0
5d980 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 75 72 }../*.** All cur
5d990 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20 rent savepoints
5d9a0 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 are stored in a
5d9b0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 61 72 linked list star
5d9c0 74 69 6e 67 20 61 74 0a 2a 2a 20 73 71 6c 69 74 ting at.** sqlit
5d9d0 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 20 54 e3.pSavepoint. T
5d9e0 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 he first element
5d9f0 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 in the list is
5da00 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
5da10 79 0a 2a 2a 20 6f 70 65 6e 65 64 20 73 61 76 65 y.** opened save
5da20 70 6f 69 6e 74 2e 20 53 61 76 65 70 6f 69 6e 74 point. Savepoint
5da30 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74 s are added to t
5da40 68 65 20 6c 69 73 74 20 62 79 20 74 68 65 20 76 he list by the v
5da50 64 62 65 0a 2a 2a 20 4f 50 5f 53 61 76 65 70 6f dbe.** OP_Savepo
5da60 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e int instruction.
5da70 0a 2a 2f 0a 73 74 72 75 63 74 20 53 61 76 65 70 .*/.struct Savep
5da80 6f 69 6e 74 20 7b 0a 20 20 63 68 61 72 20 2a 7a oint {. char *z
5da90 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 Name;
5daa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5dab0 53 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 20 28 Savepoint name (
5dac0 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 29 20 nul-terminated)
5dad0 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a */. Savepoint *
5dae0 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 pNext;
5daf0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 /* Pare
5db00 6e 74 20 73 61 76 65 70 6f 69 6e 74 20 28 69 66 nt savepoint (if
5db10 20 61 6e 79 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a any) */.};../*.
5db20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
5db30 20 61 72 65 20 75 73 65 64 20 61 73 20 74 68 65 are used as the
5db40 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
5db50 72 20 74 6f 20 73 71 6c 69 74 65 33 53 61 76 65 r to sqlite3Save
5db60 70 6f 69 6e 74 28 29 2c 0a 2a 2a 20 61 6e 64 20 point(),.** and
5db70 61 73 20 74 68 65 20 50 31 20 61 72 67 75 6d 65 as the P1 argume
5db80 6e 74 20 74 6f 20 74 68 65 20 4f 50 5f 53 61 76 nt to the OP_Sav
5db90 65 70 6f 69 6e 74 20 69 6e 73 74 72 75 63 74 69 epoint instructi
5dba0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 on..*/.#define S
5dbb0 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 20 AVEPOINT_BEGIN
5dbc0 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 41 0.#define SA
5dbd0 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 VEPOINT_RELEASE
5dbe0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 41 56 1.#define SAV
5dbf0 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
5dc00 20 20 32 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 2.../*.** Each
5dc10 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 28 SQLite module (
5dc20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 64 65 virtual table de
5dc30 66 69 6e 69 74 69 6f 6e 29 20 69 73 20 64 65 66 finition) is def
5dc40 69 6e 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e ined by an.** in
5dc50 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
5dc60 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
5dc70 65 2c 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 e, stored in the
5dc80 20 73 71 6c 69 74 65 33 2e 61 4d 6f 64 75 6c 65 sqlite3.aModule
5dc90 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 0a .** hash table..
5dca0 2a 2f 0a 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 */.struct Module
5dcb0 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 {. const sqlit
5dcc0 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
5dcd0 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 6c le; /* Cal
5dce0 6c 62 61 63 6b 20 70 6f 69 6e 74 65 72 73 20 2a lback pointers *
5dcf0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
5dd00 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
5dd10 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
5dd20 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74 passed to creat
5dd30 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 e_module() */.
5dd40 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 void *pAux;
5dd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5dd60 20 20 20 20 20 2f 2a 20 70 41 75 78 20 70 61 73 /* pAux pas
5dd70 73 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f sed to create_mo
5dd80 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 dule() */. void
5dd90 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 (*xDestroy)(voi
5dda0 64 20 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 d *);
5ddb0 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 /* Module destr
5ddc0 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a uctor function *
5ddd0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 6e 66 6f /.};../*.** info
5dde0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 rmation about ea
5ddf0 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e 20 ch column of an
5de00 53 51 4c 20 74 61 62 6c 65 20 69 73 20 68 65 6c SQL table is hel
5de10 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 d in an instance
5de20 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 .** of this stru
5de30 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 cture..*/.struct
5de40 20 43 6f 6c 75 6d 6e 20 7b 0a 20 20 63 68 61 72 Column {. char
5de50 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20 *zName; /*
5de60 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 63 6f 6c Name of this col
5de70 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 umn */. Expr *p
5de80 44 66 6c 74 3b 20 20 20 20 20 2f 2a 20 44 65 66 Dflt; /* Def
5de90 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68 ault value of th
5dea0 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 is column */. c
5deb0 68 61 72 20 2a 7a 44 66 6c 74 3b 20 20 20 20 20 har *zDflt;
5dec0 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 74 65 78 74 /* Original text
5ded0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 of the default
5dee0 76 61 6c 75 65 20 2a 2f 0a 20 20 63 68 61 72 20 value */. char
5def0 2a 7a 54 79 70 65 3b 20 20 20 20 20 2f 2a 20 44 *zType; /* D
5df00 61 74 61 20 74 79 70 65 20 66 6f 72 20 74 68 69 ata type for thi
5df10 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68 s column */. ch
5df20 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f ar *zColl; /
5df30 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 * Collating sequ
5df40 65 6e 63 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 ence. If NULL,
5df50 75 73 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 use the default
5df60 2a 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b */. u8 notNull;
5df70 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
5df80 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20 there is a NOT
5df90 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 NULL constraint
5dfa0 2a 2f 0a 20 20 75 38 20 69 73 50 72 69 6d 4b 65 */. u8 isPrimKe
5dfb0 79 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 y; /* True if
5dfc0 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20 this column is
5dfd0 70 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 4d part of the PRIM
5dfe0 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 63 68 61 ARY KEY */. cha
5dff0 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a r affinity; /*
5e000 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 One of the SQLI
5e010 54 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65 TE_AFF_... value
5e020 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c s */.#ifndef SQL
5e030 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
5e040 54 41 42 4c 45 0a 20 20 75 38 20 69 73 48 69 64 TABLE. u8 isHid
5e050 64 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 den; /* True
5e060 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 if this column
5e070 69 73 20 27 68 69 64 64 65 6e 27 20 2a 2f 0a 23 is 'hidden' */.#
5e080 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
5e090 41 20 22 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 A "Collating Seq
5e0a0 75 65 6e 63 65 22 20 69 73 20 64 65 66 69 6e 65 uence" is define
5e0b0 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 d by an instance
5e0c0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
5e0d0 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 g.** structure.
5e0e0 43 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20 61 20 Conceptually, a
5e0f0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
5e100 63 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 ce consists of a
5e110 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 61 20 63 name and.** a c
5e120 6f 6d 70 61 72 69 73 6f 6e 20 72 6f 75 74 69 6e omparison routin
5e130 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 e that defines t
5e140 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 61 74 he order of that
5e150 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a sequence..**.**
5e160 20 54 68 65 72 65 20 6d 61 79 20 74 77 6f 20 73 There may two s
5e170 65 70 61 72 61 74 65 20 69 6d 70 6c 65 6d 65 6e eparate implemen
5e180 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 63 tations of the c
5e190 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f ollation functio
5e1a0 6e 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 70 n, one.** that p
5e1b0 72 6f 63 65 73 73 65 73 20 74 65 78 74 20 69 6e rocesses text in
5e1c0 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 UTF-8 encoding
5e1d0 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 29 20 61 (CollSeq.xCmp) a
5e1e0 6e 64 20 61 6e 6f 74 68 65 72 20 74 68 61 74 0a nd another that.
5e1f0 2a 2a 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 ** processes tex
5e200 74 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 t encoded in UTF
5e210 2d 31 36 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d -16 (CollSeq.xCm
5e220 70 31 36 29 2c 20 75 73 69 6e 67 20 74 68 65 20 p16), using the
5e230 6d 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 74 69 76 machine.** nativ
5e240 65 20 62 79 74 65 20 6f 72 64 65 72 2e 20 57 68 e byte order. Wh
5e250 65 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 en a collation s
5e260 65 71 75 65 6e 63 65 20 69 73 20 69 6e 76 6f 6b equence is invok
5e270 65 64 2c 20 53 51 4c 69 74 65 20 73 65 6c 65 63 ed, SQLite selec
5e280 74 73 0a 2a 2a 20 74 68 65 20 76 65 72 73 69 6f ts.** the versio
5e290 6e 20 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 n that will requ
5e2a0 69 72 65 20 74 68 65 20 6c 65 61 73 74 20 65 78 ire the least ex
5e2b0 70 65 6e 73 69 76 65 20 65 6e 63 6f 64 69 6e 67 pensive encoding
5e2c0 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73 .** translations
5e2d0 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 , if any..**.**
5e2e0 54 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 The CollSeq.pUse
5e2f0 72 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c r member variabl
5e300 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 70 61 e is an extra pa
5e310 72 61 6d 65 74 65 72 20 74 68 61 74 20 70 61 73 rameter that pas
5e320 73 65 64 20 69 6e 0a 2a 2a 20 61 73 20 74 68 65 sed in.** as the
5e330 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
5e340 74 6f 20 74 68 65 20 55 54 46 2d 38 20 63 6f 6d to the UTF-8 com
5e350 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e parison function
5e360 2c 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53 , xCmp..** CollS
5e370 65 71 2e 70 55 73 65 72 31 36 20 69 73 20 74 68 eq.pUser16 is th
5e380 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72 e equivalent for
5e390 20 74 68 65 20 55 54 46 2d 31 36 20 63 6f 6d 70 the UTF-16 comp
5e3a0 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c arison function,
5e3b0 0a 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a .** xCmp16..**.*
5e3c0 2a 20 49 66 20 62 6f 74 68 20 43 6f 6c 6c 53 65 * If both CollSe
5e3d0 71 2e 78 43 6d 70 20 61 6e 64 20 43 6f 6c 6c 53 q.xCmp and CollS
5e3e0 65 71 2e 78 43 6d 70 31 36 20 61 72 65 20 4e 55 eq.xCmp16 are NU
5e3f0 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 LL, it means tha
5e400 74 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 t the.** collati
5e410 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 75 ng sequence is u
5e420 6e 64 65 66 69 6e 65 64 2e 20 20 49 6e 64 69 63 ndefined. Indic
5e430 65 73 20 62 75 69 6c 74 20 6f 6e 20 61 6e 20 75 es built on an u
5e440 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c ndefined.** coll
5e450 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6d ating sequence m
5e460 61 79 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f ay not be read o
5e470 72 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 r written..*/.st
5e480 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 7b 0a 20 ruct CollSeq {.
5e490 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
5e4a0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
5e4b0 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 f the collating
5e4c0 73 65 71 75 65 6e 63 65 2c 20 55 54 46 2d 38 20 sequence, UTF-8
5e4d0 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20 encoded */. u8
5e4e0 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 enc;
5e4f0 20 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 /* Text encod
5e500 69 6e 67 20 68 61 6e 64 6c 65 64 20 62 79 20 78 ing handled by x
5e510 43 6d 70 28 29 20 2a 2f 0a 20 20 75 38 20 74 79 Cmp() */. u8 ty
5e520 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pe;
5e530 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 /* One of the S
5e540 51 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76 QLITE_COLL_... v
5e550 61 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 alues below */.
5e560 20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20 void *pUser;
5e570 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
5e580 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6d 70 argument to xCmp
5e590 28 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 () */. int (*xC
5e5a0 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 mp)(void*,int, c
5e5b0 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c onst void*, int,
5e5c0 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 const void*);.
5e5d0 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
5e5e0 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 id*); /* Destru
5e5f0 63 74 6f 72 20 66 6f 72 20 70 55 73 65 72 20 2a ctor for pUser *
5e600 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f /.};../*.** Allo
5e610 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 43 6f wed values of Co
5e620 6c 6c 53 65 71 2e 74 79 70 65 3a 0a 2a 2f 0a 23 llSeq.type:.*/.#
5e630 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f define SQLITE_CO
5e640 4c 4c 5f 42 49 4e 41 52 59 20 20 31 20 20 2f 2a LL_BINARY 1 /*
5e650 20 54 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d The default mem
5e660 63 6d 70 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20 cmp() collating
5e670 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 sequence */.#def
5e680 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f ine SQLITE_COLL_
5e690 4e 4f 43 41 53 45 20 20 32 20 20 2f 2a 20 54 68 NOCASE 2 /* Th
5e6a0 65 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53 e built-in NOCAS
5e6b0 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 E collating sequ
5e6c0 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ence */.#define
5e6d0 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45 SQLITE_COLL_REVE
5e6e0 52 53 45 20 33 20 20 2f 2a 20 54 68 65 20 62 75 RSE 3 /* The bu
5e6f0 69 6c 74 2d 69 6e 20 52 45 56 45 52 53 45 20 63 ilt-in REVERSE c
5e700 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
5e710 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c e */.#define SQL
5e720 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20 ITE_COLL_USER
5e730 20 30 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 0 /* Any other
5e740 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f user-defined co
5e750 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
5e760 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72 */../*.** A sor
5e770 74 20 6f 72 64 65 72 20 63 61 6e 20 62 65 20 65 t order can be e
5e780 69 74 68 65 72 20 41 53 43 20 6f 72 20 44 45 53 ither ASC or DES
5e790 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 C..*/.#define SQ
5e7a0 4c 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20 LITE_SO_ASC
5e7b0 20 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 0 /* Sort in
5e7c0 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 ascending order
5e7d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5e7e0 45 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31 E_SO_DESC 1
5e7f0 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63 /* Sort in asc
5e800 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a ending order */.
5e810 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66 ./*.** Column af
5e820 66 69 6e 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a finity types..**
5e830 0a 2a 2a 20 54 68 65 73 65 20 75 73 65 64 20 74 .** These used t
5e840 6f 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20 o have mnemonic
5e850 6e 61 6d 65 20 6c 69 6b 65 20 27 69 27 20 66 6f name like 'i' fo
5e860 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 r SQLITE_AFF_INT
5e870 45 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20 EGER and.** 't'
5e880 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 for SQLITE_AFF_T
5e890 45 58 54 2e 20 20 42 75 74 20 77 65 20 63 61 6e EXT. But we can
5e8a0 20 73 61 76 65 20 61 20 6c 69 74 74 6c 65 20 73 save a little s
5e8b0 70 61 63 65 20 61 6e 64 20 69 6d 70 72 6f 76 65 pace and improve
5e8c0 0a 2a 2a 20 74 68 65 20 73 70 65 65 64 20 61 20 .** the speed a
5e8d0 6c 69 74 74 6c 65 20 62 79 20 6e 75 6d 62 65 72 little by number
5e8e0 69 6e 67 20 74 68 65 20 76 61 6c 75 65 73 20 63 ing the values c
5e8f0 6f 6e 73 65 63 75 74 69 76 65 6c 79 2e 20 20 0a onsecutively. .
5e900 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68 65 72 **.** But rather
5e910 20 74 68 61 6e 20 73 74 61 72 74 20 77 69 74 68 than start with
5e920 20 30 20 6f 72 20 31 2c 20 77 65 20 62 65 67 69 0 or 1, we begi
5e930 6e 20 77 69 74 68 20 27 61 27 2e 20 20 54 68 61 n with 'a'. Tha
5e940 74 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e 20 6d t way,.** when m
5e950 75 6c 74 69 70 6c 65 20 61 66 66 69 6e 69 74 79 ultiple affinity
5e960 20 74 79 70 65 73 20 61 72 65 20 63 6f 6e 63 61 types are conca
5e970 74 65 6e 61 74 65 64 20 69 6e 74 6f 20 61 20 73 tenated into a s
5e980 74 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75 73 65 tring and.** use
5e990 64 20 61 73 20 74 68 65 20 50 34 20 6f 70 65 72 d as the P4 oper
5e9a0 61 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c 20 62 and, they will b
5e9b0 65 20 6d 6f 72 65 20 72 65 61 64 61 62 6c 65 2e e more readable.
5e9c0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f .**.** Note also
5e9d0 20 74 68 61 74 20 74 68 65 20 6e 75 6d 65 72 69 that the numeri
5e9e0 63 20 74 79 70 65 73 20 61 72 65 20 67 72 6f 75 c types are grou
5e9f0 70 65 64 20 74 6f 67 65 74 68 65 72 20 73 6f 20 ped together so
5ea00 74 68 61 74 20 74 65 73 74 69 6e 67 0a 2a 2a 20 that testing.**
5ea10 66 6f 72 20 61 20 6e 75 6d 65 72 69 63 20 74 79 for a numeric ty
5ea20 70 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 63 pe is a single c
5ea30 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 23 64 omparison..*/.#d
5ea40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 efine SQLITE_AFF
5ea50 5f 54 45 58 54 20 20 20 20 20 27 61 27 0a 23 64 _TEXT 'a'.#d
5ea60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 efine SQLITE_AFF
5ea70 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a 23 64 _NONE 'b'.#d
5ea80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 efine SQLITE_AFF
5ea90 5f 4e 55 4d 45 52 49 43 20 20 27 63 27 0a 23 64 _NUMERIC 'c'.#d
5eaa0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 efine SQLITE_AFF
5eab0 5f 49 4e 54 45 47 45 52 20 20 27 64 27 0a 23 64 _INTEGER 'd'.#d
5eac0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 efine SQLITE_AFF
5ead0 5f 52 45 41 4c 20 20 20 20 20 27 65 27 0a 0a 23 _REAL 'e'..#
5eae0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 define sqlite3Is
5eaf0 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 NumericAffinity(
5eb00 58 29 20 20 28 28 58 29 3e 3d 53 51 4c 49 54 45 X) ((X)>=SQLITE
5eb10 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a 0a 2f _AFF_NUMERIC)../
5eb20 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
5eb30 41 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65 73 20 AFF_MASK values
5eb40 6d 61 73 6b 73 20 6f 66 66 20 74 68 65 20 73 69 masks off the si
5eb50 67 6e 69 66 69 63 61 6e 74 20 62 69 74 73 20 6f gnificant bits o
5eb60 66 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 f an.** affinity
5eb70 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64 65 66 value. .*/.#def
5eb80 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d ine SQLITE_AFF_M
5eb90 41 53 4b 20 20 20 20 20 30 78 36 37 0a 0a 2f 2a ASK 0x67../*
5eba0 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 62 .** Additional b
5ebb0 69 74 20 76 61 6c 75 65 73 20 74 68 61 74 20 63 it values that c
5ebc0 61 6e 20 62 65 20 4f 52 65 64 20 77 69 74 68 20 an be ORed with
5ebd0 61 6e 20 61 66 66 69 6e 69 74 79 20 77 69 74 68 an affinity with
5ebe0 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 20 out.** changing
5ebf0 74 68 65 20 61 66 66 69 6e 69 74 79 2e 0a 2a 2f the affinity..*/
5ec00 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5ec10 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30 78 30 JUMPIFNULL 0x0
5ec20 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66 20 65 8 /* jumps if e
5ec30 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
5ec40 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 NULL */.#define
5ec50 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 SQLITE_STOREP2
5ec60 20 20 20 20 20 30 78 31 30 20 20 2f 2a 20 53 74 0x10 /* St
5ec70 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 ore result in re
5ec80 67 5b 50 32 5d 20 72 61 74 68 65 72 20 74 68 61 g[P2] rather tha
5ec90 6e 20 6a 75 6d 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a n jump */../*.**
5eca0 20 41 6e 20 6f 62 6a 65 63 74 20 6f 66 20 74 68 An object of th
5ecb0 69 73 20 74 79 70 65 20 69 73 20 63 72 65 61 74 is type is creat
5ecc0 65 64 20 66 6f 72 20 65 61 63 68 20 76 69 72 74 ed for each virt
5ecd0 75 61 6c 20 74 61 62 6c 65 20 70 72 65 73 65 6e ual table presen
5ece0 74 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 t in.** the data
5ecf0 62 61 73 65 20 73 63 68 65 6d 61 2e 20 0a 2a 2a base schema. .**
5ed00 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 .** If the datab
5ed10 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 68 ase schema is sh
5ed20 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 ared, then there
5ed30 20 69 73 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 is one instance
5ed40 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 75 of this.** stru
5ed50 63 74 75 72 65 20 66 6f 72 20 65 61 63 68 20 64 cture for each d
5ed60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
5ed70 6f 6e 20 28 73 71 6c 69 74 65 33 2a 29 20 74 68 on (sqlite3*) th
5ed80 61 74 20 75 73 65 73 20 74 68 65 20 73 68 61 72 at uses the shar
5ed90 65 64 0a 2a 2a 20 73 63 68 65 6d 61 2e 20 54 68 ed.** schema. Th
5eda0 69 73 20 69 73 20 62 65 63 61 75 73 65 20 65 61 is is because ea
5edb0 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ch database conn
5edc0 65 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 ection requires
5edd0 69 74 73 20 6f 77 6e 20 75 6e 69 71 75 65 0a 2a its own unique.*
5ede0 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
5edf0 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 e sqlite3_vtab*
5ee00 68 61 6e 64 6c 65 20 75 73 65 64 20 74 6f 20 61 handle used to a
5ee10 63 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 ccess the virtua
5ee20 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 69 6d 70 6c l table .** impl
5ee30 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 73 71 6c 69 ementation. sqli
5ee40 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 te3_vtab* handle
5ee50 73 20 63 61 6e 20 6e 6f 74 20 62 65 20 73 68 61 s can not be sha
5ee60 72 65 64 20 62 65 74 77 65 65 6e 20 0a 2a 2a 20 red between .**
5ee70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
5ee80 69 6f 6e 73 2c 20 65 76 65 6e 20 77 68 65 6e 20 ions, even when
5ee90 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 the rest of the
5eea0 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
5eeb0 73 65 20 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 se .** schema is
5eec0 20 73 68 61 72 65 64 2c 20 61 73 20 74 68 65 20 shared, as the
5eed0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
5eee0 66 74 65 6e 20 73 74 6f 72 65 73 20 74 68 65 20 ften stores the
5eef0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e database.** conn
5ef00 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 70 61 ection handle pa
5ef10 73 73 65 64 20 74 6f 20 69 74 20 76 69 61 20 74 ssed to it via t
5ef20 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 6f 72 he xConnect() or
5ef30 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f xCreate() metho
5ef40 64 0a 2a 2a 20 64 75 72 69 6e 67 20 69 6e 69 74 d.** during init
5ef50 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e 74 65 72 ialization inter
5ef60 6e 61 6c 6c 79 2e 20 54 68 69 73 20 64 61 74 61 nally. This data
5ef70 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
5ef80 68 61 6e 64 6c 65 20 6d 61 79 0a 2a 2a 20 74 68 handle may.** th
5ef90 65 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 76 en used by the v
5efa0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 irtual table imp
5efb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 61 lementation to a
5efc0 63 63 65 73 73 20 72 65 61 6c 20 74 61 62 6c 65 ccess real table
5efd0 73 20 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 s .** within the
5efe0 20 64 61 74 61 62 61 73 65 2e 20 53 6f 20 74 68 database. So th
5eff0 61 74 20 74 68 65 79 20 61 70 70 65 61 72 20 61 at they appear a
5f000 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 61 s part of the ca
5f010 6c 6c 65 72 73 20 0a 2a 2a 20 74 72 61 6e 73 61 llers .** transa
5f020 63 74 69 6f 6e 2c 20 74 68 65 73 65 20 61 63 63 ction, these acc
5f030 65 73 73 65 73 20 6e 65 65 64 20 74 6f 20 62 65 esses need to be
5f040 20 6d 61 64 65 20 76 69 61 20 74 68 65 20 73 61 made via the sa
5f050 6d 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 me database .**
5f060 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 68 connection as th
5f070 61 74 20 75 73 65 64 20 74 6f 20 65 78 65 63 75 at used to execu
5f080 74 65 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e te SQL operation
5f090 73 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61 6c s on the virtual
5f0a0 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c table..**.** Al
5f0b0 6c 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 l VTable objects
5f0c0 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 that correspond
5f0d0 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 74 61 62 to a single tab
5f0e0 6c 65 20 69 6e 20 61 20 73 68 61 72 65 64 0a 2a le in a shared.*
5f0f0 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d * database schem
5f100 61 20 61 72 65 20 69 6e 69 74 69 61 6c 6c 79 20 a are initially
5f110 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b stored in a link
5f120 65 64 2d 6c 69 73 74 20 70 6f 69 6e 74 65 64 20 ed-list pointed
5f130 74 6f 20 62 79 0a 2a 2a 20 74 68 65 20 54 61 62 to by.** the Tab
5f140 6c 65 2e 70 56 54 61 62 6c 65 20 6d 65 6d 62 65 le.pVTable membe
5f150 72 20 76 61 72 69 61 62 6c 65 20 6f 66 20 74 68 r variable of th
5f160 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
5f170 54 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a Table object..**
5f180 20 57 68 65 6e 20 61 6e 20 73 71 6c 69 74 65 33 When an sqlite3
5f190 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72 61 _prepare() opera
5f1a0 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 tion is required
5f1b0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 76 to access the v
5f1c0 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 2c irtual.** table,
5f1d0 20 69 74 20 73 65 61 72 63 68 65 73 20 74 68 65 it searches the
5f1e0 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 56 54 list for the VT
5f1f0 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73 able that corres
5f200 70 6f 6e 64 73 20 74 6f 20 74 68 65 0a 2a 2a 20 ponds to the.**
5f210 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
5f220 69 6f 6e 20 64 6f 69 6e 67 20 74 68 65 20 70 72 ion doing the pr
5f230 65 70 61 72 69 6e 67 20 73 6f 20 61 73 20 74 6f eparing so as to
5f240 20 75 73 65 20 74 68 65 20 63 6f 72 72 65 63 74 use the correct
5f250 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 .** sqlite3_vtab
5f260 2a 20 68 61 6e 64 6c 65 20 69 6e 20 74 68 65 20 * handle in the
5f270 63 6f 6d 70 69 6c 65 64 20 71 75 65 72 79 2e 0a compiled query..
5f280 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 69 6e **.** When an in
5f290 2d 6d 65 6d 6f 72 79 20 54 61 62 6c 65 20 6f 62 -memory Table ob
5f2a0 6a 65 63 74 20 69 73 20 64 65 6c 65 74 65 64 20 ject is deleted
5f2b0 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65 (for example whe
5f2c0 6e 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 n the.** schema
5f2d0 69 73 20 62 65 69 6e 67 20 72 65 6c 6f 61 64 65 is being reloade
5f2e0 64 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f d for some reaso
5f2f0 6e 29 2c 20 74 68 65 20 56 54 61 62 6c 65 20 6f n), the VTable o
5f300 62 6a 65 63 74 73 20 61 72 65 20 6e 6f 74 20 0a bjects are not .
5f310 2a 2a 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74 ** deleted and t
5f320 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a he sqlite3_vtab*
5f330 20 68 61 6e 64 6c 65 73 20 61 72 65 20 6e 6f 74 handles are not
5f340 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29 65 64 xDisconnect()ed
5f350 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 .** immediately
5f360 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 79 20 . Instead, they
5f370 61 72 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 are moved from t
5f380 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62 6c 65 he Table.pVTable
5f390 20 6c 69 73 74 20 74 6f 0a 2a 2a 20 61 6e 6f 74 list to.** anot
5f3a0 68 65 72 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 her linked list
5f3b0 68 65 61 64 65 64 20 62 79 20 74 68 65 20 73 71 headed by the sq
5f3c0 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 lite3.pDisconnec
5f3d0 74 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 0a t member of the.
5f3e0 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
5f3f0 20 73 71 6c 69 74 65 33 20 73 74 72 75 63 74 75 sqlite3 structu
5f400 72 65 2e 20 54 68 65 79 20 61 72 65 20 74 68 65 re. They are the
5f410 6e 20 64 65 6c 65 74 65 64 2f 78 44 69 73 63 6f n deleted/xDisco
5f420 6e 6e 65 63 74 65 64 20 0a 2a 2a 20 6e 65 78 74 nnected .** next
5f430 20 74 69 6d 65 20 61 20 73 74 61 74 65 6d 65 6e time a statemen
5f440 74 20 69 73 20 70 72 65 70 61 72 65 64 20 75 73 t is prepared us
5f450 69 6e 67 20 73 61 69 64 20 73 71 6c 69 74 65 33 ing said sqlite3
5f460 2a 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 0a *. This is done.
5f470 2a 2a 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 ** to avoid dead
5f480 6c 6f 63 6b 20 69 73 73 75 65 73 20 69 6e 76 6f lock issues invo
5f490 6c 76 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 73 lving multiple s
5f4a0 71 6c 69 74 65 33 2e 6d 75 74 65 78 20 6d 75 74 qlite3.mutex mut
5f4b0 65 78 65 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 exes..** Refer t
5f4c0 6f 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 o comments above
5f4d0 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 function sqlite
5f4e0 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 3VtabUnlockList(
5f4f0 29 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 6c ) for an.** expl
5f500 61 6e 61 74 69 6f 6e 20 61 73 20 74 6f 20 77 68 anation as to wh
5f510 79 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 y it is safe to
5f520 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 add an entry to
5f530 61 6e 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 an sqlite3.pDisc
5f540 6f 6e 6e 65 63 74 0a 2a 2a 20 6c 69 73 74 20 77 onnect.** list w
5f550 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 74 ithout holding t
5f560 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
5f570 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 20 6d sqlite3.mutex m
5f580 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 utex..**.** The
5f590 6d 65 6d 6f 72 79 20 66 6f 72 20 6f 62 6a 65 63 memory for objec
5f5a0 74 73 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ts of this type
5f5b0 69 73 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 is always alloca
5f5c0 74 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74 ted by .** sqlit
5f5d0 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2c 20 75 73 e3DbMalloc(), us
5f5e0 69 6e 67 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 ing the connecti
5f5f0 6f 6e 20 68 61 6e 64 6c 65 20 73 74 6f 72 65 64 on handle stored
5f600 20 69 6e 20 56 54 61 62 6c 65 2e 64 62 20 61 73 in VTable.db as
5f610 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 .** the first a
5f620 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 rgument..*/.stru
5f630 63 74 20 56 54 61 62 6c 65 20 7b 0a 20 20 73 71 ct VTable {. sq
5f640 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
5f650 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
5f660 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 ase connection a
5f670 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
5f680 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 4d his table */. M
5f690 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 20 20 20 20 odule *pMod;
5f6a0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
5f6b0 74 65 72 20 74 6f 20 6d 6f 64 75 6c 65 20 69 6d ter to module im
5f6c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a plementation */.
5f6d0 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
5f6e0 70 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 50 pVtab; /* P
5f6f0 6f 69 6e 74 65 72 20 74 6f 20 76 74 61 62 20 69 ointer to vtab i
5f700 6e 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 nstance */. int
5f710 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 nRef;
5f720 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5f730 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
5f740 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
5f750 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 4e 65 78 /. VTable *pNex
5f760 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
5f770 20 4e 65 78 74 20 69 6e 20 6c 69 6e 6b 65 64 20 Next in linked
5f780 6c 69 73 74 20 28 73 65 65 20 61 62 6f 76 65 29 list (see above)
5f790 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 */.};../*.** Ea
5f7a0 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20 ch SQL table is
5f7b0 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d represented in m
5f7c0 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e 73 74 emory by an inst
5f7d0 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 ance of the.** f
5f7e0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
5f7f0 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e re..**.** Table.
5f800 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d zName is the nam
5f810 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 e of the table.
5f820 20 54 68 65 20 63 61 73 65 20 6f 66 20 74 68 65 The case of the
5f830 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 original.** CRE
5f840 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d ATE TABLE statem
5f850 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c 20 62 ent is stored, b
5f860 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74 20 73 ut case is not s
5f870 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 0a 2a ignificant for.*
5f880 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a * comparisons..*
5f890 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 *.** Table.nCol
5f8a0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
5f8b0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 columns in this
5f8c0 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 2e 61 table. Table.a
5f8d0 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e Col is a.** poin
5f8e0 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20 ter to an array
5f8f0 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75 63 74 of Column struct
5f900 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20 65 61 ures, one for ea
5f910 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a ch column..**.**
5f920 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 If the table ha
5f930 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 s an INTEGER PRI
5f940 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 54 MARY KEY, then T
5f950 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 74 68 able.iPKey is th
5f960 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 e index of.** th
5f970 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 e column that is
5f980 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f 74 68 that key. Oth
5f990 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69 50 4b erwise Table.iPK
5f9a0 65 79 20 69 73 20 6e 65 67 61 74 69 76 65 2e 20 ey is negative.
5f9b0 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20 74 68 Note.** that th
5f9c0 65 20 64 61 74 61 74 79 70 65 20 6f 66 20 74 68 e datatype of th
5f9d0 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 e PRIMARY KEY mu
5f9e0 73 74 20 62 65 20 49 4e 54 45 47 45 52 20 66 6f st be INTEGER fo
5f9f0 72 20 74 68 69 73 20 66 69 65 6c 64 20 74 6f 0a r this field to.
5fa00 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e 20 49 ** be set. An I
5fa10 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
5fa20 45 59 20 69 73 20 75 73 65 64 20 61 73 20 74 68 EY is used as th
5fa30 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61 63 68 e rowid for each
5fa40 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 20 74 row of.** the t
5fa50 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61 62 6c able. If a tabl
5fa60 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 45 52 e has no INTEGER
5fa70 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 PRIMARY KEY, th
5fa80 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77 69 en a random rowi
5fa90 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61 74 65 d.** is generate
5faa0 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f d for each row o
5fab0 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 46 f the table. TF
5fac0 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 69 _HasPrimaryKey i
5fad0 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 65 20 s set if.** the
5fae0 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20 50 52 table has any PR
5faf0 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54 45 47 IMARY KEY, INTEG
5fb00 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73 65 2e ER or otherwise.
5fb10 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 .**.** Table.tnu
5fb20 6d 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75 m is the page nu
5fb30 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f mber for the roo
5fb40 74 20 42 54 72 65 65 20 70 61 67 65 20 6f 66 20 t BTree page of
5fb50 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 the table in the
5fb60 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c .** database fil
5fb70 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69 44 62 e. If Table.iDb
5fb80 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
5fb90 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 the database ta
5fba0 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 ble backend.** i
5fbb0 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 n sqlite.aDb[].
5fbc0 20 30 20 69 73 20 66 6f 72 20 74 68 65 20 6d 61 0 is for the ma
5fbd0 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 in database and
5fbe0 31 20 69 73 20 66 6f 72 20 74 68 65 20 66 69 6c 1 is for the fil
5fbf0 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 e that.** holds
5fc00 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
5fc10 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20 20 49 and indices. I
5fc20 66 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 69 f TF_Ephemeral i
5fc30 73 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20 74 68 s set.** then th
5fc40 65 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 e table is store
5fc50 64 20 69 6e 20 61 20 66 69 6c 65 20 74 68 61 74 d in a file that
5fc60 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
5fc70 79 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77 68 65 y deleted.** whe
5fc80 6e 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f n the VDBE curso
5fc90 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69 r to the table i
5fca0 73 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20 74 68 s closed. In th
5fcb0 69 73 20 63 61 73 65 20 54 61 62 6c 65 2e 74 6e is case Table.tn
5fcc0 75 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20 56 44 um .** refers VD
5fcd0 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 BE cursor number
5fce0 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 that holds the
5fcf0 74 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 table open, not
5fd00 74 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 to the root.** p
5fd10 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 72 61 age number. Tra
5fd20 6e 73 69 65 6e 74 20 74 61 62 6c 65 73 20 61 72 nsient tables ar
5fd30 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 e used to hold t
5fd40 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61 0a he results of a.
5fd50 2a 2a 20 73 75 62 2d 71 75 65 72 79 20 74 68 61 ** sub-query tha
5fd60 74 20 61 70 70 65 61 72 73 20 69 6e 73 74 65 61 t appears instea
5fd70 64 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c d of a real tabl
5fd80 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52 e name in the FR
5fd90 4f 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 OM clause .** of
5fda0 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d a SELECT statem
5fdb0 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 ent..*/.struct T
5fdc0 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 able {. sqlite3
5fdd0 20 2a 64 62 4d 65 6d 3b 20 20 20 20 20 20 2f 2a *dbMem; /*
5fde0 20 44 42 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 DB connection u
5fdf0 73 65 64 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 sed for lookasid
5fe00 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 2a e allocations. *
5fe10 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b /. char *zName;
5fe20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
5fe30 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 of the table or
5fe40 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 view */. int i
5fe50 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 PKey;
5fe60 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 /* If not negati
5fe70 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b ve, use aCol[iPK
5fe80 65 79 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61 ey] as the prima
5fe90 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 ry key */. int
5fea0 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 nCol;
5feb0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f /* Number of co
5fec0 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 lumns in this ta
5fed0 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 ble */. Column
5fee0 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a *aCol; /*
5fef0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f Information abo
5ff00 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a ut each column *
5ff10 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 /. Index *pInde
5ff20 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 x; /* List
5ff30 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 73 20 of SQL indexes
5ff40 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a on this table. *
5ff50 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 /. int tnum;
5ff60 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
5ff70 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 BTree node for
5ff80 74 68 69 73 20 74 61 62 6c 65 20 28 73 65 65 20 this table (see
5ff90 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 note above) */.
5ffa0 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
5ffb0 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f ; /* NULL fo
5ffc0 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e 74 r tables. Point
5ffd0 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e 20 s to definition
5ffe0 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 if a view. */.
5fff0 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 u16 nRef;
60000 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
60010 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 f pointers to th
60020 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 38 is Table */. u8
60030 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20 tabFlags;
60040 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46 /* Mask of TF
60050 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 _* values */. u
60060 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20 8 keyConf;
60070 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 /* What to d
60080 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 o in case of uni
60090 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 queness conflict
600a0 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 46 on iPKey */. F
600b0 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 Key *pFKey;
600c0 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 /* Linked li
600d0 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67 st of all foreig
600e0 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74 n keys in this t
600f0 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a able */. char *
60100 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 2f zColAff; /
60110 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e * String definin
60120 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f g the affinity o
60130 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f f each column */
60140 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
60150 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70 OMIT_CHECK. Exp
60160 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20 r *pCheck;
60170 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 /* The AND of
60180 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 all CHECK constr
60190 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a aints */.#endif.
601a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
601b0 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 MIT_ALTERTABLE.
601c0 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 int addColOffse
601d0 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 t; /* Offset
601e0 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 in CREATE TABLE
601f0 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20 6e 65 stmt to add a ne
60200 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e 64 w column */.#end
60210 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 if.#ifndef SQLIT
60220 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
60230 42 4c 45 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 BLE. VTable *pV
60240 54 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 4c 69 Table; /* Li
60250 73 74 20 6f 66 20 56 54 61 62 6c 65 20 6f 62 6a st of VTable obj
60260 65 63 74 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e ects. */. int n
60270 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 ModuleArg;
60280 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 /* Number of arg
60290 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f uments to the mo
602a0 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a dule */. char *
602b0 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f *azModuleArg; /
602c0 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f * Text of all mo
602d0 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 dule args. [0] i
602e0 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f s module name */
602f0 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 67 67 65 .#endif. Trigge
60300 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 2f r *pTrigger; /
60310 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 * List of trigge
60320 72 73 20 73 74 6f 72 65 64 20 69 6e 20 70 53 63 rs stored in pSc
60330 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 65 6d 61 hema */. Schema
60340 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f *pSchema; /
60350 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f * Schema that co
60360 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c ntains this tabl
60370 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e e */. Table *pN
60380 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e extZombie; /* N
60390 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65 ext on the Parse
603a0 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74 .pZombieTab list
603b0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c */.};../*.** Al
603c0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 lowed values for
603d0 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73 2e 0a Tabe.tabFlags..
603e0 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 65 */.#define TF_Re
603f0 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 78 adonly 0x
60400 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f 6e 01 /* Read-on
60410 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 ly system table
60420 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 70 */.#define TF_Ep
60430 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 30 78 hemeral 0x
60440 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70 68 65 02 /* An ephe
60450 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 meral table */.#
60460 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50 72 69 define TF_HasPri
60470 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34 20 20 maryKey 0x04
60480 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 61 /* Table has a
60490 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a primary key */.
604a0 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74 6f 69 #define TF_Autoi
604b0 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30 38 20 ncrement 0x08
604c0 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 72 /* Integer pr
604d0 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61 75 74 imary key is aut
604e0 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 64 oincrement */.#d
604f0 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75 61 6c efine TF_Virtual
60500 20 20 20 20 20 20 20 20 20 30 78 31 30 20 20 20 0x10
60510 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75 61 6c /* Is a virtual
60520 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e table */.#defin
60530 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 74 e TF_NeedMetadat
60540 61 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 a 0x20 /*
60550 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e 64 aCol[].zType and
60560 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 aCol[].pColl mi
60570 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a ssing */..../*.*
60580 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 * Test to see wh
60590 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74 ether or not a t
605a0 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 able is a virtua
605b0 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 l table. This i
605c0 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d s.** done as a m
605d0 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20 acro so that it
605e0 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65 will be optimize
605f0 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75 d out when virtu
60600 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 70 al.** table supp
60610 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64 20 66 ort is omitted f
60620 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a rom the build..*
60630 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
60640 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
60650 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 LE.# define IsV
60660 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 20 28 irtual(X) (
60670 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 26 ((X)->tabFlags &
60680 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 29 TF_Virtual)!=0)
60690 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 .# define IsHid
606a0 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 58 denColumn(X) ((X
606b0 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 6c )->isHidden).#el
606c0 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 se.# define IsV
606d0 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 20 30 irtual(X) 0
606e0 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 .# define IsHid
606f0 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a 23 denColumn(X) 0.#
60700 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 endif../*.** Eac
60710 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f h foreign key co
60720 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e 20 69 nstraint is an i
60730 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
60740 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
60750 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 65 re..**.** A fore
60760 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63 ign key is assoc
60770 69 61 74 65 64 20 77 69 74 68 20 74 77 6f 20 74 iated with two t
60780 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f ables. The "fro
60790 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 74 m" table is.** t
607a0 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f he table that co
607b0 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 ntains the REFER
607c0 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74 68 61 ENCES clause tha
607d0 74 20 63 72 65 61 74 65 73 20 74 68 65 20 66 6f t creates the fo
607e0 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 54 reign.** key. T
607f0 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73 he "to" table is
60800 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 the table that
60810 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 is named in the
60820 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 REFERENCES claus
60830 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 e..** Consider t
60840 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a his example:.**.
60850 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41 ** CREATE TA
60860 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 20 BLE ex1(.**
60870 20 20 61 20 49 4e 54 45 47 45 52 20 50 52 49 4d a INTEGER PRIM
60880 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 20 ARY KEY,.**
60890 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f 4e 53 b INTEGER CONS
608a0 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46 45 52 TRAINT fk1 REFER
608b0 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a 2a 20 ENCES ex2(x).**
608c0 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 );.**.** For
608d0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 6b foreign key "fk
608e0 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 1", the from-tab
608f0 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e 64 20 le is "ex1" and
60900 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 the to-table is
60910 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 "ex2"..**.** Eac
60920 68 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 h REFERENCES cla
60930 75 73 65 20 67 65 6e 65 72 61 74 65 73 20 61 6e use generates an
60940 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
60950 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
60960 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 73 ture.** which is
60970 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 attached to the
60980 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 68 from-table. Th
60990 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 20 e to-table need
609a0 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e 0a 2a not exist when.*
609b0 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 * the from-table
609c0 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 is created. Th
609d0 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 74 e existence of t
609e0 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 6e he to-table is n
609f0 6f 74 20 63 68 65 63 6b 65 64 2e 0a 2a 2f 0a 73 ot checked..*/.s
60a00 74 72 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54 truct FKey {. T
60a10 61 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20 able *pFrom;
60a20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 /* The table th
60a30 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
60a40 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 REFERENCES claus
60a50 65 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 e */. FKey *pNe
60a60 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 xtFrom; /* Next
60a70 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 foreign key in
60a80 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 pFrom */. char
60a90 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 *zTo; /*
60aa0 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 Name of table th
60ab0 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 at the key point
60ac0 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 s to */. int nC
60ad0 6f 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ol; /* N
60ae0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
60af0 20 69 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a in this key */.
60b00 20 20 75 38 20 69 73 44 65 66 65 72 72 65 64 3b u8 isDeferred;
60b10 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 /* True if c
60b20 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 69 onstraint checki
60b30 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 20 74 ng is deferred t
60b40 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 ill COMMIT */.
60b50 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b 20 20 u8 updateConf;
60b60 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f /* How to reso
60b70 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 lve conflicts th
60b80 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 44 41 at occur on UPDA
60b90 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74 TE */. u8 delet
60ba0 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77 eConf; /* How
60bb0 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66 to resolve conf
60bc0 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 75 72 licts that occur
60bd0 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 on DELETE */.
60be0 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20 u8 insertConf;
60bf0 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f /* How to reso
60c00 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68 lve conflicts th
60c10 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45 at occur on INSE
60c20 52 54 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 RT */. struct s
60c30 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70 ColMap { /* Map
60c40 70 69 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 ping of columns
60c50 69 6e 20 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 in pFrom to colu
60c60 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 mns in zTo */.
60c70 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 int iFrom;
60c80 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
60c90 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d column in pFrom
60ca0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 */. char *zC
60cb0 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 ol; /* Na
60cc0 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 me of column in
60cd0 7a 54 6f 2e 20 20 49 66 20 30 20 75 73 65 20 50 zTo. If 0 use P
60ce0 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 RIMARY KEY */.
60cf0 7d 20 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 } aCol[1];
60d00 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 /* One entry f
60d10 6f 72 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20 or each of nCol
60d20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a column s */.};..
60d30 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 /*.** SQLite sup
60d40 70 6f 72 74 73 20 6d 61 6e 79 20 64 69 66 66 65 ports many diffe
60d50 72 65 6e 74 20 77 61 79 73 20 74 6f 20 72 65 73 rent ways to res
60d60 6f 6c 76 65 20 61 20 63 6f 6e 73 74 72 61 69 6e olve a constrain
60d70 74 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c t.** error. ROL
60d80 4c 42 41 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 LBACK processing
60d90 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 63 6f means that a co
60da0 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
60db0 6f 6e 0a 2a 2a 20 63 61 75 73 65 73 20 74 68 65 on.** causes the
60dc0 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 operation in pr
60dd0 6f 63 65 73 73 20 74 6f 20 66 61 69 6c 20 61 6e ocess to fail an
60de0 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e d for the curren
60df0 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a t transaction.**
60e00 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 to be rolled ba
60e10 63 6b 2e 20 20 41 42 4f 52 54 20 70 72 6f 63 65 ck. ABORT proce
60e20 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 ssing means the
60e30 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f operation in pro
60e40 63 65 73 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e cess.** fails an
60e50 64 20 61 6e 79 20 70 72 69 6f 72 20 63 68 61 6e d any prior chan
60e60 67 65 73 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e ges from that on
60e70 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 e operation are
60e80 62 61 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 backed out,.** b
60e90 75 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ut the transacti
60ea0 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 on is not rolled
60eb0 20 62 61 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f back. FAIL pro
60ec0 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 cessing means th
60ed0 61 74 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 at.** the operat
60ee0 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 ion in progress
60ef0 73 74 6f 70 73 20 61 6e 64 20 72 65 74 75 72 6e stops and return
60f00 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e s an error code.
60f10 20 20 42 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 But prior.** c
60f20 68 61 6e 67 65 73 20 64 75 65 20 74 6f 20 74 68 hanges due to th
60f30 65 20 73 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e e same operation
60f40 20 61 72 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 are not backed
60f50 6f 75 74 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 out and no rollb
60f60 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 ack.** occurs.
60f70 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 IGNORE means tha
60f80 74 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 t the particular
60f90 20 72 6f 77 20 74 68 61 74 20 63 61 75 73 65 64 row that caused
60fa0 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a the constraint.
60fb0 2a 2a 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 ** error is not
60fc0 69 6e 73 65 72 74 65 64 20 6f 72 20 75 70 64 61 inserted or upda
60fd0 74 65 64 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 ted. Processing
60fe0 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e continues and n
60ff0 6f 20 65 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 o error.** is re
61000 74 75 72 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 turned. REPLACE
61010 20 6d 65 61 6e 73 20 74 68 61 74 20 70 72 65 65 means that pree
61020 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 xisting database
61030 20 72 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 rows that cause
61040 64 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f d.** a UNIQUE co
61050 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
61060 6f 6e 20 61 72 65 20 72 65 6d 6f 76 65 64 20 73 on are removed s
61070 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 69 o that the new i
61080 6e 73 65 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 nsert or.** upda
61090 74 65 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 te can proceed.
610a0 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 Processing cont
610b0 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 inues and no err
610c0 6f 72 20 69 73 20 72 65 70 6f 72 74 65 64 2e 0a or is reported..
610d0 2a 2a 0a 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 **.** RESTRICT,
610e0 53 45 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 SETNULL, and CAS
610f0 43 41 44 45 20 61 63 74 69 6f 6e 73 20 61 70 70 CADE actions app
61100 6c 79 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 ly only to forei
61110 67 6e 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 gn keys..** REST
61120 52 49 43 54 20 69 73 20 74 68 65 20 73 61 6d 65 RICT is the same
61130 20 61 73 20 41 42 4f 52 54 20 66 6f 72 20 49 4d as ABORT for IM
61140 4d 45 44 49 41 54 45 20 66 6f 72 65 69 67 6e 20 MEDIATE foreign
61150 6b 65 79 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 keys and the.**
61160 73 61 6d 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b same as ROLLBACK
61170 20 66 6f 72 20 44 45 46 45 52 52 45 44 20 6b 65 for DEFERRED ke
61180 79 73 2e 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 ys. SETNULL mea
61190 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 ns that the fore
611a0 69 67 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 ign.** key is se
611b0 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 t to NULL. CASC
611c0 41 44 45 20 6d 65 61 6e 73 20 74 68 61 74 20 61 ADE means that a
611d0 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 DELETE or UPDAT
611e0 45 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 E of the.** refe
611f0 72 65 6e 63 65 64 20 74 61 62 6c 65 20 72 6f 77 renced table row
61200 20 69 73 20 70 72 6f 70 61 67 61 74 65 64 20 69 is propagated i
61210 6e 74 6f 20 74 68 65 20 72 6f 77 20 74 68 61 74 nto the row that
61220 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f holds the.** fo
61230 72 65 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a reign key..** .*
61240 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
61250 73 79 6d 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 symbolic values
61260 61 72 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f are used to reco
61270 72 64 20 77 68 69 63 68 20 74 79 70 65 0a 2a 2a rd which type.**
61280 20 6f 66 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 of action to ta
61290 6b 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f ke..*/.#define O
612a0 45 5f 4e 6f 6e 65 20 20 20 20 20 30 20 20 20 2f E_None 0 /
612b0 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 63 6f * There is no co
612c0 6e 73 74 72 61 69 6e 74 20 74 6f 20 63 68 65 63 nstraint to chec
612d0 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f k */.#define OE_
612e0 52 6f 6c 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 Rollback 1 /*
612f0 46 61 69 6c 20 74 68 65 20 6f 70 65 72 61 74 69 Fail the operati
61300 6f 6e 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 on and rollback
61310 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
61320 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 */.#define OE_Ab
61330 6f 72 74 20 20 20 20 32 20 20 20 2f 2a 20 42 61 ort 2 /* Ba
61340 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73 20 62 ck out changes b
61350 75 74 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 ut do no rollbac
61360 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f k transaction */
61370 0a 23 64 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c .#define OE_Fail
61380 20 20 20 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 3 /* Stop
61390 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 the operation b
613a0 75 74 20 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 ut leave all pri
613b0 6f 72 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 or changes */.#d
613c0 65 66 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 efine OE_Ignore
613d0 20 20 34 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 4 /* Ignore
613e0 74 68 65 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f the error. Do no
613f0 74 20 64 6f 20 74 68 65 20 49 4e 53 45 52 54 20 t do the INSERT
61400 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 or UPDATE */.#de
61410 66 69 6e 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 fine OE_Replace
61420 20 35 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 5 /* Delete e
61430 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 xisting record,
61440 74 68 65 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f then do INSERT o
61450 72 20 55 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 r UPDATE */..#de
61460 66 69 6e 65 20 4f 45 5f 52 65 73 74 72 69 63 74 fine OE_Restrict
61470 20 36 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 6 /* OE_Abort
61480 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 for IMMEDIATE,
61490 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 OE_Rollback for
614a0 44 45 46 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 DEFERRED */.#def
614b0 69 6e 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 ine OE_SetNull
614c0 37 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 7 /* Set the f
614d0 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 oreign key value
614e0 20 74 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 to NULL */.#def
614f0 69 6e 65 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 ine OE_SetDflt
61500 38 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 8 /* Set the f
61510 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 oreign key value
61520 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 to its default
61530 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 */.#define OE_Ca
61540 73 63 61 64 65 20 20 39 20 20 20 2f 2a 20 43 61 scade 9 /* Ca
61550 73 63 61 64 65 20 74 68 65 20 63 68 61 6e 67 65 scade the change
61560 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 s */..#define OE
61570 5f 44 65 66 61 75 6c 74 20 20 39 39 20 20 2f 2a _Default 99 /*
61580 20 44 6f 20 77 68 61 74 65 76 65 72 20 74 68 65 Do whatever the
61590 20 64 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 default action
615a0 69 73 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e is */.../*.** An
615b0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
615c0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
615d0 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20 61 ture is passed a
615e0 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 s the first.** a
615f0 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
61600 65 33 56 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 e3VdbeKeyCompare
61610 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 and is used to
61620 63 6f 6e 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 control the .**
61630 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 comparison of th
61640 65 20 74 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 e two index keys
61650 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 ..*/.struct KeyI
61660 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 nfo {. sqlite3
61670 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 *db; /* T
61680 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
61690 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 ection */. u8 e
616a0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 nc;
616b0 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 /* Text encoding
616c0 20 2d 20 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 - one of the TE
616d0 58 54 5f 55 74 66 2a 20 76 61 6c 75 65 73 20 2a XT_Utf* values *
616e0 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 /. u16 nField;
616f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
61700 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
61710 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 aColl[] */. u8
61720 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 *aSortOrder;
61730 20 2f 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 /* If defined a
61740 6e 20 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 n aSortOrder[i]
61750 69 73 20 74 72 75 65 2c 20 73 6f 72 74 20 44 45 is true, sort DE
61760 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 SC */. CollSeq
61770 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 *aColl[1]; /* C
61780 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
61790 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 e for each term
617a0 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b of the key */.};
617b0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
617c0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
617d0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 wing structure h
617e0 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e olds information
617f0 20 61 62 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 about a.** sing
61800 6c 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 le index record
61810 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 that has already
61820 20 62 65 65 6e 20 70 61 72 73 65 64 20 6f 75 74 been parsed out
61830 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c into individual
61840 0a 2a 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a .** values..**.*
61850 2a 20 41 20 72 65 63 6f 72 64 20 69 73 20 61 6e * A record is an
61860 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f 6e object that con
61870 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 tains one or mor
61880 65 20 66 69 65 6c 64 73 20 6f 66 20 64 61 74 61 e fields of data
61890 2e 0a 2a 2a 20 52 65 63 6f 72 64 73 20 61 72 65 ..** Records are
618a0 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 used to store t
618b0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 he content of a
618c0 74 61 62 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f table row and to
618d0 20 73 74 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 store.** the ke
618e0 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 y of an index.
618f0 41 20 62 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 A blob encoding
61900 6f 66 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 of a record is c
61910 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 reated by.** the
61920 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f OP_MakeRecord o
61930 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 pcode of the VDB
61940 45 20 61 6e 64 20 69 73 20 64 69 73 61 73 73 65 E and is disasse
61950 6d 62 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 mbled by the.**
61960 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 OP_Column opcode
61970 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 ..**.** This str
61980 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61 20 72 ucture holds a r
61990 65 63 6f 72 64 20 74 68 61 74 20 68 61 73 20 61 ecord that has a
619a0 6c 72 65 61 64 79 20 62 65 65 6e 20 64 69 73 61 lready been disa
619b0 73 73 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f ssembled.** into
619c0 20 69 74 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 its constituent
619d0 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 fields..*/.stru
619e0 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 ct UnpackedRecor
619f0 64 20 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 d {. KeyInfo *p
61a00 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c KeyInfo; /* Col
61a10 6c 61 74 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d lation and sort-
61a20 6f 72 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f order informatio
61a30 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c n */. u16 nFiel
61a40 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 d; /* Nu
61a50 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
61a60 69 6e 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 in apMem[] */.
61a70 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 u16 flags;
61a80 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 /* Boolean s
61a90 65 74 74 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b ettings. UNPACK
61aa0 45 44 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a ED_... below */.
61ab0 20 20 69 36 34 20 72 6f 77 69 64 3b 20 20 20 20 i64 rowid;
61ac0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 /* Used by
61ad0 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 UNPACKED_PREFIX
61ae0 5f 53 45 41 52 43 48 20 2a 2f 0a 20 20 4d 65 6d _SEARCH */. Mem
61af0 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 *aMem;
61b00 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b /* Values */.};
61b10 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 ../*.** Allowed
61b20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 6b values of Unpack
61b30 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a 2a edRecord.flags.*
61b40 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b /.#define UNPACK
61b50 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 20 ED_NEED_FREE
61b60 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 0x0001 /* Memo
61b70 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 74 ry is from sqlit
61b80 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 e3Malloc() */.#d
61b90 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e efine UNPACKED_N
61ba0 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 78 30 EED_DESTROY 0x0
61bb0 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 002 /* apMem[]s
61bc0 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 64 should all be d
61bd0 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 66 estroyed */.#def
61be0 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e ine UNPACKED_IGN
61bf0 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 30 30 ORE_ROWID 0x000
61c00 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 61 4 /* Ignore tra
61c10 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 6b iling rowid on k
61c20 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 ey1 */.#define U
61c30 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 NPACKED_INCRKEY
61c40 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 0x0008 /*
61c50 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20 61 Make this key a
61c60 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 72 n epsilon larger
61c70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 */.#define UNPA
61c80 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 CKED_PREFIX_MATC
61c90 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 20 H 0x0010 /* A
61ca0 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 prefix match is
61cb0 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f considered OK */
61cc0 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 .#define UNPACKE
61cd0 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 D_PREFIX_SEARCH
61ce0 30 78 30 30 32 30 20 20 2f 2a 20 41 20 70 72 65 0x0020 /* A pre
61cf0 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e fix match is con
61d00 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f sidered OK */../
61d10 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 69 6e *.** Each SQL in
61d20 64 65 78 20 69 73 20 72 65 70 72 65 73 65 6e 74 dex is represent
61d30 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 ed in memory by
61d40 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
61d50 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
61d60 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
61d70 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 The columns of
61d80 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 the table that a
61d90 72 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 re to be indexed
61da0 20 61 72 65 20 64 65 73 63 72 69 62 65 64 0a 2a are described.*
61db0 2a 20 62 79 20 74 68 65 20 61 69 43 6f 6c 75 6d * by the aiColum
61dc0 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 20 74 68 69 n[] field of thi
61dd0 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 46 6f s structure. Fo
61de0 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f r example, suppo
61df0 73 65 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 68 se.** we have th
61e00 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c e following tabl
61e10 65 20 61 6e 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a e and index:.**.
61e20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41 ** CREATE TA
61e30 42 4c 45 20 45 78 31 28 63 31 20 69 6e 74 2c 20 BLE Ex1(c1 int,
61e40 63 32 20 69 6e 74 2c 20 63 33 20 74 65 78 74 29 c2 int, c3 text)
61e50 3b 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 ;.** CREATE
61e60 49 4e 44 45 58 20 45 78 32 20 4f 4e 20 45 78 31 INDEX Ex2 ON Ex1
61e70 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 (c3,c1);.**.** I
61e80 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 n the Table stru
61e90 63 74 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 cture describing
61ea0 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 Ex1, nCol==3 be
61eb0 63 61 75 73 65 20 74 68 65 72 65 20 61 72 65 0a cause there are.
61ec0 2a 2a 20 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73 ** three columns
61ed0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 in the table.
61ee0 49 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 In the Index str
61ef0 75 63 74 75 72 65 20 64 65 73 63 72 69 62 69 6e ucture describin
61f00 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d g.** Ex2, nColum
61f10 6e 3d 3d 32 20 73 69 6e 63 65 20 32 20 6f 66 20 n==2 since 2 of
61f20 74 68 65 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 the 3 columns of
61f30 20 45 78 31 20 61 72 65 20 69 6e 64 65 78 65 64 Ex1 are indexed
61f40 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f ..** The value o
61f50 66 20 61 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 f aiColumn is {2
61f60 2c 20 30 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b , 0}. aiColumn[
61f70 30 5d 3d 3d 32 20 62 65 63 61 75 73 65 20 74 68 0]==2 because th
61f80 65 20 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 e .** first colu
61f90 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 mn to be indexed
61fa0 20 28 63 33 29 20 68 61 73 20 61 6e 20 69 6e 64 (c3) has an ind
61fb0 65 78 20 6f 66 20 32 20 69 6e 20 45 78 31 2e 61 ex of 2 in Ex1.a
61fc0 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 Col[]..** The se
61fd0 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 cond column to b
61fe0 65 20 69 6e 64 65 78 65 64 20 28 63 31 29 20 68 e indexed (c1) h
61ff0 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 as an index of 0
62000 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b in.** Ex1.aCol[
62010 5d 2c 20 68 65 6e 63 65 20 45 78 32 2e 61 69 43 ], hence Ex2.aiC
62020 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a olumn[1]==0..**.
62030 2a 2a 20 54 68 65 20 49 6e 64 65 78 2e 6f 6e 45 ** The Index.onE
62040 72 72 6f 72 20 66 69 65 6c 64 20 64 65 74 65 72 rror field deter
62050 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 mines whether or
62060 20 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 65 64 not the indexed
62070 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 columns.** must
62080 20 62 65 20 75 6e 69 71 75 65 20 61 6e 64 20 77 be unique and w
62090 68 61 74 20 74 6f 20 64 6f 20 69 66 20 74 68 65 hat to do if the
620a0 79 20 61 72 65 20 6e 6f 74 2e 20 20 57 68 65 6e y are not. When
620b0 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f Index.onError=O
620c0 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 E_None,.** it me
620d0 61 6e 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 ans this is not
620e0 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 a unique index.
620f0 20 4f 74 68 65 72 77 69 73 65 20 69 74 20 69 73 Otherwise it is
62100 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 0a a unique index.
62110 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 ** and the value
62120 20 6f 66 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f of Index.onErro
62130 72 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 77 r indicate the w
62140 68 69 63 68 20 63 6f 6e 66 6c 69 63 74 20 72 65 hich conflict re
62150 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 solution .** alg
62160 6f 72 69 74 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 orithm to employ
62170 20 77 68 65 6e 65 76 65 72 20 61 6e 20 61 74 74 whenever an att
62180 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 empt is made to
62190 69 6e 73 65 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 insert a non-uni
621a0 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a que.** element..
621b0 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78 20 */.struct Index
621c0 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b {. char *zName;
621d0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
621e0 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 this index */.
621f0 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 int nColumn;
62200 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f /* Number of co
62210 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 lumns in the tab
62220 6c 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20 le used by this
62230 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a index */. int *
62240 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 aiColumn; /* W
62250 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 hich columns are
62260 20 75 73 65 64 20 62 79 20 74 68 69 73 20 69 6e used by this in
62270 64 65 78 2e 20 20 31 73 74 20 69 73 20 30 20 2a dex. 1st is 0 *
62280 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 69 /. unsigned *ai
62290 52 6f 77 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c RowEst; /* Resul
622a0 74 20 6f 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73 t of ANALYZE: Es
622b0 74 2e 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64 t. rows selected
622c0 20 62 79 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 by each column
622d0 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 */. Table *pTab
622e0 6c 65 3b 20 20 20 2f 2a 20 54 68 65 20 53 51 4c le; /* The SQL
622f0 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 table being ind
62300 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6e exed */. int tn
62310 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 um; /* Pa
62320 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f ge containing ro
62330 6f 74 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 ot of this index
62340 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c in database fil
62350 65 20 2a 2f 0a 20 20 75 38 20 6f 6e 45 72 72 6f e */. u8 onErro
62360 72 3b 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 r; /* OE_Ab
62370 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 ort, OE_Ignore,
62380 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f OE_Replace, or O
62390 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 61 E_None */. u8 a
623a0 75 74 6f 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 utoIndex; /*
623b0 54 72 75 65 20 69 66 20 69 73 20 61 75 74 6f 6d True if is autom
623c0 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 atically created
623d0 20 28 65 78 3a 20 62 79 20 55 4e 49 51 55 45 29 (ex: by UNIQUE)
623e0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c */. char *zCol
623f0 41 66 66 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67 Aff; /* String
62400 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 defining the af
62410 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 finity of each c
62420 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 olumn */. Index
62430 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54 *pNext; /* T
62440 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 61 73 he next index as
62450 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 sociated with th
62460 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a e same table */.
62470 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
62480 61 3b 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e a; /* Schema con
62490 74 61 69 6e 69 6e 67 20 74 68 69 73 20 69 6e 64 taining this ind
624a0 65 78 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 ex */. u8 *aSor
624b0 74 4f 72 64 65 72 3b 20 20 2f 2a 20 41 72 72 61 tOrder; /* Arra
624c0 79 20 6f 66 20 73 69 7a 65 20 49 6e 64 65 78 2e y of size Index.
624d0 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 nColumn. True==D
624e0 45 53 43 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20 ESC, False==ASC
624f0 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f */. char **azCo
62500 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 61 79 20 6f ll; /* Array o
62510 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 f collation sequ
62520 65 6e 63 65 20 6e 61 6d 65 73 20 66 6f 72 20 69 ence names for i
62530 6e 64 65 78 20 2a 2f 0a 20 20 49 6e 64 65 78 53 ndex */. IndexS
62540 61 6d 70 6c 65 20 2a 61 53 61 6d 70 6c 65 3b 20 ample *aSample;
62550 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 /* Array of S
62560 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 QLITE_INDEX_SAMP
62570 4c 45 53 20 73 61 6d 70 6c 65 73 20 2a 2f 0a 7d LES samples */.}
62580 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 73 61 ;../*.** Each sa
62590 6d 70 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 mple stored in t
625a0 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 he sqlite_stat2
625b0 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73 65 table is represe
625c0 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 0a nted in memory .
625d0 2a 2a 20 75 73 69 6e 67 20 61 20 73 74 72 75 63 ** using a struc
625e0 74 75 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 ture of this typ
625f0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 e..*/.struct Ind
62600 65 78 53 61 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 exSample {. uni
62610 6f 6e 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a on {. char *z
62620 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 ; /* Valu
62630 65 20 69 66 20 65 54 79 70 65 20 69 73 20 53 51 e if eType is SQ
62640 4c 49 54 45 5f 54 45 58 54 20 6f 72 20 53 51 4c LITE_TEXT or SQL
62650 49 54 45 5f 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 ITE_BLOB */.
62660 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 double r;
62670 2f 2a 20 56 61 6c 75 65 20 69 66 20 65 54 79 70 /* Value if eTyp
62680 65 20 69 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41 e is SQLITE_FLOA
62690 54 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 54 45 T or SQLITE_INTE
626a0 47 45 52 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 GER */. } u;.
626b0 75 38 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 u8 eType;
626c0 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c /* SQLITE_NULL
626d0 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 , SQLITE_INTEGER
626e0 20 2e 2e 2e 20 65 74 63 2e 20 2a 2f 0a 20 20 75 ... etc. */. u
626f0 38 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 8 nByte;
62700 20 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65 /* Size in byte
62710 20 6f 66 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 of text or blob
62720 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 . */.};../*.** E
62730 61 63 68 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67 ach token coming
62740 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 65 78 65 out of the lexe
62750 72 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 r is an instance
62760 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 of.** this stru
62770 63 74 75 72 65 2e 20 20 54 6f 6b 65 6e 73 20 61 cture. Tokens a
62780 72 65 20 61 6c 73 6f 20 75 73 65 64 20 61 73 20 re also used as
62790 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65 part of an expre
627a0 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 ssion..**.** Not
627b0 65 20 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 e if Token.z==0
627c0 74 68 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e 20 61 then Token.dyn a
627d0 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61 72 65 20 75 nd Token.n are u
627e0 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a 2a 2a 20 ndefined and.**
627f0 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72 61 6e 64 may contain rand
62800 6f 6d 20 76 61 6c 75 65 73 2e 20 20 44 6f 20 6e om values. Do n
62810 6f 74 20 6d 61 6b 65 20 61 6e 79 20 61 73 73 75 ot make any assu
62820 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74 20 54 6f mptions about To
62830 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 61 6e 64 20 54 ken.dyn.** and T
62840 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20 54 6f 6b 65 oken.n when Toke
62850 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 75 63 n.z==0..*/.struc
62860 74 20 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 t Token {. cons
62870 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 2f t char *z; /
62880 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 74 6f * Text of the to
62890 6b 65 6e 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 ken. Not NULL-t
628a0 65 72 6d 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20 erminated! */.
628b0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 3b 20 unsigned int n;
628c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
628d0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 characters in th
628e0 69 73 20 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a is token */.};..
628f0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
62900 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 e of this struct
62910 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 ure contains inf
62920 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 ormation needed
62930 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 to generate.** c
62940 6f 64 65 20 66 6f 72 20 61 20 53 45 4c 45 43 54 ode for a SELECT
62950 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 that contains a
62960 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
62970 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 ns..**.** If Exp
62980 72 2e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c r.op==TK_AGG_COL
62990 55 4d 4e 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 UMN or TK_AGG_FU
629a0 4e 43 54 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 NCTION then Expr
629b0 2e 70 41 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a .pAggInfo is a.*
629c0 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 * pointer to thi
629d0 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 s structure. Th
629e0 65 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 e Expr.iColumn f
629f0 69 65 6c 64 20 69 73 20 74 68 65 20 69 6e 64 65 ield is the inde
62a00 78 20 69 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e x in.** AggInfo.
62a10 61 43 6f 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 aCol[] or AggInf
62a20 6f 2e 61 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 o.aFunc[] of inf
62a30 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 ormation needed
62a40 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 to generate.** c
62a50 6f 64 65 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 ode for that nod
62a60 65 2e 0a 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f e..**.** AggInfo
62a70 2e 70 47 72 6f 75 70 42 79 20 61 6e 64 20 41 67 .pGroupBy and Ag
62a80 67 49 6e 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 gInfo.aFunc.pExp
62a90 72 20 70 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 r point to field
62aa0 73 20 77 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 s within the.**
62ab0 6f 72 69 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 original Select
62ac0 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 structure that d
62ad0 65 73 63 72 69 62 65 73 20 74 68 65 20 53 45 4c escribes the SEL
62ae0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ECT statement.
62af0 54 68 65 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 These.** fields
62b00 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 do not need to b
62b10 65 20 66 72 65 65 64 20 77 68 65 6e 20 64 65 61 e freed when dea
62b20 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20 41 67 llocating the Ag
62b30 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e gInfo structure.
62b40 0a 2a 2f 0a 73 74 72 75 63 74 20 41 67 67 49 6e .*/.struct AggIn
62b50 66 6f 20 7b 0a 20 20 75 38 20 64 69 72 65 63 74 fo {. u8 direct
62b60 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f Mode; /
62b70 2a 20 44 69 72 65 63 74 20 72 65 6e 64 65 72 69 * Direct renderi
62b80 6e 67 20 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 ng mode means ta
62b90 6b 65 20 64 61 74 61 20 64 69 72 65 63 74 6c 79 ke data directly
62ba0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
62bb0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72 ** fr
62bc0 6f 6d 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 om source tables
62bd0 20 72 61 74 68 65 72 20 74 68 61 6e 20 66 72 6f rather than fro
62be0 6d 20 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a m accumulators *
62bf0 2f 0a 20 20 75 38 20 75 73 65 53 6f 72 74 69 6e /. u8 useSortin
62c00 67 49 64 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 gIdx; /* I
62c10 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 n direct mode, r
62c20 65 66 65 72 65 6e 63 65 20 74 68 65 20 73 6f 72 eference the sor
62c30 74 69 6e 67 20 69 6e 64 65 78 20 72 61 74 68 65 ting index rathe
62c40 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r.
62c50 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ** t
62c60 68 61 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 han the source t
62c70 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f able */. int so
62c80 72 74 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20 rtingIdx;
62c90 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 /* Cursor numb
62ca0 65 72 20 6f 66 20 74 68 65 20 73 6f 72 74 69 6e er of the sortin
62cb0 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 g index */. Exp
62cc0 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b rList *pGroupBy;
62cd0 20 20 20 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 /* The grou
62ce0 70 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 p by clause */.
62cf0 20 69 6e 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c int nSortingCol
62d00 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 umn; /* Numb
62d10 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
62d20 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 the sorting ind
62d30 65 78 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 ex */. struct A
62d40 67 67 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 ggInfo_col {
62d50 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 /* For each colu
62d60 6d 6e 20 75 73 65 64 20 69 6e 20 73 6f 75 72 63 mn used in sourc
62d70 65 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 e tables */.
62d80 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 Table *pTab;
62d90 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 /* Sour
62da0 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 ce table */.
62db0 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 int iTable;
62dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 /* Curs
62dd0 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 or number of the
62de0 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f source table */
62df0 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e . int iColumn
62e00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
62e10 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 Column number w
62e20 69 74 68 69 6e 20 74 68 65 20 73 6f 75 72 63 65 ithin the source
62e30 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e table */. in
62e40 74 20 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b t iSorterColumn;
62e50 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e /* Column
62e60 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 73 number in the s
62e70 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a orting index */.
62e80 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 int iMem;
62e90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
62ea0 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 Memory location
62eb0 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 that acts as acc
62ec0 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 umulator */.
62ed0 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 Expr *pExpr;
62ee0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
62ef0 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65 73 73 original express
62f00 69 6f 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c ion */. } *aCol
62f10 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b ;. int nColumn;
62f20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
62f30 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20 65 6e umber of used en
62f40 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 tries in aCol[]
62f50 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e */. int nColumn
62f60 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 Alloc; /*
62f70 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 Number of slots
62f80 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 43 allocated for aC
62f90 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 ol[] */. int nA
62fa0 63 63 75 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 ccumulator;
62fb0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
62fc0 6f 6c 75 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 olumns that show
62fd0 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
62fe0 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20 20 20 output..
62ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63000 20 20 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 ** Additional
63010 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 columns are used
63020 20 6f 6e 6c 79 20 61 73 20 70 61 72 61 6d 65 74 only as paramet
63030 65 72 73 20 74 6f 0a 20 20 20 20 20 20 20 20 20 ers to.
63040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63050 20 2a 2a 20 61 67 67 72 65 67 61 74 65 20 66 75 ** aggregate fu
63060 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 nctions */. str
63070 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 uct AggInfo_func
63080 20 7b 20 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 { /* For each
63090 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
630a0 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 ion */. Expr
630b0 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 *pExpr;
630c0 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f /* Expressio
630d0 6e 20 65 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 n encoding the f
630e0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 unction */. F
630f0 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 uncDef *pFunc;
63100 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 /* The a
63110 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
63120 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
63130 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d */. int iMem
63140 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
63150 20 2f 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 /* Memory locat
63160 69 6f 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 ion that acts as
63170 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a accumulator */.
63180 20 20 20 20 69 6e 74 20 69 44 69 73 74 69 6e 63 int iDistinc
63190 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 t; /*
631a0 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 Ephemeral table
631b0 75 73 65 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 used to enforce
631c0 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 DISTINCT */. }
631d0 2a 61 46 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 *aFunc;. int nF
631e0 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 unc;
631f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
63200 6e 74 72 69 65 73 20 69 6e 20 61 46 75 6e 63 5b ntries in aFunc[
63210 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 ] */. int nFunc
63220 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f Alloc; /
63230 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 * Number of slot
63240 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
63250 61 46 75 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f aFunc[] */.};../
63260 2a 0a 2a 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f *.** Each node o
63270 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 f an expression
63280 69 6e 20 74 68 65 20 70 61 72 73 65 20 74 72 65 in the parse tre
63290 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 e is an instance
632a0 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 .** of this stru
632b0 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 cture..**.** Exp
632c0 72 2e 6f 70 20 69 73 20 74 68 65 20 6f 70 63 6f r.op is the opco
632d0 64 65 2e 20 54 68 65 20 69 6e 74 65 67 65 72 20 de. The integer
632e0 70 61 72 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 parser token cod
632f0 65 73 20 61 72 65 20 72 65 75 73 65 64 0a 2a 2a es are reused.**
63300 20 61 73 20 6f 70 63 6f 64 65 73 20 68 65 72 65 as opcodes here
63310 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 . For example, t
63320 68 65 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 he parser define
63330 73 20 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e s TK_GE to be an
63340 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 integer.** code
63350 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 representing th
63360 65 20 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e e ">=" operator.
63370 20 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 This same integ
63380 65 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 er code is reuse
63390 64 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e d.** to represen
633a0 74 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 t the greater-th
633b0 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f an-or-equal-to o
633c0 70 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 perator in the e
633d0 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 xpression.** tre
633e0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
633f0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e expression is an
63400 20 53 51 4c 20 6c 69 74 65 72 61 6c 20 28 54 4b SQL literal (TK
63410 5f 49 4e 54 45 47 45 52 2c 20 54 4b 5f 46 4c 4f _INTEGER, TK_FLO
63420 41 54 2c 20 54 4b 5f 42 4c 4f 42 2c 20 0a 2a 2a AT, TK_BLOB, .**
63430 20 6f 72 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20 or TK_STRING),
63440 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 then Expr.token
63450 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 contains the tex
63460 74 20 6f 66 20 74 68 65 20 53 51 4c 20 6c 69 74 t of the SQL lit
63470 65 72 61 6c 2e 20 49 66 0a 2a 2a 20 74 68 65 20 eral. If.** the
63480 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 expression is a
63490 76 61 72 69 61 62 6c 65 20 28 54 4b 5f 56 41 52 variable (TK_VAR
634a0 49 41 42 4c 45 29 2c 20 74 68 65 6e 20 45 78 70 IABLE), then Exp
634b0 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 r.token contains
634c0 20 74 68 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c the .** variabl
634d0 65 20 6e 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c e name. Finally,
634e0 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 if the expressi
634f0 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 66 75 6e on is an SQL fun
63500 63 74 69 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49 ction (TK_FUNCTI
63510 4f 4e 29 2c 0a 2a 2a 20 74 68 65 6e 20 45 78 70 ON),.** then Exp
63520 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 r.token contains
63530 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
63540 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
63550 20 45 78 70 72 2e 70 52 69 67 68 74 20 61 6e 64 Expr.pRight and
63560 20 45 78 70 72 2e 70 4c 65 66 74 20 61 72 65 20 Expr.pLeft are
63570 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67 the left and rig
63580 68 74 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e ht subexpression
63590 73 20 6f 66 20 61 0a 2a 2a 20 62 69 6e 61 72 79 s of a.** binary
635a0 20 6f 70 65 72 61 74 6f 72 2e 20 45 69 74 68 65 operator. Eithe
635b0 72 20 6f 72 20 62 6f 74 68 20 6d 61 79 20 62 65 r or both may be
635c0 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 NULL..**.** Exp
635d0 72 2e 78 2e 70 4c 69 73 74 20 69 73 20 61 20 6c r.x.pList is a l
635e0 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 ist of arguments
635f0 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 if the expressi
63600 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 66 75 6e on is an SQL fun
63610 63 74 69 6f 6e 2c 0a 2a 2a 20 61 20 43 41 53 45 ction,.** a CASE
63620 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 61 expression or a
63630 6e 20 49 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 n IN expression
63640 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 of the form "<lh
63650 73 3e 20 49 4e 20 28 3c 79 3e 2c 20 3c 7a 3e 2e s> IN (<y>, <z>.
63660 2e 2e 29 22 2e 0a 2a 2a 20 45 78 70 72 2e 78 2e ..)"..** Expr.x.
63670 70 53 65 6c 65 63 74 20 69 73 20 75 73 65 64 20 pSelect is used
63680 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f if the expressio
63690 6e 20 69 73 20 61 20 73 75 62 2d 73 65 6c 65 63 n is a sub-selec
636a0 74 20 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 t or an expressi
636b0 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 on of.** the for
636c0 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 53 45 4c m "<lhs> IN (SEL
636d0 45 43 54 20 2e 2e 2e 29 22 2e 20 49 66 20 74 68 ECT ...)". If th
636e0 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 62 e EP_xIsSelect b
636f0 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 it is set in the
63700 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67 73 20 6d .** Expr.flags m
63710 61 73 6b 2c 20 74 68 65 6e 20 45 78 70 72 2e 78 ask, then Expr.x
63720 2e 70 53 65 6c 65 63 74 20 69 73 20 76 61 6c 69 .pSelect is vali
63730 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 45 78 d. Otherwise, Ex
63740 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 20 0a 2a pr.x.pList is .*
63750 2a 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 41 * valid..**.** A
63760 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 n expression of
63770 74 68 65 20 66 6f 72 6d 20 49 44 20 6f 72 20 49 the form ID or I
63780 44 2e 49 44 20 72 65 66 65 72 73 20 74 6f 20 61 D.ID refers to a
63790 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 column in a tab
637a0 6c 65 2e 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20 le..** For such
637b0 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 45 78 70 expressions, Exp
637c0 72 2e 6f 70 20 69 73 20 73 65 74 20 74 6f 20 54 r.op is set to T
637d0 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70 K_COLUMN and Exp
637e0 72 2e 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 r.iTable is.** t
637f0 68 65 20 69 6e 74 65 67 65 72 20 63 75 72 73 6f he integer curso
63800 72 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 56 44 r number of a VD
63810 42 45 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 BE cursor pointi
63820 6e 67 20 74 6f 20 74 68 61 74 20 74 61 62 6c 65 ng to that table
63830 20 61 6e 64 0a 2a 2a 20 45 78 70 72 2e 69 43 6f and.** Expr.iCo
63840 6c 75 6d 6e 20 69 73 20 74 68 65 20 63 6f 6c 75 lumn is the colu
63850 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 mn number for th
63860 65 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d e specific colum
63870 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 65 78 n. If the.** ex
63880 70 72 65 73 73 69 6f 6e 20 69 73 20 75 73 65 64 pression is used
63890 20 61 73 20 61 20 72 65 73 75 6c 74 20 69 6e 20 as a result in
638a0 61 6e 20 61 67 67 72 65 67 61 74 65 20 53 45 4c an aggregate SEL
638b0 45 43 54 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a ECT, then the.**
638c0 20 76 61 6c 75 65 20 69 73 20 61 6c 73 6f 20 73 value is also s
638d0 74 6f 72 65 64 20 69 6e 20 74 68 65 20 45 78 70 tored in the Exp
638e0 72 2e 69 41 67 67 20 63 6f 6c 75 6d 6e 20 69 6e r.iAgg column in
638f0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 73 the aggregate s
63900 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e o that.** it can
63910 20 62 65 20 61 63 63 65 73 73 65 64 20 61 66 74 be accessed aft
63920 65 72 20 61 6c 6c 20 61 67 67 72 65 67 61 74 65 er all aggregate
63930 73 20 61 72 65 20 63 6f 6d 70 75 74 65 64 2e 0a s are computed..
63940 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 **.** If the exp
63950 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e ression is an un
63960 62 6f 75 6e 64 20 76 61 72 69 61 62 6c 65 20 6d bound variable m
63970 61 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f arker (a questio
63980 6e 20 6d 61 72 6b 20 0a 2a 2a 20 63 68 61 72 61 n mark .** chara
63990 63 74 65 72 20 27 3f 27 20 69 6e 20 74 68 65 20 cter '?' in the
639a0 6f 72 69 67 69 6e 61 6c 20 53 51 4c 29 20 74 68 original SQL) th
639b0 65 6e 20 74 68 65 20 45 78 70 72 2e 69 54 61 62 en the Expr.iTab
639c0 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 le holds the ind
639d0 65 78 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f ex .** number fo
639e0 72 20 74 68 61 74 20 76 61 72 69 61 62 6c 65 2e r that variable.
639f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 .**.** If the ex
63a00 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75 pression is a su
63a10 62 71 75 65 72 79 20 74 68 65 6e 20 45 78 70 72 bquery then Expr
63a20 2e 69 43 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61 .iColumn holds a
63a30 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67 n integer.** reg
63a40 69 73 74 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e ister number con
63a50 74 61 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 taining the resu
63a60 6c 74 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 lt of the subque
63a70 72 79 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 ry. If the.** s
63a80 75 62 71 75 65 72 79 20 67 69 76 65 73 20 61 20 ubquery gives a
63a90 63 6f 6e 73 74 61 6e 74 20 72 65 73 75 6c 74 2c constant result,
63aa0 20 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 then iTable is
63ab0 2d 31 2e 20 20 49 66 20 74 68 65 20 73 75 62 71 -1. If the subq
63ac0 75 65 72 79 0a 2a 2a 20 67 69 76 65 73 20 61 20 uery.** gives a
63ad0 64 69 66 66 65 72 65 6e 74 20 61 6e 73 77 65 72 different answer
63ae0 20 61 74 20 64 69 66 66 65 72 65 6e 74 20 74 69 at different ti
63af0 6d 65 73 20 64 75 72 69 6e 67 20 73 74 61 74 65 mes during state
63b00 6d 65 6e 74 20 70 72 6f 63 65 73 73 69 6e 67 0a ment processing.
63b10 2a 2a 20 74 68 65 6e 20 69 54 61 62 6c 65 20 69 ** then iTable i
63b20 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 s the address of
63b30 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 a subroutine th
63b40 61 74 20 63 6f 6d 70 75 74 65 73 20 74 68 65 20 at computes the
63b50 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 subquery..**.**
63b60 49 66 20 74 68 65 20 45 78 70 72 20 69 73 20 6f If the Expr is o
63b70 66 20 74 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e f type OP_Column
63b80 2c 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 , and the table
63b90 69 74 20 69 73 20 73 65 6c 65 63 74 69 6e 67 20 it is selecting
63ba0 66 72 6f 6d 0a 2a 2a 20 69 73 20 61 20 64 69 73 from.** is a dis
63bb0 6b 20 74 61 62 6c 65 20 6f 72 20 74 68 65 20 22 k table or the "
63bc0 6f 6c 64 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61 old.*" pseudo-ta
63bd0 62 6c 65 2c 20 74 68 65 6e 20 70 54 61 62 20 70 ble, then pTab p
63be0 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 oints to the.**
63bf0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 corresponding ta
63c00 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a ble definition..
63c10 2a 2a 0a 2a 2a 20 41 4c 4c 4f 43 41 54 49 4f 4e **.** ALLOCATION
63c20 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 45 78 NOTES:.**.** Ex
63c30 70 72 20 6f 62 6a 65 63 74 73 20 63 61 6e 20 75 pr objects can u
63c40 73 65 20 61 20 6c 6f 74 20 6f 66 20 6d 65 6d 6f se a lot of memo
63c50 72 79 20 73 70 61 63 65 20 69 6e 20 64 61 74 61 ry space in data
63c60 62 61 73 65 20 73 63 68 65 6d 61 2e 20 20 54 6f base schema. To
63c70 0a 2a 2a 20 68 65 6c 70 20 72 65 64 75 63 65 20 .** help reduce
63c80 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 6d 65 memory requireme
63c90 6e 74 73 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 nts, sometimes a
63ca0 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20 77 69 n Expr object wi
63cb0 6c 6c 20 62 65 0a 2a 2a 20 74 72 75 6e 63 61 74 ll be.** truncat
63cc0 65 64 2e 20 20 41 6e 64 20 74 6f 20 72 65 64 75 ed. And to redu
63cd0 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ce the number of
63ce0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
63cf0 6f 6e 73 2c 20 73 6f 6d 65 74 69 6d 65 73 0a 2a ons, sometimes.*
63d00 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 45 78 * two or more Ex
63d10 70 72 20 6f 62 6a 65 63 74 73 20 77 69 6c 6c 20 pr objects will
63d20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73 be stored in a s
63d30 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c ingle memory all
63d40 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 6f 67 65 ocation,.** toge
63d50 74 68 65 72 20 77 69 74 68 20 45 78 70 72 2e 7a ther with Expr.z
63d60 54 6f 6b 65 6e 20 73 74 72 69 6e 67 73 2e 0a 2a Token strings..*
63d70 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 50 5f 52 *.** If the EP_R
63d80 65 64 75 63 65 64 20 61 6e 64 20 45 50 5f 54 6f educed and EP_To
63d90 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 73 20 61 72 kenOnly flags ar
63da0 65 20 73 65 74 20 77 68 65 6e 0a 2a 2a 20 61 6e e set when.** an
63db0 20 45 78 70 72 20 6f 62 6a 65 63 74 20 69 73 20 Expr object is
63dc0 74 72 75 6e 63 61 74 65 64 2e 20 20 57 68 65 6e truncated. When
63dd0 20 45 50 5f 52 65 64 75 63 65 64 20 69 73 20 73 EP_Reduced is s
63de0 65 74 2c 20 74 68 65 6e 20 61 6c 6c 0a 2a 2a 20 et, then all.**
63df0 74 68 65 20 63 68 69 6c 64 20 45 78 70 72 20 6f the child Expr o
63e00 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 45 78 bjects in the Ex
63e10 70 72 2e 70 4c 65 66 74 20 61 6e 64 20 45 78 70 pr.pLeft and Exp
63e20 72 2e 70 52 69 67 68 74 20 73 75 62 74 72 65 65 r.pRight subtree
63e30 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 61 69 6e s.** are contain
63e40 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 ed within the sa
63e50 6d 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 me memory alloca
63e60 74 69 6f 6e 2e 20 20 4e 6f 74 65 2c 20 68 6f 77 tion. Note, how
63e70 65 76 65 72 2c 20 74 68 61 74 0a 2a 2a 20 74 68 ever, that.** th
63e80 65 20 73 75 62 74 72 65 65 73 20 69 6e 20 45 78 e subtrees in Ex
63e90 70 72 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 pr.x.pList or Ex
63ea0 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 61 72 65 pr.x.pSelect are
63eb0 20 61 6c 77 61 79 73 20 73 65 70 61 72 61 74 65 always separate
63ec0 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c ly.** allocated,
63ed0 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 regardless of w
63ee0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 45 50 hether or not EP
63ef0 5f 52 65 64 75 63 65 64 20 69 73 20 73 65 74 2e _Reduced is set.
63f00 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 20 .*/.struct Expr
63f10 7b 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 {. u8 op;
63f20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
63f30 65 72 61 74 69 6f 6e 20 70 65 72 66 6f 72 6d 65 eration performe
63f40 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65 20 2a d by this node *
63f50 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 /. char affinit
63f60 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 y; /* Th
63f70 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 e affinity of th
63f80 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 69 66 e column or 0 if
63f90 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f not a column */
63fa0 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 . u16 flags;
63fb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72 /* Var
63fc0 69 6f 75 73 20 66 6c 61 67 73 2e 20 20 45 50 5f ious flags. EP_
63fd0 2a 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 * See below */.
63fe0 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 63 68 61 union {. cha
63ff0 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 r *zToken;
64000 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 76 61 6c /* Token val
64010 75 65 2e 20 5a 65 72 6f 20 74 65 72 6d 69 6e 61 ue. Zero termina
64020 74 65 64 20 61 6e 64 20 64 65 71 75 6f 74 65 64 ted and dequoted
64030 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 56 61 6c */. int iVal
64040 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ue; /
64050 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 * Integer value
64060 69 66 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 2a if EP_IntValue *
64070 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 49 /. } u;.. /* I
64080 66 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e f the EP_TokenOn
64090 6c 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 ly flag is set i
640a0 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 n the Expr.flags
640b0 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 mask, then no.
640c0 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 6c 6c ** space is all
640d0 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 66 ocated for the f
640e0 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 69 73 ields below this
640f0 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d point. An attem
64100 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73 pt to. ** acces
64110 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 73 75 s them will resu
64120 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 6c 74 lt in a segfault
64130 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e or malfunction.
64140 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . ************
64150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 45 78 *********/.. Ex
64190 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 pr *pLeft;
641a0 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 /* Left sub
641b0 6e 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a node */. Expr *
641c0 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 pRight;
641d0 20 2f 2a 20 52 69 67 68 74 20 73 75 62 6e 6f 64 /* Right subnod
641e0 65 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 e */. union {.
641f0 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 ExprList *pLi
64200 73 74 3b 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 st; /* Funct
64210 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 ion arguments or
64220 20 69 6e 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 in "<expr> IN (
64230 3c 65 78 70 72 2d 6c 69 73 74 29 22 20 2a 2f 0a <expr-list)" */.
64240 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c Select *pSel
64250 65 63 74 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 ect; /* Used
64260 20 66 6f 72 20 73 75 62 2d 73 65 6c 65 63 74 73 for sub-selects
64270 20 61 6e 64 20 22 3c 65 78 70 72 3e 20 49 4e 20 and "<expr> IN
64280 28 3c 73 65 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 (<select>)" */.
64290 20 7d 20 78 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 } x;. CollSeq
642a0 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f *pColl; /
642b0 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 * The collation
642c0 74 79 70 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 type of the colu
642d0 6d 6e 20 6f 72 20 30 20 2a 2f 0a 0a 20 20 2f 2a mn or 0 */.. /*
642e0 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 75 63 If the EP_Reduc
642f0 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 ed flag is set i
64300 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 n the Expr.flags
64310 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 mask, then no.
64320 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 6c 6c ** space is all
64330 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 66 ocated for the f
64340 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 69 73 ields below this
64350 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d point. An attem
64360 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73 pt to. ** acces
64370 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 73 75 s them will resu
64380 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 6c 74 lt in a segfault
64390 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e or malfunction.
643a0 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a . *************
643b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
643c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
643d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
643e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 69 6e 74 ********/.. int
643f0 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 iTable;
64400 20 20 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e /* TK_COLUMN
64410 3a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 : cursor number
64420 6f 66 20 74 61 62 6c 65 20 68 6f 6c 64 69 6e 67 of table holding
64430 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 column.
64440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64450 20 2a 2a 20 54 4b 5f 52 45 47 49 53 54 45 52 3a ** TK_REGISTER:
64460 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 register number
64470 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
64480 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f ** TK_
64490 54 52 49 47 47 45 52 3a 20 31 20 2d 3e 20 6e 65 TRIGGER: 1 -> ne
644a0 77 2c 20 30 20 2d 3e 20 6f 6c 64 20 2a 2f 0a 20 w, 0 -> old */.
644b0 20 69 31 36 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 i16 iColumn;
644c0 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 43 4f /* TK_CO
644d0 4c 55 4d 4e 3a 20 63 6f 6c 75 6d 6e 20 69 6e 64 LUMN: column ind
644e0 65 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f 77 69 ex. -1 for rowi
644f0 64 20 2a 2f 0a 20 20 69 31 36 20 69 41 67 67 3b d */. i16 iAgg;
64500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
64510 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20 Which entry in
64520 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d pAggInfo->aCol[]
64530 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f or ->aFunc[] */
64540 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a 6f 69 . i16 iRightJoi
64550 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 nTable; /* If
64560 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 EP_FromJoin, the
64570 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 right table of
64580 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 75 38 the join */. u8
64590 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20 20 20 flags2;
645a0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 73 /* Second s
645b0 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20 45 50 et of flags. EP
645c0 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20 6f 70 2_... */. u8 op
645d0 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2;
645e0 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f 52 45 47 /* If a TK_REG
645f0 49 53 54 45 52 2c 20 74 68 65 20 6f 72 69 67 69 ISTER, the origi
64600 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 45 78 70 nal value of Exp
64610 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 49 6e 66 r.op */. AggInf
64620 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 20 o *pAggInfo;
64630 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b 5f 41 /* Used by TK_A
64640 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 4b GG_COLUMN and TK
64650 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a 2f _AGG_FUNCTION */
64660 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 . Table *pTab;
64670 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 /* Tab
64680 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e le for TK_COLUMN
64690 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 2a 2f expressions. */
646a0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
646b0 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 69 EXPR_DEPTH>0. i
646c0 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20 20 nt nHeight;
646d0 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 /* Height
646e0 6f 66 20 74 68 65 20 74 72 65 65 20 68 65 61 64 of the tree head
646f0 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65 20 ed by this node
64700 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a */.#endif.};../*
64710 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
64720 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 6e g are the meanin
64730 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 gs of bits in th
64740 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65 e Expr.flags fie
64750 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ld..*/.#define E
64760 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 30 P_FromJoin 0x0
64770 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74 001 /* Originat
64780 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e ed in ON or USIN
64790 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f G clause of a jo
647a0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 in */.#define EP
647b0 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 30 30 _Agg 0x00
647c0 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 02 /* Contains
647d0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 67 72 one or more aggr
647e0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
647f0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 65 */.#define EP_Re
64800 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 34 20 solved 0x0004
64810 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 65 65 /* IDs have bee
64820 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f n resolved to CO
64830 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 LUMNs */.#define
64840 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 20 30 EP_Error 0
64850 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 65 73 x0008 /* Expres
64860 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e sion contains on
64870 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f 72 73 e or more errors
64880 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 */.#define EP_D
64890 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 31 30 istinct 0x0010
648a0 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 /* Aggregate f
648b0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 49 53 unction with DIS
648c0 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f TINCT keyword */
648d0 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 72 53 .#define EP_VarS
648e0 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 20 2f elect 0x0020 /
648f0 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 6f 72 * pSelect is cor
64900 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e related, not con
64910 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 stant */.#define
64920 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 20 30 EP_DblQuoted 0
64930 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e x0040 /* token.
64940 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 z was originally
64950 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65 in "..." */.#de
64960 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e fine EP_InfixFun
64970 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 c 0x0080 /* Tr
64980 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 ue for an infix
64990 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 function: LIKE,
649a0 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 GLOB, etc */.#de
649b0 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 fine EP_ExpColla
649c0 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f te 0x0100 /* Co
649d0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
649e0 20 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 specified expli
649f0 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 citly */.#define
64a00 20 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 EP_AnyAff 0
64a10 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 x0200 /* Can ta
64a20 6b 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 ke a cached colu
64a30 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 mn of any affini
64a40 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 ty */.#define EP
64a50 5f 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 _FixedDest 0x04
64a60 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 00 /* Result ne
64a70 65 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 eded in a specif
64a80 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 ic register */.#
64a90 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c define EP_IntVal
64aa0 75 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20 ue 0x0800 /*
64ab0 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f Integer value co
64ac0 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69 56 61 ntained in u.iVa
64ad0 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 lue */.#define E
64ae0 50 5f 78 49 73 53 65 6c 65 63 74 20 20 30 78 31 P_xIsSelect 0x1
64af0 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 6c 65 63 000 /* x.pSelec
64b00 74 20 69 73 20 76 61 6c 69 64 20 28 6f 74 68 65 t is valid (othe
64b10 72 77 69 73 65 20 78 2e 70 4c 69 73 74 20 69 73 rwise x.pList is
64b20 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 45 50 ) */..#define EP
64b30 5f 52 65 64 75 63 65 64 20 20 20 20 30 78 32 30 _Reduced 0x20
64b40 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 00 /* Expr stru
64b50 63 74 20 69 73 20 45 58 50 52 5f 52 45 44 55 43 ct is EXPR_REDUC
64b60 45 44 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c EDSIZE bytes onl
64b70 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f y */.#define EP_
64b80 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 30 30 TokenOnly 0x400
64b90 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63 0 /* Expr struc
64ba0 74 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 4e 4f t is EXPR_TOKENO
64bb0 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 20 6f 6e NLYSIZE bytes on
64bc0 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 ly */.#define EP
64bd0 5f 53 74 61 74 69 63 20 20 20 20 20 30 78 38 30 _Static 0x80
64be0 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 20 6d 00 /* Held in m
64bf0 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 69 6e emory not obtain
64c00 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ed from malloc()
64c10 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 */../*.** The f
64c20 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 ollowing are the
64c30 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 meanings of bit
64c40 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c s in the Expr.fl
64c50 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 ags2 field..*/.#
64c60 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f define EP2_Mallo
64c70 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 30 31 cedToken 0x0001
64c80 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 71 6c /* Need to sql
64c90 69 74 65 33 44 62 46 72 65 65 28 29 20 45 78 70 ite3DbFree() Exp
64ca0 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 66 r.zToken */.#def
64cb0 69 6e 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 ine EP2_Irreduci
64cc0 62 6c 65 20 20 20 20 30 78 30 30 30 32 20 20 2f ble 0x0002 /
64cd0 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 55 50 * Cannot EXPRDUP
64ce0 5f 52 45 44 55 43 45 20 74 68 69 73 20 45 78 70 _REDUCE this Exp
64cf0 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 r */../*.** The
64d00 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73 pseudo-routine s
64d10 71 6c 69 74 65 33 45 78 70 72 53 65 74 49 72 72 qlite3ExprSetIrr
64d20 65 64 75 63 69 62 6c 65 20 73 65 74 73 20 74 68 educible sets th
64d30 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c e EP2_Irreducibl
64d40 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e 20 e.** flag on an
64d50 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 expression struc
64d60 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61 67 ture. This flag
64d70 20 69 73 20 75 73 65 64 20 66 6f 72 20 56 56 26 is used for VV&
64d80 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 A only. The.**
64d90 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 6c 65 routine is imple
64da0 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63 72 mented as a macr
64db0 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f 72 6b o that only work
64dc0 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75 67 67 s when in debugg
64dd0 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f 20 ing mode,.** so
64de0 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 65 6e as not to burden
64df0 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f 64 65 production code
64e00 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
64e10 54 45 5f 44 45 42 55 47 0a 23 20 64 65 66 69 6e TE_DEBUG.# defin
64e20 65 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 e ExprSetIrreduc
64e30 69 62 6c 65 28 58 29 20 20 28 58 29 2d 3e 66 6c ible(X) (X)->fl
64e40 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 72 65 ags2 |= EP2_Irre
64e50 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a 23 20 ducible.#else.#
64e60 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49 72 define ExprSetIr
64e70 72 65 64 75 63 69 62 6c 65 28 58 29 0a 23 65 6e reducible(X).#en
64e80 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 dif../*.** These
64e90 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 macros can be u
64ea0 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 sed to test, set
64eb0 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 , or clear bits
64ec0 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e in the .** Expr.
64ed0 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a flags field..*/.
64ee0 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 #define ExprHasP
64ef0 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 roperty(E,P)
64f00 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 (((E)->flags&(P
64f10 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 ))==(P)).#define
64f20 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 ExprHasAnyPrope
64f30 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d rty(E,P) (((E)-
64f40 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a >flags&(P))!=0).
64f50 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 #define ExprSetP
64f60 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 roperty(E,P)
64f70 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 (E)->flags|=(P)
64f80 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 .#define ExprCle
64f90 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 arProperty(E,P)
64fa0 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 (E)->flags&=~(
64fb0 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 P)../*.** Macros
64fc0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
64fd0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
64fe0 73 20 72 65 71 75 69 72 65 64 20 62 79 20 61 20 s required by a
64ff0 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20 normal Expr .**
65000 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70 72 20 struct, an Expr
65010 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20 struct with the
65020 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 EP_Reduced flag
65030 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 set in Expr.flag
65040 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70 s .** and an Exp
65050 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68 r struct with th
65060 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 e EP_TokenOnly f
65070 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 lag set..*/.#def
65080 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a ine EXPR_FULLSIZ
65090 45 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 E size
650a0 6f 66 28 45 78 70 72 29 20 20 20 20 20 20 20 20 of(Expr)
650b0 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20 /* Full size
650c0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f */.#define EXPR_
650d0 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 20 20 REDUCEDSIZE
650e0 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 offsetof(Expr
650f0 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d ,iTable) /* Com
65100 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a 2f 0a mon features */.
65110 23 64 65 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b #define EXPR_TOK
65120 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 20 ENONLYSIZE
65130 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 70 4c offsetof(Expr,pL
65140 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65 72 20 eft) /* Fewer
65150 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a 0a features */../*.
65160 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 ** Flags passed
65170 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45 78 to the sqlite3Ex
65180 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f 6e prDup() function
65190 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 . See the header
651a0 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f comment .** abo
651b0 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 ve sqlite3ExprDu
651c0 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e p() for details.
651d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 .*/.#define EXPR
651e0 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20 20 DUP_REDUCE
651f0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55 73 0x0001 /* Us
65200 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65 20 ed reduced-size
65210 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a 2f Expr nodes */../
65220 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 65 *.** A list of e
65230 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61 63 xpressions. Eac
65240 68 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 h expression may
65250 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65 optionally have
65260 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20 a.** name. An
65270 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e expr/name combin
65280 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 ation can be use
65290 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 d in several way
652a0 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74 68 s, such.** as th
652b0 65 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72 20 e list of "expr
652c0 41 53 20 49 44 22 20 66 69 65 6c 64 73 20 66 6f AS ID" fields fo
652d0 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45 43 llowing a "SELEC
652e0 54 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 T" or in the.**
652f0 6c 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65 78 list of "ID = ex
65300 70 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e 20 pr" items in an
65310 55 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74 20 UPDATE. A list
65320 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 of expressions c
65330 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 an.** also be us
65340 65 64 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 ed as the argume
65350 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e nt to a function
65360 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 , in which case
65370 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66 the a.zName.** f
65380 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65 64 ield is not used
65390 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 ..*/.struct Expr
653a0 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 List {. int nEx
653b0 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pr;
653c0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70 /* Number of exp
653d0 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 ressions on the
653e0 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 list */. int nA
653f0 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 lloc;
65400 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
65410 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 tries allocated
65420 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 below */. int i
65430 45 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 ECursor;
65440 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 /* VDBE Cursor
65450 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
65460 20 74 68 69 73 20 45 78 70 72 4c 69 73 74 20 2a this ExprList *
65470 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c /. struct ExprL
65480 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 45 ist_item {. E
65490 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 xpr *pExpr;
654a0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 /* The lis
654b0 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 t of expressions
654c0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e */. char *zN
654d0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f ame; /
654e0 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 61 74 * Token associat
654f0 65 64 20 77 69 74 68 20 74 68 69 73 20 65 78 70 ed with this exp
65500 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 ression */. c
65510 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20 20 20 har *zSpan;
65520 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 /* Origina
65530 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 65 78 l text of the ex
65540 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 pression */.
65550 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20 20 20 u8 sortOrder;
65560 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 /* 1 for
65570 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20 41 53 DESC or 0 for AS
65580 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f 6e 65 C */. u8 done
65590 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
655a0 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 6e 64 /* A flag to ind
655b0 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f 63 65 icate when proce
655c0 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 65 ssing is finishe
655d0 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 43 6f d */. u16 iCo
655e0 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
655f0 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42 59 2c /* For ORDER BY,
65600 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 column number i
65610 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a n result set */.
65620 20 20 20 20 75 31 36 20 69 41 6c 69 61 73 3b 20 u16 iAlias;
65630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
65640 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65 2e 61 dex into Parse.a
65650 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e 61 6d Alias[] for zNam
65660 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20 20 20 e */. } *a;
65670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
65680 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 One entry for e
65690 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 2a ach expression *
656a0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 /.};../*.** An i
656b0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
656c0 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 structure is use
656d0 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 d by the parser
656e0 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 68 0a 2a to record both.*
656f0 2a 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 * the parse tree
65700 20 66 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 for an expressi
65710 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 61 6e 20 on and the span
65720 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 66 6f of input text fo
65730 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 r an.** expressi
65740 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 on..*/.struct Ex
65750 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 70 72 20 prSpan {. Expr
65760 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 *pExpr;
65770 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 /* The expressi
65780 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20 2a 2f on parse tree */
65790 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
657a0 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69 72 73 Start; /* Firs
657b0 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 69 t character of i
657c0 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 20 63 nput text */. c
657d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 3b onst char *zEnd;
657e0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68 61 72 /* One char
657f0 61 63 74 65 72 20 70 61 73 74 20 74 68 65 20 65 acter past the e
65800 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 nd of input text
65810 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
65820 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
65830 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 s structure can
65840 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c 69 hold a simple li
65850 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 st of identifier
65860 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 68 s,.** such as th
65870 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 69 e list "a,b,c" i
65880 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
65890 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a statements:.**.*
658a0 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e * INSERT IN
658b0 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c 55 TO t(a,b,c) VALU
658c0 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20 ES ...;.**
658d0 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 78 CREATE INDEX idx
658e0 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a ON t(a,b,c);.**
658f0 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49 CREATE TRI
65900 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 45 GGER trig BEFORE
65910 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c 62 UPDATE ON t(a,b
65920 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 ,c) ...;.**.** T
65930 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 20 he IdList.a.idx
65940 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 68 field is used wh
65950 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 65 en the IdList re
65960 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 73 presents the lis
65970 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e t of.** column n
65980 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 62 ames after a tab
65990 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e le name in an IN
659a0 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 SERT statement.
659b0 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e In the statemen
659c0 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 t.**.** INSE
659d0 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 RT INTO t(a,b,c)
659e0 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61 ....**.** If "a
659f0 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 6f " is the k-th co
65a00 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 74 lumn of table "t
65a10 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e 61 ", then IdList.a
65a20 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73 [0].idx==k..*/.s
65a30 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a 20 truct IdList {.
65a40 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 struct IdList_i
65a50 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a tem {. char *
65a60 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e zName; /* N
65a70 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e 74 ame of the ident
65a80 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 ifier */. int
65a90 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f idx; /
65aa0 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20 * Index in some
65ab0 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20 Table.aCol[] of
65ac0 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a a column named z
65ad0 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a Name */. } *a;.
65ae0 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 int nId;
65af0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
65b00 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 74 identifiers on t
65b10 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 he list */. int
65b20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a nAlloc; /*
65b30 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
65b40 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 es allocated for
65b50 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b a[] below */.};
65b60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d ../*.** The bitm
65b70 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 66 ask datatype def
65b80 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 73 ined below is us
65b90 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 6f ed for various o
65ba0 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a ptimizations..**
65bb0 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 69 .** Changing thi
65bc0 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 20 s from a 64-bit
65bd0 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 65 to a 32-bit type
65be0 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d 62 limits the numb
65bf0 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 er of.** tables
65c00 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20 in a join to 32
65c10 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 20 instead of 64.
65c20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 75 But it also redu
65c30 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 ces the size.**
65c40 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 62 of the library b
65c50 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 69 y 738 bytes on i
65c60 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 x86..*/.typedef
65c70 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a u64 Bitmask;../*
65c80 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f .** The number o
65c90 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d f bits in a Bitm
65ca0 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e ask. "BMS" mean
65cb0 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 22 s "BitMask Size"
65cc0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53 ..*/.#define BMS
65cd0 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 ((int)(sizeof(
65ce0 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f 2a Bitmask)*8))../*
65cf0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
65d00 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 g structure desc
65d10 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 63 ribes the FROM c
65d20 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 lause of a SELEC
65d30 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 T statement..**
65d40 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 75 Each table or su
65d50 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52 bquery in the FR
65d60 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73 OM clause is a s
65d70 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 20 eparate element
65d80 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 73 of.** the SrcLis
65d90 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a t.a[] array..**.
65da0 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 69 ** With the addi
65db0 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65 tion of multiple
65dc0 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 database suppor
65dd0 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t, the following
65de0 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 61 structure.** ca
65df0 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 n also be used t
65e00 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72 o describe a par
65e10 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 75 ticular table su
65e20 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 ch as the table
65e30 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66 that.** is modif
65e40 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54 ied by an INSERT
65e50 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 , DELETE, or UPD
65e60 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ATE statement.
65e70 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c 2c In standard SQL,
65e80 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c 65 .** such a table
65e90 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 6c must be a simpl
65ea0 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 74 e name: ID. But
65eb0 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 20 in SQLite, the
65ec0 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77 table can.** now
65ed0 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 be identified b
65ee0 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 6d y a database nam
65ef0 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 74 e, a dot, then t
65f00 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49 he table name: I
65f10 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 D.ID..**.** The
65f20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 20 jointype starts
65f30 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 20 out showing the
65f40 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 65 join type betwee
65f50 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 n the current ta
65f60 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e ble.** and the n
65f70 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 ext table on the
65f80 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 73 list. The pars
65f90 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c 69 er builds the li
65fa0 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20 st this way..**
65fb0 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c 69 But sqlite3SrcLi
65fc0 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 stShiftJoinType(
65fd0 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 74 ) later shifts t
65fe0 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20 he jointypes so
65ff0 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69 that each.** joi
66000 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 20 ntype expresses
66010 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e the join between
66020 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 the table and t
66030 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 6c he previous tabl
66040 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 63 e..*/.struct Src
66050 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 72 List {. i16 nSr
66060 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d c; /* Num
66070 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f 72 ber of tables or
66080 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 subqueries in t
66090 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a he FROM clause *
660a0 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20 /. i16 nAlloc;
660b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
660c0 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 f entries alloca
660d0 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77 ted in a[] below
660e0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 */. struct Src
660f0 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 List_item {.
66100 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b char *zDatabase;
66110 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 /* Name of dat
66120 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 abase holding th
66130 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 is table */.
66140 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
66150 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
66160 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68 table */. ch
66170 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20 ar *zAlias;
66180 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 20 /* The "B" part
66190 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 68 of a "A AS B" ph
661a0 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20 rase. zName is
661b0 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 54 the "A" */. T
661c0 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 able *pTab;
661d0 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c 65 /* An SQL table
661e0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
661f0 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53 o zName */. S
66200 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 elect *pSelect;
66210 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 /* A SELECT sta
66220 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 70 tement used in p
66230 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20 lace of a table
66240 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 69 name */. u8 i
66250 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a sPopulated; /*
66260 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 Temporary table
66270 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
66280 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 6c SELECT is popul
66290 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a ated */. u8 j
662a0 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f 2a ointype; /*
662b0 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65 Type of join be
662c0 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 20 tween this able
662d0 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 and the previous
662e0 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49 6e */. u8 notIn
662f0 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 dexed; /* Tru
66300 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 e if there is a
66310 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 NOT INDEXED clau
66320 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 se */. int iC
66330 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 ursor; /* T
66340 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e he VDBE cursor n
66350 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63 umber used to ac
66360 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20 cess this table
66370 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e */. Expr *pOn
66380 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
66390 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a ON clause of a j
663a0 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 oin */. IdLis
663b0 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 t *pUsing; /*
663c0 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 The USING clause
663d0 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 of a join */.
663e0 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 Bitmask colUse
663f0 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c d; /* Bit N (1<
66400 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d <N) set if colum
66410 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20 75 n N of pTab is u
66420 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 sed */. char
66430 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20 *zIndex; /*
66440 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d 20 Identifier from
66450 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 6e "INDEXED BY <zIn
66460 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f 0a dex>" clause */.
66470 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 Index *pInde
66480 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 x; /* Index s
66490 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 70 tructure corresp
664a0 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 78 onding to zIndex
664b0 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d 20 , if any */. }
664c0 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 a[1];
664d0 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 /* One entry f
664e0 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 69 or each identifi
664f0 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a er on the list *
66500 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d /.};../*.** Perm
66510 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 20 itted values of
66520 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f the SrcList.a.jo
66530 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a intype field.*/.
66540 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52 #define JT_INNER
66550 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 2f 0x0001 /
66560 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e * Any kind of in
66570 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69 ner or cross joi
66580 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f n */.#define JT_
66590 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 32 CROSS 0x0002
665a0 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 20 /* Explicit
665b0 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 53 use of the CROSS
665c0 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 keyword */.#def
665d0 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 ine JT_NATURAL
665e0 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 72 0x0004 /* Tr
665f0 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 61 ue for a "natura
66600 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 l" join */.#defi
66610 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 20 ne JT_LEFT
66620 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 66 0x0008 /* Lef
66630 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a t outer join */.
66640 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 54 #define JT_RIGHT
66650 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 2f 0x0010 /
66660 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a 6f * Right outer jo
66670 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
66680 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 32 _OUTER 0x002
66690 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 54 0 /* The "OUT
666a0 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 70 ER" keyword is p
666b0 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e resent */.#defin
666c0 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 30 e JT_ERROR 0
666d0 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e x0040 /* unkn
666e0 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 own or unsupport
666f0 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a ed join type */.
66700 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 50 ../*.** A WhereP
66710 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 lan object holds
66720 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 information tha
66730 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c 6f t describes a lo
66740 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67 79 okup.** strategy
66750 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a ..**.** This obj
66760 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ect is intended
66770 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75 74 to be opaque out
66780 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65 72 side of the wher
66790 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 49 e.c module..** I
667a0 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68 65 t is included he
667b0 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74 20 re only so that
667c0 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77 69 that compiler wi
667d0 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20 ll know how big
667e0 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 20 it.** is. None
667f0 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69 6e of the fields in
66800 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68 6f this object sho
66810 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74 73 uld be used outs
66820 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77 68 ide of.** the wh
66830 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a ere.c module..**
66840 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 75 .** Within the u
66850 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e nion, pIdx is on
66860 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 ly used when wsF
66870 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 58 lags&WHERE_INDEX
66880 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 70 ED is true..** p
66890 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65 Term is only use
668a0 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57 d when wsFlags&W
668b0 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73 HERE_MULTI_OR is
668c0 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 61 true. And pVta
668d0 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 bIdx.** is only
668e0 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 used when wsFlag
668f0 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 s&WHERE_VIRTUALT
66900 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20 49 ABLE is true. I
66910 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a 2a t is never the.*
66920 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72 65 * case that more
66930 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 than one of the
66940 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 se conditions is
66950 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 true..*/.struct
66960 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20 75 WherePlan {. u
66970 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20 20 32 wsFlags;
66980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
66990 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 74 WHERE_* flags t
669a0 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65 hat describe the
669b0 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 75 strategy */. u
669c0 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20 32 nEq;
669d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
669e0 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f Number of == co
669f0 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 75 nstraints */. u
66a00 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65 78 nion {. Index
66a10 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 *pIdx;
66a20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
66a30 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49 4e ex when WHERE_IN
66a40 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a 2f DEXED is true */
66a50 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65 72 . struct Wher
66a60 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 eTerm *pTerm;
66a70 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 /* WHERE cla
66a80 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52 2d use term for OR-
66a90 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73 71 search */. sq
66aa0 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f lite3_index_info
66ab0 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a 20 *pVtabIdx; /*
66ac0 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e Virtual table in
66ad0 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 dex to use */.
66ae0 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 } u;.};../*.** F
66af0 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20 6c or each nested l
66b00 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20 63 oop in a WHERE c
66b10 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74 61 lause implementa
66b20 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65 49 tion, the WhereI
66b30 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 nfo.** structure
66b40 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 contains a sing
66b50 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 le instance of t
66b60 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 his structure.
66b70 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a This structure.*
66b80 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f * is intended to
66b90 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65 20 be private the
66ba0 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 the where.c modu
66bb0 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f le and should no
66bc0 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20 6f t be.** access o
66bd0 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f 74 r modified by ot
66be0 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a her modules..**.
66bf0 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 ** The pIdxInfo
66c00 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f field is used to
66c10 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 62 help pick the b
66c20 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a 2a est index on a.*
66c30 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e * virtual table.
66c40 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 70 The pIdxInfo p
66c50 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 ointer contains
66c60 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f indexing.** info
66c70 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 rmation for the
66c80 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 68 i-th table in th
66c90 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 65 e FROM clause be
66ca0 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 2e fore reordering.
66cb0 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 78 .** All the pIdx
66cc0 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 72 Info pointers ar
66cd0 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 65 e freed by where
66ce0 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 68 InfoFree() in wh
66cf0 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 ere.c..** All ot
66d00 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 her information
66d10 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65 72 in the i-th Wher
66d20 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66 6f eLevel object fo
66d30 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 r the i-th table
66d40 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20 63 .** after FROM c
66d50 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 0a lause ordering..
66d60 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4c */.struct WhereL
66d70 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50 6c evel {. WherePl
66d80 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20 2f an plan; /
66d90 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 * query plan for
66da0 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f 66 this element of
66db0 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
66dc0 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 4a */. int iLeftJ
66dd0 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d oin; /* M
66de0 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 emory cell used
66df0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 46 to implement LEF
66e00 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f 0a T OUTER JOIN */.
66e10 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 20 int iTabCur;
66e20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
66e30 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 DBE cursor used
66e40 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 61 to access the ta
66e50 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 ble */. int iId
66e60 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f xCur; /
66e70 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f * The VDBE curso
66e80 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 r used to access
66e90 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 61 pIdx */. int a
66ea0 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 20 ddrBrk;
66eb0 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f /* Jump here to
66ec0 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 break out of th
66ed0 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 e loop */. int
66ee0 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 20 addrNxt;
66ef0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
66f00 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 74 o start the next
66f10 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 IN combination
66f20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e */. int addrCon
66f30 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 t; /* Ju
66f40 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 mp here to conti
66f50 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 nue with the nex
66f60 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a t loop cycle */.
66f70 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 3b int addrFirst;
66f80 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
66f90 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 instruction of
66fa0 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 20 interior of the
66fb0 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46 72 loop */. u8 iFr
66fc0 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 om;
66fd0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 /* Which entry i
66fe0 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
66ff0 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70 35 e */. u8 op, p5
67000 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
67010 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f 66 Opcode and P5 of
67020 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74 the opcode that
67030 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a ends the loop *
67040 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b 20 /. int p1, p2;
67050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
67060 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70 63 rands of the opc
67070 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64 73 ode used to ends
67080 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 the loop */. u
67090 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 nion {
670a0 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 /* Informat
670b0 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64 73 ion that depends
670c0 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 on plan.wsFlags
670d0 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 7b */. struct {
670e0 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20 . int nIn;
670f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
67100 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
67110 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a s in aInLoop[] *
67120 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49 /. struct I
67130 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 20 nLoop {.
67140 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 int iCur;
67150 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 /* The VD
67160 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 62 BE cursor used b
67170 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 74 y this IN operat
67180 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e or */. in
67190 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 20 t addrInTop;
671a0 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 /* Top of t
671b0 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 he IN loop */.
671c0 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20 } *aInLoop;
671d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 /* Inf
671e0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 ormation about e
671f0 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f 70 ach nested IN op
67200 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 20 erator */. }
67210 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 in;
67220 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
67230 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 plan.wsFlags&WH
67240 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20 ERE_IN_ABLE */.
67250 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 } u;.. /* The
67260 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 following field
67270 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 is really not pa
67280 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e rt of the curren
67290 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 t level. But.
672a0 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 ** we need a pla
672b0 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 74 ce to cache virt
672c0 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20 ual table index
672d0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
672e0 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61 each. ** virtua
672f0 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 l table in the F
67300 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74 ROM clause and t
67310 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74 he WhereLevel st
67320 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20 ructure is. **
67330 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 a convenient pla
67340 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 ce since there i
67350 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c s one WhereLevel
67360 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63 for each FROM c
67370 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65 lause. ** eleme
67380 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 nt.. */. sqlit
67390 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 e3_index_info *p
673a0 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 IdxInfo; /* Ind
673b0 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 ex info for n-th
673c0 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f source table */
673d0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 .};../*.** Flags
673e0 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 appropriate for
673f0 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20 the wctrlFlags
67400 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c parameter of sql
67410 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
67420 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72 .** and the Wher
67430 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73 eInfo.wctrlFlags
67440 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 member..*/.#def
67450 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 ine WHERE_ORDERB
67460 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30 Y_NORMAL 0x000
67470 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64 0 /* No-op */.#d
67480 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 efine WHERE_ORDE
67490 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30 RBY_MIN 0x0
674a0 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 001 /* ORDER BY
674b0 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d processing for m
674c0 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 in() func */.#de
674d0 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 fine WHERE_ORDER
674e0 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30 BY_MAX 0x00
674f0 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 02 /* ORDER BY p
67500 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61 rocessing for ma
67510 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 x() func */.#def
67520 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 ine WHERE_ONEPAS
67530 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30 S_DESIRED 0x000
67540 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20 4 /* Want to do
67550 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f one-pass UPDATE/
67560 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e DELETE */.#defin
67570 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 e WHERE_DUPLICAT
67580 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 20 ES_OK 0x0008
67590 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20 /* Ok to return
675a0 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20 a row more than
675b0 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 once */.#define
675c0 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 WHERE_OMIT_OPEN
675d0 20 20 20 20 20 20 20 30 78 30 30 31 30 20 2f 2a 0x0010 /*
675e0 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 72 Table cursor ar
675f0 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a e already open *
67600 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
67610 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 20 OMIT_CLOSE
67620 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 74 20 0x0020 /* Omit
67630 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20 26 close of table &
67640 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a index cursors *
67650 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
67660 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 20 FORCE_TABLE
67670 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 6e 6f 0x0040 /* Do no
67680 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f t use an index-o
67690 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f nly search */../
676a0 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 63 *.** The WHERE c
676b0 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 lause processing
676c0 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 6f routine has two
676d0 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a 2a halves. The.**
676e0 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 73 first part does
676f0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
67700 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e 64 e WHERE loop and
67710 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 the second.** h
67720 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 69 alf does the tai
67730 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c l of the WHERE l
67740 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e 63 oop. An instanc
67750 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 e of.** this str
67760 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 6e ucture is return
67770 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 ed by the first
67780 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 0a half and passed.
67790 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 6f ** into the seco
677a0 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 20 nd half to give
677b0 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 2e some continuity.
677c0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 .*/.struct Where
677d0 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 2a Info {. Parse *
677e0 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a pParse; /*
677f0 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 Parsing and cod
67800 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e e generating con
67810 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 77 63 text */. u16 wc
67820 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 2f trlFlags; /
67830 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61 6c * Flags original
67840 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c ly passed to sql
67850 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
67860 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 61 */. u8 okOnePa
67870 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6b ss; /* Ok
67880 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 73 73 to use one-pass
67890 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 55 algorithm for U
678a0 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 PDATE or DELETE
678b0 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 */. SrcList *pT
678c0 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 abList;
678d0 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 /* List of t
678e0 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 ables in the joi
678f0 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b n */. int iTop;
67900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67910 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72 /* The ver
67920 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 y beginning of t
67930 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f he WHERE loop */
67940 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 . int iContinue
67950 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
67960 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
67970 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 o continue with
67980 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20 next record */.
67990 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20 int iBreak;
679a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
679b0 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 /* Jump here to
679c0 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 break out of the
679d0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e loop */. int n
679e0 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 Level;
679f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
67a00 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f ber of nested lo
67a10 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 57 op */. struct W
67a20 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b hereClause *pWC;
67a30 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 /* Decomp
67a40 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 osition of the W
67a50 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
67a60 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d WhereLevel a[1]
67a70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
67a80 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
67a90 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 6c bout each nest l
67aa0 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a oop in WHERE */.
67ab0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65 };../*.** A Name
67ac0 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 20 Context defines
67ad0 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 a context in whi
67ae0 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 61 ch to resolve ta
67af0 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a ble and column.*
67b00 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f * names. The co
67b10 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f ntext consists o
67b20 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c f a list of tabl
67b30 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 74 es (the pSrcList
67b40 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61 ) field and.** a
67b50 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 list of named e
67b60 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73 xpression (pELis
67b70 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65 t). The named e
67b80 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d xpression list m
67b90 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 ay.** be NULL.
67ba0 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70 The pSrc corresp
67bb0 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d onds to the FROM
67bc0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c clause of a SEL
67bd0 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65 ECT or.** to the
67be0 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 65 table being ope
67bf0 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 45 rated on by INSE
67c00 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 RT, UPDATE, or D
67c10 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70 ELETE. The.** p
67c20 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 EList correspond
67c30 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20 s to the result
67c40 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 set of a SELECT
67c50 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a and is NULL for.
67c60 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 ** other stateme
67c70 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43 nts..**.** NameC
67c80 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 6e ontexts can be n
67c90 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 73 ested. When res
67ca0 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68 olving names, th
67cb0 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a e inner-most .**
67cc0 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 72 context is sear
67cd0 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 20 ched first. If
67ce0 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e no match is foun
67cf0 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 65 d, the next oute
67d00 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 r.** context is
67d10 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 65 checked. If the
67d20 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d re is still no m
67d30 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 63 atch, the next c
67d40 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 65 ontext.** is che
67d50 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f 63 cked. This proc
67d60 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e ess continues un
67d70 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 74 til either a mat
67d80 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f ch is found.** o
67d90 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61 r all contexts a
67da0 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e 20 re check. When
67db0 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 a match is found
67dc0 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 65 , the nRef membe
67dd0 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 r of.** the cont
67de0 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ext containing t
67df0 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 72 he match is incr
67e00 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 emented. .**.**
67e10 45 61 63 68 20 73 75 62 71 75 65 72 79 20 67 65 Each subquery ge
67e20 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e ts a new NameCon
67e30 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 74 text. The pNext
67e40 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f field points to
67e50 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 the.** NameCont
67e60 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 6e ext in the paren
67e70 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 74 t query. Thus t
67e80 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 63 he process of sc
67e90 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61 anning the.** Na
67ea0 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63 meContext list c
67eb0 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65 orresponds to se
67ec0 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20 arching through
67ed0 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74 successively out
67ee0 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 er.** subqueries
67ef0 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d looking for a m
67f00 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 atch..*/.struct
67f10 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20 NameContext {.
67f20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 Parse *pParse;
67f30 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 /* The pars
67f40 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 er */. SrcList
67f50 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20 *pSrcList; /*
67f60 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c One or more tabl
67f70 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c es used to resol
67f80 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78 ve names */. Ex
67f90 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 prList *pEList;
67fa0 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c /* Optional l
67fb0 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 ist of named exp
67fc0 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e ressions */. in
67fd0 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
67fe0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
67ff0 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 62 names resolved b
68000 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 2a y this context *
68010 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 /. int nErr;
68020 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
68030 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 er of errors enc
68040 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 72 ountered while r
68050 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a esolving names *
68060 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b /. u8 allowAgg;
68070 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 /* Aggr
68080 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
68090 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a allowed here */.
680a0 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 20 u8 hasAgg;
680b0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
680c0 66 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65 f aggregates are
680d0 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73 seen */. u8 is
680e0 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 20 Check;
680f0 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f 6c /* True if resol
68100 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 20 ving names in a
68110 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 CHECK constraint
68120 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 68 */. int nDepth
68130 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 ; /* De
68140 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 20 pth of subquery
68150 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72 recursion. 1 for
68160 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f no recursion */
68170 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 . AggInfo *pAgg
68180 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72 Info; /* Infor
68190 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 67 mation about agg
681a0 72 65 67 61 74 65 73 20 61 74 20 74 68 69 73 20 regates at this
681b0 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43 level */. NameC
681c0 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 20 ontext *pNext;
681d0 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e 61 /* Next outer na
681e0 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c me context. NUL
681f0 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20 L for outermost
68200 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
68210 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
68220 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
68230 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c ure contains all
68240 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
68250 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 61 needed to genera
68260 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 te code for a si
68270 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74 ngle SELECT stat
68280 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69 ement..**.** nLi
68290 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d 31 mit is set to -1
682a0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
682b0 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 6e LIMIT clause. n
682c0 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f Offset is set to
682d0 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 0..** If there
682e0 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 is a LIMIT claus
682f0 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 65 e, the parser se
68300 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65 ts nLimit to the
68310 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a value of the.**
68320 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 limit and nOffs
68330 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 et to the value
68340 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 6f of the offset (o
68350 72 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 r 0 if there is
68360 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20 not.** offset).
68370 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e But later on, n
68380 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 Limit and nOffse
68390 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d t become the mem
683a0 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a ory locations.**
683b0 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 61 in the VDBE tha
683c0 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 6d t record the lim
683d0 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 6f it and offset co
683e0 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64 unters..**.** ad
683f0 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74 drOpenEphm[] ent
68400 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 ries contain the
68410 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f address of OP_O
68420 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 penEphemeral opc
68430 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61 odes..** These a
68440 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 65 ddresses must be
68450 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 20 stored so that
68460 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 61 we can go back a
68470 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68 nd fill in.** th
68480 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64 e P4_KEYINFO and
68490 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 6c P2 parameters l
684a0 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 74 ater. Neither t
684b0 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a he KeyInfo nor.*
684c0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
684d0 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 61 columns in P2 ca
684e0 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74 n be computed at
684f0 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a the same time.*
68500 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e * as the OP_Open
68510 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e Ephm instruction
68520 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 73 is coded becaus
68530 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 e not.** enough
68540 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
68550 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 t the compound q
68560 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74 uery is known at
68570 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20 that point..**
68580 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 The KeyInfo for
68590 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 addrOpenTran[0]
685a0 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 and [1] contains
685b0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
685c0 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20 nces.** for the
685d0 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65 result set. The
685e0 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 KeyInfo for add
685f0 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e rOpenTran[2] con
68600 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a tains collating.
68610 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72 ** sequences for
68620 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
68630 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ause..*/.struct
68640 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 4c Select {. ExprL
68650 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 ist *pEList;
68660 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 20 /* The fields
68670 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f of the result */
68680 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 . u8 op;
68690 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
686a0 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b of: TK_UNION TK
686b0 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43 _ALL TK_INTERSEC
686c0 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20 T TK_EXCEPT */.
686d0 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 char affinity;
686e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52 /* MakeR
686f0 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 20 ecord with this
68700 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 54 affinity for SRT
68710 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 65 _Set */. u16 se
68720 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 lFlags;
68730 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a /* Various SF_*
68740 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63 values */. Src
68750 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 List *pSrc;
68760 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 /* The FROM
68770 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
68780 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 *pWhere;
68790 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 /* The WHERE
687a0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
687b0 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 List *pGroupBy;
687c0 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 /* The GROUP
687d0 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 BY clause */. E
687e0 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 xpr *pHaving;
687f0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 /* The HAV
68800 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ING clause */.
68810 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 ExprList *pOrder
68820 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 By; /* The OR
68830 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f DER BY clause */
68840 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f . Select *pPrio
68850 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 r; /* Pri
68860 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 or select in a c
68870 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 ompound select s
68880 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 tatement */. Se
68890 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 lect *pNext;
688a0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c /* Next sel
688b0 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20 ect to the left
688c0 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f in a compound */
688d0 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68 . Select *pRigh
688e0 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67 tmost; /* Rig
688f0 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 ht-most select i
68900 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c n a compound sel
68910 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f ect statement */
68920 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b . Expr *pLimit;
68930 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d /* LIM
68940 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e IT expression. N
68950 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 ULL means not us
68960 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ed. */. Expr *p
68970 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
68980 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 73 /* OFFSET expres
68990 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 sion. NULL means
689a0 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 not used. */.
689b0 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 int iLimit, iOff
689c0 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 set; /* Memory
689d0 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69 registers holdi
689e0 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 45 ng LIMIT & OFFSE
689f0 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 T counters */.
68a00 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 6d int addrOpenEphm
68a10 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 [3]; /* OP_Ope
68a20 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 72 nEphem opcodes r
68a30 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 73 elated to this s
68a40 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a elect */.};../*.
68a50 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
68a60 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c s for Select.sel
68a70 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 22 Flags. The "SF"
68a80 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 66 prefix stands f
68a90 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c or.** "Select Fl
68aa0 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ag"..*/.#define
68ab0 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20 SF_Distinct
68ac0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 0x0001 /* Ou
68ad0 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44 tput should be D
68ae0 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 ISTINCT */.#defi
68af0 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 ne SF_Resolved
68b00 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 0x0002 /*
68b10 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 76 Identifiers hav
68b20 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 e been resolved
68b30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67 */.#define SF_Ag
68b40 67 72 65 67 61 74 65 20 20 20 20 20 20 20 30 78 gregate 0x
68b50 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 0004 /* Contain
68b60 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 s aggregate func
68b70 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 tions */.#define
68b80 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 SF_UsesEphemera
68b90 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55 l 0x0008 /* U
68ba0 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65 ses the OpenEphe
68bb0 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a meral opcode */.
68bc0 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e #define SF_Expan
68bd0 64 65 64 20 20 20 20 20 20 20 20 30 78 30 30 31 ded 0x001
68be0 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 0 /* sqlite3Sel
68bf0 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c ectExpand() call
68c00 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 ed on this */.#d
68c10 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65 efine SF_HasType
68c20 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 20 Info 0x0020
68c30 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72 /* FROM subquer
68c40 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d ies have Table m
68c50 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a etadata */.../*.
68c60 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f ** The results o
68c70 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 62 f a select can b
68c80 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e e distributed in
68c90 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 20 several ways.
68ca0 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 65 The.** "SRT" pre
68cb0 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 43 fix means "SELEC
68cc0 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e 0a T Result Type"..
68cd0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 55 */.#define SRT_U
68ce0 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 2f nion 1 /
68cf0 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 * Store result a
68d00 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 s keys in an ind
68d10 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 ex */.#define SR
68d20 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 32 T_Except 2
68d30 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 75 /* Remove resu
68d40 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20 lt from a UNION
68d50 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 index */.#define
68d60 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20 SRT_Exists
68d70 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 20 3 /* Store 1
68d80 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 if the result is
68d90 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64 not empty */.#d
68da0 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72 efine SRT_Discar
68db0 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f 20 d 4 /* Do
68dc0 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 73 not save the res
68dd0 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f ults anywhere */
68de0 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 ../* The ORDER B
68df0 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f Y clause is igno
68e00 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 red for all of t
68e10 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66 he above */.#def
68e20 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 ine IgnorableOrd
68e30 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 65 erby(X) ((X->eDe
68e40 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 64 st)<=SRT_Discard
68e50 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f )..#define SRT_O
68e60 75 74 70 75 74 20 20 20 20 20 20 20 35 20 20 2f utput 5 /
68e70 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f * Output each ro
68e80 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23 w of result */.#
68e90 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20 define SRT_Mem
68ea0 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74 6 /* St
68eb0 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ore result in a
68ec0 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23 memory cell */.#
68ed0 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20 define SRT_Set
68ee0 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74 7 /* St
68ef0 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b ore results as k
68f00 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 eys in an index
68f10 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54 */.#define SRT_T
68f20 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f able 8 /
68f30 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 * Store result a
68f40 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61 s data with an a
68f50 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a utomatic rowid *
68f60 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70 /.#define SRT_Ep
68f70 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a hemTab 9 /*
68f80 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e Create transien
68f90 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20 t tab and store
68fa0 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a like SRT_Table *
68fb0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 6f /.#define SRT_Co
68fc0 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f 2a routine 10 /*
68fd0 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67 Generate a sing
68fe0 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 le row of result
68ff0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 */../*.** A str
69000 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 63 ucture used to c
69010 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 68 ustomize the beh
69020 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 avior of sqlite3
69030 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a Select(). See.**
69040 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 comments above
69050 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 sqlite3Select()
69060 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a for details..*/.
69070 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
69080 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 74 electDest Select
69090 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 6c Dest;.struct Sel
690a0 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65 ectDest {. u8 e
690b0 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Dest; /*
690c0 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 How to dispose
690d0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a of the results *
690e0 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 3b /. u8 affinity;
690f0 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 /* Affinit
69100 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 73 y used when eDes
69110 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 t==SRT_Set */.
69120 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 20 int iParm;
69130 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 72 /* A parameter
69140 20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 65 used by the eDe
69150 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 st disposal meth
69160 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d od */. int iMem
69170 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 ; /* Bas
69180 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 e register where
69190 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 69 results are wri
691a0 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d tten */. int nM
691b0 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e em; /* N
691c0 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 umber of registe
691d0 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a rs allocated */.
691e0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 };../*.** During
691f0 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e code generation
69200 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20 74 of statements t
69210 68 61 74 20 64 6f 20 69 6e 73 65 72 74 73 20 69 hat do inserts i
69220 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e nto AUTOINCREMEN
69230 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c 20 74 68 T .** tables, th
69240 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f e following info
69250 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 74 61 63 rmation is attac
69260 68 65 64 20 74 6f 20 74 68 65 20 54 61 62 6c 65 hed to the Table
69270 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a 2a 2a 20 .u.autoInc.p.**
69280 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 63 68 20 pointer of each
69290 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61 autoincrement ta
692a0 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 73 6f ble to record so
692b0 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 6d 61 74 me side informat
692c0 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 ion that.** the
692d0 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 6e code generator n
692e0 65 65 64 73 2e 20 20 57 65 20 68 61 76 65 20 74 eeds. We have t
692f0 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 62 6c 65 o keep per-table
69300 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a autoincrement.*
69310 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e * information in
69320 20 63 61 73 65 20 69 6e 73 65 72 74 73 20 61 72 case inserts ar
69330 65 20 64 6f 77 6e 20 77 69 74 68 69 6e 20 74 72 e down within tr
69340 69 67 67 65 72 73 2e 20 20 54 72 69 67 67 65 72 iggers. Trigger
69350 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d s do not.** norm
69360 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 74 65 20 ally coordinate
69370 74 68 65 69 72 20 61 63 74 69 76 69 74 69 65 73 their activities
69380 2c 20 62 75 74 20 77 65 20 64 6f 20 6e 65 65 64 , but we do need
69390 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 74 to coordinate t
693a0 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 61 6e he.** loading an
693b0 64 20 73 61 76 69 6e 67 20 6f 66 20 61 75 74 6f d saving of auto
693c0 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 6f 72 6d increment inform
693d0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ation..*/.struct
693e0 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 7b 0a 20 AutoincInfo {.
693f0 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 4e AutoincInfo *pN
69400 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 69 ext; /* Next i
69410 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 61 20 6c nfo block in a l
69420 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 ist of them all
69430 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 */. Table *pTab
69440 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 ; /* Ta
69450 62 6c 65 20 74 68 69 73 20 69 6e 66 6f 20 62 6c ble this info bl
69460 6f 63 6b 20 72 65 66 65 72 73 20 74 6f 20 2a 2f ock refers to */
69470 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 . int iDb;
69480 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
69490 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 x in sqlite3.aDb
694a0 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 [] of database h
694b0 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 olding pTab */.
694c0 20 69 6e 74 20 72 65 67 43 74 72 3b 20 20 20 20 int regCtr;
694d0 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
694e0 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e register holdin
694f0 67 20 74 68 65 20 72 6f 77 69 64 20 63 6f 75 6e g the rowid coun
69500 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ter */.};../*.**
69510 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 6c Size of the col
69520 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a 23 69 66 umn cache.*/.#if
69530 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f 43 4f ndef SQLITE_N_CO
69540 4c 43 41 43 48 45 0a 23 20 64 65 66 69 6e 65 20 LCACHE.# define
69550 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
69560 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a E 10.#endif../*.
69570 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f 6e 65 20 ** At least one
69580 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
69590 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
695a0 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 66 ure is created f
695b0 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 72 69 67 or each .** trig
695c0 67 65 72 20 74 68 61 74 20 6d 61 79 20 62 65 20 ger that may be
695d0 66 69 72 65 64 20 77 68 69 6c 65 20 70 61 72 73 fired while pars
695e0 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 ing an INSERT, U
695f0 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 0a PDATE or DELETE.
69600 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 41 6c ** statement. Al
69610 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 73 20 61 l such objects a
69620 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 re stored in the
69630 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 linked list hea
69640 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 73 65 2e ded at.** Parse.
69650 70 54 72 69 67 67 65 72 50 72 67 20 61 6e 64 20 pTriggerPrg and
69660 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 73 74 61 deleted once sta
69670 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61 74 69 tement compilati
69680 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 63 on has been.** c
69690 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 ompleted..**.**
696a0 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f 67 72 A Vdbe sub-progr
696b0 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e am that implemen
696c0 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e 64 20 ts the body and
696d0 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 74 WHEN clause of t
696e0 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 67 67 65 rigger.** Trigge
696f0 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c 20 61 rPrg.pTrigger, a
69700 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61 75 6c ssuming a defaul
69710 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c t ON CONFLICT cl
69720 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 69 67 67 ause of.** Trigg
69730 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20 69 73 erPrg.orconf, is
69740 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 54 stored in the T
69750 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f 67 72 riggerPrg.pProgr
69760 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 20 am variable..**
69770 54 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 67 The Parse.pTrigg
69780 65 72 50 72 67 20 6c 69 73 74 20 6e 65 76 65 72 erPrg list never
69790 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e contains two en
697a0 74 72 69 65 73 20 77 69 74 68 20 74 68 65 20 73 tries with the s
697b0 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 20 66 6f ame.** values fo
697c0 72 20 62 6f 74 68 20 70 54 72 69 67 67 65 72 20 r both pTrigger
697d0 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a 2a 0a 2a and orconf..**.*
697e0 2a 20 54 68 65 20 54 72 69 67 67 65 72 50 72 67 * The TriggerPrg
697f0 2e 6f 6c 64 6d 61 73 6b 20 76 61 72 69 61 62 6c .oldmask variabl
69800 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6d 61 e is set to a ma
69810 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c 75 sk of old.* colu
69820 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 mns.** accessed
69830 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 6f 72 (or set to 0 for
69840 20 74 72 69 67 67 65 72 73 20 66 69 72 65 64 20 triggers fired
69850 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 49 as a result of I
69860 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 65 6d NSERT .** statem
69870 65 6e 74 73 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 ents)..*/.struct
69880 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 20 TriggerPrg {.
69890 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 Trigger *pTrigge
698a0 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67 r; /* Trigg
698b0 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d 20 er this program
698c0 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 2a was coded from *
698d0 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 /. int orconf;
698e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
698f0 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 efault ON CONFLI
69900 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 53 CT policy */. S
69910 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 ubProgram *pProg
69920 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 72 61 ram; /* Progra
69930 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 70 m implementing p
69940 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 20 2a Trigger/orconf *
69950 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b /. u32 oldmask;
69960 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
69970 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c ask of old.* col
69980 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20 2a 2f umns accessed */
69990 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 . TriggerPrg *p
699a0 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 Next; /* Ne
699b0 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 72 73 xt entry in Pars
699c0 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c 69 e.pTriggerPrg li
699d0 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 st */.};../*.**
699e0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f An SQL parser co
699f0 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f ntext. A copy o
69a00 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
69a10 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 is passed throu
69a20 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72 gh.** the parser
69a30 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61 and down into a
69a40 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63 ll the parser ac
69a50 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20 tion routine in
69a60 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72 order to.** carr
69a70 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 y around informa
69a80 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f tion that is glo
69a90 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72 bal to the entir
69aa0 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 e parse..**.** T
69ab0 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 he structure is
69ac0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f divided into two
69ad0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68 parts. When th
69ae0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 e parser and cod
69af0 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61 e.** generate ca
69b00 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65 ll themselves re
69b10 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66 cursively, the f
69b20 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 irst part of the
69b30 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 structure.** is
69b40 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68 constant but th
69b50 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73 e second part is
69b60 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65 reset at the be
69b70 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 ginning and end
69b80 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72 of.** each recur
69b90 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sion..**.** The
69ba0 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 nTableLock and a
69bb0 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62 TableLock variab
69bc0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 les are only use
69bd0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d d if the shared-
69be0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72 cache .** featur
69bf0 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66 e is enabled (if
69c00 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75 sqlite3Tsd()->u
69c10 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20 seSharedData is
69c20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a true). They are.
69c30 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ** used to store
69c40 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c the set of tabl
69c50 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64 e-locks required
69c60 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e by the statemen
69c70 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 t being.** compi
69c80 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71 led. Function sq
69c90 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 lite3TableLock()
69ca0 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 is used to add
69cb0 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a entries to the.*
69cc0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 * list..*/.struc
69cd0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69 t Parse {. sqli
69ce0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
69cf0 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 /* The main dat
69d00 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 abase structure
69d10 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
69d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
69d30 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78 urn code from ex
69d40 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 ecution */. cha
69d50 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 r *zErrMsg;
69d60 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 /* An error me
69d70 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20 ssage */. Vdbe
69d80 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20 *pVdbe;
69d90 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72 /* An engine for
69da0 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62 executing datab
69db0 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a ase bytecode */.
69dc0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74 u8 colNamesSet
69dd0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61 ; /* TRUE a
69de0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 fter OP_ColumnNa
69df0 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75 me has been issu
69e00 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20 ed to pVdbe */.
69e10 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20 u8 nameClash;
69e20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61 /* A perma
69e30 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20 nent table name
69e40 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d clashes with tem
69e50 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a p table name */.
69e60 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61 u8 checkSchema
69e70 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73 ; /* Causes
69e80 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
69e90 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 heck after an er
69ea0 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74 ror */. u8 nest
69eb0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ed; /*
69ec0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
69ed0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70 d calls to the p
69ee0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72 arser/code gener
69ef0 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72 ator */. u8 par
69f00 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f seError; /
69f10 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70 * True after a p
69f20 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54 arsing error. T
69f30 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20 icket #1794 */.
69f40 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20 u8 nTempReg;
69f50 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
69f60 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 of temporary reg
69f70 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52 isters in aTempR
69f80 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65 eg[] */. u8 nTe
69f90 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f mpInUse; /
69fa0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d * Number of aTem
69fb0 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79 pReg[] currently
69fc0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
69fd0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 int aTempReg[8
69fe0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e ]; /* Holdin
69ff0 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f g area for tempo
6a000 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a rary registers *
6a010 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 /. int nRangeRe
6a020 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 g; /* Size
6a030 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 of the temporar
6a040 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b y register block
6a050 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 */. int iRange
6a060 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 Reg; /* Fi
6a070 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 rst register in
6a080 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
6a090 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e er block */. in
6a0a0 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 t nErr;
6a0b0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6a0c0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 errors seen */.
6a0d0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 int nTab;
6a0e0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6a0f0 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c of previously al
6a100 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72 located VDBE cur
6a110 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d sors */. int nM
6a120 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
6a130 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
6a140 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f ry cells used so
6a150 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 far */. int nS
6a160 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f et; /
6a170 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 * Number of sets
6a180 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a used so far */.
6a190 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20 int ckBase;
6a1a0 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 /* Base r
6a1b0 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20 egister of data
6a1c0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e during check con
6a1d0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e straints */. in
6a1e0 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 20 t iCacheLevel;
6a1f0 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 20 76 /* ColCache v
6a200 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c 43 61 alid when aColCa
6a210 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d 69 43 che[].iLevel<=iC
6a220 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 20 69 acheLevel */. i
6a230 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 20 20 nt iCacheCnt;
6a240 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 75 /* Counter u
6a250 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 sed to generate
6a260 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 75 20 aColCache[].lru
6a270 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 6e values */. u8 n
6a280 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20 ColCache;
6a290 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
6a2a0 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 6c tries in the col
6a2b0 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 umn cache */. u
6a2c0 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 8 iColCache;
6a2d0 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 /* Next entr
6a2e0 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 74 y of the cache t
6a2f0 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 73 o replace */. s
6a300 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
6a310 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65 {. int iTable
6a320 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
6a330 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 able cursor numb
6a340 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 er */. int iC
6a350 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 olumn;
6a360 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 /* Table column
6a370 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 38 number */. u8
6a380 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20 20 affChange;
6a390 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
6a3a0 68 69 73 20 72 65 67 69 73 74 65 72 20 68 61 73 his register has
6a3b0 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 79 had an affinity
6a3c0 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 75 change */. u
6a3d0 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 20 20 8 tempReg;
6a3e0 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 73 20 /* iReg is
6a3f0 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 20 a temp register
6a400 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 that needs to be
6a410 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e freed */. in
6a420 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 t iLevel;
6a430 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 6c /* Nesting l
6a440 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 evel */. int
6a450 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 iReg;
6a460 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 76 61 /* Reg with va
6a470 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 lue of this colu
6a480 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e 65 mn. 0 means none
6a490 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 75 . */. int lru
6a4a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
6a4b0 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 6c 79 * Least recently
6a4c0 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 73 20 used entry has
6a4d0 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c the smallest val
6a4e0 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61 ue */. } aColCa
6a4f0 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c che[SQLITE_N_COL
6a500 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20 CACHE]; /* One
6a510 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 for each column
6a520 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20 cache entry */.
6a530 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b 3b 20 u32 writeMask;
6a540 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 61 /* Start a
6a550 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
6a560 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 74 61 on on these data
6a570 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 20 63 bases */. u32 c
6a580 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 20 20 ookieMask;
6a590 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 63 /* Bitmask of sc
6a5a0 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 64 61 hema verified da
6a5b0 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 38 20 tabases */. u8
6a5c0 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20 20 20 isMultiWrite;
6a5d0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61 /* True if sta
6a5e0 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66 65 63 tement may affec
6a5f0 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69 70 6c t/insert multipl
6a600 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38 20 6d e rows */. u8 m
6a610 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20 20 20 ayAbort;
6a620 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61 74 /* True if stat
6a630 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f 77 20 ement may throw
6a640 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 an ABORT excepti
6a650 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b on */. int cook
6a660 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20 ieGoto; /*
6a670 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f Address of OP_Go
6a680 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72 to to cookie ver
6a690 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65 ifier subroutine
6a6a0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 */. int cookie
6a6b0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58 Value[SQLITE_MAX
6a6c0 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f _ATTACHED+2]; /
6a6d0 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b * Values of cook
6a6e0 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f ies to verify */
6a6f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
6a700 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
6a710 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f E. int nTableLo
6a720 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ck; /* Nu
6a730 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e mber of locks in
6a740 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20 aTableLock */.
6a750 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62 TableLock *aTab
6a760 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69 leLock; /* Requi
6a770 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 red table locks
6a780 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 for shared-cache
6a790 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a mode */.#endif.
6a7a0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 int regRowid;
6a7b0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
6a7c0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 er holding rowid
6a7d0 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45 of CREATE TABLE
6a7e0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 entry */. int
6a7f0 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 regRoot;
6a800 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
6a810 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e ding root page n
6a820 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62 umber for new ob
6a830 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74 6f 69 jects */. Autoi
6a840 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b 20 20 ncInfo *pAinc;
6a850 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
6a860 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45 4d 45 bout AUTOINCREME
6a870 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 NT counters */.
6a880 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20 20 20 int nMaxArg;
6a890 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61 72 67 /* Max arg
6a8a0 73 20 70 61 73 73 65 64 20 74 6f 20 75 73 65 72 s passed to user
6a8b0 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73 75 62 function by sub
6a8c0 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20 20 2f -program */.. /
6a8d0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 * Information us
6a8e0 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e 67 20 ed while coding
6a8f0 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 trigger programs
6a900 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 54 . */. Parse *pT
6a910 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a 20 50 oplevel; /* P
6a920 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66 arse structure f
6a930 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20 or main program
6a940 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 54 (or NULL) */. T
6a950 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72 54 61 able *pTriggerTa
6a960 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74 72 69 b; /* Table tri
6a970 67 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 ggers are being
6a980 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20 20 75 coded for */. u
6a990 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 20 20 32 oldmask;
6a9a0 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6f /* Mask of o
6a9b0 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 65 66 ld.* columns ref
6a9c0 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 erenced */. u8
6a9d0 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 20 20 eTriggerOp;
6a9e0 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 2c 20 /* TK_UPDATE,
6a9f0 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 4b 5f TK_INSERT or TK_
6aa00 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 65 DELETE */. u8 e
6aa10 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 Orconf;
6aa20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 /* Default ON C
6aa30 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66 ONFLICT policy f
6aa40 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70 73 or trigger steps
6aa50 20 2a 2f 0a 0a 20 20 2f 2a 20 41 62 6f 76 65 20 */.. /* Above
6aa60 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 65 74 77 is constant betw
6aa70 65 65 6e 20 72 65 63 75 72 73 69 6f 6e 73 2e 20 een recursions.
6aa80 20 42 65 6c 6f 77 20 69 73 20 72 65 73 65 74 20 Below is reset
6aa90 62 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 before and after
6aaa0 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63 75 72 . ** each recur
6aab0 73 69 6f 6e 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e sion */.. int n
6aac0 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 Var;
6aad0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 /* Number of '?'
6aae0 20 76 61 72 69 61 62 6c 65 73 20 73 65 65 6e 20 variables seen
6aaf0 69 6e 20 74 68 65 20 53 51 4c 20 73 6f 20 66 61 in the SQL so fa
6ab00 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 r */. int nVarE
6ab10 78 70 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e xpr; /* N
6ab20 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20 73 6c umber of used sl
6ab30 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78 70 72 ots in apVarExpr
6ab40 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 [] */. int nVar
6ab50 45 78 70 72 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 ExprAlloc; /*
6ab60 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 Number of alloca
6ab70 74 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 ted slots in apV
6ab80 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 arExpr[] */. Ex
6ab90 70 72 20 2a 2a 61 70 56 61 72 45 78 70 72 3b 20 pr **apVarExpr;
6aba0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 /* Pointers t
6abb0 6f 20 3a 61 61 61 20 61 6e 64 20 24 61 61 61 61 o :aaa and $aaaa
6abc0 20 77 69 6c 64 63 61 72 64 20 65 78 70 72 65 73 wildcard expres
6abd0 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e sions */. int n
6abe0 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 Alias;
6abf0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 69 /* Number of ali
6ac00 61 73 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 ased result set
6ac10 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 columns */. int
6ac20 20 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 20 20 nAliasAlloc;
6ac30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
6ac40 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 66 llocated slots f
6ac50 6f 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 or aAlias[] */.
6ac60 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 int *aAlias;
6ac70 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
6ac80 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 r used to hold a
6ac90 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 2a 2f liased result */
6aca0 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 . u8 explain;
6acb0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
6acc0 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66 if the EXPLAIN f
6acd0 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 lag is found on
6ace0 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 54 the query */. T
6acf0 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b oken sNameToken;
6ad00 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 /* Token wit
6ad10 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20 73 63 h unqualified sc
6ad20 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hema object name
6ad30 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 */. Token sLas
6ad40 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 tToken; /* Th
6ad50 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 e last token par
6ad60 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 sed */. const c
6ad70 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a har *zTail; /*
6ad80 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 61 All SQL text pa
6ad90 73 74 20 74 68 65 20 6c 61 73 74 20 73 65 6d 69 st the last semi
6ada0 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a colon parsed */.
6adb0 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 Table *pNewTab
6adc0 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61 62 6c le; /* A tabl
6add0 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 e being construc
6ade0 74 65 64 20 62 79 20 43 52 45 41 54 45 20 54 41 ted by CREATE TA
6adf0 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 BLE */. Trigger
6ae00 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b 20 20 *pNewTrigger;
6ae10 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 75 6e /* Trigger un
6ae20 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20 62 79 der construct by
6ae30 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45 a CREATE TRIGGE
6ae40 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 R */. const cha
6ae50 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b r *zAuthContext;
6ae60 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 72 61 /* The 6th para
6ae70 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 41 75 meter to db->xAu
6ae80 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a th callbacks */.
6ae90 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
6aea0 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
6aeb0 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20 . Token sArg;
6aec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6aed0 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f Complete text o
6aee0 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d f a module argum
6aef0 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 63 6c ent */. u8 decl
6af00 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 20 20 areVtab;
6af10 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
6af20 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f 64 65 nside sqlite3_de
6af30 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f 0a clare_vtab() */.
6af40 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b int nVtabLock;
6af50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6af60 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75 61 Number of virtua
6af70 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b l tables to lock
6af80 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70 */. Table **ap
6af90 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20 VtabLock;
6afa0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
6afb0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e 65 irtual tables ne
6afc0 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f eding locking */
6afd0 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e 48 .#endif. int nH
6afe0 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 eight;
6aff0 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
6b000 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 63 tree height of c
6b010 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 63 urrent sub-selec
6b020 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 5a t */. Table *pZ
6b030 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 20 2f ombieTab; /
6b040 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 20 * List of Table
6b050 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 74 objects to delet
6b060 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 6e e after code gen
6b070 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 */. TriggerPrg
6b080 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b 20 20 *pTriggerPrg;
6b090 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 /* Linked list
6b0a0 20 6f 66 20 63 6f 64 65 64 20 74 72 69 67 67 65 of coded trigge
6b0b0 72 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 rs */.};..#ifdef
6b0c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
6b0d0 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 TUALTABLE. #def
6b0e0 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 ine IN_DECLARE_V
6b0f0 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 TAB 0.#else. #d
6b100 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 efine IN_DECLARE
6b110 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64 _VTAB (pParse->d
6b120 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64 eclareVtab).#end
6b130 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 if../*.** An ins
6b140 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
6b150 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
6b160 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64 can be declared
6b170 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20 on a stack and
6b180 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 used.** to save
6b190 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43 the Parse.zAuthC
6b1a0 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20 ontext value so
6b1b0 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 that it can be r
6b1c0 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a estored later..*
6b1d0 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e /.struct AuthCon
6b1e0 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 text {. const c
6b1f0 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 har *zAuthContex
6b200 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65 t; /* Put save
6b210 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e d Parse.zAuthCon
6b220 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 text here */. P
6b230 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 arse *pParse;
6b240 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
6b250 65 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 e Parse structur
6b260 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 e */.};../*.** B
6b270 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f itfield flags fo
6b280 72 20 50 35 20 76 61 6c 75 65 20 69 6e 20 4f 50 r P5 value in OP
6b290 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 _Insert and OP_D
6b2a0 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 elete.*/.#define
6b2b0 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 OPFLAG_NCHANGE
6b2c0 20 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a 0x01 /*
6b2d0 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 Set to update d
6b2e0 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 b->nChange */.#d
6b2f0 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 efine OPFLAG_LAS
6b300 54 52 4f 57 49 44 20 20 20 20 20 30 78 30 32 20 TROWID 0x02
6b310 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 /* Set to upd
6b320 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 ate db->lastRowi
6b330 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 d */.#define OPF
6b340 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 20 20 LAG_ISUPDATE
6b350 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 68 69 0x04 /* Thi
6b360 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 61 s OP_Insert is a
6b370 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a n sql UPDATE */.
6b380 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41 #define OPFLAG_A
6b390 50 50 45 4e 44 20 20 20 20 20 20 20 20 30 78 30 PPEND 0x0
6b3a0 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 8 /* This is
6b3b0 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 likely to be an
6b3c0 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e append */.#defin
6b3d0 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b e OPFLAG_USESEEK
6b3e0 52 45 53 55 4c 54 20 30 78 31 30 20 20 20 20 2f RESULT 0x10 /
6b3f0 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64 20 61 * Try to avoid a
6b400 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65 49 6e seek in BtreeIn
6b410 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 69 6e sert() */.#defin
6b420 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 e OPFLAG_CLEARCA
6b430 43 48 45 20 20 20 20 30 78 32 30 20 20 20 20 2f CHE 0x20 /
6b440 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f 2d 74 * Clear pseudo-t
6b450 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20 4f 50 able cache in OP
6b460 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 _Column */../*.
6b470 2a 20 45 61 63 68 20 74 72 69 67 67 65 72 20 70 * Each trigger p
6b480 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 resent in the da
6b490 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 tabase schema is
6b4a0 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20 69 6e stored as an in
6b4b0 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 stance of. * str
6b4c0 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a 20 2a uct Trigger. . *
6b4d0 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 . * Pointers to
6b4e0 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 instances of str
6b4f0 75 63 74 20 54 72 69 67 67 65 72 20 61 72 65 20 uct Trigger are
6b500 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 77 61 stored in two wa
6b510 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 ys.. * 1. In the
6b520 20 22 74 72 69 67 48 61 73 68 22 20 68 61 73 68 "trigHash" hash
6b530 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f 66 20 table (part of
6b540 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74 68 61 the sqlite3* tha
6b550 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 t represents the
6b560 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61 73 65 . * database
6b570 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 ). This allows T
6b580 72 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65 rigger structure
6b590 73 20 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 s to be retrieve
6b5a0 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e d by name.. * 2.
6b5b0 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 61 73 All triggers as
6b5c0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
6b5d0 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 single table for
6b5e0 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c m a linked list,
6b5f0 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 using the. *
6b600 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 pNext member of
6b610 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 2e struct Trigger.
6b620 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 A pointer to th
6b630 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 e first element
6b640 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e of the. * lin
6b650 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74 6f 72 ked list is stor
6b660 65 64 20 61 73 20 74 68 65 20 22 70 54 72 69 67 ed as the "pTrig
6b670 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 ger" member of t
6b680 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 2a he associated. *
6b690 20 20 20 20 73 74 72 75 63 74 20 54 61 62 6c 65 struct Table
6b6a0 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 .. *. * The "ste
6b6b0 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 70 p_list" member p
6b6c0 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 oints to the fir
6b6d0 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 st element of a
6b6e0 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 linked list. * c
6b6f0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 53 51 ontaining the SQ
6b700 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73 70 65 L statements spe
6b710 63 69 66 69 65 64 20 61 73 20 74 68 65 20 74 72 cified as the tr
6b720 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 igger program..
6b730 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 */.struct Trigge
6b740 72 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d r {. char *zNam
6b750 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
6b760 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 The name of the
6b770 20 74 72 69 67 67 65 72 20 20 20 20 20 20 20 20 trigger
6b780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6b790 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 6c 65 */. char *table
6b7a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
6b7b0 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 The table or vie
6b7c0 77 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 74 w to which the t
6b7d0 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 2a rigger applies *
6b7e0 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 /. u8 op;
6b7f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
6b800 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c ne of TK_DELETE,
6b810 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 TK_UPDATE, TK_I
6b820 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 2a 2f NSERT */
6b830 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 . u8 tr_tm;
6b840 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
6b850 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 e of TRIGGER_BEF
6b860 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54 ORE, TRIGGER_AFT
6b870 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 ER */. Expr *pW
6b880 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 hen;
6b890 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c 61 75 /* The WHEN clau
6b8a0 73 65 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 se of the expres
6b8b0 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e 55 4c sion (may be NUL
6b8c0 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a L) */. IdList *
6b8d0 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 20 20 pColumns;
6b8e0 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e /* If this is an
6b8f0 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f 6c 75 UPDATE OF <colu
6b900 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 65 72 mn-list> trigger
6b910 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
6b920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
6b930 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e he <column-list>
6b940 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 is stored here
6b950 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 */. Schema *pSc
6b960 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 hema; /*
6b970 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e Schema containin
6b980 67 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f g the trigger */
6b990 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61 62 53 . Schema *pTabS
6b9a0 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 chema; /* Sc
6b9b0 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 hema containing
6b9c0 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 the table */. T
6b9d0 72 69 67 67 65 72 53 74 65 70 20 2a 73 74 65 70 riggerStep *step
6b9e0 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c _list; /* Link l
6b9f0 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 20 70 ist of trigger p
6ba00 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20 20 20 rogram steps
6ba10 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 54 72 */. Tr
6ba20 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 20 20 igger *pNext;
6ba30 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74 72 /* Next tr
6ba40 69 67 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 igger associated
6ba50 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 with the table
6ba60 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 */.};../*.** A t
6ba70 72 69 67 67 65 72 20 69 73 20 65 69 74 68 65 72 rigger is either
6ba80 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61 6e 20 a BEFORE or an
6ba90 41 46 54 45 52 20 74 72 69 67 67 65 72 2e 20 20 AFTER trigger.
6baa0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f The following co
6bab0 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 65 72 nstants.** deter
6bac0 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a 2a 0a mine which. .**.
6bad0 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 ** If there are
6bae0 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67 65 72 multiple trigger
6baf0 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f 66 20 s, you might of
6bb00 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e 64 20 some BEFORE and
6bb10 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a 20 49 some AFTER..** I
6bb20 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20 74 68 n that cases, th
6bb30 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f e constants belo
6bb40 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20 74 6f w can be ORed to
6bb50 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 gether..*/.#defi
6bb60 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 ne TRIGGER_BEFOR
6bb70 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52 49 E 1.#define TRI
6bb80 47 47 45 52 5f 41 46 54 45 52 20 20 20 32 0a 0a GGER_AFTER 2..
6bb90 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*. * An instanc
6bba0 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 e of struct Trig
6bbb0 67 65 72 53 74 65 70 20 69 73 20 75 73 65 64 20 gerStep is used
6bbc0 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e 67 6c to store a singl
6bbd0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a e SQL statement.
6bbe0 20 2a 20 74 68 61 74 20 69 73 20 61 20 70 61 72 * that is a par
6bbf0 74 20 6f 66 20 61 20 74 72 69 67 67 65 72 2d 70 t of a trigger-p
6bc00 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 rogram. . *. * I
6bc10 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 nstances of stru
6bc20 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 61 ct TriggerStep a
6bc30 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73 re stored in a s
6bc40 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73 ingly linked lis
6bc50 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 73 69 t (linked. * usi
6bc60 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22 20 6d ng the "pNext" m
6bc70 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e 63 65 ember) reference
6bc80 64 20 62 79 20 74 68 65 20 22 73 74 65 70 5f 6c d by the "step_l
6bc90 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 ist" member of t
6bca0 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 74 65 he . * associate
6bcb0 64 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 d struct Trigger
6bcc0 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 20 66 instance. The f
6bcd0 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
6bce0 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 the linked list
6bcf0 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73 74 20 is. * the first
6bd00 73 74 65 70 20 6f 66 20 74 68 65 20 74 72 69 67 step of the trig
6bd10 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 ger-program.. *
6bd20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d 65 6d . * The "op" mem
6bd30 62 65 72 20 69 6e 64 69 63 61 74 65 73 20 77 68 ber indicates wh
6bd40 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20 ether this is a
6bd50 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 45 52 "DELETE", "INSER
6bd60 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f 72 0a T", "UPDATE" or.
6bd70 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74 61 74 * "SELECT" stat
6bd80 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 6e 69 ement. The meani
6bd90 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68 65 72 ngs of the other
6bda0 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65 74 65 members is dete
6bdb0 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 0a 20 rmined by the .
6bdc0 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 22 20 * value of "op"
6bdd0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 as follows:. *.
6bde0 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 * (op == TK_INSE
6bdf0 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 20 20 RT). * orconf
6be00 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 20 4f -> stores the O
6be10 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 N CONFLICT algor
6be20 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 74 20 ithm. * pSelect
6be30 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 -> If this is
6be40 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e an INSERT INTO .
6be50 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 73 74 .. SELECT ... st
6be60 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a atement, then. *
6be70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
6be80 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e is stores a poin
6be90 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c 45 43 ter to the SELEC
6bea0 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f 74 68 T statement. Oth
6beb0 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 erwise NULL.. *
6bec0 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 target -> A t
6bed0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 oken holding the
6bee0 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 quoted name of
6bef0 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 the table to ins
6bf00 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 ert into.. * pEx
6bf10 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 68 69 prList -> If thi
6bf20 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 s is an INSERT I
6bf30 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 20 2e NTO ... VALUES .
6bf40 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 .. statement, th
6bf50 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 en. *
6bf60 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 20 76 this stores v
6bf70 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 65 alues to be inse
6bf80 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20 rted. Otherwise
6bf90 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 74 NULL.. * pIdList
6bfa0 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 -> If this is
6bfb0 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 an INSERT INTO
6bfc0 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d ... (<column-nam
6bfd0 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e 2e 20 es>) VALUES ...
6bfe0 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 . *
6bff0 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e statement, then
6c000 20 74 68 69 73 20 73 74 6f 72 65 73 20 74 68 65 this stores the
6c010 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f column-names to
6c020 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20 be. *
6c030 20 20 20 20 69 6e 73 65 72 74 65 64 20 69 6e 74 inserted int
6c040 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 o.. *. * (op ==
6c050 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 74 61 TK_DELETE). * ta
6c060 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b rget -> A tok
6c070 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 en holding the q
6c080 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 uoted name of th
6c090 65 20 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 e table to delet
6c0a0 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 e from.. * pWher
6c0b0 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 e -> The WHER
6c0c0 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 E clause of the
6c0d0 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 DELETE statement
6c0e0 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 if one is speci
6c0f0 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 fied.. *
6c100 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 Otherwise
6c110 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 NULL.. * . * (op
6c120 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 == TK_UPDATE).
6c130 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 * target -> A
6c140 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 token holding t
6c150 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f he quoted name o
6c160 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 75 f the table to u
6c170 70 64 61 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20 pdate rows of..
6c180 2a 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 * pWhere -> T
6c190 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
6c1a0 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 of the UPDATE st
6c1b0 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 atement if one i
6c1c0 73 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 s specified.. *
6c1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 Oth
6c1e0 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 erwise NULL.. *
6c1f0 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c pExprList -> A l
6c200 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d ist of the colum
6c210 6e 73 20 74 6f 20 75 70 64 61 74 65 20 61 6e 64 ns to update and
6c220 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 the expressions
6c230 20 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 20 20 to update. *
6c240 20 20 20 20 20 20 20 20 20 20 20 74 68 65 6d 20 them
6c250 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 33 55 to. See sqlite3U
6c260 70 64 61 74 65 28 29 20 64 6f 63 75 6d 65 6e 74 pdate() document
6c270 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67 ation of "pChang
6c280 65 73 22 0a 20 2a 20 20 20 20 20 20 20 20 20 20 es". *
6c290 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a argument.. *
6c2a0 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 . */.struct Tri
6c2b0 67 67 65 72 53 74 65 70 20 7b 0a 20 20 75 38 20 ggerStep {. u8
6c2c0 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 op;
6c2d0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 /* One of TK_D
6c2e0 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 ELETE, TK_UPDATE
6c2f0 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f , TK_INSERT, TK_
6c300 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38 20 6f SELECT */. u8 o
6c310 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 rconf;
6c320 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 /* OE_Rollback
6c330 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 etc. */. Trigge
6c340 72 20 2a 70 54 72 69 67 3b 20 20 20 20 20 20 2f r *pTrig; /
6c350 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 74 68 * The trigger th
6c360 61 74 20 74 68 69 73 20 73 74 65 70 20 69 73 20 at this step is
6c370 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 53 a part of */. S
6c380 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 elect *pSelect;
6c390 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 /* SELECT st
6c3a0 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20 6f 66 atment or RHS of
6c3b0 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 20 INSERT INTO ..
6c3c0 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 SELECT ... */.
6c3d0 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20 20 Token target;
6c3e0 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74 20 74 /* Target t
6c3f0 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54 45 2c able for DELETE,
6c400 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52 54 20 UPDATE, INSERT
6c410 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 */. Expr *pWher
6c420 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 e; /* The
6c430 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 66 6f WHERE clause fo
6c440 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41 r DELETE or UPDA
6c450 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 TE steps */. Ex
6c460 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c 69 73 prList *pExprLis
6c470 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75 73 65 t; /* SET clause
6c480 20 66 6f 72 20 55 50 44 41 54 45 2e 20 20 56 41 for UPDATE. VA
6c490 4c 55 45 53 20 63 6c 61 75 73 65 20 66 6f 72 20 LUES clause for
6c4a0 49 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64 4c 69 INSERT */. IdLi
6c4b0 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 st *pIdList;
6c4c0 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 /* Column names
6c4d0 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 for INSERT */.
6c4e0 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4e TriggerStep *pN
6c4f0 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e ext; /* Next in
6c500 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a the link-list *
6c510 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 /. TriggerStep
6c520 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74 *pLast; /* Last
6c530 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b element in link
6c540 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 6f 72 -list. Valid for
6c550 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 1st elem only *
6c560 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 /.};../*.** The
6c570 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
6c580 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 ure contains inf
6c590 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 ormation used by
6c5a0 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 2e 2e the sqliteFix..
6c5b0 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 73 ..** routines as
6c5c0 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65 20 70 they walk the p
6c5d0 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d 61 6b arse tree to mak
6c5e0 65 20 64 61 74 61 62 61 73 65 20 72 65 66 65 72 e database refer
6c5f0 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 63 69 ences.** explici
6c600 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 t. .*/.typedef
6c610 73 74 72 75 63 74 20 44 62 46 69 78 65 72 20 44 struct DbFixer D
6c620 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74 20 44 bFixer;.struct D
6c630 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72 73 65 bFixer {. Parse
6c640 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 2f *pParse; /
6c650 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f * The parsing co
6c660 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20 6d 65 ntext. Error me
6c670 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 68 ssages written h
6c680 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ere */. const c
6c690 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 har *zDb; /*
6c6a0 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 6f 62 Make sure all ob
6c6b0 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74 61 69 jects are contai
6c6c0 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61 74 61 ned in this data
6c6d0 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 base */. const
6c6e0 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 2f 2a char *zType; /*
6c6f0 20 54 79 70 65 20 6f 66 20 74 68 65 20 63 6f 6e Type of the con
6c700 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f tainer - used fo
6c710 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 r error messages
6c720 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 */. const Toke
6c730 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d n *pName; /* Nam
6c740 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e e of the contain
6c750 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 er - used for er
6c760 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a ror messages */.
6c770 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a };../*.** An obj
6c780 65 63 74 65 64 20 75 73 65 64 20 74 6f 20 61 63 ected used to ac
6c790 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74 65 78 cumulate the tex
6c7a0 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20 77 68 t of a string wh
6c7b0 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 ere we.** do not
6c7c0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b 6e 6f necessarily kno
6c7d0 77 20 68 6f 77 20 62 69 67 20 74 68 65 20 73 74 w how big the st
6c7e0 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 6e 20 ring will be in
6c7f0 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 the end..*/.stru
6c800 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a 20 20 ct StrAccum {.
6c810 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
6c820 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c /* Optional
6c830 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 6c 6f database for lo
6c840 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 62 65 okaside. Can be
6c850 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20 NULL */. char
6c860 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 *zBase;
6c870 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f 63 61 /* A base alloca
6c880 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 tion. Not from
6c890 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 malloc. */. cha
6c8a0 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 20 20 r *zText;
6c8b0 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 /* The string
6c8c0 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 61 72 collected so far
6c8d0 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 61 72 */. int nChar
6c8e0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 ; /* Le
6c8f0 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 ngth of the stri
6c900 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 ng so far */. i
6c910 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 nt nAlloc;
6c920 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 /* Amount of
6c930 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 space allocated
6c940 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 20 69 in zText */. i
6c950 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 nt mxAlloc;
6c960 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 /* Maximum a
6c970 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6c 65 llowed string le
6c980 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 20 6d ngth */. u8 m
6c990 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 2f allocFailed; /
6c9a0 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20 69 * Becomes true i
6c9b0 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 6c 6c f any memory all
6c9c0 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 2a 2f ocation fails */
6c9d0 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c 6c 6f . u8 useMallo
6c9e0 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 c; /* True
6c9f0 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e 6c 61 if zText is enla
6ca00 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20 72 65 rgeable using re
6ca10 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 20 20 alloc */. u8
6ca20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 20 20 tooBig;
6ca30 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20 /* Becomes true
6ca40 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 20 65 if string size e
6ca50 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 2a 2f xceeds limits */
6ca60 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 .};../*.** A poi
6ca70 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 nter to this str
6ca80 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 ucture is used t
6ca90 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 69 6e o communicate in
6caa0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f formation.** fro
6cab0 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 61 6e m sqlite3Init an
6cac0 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 d OP_ParseSchema
6cad0 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 into the sqlite
6cae0 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 3InitCallback..*
6caf0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
6cb00 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
6cb10 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
6cb20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 69 database being i
6cb30 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 nitialized */.
6cb40 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
6cb50 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 61 69 /* 0 for mai
6cb60 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31 20 66 n database. 1 f
6cb70 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 or TEMP, 2.. for
6cb80 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 20 63 ATTACHed */. c
6cb90 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 har **pzErrMsg;
6cba0 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 /* Error mess
6cbb0 61 67 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 age stored here
6cbc0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
6cbd0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 /* Resu
6cbe0 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 20 68 lt code stored h
6cbf0 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 ere */.} InitDat
6cc00 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 a;../*.** Struct
6cc10 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 67 ure containing g
6cc20 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 lobal configurat
6cc30 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 68 65 ion data for the
6cc40 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e SQLite library.
6cc50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 .**.** This stru
6cc60 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e 74 61 cture also conta
6cc70 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 20 69 ins some state i
6cc80 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 nformation..*/.s
6cc90 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e truct Sqlite3Con
6cca0 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d 65 6d fig {. int bMem
6ccb0 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 stat;
6ccc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
6ccd0 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 6d 6f e to enable memo
6cce0 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 20 69 ry status */. i
6ccf0 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b 20 20 nt bCoreMutex;
6cd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6cd10 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 /* True to enab
6cd20 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 6e 67 le core mutexing
6cd30 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c 6c 4d */. int bFullM
6cd40 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 utex;
6cd50 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
6cd60 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20 6d to enable full m
6cd70 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 utexing */. int
6cd80 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 20 20 mxStrlen;
6cd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6cda0 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 * Maximum string
6cdb0 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 6e 74 length */. int
6cdc0 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 szLookaside;
6cdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
6cde0 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 * Default lookas
6cdf0 69 64 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 ide buffer size
6ce00 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 */. int nLookas
6ce10 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ide;
6ce20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c /* Defaul
6ce30 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 t lookaside buff
6ce40 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 er count */. sq
6ce50 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
6ce60 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 s m;
6ce70 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d /* Low-level mem
6ce80 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 ory allocation i
6ce90 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 nterface */. sq
6cea0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
6ceb0 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20 20 20 ods mutex;
6cec0 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 /* Low-level mut
6ced0 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a ex interface */.
6cee0 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 sqlite3_pcache
6cef0 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68 65 3b _methods pcache;
6cf00 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c /* Low-level
6cf10 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e 74 65 page-cache inte
6cf20 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 rface */. void
6cf30 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20 20 20 *pHeap;
6cf40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6cf50 48 65 61 70 20 73 74 6f 72 61 67 65 20 73 70 61 Heap storage spa
6cf60 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 61 ce */. int nHea
6cf70 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
6cf80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
6cf90 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a 2f 0a e of pHeap[] */.
6cfa0 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 52 int mnReq, mxR
6cfb0 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 eq;
6cfc0 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d /* Min and m
6cfd0 61 78 20 68 65 61 70 20 72 65 71 75 65 73 74 73 ax heap requests
6cfe0 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 64 sizes */. void
6cff0 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20 20 20 *pScratch;
6d000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6d010 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 Scratch memory
6d020 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 */. int szScrat
6d030 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ch;
6d040 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
6d050 66 20 65 61 63 68 20 73 63 72 61 74 63 68 20 62 f each scratch b
6d060 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e uffer */. int n
6d070 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 Scratch;
6d080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6d090 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 63 Number of scratc
6d0a0 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 h buffers */. v
6d0b0 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20 20 20 oid *pPage;
6d0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d0d0 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20 6d /* Page cache m
6d0e0 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 emory */. int s
6d0f0 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 zPage;
6d100 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6d110 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 67 Size of each pag
6d120 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a e in pPage[] */.
6d130 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 int nPage;
6d140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d150 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6d160 20 70 61 67 65 73 20 69 6e 20 70 50 61 67 65 5b pages in pPage[
6d170 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 72 ] */. int mxPar
6d180 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 20 20 serStack;
6d190 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 78 69 /* maxi
6d1a0 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 mum depth of the
6d1b0 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 2a 2f parser stack */
6d1c0 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 61 63 . int sharedCac
6d1d0 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 heEnabled;
6d1e0 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 /* true if
6d1f0 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 shared-cache mod
6d200 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 2f e enabled */. /
6d210 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69 67 68 * The above migh
6d220 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 t be initialized
6d230 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 to non-zero. T
6d240 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 he following nee
6d250 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a d to always. **
6d260 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 7a 65 initially be ze
6d270 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a 2f 0a ro, however. */.
6d280 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 int isInit;
6d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d2a0 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 /* True afte
6d2b0 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e r initialization
6d2c0 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 2a 2f has finished */
6d2d0 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 65 73 . int inProgres
6d2e0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
6d2f0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 69 /* True whi
6d300 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f le initializatio
6d310 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f n in progress */
6d320 0a 20 20 69 6e 74 20 69 73 4d 75 74 65 78 49 6e . int isMutexIn
6d330 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 it;
6d340 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 /* True aft
6d350 65 72 20 6d 75 74 65 78 65 73 20 61 72 65 20 69 er mutexes are i
6d360 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 nitialized */.
6d370 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 int isMallocInit
6d380 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6d390 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 /* True after
6d3a0 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 malloc is initia
6d3b0 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 lized */. int i
6d3c0 73 50 43 61 63 68 65 49 6e 69 74 3b 20 20 20 20 sPCacheInit;
6d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6d3e0 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f True after mallo
6d3f0 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 c is initialized
6d400 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 */. sqlite3_mu
6d410 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b tex *pInitMutex;
6d420 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 /* Mutex
6d430 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 used by sqlite3
6d440 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f _initialize() */
6d450 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d . int nRefInitM
6d460 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 utex;
6d470 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
6d480 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e 69 74 f users of pInit
6d490 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a Mutex */.};../*.
6d4a0 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 ** Context point
6d4b0 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74 er passed down t
6d4c0 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d hrough the tree-
6d4d0 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 walk..*/.struct
6d4e0 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28 Walker {. int (
6d4f0 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 29 28 *xExprCallback)(
6d500 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b Walker*, Expr*);
6d510 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b /* Callback
6d520 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 for expressions
6d530 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c */. int (*xSel
6d540 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c ectCallback)(Wal
6d550 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20 ker*,Select*);
6d560 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 /* Callback for
6d570 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 61 72 SELECTs */. Par
6d580 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
6d590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d5a0 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 /* Parser
6d5b0 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20 context. */.
6d5c0 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 union {
6d5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d5e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 /* Ext
6d5f0 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c ra data for call
6d600 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 back */. Name
6d610 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 Context *pNC;
6d620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d630 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 /* Naming
6d640 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 context */.
6d650 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
6d660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
6d680 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 teger value */.
6d690 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 } u;.};../* For
6d6a0 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e ward declaration
6d6b0 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 s */.SQLITE_PRIV
6d6c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
6d6d0 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c alkExpr(Walker*,
6d6e0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
6d6f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6d700 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 te3WalkExprList(
6d710 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 Walker*, ExprLis
6d720 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6d730 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
6d740 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 alkSelect(Walker
6d750 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c *, Select*);.SQL
6d760 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6d770 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 sqlite3WalkSelec
6d780 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53 tExpr(Walker*, S
6d790 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
6d7a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6d7b0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f te3WalkSelectFro
6d7c0 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 m(Walker*, Selec
6d7d0 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 t*);../*.** Retu
6d7e0 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 rn code from the
6d7f0 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b parse-tree walk
6d800 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61 ing primitives a
6d810 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c nd their.** call
6d820 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e backs..*/.#defin
6d830 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 e WRC_Continue
6d840 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 0 /* Continu
6d850 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c e down into chil
6d860 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 dren */.#define
6d870 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20 WRC_Prune
6d880 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 1 /* Omit chil
6d890 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75 dren but continu
6d8a0 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e e walking siblin
6d8b0 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 gs */.#define WR
6d8c0 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 32 20 C_Abort 2
6d8d0 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65 /* Abandon the
6d8e0 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f tree walk */../
6d8f0 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 *.** Assuming zI
6d900 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 n points to the
6d910 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20 first byte of a
6d920 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c UTF-8 character,
6d930 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 .** advance zIn
6d940 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
6d950 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 first byte of th
6d960 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61 e next UTF-8 cha
6d970 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 racter..*/.#defi
6d980 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 ne SQLITE_SKIP_U
6d990 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 TF8(zIn) {
6d9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d9b0 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e \. if( (*(zIn
6d9c0 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 ++))>=0xc0 ){
6d9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d9e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
6d9f0 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 while( (*zIn &
6da00 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 0xc0)==0x80 ){ z
6da10 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 In++; }
6da20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 \. }
6da30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 20 20 20 20 5c 0a 7d \.}
6da60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ../*.** The SQLI
6da70 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 TE_CORRUPT_BKPT
6da80 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65 69 74 macro can be eit
6da90 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 her a constant (
6daa0 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a for production.*
6dab0 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61 20 66 * builds) or a f
6dac0 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f unction call (fo
6dad0 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20 20 49 r debugging). I
6dae0 66 20 69 74 20 69 73 20 61 20 66 75 6e 63 74 69 f it is a functi
6daf0 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 on call,.** it a
6db00 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 61 74 llows the operat
6db10 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 or to set a brea
6db20 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 70 kpoint at the sp
6db30 6f 74 20 77 68 65 72 65 20 64 61 74 61 62 61 73 ot where databas
6db40 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 e.** corruption
6db50 69 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65 is first detecte
6db60 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c d..*/.#ifdef SQL
6db70 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
6db80 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
6db90 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f qlite3Corrupt(vo
6dba0 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 id);.# define SQ
6dbb0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
6dbc0 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 T sqlite3Corrupt
6dbd0 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e ().#else.# defin
6dbe0 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 e SQLITE_CORRUPT
6dbf0 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 _BKPT SQLITE_COR
6dc00 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a RUPT.#endif../*.
6dc10 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68 20 68 ** The ctype.h h
6dc20 65 61 64 65 72 20 69 73 20 6e 65 65 64 65 64 20 eader is needed
6dc30 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20 73 79 for non-ASCII sy
6dc40 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20 61 6c stems. It is al
6dc50 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62 79 20 so.** needed by
6dc60 46 54 53 33 20 77 68 65 6e 20 46 54 53 33 20 69 FTS3 when FTS3 i
6dc70 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 s included in th
6dc80 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a e amalgamation..
6dc90 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
6dca0 53 51 4c 49 54 45 5f 41 53 43 49 49 29 20 7c 7c SQLITE_ASCII) ||
6dcb0 20 5c 0a 20 20 20 20 28 64 65 66 69 6e 65 64 28 \. (defined(
6dcc0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 SQLITE_ENABLE_FT
6dcd0 53 33 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 S3) && defined(S
6dce0 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 QLITE_AMALGAMATI
6dcf0 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c ON)).# include <
6dd00 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69 66 0a ctype.h>.#endif.
6dd10 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
6dd20 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69 6d 69 wing macros mimi
6dd30 63 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c c the standard l
6dd40 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 ibrary functions
6dd50 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a 20 69 toupper(),.** i
6dd60 73 73 70 61 63 65 28 29 2c 20 69 73 61 6c 6e 75 sspace(), isalnu
6dd70 6d 28 29 2c 20 69 73 64 69 67 69 74 28 29 20 61 m(), isdigit() a
6dd80 6e 64 20 69 73 78 64 69 67 69 74 28 29 2c 20 72 nd isxdigit(), r
6dd90 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65 espectively. The
6dda0 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 72 73 69 .** sqlite versi
6ddb0 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f ons only work fo
6ddc0 72 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65 r ASCII characte
6ddd0 72 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f rs, regardless o
6dde0 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 f locale..*/.#if
6ddf0 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 def SQLITE_ASCII
6de00 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6de10 33 54 6f 75 70 70 65 72 28 78 29 20 20 28 28 78 3Toupper(x) ((x
6de20 29 26 7e 28 73 71 6c 69 74 65 33 43 74 79 70 65 )&~(sqlite3Ctype
6de30 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 Map[(unsigned ch
6de40 61 72 29 28 78 29 5d 26 30 78 32 30 29 29 0a 23 ar)(x)]&0x20)).#
6de50 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
6de60 73 73 70 61 63 65 28 78 29 20 20 20 28 73 71 6c sspace(x) (sql
6de70 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e ite3CtypeMap[(un
6de80 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d signed char)(x)]
6de90 26 30 78 30 31 29 0a 23 20 64 65 66 69 6e 65 20 &0x01).# define
6dea0 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 sqlite3Isalnum(x
6deb0 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 ) (sqlite3Ctyp
6dec0 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 eMap[(unsigned c
6ded0 68 61 72 29 28 78 29 5d 26 30 78 30 36 29 0a 23 har)(x)]&0x06).#
6dee0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
6def0 73 61 6c 70 68 61 28 78 29 20 20 20 28 73 71 6c salpha(x) (sql
6df00 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e ite3CtypeMap[(un
6df10 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d signed char)(x)]
6df20 26 30 78 30 32 29 0a 23 20 64 65 66 69 6e 65 20 &0x02).# define
6df30 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 78 sqlite3Isdigit(x
6df40 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 ) (sqlite3Ctyp
6df50 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 eMap[(unsigned c
6df60 68 61 72 29 28 78 29 5d 26 30 78 30 34 29 0a 23 har)(x)]&0x04).#
6df70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
6df80 73 78 64 69 67 69 74 28 78 29 20 20 28 73 71 6c sxdigit(x) (sql
6df90 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e ite3CtypeMap[(un
6dfa0 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d signed char)(x)]
6dfb0 26 30 78 30 38 29 0a 23 20 64 65 66 69 6e 65 20 &0x08).# define
6dfc0 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 sqlite3Tolower(x
6dfd0 29 20 20 20 28 73 71 6c 69 74 65 33 55 70 70 65 ) (sqlite3Uppe
6dfe0 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e rToLower[(unsign
6dff0 65 64 20 63 68 61 72 29 28 78 29 5d 29 0a 23 65 ed char)(x)]).#e
6e000 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c lse.# define sql
6e010 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 ite3Toupper(x)
6e020 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69 67 6e toupper((unsign
6e030 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
6e040 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 efine sqlite3Iss
6e050 70 61 63 65 28 78 29 20 20 20 69 73 73 70 61 63 pace(x) isspac
6e060 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 e((unsigned char
6e070 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 )(x)).# define s
6e080 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 qlite3Isalnum(x)
6e090 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e 73 69 isalnum((unsi
6e0a0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 gned char)(x)).#
6e0b0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 define sqlite3I
6e0c0 73 61 6c 70 68 61 28 78 29 20 20 20 69 73 61 6c salpha(x) isal
6e0d0 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20 63 68 pha((unsigned ch
6e0e0 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 ar)(x)).# define
6e0f0 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
6e100 78 29 20 20 20 69 73 64 69 67 69 74 28 28 75 6e x) isdigit((un
6e110 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 signed char)(x))
6e120 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6e130 33 49 73 78 64 69 67 69 74 28 78 29 20 20 69 73 3Isxdigit(x) is
6e140 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e 65 64 xdigit((unsigned
6e150 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 char)(x)).# def
6e160 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 ine sqlite3Tolow
6e170 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 72 28 er(x) tolower(
6e180 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
6e190 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a x)).#endif../*.*
6e1a0 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 * Internal funct
6e1b0 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a ion prototypes.*
6e1c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
6e1d0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 int sqlite3StrI
6e1e0 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a Cmp(const char *
6e1f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b , const char *);
6e200 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e210 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d int sqlite3IsNum
6e220 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c ber(const char*,
6e230 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 int*, u8);.SQLI
6e240 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6e250 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 qlite3Strlen30(c
6e260 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 64 65 onst char*);.#de
6e270 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 72 4e fine sqlite3StrN
6e280 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 74 72 ICmp sqlite3_str
6e290 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f 50 52 nicmp..SQLITE_PR
6e2a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6e2b0 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 3MallocInit(void
6e2c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e2d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 E void sqlite3Ma
6e2e0 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 llocEnd(void);.S
6e2f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6e300 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f id *sqlite3Mallo
6e310 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 c(int);.SQLITE_P
6e320 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
6e330 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 ite3MallocZero(i
6e340 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6e350 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
6e360 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 3DbMallocZero(sq
6e370 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
6e380 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6e390 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c d *sqlite3DbMall
6e3a0 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 ocRaw(sqlite3*,
6e3b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6e3c0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
6e3d0 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 e3DbStrDup(sqlit
6e3e0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 e3*,const char*)
6e3f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e400 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 char *sqlite3Db
6e410 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a StrNDup(sqlite3*
6e420 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e ,const char*, in
6e430 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6e440 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6e450 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 Realloc(void*, i
6e460 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6e470 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
6e480 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
6e490 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 (sqlite3 *, void
6e4a0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
6e4b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
6e4c0 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 qlite3DbRealloc(
6e4d0 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 sqlite3 *, void
6e4e0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
6e4f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6e500 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 ite3DbFree(sqlit
6e510 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c e3*, void*);.SQL
6e520 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6e530 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a sqlite3MallocSiz
6e540 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 e(void*);.SQLITE
6e550 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6e560 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 ite3DbMallocSize
6e570 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a (sqlite3*, void*
6e580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e590 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 E void *sqlite3S
6e5a0 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 cratchMalloc(int
6e5b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e5c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 E void sqlite3Sc
6e5d0 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29 ratchFree(void*)
6e5e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e5f0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
6e600 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 geMalloc(int);.S
6e610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6e620 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 id sqlite3PageFr
6e630 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 ee(void*);.SQLIT
6e640 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6e650 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 qlite3MemSetDefa
6e660 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ult(void);.SQLIT
6e670 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6e680 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c qlite3BenignMall
6e690 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 ocHooks(void (*)
6e6a0 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 (void), void (*)
6e6b0 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f (void));.SQLITE_
6e6c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6e6d0 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 te3MemoryAlarm(v
6e6e0 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 oid (*)(void*, s
6e6f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e qlite3_int64, in
6e700 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 t), void*, sqlit
6e710 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a e3_int64);../*.*
6e720 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 69 74 * On systems wit
6e730 68 20 61 6d 70 6c 65 20 73 74 61 63 6b 20 73 70 h ample stack sp
6e740 61 63 65 20 61 6e 64 20 74 68 61 74 20 73 75 70 ace and that sup
6e750 70 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c 20 6d port alloca(), m
6e760 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c ake.** use of al
6e770 6c 6f 63 61 28 29 20 74 6f 20 6f 62 74 61 69 6e loca() to obtain
6e780 20 73 70 61 63 65 20 66 6f 72 20 6c 61 72 67 65 space for large
6e790 20 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63 automatic objec
6e7a0 74 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c ts. By default,
6e7b0 0a 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61 63 65 .** obtain space
6e7c0 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a from malloc()..
6e7d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 **.** The alloca
6e7e0 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 () routine never
6e7f0 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 returns NULL.
6e800 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 This will cause
6e810 63 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20 74 68 code paths.** th
6e820 61 74 20 64 65 61 6c 20 77 69 74 68 20 73 71 6c at deal with sql
6e830 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 28 29 ite3StackAlloc()
6e840 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62 65 20 failures to be
6e850 75 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a unreachable..*/.
6e860 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53 #ifdef SQLITE_US
6e870 45 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66 69 6e E_ALLOCA.# defin
6e880 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c e sqlite3StackAl
6e890 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20 61 6c locRaw(D,N) al
6e8a0 6c 6f 63 61 28 4e 29 0a 23 20 64 65 66 69 6e 65 loca(N).# define
6e8b0 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c sqlite3StackAll
6e8c0 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d 65 6d ocZero(D,N) mem
6e8d0 73 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 set(alloca(N), 0
6e8e0 2c 20 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 , N).# define sq
6e8f0 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 44 lite3StackFree(D
6e900 2c 50 29 20 20 20 20 20 20 20 0a 23 65 6c 73 65 ,P) .#else
6e910 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6e920 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 44 3StackAllocRaw(D
6e930 2c 4e 29 20 20 20 73 71 6c 69 74 65 33 44 62 4d ,N) sqlite3DbM
6e940 61 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a 23 20 allocRaw(D,N).#
6e950 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 define sqlite3St
6e960 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e ackAllocZero(D,N
6e970 29 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c ) sqlite3DbMall
6e980 6f 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 64 65 ocZero(D,N).# de
6e990 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 fine sqlite3Stac
6e9a0 6b 46 72 65 65 28 44 2c 50 29 20 20 20 20 20 20 kFree(D,P)
6e9b0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 44 sqlite3DbFree(D
6e9c0 2c 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 ,P).#endif..#ifd
6e9d0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
6e9e0 5f 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f _MEMSYS3.SQLITE_
6e9f0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 PRIVATE const sq
6ea00 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
6ea10 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 s *sqlite3MemGet
6ea20 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 Memsys3(void);.#
6ea30 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c endif.#ifdef SQL
6ea40 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
6ea50 53 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 S5.SQLITE_PRIVAT
6ea60 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f E const sqlite3_
6ea70 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c mem_methods *sql
6ea80 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 ite3MemGetMemsys
6ea90 35 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 5(void);.#endif.
6eaa0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
6eab0 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 _MUTEX_OMIT.SQLI
6eac0 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c TE_PRIVATE sql
6ead0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
6eae0 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 ds *sqlite3Defau
6eaf0 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 ltMutex(void);.S
6eb00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6eb10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 sqlite3_mutex *s
6eb20 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
6eb30 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
6eb40 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
6eb50 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 te3MutexInit(voi
6eb60 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 d);.SQLITE_PRIVA
6eb70 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
6eb80 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a MutexEnd(void);.
6eb90 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
6eba0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6ebb0 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e e3StatusValue(in
6ebc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6ebd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
6ebe0 74 61 74 75 73 41 64 64 28 69 6e 74 2c 20 69 6e tatusAdd(int, in
6ebf0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6ec00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
6ec10 74 61 74 75 73 53 65 74 28 69 6e 74 2c 20 69 6e tatusSet(int, in
6ec20 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 t);..SQLITE_PRIV
6ec30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
6ec40 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 sNaN(double);..S
6ec50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6ec60 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e id sqlite3VXPrin
6ec70 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e tf(StrAccum*, in
6ec80 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 t, const char*,
6ec90 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 va_list);.SQLITE
6eca0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
6ecb0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 qlite3MPrintf(sq
6ecc0 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 lite3*,const cha
6ecd0 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 r*, ...);.SQLITE
6ece0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
6ecf0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 qlite3VMPrintf(s
6ed00 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 qlite3*,const ch
6ed10 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 ar*, va_list);.S
6ed20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 QLITE_PRIVATE ch
6ed30 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 ar *sqlite3MAppe
6ed40 6e 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 ndf(sqlite3*,cha
6ed50 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e r*,const char*,.
6ed60 2e 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 ..);.#if defined
6ed70 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c (SQLITE_TEST) ||
6ed80 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
6ed90 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 DEBUG).SQLITE_PR
6eda0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
6edb0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
6edc0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e const char*, ...
6edd0 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 );.#endif.#if de
6ede0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
6edf0 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 T).SQLITE_PRIVAT
6ee00 45 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 E void *sqlite
6ee10 33 54 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 3TestTextToPtr(c
6ee20 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e onst char*);.#en
6ee30 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
6ee40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
6ee50 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a etString(char **
6ee60 2c 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 , sqlite3*, cons
6ee70 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 t char*, ...);.S
6ee80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6ee90 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d id sqlite3ErrorM
6eea0 73 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 sg(Parse*, const
6eeb0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 char*, ...);.SQ
6eec0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6eed0 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c d sqlite3ErrorCl
6eee0 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ear(Parse*);.SQL
6eef0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6ef00 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 sqlite3Dequote(c
6ef10 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
6ef20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6ef30 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 3KeywordCode(con
6ef40 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
6ef50 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
6ef60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6ef70 74 65 33 52 75 6e 50 61 72 73 65 72 28 50 61 72 te3RunParser(Par
6ef80 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a se*, const char*
6ef90 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 , char **);.SQLI
6efa0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6efb0 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 sqlite3FinishCod
6efc0 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ing(Parse*);.SQL
6efd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6efe0 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
6eff0 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 g(Parse*);.SQLIT
6f000 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f010 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
6f020 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e 74 29 pReg(Parse*,int)
6f030 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f040 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 int sqlite3GetT
6f050 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c empRange(Parse*,
6f060 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6f070 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6f080 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 3ReleaseTempRang
6f090 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 e(Parse*,int,int
6f0a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f0b0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
6f0c0 78 70 72 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33 xprAlloc(sqlite3
6f0d0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 *,int,const Toke
6f0e0 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f n*,int);.SQLITE_
6f0f0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
6f100 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69 74 65 lite3Expr(sqlite
6f110 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 3*,int,const cha
6f120 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6f130 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f140 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 ExprAttachSubtre
6f150 65 73 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 es(sqlite3*,Expr
6f160 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a *,Expr*,Expr*);.
6f170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
6f180 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78 70 xpr *sqlite3PExp
6f190 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 r(Parse*, int, E
6f1a0 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e xpr*, Expr*, con
6f1b0 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 st Token*);.SQLI
6f1c0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
6f1d0 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 *sqlite3ExprAnd(
6f1e0 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 sqlite3*,Expr*,
6f1f0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
6f200 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
6f210 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e ite3ExprFunction
6f220 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 (Parse*,ExprList
6f230 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
6f240 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f250 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 sqlite3ExprAssig
6f260 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 nVarNumber(Parse
6f270 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
6f280 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f290 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 qlite3ExprClear(
6f2a0 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 sqlite3*, Expr*)
6f2b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f2c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
6f2d0 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a rDelete(sqlite3*
6f2e0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6f2f0 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 _PRIVATE ExprLis
6f300 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 t *sqlite3ExprLi
6f310 73 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c stAppend(Parse*,
6f320 45 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 ExprList*,Expr*)
6f330 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f340 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
6f350 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 61 72 rListSetName(Par
6f360 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f se*,ExprList*,To
6f370 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ken*,int);.SQLIT
6f380 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f390 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 qlite3ExprListSe
6f3a0 74 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 tSpan(Parse*,Exp
6f3b0 72 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a rList*,ExprSpan*
6f3c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f3d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
6f3e0 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c prListDelete(sql
6f3f0 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a ite3*, ExprList*
6f400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f410 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 E int sqlite3Ini
6f420 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 t(sqlite3*, char
6f430 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
6f440 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 ATE int sqlite3I
6f450 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 nitCallback(void
6f460 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 *, int, char**,
6f470 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f char**);.SQLITE_
6f480 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6f490 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65 ite3Pragma(Parse
6f4a0 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c *,Token*,Token*,
6f4b0 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c Token*,int);.SQL
6f4c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f4d0 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 sqlite3ResetInt
6f4e0 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 ernalSchema(sqli
6f4f0 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 te3*, int);.SQLI
6f500 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f510 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 sqlite3BeginPars
6f520 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 e(Parse*,int);.S
6f530 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6f540 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 id sqlite3Commit
6f550 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 InternalChanges(
6f560 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
6f570 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 E_PRIVATE Table
6f580 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 *sqlite3ResultSe
6f590 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 2a tOfSelect(Parse*
6f5a0 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 ,Select*);.SQLIT
6f5b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f5c0 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 qlite3OpenMaster
6f5d0 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 Table(Parse *, i
6f5e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6f5f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f600 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 73 65 StartTable(Parse
6f610 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c *,Token*,Token*,
6f620 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 int,int,int,int)
6f630 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f640 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 void sqlite3Add
6f650 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f Column(Parse*,To
6f660 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6f670 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f680 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 e3AddNotNull(Par
6f690 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 se*, int);.SQLIT
6f6a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f6b0 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 qlite3AddPrimary
6f6c0 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 Key(Parse*, Expr
6f6d0 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c List*, int, int,
6f6e0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6f6f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f700 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 e3AddCheckConstr
6f710 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78 70 aint(Parse*, Exp
6f720 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6f730 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f740 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 AddColumnType(Pa
6f750 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 rse*,Token*);.SQ
6f760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f770 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 d sqlite3AddDefa
6f780 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a 2c ultValue(Parse*,
6f790 45 78 70 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 ExprSpan*);.SQLI
6f7a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f7b0 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 sqlite3AddCollat
6f7c0 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f eType(Parse*, To
6f7d0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
6f7e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f7f0 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72 73 65 e3EndTable(Parse
6f800 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c *,Token*,Token*,
6f810 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 Select*);..SQLIT
6f820 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 65 63 E_PRIVATE Bitvec
6f830 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 *sqlite3BitvecC
6f840 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 reate(u32);.SQLI
6f850 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6f860 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
6f870 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a (Bitvec*, u32);.
6f880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6f890 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 nt sqlite3Bitvec
6f8a0 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 Set(Bitvec*, u32
6f8b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f8c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 E void sqlite3Bi
6f8d0 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 tvecClear(Bitvec
6f8e0 2a 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 3b 0a *, u32, void*);.
6f8f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f900 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 oid sqlite3Bitve
6f910 63 44 65 73 74 72 6f 79 28 42 69 74 76 65 63 2a cDestroy(Bitvec*
6f920 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f930 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 69 74 E u32 sqlite3Bit
6f940 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 2a 29 vecSize(Bitvec*)
6f950 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f960 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 int sqlite3Bitv
6f970 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e ecBuiltinTest(in
6f980 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 t,int*);..SQLITE
6f990 5f 50 52 49 56 41 54 45 20 52 6f 77 53 65 74 20 _PRIVATE RowSet
6f9a0 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e *sqlite3RowSetIn
6f9b0 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 it(sqlite3*, voi
6f9c0 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 d*, unsigned int
6f9d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f9e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f E void sqlite3Ro
6f9f0 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65 74 wSetClear(RowSet
6fa00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6fa10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
6fa20 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 77 53 owSetInsert(RowS
6fa30 65 74 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 et*, i64);.SQLIT
6fa40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6fa50 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 28 lite3RowSetTest(
6fa60 52 6f 77 53 65 74 2a 2c 20 75 38 20 69 42 61 74 RowSet*, u8 iBat
6fa70 63 68 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 ch, i64);.SQLITE
6fa80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6fa90 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 ite3RowSetNext(R
6faa0 6f 77 53 65 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a owSet*, i64*);..
6fab0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6fac0 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74 oid sqlite3Creat
6fad0 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b eView(Parse*,Tok
6fae0 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e en*,Token*,Token
6faf0 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e *,Select*,int,in
6fb00 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 t);..#if !define
6fb10 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 d(SQLITE_OMIT_VI
6fb20 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 EW) || !defined(
6fb30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
6fb40 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 UALTABLE).SQLITE
6fb50 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
6fb60 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c qlite3ViewGetCol
6fb70 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c umnNames(Parse*,
6fb80 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 Table*);.#else.#
6fb90 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
6fba0 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 iewGetColumnName
6fbb0 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a s(A,B) 0.#endif.
6fbc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fbd0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 void sqlite3Drop
6fbe0 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 Table(Parse*, Sr
6fbf0 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 cList*, int, int
6fc00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fc10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 E void sqlite3De
6fc20 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c 65 2a leteTable(Table*
6fc30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
6fc40 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 E_OMIT_AUTOINCRE
6fc50 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 MENT.SQLITE_PRIV
6fc60 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
6fc70 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 e3AutoincrementB
6fc80 65 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 egin(Parse *pPar
6fc90 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 se);.SQLITE_PRIV
6fca0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
6fcb0 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 e3AutoincrementE
6fcc0 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 nd(Parse *pParse
6fcd0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
6fce0 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 e sqlite3Autoinc
6fcf0 72 65 6d 65 6e 74 42 65 67 69 6e 28 58 29 0a 23 rementBegin(X).#
6fd00 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 define sqlite3A
6fd10 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 utoincrementEnd(
6fd20 58 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 X).#endif.SQLITE
6fd30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6fd40 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 lite3Insert(Pars
6fd50 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 e*, SrcList*, Ex
6fd60 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a prList*, Select*
6fd70 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b , IdList*, int);
6fd80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fd90 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 void *sqlite3Arr
6fda0 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 ayAllocate(sqlit
6fdb0 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e e3*,void*,int,in
6fdc0 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a t,int*,int*,int*
6fdd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fde0 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 E IdList *sqlite
6fdf0 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 3IdListAppend(sq
6fe00 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c lite3*, IdList*,
6fe10 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
6fe20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6fe30 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 ite3IdListIndex(
6fe40 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 IdList*,const ch
6fe50 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
6fe60 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 VATE SrcList *sq
6fe70 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 lite3SrcListEnla
6fe80 72 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 rge(sqlite3*, Sr
6fe90 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 cList*, int, int
6fea0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6feb0 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 E SrcList *sqlit
6fec0 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 e3SrcListAppend(
6fed0 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 sqlite3*, SrcLis
6fee0 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 t*, Token*, Toke
6fef0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
6ff00 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
6ff10 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e ite3SrcListAppen
6ff20 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a dFromTerm(Parse*
6ff30 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 , SrcList*, Toke
6ff40 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 n*, Token*,.
6ff50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ff70 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 Token*, Select
6ff80 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 *, Expr*, IdList
6ff90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6ffa0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
6ffb0 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 rcListIndexedBy(
6ffc0 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 Parse *, SrcList
6ffd0 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 *, Token *);.SQ
6ffe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6fff0 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 sqlite3IndexedB
70000 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c yLookup(Parse *,
70010 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
70020 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f item *);.SQLITE_
70030 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70040 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 ite3SrcListShift
70050 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 JoinType(SrcList
70060 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70070 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
70080 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 rcListAssignCurs
70090 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c ors(Parse*, SrcL
700a0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
700b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
700c0 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 e3IdListDelete(s
700d0 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a qlite3*, IdList*
700e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
700f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
70100 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 cListDelete(sqli
70110 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b te3*, SrcList*);
70120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70130 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 void sqlite3Crea
70140 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 teIndex(Parse*,T
70150 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 oken*,Token*,Src
70160 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c List*,ExprList*,
70170 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 int,Token*,.
70180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70190 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c Token*, int,
701a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
701b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
701c0 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 e3DropIndex(Pars
701d0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e e*, SrcList*, in
701e0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
701f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 TE int sqlite3Se
70200 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c lect(Parse*, Sel
70210 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 ect*, SelectDest
70220 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70230 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 TE Select *sqlit
70240 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 e3SelectNew(Pars
70250 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 e*,ExprList*,Src
70260 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 List*,Expr*,Expr
70270 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 List*,.
70280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70290 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c Expr*,ExprList*,
702a0 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 int,Expr*,Expr*)
702b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
702c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c void sqlite3Sel
702d0 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 ectDelete(sqlite
702e0 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 3*, Select*);.SQ
702f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 LITE_PRIVATE Tab
70300 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 le *sqlite3SrcLi
70310 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c stLookup(Parse*,
70320 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 SrcList*);.SQLI
70330 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
70340 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 qlite3IsReadOnly
70350 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
70360 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
70370 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
70380 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 e3OpenTable(Pars
70390 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e e*, int iCur, in
703a0 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 t iDb, Table*, i
703b0 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 nt);.#if defined
703c0 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 (SQLITE_ENABLE_U
703d0 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d PDATE_DELETE_LIM
703e0 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 IT) && !defined(
703f0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
70400 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 UERY).SQLITE_PRI
70410 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
70420 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 e3LimitWhere(Par
70430 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c se *, SrcList *,
70440 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 Expr *, ExprLis
70450 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 t *, Expr *, Exp
70460 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 r *, char *);.#e
70470 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
70480 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70490 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 DeleteFrom(Parse
704a0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 *, SrcList*, Exp
704b0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
704c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
704d0 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 Update(Parse*, S
704e0 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 rcList*, ExprLis
704f0 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b t*, Expr*, int);
70500 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70510 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 WhereInfo *sqlit
70520 65 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 e3WhereBegin(Par
70530 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 se*, SrcList*, E
70540 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a xpr*, ExprList**
70550 2c 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 , u16);.SQLITE_P
70560 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70570 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 te3WhereEnd(Wher
70580 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f eInfo*);.SQLITE_
70590 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
705a0 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f te3ExprCodeGetCo
705b0 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 lumn(Parse*, Tab
705c0 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 le*, int, int, i
705d0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
705e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
705f0 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 lite3ExprCodeMov
70600 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 e(Parse*, int, i
70610 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
70620 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70630 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 lite3ExprCodeCop
70640 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 y(Parse*, int, i
70650 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
70660 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70670 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 lite3ExprCacheSt
70680 6f 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c ore(Parse*, int,
70690 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
706a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
706b0 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
706c0 50 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 Push(Parse*);.SQ
706d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
706e0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
706f0 68 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e hePop(Parse*, in
70700 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
70710 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
70720 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 xprCacheRemove(P
70730 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c arse*, int);.SQL
70740 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70750 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
70760 65 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a eClear(Parse*);.
70770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70780 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
70790 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e acheAffinityChan
707a0 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 ge(Parse*, int,
707b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
707c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
707d0 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 3ExprHardCopy(Pa
707e0 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 rse*,int,int);.S
707f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
70800 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
70810 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c e(Parse*, Expr*,
70820 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
70830 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
70840 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 3ExprCodeTemp(Pa
70850 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
70860 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
70880 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 prCodeTarget(Par
70890 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 se*, Expr*, int)
708a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
708b0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
708c0 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 CodeAndCache(Par
708d0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 se*, Expr*, int)
708e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
708f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
70900 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 rCodeConstants(P
70910 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 arse*, Expr*);.S
70920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
70930 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
70940 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a eExprList(Parse*
70950 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 , ExprList*, int
70960 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70970 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70980 74 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61 te3ExprIfTrue(Pa
70990 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
709a0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
709b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
709c0 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 te3ExprIfFalse(P
709d0 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e arse*, Expr*, in
709e0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
709f0 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 PRIVATE Table *s
70a00 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 qlite3FindTable(
70a10 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 sqlite3*,const c
70a20 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 har*, const char
70a30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70a40 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 TE Table *sqlite
70a50 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 3LocateTable(Par
70a60 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 se*,int isView,c
70a70 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 onst char*, cons
70a80 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
70a90 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a _PRIVATE Index *
70aa0 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 sqlite3FindIndex
70ab0 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 (sqlite3*,const
70ac0 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 char*, const cha
70ad0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
70ae0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70af0 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 UnlinkAndDeleteT
70b00 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e able(sqlite3*,in
70b10 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a t,const char*);.
70b20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70b30 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e oid sqlite3Unlin
70b40 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 kAndDeleteIndex(
70b50 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e sqlite3*,int,con
70b60 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
70b70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70b80 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 qlite3Vacuum(Par
70b90 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
70ba0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
70bb0 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a RunVacuum(char**
70bc0 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c , sqlite3*);.SQL
70bd0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
70be0 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f *sqlite3NameFro
70bf0 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c mToken(sqlite3*,
70c00 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
70c10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
70c20 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 ite3ExprCompare(
70c30 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 Expr*, Expr*);.S
70c40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
70c50 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e id sqlite3ExprAn
70c60 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 alyzeAggregates(
70c70 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 NameContext*, Ex
70c80 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
70c90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70ca0 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 3ExprAnalyzeAggL
70cb0 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a ist(NameContext*
70cc0 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c ,ExprList*);.SQL
70cd0 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 ITE_PRIVATE Vdbe
70ce0 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 *sqlite3GetVdbe
70cf0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
70d00 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
70d10 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78 qlite3CreateIdEx
70d20 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 pr(Parse *, cons
70d30 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
70d40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70d50 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 lite3PrngSaveSta
70d60 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 te(void);.SQLITE
70d70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70d80 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 lite3PrngRestore
70d90 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c State(void);.SQL
70da0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70db0 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 sqlite3PrngRese
70dc0 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 tState(void);.SQ
70dd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70de0 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 d sqlite3Rollbac
70df0 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a kAll(sqlite3*);.
70e00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70e10 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 oid sqlite3CodeV
70e20 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 erifySchema(Pars
70e30 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 e*, int);.SQLITE
70e40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70e50 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 lite3BeginTransa
70e60 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e ction(Parse*, in
70e70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
70e80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
70e90 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e ommitTransaction
70ea0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
70eb0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70ec0 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 lite3RollbackTra
70ed0 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 nsaction(Parse*)
70ee0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70ef0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 void sqlite3Sav
70f00 65 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 epoint(Parse*, i
70f10 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c nt, Token*);.SQL
70f20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70f30 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 sqlite3CloseSav
70f40 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 epoints(sqlite3
70f50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70f60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
70f70 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 prIsConstant(Exp
70f80 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
70f90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
70fa0 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 xprIsConstantNot
70fb0 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c Join(Expr*);.SQL
70fc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
70fd0 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
70fe0 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 stantOrFunction(
70ff0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
71000 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
71010 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 e3ExprIsInteger(
71020 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 Expr*, int*);.SQ
71030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
71040 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 sqlite3IsRowid(
71050 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
71060 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71070 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 d sqlite3Generat
71080 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65 eRowDelete(Parse
71090 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 *, Table*, int,
710a0 69 6e 74 2c 20 69 6e 74 2c 20 54 72 69 67 67 65 int, int, Trigge
710b0 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 r *, int);.SQLIT
710c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
710d0 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f qlite3GenerateRo
710e0 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72 wIndexDelete(Par
710f0 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 se*, Table*, int
71100 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f , int*);.SQLITE_
71110 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
71120 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 te3GenerateIndex
71130 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65 Key(Parse*, Inde
71140 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e x*, int, int, in
71150 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
71160 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 TE void sqlite3G
71170 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e enerateConstrain
71180 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54 tChecks(Parse*,T
71190 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20 able*,int,int,.
711a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
711b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
711c0 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74 int*,int,int
711d0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0a ,int,int,int*);.
711e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
711f0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c oid sqlite3Compl
71200 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 61 72 eteInsertion(Par
71210 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 se*, Table*, int
71220 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e 74 , int, int*, int
71230 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
71240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
71250 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 sqlite3OpenTable
71260 41 6e 64 49 6e 64 69 63 65 73 28 50 61 72 73 65 AndIndices(Parse
71270 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 *, Table*, int,
71280 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71290 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
712a0 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 3BeginWriteOpera
712b0 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 tion(Parse*, int
712c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
712d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
712e0 74 65 33 4d 61 79 41 62 6f 72 74 28 50 61 72 73 te3MayAbort(Pars
712f0 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e *);.SQLITE_PRI
71300 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71310 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 3HaltConstraint(
71320 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 68 61 Parse*, int, cha
71330 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
71340 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
71350 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 71 qlite3ExprDup(sq
71360 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 lite3*,Expr*,int
71370 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71380 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 E ExprList *sqli
71390 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 73 te3ExprListDup(s
713a0 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 73 74 qlite3*,ExprList
713b0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
713c0 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a RIVATE SrcList *
713d0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 sqlite3SrcListDu
713e0 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69 p(sqlite3*,SrcLi
713f0 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 st*,int);.SQLITE
71400 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 _PRIVATE IdList
71410 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 *sqlite3IdListDu
71420 70 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c 69 73 p(sqlite3*,IdLis
71430 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
71440 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 ATE Select *sqli
71450 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c te3SelectDup(sql
71460 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e ite3*,Select*,in
71470 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
71480 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 TE void sqlite3F
71490 75 6e 63 44 65 66 49 6e 73 65 72 74 28 46 75 6e uncDefInsert(Fun
714a0 63 44 65 66 48 61 73 68 2a 2c 20 46 75 6e 63 44 cDefHash*, FuncD
714b0 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ef*);.SQLITE_PRI
714c0 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 VATE FuncDef *sq
714d0 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f lite3FindFunctio
714e0 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 n(sqlite3*,const
714f0 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 75 char*,int,int,u
71500 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 8,int);.SQLITE_P
71510 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71520 74 65 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 te3RegisterBuilt
71530 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 inFunctions(sqli
71540 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
71550 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71560 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 e3RegisterDateTi
71570 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 meFunctions(void
71580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71590 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
715a0 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 gisterGlobalFunc
715b0 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23 69 66 tions(void);.#if
715c0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
715d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
715e0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 int sqlite3Saf
715f0 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a 29 3b etyOn(sqlite3*);
71600 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71610 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 int sqlite3Saf
71620 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 2a 29 etyOff(sqlite3*)
71630 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
71640 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
71650 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 (A) 0.# define s
71660 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
71670 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51 4c 49 A) 0.#endif.SQLI
71680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
71690 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 qlite3SafetyChec
716a0 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 kOk(sqlite3*);.S
716b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
716c0 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 t sqlite3SafetyC
716d0 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c heckSickOrOk(sql
716e0 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
716f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71700 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 te3ChangeCookie(
71710 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 Parse*, int);..#
71720 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
71730 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 TE_OMIT_VIEW) &&
71740 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
71750 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53 _OMIT_TRIGGER).S
71760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71770 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 id sqlite3Materi
71780 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73 65 2a alizeView(Parse*
71790 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c , Table*, Expr*,
717a0 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 int);.#endif..#
717b0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
717c0 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 IT_TRIGGER.SQLIT
717d0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
717e0 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 sqlite3BeginTri
717f0 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 6f 6b gger(Parse*, Tok
71800 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 en*,Token*,int,i
71810 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 4c 69 nt,IdList*,SrcLi
71820 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 st*,.
71830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71840 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 29 3b Expr*,int, int);
71850 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71860 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69 void sqlite3Fi
71870 6e 69 73 68 54 72 69 67 67 65 72 28 50 61 72 73 nishTrigger(Pars
71880 65 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a e*, TriggerStep*
71890 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
718a0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
718b0 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 sqlite3DropTrig
718c0 67 65 72 28 50 61 72 73 65 2a 2c 20 53 72 63 4c ger(Parse*, SrcL
718d0 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ist*, int);.SQLI
718e0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
718f0 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 d sqlite3DropTri
71900 67 67 65 72 50 74 72 28 50 61 72 73 65 2a 2c 20 ggerPtr(Parse*,
71910 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 Trigger*);.SQLIT
71920 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 E_PRIVATE Trig
71930 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 ger *sqlite3Trig
71940 67 65 72 73 45 78 69 73 74 28 50 61 72 73 65 20 gersExist(Parse
71950 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 *, Table*, int,
71960 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 20 2a ExprList*, int *
71970 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 pMask);.SQLITE_P
71980 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 RIVATE Trigger
71990 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 *sqlite3Trigger
719a0 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20 54 61 List(Parse *, Ta
719b0 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ble *);.SQLITE_P
719c0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
719d0 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 lite3CodeRowTrig
719e0 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 ger(Parse*, Trig
719f0 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 ger *, int, Expr
71a00 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c List*, int, Tabl
71a10 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 e *,.
71a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71a30 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 int, int, int,
71a40 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 71 6c int);. void sql
71a50 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73 28 iteViewTriggers(
71a60 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 Parse*, Table*,
71a70 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72 Expr*, int, Expr
71a80 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 List*);.SQLITE_P
71a90 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
71aa0 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 lite3DeleteTrigg
71ab0 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c erStep(sqlite3*,
71ac0 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a TriggerStep*);.
71ad0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
71ae0 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 TriggerStep *sq
71af0 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 lite3TriggerSele
71b00 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c ctStep(sqlite3*,
71b10 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 Select*);.SQLITE
71b20 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
71b30 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 erStep *sqlite3T
71b40 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 riggerInsertStep
71b50 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a (sqlite3*,Token*
71b60 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20 , IdList*,.
71b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71b90 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c ExprList*,Sel
71ba0 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49 54 45 ect*,u8);.SQLITE
71bb0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
71bc0 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 erStep *sqlite3T
71bd0 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70 riggerUpdateStep
71be0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a (sqlite3*,Token*
71bf0 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 72 ,ExprList*, Expr
71c00 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 *, u8);.SQLITE_P
71c10 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 RIVATE Trigger
71c20 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 Step *sqlite3Tri
71c30 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 73 ggerDeleteStep(s
71c40 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 qlite3*,Token*,
71c50 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
71c60 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
71c70 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 lite3DeleteTrigg
71c80 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 69 er(sqlite3*, Tri
71c90 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 gger*);.SQLITE_P
71ca0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
71cb0 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 lite3UnlinkAndDe
71cc0 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 leteTrigger(sqli
71cd0 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 te3*,int,const c
71ce0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
71cf0 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c 69 IVATE u32 sqli
71d00 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d 61 73 te3TriggerOldmas
71d10 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67 65 72 k(Parse*,Trigger
71d20 2a 2c 69 6e 74 2c 45 78 70 72 4c 69 73 74 2a 2c *,int,ExprList*,
71d30 54 61 62 6c 65 2a 2c 69 6e 74 29 3b 0a 23 20 64 Table*,int);.# d
71d40 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 efine sqlite3Par
71d50 73 65 54 6f 70 6c 65 76 65 6c 28 70 29 20 28 28 seToplevel(p) ((
71d60 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3f 20 p)->pToplevel ?
71d70 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3a (p)->pToplevel :
71d80 20 28 70 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 (p)).#else.# de
71d90 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67 fine sqlite3Trig
71da0 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c 44 2c gersExist(B,C,D,
71db0 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e 65 20 E,F) 0.# define
71dc0 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 sqlite3DeleteTri
71dd0 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65 66 69 gger(A,B).# defi
71de0 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 ne sqlite3DropTr
71df0 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a 23 20 iggerPtr(A,B).#
71e00 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55 6e define sqlite3Un
71e10 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 linkAndDeleteTri
71e20 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 64 65 gger(A,B,C).# de
71e30 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64 65 fine sqlite3Code
71e40 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 2c 43 RowTrigger(A,B,C
71e50 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c 4a 29 0a ,D,E,F,G,H,I,J).
71e60 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
71e70 54 72 69 67 67 65 72 4c 69 73 74 28 58 2c 20 59 TriggerList(X, Y
71e80 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ) 0.# define sql
71e90 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 ite3ParseTopleve
71ea0 6c 28 70 29 20 70 0a 23 20 64 65 66 69 6e 65 20 l(p) p.# define
71eb0 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4f 6c sqlite3TriggerOl
71ec0 64 6d 61 73 6b 28 41 2c 42 2c 43 2c 44 2c 45 2c dmask(A,B,C,D,E,
71ed0 46 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c F) 0.#endif..SQL
71ee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
71ef0 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 sqlite3JoinType(
71f00 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 Parse*, Token*,
71f10 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b Token*, Token*);
71f20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71f30 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 void sqlite3Crea
71f40 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 teForeignKey(Par
71f50 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 se*, ExprList*,
71f60 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 73 74 Token*, ExprList
71f70 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
71f80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71f90 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e ite3DeferForeign
71fa0 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 Key(Parse*, int)
71fb0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
71fc0 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 _OMIT_AUTHORIZAT
71fd0 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ION.SQLITE_PRIVA
71fe0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
71ff0 33 41 75 74 68 52 65 61 64 28 50 61 72 73 65 2a 3AuthRead(Parse*
72000 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a 2c 53 ,Expr*,Schema*,S
72010 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 rcList*);.SQLITE
72020 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
72030 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 qlite3AuthCheck(
72040 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f 6e 73 Parse*,int, cons
72050 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 t char*, const c
72060 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 har*, const char
72070 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72080 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
72090 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 3AuthContextPush
720a0 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 6f 6e (Parse*, AuthCon
720b0 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 text*, const cha
720c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
720d0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
720e0 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 e3AuthContextPop
720f0 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29 3b 0a (AuthContext*);.
72100 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
72110 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 61 qlite3AuthRead(a
72120 2c 62 2c 63 2c 64 29 0a 23 20 64 65 66 69 6e 65 ,b,c,d).# define
72130 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 sqlite3AuthChec
72140 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 k(a,b,c,d,e)
72150 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69 SQLITE_OK.# defi
72160 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f ne sqlite3AuthCo
72170 6e 74 65 78 74 50 75 73 68 28 61 2c 62 2c 63 29 ntextPush(a,b,c)
72180 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
72190 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 3AuthContextPop(
721a0 61 29 20 20 28 28 76 6f 69 64 29 28 61 29 29 0a a) ((void)(a)).
721b0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 #endif.SQLITE_PR
721c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
721d0 65 33 41 74 74 61 63 68 28 50 61 72 73 65 2a 2c e3Attach(Parse*,
721e0 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 45 Expr*, Expr*, E
721f0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
72200 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
72210 65 33 44 65 74 61 63 68 28 50 61 72 73 65 2a 2c e3Detach(Parse*,
72220 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
72230 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72240 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 te3BtreeFactory(
72250 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64 const sqlite3 *d
72260 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
72270 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 Filename,.
72280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72290 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c int omitJournal
722a0 2c 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 69 6e , int nCache, in
722b0 74 20 66 6c 61 67 73 2c 20 42 74 72 65 65 20 2a t flags, Btree *
722c0 2a 70 70 42 74 72 65 65 29 3b 0a 53 51 4c 49 54 *ppBtree);.SQLIT
722d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
722e0 6c 69 74 65 33 46 69 78 49 6e 69 74 28 44 62 46 lite3FixInit(DbF
722f0 69 78 65 72 2a 2c 20 50 61 72 73 65 2a 2c 20 69 ixer*, Parse*, i
72300 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c nt, const char*,
72310 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a const Token*);.
72320 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
72330 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 nt sqlite3FixSrc
72340 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c 20 53 List(DbFixer*, S
72350 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 rcList*);.SQLITE
72360 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72370 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 44 62 ite3FixSelect(Db
72380 46 69 78 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 Fixer*, Select*)
72390 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
723a0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45 int sqlite3FixE
723b0 78 70 72 28 44 62 46 69 78 65 72 2a 2c 20 45 78 xpr(DbFixer*, Ex
723c0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
723d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
723e0 46 69 78 45 78 70 72 4c 69 73 74 28 44 62 46 69 FixExprList(DbFi
723f0 78 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 xer*, ExprList*)
72400 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72410 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 int sqlite3FixT
72420 72 69 67 67 65 72 53 74 65 70 28 44 62 46 69 78 riggerStep(DbFix
72430 65 72 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 er*, TriggerStep
72440 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72450 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 TE int sqlite3At
72460 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a oF(const char *z
72470 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c 49 , double*);.SQLI
72480 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72490 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 63 qlite3GetInt32(c
724a0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
724b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
724c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 TE int sqlite3Fi
724d0 74 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 tsIn64Bits(const
724e0 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 char *, int);.S
724f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
72500 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 t sqlite3Utf16By
72510 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 teLen(const void
72520 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 43 68 *pData, int nCh
72530 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ar);.SQLITE_PRIV
72540 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 ATE int sqlite3U
72550 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 tf8CharLen(const
72560 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e char *pData, in
72570 74 20 6e 42 79 74 65 29 3b 0a 53 51 4c 49 54 45 t nByte);.SQLITE
72580 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72590 69 74 65 33 55 74 66 38 52 65 61 64 28 63 6f 6e ite3Utf8Read(con
725a0 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38 st u8*, const u8
725b0 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 **);../*.** Rout
725c0 69 6e 65 73 20 74 6f 20 72 65 61 64 20 61 6e 64 ines to read and
725d0 20 77 72 69 74 65 20 76 61 72 69 61 62 6c 65 2d write variable-
725e0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e length integers.
725f0 20 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 0a These used to.
72600 2a 2a 20 62 65 20 64 65 66 69 6e 65 64 20 6c 6f ** be defined lo
72610 63 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77 cally, but now w
72620 65 20 75 73 65 20 74 68 65 20 76 61 72 69 6e 74 e use the varint
72630 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 routines in the
72640 20 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e util.c.** file.
72650 20 20 43 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 Code should us
72660 65 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d e the MACRO form
72670 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20 s below, as the
72680 56 61 72 69 6e 74 33 32 20 76 65 72 73 69 6f 6e Varint32 version
72690 73 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64 20 74 s.** are coded t
726a0 6f 20 61 73 73 75 6d 65 20 74 68 65 20 73 69 6e o assume the sin
726b0 67 6c 65 20 62 79 74 65 20 63 61 73 65 20 69 73 gle byte case is
726c0 20 61 6c 72 65 61 64 79 20 68 61 6e 64 6c 65 64 already handled
726d0 20 28 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20 (which .** the
726e0 4d 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29 MACRO form does)
726f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
72700 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
72710 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 utVarint(unsigne
72720 64 20 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53 d char*, u64);.S
72730 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
72740 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 t sqlite3PutVari
72750 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 nt32(unsigned ch
72760 61 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54 ar*, u32);.SQLIT
72770 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c E_PRIVATE u8 sql
72780 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f ite3GetVarint(co
72790 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
727a0 72 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 51 4c r *, u64 *);.SQL
727b0 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 ITE_PRIVATE u8 s
727c0 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 qlite3GetVarint3
727d0 32 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 2(const unsigned
727e0 20 63 68 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b char *, u32 *);
727f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72800 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e int sqlite3Varin
72810 74 4c 65 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a tLen(u64 v);../*
72820 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 72 20 6f .** The header o
72830 66 20 61 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 f a record consi
72840 73 74 73 20 6f 66 20 61 20 73 65 71 75 65 6e 63 sts of a sequenc
72850 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 e variable-lengt
72860 68 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 h integers..** T
72870 68 65 73 65 20 69 6e 74 65 67 65 72 73 20 61 72 hese integers ar
72880 65 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 e almost always
72890 73 6d 61 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e small and are en
728a0 63 6f 64 65 64 20 61 73 20 61 20 73 69 6e 67 6c coded as a singl
728b0 65 20 62 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 e byte..** The f
728c0 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 ollowing macros
728d0 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 74 take advantage t
728e0 68 69 73 20 66 61 63 74 20 74 6f 20 70 72 6f 76 his fact to prov
728f0 69 64 65 20 61 20 66 61 73 74 20 65 6e 63 6f 64 ide a fast encod
72900 65 0a 2a 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 e.** and decode
72910 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 73 20 of the integers
72920 69 6e 20 61 20 72 65 63 6f 72 64 20 68 65 61 64 in a record head
72930 65 72 2e 20 20 49 74 20 69 73 20 66 61 73 74 65 er. It is faste
72940 72 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e r for the common
72950 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 74 .** case where t
72960 68 65 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 he integer is a
72970 73 69 6e 67 6c 65 20 62 79 74 65 2e 20 20 49 74 single byte. It
72980 20 69 73 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f is a little slo
72990 77 65 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 wer when the.**
729a0 69 6e 74 65 67 65 72 20 69 73 20 74 77 6f 20 6f integer is two o
729b0 72 20 6d 6f 72 65 20 62 79 74 65 73 2e 20 20 42 r more bytes. B
729c0 75 74 20 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 ut overall it is
729d0 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 faster..**.** T
729e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 he following exp
729f0 72 65 73 73 69 6f 6e 73 20 61 72 65 20 65 71 75 ressions are equ
72a00 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 ivalent:.**.**
72a10 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 x = sqlite3Ge
72a20 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 26 42 tVarint32( A, &B
72a30 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 );.** x = s
72a40 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 qlite3PutVarint3
72a50 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 2( A, B );.**.**
72a60 20 20 20 20 20 78 20 3d 20 67 65 74 56 61 72 69 x = getVari
72a70 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a nt32( A, B );.**
72a80 20 20 20 20 20 78 20 3d 20 70 75 74 56 61 72 69 x = putVari
72a90 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a nt32( A, B );.**
72aa0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 .*/.#define getV
72ab0 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75 arint32(A,B) (u
72ac0 38 29 28 28 2a 28 41 29 3c 28 75 38 29 30 78 38 8)((*(A)<(u8)0x8
72ad0 30 29 20 3f 20 28 28 42 29 20 3d 20 28 75 33 32 0) ? ((B) = (u32
72ae0 29 2a 28 41 29 29 2c 31 20 3a 20 73 71 6c 69 74 )*(A)),1 : sqlit
72af0 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 28 41 e3GetVarint32((A
72b00 29 2c 20 28 75 33 32 20 2a 29 26 28 42 29 29 29 ), (u32 *)&(B)))
72b10 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 .#define putVari
72b20 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38 29 28 nt32(A,B) (u8)(
72b30 28 28 75 33 32 29 28 42 29 3c 28 75 33 32 29 30 ((u32)(B)<(u32)0
72b40 78 38 30 29 20 3f 20 28 2a 28 41 29 20 3d 20 28 x80) ? (*(A) = (
72b50 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 42 unsigned char)(B
72b60 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 50 75 )),1 : sqlite3Pu
72b70 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28 tVarint32((A), (
72b80 42 29 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74 B))).#define get
72b90 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 Varint sqlite
72ba0 33 47 65 74 56 61 72 69 6e 74 0a 23 64 65 66 69 3GetVarint.#defi
72bb0 6e 65 20 70 75 74 56 61 72 69 6e 74 20 20 20 20 ne putVarint
72bc0 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
72bd0 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ...SQLITE_PRIVAT
72be0 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 E const char *sq
72bf0 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 lite3IndexAffini
72c00 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e tyStr(Vdbe *, In
72c10 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 dex *);.SQLITE_P
72c20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
72c30 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 te3TableAffinity
72c40 53 74 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c Str(Vdbe *, Tabl
72c50 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e *);.SQLITE_PRI
72c60 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 VATE char sqlite
72c70 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 3CompareAffinity
72c80 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 (Expr *pExpr, ch
72c90 61 72 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45 ar aff2);.SQLITE
72ca0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72cb0 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 ite3IndexAffinit
72cc0 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c yOk(Expr *pExpr,
72cd0 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 char idx_affini
72ce0 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ty);.SQLITE_PRIV
72cf0 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 ATE char sqlite3
72d00 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 ExprAffinity(Exp
72d10 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 r *pExpr);.SQLIT
72d20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
72d30 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 lite3Atoi64(cons
72d40 74 20 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a t char*, i64*);.
72d50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
72d60 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 oid sqlite3Error
72d70 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 (sqlite3*, int,
72d80 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 const char*,...)
72d90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72da0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 void *sqlite3He
72db0 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a xToBlob(sqlite3*
72dc0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c , const char *z,
72dd0 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f int n);.SQLITE_
72de0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72df0 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 50 te3TwoPartName(P
72e00 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c arse *, Token *,
72e10 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 Token *, Token
72e20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
72e30 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
72e40 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e sqlite3ErrStr(in
72e50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
72e60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 TE int sqlite3Re
72e70 61 64 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a adSchema(Parse *
72e80 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f pParse);.SQLITE_
72e90 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
72ea0 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c *sqlite3FindColl
72eb0 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 75 38 20 Seq(sqlite3*,u8
72ec0 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a enc, const char*
72ed0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
72ee0 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
72ef0 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c qlite3LocateColl
72f00 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 Seq(Parse *pPars
72f10 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e e, const char*zN
72f20 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ame);.SQLITE_PRI
72f30 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 VATE CollSeq *sq
72f40 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 lite3ExprCollSeq
72f50 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
72f60 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 Expr *pExpr);.SQ
72f70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
72f80 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65 r *sqlite3ExprSe
72f90 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 tColl(Parse *pPa
72fa0 72 73 65 2c 20 45 78 70 72 20 2a 2c 20 54 6f 6b rse, Expr *, Tok
72fb0 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 en *);.SQLITE_PR
72fc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
72fd0 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61 3CheckCollSeq(Pa
72fe0 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a rse *, CollSeq *
72ff0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73000 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 E int sqlite3Che
73010 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72 ckObjectName(Par
73020 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 se *, const char
73030 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
73040 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73050 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 VdbeSetChanges(s
73060 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0a qlite3 *, int);.
73070 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73080 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
73090 74 65 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c te3ValueText(sql
730a0 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 ite3_value*, u8)
730b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
730c0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 int sqlite3Valu
730d0 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 eBytes(sqlite3_v
730e0 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 alue*, u8);.SQLI
730f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73100 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 sqlite3ValueSetS
73110 74 72 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 tr(sqlite3_value
73120 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f *, int, const vo
73130 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20 id *,u8, .
73140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
73150 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 void(*)(void*)
73160 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73170 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 E void sqlite3Va
73180 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f lueFree(sqlite3_
73190 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
731a0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
731b0 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 value *sqlite3Va
731c0 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a lueNew(sqlite3 *
731d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
731e0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 E char *sqlite3U
731f0 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 tf16to8(sqlite3
73200 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 *, const void*,
73210 69 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c int);.#ifdef SQL
73220 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 ITE_ENABLE_STAT2
73230 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73240 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 char *sqlite3Utf
73250 38 74 6f 31 36 28 73 71 6c 69 74 65 33 20 2a 2c 8to16(sqlite3 *,
73260 20 75 38 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 u8, char *, int
73270 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 6e 64 69 66 , int *);.#endif
73280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73290 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 int sqlite3Value
732a0 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 FromExpr(sqlite3
732b0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 *, Expr *, u8,
732c0 75 38 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 u8, sqlite3_valu
732d0 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 e **);.SQLITE_PR
732e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
732f0 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 e3ValueApplyAffi
73300 6e 69 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c nity(sqlite3_val
73310 75 65 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 ue *, u8, u8);.#
73320 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d ifndef SQLITE_AM
73330 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 ALGAMATION.SQLIT
73340 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
73350 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 unsigned char sq
73360 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
73370 72 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r[];.SQLITE_PRIV
73380 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e ATE const unsign
73390 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 ed char sqlite3C
733a0 74 79 70 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54 typeMap[];.SQLIT
733b0 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 E_PRIVATE SQLITE
733c0 5f 57 53 44 20 73 74 72 75 63 74 20 53 71 6c 69 _WSD struct Sqli
733d0 74 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 te3Config sqlite
733e0 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f 3Config;.SQLITE_
733f0 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 PRIVATE SQLITE_W
73400 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 SD FuncDefHash s
73410 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 qlite3GlobalFunc
73420 74 69 6f 6e 73 3b 0a 53 51 4c 49 54 45 5f 50 52 tions;.SQLITE_PR
73430 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
73440 33 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a 23 65 3PendingByte;.#e
73450 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 ndif.SQLITE_PRIV
73460 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73470 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62 RootPageMoved(Db
73480 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
73490 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
734a0 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 d sqlite3Reindex
734b0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c (Parse*, Token*,
734c0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
734d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
734e0 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 lite3AlterFuncti
734f0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 ons(sqlite3*);.S
73500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73510 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52 id sqlite3AlterR
73520 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 73 65 enameTable(Parse
73530 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b *, SrcList*, Tok
73540 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
73550 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
73560 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 GetToken(const u
73570 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 nsigned char *,
73580 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 int *);.SQLITE_P
73590 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
735a0 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 50 te3NestedParse(P
735b0 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 arse*, const cha
735c0 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 r*, ...);.SQLITE
735d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
735e0 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
735f0 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71 redStatements(sq
73600 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f lite3*);.SQLITE_
73610 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
73620 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 ite3CodeSubselec
73630 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 t(Parse *, Expr
73640 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
73650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
73660 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 d sqlite3SelectP
73670 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 rep(Parse*, Sele
73680 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 ct*, NameContext
73690 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
736a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 TE int sqlite3Re
736b0 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 4e solveExprNames(N
736c0 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 ameContext*, Exp
736d0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
736e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
736f0 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d ResolveSelectNam
73700 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 es(Parse*, Selec
73710 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a t*, NameContext*
73720 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73730 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 E int sqlite3Res
73740 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 olveOrderGroupBy
73750 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a (Parse*, Select*
73760 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e , ExprList*, con
73770 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
73780 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
73790 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 qlite3ColumnDefa
737a0 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c ult(Vdbe *, Tabl
737b0 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a e *, int, int);.
737c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
737d0 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 oid sqlite3Alter
737e0 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 FinishAddColumn(
737f0 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a Parse *, Token *
73800 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73810 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c E void sqlite3Al
73820 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d terBeginAddColum
73830 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 n(Parse *, SrcLi
73840 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 st *);.SQLITE_PR
73850 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 IVATE CollSeq *s
73860 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 qlite3GetCollSeq
73870 28 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c 20 43 (sqlite3*, u8, C
73880 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 ollSeq *, const
73890 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
738a0 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 RIVATE char sqli
738b0 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 te3AffinityType(
738c0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
738d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
738e0 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 d sqlite3Analyze
738f0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c (Parse*, Token*,
73900 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
73910 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
73920 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 ite3InvokeBusyHa
73930 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65 ndler(BusyHandle
73940 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
73950 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
73960 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 2c 20 indDb(sqlite3*,
73970 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
73980 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73990 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 te3FindDbName(sq
739a0 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 lite3 *, const c
739b0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har *);.SQLITE_P
739c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
739d0 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 e3AnalysisLoad(s
739e0 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44 42 29 qlite3*,int iDB)
739f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73a00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c void sqlite3Del
73a10 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 eteIndexSamples(
73a20 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f Index*);.SQLITE_
73a30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
73a40 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 ite3DefaultRowEs
73a50 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 t(Index*);.SQLIT
73a60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
73a70 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 qlite3RegisterLi
73a80 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 keFunctions(sqli
73a90 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 te3*, int);.SQLI
73aa0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
73ab0 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 qlite3IsLikeFunc
73ac0 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45 78 tion(sqlite3*,Ex
73ad0 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 3b pr*,int*,char*);
73ae0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73af0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 void sqlite3Mini
73b00 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 mumFileFormat(Pa
73b10 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b rse*, int, int);
73b20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73b30 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65 void sqlite3Sche
73b40 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a maFree(void *);.
73b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
73b60 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53 63 chema *sqlite3Sc
73b70 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33 20 hemaGet(sqlite3
73b80 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c *, Btree *);.SQL
73b90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
73ba0 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
73bb0 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 ndex(sqlite3 *db
73bc0 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 51 4c , Schema *);.SQL
73bd0 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79 49 ITE_PRIVATE KeyI
73be0 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 nfo *sqlite3Inde
73bf0 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20 2a xKeyinfo(Parse *
73c00 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 , Index *);.SQLI
73c10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
73c20 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 qlite3CreateFunc
73c30 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 (sqlite3 *, cons
73c40 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 t char *, int, i
73c50 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 20 76 nt, void *, . v
73c60 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f oid (*)(sqlite3_
73c70 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
73c80 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a ite3_value **),.
73c90 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 void (*)(sqlit
73ca0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
73cb0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
73cc0 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 ), void (*)(sqli
73cd0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a te3_context*));.
73ce0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
73cf0 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 nt sqlite3ApiExi
73d00 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 t(sqlite3 *db, i
73d10 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
73d20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
73d30 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 penTempDatabase(
73d40 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 Parse *);..SQLIT
73d50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
73d60 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e qlite3StrAccumIn
73d70 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 68 it(StrAccum*, ch
73d80 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a ar*, int, int);.
73d90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73da0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 oid sqlite3StrAc
73db0 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63 cumAppend(StrAcc
73dc0 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c um*,const char*,
73dd0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
73de0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
73df0 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 e3StrAccumFinish
73e00 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c (StrAccum*);.SQL
73e10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73e20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
73e30 52 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29 Reset(StrAccum*)
73e40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c void sqlite3Sel
73e60 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65 ectDestInit(Sele
73e70 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 ctDest*,int,int)
73e80 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
73e90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 E void sqlite3Ba
73ea0 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c 69 ckupRestart(sqli
73eb0 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53 te3_backup *);.S
73ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73ed0 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 id sqlite3Backup
73ee0 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f 62 Update(sqlite3_b
73ef0 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63 ackup *, Pgno, c
73f00 6f 6e 73 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a onst u8 *);../*.
73f10 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 ** The interface
73f20 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 to the LEMON-ge
73f30 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 0a 2a nerated parser.*
73f40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
73f50 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
73f60 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 rserAlloc(void*(
73f70 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c *)(size_t));.SQL
73f80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73f90 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72 sqlite3ParserFr
73fa0 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a ee(void*, void(*
73fb0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
73fc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
73fd0 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 6f 69 qlite3Parser(voi
73fe0 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 d*, int, Token,
73ff0 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 Parse*);.#ifdef
74000 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 YYTRACKMAXSTACKD
74010 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 EPTH.SQLITE_PRIV
74020 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
74030 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b 3ParserStackPeak
74040 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a (void*);.#endif.
74050 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74060 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 6f void sqlite3Auto
74070 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 LoadExtensions(s
74080 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 qlite3*);.#ifnde
74090 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
740a0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c AD_EXTENSION.SQL
740b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
740c0 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 id sqlite3CloseE
740d0 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 xtensions(sqlite
740e0 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 3*);.#else.# def
740f0 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 ine sqlite3Close
74100 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 Extensions(X).#e
74110 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
74120 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
74130 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 _CACHE.SQLITE_PR
74140 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
74150 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 ite3TableLock(Pa
74160 72 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c rse *, int, int,
74170 20 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 u8, const char
74180 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 *);.#else. #def
74190 69 6e 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 ine sqlite3Table
741a0 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a Lock(v,w,x,y,z).
741b0 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
741c0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
741d0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
741e0 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75 sqlite3Utf8To8(u
741f0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a nsigned char*);.
74200 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
74210 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
74220 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e ALTABLE.# defin
74230 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 e sqlite3VtabCle
74240 61 72 28 59 29 0a 23 20 20 64 65 66 69 6e 65 20 ar(Y).# define
74250 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 sqlite3VtabSync(
74260 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 X,Y) SQLITE_OK.#
74270 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
74280 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a VtabRollback(X).
74290 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 # define sqlite
742a0 33 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 3VtabCommit(X).#
742b0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
742c0 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 30 VtabInSync(db) 0
742d0 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
742e0 65 33 56 74 61 62 4c 6f 63 6b 28 58 29 20 0a 23 e3VtabLock(X) .#
742f0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
74300 56 74 61 62 55 6e 6c 6f 63 6b 28 58 29 0a 23 20 VtabUnlock(X).#
74310 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
74320 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 58 29 tabUnlockList(X)
74330 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 .#else.SQLITE_PR
74340 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 IVATE void sq
74350 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54 lite3VtabClear(T
74360 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 able*);.SQLITE_P
74370 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 RIVATE int sq
74380 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 lite3VtabSync(sq
74390 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 lite3 *db, char
743a0 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
743b0 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 ATE int sqlit
743c0 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 e3VtabRollback(s
743d0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c qlite3 *db);.SQL
743e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 ITE_PRIVATE i
743f0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f nt sqlite3VtabCo
74400 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 mmit(sqlite3 *db
74410 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74420 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 E void sqlite
74430 33 56 74 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65 3VtabLock(VTable
74440 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
74450 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 ATE void sqli
74460 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 te3VtabUnlock(VT
74470 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f able *);.SQLITE_
74480 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 PRIVATE void
74490 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 sqlite3VtabUnloc
744a0 6b 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 29 3b kList(sqlite3*);
744b0 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
744c0 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 e3VtabInSync(db)
744d0 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e ((db)->nVTrans>
744e0 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61 0 && (db)->aVTra
744f0 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51 ns==0).#endif.SQ
74500 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
74510 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b d sqlite3VtabMak
74520 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a eWritable(Parse*
74530 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 ,Table*);.SQLITE
74540 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74550 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 lite3VtabBeginPa
74560 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 rse(Parse*, Toke
74570 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 n*, Token*, Toke
74580 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
74590 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
745a0 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 VtabFinishParse(
745b0 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b Parse*, Token*);
745c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
745d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
745e0 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a 29 3b ArgInit(Parse*);
745f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74600 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
74610 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 65 2a ArgExtend(Parse*
74620 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
74630 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
74640 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 lite3VtabCallCre
74650 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e ate(sqlite3*, in
74660 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c t, const char *,
74670 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 char **);.SQLIT
74680 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
74690 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e lite3VtabCallCon
746a0 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54 61 62 nect(Parse*, Tab
746b0 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 le*);.SQLITE_PRI
746c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
746d0 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 VtabCallDestroy(
746e0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 sqlite3*, int, c
746f0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 onst char *);.SQ
74700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
74710 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 sqlite3VtabBegi
74720 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 56 54 61 n(sqlite3 *, VTa
74730 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ble *);.SQLITE_P
74740 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a RIVATE FuncDef *
74750 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c sqlite3VtabOverl
74760 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 oadFunction(sqli
74770 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 te3 *,FuncDef*,
74780 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 2a 29 int nArg, Expr*)
74790 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
747a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76 void sqlite3Inv
747b0 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c alidFunction(sql
747c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
747d0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
747e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
747f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 TE int sqlite3Tr
74800 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73 ansferBindings(s
74810 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73 qlite3_stmt *, s
74820 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a qlite3_stmt *);.
74830 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74840 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 nt sqlite3Reprep
74850 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 are(Vdbe*);.SQLI
74860 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
74870 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 sqlite3ExprListC
74880 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 65 heckLength(Parse
74890 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f *, ExprList*, co
748a0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
748b0 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
748c0 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 eq *sqlite3Binar
748d0 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 yCompareCollSeq(
748e0 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c Parse *, Expr *,
748f0 20 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54 45 Expr *);.SQLITE
74900 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
74910 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 ite3TempInMemory
74920 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 2a 29 (const sqlite3*)
74930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74940 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 VTable *sqlite3
74950 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65 GetVTable(sqlite
74960 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 0a 0a 0a 3*, Table*);....
74970 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 6c 65 20 /*.** Available
74980 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72 73 2e fault injectors.
74990 20 20 53 68 6f 75 6c 64 20 62 65 20 6e 75 6d 62 Should be numb
749a0 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 ered beginning w
749b0 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e ith 0..*/.#defin
749c0 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e e SQLITE_FAULTIN
749d0 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 JECTOR_MALLOC
749e0 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 0.#define SQLI
749f0 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 TE_FAULTINJECTOR
74a00 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 0a 0a 2f _COUNT 1../
74a10 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 *.** The interfa
74a20 63 65 20 74 6f 20 74 68 65 20 63 6f 64 65 20 69 ce to the code i
74a30 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 64 20 66 n fault.c used f
74a40 6f 72 20 69 64 65 6e 74 69 66 79 69 6e 67 20 22 or identifying "
74a50 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f benign".** mallo
74a60 63 20 66 61 69 6c 75 72 65 73 2e 20 54 68 69 73 c failures. This
74a70 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 65 6e 74 is only present
74a80 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f if SQLITE_OMIT_
74a90 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 BUILTIN_TEST.**
74aa0 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a is not defined..
74ab0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
74ac0 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 E_OMIT_BUILTIN_T
74ad0 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 EST.SQLITE_PRIVA
74ae0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
74af0 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
74b00 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 oc(void);.SQLITE
74b10 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
74b20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
74b30 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 65 Malloc(void);.#e
74b40 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
74b50 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e lite3BeginBenign
74b60 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 65 66 69 Malloc(). #defi
74b70 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e ne sqlite3EndBen
74b80 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 ignMalloc().#end
74b90 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 if..#define IN_I
74ba0 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 20 20 20 NDEX_ROWID
74bb0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 49 1.#define I
74bc0 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 20 20 20 N_INDEX_EPH
74bd0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 2.#defin
74be0 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 e IN_INDEX_INDEX
74bf0 20 20 20 20 20 20 20 20 20 20 20 33 0a 53 51 4c 3.SQL
74c00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
74c10 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 sqlite3FindInInd
74c20 65 78 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 ex(Parse *, Expr
74c30 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 *, int*);..#ifd
74c40 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
74c50 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51 _ATOMIC_WRITE.SQ
74c60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
74c70 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 nt sqlite3Journa
74c80 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 lOpen(sqlite3_vf
74c90 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 s *, const char
74ca0 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 *, sqlite3_file
74cb0 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 *, int, int);.SQ
74cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
74cd0 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 nt sqlite3Journa
74ce0 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 lSize(sqlite3_vf
74cf0 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 s *);.SQLITE_PRI
74d00 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
74d10 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 e3JournalCreate(
74d20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b sqlite3_file *);
74d30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
74d40 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
74d50 69 7a 65 28 70 56 66 73 29 20 28 28 70 56 66 73 ize(pVfs) ((pVfs
74d60 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e )->szOsFile).#en
74d70 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 dif..SQLITE_PRIV
74d80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
74d90 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 MemJournalOpen(s
74da0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a qlite3_file *);.
74db0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74dc0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 nt sqlite3MemJou
74dd0 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 3b 0a rnalSize(void);.
74de0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74df0 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a nt sqlite3IsMemJ
74e00 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 ournal(sqlite3_f
74e10 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 53 51 4c ile *);..#if SQL
74e20 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 ITE_MAX_EXPR_DEP
74e30 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 TH>0.SQLITE_PRIV
74e40 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
74e50 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 e3ExprSetHeight(
74e60 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
74e70 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f xpr *p);.SQLITE_
74e80 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
74e90 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 lite3SelectExprH
74ea0 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a 29 3b eight(Select *);
74eb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74ec0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 int sqlite3Exp
74ed0 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 rCheckHeight(Par
74ee0 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 se*, int);.#else
74ef0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
74f00 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 e3ExprSetHeight(
74f10 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 20 73 x,y). #define s
74f20 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 qlite3SelectExpr
74f30 48 65 69 67 68 74 28 78 29 20 30 0a 20 20 23 64 Height(x) 0. #d
74f40 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 efine sqlite3Exp
74f50 72 43 68 65 63 6b 48 65 69 67 68 74 28 78 2c 79 rCheckHeight(x,y
74f60 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 ).#endif..SQLITE
74f70 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c _PRIVATE u32 sql
74f80 69 74 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e ite3Get4byte(con
74f90 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f st u8*);.SQLITE_
74fa0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
74fb0 69 74 65 33 50 75 74 34 62 79 74 65 28 75 38 2a ite3Put4byte(u8*
74fc0 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65 66 20 , u32);..#ifdef
74fd0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e SQLITE_ENABLE_UN
74fe0 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 LOCK_NOTIFY.SQLI
74ff0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75000 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 d sqlite3Connect
75010 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74 ionBlocked(sqlit
75020 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 20 2a 29 e3 *, sqlite3 *)
75030 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75040 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 void sqlite3C
75050 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 onnectionUnlocke
75060 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a d(sqlite3 *db);.
75070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
75080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e void sqlite3Con
75090 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71 nectionClosed(sq
750a0 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6c 73 lite3 *db);.#els
750b0 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 e. #define sqli
750c0 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f te3ConnectionBlo
750d0 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 64 65 66 cked(x,y). #def
750e0 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 ine sqlite3Conne
750f0 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 ctionUnlocked(x)
75100 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
75110 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 e3ConnectionClos
75120 65 64 28 78 29 0a 23 65 6e 64 69 66 0a 0a 23 69 ed(x).#endif..#i
75130 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
75140 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 G.SQLITE_PRIVATE
75150 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 void sqlite3P
75160 61 72 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a arserTrace(FILE*
75170 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69 , char *);.#endi
75180 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 f../*.** If the
75190 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20 49 4f SQLITE_ENABLE IO
751a0 54 52 41 43 45 20 65 78 69 73 74 73 20 74 68 65 TRACE exists the
751b0 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 n the global var
751c0 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 65 33 iable.** sqlite3
751d0 49 6f 54 72 61 63 65 20 69 73 20 61 20 70 6f 69 IoTrace is a poi
751e0 6e 74 65 72 20 74 6f 20 61 20 70 72 69 6e 74 66 nter to a printf
751f0 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 20 75 73 -like routine us
75200 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 ed to.** print I
75210 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 73 73 61 /O tracing messa
75220 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 66 20 ges. .*/.#ifdef
75230 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f SQLITE_ENABLE_IO
75240 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49 TRACE.# define I
75250 4f 54 52 41 43 45 28 41 29 20 20 69 66 28 20 73 OTRACE(A) if( s
75260 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 29 7b qlite3IoTrace ){
75270 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 sqlite3IoTrace
75280 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 A; }.SQLITE_PRIV
75290 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
752a0 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c e3VdbeIOTraceSql
752b0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
752c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 PRIVATE void (*s
752d0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 qlite3IoTrace)(c
752e0 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b onst char*,...);
752f0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
75300 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64 65 66 IOTRACE(A).# def
75310 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 49 ine sqlite3VdbeI
75320 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e OTraceSql(X).#en
75330 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a dif..#endif../**
75340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
75350 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 of sqliteInt.h
75360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
75390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
753a0 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 in file global.c
753b0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
753c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
753d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
753e0 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 33 0a ** 2008 June 13.
753f0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
75400 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
75410 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
75420 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
75430 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
75440 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
75450 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
75460 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
75470 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
75480 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
75490 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
754a0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
754b0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
754c0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
754d0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
754e0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
754f0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
75500 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
75510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
75550 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
75560 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 ontains definiti
75570 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 ons of global va
75580 72 69 61 62 6c 65 73 20 61 6e 64 20 63 6f 6e 74 riables and cont
75590 61 6e 74 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6e ants..*/.../* An
755a0 20 61 72 72 61 79 20 74 6f 20 6d 61 70 20 61 6c array to map al
755b0 6c 20 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 l upper-case cha
755c0 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65 racters into the
755d0 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ir corresponding
755e0 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 .** lower-case c
755f0 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a haracter. .**.**
75600 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f 6e SQLite only con
75610 73 69 64 65 72 73 20 55 53 2d 41 53 43 49 49 20 siders US-ASCII
75620 28 6f 72 20 45 42 43 44 49 43 29 20 63 68 61 72 (or EBCDIC) char
75630 61 63 74 65 72 73 2e 20 20 57 65 20 64 6f 20 6e acters. We do n
75640 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 61 73 ot.** handle cas
75650 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 66 6f e conversions fo
75660 72 20 74 68 65 20 55 54 46 20 63 68 61 72 61 63 r the UTF charac
75670 74 65 72 20 73 65 74 20 73 69 6e 63 65 20 74 68 ter set since th
75680 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f e tables.** invo
75690 6c 76 65 64 20 61 72 65 20 6e 65 61 72 6c 79 20 lved are nearly
756a0 61 73 20 62 69 67 20 6f 72 20 62 69 67 67 65 72 as big or bigger
756b0 20 74 68 61 6e 20 53 51 4c 69 74 65 20 69 74 73 than SQLite its
756c0 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 elf..*/.SQLITE_P
756d0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 RIVATE const uns
756e0 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 igned char sqlit
756f0 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d e3UpperToLower[]
75700 20 3d 20 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 = {.#ifdef SQLI
75710 54 45 5f 41 53 43 49 49 0a 20 20 20 20 20 20 30 TE_ASCII. 0
75720 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 , 1, 2, 3, 4
75730 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 , 5, 6, 7, 8
75740 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 , 9, 10, 11, 12
75750 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 31 36 , 13, 14, 15, 16
75760 2c 20 31 37 2c 0a 20 20 20 20 20 31 38 2c 20 31 , 17,. 18, 1
75770 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 9, 20, 21, 22, 2
75780 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 3, 24, 25, 26, 2
75790 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 7, 28, 29, 30, 3
757a0 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 1, 32, 33, 34, 3
757b0 35 2c 0a 20 20 20 20 20 33 36 2c 20 33 37 2c 20 5,. 36, 37,
757c0 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 38, 39, 40, 41,
757d0 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 42, 43, 44, 45,
757e0 34 36 2c 20 34 37 2c 20 34 38 2c 20 34 39 2c 20 46, 47, 48, 49,
757f0 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 0a 50, 51, 52, 53,.
75800 20 20 20 20 20 35 34 2c 20 35 35 2c 20 35 36 2c 54, 55, 56,
75810 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 57, 58, 59, 60,
75820 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 36 34 2c 61, 62, 63, 64,
75830 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 97, 98, 99,100,
75840 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a 20 20 20 101,102,103,.
75850 20 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 104,105,106,107
75860 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 ,108,109,110,111
75870 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 ,112,113,114,115
75880 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 ,116,117,118,119
75890 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20 20 31 32 ,120,121,. 12
758a0 32 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 2, 91, 92, 93, 9
758b0 34 2c 20 39 35 2c 20 39 36 2c 20 39 37 2c 20 39 4, 95, 96, 97, 9
758c0 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 8, 99,100,101,10
758d0 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 31 30 2,103,104,105,10
758e0 36 2c 31 30 37 2c 0a 20 20 20 20 31 30 38 2c 31 6,107,. 108,1
758f0 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 09,110,111,112,1
75900 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 13,114,115,116,1
75910 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 17,118,119,120,1
75920 32 31 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 21,122,123,124,1
75930 32 35 2c 0a 20 20 20 20 31 32 36 2c 31 32 37 2c 25,. 126,127,
75940 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 128,129,130,131,
75950 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 132,133,134,135,
75960 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 136,137,138,139,
75970 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 140,141,142,143,
75980 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 . 144,145,146
75990 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 ,147,148,149,150
759a0 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 ,151,152,153,154
759b0 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 38 ,155,156,157,158
759c0 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c 0a 20 20 ,159,160,161,.
759d0 20 20 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 162,163,164,16
759e0 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 5,166,167,168,16
759f0 39 2c 31 37 30 2c 31 37 31 2c 31 37 32 2c 31 37 9,170,171,172,17
75a00 33 2c 31 37 34 2c 31 37 35 2c 31 37 36 2c 31 37 3,174,175,176,17
75a10 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20 20 20 31 7,178,179,. 1
75a20 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 80,181,182,183,1
75a30 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 84,185,186,187,1
75a40 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 31 88,189,190,191,1
75a50 39 32 2c 31 39 33 2c 31 39 34 2c 31 39 35 2c 31 92,193,194,195,1
75a60 39 36 2c 31 39 37 2c 0a 20 20 20 20 31 39 38 2c 96,197,. 198,
75a70 31 39 39 2c 32 30 30 2c 32 30 31 2c 32 30 32 2c 199,200,201,202,
75a80 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 203,204,205,206,
75a90 32 30 37 2c 32 30 38 2c 32 30 39 2c 32 31 30 2c 207,208,209,210,
75aa0 32 31 31 2c 32 31 32 2c 32 31 33 2c 32 31 34 2c 211,212,213,214,
75ab0 32 31 35 2c 0a 20 20 20 20 32 31 36 2c 32 31 37 215,. 216,217
75ac0 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 ,218,219,220,221
75ad0 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c 32 32 35 ,222,223,224,225
75ae0 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c 32 32 39 ,226,227,228,229
75af0 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c 32 33 33 ,230,231,232,233
75b00 2c 0a 20 20 20 20 32 33 34 2c 32 33 35 2c 32 33 ,. 234,235,23
75b10 36 2c 32 33 37 2c 32 33 38 2c 32 33 39 2c 32 34 6,237,238,239,24
75b20 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 0,241,242,243,24
75b30 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 4,245,246,247,24
75b40 38 2c 32 34 39 2c 32 35 30 2c 32 35 31 2c 0a 20 8,249,250,251,.
75b50 20 20 20 32 35 32 2c 32 35 33 2c 32 35 34 2c 32 252,253,254,2
75b60 35 35 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 55.#endif.#ifdef
75b70 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 SQLITE_EBCDIC.
75b80 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 0, 1, 2,
75b90 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 3, 4, 5, 6,
75ba0 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 7, 8, 9, 10,
75bb0 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 11, 12, 13, 14,
75bc0 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 15, /* 0x */.
75bd0 20 20 31 36 2c 20 31 37 2c 20 31 38 2c 20 31 39 16, 17, 18, 19
75be0 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 , 20, 21, 22, 23
75bf0 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 , 24, 25, 26, 27
75c00 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 , 28, 29, 30, 31
75c10 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20 20 , /* 1x */.
75c20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 20 32, 33, 34, 35,
75c30 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 36, 37, 38, 39,
75c40 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 40, 41, 42, 43,
75c50 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 44, 45, 46, 47,
75c60 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 20 34 38 /* 2x */. 48
75c70 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 , 49, 50, 51, 52
75c80 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c 20 35 36 , 53, 54, 55, 56
75c90 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 , 57, 58, 59, 60
75ca0 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 2f 2a , 61, 62, 63, /*
75cb0 20 33 78 20 2a 2f 0a 20 20 20 20 20 36 34 2c 20 3x */. 64,
75cc0 36 35 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 65, 66, 67, 68,
75cd0 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 69, 70, 71, 72,
75ce0 37 33 2c 20 37 34 2c 20 37 35 2c 20 37 36 2c 20 73, 74, 75, 76,
75cf0 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f 2a 20 34 77, 78, 79, /* 4
75d00 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c 20 38 31 x */. 80, 81
75d10 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 , 82, 83, 84, 85
75d20 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 , 86, 87, 88, 89
75d30 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c 20 39 33 , 90, 91, 92, 93
75d40 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20 35 78 20 , 94, 95, /* 5x
75d50 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39 37 2c 20 */. 96, 97,
75d60 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 66, 67, 68, 69,
75d70 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 31 70, 71, 72, 73,1
75d80 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 06,107,108,109,1
75d90 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 10,111, /* 6x */
75da0 0a 20 20 20 20 31 31 32 2c 20 38 31 2c 20 38 32 . 112, 81, 82
75db0 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 , 83, 84, 85, 86
75dc0 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 31 32 32 , 87, 88, 89,122
75dd0 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 31 32 36 ,123,124,125,126
75de0 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 ,127, /* 7x */.
75df0 20 20 20 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 128,129,130,1
75e00 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 31,132,133,134,1
75e10 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 35,136,137,138,1
75e20 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 39,140,141,142,1
75e30 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 43, /* 8x */.
75e40 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 144,145,146,147
75e50 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 ,148,149,150,151
75e60 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 ,152,153,154,155
75e70 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c 31 35 39 ,156,157,156,159
75e80 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 , /* 9x */. 1
75e90 36 30 2c 31 36 31 2c 31 36 32 2c 31 36 33 2c 31 60,161,162,163,1
75ea0 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 64,165,166,167,1
75eb0 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 68,169,170,171,1
75ec0 34 30 2c 31 34 31 2c 31 34 32 2c 31 37 35 2c 20 40,141,142,175,
75ed0 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 31 37 36 /* Ax */. 176
75ee0 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 31 38 30 ,177,178,179,180
75ef0 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 ,181,182,183,184
75f00 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 ,185,186,187,188
75f10 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a ,189,190,191, /*
75f20 20 42 78 20 2a 2f 0a 20 20 20 20 31 39 32 2c 31 Bx */. 192,1
75f30 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 29,130,131,132,1
75f40 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33,134,135,136,1
75f50 33 37 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 37,202,203,204,2
75f60 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43 05,206,207, /* C
75f70 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c 31 34 35 x */. 208,145
75f80 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 ,146,147,148,149
75f90 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 ,150,151,152,153
75fa0 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 ,218,219,220,221
75fb0 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20 ,222,223, /* Dx
75fc0 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32 35 2c 31 */. 224,225,1
75fd0 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 62,163,164,165,1
75fe0 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 32 66,167,168,169,2
75ff0 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 32,203,204,205,2
76000 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 06,207, /* Ex */
76010 0a 20 20 20 20 32 33 39 2c 32 34 30 2c 32 34 31 . 239,240,241
76020 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 ,242,243,244,245
76030 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 ,246,247,248,249
76040 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 ,219,220,221,222
76050 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 ,255, /* Fx */.#
76060 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
76070 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 32 35 The following 25
76080 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70 20 74 61 6 byte lookup ta
76090 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 ble is used to s
760a0 75 70 70 6f 72 74 20 53 51 4c 69 74 65 73 20 62 upport SQLites b
760b0 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71 75 69 76 uilt-in.** equiv
760c0 61 6c 65 6e 74 73 20 74 6f 20 74 68 65 20 66 6f alents to the fo
760d0 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64 61 72 64 llowing standard
760e0 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f library functio
760f0 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 73 73 70 ns:.**.** issp
76100 61 63 65 28 29 20 20 20 20 20 20 20 20 20 20 20 ace()
76110 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
76120 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68 61 28 29 1.** isalpha()
76130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76140 20 20 20 20 20 20 20 20 30 78 30 32 0a 2a 2a 20 0x02.**
76150 20 20 69 73 64 69 67 69 74 28 29 20 20 20 20 20 isdigit()
76160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76170 20 20 20 30 78 30 34 0a 2a 2a 20 20 20 69 73 61 0x04.** isa
76180 6c 6e 75 6d 28 29 20 20 20 20 20 20 20 20 20 20 lnum()
76190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 0x
761a0 30 36 0a 2a 2a 20 20 20 69 73 78 64 69 67 69 74 06.** isxdigit
761b0 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ()
761c0 20 20 20 20 20 20 20 20 20 30 78 30 38 0a 2a 2a 0x08.**
761d0 20 20 20 74 6f 75 70 70 65 72 28 29 20 20 20 20 toupper()
761e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
761f0 20 20 20 20 30 78 32 30 0a 2a 2a 0a 2a 2a 20 42 0x20.**.** B
76200 69 74 20 30 78 32 30 20 69 73 20 73 65 74 20 69 it 0x20 is set i
76210 66 20 74 68 65 20 6d 61 70 70 65 64 20 63 68 61 f the mapped cha
76220 72 61 63 74 65 72 20 72 65 71 75 69 72 65 73 20 racter requires
76230 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f 20 75 translation to u
76240 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e 20 69 2e pper.** case. i.
76250 65 2e 20 69 66 20 74 68 65 20 63 68 61 72 61 63 e. if the charac
76260 74 65 72 20 69 73 20 61 20 6c 6f 77 65 72 2d 63 ter is a lower-c
76270 61 73 65 20 41 53 43 49 49 20 63 68 61 72 61 63 ase ASCII charac
76280 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20 ter..** If x is
76290 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43 a lower-case ASC
762a0 49 49 20 63 68 61 72 61 63 74 65 72 2c 20 74 68 II character, th
762b0 65 6e 20 69 74 73 20 75 70 70 65 72 2d 63 61 73 en its upper-cas
762c0 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 e equivalent.**
762d0 69 73 20 28 78 20 2d 20 30 78 32 30 29 2e 20 54 is (x - 0x20). T
762e0 68 65 72 65 66 6f 72 65 20 74 6f 75 70 70 65 72 herefore toupper
762f0 28 29 20 63 61 6e 20 62 65 20 69 6d 70 6c 65 6d () can be implem
76300 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 ented as:.**.**
76310 20 20 28 78 20 26 20 7e 28 6d 61 70 5b 78 5d 26 (x & ~(map[x]&
76320 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20 53 74 61 0x20)).**.** Sta
76330 6e 64 61 72 64 20 66 75 6e 63 74 69 6f 6e 20 74 ndard function t
76340 6f 6c 6f 77 65 72 28 29 20 69 73 20 69 6d 70 6c olower() is impl
76350 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74 68 emented using th
76360 65 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f e sqlite3UpperTo
76370 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72 72 61 79 Lower[].** array
76380 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 75 . tolower() is u
76390 73 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 sed more often t
763a0 68 61 6e 20 74 6f 75 70 70 65 72 28 29 20 62 79 han toupper() by
763b0 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 SQLite..**.** S
763c0 51 4c 69 74 65 27 73 20 76 65 72 73 69 6f 6e 73 QLite's versions
763d0 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c 20 74 are identical t
763e0 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 76 o the standard v
763f0 65 72 73 69 6f 6e 73 20 61 73 73 75 6d 69 6e 67 ersions assuming
76400 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20 6f 66 20 a.** locale of
76410 22 43 22 2e 20 54 68 65 79 20 61 72 65 20 69 6d "C". They are im
76420 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6d 61 63 plemented as mac
76430 72 6f 73 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 ros in sqliteInt
76440 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 .h..*/.#ifdef SQ
76450 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 LITE_ASCII.SQLIT
76460 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
76470 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 unsigned char sq
76480 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 32 35 lite3CtypeMap[25
76490 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 6] = {. 0x00, 0
764a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
764b0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
764c0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 30 2e 0, 0x00, /* 00.
764d0 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .07 ........
764e0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c */. 0x00, 0x01,
764f0 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 0x01, 0x01, 0x0
76500 31 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 1, 0x01, 0x00, 0
76510 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e 30 66 20 x00, /* 08..0f
76520 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
76530 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76540 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76550 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76560 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 20 20 2e /* 10..17 .
76570 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 ....... */. 0x0
76580 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76590 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
765a0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
765b0 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e 2e 2e 2e 18..1f .....
765c0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31 2c 20 30 ... */. 0x01, 0
765d0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
765e0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
765f0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 30 2e 0, 0x00, /* 20.
76600 2e 32 37 20 20 20 20 20 21 22 23 24 25 26 27 20 .27 !"#$%&'
76610 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c */. 0x00, 0x00,
76620 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76630 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76640 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e 32 66 20 x00, /* 28..2f
76650 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f 0a 20 ()*+,-./ */.
76660 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 0x0c, 0x0c, 0x0
76670 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 c, 0x0c, 0x0c, 0
76680 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c x0c, 0x0c, 0x0c,
76690 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20 20 20 30 /* 30..37 0
766a0 31 32 33 34 35 36 37 20 2a 2f 0a 20 20 30 78 30 1234567 */. 0x0
766b0 63 2c 20 30 78 30 63 2c 20 30 78 30 30 2c 20 30 c, 0x0c, 0x00, 0
766c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
766d0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 0x00, 0x00, /*
766e0 20 33 38 2e 2e 33 66 20 20 20 20 38 39 3a 3b 3c 38..3f 89:;<
766f0 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30 30 2c 20 =>? */.. 0x00,
76700 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 0x0a, 0x0a, 0x0a
76710 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 , 0x0a, 0x0a, 0x
76720 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 30 0a, 0x02, /* 40
76730 2e 2e 34 37 20 20 20 20 40 41 42 43 44 45 46 47 ..47 @ABCDEFG
76740 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 */. 0x02, 0x02
76750 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
76760 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
76770 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e 2e 34 66 0x02, /* 48..4f
76780 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20 2a 2f 0a HIJKLMNO */.
76790 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 0x02, 0x02, 0x
767a0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
767b0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
767c0 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20 20 20 20 , /* 50..57
767d0 50 51 52 53 54 55 56 57 20 2a 2f 0a 20 20 30 78 PQRSTUVW */. 0x
767e0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
767f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76800 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
76810 2a 20 35 38 2e 2e 35 66 20 20 20 20 58 59 5a 5b * 58..5f XYZ[
76820 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30 30 2c 20 \]^_ */. 0x00,
76830 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 0x2a, 0x2a, 0x2a
76840 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 , 0x2a, 0x2a, 0x
76850 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 30 2a, 0x22, /* 60
76860 2e 2e 36 37 20 20 20 20 60 61 62 63 64 65 66 67 ..67 `abcdefg
76870 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 */. 0x22, 0x22
76880 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
76890 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
768a0 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e 2e 36 66 0x22, /* 68..6f
768b0 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20 2a 2f 0a hijklmno */.
768c0 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 0x22, 0x22, 0x
768d0 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
768e0 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
768f0 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20 20 20 20 , /* 70..77
76900 70 71 72 73 74 75 76 77 20 2a 2f 0a 20 20 30 78 pqrstuvw */. 0x
76910 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
76920 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76930 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
76940 2a 20 37 38 2e 2e 37 66 20 20 20 20 78 79 7a 7b * 78..7f xyz{
76950 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78 30 30 2c |}~. */.. 0x00,
76960 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76970 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76980 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38 x00, 0x00, /* 8
76990 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 0..87 .......
769a0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
769b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
769c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
769d0 20 30 78 30 30 2c 20 20 2f 2a 20 38 38 2e 2e 38 0x00, /* 88..8
769e0 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f f ........ */
769f0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
76a00 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76a10 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76a20 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 20 20 20 0, /* 90..97
76a30 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
76a40 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76a50 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76a60 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
76a70 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20 2e 2e 2e /* 98..9f ...
76a80 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
76a90 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76aa0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76ab0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61 x00, 0x00, /* a
76ac0 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 0..a7 .......
76ad0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
76ae0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
76af0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76b00 20 30 78 30 30 2c 20 20 2f 2a 20 61 38 2e 2e 61 0x00, /* a8..a
76b10 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f f ........ */
76b20 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
76b30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76b40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76b50 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37 20 20 20 0, /* b0..b7
76b60 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
76b70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
76b80 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
76b90 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
76ba0 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20 2e 2e 2e /* b8..bf ...
76bb0 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 78 30 30 ..... */.. 0x00
76bc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76bd0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76be0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
76bf0 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e 2e 2e 2e c0..c7 ......
76c00 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
76c10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76c20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76c30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 63 38 2e 2e , 0x00, /* c8..
76c40 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a cf ........ *
76c50 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
76c60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76c70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76c80 30 30 2c 20 20 2f 2a 20 64 30 2e 2e 64 37 20 20 00, /* d0..d7
76c90 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
76ca0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76cb0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76cc0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76cd0 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20 20 2e 2e /* d8..df ..
76ce0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 ...... */. 0x00
76cf0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76d00 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76d10 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
76d20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e 2e 2e 2e e0..e7 ......
76d30 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
76d40 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76d50 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76d60 2c 20 30 78 30 30 2c 20 20 2f 2a 20 65 38 2e 2e , 0x00, /* e8..
76d70 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a ef ........ *
76d80 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
76d90 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76da0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76db0 30 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 37 20 20 00, /* f0..f7
76dc0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
76dd0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76de0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76df0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 20 20 00, 0x00, 0x00
76e00 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 20 2e 2e /* f8..ff ..
76e10 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a 23 65 6e ...... */.};.#en
76e20 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 dif..../*.** The
76e30 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67 6c following singl
76e40 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 eton contains th
76e50 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 e global configu
76e60 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 ration for.** th
76e70 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
76e80 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
76e90 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 ATE SQLITE_WSD s
76ea0 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e truct Sqlite3Con
76eb0 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 fig sqlite3Confi
76ec0 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45 5f g = {. SQLITE_
76ed0 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 DEFAULT_MEMSTATU
76ee0 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74 20 S, /* bMemstat
76ef0 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20 */. 1,
76f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76f10 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 20 2a /* bCoreMutex *
76f20 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 52 45 /. SQLITE_THRE
76f30 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 20 20 ADSAFE==1,
76f40 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a 2f /* bFullMutex */
76f50 0a 20 20 20 30 78 37 66 66 66 66 66 66 65 2c 20 . 0x7ffffffe,
76f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
76f70 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 * mxStrlen */.
76f80 20 31 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 100,
76f90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
76fa0 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 zLookaside */.
76fb0 20 35 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 500,
76fc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e /* n
76fd0 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 Lookaside */.
76fe0 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 {0,0,0,0,0,0,0,0
76ff0 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 20 }, /* m
77000 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 */. {0,0,0,0,0
77010 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20 ,0,0,0,0},
77020 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 /* mutex */.
77030 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 {0,0,0,0,0,0,0,0
77040 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20 70 63 ,0,0,0}, /* pc
77050 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 6f 69 64 ache */. (void
77060 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 *)0,
77070 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70 20 2a /* pHeap *
77080 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
77090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
770a0 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20 20 30 /* nHeap */. 0
770b0 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 , 0,
770c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e 48 /* mnH
770d0 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0a 20 eap, mxHeap */.
770e0 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 (void*)0,
770f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
77100 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 pScratch */. 0
77110 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
77120 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 53 /* szS
77130 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20 cratch */. 0,
77140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77150 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63 72 61 /* nScra
77160 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a tch */. (void*
77170 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 )0,
77180 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 2a 2f /* pPage */
77190 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 . 0,
771a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
771b0 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20 20 30 * szPage */. 0
771c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
771d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 50 61 /* nPa
771e0 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ge */. 0,
771f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77200 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65 72 53 /* mxParserS
77210 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 20 20 tack */. 0,
77220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77230 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64 43 /* sharedC
77240 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 acheEnabled */.
77250 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 /* All the res
77260 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 t should always
77270 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 be initialized t
77280 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 30 2c 20 o zero */. 0,
77290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
772a0 20 20 20 20 20 20 20 20 2f 2a 20 69 73 49 6e 69 /* isIni
772b0 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 t */. 0,
772c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
772d0 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 65 73 73 /* inProgress
772e0 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
772f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77300 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 6e 69 74 /* isMutexInit
77310 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
77320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77330 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 /* isMallocIni
77340 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 t */. 0,
77350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77360 20 20 20 2f 2a 20 69 73 50 43 61 63 68 65 49 6e /* isPCacheIn
77370 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 it */. 0,
77380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77390 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75 74 65 /* pInitMute
773a0 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 x */. 0,
773b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
773c0 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74 4d 75 /* nRefInitMu
773d0 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a tex */.};.../*.*
773e0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 * Hash table for
773f0 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e global function
77400 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f s - functions co
77410 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 64 mmon to all.** d
77420 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
77430 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e 69 74 ons. After init
77440 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 69 73 ialization, this
77450 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 65 61 table is.** rea
77460 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 d-only..*/.SQLIT
77470 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 E_PRIVATE SQLITE
77480 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 _WSD FuncDefHash
77490 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 sqlite3GlobalFu
774a0 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a 2a 2a 20 nctions;../*.**
774b0 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 The value of the
774c0 20 22 70 65 6e 64 69 6e 67 22 20 62 79 74 65 20 "pending" byte
774d0 6d 75 73 74 20 62 65 20 30 78 34 30 30 30 30 30 must be 0x400000
774e0 30 30 20 28 31 20 62 79 74 65 20 70 61 73 74 20 00 (1 byte past
774f0 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61 62 79 74 the.** 1-gibabyt
77500 65 20 62 6f 75 6e 64 61 72 79 29 20 69 6e 20 61 e boundary) in a
77510 20 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 74 61 compatible data
77520 62 61 73 65 2e 20 20 53 51 4c 69 74 65 20 6e 65 base. SQLite ne
77530 76 65 72 20 75 73 65 73 0a 2a 2a 20 74 68 65 20 ver uses.** the
77540 64 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68 database page th
77550 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
77560 70 65 6e 64 69 6e 67 20 62 79 74 65 2e 20 20 49 pending byte. I
77570 74 20 6e 65 76 65 72 20 61 74 74 65 6d 70 74 73 t never attempts
77580 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f 72 20 77 .** to read or w
77590 72 69 74 65 20 74 68 61 74 20 70 61 67 65 2e 20 rite that page.
775a0 20 54 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 The pending byt
775b0 65 20 70 61 67 65 20 69 73 20 73 65 74 20 61 73 e page is set as
775c0 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75 73 65 20 sign.** for use
775d0 62 79 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 by the VFS layer
775e0 73 20 61 73 20 73 70 61 63 65 20 66 6f 72 20 6d s as space for m
775f0 61 6e 61 67 69 6e 67 20 66 69 6c 65 20 6c 6f 63 anaging file loc
77600 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 ks..**.** During
77610 20 74 65 73 74 69 6e 67 2c 20 69 74 20 69 73 20 testing, it is
77620 6f 66 74 65 6e 20 64 65 73 69 72 61 62 6c 65 20 often desirable
77630 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 to move the pend
77640 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a 2a 20 61 ing byte to.** a
77650 20 64 69 66 66 65 72 65 6e 74 20 70 6f 73 69 74 different posit
77660 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e ion in the file.
77670 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 63 6f This allows co
77680 64 65 20 74 68 61 74 20 68 61 73 20 74 6f 0a 2a de that has to.*
77690 2a 20 64 65 61 6c 20 77 69 74 68 20 74 68 65 20 * deal with the
776a0 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20 pending byte to
776b0 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20 74 68 61 run on files tha
776c0 74 20 61 72 65 20 6d 75 63 68 20 73 6d 61 6c 6c t are much small
776d0 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20 47 69 42 er.** than 1 GiB
776e0 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 . The sqlite3_t
776f0 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e est_control() in
77700 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 terface can be u
77710 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76 65 20 74 sed to.** move t
77720 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e he pending byte.
77730 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54 41 4e 54 .**.** IMPORTANT
77740 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 : Changing the
77750 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20 pending byte to
77760 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20 any value other
77770 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30 30 30 30 than.** 0x400000
77780 30 30 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 00 results in an
77790 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 incompatible da
777a0 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 6d tabase file form
777b0 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 at!.** Changing
777c0 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 the pending byte
777d0 20 64 75 72 69 6e 67 20 6f 70 65 72 61 74 69 6e during operatin
777e0 67 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 g results in und
777f0 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 64 69 efined.** and di
77800 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61 76 69 leterious behavi
77810 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 or..*/.SQLITE_PR
77820 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
77830 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d 20 30 3PendingByte = 0
77840 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f 2a 2a 2a x40000000;../***
77850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
77860 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a of global.c ****
77870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
77880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
77890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
778a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
778b0 6e 20 66 69 6c 65 20 73 74 61 74 75 73 2e 63 20 n file status.c
778c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
778d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
778e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
778f0 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 38 0a 2a * 2008 June 18.*
77900 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
77910 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
77920 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
77930 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
77940 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
77950 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
77960 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
77970 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
77980 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
77990 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
779a0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
779b0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
779c0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
779d0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
779e0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
779f0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
77a00 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
77a10 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
77a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
77a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
77a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
77a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
77a60 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 .** This module
77a70 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 implements the s
77a80 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 20 qlite3_status()
77a90 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 72 65 interface and re
77aa0 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f lated.** functio
77ab0 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 nality..**.** $I
77ac0 64 3a 20 73 74 61 74 75 73 2e 63 2c 76 20 31 2e d: status.c,v 1.
77ad0 39 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a 9 2008/09/02 00:
77ae0 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a 52:52 drh Exp $.
77af0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 */../*.** Variab
77b00 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20 les in which to
77b10 72 65 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e record status in
77b20 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 formation..*/.ty
77b30 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
77b40 69 74 65 33 53 74 61 74 54 79 70 65 20 73 71 6c ite3StatType sql
77b50 69 74 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74 ite3StatType;.st
77b60 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
77b70 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 struct sqlite3St
77b80 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e atType {. int n
77b90 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 owValue[9];
77ba0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 /* Current v
77bb0 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 alue */. int mx
77bc0 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 Value[9];
77bd0 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 /* Maximum va
77be0 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 lue */.} sqlite3
77bf0 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b Stat = { {0,}, {
77c00 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 0,} };.../* The
77c10 22 77 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 "wsdStat" macro
77c20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 will resolve to
77c30 74 68 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 the status infor
77c40 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 mation.** state
77c50 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 vector. If writ
77c60 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 able static data
77c70 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 is unsupported
77c80 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a on the target,.*
77c90 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 * we have to loc
77ca0 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 ate the state ve
77cb0 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 ctor at run-time
77cc0 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 . In the more c
77cd0 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 ommon.** case wh
77ce0 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 ere writable sta
77cf0 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 tic data is supp
77d00 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 orted, wsdStat c
77d10 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c an refer directl
77d20 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c y.** to the "sql
77d30 69 74 65 33 53 74 61 74 22 20 73 74 61 74 65 20 ite3Stat" state
77d40 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 vector declared
77d50 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 above..*/.#ifdef
77d60 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 SQLITE_OMIT_WSD
77d70 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 .# define wsdSta
77d80 74 49 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 tInit sqlite3St
77d90 61 74 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f atType *x = &GLO
77da0 42 41 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 BAL(sqlite3StatT
77db0 79 70 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 ype,sqlite3Stat)
77dc0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 .# define wsdSta
77dd0 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 t x[0].#else.# d
77de0 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 efine wsdStatIni
77df0 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 t.# define wsdSt
77e00 61 74 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 at sqlite3Stat.#
77e10 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 endif../*.** Ret
77e20 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 urn the current
77e30 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 value of a statu
77e40 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a s parameter..*/.
77e50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
77e60 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 nt sqlite3Status
77e70 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 Value(int op){.
77e80 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 wsdStatInit;.
77e90 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 assert( op>=0 &&
77ea0 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 op<ArraySize(ws
77eb0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 dStat.nowValue)
77ec0 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 );. return wsdS
77ed0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
77ee0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e ;.}../*.** Add N
77ef0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
77f00 20 61 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 a status record
77f10 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 . It is assumed
77f20 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c that the.** cal
77f30 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 ler holds approp
77f40 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a riate locks..*/.
77f50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
77f60 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
77f70 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 sAdd(int op, int
77f80 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e N){. wsdStatIn
77f90 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 it;. assert( op
77fa0 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 >=0 && op<ArrayS
77fb0 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 ize(wsdStat.nowV
77fc0 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 alue) );. wsdSt
77fd0 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 at.nowValue[op]
77fe0 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 += N;. if( wsdS
77ff0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
78000 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 >wsdStat.mxValue
78010 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 [op] ){. wsdS
78020 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 tat.mxValue[op]
78030 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c = wsdStat.nowVal
78040 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f ue[op];. }.}../
78050 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c *.** Set the val
78060 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 ue of a status t
78070 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 o X..*/.SQLITE_P
78080 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
78090 74 65 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 te3StatusSet(int
780a0 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 op, int X){. w
780b0 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 sdStatInit;. as
780c0 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f sert( op>=0 && o
780d0 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 p<ArraySize(wsdS
780e0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b tat.nowValue) );
780f0 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 . wsdStat.nowVa
78100 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 lue[op] = X;. i
78110 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 f( wsdStat.nowVa
78120 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e lue[op]>wsdStat.
78130 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 mxValue[op] ){.
78140 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c wsdStat.mxVal
78150 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 ue[op] = wsdStat
78160 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 .nowValue[op];.
78170 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 }.}../*.** Quer
78180 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 y status informa
78190 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tion..**.** This
781a0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
781b0 61 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 assumes that rea
781c0 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 ding or writing
781d0 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 an aligned.** 32
781e0 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 -bit integer is
781f0 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 an atomic operat
78200 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 ion. If that as
78210 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 sumption is not
78220 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 true,.** then th
78230 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f is routine is no
78240 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f t threadsafe..*/
78250 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
78260 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 sqlite3_status(i
78270 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 nt op, int *pCur
78280 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 rent, int *pHigh
78290 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 water, int reset
782a0 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 Flag){. wsdStat
782b0 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 Init;. if( op<0
782c0 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a || op>=ArraySiz
782d0 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c e(wsdStat.nowVal
782e0 75 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ue) ){. retur
782f0 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
78300 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 . }. *pCurrent
78310 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 = wsdStat.nowVa
78320 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 lue[op];. *pHig
78330 68 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74 hwater = wsdStat
78340 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 .mxValue[op];.
78350 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b if( resetFlag ){
78360 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 . wsdStat.mxV
78370 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 alue[op] = wsdSt
78380 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b at.nowValue[op];
78390 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
783a0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
783b0 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69 * Query status i
783c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 nformation for a
783d0 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
783e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 connection.*/.S
783f0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
78400 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
78410 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
78420 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
78430 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
78440 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73 ion whose status
78450 20 69 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 is desired */.
78460 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 int op,
78470 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 /* Status
78480 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a verb */. int *
78490 70 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20 pCurrent,
784a0 20 2f 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e /* Write curren
784b0 74 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a t value here */.
784c0 20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 int *pHighwate
784d0 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 r, /* Write
784e0 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b high-water mark
784f0 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 here */. int r
78500 65 73 65 74 46 6c 61 67 20 20 20 20 20 20 20 20 esetFlag
78510 20 2f 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77 /* Reset high-w
78520 61 74 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75 ater mark if tru
78530 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 e */.){. switch
78540 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 ( op ){. case
78550 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 SQLITE_DBSTATUS
78560 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a _LOOKASIDE_USED:
78570 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 {. *pCurre
78580 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 nt = db->lookasi
78590 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a de.nOut;. *
785a0 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d pHighwater = db-
785b0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 >lookaside.mxOut
785c0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65 ;. if( rese
785d0 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 tFlag ){.
785e0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d db->lookaside.m
785f0 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 xOut = db->looka
78600 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 side.nOut;.
78610 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
78620 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
78630 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 t: {. retur
78640 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
78650 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
78660 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
78670 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
78680 20 45 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63 End of status.c
78690 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
786a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
786b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
786c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
786d0 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65 Begin file date
786e0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
786f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
78710 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f ./*.** 2003 Octo
78720 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 31.**.** The
78730 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
78740 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
78750 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
78760 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
78770 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
78780 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
78790 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
787a0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
787b0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
787c0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
787d0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
787e0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
787f0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
78800 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
78810 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
78820 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
78830 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
78840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78880 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
78890 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
788a0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
788b0 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 implement date
788c0 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 and time.** func
788d0 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 tions for SQLite
788e0 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 . .**.** There
788f0 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f is only one expo
78900 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 rted symbol in t
78910 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 his file - the f
78920 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 unction.** sqlit
78930 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 e3RegisterDateTi
78940 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f meFunctions() fo
78950 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f und at the botto
78960 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a m of the file..*
78970 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 * All other code
78980 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e has file scope.
78990 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61 74 65 .**.** $Id: date
789a0 2e 63 2c 76 20 31 2e 31 30 37 20 32 30 30 39 2f .c,v 1.107 2009/
789b0 30 35 2f 30 33 20 32 30 3a 32 33 3a 35 33 20 64 05/03 20:23:53 d
789c0 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 53 rh Exp $.**.** S
789d0 51 4c 69 74 65 20 70 72 6f 63 65 73 73 65 73 20 QLite processes
789e0 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20 64 61 all times and da
789f0 74 65 73 20 61 73 20 4a 75 6c 69 61 6e 20 44 61 tes as Julian Da
78a00 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 0a y numbers. The.
78a10 2a 2a 20 64 61 74 65 73 20 61 6e 64 20 74 69 6d ** dates and tim
78a20 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 61 73 es are stored as
78a30 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 the number of d
78a40 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f 6e 0a 2a ays since noon.*
78a50 2a 20 69 6e 20 47 72 65 65 6e 77 69 63 68 20 6f * in Greenwich o
78a60 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 34 n November 24, 4
78a70 37 31 34 20 42 2e 43 2e 20 61 63 63 6f 72 64 69 714 B.C. accordi
78a80 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 ng to the Gregor
78a90 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 72 20 ian.** calendar
78aa0 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 31 system. .**.** 1
78ab0 39 37 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 970-01-01 00:00:
78ac0 30 30 20 69 73 20 4a 44 20 32 34 34 30 35 38 37 00 is JD 2440587
78ad0 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 30 31 .5.** 2000-01-01
78ae0 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 00:00:00 is JD
78af0 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 20 2451544.5.**.**
78b00 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f This implementio
78b10 6e 20 72 65 71 75 69 72 65 73 20 79 65 61 72 73 n requires years
78b20 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 to be expressed
78b30 20 61 73 20 61 20 34 2d 64 69 67 69 74 20 6e 75 as a 4-digit nu
78b40 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68 20 6d 65 mber.** which me
78b50 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79 20 64 61 ans that only da
78b60 74 65 73 20 62 65 74 77 65 65 6e 20 30 30 30 30 tes between 0000
78b70 2d 30 31 2d 30 31 20 61 6e 64 20 39 39 39 39 2d -01-01 and 9999-
78b80 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 65 20 12-31 can.** be
78b90 72 65 70 72 65 73 65 6e 74 65 64 2c 20 65 76 65 represented, eve
78ba0 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69 61 6e 20 n though julian
78bb0 64 61 79 20 6e 75 6d 62 65 72 73 20 61 6c 6c 6f day numbers allo
78bc0 77 20 61 20 6d 75 63 68 20 77 69 64 65 72 0a 2a w a much wider.*
78bd0 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 65 73 * range of dates
78be0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 65 67 ..**.** The Greg
78bf0 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 73 orian calendar s
78c00 79 73 74 65 6d 20 69 73 20 75 73 65 64 20 66 6f ystem is used fo
78c10 72 20 61 6c 6c 20 64 61 74 65 73 20 61 6e 64 20 r all dates and
78c20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 times,.** even t
78c30 68 6f 73 65 20 74 68 61 74 20 70 72 65 64 61 74 hose that predat
78c40 65 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20 e the Gregorian
78c50 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 73 74 6f calendar. Histo
78c60 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 0a 2a 2a rians usually.**
78c70 20 75 73 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 use the Julian
78c80 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 64 61 74 calendar for dat
78c90 65 73 20 70 72 69 6f 72 20 74 6f 20 31 35 38 32 es prior to 1582
78ca0 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 72 20 73 -10-15 and for s
78cb0 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20 61 66 74 ome.** dates aft
78cc0 65 72 77 61 72 64 73 2c 20 64 65 70 65 6e 64 69 erwards, dependi
78cd0 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 20 42 ng on locale. B
78ce0 65 77 61 72 65 20 6f 66 20 74 68 69 73 20 64 69 eware of this di
78cf0 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 fference..**.**
78d00 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 The conversion a
78d10 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20 69 6d lgorithms are im
78d20 70 6c 65 6d 65 6e 74 65 64 20 62 61 73 65 64 20 plemented based
78d30 6f 6e 20 64 65 73 63 72 69 70 74 69 6f 6e 73 0a on descriptions.
78d40 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 ** in the follow
78d50 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a 2a 20 ing text:.**.**
78d60 20 20 20 20 20 4a 65 61 6e 20 4d 65 65 75 73 0a Jean Meeus.
78d70 2a 2a 20 20 20 20 20 20 41 73 74 72 6f 6e 6f 6d ** Astronom
78d80 69 63 61 6c 20 41 6c 67 6f 72 69 74 68 6d 73 2c ical Algorithms,
78d90 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c 20 31 39 2nd Edition, 19
78da0 39 38 0a 2a 2a 20 20 20 20 20 20 49 53 42 4d 20 98.** ISBM
78db0 30 2d 39 34 33 33 39 36 2d 36 31 2d 31 0a 2a 2a 0-943396-61-1.**
78dc0 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e 6e 2d 42 Willmann-B
78dd0 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 20 20 ell, Inc.**
78de0 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69 72 67 69 Richmond, Virgi
78df0 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a 23 69 6e nia (USA).*/.#in
78e00 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 0a clude <time.h>..
78e10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
78e20 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e MIT_DATETIME_FUN
78e30 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72 65 63 CS../*.** On rec
78e40 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c 61 74 ent Windows plat
78e50 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 61 6c forms, the local
78e60 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 69 6f time_s() functio
78e70 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a n is available.*
78e80 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 * as part of the
78e90 20 22 53 65 63 75 72 65 20 43 52 54 22 2e 20 49 "Secure CRT". I
78ea0 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 t is essentially
78eb0 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a equivalent to .
78ec0 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29 ** localtime_r()
78ed0 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64 65 72 available under
78ee0 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c 61 74 most POSIX plat
78ef0 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20 74 68 forms, except th
78f00 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64 65 72 at the .** order
78f10 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 of the paramete
78f20 72 73 20 69 73 20 72 65 76 65 72 73 65 64 2e 0a rs is reversed..
78f30 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70 3a 2f **.** See http:/
78f40 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66 74 2e /msdn.microsoft.
78f50 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72 61 72 com/en-us/librar
78f60 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e 38 30 y/a442x3ye(VS.80
78f70 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 ).aspx..**.** If
78f80 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f the user has no
78f90 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f 20 75 t indicated to u
78fa0 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29 se localtime_r()
78fb0 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 or localtime_s(
78fc0 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20 63 68 ).** already, ch
78fd0 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56 43 20 eck for an MSVC
78fe0 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d 65 6e build environmen
78ff0 74 20 74 68 61 74 20 70 72 6f 76 69 64 65 73 20 t that provides
79000 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 .** localtime_s(
79010 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e )..*/.#if !defin
79020 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ed(HAVE_LOCALTIM
79030 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e 65 64 E_R) && !defined
79040 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f (HAVE_LOCALTIME_
79050 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64 65 66 S) && \. def
79060 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 26 ined(_MSC_VER) &
79070 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54 5f 49 & defined(_CRT_I
79080 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43 41 54 NSECURE_DEPRECAT
79090 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56 45 5f E).#define HAVE_
790a0 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a 23 65 LOCALTIME_S 1.#e
790b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 ndif../*.** A st
790c0 72 75 63 74 75 72 65 20 66 6f 72 20 68 6f 6c 64 ructure for hold
790d0 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 64 61 74 ing a single dat
790e0 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a 74 e and time..*/.t
790f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 61 ypedef struct Da
79100 74 65 54 69 6d 65 20 44 61 74 65 54 69 6d 65 3b teTime DateTime;
79110 0a 73 74 72 75 63 74 20 44 61 74 65 54 69 6d 65 .struct DateTime
79120 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 {. sqlite3_int
79130 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 65 20 6a 64 iJD; /* The j
79140 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 ulian day number
79150 20 74 69 6d 65 73 20 38 36 34 30 30 30 30 30 20 times 86400000
79160 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 */. int Y, M, D
79170 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 61 72 2c ; /* Year,
79180 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61 79 20 month, and day
79190 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b 20 20 */. int h, m;
791a0 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75 72 20 /* Hour
791b0 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 and minutes */.
791c0 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20 20 20 int tz;
791d0 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e 65 20 /* Timezone
791e0 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e 75 74 65 offset in minute
791f0 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 73 3b s */. double s;
79200 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 /* Sec
79210 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72 20 76 onds */. char v
79220 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f 2a 20 alidYMD; /*
79230 54 72 75 65 20 28 31 29 20 69 66 20 59 2c 4d 2c True (1) if Y,M,
79240 44 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 D are valid */.
79250 20 63 68 61 72 20 76 61 6c 69 64 48 4d 53 3b 20 char validHMS;
79260 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 /* True (1)
79270 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 76 61 6c if h,m,s are val
79280 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c id */. char val
79290 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a 20 54 72 idJD; /* Tr
792a0 75 65 20 28 31 29 20 69 66 20 69 4a 44 20 69 73 ue (1) if iJD is
792b0 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 valid */. char
792c0 20 76 61 6c 69 64 54 5a 3b 20 20 20 20 20 20 2f validTZ; /
792d0 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 74 7a * True (1) if tz
792e0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d 3b 0a is valid */.};.
792f0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
79300 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65 20 6f zDate into one o
79310 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72 73 2e r more integers.
79320 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 Additional arg
79330 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65 20 69 uments.** come i
79340 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20 61 73 n groups of 5 as
79350 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
79360 20 20 20 20 20 20 4e 20 20 20 20 20 20 20 6e 75 N nu
79370 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73 20 69 mber of digits i
79380 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a n the integer.**
79390 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20 20 6d min m
793a0 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 inimum allowed v
793b0 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 alue of the inte
793c0 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 61 78 ger.** max
793d0 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61 6c 6c maximum all
793e0 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 owed value of th
793f0 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 e integer.**
79400 20 20 20 6e 65 78 74 43 20 20 20 66 69 72 73 74 nextC first
79410 20 63 68 61 72 61 63 74 65 72 20 61 66 74 65 72 character after
79420 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 the integer.**
79430 20 20 20 20 20 20 70 56 61 6c 20 20 20 20 77 68 pVal wh
79440 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 ere to write the
79450 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75 65 2e integers value.
79460 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f .**.** Conversio
79470 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e 74 69 ns continue unti
79480 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78 74 43 l one with nextC
79490 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 ==0 is encounter
794a0 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e 63 74 ed..** The funct
794b0 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 ion returns the
794c0 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63 65 73 number of succes
794d0 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f 6e 73 sful conversions
794e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
794f0 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73 74 20 getDigits(const
79500 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e 2e 2e char *zDate, ...
79510 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b ){. va_list ap;
79520 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20 69 6e . int val;. in
79530 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e 3b 0a t N;. int min;.
79540 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69 6e 74 int max;. int
79550 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20 2a 70 nextC;. int *p
79560 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d Val;. int cnt =
79570 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 0;. va_start(a
79580 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64 6f 7b p, zDate);. do{
79590 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72 67 28 . N = va_arg(
795a0 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 69 ap, int);. mi
795b0 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 n = va_arg(ap, i
795c0 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d 20 76 nt);. max = v
795d0 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a a_arg(ap, int);.
795e0 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61 5f 61 nextC = va_a
795f0 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 rg(ap, int);.
79600 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67 28 61 pVal = va_arg(a
79610 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 76 61 p, int*);. va
79620 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 l = 0;. while
79630 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20 69 ( N-- ){. i
79640 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 f( !sqlite3Isdig
79650 69 74 28 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 it(*zDate) ){.
79660 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 goto end_g
79670 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20 20 20 etDigits;.
79680 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d 20 76 61 }. val = va
79690 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20 l*10 + *zDate -
796a0 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 61 74 65 '0';. zDate
796b0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
796c0 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c ( val<min || val
796d0 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43 21 3d >max || (nextC!=
796e0 30 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a 44 61 0 && nextC!=*zDa
796f0 74 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 te) ){. got
79700 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b o end_getDigits;
79710 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 56 61 6c . }. *pVal
79720 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44 61 74 = val;. zDat
79730 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a e++;. cnt++;.
79740 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 74 43 20 }while( nextC
79750 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69 74 73 );.end_getDigits
79760 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a :. va_end(ap);.
79770 20 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a return cnt;.}.
79780 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65 78 74 ./*.** Read text
79790 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f from z[] and co
797a0 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 66 6c 6f nvert into a flo
797b0 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 ating point numb
797c0 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 er. Return.** t
797d0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 he number of dig
797e0 69 74 73 20 63 6f 6e 76 65 72 74 65 64 2e 0a 2a its converted..*
797f0 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 6c /.#define getVal
79800 75 65 20 73 71 6c 69 74 65 33 41 74 6f 46 0a 0a ue sqlite3AtoF..
79810 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 74 69 /*.** Parse a ti
79820 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69 6f 6e mezone extension
79830 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 61 on the end of a
79840 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54 date-time..** T
79850 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 he extension is
79860 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a of the form:.**.
79870 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d 29 48 ** (+/-)H
79880 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20 74 68 H:MM.**.** Or th
79890 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74 69 6f e "zulu" notatio
798a0 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 n:.**.**
798b0 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 Z.**.** If the p
798c0 61 72 73 65 20 69 73 20 73 75 63 63 65 73 73 66 arse is successf
798d0 75 6c 2c 20 77 72 69 74 65 20 74 68 65 20 6e 75 ul, write the nu
798e0 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 65 73 0a mber of minutes.
798f0 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 69 6e 20 ** of change in
79900 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75 72 6e p->tz and return
79910 20 30 2e 20 20 49 66 20 61 20 70 61 72 73 65 72 0. If a parser
79920 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a error occurs,.*
79930 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 * return non-zer
79940 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73 73 69 o..**.** A missi
79950 6e 67 20 73 70 65 63 69 66 69 65 72 20 69 73 20 ng specifier is
79960 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 20 61 not considered a
79970 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 n error..*/.stat
79980 69 63 20 69 6e 74 20 70 61 72 73 65 54 69 6d 65 ic int parseTime
79990 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61 72 20 zone(const char
799a0 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65 *zDate, DateTime
799b0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67 6e 20 *p){. int sgn
799c0 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72 2c 20 = 0;. int nHr,
799d0 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 nMn;. int c;.
799e0 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
799f0 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 29 7b space(*zDate) ){
79a00 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d zDate++; }. p-
79a10 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20 3d 20 2a >tz = 0;. c = *
79a20 7a 44 61 74 65 3b 0a 20 20 69 66 28 20 63 3d 3d zDate;. if( c==
79a30 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d '-' ){. sgn =
79a40 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 -1;. }else if(
79a50 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 73 c=='+' ){. s
79a60 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65 6c 73 65 gn = +1;. }else
79a70 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c 7c 20 63 if( c=='Z' || c
79a80 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20 7a 44 61 =='z' ){. zDa
79a90 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 7a te++;. goto z
79aa0 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d 65 6c 73 ulu_time;. }els
79ab0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 63 21 e{. return c!
79ac0 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 2b =0;. }. zDate+
79ad0 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 +;. if( getDigi
79ae0 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 ts(zDate, 2, 0,
79af0 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 2c 20 32 14, ':', &nHr, 2
79b00 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6e 4d 6e , 0, 59, 0, &nMn
79b10 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 )!=2 ){. retu
79b20 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 rn 1;. }. zDat
79b30 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e 74 7a 20 e += 5;. p->tz
79b40 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72 = sgn*(nMn + nHr
79b50 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a *60);.zulu_time:
79b60 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 . while( sqlite
79b70 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 3Isspace(*zDate)
79b80 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 ){ zDate++; }.
79b90 20 72 65 74 75 72 6e 20 2a 7a 44 61 74 65 21 3d return *zDate!=
79ba0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 0;.}../*.** Pars
79bb0 65 20 74 69 6d 65 73 20 6f 66 20 74 68 65 20 66 e times of the f
79bc0 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20 48 48 3a orm HH:MM or HH:
79bd0 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d 4d 3a 53 MM:SS or HH:MM:S
79be0 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68 65 20 48 S.FFFF..** The H
79bf0 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53 20 6d 75 H, MM, and SS mu
79c00 73 74 20 65 61 63 68 20 62 65 20 65 78 61 63 74 st each be exact
79c10 6c 79 20 32 20 64 69 67 69 74 73 2e 20 20 54 68 ly 2 digits. Th
79c20 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 e.** fractional
79c30 73 65 63 6f 6e 64 73 20 46 46 46 46 20 63 61 6e seconds FFFF can
79c40 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 be one or more
79c50 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 digits..**.** Re
79c60 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65 20 turn 1 if there
79c70 69 73 20 61 20 70 61 72 73 69 6e 67 20 65 72 72 is a parsing err
79c80 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73 75 63 63 or and 0 on succ
79c90 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ess..*/.static i
79ca0 6e 74 20 70 61 72 73 65 48 68 4d 6d 53 73 28 63 nt parseHhMmSs(c
79cb0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 onst char *zDate
79cc0 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a , DateTime *p){.
79cd0 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73 3b 0a 20 int h, m, s;.
79ce0 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20 30 2e 30 double ms = 0.0
79cf0 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 74 ;. if( getDigit
79d00 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 32 s(zDate, 2, 0, 2
79d10 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 2c 20 30 4, ':', &h, 2, 0
79d20 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 3d 32 20 , 59, 0, &m)!=2
79d30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
79d40 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 . }. zDate +=
79d50 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 5;. if( *zDate=
79d60 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 =':' ){. zDat
79d70 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 67 65 74 e++;. if( get
79d80 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c Digits(zDate, 2,
79d90 20 30 2c 20 35 39 2c 20 30 2c 20 26 73 29 21 3d 0, 59, 0, &s)!=
79da0 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 1 ){. retur
79db0 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a n 1;. }. z
79dc0 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20 20 20 69 Date += 2;. i
79dd0 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e 27 20 26 f( *zDate=='.' &
79de0 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 & sqlite3Isdigit
79df0 28 7a 44 61 74 65 5b 31 5d 29 20 29 7b 0a 20 20 (zDate[1]) ){.
79e00 20 20 20 20 64 6f 75 62 6c 65 20 72 53 63 61 6c double rScal
79e10 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 7a e = 1.0;. z
79e20 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 77 68 Date++;. wh
79e30 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
79e40 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b 0a 20 git(*zDate) ){.
79e50 20 20 20 20 20 20 20 6d 73 20 3d 20 6d 73 2a 31 ms = ms*1
79e60 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20 27 0.0 + *zDate - '
79e70 30 27 3b 0a 20 20 20 20 20 20 20 20 72 53 63 61 0';. rSca
79e80 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20 le *= 10.0;.
79e90 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 zDate++;.
79ea0 20 20 20 7d 0a 20 20 20 20 20 20 6d 73 20 2f 3d }. ms /=
79eb0 20 72 53 63 61 6c 65 3b 0a 20 20 20 20 7d 0a 20 rScale;. }.
79ec0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 }else{. s =
79ed0 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 0;. }. p->vali
79ee0 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 dJD = 0;. p->va
79ef0 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 70 2d lidHMS = 1;. p-
79f00 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e 6d 20 3d >h = h;. p->m =
79f10 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 20 2b m;. p->s = s +
79f20 20 6d 73 3b 0a 20 20 69 66 28 20 70 61 72 73 65 ms;. if( parse
79f30 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74 65 2c 20 Timezone(zDate,
79f40 70 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 p) ) return 1;.
79f50 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 28 70 p->validTZ = (p
79f60 2d 3e 74 7a 21 3d 30 29 3f 31 3a 30 3b 0a 20 20 ->tz!=0)?1:0;.
79f70 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
79f80 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 ** Convert from
79f90 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d YYYY-MM-DD HH:MM
79fa0 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e 20 64 61 :SS to julian da
79fb0 79 2e 20 20 57 65 20 61 6c 77 61 79 73 20 61 73 y. We always as
79fc0 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 sume.** that the
79fd0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 73 20 61 YYYY-MM-DD is a
79fe0 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
79ff0 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 Gregorian calend
7a000 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 ar..**.** Refere
7a010 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 61 67 65 nce: Meeus page
7a020 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 61.*/.static vo
7a030 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 44 61 74 id computeJD(Dat
7a040 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 eTime *p){. int
7a050 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 42 2c 20 Y, M, D, A, B,
7a060 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 28 20 70 X1, X2;.. if( p
7a070 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 65 74 75 ->validJD ) retu
7a080 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c rn;. if( p->val
7a090 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 59 20 3d idYMD ){. Y =
7a0a0 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 3d 20 70 p->Y;. M = p
7a0b0 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 70 2d 3e ->M;. D = p->
7a0c0 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 D;. }else{.
7a0d0 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a 20 49 66 Y = 2000; /* If
7a0e0 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 66 69 65 no YMD specifie
7a0f0 64 2c 20 61 73 73 75 6d 65 20 32 30 30 30 2d 4a d, assume 2000-J
7a100 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 4d 20 3d an-01 */. M =
7a110 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 3b 0a 20 1;. D = 1;.
7a120 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 20 29 7b }. if( M<=2 ){
7a130 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 20 4d 20 . Y--;. M
7a140 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 41 20 3d += 12;. }. A =
7a150 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d 20 32 20 Y/100;. B = 2
7a160 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 20 20 58 - A + (A/4);. X
7a170 31 20 3d 20 33 36 35 32 35 2a 28 59 2b 34 37 31 1 = 36525*(Y+471
7a180 36 29 2f 31 30 30 3b 0a 20 20 58 32 20 3d 20 33 6)/100;. X2 = 3
7a190 30 36 30 30 31 2a 28 4d 2b 31 29 2f 31 30 30 30 06001*(M+1)/1000
7a1a0 30 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 0;. p->iJD = (s
7a1b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 58 qlite3_int64)((X
7a1c0 31 20 2b 20 58 32 20 2b 20 44 20 2b 20 42 20 2d 1 + X2 + D + B -
7a1d0 20 31 35 32 34 2e 35 20 29 20 2a 20 38 36 34 30 1524.5 ) * 8640
7a1e0 30 30 30 30 29 3b 0a 20 20 70 2d 3e 76 61 6c 69 0000);. p->vali
7a1f0 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66 28 20 70 dJD = 1;. if( p
7a200 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b 0a 20 20 ->validHMS ){.
7a210 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 70 2d 3e 68 p->iJD += p->h
7a220 2a 33 36 30 30 30 30 30 20 2b 20 70 2d 3e 6d 2a *3600000 + p->m*
7a230 36 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 60000 + (sqlite3
7a240 5f 69 6e 74 36 34 29 28 70 2d 3e 73 2a 31 30 30 _int64)(p->s*100
7a250 30 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 0);. if( p->v
7a260 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 20 20 alidTZ ){.
7a270 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e 74 7a 2a p->iJD -= p->tz*
7a280 36 30 30 30 30 3b 0a 20 20 20 20 20 20 70 2d 3e 60000;. p->
7a290 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 validYMD = 0;.
7a2a0 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 p->validHMS
7a2b0 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 = 0;. p->va
7a2c0 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 7d lidTZ = 0;. }
7a2d0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 . }.}../*.** Pa
7a2e0 72 73 65 20 64 61 74 65 73 20 6f 66 20 74 68 65 rse dates of the
7a2f0 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 form.**.**
7a300 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d YYYY-MM-DD HH:MM
7a310 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20 20 20 59 :SS.FFF.** Y
7a320 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a YYY-MM-DD HH:MM:
7a330 53 53 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d SS.** YYYY-M
7a340 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a 20 20 20 M-DD HH:MM.**
7a350 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2a 0a YYYY-MM-DD.**.
7a360 2a 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73 ** Write the res
7a370 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 44 61 74 ult into the Dat
7a380 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20 eTime structure
7a390 61 6e 64 20 72 65 74 75 72 6e 20 30 0a 2a 2a 20 and return 0.**
7a3a0 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 on success and 1
7a3b0 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 if the input st
7a3c0 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 20 77 65 ring is not a we
7a3d0 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 64 61 74 ll-formed.** dat
7a3e0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
7a3f0 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 63 parseYyyyMmDd(c
7a400 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 onst char *zDate
7a410 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a , DateTime *p){.
7a420 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 6e int Y, M, D, n
7a430 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44 61 74 65 eg;.. if( zDate
7a440 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 [0]=='-' ){.
7a450 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 6e 65 67 zDate++;. neg
7a460 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
7a470 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a neg = 0;. }.
7a480 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 if( getDigits(
7a490 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39 39 2c 27 zDate,4,0,9999,'
7a4a0 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c 27 2d 27 -',&Y,2,1,12,'-'
7a4b0 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c 26 44 29 ,&M,2,1,31,0,&D)
7a4c0 21 3d 33 20 29 7b 0a 20 20 20 20 72 65 74 75 72 !=3 ){. retur
7a4d0 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 n 1;. }. zDate
7a4e0 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65 28 += 10;. while(
7a4f0 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
7a500 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54 27 3d 3d *zDate) || 'T'==
7a510 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 7b 20 7a *(u8*)zDate ){ z
7a520 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 Date++; }. if(
7a530 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 parseHhMmSs(zDat
7a540 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 e, p)==0 ){.
7a550 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 20 74 69 /* We got the ti
7a560 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 me */. }else if
7a570 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 7b 0a 20 ( *zDate==0 ){.
7a580 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d p->validHMS =
7a590 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
7a5a0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
7a5b0 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b p->validJD = 0;
7a5c0 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d . p->validYMD =
7a5d0 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 6e 65 67 1;. p->Y = neg
7a5e0 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 70 2d 3e ? -Y : Y;. p->
7a5f0 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 20 3d 20 M = M;. p->D =
7a600 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 D;. if( p->vali
7a610 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 dTZ ){. compu
7a620 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a 20 20 72 teJD(p);. }. r
7a630 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
7a640 2a 20 53 65 74 20 74 68 65 20 74 69 6d 65 20 74 * Set the time t
7a650 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 o the current ti
7a660 6d 65 20 72 65 70 6f 72 74 65 64 20 62 79 20 74 me reported by t
7a670 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61 74 69 63 he VFS.*/.static
7a680 20 76 6f 69 64 20 73 65 74 44 61 74 65 54 69 6d void setDateTim
7a690 65 54 6f 43 75 72 72 65 6e 74 28 73 71 6c 69 74 eToCurrent(sqlit
7a6a0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
7a6b0 65 78 74 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 ext, DateTime *p
7a6c0 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 ){. double r;.
7a6d0 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 sqlite3 *db = s
7a6e0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
7a6f0 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
7a700 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75 );. sqlite3OsCu
7a710 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 rrentTime(db->pV
7a720 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d 3e 69 4a fs, &r);. p->iJ
7a730 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 D = (sqlite3_int
7a740 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30 64)(r*86400000.0
7a750 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d 3e 76 61 + 0.5);. p->va
7a760 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a lidJD = 1;.}../*
7a770 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 70 .** Attempt to p
7a780 61 72 73 65 20 74 68 65 20 67 69 76 65 6e 20 73 arse the given s
7a790 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a 75 6c tring into a Jul
7a7a0 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72 2e 20 ian Day Number.
7a7b0 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e Return.** the n
7a7c0 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e umber of errors.
7a7d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
7a7e0 77 69 6e 67 20 61 72 65 20 61 63 63 65 70 74 61 wing are accepta
7a7f0 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20 74 68 ble forms for th
7a800 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 3a 0a e input string:.
7a810 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59 59 2d **.** YYYY-
7a820 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 MM-DD HH:MM:SS.F
7a830 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a 2a 20 FF +/-HH:MM.**
7a840 20 20 20 20 20 44 44 44 44 2e 44 44 20 0a 2a 2a DDDD.DD .**
7a850 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a 2a 20 now.**.**
7a860 49 6e 20 74 68 65 20 66 69 72 73 74 20 66 6f 72 In the first for
7a870 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d 4d 20 m, the +/-HH:MM
7a880 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69 6f 6e is always option
7a890 61 6c 2e 20 20 54 68 65 20 66 72 61 63 74 69 6f al. The fractio
7a8a0 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73 20 65 nal.** seconds e
7a8b0 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 20 22 2e xtension (the ".
7a8c0 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f 6e 61 FFF") is optiona
7a8d0 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 73 20 l. The seconds
7a8e0 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a 53 53 portion.** (":SS
7a8f0 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f 6e .FFF") is option
7a900 2e 20 20 54 68 65 20 79 65 61 72 20 61 6e 64 20 . The year and
7a910 64 61 74 65 20 63 61 6e 20 62 65 20 6f 6d 69 74 date can be omit
7a920 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 ted as long.** a
7a930 73 20 74 68 65 72 65 20 69 73 20 61 20 74 69 6d s there is a tim
7a940 65 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 74 e string. The t
7a950 69 6d 65 20 73 74 72 69 6e 67 20 63 61 6e 20 62 ime string can b
7a960 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e e omitted as lon
7a970 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73 g.** as there is
7a980 20 61 20 79 65 61 72 20 61 6e 64 20 64 61 74 65 a year and date
7a990 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
7a9a0 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d 65 28 parseDateOrTime(
7a9b0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
7a9c0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 xt *context, .
7a9d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 const char *zDat
7a9e0 65 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a e, . DateTime *
7a9f0 70 0a 29 7b 0a 20 20 69 6e 74 20 69 73 52 65 61 p.){. int isRea
7aa00 6c 4e 75 6d 3b 20 20 20 20 2f 2a 20 52 65 74 75 lNum; /* Retu
7aa10 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 49 rn from sqlite3I
7aa20 73 4e 75 6d 62 65 72 28 29 2e 20 20 4e 6f 74 20 sNumber(). Not
7aa30 75 73 65 64 20 2a 2f 0a 20 20 69 66 28 20 70 61 used */. if( pa
7aa40 72 73 65 59 79 79 79 4d 6d 44 64 28 7a 44 61 74 rseYyyyMmDd(zDat
7aa50 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 e,p)==0 ){. r
7aa60 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 eturn 0;. }else
7aa70 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 if( parseHhMmSs
7aa80 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b (zDate, p)==0 ){
7aa90 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
7aaa0 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 }else if( sqlit
7aab0 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74 65 2c e3StrICmp(zDate,
7aac0 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20 20 20 20 "now")==0){.
7aad0 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 setDateTimeToCur
7aae0 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 29 rent(context, p)
7aaf0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
7ab00 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 }else if( sqli
7ab10 74 65 33 49 73 4e 75 6d 62 65 72 28 7a 44 61 74 te3IsNumber(zDat
7ab20 65 2c 20 26 69 73 52 65 61 6c 4e 75 6d 2c 20 53 e, &isRealNum, S
7ab30 51 4c 49 54 45 5f 55 54 46 38 29 20 29 7b 0a 20 QLITE_UTF8) ){.
7ab40 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 20 double r;.
7ab50 20 67 65 74 56 61 6c 75 65 28 7a 44 61 74 65 2c getValue(zDate,
7ab60 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e 69 4a 44 &r);. p->iJD
7ab70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 = (sqlite3_int6
7ab80 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30 20 4)(r*86400000.0
7ab90 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 + 0.5);. p->v
7aba0 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 20 20 alidJD = 1;.
7abb0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
7abc0 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a return 1;.}../*.
7abd0 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 59 ** Compute the Y
7abe0 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61 6e 64 20 ear, Month, and
7abf0 44 61 79 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c Day from the jul
7ac00 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a ian day number..
7ac10 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
7ac20 6f 6d 70 75 74 65 59 4d 44 28 44 61 74 65 54 69 omputeYMD(DateTi
7ac30 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 5a 2c me *p){. int Z,
7ac40 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20 45 2c 20 A, B, C, D, E,
7ac50 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c X1;. if( p->val
7ac60 69 64 59 4d 44 20 29 20 72 65 74 75 72 6e 3b 0a idYMD ) return;.
7ac70 20 20 69 66 28 20 21 70 2d 3e 76 61 6c 69 64 4a if( !p->validJ
7ac80 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59 20 3d 20 D ){. p->Y =
7ac90 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 2000;. p->M =
7aca0 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 31 1;. p->D = 1
7acb0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 5a ;. }else{. Z
7acc0 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e 69 4a 44 = (int)((p->iJD
7acd0 20 2b 20 34 33 32 30 30 30 30 30 29 2f 38 36 34 + 43200000)/864
7ace0 30 30 30 30 30 29 3b 0a 20 20 20 20 41 20 3d 20 00000);. A =
7acf0 28 69 6e 74 29 28 28 5a 20 2d 20 31 38 36 37 32 (int)((Z - 18672
7ad00 31 36 2e 32 35 29 2f 33 36 35 32 34 2e 32 35 29 16.25)/36524.25)
7ad10 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b 20 31 20 ;. A = Z + 1
7ad20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a 20 20 20 + A - (A/4);.
7ad30 20 42 20 3d 20 41 20 2b 20 31 35 32 34 3b 0a 20 B = A + 1524;.
7ad40 20 20 20 43 20 3d 20 28 69 6e 74 29 28 28 42 20 C = (int)((B
7ad50 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e 32 35 29 - 122.1)/365.25)
7ad60 3b 0a 20 20 20 20 44 20 3d 20 28 33 36 35 32 35 ;. D = (36525
7ad70 2a 43 29 2f 31 30 30 3b 0a 20 20 20 20 45 20 3d *C)/100;. E =
7ad80 20 28 69 6e 74 29 28 28 42 2d 44 29 2f 33 30 2e (int)((B-D)/30.
7ad90 36 30 30 31 29 3b 0a 20 20 20 20 58 31 20 3d 20 6001);. X1 =
7ada0 28 69 6e 74 29 28 33 30 2e 36 30 30 31 2a 45 29 (int)(30.6001*E)
7adb0 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 42 20 2d ;. p->D = B -
7adc0 20 44 20 2d 20 58 31 3b 0a 20 20 20 20 70 2d 3e D - X1;. p->
7add0 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d 31 20 3a M = E<14 ? E-1 :
7ade0 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d 3e 59 20 E-13;. p->Y
7adf0 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 2d 20 34 = p->M>2 ? C - 4
7ae00 37 31 36 20 3a 20 43 20 2d 20 34 37 31 35 3b 0a 716 : C - 4715;.
7ae10 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d }. p->validYM
7ae20 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D = 1;.}../*.**
7ae30 43 6f 6d 70 75 74 65 20 74 68 65 20 48 6f 75 72 Compute the Hour
7ae40 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64 20 53 65 , Minute, and Se
7ae50 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68 65 20 6a conds from the j
7ae60 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 ulian day number
7ae70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
7ae80 20 63 6f 6d 70 75 74 65 48 4d 53 28 44 61 74 65 computeHMS(Date
7ae90 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Time *p){. int
7aea0 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 s;. if( p->vali
7aeb0 64 48 4d 53 20 29 20 72 65 74 75 72 6e 3b 0a 20 dHMS ) return;.
7aec0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
7aed0 20 73 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e 69 s = (int)((p->i
7aee0 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 20 25 JD + 43200000) %
7aef0 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 70 2d 86400000);. p-
7af00 3e 73 20 3d 20 73 2f 31 30 30 30 2e 30 3b 0a 20 >s = s/1000.0;.
7af10 20 73 20 3d 20 28 69 6e 74 29 70 2d 3e 73 3b 0a s = (int)p->s;.
7af20 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a 20 20 70 p->s -= s;. p
7af30 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b 0a 20 20 ->h = s/3600;.
7af40 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 30 3b 0a s -= p->h*3600;.
7af50 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30 3b 0a 20 p->m = s/60;.
7af60 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20 70 2d 3e p->s += s - p->
7af70 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 m*60;. p->valid
7af80 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a HMS = 1;.}../*.*
7af90 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74 68 20 59 * Compute both Y
7afa0 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f 0a 73 74 MD and HMS.*/.st
7afb0 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 atic void comput
7afc0 65 59 4d 44 5f 48 4d 53 28 44 61 74 65 54 69 6d eYMD_HMS(DateTim
7afd0 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70 75 74 65 e *p){. compute
7afe0 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d 70 75 74 YMD(p);. comput
7aff0 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a eHMS(p);.}../*.*
7b000 2a 20 43 6c 65 61 72 20 74 68 65 20 59 4d 44 20 * Clear the YMD
7b010 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 68 65 20 and HMS and the
7b020 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 TZ.*/.static voi
7b030 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 d clearYMD_HMS_T
7b040 5a 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a Z(DateTime *p){.
7b050 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 p->validYMD =
7b060 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 0;. p->validHMS
7b070 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 = 0;. p->valid
7b080 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 TZ = 0;.}..#ifnd
7b090 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
7b0a0 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a 2a 20 43 OCALTIME./*.** C
7b0b0 6f 6d 70 75 74 65 20 74 68 65 20 64 69 66 66 65 ompute the diffe
7b0c0 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c 6c 69 73 rence (in millis
7b0d0 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65 74 77 65 econds).** betwe
7b0e0 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61 6e 64 en localtime and
7b0f0 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 UTC (a.k.a. GMT
7b100 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 69 6d ).** for the tim
7b110 65 20 76 61 6c 75 65 20 70 20 77 68 65 72 65 20 e value p where
7b120 70 20 69 73 20 69 6e 20 55 54 43 2e 0a 2a 2f 0a p is in UTC..*/.
7b130 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 69 static sqlite3_i
7b140 6e 74 36 34 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 nt64 localtimeOf
7b150 66 73 65 74 28 44 61 74 65 54 69 6d 65 20 2a 70 fset(DateTime *p
7b160 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 2c ){. DateTime x,
7b170 20 79 3b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a y;. time_t t;.
7b180 20 20 78 20 3d 20 2a 70 3b 0a 20 20 63 6f 6d 70 x = *p;. comp
7b190 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a uteYMD_HMS(&x);.
7b1a0 20 20 69 66 28 20 78 2e 59 3c 31 39 37 31 20 7c if( x.Y<1971 |
7b1b0 7c 20 78 2e 59 3e 3d 32 30 33 38 20 29 7b 0a 20 | x.Y>=2038 ){.
7b1c0 20 20 20 78 2e 59 20 3d 20 32 30 30 30 3b 0a 20 x.Y = 2000;.
7b1d0 20 20 20 78 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 x.M = 1;.
7b1e0 78 2e 44 20 3d 20 31 3b 0a 20 20 20 20 78 2e 68 x.D = 1;. x.h
7b1f0 20 3d 20 30 3b 0a 20 20 20 20 78 2e 6d 20 3d 20 = 0;. x.m =
7b200 30 3b 0a 20 20 20 20 78 2e 73 20 3d 20 30 2e 30 0;. x.s = 0.0
7b210 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
7b220 20 69 6e 74 20 73 20 3d 20 28 69 6e 74 29 28 78 int s = (int)(x
7b230 2e 73 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 78 .s + 0.5);. x
7b240 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 20 78 2e .s = s;. }. x.
7b250 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 61 6c 69 tz = 0;. x.vali
7b260 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 dJD = 0;. compu
7b270 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 20 3d 20 teJD(&x);. t =
7b280 28 74 69 6d 65 5f 74 29 28 78 2e 69 4a 44 2f 31 (time_t)(x.iJD/1
7b290 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 28 000 - 21086676*(
7b2a0 69 36 34 29 31 30 30 30 30 29 3b 0a 23 69 66 64 i64)10000);.#ifd
7b2b0 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ef HAVE_LOCALTIM
7b2c0 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 E_R. {. stru
7b2d0 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 ct tm sLocal;.
7b2e0 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 26 74 localtime_r(&t
7b2f0 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 , &sLocal);.
7b300 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.Y = sLocal.tm_
7b310 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 year + 1900;.
7b320 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.M = sLocal.tm
7b330 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e _mon + 1;. y.
7b340 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 D = sLocal.tm_md
7b350 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c ay;. y.h = sL
7b360 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 ocal.tm_hour;.
7b370 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.m = sLocal.t
7b380 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d m_min;. y.s =
7b390 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a sLocal.tm_sec;.
7b3a0 20 20 7d 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 }.#elif define
7b3b0 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 d(HAVE_LOCALTIME
7b3c0 5f 53 29 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 _S). {. stru
7b3d0 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 ct tm sLocal;.
7b3e0 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 73 localtime_s(&s
7b3f0 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 20 Local, &t);.
7b400 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.Y = sLocal.tm_
7b410 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 year + 1900;.
7b420 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.M = sLocal.tm
7b430 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e _mon + 1;. y.
7b440 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 D = sLocal.tm_md
7b450 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c ay;. y.h = sL
7b460 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 ocal.tm_hour;.
7b470 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.m = sLocal.t
7b480 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d m_min;. y.s =
7b490 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a sLocal.tm_sec;.
7b4a0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 }.#else. {.
7b4b0 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d struct tm *pTm
7b4c0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
7b4d0 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 tex_enter(sqlite
7b4e0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
7b4f0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
7b500 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54 MASTER));. pT
7b510 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74 m = localtime(&t
7b520 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d );. y.Y = pTm
7b530 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 ->tm_year + 1900
7b540 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d ;. y.M = pTm-
7b550 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 >tm_mon + 1;.
7b560 20 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d y.D = pTm->tm_m
7b570 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70 day;. y.h = p
7b580 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 Tm->tm_hour;.
7b590 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d y.m = pTm->tm_m
7b5a0 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54 in;. y.s = pT
7b5b0 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73 m->tm_sec;. s
7b5c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
7b5d0 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ve(sqlite3MutexA
7b5e0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
7b5f0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
7b600 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
7b610 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a y.validYMD = 1;.
7b620 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31 y.validHMS = 1
7b630 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20 ;. y.validJD =
7b640 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d 0;. y.validTZ =
7b650 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 0;. computeJD(
7b660 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e &y);. return y.
7b670 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 iJD - x.iJD;.}.#
7b680 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
7b690 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a OMIT_LOCALTIME *
7b6a0 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 /../*.** Process
7b6b0 20 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61 a modifier to a
7b6c0 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70 date-time stamp
7b6d0 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73 . The modifiers
7b6e0 20 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f are.** as follo
7b6f0 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e ws:.**.** NN
7b700 4e 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e N days.** NN
7b710 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e N hours.** N
7b720 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20 NN minutes.**
7b730 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e NNN.NNNN secon
7b740 64 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f ds.** NNN mo
7b750 6e 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 nths.** NNN
7b760 79 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61 years.** sta
7b770 72 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 rt of month.**
7b780 20 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72 start of year
7b790 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 .** start of
7b7a0 20 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61 week.** sta
7b7b0 72 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20 rt of day.**
7b7c0 20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 weekday N.**
7b7d0 20 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20 unixepoch.**
7b7e0 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 localtime.**
7b7f0 20 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 utc.**.** Re
7b800 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 turn 0 on succes
7b810 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 s and 1 if there
7b820 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 is any kind of
7b830 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 error..*/.static
7b840 20 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 69 int parseModifi
7b850 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a er(const char *z
7b860 4d 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 Mod, DateTime *p
7b870 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b ){. int rc = 1;
7b880 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 . int n;. doub
7b890 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c le r;. char *z,
7b8a0 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d zBuf[30];. z =
7b8b0 20 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 zBuf;. for(n=0
7b8c0 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42 ; n<ArraySize(zB
7b8d0 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d uf)-1 && zMod[n]
7b8e0 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d ; n++){. z[n]
7b8f0 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 = (char)sqlite3
7b900 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 UpperToLower[(u8
7b910 29 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 )zMod[n]];. }.
7b920 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69 z[n] = 0;. swi
7b930 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 tch( z[0] ){.#if
7b940 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
7b950 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 63 _LOCALTIME. c
7b960 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20 ase 'l': {.
7b970 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 /* localtime
7b980 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
7b990 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 ** Assuming the
7b9a0 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 6c current time val
7b9b0 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e 61 ue is UTC (a.k.a
7b9c0 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 74 . GMT), shift it
7b9d0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f to. ** sho
7b9e0 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20 w local time..
7b9f0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
7ba00 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 strcmp(z, "loca
7ba10 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20 ltime")==0 ){.
7ba20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
7ba30 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 p);. p->i
7ba40 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f JD += localtimeO
7ba50 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 ffset(p);.
7ba60 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 clearYMD_HMS_T
7ba70 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 Z(p);. rc
7ba80 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 0;. }.
7ba90 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
7baa0 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 .#endif. case
7bab0 20 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 'u': {. /*
7bac0 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69 . ** uni
7bad0 78 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a xepoch. **.
7bae0 20 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74 ** Treat t
7baf0 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
7bb00 20 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68 of p->iJD as th
7bb10 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 e number of.
7bb20 20 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e ** seconds sin
7bb30 63 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72 ce 1970. Conver
7bb40 74 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69 t to a real juli
7bb50 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20 an day number..
7bb60 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
7bb70 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69 ( strcmp(z, "uni
7bb80 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70 xepoch")==0 && p
7bb90 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 ->validJD ){.
7bba0 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 70 p->iJD = (p
7bbb0 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 29 2f 38 ->iJD + 43200)/8
7bbc0 36 34 30 30 20 2b 20 32 31 30 38 36 36 37 36 2a 6400 + 21086676*
7bbd0 28 69 36 34 29 31 30 30 30 30 30 30 30 3b 0a 20 (i64)10000000;.
7bbe0 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f clearYMD_
7bbf0 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 HMS_TZ(p);.
7bc00 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
7bc10 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
7bc20 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 E_OMIT_LOCALTIME
7bc30 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 . else if(
7bc40 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22 29 strcmp(z, "utc")
7bc50 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
7bc60 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 31 3b qlite3_int64 c1;
7bc70 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
7bc80 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 JD(p);. c
7bc90 31 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 1 = localtimeOff
7bca0 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 set(p);.
7bcb0 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 p->iJD -= c1;.
7bcc0 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 clearYMD_H
7bcd0 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 MS_TZ(p);.
7bce0 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 63 31 20 2d p->iJD += c1 -
7bcf0 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 localtimeOffset
7bd00 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 (p);. rc
7bd10 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e = 0;. }.#en
7bd20 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b dif. break;
7bd30 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
7bd40 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 'w': {. /*.
7bd50 20 20 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b ** week
7bd60 64 61 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 day N. **.
7bd70 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 ** Move the
7bd80 20 64 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d date to the sam
7bd90 65 20 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65 e time on the ne
7bda0 78 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 xt occurrence of
7bdb0 0a 20 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61 . ** weekda
7bdc0 79 20 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e y N where 0==Sun
7bdd0 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 day, 1==Monday,
7bde0 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 and so forth. I
7bdf0 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 f the. ** d
7be00 61 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f ate is already o
7be10 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 n the appropriat
7be20 65 20 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20 e weekday, this
7be30 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 is a no-op..
7be40 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 */. if( s
7be50 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64 trncmp(z, "weekd
7be60 61 79 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67 ay ", 8)==0 && g
7be70 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 etValue(&z[8],&r
7be80 29 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 )>0.
7be90 20 20 20 20 20 26 26 20 28 6e 3d 28 69 6e 74 29 && (n=(int)
7bea0 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26 r)==r && n>=0 &&
7beb0 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20 r<7 ){.
7bec0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 5a 3b sqlite3_int64 Z;
7bed0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
7bee0 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 YMD_HMS(p);.
7bef0 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d p->validTZ =
7bf00 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 0;. p->v
7bf10 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 alidJD = 0;.
7bf20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
7bf30 3b 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 28 28 ;. Z = ((
7bf40 70 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 30 30 p->iJD + 1296000
7bf50 30 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20 00)/86400000) %
7bf60 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 5a 7;. if( Z
7bf70 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 >n ) Z -= 7;.
7bf80 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
7bf90 6e 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 3b n - Z)*86400000;
7bfa0 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d . clearYM
7bfb0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
7bfc0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7bfd0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
7bfe0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
7bff0 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 's': {. /*
7c000 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73 74 61 . ** sta
7c010 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20 20 20 rt of TTTTT.
7c020 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f **. ** Mo
7c030 76 65 20 74 68 65 20 64 61 74 65 20 62 61 63 6b ve the date back
7c040 77 61 72 64 73 20 74 6f 20 74 68 65 20 62 65 67 wards to the beg
7c050 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 75 inning of the cu
7c060 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 20 20 rrent day,.
7c070 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 ** or month or
7c080 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 year.. */.
7c090 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70 if( strncmp
7c0a0 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 22 2c (z, "start of ",
7c0b0 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a 9)!=0 ) break;.
7c0c0 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 z += 9;.
7c0d0 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 computeYMD(p
7c0e0 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 );. p->vali
7c0f0 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 20 20 dHMS = 1;.
7c100 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b p->h = p->m = 0;
7c110 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e . p->s = 0.
7c120 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 0;. p->vali
7c130 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 dTZ = 0;. p
7c140 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 ->validJD = 0;.
7c150 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 if( strcmp(
7c160 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b z,"month")==0 ){
7c170 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 . p->D =
7c180 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 1;. rc =
7c190 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 0;. }else i
7c1a0 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 f( strcmp(z,"yea
7c1b0 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 r")==0 ){.
7c1c0 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b computeYMD(p);
7c1d0 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20 . p->M =
7c1e0 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 1;. p->D
7c1f0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 = 1;. rc
7c200 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
7c210 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 64 if( strcmp(z,"d
7c220 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ay")==0 ){.
7c230 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
7c240 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
7c250 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 }. case '
7c260 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27 2d 27 +':. case '-'
7c270 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 0a :. case '0':.
7c280 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a 20 20 case '1':.
7c290 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 20 20 case '2':.
7c2a0 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20 63 61 case '3':. ca
7c2b0 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 se '4':. case
7c2c0 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '5':. case '
7c2d0 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27 37 27 6':. case '7'
7c2e0 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27 3a 0a :. case '8':.
7c2f0 20 20 20 20 63 61 73 65 20 27 39 27 3a 20 7b 0a case '9': {.
7c300 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 52 6f double rRo
7c310 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 6e 20 3d under;. n =
7c320 20 67 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29 getValue(z, &r)
7c330 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
7c340 6e 3e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 n>=1 );. if
7c350 28 20 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 ( z[n]==':' ){.
7c360 20 20 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69 /* A modi
7c370 66 69 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d fier of the form
7c380 20 28 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 (+|-)HH:MM:SS.F
7c390 46 46 20 61 64 64 73 20 28 6f 72 20 73 75 62 74 FF adds (or subt
7c3a0 72 61 63 74 73 29 20 74 68 65 0a 20 20 20 20 20 racts) the.
7c3b0 20 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 ** specified
7c3c0 6e 75 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c number of hours,
7c3d0 20 6d 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64 minutes, second
7c3e0 73 2c 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61 s, and fractiona
7c3f0 6c 20 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20 l seconds.
7c400 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65 ** to the time
7c410 2e 20 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61 . The ".FFF" ma
7c420 79 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 y be omitted. T
7c430 68 65 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79 he ":SS.FFF" may
7c440 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f be. ** o
7c450 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20 mitted..
7c460 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 */. const
7c470 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 char *z2 = z;.
7c480 20 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20 DateTime
7c490 74 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 tx;. sqli
7c4a0 74 65 33 5f 69 6e 74 36 34 20 64 61 79 3b 0a 20 te3_int64 day;.
7c4b0 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 if( !sqli
7c4c0 74 65 33 49 73 64 69 67 69 74 28 2a 7a 32 29 20 te3Isdigit(*z2)
7c4d0 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20 ) z2++;.
7c4e0 6d 65 6d 73 65 74 28 26 74 78 2c 20 30 2c 20 73 memset(&tx, 0, s
7c4f0 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20 20 20 izeof(tx));.
7c500 20 20 20 20 69 66 28 20 70 61 72 73 65 48 68 4d if( parseHhM
7c510 6d 53 73 28 7a 32 2c 20 26 74 78 29 20 29 20 62 mSs(z2, &tx) ) b
7c520 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 6f reak;. co
7c530 6d 70 75 74 65 4a 44 28 26 74 78 29 3b 0a 20 20 mputeJD(&tx);.
7c540 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d 3d 20 tx.iJD -=
7c550 34 33 32 30 30 30 30 30 3b 0a 20 20 20 20 20 20 43200000;.
7c560 20 20 64 61 79 20 3d 20 74 78 2e 69 4a 44 2f 38 day = tx.iJD/8
7c570 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 6400000;.
7c580 20 74 78 2e 69 4a 44 20 2d 3d 20 64 61 79 2a 38 tx.iJD -= day*8
7c590 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 6400000;.
7c5a0 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 if( z[0]=='-' )
7c5b0 20 74 78 2e 69 4a 44 20 3d 20 2d 74 78 2e 69 4a tx.iJD = -tx.iJ
7c5c0 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 D;. compu
7c5d0 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 teJD(p);.
7c5e0 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a clearYMD_HMS_TZ
7c5f0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
7c600 69 4a 44 20 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 iJD += tx.iJD;.
7c610 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
7c620 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
7c630 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20 2b 3d }. z +=
7c640 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 n;. while(
7c650 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
7c660 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 *z) ) z++;.
7c670 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
7c680 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 20 20 69 en30(z);. i
7c690 66 28 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 f( n>10 || n<3 )
7c6a0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66 break;. if
7c6b0 28 20 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b ( z[n-1]=='s' ){
7c6c0 20 7a 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d z[n-1] = 0; n--
7c6d0 3b 20 7d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 ; }. comput
7c6e0 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 72 63 eJD(p);. rc
7c6f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 52 6f 75 = 0;. rRou
7c700 6e 64 65 72 20 3d 20 72 3c 30 20 3f 20 2d 30 2e nder = r<0 ? -0.
7c710 35 20 3a 20 2b 30 2e 35 3b 0a 20 20 20 20 20 20 5 : +0.5;.
7c720 69 66 28 20 6e 3d 3d 33 20 26 26 20 73 74 72 63 if( n==3 && strc
7c730 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 mp(z,"day")==0 )
7c740 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 {. p->iJD
7c750 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 += (sqlite3_int
7c760 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30 64)(r*86400000.0
7c770 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 + rRounder);.
7c780 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d }else if( n=
7c790 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 =4 && strcmp(z,"
7c7a0 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 hour")==0 ){.
7c7b0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
7c7c0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
7c7d0 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 32 34 2e *(86400000.0/24.
7c7e0 30 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 0) + rRounder);.
7c7f0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
7c800 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a n==6 && strcmp(z
7c810 2c 22 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b ,"minute")==0 ){
7c820 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
7c830 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 += (sqlite3_int6
7c840 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 2e 30 4)(r*(86400000.0
7c850 2f 28 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20 /(24.0*60.0)) +
7c860 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
7c870 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20 }else if( n==6
7c880 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 63 && strcmp(z,"sec
7c890 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 ond")==0 ){.
7c8a0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 p->iJD += (s
7c8b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a qlite3_int64)(r*
7c8c0 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e (86400000.0/(24.
7c8d0 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b 20 0*60.0*60.0)) +
7c8e0 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
7c8f0 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 35 20 }else if( n==5
7c900 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e && strcmp(z,"mon
7c910 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 th")==0 ){.
7c920 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 20 20 int x, y;.
7c930 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f computeYMD_
7c940 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 HMS(p);.
7c950 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74 29 72 3b 0a p->M += (int)r;.
7c960 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d 3e 4d x = p->M
7c970 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 >0 ? (p->M-1)/12
7c980 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b : (p->M-12)/12;
7c990 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d . p->Y +=
7c9a0 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d x;. p->M
7c9b0 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20 20 20 -= x*12;.
7c9c0 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 p->validJD = 0
7c9d0 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
7c9e0 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 eJD(p);.
7c9f0 79 20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 y = (int)r;.
7ca00 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b 0a if( y!=r ){.
7ca10 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 p->iJD
7ca20 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 += (sqlite3_int
7ca30 36 34 29 28 28 72 20 2d 20 79 29 2a 33 30 2e 30 64)((r - y)*30.0
7ca40 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52 *86400000.0 + rR
7ca50 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 20 ounder);.
7ca60 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 }. }else i
7ca70 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d f( n==4 && strcm
7ca80 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 p(z,"year")==0 )
7ca90 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 79 20 {. int y
7caa0 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 = (int)r;.
7cab0 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 computeYMD_HMS
7cac0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e (p);. p->
7cad0 59 20 2b 3d 20 79 3b 0a 20 20 20 20 20 20 20 20 Y += y;.
7cae0 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a p->validJD = 0;.
7caf0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a computeJ
7cb00 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 D(p);. if
7cb10 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 ( y!=r ){.
7cb20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 p->iJD += (s
7cb30 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72 qlite3_int64)((r
7cb40 20 2d 20 79 29 2a 33 36 35 2e 30 2a 38 36 34 30 - y)*365.0*8640
7cb50 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65 0000.0 + rRounde
7cb60 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 r);. }.
7cb70 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
7cb80 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 rc = 1;.
7cb90 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 59 4d }. clearYM
7cba0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
7cbb0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
7cbc0 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
7cbd0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7cbe0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
7cbf0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 c;.}../*.** Proc
7cc00 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74 69 6f ess time functio
7cc10 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 61 72 n arguments. ar
7cc20 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74 65 2d gv[0] is a date-
7cc30 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 time stamp..** a
7cc40 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f rgv[1] and follo
7cc50 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 69 65 wing are modifie
7cc60 72 73 2e 20 20 50 61 72 73 65 20 74 68 65 6d 20 rs. Parse them
7cc70 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a all and write.**
7cc80 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74 the resulting t
7cc90 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 61 74 ime into the Dat
7cca0 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20 eTime structure
7ccb0 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a 2a 20 p. Return 0.**
7ccc0 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 on success and 1
7ccd0 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e if there are an
7cce0 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 y errors..**.**
7ccf0 49 66 20 74 68 65 72 65 20 61 72 65 20 7a 65 72 If there are zer
7cd00 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28 69 66 o parameters (if
7cd10 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20 69 73 even argv[0] is
7cd20 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a 20 74 undefined).** t
7cd30 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64 65 66 hen assume a def
7cd40 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 22 6e ault value of "n
7cd50 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30 5d 2e ow" for argv[0].
7cd60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 .*/.static int i
7cd70 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 sDate(. sqlite3
7cd80 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7cd90 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 t, . int argc,
7cda0 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
7cdb0 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61 74 65 **argv, . Date
7cdc0 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 Time *p.){. int
7cdd0 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 i;. const unsi
7cde0 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 gned char *z;.
7cdf0 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d 65 6d int eType;. mem
7ce00 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 set(p, 0, sizeof
7ce10 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61 72 67 (*p));. if( arg
7ce20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65 74 44 c==0 ){. setD
7ce30 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 ateTimeToCurrent
7ce40 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20 (context, p);.
7ce50 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79 70 65 }else if( (eType
7ce60 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
7ce70 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 3d _type(argv[0]))=
7ce80 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a 20 20 =SQLITE_FLOAT.
7ce90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7cea0 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 || eType==SQLIT
7ceb0 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 E_INTEGER ){.
7cec0 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 p->iJD = (sqlit
7ced0 65 33 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 e3_int64)(sqlite
7cee0 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 3_value_double(a
7cef0 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30 30 30 rgv[0])*86400000
7cf00 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 .0 + 0.5);. p
7cf10 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 ->validJD = 1;.
7cf20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 }else{. z =
7cf30 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
7cf40 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 xt(argv[0]);.
7cf50 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 73 65 if( !z || parse
7cf60 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e 74 65 DateOrTime(conte
7cf70 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 70 29 xt, (char*)z, p)
7cf80 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
7cf90 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
7cfa0 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b for(i=1; i<argc;
7cfb0 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28 i++){. if( (
7cfc0 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 z = sqlite3_valu
7cfd0 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29 29 e_text(argv[i]))
7cfe0 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64 69 ==0 || parseModi
7cff0 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c 20 70 fier((char*)z, p
7d000 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
7d010 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
7d020 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f return 0;.}.../
7d030 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
7d040 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c ng routines impl
7d050 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75 ement the variou
7d060 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20 s date and time
7d070 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 functions.** of
7d080 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a SQLite..*/../*.*
7d090 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 20 * julianday(
7d0a0 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c TIMESTRING, MOD,
7d0b0 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a MOD, ...).**.**
7d0c0 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 6c 69 Return the juli
7d0d0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 6f 66 an day number of
7d0e0 20 74 68 65 20 64 61 74 65 20 73 70 65 63 69 66 the date specif
7d0f0 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d ied in the argum
7d100 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ents.*/.static v
7d110 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e oid juliandayFun
7d120 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
7d130 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
7d140 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
7d150 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
7d160 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 v.){. DateTime
7d170 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 x;. if( isDate(
7d180 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 context, argc, a
7d190 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 rgv, &x)==0 ){.
7d1a0 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 computeJD(&x)
7d1b0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
7d1c0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 sult_double(cont
7d1d0 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 30 ext, x.iJD/86400
7d1e0 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 000.0);. }.}../
7d1f0 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d 65 *.** datetime
7d200 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f ( TIMESTRING, MO
7d210 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a D, MOD, ...).**.
7d220 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d ** Return YYYY-M
7d230 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f M-DD HH:MM:SS.*/
7d240 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 .static void dat
7d250 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c etimeFunc(. sql
7d260 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7d270 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
7d280 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
7d290 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 ue **argv.){. D
7d2a0 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 ateTime x;. if(
7d2b0 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c isDate(context,
7d2c0 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 argc, argv, &x)
7d2d0 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 ==0 ){. char
7d2e0 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 zBuf[100];. c
7d2f0 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 omputeYMD_HMS(&x
7d300 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 );. sqlite3_s
7d310 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
7d320 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 34 Buf), zBuf, "%04
7d330 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32 64 d-%02d-%02d %02d
7d340 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 20 :%02d:%02d",.
7d350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7d360 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c x.Y, x.M, x.D,
7d370 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 x.h, x.m, (int)
7d380 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69 (x.s));. sqli
7d390 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
7d3a0 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d context, zBuf, -
7d3b0 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 1, SQLITE_TRANSI
7d3c0 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ENT);. }.}../*.
7d3d0 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d 45 ** time( TIME
7d3e0 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
7d3f0 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
7d400 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a urn HH:MM:SS.*/.
7d410 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65 static void time
7d420 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
7d430 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
7d440 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
7d450 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
7d460 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 argv.){. DateTi
7d470 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 me x;. if( isDa
7d480 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 te(context, argc
7d490 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 , argv, &x)==0 )
7d4a0 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b {. char zBuf[
7d4b0 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 100];. comput
7d4c0 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71 eHMS(&x);. sq
7d4d0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
7d4e0 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
7d4f0 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25 30 f, "%02d:%02d:%0
7d500 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 2d", x.h, x.m, (
7d510 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 71 int)x.s);. sq
7d520 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
7d530 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
7d540 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
7d550 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f SIENT);. }.}../
7d560 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49 *.** date( TI
7d570 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d MESTRING, MOD, M
7d580 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 OD, ...).**.** R
7d590 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 eturn YYYY-MM-DD
7d5a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7d5b0 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 dateFunc(. sqli
7d5c0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
7d5d0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
7d5e0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
7d5f0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 e **argv.){. Da
7d600 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 teTime x;. if(
7d610 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 isDate(context,
7d620 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d argc, argv, &x)=
7d630 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a =0 ){. char z
7d640 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f Buf[100];. co
7d650 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 20 mputeYMD(&x);.
7d660 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7d670 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c tf(sizeof(zBuf),
7d680 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 zBuf, "%04d-%02
7d690 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2e d-%02d", x.Y, x.
7d6a0 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 6c M, x.D);. sql
7d6b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
7d6c0 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 (context, zBuf,
7d6d0 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 -1, SQLITE_TRANS
7d6e0 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a IENT);. }.}../*
7d6f0 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65 28 .** strftime(
7d700 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 52 FORMAT, TIMESTR
7d710 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e ING, MOD, MOD, .
7d720 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ..).**.** Return
7d730 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72 69 a string descri
7d740 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 20 bed by FORMAT.
7d750 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 66 Conversions as f
7d760 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
7d770 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 68 %d day of month
7d780 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 61 .** %f ** fra
7d790 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 ctional seconds
7d7a0 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 20 SS.SSS.** %H
7d7b0 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 20 hour 00-24.**
7d7c0 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61 72 %j day of year
7d7d0 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 000-366.** %J
7d7e0 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 20 ** Julian day
7d7f0 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 20 number.** %m
7d800 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 20 month 01-12.**
7d810 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d 35 %M minute 00-5
7d820 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 6e 9.** %s secon
7d830 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30 31 ds since 1970-01
7d840 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 63 -01.** %S sec
7d850 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 20 onds 00-59.**
7d860 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20 %w day of week
7d870 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 0-6 sunday==0.*
7d880 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20 * %W week of
7d890 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 20 year 00-53.**
7d8a0 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39 39 %Y year 0000-99
7d8b0 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 2f 99.** %% %.*/
7d8c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72 .static void str
7d8d0 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c ftimeFunc(. sql
7d8e0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7d8f0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
7d900 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
7d910 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 ue **argv.){. D
7d920 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 34 ateTime x;. u64
7d930 20 6e 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 6a n;. size_t i,j
7d940 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 ;. char *z;. s
7d950 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f qlite3 *db;. co
7d960 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d nst char *zFmt =
7d970 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 (const char*)sq
7d980 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
7d990 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61 (argv[0]);. cha
7d9a0 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69 r zBuf[100];. i
7d9b0 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 f( zFmt==0 || is
7d9c0 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 Date(context, ar
7d9d0 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78 gc-1, argv+1, &x
7d9e0 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 ) ) return;. db
7d9f0 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 = sqlite3_conte
7da00 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e xt_db_handle(con
7da10 74 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30 text);. for(i=0
7da20 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 , n=1; zFmt[i];
7da30 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 i++, n++){. i
7da40 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 f( zFmt[i]=='%'
7da50 29 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 ){. switch(
7da60 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 zFmt[i+1] ){.
7da70 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a case 'd':.
7da80 20 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27 case 'H'
7da90 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
7daa0 6d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 m':. case
7dab0 20 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 'M':. ca
7dac0 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20 se 'S':.
7dad0 63 61 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20 case 'W':.
7dae0 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 n++;.
7daf0 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20 /* fall thru
7db00 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 */. case
7db10 27 77 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 'w':. cas
7db20 65 20 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20 e '%':.
7db30 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
7db40 63 61 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20 case 'f':.
7db50 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 n += 8;.
7db60 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7db70 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20 case 'j':.
7db80 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b n += 3;
7db90 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
7dba0 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
7dbb0 59 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 Y':. n
7dbc0 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 += 8;.
7dbd0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
7dbe0 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20 ase 's':.
7dbf0 20 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20 case 'J':.
7dc00 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 n += 50;.
7dc10 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7dc20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a default:.
7dc30 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
7dc40 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65 ; /* ERROR. re
7dc50 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 turn a NULL */.
7dc60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b }. i++
7dc70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 74 65 ;. }. }. te
7dc80 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f stcase( n==sizeo
7dc90 66 28 7a 42 75 66 29 2d 31 20 29 3b 0a 20 20 74 f(zBuf)-1 );. t
7dca0 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 estcase( n==size
7dcb0 6f 66 28 7a 42 75 66 29 20 29 3b 0a 20 20 74 65 of(zBuf) );. te
7dcc0 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29 stcase( n==(u64)
7dcd0 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
7dce0 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b E_LIMIT_LENGTH]+
7dcf0 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 1 );. testcase(
7dd00 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 n==(u64)db->aLi
7dd10 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
7dd20 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 69 66 _LENGTH] );. if
7dd30 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29 ( n<sizeof(zBuf)
7dd40 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 ){. z = zBuf
7dd50 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e ;. }else if( n>
7dd60 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b (u64)db->aLimit[
7dd70 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
7dd80 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 GTH] ){. sqli
7dd90 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
7dda0 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 _toobig(context)
7ddb0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
7ddc0 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 }else{. z = s
7ddd0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
7dde0 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 w(db, (int)n);.
7ddf0 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 if( z==0 ){.
7de00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
7de10 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 ult_error_nomem(
7de20 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 context);.
7de30 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
7de40 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 }. computeJD(&x
7de50 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f );. computeYMD_
7de60 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69 HMS(&x);. for(i
7de70 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 =j=0; zFmt[i]; i
7de80 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d ++){. if( zFm
7de90 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20 t[i]!='%' ){.
7dea0 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 z[j++] = zFmt
7deb0 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a [i];. }else{.
7dec0 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 i++;.
7ded0 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d switch( zFmt[i]
7dee0 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ){. case
7def0 20 27 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'd': sqlite3_s
7df00 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d nprintf(3, &z[j]
7df10 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b ,"%02d",x.D); j+
7df20 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 =2; break;.
7df30 20 20 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20 case 'f': {.
7df40 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20 double
7df50 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20 s = x.s;.
7df60 20 20 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20 if( s>59.999
7df70 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20 ) s = 59.999;.
7df80 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
7df90 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a snprintf(7, &z[j
7dfa0 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a ],"%06.3f", s);.
7dfb0 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 j += s
7dfc0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 qlite3Strlen30(&
7dfd0 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 z[j]);.
7dfe0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
7dff0 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 }. case '
7e000 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 H': sqlite3_snp
7e010 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 rintf(3, &z[j],"
7e020 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 %02d",x.h); j+=2
7e030 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
7e040 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20 46 61 case 'W': /* Fa
7e050 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 ll thru */.
7e060 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20 case 'j': {.
7e070 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 61 int nDa
7e080 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f y; /
7e090 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 79 73 * Number of days
7e0a0 20 73 69 6e 63 65 20 31 73 74 20 64 61 79 20 6f since 1st day o
7e0b0 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20 20 20 f year */.
7e0c0 20 20 20 20 44 61 74 65 54 69 6d 65 20 79 20 3d DateTime y =
7e0d0 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e x;. y.
7e0e0 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 validJD = 0;.
7e0f0 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a y.M = 1;.
7e100 20 20 20 20 20 20 20 20 20 20 79 2e 44 20 3d 20 y.D =
7e110 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6d 1;. com
7e120 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 20 20 puteJD(&y);.
7e130 20 20 20 20 20 20 6e 44 61 79 20 3d 20 28 69 6e nDay = (in
7e140 74 29 28 28 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b t)((x.iJD-y.iJD+
7e150 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 43200000)/864000
7e160 30 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 00);. i
7e170 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 f( zFmt[i]=='W'
7e180 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
7e190 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f nt wd; /* 0=Mo
7e1a0 6e 64 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c nday, 1=Tuesday,
7e1b0 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f ... 6=Sunday */
7e1c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 77 64 20 . wd
7e1d0 3d 20 28 69 6e 74 29 28 28 28 78 2e 69 4a 44 2b = (int)(((x.iJD+
7e1e0 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 43200000)/864000
7e1f0 30 30 29 25 37 29 3b 0a 20 20 20 20 20 20 20 20 00)%7);.
7e200 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
7e210 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 intf(3, &z[j],"%
7e220 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29 02d",(nDay+7-wd)
7e230 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 /7);.
7e240 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 j += 2;.
7e250 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
7e260 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
7e270 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c printf(4, &z[j],
7e280 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a "%03d",nDay+1);.
7e290 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d j +=
7e2a0 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 3;. }.
7e2b0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7e2c0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7e2d0 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20 case 'J': {.
7e2e0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
7e2f0 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a _snprintf(20, &z
7e300 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a [j],"%.16g",x.iJ
7e310 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 D/86400000.0);.
7e320 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 71 6c 69 j+=sqli
7e330 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a te3Strlen30(&z[j
7e340 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 ]);. br
7e350 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
7e360 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a case 'm':
7e370 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7e380 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 tf(3, &z[j],"%02
7e390 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 d",x.M); j+=2; b
7e3a0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
7e3b0 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74 65 33 se 'M': sqlite3
7e3c0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b _snprintf(3, &z[
7e3d0 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20 j],"%02d",x.m);
7e3e0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 j+=2; break;.
7e3f0 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b case 's': {
7e400 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
7e410 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c 26 e3_snprintf(30,&
7e420 7a 5b 6a 5d 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 z[j],"%lld",.
7e430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7e440 20 20 20 20 20 20 20 20 28 69 36 34 29 28 78 2e (i64)(x.
7e450 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 iJD/1000 - 21086
7e460 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 29 29 676*(i64)10000))
7e470 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d ;. j +=
7e480 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
7e490 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 (&z[j]);.
7e4a0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
7e4b0 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 }. case
7e4c0 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 'S': sqlite3_s
7e4d0 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c nprintf(3,&z[j],
7e4e0 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e 73 29 "%02d",(int)x.s)
7e4f0 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 ; j+=2; break;.
7e500 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a case 'w':
7e510 20 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a {. z[j
7e520 2b 2b 5d 20 3d 20 28 63 68 61 72 29 28 28 28 78 ++] = (char)(((x
7e530 2e 69 4a 44 2b 31 32 39 36 30 30 30 30 30 29 2f .iJD+129600000)/
7e540 38 36 34 30 30 30 30 30 29 20 25 20 37 29 20 2b 86400000) % 7) +
7e550 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '0';.
7e560 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
7e570 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59 . case 'Y
7e580 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73 ': {. s
7e590 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7e5a0 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 5,&z[j],"%04d",x
7e5b0 2e 59 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53 .Y); j+=sqlite3S
7e5c0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a trlen30(&z[j]);.
7e5d0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7e5e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7e5f0 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b default: z[
7e600 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61 j++] = '%'; brea
7e610 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
7e620 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b . }. z[j] = 0;
7e630 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
7e640 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
7e650 7a 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 z, -1,.
7e660 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 3d 3d z==
7e670 7a 42 75 66 20 3f 20 53 51 4c 49 54 45 5f 54 52 zBuf ? SQLITE_TR
7e680 41 4e 53 49 45 4e 54 20 3a 20 53 51 4c 49 54 45 ANSIENT : SQLITE
7e690 5f 44 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a _DYNAMIC);.}../*
7e6a0 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 .** current_time
7e6b0 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 ().**.** This fu
7e6c0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
7e6d0 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 he same value as
7e6e0 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f time('now')..*/
7e6f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 74 69 .static void cti
7e700 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 meFunc(. sqlite
7e710 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
7e720 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
7e730 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
7e740 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
7e750 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
7e760 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
7e770 74 55 73 65 64 32 29 3b 0a 20 20 74 69 6d 65 46 tUsed2);. timeF
7e780 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 unc(context, 0,
7e790 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 0);.}../*.** cur
7e7a0 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a rent_date().**.*
7e7b0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
7e7c0 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 returns the same
7e7d0 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 28 27 value as date('
7e7e0 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 now')..*/.static
7e7f0 20 76 6f 69 64 20 63 64 61 74 65 46 75 6e 63 28 void cdateFunc(
7e800 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
7e810 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
7e820 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
7e830 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f lite3_value **No
7e840 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 tUsed2.){. UNUS
7e850 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
7e860 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 tUsed, NotUsed2)
7e870 3b 0a 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e ;. dateFunc(con
7e880 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a text, 0, 0);.}..
7e890 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 /*.** current_ti
7e8a0 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 mestamp().**.**
7e8b0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
7e8c0 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 turns the same v
7e8d0 61 6c 75 65 20 61 73 20 64 61 74 65 74 69 6d 65 alue as datetime
7e8e0 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 ('now')..*/.stat
7e8f0 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61 ic void ctimesta
7e900 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 mpFunc(. sqlite
7e910 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
7e920 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
7e930 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
7e940 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
7e950 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
7e960 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
7e970 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 74 tUsed2);. datet
7e980 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c imeFunc(context,
7e990 20 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0, 0);.}.#endif
7e9a0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c /* !defined(SQL
7e9b0 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d ITE_OMIT_DATETIM
7e9c0 45 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 E_FUNCS) */..#if
7e9d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
7e9e0 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f DATETIME_FUNCS./
7e9f0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72 *.** If the libr
7ea00 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ary is compiled
7ea10 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c to omit the full
7ea20 2d 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20 -scale date and
7ea30 74 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 time.** handling
7ea40 20 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c (to get a small
7ea50 65 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20 er binary), the
7ea60 66 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 following minima
7ea70 6c 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 l version.** of
7ea80 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75 the functions cu
7ea90 72 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75 rrent_time(), cu
7eaa0 72 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64 rrent_date() and
7eab0 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 current_timesta
7eac0 6d 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c mp().** are incl
7ead0 75 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68 uded instead. Th
7eae0 69 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74 is is to support
7eaf0 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 column declarat
7eb00 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63 ions that.** inc
7eb10 6c 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55 lude "DEFAULT CU
7eb20 52 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e RRENT_TIME" etc.
7eb30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
7eb40 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d tion uses the C-
7eb50 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e library function
7eb60 73 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65 s time(), gmtime
7eb70 28 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69 ().** and strfti
7eb80 6d 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74 me(). The format
7eb90 20 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20 string to pass
7eba0 74 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73 to strftime() is
7ebb0 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20 supplied.** as
7ebc0 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f the user-data fo
7ebd0 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a r the function..
7ebe0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
7ebf0 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a urrentTimeFunc(.
7ec00 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
7ec10 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
7ec20 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
7ec30 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
7ec40 7b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 {. time_t t;.
7ec50 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 char *zFormat =
7ec60 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f (char *)sqlite3_
7ec70 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 user_data(contex
7ec80 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 t);. sqlite3 *d
7ec90 62 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a b;. double rT;.
7eca0 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b char zBuf[20];
7ecb0 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d .. UNUSED_PARAM
7ecc0 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 55 4e ETER(argc);. UN
7ecd0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 USED_PARAMETER(a
7ece0 72 67 76 29 3b 0a 0a 20 20 64 62 20 3d 20 73 71 rgv);.. db = sq
7ecf0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
7ed00 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
7ed10 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 ;. sqlite3OsCur
7ed20 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 rentTime(db->pVf
7ed30 73 2c 20 26 72 54 29 3b 0a 23 69 66 6e 64 65 66 s, &rT);.#ifndef
7ed40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
7ed50 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 74 20 ATING_POINT. t
7ed60 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20 2d 20 = 86400.0*(rT -
7ed70 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30 2e 35 2440587.5) + 0.5
7ed80 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 77 69 74 ;.#else. /* wit
7ed90 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f hout floating po
7eda0 69 6e 74 20 73 75 70 70 6f 72 74 2c 20 72 54 20 int support, rT
7edb0 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 will have. ** a
7edc0 6c 72 65 61 64 79 20 6c 6f 73 74 20 66 72 61 63 lready lost frac
7edd0 74 69 6f 6e 61 6c 20 64 61 79 20 70 72 65 63 69 tional day preci
7ede0 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d sion.. */. t =
7edf0 20 38 36 34 30 30 20 2a 20 28 72 54 20 2d 20 32 86400 * (rT - 2
7ee00 34 34 30 35 38 37 29 20 2d 20 34 33 32 30 30 3b 440587) - 43200;
7ee10 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48 .#endif.#ifdef H
7ee20 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 7b AVE_GMTIME_R. {
7ee30 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 . struct tm s
7ee40 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 5f Now;. gmtime_
7ee50 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 r(&t, &sNow);.
7ee60 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c strftime(zBuf,
7ee70 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73 20, zFormat, &s
7ee80 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a Now);. }.#else.
7ee90 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 {. struct t
7eea0 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69 m *pTm;. sqli
7eeb0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
7eec0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
7eed0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
7eee0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a TATIC_MASTER));.
7eef0 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65 pTm = gmtime
7ef00 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69 (&t);. strfti
7ef10 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f me(zBuf, 20, zFo
7ef20 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20 rmat, pTm);.
7ef30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
7ef40 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ave(sqlite3Mutex
7ef50 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
7ef60 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
7ef70 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a ));. }.#endif..
7ef80 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
7ef90 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a _text(context, z
7efa0 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f Buf, -1, SQLITE_
7efb0 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 TRANSIENT);.}.#e
7efc0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ndif../*.** This
7efd0 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 function regist
7efe0 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20 ered all of the
7eff0 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e above C function
7f000 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 s as SQL.** func
7f010 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f tions. This sho
7f020 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20 uld be the only
7f030 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 routine in this
7f040 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74 file with.** ext
7f050 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a ernal linkage..*
7f060 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
7f070 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 void sqlite3Reg
7f080 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e isterDateTimeFun
7f090 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 ctions(void){.
7f0a0 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 static SQLITE_WS
7f0b0 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 54 D FuncDef aDateT
7f0c0 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 imeFuncs[] = {.#
7f0d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
7f0e0 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 IT_DATETIME_FUNC
7f0f0 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6a S. FUNCTION(j
7f100 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 20 ulianday,
7f110 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 61 -1, 0, 0, julia
7f120 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 20 ndayFunc ),.
7f130 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 20 FUNCTION(date,
7f140 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 -1, 0
7f150 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 20 , 0, dateFunc
7f160 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
7f170 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 20 ON(time,
7f180 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 74 -1, 0, 0, t
7f190 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a imeFunc ),.
7f1a0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74 FUNCTION(dat
7f1b0 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 2d etime, -
7f1c0 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 6d 1, 0, 0, datetim
7f1d0 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55 eFunc ),. FU
7f1e0 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 2c NCTION(strftime,
7f1f0 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 -1, 0,
7f200 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20 0, strftimeFunc
7f210 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
7f220 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 (current_time,
7f230 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 69 0, 0, 0, cti
7f240 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 meFunc ),.
7f250 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 FUNCTION(curre
7f260 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c nt_timestamp, 0,
7f270 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 6d 0, 0, ctimestam
7f280 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43 pFunc),. FUNC
7f290 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 TION(current_dat
7f2a0 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c e, 0, 0, 0,
7f2b0 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 29 cdateFunc )
7f2c0 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 5f ,.#else. STR_
7f2d0 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 FUNCTION(current
7f2e0 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22 _time, 0, "
7f2f0 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20 %H:%M:%S",
7f300 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69 0, currentTi
7f310 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 meFunc),. STR
7f320 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e _FUNCTION(curren
7f330 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 t_timestamp, 0,
7f340 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 20 "%Y-%m-%d",
7f350 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 0, currentT
7f360 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 imeFunc),. ST
7f370 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 R_FUNCTION(curre
7f380 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c nt_date, 0,
7f390 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 4d "%Y-%m-%d %H:%M
7f3a0 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e 74 :%S", 0, current
7f3b0 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 69 TimeFunc),.#endi
7f3c0 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a f. };. int i;.
7f3d0 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 FuncDefHash *p
7f3e0 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 Hash = &GLOBAL(F
7f3f0 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 uncDefHash, sqli
7f400 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f te3GlobalFunctio
7f410 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a ns);. FuncDef *
7f420 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 aFunc = (FuncDef
7f430 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 *)&GLOBAL(FuncDe
7f440 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 f, aDateTimeFunc
7f450 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 s);.. for(i=0;
7f460 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 74 i<ArraySize(aDat
7f470 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b 2b eTimeFuncs); i++
7f480 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 ){. sqlite3Fu
7f490 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 ncDefInsert(pHas
7f4a0 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 h, &aFunc[i]);.
7f4b0 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
7f4c0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74 ***** End of dat
7f4d0 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
7f4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f500 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
7f510 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
7f520 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.c **********
7f530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f550 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 ****/./*.** 2005
7f560 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a November 29.**.
7f570 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
7f580 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
7f590 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
7f5a0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
7f5b0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
7f5c0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
7f5d0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
7f5e0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
7f5f0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
7f600 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
7f610 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
7f620 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
7f630 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
7f640 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
7f650 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
7f660 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
7f670 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
7f680 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
7f690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7f6d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
7f6e0 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74 contains OS int
7f6f0 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 74 erface code that
7f700 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c is common to al
7f710 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 l.** architectur
7f720 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f es..**.** $Id: o
7f730 73 2e 63 2c 76 20 31 2e 31 32 37 20 32 30 30 39 s.c,v 1.127 2009
7f740 2f 30 37 2f 32 37 20 31 31 3a 34 31 3a 32 31 20 /07/27 11:41:21
7f750 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
7f760 24 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 5f 53 51 $.*/.#define _SQ
7f770 4c 49 54 45 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e LITE_OS_C_ 1.#un
7f780 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 def _SQLITE_OS_C
7f790 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 _../*.** The def
7f7a0 61 75 6c 74 20 53 51 4c 69 74 65 20 73 71 6c 69 ault SQLite sqli
7f7b0 74 65 33 5f 76 66 73 20 69 6d 70 6c 65 6d 65 6e te3_vfs implemen
7f7c0 74 61 74 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 tations do not a
7f7d0 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 llocate.** memor
7f7e0 79 20 28 61 63 74 75 61 6c 6c 79 2c 20 6f 73 5f y (actually, os_
7f7f0 75 6e 69 78 2e 63 20 61 6c 6c 6f 63 61 74 65 73 unix.c allocates
7f800 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 a small amount
7f810 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f of memory.** fro
7f820 6d 20 77 69 74 68 69 6e 20 4f 73 4f 70 65 6e 28 m within OsOpen(
7f830 29 29 2c 20 62 75 74 20 73 6f 6d 65 20 74 68 69 )), but some thi
7f840 72 64 2d 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 rd-party impleme
7f850 6e 74 61 74 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a ntations may..**
7f860 20 53 6f 20 77 65 20 74 65 73 74 20 74 68 65 20 So we test the
7f870 65 66 66 65 63 74 73 20 6f 66 20 61 20 6d 61 6c effects of a mal
7f880 6c 6f 63 28 29 20 66 61 69 6c 69 6e 67 20 61 6e loc() failing an
7f890 64 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 58 d the sqlite3OsX
7f8a0 58 58 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e XX().** function
7f8b0 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 returning SQLIT
7f8c0 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 E_IOERR_NOMEM us
7f8d0 69 6e 67 20 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 ing the DO_OS_MA
7f8e0 4c 4c 4f 43 5f 54 45 53 54 20 6d 61 63 72 6f 2e LLOC_TEST macro.
7f8f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f .**.** The follo
7f900 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 61 wing functions a
7f910 72 65 20 69 6e 73 74 72 75 6d 65 6e 74 65 64 20 re instrumented
7f920 66 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 for malloc() fai
7f930 6c 75 72 65 20 0a 2a 2a 20 74 65 73 74 69 6e 67 lure .** testing
7f940 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 :.**.** sqli
7f950 74 65 33 4f 73 4f 70 65 6e 28 29 0a 2a 2a 20 20 te3OsOpen().**
7f960 20 20 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 sqlite3OsRead
7f970 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
7f980 33 4f 73 57 72 69 74 65 28 29 0a 2a 2a 20 20 20 3OsWrite().**
7f990 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 sqlite3OsSync(
7f9a0 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
7f9b0 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a 2a 2f 0a 23 OsLock().**.*/.#
7f9c0 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
7f9d0 45 5f 54 45 53 54 29 20 26 26 20 28 53 51 4c 49 E_TEST) && (SQLI
7f9e0 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30 29 0a 20 20 TE_OS_WIN==0).
7f9f0 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 #define DO_OS_MA
7fa00 4c 4c 4f 43 5f 54 45 53 54 28 78 29 20 69 66 20 LLOC_TEST(x) if
7fa10 28 21 78 20 7c 7c 20 21 73 71 6c 69 74 65 33 49 (!x || !sqlite3I
7fa20 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 78 29 29 20 sMemJournal(x))
7fa30 7b 20 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 64 { \. void
7fa40 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71 *pTstAlloc = sq
7fa50 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b lite3Malloc(10);
7fa60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7fa70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
7fa80 20 20 20 69 66 20 28 21 70 54 73 74 41 6c 6c 6f if (!pTstAllo
7fa90 63 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 c) return SQLITE
7faa0 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20 _IOERR_NOMEM;
7fab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7fac0 20 20 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 65 \. sqlite
7fad0 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c 6f 63 3_free(pTstAlloc
7fae0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
7faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7fb00 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
7fb10 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
7fb20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
7fb30 53 54 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a ST(x).#endif../*
7fb40 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
7fb50 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 g routines are c
7fb60 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 onvenience wrapp
7fb70 65 72 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f ers around metho
7fb80 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c ds.** of the sql
7fb90 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 ite3_file object
7fba0 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c . This is mostl
7fbb0 79 20 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 y just syntactic
7fbc0 20 73 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f sugar. All.** o
7fbd0 66 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 f this would be
7fbe0 63 6f 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d completely autom
7fbf0 61 74 69 63 20 69 66 20 53 51 4c 69 74 65 20 77 atic if SQLite w
7fc00 65 72 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a ere coded using.
7fc10 2a 2a 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f ** C++ instead o
7fc20 66 20 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a f plain old C..*
7fc30 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
7fc40 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c int sqlite3OsCl
7fc50 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ose(sqlite3_file
7fc60 20 2a 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 *pId){. int rc
7fc70 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
7fc80 69 66 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 if( pId->pMethod
7fc90 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 s ){. rc = pI
7fca0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c d->pMethods->xCl
7fcb0 6f 73 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49 ose(pId);. pI
7fcc0 64 2d 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b d->pMethods = 0;
7fcd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
7fce0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
7fcf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
7fd00 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c Read(sqlite3_fil
7fd10 65 20 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 e *id, void *pBu
7fd20 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 f, int amt, i64
7fd30 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 offset){. DO_OS
7fd40 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 _MALLOC_TEST(id)
7fd50 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 ;. return id->p
7fd60 4d 65 74 68 6f 64 73 2d 3e 78 52 65 61 64 28 69 Methods->xRead(i
7fd70 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 d, pBuf, amt, of
7fd80 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f fset);.}.SQLITE_
7fd90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7fda0 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c 69 74 te3OsWrite(sqlit
7fdb0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 63 6f 6e e3_file *id, con
7fdc0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 st void *pBuf, i
7fdd0 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 nt amt, i64 offs
7fde0 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c et){. DO_OS_MAL
7fdf0 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 LOC_TEST(id);.
7fe00 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
7fe10 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64 2c 20 ods->xWrite(id,
7fe20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 pBuf, amt, offse
7fe30 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 t);.}.SQLITE_PRI
7fe40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
7fe50 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 OsTruncate(sqlit
7fe60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 e3_file *id, i64
7fe70 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 72 6e size){. return
7fe80 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
7fe90 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73 69 7a Truncate(id, siz
7fea0 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 e);.}.SQLITE_PRI
7feb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
7fec0 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 OsSync(sqlite3_f
7fed0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 ile *id, int fla
7fee0 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c gs){. DO_OS_MAL
7fef0 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 LOC_TEST(id);.
7ff00 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
7ff10 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 ods->xSync(id, f
7ff20 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f lags);.}.SQLITE_
7ff30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
7ff40 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 te3OsFileSize(sq
7ff50 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
7ff60 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 i64 *pSize){. D
7ff70 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
7ff80 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 (id);. return i
7ff90 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 d->pMethods->xFi
7ffa0 6c 65 53 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 leSize(id, pSize
7ffb0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
7ffc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
7ffd0 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 sLock(sqlite3_fi
7ffe0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b le *id, int lock
7fff0 54 79 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d Type){. DO_OS_M
80000 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a ALLOC_TEST(id);.
80010 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
80020 74 68 6f 64 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c thods->xLock(id,
80030 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 lockType);.}.SQ
80040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
80050 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b sqlite3OsUnlock
80060 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
80070 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 d, int lockType)
80080 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 {. return id->p
80090 4d 65 74 68 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b Methods->xUnlock
800a0 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a (id, lockType);.
800b0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
800c0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 68 int sqlite3OsCh
800d0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
800e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
800f0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b , int *pResOut){
80100 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
80110 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 TEST(id);. retu
80120 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
80130 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c >xCheckReservedL
80140 6f 63 6b 28 69 64 2c 20 70 52 65 73 4f 75 74 29 ock(id, pResOut)
80150 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
80160 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
80170 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 FileControl(sqli
80180 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
80190 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 t op, void *pArg
801a0 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e ){. return id->
801b0 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43 pMethods->xFileC
801c0 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 ontrol(id, op, p
801d0 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 Arg);.}.SQLITE_P
801e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
801f0 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 e3OsSectorSize(s
80200 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
80210 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f {. int (*xSecto
80220 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 rSize)(sqlite3_f
80230 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74 ile*) = id->pMet
80240 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a hods->xSectorSiz
80250 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 78 53 65 e;. return (xSe
80260 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 74 ctorSize ? xSect
80270 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51 4c orSize(id) : SQL
80280 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 ITE_DEFAULT_SECT
80290 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49 OR_SIZE);.}.SQLI
802a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
802b0 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
802c0 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 aracteristics(sq
802d0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
802e0 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
802f0 65 74 68 6f 64 73 2d 3e 78 44 65 76 69 63 65 43 ethods->xDeviceC
80300 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 69 haracteristics(i
80310 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 d);.}../*.** The
80320 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 next group of r
80330 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 outines are conv
80340 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 enience wrappers
80350 20 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56 around the.** V
80360 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 FS methods..*/.S
80370 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
80380 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 t sqlite3OsOpen(
80390 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
803a0 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 pVfs, . const c
803b0 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73 har *zPath, . s
803c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
803d0 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 le, . int flags
803e0 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 , . int *pFlags
803f0 4f 75 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b Out.){. int rc;
80400 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f . DO_OS_MALLOC_
80410 54 45 53 54 28 30 29 3b 0a 20 20 2f 2a 20 30 78 TEST(0);. /* 0x
80420 37 66 31 66 20 69 73 20 61 20 6d 61 73 6b 20 6f 7f1f is a mask o
80430 66 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 f SQLITE_OPEN_ f
80440 6c 61 67 73 20 74 68 61 74 20 61 72 65 20 76 61 lags that are va
80450 6c 69 64 20 74 6f 20 62 65 20 70 61 73 73 65 64 lid to be passed
80460 0a 20 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 . ** down into
80470 74 68 65 20 56 46 53 20 6c 61 79 65 72 2e 20 20 the VFS layer.
80480 53 6f 6d 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e Some SQLITE_OPEN
80490 5f 20 66 6c 61 67 73 20 28 66 6f 72 20 65 78 61 _ flags (for exa
804a0 6d 70 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 mple,. ** SQLIT
804b0 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 E_OPEN_FULLMUTEX
804c0 20 6f 72 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f or SQLITE_OPEN_
804d0 53 48 41 52 45 44 43 41 43 48 45 29 20 61 72 65 SHAREDCACHE) are
804e0 20 62 6c 6f 63 6b 65 64 20 62 65 66 6f 72 65 0a blocked before.
804f0 20 20 2a 2a 20 72 65 61 63 68 69 6e 67 20 74 68 ** reaching th
80500 65 20 56 46 53 2e 20 2a 2f 0a 20 20 72 63 20 3d e VFS. */. rc =
80510 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 pVfs->xOpen(pVf
80520 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c s, zPath, pFile,
80530 20 66 6c 61 67 73 20 26 20 30 78 37 66 31 66 2c flags & 0x7f1f,
80540 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 20 20 61 pFlagsOut);. a
80550 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
80560 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 E_OK || pFile->p
80570 4d 65 74 68 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 Methods==0 );.
80580 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c return rc;.}.SQL
80590 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
805a0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
805b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
805c0 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a s, const char *z
805d0 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79 6e Path, int dirSyn
805e0 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 c){. return pVf
805f0 73 2d 3e 78 44 65 6c 65 74 65 28 70 56 66 73 2c s->xDelete(pVfs,
80600 20 7a 50 61 74 68 2c 20 64 69 72 53 79 6e 63 29 zPath, dirSync)
80610 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
80620 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
80630 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 Access(. sqlite
80640 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 3_vfs *pVfs, .
80650 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
80660 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c h, . int flags,
80670 20 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 . int *pResOut
80680 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c .){. DO_OS_MALL
80690 4f 43 5f 54 45 53 54 28 30 29 3b 0a 20 20 72 65 OC_TEST(0);. re
806a0 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65 turn pVfs->xAcce
806b0 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 ss(pVfs, zPath,
806c0 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b flags, pResOut);
806d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
806e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 E int sqlite3OsF
806f0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 ullPathname(. s
80700 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
80710 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 , . const char
80720 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e *zPath, . int n
80730 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 PathOut, . char
80740 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 *zPathOut.){.
80750 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 75 return pVfs->xFu
80760 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c llPathname(pVfs,
80770 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 74 zPath, nPathOut
80780 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 23 , zPathOut);.}.#
80790 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
807a0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
807b0 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 N.SQLITE_PRIVATE
807c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 void *sqlite3Os
807d0 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 DlOpen(sqlite3_v
807e0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 fs *pVfs, const
807f0 63 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 20 char *zPath){.
80800 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c return pVfs->xDl
80810 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 Open(pVfs, zPath
80820 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
80830 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
80840 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 OsDlError(sqlite
80850 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
80860 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 nByte, char *zB
80870 75 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d 3e ufOut){. pVfs->
80880 78 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e xDlError(pVfs, n
80890 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a Byte, zBufOut);.
808a0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
808b0 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f void (*sqlite3O
808c0 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 sDlSym(sqlite3_v
808d0 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a fs *pVfs, void *
808e0 70 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 pHdle, const cha
808f0 72 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b r *zSym))(void){
80900 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e . return pVfs->
80910 78 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48 64 xDlSym(pVfs, pHd
80920 6c 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51 4c le, zSym);.}.SQL
80930 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
80940 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 sqlite3OsDlClos
80950 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 e(sqlite3_vfs *p
80960 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 Vfs, void *pHand
80970 6c 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c le){. pVfs->xDl
80980 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61 6e Close(pVfs, pHan
80990 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f dle);.}.#endif /
809a0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f * SQLITE_OMIT_LO
809b0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a AD_EXTENSION */.
809c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
809d0 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 nt sqlite3OsRand
809e0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 omness(sqlite3_v
809f0 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 fs *pVfs, int nB
80a00 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f yte, char *zBufO
80a10 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 ut){. return pV
80a20 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 28 fs->xRandomness(
80a30 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 pVfs, nByte, zBu
80a40 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f fOut);.}.SQLITE_
80a50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
80a60 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 te3OsSleep(sqlit
80a70 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
80a80 74 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65 74 t nMicro){. ret
80a90 75 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 70 urn pVfs->xSleep
80aa0 28 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a (pVfs, nMicro);.
80ab0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
80ac0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 int sqlite3OsCu
80ad0 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 rrentTime(sqlite
80ae0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 3_vfs *pVfs, dou
80af0 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a ble *pTimeOut){.
80b00 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 return pVfs->x
80b10 43 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66 73 CurrentTime(pVfs
80b20 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a , pTimeOut);.}..
80b30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
80b40 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e nt sqlite3OsOpen
80b50 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 Malloc(. sqlite
80b60 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 3_vfs *pVfs, .
80b70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
80b80 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 e, . sqlite3_fi
80b90 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20 le **ppFile, .
80ba0 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74 int flags,. int
80bb0 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20 *pOutFlags.){.
80bc0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
80bd0 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 _NOMEM;. sqlite
80be0 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 3_file *pFile;.
80bf0 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 65 pFile = (sqlite
80c00 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 3_file *)sqlite3
80c10 4d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f Malloc(pVfs->szO
80c20 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 sFile);. if( pF
80c30 69 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ile ){. rc =
80c40 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 sqlite3OsOpen(pV
80c50 66 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65 fs, zFile, pFile
80c60 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 , flags, pOutFla
80c70 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 gs);. if( rc!
80c80 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
80c90 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
80ca0 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c (pFile);. }el
80cb0 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 6c se{. *ppFil
80cc0 65 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d e = pFile;. }
80cd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
80ce0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
80cf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
80d00 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 CloseFree(sqlite
80d10 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 3_file *pFile){.
80d20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
80d30 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
80d40 70 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20 pFile );. rc =
80d50 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
80d60 46 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 File);. sqlite3
80d70 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 _free(pFile);.
80d80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
80d90 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
80da0 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 n is a wrapper a
80db0 72 6f 75 6e 64 20 74 68 65 20 4f 53 20 73 70 65 round the OS spe
80dc0 63 69 66 69 63 20 69 6d 70 6c 65 6d 65 6e 74 61 cific implementa
80dd0 74 69 6f 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 tion of.** sqlit
80de0 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 54 68 e3_os_init(). Th
80df0 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 e purpose of the
80e00 20 77 72 61 70 70 65 72 20 69 73 20 74 6f 20 70 wrapper is to p
80e10 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61 62 rovide the.** ab
80e20 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 ility to simulat
80e30 65 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 e a malloc failu
80e40 72 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 re, so that the
80e50 68 61 6e 64 6c 69 6e 67 20 6f 66 20 61 6e 0a 2a handling of an.*
80e60 2a 20 65 72 72 6f 72 20 69 6e 20 73 71 6c 69 74 * error in sqlit
80e70 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 62 79 20 e3_os_init() by
80e80 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 73 the upper layers
80e90 20 63 61 6e 20 62 65 20 74 65 73 74 65 64 2e 0a can be tested..
80ea0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
80eb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 E int sqlite3OsI
80ec0 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 76 6f 69 nit(void){. voi
80ed0 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d d *p = sqlite3_m
80ee0 61 6c 6c 6f 63 28 31 30 29 3b 0a 20 20 69 66 28 alloc(10);. if(
80ef0 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 p==0 ) return S
80f00 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 QLITE_NOMEM;. s
80f10 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
80f20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
80f30 5f 6f 73 5f 69 6e 69 74 28 29 3b 0a 7d 0a 0a 2f _os_init();.}../
80f40 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 *.** The list of
80f50 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20 all registered
80f60 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 VFS implementati
80f70 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ons..*/.static s
80f80 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c qlite3_vfs * SQL
80f90 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20 ITE_WSD vfsList
80fa0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73 = 0;.#define vfs
80fb0 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69 List GLOBAL(sqli
80fc0 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69 te3_vfs *, vfsLi
80fd0 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 st)../*.** Locat
80fe0 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e e a VFS by name.
80ff0 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20 If no name is
81000 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65 given, simply re
81010 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73 turn the.** firs
81020 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73 t VFS on the lis
81030 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
81040 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 sqlite3_vfs *sq
81050 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 lite3_vfs_find(c
81060 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29 onst char *zVfs)
81070 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
81080 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 53 *pVfs = 0;.#if S
81090 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
810a0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
810b0 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a *mutex;.#endif.
810c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
810d0 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 MIT_AUTOINIT. i
810e0 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f nt rc = sqlite3_
810f0 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 initialize();.
81100 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
81110 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 0;.#endif.#if SQ
81120 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
81130 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 mutex = sqlite
81140 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
81150 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
81160 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a MASTER);.#endif.
81170 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
81180 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 enter(mutex);.
81190 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c 69 for(pVfs = vfsLi
811a0 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d 70 st; pVfs; pVfs=p
811b0 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Vfs->pNext){.
811c0 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 62 if( zVfs==0 ) b
811d0 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 74 reak;. if( st
811e0 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d rcmp(zVfs, pVfs-
811f0 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 >zName)==0 ) bre
81200 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ak;. }. sqlite
81210 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
81220 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 tex);. return p
81230 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e Vfs;.}../*.** Un
81240 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d 20 link a VFS from
81250 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a the linked list.
81260 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 */.static void v
81270 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 33 fsUnlink(sqlite3
81280 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 61 _vfs *pVfs){. a
81290 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
812a0 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 utex_held(sqlite
812b0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
812c0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
812d0 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 66 MASTER)) );. if
812e0 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 20 ( pVfs==0 ){.
812f0 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d /* No-op */. }
81300 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 else if( vfsList
81310 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 66 ==pVfs ){. vf
81320 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e sList = pVfs->pN
81330 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ext;. }else if(
81340 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 20 vfsList ){.
81350 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d sqlite3_vfs *p =
81360 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68 vfsList;. wh
81370 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26 ile( p->pNext &&
81380 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20 p->pNext!=pVfs
81390 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e ){. p = p->
813a0 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 pNext;. }.
813b0 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 if( p->pNext==p
813c0 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e Vfs ){. p->
813d0 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e pNext = pVfs->pN
813e0 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ext;. }. }.}
813f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 ../*.** Register
81400 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 20 a VFS with the
81410 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68 system. It is h
81420 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73 armless to regis
81430 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 ter the same.**
81440 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d VFS multiple tim
81450 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53 es. The new VFS
81460 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66 becomes the def
81470 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74 ault if makeDflt
81480 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a is.** true..*/.
81490 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
814a0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
814b0 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ter(sqlite3_vfs
814c0 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44 *pVfs, int makeD
814d0 66 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f flt){. sqlite3_
814e0 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30 mutex *mutex = 0
814f0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
81500 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 _OMIT_AUTOINIT.
81510 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
81520 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 3_initialize();.
81530 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
81540 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 6d n rc;.#endif. m
81550 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 utex = sqlite3Mu
81560 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
81570 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
81580 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f TER);. sqlite3_
81590 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
815a0 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 x);. vfsUnlink(
815b0 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b pVfs);. if( mak
815c0 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74 eDflt || vfsList
815d0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d ==0 ){. pVfs-
815e0 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 >pNext = vfsList
815f0 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 ;. vfsList =
81600 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 pVfs;. }else{.
81610 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d pVfs->pNext =
81620 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b vfsList->pNext;
81630 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e . vfsList->pN
81640 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a ext = pVfs;. }.
81650 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 74 assert(vfsList
81660 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
81670 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b ex_leave(mutex);
81680 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
81690 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e _OK;.}../*.** Un
816a0 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 73 register a VFS s
816b0 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20 o that it is no
816c0 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c longer accessibl
816d0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 e..*/.SQLITE_API
816e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 int sqlite3_vfs
816f0 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69 _unregister(sqli
81700 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a te3_vfs *pVfs){.
81710 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
81720 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f DSAFE. sqlite3_
81730 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73 mutex *mutex = s
81740 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
81750 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
81760 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65 ATIC_MASTER);.#e
81770 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d ndif. sqlite3_m
81780 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
81790 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 );. vfsUnlink(p
817a0 56 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f Vfs);. sqlite3_
817b0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
817c0 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c x);. return SQL
817d0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a ITE_OK;.}../****
817e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
817f0 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a f os.c *********
81800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
81830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
81840 20 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a file fault.c **
81850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
81870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
81880 20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2008 Jan 22.**.
81890 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
818a0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
818b0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
818c0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
818d0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
818e0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
818f0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
81900 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
81910 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
81920 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
81930 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
81940 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
81950 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
81960 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
81970 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
81980 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
81990 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
819a0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
819b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
819e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
819f0 2a 20 24 49 64 3a 20 66 61 75 6c 74 2e 63 2c 76 * $Id: fault.c,v
81a00 20 31 2e 31 31 20 32 30 30 38 2f 30 39 2f 30 32 1.11 2008/09/02
81a10 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 00:52:52 drh Ex
81a20 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 p $.*/../*.** Th
81a30 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
81a40 20 63 6f 64 65 20 74 6f 20 73 75 70 70 6f 72 74 code to support
81a50 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 the concept of
81a60 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c "benign" .** mal
81a70 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 28 77 68 loc failures (wh
81a80 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 en the xMalloc()
81a90 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 20 6d or xRealloc() m
81aa0 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20 ethod of the.**
81ab0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
81ac0 6f 64 73 20 73 74 72 75 63 74 75 72 65 20 66 61 ods structure fa
81ad0 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ils to allocate
81ae0 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 a block of memor
81af0 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 73 y.** and returns
81b00 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 0). .**.** Most
81b10 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
81b20 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e are non-benign.
81b30 20 41 66 74 65 72 20 74 68 65 79 20 6f 63 63 75 After they occu
81b40 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 61 62 61 r, SQLite.** aba
81b50 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 72 65 6e ndons the curren
81b60 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 t operation and
81b70 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 returns an error
81b80 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 0a 2a code (usually.*
81b90 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 * SQLITE_NOMEM)
81ba0 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48 6f 77 to the user. How
81bb0 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73 20 ever, sometimes
81bc0 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 6e a fault is not n
81bd0 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 66 61 ecessarily.** fa
81be0 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 tal. For example
81bf0 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 , if a malloc fa
81c00 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69 7a 69 ils while resizi
81c10 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 2c ng a hash table,
81c20 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 63 6f 6d this .** is com
81c30 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65 72 61 pletely recovera
81c40 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20 6e 6f ble simply by no
81c50 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74 20 74 t carrying out t
81c60 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65 20 0a he resize. The .
81c70 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 77 69 ** hash table wi
81c80 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 66 ll continue to f
81c90 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 unction normally
81ca0 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 20 66 . So a malloc f
81cb0 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 72 69 6e ailure .** durin
81cc0 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 72 g a hash table r
81cd0 65 73 69 7a 65 20 69 73 20 61 20 62 65 6e 69 67 esize is a benig
81ce0 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69 n fault..*/...#i
81cf0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
81d00 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 0a T_BUILTIN_TEST..
81d10 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61 72 /*.** Global var
81d20 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 iables..*/.typed
81d30 65 66 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e ef struct Benign
81d40 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 MallocHooks Beni
81d50 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73 gnMallocHooks;.s
81d60 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
81d70 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 struct BenignMa
81d80 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f llocHooks {. vo
81d90 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 id (*xBenignBegi
81da0 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 n)(void);. void
81db0 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 (*xBenignEnd)(v
81dc0 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 65 33 48 oid);.} sqlite3H
81dd0 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d 3b ooks = { 0, 0 };
81de0 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 48 6f 6f ../* The "wsdHoo
81df0 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 ks" macro will r
81e00 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 70 esolve to the ap
81e10 70 72 6f 70 72 69 61 74 65 20 42 65 6e 69 67 6e propriate Benign
81e20 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73 MallocHooks.** s
81e30 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 77 72 tructure. If wr
81e40 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 itable static da
81e50 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 ta is unsupporte
81e60 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c d on the target,
81e70 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c .** we have to l
81e80 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20 ocate the state
81e90 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 vector at run-ti
81ea0 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 me. In the more
81eb0 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 common.** case
81ec0 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 where writable s
81ed0 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75 tatic data is su
81ee0 70 70 6f 72 74 65 64 2c 20 77 73 64 48 6f 6f 6b pported, wsdHook
81ef0 73 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 s can refer dire
81f00 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 ctly.** to the "
81f10 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 20 73 74 sqlite3Hooks" st
81f20 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 ate vector decla
81f30 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 red above..*/.#i
81f40 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
81f50 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 _WSD.# define ws
81f60 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 20 42 dHooksInit \. B
81f70 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
81f80 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 42 65 *x = &GLOBAL(Be
81f90 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c nignMallocHooks,
81fa0 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 0a 23 20 sqlite3Hooks).#
81fb0 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20 define wsdHooks
81fc0 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 x[0].#else.# def
81fd0 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 ine wsdHooksInit
81fe0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f .# define wsdHoo
81ff0 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 0a ks sqlite3Hooks.
82000 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 #endif.../*.** R
82010 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f egister hooks to
82020 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 6c 69 74 call when sqlit
82030 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
82040 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c loc() and.** sql
82050 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
82060 6c 6f 63 28 29 20 61 72 65 20 63 61 6c 6c 65 64 loc() are called
82070 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a , respectively..
82080 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
82090 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 E void sqlite3Be
820a0 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 nignMallocHooks(
820b0 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 . void (*xBenig
820c0 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 2c 0a 20 nBegin)(void),.
820d0 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 void (*xBenignE
820e0 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a 20 20 77 nd)(void).){. w
820f0 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 77 sdHooksInit;. w
82100 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 sdHooks.xBenignB
82110 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 6e 42 65 egin = xBenignBe
82120 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e gin;. wsdHooks.
82130 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 78 42 65 xBenignEnd = xBe
82140 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a nignEnd;.}../*.*
82150 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65 33 45 * This (sqlite3E
82160 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 ndBenignMalloc()
82170 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53 ) is called by S
82180 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20 69 6e QLite code to in
82190 64 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 73 dicate that.** s
821a0 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 ubsequent malloc
821b0 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 62 65 failures are be
821c0 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74 6f 20 nign. A call to
821d0 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
821e0 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e 64 69 Malloc().** indi
821f0 63 61 74 65 73 20 74 68 61 74 20 73 75 62 73 65 cates that subse
82200 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 quent malloc fai
82210 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 lures are non-be
82220 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f nign..*/.SQLITE_
82230 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
82240 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
82250 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 alloc(void){. w
82260 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 sdHooksInit;. i
82270 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e f( wsdHooks.xBen
82280 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 ignBegin ){.
82290 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e wsdHooks.xBenign
822a0 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53 Begin();. }.}.S
822b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
822c0 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e id sqlite3EndBen
822d0 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b ignMalloc(void){
822e0 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b . wsdHooksInit;
822f0 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e . if( wsdHooks.
82300 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b 0a 20 20 xBenignEnd ){.
82310 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 wsdHooks.xBeni
82320 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a gnEnd();. }.}..
82330 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 6e #endif /* #ifn
82340 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
82350 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a BUILTIN_TEST */.
82360 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
82370 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 2e 63 20 End of fault.c
82380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
823a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
823b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
823c0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 30 Begin file mem0
823d0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
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 2f ***************/
82400 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f ./*.** 2008 Octo
82410 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 28.**.** The
82420 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
82430 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
82440 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
82450 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
82460 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
82470 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
82480 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
82490 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
824a0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
824b0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
824c0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
824d0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
824e0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
824f0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
82500 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
82510 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
82520 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
82530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82570 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
82580 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 file contains a
82590 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 61 6c no-op memory al
825a0 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 location drivers
825b0 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0a 2a 2a for use when.**
825c0 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c SQLITE_ZERO_MAL
825d0 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64 2e 20 LOC is defined.
825e0 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 The allocation
825f0 64 72 69 76 65 72 73 20 69 6d 70 6c 65 6d 65 6e drivers implemen
82600 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 6c 77 61 ted.** here alwa
82610 79 73 20 66 61 69 6c 2e 20 20 53 51 4c 69 74 65 ys fail. SQLite
82620 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 61 74 will not operat
82630 65 20 77 69 74 68 20 74 68 65 73 65 20 64 72 69 e with these dri
82640 76 65 72 73 2e 20 20 54 68 65 73 65 0a 2a 2a 20 vers. These.**
82650 61 72 65 20 6d 65 72 65 6c 79 20 70 6c 61 63 65 are merely place
82660 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 6c 20 64 holders. Real d
82670 72 69 76 65 72 73 20 6d 75 73 74 20 62 65 20 73 rivers must be s
82680 75 62 73 74 69 74 75 74 65 64 20 75 73 69 6e 67 ubstituted using
82690 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 .** sqlite3_conf
826a0 69 67 28 29 20 62 65 66 6f 72 65 20 53 51 4c 69 ig() before SQLi
826b0 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 74 65 2e te will operate.
826c0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 30 .**.** $Id: mem0
826d0 2e 63 2c 76 20 31 2e 31 20 32 30 30 38 2f 31 30 .c,v 1.1 2008/10
826e0 2f 32 38 20 31 38 3a 35 38 3a 32 30 20 64 72 68 /28 18:58:20 drh
826f0 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
82700 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
82710 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
82720 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 cator is the def
82730 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 ault. It is.**
82740 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 used when no oth
82750 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 er memory alloca
82760 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64 tor is specified
82770 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 using compile-t
82780 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a ime.** macros..*
82790 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
827a0 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a ZERO_MALLOC../*.
827b0 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e ** No-op version
827c0 73 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 s of all memory
827d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 allocation routi
827e0 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f nes.*/.static vo
827f0 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 id *sqlite3MemMa
82800 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b lloc(int nByte){
82810 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 return 0; }.sta
82820 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
82830 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 MemFree(void *pP
82840 72 69 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d rior){ return; }
82850 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 .static void *sq
82860 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 lite3MemRealloc(
82870 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e void *pPrior, in
82880 74 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e t nByte){ return
82890 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 0; }.static int
828a0 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 sqlite3MemSize(
828b0 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 void *pPrior){ r
828c0 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 eturn 0; }.stati
828d0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
828e0 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20 Roundup(int n){
828f0 72 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 return n; }.stat
82900 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
82910 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 mInit(void *NotU
82920 73 65 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c sed){ return SQL
82930 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
82940 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
82950 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e Shutdown(void *N
82960 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 3b otUsed){ return;
82970 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 }../*.** This r
82980 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e outine is the on
82990 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 ly routine in th
829a0 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 is file with ext
829b0 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a ernal linkage..*
829c0 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 *.** Populate th
829d0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f e low-level memo
829e0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 ry allocation fu
829f0 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 nction pointers
82a00 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f in.** sqlite3Glo
82a10 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 balConfig.m with
82a20 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 pointers to the
82a30 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 routines in thi
82a40 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 s file..*/.SQLIT
82a50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
82a60 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 qlite3MemSetDefa
82a70 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 ult(void){. sta
82a80 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
82a90 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 3_mem_methods de
82aa0 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b faultMethods = {
82ab0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
82ac0 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c Malloc,. sql
82ad0 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 ite3MemFree,.
82ae0 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c sqlite3MemReal
82af0 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 loc,. sqlite
82b00 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 3MemSize,. s
82b10 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 qlite3MemRoundup
82b20 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
82b30 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 mInit,. sqli
82b40 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a te3MemShutdown,.
82b50 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 0. };. sq
82b60 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c lite3_config(SQL
82b70 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f ITE_CONFIG_MALLO
82b80 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f C, &defaultMetho
82b90 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f ds);.}..#endif /
82ba0 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 * SQLITE_ZERO_MA
82bb0 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a LLOC */../******
82bc0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
82bd0 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem0.c *********
82be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82c00 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
82c10 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
82c20 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a ile mem1.c *****
82c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82c50 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
82c60 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 007 August 14.**
82c70 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
82c80 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
82c90 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
82ca0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
82cb0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
82cc0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
82cd0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
82ce0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
82cf0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
82d00 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
82d10 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
82d20 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
82d30 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
82d40 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
82d50 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
82d60 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
82d70 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
82d80 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
82d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
82dd0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
82de0 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 tains low-level
82df0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
82e00 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 n drivers for wh
82e10 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c en.** SQLite wil
82e20 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 l use the standa
82e30 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c rd C-library mal
82e40 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 loc/realloc/free
82e50 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f interface.** to
82e60 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f obtain the memo
82e70 72 79 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a ry it needs..**.
82e80 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
82e90 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 tains implementa
82ea0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 tions of the low
82eb0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
82ec0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 location.** rout
82ed0 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69 ines specified i
82ee0 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 n the sqlite3_me
82ef0 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 m_methods object
82f00 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d ..**.** $Id: mem
82f10 31 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 2f 1.c,v 1.30 2009/
82f20 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 33 20 64 03/23 04:33:33 d
82f30 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 anielk1977 Exp $
82f40 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .*/../*.** This
82f50 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d version of the m
82f60 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
82f70 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 is the default.
82f80 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 It is.** used w
82f90 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d hen no other mem
82fa0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 ory allocator is
82fb0 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 specified using
82fc0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a compile-time.**
82fd0 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 macros..*/.#ifd
82fe0 65 66 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d ef SQLITE_SYSTEM
82ff0 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c _MALLOC../*.** L
83000 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 ike malloc(), bu
83010 74 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73 t remember the s
83020 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 ize of the alloc
83030 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 ation.** so that
83040 20 77 65 20 63 61 6e 20 66 69 6e 64 20 69 74 20 we can find it
83050 6c 61 74 65 72 20 75 73 69 6e 67 20 73 71 6c 69 later using sqli
83060 74 65 33 4d 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a te3MemSize()..**
83070 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 .** For this low
83080 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 -level routine,
83090 77 65 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 we are guarantee
830a0 64 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 d that nByte>0 b
830b0 65 63 61 75 73 65 0a 2a 2a 20 63 61 73 65 73 20 ecause.** cases
830c0 6f 66 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c of nByte<=0 will
830d0 20 62 65 20 69 6e 74 65 72 63 65 70 74 65 64 20 be intercepted
830e0 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68 20 62 and dealt with b
830f0 79 20 68 69 67 68 65 72 20 6c 65 76 65 6c 0a 2a y higher level.*
83100 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 * routines..*/.s
83110 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 tatic void *sqli
83120 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 te3MemMalloc(int
83130 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 nByte){. sqlit
83140 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 e3_int64 *p;. a
83150 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 ssert( nByte>0 )
83160 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e ;. nByte = ROUN
83170 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d D8(nByte);. p =
83180 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2b 38 malloc( nByte+8
83190 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 );. if( p ){.
831a0 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b p[0] = nByte;
831b0 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 . p++;. }.
831c0 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 70 return (void *)p
831d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 ;.}../*.** Like
831e0 66 72 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73 free() but works
831f0 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 for allocations
83200 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
83210 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 qlite3MemMalloc(
83220 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d ).** or sqlite3M
83230 65 6d 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a emRealloc()..**.
83240 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d ** For this low-
83250 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 level routine, w
83260 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 e already know t
83270 68 61 74 20 70 50 72 69 6f 72 21 3d 30 20 73 69 hat pPrior!=0 si
83280 6e 63 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 nce.** cases whe
83290 72 65 20 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c re pPrior==0 wil
832a0 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 l have been inte
832b0 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 cepted and dealt
832c0 20 77 69 74 68 0a 2a 2a 20 62 79 20 68 69 67 68 with.** by high
832d0 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 er-level routine
832e0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
832f0 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 d sqlite3MemFree
83300 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a (void *pPrior){.
83310 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
83320 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e *p = (sqlite3_in
83330 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 t64*)pPrior;. a
83340 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 ssert( pPrior!=0
83350 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 66 72 65 );. p--;. fre
83360 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c e(p);.}../*.** L
83370 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20 ike realloc().
83380 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 Resize an alloca
83390 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 tion previously
833a0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
833b0 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f sqlite3MemMallo
833c0 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 c()..**.** For t
833d0 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e his low-level in
833e0 74 65 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77 terface, we know
833f0 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d 30 2e that pPrior!=0.
83400 20 20 43 61 73 65 73 20 77 68 65 72 65 0a 2a 2a Cases where.**
83410 20 70 50 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65 pPrior==0 while
83420 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 have been inter
83430 63 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72 cepted by higher
83440 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 20 61 -level routine a
83450 6e 64 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64 nd.** redirected
83460 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69 to xMalloc. Si
83470 6d 69 6c 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77 milarly, we know
83480 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65 that nByte>0 be
83490 63 61 75 73 65 73 0a 2a 2a 20 63 61 73 65 73 20 causes.** cases
834a0 77 68 65 72 65 20 6e 42 79 74 65 3c 3d 30 20 77 where nByte<=0 w
834b0 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e ill have been in
834c0 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69 67 tercepted by hig
834d0 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 her-level.** rou
834e0 74 69 6e 65 73 20 61 6e 64 20 72 65 64 69 72 65 tines and redire
834f0 63 74 65 64 20 74 6f 20 78 46 72 65 65 2e 0a 2a cted to xFree..*
83500 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 /.static void *s
83510 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
83520 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 (void *pPrior, i
83530 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c nt nByte){. sql
83540 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 ite3_int64 *p =
83550 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
83560 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 pPrior;. assert
83570 28 20 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e ( pPrior!=0 && n
83580 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 Byte>0 );. nByt
83590 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 e = ROUND8(nByte
835a0 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 );. p = (sqlite
835b0 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
835c0 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 72 65 . p--;. p = re
835d0 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 74 65 2b 38 alloc(p, nByte+8
835e0 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 );. if( p ){.
835f0 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b p[0] = nByte;
83600 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 . p++;. }.
83610 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b return (void*)p;
83620 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 .}../*.** Report
83630 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 the allocated s
83640 69 7a 65 20 6f 66 20 61 20 70 72 69 6f 72 20 72 ize of a prior r
83650 65 74 75 72 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c eturn from xMall
83660 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 52 65 61 6c oc().** or xReal
83670 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
83680 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 int sqlite3MemS
83690 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ize(void *pPrior
836a0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 ){. sqlite3_int
836b0 36 34 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 64 *p;. if( pPr
836c0 69 6f 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ior==0 ) return
836d0 30 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 0;. p = (sqlite
836e0 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
836f0 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e . p--;. return
83700 20 28 69 6e 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f (int)p[0];.}../
83710 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 *.** Round up a
83720 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 request size to
83730 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 the next valid a
83740 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a llocation size..
83750 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
83760 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 lite3MemRoundup(
83770 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e int n){. return
83780 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f ROUND8(n);.}../
83790 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
837a0 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a this module..*/.
837b0 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
837c0 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a e3MemInit(void *
837d0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 NotUsed){. UNUS
837e0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
837f0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 Used);. return
83800 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
83810 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 .** Deinitialize
83820 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f this module..*/
83830 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
83840 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 ite3MemShutdown(
83850 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
83860 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
83870 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
83880 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eturn;.}../*.**
83890 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
838a0 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 the only routine
838b0 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 in this file wi
838c0 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b th external link
838d0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c age..**.** Popul
838e0 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 ate the low-leve
838f0 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
83900 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 ion function poi
83910 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 nters in.** sqli
83920 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
83930 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 m with pointers
83940 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 to the routines
83950 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f in this file..*/
83960 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
83970 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 void sqlite3MemS
83980 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b etDefault(void){
83990 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
839a0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
839b0 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f ods defaultMetho
839c0 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 ds = {. sqli
839d0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 te3MemMalloc,.
839e0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 sqlite3MemFre
839f0 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d e,. sqlite3M
83a00 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 emRealloc,.
83a10 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a sqlite3MemSize,.
83a20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 sqlite3MemR
83a30 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c oundup,. sql
83a40 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 ite3MemInit,.
83a50 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 sqlite3MemShut
83a60 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d down,. 0. }
83a70 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ;. sqlite3_conf
83a80 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
83a90 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c _MALLOC, &defaul
83aa0 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 tMethods);.}..#e
83ab0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53 ndif /* SQLITE_S
83ac0 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a YSTEM_MALLOC */.
83ad0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
83ae0 20 45 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a End of mem1.c *
83af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
83b20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
83b30 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32 Begin file mem2
83b40 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
83b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
83b70 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
83b80 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 15.**.** The
83b90 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
83ba0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
83bb0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
83bc0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
83bd0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
83be0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
83bf0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
83c00 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
83c10 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
83c20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
83c30 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
83c40 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
83c50 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
83c60 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
83c70 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
83c80 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
83c90 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
83ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83ce0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
83cf0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f file contains lo
83d00 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
83d10 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 llocation driver
83d20 73 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 s for when.** SQ
83d30 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 Lite will use th
83d40 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 e standard C-lib
83d50 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c rary malloc/real
83d60 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 loc/free interfa
83d70 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 ce.** to obtain
83d80 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 the memory it ne
83d90 65 64 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67 eds while adding
83da0 20 6c 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f lots of additio
83db0 6e 61 6c 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a nal debugging.**
83dc0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 information to
83dd0 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 each allocation
83de0 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70 in order to help
83df0 20 64 65 74 65 63 74 20 61 6e 64 20 66 69 78 20 detect and fix
83e00 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 memory.** leaks
83e10 61 6e 64 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 and memory usage
83e20 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 errors..**.** T
83e30 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
83e40 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
83e50 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76 s of the low-lev
83e60 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
83e70 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
83e80 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
83e90 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 e sqlite3_mem_me
83ea0 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a thods object..**
83eb0 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 32 2e 63 2c .** $Id: mem2.c,
83ec0 76 20 31 2e 34 35 20 32 30 30 39 2f 30 33 2f 32 v 1.45 2009/03/2
83ed0 33 20 30 34 3a 33 33 3a 33 33 20 64 61 6e 69 65 3 04:33:33 danie
83ee0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
83ef0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 ./*.** This vers
83f00 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
83f10 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 y allocator is u
83f20 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a sed only if the.
83f30 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 ** SQLITE_MEMDEB
83f40 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 UG macro is defi
83f50 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ned.*/.#ifdef SQ
83f60 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f LITE_MEMDEBUG../
83f70 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61 *.** The backtra
83f80 63 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 ce functionality
83f90 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
83fa0 6c 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f le with GLIBC.*/
83fb0 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f .#ifdef __GLIBC_
83fc0 5f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62 _. extern int b
83fd0 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c acktrace(void**,
83fe0 69 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 int);. extern v
83ff0 6f 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79 oid backtrace_sy
84000 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f mbols_fd(void*co
84010 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 nst*,int,int);.#
84020 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61 else.# define ba
84030 63 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a 23 cktrace(A,B) 1.#
84040 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 define backtrac
84050 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 e_symbols_fd(A,B
84060 2c 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ,C).#endif../*.*
84070 2a 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c * Each memory al
84080 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c location looks l
84090 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
840a0 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
840b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
840c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
840d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
840e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 ---------.** |
840f0 54 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 Title | backtra
84100 63 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d ce pointers | M
84110 65 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c emBlockHdr | al
84120 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 location | EndG
84130 75 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d uard |.** -----
84140 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84150 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84160 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84170 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84180 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 ---.**.** The ap
84190 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 plication code s
841a0 65 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 ees only a point
841b0 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 er to the alloca
841c0 74 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a tion. We have.*
841d0 2a 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f * to back up fro
841e0 6d 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e m the allocation
841f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 pointer to find
84200 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 the MemBlockHdr
84210 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f . The.** MemBlo
84220 63 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74 ckHdr tells us t
84230 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 he size of the a
84240 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 llocation and th
84250 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 e number of.** b
84260 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 acktrace pointer
84270 73 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c 73 s. There is als
84280 6f 20 61 20 67 75 61 72 64 20 77 6f 72 64 20 61 o a guard word a
84290 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
842a0 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e .** MemBlockHdr.
842b0 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c .*/.struct MemBl
842c0 6f 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69 ockHdr {. i64 i
842d0 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
842e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
842f0 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61 * Size of this a
84300 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 llocation */. s
84310 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 truct MemBlockHd
84320 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 r *pNext, *pPrev
84330 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 ; /* Linked lis
84340 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 t of all unfreed
84350 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 memory */. cha
84360 72 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20 r nBacktrace;
84370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
84380 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 /* Number of ba
84390 63 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73 cktraces on this
843a0 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 alloc */. char
843b0 20 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 nBacktraceSlots
843c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
843d0 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63 /* Available bac
843e0 6b 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a ktrace slots */.
843f0 20 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 short nTitle;
84400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
84410 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
84420 66 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 f title; include
84430 73 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 s '\0' */. int
84440 69 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20 iForeGuard;
84450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
84460 2f 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f /* Guard word fo
84470 72 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a r sanity */.};..
84480 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 /*.** Guard word
84490 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 s.*/.#define FOR
844a0 45 47 55 41 52 44 20 30 78 38 30 46 35 45 31 35 EGUARD 0x80F5E15
844b0 33 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 55 3.#define REARGU
844c0 41 52 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a ARD 0xE4676B53..
844d0 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /*.** Number of
844e0 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 malloc size incr
844f0 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e ements to track.
84500 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 .*/.#define NCSI
84510 5a 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 ZE 1000../*.**
84520 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 All of the stati
84530 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 c variables used
84540 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 by this module
84550 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a are collected.**
84560 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 into a single s
84570 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 tructure named "
84580 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74 mem". This is t
84590 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 o keep the.** st
845a0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f atic variables o
845b0 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 rganized and to
845c0 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 reduce namespace
845d0 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 pollution.** wh
845e0 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 en this module i
845f0 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 s combined with
84600 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 other in the ama
84610 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 lgamation..*/.st
84620 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 atic struct {.
84630 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 . /*. ** Mutex
84640 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 to control acce
84650 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 ss to the memory
84660 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
84670 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 ystem.. */. sq
84680 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
84690 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 ex;.. /*. ** H
846a0 65 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 20 ead and tail of
846b0 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 a linked list of
846c0 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 all outstanding
846d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a allocations. *
846e0 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c /. struct MemBl
846f0 6f 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b 0a ockHdr *pFirst;.
84700 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
84710 6b 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a kHdr *pLast;. .
84720 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75 /*. ** The nu
84730 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f mber of levels o
84740 66 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20 73 f backtrace to s
84750 61 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 ave in new alloc
84760 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 ations.. */. i
84770 6e 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 nt nBacktrace;.
84780 20 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 void (*xBacktra
84790 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f ce)(int, int, vo
847a0 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 id **);.. /*.
847b0 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f ** Title text to
847c0 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 insert in front
847d0 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 of each block.
847e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 */. int nTitle
847f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 ; /* Byte
84800 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 s of zTitle to s
84810 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 ave. Includes '
84820 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20 \0' and padding
84830 2a 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65 */. char zTitle
84840 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 [100]; /* The t
84850 69 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 itle text */..
84860 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 /* . ** sqlite3
84870 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 MallocDisallow()
84880 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 increments the
84890 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65 following counte
848a0 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d r.. ** sqlite3M
848b0 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63 allocAllow() dec
848c0 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f rements it.. */
848d0 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b . int disallow;
848e0 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 /* Do not allow
848f0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
84900 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a on */.. /*. **
84910 20 47 61 74 68 65 72 20 73 74 61 74 69 73 74 69 Gather statisti
84920 63 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 20 cs on the sizes
84930 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 of memory alloca
84940 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c tions.. ** nAll
84950 6f 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 6d oc[i] is the num
84960 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f ber of allocatio
84970 6e 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69 2a n attempts of i*
84980 38 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 8. ** bytes. i
84990 3d 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20 ==NCSIZE is the
849a0 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 number of alloca
849b0 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f tion attempts fo
849c0 72 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 r. ** sizes mor
849d0 65 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 e than NCSIZE*8
849e0 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e bytes.. */. in
849f0 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d t nAlloc[NCSIZE]
84a00 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 ; /* Total
84a10 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 number of alloca
84a20 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e tions */. int n
84a30 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b Current[NCSIZE];
84a40 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e /* Current n
84a50 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
84a60 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 ions */. int mx
84a70 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b Current[NCSIZE];
84a80 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 /* Highwater
84a90 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e mark for nCurren
84aa0 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f t */..} mem;.../
84ab0 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f *.** Adjust memo
84ac0 72 79 20 75 73 61 67 65 20 73 74 61 74 69 73 74 ry usage statist
84ad0 69 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ics.*/.static vo
84ae0 69 64 20 61 64 6a 75 73 74 53 74 61 74 73 28 69 id adjustStats(i
84af0 6e 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e nt iSize, int in
84b00 63 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 crement){. int
84b10 69 20 3d 20 52 4f 55 4e 44 38 28 69 53 69 7a 65 i = ROUND8(iSize
84b20 29 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53 )/8;. if( i>NCS
84b30 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d IZE-1 ){. i =
84b40 20 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d NCSIZE - 1;. }
84b50 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74 . if( increment
84b60 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 >0 ){. mem.nA
84b70 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d lloc[i]++;. m
84b80 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b em.nCurrent[i]++
84b90 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 ;. if( mem.nC
84ba0 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 urrent[i]>mem.mx
84bb0 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 Current[i] ){.
84bc0 20 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e mem.mxCurren
84bd0 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 t[i] = mem.nCurr
84be0 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 ent[i];. }.
84bf0 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e }else{. mem.n
84c00 43 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 Current[i]--;.
84c10 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43 assert( mem.nC
84c20 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a urrent[i]>=0 );.
84c30 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 }.}../*.** Giv
84c40 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e en an allocation
84c50 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c , find the MemBl
84c60 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20 ockHdr for that
84c70 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a allocation..**.*
84c80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
84c90 68 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 hecks the guards
84ca0 20 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f at either end o
84cb0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
84cc0 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 and.** if they
84cd0 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74 are incorrect it
84ce0 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 asserts..*/.sta
84cf0 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c tic struct MemBl
84d00 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d ockHdr *sqlite3M
84d10 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 76 emsysGetHeader(v
84d20 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e oid *pAllocation
84d30 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 ){. struct MemB
84d40 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e lockHdr *p;. in
84d50 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70 t *pInt;. u8 *p
84d60 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 U8;. int nReser
84d70 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75 ve;.. p = (stru
84d80 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 ct MemBlockHdr*)
84d90 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 pAllocation;. p
84da0 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d --;. assert( p-
84db0 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 6e >iForeGuard==(in
84dc0 74 29 46 4f 52 45 47 55 41 52 44 20 29 3b 0a 20 t)FOREGUARD );.
84dd0 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 4e nReserve = ROUN
84de0 44 38 28 70 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 D8(p->iSize);.
84df0 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70 41 6c pInt = (int*)pAl
84e00 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 55 38 20 location;. pU8
84e10 3d 20 28 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69 = (u8*)pAllocati
84e20 6f 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 on;. assert( pI
84e30 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 nt[nReserve/size
84e40 6f 66 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52 of(int)]==(int)R
84e50 45 41 52 47 55 41 52 44 20 29 3b 0a 20 20 2f 2a EARGUARD );. /*
84e60 20 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e 79 This checks any
84e70 20 6f 66 20 74 68 65 20 22 65 78 74 72 61 22 20 of the "extra"
84e80 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 bytes allocated
84e90 64 75 65 0a 20 20 2a 2a 20 74 6f 20 72 6f 75 6e due. ** to roun
84ea0 64 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38 20 ding up to an 8
84eb0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74 6f byte boundary to
84ec0 20 65 6e 73 75 72 65 20 0a 20 20 2a 2a 20 74 68 ensure . ** th
84ed0 65 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e 20 ey haven't been
84ee0 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a overwritten.. *
84ef0 2f 0a 20 20 77 68 69 6c 65 28 20 6e 52 65 73 65 /. while( nRese
84f00 72 76 65 2d 2d 20 3e 20 70 2d 3e 69 53 69 7a 65 rve-- > p->iSize
84f10 20 29 20 61 73 73 65 72 74 28 20 70 55 38 5b 6e ) assert( pU8[n
84f20 52 65 73 65 72 76 65 5d 3d 3d 30 78 36 35 20 29 Reserve]==0x65 )
84f30 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a ;. return p;.}.
84f40 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
84f50 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
84f60 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f s currently allo
84f70 63 61 74 65 64 20 61 74 20 61 64 64 72 65 73 73 cated at address
84f80 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e p..*/.static in
84f90 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 t sqlite3MemSize
84fa0 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 72 (void *p){. str
84fb0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
84fc0 2a 70 48 64 72 3b 0a 20 20 69 66 28 20 21 70 20 *pHdr;. if( !p
84fd0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
84fe0 0a 20 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 71 . }. pHdr = sq
84ff0 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 lite3MemsysGetHe
85000 61 64 65 72 28 70 29 3b 0a 20 20 72 65 74 75 72 ader(p);. retur
85010 6e 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d n pHdr->iSize;.}
85020 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
85030 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
85040 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
85050 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e em..*/.static in
85060 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 t sqlite3MemInit
85070 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b (void *NotUsed){
85080 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
85090 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
850a0 61 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 28 assert( (sizeof(
850b0 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
850c0 64 72 29 26 37 29 20 3d 3d 20 30 20 29 3b 0a 20 dr)&7) == 0 );.
850d0 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f if( !sqlite3Glo
850e0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
850f0 61 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 at ){. /* If
85100 6d 65 6d 6f 72 79 20 73 74 61 74 75 73 20 69 73 memory status is
85110 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 enabled, then t
85120 68 65 20 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 70 he malloc.c wrap
85130 70 65 72 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 per will already
85140 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 20 74 68 65 . ** hold the
85150 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 STATIC_MEM mute
85160 78 20 77 68 65 6e 20 74 68 65 20 72 6f 75 74 69 x when the routi
85170 6e 65 73 20 68 65 72 65 20 61 72 65 20 69 6e 76 nes here are inv
85180 6f 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d oked. */. mem
85190 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 .mutex = sqlite3
851a0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 MutexAlloc(SQLIT
851b0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
851c0 45 4d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 EM);. }. retur
851d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
851e0 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 /*.** Deinitiali
851f0 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c ze the memory al
85200 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
85210 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f em..*/.static vo
85220 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 id sqlite3MemShu
85230 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
85240 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
85250 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
85260 29 3b 0a 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d );. mem.mutex =
85270 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 0;.}../*.** Rou
85280 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 nd up a request
85290 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 size to the next
852a0 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f valid allocatio
852b0 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 n size..*/.stati
852c0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
852d0 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a Roundup(int n){.
852e0 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 return ROUND8(
852f0 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c n);.}../*.** All
85300 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 ocate nByte byte
85310 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a s of memory..*/.
85320 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c static void *sql
85330 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e ite3MemMalloc(in
85340 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 t nByte){. stru
85350 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
85360 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 pHdr;. void **p
85370 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 Bt;. char *z;.
85380 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f int *pInt;. vo
85390 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 id *p = 0;. int
853a0 20 74 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e totalSize;. in
853b0 74 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71 t nReserve;. sq
853c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
853d0 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 r(mem.mutex);.
853e0 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 assert( mem.disa
853f0 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65 llow==0 );. nRe
85400 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 6e serve = ROUND8(n
85410 42 79 74 65 29 3b 0a 20 20 74 6f 74 61 6c 53 69 Byte);. totalSi
85420 7a 65 20 3d 20 6e 52 65 73 65 72 76 65 20 2b 20 ze = nReserve +
85430 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 20 sizeof(*pHdr) +
85440 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20 sizeof(int) +.
85450 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d mem
85460 2e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 .nBacktrace*size
85470 6f 66 28 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e of(void*) + mem.
85480 6e 54 69 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61 nTitle;. p = ma
85490 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b lloc(totalSize);
854a0 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
854b0 7a 20 3d 20 70 3b 0a 20 20 20 20 70 42 74 20 3d z = p;. pBt =
854c0 20 28 76 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e (void**)&z[mem.
854d0 6e 54 69 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64 nTitle];. pHd
854e0 72 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 r = (struct MemB
854f0 6c 6f 63 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65 lockHdr*)&pBt[me
85500 6d 2e 6e 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20 m.nBacktrace];.
85510 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d pHdr->pNext =
85520 20 30 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 0;. pHdr->pP
85530 72 65 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b rev = mem.pLast;
85540 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61 . if( mem.pLa
85550 73 74 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e st ){. mem.
85560 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 pLast->pNext = p
85570 48 64 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a Hdr;. }else{.
85580 20 20 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 mem.pFirst
85590 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20 = pHdr;. }.
855a0 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 mem.pLast = p
855b0 48 64 72 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69 Hdr;. pHdr->i
855c0 46 6f 72 65 47 75 61 72 64 20 3d 20 46 4f 52 45 ForeGuard = FORE
855d0 47 55 41 52 44 3b 0a 20 20 20 20 70 48 64 72 2d GUARD;. pHdr-
855e0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
855f0 20 3d 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 = mem.nBacktrac
85600 65 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 54 69 e;. pHdr->nTi
85610 74 6c 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65 tle = mem.nTitle
85620 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42 ;. if( mem.nB
85630 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20 acktrace ){.
85640 20 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34 30 void *aAddr[40
85650 5d 3b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e ];. pHdr->n
85660 42 61 63 6b 74 72 61 63 65 20 3d 20 62 61 63 6b Backtrace = back
85670 74 72 61 63 65 28 61 41 64 64 72 2c 20 6d 65 6d trace(aAddr, mem
85680 2e 6e 42 61 63 6b 74 72 61 63 65 2b 31 29 2d 31 .nBacktrace+1)-1
85690 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 ;. memcpy(p
856a0 42 74 2c 20 26 61 41 64 64 72 5b 31 5d 2c 20 70 Bt, &aAddr[1], p
856b0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a Hdr->nBacktrace*
856c0 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a sizeof(void*));.
856d0 20 20 20 20 20 20 61 73 73 65 72 74 28 70 42 74 assert(pBt
856e0 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 [0]);. if(
856f0 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 29 mem.xBacktrace )
85700 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78 42 {. mem.xB
85710 61 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c 20 acktrace(nByte,
85720 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
85730 2d 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b 0a -1, &aAddr[1]);.
85740 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
85750 65 7b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e e{. pHdr->n
85760 42 61 63 6b 74 72 61 63 65 20 3d 20 30 3b 0a 20 Backtrace = 0;.
85770 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d }. if( mem
85780 2e 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20 20 .nTitle ){.
85790 20 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e 7a memcpy(z, mem.z
857a0 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74 6c Title, mem.nTitl
857b0 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 48 e);. }. pH
857c0 64 72 2d 3e 69 53 69 7a 65 20 3d 20 6e 42 79 74 dr->iSize = nByt
857d0 65 3b 0a 20 20 20 20 61 64 6a 75 73 74 53 74 61 e;. adjustSta
857e0 74 73 28 6e 42 79 74 65 2c 20 2b 31 29 3b 0a 20 ts(nByte, +1);.
857f0 20 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 pInt = (int*)
85800 26 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20 70 49 &pHdr[1];. pI
85810 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65 nt[nReserve/size
85820 6f 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52 47 of(int)] = REARG
85830 55 41 52 44 3b 0a 20 20 20 20 6d 65 6d 73 65 74 UARD;. memset
85840 28 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52 65 (pInt, 0x65, nRe
85850 73 65 72 76 65 29 3b 0a 20 20 20 20 70 20 3d 20 serve);. p =
85860 28 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 7d (void*)pInt;. }
85870 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
85880 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 _leave(mem.mutex
85890 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 0a );. return p; .
858a0 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 }../*.** Free me
858b0 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 mory..*/.static
858c0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 void sqlite3MemF
858d0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ree(void *pPrior
858e0 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 ){. struct MemB
858f0 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 lockHdr *pHdr;.
85900 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 void **pBt;. c
85910 68 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74 har *z;. assert
85920 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
85930 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 7c onfig.bMemstat |
85940 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 29 | mem.mutex!=0 )
85950 3b 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74 ;. pHdr = sqlit
85960 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 e3MemsysGetHeade
85970 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 42 74 r(pPrior);. pBt
85980 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b = (void**)pHdr;
85990 0a 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e . pBt -= pHdr->
859a0 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b nBacktraceSlots;
859b0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
859c0 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78 _enter(mem.mutex
859d0 29 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 );. if( pHdr->p
859e0 50 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73 65 Prev ){. asse
859f0 72 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76 2d rt( pHdr->pPrev-
85a00 3e 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b 0a >pNext==pHdr );.
85a10 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 2d pHdr->pPrev-
85a20 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e 70 >pNext = pHdr->p
85a30 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Next;. }else{.
85a40 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 assert( mem.p
85a50 46 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 First==pHdr );.
85a60 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 mem.pFirst =
85a70 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d pHdr->pNext;. }
85a80 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e 65 . if( pHdr->pNe
85a90 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 xt ){. assert
85aa0 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 ( pHdr->pNext->p
85ab0 50 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20 20 Prev==pHdr );.
85ac0 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70 pHdr->pNext->p
85ad0 50 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 72 Prev = pHdr->pPr
85ae0 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ev;. }else{.
85af0 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c 61 assert( mem.pLa
85b00 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20 st==pHdr );.
85b10 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 mem.pLast = pHdr
85b20 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 7a ->pPrev;. }. z
85b30 20 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a 20 = (char*)pBt;.
85b40 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69 74 z -= pHdr->nTit
85b50 6c 65 3b 0a 20 20 61 64 6a 75 73 74 53 74 61 74 le;. adjustStat
85b60 73 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 2d s(pHdr->iSize, -
85b70 31 29 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c 20 1);. memset(z,
85b80 30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f 69 0x2b, sizeof(voi
85b90 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b 74 d*)*pHdr->nBackt
85ba0 72 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a 65 raceSlots + size
85bb0 6f 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20 20 of(*pHdr) +.
85bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 48 pH
85bd0 64 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a 65 dr->iSize + size
85be0 6f 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d 3e of(int) + pHdr->
85bf0 6e 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65 28 nTitle);. free(
85c00 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 z);. sqlite3_mu
85c10 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 tex_leave(mem.mu
85c20 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a tex); .}../*.**
85c30 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 Change the size
85c40 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 of an existing
85c50 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
85c60 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 n..**.** For thi
85c70 73 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70 6c s debugging impl
85c80 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20 2a ementation, we *
85c90 61 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20 63 always* make a c
85ca0 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c opy of the.** al
85cb0 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61 20 location into a
85cc0 6e 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65 6d new place in mem
85cd0 6f 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77 61 ory. In this wa
85ce0 79 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 69 y, if the .** hi
85cf0 67 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 20 gher level code
85d00 69 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72 is using pointer
85d10 20 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f to the old allo
85d20 63 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a 2a cation, it is .*
85d30 2a 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65 * much more like
85d40 6c 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 20 ly to break and
85d50 77 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 65 we are much more
85d60 20 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 0a liking to find.
85d70 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f ** the error..*/
85d80 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 .static void *sq
85d90 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 lite3MemRealloc(
85da0 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e void *pPrior, in
85db0 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75 t nByte){. stru
85dc0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
85dd0 70 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69 64 20 pOldHdr;. void
85de0 2a 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74 28 *pNew;. assert(
85df0 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 mem.disallow==0
85e00 20 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20 );. pOldHdr =
85e10 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 sqlite3MemsysGet
85e20 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a Header(pPrior);.
85e30 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 pNew = sqlite3
85e40 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 MemMalloc(nByte)
85e50 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a ;. if( pNew ){.
85e60 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c memcpy(pNew,
85e70 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70 pPrior, nByte<p
85e80 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20 OldHdr->iSize ?
85e90 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d nByte : pOldHdr-
85ea0 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 >iSize);. if(
85eb0 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e nByte>pOldHdr->
85ec0 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6d iSize ){. m
85ed0 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 emset(&((char*)p
85ee0 4e 65 77 29 5b 70 4f 6c 64 48 64 72 2d 3e 69 53 New)[pOldHdr->iS
85ef0 69 7a 65 5d 2c 20 30 78 32 62 2c 20 6e 42 79 74 ize], 0x2b, nByt
85f00 65 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69 e - pOldHdr->iSi
85f10 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ze);. }. s
85f20 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 70 50 qlite3MemFree(pP
85f30 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 rior);. }. ret
85f40 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a urn pNew;.}../*.
85f50 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 ** Populate the
85f60 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
85f70 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 allocation func
85f80 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e tion pointers in
85f90 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 .** sqlite3Globa
85fa0 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 lConfig.m with p
85fb0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 ointers to the r
85fc0 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 outines in this
85fd0 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f file..*/.SQLITE_
85fe0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
85ff0 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c ite3MemSetDefaul
86000 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 t(void){. stati
86010 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
86020 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 mem_methods defa
86030 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 ultMethods = {.
86040 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 sqlite3MemMa
86050 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 lloc,. sqlit
86060 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 e3MemFree,.
86070 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f sqlite3MemReallo
86080 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d c,. sqlite3M
86090 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c emSize,. sql
860a0 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a ite3MemRoundup,.
860b0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 sqlite3MemI
860c0 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 nit,. sqlite
860d0 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 3MemShutdown,.
860e0 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 0. };. sqli
860f0 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
86100 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c E_CONFIG_MALLOC,
86110 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 &defaultMethods
86120 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 );.}../*.** Set
86130 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 61 the number of ba
86140 63 6b 74 72 61 63 65 20 6c 65 76 65 6c 73 20 6b cktrace levels k
86150 65 70 74 20 66 6f 72 20 65 61 63 68 20 61 6c 6c ept for each all
86160 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61 ocation..** A va
86170 6c 75 65 20 6f 66 20 7a 65 72 6f 20 74 75 72 6e lue of zero turn
86180 73 20 6f 66 66 20 62 61 63 6b 74 72 61 63 69 6e s off backtracin
86190 67 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 69 g. The number i
861a0 73 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65 64 s always rounded
861b0 0a 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c 74 .** up to a mult
861c0 69 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53 51 iple of 2..*/.SQ
861d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
861e0 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 d sqlite3Memdebu
861f0 67 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20 64 gBacktrace(int d
86200 65 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65 70 epth){. if( dep
86210 74 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d 20 th<0 ){ depth =
86220 30 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74 68 0; }. if( depth
86230 3e 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20 32 >20 ){ depth = 2
86240 30 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20 28 0; }. depth = (
86250 64 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a 20 depth+1)&0xfe;.
86260 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 mem.nBacktrace
86270 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c 49 = depth;.}..SQLI
86280 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
86290 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 42 sqlite3MemdebugB
862a0 61 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63 6b acktraceCallback
862b0 28 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 (void (*xBacktra
862c0 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f ce)(int, int, vo
862d0 69 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e 78 id **)){. mem.x
862e0 42 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61 63 Backtrace = xBac
862f0 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ktrace;.}../*.**
86300 20 53 65 74 20 74 68 65 20 74 69 74 6c 65 20 73 Set the title s
86310 74 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65 71 tring for subseq
86320 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 uent allocations
86330 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
86340 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
86350 4d 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c 65 MemdebugSettitle
86360 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 69 (const char *zTi
86370 74 6c 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 tle){. unsigned
86380 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 int n = sqlite3
86390 53 74 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65 29 Strlen30(zTitle)
863a0 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 5f + 1;. sqlite3_
863b0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e mutex_enter(mem.
863c0 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 3e mutex);. if( n>
863d0 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 =sizeof(mem.zTit
863e0 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f 66 le) ) n = sizeof
863f0 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b 0a (mem.zTitle)-1;.
86400 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54 69 memcpy(mem.zTi
86410 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29 3b tle, zTitle, n);
86420 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b 6e 5d . mem.zTitle[n]
86430 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 69 74 = 0;. mem.nTit
86440 6c 65 20 3d 20 52 4f 55 4e 44 38 28 6e 29 3b 0a le = ROUND8(n);.
86450 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
86460 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29 leave(mem.mutex)
86470 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
86480 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
86490 4d 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b 0a MemdebugSync(){.
864a0 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 struct MemBloc
864b0 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66 6f kHdr *pHdr;. fo
864c0 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 r(pHdr=mem.pFirs
864d0 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 t; pHdr; pHdr=pH
864e0 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 dr->pNext){.
864f0 76 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76 6f void **pBt = (vo
86500 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 70 id**)pHdr;. p
86510 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63 Bt -= pHdr->nBac
86520 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20 ktraceSlots;.
86530 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 mem.xBacktrace(
86540 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48 64 pHdr->iSize, pHd
86550 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c r->nBacktrace-1,
86560 20 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a 7d &pBt[1]);. }.}
86570 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 ../*.** Open the
86580 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 file indicated
86590 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 and write a log
865a0 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d of all unfreed m
865b0 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 emory .** alloca
865c0 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 tions into that
865d0 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 log..*/.SQLITE_P
865e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
865f0 74 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70 28 te3MemdebugDump(
86600 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
86610 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a ename){. FILE *
86620 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65 out;. struct Me
86630 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b mBlockHdr *pHdr;
86640 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 . void **pBt;.
86650 20 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d 20 int i;. out =
86660 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c fopen(zFilename,
86670 20 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75 74 "w");. if( out
86680 3d 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e ==0 ){. fprin
86690 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 tf(stderr, "** U
866a0 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 nable to output
866b0 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74 memory debug out
866c0 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e put log: %s **\n
866d0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
866e0 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 zFilename
866f0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
86700 20 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 }. for(pHdr=me
86710 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 m.pFirst; pHdr;
86720 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 pHdr=pHdr->pNext
86730 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d ){. char *z =
86740 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20 20 (char*)pHdr;.
86750 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 z -= pHdr->nBa
86760 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69 7a cktraceSlots*siz
86770 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48 64 eof(void*) + pHd
86780 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 66 r->nTitle;. f
86790 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a 2a printf(out, "***
867a0 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74 20 * %lld bytes at
867b0 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a 5c %p from %s ****\
867c0 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 n", .
867d0 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26 70 pHdr->iSize, &p
867e0 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 54 Hdr[1], pHdr->nT
867f0 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f 22 itle ? z : "???"
86800 29 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72 2d );. if( pHdr-
86810 3e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 >nBacktrace ){.
86820 20 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74 29 fflush(out)
86830 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28 76 ;. pBt = (v
86840 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 oid**)pHdr;.
86850 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e pBt -= pHdr->n
86860 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a BacktraceSlots;.
86870 20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65 5f backtrace_
86880 73 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c 20 symbols_fd(pBt,
86890 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
868a0 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b 0a , fileno(out));.
868b0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
868c0 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a t, "\n");. }.
868d0 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 }. fprintf(ou
868e0 74 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b t, "COUNTS:\n");
868f0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 . for(i=0; i<NC
86900 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 SIZE-1; i++){.
86910 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 if( mem.nAlloc
86920 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70 72 [i] ){. fpr
86930 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 intf(out, " %5
86940 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 d: %10d %10d %10
86950 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 d\n", .
86960 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c i*8, mem.nAll
86970 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72 oc[i], mem.nCurr
86980 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75 ent[i], mem.mxCu
86990 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 7d rrent[i]);. }
869a0 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e 6e . }. if( mem.n
869b0 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 20 Alloc[NCSIZE-1]
869c0 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f ){. fprintf(o
869d0 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31 30 ut, " %5d: %10
869e0 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 0a d %10d %10d\n",.
869f0 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 43 53 NCS
86a00 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41 6c IZE*8-8, mem.nAl
86a10 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a 20 loc[NCSIZE-1],.
86a20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e mem.
86a30 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d nCurrent[NCSIZE-
86a40 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 1], mem.mxCurren
86a50 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20 20 t[NCSIZE-1]);.
86a60 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b }. fclose(out);
86a70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
86a80 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 the number of t
86a90 69 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d 4d imes sqlite3MemM
86aa0 61 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65 6e alloc() has been
86ab0 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 called..*/.SQLI
86ac0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
86ad0 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d 61 qlite3MemdebugMa
86ae0 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20 69 llocCount(){. i
86af0 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f 74 nt i;. int nTot
86b00 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d al = 0;. for(i=
86b10 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b 2b 0; i<NCSIZE; i++
86b20 29 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b 3d ){. nTotal +=
86b30 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b 0a mem.nAlloc[i];.
86b40 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54 6f }. return nTo
86b50 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 tal;.}...#endif
86b60 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 /* SQLITE_MEMDEB
86b70 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a UG */../********
86b80 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
86b90 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m2.c ***********
86ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86bc0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
86bd0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
86be0 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a e mem3.c *******
86bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86c10 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
86c20 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a 7 October 14.**.
86c30 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
86c40 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
86c50 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
86c60 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
86c70 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
86c80 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
86c90 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
86ca0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
86cb0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
86cc0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
86cd0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
86ce0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
86cf0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
86d00 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
86d10 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
86d20 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
86d30 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
86d40 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
86d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
86d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
86d90 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
86da0 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e s the C function
86db0 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
86dc0 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c a memory.** all
86dd0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
86de0 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c m for use by SQL
86df0 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ite. .**.** This
86e00 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 version of the
86e10 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
86e20 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74 n subsystem omit
86e30 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20 s all.** use of
86e40 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53 51 malloc(). The SQ
86e50 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 6c 69 Lite user suppli
86e60 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 es a block of me
86e70 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 mory.** before c
86e80 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 alling sqlite3_i
86e90 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d nitialize() from
86ea0 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f which allocatio
86eb0 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 ns.** are made a
86ec0 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 nd returned by t
86ed0 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 he xMalloc() and
86ee0 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 xRealloc() .**
86ef0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e implementations.
86f00 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e Once sqlite3_in
86f10 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 itialize() has b
86f20 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 een called,.** t
86f30 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d he amount of mem
86f40 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f ory available to
86f50 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 SQLite is fixed
86f60 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 and cannot.** b
86f70 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a e changed..**.**
86f80 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
86f90 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
86fa0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d cation subsystem
86fb0 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 is included.**
86fc0 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c in the build onl
86fd0 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 y if SQLITE_ENAB
86fe0 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 65 LE_MEMSYS3 is de
86ff0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 fined..**.** $Id
87000 3a 20 6d 65 6d 33 2e 63 2c 76 20 31 2e 32 35 20 : mem3.c,v 1.25
87010 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 2008/11/19 16:52
87020 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :44 danielk1977
87030 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
87040 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
87050 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
87060 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 75 69 ator is only bui
87070 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72 lt into the libr
87080 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e ary.** SQLITE_EN
87090 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 ABLE_MEMSYS3 is
870a0 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e 69 6e defined. Definin
870b0 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 64 6f g this symbol do
870c0 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e 20 74 es not.** mean t
870d0 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 hat the library
870e0 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d 6f 72 will use a memor
870f0 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 75 6c y-pool by defaul
87100 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a 2a 20 t, just that.**
87110 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e it is available.
87120 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 6c 6c The mempool all
87130 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 76 61 ocator is activa
87140 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 0a 2a ted by calling.*
87150 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 * sqlite3_config
87160 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ()..*/.#ifdef SQ
87170 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
87180 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d YS3../*.** Maxim
87190 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 um size (in Mem3
871a0 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73 6d Blocks) of a "sm
871b0 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23 all" chunk..*/.#
871c0 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20 define MX_SMALL
871d0 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 10.../*.** Numbe
871e0 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68 61 r of freelist ha
871f0 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66 sh slots.*/.#def
87200 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a 0a ine N_HASH 61..
87210 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61 /*.** A memory a
87220 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f 20 llocation (also
87230 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b 22 called a "chunk"
87240 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77 ) consists of tw
87250 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c o or .** more bl
87260 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 68 20 ocks where each
87270 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65 73 block is 8 bytes
87280 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 20 62 . The first 8 b
87290 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20 68 ytes are .** a h
872a0 65 61 64 65 72 20 74 68 61 74 20 69 73 20 6e 6f eader that is no
872b0 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 t returned to th
872c0 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 e user..**.** A
872d0 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 20 chunk is two or
872e0 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 74 more blocks that
872f0 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63 6b is either check
87300 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72 65 ed out or.** fre
87310 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62 6c e. The first bl
87320 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20 75 ock has format u
87330 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69 7a .hdr. u.hdr.siz
87340 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20 74 e4x is 4 times t
87350 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 he.** size of th
87360 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 e allocation in
87370 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61 6c blocks if the al
87380 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
87390 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e 73 ..** The u.hdr.s
873a0 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 20 74 ize4x&1 bit is t
873b0 72 75 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b rue if the chunk
873c0 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20 is checked out
873d0 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 and.** false if
873e0 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 the chunk is on
873f0 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 54 the freelist. T
87400 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 he u.hdr.size4x&
87410 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 2 bit.** is true
87420 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 if the previous
87430 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 chunk is checke
87440 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65 20 d out and false
87450 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f if the.** previo
87460 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65 65 us chunk is free
87470 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72 65 . The u.hdr.pre
87480 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20 74 vSize field is t
87490 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 he size of.** th
874a0 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b e previous chunk
874b0 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 in blocks if th
874c0 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b e previous chunk
874d0 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66 72 is on the.** fr
874e0 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20 70 eelist. If the p
874f0 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 revious chunk is
87500 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 checked out, th
87510 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65 76 en.** u.hdr.prev
87520 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72 74 Size can be part
87530 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 of the data for
87540 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64 20 that chunk and
87550 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65 should.** not be
87560 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
87570 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65 6e ..**.** We often
87580 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 75 6e identify a chun
87590 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 20 69 k by its index i
875a0 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20 n mem3.aPool[].
875b0 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69 73 When.** this is
875c0 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e 6b done, the chunk
875d0 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f index refers to
875e0 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63 the second bloc
875f0 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75 6e k of.** the chun
87600 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c k. In this way,
87610 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b the first chunk
87620 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
87630 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 1..** A chunk i
87640 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 20 ndex of 0 means
87650 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 20 "no such chunk"
87660 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 69 76 and is the equiv
87670 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e 55 alent.** of a NU
87680 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a LL pointer..**.*
87690 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f * The second blo
876a0 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b ck of free chunk
876b0 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d s is of the form
876c0 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a 2a u.list. The.**
876d0 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f 72 6d two fields form
876e0 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64 a double-linked
876f0 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b 73 20 list of chunks
87700 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a 65 73 of related sizes
87710 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f ..** Pointers to
87720 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 the head of the
87730 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 65 64 list are stored
87740 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c in mem3.aiSmall
87750 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c [] .** for small
87760 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 6d 65 er chunks and me
87770 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f 72 20 m3.aiHash[] for
87780 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e 0a 2a larger chunks..*
87790 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 *.** The second
877a0 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 6e 6b block of a chunk
877b0 20 69 73 20 75 73 65 72 20 64 61 74 61 20 69 66 is user data if
877c0 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 63 68 the chunk is ch
877d0 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e 20 20 ecked .** out.
877e0 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 63 68 If a chunk is ch
877f0 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 20 75 ecked out, the u
87800 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 78 74 ser data may ext
87810 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 end into.** the
87820 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 76 u.hdr.prevSize v
87830 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c alue of the foll
87840 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a owing chunk..*/.
87850 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d typedef struct M
87860 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f em3Block Mem3Blo
87870 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 33 42 ck;.struct Mem3B
87880 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b lock {. union {
87890 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 . struct {.
878a0 20 20 20 20 75 33 32 20 70 72 65 76 53 69 7a 65 u32 prevSize
878b0 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 ; /* Size of p
878c0 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e revious chunk in
878d0 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 Mem3Block eleme
878e0 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 nts */. u32
878f0 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a 20 size4x; /*
87900 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 63 4x the size of c
87910 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e 20 urrent chunk in
87920 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e Mem3Block elemen
87930 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 72 3b ts */. } hdr;
87940 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20 . struct {.
87950 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 20 20 u32 next;
87960 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 /* Index in
87970 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 mem3.aPool[] of
87980 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20 next free chunk
87990 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 72 65 */. u32 pre
879a0 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 v; /* Inde
879b0 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x in mem3.aPool[
879c0 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72 ] of previous fr
879d0 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 ee chunk */.
879e0 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b 0a 7d } list;. } u;.}
879f0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 ;../*.** All of
87a00 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 the static varia
87a10 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69 bles used by thi
87a20 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c s module are col
87a30 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 lected.** into a
87a40 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 single structur
87a50 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 2e 20 e named "mem3".
87a60 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 This is to keep
87a70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 the.** static v
87a80 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a ariables organiz
87a90 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 ed and to reduce
87aa0 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 namespace pollu
87ab0 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 tion.** when thi
87ac0 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 s module is comb
87ad0 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 ined with other
87ae0 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 in the amalgamat
87af0 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 ion..*/.static S
87b00 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
87b10 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a 20 20 Mem3Global {.
87b20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 /*. ** Memory a
87b30 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c vailable for all
87b40 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 69 ocation. nPool i
87b50 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 s the size of th
87b60 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 69 6e e array. ** (in
87b70 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 6f 69 Mem3Blocks) poi
87b80 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f 6f 6c nted to by aPool
87b90 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a 20 20 less 2.. */.
87ba0 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 6d u32 nPool;. Mem
87bb0 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b 0a 0a 3Block *aPool;..
87bc0 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20 69 /*. ** True i
87bd0 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 74 f we are evaluat
87be0 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 ing an out-of-me
87bf0 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 mory callback..
87c00 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d 42 */. int alarmB
87c10 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a usy;. . /*. *
87c20 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 * Mutex to contr
87c30 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ol access to the
87c40 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
87c50 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 on subsystem..
87c60 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
87c70 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20 20 ex *mutex;. .
87c80 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e 69 /*. ** The mini
87c90 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 mum amount of fr
87ca0 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77 65 ee space that we
87cb0 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a 2f have seen.. */
87cc0 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72 3b . u32 mnMaster;
87cd0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61 73 .. /*. ** iMas
87ce0 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65 78 ter is the index
87cf0 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 of the master c
87d00 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77 20 hunk. Most new
87d10 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2a allocations. **
87d20 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74 68 occur off of th
87d30 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61 73 is chunk. szMas
87d40 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65 20 ter is the size
87d50 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 0a (in Mem3Blocks).
87d60 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72 ** of the curr
87d70 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d 61 ent master. iMa
87d80 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68 65 ster is 0 if the
87d90 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65 72 re is not master
87da0 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 chunk.. ** The
87db0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69 73 master chunk is
87dc0 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20 74 not in either t
87dd0 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20 61 he aiHash[] or a
87de0 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a 20 iSmall[].. */.
87df0 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20 20 u32 iMaster;.
87e00 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a 20 u32 szMaster;..
87e10 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20 6f /*. ** Array o
87e20 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65 20 f lists of free
87e30 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e 67 blocks according
87e40 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 to the block si
87e50 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d 61 ze . ** for sma
87e60 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72 20 ller chunks, or
87e70 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62 6c a hash on the bl
87e80 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61 72 ock size for lar
87e90 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e ger. ** chunks.
87ea0 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53 6d . */. u32 aiSm
87eb0 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d 3b all[MX_SMALL-1];
87ec0 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 /* For sizes
87ed0 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d 41 2 through MX_SMA
87ee0 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a 2f LL, inclusive */
87ef0 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e 5f . u32 aiHash[N_
87f00 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a HASH]; /*
87f10 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53 4d For sizes MX_SM
87f20 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65 72 ALL+1 and larger
87f30 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b 20 39 */.} mem3 = { 9
87f40 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 65 66 7535575 };..#def
87f50 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 4c 28 ine mem3 GLOBAL(
87f60 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61 struct Mem3Globa
87f70 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a 20 l, mem3)../*.**
87f80 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b Unlink the chunk
87f90 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 at mem3.aPool[i
87fa0 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69 ] from list it i
87fb0 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f s currently.** o
87fc0 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 n. *pRoot is th
87fd0 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 69 73 e list that i is
87fe0 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f a member of..*/
87ff0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
88000 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 sys3UnlinkFromLi
88010 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70 st(u32 i, u32 *p
88020 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e 65 78 Root){. u32 nex
88030 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t = mem3.aPool[i
88040 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20 ].u.list.next;.
88050 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d 33 u32 prev = mem3
88060 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
88070 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28 .prev;. assert(
88080 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
88090 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
880a0 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d 3d 30 );. if( prev==0
880b0 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 20 3d ){. *pRoot =
880c0 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a next;. }else{.
880d0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 mem3.aPool[p
880e0 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 rev].u.list.next
880f0 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 = next;. }. i
88100 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 20 6d f( next ){. m
88110 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e em3.aPool[next].
88120 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 70 72 u.list.prev = pr
88130 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 2e 61 ev;. }. mem3.a
88140 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[i].u.list.n
88150 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 33 2e ext = 0;. mem3.
88160 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
88170 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a prev = 0;.}../*.
88180 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 ** Unlink the ch
88190 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20 66 unk at index i f
881a0 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 65 72 rom .** whatever
881b0 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 list is current
881c0 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a ly a member of..
881d0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
881e0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 33 32 emsys3Unlink(u32
881f0 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c i){. u32 size,
88200 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28 hash;. assert(
88210 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
88220 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
88230 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 );. assert( (me
88240 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
88250 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d hdr.size4x & 1)=
88260 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
88270 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d i>=1 );. size =
88280 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
88290 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b .u.hdr.size4x/4;
882a0 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d . assert( size=
882b0 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 =mem3.aPool[i+si
882c0 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 ze-1].u.hdr.prev
882d0 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Size );. assert
882e0 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 69 ( size>=2 );. i
882f0 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d f( size <= MX_SM
88300 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 ALL ){. memsy
88310 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 s3UnlinkFromList
88320 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c (i, &mem3.aiSmal
88330 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65 l[size-2]);. }e
88340 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 lse{. hash =
88350 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a 20 size % N_HASH;.
88360 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
88370 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d FromList(i, &mem
88380 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 3.aiHash[hash]);
88390 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 . }.}../*.** Li
883a0 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 nk the chunk at
883b0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f mem3.aPool[i] so
883c0 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 that is on the
883d0 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20 61 list rooted.** a
883e0 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61 t *pRoot..*/.sta
883f0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
88400 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33 32 LinkIntoList(u32
88410 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b i, u32 *pRoot){
88420 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
88430 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
88440 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6d m3.mutex) );. m
88450 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c em3.aPool[i].u.l
88460 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 6f 6f ist.next = *pRoo
88470 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b t;. mem3.aPool[
88480 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d i].u.list.prev =
88490 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f 6f 74 0;. if( *pRoot
884a0 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ){. mem3.aPo
884b0 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c 69 73 ol[*pRoot].u.lis
884c0 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a t.prev = i;. }.
884d0 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a 7d 0a *pRoot = i;.}.
884e0 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 ./*.** Link the
884f0 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 chunk at index i
88500 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68 65 into either the
88510 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 appropriate.**
88520 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 73 74 small chunk list
88530 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6c 61 , or into the la
88540 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74 rge chunk hash t
88550 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 able..*/.static
88560 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e 6b void memsys3Link
88570 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73 (u32 i){. u32 s
88580 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 ize, hash;. ass
88590 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
885a0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
885b0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
885c0 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 i>=1 );. asser
885d0 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
885e0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
885f0 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 73 69 & 1)==0 );. si
88600 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
88610 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
88620 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73 x/4;. assert( s
88630 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ize==mem3.aPool[
88640 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e i+size-1].u.hdr.
88650 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 prevSize );. as
88660 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b sert( size>=2 );
88670 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d . if( size <= M
88680 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d X_SMALL ){. m
88690 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 emsys3LinkIntoLi
886a0 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d st(i, &mem3.aiSm
886b0 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 all[size-2]);.
886c0 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 }else{. hash
886d0 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b = size % N_HASH;
886e0 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b . memsys3Link
886f0 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d IntoList(i, &mem
88700 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b 3.aiHash[hash]);
88710 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
88720 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 the STATIC_MEM
88730 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 mutex is not alr
88740 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 eady held, obtai
88750 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 n it now. The mu
88760 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 tex.** will alre
88770 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 ady be held (obt
88780 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e ained by code in
88790 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a malloc.c) if.**
887a0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
887b0 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 nfig.bMemStat is
887c0 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 true..*/.static
887d0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 6e 74 void memsys3Ent
887e0 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 er(void){. if(
887f0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
88800 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 fig.bMemstat==0
88810 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 3d 30 && mem3.mutex==0
88820 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d 75 74 ){. mem3.mut
88830 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 ex = sqlite3Mute
88840 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
88850 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b TEX_STATIC_MEM);
88860 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
88870 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e utex_enter(mem3.
88880 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 mutex);.}.static
88890 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65 61 void memsys3Lea
888a0 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 ve(void){. sqli
888b0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
888c0 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a mem3.mutex);.}..
888d0 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 68 65 /*.** Called whe
888e0 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20 n we are unable
888f0 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 61 6c to satisfy an al
88900 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 79 74 location of nByt
88910 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f es..*/.static vo
88920 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d id memsys3OutOfM
88930 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 65 29 emory(int nByte)
88940 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e 61 6c {. if( !mem3.al
88950 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 20 6d armBusy ){. m
88960 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 em3.alarmBusy =
88970 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 1;. assert( s
88980 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
88990 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b d(mem3.mutex) );
889a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
889b0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 ex_leave(mem3.mu
889c0 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 tex);. sqlite
889d0 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
889e0 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c (nByte);. sql
889f0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
88a00 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem3.mutex);.
88a10 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 mem3.alarmBusy
88a20 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a = 0;. }.}.../*
88a30 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 20 61 .** Chunk i is a
88a40 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 61 74 free chunk that
88a50 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b has been unlink
88a60 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 73 20 ed. Adjust its
88a70 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d 65 74 .** size paramet
88a80 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d 6f 75 ers for check-ou
88a90 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 t and return a p
88aa0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a 2a ointer to the .*
88ab0 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e 20 6f * user portion o
88ac0 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a 2f 0a f the chunk..*/.
88ad0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d static void *mem
88ae0 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 33 32 sys3Checkout(u32
88af0 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 29 7b i, u32 nBlock){
88b00 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 73 65 . u32 x;. asse
88b10 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
88b20 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 x_held(mem3.mute
88b30 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
88b40 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 i>=1 );. assert
88b50 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 ( mem3.aPool[i-1
88b60 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 ].u.hdr.size4x/4
88b70 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 ==nBlock );. as
88b80 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c sert( mem3.aPool
88b90 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 [i+nBlock-1].u.h
88ba0 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e 42 6c dr.prevSize==nBl
88bb0 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d 65 6d ock );. x = mem
88bc0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
88bd0 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d 65 6d dr.size4x;. mem
88be0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
88bf0 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f dr.size4x = nBlo
88c00 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 32 29 ck*4 | 1 | (x&2)
88c10 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ;. mem3.aPool[i
88c20 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72 +nBlock-1].u.hdr
88c30 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f .prevSize = nBlo
88c40 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ck;. mem3.aPool
88c50 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 [i+nBlock-1].u.h
88c60 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a dr.size4x |= 2;.
88c70 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e 61 return &mem3.a
88c80 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a Pool[i];.}../*.*
88c90 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 65 20 * Carve a piece
88ca0 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 20 6f off of the end o
88cb0 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 73 74 f the mem3.iMast
88cc0 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 2a er free chunk..*
88cd0 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 * Return a point
88ce0 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61 6c er to the new al
88cf0 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c 20 69 location. Or, i
88d00 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 f the master chu
88d10 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 nk.** is not lar
88d20 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 75 72 ge enough, retur
88d30 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 n 0..*/.static v
88d40 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 6f 6d oid *memsys3From
88d50 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c 6f 63 Master(u32 nBloc
88d60 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 k){. assert( sq
88d70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
88d80 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a (mem3.mutex) );.
88d90 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 73 assert( mem3.s
88da0 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 zMaster>=nBlock
88db0 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 3e );. if( nBlock>
88dc0 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 =mem3.szMaster-1
88dd0 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 ){. /* Use t
88de0 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 he entire master
88df0 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 20 */. void *p
88e00 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 = memsys3Checkou
88e10 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20 t(mem3.iMaster,
88e20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a mem3.szMaster);.
88e30 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 mem3.iMaster
88e40 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 = 0;. mem3.s
88e50 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 zMaster = 0;.
88e60 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d mem3.mnMaster =
88e70 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 0;. return p
88e80 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
88e90 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 73 74 * Split the mast
88ea0 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 75 72 er block. Retur
88eb0 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f 0a 20 n the tail. */.
88ec0 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 3b 0a u32 newi, x;.
88ed0 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d 33 2e newi = mem3.
88ee0 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 2e 73 iMaster + mem3.s
88ef0 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f 63 6b zMaster - nBlock
88f00 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 65 ;. assert( ne
88f10 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 wi > mem3.iMaste
88f20 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d 33 2e r+1 );. mem3.
88f30 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
88f40 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 er+mem3.szMaster
88f50 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 -1].u.hdr.prevSi
88f60 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 ze = nBlock;.
88f70 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 mem3.aPool[mem3
88f80 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a .iMaster+mem3.sz
88f90 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
88fa0 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 20 size4x |= 2;.
88fb0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69 mem3.aPool[newi
88fc0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
88fd0 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b = nBlock*4 + 1;
88fe0 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 . mem3.szMast
88ff0 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 er -= nBlock;.
89000 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 mem3.aPool[new
89010 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 i-1].u.hdr.prevS
89020 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 ize = mem3.szMas
89030 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d 65 6d ter;. x = mem
89040 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
89050 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ster-1].u.hdr.si
89060 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 6d 65 ze4x & 2;. me
89070 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d m3.aPool[mem3.iM
89080 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 aster-1].u.hdr.s
89090 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d ize4x = mem3.szM
890a0 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20 aster*4 | x;.
890b0 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 if( mem3.szMast
890c0 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 er < mem3.mnMast
890d0 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 er ){. mem3
890e0 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 .mnMaster = mem3
890f0 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d .szMaster;. }
89100 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 . return (voi
89110 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e d*)&mem3.aPool[n
89120 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ewi];. }.}../*.
89130 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 65 ** *pRoot is the
89140 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 74 20 head of a list
89150 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 6f of free chunks o
89160 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 0a f the same size.
89170 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a 65 20 ** or same size
89180 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 72 20 hash. In other
89190 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 69 73 words, *pRoot is
891a0 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 69 74 an entry in eit
891b0 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 53 6d her.** mem3.aiSm
891c0 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e 61 69 all[] or mem3.ai
891d0 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 Hash[]. .**.**
891e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 61 This routine exa
891f0 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69 65 mines all entrie
89200 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 6c s on the given l
89210 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a ist and tries.**
89220 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61 63 to coalesce eac
89230 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 61 h entries with a
89240 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68 75 djacent free chu
89250 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 nks. .**.** If
89260 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b 20 it sees a chunk
89270 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 that is larger t
89280 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 han mem3.iMaster
89290 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a 2a , it replaces .*
892a0 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65 * the current me
892b0 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68 20 m3.iMaster with
892c0 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 63 the new larger c
892d0 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 20 hunk. In order
892e0 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d 33 for.** this mem3
892f0 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63 65 .iMaster replace
89300 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 68 ment to work, th
89310 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6d e master chunk m
89320 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64 ust be.** linked
89330 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 into the hash t
89340 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73 20 ables. That is
89350 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73 not the normal s
89360 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61 69 tate of.** affai
89370 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20 rs, of course.
89380 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 The calling rout
89390 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74 68 ine must link th
893a0 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75 6e e master.** chun
893b0 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69 6e k before invokin
893c0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 g this routine,
893d0 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e 6b then must unlink
893e0 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 0a 2a the (possibly.*
893f0 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 74 65 * changed) maste
89400 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68 69 r chunk once thi
89410 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69 s routine has fi
89420 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 nished..*/.stati
89430 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d 65 c void memsys3Me
89440 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 29 7b rge(u32 *pRoot){
89450 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 70 72 . u32 iNext, pr
89460 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b 0a ev, size, i, x;.
89470 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
89480 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
89490 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 m3.mutex) );. f
894a0 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30 or(i=*pRoot; i>0
894b0 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 20 20 ; i=iNext){.
894c0 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f iNext = mem3.aPo
894d0 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 ol[i].u.list.nex
894e0 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 t;. size = me
894f0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
89500 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20 hdr.size4x;.
89510 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 31 29 assert( (size&1)
89520 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28 ==0 );. if( (
89530 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a 20 20 size&2)==0 ){.
89540 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
89550 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52 6f kFromList(i, pRo
89560 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ot);. asser
89570 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f t( i > mem3.aPoo
89580 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 l[i-1].u.hdr.pre
89590 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 70 vSize );. p
895a0 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e 61 rev = i - mem3.a
895b0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
895c0 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20 20 prevSize;.
895d0 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 74 20 if( prev==iNext
895e0 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 ){. iNext
895f0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 = mem3.aPool[pr
89600 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b ev].u.list.next;
89610 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
89620 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 72 65 emsys3Unlink(pre
89630 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d v);. size =
89640 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 70 72 i + size/4 - pr
89650 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 ev;. x = me
89660 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d m3.aPool[prev-1]
89670 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 .u.hdr.size4x &
89680 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 2;. mem3.aP
89690 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 64 ool[prev-1].u.hd
896a0 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a 65 2a r.size4x = size*
896b0 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 4 | x;. mem
896c0 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 69 7a 3.aPool[prev+siz
896d0 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 e-1].u.hdr.prevS
896e0 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 ize = size;.
896f0 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 70 72 memsys3Link(pr
89700 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 70 ev);. i = p
89710 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a rev;. }else{.
89720 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 34 3b size /= 4;
89730 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 . }. if( s
89740 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ize>mem3.szMaste
89750 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 2e r ){. mem3.
89760 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 20 20 iMaster = i;.
89770 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 mem3.szMaster
89780 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 = size;. }.
89790 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 }.}../*.** Retu
897a0 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 rn a block of me
897b0 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74 mory of at least
897c0 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e nBytes in size.
897d0 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 .** Return NULL
897e0 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a if unable..**.**
897f0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
89800 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
89810 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65 necessary mutexe
89820 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a s, if any, are.*
89830 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62 * already held b
89840 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65 y the caller. He
89850 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f nce "Unsafe"..*/
89860 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
89870 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 msys3MallocUnsaf
89880 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 e(int nByte){.
89890 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e 42 6c u32 i;. u32 nBl
898a0 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 72 65 ock;. u32 toFre
898b0 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 e;.. assert( sq
898c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
898d0 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a (mem3.mutex) );.
898e0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
898f0 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29 (Mem3Block)==8 )
89900 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 31 ;. if( nByte<=1
89910 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 2 ){. nBlock
89920 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 2;. }else{.
89930 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 nBlock = (nByt
89940 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d 0a 20 e + 11)/8;. }.
89950 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 6b 3e assert( nBlock>
89960 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 45 50 =2 );.. /* STEP
89970 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f 1:. ** Look fo
89980 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 r an entry of th
89990 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 69 e correct size i
899a0 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 6d 61 n either the sma
899b0 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 74 61 ll. ** chunk ta
899c0 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 6c 61 ble or in the la
899d0 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74 rge chunk hash t
899e0 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20 able. This is.
899f0 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6d ** successful m
89a00 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d 65 20 ost of the time
89a10 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 20 6f (about 9 times o
89a20 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a 2f 0a ut of 10).. */.
89a30 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20 if( nBlock <=
89a40 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 MX_SMALL ){.
89a50 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c i = mem3.aiSmall
89a60 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 20 20 [nBlock-2];.
89a70 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 if( i>0 ){.
89a80 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 memsys3UnlinkFr
89a90 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e omList(i, &mem3.
89aa0 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 aiSmall[nBlock-2
89ab0 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ]);. return
89ac0 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 memsys3Checkout
89ad0 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 (i, nBlock);.
89ae0 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
89af0 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c 6f 63 int hash = nBloc
89b00 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20 k % N_HASH;.
89b10 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 61 73 for(i=mem3.aiHas
89b20 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 69 3d h[hash]; i>0; i=
89b30 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e mem3.aPool[i].u.
89b40 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 list.next){.
89b50 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c if( mem3.aPool
89b60 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
89b70 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 4x/4==nBlock ){.
89b80 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 memsys3U
89b90 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c nlinkFromList(i,
89ba0 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 &mem3.aiHash[ha
89bb0 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 72 65 sh]);. re
89bc0 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 turn memsys3Chec
89bd0 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b kout(i, nBlock);
89be0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
89bf0 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 32 3a }.. /* STEP 2:
89c00 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61 74 . ** Try to sat
89c10 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 isfy the allocat
89c20 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 ion by carving a
89c30 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 piece off of th
89c40 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 e end. ** of th
89c50 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 e master chunk.
89c60 20 54 68 69 73 20 73 74 65 70 20 75 73 75 61 6c This step usual
89c70 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 65 70 ly works if step
89c80 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 1 fails.. */.
89c90 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 if( mem3.szMast
89ca0 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 er>=nBlock ){.
89cb0 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 return memsys3
89cc0 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 63 FromMaster(nBloc
89cd0 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 53 k);. }... /* S
89ce0 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 4c 6f TEP 3: . ** Lo
89cf0 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 op through the e
89d00 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 6f 6f ntire memory poo
89d10 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 64 6a l. Coalesce adj
89d20 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a 2a 20 acent free. **
89d30 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d 70 75 chunks. Recompu
89d40 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 te the master ch
89d50 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 67 65 unk as the large
89d60 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 20 st free chunk..
89d70 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 67 61 ** Then try aga
89d80 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 in to satisfy th
89d90 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 e allocation by
89da0 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 carving a piece
89db0 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 off. ** of the
89dc0 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65 end of the maste
89dd0 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 r chunk. This s
89de0 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 72 79 tep happens very
89df0 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77 65 . ** rarely (we
89e00 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 20 66 hope!). */. f
89e10 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b or(toFree=nBlock
89e20 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65 6d *16; toFree<(mem
89e30 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46 3.nPool*16); toF
89e40 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 20 6d ree *= 2){. m
89e50 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f 72 emsys3OutOfMemor
89e60 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 20 69 y(toFree);. i
89e70 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 f( mem3.iMaster
89e80 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 ){. memsys3
89e90 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 Link(mem3.iMaste
89ea0 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 r);. mem3.i
89eb0 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
89ec0 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 mem3.szMaster
89ed0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 = 0;. }. f
89ee0 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48 or(i=0; i<N_HASH
89ef0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 ; i++){. me
89f00 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 msys3Merge(&mem3
89f10 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 20 20 .aiHash[i]);.
89f20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 }. for(i=0;
89f30 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b i<MX_SMALL-1; i+
89f40 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 +){. memsys
89f50 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 53 3Merge(&mem3.aiS
89f60 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a mall[i]);. }.
89f70 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d if( mem3.szM
89f80 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d aster ){. m
89f90 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d emsys3Unlink(mem
89fa0 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 3.iMaster);.
89fb0 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 if( mem3.szMas
89fc0 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 ter>=nBlock ){.
89fd0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 return me
89fe0 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 msys3FromMaster(
89ff0 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d nBlock);. }
8a000 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
8a010 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 If none of the
8a020 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74 68 above worked, th
8a030 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a 20 en we fail. */.
8a040 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
8a050 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73 .** Free an outs
8a060 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 tanding memory a
8a070 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a llocation..**.**
8a080 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 This function a
8a090 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 ssumes that the
8a0a0 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65 necessary mutexe
8a0b0 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a s, if any, are.*
8a0c0 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62 * already held b
8a0d0 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65 y the caller. He
8a0e0 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f nce "Unsafe"..*/
8a0f0 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65 .void memsys3Fre
8a100 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 4f eUnsafe(void *pO
8a110 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b ld){. Mem3Block
8a120 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b *p = (Mem3Block
8a130 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 3b *)pOld;. int i;
8a140 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 3b 0a . u32 size, x;.
8a150 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
8a160 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
8a170 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 3.mutex) );. as
8a180 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 50 6f sert( p>mem3.aPo
8a190 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e 61 50 ol && p<&mem3.aP
8a1a0 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20 ool[mem3.nPool]
8a1b0 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d );. i = p - mem
8a1c0 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 72 3.aPool;. asser
8a1d0 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 t( (mem3.aPool[i
8a1e0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8a1f0 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 7a 65 &1)==1 );. size
8a200 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
8a210 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
8a220 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 2b 73 4;. assert( i+s
8a230 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b ize<=mem3.nPool+
8a240 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 1 );. mem3.aPoo
8a250 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
8a260 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d e4x &= ~1;. mem
8a270 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 3.aPool[i+size-1
8a280 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
8a290 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e = size;. mem3.
8a2a0 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e aPool[i+size-1].
8a2b0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 u.hdr.size4x &=
8a2c0 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69 6e ~2;. memsys3Lin
8a2d0 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 20 k(i);.. /* Try
8a2e0 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d 61 to expand the ma
8a2f0 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20 6e ster using the n
8a300 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e 6b ewly freed chunk
8a310 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 69 */. if( mem3.i
8a320 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77 68 Master ){. wh
8a330 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c ile( (mem3.aPool
8a340 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d [mem3.iMaster-1]
8a350 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29 .u.hdr.size4x&2)
8a360 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a ==0 ){. siz
8a370 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d e = mem3.aPool[m
8a380 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
8a390 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 .hdr.prevSize;.
8a3a0 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 mem3.iMaste
8a3b0 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 r -= size;.
8a3c0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b mem3.szMaster +
8a3d0 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 = size;. me
8a3e0 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33 msys3Unlink(mem3
8a3f0 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 .iMaster);.
8a400 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x = mem3.aPool[
8a410 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
8a420 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 u.hdr.size4x & 2
8a430 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f ;. mem3.aPo
8a440 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d ol[mem3.iMaster-
8a450 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8a460 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a = mem3.szMaster*
8a470 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 4 | x;. mem
8a480 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
8a490 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
8a4a0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 er-1].u.hdr.prev
8a4b0 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 Size = mem3.szMa
8a4c0 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ster;. }.
8a4d0 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d x = mem3.aPool[m
8a4e0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
8a4f0 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b .hdr.size4x & 2;
8a500 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d . while( (mem
8a510 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
8a520 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
8a530 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
8a540 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 4x&1)==0 ){.
8a550 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 memsys3Unlink(
8a560 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d mem3.iMaster+mem
8a570 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 3.szMaster);.
8a580 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 mem3.szMaster
8a590 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d += mem3.aPool[m
8a5a0 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 em3.iMaster+mem3
8a5b0 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 .szMaster-1].u.h
8a5c0 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 20 dr.size4x/4;.
8a5d0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 mem3.aPool[me
8a5e0 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e m3.iMaster-1].u.
8a5f0 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d hdr.size4x = mem
8a600 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3.szMaster*4 | x
8a610 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f ;. mem3.aPo
8a620 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b ol[mem3.iMaster+
8a630 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d mem3.szMaster-1]
8a640 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
8a650 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b = mem3.szMaster;
8a660 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
8a670 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
8a680 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 ize of an outsta
8a690 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e nding allocation
8a6a0 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 , in bytes. The
8a6b0 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 .** size returne
8a6c0 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 d omits the 8-by
8a6d0 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 te header overhe
8a6e0 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a ad. This only.*
8a6f0 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e * works for chun
8a700 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 ks that are curr
8a710 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 ently checked ou
8a720 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
8a730 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f 69 memsys3Size(voi
8a740 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f d *p){. Mem3Blo
8a750 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 66 ck *pBlock;. if
8a760 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
8a770 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 28 4d 0;. pBlock = (M
8a780 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61 em3Block*)p;. a
8a790 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b 5b 2d ssert( (pBlock[-
8a7a0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 1].u.hdr.size4x&
8a7b0 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 1)!=0 );. retur
8a7c0 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e n (pBlock[-1].u.
8a7d0 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 2a 32 hdr.size4x&~3)*2
8a7e0 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 - 4;.}../*.** R
8a7f0 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 ound up a reques
8a800 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 t size to the ne
8a810 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 xt valid allocat
8a820 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 ion size..*/.sta
8a830 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 52 tic int memsys3R
8a840 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 oundup(int n){.
8a850 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 20 if( n<=12 ){.
8a860 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 20 7d return 12;. }
8a870 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
8a880 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34 ((n+11)&~7) - 4
8a890 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
8a8a0 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f llocate nBytes o
8a8b0 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 f memory..*/.sta
8a8c0 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 tic void *memsys
8a8d0 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 3Malloc(int nByt
8a8e0 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 es){. sqlite3_i
8a8f0 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72 nt64 *p;. asser
8a900 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b 20 20 t( nBytes>0 );
8a910 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f /* mallo
8a920 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 74 20 c.c filters out
8a930 30 20 62 79 74 65 20 72 65 71 75 65 73 74 73 20 0 byte requests
8a940 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 */. memsys3Ente
8a950 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 r();. p = memsy
8a960 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e s3MallocUnsafe(n
8a970 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 79 73 Bytes);. memsys
8a980 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 3Leave();. retu
8a990 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a rn (void*)p; .}.
8a9a0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f ./*.** Free memo
8a9b0 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 ry..*/.void mems
8a9c0 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a 70 50 ys3Free(void *pP
8a9d0 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28 rior){. assert(
8a9e0 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d 65 6d pPrior );. mem
8a9f0 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 6d sys3Enter();. m
8aa00 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 emsys3FreeUnsafe
8aa10 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 (pPrior);. mems
8aa20 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a 0a 2f ys3Leave();.}../
8aa30 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
8aa40 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 size of an exist
8aa50 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ing memory alloc
8aa60 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 2a 6d ation.*/.void *m
8aa70 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 76 6f emsys3Realloc(vo
8aa80 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 id *pPrior, int
8aa90 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e nBytes){. int n
8aaa0 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a Old;. void *p;.
8aab0 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 if( pPrior==0
8aac0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 ){. return sq
8aad0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 lite3_malloc(nBy
8aae0 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 tes);. }. if(
8aaf0 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 nBytes<=0 ){.
8ab00 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 sqlite3_free(pP
8ab10 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72 rior);. retur
8ab20 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 n 0;. }. nOld
8ab30 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 70 50 = memsys3Size(pP
8ab40 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 rior);. if( nBy
8ab50 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e 42 79 tes<=nOld && nBy
8ab60 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 29 7b tes>=nOld-128 ){
8ab70 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 69 . return pPri
8ab80 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 or;. }. memsys
8ab90 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 3Enter();. p =
8aba0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 memsys3MallocUns
8abb0 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 afe(nBytes);. i
8abc0 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 f( p ){. if(
8abd0 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a 20 nOld<nBytes ){.
8abe0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 memcpy(p, p
8abf0 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 Prior, nOld);.
8ac00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d }else{. m
8ac10 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c emcpy(p, pPrior,
8ac20 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 7d 0a nBytes);. }.
8ac30 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55 memsys3FreeU
8ac40 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 nsafe(pPrior);.
8ac50 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 }. memsys3Leav
8ac60 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b e();. return p;
8ac70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
8ac80 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 lize this module
8ac90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8aca0 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f 69 64 memsys3Init(void
8acb0 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e *NotUsed){. UN
8acc0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
8acd0 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 21 otUsed);. if( !
8ace0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
8acf0 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 20 20 fig.pHeap ){.
8ad00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
8ad10 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 RROR;. }.. /*
8ad20 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20 Store a pointer
8ad30 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 6c to the memory bl
8ad40 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73 74 ock in global st
8ad50 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a 2f ructure mem3. */
8ad60 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f . assert( sizeo
8ad70 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 f(Mem3Block)==8
8ad80 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20 );. mem3.aPool
8ad90 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29 73 = (Mem3Block *)s
8ada0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
8adb0 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 6d 33 ig.pHeap;. mem3
8adc0 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74 65 .nPool = (sqlite
8add0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 3GlobalConfig.nH
8ade0 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65 6d eap / sizeof(Mem
8adf0 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a 0a 20 3Block)) - 2;..
8ae00 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
8ae10 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e he master block.
8ae20 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 */. mem3.szMas
8ae30 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c ter = mem3.nPool
8ae40 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 ;. mem3.mnMaste
8ae50 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 r = mem3.szMaste
8ae60 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 r;. mem3.iMaste
8ae70 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e 61 50 r = 1;. mem3.aP
8ae80 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 69 7a ool[0].u.hdr.siz
8ae90 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a 4d 61 e4x = (mem3.szMa
8aea0 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a 20 20 ster<<2) + 2;.
8aeb0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8aec0 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 72 65 nPool].u.hdr.pre
8aed0 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e 50 6f vSize = mem3.nPo
8aee0 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c ol;. mem3.aPool
8aef0 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 [mem3.nPool].u.h
8af00 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a 0a dr.size4x = 1;..
8af10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
8af20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 OK;.}../*.** Dei
8af30 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d nitialize this m
8af40 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 odule..*/.static
8af50 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68 75 void memsys3Shu
8af60 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 tdown(void *NotU
8af70 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
8af80 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
8af90 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a );. return;.}..
8afa0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 ../*.** Open the
8afb0 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20 file indicated
8afc0 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20 and write a log
8afd0 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d of all unfreed m
8afe0 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 emory .** alloca
8aff0 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20 tions into that
8b000 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 log..*/.SQLITE_P
8b010 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
8b020 74 65 33 4d 65 6d 73 79 73 33 44 75 6d 70 28 63 te3Memsys3Dump(c
8b030 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
8b040 6e 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51 name){.#ifdef SQ
8b050 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c LITE_DEBUG. FIL
8b060 45 20 2a 6f 75 74 3b 0a 20 20 75 33 32 20 69 2c E *out;. u32 i,
8b070 20 6a 3b 0a 20 20 75 33 32 20 73 69 7a 65 3b 0a j;. u32 size;.
8b080 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d if( zFilename=
8b090 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b =0 || zFilename[
8b0a0 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 0]==0 ){. out
8b0b0 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c = stdout;. }el
8b0c0 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f se{. out = fo
8b0d0 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 pen(zFilename, "
8b0e0 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 w");. if( out
8b0f0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72 ==0 ){. fpr
8b100 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a intf(stderr, "**
8b110 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 Unable to outpu
8b120 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f t memory debug o
8b130 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a utput log: %s **
8b140 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
8b150 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 zFile
8b160 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 name);. ret
8b170 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 urn;. }. }.
8b180 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b memsys3Enter();
8b190 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
8b1a0 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 20 "CHUNKS:\n");.
8b1b0 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33 for(i=1; i<=mem3
8b1c0 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f .nPool; i+=size/
8b1d0 34 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 4){. size = m
8b1e0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
8b1f0 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 .hdr.size4x;.
8b200 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31 20 29 if( size/4<=1 )
8b210 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
8b220 6f 75 74 2c 20 22 25 70 20 73 69 7a 65 20 65 72 out, "%p size er
8b230 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 ror\n", &mem3.aP
8b240 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 ool[i]);. a
8b250 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 ssert( 0 );.
8b260 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
8b270 20 20 20 69 66 28 20 28 73 69 7a 65 26 31 29 3d if( (size&1)=
8b280 3d 30 20 26 26 20 6d 65 6d 33 2e 61 50 6f 6f 6c =0 && mem3.aPool
8b290 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 [i+size/4-1].u.h
8b2a0 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73 69 7a dr.prevSize!=siz
8b2b0 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66 70 72 e/4 ){. fpr
8b2c0 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61 intf(out, "%p ta
8b2d0 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e 6f 74 il size does not
8b2e0 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65 6d 33 match\n", &mem3
8b2f0 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 .aPool[i]);.
8b300 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 assert( 0 );.
8b310 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
8b320 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65 6d 33 }. if( ((mem3
8b330 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d .aPool[i+size/4-
8b340 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 1].u.hdr.size4x&
8b350 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31 29 2)>>1)!=(size&1)
8b360 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 ){. fprint
8b370 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20 f(out, "%p tail
8b380 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69 73 20 checkout bit is
8b390 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d incorrect\n", &m
8b3a0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 em3.aPool[i]);.
8b3b0 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 assert( 0 )
8b3c0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
8b3d0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a }. if( siz
8b3e0 65 26 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 e&1 ){. fpr
8b3f0 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 intf(out, "%p %6
8b400 64 20 62 79 74 65 73 20 63 68 65 63 6b 65 64 20 d bytes checked
8b410 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 out\n", &mem3.aP
8b420 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 ool[i], (size/4)
8b430 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 *8-8);. }else
8b440 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
8b450 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 out, "%p %6d byt
8b460 65 73 20 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d es free%s\n", &m
8b470 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 em3.aPool[i], (s
8b480 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20 ize/4)*8-8,.
8b490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 3d i=
8b4a0 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3f 20 =mem3.iMaster ?
8b4b0 22 20 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 3a 20 " **master**" :
8b4c0 22 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 "");. }. }.
8b4d0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 for(i=0; i<MX_S
8b4e0 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 MALL-1; i++){.
8b4f0 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 53 6d 61 if( mem3.aiSma
8b500 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 ll[i]==0 ) conti
8b510 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 nue;. fprintf
8b520 28 6f 75 74 2c 20 22 73 6d 61 6c 6c 28 25 32 64 (out, "small(%2d
8b530 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 ):", i);. for
8b540 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c (j = mem3.aiSmal
8b550 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d l[i]; j>0; j=mem
8b560 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 3.aPool[j].u.lis
8b570 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66 t.next){. f
8b580 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 printf(out, " %p
8b590 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f (%d)", &mem3.aPo
8b5a0 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 ol[j],.
8b5b0 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c (mem3.aPool
8b5c0 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [j-1].u.hdr.size
8b5d0 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 4x/4)*8-8);.
8b5e0 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 }. fprintf(ou
8b5f0 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 t, "\n"); . }.
8b600 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 for(i=0; i<N_HA
8b610 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 SH; i++){. if
8b620 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d ( mem3.aiHash[i]
8b630 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a ==0 ) continue;.
8b640 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
8b650 20 22 68 61 73 68 28 25 32 64 29 3a 22 2c 20 69 "hash(%2d):", i
8b660 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d );. for(j = m
8b670 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3b 20 6a em3.aiHash[i]; j
8b680 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c >0; j=mem3.aPool
8b690 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 [j].u.list.next)
8b6a0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
8b6b0 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 out, " %p(%d)",
8b6c0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a &mem3.aPool[j],.
8b6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d (m
8b6e0 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 em3.aPool[j-1].u
8b6f0 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 .hdr.size4x/4)*8
8b700 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 -8);. }. f
8b710 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 printf(out, "\n"
8b720 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 ); . }. fprint
8b730 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72 3d 25 f(out, "master=%
8b740 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 74 d\n", mem3.iMast
8b750 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f er);. fprintf(o
8b760 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64 5c ut, "nowUsed=%d\
8b770 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 n", mem3.nPool*8
8b780 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 - mem3.szMaster
8b790 2a 38 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f *8);. fprintf(o
8b7a0 75 74 2c 20 22 6d 78 55 73 65 64 3d 25 64 5c 6e ut, "mxUsed=%d\n
8b7b0 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20 ", mem3.nPool*8
8b7c0 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a - mem3.mnMaster*
8b7d0 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 8);. sqlite3_mu
8b7e0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d tex_leave(mem3.m
8b7f0 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 75 74 utex);. if( out
8b800 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 ==stdout ){.
8b810 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a fflush(stdout);.
8b820 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c }else{. fcl
8b830 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65 ose(out);. }.#e
8b840 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 lse. UNUSED_PAR
8b850 41 4d 45 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65 AMETER(zFilename
8b860 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a );.#endif.}../*.
8b870 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
8b880 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 is the only rout
8b890 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ine in this file
8b8a0 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 0a with external .
8b8b0 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a ** linkage..**.*
8b8c0 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c * Populate the l
8b8d0 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
8b8e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
8b8f0 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a ion pointers in.
8b900 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ** sqlite3Global
8b910 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f Config.m with po
8b920 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f inters to the ro
8b930 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 utines in this f
8b940 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75 ile. The.** argu
8b950 6d 65 6e 74 73 20 73 70 65 63 69 66 79 20 74 68 ments specify th
8b960 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 e block of memor
8b970 79 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a y to manage..**.
8b980 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
8b990 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62 is only called b
8b9a0 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 y sqlite3_config
8b9b0 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72 (), and therefor
8b9c0 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 75 e.** is not requ
8b9d0 69 72 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 ired to be threa
8b9e0 64 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f 74 dsafe (it is not
8b9f0 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
8ba00 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 VATE const sqlit
8ba10 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a e3_mem_methods *
8ba20 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d sqlite3MemGetMem
8ba30 73 79 73 33 28 76 6f 69 64 29 7b 0a 20 20 73 74 sys3(void){. st
8ba40 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
8ba50 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d e3_mem_methods m
8ba60 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d 20 empoolMethods =
8ba70 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 61 {. memsys3Ma
8ba80 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 lloc,. memsy
8ba90 73 33 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d s3Free,. mem
8baa0 73 79 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 sys3Realloc,.
8bab0 20 20 6d 65 6d 73 79 73 33 53 69 7a 65 2c 0a 20 memsys3Size,.
8bac0 20 20 20 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64 memsys3Round
8bad0 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 up,. memsys3
8bae0 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 Init,. memsy
8baf0 73 33 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 s3Shutdown,.
8bb00 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 0. };. return
8bb10 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 &mempoolMethods
8bb20 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
8bb30 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
8bb40 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a SYS3 */../******
8bb50 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
8bb60 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mem3.c *********
8bb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bb90 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
8bba0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
8bbb0 69 6c 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a ile mem5.c *****
8bbc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bbe0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
8bbf0 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 007 October 14.*
8bc00 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
8bc10 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
8bc20 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
8bc30 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
8bc40 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
8bc50 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
8bc60 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
8bc70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
8bc80 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
8bc90 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
8bca0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
8bcb0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
8bcc0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
8bcd0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
8bce0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
8bcf0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
8bd00 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
8bd10 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
8bd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8bd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
8bd60 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
8bd70 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
8bd80 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
8bd90 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 nt a memory.** a
8bda0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
8bdb0 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 tem for use by S
8bdc0 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 QLite. .**.** Th
8bdd0 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 is version of th
8bde0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
8bdf0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d ion subsystem om
8be00 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f its all.** use o
8be10 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 f malloc(). The
8be20 61 70 70 6c 69 63 61 74 69 6f 6e 20 67 69 76 65 application give
8be30 73 20 53 51 4c 69 74 65 20 61 20 62 6c 6f 63 6b s SQLite a block
8be40 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 of memory.** be
8be50 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c fore calling sql
8be60 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
8be70 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c ) from which all
8be80 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 ocations.** are
8be90 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 made and returne
8bea0 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 d by the xMalloc
8beb0 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 () and xRealloc(
8bec0 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 ) .** implementa
8bed0 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 tions. Once sqli
8bee0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
8bef0 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
8bf00 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 ,.** the amount
8bf10 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 of memory availa
8bf20 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 ble to SQLite is
8bf30 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f fixed and canno
8bf40 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e t.** be changed.
8bf50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 .**.** This vers
8bf60 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
8bf70 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
8bf80 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 system is includ
8bf90 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 ed.** in the bui
8bfa0 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 ld only if SQLIT
8bfb0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 E_ENABLE_MEMSYS5
8bfc0 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a is defined..**.
8bfd0 2a 2a 20 54 68 69 73 20 6d 65 6d 6f 72 79 20 61 ** This memory a
8bfe0 6c 6c 6f 63 61 74 6f 72 20 75 73 65 73 20 74 68 llocator uses th
8bff0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6c 67 6f e following algo
8c000 72 69 74 68 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 rithm:.**.** 1
8c010 2e 20 20 41 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c . All memory al
8c020 6c 6f 63 61 74 69 6f 6e 73 20 73 69 7a 65 73 20 locations sizes
8c030 61 72 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 are rounded up t
8c040 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a o a power of 2..
8c050 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 49 66 20 74 **.** 2. If t
8c060 77 6f 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 wo adjacent free
8c070 20 62 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20 blocks are the
8c080 68 61 6c 76 65 73 20 6f 66 20 61 20 6c 61 72 67 halves of a larg
8c090 65 72 20 62 6c 6f 63 6b 2c 0a 2a 2a 20 20 20 20 er block,.**
8c0a0 20 20 20 74 68 65 6e 20 74 68 65 20 74 77 6f 20 then the two
8c0b0 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 61 6c 65 blocks are coale
8c0c0 73 65 64 20 69 6e 74 6f 20 74 68 65 20 73 69 6e sed into the sin
8c0d0 67 6c 65 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b gle larger block
8c0e0 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 4e 65 ..**.** 3. Ne
8c0f0 77 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f w memory is allo
8c100 63 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 cated from the f
8c110 69 72 73 74 20 61 76 61 69 6c 61 62 6c 65 20 66 irst available f
8c120 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a ree block..**.**
8c130 20 54 68 69 73 20 61 6c 67 6f 72 69 74 68 6d 20 This algorithm
8c140 69 73 20 64 65 73 63 72 69 62 65 64 20 69 6e 3a is described in:
8c150 20 4a 2e 20 4d 2e 20 52 6f 62 73 6f 6e 2e 20 22 J. M. Robson. "
8c160 42 6f 75 6e 64 73 20 66 6f 72 20 53 6f 6d 65 20 Bounds for Some
8c170 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 43 6f 6e Functions.** Con
8c180 63 65 72 6e 69 6e 67 20 44 79 6e 61 6d 69 63 20 cerning Dynamic
8c190 53 74 6f 72 61 67 65 20 41 6c 6c 6f 63 61 74 69 Storage Allocati
8c1a0 6f 6e 22 2e 20 4a 6f 75 72 6e 61 6c 20 6f 66 20 on". Journal of
8c1b0 74 68 65 20 41 73 73 6f 63 69 61 74 69 6f 6e 20 the Association
8c1c0 66 6f 72 0a 2a 2a 20 43 6f 6d 70 75 74 69 6e 67 for.** Computing
8c1d0 20 4d 61 63 68 69 6e 65 72 79 2c 20 56 6f 6c 75 Machinery, Volu
8c1e0 6d 65 20 32 31 2c 20 4e 75 6d 62 65 72 20 38 2c me 21, Number 8,
8c1f0 20 4a 75 6c 79 20 31 39 37 34 2c 20 70 61 67 65 July 1974, page
8c200 73 20 34 39 31 2d 34 39 39 2e 0a 2a 2a 20 0a 2a s 491-499..** .*
8c210 2a 20 4c 65 74 20 6e 20 62 65 20 74 68 65 20 73 * Let n be the s
8c220 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 ize of the large
8c230 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 69 st allocation di
8c240 76 69 64 65 64 20 62 79 20 74 68 65 20 6d 69 6e vided by the min
8c250 69 6d 75 6d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 imum.** allocati
8c260 6f 6e 20 73 69 7a 65 20 28 61 66 74 65 72 20 72 on size (after r
8c270 6f 75 6e 64 69 6e 67 20 61 6c 6c 20 73 69 7a 65 ounding all size
8c280 73 20 75 70 20 74 6f 20 61 20 70 6f 77 65 72 20 s up to a power
8c290 6f 66 20 32 2e 29 20 20 4c 65 74 20 4d 0a 2a 2a of 2.) Let M.**
8c2a0 20 62 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 be the maximum
8c2b0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
8c2c0 20 65 76 65 72 20 6f 75 74 73 74 61 6e 64 69 6e ever outstandin
8c2d0 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 g at one time.
8c2e0 4c 65 74 0a 2a 2a 20 4e 20 62 65 20 74 68 65 20 Let.** N be the
8c2f0 74 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 total amount of
8c300 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 memory available
8c310 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e for allocation.
8c320 20 20 52 6f 62 73 6f 6e 0a 2a 2a 20 70 72 6f 76 Robson.** prov
8c330 65 64 20 74 68 61 74 20 74 68 69 73 20 6d 65 6d ed that this mem
8c340 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 77 69 ory allocator wi
8c350 6c 6c 20 6e 65 76 65 72 20 62 72 65 61 6b 64 6f ll never breakdo
8c360 77 6e 20 64 75 65 20 74 6f 20 0a 2a 2a 20 66 72 wn due to .** fr
8c370 61 67 6d 65 6e 74 61 74 69 6f 6e 20 61 73 20 6c agmentation as l
8c380 6f 6e 67 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f ong as the follo
8c390 77 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 wing constraint
8c3a0 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 holds:.**.**
8c3b0 20 20 4e 20 3e 3d 20 20 4d 2a 28 31 20 2b 20 6c N >= M*(1 + l
8c3c0 6f 67 32 28 6e 29 2f 32 29 20 2d 20 6e 20 2b 20 og2(n)/2) - n +
8c3d0 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 1.**.** The sqli
8c3e0 74 65 33 5f 73 74 61 74 75 73 28 29 20 6c 6f 67 te3_status() log
8c3f0 69 63 20 74 72 61 63 6b 73 20 74 68 65 20 6d 61 ic tracks the ma
8c400 78 69 6d 75 6d 20 76 61 6c 75 65 73 20 6f 66 20 ximum values of
8c410 6e 20 61 6e 64 20 4d 20 73 6f 0a 2a 2a 20 74 68 n and M so.** th
8c420 61 74 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f at an applicatio
8c430 6e 20 63 61 6e 2c 20 61 74 20 61 6e 79 20 74 69 n can, at any ti
8c440 6d 65 2c 20 76 65 72 69 66 79 20 74 68 69 73 20 me, verify this
8c450 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 0a constraint..*/..
8c460 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 /*.** This versi
8c470 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 on of the memory
8c480 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 allocator is us
8c490 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a ed only when .**
8c4a0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
8c4b0 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 6e 65 EMSYS5 is define
8c4c0 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c d..*/.#ifdef SQL
8c4d0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 ITE_ENABLE_MEMSY
8c4e0 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 S5../*.** A mini
8c4f0 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 mum allocation i
8c500 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
8c510 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
8c520 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 tructure..** Lar
8c530 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 ger allocations
8c540 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 are an array of
8c550 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73 these structures
8c560 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 73 69 where the.** si
8c570 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 ze of the array
8c580 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e is a power of 2.
8c590 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 .**.** The size
8c5a0 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 6d of this object m
8c5b0 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f ust be a power o
8c5c0 66 20 74 77 6f 2e 20 20 54 68 61 74 20 66 61 63 f two. That fac
8c5d0 74 20 69 73 0a 2a 2a 20 76 65 72 69 66 69 65 64 t is.** verified
8c5e0 20 69 6e 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 in memsys5Init(
8c5f0 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 )..*/.typedef st
8c600 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65 ruct Mem5Link Me
8c610 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d m5Link;.struct M
8c620 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20 em5Link {. int
8c630 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 next; /* I
8c640 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65 ndex of next fre
8c650 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74 e chunk */. int
8c660 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 prev; /*
8c670 49 6e 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75 Index of previou
8c680 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a s free chunk */.
8c690 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 };../*.** Maximu
8c6a0 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c m size of any al
8c6b0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c location is ((1<
8c6c0 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 73 7a <LOGMAX)*mem5.sz
8c6d0 41 74 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20 Atom). Since.**
8c6e0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 69 73 20 61 mem5.szAtom is a
8c6f0 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 38 lways at least 8
8c700 20 61 6e 64 20 33 32 2d 62 69 74 20 69 6e 74 65 and 32-bit inte
8c710 67 65 72 73 20 61 72 65 20 75 73 65 64 2c 0a 2a gers are used,.*
8c720 2a 20 69 74 20 69 73 20 6e 6f 74 20 61 63 74 75 * it is not actu
8c730 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 20 74 6f ally possible to
8c740 20 72 65 61 63 68 20 74 68 69 73 20 6c 69 6d 69 reach this limi
8c750 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 4f t..*/.#define LO
8c760 47 4d 41 58 20 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d GMAX 30../*.** M
8c770 61 73 6b 73 20 75 73 65 64 20 66 6f 72 20 6d 65 asks used for me
8c780 6d 35 2e 61 43 74 72 6c 5b 5d 20 65 6c 65 6d 65 m5.aCtrl[] eleme
8c790 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 nts..*/.#define
8c7a0 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 20 20 30 78 CTRL_LOGSIZE 0x
8c7b0 31 66 20 20 20 20 2f 2a 20 4c 6f 67 32 20 53 69 1f /* Log2 Si
8c7c0 7a 65 20 6f 66 20 74 68 69 73 20 62 6c 6f 63 6b ze of this block
8c7d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c */.#define CTRL
8c7e0 5f 46 52 45 45 20 20 20 20 20 30 78 32 30 20 20 _FREE 0x20
8c7f0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74 /* True if not
8c800 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
8c810 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ./*.** All of th
8c820 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c e static variabl
8c830 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 es used by this
8c840 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 module are colle
8c850 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 cted.** into a s
8c860 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 ingle structure
8c870 6e 61 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54 named "mem5". T
8c880 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 his is to keep t
8c890 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 he.** static var
8c8a0 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 iables organized
8c8b0 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e and to reduce n
8c8c0 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 amespace polluti
8c8d0 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 on.** when this
8c8e0 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e module is combin
8c8f0 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e ed with other in
8c900 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f the amalgamatio
8c910 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c n..*/.static SQL
8c920 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d ITE_WSD struct M
8c930 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a em5Global {. /*
8c940 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61 . ** Memory ava
8c950 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 ilable for alloc
8c960 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74 ation. */. int
8c970 20 73 7a 41 74 6f 6d 3b 20 20 20 20 20 20 2f 2a szAtom; /*
8c980 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62 Smallest possib
8c990 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e le allocation in
8c9a0 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 bytes */. int
8c9b0 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 nBlock; /*
8c9c0 4e 75 6d 62 65 72 20 6f 66 20 73 7a 41 74 6f 6d Number of szAtom
8c9d0 20 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e sized blocks in
8c9e0 20 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a zPool */. u8 *
8c9f0 7a 50 6f 6f 6c 3b 20 20 20 20 20 20 20 2f 2a 20 zPool; /*
8ca00 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 Memory available
8ca10 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 to be allocated
8ca20 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a */. . /*. **
8ca30 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f Mutex to contro
8ca40 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 l access to the
8ca50 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
8ca60 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a n subsystem.. *
8ca70 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 /. sqlite3_mute
8ca80 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a x *mutex;.. /*.
8ca90 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 ** Performance
8caa0 20 73 74 61 74 69 73 74 69 63 73 0a 20 20 2a 2f statistics. */
8cab0 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 . u64 nAlloc;
8cac0 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 /* Total
8cad0 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 number of calls
8cae0 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 to malloc */. u
8caf0 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 64 totalAlloc;
8cb00 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61 /* Total of a
8cb10 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 ll malloc calls
8cb20 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74 65 72 - includes inter
8cb30 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20 75 36 nal frag */. u6
8cb40 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b 20 20 4 totalExcess;
8cb50 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72 /* Total inter
8cb60 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f nal fragmentatio
8cb70 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65 n */. u32 curre
8cb80 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20 43 75 ntOut; /* Cu
8cb90 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20 rrent checkout,
8cba0 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e including intern
8cbb0 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e al fragmentation
8cbc0 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e */. u32 curren
8cbd0 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72 tCount; /* Cur
8cbe0 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 rent number of d
8cbf0 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f 75 74 istinct checkout
8cc00 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75 s */. u32 maxOu
8cc10 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 t; /* Ma
8cc20 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 ximum instantane
8cc30 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74 20 2a ous currentOut *
8cc40 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75 6e 74 /. u32 maxCount
8cc50 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d ; /* Maxim
8cc60 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 um instantaneous
8cc70 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f currentCount */
8cc80 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75 65 73 . u32 maxReques
8cc90 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 t; /* Larges
8cca0 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78 t allocation (ex
8ccb0 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74 65 72 clusive of inter
8ccc0 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a nal frag) */. .
8ccd0 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20 /*. ** Lists
8cce0 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 2e 20 of free blocks.
8ccf0 20 61 69 46 72 65 65 6c 69 73 74 5b 30 5d 20 69 aiFreelist[0] i
8cd00 73 20 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 s a list of free
8cd10 20 62 6c 6f 63 6b 73 20 6f 66 0a 20 20 2a 2a 20 blocks of. **
8cd20 73 69 7a 65 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d size mem5.szAtom
8cd30 2e 20 20 61 69 46 72 65 65 6c 69 73 74 5b 31 5d . aiFreelist[1]
8cd40 20 68 6f 6c 64 73 20 62 6c 6f 63 6b 73 20 6f 66 holds blocks of
8cd50 20 73 69 7a 65 20 73 7a 41 74 6f 6d 2a 32 2e 0a size szAtom*2..
8cd60 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 6f 72 74 ** and so fort
8cd70 68 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69 h.. */. int ai
8cd80 46 72 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b Freelist[LOGMAX+
8cd90 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 1];.. /*. ** S
8cda0 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e pace for trackin
8cdb0 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61 g which blocks a
8cdc0 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 re checked out a
8cdd0 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a nd the size. **
8cde0 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20 of each block.
8cdf0 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c One byte per bl
8ce00 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a ock.. */. u8 *
8ce10 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d aCtrl;..} mem5 =
8ce20 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 { 0 };../*.** A
8ce30 63 63 65 73 73 20 74 68 65 20 73 74 61 74 69 63 ccess the static
8ce40 20 76 61 72 69 61 62 6c 65 20 74 68 72 6f 75 67 variable throug
8ce50 68 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 53 51 h a macro for SQ
8ce60 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 2a 2f LITE_OMIT_WSD.*/
8ce70 0a 23 64 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c .#define mem5 GL
8ce80 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 35 OBAL(struct Mem5
8ce90 47 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 2f Global, mem5)../
8cea0 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 6d 65 *.** Assuming me
8ceb0 6d 35 2e 7a 50 6f 6f 6c 20 69 73 20 64 69 76 69 m5.zPool is divi
8cec0 64 65 64 20 75 70 20 69 6e 74 6f 20 61 6e 20 61 ded up into an a
8ced0 72 72 61 79 20 6f 66 20 4d 65 6d 35 4c 69 6e 6b rray of Mem5Link
8cee0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2c 20 .** structures,
8cef0 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
8cf00 20 74 6f 20 74 68 65 20 69 64 78 2d 74 68 20 73 to the idx-th s
8cf10 75 63 68 20 6c 69 6b 2e 0a 2a 2f 0a 23 64 65 66 uch lik..*/.#def
8cf20 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 64 78 ine MEM5LINK(idx
8cf30 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28 ) ((Mem5Link *)(
8cf40 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78 &mem5.zPool[(idx
8cf50 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d 29 29 )*mem5.szAtom]))
8cf60 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 ../*.** Unlink t
8cf70 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 he chunk at mem5
8cf80 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c .aPool[i] from l
8cf90 69 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e ist it is curren
8cfa0 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 tly.** on. It s
8cfb0 68 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 6f hould be found o
8cfc0 6e 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 n mem5.aiFreelis
8cfd0 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a t[iLogsize]..*/.
8cfe0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
8cff0 79 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c ys5Unlink(int i,
8d000 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a int iLogsize){.
8d010 20 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 76 int next, prev
8d020 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 ;. assert( i>=0
8d030 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 && i<mem5.nBloc
8d040 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 k );. assert( i
8d050 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c Logsize>=0 && iL
8d060 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 ogsize<=LOGMAX )
8d070 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d ;. assert( (mem
8d080 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 5.aCtrl[i] & CTR
8d090 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 L_LOGSIZE)==iLog
8d0a0 73 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74 20 size );.. next
8d0b0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e = MEM5LINK(i)->n
8d0c0 65 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d 45 ext;. prev = ME
8d0d0 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 3b M5LINK(i)->prev;
8d0e0 0a 20 20 69 66 28 20 70 72 65 76 3c 30 20 29 7b . if( prev<0 ){
8d0f0 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 . mem5.aiFree
8d100 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d list[iLogsize] =
8d110 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a next;. }else{.
8d120 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72 65 MEM5LINK(pre
8d130 76 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 3b v)->next = next;
8d140 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 3e . }. if( next>
8d150 3d 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 =0 ){. MEM5LI
8d160 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 20 3d NK(next)->prev =
8d170 20 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a prev;. }.}../*
8d180 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75 .** Link the chu
8d190 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c nk at mem5.aPool
8d1a0 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f [i] so that is o
8d1b0 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a 2a n the iLogsize.*
8d1c0 2a 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f 0a * free list..*/.
8d1d0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
8d1e0 79 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 ys5Link(int i, i
8d1f0 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 nt iLogsize){.
8d200 69 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 74 28 int x;. assert(
8d210 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
8d220 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29 20 eld(mem5.mutex)
8d230 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d );. assert( i>=
8d240 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 0 && i<mem5.nBlo
8d250 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ck );. assert(
8d260 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 iLogsize>=0 && i
8d270 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 Logsize<=LOGMAX
8d280 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 );. assert( (me
8d290 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 m5.aCtrl[i] & CT
8d2a0 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f RL_LOGSIZE)==iLo
8d2b0 67 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d 20 gsize );.. x =
8d2c0 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 MEM5LINK(i)->nex
8d2d0 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c t = mem5.aiFreel
8d2e0 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 ist[iLogsize];.
8d2f0 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 MEM5LINK(i)->pr
8d300 65 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 78 ev = -1;. if( x
8d310 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 >=0 ){. asser
8d320 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b t( x<mem5.nBlock
8d330 20 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b );. MEM5LINK
8d340 28 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a 20 (x)->prev = i;.
8d350 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 }. mem5.aiFree
8d360 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d list[iLogsize] =
8d370 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 i;.}../*.** If
8d380 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d the STATIC_MEM m
8d390 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 65 utex is not alre
8d3a0 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 6e ady held, obtain
8d3b0 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 74 it now. The mut
8d3c0 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 61 ex.** will alrea
8d3d0 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 61 dy be held (obta
8d3e0 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 ined by code in
8d3f0 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 malloc.c) if.**
8d400 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
8d410 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 20 fig.bMemStat is
8d420 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 true..*/.static
8d430 76 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 65 void memsys5Ente
8d440 72 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 r(void){. sqlit
8d450 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
8d460 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 em5.mutex);.}.st
8d470 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
8d480 35 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 5Leave(void){.
8d490 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
8d4a0 61 76 65 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b ave(mem5.mutex);
8d4b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
8d4c0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 the size of an
8d4d0 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f outstanding allo
8d4e0 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73 cation, in bytes
8d4f0 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72 . The.** size r
8d500 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68 eturned omits th
8d510 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 e 8-byte header
8d520 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 20 overhead. This
8d530 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f only.** works fo
8d540 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72 r chunks that ar
8d550 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 e currently chec
8d560 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 ked out..*/.stat
8d570 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 53 69 ic int memsys5Si
8d580 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 ze(void *p){. i
8d590 6e 74 20 69 53 69 7a 65 20 3d 20 30 3b 0a 20 20 nt iSize = 0;.
8d5a0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 if( p ){. int
8d5b0 20 69 20 3d 20 28 28 75 38 20 2a 29 70 2d 6d 65 i = ((u8 *)p-me
8d5c0 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 73 m5.zPool)/mem5.s
8d5d0 7a 41 74 6f 6d 3b 0a 20 20 20 20 61 73 73 65 72 zAtom;. asser
8d5e0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d t( i>=0 && i<mem
8d5f0 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 5.nBlock );.
8d600 69 53 69 7a 65 20 3d 20 6d 65 6d 35 2e 73 7a 41 iSize = mem5.szA
8d610 74 6f 6d 20 2a 20 28 31 20 3c 3c 20 28 6d 65 6d tom * (1 << (mem
8d620 35 2e 61 43 74 72 6c 5b 69 5d 26 43 54 52 4c 5f 5.aCtrl[i]&CTRL_
8d630 4c 4f 47 53 49 5a 45 29 29 3b 0a 20 20 7d 0a 20 LOGSIZE));. }.
8d640 20 72 65 74 75 72 6e 20 69 53 69 7a 65 3b 0a 7d return iSize;.}
8d650 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 ../*.** Find the
8d660 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f 6e 20 first entry on
8d670 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 4c 6f the freelist iLo
8d680 67 73 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b 20 74 gsize. Unlink t
8d690 68 61 74 0a 2a 2a 20 65 6e 74 72 79 20 61 6e 64 hat.** entry and
8d6a0 20 72 65 74 75 72 6e 20 69 74 73 20 69 6e 64 65 return its inde
8d6b0 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e x. .*/.static in
8d6c0 74 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 t memsys5UnlinkF
8d6d0 69 72 73 74 28 69 6e 74 20 69 4c 6f 67 73 69 7a irst(int iLogsiz
8d6e0 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 e){. int i;. i
8d6f0 6e 74 20 69 46 69 72 73 74 3b 0a 0a 20 20 61 73 nt iFirst;.. as
8d700 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d sert( iLogsize>=
8d710 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 0 && iLogsize<=L
8d720 4f 47 4d 41 58 20 29 3b 0a 20 20 69 20 3d 20 69 OGMAX );. i = i
8d730 46 69 72 73 74 20 3d 20 6d 65 6d 35 2e 61 69 46 First = mem5.aiF
8d740 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 reelist[iLogsize
8d750 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 46 69 ];. assert( iFi
8d760 72 73 74 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c rst>=0 );. whil
8d770 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 69 66 e( i>0 ){. if
8d780 28 20 69 3c 69 46 69 72 73 74 20 29 20 69 46 69 ( i<iFirst ) iFi
8d790 72 73 74 20 3d 20 69 3b 0a 20 20 20 20 69 20 3d rst = i;. i =
8d7a0 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 MEM5LINK(i)->ne
8d7b0 78 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 xt;. }. memsys
8d7c0 35 55 6e 6c 69 6e 6b 28 69 46 69 72 73 74 2c 20 5Unlink(iFirst,
8d7d0 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 72 65 74 iLogsize);. ret
8d7e0 75 72 6e 20 69 46 69 72 73 74 3b 0a 7d 0a 0a 2f urn iFirst;.}../
8d7f0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c *.** Return a bl
8d800 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 ock of memory of
8d810 20 61 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 at least nBytes
8d820 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 in size..** Ret
8d830 75 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 urn NULL if unab
8d840 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c le. Return NULL
8d850 20 69 66 20 6e 42 79 74 65 73 3d 3d 30 2e 0a 2a if nBytes==0..*
8d860 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 *.** The caller
8d870 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 guarantees that
8d880 6e 42 79 74 65 20 70 6f 73 69 74 69 76 65 2e 0a nByte positive..
8d890 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 **.** The caller
8d8a0 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 61 20 has obtained a
8d8b0 6d 75 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69 mutex prior to i
8d8c0 6e 76 6f 6b 69 6e 67 20 74 68 69 73 0a 2a 2a 20 nvoking this.**
8d8d0 72 6f 75 74 69 6e 65 20 73 6f 20 74 68 65 72 65 routine so there
8d8e0 20 69 73 20 6e 65 76 65 72 20 61 6e 79 20 63 68 is never any ch
8d8f0 61 6e 63 65 20 74 68 61 74 20 74 77 6f 20 6f 72 ance that two or
8d900 20 6d 6f 72 65 0a 2a 2a 20 74 68 72 65 61 64 73 more.** threads
8d910 20 63 61 6e 20 62 65 20 69 6e 20 74 68 69 73 20 can be in this
8d920 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65 20 73 routine at the s
8d930 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 ame time..*/.sta
8d940 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 tic void *memsys
8d950 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e 5MallocUnsafe(in
8d960 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 t nByte){. int
8d970 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 i; /*
8d980 49 6e 64 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e Index of a mem5.
8d990 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0a aPool[] slot */.
8d9a0 20 20 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20 int iBin;
8d9b0 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f /* Index into
8d9c0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
8d9d0 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 46 75 6c [] */. int iFul
8d9e0 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 lSz; /* Size
8d9f0 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 of allocation r
8da00 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77 ounded up to pow
8da10 65 72 20 6f 66 20 32 20 2a 2f 0a 20 20 69 6e 74 er of 2 */. int
8da20 20 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a iLogsize; /*
8da30 20 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a Log2 of iFullSz
8da40 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 /POW2_MIN */..
8da50 2f 2a 20 6e 42 79 74 65 20 6d 75 73 74 20 62 65 /* nByte must be
8da60 20 61 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 a positive */.
8da70 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 assert( nByte>0
8da80 20 29 3b 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 74 );.. /* Keep t
8da90 72 61 63 6b 20 6f 66 20 74 68 65 20 6d 61 78 69 rack of the maxi
8daa0 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 mum allocation r
8dab0 65 71 75 65 73 74 2e 20 20 45 76 65 6e 20 75 6e equest. Even un
8dac0 66 75 6c 66 69 6c 6c 65 64 0a 20 20 2a 2a 20 72 fulfilled. ** r
8dad0 65 71 75 65 73 74 73 20 61 72 65 20 63 6f 75 6e equests are coun
8dae0 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 28 75 33 ted */. if( (u3
8daf0 32 29 6e 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78 2)nByte>mem5.max
8db00 52 65 71 75 65 73 74 20 29 7b 0a 20 20 20 20 6d Request ){. m
8db10 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 3d em5.maxRequest =
8db20 20 6e 42 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f nByte;. }.. /
8db30 2a 20 41 62 6f 72 74 20 69 66 20 74 68 65 20 72 * Abort if the r
8db40 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 equested allocat
8db50 69 6f 6e 20 73 69 7a 65 20 69 73 20 6c 61 72 67 ion size is larg
8db60 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 er than the larg
8db70 65 73 74 0a 20 20 2a 2a 20 70 6f 77 65 72 20 6f est. ** power o
8db80 66 20 74 77 6f 20 74 68 61 74 20 77 65 20 63 61 f two that we ca
8db90 6e 20 72 65 70 72 65 73 65 6e 74 20 75 73 69 6e n represent usin
8dba0 67 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 g 32-bit signed
8dbb0 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 integers.. */.
8dbc0 20 69 66 28 20 6e 42 79 74 65 20 3e 20 30 78 34 if( nByte > 0x4
8dbd0 30 30 30 30 30 30 30 20 29 7b 0a 20 20 20 20 72 0000000 ){. r
8dbe0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 eturn 0;. }..
8dbf0 2f 2a 20 52 6f 75 6e 64 20 6e 42 79 74 65 20 75 /* Round nByte u
8dc00 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 p to the next va
8dc10 6c 69 64 20 70 6f 77 65 72 20 6f 66 20 74 77 6f lid power of two
8dc20 20 2a 2f 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 */. for(iFullS
8dc30 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2c 20 69 z=mem5.szAtom, i
8dc40 4c 6f 67 73 69 7a 65 3d 30 3b 20 69 46 75 6c 6c Logsize=0; iFull
8dc50 53 7a 3c 6e 42 79 74 65 3b 20 69 46 75 6c 6c 53 Sz<nByte; iFullS
8dc60 7a 20 2a 3d 20 32 2c 20 69 4c 6f 67 73 69 7a 65 z *= 2, iLogsize
8dc70 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ++){}.. /* Make
8dc80 20 73 75 72 65 20 6d 65 6d 35 2e 61 69 46 72 65 sure mem5.aiFre
8dc90 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 elist[iLogsize]
8dca0 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 contains at leas
8dcb0 74 20 6f 6e 65 20 66 72 65 65 0a 20 20 2a 2a 20 t one free. **
8dcc0 62 6c 6f 63 6b 2e 20 20 49 66 20 6e 6f 74 2c 20 block. If not,
8dcd0 74 68 65 6e 20 73 70 6c 69 74 20 61 20 62 6c 6f then split a blo
8dce0 63 6b 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6c ck of the next l
8dcf0 61 72 67 65 72 20 70 6f 77 65 72 20 6f 66 0a 20 arger power of.
8dd00 20 2a 2a 20 74 77 6f 20 69 6e 20 6f 72 64 65 72 ** two in order
8dd10 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 to create a new
8dd20 20 66 72 65 65 20 62 6c 6f 63 6b 20 6f 66 20 73 free block of s
8dd30 69 7a 65 20 69 4c 6f 67 73 69 7a 65 2e 0a 20 20 ize iLogsize..
8dd40 2a 2f 0a 20 20 66 6f 72 28 69 42 69 6e 3d 69 4c */. for(iBin=iL
8dd50 6f 67 73 69 7a 65 3b 20 6d 65 6d 35 2e 61 69 46 ogsize; mem5.aiF
8dd60 72 65 65 6c 69 73 74 5b 69 42 69 6e 5d 3c 30 20 reelist[iBin]<0
8dd70 26 26 20 69 42 69 6e 3c 3d 4c 4f 47 4d 41 58 3b && iBin<=LOGMAX;
8dd80 20 69 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 iBin++){}. if(
8dd90 20 69 42 69 6e 3e 4c 4f 47 4d 41 58 20 29 20 72 iBin>LOGMAX ) r
8dda0 65 74 75 72 6e 20 30 3b 0a 20 20 69 20 3d 20 6d eturn 0;. i = m
8ddb0 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73 emsys5UnlinkFirs
8ddc0 74 28 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65 t(iBin);. while
8ddd0 28 20 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20 ( iBin>iLogsize
8dde0 29 7b 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69 ){. int newSi
8ddf0 7a 65 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b ze;.. iBin--;
8de00 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31 . newSize = 1
8de10 20 3c 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 << iBin;. me
8de20 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69 m5.aCtrl[i+newSi
8de30 7a 65 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 ze] = CTRL_FREE
8de40 7c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73 | iBin;. mems
8de50 79 73 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a ys5Link(i+newSiz
8de60 65 2c 20 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20 e, iBin);. }.
8de70 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 3d 20 mem5.aCtrl[i] =
8de80 69 4c 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 iLogsize;.. /*
8de90 55 70 64 61 74 65 20 61 6c 6c 6f 63 61 74 6f 72 Update allocator
8dea0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 performance sta
8deb0 74 69 73 74 69 63 73 2e 20 2a 2f 0a 20 20 6d 65 tistics. */. me
8dec0 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d m5.nAlloc++;. m
8ded0 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 2b em5.totalAlloc +
8dee0 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d 65 6d = iFullSz;. mem
8def0 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 2b 3d 5.totalExcess +=
8df00 20 69 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 74 65 iFullSz - nByte
8df10 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 ;. mem5.current
8df20 43 6f 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d 35 2e Count++;. mem5.
8df30 63 75 72 72 65 6e 74 4f 75 74 20 2b 3d 20 69 46 currentOut += iF
8df40 75 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6d 65 6d ullSz;. if( mem
8df50 35 2e 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d 35 2e 5.maxCount<mem5.
8df60 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 29 20 6d currentCount ) m
8df70 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 3d 20 6d em5.maxCount = m
8df80 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
8df90 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 ;. if( mem5.max
8dfa0 4f 75 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 Out<mem5.current
8dfb0 4f 75 74 20 29 20 6d 65 6d 35 2e 6d 61 78 4f 75 Out ) mem5.maxOu
8dfc0 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 t = mem5.current
8dfd0 4f 75 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 Out;.. /* Retur
8dfe0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
8dff0 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d he allocated mem
8e000 6f 72 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e ory. */. return
8e010 20 28 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50 (void*)&mem5.zP
8e020 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41 74 6f ool[i*mem5.szAto
8e030 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 m];.}../*.** Fre
8e040 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 e an outstanding
8e050 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
8e060 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f on..*/.static vo
8e070 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e id memsys5FreeUn
8e080 73 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29 safe(void *pOld)
8e090 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 69 4c {. u32 size, iL
8e0a0 6f 67 73 69 7a 65 3b 0a 20 20 69 6e 74 20 69 42 ogsize;. int iB
8e0b0 6c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 lock;.. /* Set
8e0c0 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e iBlock to the in
8e0d0 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b dex of the block
8e0e0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
8e0f0 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 Old in . ** the
8e100 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35 2e 73 array of mem5.s
8e110 7a 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 6b zAtom byte block
8e120 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 s pointed to by
8e130 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a 2f mem5.zPool.. */
8e140 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 38 . iBlock = ((u8
8e150 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f *)pOld-mem5.zPo
8e160 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b ol)/mem5.szAtom;
8e170 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
8e180 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f t the pointer pO
8e190 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 ld points to a v
8e1a0 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62 alid, non-free b
8e1b0 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72 lock. */. asser
8e1c0 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20 t( iBlock>=0 &&
8e1d0 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f iBlock<mem5.nBlo
8e1e0 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ck );. assert(
8e1f0 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 ((u8 *)pOld-mem5
8e200 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73 7a 41 .zPool)%mem5.szA
8e210 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 tom==0 );. asse
8e220 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b rt( (mem5.aCtrl[
8e230 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46 iBlock] & CTRL_F
8e240 52 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c REE)==0 );.. iL
8e250 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43 ogsize = mem5.aC
8e260 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 trl[iBlock] & CT
8e270 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 RL_LOGSIZE;. si
8e280 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 ze = 1<<iLogsize
8e290 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f ;. assert( iBlo
8e2a0 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d ck+size-1<(u32)m
8e2b0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20 em5.nBlock );..
8e2c0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8e2d0 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 ck] |= CTRL_FREE
8e2e0 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 ;. mem5.aCtrl[i
8e2f0 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d Block+size-1] |=
8e300 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73 CTRL_FREE;. as
8e310 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
8e320 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61 ntCount>0 );. a
8e330 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 ssert( mem5.curr
8e340 65 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65 entOut>=(size*me
8e350 6d 35 2e 73 7a 41 74 6f 6d 29 20 29 3b 0a 20 20 m5.szAtom) );.
8e360 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e mem5.currentCoun
8e370 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 t--;. mem5.curr
8e380 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d entOut -= size*m
8e390 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 61 73 em5.szAtom;. as
8e3a0 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
8e3b0 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e ntOut>0 || mem5.
8e3c0 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 currentCount==0
8e3d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d );. assert( mem
8e3e0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 5.currentCount>0
8e3f0 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 || mem5.current
8e400 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d Out==0 );.. mem
8e410 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
8e420 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c = CTRL_FREE | iL
8e430 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28 ogsize;. while(
8e440 20 41 4c 57 41 59 53 28 69 4c 6f 67 73 69 7a 65 ALWAYS(iLogsize
8e450 3c 4c 4f 47 4d 41 58 29 20 29 7b 0a 20 20 20 20 <LOGMAX) ){.
8e460 69 6e 74 20 69 42 75 64 64 79 3b 0a 20 20 20 20 int iBuddy;.
8e470 69 66 28 20 28 69 42 6c 6f 63 6b 3e 3e 69 4c 6f if( (iBlock>>iLo
8e480 67 73 69 7a 65 29 20 26 20 31 20 29 7b 0a 20 20 gsize) & 1 ){.
8e490 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c iBuddy = iBl
8e4a0 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a 20 20 20 20 ock - size;.
8e4b0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 42 75 }else{. iBu
8e4c0 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2b 20 73 ddy = iBlock + s
8e4d0 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 ize;. }. a
8e4e0 73 73 65 72 74 28 20 69 42 75 64 64 79 3e 3d 30 ssert( iBuddy>=0
8e4f0 20 29 3b 0a 20 20 20 20 69 66 28 20 28 69 42 75 );. if( (iBu
8e500 64 64 79 2b 28 31 3c 3c 69 4c 6f 67 73 69 7a 65 ddy+(1<<iLogsize
8e510 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 ))>mem5.nBlock )
8e520 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 break;. if(
8e530 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 mem5.aCtrl[iBudd
8e540 79 5d 21 3d 28 43 54 52 4c 5f 46 52 45 45 20 7c y]!=(CTRL_FREE |
8e550 20 69 4c 6f 67 73 69 7a 65 29 20 29 20 62 72 65 iLogsize) ) bre
8e560 61 6b 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 55 ak;. memsys5U
8e570 6e 6c 69 6e 6b 28 69 42 75 64 64 79 2c 20 69 4c nlink(iBuddy, iL
8e580 6f 67 73 69 7a 65 29 3b 0a 20 20 20 20 69 4c 6f ogsize);. iLo
8e590 67 73 69 7a 65 2b 2b 3b 0a 20 20 20 20 69 66 28 gsize++;. if(
8e5a0 20 69 42 75 64 64 79 3c 69 42 6c 6f 63 6b 20 29 iBuddy<iBlock )
8e5b0 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 {. mem5.aCt
8e5c0 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 43 54 52 rl[iBuddy] = CTR
8e5d0 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a L_FREE | iLogsiz
8e5e0 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 e;. mem5.aC
8e5f0 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 30 3b trl[iBlock] = 0;
8e600 0a 20 20 20 20 20 20 69 42 6c 6f 63 6b 20 3d 20 . iBlock =
8e610 69 42 75 64 64 79 3b 0a 20 20 20 20 7d 65 6c 73 iBuddy;. }els
8e620 65 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 e{. mem5.aC
8e630 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 trl[iBlock] = CT
8e640 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 RL_FREE | iLogsi
8e650 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 ze;. mem5.a
8e660 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 30 Ctrl[iBuddy] = 0
8e670 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69 7a 65 ;. }. size
8e680 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d 65 6d *= 2;. }. mem
8e690 73 79 73 35 4c 69 6e 6b 28 69 42 6c 6f 63 6b 2c sys5Link(iBlock,
8e6a0 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 7d 0a 0a 2f iLogsize);.}../
8e6b0 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 *.** Allocate nB
8e6c0 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a ytes of memory.*
8e6d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d /.static void *m
8e6e0 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 28 69 6e 74 emsys5Malloc(int
8e6f0 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 nBytes){. sqli
8e700 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 30 te3_int64 *p = 0
8e710 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 30 ;. if( nBytes>0
8e720 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35 45 ){. memsys5E
8e730 6e 74 65 72 28 29 3b 0a 20 20 20 20 70 20 3d 20 nter();. p =
8e740 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 memsys5MallocUns
8e750 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 afe(nBytes);.
8e760 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b memsys5Leave();
8e770 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 . }. return (v
8e780 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a oid*)p; .}../*.*
8e790 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a * Free memory..*
8e7a0 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 6c *.** The outer l
8e7b0 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ayer memory allo
8e7c0 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20 74 cator prevents t
8e7d0 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d his routine from
8e7e0 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 .** being called
8e7f0 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 2e with pPrior==0.
8e800 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8e810 6d 65 6d 73 79 73 35 46 72 65 65 28 76 6f 69 64 memsys5Free(void
8e820 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 *pPrior){. ass
8e830 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 ert( pPrior!=0 )
8e840 3b 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 ;. memsys5Enter
8e850 28 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65 ();. memsys5Fre
8e860 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b eUnsafe(pPrior);
8e870 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
8e880 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 ); .}../*.** Ch
8e890 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 ange the size of
8e8a0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d an existing mem
8e8b0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a ory allocation..
8e8c0 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 **.** The outer
8e8d0 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c layer memory all
8e8e0 6f 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20 ocator prevents
8e8f0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f this routine fro
8e900 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 m.** being calle
8e910 64 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 d with pPrior==0
8e920 2e 20 20 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 73 . .**.** nBytes
8e930 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c is always a val
8e940 75 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ue obtained from
8e950 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f a prior call to
8e960 0a 2a 2a 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 .** memsys5Round
8e970 28 29 2e 20 20 48 65 6e 63 65 20 6e 42 79 74 65 (). Hence nByte
8e980 73 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f s is always a no
8e990 6e 2d 6e 65 67 61 74 69 76 65 20 70 6f 77 65 72 n-negative power
8e9a0 0a 2a 2a 20 6f 66 20 74 77 6f 2e 20 20 49 66 20 .** of two. If
8e9b0 6e 42 79 74 65 73 3d 3d 30 20 74 68 61 74 20 6d nBytes==0 that m
8e9c0 65 61 6e 73 20 74 68 61 74 20 61 6e 20 6f 76 65 eans that an ove
8e9d0 72 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e rsize allocation
8e9e0 0a 2a 2a 20 28 61 6e 20 61 6c 6c 6f 63 61 74 69 .** (an allocati
8e9f0 6f 6e 20 6c 61 72 67 65 72 20 74 68 61 6e 20 30 on larger than 0
8ea00 78 34 30 30 30 30 30 30 30 29 20 77 61 73 20 72 x40000000) was r
8ea10 65 71 75 65 73 74 65 64 20 61 6e 64 20 74 68 69 equested and thi
8ea20 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 68 6f s.** routine sho
8ea30 75 6c 64 20 72 65 74 75 72 6e 20 30 20 77 69 74 uld return 0 wit
8ea40 68 6f 75 74 20 66 72 65 65 69 6e 67 20 70 50 72 hout freeing pPr
8ea50 69 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ior..*/.static v
8ea60 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52 65 61 6c oid *memsys5Real
8ea70 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 loc(void *pPrior
8ea80 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 , int nBytes){.
8ea90 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 int nOld;. voi
8eaa0 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 d *p;. assert(
8eab0 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 61 pPrior!=0 );. a
8eac0 73 73 65 72 74 28 20 28 6e 42 79 74 65 73 26 28 ssert( (nBytes&(
8ead0 6e 42 79 74 65 73 2d 31 29 29 3d 3d 30 20 29 3b nBytes-1))==0 );
8eae0 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
8eaf0 73 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 6e 42 s>=0 );. if( nB
8eb00 79 74 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 72 ytes==0 ){. r
8eb10 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
8eb20 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 35 53 69 7a Old = memsys5Siz
8eb30 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 e(pPrior);. if(
8eb40 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 29 7b nBytes<=nOld ){
8eb50 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 69 . return pPri
8eb60 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 or;. }. memsys
8eb70 35 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 5Enter();. p =
8eb80 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 memsys5MallocUns
8eb90 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 afe(nBytes);. i
8eba0 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 63 f( p ){. memc
8ebb0 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f py(p, pPrior, nO
8ebc0 6c 64 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 ld);. memsys5
8ebd0 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f FreeUnsafe(pPrio
8ebe0 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 r);. }. memsys
8ebf0 35 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75 5Leave();. retu
8ec00 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn p;.}../*.** R
8ec10 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 ound up a reques
8ec20 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 t size to the ne
8ec30 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 xt valid allocat
8ec40 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0a 2a 2a ion size. If.**
8ec50 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
8ec60 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 is too large to
8ec70 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 be handled by th
8ec80 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 79 is allocation sy
8ec90 73 74 65 6d 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 stem,.** return
8eca0 30 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 0..**.** All all
8ecb0 6f 63 61 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 ocations must be
8ecc0 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 a power of two
8ecd0 61 6e 64 20 6d 75 73 74 20 62 65 20 65 78 70 72 and must be expr
8ece0 65 73 73 65 64 20 62 79 20 61 0a 2a 2a 20 33 32 essed by a.** 32
8ecf0 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
8ed00 67 65 72 2e 20 20 48 65 6e 63 65 20 74 68 65 20 ger. Hence the
8ed10 6c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 largest allocati
8ed20 6f 6e 20 69 73 20 30 78 34 30 30 30 30 30 30 30 on is 0x40000000
8ed30 0a 2a 2a 20 6f 72 20 31 30 37 33 37 34 31 38 32 .** or 107374182
8ed40 34 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 4 bytes..*/.stat
8ed50 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 52 6f ic int memsys5Ro
8ed60 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 undup(int n){.
8ed70 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 int iFullSz;. i
8ed80 66 28 20 6e 20 3e 20 30 78 34 30 30 30 30 30 30 f( n > 0x4000000
8ed90 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
8eda0 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 for(iFullSz=mem5
8edb0 2e 73 7a 41 74 6f 6d 3b 20 69 46 75 6c 6c 53 7a .szAtom; iFullSz
8edc0 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 <n; iFullSz *= 2
8edd0 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 75 6c );. return iFul
8ede0 6c 53 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 lSz;.}../*.** Re
8edf0 74 75 72 6e 20 74 68 65 20 63 65 69 6c 69 6e 67 turn the ceiling
8ee00 20 6f 66 20 74 68 65 20 6c 6f 67 61 72 69 74 68 of the logarith
8ee10 6d 20 62 61 73 65 20 32 20 6f 66 20 69 56 61 6c m base 2 of iVal
8ee20 75 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c ue..**.** Exampl
8ee30 65 73 3a 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 es: memsys5Log
8ee40 28 31 29 20 2d 3e 20 30 0a 2a 2a 20 20 20 20 20 (1) -> 0.**
8ee50 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c memsys5L
8ee60 6f 67 28 32 29 20 2d 3e 20 31 0a 2a 2a 20 20 20 og(2) -> 1.**
8ee70 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 memsys
8ee80 35 4c 6f 67 28 34 29 20 2d 3e 20 32 0a 2a 2a 20 5Log(4) -> 2.**
8ee90 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 mems
8eea0 79 73 35 4c 6f 67 28 35 29 20 2d 3e 20 33 0a 2a ys5Log(5) -> 3.*
8eeb0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 * me
8eec0 6d 73 79 73 35 4c 6f 67 28 38 29 20 2d 3e 20 33 msys5Log(8) -> 3
8eed0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
8eee0 6d 65 6d 73 79 73 35 4c 6f 67 28 39 29 20 2d 3e memsys5Log(9) ->
8eef0 20 34 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 4.*/.static int
8ef00 20 6d 65 6d 73 79 73 35 4c 6f 67 28 69 6e 74 20 memsys5Log(int
8ef10 69 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 69 iValue){. int i
8ef20 4c 6f 67 3b 0a 20 20 66 6f 72 28 69 4c 6f 67 3d Log;. for(iLog=
8ef30 30 3b 20 28 31 3c 3c 69 4c 6f 67 29 3c 69 56 61 0; (1<<iLog)<iVa
8ef40 6c 75 65 3b 20 69 4c 6f 67 2b 2b 29 3b 0a 20 20 lue; iLog++);.
8ef50 72 65 74 75 72 6e 20 69 4c 6f 67 3b 0a 7d 0a 0a return iLog;.}..
8ef60 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
8ef70 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
8ef80 63 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 cator..**.** Thi
8ef90 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 s routine is not
8efa0 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 threadsafe. Th
8efb0 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 62 65 e caller must be
8efc0 20 68 6f 6c 64 69 6e 67 20 61 20 6d 75 74 65 78 holding a mutex
8efd0 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 6d .** to prevent m
8efe0 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 20 ultiple threads
8eff0 66 72 6f 6d 20 65 6e 74 65 72 69 6e 67 20 61 74 from entering at
8f000 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a the same time..
8f010 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 */.static int me
8f020 6d 73 79 73 35 49 6e 69 74 28 76 6f 69 64 20 2a msys5Init(void *
8f030 4e 6f 74 55 73 65 64 29 7b 0a 20 20 69 6e 74 20 NotUsed){. int
8f040 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ii; /
8f050 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
8f060 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 /. int nByte;
8f070 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
8f080 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d of bytes of mem
8f090 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f ory available to
8f0a0 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 6f 72 20 this allocator
8f0b0 2a 2f 0a 20 20 75 38 20 2a 7a 42 79 74 65 3b 20 */. u8 *zByte;
8f0c0 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
8f0d0 79 20 75 73 61 62 6c 65 20 62 79 20 74 68 69 73 y usable by this
8f0e0 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 allocator */.
8f0f0 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20 int nMinLog;
8f100 20 20 20 2f 2a 20 4c 6f 67 20 62 61 73 65 20 32 /* Log base 2
8f110 20 6f 66 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f of minimum allo
8f120 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 6e 20 62 cation size in b
8f130 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f ytes */. int iO
8f140 66 66 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 ffset; /*
8f150 41 6e 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 6d An offset into m
8f160 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 2a 2f 0a 0a em5.aCtrl[] */..
8f170 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
8f180 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 ER(NotUsed);..
8f190 2f 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f /* For the purpo
8f1a0 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 ses of this rout
8f1b0 69 6e 65 2c 20 64 69 73 61 62 6c 65 20 74 68 65 ine, disable the
8f1c0 20 6d 75 74 65 78 20 2a 2f 0a 20 20 6d 65 6d 35 mutex */. mem5
8f1d0 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 0a 20 20 2f .mutex = 0;.. /
8f1e0 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20 * The size of a
8f1f0 4d 65 6d 35 4c 69 6e 6b 20 6f 62 6a 65 63 74 20 Mem5Link object
8f200 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 must be a power
8f210 6f 66 20 74 77 6f 2e 20 20 56 65 72 69 66 79 20 of two. Verify
8f220 74 68 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 69 that. ** this i
8f230 73 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 61 s case.. */. a
8f240 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 28 4d ssert( (sizeof(M
8f250 65 6d 35 4c 69 6e 6b 29 26 28 73 69 7a 65 6f 66 em5Link)&(sizeof
8f260 28 4d 65 6d 35 4c 69 6e 6b 29 2d 31 29 29 3d 3d (Mem5Link)-1))==
8f270 30 20 29 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 0 );.. nByte =
8f280 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
8f290 66 69 67 2e 6e 48 65 61 70 3b 0a 20 20 7a 42 79 fig.nHeap;. zBy
8f2a0 74 65 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 te = (u8*)sqlite
8f2b0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 3GlobalConfig.pH
8f2c0 65 61 70 3b 0a 20 20 61 73 73 65 72 74 28 20 7a eap;. assert( z
8f2d0 42 79 74 65 21 3d 30 20 29 3b 20 20 2f 2a 20 73 Byte!=0 ); /* s
8f2e0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
8f2f0 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 6f does not allow o
8f300 74 68 65 72 77 69 73 65 20 2a 2f 0a 0a 20 20 6e therwise */.. n
8f310 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 MinLog = memsys5
8f320 4c 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 Log(sqlite3Globa
8f330 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a lConfig.mnReq);.
8f340 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 mem5.szAtom =
8f350 28 31 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20 (1<<nMinLog);.
8f360 77 68 69 6c 65 28 20 28 69 6e 74 29 73 69 7a 65 while( (int)size
8f370 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d of(Mem5Link)>mem
8f380 35 2e 73 7a 41 74 6f 6d 20 29 7b 0a 20 20 20 20 5.szAtom ){.
8f390 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 6d 65 mem5.szAtom = me
8f3a0 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 31 3b 0a m5.szAtom << 1;.
8f3b0 20 20 7d 0a 0a 20 20 6d 65 6d 35 2e 6e 42 6c 6f }.. mem5.nBlo
8f3c0 63 6b 20 3d 20 28 6e 42 79 74 65 20 2f 20 28 6d ck = (nByte / (m
8f3d0 65 6d 35 2e 73 7a 41 74 6f 6d 2b 73 69 7a 65 6f em5.szAtom+sizeo
8f3e0 66 28 75 38 29 29 29 3b 0a 20 20 6d 65 6d 35 2e f(u8)));. mem5.
8f3f0 7a 50 6f 6f 6c 20 3d 20 7a 42 79 74 65 3b 0a 20 zPool = zByte;.
8f400 20 6d 65 6d 35 2e 61 43 74 72 6c 20 3d 20 28 75 mem5.aCtrl = (u
8f410 38 20 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 8 *)&mem5.zPool[
8f420 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 2a 6d 65 6d 35 mem5.nBlock*mem5
8f430 2e 73 7a 41 74 6f 6d 5d 3b 0a 0a 20 20 66 6f 72 .szAtom];.. for
8f440 28 69 69 3d 30 3b 20 69 69 3c 3d 4c 4f 47 4d 41 (ii=0; ii<=LOGMA
8f450 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 6d 65 X; ii++){. me
8f460 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 69 m5.aiFreelist[ii
8f470 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 69 ] = -1;. }.. i
8f480 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f Offset = 0;. fo
8f490 72 28 69 69 3d 4c 4f 47 4d 41 58 3b 20 69 69 3e r(ii=LOGMAX; ii>
8f4a0 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 20 20 20 69 =0; ii--){. i
8f4b0 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 28 31 3c 3c nt nAlloc = (1<<
8f4c0 69 69 29 3b 0a 20 20 20 20 69 66 28 20 28 69 4f ii);. if( (iO
8f4d0 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3c 3d 6d ffset+nAlloc)<=m
8f4e0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20 em5.nBlock ){.
8f4f0 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 mem5.aCtrl[i
8f500 4f 66 66 73 65 74 5d 20 3d 20 69 69 20 7c 20 43 Offset] = ii | C
8f510 54 52 4c 5f 46 52 45 45 3b 0a 20 20 20 20 20 20 TRL_FREE;.
8f520 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 4f 66 66 memsys5Link(iOff
8f530 73 65 74 2c 20 69 69 29 3b 0a 20 20 20 20 20 20 set, ii);.
8f540 69 4f 66 66 73 65 74 20 2b 3d 20 6e 41 6c 6c 6f iOffset += nAllo
8f550 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 c;. }. ass
8f560 65 72 74 28 28 69 4f 66 66 73 65 74 2b 6e 41 6c ert((iOffset+nAl
8f570 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b loc)>mem5.nBlock
8f580 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
8f590 61 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 a mutex is requi
8f5a0 72 65 64 20 66 6f 72 20 6e 6f 72 6d 61 6c 20 6f red for normal o
8f5b0 70 65 72 61 74 69 6f 6e 2c 20 61 6c 6c 6f 63 61 peration, alloca
8f5c0 74 65 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20 te one */. if(
8f5d0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
8f5e0 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20 fig.bMemstat==0
8f5f0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74 65 ){. mem5.mute
8f600 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 x = sqlite3Mutex
8f610 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
8f620 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a EX_STATIC_MEM);.
8f630 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 }.. return SQ
8f640 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
8f650 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 * Deinitialize t
8f660 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 his module..*/.s
8f670 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
8f680 73 35 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 s5Shutdown(void
8f690 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 *NotUsed){. UNU
8f6a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
8f6b0 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 35 2e 6d tUsed);. mem5.m
8f6c0 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65 74 75 utex = 0;. retu
8f6d0 72 6e 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 rn;.}..#ifdef SQ
8f6e0 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LITE_TEST./*.**
8f6f0 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e Open the file in
8f700 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 dicated and writ
8f710 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 e a log of all u
8f720 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a nfreed memory .*
8f730 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e * allocations in
8f740 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a to that log..*/.
8f750 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
8f760 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 oid sqlite3Memsy
8f770 73 35 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 s5Dump(const cha
8f780 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
8f790 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 69 6e FILE *out;. in
8f7a0 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 69 6e 74 t i, j, n;. int
8f7b0 20 6e 4d 69 6e 4c 6f 67 3b 0a 0a 20 20 69 66 28 nMinLog;.. if(
8f7c0 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c zFilename==0 ||
8f7d0 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 zFilename[0]==0
8f7e0 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 ){. out = st
8f7f0 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 dout;. }else{.
8f800 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a out = fopen(z
8f810 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a Filename, "w");.
8f820 20 20 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 if( out==0 )
8f830 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
8f840 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 stderr, "** Unab
8f850 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d le to output mem
8f860 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 ory debug output
8f870 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a log: %s **\n",.
8f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8f890 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 zFilename)
8f8a0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
8f8b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 }. }. mems
8f8c0 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6e 4d ys5Enter();. nM
8f8d0 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c inLog = memsys5L
8f8e0 6f 67 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 3b og(mem5.szAtom);
8f8f0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 4c . for(i=0; i<=L
8f900 4f 47 4d 41 58 20 26 26 20 69 2b 6e 4d 69 6e 4c OGMAX && i+nMinL
8f910 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 og<32; i++){.
8f920 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d 6d 65 6d 35 for(n=0, j=mem5
8f930 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 5d 3b 20 .aiFreelist[i];
8f940 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45 4d 35 4c 49 j>=0; j = MEM5LI
8f950 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c 20 6e 2b 2b NK(j)->next, n++
8f960 29 7b 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ){}. fprintf(
8f970 6f 75 74 2c 20 22 66 72 65 65 6c 69 73 74 20 69 out, "freelist i
8f980 74 65 6d 73 20 6f 66 20 73 69 7a 65 20 25 64 3a tems of size %d:
8f990 20 25 64 5c 6e 22 2c 20 6d 65 6d 35 2e 73 7a 41 %d\n", mem5.szA
8f9a0 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 3b 0a 20 20 tom << i, n);.
8f9b0 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c }. fprintf(out,
8f9c0 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 20 20 20 "mem5.nAlloc
8f9d0 20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d = %llu\n", m
8f9e0 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 66 em5.nAlloc);. f
8f9f0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d printf(out, "mem
8fa00 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 20 20 3d 5.totalAlloc =
8fa10 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 %llu\n", mem5.t
8fa20 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a 20 20 66 70 otalAlloc);. fp
8fa30 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 rintf(out, "mem5
8fa40 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 20 3d 20 .totalExcess =
8fa50 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f %llu\n", mem5.to
8fa60 74 61 6c 45 78 63 65 73 73 29 3b 0a 20 20 66 70 talExcess);. fp
8fa70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 rintf(out, "mem5
8fa80 2e 63 75 72 72 65 6e 74 4f 75 74 20 20 20 3d 20 .currentOut =
8fa90 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 %u\n", mem5.curr
8faa0 65 6e 74 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e entOut);. fprin
8fab0 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 75 tf(out, "mem5.cu
8fac0 72 72 65 6e 74 43 6f 75 6e 74 20 3d 20 25 75 5c rrentCount = %u\
8fad0 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 n", mem5.current
8fae0 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 Count);. fprint
8faf0 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 f(out, "mem5.max
8fb00 4f 75 74 20 20 20 20 20 20 20 3d 20 25 75 5c 6e Out = %u\n
8fb10 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 29 3b ", mem5.maxOut);
8fb20 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
8fb30 22 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 20 "mem5.maxCount
8fb40 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 = %u\n", mem5
8fb50 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a 20 20 66 70 .maxCount);. fp
8fb60 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 rintf(out, "mem5
8fb70 2e 6d 61 78 52 65 71 75 65 73 74 20 20 20 3d 20 .maxRequest =
8fb80 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 52 %u\n", mem5.maxR
8fb90 65 71 75 65 73 74 29 3b 0a 20 20 6d 65 6d 73 79 equest);. memsy
8fba0 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 69 66 28 s5Leave();. if(
8fbb0 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a out==stdout ){.
8fbc0 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 fflush(stdou
8fbd0 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
8fbe0 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 fclose(out);.
8fbf0 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a }.}.#endif../*.*
8fc00 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
8fc10 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 s the only routi
8fc20 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 ne in this file
8fc30 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a with external .*
8fc40 2a 20 6c 69 6e 6b 61 67 65 2e 20 49 74 20 72 65 * linkage. It re
8fc50 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
8fc60 74 6f 20 61 20 73 74 61 74 69 63 20 73 71 6c 69 to a static sqli
8fc70 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a te3_mem_methods.
8fc80 2a 2a 20 73 74 72 75 63 74 20 70 6f 70 75 6c 61 ** struct popula
8fc90 74 65 64 20 77 69 74 68 20 74 68 65 20 6d 65 6d ted with the mem
8fca0 73 79 73 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f sys5 methods..*/
8fcb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8fcc0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
8fcd0 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 m_methods *sqlit
8fce0 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 e3MemGetMemsys5(
8fcf0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
8fd00 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 const sqlite3_me
8fd10 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 6d 73 79 73 m_methods memsys
8fd20 35 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 5Methods = {.
8fd30 20 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c memsys5Malloc,
8fd40 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 46 72 65 . memsys5Fre
8fd50 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 e,. memsys5R
8fd60 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d ealloc,. mem
8fd70 73 79 73 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d sys5Size,. m
8fd80 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20 emsys5Roundup,.
8fd90 20 20 20 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c memsys5Init,
8fda0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 68 75 . memsys5Shu
8fdb0 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 tdown,. 0.
8fdc0 7d 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d };. return &mem
8fdd0 73 79 73 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a sys5Methods;.}..
8fde0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
8fdf0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 _ENABLE_MEMSYS5
8fe00 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
8fe10 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e *** End of mem5.
8fe20 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
8fe30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fe40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fe50 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
8fe60 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d *** Begin file m
8fe70 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utex.c *********
8fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fe90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fea0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 **/./*.** 2007 A
8feb0 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 ugust 14.**.** T
8fec0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
8fed0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
8fee0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
8fef0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
8ff00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
8ff10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
8ff20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
8ff30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
8ff40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
8ff50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
8ff60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
8ff70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
8ff80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
8ff90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
8ffa0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
8ffb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
8ffc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
8ffd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8ffe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8fff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90010 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
90020 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 file contains th
90030 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 e C functions th
90040 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 at implement mut
90050 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 exes..**.** This
90060 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
90070 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d ode that is comm
90080 6f 6e 20 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75 on across all mu
90090 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
900a0 6f 6e 73 2e 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a ons...**.** $Id:
900b0 20 6d 75 74 65 78 2e 63 2c 76 20 31 2e 33 31 20 mutex.c,v 1.31
900c0 32 30 30 39 2f 30 37 2f 31 36 20 31 38 3a 32 31 2009/07/16 18:21
900d0 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :18 drh Exp $.*/
900e0 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 ..#if defined(SQ
900f0 4c 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 21 LITE_DEBUG) && !
90100 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
90110 55 54 45 58 5f 4f 4d 49 54 29 0a 2f 2a 0a 2a 2a UTEX_OMIT)./*.**
90120 20 46 6f 72 20 64 65 62 75 67 67 69 6e 67 20 70 For debugging p
90130 75 72 70 6f 73 65 73 2c 20 72 65 63 6f 72 64 20 urposes, record
90140 77 68 65 6e 20 74 68 65 20 6d 75 74 65 78 20 73 when the mutex s
90150 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 ubsystem is init
90160 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 6e 64 20 75 ialized.** and u
90170 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 6f 20 ninitialized so
90180 74 68 61 74 20 77 65 20 63 61 6e 20 61 73 73 65 that we can asse
90190 72 74 28 29 20 69 66 20 74 68 65 72 65 20 69 73 rt() if there is
901a0 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a an attempt to.*
901b0 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74 * allocate a mut
901c0 65 78 20 77 68 69 6c 65 20 74 68 65 20 73 79 73 ex while the sys
901d0 74 65 6d 20 69 73 20 75 6e 69 6e 69 74 69 61 6c tem is uninitial
901e0 69 7a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ized..*/.static
901f0 53 51 4c 49 54 45 5f 57 53 44 20 69 6e 74 20 6d SQLITE_WSD int m
90200 75 74 65 78 49 73 49 6e 69 74 20 3d 20 30 3b 0a utexIsInit = 0;.
90210 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
90220 5f 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 6e _DEBUG */...#ifn
90230 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 def SQLITE_MUTEX
90240 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 _OMIT./*.** Init
90250 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 ialize the mutex
90260 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 system..*/.SQLI
90270 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
90280 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 qlite3MutexInit(
90290 76 6f 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 void){ . int rc
902a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
902b0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
902c0 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 lConfig.bCoreMut
902d0 65 78 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 ex ){. if( !s
902e0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
902f0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 ig.mutex.xMutexA
90300 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a lloc ){. /*
90310 20 49 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c If the xMutexAl
90320 6c 6f 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e loc method has n
90330 6f 74 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 ot been set, the
90340 6e 20 74 68 65 20 75 73 65 72 20 64 69 64 20 6e n the user did n
90350 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 ot. ** inst
90360 61 6c 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c all a mutex impl
90370 65 6d 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 ementation via s
90380 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 qlite3_config()
90390 70 72 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 prior to .
903a0 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 ** sqlite3_initi
903b0 61 6c 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 alize() being ca
903c0 6c 6c 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b lled. This block
903d0 20 63 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 copies pointers
903e0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 to. ** the
903f0 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 default impleme
90400 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 ntation into the
90410 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
90420 6e 66 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a nfig structure..
90430 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
90440 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
90450 68 6f 64 73 20 2a 70 46 72 6f 6d 20 3d 20 73 71 hods *pFrom = sq
90460 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 lite3DefaultMute
90470 78 28 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 x();. sqlit
90480 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
90490 20 2a 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33 *pTo = &sqlite3
904a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 GlobalConfig.mut
904b0 65 78 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 ex;.. memcp
904c0 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 6f 66 y(pTo, pFrom, of
904d0 66 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d fsetof(sqlite3_m
904e0 75 74 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d utex_methods, xM
904f0 75 74 65 78 41 6c 6c 6f 63 29 29 3b 0a 20 20 20 utexAlloc));.
90500 20 20 20 6d 65 6d 63 70 79 28 26 70 54 6f 2d 3e memcpy(&pTo->
90510 78 4d 75 74 65 78 46 72 65 65 2c 20 26 70 46 72 xMutexFree, &pFr
90520 6f 6d 2d 3e 78 4d 75 74 65 78 46 72 65 65 2c 0a om->xMutexFree,.
90530 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a siz
90540 65 6f 66 28 2a 70 54 6f 29 20 2d 20 6f 66 66 73 eof(*pTo) - offs
90550 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 etof(sqlite3_mut
90560 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 ex_methods, xMut
90570 65 78 46 72 65 65 29 29 3b 0a 20 20 20 20 20 20 exFree));.
90580 70 54 6f 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 pTo->xMutexAlloc
90590 20 3d 20 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78 = pFrom->xMutex
905a0 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 Alloc;. }.
905b0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f rc = sqlite3Glo
905c0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
905d0 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 xMutexInit();.
905e0 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
905f0 5f 44 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 _DEBUG. GLOBAL(
90600 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 int, mutexIsInit
90610 29 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 ) = 1;.#endif..
90620 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
90630 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 *.** Shutdown th
90640 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 20 e mutex system.
90650 54 68 69 73 20 63 61 6c 6c 20 66 72 65 65 73 20 This call frees
90660 72 65 73 6f 75 72 63 65 73 20 61 6c 6c 6f 63 61 resources alloca
90670 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 ted by.** sqlite
90680 33 4d 75 74 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 3MutexInit()..*/
90690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
906a0 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 int sqlite3Mutex
906b0 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 End(void){. int
906c0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
906d0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
906e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
906f0 2e 78 4d 75 74 65 78 45 6e 64 20 29 7b 0a 20 20 .xMutexEnd ){.
90700 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c rc = sqlite3Gl
90710 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
90720 2e 78 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 .xMutexEnd();.
90730 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
90740 5f 44 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 _DEBUG. GLOBAL(
90750 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 int, mutexIsInit
90760 29 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 ) = 0;.#endif..
90770 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
90780 2a 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 *.** Retrieve a
90790 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
907a0 74 69 63 20 6d 75 74 65 78 20 6f 72 20 61 6c 6c tic mutex or all
907b0 6f 63 61 74 65 20 61 20 6e 65 77 20 64 79 6e 61 ocate a new dyna
907c0 6d 69 63 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 mic one..*/.SQLI
907d0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d TE_API sqlite3_m
907e0 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 utex *sqlite3_mu
907f0 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69 64 tex_alloc(int id
90800 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ){.#ifndef SQLIT
90810 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
90820 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
90830 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 itialize() ) ret
90840 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 urn 0;.#endif.
90850 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
90860 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
90870 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 .xMutexAlloc(id)
90880 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
90890 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ATE sqlite3_mute
908a0 78 20 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 x *sqlite3MutexA
908b0 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 lloc(int id){.
908c0 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 if( !sqlite3Glob
908d0 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 alConfig.bCoreMu
908e0 74 65 78 20 29 7b 0a 20 20 20 20 72 65 74 75 72 tex ){. retur
908f0 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 n 0;. }. asser
90900 74 28 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d t( GLOBAL(int, m
90910 75 74 65 78 49 73 49 6e 69 74 29 20 29 3b 0a 20 utexIsInit) );.
90920 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 return sqlite3G
90930 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
90940 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 x.xMutexAlloc(id
90950 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 );.}../*.** Free
90960 20 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 a dynamic mutex
90970 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
90980 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
90990 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f ex_free(sqlite3_
909a0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 mutex *p){. if(
909b0 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 p ){. sqlite
909c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
909d0 74 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 tex.xMutexFree(p
909e0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
909f0 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 Obtain the mutex
90a00 20 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 p. If some othe
90a10 72 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 r thread already
90a20 20 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 has the mutex,
90a30 62 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 block.** until i
90a40 74 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 t can be obtaine
90a50 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
90a60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 void sqlite3_mu
90a70 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 tex_enter(sqlite
90a80 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 3_mutex *p){. i
90a90 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 f( p ){. sqli
90aa0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
90ab0 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 mutex.xMutexEnte
90ac0 72 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a r(p);. }.}../*.
90ad0 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 ** Obtain the mu
90ae0 74 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 tex p. If succes
90af0 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c sful, return SQL
90b00 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 ITE_OK. Otherwis
90b10 65 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a e, if another.**
90b20 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 thread holds th
90b30 65 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 e mutex and it c
90b40 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 annot be obtaine
90b50 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 d, return SQLITE
90b60 5f 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 _BUSY..*/.SQLITE
90b70 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
90b80 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 _mutex_try(sqlit
90b90 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
90ba0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
90bb0 4f 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 OK;. if( p ){.
90bc0 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
90bd0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
90be0 74 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 tex.xMutexTry(p)
90bf0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
90c00 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 c;.}../*.** The
90c10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
90c20 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 ave() routine ex
90c30 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 its a mutex that
90c40 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a was previously.
90c50 2a 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 ** entered by th
90c60 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 e same thread.
90c70 54 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 The behavior is
90c80 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
90c90 20 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f mutex .** is no
90ca0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 t currently ente
90cb0 72 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 red. If a NULL p
90cc0 6f 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 ointer is passed
90cd0 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a as an argument.
90ce0 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
90cf0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
90d00 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
90d10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
90d20 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ave(sqlite3_mute
90d30 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 x *p){. if( p )
90d40 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f {. sqlite3Glo
90d50 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
90d60 78 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a xMutexLeave(p);.
90d70 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e }.}..#ifndef N
90d80 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 DEBUG./*.** The
90d90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
90da0 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 ld() and sqlite3
90db0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
90dc0 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 routine are.**
90dd0 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 intended for use
90de0 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
90df0 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a statements..*/.
90e00 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
90e10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
90e20 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
90e30 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d *p){. return p=
90e40 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f =0 || sqlite3Glo
90e50 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e balConfig.mutex.
90e60 78 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d xMutexHeld(p);.}
90e70 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
90e80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
90e90 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 theld(sqlite3_mu
90ea0 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 tex *p){. retur
90eb0 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 n p==0 || sqlite
90ec0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
90ed0 74 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c tex.xMutexNothel
90ee0 64 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a d(p);.}.#endif..
90ef0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
90f00 5f 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a 0a _OMIT_MUTEX */..
90f10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
90f20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a End of mutex.c *
90f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
90f60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
90f70 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 Begin file mutex
90f80 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a _noop.c ********
90f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
90fb0 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 /*.** 2008 Octob
90fc0 65 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 07.**.** The
90fd0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
90fe0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
90ff0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
91000 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
91010 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
91020 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
91030 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
91040 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
91050 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
91060 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
91070 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
91080 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
91090 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
910a0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
910b0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
910c0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
910d0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
910e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
910f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91120 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
91130 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 e contains the C
91140 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
91150 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 implement mutexe
91160 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d s..**.** This im
91170 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 plementation in
91180 74 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e this file does n
91190 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d ot provide any m
911a0 75 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 utual.** exclusi
911b0 6f 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 on and is thus s
911c0 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 uitable for use
911d0 6f 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 only in applicat
911e0 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 ions.** that use
911f0 20 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e SQLite in a sin
91200 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 gle thread. The
91210 20 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 routines define
91220 64 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c d.** here are pl
91230 61 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 ace-holders. Ap
91240 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 plications can s
91250 75 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e ubstitute workin
91260 67 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 g.** mutex routi
91270 6e 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d nes at start-tim
91280 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a e using the.**.*
91290 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
912a0 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 nfig(SQLITE_CONF
912b0 49 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a IG_MUTEX,...).**
912c0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a .** interface..*
912d0 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 *.** If compiled
912e0 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 with SQLITE_DEB
912f0 55 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f UG, then additio
91300 6e 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 nal logic is ins
91310 65 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f erted.** that do
91320 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e es error checkin
91330 67 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 g on mutexes to
91340 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 make sure they a
91350 72 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c re being.** call
91360 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a ed correctly..**
91370 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e .** $Id: mutex_n
91380 6f 6f 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 oop.c,v 1.3 2008
91390 2f 31 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 20 /12/05 17:17:08
913a0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 drh Exp $.*/...#
913b0 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
913c0 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 E_MUTEX_NOOP) &&
913d0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
913e0 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 _DEBUG)./*.** St
913f0 75 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 ub routines for
91400 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 all mutex method
91410 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
91420 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e utines provide n
91430 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 o mutual exclusi
91440 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 on or error chec
91450 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 king..*/.static
91460 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c int noopMutexHel
91470 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 d(sqlite3_mutex
91480 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d *p){ return 1; }
91490 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 .static int noop
914a0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c MutexNotheld(sql
914b0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 ite3_mutex *p){
914c0 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 return 1; }.stat
914d0 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 ic int noopMutex
914e0 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 Init(void){ retu
914f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a rn SQLITE_OK; }.
91500 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d static int noopM
91510 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 utexEnd(void){ r
91520 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
91530 20 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 }.static sqlite
91540 33 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 3_mutex *noopMut
91550 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b exAlloc(int id){
91560 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 return (sqlite3
91570 5f 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 _mutex*)8; }.sta
91580 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 tic void noopMut
91590 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d exFree(sqlite3_m
915a0 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e utex *p){ return
915b0 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ; }.static void
915c0 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 noopMutexEnter(s
915d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
915e0 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 { return; }.stat
915f0 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 ic int noopMutex
91600 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 Try(sqlite3_mute
91610 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 x *p){ return SQ
91620 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 LITE_OK; }.stati
91630 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 c void noopMutex
91640 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 Leave(sqlite3_mu
91650 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b tex *p){ return;
91660 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 }..SQLITE_PRIVA
91670 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 TE sqlite3_mutex
91680 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 _methods *sqlite
91690 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 3DefaultMutex(vo
916a0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 id){. static sq
916b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
916c0 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 ods sMutex = {.
916d0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 noopMutexInit
916e0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 ,. noopMutexE
916f0 6e 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 nd,. noopMute
91700 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 xAlloc,. noop
91710 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e MutexFree,. n
91720 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 oopMutexEnter,.
91730 20 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c noopMutexTry,
91740 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 . noopMutexLe
91750 61 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 ave,.. noopMu
91760 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f texHeld,. noo
91770 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 pMutexNotheld.
91780 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d };.. return &sM
91790 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f utex;.}.#endif /
917a0 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 * defined(SQLITE
917b0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 _MUTEX_NOOP) &&
917c0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
917d0 44 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 DEBUG) */..#if d
917e0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 efined(SQLITE_MU
917f0 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 TEX_NOOP) && def
91800 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
91810 47 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 G)./*.** In this
91820 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c implementation,
91830 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 error checking
91840 69 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 is provided for
91850 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 testing.** and d
91860 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 ebugging purpose
91870 73 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 s. The mutexes
91880 73 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f still do not pro
91890 76 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 vide any.** mutu
918a0 61 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f al exclusion..*/
918b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 ../*.** The mute
918c0 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 x object.*/.stru
918d0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ct sqlite3_mutex
918e0 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 {. int id;
918f0 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 /* The mutex ty
91900 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b pe */. int cnt;
91910 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
91920 20 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 entries without
91930 20 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 a matching leav
91940 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 e */.};../*.** T
91950 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
91960 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 _held() and sqli
91970 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c te3_mutex_nothel
91980 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a d() routine are.
91990 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 ** intended for
919a0 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 use inside asser
919b0 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a t() statements..
919c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 */.static int de
919d0 62 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c bugMutexHeld(sql
919e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
919f0 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c return p==0 ||
91a00 20 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 p->cnt>0;.}.sta
91a10 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 tic int debugMut
91a20 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 exNotheld(sqlite
91a30 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 3_mutex *p){. r
91a40 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d eturn p==0 || p-
91a50 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a >cnt==0;.}../*.*
91a60 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 * Initialize and
91a70 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 deinitialize th
91a80 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
91a90 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 m..*/.static int
91aa0 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 debugMutexInit(
91ab0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 void){ return SQ
91ac0 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 LITE_OK; }.stati
91ad0 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 c int debugMutex
91ae0 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 End(void){ retur
91af0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a n SQLITE_OK; }..
91b00 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 /*.** The sqlite
91b10 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
91b20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 routine allocate
91b30 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 s a new.** mutex
91b40 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 and returns a p
91b50 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 ointer to it. I
91b60 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c f it returns NUL
91b70 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 L.** that means
91b80 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 that a mutex cou
91b90 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 ld not be alloca
91ba0 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 ted. .*/.static
91bb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 sqlite3_mutex *d
91bc0 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 ebugMutexAlloc(i
91bd0 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 nt id){. static
91be0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 sqlite3_mutex a
91bf0 53 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c Static[6];. sql
91c00 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 ite3_mutex *pNew
91c10 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 = 0;. switch(
91c20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 id ){. case S
91c30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
91c40 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 :. case SQLIT
91c50 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
91c60 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 E: {. pNew
91c70 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
91c80 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a sizeof(*pNew));.
91c90 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 if( pNew )
91ca0 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e {. pNew->
91cb0 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 id = id;.
91cc0 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a pNew->cnt = 0;.
91cd0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
91ce0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
91cf0 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
91d00 61 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 assert( id-2 >=
91d10 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 0 );. asser
91d20 74 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 t( id-2 < (int)(
91d30 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f sizeof(aStatic)/
91d40 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 sizeof(aStatic[0
91d50 5d 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 ])) );. pNe
91d60 77 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d w = &aStatic[id-
91d70 32 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 2];. pNew->
91d80 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 id = id;. b
91d90 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
91da0 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d return pNew;.}
91db0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
91dc0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 tine deallocates
91dd0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c a previously al
91de0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a located mutex..*
91df0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 /.static void de
91e00 62 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c bugMutexFree(sql
91e10 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
91e20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 assert( p->cnt
91e30 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
91e40 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
91e50 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e UTEX_FAST || p->
91e60 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
91e70 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
91e80 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
91e90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
91ea0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
91eb0 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f r() and sqlite3_
91ec0 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 mutex_try() rout
91ed0 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 ines attempt.**
91ee0 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 to enter a mutex
91ef0 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 . If another th
91f00 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
91f10 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
91f20 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
91f30 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
91f40 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
91f50 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
91f60 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
91f70 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 ITE_BUSY. The s
91f80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
91f90 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
91fa0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a urns SQLITE_OK.*
91fb0 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 * upon successfu
91fc0 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 l entry. Mutexe
91fd0 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 s created using
91fe0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
91ff0 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 URSIVE can.** be
92000 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c entered multipl
92010 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 e times by the s
92020 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 ame thread. In
92030 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a such cases the,.
92040 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 ** mutex must be
92050 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c exited an equal
92060 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
92070 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 before another
92080 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e thread.** can en
92090 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d ter. If the sam
920a0 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 e thread tries t
920b0 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 o enter any othe
920c0 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a r kind of mutex.
920d0 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ** more than onc
920e0 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 e, the behavior
920f0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f is undefined..*/
92100 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 .static void deb
92110 75 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c ugMutexEnter(sql
92120 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
92130 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
92140 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
92150 43 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 CURSIVE || debug
92160 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
92170 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d );. p->cnt++;.}
92180 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 .static int debu
92190 67 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 gMutexTry(sqlite
921a0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 3_mutex *p){. a
921b0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
921c0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
921d0 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 SIVE || debugMut
921e0 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a exNotheld(p) );.
921f0 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 p->cnt++;. re
92200 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
92210 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
92220 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
92230 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
92240 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
92250 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
92260 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
92270 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
92280 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 behavior.** is
92290 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
922a0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 mutex is not cu
922b0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
922c0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 or.** is not cur
922d0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
922e0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
922f0 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a ever do either..
92300 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 */.static void d
92310 65 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 ebugMutexLeave(s
92320 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
92330 7b 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 {. assert( debu
92340 67 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b gMutexHeld(p) );
92350 0a 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 . p->cnt--;. a
92360 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
92370 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
92380 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 SIVE || debugMut
92390 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a exNotheld(p) );.
923a0 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
923b0 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f E sqlite3_mutex_
923c0 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
923d0 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 DefaultMutex(voi
923e0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c d){. static sql
923f0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
92400 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 ds sMutex = {.
92410 20 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 debugMutexInit
92420 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 ,. debugMutex
92430 45 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 End,. debugMu
92440 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 texAlloc,. de
92450 62 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 bugMutexFree,.
92460 20 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 debugMutexEnte
92470 72 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 r,. debugMute
92480 78 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d xTry,. debugM
92490 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 utexLeave,..
924a0 64 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a debugMutexHeld,.
924b0 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f debugMutexNo
924c0 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 theld. };.. re
924d0 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a turn &sMutex;.}.
924e0 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 #endif /* define
924f0 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e d(SQLITE_MUTEX_N
92500 4f 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 OOP) && defined(
92510 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f SQLITE_DEBUG) */
92520 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
92530 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e * End of mutex_n
92540 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a oop.c **********
92550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92570 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
92580 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 * Begin file mut
92590 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a ex_os2.c *******
925a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
925b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
925c0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 /./*.** 2007 Aug
925d0 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 ust 28.**.** The
925e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
925f0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
92600 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
92610 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
92620 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
92630 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
92640 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
92650 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
92660 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
92670 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
92680 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
92690 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
926a0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
926b0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
926c0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
926d0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
926e0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
926f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92730 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
92740 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
92750 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
92760 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 implement mutex
92770 65 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a 2a es for OS/2.**.*
92780 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 32 * $Id: mutex_os2
92790 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 31 .c,v 1.11 2008/1
927a0 31 2f 32 32 20 31 39 3a 35 30 3a 35 34 20 70 77 1/22 19:50:54 pw
927b0 65 69 6c 62 61 63 68 65 72 20 45 78 70 20 24 0a eilbacher Exp $.
927c0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f */../*.** The co
927d0 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
927e0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 is only used if
927f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 SQLITE_MUTEX_OS2
92800 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 is defined..**
92810 53 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 See the mutex.h
92820 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 file for details
92830 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
92840 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a TE_MUTEX_OS2../*
92850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92860 2a 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 ***** OS/2 Mutex
92870 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
92880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92890 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
928a0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
928b0 20 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 of mutexes is b
928c0 75 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f uilt using the O
928d0 53 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a S/2 API..*/../*.
928e0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a ** The mutex obj
928f0 65 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 ect.** Each recu
92900 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 rsive mutex is a
92910 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
92920 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
92930 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 cture..*/.struct
92940 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b sqlite3_mutex {
92950 0a 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 . HMTX mutex;
92960 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f /* Mutex co
92970 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f ntrolling the lo
92980 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b ck */. int id;
92990 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 /* Mut
929a0 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 ex type */. int
929b0 20 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f nRef; /
929c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 * Number of refe
929d0 72 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 rences */. TID
929e0 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a owner; /*
929f0 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 Thread holding
92a00 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b this mutex */.};
92a10 0a 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 ..#define OS2_MU
92a20 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 TEX_INITIALIZER
92a30 20 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 0,0,0,0../*.**
92a40 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
92a50 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 deinitialize the
92a60 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d mutex subsystem
92a70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
92a80 6f 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 os2MutexInit(voi
92a90 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
92aa0 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 E_OK; }.static i
92ab0 6e 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 nt os2MutexEnd(v
92ac0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c oid){ return SQL
92ad0 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK; }../*.**
92ae0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
92af0 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 ex_alloc() routi
92b00 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e ne allocates a n
92b10 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 ew.** mutex and
92b20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
92b30 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 r to it. If it
92b40 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 returns NULL.**
92b50 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 that means that
92b60 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f a mutex could no
92b70 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 t be allocated.
92b80 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
92b90 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b unwind its stack
92ba0 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 and return an e
92bb0 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d rror. The argum
92bc0 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 ent.** to sqlite
92bd0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
92be0 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 is one of these
92bf0 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
92c00 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a s:.**.** <ul>.**
92c10 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
92c20 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 TEX_FAST
92c30 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 0.** <li>
92c40 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 SQLITE_MUTEX_R
92c50 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 ECURSIVE
92c60 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 1.** <li> SQL
92c70 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
92c80 5f 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a _MASTER 2.*
92c90 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
92ca0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 UTEX_STATIC_MEM
92cb0 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3.** <li
92cc0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
92cd0 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 STATIC_PRNG
92ce0 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 4.** </ul>.**
92cf0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 .** The first tw
92d00 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 o constants caus
92d10 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
92d20 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 alloc() to creat
92d30 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 e.** a new mutex
92d40 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 . The new mutex
92d50 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 is recursive wh
92d60 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f en SQLITE_MUTEX_
92d70 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 RECURSIVE.** is
92d80 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 used but not nec
92d90 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e essarily so when
92da0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
92db0 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 ST is used..** T
92dc0 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 he mutex impleme
92dd0 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 ntation does not
92de0 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 need to make a
92df0 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 distinction.** b
92e00 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 etween SQLITE_MU
92e10 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e TEX_RECURSIVE an
92e20 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 d SQLITE_MUTEX_F
92e30 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a AST if it does.*
92e40 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 * not want to.
92e50 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 But SQLite will
92e60 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 only request a r
92e70 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 ecursive mutex i
92e80 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 n.** cases where
92e90 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 it really needs
92ea0 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 one. If a fast
92eb0 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 er non-recursive
92ec0 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d mutex.** implem
92ed0 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 entation is avai
92ee0 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 lable on the hos
92ef0 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 t platform, the
92f00 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a mutex subsystem.
92f10 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 ** might return
92f20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 such a mutex in
92f30 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 response to SQLI
92f40 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a TE_MUTEX_FAST..*
92f50 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 *.** The other a
92f60 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 llowed parameter
92f70 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 s to sqlite3_mut
92f80 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 ex_alloc() each
92f90 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e return.** a poin
92fa0 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 ter to a static
92fb0 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 preexisting mute
92fc0 78 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 x. Three static
92fd0 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 mutexes are.**
92fe0 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 used by the curr
92ff0 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 ent version of S
93000 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 QLite. Future v
93010 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
93020 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 e.** may add add
93030 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d itional static m
93040 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 utexes. Static
93050 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 mutexes are for
93060 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 internal.** use
93070 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 by SQLite only.
93080 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 Applications th
93090 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 at use SQLite mu
930a0 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 texes should.**
930b0 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e use only the dyn
930c0 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 amic mutexes ret
930d0 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f urned by SQLITE_
930e0 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a MUTEX_FAST or.**
930f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
93100 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e CURSIVE..**.** N
93110 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 ote that if one
93120 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d of the dynamic m
93130 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 utex parameters
93140 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 (SQLITE_MUTEX_FA
93150 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f ST.** or SQLITE_
93160 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 MUTEX_RECURSIVE)
93170 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 is used then sq
93180 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
93190 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 c().** returns a
931a0 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 different mutex
931b0 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 on every call.
931c0 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 But for the sta
931d0 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 tic.** mutex typ
931e0 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 es, the same mut
931f0 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f ex is returned o
93200 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 n every call tha
93210 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d t has.** the sam
93220 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a e type number..*
93230 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 /.static sqlite3
93240 5f 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 _mutex *os2Mutex
93250 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 Alloc(int iType)
93260 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
93270 78 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 x *p = NULL;. s
93280 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a witch( iType ){.
93290 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
932a0 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 MUTEX_FAST:.
932b0 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 case SQLITE_MUTE
932c0 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 X_RECURSIVE: {.
932d0 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p = sqlite3
932e0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 MallocZero( size
932f0 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 of(*p) );.
93300 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 if( p ){.
93310 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
93320 20 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 if( DosC
93330 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 reateMutexSem( 0
93340 2c 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 , &p->mutex, 0,
93350 46 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 FALSE ) != NO_ER
93360 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ROR ){.
93370 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 sqlite3_free( p
93380 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 );. p
93390 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 = NULL;.
933a0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
933b0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
933c0 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
933d0 20 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c static volatil
933e0 65 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 e int isInit = 0
933f0 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 ;. static s
93400 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 qlite3_mutex sta
93410 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b ticMutexes[] = {
93420 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d . { OS2_M
93430 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
93440 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f , },. { O
93450 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c S2_MUTEX_INITIAL
93460 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 IZER, },.
93470 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 { OS2_MUTEX_INI
93480 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 TIALIZER, },.
93490 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 { OS2_MUTEX
934a0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c _INITIALIZER, },
934b0 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d . { OS2_M
934c0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
934d0 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f , },. { O
934e0 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c S2_MUTEX_INITIAL
934f0 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d IZER, },. }
93500 3b 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73 ;. if ( !is
93510 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Init ){.
93520 41 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 APIRET rc;.
93530 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 PTIB ptib;.
93540 20 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b PPIB ppib;
93550 0a 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 . HMTX mu
93560 74 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 tex;. cha
93570 72 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 r name[32];.
93580 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
93590 6f 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 ocks( &ptib, &pp
935a0 69 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ib );. sq
935b0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 lite3_snprintf(
935c0 73 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 sizeof(name), na
935d0 6d 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 me, "\\SEM32\\SQ
935e0 4c 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 LITE%04x",.
935f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
93600 20 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 ppib->pib_u
93610 6c 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 lpid );.
93620 77 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 while( !isInit )
93630 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 {. mute
93640 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 x = 0;.
93650 20 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d rc = DosCreateM
93660 75 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 utexSem( name, &
93670 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 mutex, 0, FALSE)
93680 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
93690 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 rc == NO_ERROR )
936a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e {. un
936b0 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 signed int i;.
936c0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 if( !i
936d0 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 sInit ){.
936e0 20 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 for( i =
936f0 30 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 0; i < sizeof(st
93700 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a aticMutexes)/siz
93710 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 eof(staticMutexe
93720 73 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 s[0]); i++ ){.
93730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f Do
93740 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 sCreateMutexSem(
93750 20 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 0, &staticMutex
93760 65 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 es[i].mutex, 0,
93770 46 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 FALSE );.
93780 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
93790 20 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 isInit =
937a0 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 1;. }
937b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 . Dos
937c0 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d CloseMutexSem( m
937d0 75 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 utex );.
937e0 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d }else if( rc =
937f0 3d 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 = ERROR_DUPLICAT
93800 45 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 E_NAME ){.
93810 20 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 DosSleep(
93820 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 1 );. }
93830 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
93840 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 return p;.
93850 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
93860 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
93870 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 assert( iType-2
93880 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 >= 0 );. as
93890 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 sert( iType-2 <
938a0 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 sizeof(staticMut
938b0 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 exes)/sizeof(sta
938c0 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 ticMutexes[0]) )
938d0 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 ;. p = &sta
938e0 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 ticMutexes[iType
938f0 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 -2];. p->id
93900 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 = iType;.
93910 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
93920 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
93930 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
93940 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 ine deallocates
93950 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c a previously all
93960 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a ocated mutex..**
93970 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 SQLite is caref
93980 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 ul to deallocate
93990 20 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 every mutex tha
939a0 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a t it allocates..
939b0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f */.static void o
939c0 73 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 s2MutexFree(sqli
939d0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
939e0 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
939f0 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rn;. assert( p-
93a00 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 >nRef==0 );. as
93a10 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
93a20 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c ITE_MUTEX_FAST |
93a30 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f | p->id==SQLITE_
93a40 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
93a50 29 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 );. DosCloseMut
93a60 65 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 exSem( p->mutex
93a70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 );. sqlite3_fre
93a80 65 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a e( p );.}../*.**
93a90 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
93aa0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 ex_enter() and s
93ab0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
93ac0 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 () routines atte
93ad0 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 mpt.** to enter
93ae0 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f a mutex. If ano
93af0 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 ther thread is a
93b00 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 lready within th
93b10 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 e mutex,.** sqli
93b20 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
93b30 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 ) will block and
93b40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
93b50 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e ry() will return
93b60 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e .** SQLITE_BUSY.
93b70 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 The sqlite3_mu
93b80 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 tex_try() interf
93b90 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ace returns SQLI
93ba0 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 TE_OK.** upon su
93bb0 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 ccessful entry.
93bc0 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 Mutexes created
93bd0 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 using SQLITE_MU
93be0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 TEX_RECURSIVE ca
93bf0 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 n.** be entered
93c00 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 multiple times b
93c10 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 y the same threa
93c20 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 d. In such case
93c30 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 s the,.** mutex
93c40 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 must be exited a
93c50 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f n equal number o
93c60 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 f times before a
93c70 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a nother thread.**
93c80 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 can enter. If
93c90 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 the same thread
93ca0 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 tries to enter a
93cb0 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 ny other kind of
93cc0 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 mutex.** more t
93cd0 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 han once, the be
93ce0 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
93cf0 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ned..*/.static v
93d00 6f 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65 oid os2MutexEnte
93d10 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 r(sqlite3_mutex
93d20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a *p){. TID tid;.
93d30 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 PID holder1;.
93d40 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a ULONG holder2;.
93d50 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
93d60 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
93d70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
93d80 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 EX_RECURSIVE ||
93d90 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 os2MutexNotheld(
93da0 70 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65 p) );. DosReque
93db0 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 stMutexSem(p->mu
93dc0 74 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e tex, SEM_INDEFIN
93dd0 49 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73 ITE_WAIT);. Dos
93de0 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d QueryMutexSem(p-
93df0 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 >mutex, &holder1
93e00 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 , &tid, &holder2
93e10 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 );. p->owner =
93e20 74 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b tid;. p->nRef++
93e30 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f ;.}.static int o
93e40 73 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 s2MutexTry(sqlit
93e50 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
93e60 69 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69 int rc;. TID ti
93e70 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 d;. PID holder1
93e80 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 ;. ULONG holder
93e90 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 2;. if( p==0 )
93ea0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
93eb0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
93ec0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
93ed0 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 RECURSIVE || os2
93ee0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 MutexNotheld(p)
93ef0 29 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75 );. if( DosRequ
93f00 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d estMutexSem(p->m
93f10 75 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49 utex, SEM_IMMEDI
93f20 41 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e ATE_RETURN) == N
93f30 4f 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44 O_ERROR) {. D
93f40 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 osQueryMutexSem(
93f50 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 p->mutex, &holde
93f60 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 r1, &tid, &holde
93f70 72 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 r2);. p->owne
93f80 72 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e r = tid;. p->
93f90 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d nRef++;. rc =
93fa0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 SQLITE_OK;. }
93fb0 65 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20 else {. rc =
93fc0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
93fd0 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
93fe0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
93ff0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
94000 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 ) routine exits
94010 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 a mutex that was
94020 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 .** previously e
94030 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 ntered by the sa
94040 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 me thread. The
94050 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 behavior.** is u
94060 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
94070 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 mutex is not cur
94080 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f rently entered o
94090 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 r.** is not curr
940a0 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e ently allocated.
940b0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 SQLite will ne
940c0 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a ver do either..*
940d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 /.static void os
940e0 32 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 2MutexLeave(sqli
940f0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
94100 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 TID tid;. PID
94110 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 holder1;. ULONG
94120 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 holder2;. if(
94130 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
94140 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
94150 3e 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 >0 );. DosQuery
94160 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 MutexSem(p->mute
94170 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 x, &holder1, &ti
94180 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 d, &holder2);.
94190 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 assert( p->owner
941a0 3d 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52 ==tid );. p->nR
941b0 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 ef--;. assert(
941c0 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d p->nRef==0 || p-
941d0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
941e0 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 X_RECURSIVE );.
941f0 20 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78 DosReleaseMutex
94200 53 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d Sem(p->mutex);.}
94210 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
94220 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 DEBUG./*.** The
94230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
94240 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 ld() and sqlite3
94250 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 _mutex_notheld()
94260 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 routine are.**
94270 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 intended for use
94280 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
94290 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a statements..*/.
942a0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 static int os2Mu
942b0 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f texHeld(sqlite3_
942c0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 mutex *p){. TID
942d0 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b tid;. PID pid;
942e0 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 . ULONG ulCount
942f0 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 ;. PTIB ptib;.
94300 20 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20 if( p!=0 ) {.
94310 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 DosQueryMutexS
94320 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 em(p->mutex, &pi
94330 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e d, &tid, &ulCoun
94340 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 t);. } else {.
94350 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f DosGetInfoBlo
94360 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 cks(&ptib, NULL)
94370 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 ;. tid = ptib
94380 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 ->tib_ptib2->tib
94390 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 2_ultid;. }. r
943a0 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70 eturn p==0 || (p
943b0 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e ->nRef!=0 && p->
943c0 6f 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73 owner==tid);.}.s
943d0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 tatic int os2Mut
943e0 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 exNotheld(sqlite
943f0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 3_mutex *p){. T
94400 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 ID tid;. PID pi
94410 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 d;. ULONG ulCou
94420 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b nt;. PTIB ptib;
94430 0a 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b . if( p!= 0 ) {
94440 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 . DosQueryMut
94450 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 exSem(p->mutex,
94460 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 &pid, &tid, &ulC
94470 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 ount);. } else
94480 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f {. DosGetInfo
94490 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 Blocks(&ptib, NU
944a0 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 LL);. tid = p
944b0 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e tib->tib_ptib2->
944c0 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a tib2_ultid;. }.
944d0 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c return p==0 ||
944e0 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 p->nRef==0 || p
944f0 2d 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a ->owner!=tid;.}.
94500 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
94510 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d RIVATE sqlite3_m
94520 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 utex_methods *sq
94530 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 lite3DefaultMute
94540 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 x(void){. stati
94550 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f c sqlite3_mutex_
94560 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d methods sMutex =
94570 20 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 {. os2MutexI
94580 6e 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 nit,. os2Mute
94590 78 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 xEnd,. os2Mut
945a0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 exAlloc,. os2
945b0 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f MutexFree,. o
945c0 73 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 s2MutexEnter,.
945d0 20 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 os2MutexTry,.
945e0 20 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 os2MutexLeave
945f0 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ,.#ifdef SQLITE_
94600 44 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 DEBUG. os2Mut
94610 65 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d exHeld,. os2M
94620 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 utexNotheld.#end
94630 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 if. };.. retur
94640 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e n &sMutex;.}.#en
94650 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 dif /* SQLITE_MU
94660 54 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a TEX_OS2 */../***
94670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
94680 6f 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a of mutex_os2.c *
94690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
946a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
946b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
946c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
946d0 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 n file mutex_uni
946e0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
946f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
94710 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 * 2007 August 28
94720 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
94730 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
94740 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
94750 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
94760 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
94770 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
94780 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
94790 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
947a0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
947b0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
947c0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
947d0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
947e0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
947f0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
94800 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
94810 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
94820 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
94830 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
94840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
94870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
94880 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
94890 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
948a0 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
948b0 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 ment mutexes for
948c0 20 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20 pthreads.**.**
948d0 24 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e $Id: mutex_unix.
948e0 63 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 32 c,v 1.16 2008/12
948f0 2f 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72 68 /08 18:19:18 drh
94900 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
94910 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 The code in thi
94920 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 s file is only u
94930 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f sed if we are co
94940 6d 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 mpiling threadsa
94950 66 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 fe.** under unix
94960 20 77 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a with pthreads..
94970 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
94980 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 this implementat
94990 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 ion requires a v
949a0 65 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 ersion of pthrea
949b0 64 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f ds that.** suppo
949c0 72 74 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 rts recursive mu
949d0 74 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 texes..*/.#ifdef
949e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 SQLITE_MUTEX_PT
949f0 48 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 HREADS..#include
94a00 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f <pthread.h>.../
94a10 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 *.** Each recurs
94a20 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 ive mutex is an
94a30 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
94a40 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
94a50 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 ure..*/.struct s
94a60 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 qlite3_mutex {.
94a70 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 pthread_mutex_t
94a80 20 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d mutex; /* M
94a90 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 utex controlling
94aa0 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 the lock */. i
94ab0 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 nt id;
94ac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 /* Mut
94ad0 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 ex type */. int
94ae0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 nRef;
94af0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
94b00 72 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a r of entrances *
94b10 2f 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 /. pthread_t ow
94b20 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f ner; /
94b30 2a 20 54 68 72 65 61 64 20 74 68 61 74 20 69 73 * Thread that is
94b40 20 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 within this mut
94b50 65 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ex */.#ifdef SQL
94b60 49 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 ITE_DEBUG. int
94b70 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 trace;
94b80 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
94b90 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 o trace changes
94ba0 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 */.#endif.};.#if
94bb0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
94bc0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 .#define SQLITE3
94bd0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
94be0 45 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 ER { PTHREAD_MUT
94bf0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 EX_INITIALIZER,
94c00 30 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 0, 0, (pthread_t
94c10 29 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 )0, 0 }.#else.#d
94c20 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 efine SQLITE3_MU
94c30 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 TEX_INITIALIZER
94c40 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f { PTHREAD_MUTEX_
94c50 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 INITIALIZER, 0,
94c60 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 0, (pthread_t)0
94c70 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
94c80 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
94c90 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c x_held() and sql
94ca0 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 ite3_mutex_nothe
94cb0 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 ld() routine are
94cc0 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 .** intended for
94cd0 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 use only inside
94ce0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
94cf0 65 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 ents. On some p
94d00 6c 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 latforms,.** the
94d10 72 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 re might be race
94d20 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 conditions that
94d30 20 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 can cause these
94d40 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 routines to.**
94d50 64 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 deliver incorrec
94d60 74 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 t results. In p
94d70 61 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 articular, if pt
94d80 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 hread_equal() is
94d90 0a 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 .** not an atomi
94da0 63 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 c operation, the
94db0 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 n these routines
94dc0 20 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a might delivery.
94dd0 2a 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 ** incorrect res
94de0 75 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 ults. On most p
94df0 6c 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 latforms, pthrea
94e00 64 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a d_equal() is a .
94e10 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 ** comparison of
94e20 20 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e two integers an
94e30 64 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 d is therefore a
94e40 74 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 tomic. But we a
94e50 72 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 re.** told that
94e60 48 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 HPUX is not such
94e70 20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 a platform. If
94e80 20 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 so, then these
94e90 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c routines.** will
94ea0 20 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b not always work
94eb0 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 correctly on HP
94ec0 55 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f UX..**.** On tho
94ed0 73 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 se platforms whe
94ee0 72 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c re pthread_equal
94ef0 28 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 () is not atomic
94f00 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 , SQLite.** shou
94f10 6c 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 ld be compiled w
94f20 69 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f ithout -DSQLITE_
94f30 44 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d DEBUG and with -
94f40 44 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 DNDEBUG to.** ma
94f50 6b 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 ke sure no asser
94f60 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 t() statements a
94f70 72 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 re evaluated and
94f80 20 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 hence these.**
94f90 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 routines are nev
94fa0 65 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 er called..*/.#i
94fb0 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 f !defined(NDEBU
94fc0 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 G) || defined(SQ
94fd0 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 LITE_DEBUG).stat
94fe0 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 ic int pthreadMu
94ff0 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f texHeld(sqlite3_
95000 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 mutex *p){. ret
95010 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 urn (p->nRef!=0
95020 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c && pthread_equal
95030 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 (p->owner, pthre
95040 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 ad_self()));.}.s
95050 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 tatic int pthrea
95060 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 dMutexNotheld(sq
95070 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
95080 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 . return p->nRe
95090 66 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f f==0 || pthread_
950a0 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 equal(p->owner,
950b0 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d pthread_self())=
950c0 3d 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a =0;.}.#endif../*
950d0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 .** Initialize a
950e0 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 nd deinitialize
950f0 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
95100 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 tem..*/.static i
95110 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 nt pthreadMutexI
95120 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 nit(void){ retur
95130 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 n SQLITE_OK; }.s
95140 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 tatic int pthrea
95150 64 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b dMutexEnd(void){
95160 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
95170 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K; }../*.** The
95180 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
95190 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c loc() routine al
951a0 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a locates a new.**
951b0 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 mutex and retur
951c0 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
951d0 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 it. If it retur
951e0 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
951f0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
95200 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
95210 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 allocated. SQLi
95220 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e te.** will unwin
95230 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 d its stack and
95240 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e return an error.
95250 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a The argument.*
95260 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 * to sqlite3_mut
95270 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e ex_alloc() is on
95280 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
95290 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a er constants:.**
952a0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
952b0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 SQLITE_MUTEX_F
952c0 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c AST.** <li> SQL
952d0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
952e0 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c IVE.** <li> SQL
952f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
95300 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 _MASTER.** <li>
95310 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
95320 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e ATIC_MEM.** <li>
95330 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
95340 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c TATIC_MEM2.** <l
95350 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
95360 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 _STATIC_PRNG.**
95370 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
95380 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a EX_STATIC_LRU.**
95390 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 <li> SQLITE_MU
953a0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a TEX_STATIC_LRU2.
953b0 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
953c0 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e he first two con
953d0 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c stants cause sql
953e0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
953f0 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 () to create.**
95400 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 a new mutex. Th
95410 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 e new mutex is r
95420 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 ecursive when SQ
95430 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
95440 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 SIVE.** is used
95450 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 but not necessar
95460 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 ily so when SQLI
95470 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 TE_MUTEX_FAST is
95480 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 used..** The mu
95490 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
954a0 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 on does not need
954b0 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 to make a disti
954c0 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 nction.** betwee
954d0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
954e0 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c ECURSIVE and SQL
954f0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 ITE_MUTEX_FAST i
95500 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 f it does.** not
95510 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 want to. But S
95520 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 QLite will only
95530 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 request a recurs
95540 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 ive mutex in.**
95550 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 cases where it r
95560 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e eally needs one.
95570 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f If a faster no
95580 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 n-recursive mute
95590 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 x.** implementat
955a0 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 ion is available
955b0 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 on the host pla
955c0 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 tform, the mutex
955d0 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 subsystem.** mi
955e0 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 ght return such
955f0 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f a mutex in respo
95600 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 nse to SQLITE_MU
95610 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 TEX_FAST..**.**
95620 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 The other allowe
95630 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 d parameters to
95640 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
95650 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 loc() each retur
95660 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 n.** a pointer t
95670 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 o a static preex
95680 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 isting mutex. S
95690 69 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 ix static mutexe
956a0 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 s are.** used by
956b0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 the current ver
956c0 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 sion of SQLite.
956d0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
956e0 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 of SQLite.** ma
956f0 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c y add additional
95700 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e static mutexes.
95710 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 Static mutexes
95720 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 are for interna
95730 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 l.** use by SQLi
95740 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 te only. Applic
95750 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 ations that use
95760 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 SQLite mutexes s
95770 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c hould.** use onl
95780 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 y the dynamic mu
95790 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 texes returned b
957a0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 y SQLITE_MUTEX_F
957b0 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 AST or.** SQLITE
957c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
957d0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
957e0 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 t if one of the
957f0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 dynamic mutex pa
95800 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 rameters (SQLITE
95810 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f _MUTEX_FAST.** o
95820 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 r SQLITE_MUTEX_R
95830 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 ECURSIVE) is use
95840 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d d then sqlite3_m
95850 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 utex_alloc().**
95860 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 returns a differ
95870 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 ent mutex on eve
95880 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f ry call. But fo
95890 72 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a r the static .**
958a0 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 mutex types, th
958b0 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 e same mutex is
958c0 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 returned on ever
958d0 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a y call that has.
958e0 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 ** the same type
958f0 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
95900 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
95910 20 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c *pthreadMutexAl
95920 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a loc(int iType){.
95930 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
95940 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 _mutex staticMut
95950 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 exes[] = {. S
95960 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 QLITE3_MUTEX_INI
95970 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 TIALIZER,. SQ
95980 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 LITE3_MUTEX_INIT
95990 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c IALIZER,. SQL
959a0 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
959b0 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 ALIZER,. SQLI
959c0 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
959d0 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 LIZER,. SQLIT
959e0 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
959f0 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 IZER,. SQLITE
95a00 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
95a10 5a 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 ZER. };. sqlit
95a20 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 e3_mutex *p;. s
95a30 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a witch( iType ){.
95a40 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
95a50 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a MUTEX_RECURSIVE:
95a60 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c {. p = sql
95a70 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 ite3MallocZero(
95a80 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 sizeof(*p) );.
95a90 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 if( p ){.#if
95aa0 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 def SQLITE_HOMEG
95ab0 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d ROWN_RECURSIVE_M
95ac0 55 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 UTEX. /*
95ad0 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 If recursive mut
95ae0 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 exes are not ava
95af0 69 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 ilable, we will
95b00 68 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 have to.
95b10 2a 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e ** build our own
95b20 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f . See below. */
95b30 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
95b40 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e _mutex_init(&p->
95b50 6d 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 mutex, 0);.#else
95b60 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 . /* Use
95b70 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 a recursive mute
95b80 78 20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c x if it is avail
95b90 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 able */.
95ba0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 pthread_mutexatt
95bb0 72 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74 r_t recursiveAtt
95bc0 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 r;. pthre
95bd0 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 ad_mutexattr_ini
95be0 74 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 t(&recursiveAttr
95bf0 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 );. pthre
95c00 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 ad_mutexattr_set
95c10 74 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41 type(&recursiveA
95c20 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 ttr, PTHREAD_MUT
95c30 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 EX_RECURSIVE);.
95c40 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
95c50 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 utex_init(&p->mu
95c60 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 tex, &recursiveA
95c70 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 ttr);. pt
95c80 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f hread_mutexattr_
95c90 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 destroy(&recursi
95ca0 76 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a veAttr);.#endif.
95cb0 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 p->id =
95cc0 69 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 iType;. }.
95cd0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
95ce0 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
95cf0 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a E_MUTEX_FAST: {.
95d00 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 p = sqlite
95d10 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 3MallocZero( siz
95d20 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 eof(*p) );.
95d30 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 if( p ){.
95d40 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b p->id = iType;
95d50 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
95d60 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e _mutex_init(&p->
95d70 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 mutex, 0);.
95d80 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
95d90 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
95da0 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 t: {. asser
95db0 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 t( iType-2 >= 0
95dc0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
95dd0 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 iType-2 < Array
95de0 53 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 Size(staticMutex
95df0 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d es) );. p =
95e00 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b &staticMutexes[
95e10 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 iType-2];.
95e20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 p->id = iType;.
95e30 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
95e40 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 }. }. return p
95e50 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 ;.}.../*.** This
95e60 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 routine dealloc
95e70 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c ates a previousl
95e80 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d y.** allocated m
95e90 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 utex. SQLite is
95ea0 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c careful to deal
95eb0 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 locate every.**
95ec0 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c mutex that it al
95ed0 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 locates..*/.stat
95ee0 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d ic void pthreadM
95ef0 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 utexFree(sqlite3
95f00 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
95f10 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 sert( p->nRef==0
95f20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
95f30 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
95f40 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d X_FAST || p->id=
95f50 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
95f60 43 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 CURSIVE );. pth
95f70 72 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 read_mutex_destr
95f80 6f 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 oy(&p->mutex);.
95f90 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
95fa0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
95fb0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
95fc0 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 er() and sqlite3
95fd0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 _mutex_try() rou
95fe0 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a tines attempt.**
95ff0 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 to enter a mute
96000 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 x. If another t
96010 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 hread is already
96020 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 within the mute
96030 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 x,.** sqlite3_mu
96040 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c tex_enter() will
96050 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 block and sqlit
96060 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 e3_mutex_try() w
96070 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 ill return.** SQ
96080 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 LITE_BUSY. The
96090 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
960a0 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 y() interface re
960b0 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a turns SQLITE_OK.
960c0 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 ** upon successf
960d0 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 ul entry. Mutex
960e0 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 es created using
960f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
96100 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 CURSIVE can.** b
96110 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 e entered multip
96120 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 le times by the
96130 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e same thread. In
96140 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c such cases the,
96150 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 .** mutex must b
96160 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 e exited an equa
96170 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 l number of time
96180 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 s before another
96190 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 thread.** can e
961a0 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 nter. If the sa
961b0 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 me thread tries
961c0 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 to enter any oth
961d0 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 er kind of mutex
961e0 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e .** more than on
961f0 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 ce, the behavior
96200 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a is undefined..*
96210 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 /.static void pt
96220 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 hreadMutexEnter(
96230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
96240 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e ){. assert( p->
96250 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
96260 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 _RECURSIVE || pt
96270 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c hreadMutexNothel
96280 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 d(p) );..#ifdef
96290 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e SQLITE_HOMEGROWN
962a0 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 _RECURSIVE_MUTEX
962b0 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 . /* If recursi
962c0 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e ve mutexes are n
962d0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 ot available, th
962e0 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 en we have to gr
962f0 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e ow. ** our own.
96300 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 This implement
96310 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ation assumes th
96320 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c at pthread_equal
96330 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 (). ** is atomi
96340 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e c - that it cann
96350 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20 69 ot be deceived i
96360 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c nto thinking sel
96370 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 f. ** and p->ow
96380 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 ner are equal if
96390 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 p->owner change
963a0 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 s between two va
963b0 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 lues. ** that a
963c0 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 re not equal to
963d0 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 self while the c
963e0 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b omparison is tak
963f0 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 ing place.. **
96400 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 This implementat
96410 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 ion also assumes
96420 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 a coherent cach
96430 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 e - that . ** s
96440 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 eparate processe
96450 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 s cannot read di
96460 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 fferent values f
96470 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a rom the same. *
96480 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65 * address at the
96490 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 same time. If
964a0 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 either of these
964b0 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 two conditions.
964c0 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c ** are not met,
964d0 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 then the mutexe
964e0 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 s will fail and
964f0 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 problems will re
96500 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 sult.. */. {.
96510 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c pthread_t sel
96520 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 f = pthread_self
96530 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e ();. if( p->n
96540 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 Ref>0 && pthread
96550 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c _equal(p->owner,
96560 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 self) ){.
96570 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d p->nRef++;. }
96580 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 else{. pthr
96590 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 ead_mutex_lock(&
965a0 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 p->mutex);.
965b0 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
965c0 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e ==0 );. p->
965d0 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 owner = self;.
965e0 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b p->nRef = 1;
965f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 . }. }.#else
96600 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 . /* Use the bu
96610 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 ilt-in recursive
96620 20 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 79 mutexes if they
96630 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a are available..
96640 20 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d */. pthread_m
96650 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 utex_lock(&p->mu
96660 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 tex);. p->owner
96670 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 = pthread_self(
96680 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a );. p->nRef++;.
96690 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
966a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 QLITE_DEBUG. if
966b0 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 ( p->trace ){.
966c0 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 printf("enter
966d0 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69 mutex %p (%d) wi
966e0 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 th nRef=%d\n", p
966f0 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e , p->trace, p->n
96700 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 Ref);. }.#endif
96710 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 .}.static int pt
96720 68 72 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 hreadMutexTry(sq
96730 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
96740 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 . int rc;. ass
96750 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
96760 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
96770 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 VE || pthreadMut
96780 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a exNotheld(p) );.
96790 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 .#ifdef SQLITE_H
967a0 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 OMEGROWN_RECURSI
967b0 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 VE_MUTEX. /* If
967c0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
967d0 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c es are not avail
967e0 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 able, then we ha
967f0 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 ve to grow. **
96800 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 our own. This i
96810 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 mplementation as
96820 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 sumes that pthre
96830 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 ad_equal(). **
96840 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 is atomic - that
96850 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 it cannot be de
96860 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e ceived into thin
96870 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 king self. ** a
96880 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 nd p->owner are
96890 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 equal if p->owne
968a0 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 r changes betwee
968b0 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a n two values. *
968c0 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 * that are not e
968d0 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 qual to self whi
968e0 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f le the compariso
968f0 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 n is taking plac
96900 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 e.. ** This imp
96910 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f lementation also
96920 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 assumes a coher
96930 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 ent cache - that
96940 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 . ** separate
96950 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 processes cannot
96960 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 read different
96970 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 values from the
96980 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 same. ** addres
96990 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 s at the same ti
969a0 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f me. If either o
969b0 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 f these two cond
969c0 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 itions. ** are
969d0 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 not met, then th
969e0 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 e mutexes will f
969f0 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 ail and problems
96a00 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 will result..
96a10 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 */. {. pthre
96a20 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 ad_t self = pthr
96a30 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 ead_self();.
96a40 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 if( p->nRef>0 &&
96a50 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 pthread_equal(p
96a60 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 ->owner, self) )
96a70 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b {. p->nRef+
96a80 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 +;. rc = SQ
96a90 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c LITE_OK;. }el
96aa0 73 65 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d se if( pthread_m
96ab0 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d utex_trylock(&p-
96ac0 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 >mutex)==0 ){.
96ad0 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e assert( p->n
96ae0 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 Ref==0 );.
96af0 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b p->owner = self;
96b00 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d . p->nRef =
96b10 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 1;. rc = S
96b20 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 QLITE_OK;. }e
96b30 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
96b40 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
96b50 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f }. }.#else. /
96b60 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d * Use the built-
96b70 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 in recursive mut
96b80 65 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65 exes if they are
96b90 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f available.. */
96ba0 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d . if( pthread_m
96bb0 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d utex_trylock(&p-
96bc0 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 >mutex)==0 ){.
96bd0 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 p->owner = pth
96be0 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 read_self();.
96bf0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
96c00 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
96c10 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
96c20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
96c30 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 }.#endif..#ifde
96c40 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
96c50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
96c60 4f 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 OK && p->trace )
96c70 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e {. printf("en
96c80 74 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 ter mutex %p (%d
96c90 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e ) with nRef=%d\n
96ca0 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 ", p, p->trace,
96cb0 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 p->nRef);. }.#e
96cc0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 ndif. return rc
96cd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
96ce0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
96cf0 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 ve() routine exi
96d00 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 ts a mutex that
96d10 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c was.** previousl
96d20 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 y entered by the
96d30 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 same thread. T
96d40 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 he behavior.** i
96d50 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 s undefined if t
96d60 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 he mutex is not
96d70 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 currently entere
96d80 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 d or.** is not c
96d90 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 urrently allocat
96da0 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c ed. SQLite will
96db0 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 never do either
96dc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
96dd0 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 pthreadMutexLea
96de0 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ve(sqlite3_mutex
96df0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
96e00 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 pthreadMutexHeld
96e10 28 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 (p) );. p->nRef
96e20 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d --;. assert( p-
96e30 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 >nRef==0 || p->i
96e40 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
96e50 52 45 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 RECURSIVE );..#i
96e60 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 fdef SQLITE_HOME
96e70 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f GROWN_RECURSIVE_
96e80 4d 55 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e MUTEX. if( p->n
96e90 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 Ref==0 ){. pt
96ea0 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f hread_mutex_unlo
96eb0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 ck(&p->mutex);.
96ec0 20 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 }.#else. pthre
96ed0 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 ad_mutex_unlock(
96ee0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 &p->mutex);.#end
96ef0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
96f00 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d E_DEBUG. if( p-
96f10 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 >trace ){. pr
96f20 69 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 intf("leave mute
96f30 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e x %p (%d) with n
96f40 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d Ref=%d\n", p, p-
96f50 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 >trace, p->nRef)
96f60 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a ;. }.#endif.}..
96f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
96f80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
96f90 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 hods *sqlite3Def
96fa0 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b aultMutex(void){
96fb0 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
96fc0 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
96fd0 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 sMutex = {. p
96fe0 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c threadMutexInit,
96ff0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 . pthreadMute
97000 78 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 xEnd,. pthrea
97010 64 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 dMutexAlloc,.
97020 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 pthreadMutexFre
97030 65 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 e,. pthreadMu
97040 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 texEnter,. pt
97050 68 72 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 hreadMutexTry,.
97060 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c pthreadMutexL
97070 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c eave,.#ifdef SQL
97080 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 ITE_DEBUG. pt
97090 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a hreadMutexHeld,.
970a0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 pthreadMutex
970b0 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 Notheld.#else.
970c0 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 0,. 0.#endi
970d0 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e f. };.. return
970e0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e &sMutex;.}..#en
970f0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 dif /* SQLITE_MU
97100 54 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a TEX_PTHREAD */..
97110 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
97120 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 End of mutex_uni
97130 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
97140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
97160 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
97170 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 Begin file mutex
97180 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _w32.c *********
97190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
971a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
971b0 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
971c0 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 14.**.** The a
971d0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
971e0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
971f0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
97200 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
97210 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
97220 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
97230 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
97240 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
97250 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
97260 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
97270 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
97280 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
97290 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
972a0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
972b0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
972c0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
972d0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
972e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
972f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97320 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
97330 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 contains the C
97340 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 functions that i
97350 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 mplement mutexes
97360 20 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a for win32.**.**
97370 20 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e $Id: mutex_w32.
97380 63 2c 76 20 31 2e 31 38 20 32 30 30 39 2f 30 38 c,v 1.18 2009/08
97390 2f 31 30 20 30 33 3a 32 33 3a 32 31 20 73 68 61 /10 03:23:21 sha
973a0 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a ne Exp $.*/../*.
973b0 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 ** The code in t
973c0 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 his file is only
973d0 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 used if we are
973e0 63 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 compiling multit
973f0 68 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 hreaded.** on a
97400 77 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f win32 system..*/
97410 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d .#ifdef SQLITE_M
97420 55 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 UTEX_W32../*.**
97430 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d Each recursive m
97440 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 utex is an insta
97450 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
97460 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
97470 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
97480 33 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 3_mutex {. CRIT
97490 49 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 ICAL_SECTION mut
974a0 65 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 ex; /* Mutex
974b0 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 controlling the
974c0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 lock */. int id
974d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
974e0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 /* Mutex ty
974f0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 pe */. int nRef
97500 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
97510 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
97520 65 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 enterances */.
97530 44 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 DWORD owner;
97540 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
97550 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 read holding thi
97560 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f s mutex */.};../
97570 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
97580 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 (non-zero) if w
97590 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e e are running un
975a0 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b der WinNT, Win2K
975b0 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 , WinXP,.** or W
975c0 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 inCE. Return fa
975d0 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 lse (zero) for W
975e0 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 in95, Win98, or
975f0 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 WinME..**.** Her
97600 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 e is an interest
97610 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a ing observation:
97620 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 Win95, Win98,
97630 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a and WinME lack.*
97640 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 * the LockFileEx
97650 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 () API. But we
97660 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 can still static
97670 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 ally link agains
97680 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 t that.** API as
97690 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 long as we don'
976a0 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 t call it win ru
976b0 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d nning Win95/98/M
976c0 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a E. A call to.**
976d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
976e0 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 used to determi
976f0 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 ne if the host i
97700 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 s Win95/98/ME or
97710 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 .** WinNT/2K/XP
97720 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 so that we will
97730 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 know whether or
97740 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c not we can safel
97750 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f y call.** the Lo
97760 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a ckFileEx() API..
97770 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 **.** mutexIsNT(
97780 29 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 ) is only used f
97790 6f 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 or the TryEnterC
977a0 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 riticalSection()
977b0 20 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 API call,.** wh
977c0 69 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 ich is only avai
977d0 6c 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 lable if your ap
977e0 70 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f plication was co
977f0 6d 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 mpiled with .**
97800 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 _WIN32_WINNT def
97810 69 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 ined to a value
97820 3e 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 >= 0x0400. Curr
97830 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a ently, the only.
97840 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e ** call to TryEn
97850 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
97860 6f 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 65 on() is #ifdef'e
97870 64 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 d out, so #ifdef
97880 20 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 .** this out as
97890 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a well..*/.#if 0.
978a0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
978b0 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 NCE.# define mut
978c0 65 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 exIsNT() (1).#e
978d0 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 lse. static int
978e0 20 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 mutexIsNT(void)
978f0 7b 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 {. static int
97900 20 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 osType = 0;.
97910 20 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 if( osType==0 )
97920 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f {. OSVERSIO
97930 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 NINFO sInfo;.
97940 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 sInfo.dwOSVer
97950 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 sionInfoSize = s
97960 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 izeof(sInfo);.
97970 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 GetVersionEx
97980 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 (&sInfo);.
97990 6f 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 osType = sInfo.d
979a0 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 wPlatformId==VER
979b0 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f _PLATFORM_WIN32_
979c0 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 NT ? 2 : 1;.
979d0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 }. return osT
979e0 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 ype==2;. }.#end
979f0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f if /* SQLITE_OS_
97a00 57 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a WINCE */.#endif.
97a10 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
97a20 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
97a30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
97a40 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
97a50 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
97a60 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
97a70 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
97a80 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 only inside asse
97a90 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
97aa0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
97ab0 69 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 inMutexHeld(sqli
97ac0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
97ad0 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 return p->nRef!
97ae0 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d =0 && p->owner==
97af0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 GetCurrentThread
97b00 49 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 Id();.}.static i
97b10 6e 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 nt winMutexNothe
97b20 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
97b30 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
97b40 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
97b50 6f 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e owner!=GetCurren
97b60 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 tThreadId();.}.#
97b70 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e endif.../*.** In
97b80 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 itialize and dei
97b90 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 nitialize the mu
97ba0 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a tex subsystem..*
97bb0 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 /.static sqlite3
97bc0 5f 6d 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f _mutex winMutex_
97bd0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d staticMutexes[6]
97be0 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e ;.static int win
97bf0 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 Mutex_isInit = 0
97c00 3b 0a 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78 ;./* As winMutex
97c10 49 6e 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75 Init() and winMu
97c20 74 65 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c texEnd() are cal
97c30 6c 65 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f led as part.** o
97c40 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e f the sqlite3_in
97c50 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c itialize and sql
97c60 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a ite3_shutdown().
97c70 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74 ** processing, t
97c80 68 65 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22 he "interlocked"
97c90 20 6d 61 67 69 63 20 69 73 20 70 72 6f 62 61 62 magic is probab
97ca0 6c 79 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74 ly not.** strict
97cb0 6c 79 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f ly necessary..*/
97cc0 0a 73 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e .static long win
97cd0 4d 75 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a Mutex_lock = 0;.
97ce0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d .static int winM
97cf0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 utexInit(void){
97d00 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 . /* The first
97d10 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 to increment to
97d20 31 20 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e 1 does actual in
97d30 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a itialization */.
97d40 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 if( Interlocke
97d50 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 dCompareExchange
97d60 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c (&winMutex_lock,
97d70 20 31 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 1, 0)==0 ){.
97d80 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 int i;. for(
97d90 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 i=0; i<ArraySize
97da0 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 (winMutex_static
97db0 4d 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a Mutexes); i++){.
97dc0 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 Initialize
97dd0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
97de0 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 &winMutex_static
97df0 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 Mutexes[i].mutex
97e00 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e );. }. win
97e10 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31 Mutex_isInit = 1
97e20 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
97e30 2a 20 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69 * Someone else i
97e40 73 20 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 s in the process
97e50 20 6f 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20 of initing the
97e60 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a static mutexes *
97e70 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 21 77 69 /. while( !wi
97e80 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b nMutex_isInit ){
97e90 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b . Sleep(1);
97ea0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
97eb0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a urn SQLITE_OK; .
97ec0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 }..static int wi
97ed0 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b nMutexEnd(void){
97ee0 20 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 . /* The first
97ef0 20 74 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f to decrement to
97f00 20 30 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73 0 does actual s
97f10 68 75 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77 hutdown . ** (w
97f20 68 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74 hich should be t
97f30 68 65 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64 he last to shutd
97f40 6f 77 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49 own.) */. if( I
97f50 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 nterlockedCompar
97f60 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 eExchange(&winMu
97f70 74 65 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d tex_lock, 0, 1)=
97f80 3d 31 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69 =1 ){. if( wi
97f90 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 nMutex_isInit==1
97fa0 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b ){. int i;
97fb0 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
97fc0 69 3c 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d i<ArraySize(winM
97fd0 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 utex_staticMutex
97fe0 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 es); i++){.
97ff0 20 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 DeleteCritica
98000 6c 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 lSection(&winMut
98010 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
98020 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 [i].mutex);.
98030 20 20 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74 }. winMut
98040 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 ex_isInit = 0;.
98050 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
98060 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
98070 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
98080 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
98090 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 routine allocat
980a0 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 es a new.** mute
980b0 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 x and returns a
980c0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 pointer to it.
980d0 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 If it returns NU
980e0 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 LL.** that means
980f0 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f that a mutex co
98100 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 uld not be alloc
98110 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a ated. SQLite.**
98120 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 will unwind its
98130 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 stack and retur
98140 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 n an error. The
98150 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
98160 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
98170 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 loc() is one of
98180 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f these integer co
98190 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c nstants:.**.** <
981a0 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c ul>.** <li> SQL
981b0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a ITE_MUTEX_FAST.*
981c0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
981d0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a UTEX_RECURSIVE.*
981e0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
981f0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
98200 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 ER.** <li> SQLI
98210 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
98220 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c MEM.** <li> SQL
98230 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
98240 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _MEM2.** <li> S
98250 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
98260 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 IC_PRNG.** <li>
98270 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
98280 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e ATIC_LRU.** <li>
98290 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
982a0 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f TATIC_LRU2.** </
982b0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ul>.**.** The fi
982c0 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 rst two constant
982d0 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f s cause sqlite3_
982e0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f mutex_alloc() to
982f0 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 create.** a new
98300 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 mutex. The new
98310 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 mutex is recurs
98320 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f ive when SQLITE_
98330 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a MUTEX_RECURSIVE.
98340 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e ** is used but n
98350 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 ot necessarily s
98360 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 o when SQLITE_MU
98370 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 TEX_FAST is used
98380 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 ..** The mutex i
98390 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f mplementation do
983a0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
983b0 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f ake a distinctio
983c0 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c n.** between SQL
983d0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
983e0 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d IVE and SQLITE_M
983f0 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 UTEX_FAST if it
98400 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 does.** not want
98410 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 to. But SQLite
98420 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 will only reque
98430 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d st a recursive m
98440 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 utex in.** cases
98450 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 where it really
98460 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 needs one. If
98470 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 a faster non-rec
98480 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 ursive mutex.**
98490 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
984a0 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 s available on t
984b0 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d he host platform
984c0 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 , the mutex subs
984d0 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 ystem.** might r
984e0 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 eturn such a mut
984f0 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 ex in response t
98500 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 o SQLITE_MUTEX_F
98510 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f AST..**.** The o
98520 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 ther allowed par
98530 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 ameters to sqlit
98540 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
98550 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 each return.**
98560 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 a pointer to a s
98570 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e tatic preexistin
98580 67 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 g mutex. Six st
98590 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 atic mutexes are
985a0 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 .** used by the
985b0 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 current version
985c0 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 of SQLite. Futu
985d0 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 re versions of S
985e0 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 QLite.** may add
985f0 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 additional stat
98600 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 ic mutexes. Sta
98610 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 tic mutexes are
98620 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 for internal.**
98630 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e use by SQLite on
98640 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e ly. Application
98650 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 s that use SQLit
98660 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 e mutexes should
98670 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 .** use only the
98680 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 dynamic mutexes
98690 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c returned by SQL
986a0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f ITE_MUTEX_FAST o
986b0 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 r.** SQLITE_MUTE
986c0 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a X_RECURSIVE..**.
986d0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 ** Note that if
986e0 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d one of the dynam
986f0 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 ic mutex paramet
98700 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 ers (SQLITE_MUTE
98710 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c X_FAST.** or SQL
98720 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
98730 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 IVE) is used the
98740 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f n sqlite3_mutex_
98750 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 alloc().** retur
98760 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d ns a different m
98770 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 utex on every ca
98780 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 ll. But for the
98790 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 static .** mute
987a0 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d x types, the sam
987b0 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 e mutex is retur
987c0 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c ned on every cal
987d0 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 l that has.** th
987e0 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 e same type numb
987f0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 er..*/.static sq
98800 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e lite3_mutex *win
98810 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 MutexAlloc(int i
98820 54 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 Type){. sqlite3
98830 5f 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 _mutex *p;.. sw
98840 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 itch( iType ){.
98850 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
98860 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 UTEX_FAST:. c
98870 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
98880 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 _RECURSIVE: {.
98890 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d p = sqlite3M
988a0 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
988b0 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 f(*p) );. i
988c0 66 28 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20 f( p ){ .
988d0 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b p->id = iType;
988e0 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c . Initial
988f0 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 izeCriticalSecti
98900 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 on(&p->mutex);.
98910 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
98920 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
98930 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 fault: {. a
98940 73 73 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f ssert( winMutex_
98950 69 73 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20 isInit==1 );.
98960 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 assert( iType
98970 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 -2 >= 0 );.
98980 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 assert( iType-2
98990 20 3c 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e < ArraySize(win
989a0 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 Mutex_staticMute
989b0 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 xes) );. p
989c0 3d 20 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 = &winMutex_stat
989d0 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d icMutexes[iType-
989e0 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 2];. p->id
989f0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 = iType;. b
98a00 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
98a10 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a return p;.}...
98a20 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
98a30 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 ne deallocates a
98a40 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 previously.** a
98a50 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 llocated mutex.
98a60 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 SQLite is caref
98a70 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 ul to deallocate
98a80 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 every.** mutex
98a90 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 that it allocate
98aa0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
98ab0 64 20 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 d winMutexFree(s
98ac0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
98ad0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b {. assert( p );
98ae0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 . assert( p->nR
98af0 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ef==0 );. asser
98b00 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
98b10 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 _MUTEX_FAST || p
98b20 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
98b30 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a EX_RECURSIVE );.
98b40 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c DeleteCritical
98b50 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 Section(&p->mute
98b60 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 x);. sqlite3_fr
98b70 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ee(p);.}../*.**
98b80 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
98b90 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 x_enter() and sq
98ba0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
98bb0 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d ) routines attem
98bc0 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 pt.** to enter a
98bd0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 mutex. If anot
98be0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c her thread is al
98bf0 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 ready within the
98c00 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 mutex,.** sqlit
98c10 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
98c20 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 will block and
98c30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
98c40 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a y() will return.
98c50 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 ** SQLITE_BUSY.
98c60 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
98c70 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 ex_try() interfa
98c80 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ce returns SQLIT
98c90 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 E_OK.** upon suc
98ca0 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 cessful entry.
98cb0 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 Mutexes created
98cc0 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 using SQLITE_MUT
98cd0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
98ce0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d .** be entered m
98cf0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 ultiple times by
98d00 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
98d10 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 . In such cases
98d20 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d the,.** mutex m
98d30 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e ust be exited an
98d40 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 equal number of
98d50 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e times before an
98d60 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 other thread.**
98d70 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 can enter. If t
98d80 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 he same thread t
98d90 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e ries to enter an
98da0 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 y other kind of
98db0 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 mutex.** more th
98dc0 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 an once, the beh
98dd0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
98de0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
98df0 69 64 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 id winMutexEnter
98e00 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
98e10 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
98e20 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
98e30 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 X_RECURSIVE || w
98e40 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 inMutexNotheld(p
98e50 29 20 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 ) );. EnterCrit
98e60 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e icalSection(&p->
98e70 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e mutex);. p->own
98e80 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 er = GetCurrentT
98e90 68 72 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d hreadId(); . p-
98ea0 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 >nRef++;.}.stati
98eb0 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 c int winMutexTr
98ec0 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 y(sqlite3_mutex
98ed0 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 *p){. int rc =
98ee0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 SQLITE_BUSY;. a
98ef0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
98f00 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
98f10 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 SIVE || winMutex
98f20 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
98f30 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 /*. ** The sqli
98f40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
98f50 72 6f 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 routine is very
98f60 72 61 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 rarely used, and
98f70 20 77 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 when it. ** is
98f80 20 75 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 used it is mere
98f90 6c 79 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 ly an optimizati
98fa0 6f 6e 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b on. So it is OK
98fb0 20 66 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 for it to alway
98fc0 73 0a 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 s. ** fail. .
98fd0 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 **. ** The Try
98fe0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
98ff0 74 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 tion() interface
99000 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
99010 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a le on WinNT.. *
99020 2a 20 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f * And some windo
99030 77 73 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d ws compilers com
99040 70 6c 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 plain if you try
99050 20 74 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f to use it witho
99060 75 74 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f ut. ** first do
99070 69 6e 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 ing some #define
99080 73 20 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 s that prevent S
99090 51 4c 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 QLite from build
990a0 69 6e 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 ing on Win98..
990b0 2a 2a 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 ** For that reas
990c0 6f 6e 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 on, we will omit
990d0 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 this optimizati
990e0 6f 6e 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 on for now. See
990f0 0a 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 . ** ticket #26
99100 38 35 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 85.. */.#if 0.
99110 20 69 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 if( mutexIsNT()
99120 20 26 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 && TryEnterCrit
99130 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e icalSection(&p->
99140 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d mutex) ){. p-
99150 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 >owner = GetCurr
99160 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 entThreadId();.
99170 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 p->nRef++;.
99180 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
99190 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e ;. }.#else. UN
991a0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
991b0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 );.#endif. retu
991c0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
991d0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
991e0 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e x_leave() routin
991f0 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 e exits a mutex
99200 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 that was.** prev
99210 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 iously entered b
99220 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 y the same threa
99230 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 d. The behavior
99240 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 .** is undefined
99250 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 if the mutex is
99260 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 not currently e
99270 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 ntered or.** is
99280 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c not currently al
99290 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 located. SQLite
992a0 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 will never do e
992b0 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 ither..*/.static
992c0 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 void winMutexLe
992d0 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ave(sqlite3_mute
992e0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 x *p){. assert(
992f0 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 p->nRef>0 );.
99300 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 assert( p->owner
99310 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 ==GetCurrentThre
99320 61 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e adId() );. p->n
99330 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 Ref--;. assert(
99340 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 p->nRef==0 || p
99350 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
99360 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a EX_RECURSIVE );.
99370 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 LeaveCriticalS
99380 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 ection(&p->mutex
99390 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 );.}..SQLITE_PRI
993a0 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 VATE sqlite3_mut
993b0 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 ex_methods *sqli
993c0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
993d0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
993e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
993f0 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b thods sMutex = {
99400 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 . winMutexIni
99410 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 t,. winMutexE
99420 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 nd,. winMutex
99430 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 Alloc,. winMu
99440 74 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e texFree,. win
99450 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 MutexEnter,.
99460 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 winMutexTry,.
99470 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a winMutexLeave,.
99480 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
99490 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 BUG. winMutex
994a0 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 Held,. winMut
994b0 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a exNotheld.#else.
994c0 20 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 0,. 0.#en
994d0 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 dif. };.. retu
994e0 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 rn &sMutex;.}.#e
994f0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
99500 55 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a UTEX_W32 */../**
99510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
99520 20 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 of mutex_w32.c
99530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
99560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
99570 69 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 in file malloc.c
99580 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
99590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
995a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
995b0 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
995c0 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
995d0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
995e0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
995f0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
99600 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
99610 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
99620 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
99630 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
99640 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
99650 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
99660 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
99670 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
99680 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
99690 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
996a0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
996b0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
996c0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
996d0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
996e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
996f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99720 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 ***.**.** Memory
99730 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 allocation func
99740 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 tions used throu
99750 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a ghout sqlite..**
99760 0a 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e .** $Id: malloc.
99770 63 2c 76 20 31 2e 36 36 20 32 30 30 39 2f 30 37 c,v 1.66 2009/07
99780 2f 31 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68 /17 11:44:07 drh
99790 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a Exp $.*/../*.**
997a0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 This routine ru
997b0 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f ns when the memo
997c0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 65 ry allocator see
997d0 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 6f s that the.** to
997e0 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 tal memory alloc
997f0 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 74 ation is about t
99800 6f 20 65 78 63 65 65 64 20 74 68 65 20 73 6f 66 o exceed the sof
99810 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 2e t heap.** limit.
99820 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
99830 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 softHeapLimitEnf
99840 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 20 2a 4e orcer(. void *N
99850 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 otUsed, . sqlit
99860 65 33 5f 69 6e 74 36 34 20 4e 6f 74 55 73 65 64 e3_int64 NotUsed
99870 32 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53 69 2,. int allocSi
99880 7a 65 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 ze.){. UNUSED_P
99890 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
998a0 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 d, NotUsed2);.
998b0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
998c0 6d 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a 65 memory(allocSize
998d0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 );.}../*.** Set
998e0 74 68 65 20 73 6f 66 74 20 68 65 61 70 2d 73 69 the soft heap-si
998f0 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 ze limit for the
99900 20 6c 69 62 72 61 72 79 2e 20 50 61 73 73 69 6e library. Passin
99910 67 20 61 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a 20 g a zero or .**
99920 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69 negative value i
99930 6e 64 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d 69 ndicates no limi
99940 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
99950 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f void sqlite3_so
99960 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e ft_heap_limit(in
99970 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f t n){. sqlite3_
99980 75 69 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a 20 uint64 iLimit;.
99990 20 69 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20 20 int overage;.
999a0 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 69 if( n<0 ){. i
999b0 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c Limit = 0;. }el
999c0 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d se{. iLimit =
999d0 20 6e 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 n;. }.#ifndef
999e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
999f0 49 4e 49 54 0a 20 20 73 71 6c 69 74 65 33 5f 69 INIT. sqlite3_i
99a00 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 23 65 6e nitialize();.#en
99a10 64 69 66 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 dif. if( iLimit
99a20 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 >0 ){. sqlite
99a30 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66 3MemoryAlarm(sof
99a40 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 tHeapLimitEnforc
99a50 65 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a er, 0, iLimit);.
99a60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
99a70 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 ite3MemoryAlarm(
99a80 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 0, 0, 0);. }.
99a90 6f 76 65 72 61 67 65 20 3d 20 28 69 6e 74 29 28 overage = (int)(
99aa0 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 sqlite3_memory_u
99ab0 73 65 64 28 29 20 2d 20 28 69 36 34 29 6e 29 3b sed() - (i64)n);
99ac0 0a 20 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30 . if( overage>0
99ad0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
99ae0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f release_memory(o
99af0 76 65 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a verage);. }.}..
99b00 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f /*.** Attempt to
99b10 20 72 65 6c 65 61 73 65 20 75 70 20 74 6f 20 6e release up to n
99b20 20 62 79 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73 bytes of non-es
99b30 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63 sential memory c
99b40 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64 urrently.** held
99b50 20 62 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65 by SQLite. An e
99b60 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 xample of non-es
99b70 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69 sential memory i
99b80 73 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f s memory used to
99b90 0a 2a 2a 20 63 61 63 68 65 20 64 61 74 61 62 61 .** cache databa
99ba0 73 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72 se pages that ar
99bb0 65 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 e not currently
99bc0 69 6e 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 in use..*/.SQLIT
99bd0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
99be0 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
99bf0 28 69 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20 (int n){.#ifdef
99c00 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
99c10 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a MORY_MANAGEMENT.
99c20 20 20 69 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a int nRet = 0;.
99c30 23 69 66 20 30 0a 20 20 6e 52 65 74 20 2b 3d 20 #if 0. nRet +=
99c40 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 sqlite3VdbeRelea
99c50 73 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a 23 65 6e seMemory(n);.#en
99c60 64 69 66 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71 dif. nRet += sq
99c70 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 lite3PcacheRelea
99c80 73 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 29 seMemory(n-nRet)
99c90 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74 3b ;. return nRet;
99ca0 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f .#else. UNUSED_
99cb0 50 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20 PARAMETER(n);.
99cc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
99cd0 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a ;.#endif.}../*.*
99ce0 2a 20 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 * State informat
99cf0 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65 ion local to the
99d00 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
99d10 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f on subsystem..*/
99d20 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 .static SQLITE_W
99d30 53 44 20 73 74 72 75 63 74 20 4d 65 6d 30 47 6c SD struct Mem0Gl
99d40 6f 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62 obal {. /* Numb
99d50 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 er of free pages
99d60 20 66 6f 72 20 73 63 72 61 74 63 68 20 61 6e 64 for scratch and
99d70 20 70 61 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f page-cache memo
99d80 72 79 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72 ry */. u32 nScr
99d90 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 atchFree;. u32
99da0 6e 50 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 71 nPageFree;.. sq
99db0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
99dc0 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d ex; /* M
99dd0 75 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69 7a utex to serializ
99de0 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f e access */.. /
99df0 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d *. ** The alarm
99e00 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74 callback and it
99e10 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68 s arguments. Th
99e20 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63 e mem0.mutex loc
99e30 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68 k will. ** be h
99e40 65 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 61 eld while the ca
99e50 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e llback is runnin
99e60 67 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 61 g. Recursive ca
99e70 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 lls into. ** th
99e80 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 e memory subsyst
99e90 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20 em are allowed,
99ea0 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62 but no new callb
99eb0 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a acks will be. *
99ec0 2a 20 69 73 73 75 65 64 2e 0a 20 20 2a 2f 0a 20 * issued.. */.
99ed0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61 sqlite3_int64 a
99ee0 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a 20 larmThreshold;.
99ef0 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61 6c void (*alarmCal
99f00 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73 71 lback)(void*, sq
99f10 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 lite3_int64,int)
99f20 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d 41 ;. void *alarmA
99f30 72 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 rg;.. /*. ** P
99f40 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 65 ointers to the e
99f50 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c 6f nd of sqlite3Glo
99f60 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 balConfig.pScrat
99f70 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73 71 6c 69 ch and. ** sqli
99f80 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
99f90 70 50 61 67 65 20 74 6f 20 61 20 62 6c 6f 63 6b pPage to a block
99fa0 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 of memory that
99fb0 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20 77 68 69 records. ** whi
99fc0 63 68 20 70 61 67 65 73 20 61 72 65 20 61 76 61 ch pages are ava
99fd0 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 75 ilable.. */. u
99fe0 33 32 20 2a 61 53 63 72 61 74 63 68 46 72 65 65 32 *aScratchFree
99ff0 3b 0a 20 20 75 33 32 20 2a 61 50 61 67 65 46 72 ;. u32 *aPageFr
9a000 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 7b 20 30 ee;.} mem0 = { 0
9a010 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
9a020 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 69 6e 0, 0 };..#defin
9a030 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 73 74 e mem0 GLOBAL(st
9a040 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 2c ruct Mem0Global,
9a050 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e mem0)../*.** In
9a060 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d itialize the mem
9a070 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 ory allocation s
9a080 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c ubsystem..*/.SQL
9a090 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
9a0a0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 sqlite3MallocIni
9a0b0 74 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 t(void){. if( s
9a0c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9a0d0 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20 ig.m.xMalloc==0
9a0e0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ){. sqlite3Me
9a0f0 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 mSetDefault();.
9a100 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d }. memset(&mem
9a110 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 0, 0, sizeof(mem
9a120 30 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 0));. if( sqlit
9a130 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 e3GlobalConfig.b
9a140 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 CoreMutex ){.
9a150 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73 71 mem0.mutex = sq
9a160 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
9a170 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
9a180 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 TIC_MEM);. }.
9a190 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9a1a0 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 lConfig.pScratch
9a1b0 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 && sqlite3Globa
9a1c0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
9a1d0 68 3e 3d 31 30 30 0a 20 20 20 20 20 20 26 26 20 h>=100. &&
9a1e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9a1f0 66 69 67 2e 6e 53 63 72 61 74 63 68 3e 3d 30 20 fig.nScratch>=0
9a200 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
9a210 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
9a220 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 onfig.szScratch
9a230 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c = ROUNDDOWN8(sql
9a240 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9a250 2e 73 7a 53 63 72 61 74 63 68 2d 34 29 3b 0a 20 .szScratch-4);.
9a260 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 mem0.aScratch
9a270 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26 28 28 Free = (u32*)&((
9a280 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f char*)sqlite3Glo
9a290 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 balConfig.pScrat
9a2a0 63 68 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 ch).
9a2b0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c [sqlite3Gl
9a2c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9a2d0 61 74 63 68 2a 73 71 6c 69 74 65 33 47 6c 6f 62 atch*sqlite3Glob
9a2e0 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 alConfig.nScratc
9a2f0 68 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b h];. for(i=0;
9a300 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c i<sqlite3Global
9a310 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3b Config.nScratch;
9a320 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 63 72 i++){ mem0.aScr
9a330 61 74 63 68 46 72 65 65 5b 69 5d 20 3d 20 69 3b atchFree[i] = i;
9a340 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 63 72 }. mem0.nScr
9a350 61 74 63 68 46 72 65 65 20 3d 20 73 71 6c 69 74 atchFree = sqlit
9a360 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9a370 53 63 72 61 74 63 68 3b 0a 20 20 7d 65 6c 73 65 Scratch;. }else
9a380 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f {. sqlite3Glo
9a390 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 balConfig.pScrat
9a3a0 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 ch = 0;. sqli
9a3b0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9a3c0 73 7a 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 szScratch = 0;.
9a3d0 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
9a3e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 GlobalConfig.pPa
9a3f0 67 65 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f ge && sqlite3Glo
9a400 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 balConfig.szPage
9a410 3e 3d 35 31 32 0a 20 20 20 20 20 20 26 26 20 73 >=512. && s
9a420 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9a430 69 67 2e 6e 50 61 67 65 3e 3d 31 20 29 7b 0a 20 ig.nPage>=1 ){.
9a440 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e int i;. in
9a450 74 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20 t overhead;.
9a460 69 6e 74 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f int sz = ROUNDDO
9a470 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 WN8(sqlite3Globa
9a480 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 29 3b lConfig.szPage);
9a490 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c . int n = sql
9a4a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9a4b0 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72 .nPage;. over
9a4c0 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a head = (4*n + sz
9a4d0 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71 - 1)/sz;. sq
9a4e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9a4f0 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68 g.nPage -= overh
9a500 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50 ead;. mem0.aP
9a510 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29 ageFree = (u32*)
9a520 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 &((char*)sqlite3
9a530 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 GlobalConfig.pPa
9a540 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 ge).
9a550 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c [sqlite3Gl
9a560 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 obalConfig.szPag
9a570 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 e*sqlite3GlobalC
9a580 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20 onfig.nPage];.
9a590 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c for(i=0; i<sql
9a5a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9a5b0 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65 .nPage; i++){ me
9a5c0 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20 m0.aPageFree[i]
9a5d0 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e = i; }. mem0.
9a5e0 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69 nPageFree = sqli
9a5f0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9a600 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a nPage;. }else{.
9a610 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9a620 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 lConfig.pPage =
9a630 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 0;. sqlite3Gl
9a640 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 obalConfig.szPag
9a650 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 e = 0;. }. ret
9a660 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 urn sqlite3Globa
9a670 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28 lConfig.m.xInit(
9a680 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9a690 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b fig.m.pAppData);
9a6a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 .}../*.** Deinit
9a6b0 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 ialize the memor
9a6c0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
9a6d0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 system..*/.SQLIT
9a6e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
9a6f0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 qlite3MallocEnd(
9a700 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c void){. if( sql
9a710 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9a720 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a .m.xShutdown ){.
9a730 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9a740 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 lConfig.m.xShutd
9a750 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 own(sqlite3Globa
9a760 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 lConfig.m.pAppDa
9a770 74 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 ta);. }. memse
9a780 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 t(&mem0, 0, size
9a790 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f 2a of(mem0));.}../*
9a7a0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 .** Return the a
9a7b0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 mount of memory
9a7c0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 currently checke
9a7d0 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 d out..*/.SQLITE
9a7e0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 _API sqlite3_int
9a7f0 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 64 sqlite3_memor
9a800 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 20 y_used(void){.
9a810 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c int n, mx;. sql
9a820 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a ite3_int64 res;.
9a830 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 sqlite3_status
9a840 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9a850 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 EMORY_USED, &n,
9a860 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 3d &mx, 0);. res =
9a870 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
9a880 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 n; /* Work arou
9a890 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e nd bug in Borlan
9a8a0 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 d C. Ticket #321
9a8b0 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 6 */. return re
9a8c0 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 s;.}../*.** Retu
9a8d0 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 rn the maximum a
9a8e0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 mount of memory
9a8f0 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62 65 that has ever be
9a900 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 en.** checked ou
9a910 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 74 t since either t
9a920 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 he beginning of
9a930 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 this process.**
9a940 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 or since the mos
9a950 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a t recent reset..
9a960 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
9a970 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 lite3_int64 sqli
9a980 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 te3_memory_highw
9a990 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c ater(int resetFl
9a9a0 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 ag){. int n, mx
9a9b0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ;. sqlite3_int6
9a9c0 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 4 res;. sqlite3
9a9d0 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53 _status(SQLITE_S
9a9e0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
9a9f0 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 65 D, &n, &mx, rese
9aa00 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d 20 tFlag);. res =
9aa10 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6d (sqlite3_int64)m
9aa20 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75 x; /* Work arou
9aa30 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e nd bug in Borlan
9aa40 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31 d C. Ticket #321
9aa50 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65 6 */. return re
9aa60 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e s;.}../*.** Chan
9aa70 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61 6c ge the alarm cal
9aa80 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f lback.*/.SQLITE_
9aa90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
9aaa0 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 0a te3MemoryAlarm(.
9aab0 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 void(*xCallbac
9aac0 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 k)(void *pArg, s
9aad0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 qlite3_int64 use
9aae0 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 d,int N),. void
9aaf0 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 *pArg,. sqlite
9ab00 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 3_int64 iThresho
9ab10 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f ld.){. sqlite3_
9ab20 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
9ab30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e .mutex);. mem0.
9ab40 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 alarmCallback =
9ab50 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d xCallback;. mem
9ab60 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 0.alarmArg = pAr
9ab70 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 g;. mem0.alarmT
9ab80 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 65 hreshold = iThre
9ab90 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 shold;. sqlite3
9aba0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
9abb0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 0.mutex);. retu
9abc0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
9abd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
9abe0 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a OMIT_DEPRECATED.
9abf0 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 /*.** Deprecated
9ac00 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 external interf
9ac10 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 ace. Internal/c
9ac20 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a ore SQLite code.
9ac30 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 ** should call s
9ac40 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 qlite3MemoryAlar
9ac50 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 m..*/.SQLITE_API
9ac60 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d int sqlite3_mem
9ac70 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f 69 ory_alarm(. voi
9ac80 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f d(*xCallback)(vo
9ac90 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 id *pArg, sqlite
9aca0 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 3_int64 used,int
9acb0 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 N),. void *pAr
9acc0 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 g,. sqlite3_int
9acd0 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 64 iThreshold.){
9ace0 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
9acf0 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 61 3MemoryAlarm(xCa
9ad00 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 54 llback, pArg, iT
9ad10 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 6e hreshold);.}.#en
9ad20 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 dif../*.** Trigg
9ad30 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f er the alarm .*/
9ad40 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
9ad50 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 ite3MallocAlarm(
9ad60 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f int nByte){. vo
9ad70 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 id (*xCallback)(
9ad80 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e void*,sqlite3_in
9ad90 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69 t64,int);. sqli
9ada0 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65 te3_int64 nowUse
9adb0 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b d;. void *pArg;
9adc0 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 . if( mem0.alar
9add0 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 mCallback==0 ) r
9ade0 65 74 75 72 6e 3b 0a 20 20 78 43 61 6c 6c 62 61 eturn;. xCallba
9adf0 63 6b 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 ck = mem0.alarmC
9ae00 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73 allback;. nowUs
9ae10 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 ed = sqlite3Stat
9ae20 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 usValue(SQLITE_S
9ae30 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
9ae40 44 29 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d D);. pArg = mem
9ae50 30 2e 61 6c 61 72 6d 41 72 67 3b 0a 20 20 6d 65 0.alarmArg;. me
9ae60 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b m0.alarmCallback
9ae70 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f = 0;. sqlite3_
9ae80 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9ae90 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c .mutex);. xCall
9aea0 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73 back(pArg, nowUs
9aeb0 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71 ed, nByte);. sq
9aec0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
9aed0 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 r(mem0.mutex);.
9aee0 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 mem0.alarmCallb
9aef0 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b ack = xCallback;
9af00 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 . mem0.alarmArg
9af10 20 3d 20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a = pArg;.}../*.*
9af20 2a 20 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c * Do a memory al
9af30 6c 6f 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74 location with st
9af40 61 74 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61 atistics and ala
9af50 72 6d 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65 rms. Assume the
9af60 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 .** lock is alre
9af70 61 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 ady held..*/.sta
9af80 74 69 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 tic int mallocWi
9af90 74 68 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76 thAlarm(int n, v
9afa0 6f 69 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 oid **pp){. int
9afb0 20 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a nFull;. void *
9afc0 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c p;. assert( sql
9afd0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
9afe0 6d 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20 mem0.mutex) );.
9aff0 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 nFull = sqlite3
9b000 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9b010 52 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71 Roundup(n);. sq
9b020 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 lite3StatusSet(S
9b030 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c QLITE_STATUS_MAL
9b040 4c 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 LOC_SIZE, n);.
9b050 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 if( mem0.alarmCa
9b060 6c 6c 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20 llback!=0 ){.
9b070 20 69 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c int nUsed = sql
9b080 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 ite3StatusValue(
9b090 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 SQLITE_STATUS_ME
9b0a0 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20 MORY_USED);.
9b0b0 69 66 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 if( nUsed+nFull
9b0c0 3e 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 >= mem0.alarmThr
9b0d0 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 eshold ){.
9b0e0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 sqlite3MallocAla
9b0f0 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d rm(nFull);. }
9b100 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74 . }. p = sqlit
9b110 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9b120 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b .xMalloc(nFull);
9b130 0a 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20 6d . if( p==0 && m
9b140 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 em0.alarmCallbac
9b150 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 k ){. sqlite3
9b160 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c MallocAlarm(nFul
9b170 6c 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 l);. p = sqli
9b180 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b190 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 m.xMalloc(nFull)
9b1a0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b ;. }. if( p ){
9b1b0 0a 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c . nFull = sql
9b1c0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
9b1d0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 );. sqlite3St
9b1e0 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
9b1f0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
9b200 44 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 D, nFull);. }.
9b210 20 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 *pp = p;. retu
9b220 72 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a rn nFull;.}../*.
9b230 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f ** Allocate memo
9b240 72 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ry. This routin
9b250 65 20 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 65 e is like sqlite
9b260 33 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 70 3_malloc() excep
9b270 74 20 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 73 t that it.** ass
9b280 75 6d 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 umes the memory
9b290 73 75 62 73 79 73 74 65 6d 20 68 61 73 20 61 6c subsystem has al
9b2a0 72 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 ready been initi
9b2b0 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 alized..*/.SQLIT
9b2c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
9b2d0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e sqlite3Malloc(in
9b2e0 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b t n){. void *p;
9b2f0 0a 20 20 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 6e . if( n<=0 || n
9b300 3e 3d 30 78 37 66 66 66 66 66 30 30 20 29 7b 0a >=0x7fffff00 ){.
9b310 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 /* A memory
9b320 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20 allocation of a
9b330 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
9b340 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74 68 which is near th
9b350 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a e maximum. **
9b360 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
9b370 76 61 6c 75 65 20 6d 69 67 68 74 20 63 61 75 73 value might caus
9b380 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76 65 e an integer ove
9b390 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66 20 rflow inside of
9b3a0 74 68 65 0a 20 20 20 20 2a 2a 20 78 4d 61 6c 6c the. ** xMall
9b3b0 6f 63 28 29 2e 20 20 48 65 6e 63 65 20 77 65 20 oc(). Hence we
9b3c0 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 limit the maximu
9b3d0 6d 20 73 69 7a 65 20 74 6f 20 30 78 37 66 66 66 m size to 0x7fff
9b3e0 66 66 30 30 2c 20 67 69 76 69 6e 67 0a 20 20 20 ff00, giving.
9b3f0 20 2a 2a 20 32 35 35 20 62 79 74 65 73 20 6f 66 ** 255 bytes of
9b400 20 6f 76 65 72 68 65 61 64 2e 20 20 53 51 4c 69 overhead. SQLi
9b410 74 65 20 69 74 73 65 6c 66 20 77 69 6c 6c 20 6e te itself will n
9b420 65 76 65 72 20 75 73 65 20 61 6e 79 74 68 69 6e ever use anythin
9b430 67 20 6e 65 61 72 0a 20 20 20 20 2a 2a 20 74 68 g near. ** th
9b440 69 73 20 61 6d 6f 75 6e 74 2e 20 20 54 68 65 20 is amount. The
9b450 6f 6e 6c 79 20 77 61 79 20 74 6f 20 72 65 61 63 only way to reac
9b460 68 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20 77 h the limit is w
9b470 69 74 68 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c ith sqlite3_mall
9b480 6f 63 28 29 20 2a 2f 0a 20 20 20 20 70 20 3d 20 oc() */. p =
9b490 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 0;. }else if( s
9b4a0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9b4b0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 ig.bMemstat ){.
9b4c0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9b4d0 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
9b4e0 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 69 x);. mallocWi
9b4f0 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a thAlarm(n, &p);.
9b500 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9b510 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9b520 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ex);. }else{.
9b530 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f p = sqlite3Glo
9b540 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c balConfig.m.xMal
9b550 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 loc(n);. }. re
9b560 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
9b570 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 This version of
9b580 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
9b590 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75 73 cation is for us
9b5a0 65 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 e by the applica
9b5b0 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 6d tion..** First m
9b5c0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65 6d ake sure the mem
9b5d0 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 69 73 ory subsystem is
9b5e0 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 initialized, th
9b5f0 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c en do the.** all
9b600 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 ocation..*/.SQLI
9b610 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
9b620 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20 ite3_malloc(int
9b630 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 n){.#ifndef SQLI
9b640 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 TE_OMIT_AUTOINIT
9b650 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 . if( sqlite3_i
9b660 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 nitialize() ) re
9b670 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 turn 0;.#endif.
9b680 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d return sqlite3M
9b690 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a alloc(n);.}../*.
9b6a0 2a 2a 20 45 61 63 68 20 74 68 72 65 61 64 20 6d ** Each thread m
9b6b0 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 ay only have a s
9b6c0 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e ingle outstandin
9b6d0 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f g allocation fro
9b6e0 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 6c m.** xScratchMal
9b6f0 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 66 loc(). We verif
9b700 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e y this constrain
9b710 74 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d t in the single-
9b720 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73 65 threaded.** case
9b730 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 61 by setting scra
9b740 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31 tchAllocOut to 1
9b750 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 when an allocat
9b760 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74 61 ion.** is outsta
9b770 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20 69 nding clearing i
9b780 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f 63 t when the alloc
9b790 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a ation is freed..
9b7a0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
9b7b0 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 READSAFE==0 && !
9b7c0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a defined(NDEBUG).
9b7d0 73 74 61 74 69 63 20 69 6e 74 20 73 63 72 61 74 static int scrat
9b7e0 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a chAllocOut = 0;.
9b7f0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 #endif.../*.** A
9b800 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 llocate memory t
9b810 68 61 74 20 69 73 20 74 6f 20 62 65 20 75 73 65 hat is to be use
9b820 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 72 d and released r
9b830 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54 68 ight away..** Th
9b840 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 is routine is si
9b850 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61 28 milar to alloca(
9b860 29 20 69 6e 20 74 68 61 74 20 69 74 20 69 73 20 ) in that it is
9b870 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a 20 not intended.**
9b880 66 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20 77 for situations w
9b890 68 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 here the memory
9b8a0 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c 6f might be held lo
9b8b0 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a 2a ng-term. This.*
9b8c0 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 * routine is int
9b8d0 65 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65 6d ended to get mem
9b8e0 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67 65 ory to old large
9b8f0 20 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 0a transient data.
9b900 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 68 ** structures th
9b910 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72 at would not nor
9b920 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68 65 mally fit on the
9b930 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a 20 stack of an.**
9b940 65 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73 73 embedded process
9b950 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 or..*/.SQLITE_PR
9b960 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
9b970 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 te3ScratchMalloc
9b980 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 (int n){. void
9b990 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e *p;. assert( n>
9b9a0 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 0 );..#if SQLITE
9b9b0 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 _THREADSAFE==0 &
9b9c0 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 & !defined(NDEBU
9b9d0 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 G). /* Verify t
9b9e0 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e hat no more than
9b9f0 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c one scratch all
9ba00 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 ocation per thre
9ba10 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 74 73 74 ad. ** is outst
9ba20 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 anding at one ti
9ba30 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e me. (This is on
9ba40 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68 ly checked in th
9ba50 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 e. ** single-th
9ba60 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e 63 readed case sinc
9ba70 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74 68 e checking in th
9ba80 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 e multi-threaded
9ba90 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f 75 6c 64 case. ** would
9baa0 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f be much more co
9bab0 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 mplicated.) */.
9bac0 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63 68 assert( scratch
9bad0 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a 23 AllocOut==0 );.#
9bae0 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c endif.. if( sql
9baf0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9bb00 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 29 7b 0a .szScratch<n ){.
9bb10 20 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63 68 goto scratch
9bb20 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 65 6c _overflow;. }el
9bb30 73 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 74 65 se{ . sqlite
9bb40 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
9bb50 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 69 m0.mutex);. i
9bb60 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 f( mem0.nScratch
9bb70 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Free==0 ){.
9bb80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
9bb90 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 eave(mem0.mutex)
9bba0 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 63 72 ;. goto scr
9bbb0 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 atch_overflow;.
9bbc0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
9bbd0 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20 3d int i;. i =
9bbe0 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 mem0.aScratchFr
9bbf0 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61 74 ee[--mem0.nScrat
9bc00 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 20 20 69 chFree];. i
9bc10 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 *= sqlite3Globa
9bc20 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 lConfig.szScratc
9bc30 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 h;. sqlite3
9bc40 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 StatusAdd(SQLITE
9bc50 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
9bc60 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 20 20 20 USED, 1);.
9bc70 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 sqlite3StatusSet
9bc80 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 (SQLITE_STATUS_S
9bc90 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b CRATCH_SIZE, n);
9bca0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
9bcb0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9bcc0 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 20 mutex);. p
9bcd0 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 = (void*)&((char
9bce0 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 *)sqlite3GlobalC
9bcf0 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 5b onfig.pScratch)[
9bd00 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 i];. assert
9bd10 28 20 20 28 28 28 75 38 2a 29 70 20 2d 20 28 75 ( (((u8*)p - (u
9bd20 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29 3b 8*)0) & 7)==0 );
9bd30 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 20 53 . }. }.#if S
9bd40 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
9bd50 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 ==0 && !defined(
9bd60 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 NDEBUG). scratc
9bd70 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 hAllocOut = p!=0
9bd80 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 ;.#endif.. retu
9bd90 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63 68 5f 6f rn p;..scratch_o
9bda0 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20 73 verflow:. if( s
9bdb0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9bdc0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 ig.bMemstat ){.
9bdd0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9bde0 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
9bdf0 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 x);. sqlite3S
9be00 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f tatusSet(SQLITE_
9be10 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 STATUS_SCRATCH_S
9be20 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 3d IZE, n);. n =
9be30 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d mallocWithAlarm
9be40 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 28 (n, &p);. if(
9be50 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74 p ) sqlite3Stat
9be60 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
9be70 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 TUS_SCRATCH_OVER
9be80 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73 71 FLOW, n);. sq
9be90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
9bea0 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
9beb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 }else{. p =
9bec0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9bed0 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 fig.m.xMalloc(n)
9bee0 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 ;. }.#if SQLITE
9bef0 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 _THREADSAFE==0 &
9bf00 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 & !defined(NDEBU
9bf10 47 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c 6f G). scratchAllo
9bf20 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e cOut = p!=0;.#en
9bf30 64 69 66 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 dif. return p;
9bf40 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 .}.SQLITE_PRI
9bf50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
9bf60 33 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 3ScratchFree(voi
9bf70 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 d *p){. if( p )
9bf80 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 {..#if SQLITE_TH
9bf90 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 READSAFE==0 && !
9bfa0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a defined(NDEBUG).
9bfb0 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 /* Verify th
9bfc0 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 at no more than
9bfd0 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f one scratch allo
9bfe0 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 61 cation per threa
9bff0 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 74 73 d. ** is outs
9c000 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 tanding at one t
9c010 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f ime. (This is o
9c020 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 nly checked in t
9c030 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 he. ** single
9c040 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73 -threaded case s
9c050 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e ince checking in
9c060 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 the multi-threa
9c070 64 65 64 20 63 61 73 65 0a 20 20 20 20 2a 2a 20 ded case. **
9c080 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f would be much mo
9c090 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 re complicated.)
9c0a0 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
9c0b0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d scratchAllocOut=
9c0c0 3d 31 20 29 3b 0a 20 20 20 20 73 63 72 61 74 63 =1 );. scratc
9c0d0 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23 hAllocOut = 0;.#
9c0e0 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 73 endif.. if( s
9c0f0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9c100 69 67 2e 70 53 63 72 61 74 63 68 3d 3d 30 0a 20 ig.pScratch==0.
9c110 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c 73 || p<s
9c120 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9c130 69 67 2e 70 53 63 72 61 74 63 68 0a 20 20 20 20 ig.pScratch.
9c140 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 76 6f || p>=(vo
9c150 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72 61 74 63 id*)mem0.aScratc
9c160 68 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 69 hFree ){. i
9c170 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c f( sqlite3Global
9c180 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 Config.bMemstat
9c190 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 ){. int i
9c1a0 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 Size = sqlite3Ma
9c1b0 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 llocSize(p);.
9c1c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
9c1d0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
9c1e0 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71 tex);. sq
9c1f0 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
9c200 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
9c210 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d ATCH_OVERFLOW, -
9c220 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 iSize);.
9c230 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
9c240 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9c250 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53 69 EMORY_USED, -iSi
9c260 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ze);. sql
9c270 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9c280 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 .m.xFree(p);.
9c290 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
9c2a0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
9c2b0 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 tex);. }els
9c2c0 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 e{. sqlit
9c2d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
9c2e0 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 .xFree(p);.
9c2f0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
9c300 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
9c310 20 69 20 3d 20 28 69 6e 74 29 28 28 75 38 2a 29 i = (int)((u8*)
9c320 70 20 2d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 p - (u8*)sqlite3
9c330 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9c340 72 61 74 63 68 29 3b 0a 20 20 20 20 20 20 69 20 ratch);. i
9c350 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c /= sqlite3Global
9c360 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 Config.szScratch
9c370 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
9c380 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74 65 i>=0 && i<sqlite
9c390 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 3GlobalConfig.nS
9c3a0 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 20 cratch );.
9c3b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
9c3c0 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ter(mem0.mutex);
9c3d0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d . assert( m
9c3e0 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 em0.nScratchFree
9c3f0 3c 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c 6f <(u32)sqlite3Glo
9c400 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 balConfig.nScrat
9c410 63 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 30 ch );. mem0
9c420 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d 65 .aScratchFree[me
9c430 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 2b m0.nScratchFree+
9c440 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 71 +] = i;. sq
9c450 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
9c460 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
9c470 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b 0a ATCH_USED, -1);.
9c480 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
9c490 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
9c4a0 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d utex);. }. }
9c4b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 69 .}../*.** TRUE i
9c4c0 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73 69 f p is a lookasi
9c4d0 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 de memory alloca
9c4e0 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f 0a tion from db.*/.
9c4f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
9c500 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 74 MIT_LOOKASIDE.st
9c510 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b 61 atic int isLooka
9c520 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 side(sqlite3 *db
9c530 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 , void *p){. re
9c540 74 75 72 6e 20 64 62 20 26 26 20 70 20 26 26 20 turn db && p &&
9c550 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 p>=db->lookaside
9c560 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64 62 2d .pStart && p<db-
9c570 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 3b >lookaside.pEnd;
9c580 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 .}.#else.#define
9c590 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 2c 42 isLookaside(A,B
9c5a0 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ) 0.#endif../*.*
9c5b0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a * Return the siz
9c5c0 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c e of a memory al
9c5d0 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f 75 location previou
9c5e0 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f sly obtained fro
9c5f0 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c m.** sqlite3Mall
9c600 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f oc() or sqlite3_
9c610 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c malloc()..*/.SQL
9c620 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
9c630 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a sqlite3MallocSiz
9c640 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65 e(void *p){. re
9c650 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 turn sqlite3Glob
9c660 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 alConfig.m.xSize
9c670 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 (p);.}.SQLITE_PR
9c680 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
9c690 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 3DbMallocSize(sq
9c6a0 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 lite3 *db, void
9c6b0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64 *p){. assert( d
9c6c0 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
9c6d0 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d mutex_held(db->m
9c6e0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 69 utex) );. if( i
9c6f0 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 sLookaside(db, p
9c700 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
9c710 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a db->lookaside.sz
9c720 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
9c730 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f eturn sqlite3Glo
9c740 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a balConfig.m.xSiz
9c750 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a e(p);. }.}../*.
9c760 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 70 ** Free memory p
9c770 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e reviously obtain
9c780 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d ed from sqlite3M
9c790 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
9c7a0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
9c7b0 74 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70 te3_free(void *p
9c7c0 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 ){. if( p==0 )
9c7d0 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 return;. if( sq
9c7e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9c7f0 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 g.bMemstat ){.
9c800 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9c810 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 enter(mem0.mutex
9c820 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 );. sqlite3St
9c830 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
9c840 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 TATUS_MEMORY_USE
9c850 44 2c 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f D, -sqlite3Mallo
9c860 63 53 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 73 cSize(p));. s
9c870 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9c880 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 ig.m.xFree(p);.
9c890 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9c8a0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 _leave(mem0.mute
9c8b0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 x);. }else{.
9c8c0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9c8d0 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b nfig.m.xFree(p);
9c8e0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 . }.}../*.** Fr
9c8f0 65 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d ee memory that m
9c900 69 67 68 74 20 62 65 20 61 73 73 6f 63 69 61 74 ight be associat
9c910 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 ed with a partic
9c920 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a ular database.**
9c930 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a connection..*/.
9c940 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
9c950 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 65 oid sqlite3DbFre
9c960 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 e(sqlite3 *db, v
9c970 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 oid *p){. asser
9c980 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 t( db==0 || sqli
9c990 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
9c9a0 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 b->mutex) );. i
9c9b0 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 f( isLookaside(d
9c9c0 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f 6f b, p) ){. Loo
9c9d0 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 kasideSlot *pBuf
9c9e0 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f = (LookasideSlo
9c9f0 74 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d 3e t*)p;. pBuf->
9ca00 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b pNext = db->look
9ca10 61 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 aside.pFree;.
9ca20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 db->lookaside.p
9ca30 46 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 20 Free = pBuf;.
9ca40 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e db->lookaside.n
9ca50 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b 0a Out--;. }else{.
9ca60 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
9ca70 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
9ca80 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a * Change the siz
9ca90 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 e of an existing
9caa0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
9cab0 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 on.*/.SQLITE_PRI
9cac0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
9cad0 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a e3Realloc(void *
9cae0 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73 pOld, int nBytes
9caf0 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e ){. int nOld, n
9cb00 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 New;. void *pNe
9cb10 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30 w;. if( pOld==0
9cb20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 ){. return s
9cb30 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 qlite3Malloc(nBy
9cb40 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 tes);. }. if(
9cb50 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 nBytes<=0 ){.
9cb60 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f sqlite3_free(pO
9cb70 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ld);. return
9cb80 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 0;. }. if( nBy
9cb90 74 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30 20 tes>=0x7fffff00
9cba0 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 30 78 ){. /* The 0x
9cbb0 37 66 66 66 66 30 30 20 6c 69 6d 69 74 20 74 65 7ffff00 limit te
9cbc0 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64 20 rm is explained
9cbd0 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 73 in comments on s
9cbe0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a qlite3Malloc() *
9cbf0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a /. return 0;.
9cc00 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c }. nOld = sql
9cc10 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
9cc20 4f 6c 64 29 3b 0a 20 20 6e 4e 65 77 20 3d 20 73 Old);. nNew = s
9cc30 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9cc40 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 42 ig.m.xRoundup(nB
9cc50 79 74 65 73 29 3b 0a 20 20 69 66 28 20 6e 4f 6c ytes);. if( nOl
9cc60 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20 70 d==nNew ){. p
9cc70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20 7d 65 New = pOld;. }e
9cc80 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 lse if( sqlite3G
9cc90 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
9cca0 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 stat ){. sqli
9ccb0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9ccc0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9ccd0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 sqlite3StatusSe
9cce0 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f t(SQLITE_STATUS_
9ccf0 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 79 MALLOC_SIZE, nBy
9cd00 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 73 71 tes);. if( sq
9cd10 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 lite3StatusValue
9cd20 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9cd30 45 4d 4f 52 59 5f 55 53 45 44 29 2b 6e 4e 65 77 EMORY_USED)+nNew
9cd40 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 20 20 20 -nOld >= .
9cd50 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 mem0.alarmTh
9cd60 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 reshold ){.
9cd70 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c sqlite3MallocAl
9cd80 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a arm(nNew-nOld);.
9cd90 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20 3d }. pNew =
9cda0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9cdb0 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 nfig.m.xRealloc(
9cdc0 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 pOld, nNew);.
9cdd0 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20 if( pNew==0 &&
9cde0 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
9cdf0 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ck ){. sqli
9ce00 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e te3MallocAlarm(n
9ce10 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 70 4e Bytes);. pN
9ce20 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ew = sqlite3Glob
9ce30 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c alConfig.m.xReal
9ce40 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b loc(pOld, nNew);
9ce50 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
9ce60 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 6e 4e 65 New ){. nNe
9ce70 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f w = sqlite3Mallo
9ce80 63 53 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20 cSize(pNew);.
9ce90 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9cea0 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
9ceb0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e S_MEMORY_USED, n
9cec0 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d New-nOld);. }
9ced0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
9cee0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 ex_leave(mem0.mu
9cef0 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 tex);. }else{.
9cf00 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 pNew = sqlite
9cf10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9cf20 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e xRealloc(pOld, n
9cf30 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 New);. }. retu
9cf40 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
9cf50 2a 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74 * The public int
9cf60 65 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74 65 erface to sqlite
9cf70 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20 3Realloc. Make
9cf80 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 65 sure that the me
9cf90 6d 6f 72 79 0a 2a 2a 20 73 75 62 73 79 73 74 65 mory.** subsyste
9cfa0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 m is initialized
9cfb0 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 prior to invoki
9cfc0 6e 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63 ng sqliteRealloc
9cfd0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
9cfe0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 void *sqlite3_re
9cff0 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 alloc(void *pOld
9d000 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 , int n){.#ifnde
9d010 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
9d020 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c TOINIT. if( sql
9d030 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
9d040 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 ) ) return 0;.#e
9d050 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 ndif. return sq
9d060 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 4f 6c lite3Realloc(pOl
9d070 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a d, n);.}.../*.**
9d080 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 Allocate and ze
9d090 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a 53 ro memory..*/ .S
9d0a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9d0b0 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f id *sqlite3Mallo
9d0c0 63 5a 65 72 6f 28 69 6e 74 20 6e 29 7b 0a 20 20 cZero(int n){.
9d0d0 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 void *p = sqlite
9d0e0 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 3Malloc(n);. if
9d0f0 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 ( p ){. memse
9d100 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a t(p, 0, n);. }.
9d110 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
9d120 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e *.** Allocate an
9d130 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 d zero memory.
9d140 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f If the allocatio
9d150 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a n fails, make.**
9d160 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 the mallocFaile
9d170 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f d flag in the co
9d180 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 nnection pointer
9d190 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
9d1a0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
9d1b0 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 3DbMallocZero(sq
9d1c0 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e lite3 *db, int n
9d1d0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 ){. void *p = s
9d1e0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
9d1f0 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28 20 w(db, n);. if(
9d200 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 p ){. memset(
9d210 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 p, 0, n);. }.
9d220 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
9d230 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 ** Allocate and
9d240 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66 zero memory. If
9d250 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
9d260 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 fails, make.** t
9d270 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
9d280 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e flag in the conn
9d290 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a ection pointer..
9d2a0 2a 2a 0a 2a 2a 20 49 66 20 64 62 21 3d 30 20 61 **.** If db!=0 a
9d2b0 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 nd db->mallocFai
9d2c0 6c 65 64 20 69 73 20 74 72 75 65 20 28 69 6e 64 led is true (ind
9d2d0 69 63 61 74 69 6e 67 20 61 20 70 72 69 6f 72 20 icating a prior
9d2e0 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 6c 75 72 malloc.** failur
9d2f0 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 e on the same da
9d300 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
9d310 6e 29 20 74 68 65 6e 20 61 6c 77 61 79 73 20 72 n) then always r
9d320 65 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 65 6e 63 eturn 0..** Henc
9d330 65 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c e for a particul
9d340 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ar database conn
9d350 65 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 6d 61 6c ection, once mal
9d360 6c 6f 63 20 73 74 61 72 74 73 0a 2a 2a 20 66 61 loc starts.** fa
9d370 69 6c 69 6e 67 2c 20 69 74 20 66 61 69 6c 73 20 iling, it fails
9d380 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 75 6e 74 consistently unt
9d390 69 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 il mallocFailed
9d3a0 69 73 20 72 65 73 65 74 2e 0a 2a 2a 20 54 68 69 is reset..** Thi
9d3b0 73 20 69 73 20 61 6e 20 69 6d 70 6f 72 74 61 6e s is an importan
9d3c0 74 20 61 73 73 75 6d 70 74 69 6f 6e 2e 20 20 54 t assumption. T
9d3d0 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 70 6c here are many pl
9d3e0 61 63 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 63 aces in the.** c
9d3f0 6f 64 65 20 74 68 61 74 20 64 6f 20 74 68 69 6e ode that do thin
9d400 67 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a gs like this:.**
9d410 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 .** int
9d420 2a 61 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 *a = (int*)sqlit
9d430 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
9d440 2c 20 31 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 , 100);.**
9d450 20 20 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e 74 int *b = (int
9d460 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *)sqlite3DbMallo
9d470 63 52 61 77 28 64 62 2c 20 32 30 30 29 3b 0a 2a cRaw(db, 200);.*
9d480 2a 20 20 20 20 20 20 20 20 20 69 66 28 20 62 20 * if( b
9d490 29 20 61 5b 31 30 5d 20 3d 20 39 3b 0a 2a 2a 0a ) a[10] = 9;.**.
9d4a0 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 ** In other word
9d4b0 73 2c 20 69 66 20 61 20 73 75 62 73 65 71 75 65 s, if a subseque
9d4c0 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20 22 nt malloc (ex: "
9d4d0 62 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20 69 b") worked, it i
9d4e0 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61 s assumed.** tha
9d4f0 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61 6c 6c t all prior mall
9d500 6f 63 73 20 28 65 78 3a 20 22 61 22 29 20 77 6f ocs (ex: "a") wo
9d510 72 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 4c rked too..*/.SQL
9d520 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9d530 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *sqlite3DbMallo
9d540 63 52 61 77 28 73 71 6c 69 74 65 33 20 2a 64 62 cRaw(sqlite3 *db
9d550 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 , int n){. void
9d560 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 64 *p;. assert( d
9d570 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
9d580 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d mutex_held(db->m
9d590 75 74 65 78 29 20 29 3b 0a 23 69 66 6e 64 65 66 utex) );.#ifndef
9d5a0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f SQLITE_OMIT_LOO
9d5b0 4b 41 53 49 44 45 0a 20 20 69 66 28 20 64 62 20 KASIDE. if( db
9d5c0 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 ){. Lookaside
9d5d0 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 20 20 Slot *pBuf;.
9d5e0 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
9d5f0 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 iled ){. re
9d600 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
9d610 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 if( db->lookas
9d620 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 26 26 20 ide.bEnabled &&
9d630 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 n<=db->lookaside
9d640 2e 73 7a 0a 20 20 20 20 20 20 20 20 20 26 26 20 .sz. &&
9d650 28 70 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b (pBuf = db->look
9d660 61 73 69 64 65 2e 70 46 72 65 65 29 21 3d 30 20 aside.pFree)!=0
9d670 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f ){. db->loo
9d680 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 kaside.pFree = p
9d690 42 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Buf->pNext;.
9d6a0 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e db->lookaside.
9d6b0 6e 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 nOut++;. if
9d6c0 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e ( db->lookaside.
9d6d0 6e 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 nOut>db->lookasi
9d6e0 64 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20 20 de.mxOut ){.
9d6f0 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 db->lookasid
9d700 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f e.mxOut = db->lo
9d710 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 okaside.nOut;.
9d720 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
9d730 72 6e 20 28 76 6f 69 64 2a 29 70 42 75 66 3b 0a rn (void*)pBuf;.
9d740 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a }. }.#else.
9d750 20 20 69 66 28 20 64 62 20 26 26 20 64 62 2d 3e if( db && db->
9d760 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
9d770 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
9d780 7d 0a 23 65 6e 64 69 66 0a 20 20 70 20 3d 20 73 }.#endif. p = s
9d790 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b qlite3Malloc(n);
9d7a0 0a 20 20 69 66 28 20 21 70 20 26 26 20 64 62 20 . if( !p && db
9d7b0 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f ){. db->mallo
9d7c0 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d cFailed = 1;. }
9d7d0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
9d7e0 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 /*.** Resize the
9d7f0 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 block of memory
9d800 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
9d810 20 74 6f 20 6e 20 62 79 74 65 73 2e 20 49 66 20 to n bytes. If
9d820 74 68 65 0a 2a 2a 20 72 65 73 69 7a 65 20 66 61 the.** resize fa
9d830 69 6c 73 2c 20 73 65 74 20 74 68 65 20 6d 61 6c ils, set the mal
9d840 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 locFailed flag i
9d850 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e n the connection
9d860 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 object..*/.SQLI
9d870 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
9d880 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f *sqlite3DbReallo
9d890 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 c(sqlite3 *db, v
9d8a0 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a oid *p, int n){.
9d8b0 20 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d 20 30 void *pNew = 0
9d8c0 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d ;. assert( db!=
9d8d0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
9d8e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
9d8f0 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
9d900 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
9d910 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 Failed==0 ){.
9d920 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 if( p==0 ){.
9d930 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
9d940 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
9d950 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 n);. }. i
9d960 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 f( isLookaside(d
9d970 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 20 69 b, p) ){. i
9d980 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 f( n<=db->lookas
9d990 69 64 65 2e 73 7a 20 29 7b 0a 20 20 20 20 20 20 ide.sz ){.
9d9a0 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 return p;.
9d9b0 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d }. pNew =
9d9c0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
9d9d0 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 Raw(db, n);.
9d9e0 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 if( pNew ){.
9d9f0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 memcpy(pNe
9da00 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 73 w, p, db->lookas
9da10 69 64 65 2e 73 7a 29 3b 0a 20 20 20 20 20 20 20 ide.sz);.
9da20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
9da30 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 b, p);. }.
9da40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
9da50 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 pNew = sqlite3_r
9da60 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20 20 ealloc(p, n);.
9da70 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b if( !pNew ){
9da80 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c . db->mal
9da90 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
9daa0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
9dab0 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
9dac0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 }../*.** Attempt
9dad0 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20 70 to reallocate p
9dae0 2e 20 20 49 66 20 74 68 65 20 72 65 61 6c 6c 6f . If the reallo
9daf0 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 cation fails, th
9db00 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e 64 en free p.** and
9db10 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 set the mallocF
9db20 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 ailed flag in th
9db30 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
9db40 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
9db50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
9db60 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f qlite3DbReallocO
9db70 72 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 rFree(sqlite3 *d
9db80 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 b, void *p, int
9db90 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 n){. void *pNew
9dba0 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 ;. pNew = sqlit
9dbb0 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 e3DbRealloc(db,
9dbc0 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 4e p, n);. if( !pN
9dbd0 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ew ){. sqlite
9dbe0 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 3DbFree(db, p);.
9dbf0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
9dc00 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 w;.}../*.** Make
9dc10 20 61 20 63 6f 70 79 20 6f 66 20 61 20 73 74 72 a copy of a str
9dc20 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 ing in memory ob
9dc30 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 tained from sqli
9dc40 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 73 teMalloc(). Thes
9dc50 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 e .** functions
9dc60 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d 61 6c 6c call sqlite3Mall
9dc70 6f 63 52 61 77 28 29 20 64 69 72 65 63 74 6c 79 ocRaw() directly
9dc80 20 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69 instead of sqli
9dc90 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 69 73 teMalloc(). This
9dca0 0a 2a 2a 20 69 73 20 62 65 63 61 75 73 65 20 77 .** is because w
9dcb0 68 65 6e 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 hen memory debug
9dcc0 67 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f ging is turned o
9dcd0 6e 2c 20 74 68 65 73 65 20 74 77 6f 20 66 75 6e n, these two fun
9dce0 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 63 ctions are .** c
9dcf0 61 6c 6c 65 64 20 76 69 61 20 6d 61 63 72 6f 73 alled via macros
9dd00 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 that record the
9dd10 20 63 75 72 72 65 6e 74 20 66 69 6c 65 20 61 6e current file an
9dd20 64 20 6c 69 6e 65 20 6e 75 6d 62 65 72 20 69 6e d line number in
9dd30 20 74 68 65 0a 2a 2a 20 54 68 72 65 61 64 44 61 the.** ThreadDa
9dd40 74 61 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f ta structure..*/
9dd50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9dd60 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 char *sqlite3DbS
9dd70 74 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 trDup(sqlite3 *d
9dd80 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
9dd90 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b ){. char *zNew;
9dda0 0a 20 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20 69 . size_t n;. i
9ddb0 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 f( z==0 ){. r
9ddc0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
9ddd0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
9dde0 33 30 28 7a 29 20 2b 20 31 3b 0a 20 20 61 73 73 30(z) + 1;. ass
9ddf0 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 66 ert( (n&0x7fffff
9de00 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 ff)==n );. zNew
9de10 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
9de20 6f 63 52 61 77 28 64 62 2c 20 28 69 6e 74 29 6e ocRaw(db, (int)n
9de30 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b );. if( zNew ){
9de40 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 . memcpy(zNew
9de50 2c 20 7a 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 , z, n);. }. r
9de60 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 eturn zNew;.}.SQ
9de70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
9de80 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e r *sqlite3DbStrN
9de90 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c Dup(sqlite3 *db,
9dea0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
9deb0 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a int n){. char *
9dec0 7a 4e 65 77 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 zNew;. if( z==0
9ded0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
9dee0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
9def0 28 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d (n&0x7fffffff)==
9df00 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 n );. zNew = sq
9df10 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
9df20 28 64 62 2c 20 6e 2b 31 29 3b 0a 20 20 69 66 28 (db, n+1);. if(
9df30 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d zNew ){. mem
9df40 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b cpy(zNew, z, n);
9df50 0a 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30 . zNew[n] = 0
9df60 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
9df70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 New;.}../*.** Cr
9df80 65 61 74 65 20 61 20 73 74 72 69 6e 67 20 66 72 eate a string fr
9df90 6f 6d 20 74 68 65 20 7a 46 72 6f 6d 61 74 20 61 om the zFromat a
9dfa0 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 rgument and the
9dfb0 76 61 5f 6c 69 73 74 20 74 68 61 74 20 66 6f 6c va_list that fol
9dfc0 6c 6f 77 73 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 lows..** Store t
9dfd0 68 65 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d he string in mem
9dfe0 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
9dff0 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 m sqliteMalloc()
9e000 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 0a 2a 2a and make *pz.**
9e010 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73 point to that s
9e020 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tring..*/.SQLITE
9e030 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
9e040 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 63 lite3SetString(c
9e050 68 61 72 20 2a 2a 70 7a 2c 20 73 71 6c 69 74 65 har **pz, sqlite
9e060 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
9e070 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 r *zFormat, ...)
9e080 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a {. va_list ap;.
9e090 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 76 61 char *z;.. va
9e0a0 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
9e0b0 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 at);. z = sqlit
9e0c0 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a e3VMPrintf(db, z
9e0d0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 Format, ap);. v
9e0e0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c a_end(ap);. sql
9e0f0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a ite3DbFree(db, *
9e100 70 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a pz);. *pz = z;.
9e110 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 }.../*.** This f
9e120 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 unction must be
9e130 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 65 78 called before ex
9e140 69 74 69 6e 67 20 61 6e 79 20 41 50 49 20 66 75 iting any API fu
9e150 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a nction (i.e. .**
9e160 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 returning contr
9e170 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 29 20 ol to the user)
9e180 74 68 61 74 20 68 61 73 20 63 61 6c 6c 65 64 20 that has called
9e190 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f sqlite3_malloc o
9e1a0 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 r.** sqlite3_rea
9e1b0 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 lloc..**.** The
9e1c0 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 returned value i
9e1d0 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 63 6f 70 s normally a cop
9e1e0 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 y of the second
9e1f0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 argument to this
9e200 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 48 6f .** function. Ho
9e210 77 65 76 65 72 2c 20 69 66 20 61 20 6d 61 6c 6c wever, if a mall
9e220 6f 63 28 29 20 66 61 69 6c 75 72 65 20 68 61 73 oc() failure has
9e230 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 20 occurred since
9e240 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 the previous.**
9e250 69 6e 76 6f 63 61 74 69 6f 6e 20 53 51 4c 49 54 invocation SQLIT
9e260 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 E_NOMEM is retur
9e270 6e 65 64 20 69 6e 73 74 65 61 64 2e 20 0a 2a 2a ned instead. .**
9e280 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73 74 .** If the first
9e290 20 61 72 67 75 6d 65 6e 74 2c 20 64 62 2c 20 69 argument, db, i
9e2a0 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 61 s not NULL and a
9e2b0 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 malloc() error
9e2c0 68 61 73 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a has occurred,.**
9e2d0 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63 then the connec
9e2e0 74 69 6f 6e 20 65 72 72 6f 72 2d 63 6f 64 65 20 tion error-code
9e2f0 28 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 (the value retur
9e300 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65 ned by sqlite3_e
9e310 72 72 63 6f 64 65 28 29 29 0a 2a 2a 20 69 73 20 rrcode()).** is
9e320 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f set to SQLITE_NO
9e330 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 MEM..*/.SQLITE_P
9e340 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
9e350 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65 e3ApiExit(sqlite
9e360 33 2a 20 64 62 2c 20 69 6e 74 20 72 63 29 7b 0a 3* db, int rc){.
9e370 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 20 68 /* If the db h
9e380 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c andle is not NUL
9e390 4c 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 L, then we must
9e3a0 68 6f 6c 64 20 74 68 65 20 63 6f 6e 6e 65 63 74 hold the connect
9e3b0 69 6f 6e 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 ion handle. **
9e3c0 6d 75 74 65 78 20 68 65 72 65 2e 20 4f 74 68 65 mutex here. Othe
9e3d0 72 77 69 73 65 20 74 68 65 20 72 65 61 64 20 28 rwise the read (
9e3e0 61 6e 64 20 70 6f 73 73 69 62 6c 65 20 77 72 69 and possible wri
9e3f0 74 65 29 20 6f 66 20 64 62 2d 3e 6d 61 6c 6c 6f te) of db->mallo
9e400 63 46 61 69 6c 65 64 20 0a 20 20 2a 2a 20 69 73 cFailed . ** is
9e410 20 75 6e 73 61 66 65 2c 20 61 73 20 69 73 20 74 unsafe, as is t
9e420 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 he call to sqlit
9e430 65 33 45 72 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a e3Error().. */.
9e440 20 20 61 73 73 65 72 74 28 20 21 64 62 20 7c 7c assert( !db ||
9e450 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
9e460 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
9e470 3b 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 64 ;. if( db && (d
9e480 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
9e490 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f || rc==SQLITE_IO
9e4a0 45 52 52 5f 4e 4f 4d 45 4d 29 20 29 7b 0a 20 20 ERR_NOMEM) ){.
9e4b0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 sqlite3Error(d
9e4c0 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c b, SQLITE_NOMEM,
9e4d0 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 0);. db->mal
9e4e0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 locFailed = 0;.
9e4f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
9e500 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 OMEM;. }. retu
9e510 72 6e 20 72 63 20 26 20 28 64 62 20 3f 20 64 62 rn rc & (db ? db
9e520 2d 3e 65 72 72 4d 61 73 6b 20 3a 20 30 78 66 66 ->errMask : 0xff
9e530 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a );.}../*********
9e540 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 6c ***** End of mal
9e550 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a loc.c **********
9e560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e580 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
9e590 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
9e5a0 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a printf.c ******
9e5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e5d0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ****/./*.** The
9e5e0 22 70 72 69 6e 74 66 22 20 63 6f 64 65 20 74 68 "printf" code th
9e5f0 61 74 20 66 6f 6c 6c 6f 77 73 20 64 61 74 65 73 at follows dates
9e600 20 66 72 6f 6d 20 74 68 65 20 31 39 38 30 27 73 from the 1980's
9e610 2e 20 20 49 74 20 69 73 20 69 6e 0a 2a 2a 20 74 . It is in.** t
9e620 68 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69 6e he public domain
9e630 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 . The original
9e640 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 69 6e 63 comments are inc
9e650 6c 75 64 65 64 20 68 65 72 65 20 66 6f 72 0a 2a luded here for.*
9e660 2a 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 20 * completeness.
9e670 20 54 68 65 79 20 61 72 65 20 76 65 72 79 20 6f They are very o
9e680 75 74 2d 6f 66 2d 64 61 74 65 20 62 75 74 20 6d ut-of-date but m
9e690 69 67 68 74 20 62 65 20 75 73 65 66 75 6c 20 61 ight be useful a
9e6a0 73 0a 2a 2a 20 61 6e 20 68 69 73 74 6f 72 69 63 s.** an historic
9e6b0 61 6c 20 72 65 66 65 72 65 6e 63 65 2e 20 20 4d al reference. M
9e6c0 6f 73 74 20 6f 66 20 74 68 65 20 22 65 6e 68 61 ost of the "enha
9e6d0 6e 63 65 6d 65 6e 74 73 22 20 68 61 76 65 20 62 ncements" have b
9e6e0 65 65 6e 20 62 61 63 6b 65 64 0a 2a 2a 20 6f 75 een backed.** ou
9e6f0 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 75 t so that the fu
9e700 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 6e nctionality is n
9e710 6f 77 20 74 68 65 20 73 61 6d 65 20 61 73 20 73 ow the same as s
9e720 74 61 6e 64 61 72 64 20 70 72 69 6e 74 66 28 29 tandard printf()
9e730 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 69 ..**.** $Id: pri
9e740 6e 74 66 2e 63 2c 76 20 31 2e 31 30 34 20 32 30 ntf.c,v 1.104 20
9e750 30 39 2f 30 36 2f 30 33 20 30 31 3a 32 34 3a 35 09/06/03 01:24:5
9e760 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 4 drh Exp $.**.*
9e770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9e7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
9e7c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 6f The following mo
9e7d0 64 75 6c 65 73 20 69 73 20 61 6e 20 65 6e 68 61 dules is an enha
9e7e0 6e 63 65 64 20 72 65 70 6c 61 63 65 6d 65 6e 74 nced replacement
9e7f0 20 66 6f 72 20 74 68 65 20 22 70 72 69 6e 74 66 for the "printf
9e800 22 20 73 75 62 72 6f 75 74 69 6e 65 73 0a 2a 2a " subroutines.**
9e810 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 73 74 found in the st
9e820 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 andard C library
9e830 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
9e840 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20 61 72 enhancements ar
9e850 65 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 3a 0a e.** supported:.
9e860 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 64 **.** + Ad
9e870 64 69 74 69 6f 6e 61 6c 20 66 75 6e 63 74 69 6f ditional functio
9e880 6e 73 2e 20 20 54 68 65 20 73 74 61 6e 64 61 72 ns. The standar
9e890 64 20 73 65 74 20 6f 66 20 22 70 72 69 6e 74 66 d set of "printf
9e8a0 22 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 " functions.**
9e8b0 20 20 20 20 20 20 20 69 6e 63 6c 75 64 65 73 20 includes
9e8c0 70 72 69 6e 74 66 2c 20 66 70 72 69 6e 74 66 2c printf, fprintf,
9e8d0 20 73 70 72 69 6e 74 66 2c 20 76 70 72 69 6e 74 sprintf, vprint
9e8e0 66 2c 20 76 66 70 72 69 6e 74 66 2c 20 61 6e 64 f, vfprintf, and
9e8f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 76 73 70 72 .** vspr
9e900 69 6e 74 66 2e 20 20 54 68 69 73 20 6d 6f 64 75 intf. This modu
9e910 6c 65 20 61 64 64 73 20 74 68 65 20 66 6f 6c 6c le adds the foll
9e920 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 owing:.**.**
9e930 20 20 20 20 20 20 20 2a 20 20 73 6e 70 72 69 6e * snprin
9e940 74 66 20 2d 2d 20 57 6f 72 6b 73 20 6c 69 6b 65 tf -- Works like
9e950 20 73 70 72 69 6e 74 66 2c 20 62 75 74 20 68 61 sprintf, but ha
9e960 73 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d s an extra argum
9e970 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ent.**
9e980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9e990 77 68 69 63 68 20 69 73 20 74 68 65 20 73 69 7a which is the siz
9e9a0 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 e of the buffer
9e9b0 77 72 69 74 74 65 6e 20 74 6f 2e 0a 2a 2a 0a 2a written to..**.*
9e9c0 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 6d * * m
9e9d0 70 72 69 6e 74 66 20 2d 2d 20 20 53 69 6d 69 6c printf -- Simil
9e9e0 61 72 20 74 6f 20 73 70 72 69 6e 74 66 2e 20 20 ar to sprintf.
9e9f0 57 72 69 74 65 73 20 6f 75 74 70 75 74 20 74 6f Writes output to
9ea00 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 memory.**
9ea10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ea20 20 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f obtained fro
9ea30 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 m malloc..**.**
9ea40 20 20 20 20 20 20 20 20 20 20 2a 20 20 78 70 72 * xpr
9ea50 69 6e 74 66 20 2d 2d 20 20 43 61 6c 6c 73 20 61 intf -- Calls a
9ea60 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 69 73 function to dis
9ea70 70 6f 73 65 20 6f 66 20 6f 75 74 70 75 74 2e 0a pose of output..
9ea80 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **.**
9ea90 2a 20 20 6e 70 72 69 6e 74 66 20 2d 2d 20 20 4e * nprintf -- N
9eaa0 6f 20 6f 75 74 70 75 74 2c 20 62 75 74 20 72 65 o output, but re
9eab0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 turns the number
9eac0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 0a 2a of characters.*
9ead0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
9eae0 20 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 that
9eaf0 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 would have been
9eb00 6f 75 74 70 75 74 20 62 79 20 70 72 69 6e 74 66 output by printf
9eb10 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 ..**.**
9eb20 20 20 2a 20 20 41 20 76 2d 20 76 65 72 73 69 6f * A v- versio
9eb30 6e 20 28 65 78 3a 20 76 73 6e 70 72 69 6e 74 66 n (ex: vsnprintf
9eb40 29 20 6f 66 20 65 76 65 72 79 20 66 75 6e 63 74 ) of every funct
9eb50 69 6f 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 20 ion is also.**
9eb60 20 20 20 20 20 20 20 20 20 20 20 20 73 75 70 70 supp
9eb70 6c 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 lied..**.**
9eb80 20 2b 20 20 41 20 66 65 77 20 65 78 74 65 6e 73 + A few extens
9eb90 69 6f 6e 73 20 74 6f 20 74 68 65 20 66 6f 72 6d ions to the form
9eba0 61 74 74 69 6e 67 20 6e 6f 74 61 74 69 6f 6e 20 atting notation
9ebb0 61 72 65 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a are supported:.*
9ebc0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
9ebd0 20 20 54 68 65 20 22 3d 22 20 66 6c 61 67 20 28 The "=" flag (
9ebe0 73 69 6d 69 6c 61 72 20 74 6f 20 22 2d 22 29 20 similar to "-")
9ebf0 63 61 75 73 65 73 20 74 68 65 20 6f 75 74 70 75 causes the outpu
9ec00 74 20 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 t to be.**
9ec10 20 20 20 20 20 20 20 20 62 65 20 63 65 6e 74 65 be cente
9ec20 72 65 64 20 69 6e 20 74 68 65 20 61 70 70 72 6f red in the appro
9ec30 70 72 69 61 74 65 6c 79 20 73 69 7a 65 64 20 66 priately sized f
9ec40 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ield..**.**
9ec50 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 62 20 * The %b
9ec60 66 69 65 6c 64 20 6f 75 74 70 75 74 73 20 61 6e field outputs an
9ec70 20 69 6e 74 65 67 65 72 20 69 6e 20 62 69 6e 61 integer in bina
9ec80 72 79 20 6e 6f 74 61 74 69 6f 6e 2e 0a 2a 2a 0a ry notation..**.
9ec90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 ** *
9eca0 54 68 65 20 25 63 20 66 69 65 6c 64 20 6e 6f 77 The %c field now
9ecb0 20 61 63 63 65 70 74 73 20 61 20 70 72 65 63 69 accepts a preci
9ecc0 73 69 6f 6e 2e 20 20 54 68 65 20 63 68 61 72 61 sion. The chara
9ecd0 63 74 65 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 cter output.**
9ece0 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 72 is r
9ecf0 65 70 65 61 74 65 64 20 62 79 20 74 68 65 20 6e epeated by the n
9ed00 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 umber of times t
9ed10 68 65 20 70 72 65 63 69 73 69 6f 6e 20 73 70 65 he precision spe
9ed20 63 69 66 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 cifies..**.**
9ed30 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 * The %
9ed40 27 20 66 69 65 6c 64 20 77 6f 72 6b 73 20 6c 69 ' field works li
9ed50 6b 65 20 25 63 2c 20 62 75 74 20 74 61 6b 65 73 ke %c, but takes
9ed60 20 61 73 20 69 74 73 20 63 68 61 72 61 63 74 65 as its characte
9ed70 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 r the.**
9ed80 20 20 20 20 20 20 6e 65 78 74 20 63 68 61 72 61 next chara
9ed90 63 74 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d cter of the form
9eda0 61 74 20 73 74 72 69 6e 67 2c 20 69 6e 73 74 65 at string, inste
9edb0 61 64 20 6f 66 20 74 68 65 20 6e 65 78 74 0a 2a ad of the next.*
9edc0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 * a
9edd0 72 67 75 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 rgument. For ex
9ede0 61 6d 70 6c 65 2c 20 20 70 72 69 6e 74 66 28 22 ample, printf("
9edf0 25 2e 37 38 27 2d 22 29 20 20 70 72 69 6e 74 73 %.78'-") prints
9ee00 20 37 38 20 6d 69 6e 75 73 0a 2a 2a 20 20 20 20 78 minus.**
9ee10 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 73 2c signs,
9ee20 20 74 68 65 20 73 61 6d 65 20 61 73 20 20 70 72 the same as pr
9ee30 69 6e 74 66 28 22 25 2e 37 38 63 22 2c 27 2d 27 intf("%.78c",'-'
9ee40 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 )..**.** +
9ee50 20 57 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 75 When compiled u
9ee60 73 69 6e 67 20 47 43 43 20 6f 6e 20 61 20 53 50 sing GCC on a SP
9ee70 41 52 43 2c 20 74 68 69 73 20 76 65 72 73 69 6f ARC, this versio
9ee80 6e 20 6f 66 20 70 72 69 6e 74 66 20 69 73 0a 2a n of printf is.*
9ee90 2a 20 20 20 20 20 20 20 20 20 66 61 73 74 65 72 * faster
9eea0 20 74 68 61 6e 20 74 68 65 20 6c 69 62 72 61 72 than the librar
9eeb0 79 20 70 72 69 6e 74 66 20 66 6f 72 20 53 55 4e y printf for SUN
9eec0 20 4f 53 20 34 2e 31 2e 0a 2a 2a 0a 2a 2a 20 20 OS 4.1..**.**
9eed0 20 20 20 20 2b 20 20 41 6c 6c 20 66 75 6e 63 74 + All funct
9eee0 69 6f 6e 73 20 61 72 65 20 66 75 6c 6c 79 20 72 ions are fully r
9eef0 65 65 6e 74 72 61 6e 74 2e 0a 2a 2a 0a 2a 2f 0a eentrant..**.*/.
9ef00 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f ./*.** Conversio
9ef10 6e 20 74 79 70 65 73 20 66 61 6c 6c 20 69 6e 74 n types fall int
9ef20 6f 20 76 61 72 69 6f 75 73 20 63 61 74 65 67 6f o various catego
9ef30 72 69 65 73 20 61 73 20 64 65 66 69 6e 65 64 20 ries as defined
9ef40 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 by the.** follow
9ef50 69 6e 67 20 65 6e 75 6d 65 72 61 74 69 6f 6e 2e ing enumeration.
9ef60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 52 41 .*/.#define etRA
9ef70 44 49 58 20 20 20 20 20 20 20 31 20 2f 2a 20 49 DIX 1 /* I
9ef80 6e 74 65 67 65 72 20 74 79 70 65 73 2e 20 20 25 nteger types. %
9ef90 64 2c 20 25 78 2c 20 25 6f 2c 20 61 6e 64 20 73 d, %x, %o, and s
9efa0 6f 20 66 6f 72 74 68 20 2a 2f 0a 23 64 65 66 69 o forth */.#defi
9efb0 6e 65 20 65 74 46 4c 4f 41 54 20 20 20 20 20 20 ne etFLOAT
9efc0 20 32 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 2 /* Floating p
9efd0 6f 69 6e 74 2e 20 20 25 66 20 2a 2f 0a 23 64 65 oint. %f */.#de
9efe0 66 69 6e 65 20 65 74 45 58 50 20 20 20 20 20 20 fine etEXP
9eff0 20 20 20 33 20 2f 2a 20 45 78 70 6f 6e 65 6e 74 3 /* Exponent
9f000 69 6f 6e 61 6c 20 6e 6f 74 61 74 69 6f 6e 2e 20 ional notation.
9f010 25 65 20 61 6e 64 20 25 45 20 2a 2f 0a 23 64 65 %e and %E */.#de
9f020 66 69 6e 65 20 65 74 47 45 4e 45 52 49 43 20 20 fine etGENERIC
9f030 20 20 20 34 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 4 /* Floating
9f040 20 6f 72 20 65 78 70 6f 6e 65 6e 74 69 61 6c 2c or exponential,
9f050 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 65 78 depending on ex
9f060 70 6f 6e 65 6e 74 2e 20 25 67 20 2a 2f 0a 23 64 ponent. %g */.#d
9f070 65 66 69 6e 65 20 65 74 53 49 5a 45 20 20 20 20 efine etSIZE
9f080 20 20 20 20 35 20 2f 2a 20 52 65 74 75 72 6e 20 5 /* Return
9f090 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
9f0a0 74 65 72 73 20 70 72 6f 63 65 73 73 65 64 20 73 ters processed s
9f0b0 6f 20 66 61 72 2e 20 25 6e 20 2a 2f 0a 23 64 65 o far. %n */.#de
9f0c0 66 69 6e 65 20 65 74 53 54 52 49 4e 47 20 20 20 fine etSTRING
9f0d0 20 20 20 36 20 2f 2a 20 53 74 72 69 6e 67 73 2e 6 /* Strings.
9f0e0 20 25 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %s */.#define e
9f0f0 74 44 59 4e 53 54 52 49 4e 47 20 20 20 37 20 2f tDYNSTRING 7 /
9f100 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c * Dynamically al
9f110 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67 73 2e located strings.
9f120 20 25 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %z */.#define e
9f130 74 50 45 52 43 45 4e 54 20 20 20 20 20 38 20 2f tPERCENT 8 /
9f140 2a 20 50 65 72 63 65 6e 74 20 73 79 6d 62 6f 6c * Percent symbol
9f150 2e 20 25 25 20 2a 2f 0a 23 64 65 66 69 6e 65 20 . %% */.#define
9f160 65 74 43 48 41 52 58 20 20 20 20 20 20 20 39 20 etCHARX 9
9f170 2f 2a 20 43 68 61 72 61 63 74 65 72 73 2e 20 25 /* Characters. %
9f180 63 20 2a 2f 0a 2f 2a 20 54 68 65 20 72 65 73 74 c */./* The rest
9f190 20 61 72 65 20 65 78 74 65 6e 73 69 6f 6e 73 2c are extensions,
9f1a0 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 6f not normally fo
9f1b0 75 6e 64 20 69 6e 20 70 72 69 6e 74 66 28 29 20 und in printf()
9f1c0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c */.#define etSQL
9f1d0 45 53 43 41 50 45 20 20 31 30 20 2f 2a 20 53 74 ESCAPE 10 /* St
9f1e0 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27 27 20 rings with '\''
9f1f0 64 6f 75 62 6c 65 64 2e 20 20 25 71 20 2a 2f 0a doubled. %q */.
9f200 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43 #define etSQLESC
9f210 41 50 45 32 20 31 31 20 2f 2a 20 53 74 72 69 6e APE2 11 /* Strin
9f220 67 73 20 77 69 74 68 20 27 5c 27 27 20 64 6f 75 gs with '\'' dou
9f230 62 6c 65 64 20 61 6e 64 20 65 6e 63 6c 6f 73 65 bled and enclose
9f240 64 20 69 6e 20 27 27 2c 0a 20 20 20 20 20 20 20 d in '',.
9f250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f260 20 20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 NULL pointers
9f270 20 72 65 70 6c 61 63 65 64 20 62 79 20 53 51 4c replaced by SQL
9f280 20 4e 55 4c 4c 2e 20 20 25 51 20 2a 2f 0a 23 64 NULL. %Q */.#d
9f290 65 66 69 6e 65 20 65 74 54 4f 4b 45 4e 20 20 20 efine etTOKEN
9f2a0 20 20 20 31 32 20 2f 2a 20 61 20 70 6f 69 6e 74 12 /* a point
9f2b0 65 72 20 74 6f 20 61 20 54 6f 6b 65 6e 20 73 74 er to a Token st
9f2c0 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 ructure */.#defi
9f2d0 6e 65 20 65 74 53 52 43 4c 49 53 54 20 20 20 20 ne etSRCLIST
9f2e0 31 33 20 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 13 /* a pointer
9f2f0 74 6f 20 61 20 53 72 63 4c 69 73 74 20 2a 2f 0a to a SrcList */.
9f300 23 64 65 66 69 6e 65 20 65 74 50 4f 49 4e 54 45 #define etPOINTE
9f310 52 20 20 20 20 31 34 20 2f 2a 20 54 68 65 20 25 R 14 /* The %
9f320 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a p conversion */.
9f330 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43 #define etSQLESC
9f340 41 50 45 33 20 31 35 20 2f 2a 20 25 77 20 2d 3e APE3 15 /* %w ->
9f350 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c Strings with '\
9f360 22 27 20 64 6f 75 62 6c 65 64 20 2a 2f 0a 23 64 "' doubled */.#d
9f370 65 66 69 6e 65 20 65 74 4f 52 44 49 4e 41 4c 20 efine etORDINAL
9f380 20 20 20 31 36 20 2f 2a 20 25 72 20 2d 3e 20 31 16 /* %r -> 1
9f390 73 74 2c 20 32 6e 64 2c 20 33 72 64 2c 20 34 74 st, 2nd, 3rd, 4t
9f3a0 68 2c 20 65 74 63 2e 20 20 45 6e 67 6c 69 73 68 h, etc. English
9f3b0 20 6f 6e 6c 79 20 2a 2f 0a 0a 23 64 65 66 69 6e only */..#defin
9f3c0 65 20 65 74 49 4e 56 41 4c 49 44 20 20 20 20 20 e etINVALID
9f3d0 30 20 2f 2a 20 41 6e 79 20 75 6e 72 65 63 6f 67 0 /* Any unrecog
9f3e0 6e 69 7a 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e nized conversion
9f3f0 20 74 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a type */.../*.**
9f400 20 41 6e 20 22 65 74 42 79 74 65 22 20 69 73 20 An "etByte" is
9f410 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 an 8-bit unsigne
9f420 64 20 76 61 6c 75 65 2e 0a 2a 2f 0a 74 79 70 65 d value..*/.type
9f430 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 def unsigned cha
9f440 72 20 65 74 42 79 74 65 3b 0a 0a 2f 2a 0a 2a 2a r etByte;../*.**
9f450 20 45 61 63 68 20 62 75 69 6c 74 69 6e 20 63 6f Each builtin co
9f460 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 nversion charact
9f470 65 72 20 28 65 78 3a 20 74 68 65 20 27 64 27 20 er (ex: the 'd'
9f480 69 6e 20 22 25 64 22 29 20 69 73 20 64 65 73 63 in "%d") is desc
9f490 72 69 62 65 64 0a 2a 2a 20 62 79 20 61 6e 20 69 ribed.** by an i
9f4a0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
9f4b0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
9f4c0 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 re.*/.typedef st
9f4d0 72 75 63 74 20 65 74 5f 69 6e 66 6f 20 7b 20 20 ruct et_info {
9f4e0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
9f4f0 61 62 6f 75 74 20 65 61 63 68 20 66 6f 72 6d 61 about each forma
9f500 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 63 68 61 t field */. cha
9f510 72 20 66 6d 74 74 79 70 65 3b 20 20 20 20 20 20 r fmttype;
9f520 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 72 /* The for
9f530 6d 61 74 20 66 69 65 6c 64 20 63 6f 64 65 20 6c mat field code l
9f540 65 74 74 65 72 20 2a 2f 0a 20 20 65 74 42 79 74 etter */. etByt
9f550 65 20 62 61 73 65 3b 20 20 20 20 20 20 20 20 20 e base;
9f560 20 20 20 20 2f 2a 20 54 68 65 20 62 61 73 65 20 /* The base
9f570 66 6f 72 20 72 61 64 69 78 20 63 6f 6e 76 65 72 for radix conver
9f580 73 69 6f 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 sion */. etByte
9f590 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
9f5a0 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 /* One or mor
9f5b0 65 20 6f 66 20 46 4c 41 47 5f 20 63 6f 6e 73 74 e of FLAG_ const
9f5c0 61 6e 74 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 ants below */.
9f5d0 65 74 42 79 74 65 20 74 79 70 65 3b 20 20 20 20 etByte type;
9f5e0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 /* Conv
9f5f0 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 ersion paradigm
9f600 2a 2f 0a 20 20 65 74 42 79 74 65 20 63 68 61 72 */. etByte char
9f610 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a set; /*
9f620 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 44 69 Offset into aDi
9f630 67 69 74 73 5b 5d 20 6f 66 20 74 68 65 20 64 69 gits[] of the di
9f640 67 69 74 73 20 73 74 72 69 6e 67 20 2a 2f 0a 20 gits string */.
9f650 20 65 74 42 79 74 65 20 70 72 65 66 69 78 3b 20 etByte prefix;
9f660 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
9f670 73 65 74 20 69 6e 74 6f 20 61 50 72 65 66 69 78 set into aPrefix
9f680 5b 5d 20 6f 66 20 74 68 65 20 70 72 65 66 69 78 [] of the prefix
9f690 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 65 74 5f string */.} et_
9f6a0 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c info;../*.** All
9f6b0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 owed values for
9f6c0 65 74 5f 69 6e 66 6f 2e 66 6c 61 67 73 0a 2a 2f et_info.flags.*/
9f6d0 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 49 .#define FLAG_SI
9f6e0 47 4e 45 44 20 20 31 20 20 20 20 20 2f 2a 20 54 GNED 1 /* T
9f6f0 72 75 65 20 69 66 20 74 68 65 20 76 61 6c 75 65 rue if the value
9f700 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 73 20 73 to convert is s
9f710 69 67 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 igned */.#define
9f720 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 20 32 20 FLAG_INTERN 2
9f730 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 /* True if f
9f740 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 or internal use
9f750 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 only */.#define
9f760 46 4c 41 47 5f 53 54 52 49 4e 47 20 20 34 20 20 FLAG_STRING 4
9f770 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 69 6e 66 69 /* Allow infi
9f780 6e 69 74 79 20 70 72 65 63 69 73 69 6f 6e 20 2a nity precision *
9f790 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f /.../*.** The fo
9f7a0 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 69 73 llowing table is
9f7b0 20 73 65 61 72 63 68 65 64 20 6c 69 6e 65 61 72 searched linear
9f7c0 6c 79 2c 20 73 6f 20 69 74 20 69 73 20 67 6f 6f ly, so it is goo
9f7d0 64 20 74 6f 20 70 75 74 20 74 68 65 0a 2a 2a 20 d to put the.**
9f7e0 6d 6f 73 74 20 66 72 65 71 75 65 6e 74 6c 79 20 most frequently
9f7f0 75 73 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 used conversion
9f800 74 79 70 65 73 20 66 69 72 73 74 2e 0a 2a 2f 0a types first..*/.
9f810 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 static const cha
9f820 72 20 61 44 69 67 69 74 73 5b 5d 20 3d 20 22 30 r aDigits[] = "0
9f830 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 30 123456789ABCDEF0
9f840 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 22 123456789abcdef"
9f850 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 ;.static const c
9f860 68 61 72 20 61 50 72 65 66 69 78 5b 5d 20 3d 20 har aPrefix[] =
9f870 22 2d 78 30 5c 30 30 30 58 30 22 3b 0a 73 74 61 "-x0\000X0";.sta
9f880 74 69 63 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 tic const et_inf
9f890 6f 20 66 6d 74 69 6e 66 6f 5b 5d 20 3d 20 7b 0a o fmtinfo[] = {.
9f8a0 20 20 7b 20 20 27 64 27 2c 20 31 30 2c 20 31 2c { 'd', 10, 1,
9f8b0 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 etRADIX, 0
9f8c0 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 73 27 , 0 },. { 's'
9f8d0 2c 20 20 30 2c 20 34 2c 20 65 74 53 54 52 49 4e , 0, 4, etSTRIN
9f8e0 47 2c 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a G, 0, 0 },.
9f8f0 20 20 7b 20 20 27 67 27 2c 20 20 30 2c 20 31 2c { 'g', 0, 1,
9f900 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 33 etGENERIC, 3
9f910 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 7a 27 0, 0 },. { 'z'
9f920 2c 20 20 30 2c 20 34 2c 20 65 74 44 59 4e 53 54 , 0, 4, etDYNST
9f930 52 49 4e 47 2c 20 20 30 2c 20 20 30 20 7d 2c 0a RING, 0, 0 },.
9f940 20 20 7b 20 20 27 71 27 2c 20 20 30 2c 20 34 2c { 'q', 0, 4,
9f950 20 65 74 53 51 4c 45 53 43 41 50 45 2c 20 20 30 etSQLESCAPE, 0
9f960 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 51 27 , 0 },. { 'Q'
9f970 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 , 0, 4, etSQLES
9f980 43 41 50 45 32 2c 20 30 2c 20 20 30 20 7d 2c 0a CAPE2, 0, 0 },.
9f990 20 20 7b 20 20 27 77 27 2c 20 20 30 2c 20 34 2c { 'w', 0, 4,
9f9a0 20 65 74 53 51 4c 45 53 43 41 50 45 33 2c 20 30 etSQLESCAPE3, 0
9f9b0 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 63 27 , 0 },. { 'c'
9f9c0 2c 20 20 30 2c 20 30 2c 20 65 74 43 48 41 52 58 , 0, 0, etCHARX
9f9d0 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a , 0, 0 },.
9f9e0 20 20 7b 20 20 27 6f 27 2c 20 20 38 2c 20 30 2c { 'o', 8, 0,
9f9f0 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 etRADIX, 0
9fa00 2c 20 20 32 20 7d 2c 0a 20 20 7b 20 20 27 75 27 , 2 },. { 'u'
9fa10 2c 20 31 30 2c 20 30 2c 20 65 74 52 41 44 49 58 , 10, 0, etRADIX
9fa20 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a , 0, 0 },.
9fa30 20 20 7b 20 20 27 78 27 2c 20 31 36 2c 20 30 2c { 'x', 16, 0,
9fa40 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 31 etRADIX, 1
9fa50 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 20 27 58 27 6, 1 },. { 'X'
9fa60 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58 , 16, 0, etRADIX
9fa70 2c 20 20 20 20 20 20 30 2c 20 20 34 20 7d 2c 0a , 0, 4 },.
9fa80 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
9fa90 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
9faa0 4e 54 0a 20 20 7b 20 20 27 66 27 2c 20 20 30 2c NT. { 'f', 0,
9fab0 20 31 2c 20 65 74 46 4c 4f 41 54 2c 20 20 20 20 1, etFLOAT,
9fac0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 0, 0 },. {
9fad0 27 65 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 'e', 0, 1, etEX
9fae0 50 2c 20 20 20 20 20 20 20 20 33 30 2c 20 30 20 P, 30, 0
9faf0 7d 2c 0a 20 20 7b 20 20 27 45 27 2c 20 20 30 2c },. { 'E', 0,
9fb00 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20 1, etEXP,
9fb10 20 20 31 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 14, 0 },. {
9fb20 27 47 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 'G', 0, 1, etGE
9fb30 4e 45 52 49 43 2c 20 20 20 20 31 34 2c 20 30 20 NERIC, 14, 0
9fb40 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7b 20 20 27 },.#endif. { '
9fb50 69 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 i', 10, 1, etRAD
9fb60 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d IX, 0, 0 }
9fb70 2c 0a 20 20 7b 20 20 27 6e 27 2c 20 20 30 2c 20 ,. { 'n', 0,
9fb80 30 2c 20 65 74 53 49 5a 45 2c 20 20 20 20 20 20 0, etSIZE,
9fb90 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
9fba0 25 27 2c 20 20 30 2c 20 30 2c 20 65 74 50 45 52 %', 0, 0, etPER
9fbb0 43 45 4e 54 2c 20 20 20 20 30 2c 20 20 30 20 7d CENT, 0, 0 }
9fbc0 2c 0a 20 20 7b 20 20 27 70 27 2c 20 31 36 2c 20 ,. { 'p', 16,
9fbd0 30 2c 20 65 74 50 4f 49 4e 54 45 52 2c 20 20 20 0, etPOINTER,
9fbe0 20 30 2c 20 20 31 20 7d 2c 0a 0a 2f 2a 20 41 6c 0, 1 },../* Al
9fbf0 6c 20 74 68 65 20 72 65 73 74 20 68 61 76 65 20 l the rest have
9fc00 74 68 65 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 the FLAG_INTERN
9fc10 62 69 74 20 73 65 74 20 61 6e 64 20 61 72 65 20 bit set and are
9fc20 74 68 75 73 20 66 6f 72 20 69 6e 74 65 72 6e 61 thus for interna
9fc30 6c 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 2a 2f l.** use only */
9fc40 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 . { 'T', 0, 2
9fc50 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 , etTOKEN,
9fc60 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 0, 0 },. { 'S
9fc70 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c ', 0, 2, etSRCL
9fc80 49 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c IST, 0, 0 },
9fc90 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 . { 'r', 10, 3
9fca0 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 , etORDINAL,
9fcb0 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 0, 0 },.};../*.
9fcc0 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ** If SQLITE_OMI
9fcd0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
9fce0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 is defined, the
9fcf0 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c n none of the fl
9fd00 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 oating point.**
9fd10 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c conversions will
9fd20 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 work..*/.#ifnde
9fd30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
9fd40 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a OATING_POINT./*.
9fd50 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64 ** "*val" is a d
9fd60 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20 ouble such that
9fd70 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30 0.1 <= *val < 10
9fd80 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 .0.** Return the
9fd90 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20 ascii code for
9fda0 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69 the leading digi
9fdb0 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a t of *val, then.
9fdc0 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61 ** multiply "*va
9fdd0 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65 l" by 10.0 to re
9fde0 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a normalize..**.**
9fdf0 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20 Example:.**
9fe00 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c input: *val
9fe10 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20 = 3.14159.**
9fe20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61 output: *va
9fe30 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75 l = 1.4159 fu
9fe40 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 nction return =
9fe50 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f '3'.**.** The co
9fe60 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e unter *cnt is in
9fe70 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 cremented each t
9fe80 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e ime. After coun
9fe90 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31 ter exceeds.** 1
9fea0 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 6 (the number of
9feb0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 significant dig
9fec0 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20 its in a 64-bit
9fed0 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a float) '0' is.**
9fee0 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64 always returned
9fef0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
9ff00 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e et_getdigit(LON
9ff10 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61 GDOUBLE_TYPE *va
9ff20 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 l, int *cnt){.
9ff30 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e int digit;. LON
9ff40 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a GDOUBLE_TYPE d;.
9ff50 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e if( (*cnt)++ >
9ff60 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30 = 16 ) return '0
9ff70 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e ';. digit = (in
9ff80 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69 t)*val;. d = di
9ff90 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20 git;. digit +=
9ffa0 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a '0';. *val = (*
9ffb0 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 val - d)*10.0;.
9ffc0 20 72 65 74 75 72 6e 20 28 63 68 61 72 29 64 69 return (char)di
9ffd0 67 69 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a git;.}.#endif /*
9ffe0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
9fff0 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a ATING_POINT */..
a0000 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 /*.** Append N s
a0010 70 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20 pace characters
a0020 74 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 to the given str
a0030 69 6e 67 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 ing buffer..*/.s
a0040 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e tatic void appen
a0050 64 53 70 61 63 65 28 53 74 72 41 63 63 75 6d 20 dSpace(StrAccum
a0060 2a 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b *pAccum, int N){
a0070 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
a0080 63 68 61 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d char zSpaces[] =
a0090 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
a00a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
a00b0 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69 ;. while( N>=(i
a00c0 6e 74 29 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 nt)sizeof(zSpace
a00d0 73 29 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 s)-1 ){. sqli
a00e0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a00f0 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65 d(pAccum, zSpace
a0100 73 2c 20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 s, sizeof(zSpace
a0110 73 29 2d 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 s)-1);. N -=
a0120 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d sizeof(zSpaces)-
a0130 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30 1;. }. if( N>0
a0140 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
a0150 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 trAccumAppend(pA
a0160 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e ccum, zSpaces, N
a0170 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
a0180 4f 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74 68 On machines with
a0190 20 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73 a small stack s
a01a0 69 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65 64 ize, you can red
a01b0 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c efine the.** SQL
a01c0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 ITE_PRINT_BUF_SI
a01d0 5a 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68 ZE to be less th
a01e0 61 6e 20 33 35 30 2e 0a 2a 2f 0a 23 69 66 6e 64 an 350..*/.#ifnd
a01f0 65 66 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f ef SQLITE_PRINT_
a0200 42 55 46 5f 53 49 5a 45 0a 23 20 69 66 20 64 65 BUF_SIZE.# if de
a0210 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 fined(SQLITE_SMA
a0220 4c 4c 5f 53 54 41 43 4b 29 0a 23 20 20 20 64 65 LL_STACK).# de
a0230 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e fine SQLITE_PRIN
a0240 54 5f 42 55 46 5f 53 49 5a 45 20 35 30 0a 23 20 T_BUF_SIZE 50.#
a0250 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20 else.# define
a0260 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
a0270 5f 53 49 5a 45 20 33 35 30 0a 23 20 65 6e 64 69 _SIZE 350.# endi
a0280 66 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 f.#endif.#define
a0290 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 54 etBUFSIZE SQLIT
a02a0 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 E_PRINT_BUF_SIZE
a02b0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
a02c0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a output buffer *
a02d0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f /../*.** The roo
a02e0 74 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 t program. All
a02f0 76 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20 variations call
a0300 74 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a this core..**.**
a0310 20 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75 INPUTS:.** fu
a0320 6e 63 20 20 20 54 68 69 73 20 69 73 20 61 20 70 nc This is a p
a0330 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 ointer to a func
a0340 74 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72 65 tion taking thre
a0350 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 e arguments.**
a0360 20 20 20 20 20 20 20 20 20 20 31 2e 20 41 20 70 1. A p
a0370 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 ointer to anythi
a0380 6e 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68 65 ng. Same as the
a0390 20 22 61 72 67 22 20 70 61 72 61 6d 65 74 65 72 "arg" parameter
a03a0 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 ..**
a03b0 32 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 2. A pointer to
a03c0 74 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61 72 the list of char
a03d0 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 acters to be out
a03e0 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 put.**
a03f0 20 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69 73 (Note, this
a0400 20 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c list is NOT nul
a0410 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a l terminated.).*
a0420 2a 20 20 20 20 20 20 20 20 20 20 20 20 33 2e 20 * 3.
a0430 41 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 An integer numbe
a0440 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 r of characters
a0450 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a to be output..**
a0460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
a0470 4e 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62 65 Note: This numbe
a0480 72 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f 2e r might be zero.
a0490 29 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20 ).**.** arg
a04a0 20 54 68 69 73 20 69 73 20 74 68 65 20 70 6f 69 This is the poi
a04b0 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 nter to anything
a04c0 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 70 which will be p
a04d0 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 assed as the.**
a04e0 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 61 first a
a04f0 72 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63 rgument to "func
a0500 22 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20 77 ". Use it for w
a0510 68 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b 65 hatever you like
a0520 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20 ..**.** fmt
a0530 20 54 68 69 73 20 69 73 20 74 68 65 20 66 6f 72 This is the for
a0540 6d 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20 69 mat string, as i
a0550 6e 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e n the usual prin
a0560 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20 t..**.** ap
a0570 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e This is a poin
a0580 74 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f 66 ter to a list of
a0590 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d arguments. Sam
a05a0 65 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 e as in.**
a05b0 20 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a vfprint..**.
a05c0 2a 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20 ** OUTPUTS:.**
a05d0 20 20 20 20 20 20 20 20 54 68 65 20 72 65 74 75 The retu
a05e0 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 rn value is the
a05f0 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
a0600 63 68 61 72 61 63 74 65 72 73 20 73 65 6e 74 20 characters sent
a0610 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 to.** t
a0620 68 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e he function "fun
a0630 63 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31 20 c". Returns -1
a0640 6f 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a on a error..**.*
a0650 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 * Note that the
a0660 6f 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 61 order in which a
a0670 75 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62 6c utomatic variabl
a0680 65 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20 es are declared
a0690 62 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74 below.** seems t
a06a0 6f 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69 66 o make a big dif
a06b0 66 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65 72 ference in deter
a06c0 6d 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74 20 mining how fast
a06d0 74 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77 69 this beast.** wi
a06e0 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ll run..*/.SQLIT
a06f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
a0700 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 0a qlite3VXPrintf(.
a0710 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 StrAccum *pAcc
a0720 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 um,
a0730 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 /* Accumula
a0740 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 te results here
a0750 2a 2f 0a 20 20 69 6e 74 20 75 73 65 45 78 74 65 */. int useExte
a0760 6e 64 65 64 2c 20 20 20 20 20 20 20 20 20 20 20 nded,
a0770 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 /* Allow
a0780 20 65 78 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76 extended %-conv
a0790 65 72 73 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e ersions */. con
a07a0 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20 st char *fmt,
a07b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a07c0 2f 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 /* Format string
a07d0 20 2a 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 */. va_list ap
a07e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a07f0 20 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75 /* argu
a0800 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e ments */.){. in
a0810 74 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 t c;
a0820 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
a0830 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 character in th
a0840 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 e format string
a0850 2a 2f 0a 20 20 63 68 61 72 20 2a 62 75 66 70 74 */. char *bufpt
a0860 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a0870 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
a0880 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 e conversion buf
a0890 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 fer */. int pre
a08a0 63 69 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 cision;
a08b0 20 20 20 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e /* Precision
a08c0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
a08d0 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c field */. int l
a08e0 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20 ength;
a08f0 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
a0900 6f 66 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a of the field */.
a0910 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 int idx;
a0920 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a0930 41 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 A general purpos
a0940 65 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a e loop counter *
a0950 2f 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20 /. int width;
a0960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a0970 2a 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63 * Width of the c
a0980 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a urrent field */.
a0990 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65 etByte flag_le
a09a0 66 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20 ftjustify; /*
a09b0 54 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67 True if "-" flag
a09c0 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 is present */.
a09d0 20 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75 etByte flag_plu
a09e0 73 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54 ssign; /* T
a09f0 72 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20 rue if "+" flag
a0a00 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 is present */.
a0a10 65 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e etByte flag_blan
a0a20 6b 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72 ksign; /* Tr
a0a30 75 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69 ue if " " flag i
a0a40 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 s present */. e
a0a50 74 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72 tByte flag_alter
a0a60 6e 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 nateform; /* Tru
a0a70 65 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73 e if "#" flag is
a0a80 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 present */. et
a0a90 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72 Byte flag_altfor
a0aa0 6d 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 m2; /* True
a0ab0 20 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20 if "!" flag is
a0ac0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
a0ad0 79 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 yte flag_zeropad
a0ae0 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
a0af0 69 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63 if field width c
a0b00 6f 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77 onstant starts w
a0b10 69 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 ith zero */. et
a0b20 42 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 Byte flag_long;
a0b30 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
a0b40 20 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20 if "l" flag is
a0b50 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 present */. etB
a0b60 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e yte flag_longlon
a0b70 67 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 g; /* True
a0b80 69 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 if the "ll" flag
a0b90 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 is present */.
a0ba0 20 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 etByte done;
a0bb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
a0bc0 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 oop termination
a0bd0 66 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 flag */. sqlite
a0be0 5f 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75 _uint64 longvalu
a0bf0 65 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f e; /* Value fo
a0c00 72 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20 r integer types
a0c10 2a 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f */. LONGDOUBLE_
a0c20 54 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20 TYPE realvalue;
a0c30 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61 /* Value for rea
a0c40 6c 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e l types */. con
a0c50 73 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f st et_info *info
a0c60 70 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 p; /* Point
a0c70 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 er to the approp
a0c80 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63 riate info struc
a0c90 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62 ture */. char b
a0ca0 75 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20 uf[etBUFSIZE];
a0cb0 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 /* Conversi
a0cc0 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 on buffer */. c
a0cd0 68 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20 har prefix;
a0ce0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 /* Pre
a0cf0 66 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20 fix character.
a0d00 22 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 "+" or "-" or "
a0d10 22 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 " or '\0'. */.
a0d20 65 74 42 79 74 65 20 78 74 79 70 65 20 3d 20 30 etByte xtype = 0
a0d30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f ; /* Co
a0d40 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 nversion paradig
a0d50 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 m */. char *zEx
a0d60 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 tra;
a0d70 20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72 /* Extra memor
a0d80 79 20 75 73 65 64 20 66 6f 72 20 65 74 54 43 4c y used for etTCL
a0d90 45 53 43 41 50 45 20 63 6f 6e 76 65 72 73 69 6f ESCAPE conversio
a0da0 6e 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ns */.#ifndef SQ
a0db0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
a0dc0 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20 NG_POINT. int
a0dd0 65 78 70 2c 20 65 32 3b 20 20 20 20 20 20 20 20 exp, e2;
a0de0 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e /* exponen
a0df0 74 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62 65 72 t of real number
a0e00 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f s */. double ro
a0e10 75 6e 64 65 72 3b 20 20 20 20 20 20 20 20 20 20 under;
a0e20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 72 6f /* Used for ro
a0e30 75 6e 64 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 unding floating
a0e40 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a point values */.
a0e50 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70 etByte flag_dp
a0e60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a0e70 54 72 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20 True if decimal
a0e80 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 point should be
a0e90 73 68 6f 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 shown */. etByt
a0ea0 65 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 e flag_rtz;
a0eb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a0ec0 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 trailing zeros
a0ed0 73 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 should be remove
a0ee0 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c d */. etByte fl
a0ef0 61 67 5f 65 78 70 3b 20 20 20 20 20 20 20 20 20 ag_exp;
a0f00 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 6f 72 /* True to for
a0f10 63 65 20 64 69 73 70 6c 61 79 20 6f 66 20 74 68 ce display of th
a0f20 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 e exponent */.
a0f30 69 6e 74 20 6e 73 64 3b 20 20 20 20 20 20 20 20 int nsd;
a0f40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
a0f50 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 mber of signific
a0f60 61 6e 74 20 64 69 67 69 74 73 20 72 65 74 75 72 ant digits retur
a0f70 6e 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 ned */.#endif..
a0f80 20 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62 length = 0;. b
a0f90 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 ufpt = 0;. for(
a0fa0 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b ; (c=(*fmt))!=0;
a0fb0 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 66 28 ++fmt){. if(
a0fc0 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 c!='%' ){.
a0fd0 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 20 20 int amt;.
a0fe0 62 75 66 70 74 20 3d 20 28 63 68 61 72 20 2a 29 bufpt = (char *)
a0ff0 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d fmt;. amt =
a1000 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 1;. while(
a1010 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 (c=(*++fmt))!='
a1020 25 27 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74 %' && c!=0 ) amt
a1030 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ++;. sqlite
a1040 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
a1050 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61 pAccum, bufpt, a
a1060 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 mt);. if( c
a1070 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ==0 ) break;.
a1080 20 7d 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a }. if( (c=(*
a1090 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20 ++fmt))==0 ){.
a10a0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a10b0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a10c0 2c 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 20 20 , "%", 1);.
a10d0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
a10e0 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 /* Find out wh
a10f0 61 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65 at flags are pre
a1100 73 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 sent */. flag
a1110 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66 _leftjustify = f
a1120 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 lag_plussign = f
a1130 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 lag_blanksign =
a1140 0a 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 . flag_alter
a1150 6e 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f nateform = flag_
a1160 61 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f altform2 = flag_
a1170 7a 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 zeropad = 0;.
a1180 20 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 done = 0;. d
a1190 6f 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 o{. switch(
a11a0 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 c ){. ca
a11b0 73 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c se '-': flag_l
a11c0 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 eftjustify = 1;
a11d0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
a11e0 20 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66 case '+': f
a11f0 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 lag_plussign = 1
a1200 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a ; break;.
a1210 20 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27 case ' '
a1220 3a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 : flag_blanksi
a1230 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72 gn = 1; br
a1240 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
a1250 65 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c e '#': flag_al
a1260 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b ternateform = 1;
a1270 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a1280 20 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c case '!': fl
a1290 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b ag_altform2 = 1;
a12a0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
a12b0 20 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a case '0':
a12c0 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 flag_zeropad
a12d0 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65 = 1; bre
a12e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61 ak;. defa
a12f0 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 ult: done = 1
a1300 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a1310 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
a1320 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f . }while( !do
a1330 6e 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 ne && (c=(*++fmt
a1340 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 ))!=0 );. /*
a1350 47 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69 Get the field wi
a1360 64 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 dth */. width
a1370 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d = 0;. if( c=
a1380 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69 ='*' ){. wi
a1390 64 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c dth = va_arg(ap,
a13a0 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 int);. if(
a13b0 77 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 width<0 ){.
a13c0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
a13d0 69 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ify = 1;.
a13e0 20 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b width = -width;
a13f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 . }. c
a1400 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d = *++fmt;. }
a1410 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c else{. whil
a1420 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d e( c>='0' && c<=
a1430 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 '9' ){. w
a1440 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20 idth = width*10
a1450 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 + c - '0';.
a1460 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
a1470 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
a1480 20 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42 if( width > etB
a1490 55 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 UFSIZE-10 ){.
a14a0 20 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46 width = etBUF
a14b0 53 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 SIZE-10;. }.
a14c0 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72 /* Get the pr
a14d0 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 ecision */. i
a14e0 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 f( c=='.' ){.
a14f0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 precision = 0
a1500 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 ;. c = *++f
a1510 6d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d mt;. if( c=
a1520 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='*' ){.
a1530 70 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 precision = va_a
a1540 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 rg(ap,int);.
a1550 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
a1560 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 n<0 ) precision
a1570 3d 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 = -precision;.
a1580 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 c = *++fmt
a1590 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
a15a0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e while( c>
a15b0 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 ='0' && c<='9' )
a15c0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63 {. prec
a15d0 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f ision = precisio
a15e0 6e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a n*10 + c - '0';.
a15f0 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b c = *+
a1600 2b 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a +fmt;. }.
a1610 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
a1620 65 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 e{. precisi
a1630 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 on = -1;. }.
a1640 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f /* Get the co
a1650 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f nversion type mo
a1660 64 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 difier */. if
a1670 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 ( c=='l' ){.
a1680 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b flag_long = 1;
a1690 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d . c = *++fm
a16a0 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d t;. if( c==
a16b0 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66 'l' ){. f
a16c0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 lag_longlong = 1
a16d0 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b ;. c = *+
a16e0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 +fmt;. }els
a16f0 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f e{. flag_
a1700 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 longlong = 0;.
a1710 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
a1720 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 . flag_long
a1730 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 = flag_longlong
a1740 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
a1750 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66 /* Fetch the inf
a1760 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 o entry for the
a1770 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 field */. inf
a1780 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 30 5d op = &fmtinfo[0]
a1790 3b 0a 20 20 20 20 78 74 79 70 65 20 3d 20 65 74 ;. xtype = et
a17a0 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 66 6f 72 INVALID;. for
a17b0 28 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 (idx=0; idx<Arra
a17c0 79 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 ySize(fmtinfo);
a17d0 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 idx++){. if
a17e0 28 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 ( c==fmtinfo[idx
a17f0 5d 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 ].fmttype ){.
a1800 20 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d infop = &fm
a1810 74 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 tinfo[idx];.
a1820 20 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e if( useExten
a1830 64 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 ded || (infop->f
a1840 6c 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 lags & FLAG_INTE
a1850 52 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 RN)==0 ){.
a1860 20 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f xtype = info
a1870 70 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20 p->type;.
a1880 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a1890 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
a18a0 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 }. brea
a18b0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
a18c0 0a 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b . zExtra = 0;
a18d0 0a 0a 0a 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 ... /* Limit
a18e0 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20 74 6f the precision to
a18f0 20 70 72 65 76 65 6e 74 20 6f 76 65 72 66 6c 6f prevent overflo
a1900 77 69 6e 67 20 62 75 66 5b 5d 20 64 75 72 69 6e wing buf[] durin
a1910 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a g conversion */.
a1920 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
a1930 6e 3e 65 74 42 55 46 53 49 5a 45 2d 34 30 20 26 n>etBUFSIZE-40 &
a1940 26 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 & (infop->flags
a1950 26 20 46 4c 41 47 5f 53 54 52 49 4e 47 29 3d 3d & FLAG_STRING)==
a1960 30 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69 0 ){. preci
a1970 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 sion = etBUFSIZE
a1980 2d 34 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 -40;. }..
a1990 2f 2a 0a 20 20 20 20 2a 2a 20 41 74 20 74 68 69 /*. ** At thi
a19a0 73 20 70 6f 69 6e 74 2c 20 76 61 72 69 61 62 6c s point, variabl
a19b0 65 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a es are initializ
a19c0 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 ed as follows:.
a19d0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 66 **. ** f
a19e0 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 lag_alternatefor
a19f0 6d 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 m TRUE
a1a00 69 66 20 61 20 27 23 27 20 69 73 20 70 72 65 73 if a '#' is pres
a1a10 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c ent.. ** fl
a1a20 61 67 5f 61 6c 74 66 6f 72 6d 32 20 20 20 20 20 ag_altform2
a1a30 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 TRUE i
a1a40 66 20 61 20 27 21 27 20 69 73 20 70 72 65 73 65 f a '!' is prese
a1a50 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 nt.. ** fla
a1a60 67 5f 70 6c 75 73 73 69 67 6e 20 20 20 20 20 20 g_plussign
a1a70 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 TRUE if
a1a80 20 61 20 27 2b 27 20 69 73 20 70 72 65 73 65 6e a '+' is presen
a1a90 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 t.. ** flag
a1aa0 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 20 20 20 _leftjustify
a1ab0 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 TRUE if
a1ac0 61 20 27 2d 27 20 69 73 20 70 72 65 73 65 6e 74 a '-' is present
a1ad0 20 6f 72 20 69 66 20 74 68 65 0a 20 20 20 20 2a or if the. *
a1ae0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
a1af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1b00 66 69 65 6c 64 20 77 69 64 74 68 20 77 61 73 20 field width was
a1b10 6e 65 67 61 74 69 76 65 2e 0a 20 20 20 20 2a 2a negative.. **
a1b20 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 flag_zeropad
a1b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 T
a1b40 52 55 45 20 69 66 20 74 68 65 20 77 69 64 74 68 RUE if the width
a1b50 20 62 65 67 61 6e 20 77 69 74 68 20 30 2e 0a 20 began with 0..
a1b60 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e ** flag_lon
a1b70 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g
a1b80 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 TRUE if the
a1b90 6c 65 74 74 65 72 20 27 6c 27 20 28 65 6c 6c 29 letter 'l' (ell)
a1ba0 20 70 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a prefixed. **
a1bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
a1bd0 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 he conversion ch
a1be0 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 aracter.. **
a1bf0 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 flag_longlong
a1c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 TR
a1c10 55 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 UE if the letter
a1c20 20 27 6c 6c 27 20 28 65 6c 6c 20 65 6c 6c 29 20 'll' (ell ell)
a1c30 70 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a 20 prefixed. **
a1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
a1c60 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 e conversion cha
a1c70 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20 racter.. **
a1c80 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 flag_blanksign
a1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 TRU
a1ca0 45 20 69 66 20 61 20 27 20 27 20 69 73 20 70 72 E if a ' ' is pr
a1cb0 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 esent.. **
a1cc0 77 69 64 74 68 20 20 20 20 20 20 20 20 20 20 20 width
a1cd0 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
a1ce0 73 70 65 63 69 66 69 65 64 20 66 69 65 6c 64 20 specified field
a1cf0 77 69 64 74 68 2e 20 20 54 68 69 73 20 69 73 0a width. This is.
a1d00 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
a1d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1d20 20 20 20 20 20 61 6c 77 61 79 73 20 6e 6f 6e 2d always non-
a1d30 6e 65 67 61 74 69 76 65 2e 20 20 5a 65 72 6f 20 negative. Zero
a1d40 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a is the default..
a1d50 20 20 20 20 2a 2a 20 20 20 70 72 65 63 69 73 69 ** precisi
a1d60 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 on
a1d70 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69 The specifi
a1d80 65 64 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 ed precision. T
a1d90 68 65 20 64 65 66 61 75 6c 74 0a 20 20 20 20 2a he default. *
a1da0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
a1db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1dc0 69 73 20 2d 31 2e 0a 20 20 20 20 2a 2a 20 20 20 is -1.. **
a1dd0 78 74 79 70 65 20 20 20 20 20 20 20 20 20 20 20 xtype
a1de0 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
a1df0 63 6c 61 73 73 20 6f 66 20 74 68 65 20 63 6f 6e class of the con
a1e00 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 version.. **
a1e10 20 20 69 6e 66 6f 70 20 20 20 20 20 20 20 20 20 infop
a1e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 6f Po
a1e30 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 inter to the app
a1e40 72 6f 70 72 69 61 74 65 20 69 6e 66 6f 20 73 74 ropriate info st
a1e50 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ruct.. */.
a1e60 20 73 77 69 74 63 68 28 20 78 74 79 70 65 20 29 switch( xtype )
a1e70 7b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50 {. case etP
a1e80 4f 49 4e 54 45 52 3a 0a 20 20 20 20 20 20 20 20 OINTER:.
a1e90 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 flag_longlong =
a1ea0 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 sizeof(char*)==s
a1eb0 69 7a 65 6f 66 28 69 36 34 29 3b 0a 20 20 20 20 izeof(i64);.
a1ec0 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 flag_long =
a1ed0 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 sizeof(char*)==s
a1ee0 69 7a 65 6f 66 28 6c 6f 6e 67 20 69 6e 74 29 3b izeof(long int);
a1ef0 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c . /* Fall
a1f00 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74 68 through into th
a1f10 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 e next case */.
a1f20 20 20 20 20 20 63 61 73 65 20 65 74 4f 52 44 49 case etORDI
a1f30 4e 41 4c 3a 0a 20 20 20 20 20 20 63 61 73 65 20 NAL:. case
a1f40 65 74 52 41 44 49 58 3a 0a 20 20 20 20 20 20 20 etRADIX:.
a1f50 20 69 66 28 20 69 6e 66 6f 70 2d 3e 66 6c 61 67 if( infop->flag
a1f60 73 20 26 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 s & FLAG_SIGNED
a1f70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34 ){. i64
a1f80 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 v;. if
a1f90 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 ( flag_longlong
a1fa0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 ){. v
a1fb0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 36 34 = va_arg(ap,i64
a1fc0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
a1fd0 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 se if( flag_long
a1fe0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a1ff0 76 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f v = va_arg(ap,lo
a2000 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 ng int);.
a2010 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a2020 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 v = va_arg
a2030 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 (ap,int);.
a2040 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
a2050 69 66 28 20 76 3c 30 20 29 7b 0a 20 20 20 20 20 if( v<0 ){.
a2060 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 longvalue
a2070 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 20 = -v;.
a2080 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b prefix = '-';
a2090 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
a20a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f {. lo
a20b0 6e 67 76 61 6c 75 65 20 3d 20 76 3b 0a 20 20 20 ngvalue = v;.
a20c0 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 if( fla
a20d0 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 g_plussign )
a20e0 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27 prefix = '+'
a20f0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c ;. el
a2100 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e se if( flag_blan
a2110 6b 73 69 67 6e 20 29 20 20 70 72 65 66 69 78 20 ksign ) prefix
a2120 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20 = ' ';.
a2130 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 else
a2140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 pr
a2150 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 efix = 0;.
a2160 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
a2170 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 lse{. i
a2180 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 f( flag_longlong
a2190 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a21a0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 longvalue = va_a
a21b0 72 67 28 61 70 2c 75 36 34 29 3b 0a 20 20 20 20 rg(ap,u64);.
a21c0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
a21d0 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 flag_long ){.
a21e0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c longval
a21f0 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 ue = va_arg(ap,u
a2200 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 nsigned long int
a2210 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
a2220 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
a2230 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 longvalue = va_a
a2240 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 rg(ap,unsigned i
a2250 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d nt);. }
a2260 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 . prefi
a2270 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d x = 0;. }
a2280 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f 6e . if( lon
a2290 67 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c 61 67 gvalue==0 ) flag
a22a0 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d _alternateform =
a22b0 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0;. if(
a22c0 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 flag_zeropad &&
a22d0 70 72 65 63 69 73 69 6f 6e 3c 77 69 64 74 68 2d precision<width-
a22e0 28 70 72 65 66 69 78 21 3d 30 29 20 29 7b 0a 20 (prefix!=0) ){.
a22f0 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 precisi
a2300 6f 6e 20 3d 20 77 69 64 74 68 2d 28 70 72 65 66 on = width-(pref
a2310 69 78 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 ix!=0);.
a2320 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20 }. bufpt
a2330 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 = &buf[etBUFSIZE
a2340 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 -1];. if(
a2350 20 78 74 79 70 65 3d 3d 65 74 4f 52 44 49 4e 41 xtype==etORDINA
a2360 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 L ){. s
a2370 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
a2380 20 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73 74 6e zOrd[] = "thstn
a2390 64 72 64 22 3b 0a 20 20 20 20 20 20 20 20 20 20 drd";.
a23a0 69 6e 74 20 78 20 3d 20 28 69 6e 74 29 28 6c 6f int x = (int)(lo
a23b0 6e 67 76 61 6c 75 65 20 25 20 31 30 29 3b 0a 20 ngvalue % 10);.
a23c0 20 20 20 20 20 20 20 20 20 69 66 28 20 78 3e 3d if( x>=
a23d0 34 20 7c 7c 20 28 6c 6f 6e 67 76 61 6c 75 65 2f 4 || (longvalue/
a23e0 31 30 29 25 31 30 3d 3d 31 20 29 7b 0a 20 20 20 10)%10==1 ){.
a23f0 20 20 20 20 20 20 20 20 20 78 20 3d 20 30 3b 0a x = 0;.
a2400 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
a2410 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 53 buf[etBUFS
a2420 49 5a 45 2d 33 5d 20 3d 20 7a 4f 72 64 5b 78 2a IZE-3] = zOrd[x*
a2430 32 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 2];. bu
a2440 66 5b 65 74 42 55 46 53 49 5a 45 2d 32 5d 20 3d f[etBUFSIZE-2] =
a2450 20 7a 4f 72 64 5b 78 2a 32 2b 31 5d 3b 0a 20 20 zOrd[x*2+1];.
a2460 20 20 20 20 20 20 20 20 62 75 66 70 74 20 2d 3d bufpt -=
a2470 20 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 2;. }.
a2480 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 {.
a2490 20 20 72 65 67 69 73 74 65 72 20 63 6f 6e 73 74 register const
a24a0 20 63 68 61 72 20 2a 63 73 65 74 3b 20 20 20 20 char *cset;
a24b0 20 20 2f 2a 20 55 73 65 20 72 65 67 69 73 74 65 /* Use registe
a24c0 72 73 20 66 6f 72 20 73 70 65 65 64 20 2a 2f 0a rs for speed */.
a24d0 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74 regist
a24e0 65 72 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 er int base;.
a24f0 20 20 20 20 20 20 20 63 73 65 74 20 3d 20 26 61 cset = &a
a2500 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 Digits[infop->ch
a2510 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 arset];.
a2520 20 20 62 61 73 65 20 3d 20 69 6e 66 6f 70 2d 3e base = infop->
a2530 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 base;.
a2540 64 6f 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 do{
a2550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a2570 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 73 63 69 Convert to asci
a2580 69 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 i */.
a2590 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 63 73 *(--bufpt) = cs
a25a0 65 74 5b 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73 et[longvalue%bas
a25b0 65 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e];.
a25c0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67 longvalue = long
a25d0 76 61 6c 75 65 2f 62 61 73 65 3b 0a 20 20 20 20 value/base;.
a25e0 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f }while( lo
a25f0 6e 67 76 61 6c 75 65 3e 30 20 29 3b 0a 20 20 20 ngvalue>0 );.
a2600 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c }. l
a2610 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 26 62 ength = (int)(&b
a2620 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d uf[etBUFSIZE-1]-
a2630 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 bufpt);.
a2640 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f for(idx=precisio
a2650 6e 2d 6c 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b n-length; idx>0;
a2660 20 69 64 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20 idx--){.
a2670 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 *(--bufpt) =
a2680 27 30 27 3b 20 20 20 20 20 20 20 20 20 20 20 20 '0';
a2690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a26a0 20 2f 2a 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a /* Zero pad */.
a26b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a26c0 20 20 69 66 28 20 70 72 65 66 69 78 20 29 20 2a if( prefix ) *
a26d0 28 2d 2d 62 75 66 70 74 29 20 3d 20 70 72 65 66 (--bufpt) = pref
a26e0 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ix;
a26f0 20 20 2f 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f /* Add sign */
a2700 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 . if( fla
a2710 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 g_alternateform
a2720 26 26 20 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 && infop->prefix
a2730 20 29 7b 20 20 20 20 20 20 2f 2a 20 41 64 64 20 ){ /* Add
a2740 22 30 22 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20 "0" or "0x" */.
a2750 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 const c
a2760 68 61 72 20 2a 70 72 65 3b 0a 20 20 20 20 20 20 har *pre;.
a2770 20 20 20 20 63 68 61 72 20 78 3b 0a 20 20 20 20 char x;.
a2780 20 20 20 20 20 20 70 72 65 20 3d 20 26 61 50 72 pre = &aPr
a2790 65 66 69 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66 efix[infop->pref
a27a0 69 78 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66 ix];. f
a27b0 6f 72 28 3b 20 28 78 3d 28 2a 70 72 65 29 29 21 or(; (x=(*pre))!
a27c0 3d 30 3b 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62 =0; pre++) *(--b
a27d0 75 66 70 74 29 20 3d 20 78 3b 0a 20 20 20 20 20 ufpt) = x;.
a27e0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e }. len
a27f0 67 74 68 20 3d 20 28 69 6e 74 29 28 26 62 75 66 gth = (int)(&buf
a2800 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 [etBUFSIZE-1]-bu
a2810 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 fpt);. br
a2820 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 eak;. case
a2830 65 74 46 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63 etFLOAT:. c
a2840 61 73 65 20 65 74 45 58 50 3a 0a 20 20 20 20 20 ase etEXP:.
a2850 20 63 61 73 65 20 65 74 47 45 4e 45 52 49 43 3a case etGENERIC:
a2860 0a 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c . realval
a2870 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 64 ue = va_arg(ap,d
a2880 6f 75 62 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20 ouble);.#ifndef
a2890 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
a28a0 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 TING_POINT.
a28b0 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e if( precision
a28c0 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d <0 ) precision =
a28d0 20 36 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6; /* S
a28e0 65 74 20 64 65 66 61 75 6c 74 20 70 72 65 63 69 et default preci
a28f0 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 sion */.
a2900 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 if( precision>et
a2910 42 55 46 53 49 5a 45 2f 32 2d 31 30 20 29 20 70 BUFSIZE/2-10 ) p
a2920 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 recision = etBUF
a2930 53 49 5a 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20 SIZE/2-10;.
a2940 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 if( realvalue
a2950 3c 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 <0.0 ){.
a2960 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72 realvalue = -r
a2970 65 61 6c 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 ealvalue;.
a2980 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 prefix = '-'
a2990 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
a29a0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 . if( f
a29b0 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 lag_plussign )
a29c0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d prefix =
a29d0 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '+';.
a29e0 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c else if( flag_bl
a29f0 61 6e 6b 73 69 67 6e 20 29 20 20 20 20 70 72 65 anksign ) pre
a2a00 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 fix = ' ';.
a2a10 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 else
a2a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2a30 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 prefix = 0;.
a2a40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a2a50 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e if( xtype==etGEN
a2a60 45 52 49 43 20 26 26 20 70 72 65 63 69 73 69 6f ERIC && precisio
a2a70 6e 3e 30 20 29 20 70 72 65 63 69 73 69 6f 6e 2d n>0 ) precision-
a2a80 2d 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20 -;.#if 0.
a2a90 20 2f 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 /* Rounding wor
a2aa0 6b 73 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e ks like BSD when
a2ab0 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e the constant 0.
a2ac0 34 39 39 39 20 69 73 20 75 73 65 64 2e 20 20 57 4999 is used. W
a2ad0 69 65 72 64 21 20 2a 2f 0a 20 20 20 20 20 20 20 ierd! */.
a2ae0 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 for(idx=precisi
a2af0 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 on, rounder=0.49
a2b00 39 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 99; idx>0; idx--
a2b10 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b , rounder*=0.1);
a2b20 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f .#else. /
a2b30 2a 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20 * It makes more
a2b40 73 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e 35 sense to use 0.5
a2b50 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 */. for(
a2b60 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 idx=precision, r
a2b70 6f 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e ounder=0.5; idx>
a2b80 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 0; idx--, rounde
a2b90 72 2a 3d 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 r*=0.1){}.#endif
a2ba0 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 . if( xty
a2bb0 70 65 3d 3d 65 74 46 4c 4f 41 54 20 29 20 72 65 pe==etFLOAT ) re
a2bc0 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 alvalue += round
a2bd0 65 72 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e er;. /* N
a2be0 6f 72 6d 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c ormalize realval
a2bf0 75 65 20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e ue to within 10.
a2c00 30 20 3e 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d 0 > realvalue >=
a2c10 20 31 2e 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 1.0 */.
a2c20 65 78 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 exp = 0;.
a2c30 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 if( sqlite3IsNa
a2c40 4e 28 28 64 6f 75 62 6c 65 29 72 65 61 6c 76 61 N((double)realva
a2c50 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 lue) ){.
a2c60 20 20 62 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b bufpt = "NaN";
a2c70 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 . lengt
a2c80 68 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 h = 3;.
a2c90 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
a2ca0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65 }. if( re
a2cb0 61 6c 76 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20 alvalue>0.0 ){.
a2cc0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
a2cd0 72 65 61 6c 76 61 6c 75 65 3e 3d 31 65 33 32 20 realvalue>=1e32
a2ce0 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 && exp<=350 ){ r
a2cf0 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 2d 33 ealvalue *= 1e-3
a2d00 32 3b 20 65 78 70 2b 3d 33 32 3b 20 7d 0a 20 20 2; exp+=32; }.
a2d10 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 while( r
a2d20 65 61 6c 76 61 6c 75 65 3e 3d 31 65 38 20 26 26 ealvalue>=1e8 &&
a2d30 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 exp<=350 ){ rea
a2d40 6c 76 61 6c 75 65 20 2a 3d 20 31 65 2d 38 3b 20 lvalue *= 1e-8;
a2d50 65 78 70 2b 3d 38 3b 20 7d 0a 20 20 20 20 20 20 exp+=8; }.
a2d60 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 while( realv
a2d70 61 6c 75 65 3e 3d 31 30 2e 30 20 26 26 20 65 78 alue>=10.0 && ex
a2d80 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 p<=350 ){ realva
a2d90 6c 75 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b lue *= 0.1; exp+
a2da0 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 +; }. w
a2db0 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c hile( realvalue<
a2dc0 31 65 2d 38 20 29 7b 20 72 65 61 6c 76 61 6c 75 1e-8 ){ realvalu
a2dd0 65 20 2a 3d 20 31 65 38 3b 20 65 78 70 2d 3d 38 e *= 1e8; exp-=8
a2de0 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 ; }. wh
a2df0 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 ile( realvalue<1
a2e00 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 .0 ){ realvalue
a2e10 2a 3d 20 31 30 2e 30 3b 20 65 78 70 2d 2d 3b 20 *= 10.0; exp--;
a2e20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
a2e30 65 78 70 3e 33 35 30 20 29 7b 0a 20 20 20 20 20 exp>350 ){.
a2e40 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 if( prefi
a2e50 78 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 x=='-' ){.
a2e60 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
a2e70 22 2d 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 "-Inf";.
a2e80 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 72 }else if( pr
a2e90 65 66 69 78 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 efix=='+' ){.
a2ea0 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 bufpt
a2eb0 20 3d 20 22 2b 49 6e 66 22 3b 0a 20 20 20 20 20 = "+Inf";.
a2ec0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a2ed0 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 bufp
a2ee0 74 20 3d 20 22 49 6e 66 22 3b 0a 20 20 20 20 20 t = "Inf";.
a2ef0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a2f00 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 length = sq
a2f10 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 lite3Strlen30(bu
a2f20 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 fpt);.
a2f30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
a2f40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
a2f50 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
a2f60 75 66 3b 0a 20 20 20 20 20 20 20 20 2f 2a 0a 20 uf;. /*.
a2f70 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 ** If the
a2f80 20 66 69 65 6c 64 20 74 79 70 65 20 69 73 20 65 field type is e
a2f90 74 47 45 4e 45 52 49 43 2c 20 74 68 65 6e 20 63 tGENERIC, then c
a2fa0 6f 6e 76 65 72 74 20 74 6f 20 65 69 74 68 65 72 onvert to either
a2fb0 20 65 74 45 58 50 0a 20 20 20 20 20 20 20 20 2a etEXP. *
a2fc0 2a 20 6f 72 20 65 74 46 4c 4f 41 54 2c 20 61 73 * or etFLOAT, as
a2fd0 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 20 20 appropriate..
a2fe0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
a2ff0 20 66 6c 61 67 5f 65 78 70 20 3d 20 78 74 79 70 flag_exp = xtyp
a3000 65 3d 3d 65 74 45 58 50 3b 0a 20 20 20 20 20 20 e==etEXP;.
a3010 20 20 69 66 28 20 78 74 79 70 65 21 3d 65 74 46 if( xtype!=etF
a3020 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 LOAT ){.
a3030 20 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20 72 realvalue += r
a3040 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20 20 ounder;.
a3050 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e if( realvalue>
a3060 3d 31 30 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c =10.0 ){ realval
a3070 75 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b ue *= 0.1; exp++
a3080 3b 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ; }. }.
a3090 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d if( xtype=
a30a0 3d 65 74 47 45 4e 45 52 49 43 20 29 7b 0a 20 20 =etGENERIC ){.
a30b0 20 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a flag_rtz
a30c0 20 3d 20 21 66 6c 61 67 5f 61 6c 74 65 72 6e 61 = !flag_alterna
a30d0 74 65 66 6f 72 6d 3b 0a 20 20 20 20 20 20 20 20 teform;.
a30e0 20 20 69 66 28 20 65 78 70 3c 2d 34 20 7c 7c 20 if( exp<-4 ||
a30f0 65 78 70 3e 70 72 65 63 69 73 69 6f 6e 20 29 7b exp>precision ){
a3100 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79 . xty
a3110 70 65 20 3d 20 65 74 45 58 50 3b 0a 20 20 20 20 pe = etEXP;.
a3120 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a3130 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 precisi
a3140 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 20 2d on = precision -
a3150 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 exp;.
a3160 20 20 78 74 79 70 65 20 3d 20 65 74 46 4c 4f 41 xtype = etFLOA
a3170 54 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 T;. }.
a3180 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a3190 20 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a flag_rtz
a31a0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 0;. }.
a31b0 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 if( xtyp
a31c0 65 3d 3d 65 74 45 58 50 20 29 7b 0a 20 20 20 20 e==etEXP ){.
a31d0 20 20 20 20 20 20 65 32 20 3d 20 30 3b 0a 20 20 e2 = 0;.
a31e0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a31f0 20 20 20 20 20 20 20 65 32 20 3d 20 65 78 70 3b e2 = exp;
a3200 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a3210 20 20 20 6e 73 64 20 3d 20 30 3b 0a 20 20 20 20 nsd = 0;.
a3220 20 20 20 20 66 6c 61 67 5f 64 70 20 3d 20 28 70 flag_dp = (p
a3230 72 65 63 69 73 69 6f 6e 3e 30 20 3f 31 3a 30 29 recision>0 ?1:0)
a3240 20 7c 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 | flag_alternat
a3250 65 66 6f 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74 eform | flag_alt
a3260 66 6f 72 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f form2;. /
a3270 2a 20 54 68 65 20 73 69 67 6e 20 69 6e 20 66 72 * The sign in fr
a3280 6f 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ont of the numbe
a3290 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 r */. if(
a32a0 20 70 72 65 66 69 78 20 29 7b 0a 20 20 20 20 20 prefix ){.
a32b0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a32c0 3d 20 70 72 65 66 69 78 3b 0a 20 20 20 20 20 20 = prefix;.
a32d0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 }. /* D
a32e0 69 67 69 74 73 20 70 72 69 6f 72 20 74 6f 20 74 igits prior to t
a32f0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 he decimal point
a3300 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
a3310 65 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e2<0 ){.
a3320 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 *(bufpt++) = '
a3330 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 0';. }els
a3340 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 e{. for
a3350 28 3b 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b (; e2>=0; e2--){
a3360 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 . *(b
a3370 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 ufpt++) = et_get
a3380 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 digit(&realvalue
a3390 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 ,&nsd);.
a33a0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
a33b0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 63 /* The dec
a33c0 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 imal point */.
a33d0 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 64 if( flag_d
a33e0 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a p ){. *
a33f0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b (bufpt++) = '.';
a3400 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a3410 20 20 20 2f 2a 20 22 30 22 20 64 69 67 69 74 73 /* "0" digits
a3420 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 6d after the decim
a3430 61 6c 20 70 6f 69 6e 74 20 62 75 74 20 62 65 66 al point but bef
a3440 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 20 20 ore the first.
a3450 20 20 20 20 20 20 2a 2a 20 73 69 67 6e 69 66 69 ** signifi
a3460 63 61 6e 74 20 64 69 67 69 74 20 6f 66 20 74 68 cant digit of th
a3470 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 e number */.
a3480 20 20 20 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32 for(e2++; e2
a3490 3c 30 3b 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c <0; precision--,
a34a0 20 65 32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 e2++){.
a34b0 20 20 61 73 73 65 72 74 28 20 70 72 65 63 69 73 assert( precis
a34c0 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 ion>0 );.
a34d0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 *(bufpt++) =
a34e0 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 '0';. }.
a34f0 20 20 20 20 20 20 20 2f 2a 20 53 69 67 6e 69 66 /* Signif
a3500 69 63 61 6e 74 20 64 69 67 69 74 73 20 61 66 74 icant digits aft
a3510 65 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 er the decimal p
a3520 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 oint */.
a3530 77 68 69 6c 65 28 20 28 70 72 65 63 69 73 69 6f while( (precisio
a3540 6e 2d 2d 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 n--)>0 ){.
a3550 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d *(bufpt++) =
a3560 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 et_getdigit(&re
a3570 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 alvalue,&nsd);.
a3580 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a3590 20 2f 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c /* Remove trail
a35a0 69 6e 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68 ing zeros and th
a35b0 65 20 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69 e "." if no digi
a35c0 74 73 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e ts follow the ".
a35d0 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 " */. if(
a35e0 20 66 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c 61 flag_rtz && fla
a35f0 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20 g_dp ){.
a3600 20 20 77 68 69 6c 65 28 20 62 75 66 70 74 5b 2d while( bufpt[-
a3610 31 5d 3d 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75 1]=='0' ) *(--bu
a3620 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 fpt) = 0;.
a3630 20 20 20 20 61 73 73 65 72 74 28 20 62 75 66 70 assert( bufp
a3640 74 3e 62 75 66 20 29 3b 0a 20 20 20 20 20 20 20 t>buf );.
a3650 20 20 20 69 66 28 20 62 75 66 70 74 5b 2d 31 5d if( bufpt[-1]
a3660 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 =='.' ){.
a3670 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c if( flag_al
a3680 74 66 6f 72 6d 32 20 29 7b 0a 20 20 20 20 20 20 tform2 ){.
a3690 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a36a0 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 +) = '0';.
a36b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a36c0 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 *(--b
a36d0 75 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 ufpt) = 0;.
a36e0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a36f0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
a3700 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 /* Add th
a3710 65 20 22 65 4e 4e 4e 22 20 73 75 66 66 69 78 20 e "eNNN" suffix
a3720 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 */. if( f
a3730 6c 61 67 5f 65 78 70 20 7c 7c 20 78 74 79 70 65 lag_exp || xtype
a3740 3d 3d 65 74 45 58 50 20 29 7b 0a 20 20 20 20 20 ==etEXP ){.
a3750 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 *(bufpt++)
a3760 3d 20 61 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d = aDigits[infop-
a3770 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 >charset];.
a3780 20 20 20 20 20 69 66 28 20 65 78 70 3c 30 20 29 if( exp<0 )
a3790 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 {. *(
a37a0 62 75 66 70 74 2b 2b 29 20 3d 20 27 2d 27 3b 20 bufpt++) = '-';
a37b0 65 78 70 20 3d 20 2d 65 78 70 3b 0a 20 20 20 20 exp = -exp;.
a37c0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a37d0 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a37e0 2b 2b 29 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 ++) = '+';.
a37f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
a3800 20 69 66 28 20 65 78 70 3e 3d 31 30 30 20 29 7b if( exp>=100 ){
a3810 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 . *(b
a3820 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 ufpt++) = (char)
a3830 28 28 65 78 70 2f 31 30 30 29 2b 27 30 27 29 3b ((exp/100)+'0');
a3840 20 20 20 20 20 20 20 20 2f 2a 20 31 30 30 27 73 /* 100's
a3850 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 digit */.
a3860 20 20 20 20 20 20 65 78 70 20 25 3d 20 31 30 30 exp %= 100
a3870 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a3880 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a3890 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78 70 2f +) = (char)(exp/
a38a0 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20 10+'0');
a38b0 20 20 20 20 20 2f 2a 20 31 30 27 73 20 64 69 67 /* 10's dig
a38c0 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 it */.
a38d0 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 *(bufpt++) = (ch
a38e0 61 72 29 28 65 78 70 25 31 30 2b 27 30 27 29 3b ar)(exp%10+'0');
a38f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a3900 31 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 1's digit */.
a3910 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a }. *
a3920 62 75 66 70 74 20 3d 20 30 3b 0a 0a 20 20 20 20 bufpt = 0;..
a3930 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 76 65 /* The conve
a3940 72 74 65 64 20 6e 75 6d 62 65 72 20 69 73 20 69 rted number is i
a3950 6e 20 62 75 66 5b 5d 20 61 6e 64 20 7a 65 72 6f n buf[] and zero
a3960 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 4f 75 74 terminated. Out
a3970 70 75 74 20 69 74 2e 0a 20 20 20 20 20 20 20 20 put it..
a3980 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
a3990 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 74 68 number is in th
a39a0 65 20 75 73 75 61 6c 20 6f 72 64 65 72 2c 20 6e e usual order, n
a39b0 6f 74 20 72 65 76 65 72 73 65 64 20 61 73 20 77 ot reversed as w
a39c0 69 74 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 ith. ** i
a39d0 6e 74 65 67 65 72 20 63 6f 6e 76 65 72 73 69 6f nteger conversio
a39e0 6e 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c ns. */. l
a39f0 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 62 75 ength = (int)(bu
a3a00 66 70 74 2d 62 75 66 29 3b 0a 20 20 20 20 20 20 fpt-buf);.
a3a10 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a bufpt = buf;..
a3a20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 /* Speci
a3a30 61 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65 al case: Add le
a3a40 61 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74 ading zeros if t
a3a50 68 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 he flag_zeropad
a3a60 66 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20 flag is.
a3a70 2a 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72 ** set and we ar
a3a80 65 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69 e not left justi
a3a90 66 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 fied */.
a3aa0 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 if( flag_zeropad
a3ab0 20 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 && !flag_leftju
a3ac0 73 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20 stify && length
a3ad0 3c 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20 < width){.
a3ae0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
a3af0 20 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 int nPad =
a3b00 77 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a width - length;.
a3b10 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d for(i=
a3b20 77 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 width; i>=nPad;
a3b30 69 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 i--){.
a3b40 20 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66 bufpt[i] = buf
a3b50 70 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20 pt[i-nPad];.
a3b60 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a3b70 20 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b i = prefix!=0;
a3b80 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
a3b90 28 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74 ( nPad-- ) bufpt
a3ba0 5b 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 [i++] = '0';.
a3bb0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a3bc0 77 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d width;. }
a3bd0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
a3be0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 break;. cas
a3bf0 65 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20 e etSIZE:.
a3c00 20 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e *(va_arg(ap,in
a3c10 74 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e t*)) = pAccum->n
a3c20 43 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65 Char;. le
a3c30 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 ngth = width = 0
a3c40 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
a3c50 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45 . case etPE
a3c60 52 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 RCENT:. b
a3c70 75 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 uf[0] = '%';.
a3c80 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 bufpt = buf
a3c90 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 ;. length
a3ca0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 = 1;. br
a3cb0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 eak;. case
a3cc0 65 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20 etCHARX:.
a3cd0 20 63 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 c = va_arg(ap,i
a3ce0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 nt);. buf
a3cf0 5b 30 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 [0] = (char)c;.
a3d00 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69 if( preci
a3d10 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 sion>=0 ){.
a3d20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 for(idx=1;
a3d30 69 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 idx<precision; i
a3d40 64 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d dx++) buf[idx] =
a3d50 20 28 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 (char)c;.
a3d60 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 length = pre
a3d70 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 cision;.
a3d80 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a3d90 20 6c 65 6e 67 74 68 20 3d 31 3b 0a 20 20 20 20 length =1;.
a3da0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 }. bu
a3db0 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 fpt = buf;.
a3dc0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a3dd0 63 61 73 65 20 65 74 53 54 52 49 4e 47 3a 0a 20 case etSTRING:.
a3de0 20 20 20 20 20 63 61 73 65 20 65 74 44 59 4e 53 case etDYNS
a3df0 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 20 20 62 TRING:. b
a3e00 75 66 70 74 20 3d 20 76 61 5f 61 72 67 28 61 70 ufpt = va_arg(ap
a3e10 2c 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 ,char*);.
a3e20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b if( bufpt==0 ){
a3e30 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 . bufpt
a3e40 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 20 20 7d = "";. }
a3e50 65 6c 73 65 20 69 66 28 20 78 74 79 70 65 3d 3d else if( xtype==
a3e60 65 74 44 59 4e 53 54 52 49 4e 47 20 29 7b 0a 20 etDYNSTRING ){.
a3e70 20 20 20 20 20 20 20 20 20 7a 45 78 74 72 61 20 zExtra
a3e80 3d 20 62 75 66 70 74 3b 0a 20 20 20 20 20 20 20 = bufpt;.
a3e90 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 }. if( p
a3ea0 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 recision>=0 ){.
a3eb0 20 20 20 20 20 20 20 20 20 66 6f 72 28 6c 65 6e for(len
a3ec0 67 74 68 3d 30 3b 20 6c 65 6e 67 74 68 3c 70 72 gth=0; length<pr
a3ed0 65 63 69 73 69 6f 6e 20 26 26 20 62 75 66 70 74 ecision && bufpt
a3ee0 5b 6c 65 6e 67 74 68 5d 3b 20 6c 65 6e 67 74 68 [length]; length
a3ef0 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 7d 65 ++){}. }e
a3f00 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6c lse{. l
a3f10 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 ength = sqlite3S
a3f20 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a trlen30(bufpt);.
a3f30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a3f40 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
a3f50 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a ase etSQLESCAPE:
a3f60 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 . case etSQ
a3f70 4c 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20 LESCAPE2:.
a3f80 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 case etSQLESCAPE
a3f90 33 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 3: {. int
a3fa0 20 69 2c 20 6a 2c 20 6e 2c 20 69 73 6e 75 6c 6c i, j, n, isnull
a3fb0 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 65 ;. int ne
a3fc0 65 64 51 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 edQuote;.
a3fd0 20 63 68 61 72 20 63 68 3b 0a 20 20 20 20 20 20 char ch;.
a3fe0 20 20 63 68 61 72 20 71 20 3d 20 28 28 78 74 79 char q = ((xty
a3ff0 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 33 pe==etSQLESCAPE3
a4000 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20 20 20 2f )?'"':'\''); /
a4010 2a 20 51 75 6f 74 65 20 63 68 61 72 61 63 74 65 * Quote characte
a4020 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61 r */. cha
a4030 72 20 2a 65 73 63 61 72 67 20 3d 20 76 61 5f 61 r *escarg = va_a
a4040 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20 rg(ap,char*);.
a4050 20 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d 20 65 isnull = e
a4060 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20 20 20 20 scarg==0;.
a4070 20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29 20 65 if( isnull ) e
a4080 73 63 61 72 67 20 3d 20 28 78 74 79 70 65 3d 3d scarg = (xtype==
a4090 65 74 53 51 4c 45 53 43 41 50 45 32 20 3f 20 22 etSQLESCAPE2 ? "
a40a0 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c 29 22 NULL" : "(NULL)"
a40b0 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 );. for(i
a40c0 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67 =n=0; (ch=escarg
a40d0 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 [i])!=0; i++){.
a40e0 20 20 20 20 20 20 20 20 20 69 66 28 20 63 68 3d if( ch=
a40f0 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 =q ) n++;.
a4100 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 65 }. nee
a4110 64 51 75 6f 74 65 20 3d 20 21 69 73 6e 75 6c 6c dQuote = !isnull
a4120 20 26 26 20 78 74 79 70 65 3d 3d 65 74 53 51 4c && xtype==etSQL
a4130 45 53 43 41 50 45 32 3b 0a 20 20 20 20 20 20 20 ESCAPE2;.
a4140 20 6e 20 2b 3d 20 69 20 2b 20 31 20 2b 20 6e 65 n += i + 1 + ne
a4150 65 64 51 75 6f 74 65 2a 32 3b 0a 20 20 20 20 20 edQuote*2;.
a4160 20 20 20 69 66 28 20 6e 3e 65 74 42 55 46 53 49 if( n>etBUFSI
a4170 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ZE ){.
a4180 62 75 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d bufpt = zExtra =
a4190 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 sqlite3Malloc(
a41a0 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 n );. i
a41b0 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20 f( bufpt==0 ){.
a41c0 20 20 20 20 20 20 20 20 20 20 20 70 41 63 63 75 pAccu
a41d0 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 m->mallocFailed
a41e0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 1;.
a41f0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 return;.
a4200 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c }. }el
a4210 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75 se{. bu
a4220 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 fpt = buf;.
a4230 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6a 20 3d }. j =
a4240 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0;. if(
a4250 6e 65 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 needQuote ) bufp
a4260 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 t[j++] = q;.
a4270 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 68 for(i=0; (ch
a4280 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 =escarg[i])!=0;
a4290 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 i++){.
a42a0 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b bufpt[j++] = ch;
a42b0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 . if( c
a42c0 68 3d 3d 71 20 29 20 62 75 66 70 74 5b 6a 2b 2b h==q ) bufpt[j++
a42d0 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 ] = ch;.
a42e0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 65 }. if( ne
a42f0 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 74 5b edQuote ) bufpt[
a4300 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20 20 j++] = q;.
a4310 20 20 62 75 66 70 74 5b 6a 5d 20 3d 20 30 3b 0a bufpt[j] = 0;.
a4320 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a4330 20 6a 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 j;. /* T
a4340 68 65 20 70 72 65 63 69 73 69 6f 6e 20 69 73 20 he precision is
a4350 69 67 6e 6f 72 65 64 20 6f 6e 20 25 71 20 61 6e ignored on %q an
a4360 64 20 25 51 20 2a 2f 0a 20 20 20 20 20 20 20 20 d %Q */.
a4370 2f 2a 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e /* if( precision
a4380 3e 3d 30 20 26 26 20 70 72 65 63 69 73 69 6f 6e >=0 && precision
a4390 3c 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67 74 68 <length ) length
a43a0 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 20 2a 2f = precision; */
a43b0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
a43c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
a43d0 73 65 20 65 74 54 4f 4b 45 4e 3a 20 7b 0a 20 20 se etTOKEN: {.
a43e0 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 6f Token *pTo
a43f0 6b 65 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c ken = va_arg(ap,
a4400 20 54 6f 6b 65 6e 2a 29 3b 0a 20 20 20 20 20 20 Token*);.
a4410 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a if( pToken ){.
a4420 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
a4430 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
a4440 70 41 63 63 75 6d 2c 20 28 63 6f 6e 73 74 20 63 pAccum, (const c
a4450 68 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 har*)pToken->z,
a4460 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 pToken->n);.
a4470 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 }. le
a4480 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 ngth = width = 0
a4490 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
a44a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 . }. c
a44b0 61 73 65 20 65 74 53 52 43 4c 49 53 54 3a 20 7b ase etSRCLIST: {
a44c0 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74 . SrcList
a44d0 20 2a 70 53 72 63 20 3d 20 76 61 5f 61 72 67 28 *pSrc = va_arg(
a44e0 61 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 20 ap, SrcList*);.
a44f0 20 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 76 int k = v
a4500 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a a_arg(ap, int);.
a4510 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53 struct S
a4520 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 rcList_item *pIt
a4530 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 6b 5d em = &pSrc->a[k]
a4540 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
a4550 28 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 72 63 ( k>=0 && k<pSrc
a4560 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20 ->nSrc );.
a4570 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 if( pItem->zDa
a4580 74 61 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 tabase ){.
a4590 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 sqlite3StrAc
a45a0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d cumAppend(pAccum
a45b0 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 , pItem->zDataba
a45c0 73 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 se, -1);.
a45d0 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a45e0 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
a45f0 20 22 2e 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 ".", 1);.
a4600 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 }. sqli
a4610 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a4620 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d d(pAccum, pItem-
a4630 3e 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 >zName, -1);.
a4640 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 length = wi
a4650 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dth = 0;.
a4660 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
a4670 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b default: {
a4680 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
a4690 20 78 74 79 70 65 3d 3d 65 74 49 4e 56 41 4c 49 xtype==etINVALI
a46a0 44 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 D );. ret
a46b0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 urn;. }.
a46c0 20 7d 2f 2a 20 45 6e 64 20 73 77 69 74 63 68 20 }/* End switch
a46d0 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20 over the format
a46e0 74 79 70 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20 type */. /*.
a46f0 20 20 20 2a 2a 20 54 68 65 20 74 65 78 74 20 6f ** The text o
a4700 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e f the conversion
a4710 20 69 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 is pointed to b
a4720 79 20 22 62 75 66 70 74 22 20 61 6e 64 20 69 73 y "bufpt" and is
a4730 0a 20 20 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22 . ** "length"
a4740 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 characters long
a4750 2e 20 20 54 68 65 20 66 69 65 6c 64 20 77 69 64 . The field wid
a4760 74 68 20 69 73 20 22 77 69 64 74 68 22 2e 20 20 th is "width".
a4770 44 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 75 Do. ** the ou
a4780 74 70 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tput.. */.
a4790 20 69 66 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a if( !flag_leftj
a47a0 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20 ustify ){.
a47b0 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70 register int nsp
a47c0 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63 ace;. nspac
a47d0 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68 e = width-length
a47e0 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61 ;. if( nspa
a47f0 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ce>0 ){.
a4800 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63 appendSpace(pAcc
a4810 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20 um, nspace);.
a4820 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
a4830 66 28 20 6c 65 6e 67 74 68 3e 30 20 29 7b 0a 20 f( length>0 ){.
a4840 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 sqlite3StrA
a4850 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 ccumAppend(pAccu
a4860 6d 2c 20 62 75 66 70 74 2c 20 6c 65 6e 67 74 68 m, bufpt, length
a4870 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
a4880 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 flag_leftjustif
a4890 79 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 y ){. regis
a48a0 74 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a ter int nspace;.
a48b0 20 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 nspace = w
a48c0 69 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 idth-length;.
a48d0 20 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 if( nspace>0
a48e0 29 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e ){. appen
a48f0 64 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e dSpace(pAccum, n
a4900 73 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a space);. }.
a4910 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 45 }. if( zE
a4920 78 74 72 61 20 29 7b 0a 20 20 20 20 20 20 73 71 xtra ){. sq
a4930 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 78 74 72 lite3_free(zExtr
a4940 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 2f 2a 20 a);. }. }/*
a4950 45 6e 64 20 66 6f 72 20 6c 6f 6f 70 20 6f 76 65 End for loop ove
a4960 72 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 r the format str
a4970 69 6e 67 20 2a 2f 0a 7d 20 2f 2a 20 45 6e 64 20 ing */.} /* End
a4980 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a of function */..
a4990 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 62 /*.** Append N b
a49a0 79 74 65 73 20 6f 66 20 74 65 78 74 20 66 72 6f ytes of text fro
a49b0 6d 20 7a 20 74 6f 20 74 68 65 20 53 74 72 41 63 m z to the StrAc
a49c0 63 75 6d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 cum object..*/.S
a49d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
a49e0 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 id sqlite3StrAcc
a49f0 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63 75 umAppend(StrAccu
a4a00 6d 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 m *p, const char
a4a10 20 2a 7a 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 61 *z, int N){. a
a4a20 73 73 65 72 74 28 20 7a 21 3d 30 20 7c 7c 20 4e ssert( z!=0 || N
a4a30 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e ==0 );. if( p->
a4a40 74 6f 6f 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c tooBig | p->mall
a4a50 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
a4a60 74 65 73 74 63 61 73 65 28 70 2d 3e 74 6f 6f 42 testcase(p->tooB
a4a70 69 67 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 ig);. testcas
a4a80 65 28 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 e(p->mallocFaile
a4a90 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a d);. return;.
a4aa0 20 20 7d 0a 20 20 69 66 28 20 4e 3c 30 20 29 7b }. if( N<0 ){
a4ab0 0a 20 20 20 20 4e 20 3d 20 73 71 6c 69 74 65 33 . N = sqlite3
a4ac0 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 7d Strlen30(z);. }
a4ad0 0a 20 20 69 66 28 20 4e 3d 3d 30 20 7c 7c 20 4e . if( N==0 || N
a4ae0 45 56 45 52 28 7a 3d 3d 30 29 20 29 7b 0a 20 20 EVER(z==0) ){.
a4af0 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
a4b00 69 66 28 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e if( p->nChar+N >
a4b10 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 = p->nAlloc ){.
a4b20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 char *zNew;.
a4b30 20 20 20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61 if( !p->useMa
a4b40 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 2d lloc ){. p-
a4b50 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 >tooBig = 1;.
a4b60 20 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 N = p->nAlloc
a4b70 20 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b - p->nChar - 1;
a4b80 0a 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20 . if( N<=0
a4b90 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
a4ba0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d n;. }. }
a4bb0 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 36 34 20 else{. i64
a4bc0 73 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72 szNew = p->nChar
a4bd0 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d ;. szNew +=
a4be0 20 4e 20 2b 20 31 3b 0a 20 20 20 20 20 20 69 66 N + 1;. if
a4bf0 28 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41 ( szNew > p->mxA
a4c00 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 lloc ){.
a4c10 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 sqlite3StrAccumR
a4c20 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 eset(p);.
a4c30 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a p->tooBig = 1;.
a4c40 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
a4c50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a4c60 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d p->nAlloc =
a4c70 20 28 69 6e 74 29 73 7a 4e 65 77 3b 0a 20 20 20 (int)szNew;.
a4c80 20 20 20 7d 0a 20 20 20 20 20 20 7a 4e 65 77 20 }. zNew
a4c90 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
a4ca0 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e cRaw(p->db, p->n
a4cb0 41 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 20 20 69 Alloc );. i
a4cc0 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20 f( zNew ){.
a4cd0 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 memcpy(zNew,
a4ce0 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68 p->zText, p->nCh
a4cf0 61 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ar);. sql
a4d00 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 ite3StrAccumRese
a4d10 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d t(p);. p-
a4d20 3e 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0a 20 >zText = zNew;.
a4d30 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a4d40 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->mallocFai
a4d50 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 led = 1;.
a4d60 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a4d70 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 Reset(p);.
a4d80 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
a4d90 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 }. }. }. me
a4da0 6d 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70 mcpy(&p->zText[p
a4db0 2d 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b ->nChar], z, N);
a4dc0 0a 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e . p->nChar += N
a4dd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73 ;.}../*.** Finis
a4de0 68 20 6f 66 66 20 61 20 73 74 72 69 6e 67 20 62 h off a string b
a4df0 79 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 69 74 y making sure it
a4e00 20 69 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 is zero-termina
a4e10 74 65 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ted..** Return a
a4e20 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
a4e30 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 resulting string
a4e40 2e 20 20 52 65 74 75 72 6e 20 61 20 4e 55 4c 4c . Return a NULL
a4e50 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 61 .** pointer if a
a4e60 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 ny kind of error
a4e70 20 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 was encountered
a4e80 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a4e90 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
a4ea0 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 3StrAccumFinish(
a4eb0 53 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 StrAccum *p){.
a4ec0 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a if( p->zText ){.
a4ed0 20 20 20 20 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e p->zText[p->
a4ee0 6e 43 68 61 72 5d 20 3d 20 30 3b 0a 20 20 20 20 nChar] = 0;.
a4ef0 69 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 if( p->useMalloc
a4f00 20 26 26 20 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d && p->zText==p-
a4f10 3e 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20 >zBase ){.
a4f20 70 2d 3e 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 p->zText = sqlit
a4f30 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d e3DbMallocRaw(p-
a4f40 3e 64 62 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 20 >db, p->nChar+1
a4f50 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e );. if( p->
a4f60 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 zText ){.
a4f70 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74 memcpy(p->zText
a4f80 2c 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e , p->zBase, p->n
a4f90 43 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d Char+1);. }
a4fa0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d else{. p-
a4fb0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 >mallocFailed =
a4fc0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
a4fd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d . }. return p-
a4fe0 3e 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a >zText;.}../*.**
a4ff0 20 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63 Reset an StrAcc
a5000 75 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c um string. Recl
a5010 61 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64 aim all malloced
a5020 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 memory..*/.SQLI
a5030 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a5040 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 sqlite3StrAccumR
a5050 65 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70 eset(StrAccum *p
a5060 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 ){. if( p->zTex
a5070 74 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20 t!=p->zBase ){.
a5080 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
a5090 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78 74 (p->db, p->zText
a50a0 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 7a 54 65 78 );. }. p->zTex
a50b0 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 t = 0;.}../*.**
a50c0 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 73 74 72 Initialize a str
a50d0 69 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a ing accumulator.
a50e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
a50f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
a5100 72 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 rAccumInit(StrAc
a5110 63 75 6d 20 2a 70 2c 20 63 68 61 72 20 2a 7a 42 cum *p, char *zB
a5120 61 73 65 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 ase, int n, int
a5130 6d 78 29 7b 0a 20 20 70 2d 3e 7a 54 65 78 74 20 mx){. p->zText
a5140 3d 20 70 2d 3e 7a 42 61 73 65 20 3d 20 7a 42 61 = p->zBase = zBa
a5150 73 65 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b se;. p->db = 0;
a5160 0a 20 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b . p->nChar = 0;
a5170 0a 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e . p->nAlloc = n
a5180 3b 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d ;. p->mxAlloc =
a5190 20 6d 78 3b 0a 20 20 70 2d 3e 75 73 65 4d 61 6c mx;. p->useMal
a51a0 6c 6f 63 20 3d 20 31 3b 0a 20 20 70 2d 3e 74 6f loc = 1;. p->to
a51b0 6f 42 69 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 6d oBig = 0;. p->m
a51c0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b allocFailed = 0;
a51d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 .}../*.** Print
a51e0 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 into memory obta
a51f0 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
a5200 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 Malloc(). Use t
a5210 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 he internal.** %
a5220 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 -conversion exte
a5230 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 nsions..*/.SQLIT
a5240 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
a5250 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
a5260 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e sqlite3 *db, con
a5270 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
a5280 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 , va_list ap){.
a5290 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 char *z;. char
a52a0 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 zBase[SQLITE_PR
a52b0 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 INT_BUF_SIZE];.
a52c0 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 StrAccum acc;.
a52d0 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 assert( db!=0 )
a52e0 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 ;. sqlite3StrAc
a52f0 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 cumInit(&acc, zB
a5300 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 ase, sizeof(zBas
a5310 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 e),.
a5320 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c db->aL
a5330 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
a5340 54 5f 4c 45 4e 47 54 48 5d 29 3b 0a 20 20 61 63 T_LENGTH]);. ac
a5350 63 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 6c c.db = db;. sql
a5360 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 ite3VXPrintf(&ac
a5370 63 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 c, 1, zFormat, a
a5380 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 p);. z = sqlite
a5390 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 3StrAccumFinish(
a53a0 26 61 63 63 29 3b 0a 20 20 69 66 28 20 61 63 63 &acc);. if( acc
a53b0 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b .mallocFailed ){
a53c0 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 . db->mallocF
a53d0 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 ailed = 1;. }.
a53e0 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a return z;.}../*
a53f0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d .** Print into m
a5400 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
a5410 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 rom sqliteMalloc
a5420 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 (). Use the int
a5430 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 ernal.** %-conve
a5440 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 rsion extensions
a5450 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a5460 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
a5470 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 3MPrintf(sqlite3
a5480 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
a5490 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
a54a0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
a54b0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 char *z;. va_s
a54c0 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 tart(ap, zFormat
a54d0 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 );. z = sqlite3
a54e0 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f VMPrintf(db, zFo
a54f0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
a5500 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 end(ap);. retur
a5510 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 n z;.}../*.** Li
a5520 6b 65 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 ke sqlite3MPrint
a5530 66 28 29 2c 20 62 75 74 20 63 61 6c 6c 20 73 71 f(), but call sq
a5540 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e lite3DbFree() on
a5550 20 7a 53 74 72 20 61 66 74 65 72 20 66 6f 72 6d zStr after form
a5560 61 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 73 74 atting.** the st
a5570 72 69 6e 67 20 61 6e 64 20 62 65 66 6f 72 65 20 ring and before
a5580 72 65 74 75 72 6e 6e 69 6e 67 2e 20 20 54 68 69 returnning. Thi
a5590 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74 s routine is int
a55a0 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 ended to be used
a55b0 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 61 6e .** to modify an
a55c0 20 65 78 69 73 74 69 6e 67 20 73 74 72 69 6e 67 existing string
a55d0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a . For example:.
a55e0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20 3d 20 **.** x =
a55f0 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
a5600 62 2c 20 78 2c 20 22 70 72 65 66 69 78 20 25 73 b, x, "prefix %s
a5610 20 73 75 66 66 69 78 22 2c 20 78 29 3b 0a 2a 2a suffix", x);.**
a5620 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a5630 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
a5640 4d 41 70 70 65 6e 64 66 28 73 71 6c 69 74 65 33 MAppendf(sqlite3
a5650 20 2a 64 62 2c 20 63 68 61 72 20 2a 7a 53 74 72 *db, char *zStr
a5660 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
a5670 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 ormat, ...){. v
a5680 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 a_list ap;. cha
a5690 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 r *z;. va_start
a56a0 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 (ap, zFormat);.
a56b0 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 z = sqlite3VMPr
a56c0 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 intf(db, zFormat
a56d0 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 , ap);. va_end(
a56e0 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 ap);. sqlite3Db
a56f0 46 72 65 65 28 64 62 2c 20 7a 53 74 72 29 3b 0a Free(db, zStr);.
a5700 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f return z;.}../
a5710 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 *.** Print into
a5720 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
a5730 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c from sqlite3_mal
a5740 6c 6f 63 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 loc(). Omit the
a5750 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 internal.** %-c
a5760 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 onversion extens
a5770 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
a5780 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 API char *sqlite
a5790 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 3_vmprintf(const
a57a0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
a57b0 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 63 va_list ap){. c
a57c0 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 7a har *z;. char z
a57d0 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e Base[SQLITE_PRIN
a57e0 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 T_BUF_SIZE];. S
a57f0 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 23 69 66 trAccum acc;.#if
a5800 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
a5810 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 _AUTOINIT. if(
a5820 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
a5830 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b ze() ) return 0;
a5840 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 .#endif. sqlite
a5850 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 3StrAccumInit(&a
a5860 63 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f cc, zBase, sizeo
a5870 66 28 7a 42 61 73 65 29 2c 20 53 51 4c 49 54 45 f(zBase), SQLITE
a5880 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a 20 20 _MAX_LENGTH);.
a5890 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 sqlite3VXPrintf(
a58a0 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 &acc, 0, zFormat
a58b0 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c , ap);. z = sql
a58c0 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 ite3StrAccumFini
a58d0 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74 75 sh(&acc);. retu
a58e0 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 rn z;.}../*.** P
a58f0 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 rint into memory
a5900 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
a5910 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 28 qlite3_malloc()(
a5920 29 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 ). Omit the int
a5930 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 ernal.** %-conve
a5940 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 rsion extensions
a5950 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
a5960 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70 char *sqlite3_mp
a5970 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 rintf(const char
a5980 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
a5990 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
a59a0 20 63 68 61 72 20 2a 7a 3b 0a 23 69 66 6e 64 65 char *z;.#ifnde
a59b0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
a59c0 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c TOINIT. if( sql
a59d0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
a59e0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 ) ) return 0;.#e
a59f0 6e 64 69 66 0a 20 20 76 61 5f 73 74 61 72 74 28 ndif. va_start(
a5a00 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
a5a10 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 z = sqlite3_vmpr
a5a20 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70 intf(zFormat, ap
a5a30 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b );. va_end(ap);
a5a40 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a . return z;.}..
a5a50 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6e /*.** sqlite3_sn
a5a60 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 20 6c printf() works l
a5a70 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 20 65 ike snprintf() e
a5a80 78 63 65 70 74 20 74 68 61 74 20 69 74 20 69 67 xcept that it ig
a5a90 6e 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63 75 72 nores the.** cur
a5aa0 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65 74 74 rent locale sett
a5ab0 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 69 ings. This is i
a5ac0 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53 51 4c mportant for SQL
a5ad0 69 74 65 20 62 65 63 61 75 73 65 20 77 65 0a 2a ite because we.*
a5ae0 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74 * are not able t
a5af0 6f 20 75 73 65 20 61 20 22 2c 22 20 61 73 20 74 o use a "," as t
a5b00 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 he decimal point
a5b10 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 22 2e 22 in place of "."
a5b20 20 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 as.** specified
a5b30 20 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c 65 73 by some locales
a5b40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
a5b50 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e char *sqlite3_sn
a5b60 70 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20 63 68 printf(int n, ch
a5b70 61 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73 74 20 ar *zBuf, const
a5b80 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
a5b90 2e 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a ..){. char *z;.
a5ba0 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
a5bb0 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 0a 20 StrAccum acc;..
a5bc0 20 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20 if( n<=0 ){.
a5bd0 20 72 65 74 75 72 6e 20 7a 42 75 66 3b 0a 20 20 return zBuf;.
a5be0 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 }. sqlite3StrAc
a5bf0 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 cumInit(&acc, zB
a5c00 75 66 2c 20 6e 2c 20 30 29 3b 0a 20 20 61 63 63 uf, n, 0);. acc
a5c10 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a .useMalloc = 0;.
a5c20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a 46 va_start(ap,zF
a5c30 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 65 ormat);. sqlite
a5c40 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 3VXPrintf(&acc,
a5c50 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0, zFormat, ap);
a5c60 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 . va_end(ap);.
a5c70 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 z = sqlite3StrA
a5c80 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 ccumFinish(&acc)
a5c90 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a ;. return z;.}.
a5ca0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
a5cb0 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a ITE_DEBUG)./*.**
a5cc0 20 41 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 A version of pr
a5cd0 69 6e 74 66 28 29 20 74 68 61 74 20 75 6e 64 65 intf() that unde
a5ce0 72 73 74 61 6e 64 73 20 25 6c 6c 64 2e 20 20 55 rstands %lld. U
a5cf0 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e sed for debuggin
a5d00 67 2e 0a 2a 2a 20 54 68 65 20 70 72 69 6e 74 66 g..** The printf
a5d10 28 29 20 62 75 69 6c 74 20 69 6e 74 6f 20 73 6f () built into so
a5d20 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 me versions of w
a5d30 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 indows does not
a5d40 75 6e 64 65 72 73 74 61 6e 64 20 25 6c 6c 64 0a understand %lld.
a5d50 2a 2a 20 61 6e 64 20 73 65 67 66 61 75 6c 74 73 ** and segfaults
a5d60 20 69 66 20 79 6f 75 20 67 69 76 65 20 69 74 20 if you give it
a5d70 61 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 2e a long long int.
a5d80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a5d90 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
a5da0 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74 ebugPrintf(const
a5db0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
a5dc0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
a5dd0 61 70 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 ap;. StrAccum a
a5de0 63 63 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b cc;. char zBuf[
a5df0 35 30 30 5d 3b 0a 20 20 73 71 6c 69 74 65 33 53 500];. sqlite3S
a5e00 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 trAccumInit(&acc
a5e10 2c 20 7a 42 75 66 2c 20 73 69 7a 65 6f 66 28 7a , zBuf, sizeof(z
a5e20 42 75 66 29 2c 20 30 29 3b 0a 20 20 61 63 63 2e Buf), 0);. acc.
a5e30 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 useMalloc = 0;.
a5e40 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f va_start(ap,zFo
a5e50 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 rmat);. sqlite3
a5e60 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30 VXPrintf(&acc, 0
a5e70 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a5e80 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
a5e90 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
a5ea0 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 66 inish(&acc);. f
a5eb0 70 72 69 6e 74 66 28 73 74 64 6f 75 74 2c 22 25 printf(stdout,"%
a5ec0 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 66 66 6c s", zBuf);. ffl
a5ed0 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 7d 0a 23 ush(stdout);.}.#
a5ee0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a endif../********
a5ef0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 ****** End of pr
a5f00 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a intf.c *********
a5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5f30 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
a5f40 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
a5f50 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a e random.c *****
a5f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a5f80 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
a5f90 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
a5fa0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
a5fb0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
a5fc0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
a5fd0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
a5fe0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
a5ff0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
a6000 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
a6010 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
a6020 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
a6030 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
a6040 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
a6050 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
a6060 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
a6070 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
a6080 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
a6090 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
a60a0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
a60b0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
a60c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a60d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a60e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a60f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
a6100 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
a6110 69 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c ins code to impl
a6120 65 6d 65 6e 74 20 61 20 70 73 65 75 64 6f 2d 72 ement a pseudo-r
a6130 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a 2a 20 andom number.**
a6140 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 generator (PRNG)
a6150 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a for SQLite..**.
a6160 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 ** Random number
a6170 73 20 61 72 65 20 75 73 65 64 20 62 79 20 73 6f s are used by so
a6180 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
a6190 73 65 20 62 61 63 6b 65 6e 64 73 20 69 6e 20 6f se backends in o
a61a0 72 64 65 72 0a 2a 2a 20 74 6f 20 67 65 6e 65 72 rder.** to gener
a61b0 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 ate random integ
a61c0 65 72 20 6b 65 79 73 20 66 6f 72 20 74 61 62 6c er keys for tabl
a61d0 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20 66 69 6c es or random fil
a61e0 65 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 enames..**.** $I
a61f0 64 3a 20 72 61 6e 64 6f 6d 2e 63 2c 76 20 31 2e d: random.c,v 1.
a6200 32 39 20 32 30 30 38 2f 31 32 2f 31 30 20 31 39 29 2008/12/10 19
a6210 3a 32 36 3a 32 34 20 64 72 68 20 45 78 70 20 24 :26:24 drh Exp $
a6220 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68 72 .*/.../* All thr
a6230 65 61 64 73 20 73 68 61 72 65 20 61 20 73 69 6e eads share a sin
a6240 67 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 gle random numbe
a6250 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20 r generator..**
a6260 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 This structure i
a6270 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 s the current st
a6280 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72 ate of the gener
a6290 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ator..*/.static
a62a0 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 SQLITE_WSD struc
a62b0 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 t sqlite3PrngTyp
a62c0 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 e {. unsigned c
a62d0 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20 20 20 har isInit;
a62e0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a62f0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
a6300 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 unsigned char i
a6310 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 , j;
a6320 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 6c /* State variabl
a6330 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 es */. unsigned
a6340 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20 20 char s[256];
a6350 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 /* State
a6360 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d 20 73 variables */.} s
a6370 71 6c 69 74 65 33 50 72 6e 67 3b 0a 0a 2f 2a 0a qlite3Prng;../*.
a6380 2a 2a 20 47 65 74 20 61 20 73 69 6e 67 6c 65 20 ** Get a single
a6390 38 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 8-bit random val
a63a0 75 65 20 66 72 6f 6d 20 74 68 65 20 52 43 34 20 ue from the RC4
a63b0 50 52 4e 47 2e 20 20 54 68 65 20 4d 75 74 65 78 PRNG. The Mutex
a63c0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 68 65 6c 64 .** must be held
a63d0 20 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e 67 while executing
a63e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
a63f0 2a 0a 2a 2a 20 57 68 79 20 6e 6f 74 20 6a 75 73 *.** Why not jus
a6400 74 20 75 73 65 20 61 20 6c 69 62 72 61 72 79 20 t use a library
a6410 72 61 6e 64 6f 6d 20 67 65 6e 65 72 61 74 6f 72 random generator
a6420 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 20 like lrand48()
a6430 66 6f 72 20 74 68 69 73 3f 0a 2a 2a 20 42 65 63 for this?.** Bec
a6440 61 75 73 65 20 74 68 65 20 4f 50 5f 4e 65 77 52 ause the OP_NewR
a6450 6f 77 69 64 20 6f 70 63 6f 64 65 20 69 6e 20 74 owid opcode in t
a6460 68 65 20 56 44 42 45 20 64 65 70 65 6e 64 73 20 he VDBE depends
a6470 6f 6e 20 68 61 76 69 6e 67 20 61 20 76 65 72 79 on having a very
a6480 0a 2a 2a 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 .** good source
a6490 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 of random number
a64a0 73 2e 20 20 54 68 65 20 6c 72 61 6e 64 34 38 28 s. The lrand48(
a64b0 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 ) library functi
a64c0 6f 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 62 on may.** well b
a64d0 65 20 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20 e good enough.
a64e0 42 75 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20 But maybe not.
a64f0 4f 72 20 6d 61 79 62 65 20 6c 72 61 6e 64 34 38 Or maybe lrand48
a6500 28 29 20 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 73 () has some.** s
a6510 75 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f ubtle problems o
a6520 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 n some systems t
a6530 68 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 hat could cause
a6540 70 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69 73 problems. It is
a6550 20 68 61 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 hard.** to know
a6560 2e 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 . To minimize t
a6570 68 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c he risk of probl
a6580 65 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20 6c ems due to bad l
a6590 72 61 6e 64 34 38 28 29 0a 2a 2a 20 69 6d 70 6c rand48().** impl
a65a0 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51 4c ementations, SQL
a65b0 69 74 65 20 75 73 65 73 20 74 68 69 73 20 72 61 ite uses this ra
a65c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
a65d0 72 61 74 6f 72 20 62 61 73 65 64 0a 2a 2a 20 6f rator based.** o
a65e0 6e 20 52 43 34 2c 20 77 68 69 63 68 20 77 65 20 n RC4, which we
a65f0 6b 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72 79 20 know works very
a6600 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 well..**.** (Lat
a6610 65 72 29 3a 20 20 41 63 74 75 61 6c 6c 79 2c 20 er): Actually,
a6620 4f 50 5f 4e 65 77 52 6f 77 69 64 20 64 6f 65 73 OP_NewRowid does
a6630 20 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20 61 not depend on a
a6640 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 0a good source of.
a6650 2a 2a 20 72 61 6e 64 6f 6d 6e 65 73 73 20 61 6e ** randomness an
a6660 79 20 6d 6f 72 65 2e 20 20 42 75 74 20 77 65 20 y more. But we
a6670 77 69 6c 6c 20 6c 65 61 76 65 20 74 68 69 73 20 will leave this
a6680 63 6f 64 65 20 69 6e 20 61 6c 6c 20 74 68 65 20 code in all the
a6690 73 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 same..*/.static
a66a0 75 38 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 6f u8 randomByte(vo
a66b0 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 id){. unsigned
a66c0 63 68 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54 char t;... /* T
a66d0 68 65 20 22 77 73 64 50 72 6e 67 22 20 6d 61 63 he "wsdPrng" mac
a66e0 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 ro will resolve
a66f0 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 to the pseudo-ra
a6700 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
a6710 72 61 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 rator. ** state
a6720 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 vector. If wri
a6730 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 table static dat
a6740 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 a is unsupported
a6750 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a on the target,.
a6760 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 ** we have to
a6770 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 locate the state
a6780 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 vector at run-t
a6790 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 ime. In the mor
a67a0 65 20 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61 e common. ** ca
a67b0 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c se where writabl
a67c0 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 e static data is
a67d0 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50 supported, wsdP
a67e0 72 6e 67 20 63 61 6e 20 72 65 66 65 72 20 64 69 rng can refer di
a67f0 72 65 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 74 rectly. ** to t
a6800 68 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22 he "sqlite3Prng"
a6810 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 state vector de
a6820 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 20 clared above..
a6830 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
a6840 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 75 _OMIT_WSD. stru
a6850 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 ct sqlite3PrngTy
a6860 70 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c 28 pe *p = &GLOBAL(
a6870 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
a6880 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 ngType, sqlite3P
a6890 72 6e 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 77 rng);.# define w
a68a0 73 64 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c 73 sdPrng p[0].#els
a68b0 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 e.# define wsdPr
a68c0 6e 67 20 73 71 6c 69 74 65 33 50 72 6e 67 0a 23 ng sqlite3Prng.#
a68d0 65 6e 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69 endif... /* Ini
a68e0 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74 tialize the stat
a68f0 65 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 e of the random
a6900 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
a6910 20 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20 once,. ** the
a6920 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 first time this
a6930 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
a6940 64 2e 20 20 54 68 65 20 73 65 65 64 20 76 61 6c d. The seed val
a6950 75 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 ue does. ** not
a6960 20 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e need to contain
a6970 20 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d a lot of random
a6980 6e 65 73 73 20 73 69 6e 63 65 20 77 65 20 61 72 ness since we ar
a6990 65 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e e not. ** tryin
a69a0 67 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65 g to do secure e
a69b0 6e 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79 ncryption or any
a69c0 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e thing like that.
a69d0 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 ... **. ** Not
a69e0 68 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c hing in this fil
a69f0 65 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c e or anywhere el
a6a00 73 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 se in SQLite doe
a6a10 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20 s any kind of.
a6a20 2a 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 ** encryption.
a6a30 54 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 The RC4 algorith
a6a40 6d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 m is being used
a6a50 61 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 64 as a PRNG (pseud
a6a60 6f 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 o-random. ** nu
a6a70 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20 mber generator)
a6a80 6e 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70 not as an encryp
a6a90 74 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a tion device.. *
a6aa0 2f 0a 20 20 69 66 28 20 21 77 73 64 50 72 6e 67 /. if( !wsdPrng
a6ab0 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 69 .isInit ){. i
a6ac0 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 6b nt i;. char k
a6ad0 5b 32 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 72 [256];. wsdPr
a6ae0 6e 67 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 73 ng.j = 0;. ws
a6af0 64 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 20 dPrng.i = 0;.
a6b00 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d sqlite3OsRandom
a6b10 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 ness(sqlite3_vfs
a6b20 5f 66 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 6b _find(0), 256, k
a6b30 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
a6b40 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 i<256; i++){.
a6b50 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 wsdPrng.s[i]
a6b60 3d 20 28 75 38 29 69 3b 0a 20 20 20 20 7d 0a 20 = (u8)i;. }.
a6b70 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 for(i=0; i<25
a6b80 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 6; i++){. w
a6b90 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 77 73 64 50 sdPrng.j += wsdP
a6ba0 72 6e 67 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d 3b rng.s[i] + k[i];
a6bb0 0a 20 20 20 20 20 20 74 20 3d 20 77 73 64 50 72 . t = wsdPr
a6bc0 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b ng.s[wsdPrng.j];
a6bd0 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73 . wsdPrng.s
a6be0 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77 73 [wsdPrng.j] = ws
a6bf0 64 50 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 20 dPrng.s[i];.
a6c00 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d wsdPrng.s[i] =
a6c10 20 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 73 t;. }. ws
a6c20 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20 31 dPrng.isInit = 1
a6c30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 ;. }.. /* Gene
a6c40 72 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 rate and return
a6c50 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62 79 single random by
a6c60 74 65 0a 20 20 2a 2f 0a 20 20 77 73 64 50 72 6e te. */. wsdPrn
a6c70 67 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 77 73 64 g.i++;. t = wsd
a6c80 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 Prng.s[wsdPrng.i
a6c90 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b ];. wsdPrng.j +
a6ca0 3d 20 74 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 = t;. wsdPrng.s
a6cb0 5b 77 73 64 50 72 6e 67 2e 69 5d 20 3d 20 77 73 [wsdPrng.i] = ws
a6cc0 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e dPrng.s[wsdPrng.
a6cd0 6a 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 5b j];. wsdPrng.s[
a6ce0 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b 0a wsdPrng.j] = t;.
a6cf0 20 20 74 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 t += wsdPrng.s
a6d00 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 72 [wsdPrng.i];. r
a6d10 65 74 75 72 6e 20 77 73 64 50 72 6e 67 2e 73 5b eturn wsdPrng.s[
a6d20 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 t];.}../*.** Ret
a6d30 75 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 74 urn N random byt
a6d40 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 es..*/.SQLITE_AP
a6d50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
a6d60 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c andomness(int N,
a6d70 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 void *pBuf){.
a6d80 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
a6d90 42 75 66 20 3d 20 70 42 75 66 3b 0a 23 69 66 20 Buf = pBuf;.#if
a6da0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
a6db0 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E. sqlite3_mute
a6dc0 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 x *mutex = sqlit
a6dd0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
a6de0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
a6df0 5f 50 52 4e 47 29 3b 0a 23 65 6e 64 69 66 0a 20 _PRNG);.#endif.
a6e00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
a6e10 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 77 nter(mutex);. w
a6e20 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 hile( N-- ){.
a6e30 20 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 72 61 6e *(zBuf++) = ran
a6e40 64 6f 6d 42 79 74 65 28 29 3b 0a 20 20 7d 0a 20 domByte();. }.
a6e50 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
a6e60 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a 0a eave(mutex);.}..
a6e70 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
a6e80 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
a6e90 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 65 73 74 69 ./*.** For testi
a6ea0 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65 20 ng purposes, we
a6eb0 73 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20 74 sometimes want t
a6ec0 6f 20 70 72 65 73 65 72 76 65 20 74 68 65 20 73 o preserve the s
a6ed0 74 61 74 65 20 6f 66 0a 2a 2a 20 50 52 4e 47 20 tate of.** PRNG
a6ee0 61 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65 20 and restore the
a6ef0 50 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76 65 PRNG to its save
a6f00 64 20 73 74 61 74 65 20 61 74 20 61 20 6c 61 74 d state at a lat
a6f10 65 72 20 74 69 6d 65 2c 20 6f 72 0a 2a 2a 20 74 er time, or.** t
a6f20 6f 20 72 65 73 65 74 20 74 68 65 20 50 52 4e 47 o reset the PRNG
a6f30 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 to its initial
a6f40 73 74 61 74 65 2e 20 20 54 68 65 73 65 20 72 6f state. These ro
a6f50 75 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c 69 73 utines accomplis
a6f60 68 0a 2a 2a 20 74 68 6f 73 65 20 74 61 73 6b 73 h.** those tasks
a6f70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
a6f80 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c te3_test_control
a6f90 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c () interface cal
a6fa0 6c 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 ls these routine
a6fb0 73 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 s to.** control
a6fc0 74 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74 61 the PRNG..*/.sta
a6fd0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 tic SQLITE_WSD s
a6fe0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a6ff0 67 54 79 70 65 20 73 71 6c 69 74 65 33 53 61 76 gType sqlite3Sav
a7000 65 64 50 72 6e 67 3b 0a 53 51 4c 49 54 45 5f 50 edPrng;.SQLITE_P
a7010 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a7020 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65 te3PrngSaveState
a7030 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 (void){. memcpy
a7040 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 (. &GLOBAL(st
a7050 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
a7060 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 Type, sqlite3Sav
a7070 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c edPrng),. &GL
a7080 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 OBAL(struct sqli
a7090 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c te3PrngType, sql
a70a0 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 73 ite3Prng),. s
a70b0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e izeof(sqlite3Prn
a70c0 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 g). );.}.SQLITE
a70d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a70e0 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 lite3PrngRestore
a70f0 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d State(void){. m
a7100 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42 emcpy(. &GLOB
a7110 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 AL(struct sqlite
a7120 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 3PrngType, sqlit
a7130 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c e3Prng),. &GL
a7140 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 OBAL(struct sqli
a7150 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c te3PrngType, sql
a7160 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a ite3SavedPrng),.
a7170 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 sizeof(sqlit
a7180 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 e3Prng). );.}.S
a7190 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
a71a0 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 id sqlite3PrngRe
a71b0 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b 0a setState(void){.
a71c0 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 GLOBAL(struct
a71d0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c sqlite3PrngType,
a71e0 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69 73 sqlite3Prng).is
a71f0 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 Init = 0;.}.#end
a7200 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
a7210 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a T_BUILTIN_TEST *
a7220 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
a7230 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f 6d ** End of random
a7240 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
a7250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7270 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
a7280 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 ** Begin file ut
a7290 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.c ************
a72a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a72b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a72c0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 */./*.** 2004 Ap
a72d0 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 ril 13.**.** The
a72e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
a72f0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
a7300 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
a7310 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
a7320 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
a7330 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
a7340 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
a7350 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
a7360 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
a7370 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
a7380 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
a7390 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
a73a0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
a73b0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
a73c0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
a73d0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
a73e0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
a73f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7430 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
a7440 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 le contains rout
a7450 69 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72 61 ines used to tra
a7460 6e 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20 55 nslate between U
a7470 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31 36 TF-8, .** UTF-16
a7480 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64 20 , UTF-16BE, and
a7490 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20 UTF-16LE..**.**
a74a0 24 49 64 3a 20 75 74 66 2e 63 2c 76 20 31 2e 37 $Id: utf.c,v 1.7
a74b0 33 20 32 30 30 39 2f 30 34 2f 30 31 20 31 38 3a 3 2009/04/01 18:
a74c0 34 30 3a 33 32 20 64 72 68 20 45 78 70 20 24 0a 40:32 drh Exp $.
a74d0 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 **.** Notes on U
a74e0 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79 TF-8:.**.** By
a74f0 74 65 2d 30 20 20 20 20 42 79 74 65 2d 31 20 20 te-0 Byte-1
a7500 20 20 42 79 74 65 2d 32 20 20 20 20 42 79 74 65 Byte-2 Byte
a7510 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 -3 Value.**
a7520 30 78 78 78 78 78 78 78 20 20 20 20 20 20 20 20 0xxxxxxx
a7530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7540 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 0000000
a7550 30 20 30 30 30 30 30 30 30 30 20 30 78 78 78 78 0 00000000 0xxxx
a7560 78 78 78 0a 2a 2a 20 20 31 31 30 79 79 79 79 79 xxx.** 110yyyyy
a7570 20 20 31 30 78 78 78 78 78 78 20 20 20 20 20 20 10xxxxxx
a7580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7590 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30 79 00000000 00000y
a75a0 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 yy yyxxxxxx.**
a75b0 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79 79 79 1110zzzz 10yyyy
a75c0 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20 yy 10xxxxxx
a75d0 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 0000000
a75e0 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 0 zzzzyyyy yyxxx
a75f0 78 78 78 0a 2a 2a 20 20 31 31 31 31 30 75 75 75 xxx.** 11110uuu
a7600 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30 79 79 10uuzzzz 10yy
a7610 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 yyyy 10xxxxxx
a7620 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 000uuuuu zzzzyy
a7630 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a yy yyxxxxxx.**.*
a7640 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54 *.** Notes on UT
a7650 46 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77 77 F-16: (with www
a7660 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a w+1==uuuuu).**.*
a7670 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20 20 * Word-0
a7680 20 20 20 20 20 20 20 20 20 20 20 20 57 6f 72 64 Word
a7690 2d 31 20 20 20 20 20 20 20 20 20 20 56 61 6c 75 -1 Valu
a76a0 65 0a 2a 2a 20 20 31 31 30 31 31 30 77 77 20 77 e.** 110110ww w
a76b0 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31 31 31 wzzzzyy 110111
a76c0 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20 30 yy yyxxxxxx 0
a76d0 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79 00uuuuu zzzzyyyy
a76e0 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 7a 7a yyxxxxxx.** zz
a76f0 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 20 zzyyyy yyxxxxxx
a7700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7710 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 00000000
a7720 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 zzzzyyyy yyxxxxx
a7730 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f x.**.**.** BOM o
a7740 72 20 42 79 74 65 20 4f 72 64 65 72 20 4d 61 72 r Byte Order Mar
a7750 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66 20 30 k:.** 0xff 0
a7760 78 66 65 20 20 20 6c 69 74 74 6c 65 2d 65 6e 64 xfe little-end
a7770 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f ian utf-16 follo
a7780 77 73 0a 2a 2a 20 20 20 20 20 30 78 66 65 20 30 ws.** 0xfe 0
a7790 78 66 66 20 20 20 62 69 67 2d 65 6e 64 69 61 6e xff big-endian
a77a0 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a utf-16 follows.
a77b0 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*/./*********
a77c0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 ***** Include vd
a77d0 62 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d beInt.h in the m
a77e0 69 64 64 6c 65 20 6f 66 20 75 74 66 2e 63 20 2a iddle of utf.c *
a77f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7800 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
a7810 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
a7820 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a vdbeInt.h *****
a7830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7850 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 ****/./*.** 2003
a7860 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a September 6.**.
a7870 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
a7880 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
a7890 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
a78a0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
a78b0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
a78c0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
a78d0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
a78e0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
a78f0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
a7900 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
a7910 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
a7920 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
a7930 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
a7940 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
a7950 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
a7960 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
a7970 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
a7980 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
a7990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a79a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a79b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a79c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
a79d0 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 his is the heade
a79e0 72 20 66 69 6c 65 20 66 6f 72 20 69 6e 66 6f 72 r file for infor
a79f0 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 70 mation that is p
a7a00 72 69 76 61 74 65 20 74 6f 20 74 68 65 0a 2a 2a rivate to the.**
a7a10 20 56 44 42 45 2e 20 20 54 68 69 73 20 69 6e 66 VDBE. This inf
a7a20 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 74 6f ormation used to
a7a30 20 61 6c 6c 20 62 65 20 61 74 20 74 68 65 20 74 all be at the t
a7a40 6f 70 20 6f 66 20 74 68 65 20 73 69 6e 67 6c 65 op of the single
a7a50 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64 65 20 .** source code
a7a60 66 69 6c 65 20 22 76 64 62 65 2e 63 22 2e 20 20 file "vdbe.c".
a7a70 57 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 62 When that file b
a7a80 65 63 61 6d 65 20 74 6f 6f 20 62 69 67 20 28 6f ecame too big (o
a7a90 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69 6e 65 ver.** 6000 line
a7aa0 73 20 6c 6f 6e 67 29 20 69 74 20 77 61 73 20 73 s long) it was s
a7ab0 70 6c 69 74 20 75 70 20 69 6e 74 6f 20 73 65 76 plit up into sev
a7ac0 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66 69 6c eral smaller fil
a7ad0 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73 20 68 es and.** this h
a7ae0 65 61 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f eader informatio
a7af0 6e 20 77 61 73 20 66 61 63 74 6f 72 65 64 20 6f n was factored o
a7b00 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 ut..**.** $Id: v
a7b10 64 62 65 49 6e 74 2e 68 2c 76 20 31 2e 31 37 34 dbeInt.h,v 1.174
a7b20 20 32 30 30 39 2f 30 36 2f 32 33 20 31 34 3a 31 2009/06/23 14:1
a7b30 35 3a 30 34 20 64 72 68 20 45 78 70 20 24 0a 2a 5:04 drh Exp $.*
a7b40 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49 /.#ifndef _VDBEI
a7b50 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 NT_H_.#define _V
a7b60 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a DBEINT_H_../*.**
a7b70 20 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 SQL is translat
a7b80 65 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e ed into a sequen
a7b90 63 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f ce of instructio
a7ba0 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 ns to be.** exec
a7bb0 75 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61 uted by a virtua
a7bc0 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 l machine. Each
a7bd0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 instruction is
a7be0 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f an instance.** o
a7bf0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
a7c00 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 structure..*/.ty
a7c10 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
a7c20 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 eOp Op;../*.** B
a7c30 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f oolean values.*/
a7c40 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 .typedef unsigne
a7c50 64 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a d char Bool;../*
a7c60 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 .** A cursor is
a7c70 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 a pointer into a
a7c80 20 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 single BTree wi
a7c90 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 thin a database
a7ca0 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 file..** The cur
a7cb0 73 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 sor can seek to
a7cc0 61 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 a BTree entry wi
a7cd0 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 th a particular
a7ce0 6b 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 key, or.** loop
a7cf0 6f 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 over all entries
a7d00 20 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 of the Btree.
a7d10 59 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 You can also ins
a7d20 65 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a ert new BTree.**
a7d30 20 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 entries or retr
a7d40 69 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 ieve the key or
a7d50 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e data from the en
a7d60 74 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 try that the cur
a7d70 73 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e sor.** is curren
a7d80 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e tly pointing to.
a7d90 0a 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 .** .** Every cu
a7da0 72 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 rsor that the vi
a7db0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 rtual machine ha
a7dc0 73 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 s open is repres
a7dd0 65 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 ented by an.** i
a7de0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
a7df0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
a7e00 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 re..**.** If the
a7e10 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72 VdbeCursor.isTr
a7e20 69 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73 iggerRow flag is
a7e30 20 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68 set it means th
a7e40 61 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 at this cursor i
a7e50 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69 s.** really a si
a7e60 6e 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65 ngle row that re
a7e70 70 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57 presents the NEW
a7e80 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 or OLD pseudo-t
a7e90 61 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 able of.** a row
a7ea0 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64 trigger. The d
a7eb0 61 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 ata for the row
a7ec0 69 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 is stored in Vdb
a7ed0 65 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e eCursor.pData an
a7ee0 64 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 d.** the rowid i
a7ef0 73 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e s in VdbeCursor.
a7f00 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 iKey..*/.struct
a7f10 56 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42 VdbeCursor {. B
a7f20 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 tCursor *pCursor
a7f30 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 ; /* The curs
a7f40 6f 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 or structure of
a7f50 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 the backend */.
a7f60 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
a7f70 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
a7f80 6f 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 of cursor databa
a7f90 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 se in db->aDb[]
a7fa0 28 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 (or -1) */. i64
a7fb0 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 lastRowid;
a7fc0 20 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 /* Last rowid
a7fd0 20 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 from a Next or
a7fe0 4e 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f NextIdx operatio
a7ff0 6e 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f n */. Bool zero
a8000 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ed; /*
a8010 54 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f True if zeroed o
a8020 75 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 ut and ready for
a8030 20 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c reuse */. Bool
a8040 20 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 rowidIsValid;
a8050 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 /* True if las
a8060 74 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 tRowid is valid
a8070 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 */. Bool atFirs
a8080 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 t; /* Tr
a8090 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 ue if pointing t
a80a0 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f o first entry */
a80b0 0a 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f . Bool useRando
a80c0 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 mRowid; /* Gene
a80d0 72 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 rate new record
a80e0 6e 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e numbers semi-ran
a80f0 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 domly */. Bool
a8100 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 nullRow;
a8110 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e /* True if poin
a8120 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 ting to a row wi
a8130 74 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 th no data */.
a8140 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 Bool deferredMov
a8150 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 eto; /* A call
a8160 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d to sqlite3BtreeM
a8170 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 oveto() is neede
a8180 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 d */. Bool isTa
a8190 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ble; /*
a81a0 54 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 True if a table
a81b0 72 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 requiring intege
a81c0 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c r keys */. Bool
a81d0 20 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 isIndex;
a81e0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 /* True if an
a81f0 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 index containing
a8200 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 keys only - no
a8210 64 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f data */. i64 mo
a8220 76 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 vetoTarget;
a8230 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 /* Argument to t
a8240 68 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 he deferred sqli
a8250 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 te3BtreeMoveto()
a8260 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 */. Btree *pBt
a8270 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 ; /* S
a8280 65 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c eparate file hol
a8290 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 ding temporary t
a82a0 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 70 73 able */. int ps
a82b0 65 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 eudoTableReg;
a82c0 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 /* Register hold
a82d0 69 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 ing pseudotable
a82e0 63 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 4b 65 content. */. Ke
a82f0 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b yInfo *pKeyInfo;
a8300 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 /* Info abou
a8310 74 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65 t index keys nee
a8320 64 65 64 20 62 79 20 69 6e 64 65 78 20 63 75 72 ded by index cur
a8330 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 sors */. int nF
a8340 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 ield;
a8350 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 /* Number of fie
a8360 6c 64 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 lds in the heade
a8370 72 20 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f r */. i64 seqCo
a8380 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 unt; /*
a8390 53 65 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 Sequence counter
a83a0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 */. sqlite3_vt
a83b0 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 ab_cursor *pVtab
a83c0 43 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 Cursor; /* The
a83d0 63 75 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72 cursor for a vir
a83e0 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 tual table */.
a83f0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
a8400 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 dule *pModule;
a8410 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 /* Module for
a8420 20 63 75 72 73 6f 72 20 70 56 74 61 62 43 75 72 cursor pVtabCur
a8430 73 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73 sor */.. /* Res
a8440 75 6c 74 20 6f 66 20 6c 61 73 74 20 73 71 6c 69 ult of last sqli
a8450 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 te3BtreeMoveto()
a8460 20 64 6f 6e 65 20 62 79 20 61 6e 20 4f 50 5f 4e done by an OP_N
a8470 6f 74 45 78 69 73 74 73 20 6f 72 20 0a 20 20 2a otExists or . *
a8480 2a 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 * OP_IsUnique op
a8490 63 6f 64 65 20 6f 6e 20 74 68 69 73 20 63 75 72 code on this cur
a84a0 73 6f 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 sor. */. int se
a84b0 65 6b 52 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 ekResult;.. /*
a84c0 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69 Cached informati
a84d0 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61 on about the hea
a84e0 64 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 der for the data
a84f0 20 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65 record that the
a8500 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 20 . ** cursor is
a8510 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
a8520 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 6c ng to. Only val
a8530 69 64 20 69 66 20 63 61 63 68 65 53 74 61 74 75 id if cacheStatu
a8540 73 20 6d 61 74 63 68 65 73 0a 20 20 2a 2a 20 56 s matches. ** V
a8550 64 62 65 2e 63 61 63 68 65 43 74 72 2e 20 20 56 dbe.cacheCtr. V
a8560 64 62 65 2e 63 61 63 68 65 43 74 72 20 77 69 6c dbe.cacheCtr wil
a8570 6c 20 6e 65 76 65 72 20 74 61 6b 65 20 6f 6e 20 l never take on
a8580 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a the value of. *
a8590 2a 20 43 41 43 48 45 5f 53 54 41 4c 45 20 61 6e * CACHE_STALE an
a85a0 64 20 73 6f 20 73 65 74 74 69 6e 67 20 63 61 63 d so setting cac
a85b0 68 65 53 74 61 74 75 73 3d 43 41 43 48 45 5f 53 heStatus=CACHE_S
a85c0 54 41 4c 45 20 67 75 61 72 61 6e 74 65 65 73 20 TALE guarantees
a85d0 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 63 61 that. ** the ca
a85e0 63 68 65 20 69 73 20 6f 75 74 20 6f 66 20 64 61 che is out of da
a85f0 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 52 te.. **. ** aR
a8600 6f 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 ow might point t
a8610 6f 20 28 65 70 68 65 6d 65 72 61 6c 29 20 64 61 o (ephemeral) da
a8620 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 ta for the curre
a8630 6e 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 6d 69 nt row, or it mi
a8640 67 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 4c 4c ght. ** be NULL
a8650 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 .. */. u32 cac
a8660 68 65 53 74 61 74 75 73 3b 20 20 20 20 20 20 2f heStatus; /
a8670 2a 20 43 61 63 68 65 20 69 73 20 76 61 6c 69 64 * Cache is valid
a8680 20 69 66 20 74 68 69 73 20 6d 61 74 63 68 65 73 if this matches
a8690 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 2a Vdbe.cacheCtr *
a86a0 2f 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53 /. int payloadS
a86b0 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 ize; /* Tot
a86c0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
a86d0 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 es in the record
a86e0 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 */. u32 *aType
a86f0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
a8700 79 70 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61 ype values for a
a8710 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 ll entries in th
a8720 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 e record */. u3
a8730 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 2 *aOffset;
a8740 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20 6f 66 /* Cached of
a8750 66 73 65 74 73 20 74 6f 20 74 68 65 20 73 74 61 fsets to the sta
a8760 72 74 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d rt of each colum
a8770 6e 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 ns data */. u8
a8780 2a 61 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 *aRow;
a8790 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 74 /* Data for t
a87a0 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 he current row,
a87b0 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 if all on one pa
a87c0 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 ge */.};.typedef
a87d0 20 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 struct VdbeCurs
a87e0 6f 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0a 0a or VdbeCursor;..
a87f0 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 75 62 /*.** When a sub
a8800 2d 70 72 6f 67 72 61 6d 20 69 73 20 65 78 65 63 -program is exec
a8810 75 74 65 64 20 28 4f 50 5f 50 72 6f 67 72 61 6d uted (OP_Program
a8820 29 2c 20 61 20 73 74 72 75 63 74 75 72 65 20 6f ), a structure o
a8830 66 20 74 68 69 73 20 74 79 70 65 0a 2a 2a 20 69 f this type.** i
a8840 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73 s allocated to s
a8850 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 tore the current
a8860 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 72 value of the pr
a8870 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 2c 20 61 ogram counter, a
a8880 73 0a 2a 2a 20 77 65 6c 6c 20 61 73 20 74 68 65 s.** well as the
a8890 20 63 75 72 72 65 6e 74 20 6d 65 6d 6f 72 79 20 current memory
a88a0 63 65 6c 6c 20 61 72 72 61 79 20 61 6e 64 20 76 cell array and v
a88b0 61 72 69 6f 75 73 20 6f 74 68 65 72 20 66 72 61 arious other fra
a88c0 6d 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 76 me specific.** v
a88d0 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 alues stored in
a88e0 74 68 65 20 56 64 62 65 20 73 74 72 75 63 74 2e the Vdbe struct.
a88f0 20 57 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 When the sub-pr
a8900 6f 67 72 61 6d 20 69 73 20 66 69 6e 69 73 68 65 ogram is finishe
a8910 64 2c 20 0a 2a 2a 20 74 68 65 73 65 20 76 61 6c d, .** these val
a8920 75 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 62 ues are copied b
a8930 61 63 6b 20 74 6f 20 74 68 65 20 56 64 62 65 20 ack to the Vdbe
a8940 66 72 6f 6d 20 74 68 65 20 56 64 62 65 46 72 61 from the VdbeFra
a8950 6d 65 20 73 74 72 75 63 74 75 72 65 2c 0a 2a 2a me structure,.**
a8960 20 72 65 73 74 6f 72 69 6e 67 20 74 68 65 20 73 restoring the s
a8970 74 61 74 65 20 6f 66 20 74 68 65 20 56 4d 20 74 tate of the VM t
a8980 6f 20 61 73 20 69 74 20 77 61 73 20 62 65 66 6f o as it was befo
a8990 72 65 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 re the sub-progr
a89a0 61 6d 0a 2a 2a 20 62 65 67 61 6e 20 65 78 65 63 am.** began exec
a89b0 75 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 72 61 uting..**.** Fra
a89c0 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 mes are stored i
a89d0 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 n a linked list
a89e0 68 65 61 64 65 64 20 61 74 20 56 64 62 65 2e 70 headed at Vdbe.p
a89f0 50 61 72 65 6e 74 2e 20 56 64 62 65 2e 70 50 61 Parent. Vdbe.pPa
a8a00 72 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70 rent.** is the p
a8a10 61 72 65 6e 74 20 6f 66 20 74 68 65 20 63 75 72 arent of the cur
a8a20 72 65 6e 74 20 66 72 61 6d 65 2c 20 6f 72 20 7a rent frame, or z
a8a30 65 72 6f 20 69 66 20 74 68 65 20 63 75 72 72 65 ero if the curre
a8a40 6e 74 20 66 72 61 6d 65 0a 2a 2a 20 69 73 20 74 nt frame.** is t
a8a50 68 65 20 6d 61 69 6e 20 56 64 62 65 20 70 72 6f he main Vdbe pro
a8a60 67 72 61 6d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 gram..*/.typedef
a8a70 20 73 74 72 75 63 74 20 56 64 62 65 46 72 61 6d struct VdbeFram
a8a80 65 20 56 64 62 65 46 72 61 6d 65 3b 0a 73 74 72 e VdbeFrame;.str
a8a90 75 63 74 20 56 64 62 65 46 72 61 6d 65 20 7b 0a uct VdbeFrame {.
a8aa0 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 Vdbe *v;
a8ab0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 /* VM
a8ac0 74 68 69 73 20 66 72 61 6d 65 20 62 65 6c 6f 6e this frame belon
a8ad0 67 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 70 gs to */. int p
a8ae0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
a8af0 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f /* Program Co
a8b00 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 61 unter */. Op *a
a8b10 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Op;
a8b20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6e /* Program in
a8b30 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 structions */.
a8b40 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 int nOp;
a8b50 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
a8b60 6f 66 20 61 4f 70 20 61 72 72 61 79 20 2a 2f 0a of aOp array */.
a8b70 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 Mem *aMem;
a8b80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 /* Arr
a8b90 61 79 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c ay of memory cel
a8ba0 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d ls */. int nMem
a8bb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a8bc0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
a8bd0 72 69 65 73 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a ries in aMem */.
a8be0 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 VdbeCursor **a
a8bf0 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 45 6c 65 pCsr; /* Ele
a8c00 6d 65 6e 74 20 6f 66 20 56 64 62 65 20 63 75 72 ment of Vdbe cur
a8c10 73 6f 72 73 20 2a 2f 0a 20 20 75 31 36 20 6e 43 sors */. u16 nC
a8c20 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 ursor;
a8c30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
a8c40 6e 74 72 69 65 73 20 69 6e 20 61 70 43 73 72 20 ntries in apCsr
a8c50 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e */. void *token
a8c60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a8c70 43 6f 70 79 20 6f 66 20 53 75 62 50 72 6f 67 72 Copy of SubProgr
a8c80 61 6d 2e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e am.token */. in
a8c90 74 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20 t nChildMem;
a8ca0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
a8cb0 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 of memory cells
a8cc0 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20 for child frame
a8cd0 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43 */. int nChildC
a8ce0 73 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 sr; /*
a8cf0 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 Number of cursor
a8d00 73 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d s for child fram
a8d10 65 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 e */. i64 lastR
a8d20 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f owid; /
a8d30 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f * Last insert ro
a8d40 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 wid (sqlite3.las
a8d50 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 tRowid) */. int
a8d60 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 nChange;
a8d70 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e /* Statemen
a8d80 74 20 63 68 61 6e 67 65 73 20 28 56 64 62 65 2e t changes (Vdbe.
a8d90 6e 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a 2f nChanges) */
a8da0 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50 . VdbeFrame *pP
a8db0 61 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61 arent; /* Pa
a8dc0 72 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61 rent of this fra
a8dd0 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e me */.};..#defin
a8de0 65 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 e VdbeFrameMem(p
a8df0 29 20 28 28 4d 65 6d 20 2a 29 26 28 28 75 38 20 ) ((Mem *)&((u8
a8e00 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65 *)p)[ROUND8(size
a8e10 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29 5d 29 of(VdbeFrame))])
a8e20 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20 ../*.** A value
a8e30 66 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63 for VdbeCursor.c
a8e40 61 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d acheValid that m
a8e50 65 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69 eans the cache i
a8e60 73 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64 s always invalid
a8e70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43 ..*/.#define CAC
a8e80 48 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a HE_STALE 0../*.*
a8e90 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 * Internally, th
a8ea0 65 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74 e vdbe manipulat
a8eb0 65 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51 es nearly all SQ
a8ec0 4c 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a L values as Mem.
a8ed0 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45 ** structures. E
a8ee0 61 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d ach Mem struct m
a8ef0 61 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c ay cache multipl
a8f00 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e e representation
a8f10 73 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e s (string,.** in
a8f20 74 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74 teger etc.) of t
a8f30 68 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20 he same value.
a8f40 41 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65 A value (and the
a8f50 72 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63 refore Mem struc
a8f60 74 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65 ture).** has the
a8f70 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65 following prope
a8f80 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63 rties:.**.** Eac
a8f90 68 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61 h value has a ma
a8fa0 6e 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65 nifest type. The
a8fb0 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f manifest type o
a8fc0 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 f the value stor
a8fd0 65 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73 ed.** in a Mem s
a8fe0 74 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65 truct is returne
a8ff0 64 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65 d by the MemType
a9000 28 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68 (Mem*) macro. Th
a9010 65 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65 e type is.** one
a9020 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c of SQLITE_NULL,
a9030 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c SQLITE_INTEGER,
a9040 20 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51 SQLITE_REAL, SQ
a9050 4c 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20 LITE_TEXT or.**
a9060 53 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a SQLITE_BLOB..*/.
a9070 73 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75 struct Mem {. u
a9080 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 nion {. i64 i
a9090 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
a90a0 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e * Integer value.
a90b0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 */. int nZer
a90c0 6f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 o; /* U
a90d0 73 65 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d sed when bit MEM
a90e0 5f 5a 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20 _Zero is set in
a90f0 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e flags */. Fun
a9100 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 cDef *pDef;
a9110 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 /* Used only wh
a9120 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 en flags==MEM_Ag
a9130 67 20 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20 g */. RowSet
a9140 2a 70 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20 *pRowSet; /*
a9150 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 Used only when f
a9160 6c 61 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74 lags==MEM_RowSet
a9170 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d */. VdbeFram
a9180 65 20 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55 e *pFrame; /* U
a9190 73 65 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d sed when flags==
a91a0 4d 45 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d MEM_Frame */. }
a91b0 20 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 u;. double r;
a91c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 /* Rea
a91d0 6c 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c l value */. sql
a91e0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
a91f0 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 /* The associat
a9200 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ed database conn
a9210 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 ection */. char
a9220 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 *z;
a9230 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f /* String or BLO
a9240 42 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 B value */. int
a9250 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
a9260 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 /* Number of ch
a9270 61 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 aracters in stri
a9280 6e 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 ng value, exclud
a9290 69 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 ing '\0' */. u1
a92a0 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 6 flags;
a92b0 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e /* Some combin
a92c0 61 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c ation of MEM_Nul
a92d0 6c 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f l, MEM_Str, MEM_
a92e0 44 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 Dyn, etc. */. u
a92f0 38 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 8 type;
a9300 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c /* One of SQL
a9310 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 ITE_NULL, SQLITE
a9320 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e _TEXT, SQLITE_IN
a9330 54 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 TEGER, etc */.
a9340 75 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 u8 enc;
a9350 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 /* SQLITE_UT
a9360 46 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 F8, SQLITE_UTF16
a9370 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 BE, SQLITE_UTF16
a9380 4c 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 LE */. void (*x
a9390 44 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f Del)(void *); /
a93a0 2a 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 * If not null, c
a93b0 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
a93c0 6e 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e n to delete Mem.
a93d0 7a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 z */. char *zMa
a93e0 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 lloc; /* Dy
a93f0 6e 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c namic buffer all
a9400 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 ocated by sqlite
a9410 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 3_malloc() */.};
a9420 0a 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 ../* One or more
a9430 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
a9440 67 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 g flags are set
a9450 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 to indicate the
a9460 76 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 validOK.** repre
a9470 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 sentations of th
a9480 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 e value stored i
a9490 6e 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 n the Mem struct
a94a0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d ..**.** If the M
a94b0 45 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 EM_Null flag is
a94c0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 set, then the va
a94d0 6c 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 lue is an SQL NU
a94e0 4c 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 LL value..** No
a94f0 6f 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 other flags may
a9500 62 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 be set in this c
a9510 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ase..**.** If th
a9520 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 e MEM_Str flag i
a9530 73 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a s set then Mem.z
a9540 20 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 points at a str
a9550 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
a9560 6f 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 on..** Usually t
a9570 68 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 his is encoded i
a9580 6e 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f n the same unico
a9590 64 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 de encoding as t
a95a0 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 he main.** datab
a95b0 61 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 ase (see below f
a95c0 6f 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 or exceptions).
a95d0 49 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 If the MEM_Term
a95e0 66 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 flag is also.**
a95f0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 set, then the st
a9600 72 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d ring is nul term
a9610 69 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f inated. The MEM_
a9620 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c Int and MEM_Real
a9630 20 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 .** flags may c
a9640 6f 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 oexist with the
a9650 4d 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a MEM_Str flag..**
a9660 0a 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 .** Multiple of
a9670 74 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e these values can
a9680 20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 appear in Mem.f
a9690 6c 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 lags. But only
a96a0 6f 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 one.** at a time
a96b0 20 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d can appear in M
a96c0 65 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 em.type..*/.#def
a96d0 69 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 ine MEM_Null
a96e0 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 0x0001 /* Va
a96f0 6c 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 lue is NULL */.#
a9700 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 define MEM_Str
a9710 20 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 0x0002 /*
a9720 20 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 Value is a stri
a9730 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 ng */.#define ME
a9740 4d 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 M_Int 0x00
a9750 30 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 04 /* Value is
a9760 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 an integer */.#
a9770 64 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 define MEM_Real
a9780 20 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 0x0008 /*
a9790 20 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c Value is a real
a97a0 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 number */.#defi
a97b0 6e 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 ne MEM_Blob
a97c0 20 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 0x0010 /* Val
a97d0 75 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a ue is a BLOB */.
a97e0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 #define MEM_RowS
a97f0 65 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f et 0x0020 /
a9800 2a 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 * Value is a Row
a9810 53 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 Set object */.#d
a9820 65 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20 efine MEM_Frame
a9830 20 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20 0x0040 /*
a9840 56 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46 Value is a VdbeF
a9850 72 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 rame object */.#
a9860 64 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d define MEM_TypeM
a9870 61 73 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a ask 0x00ff /*
a9880 20 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 Mask of type bi
a9890 74 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 ts */../* Whenev
a98a0 65 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 er Mem contains
a98b0 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f a valid string o
a98c0 72 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 r blob represent
a98d0 61 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a ation, one of.**
a98e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
a98f0 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 lags must be set
a9900 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
a9910 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d e memory managem
a9920 65 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f ent.** policy fo
a9930 72 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 r Mem.z. The ME
a9940 4d 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c M_Term flag tell
a9950 73 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 s us whether or
a9960 6e 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e not the.** strin
a9970 67 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 g is \000 or \u0
a9980 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 000 terminated.*
a9990 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 /.#define MEM_Te
a99a0 72 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20 rm 0x0200
a99b0 20 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 /* String rep i
a99c0 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 s nul terminated
a99d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
a99e0 44 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 Dyn 0x0400
a99f0 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 /* Need to ca
a9a00 6c 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 ll sqliteFree()
a9a10 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 on Mem.z */.#def
a9a20 69 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 ine MEM_Static
a9a30 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 0x0800 /* Me
a9a40 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 m.z points to a
a9a50 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f static string */
a9a60 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 .#define MEM_Eph
a9a70 65 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20 em 0x1000
a9a80 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 /* Mem.z points
a9a90 74 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 to an ephemeral
a9aa0 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
a9ab0 65 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 e MEM_Agg
a9ac0 30 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 0x2000 /* Mem.
a9ad0 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 z points to an a
a9ae0 67 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 gg function cont
a9af0 65 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ext */.#define M
a9b00 45 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 EM_Zero 0x4
a9b10 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 000 /* Mem.i c
a9b20 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 ontains count of
a9b30 20 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 0s appended to
a9b40 62 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 blob */..#ifdef
a9b50 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
a9b60 42 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 BLOB. #undef ME
a9b70 4d 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 M_Zero. #define
a9b80 20 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 MEM_Zero 0x0000
a9b90 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
a9ba0 43 6c 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 Clear any existi
a9bb0 6e 67 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 ng type flags fr
a9bc0 6f 6d 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 om a Mem and rep
a9bd0 6c 61 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 lace them with f
a9be0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 .*/.#define MemS
a9bf0 65 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 etTypeFlag(p, f)
a9c00 20 5c 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 \. ((p)->flag
a9c10 73 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 s = ((p)->flags&
a9c20 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d ~(MEM_TypeMask|M
a9c30 45 4d 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f EM_Zero))|f).../
a9c40 2a 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 * A VdbeFunc is
a9c50 6a 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 just a FuncDef (
a9c60 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 defined in sqlit
a9c70 65 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e eInt.h) that con
a9c80 74 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f tains.** additio
a9c90 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 nal information
a9ca0 61 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 about auxiliary
a9cb0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e information boun
a9cc0 64 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a d to arguments.*
a9cd0 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f * of the functio
a9ce0 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 n. This is used
a9cf0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
a9d00 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 e sqlite3_get_au
a9d10 78 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 xdata().** and s
a9d20 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
a9d30 74 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 ta() APIs. The
a9d40 22 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d "auxdata" is som
a9d50 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 e auxiliary data
a9d60 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 .** that can be
a9d70 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
a9d80 61 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d a constant argum
a9d90 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ent to a functio
a9da0 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f n. This.** allo
a9db0 77 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 ws functions suc
a9dc0 68 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f h as "regexp" to
a9dd0 20 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 compile their c
a9de0 6f 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a onstant regular.
a9df0 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 ** expression ar
a9e00 67 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 gument once and
a9e10 72 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 reused the compi
a9e20 6c 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c led code for mul
a9e30 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 tiple.** invocat
a9e40 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ions..*/.struct
a9e50 56 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e VdbeFunc {. Fun
a9e60 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 cDef *pFunc;
a9e70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
a9e80 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 e definition of
a9e90 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a the function */.
a9ea0 20 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 int nAux;
a9eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a9ec0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
a9ed0 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 ries allocated f
a9ee0 6f 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 or apAux[] */.
a9ef0 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b struct AuxData {
a9f00 0a 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b . void *pAux;
a9f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a9f20 20 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 /* Aux data f
a9f30 6f 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 or the i-th argu
a9f40 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 ment */. void
a9f50 20 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 (*xDelete)(void
a9f60 20 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 *); /* Des
a9f70 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 tructor for the
a9f80 61 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 aux data */. }
a9f90 61 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 apAux[1];
a9fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
a9fb0 6e 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 ne slot for each
a9fc0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
a9fd0 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 nt */.};../*.**
a9fe0 54 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 The "context" ar
a9ff0 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 gument for a ins
aa000 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f tallable functio
aa010 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f n. A pointer to
aa020 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 an.** instance
aa030 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
aa040 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 e is the first a
aa050 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 rgument to the r
aa060 6f 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 outines used.**
aa070 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 implement the SQ
aa080 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a L functions..**.
aa090 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79 ** There is a ty
aa0a0 70 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 pedef for this s
aa0b0 74 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 tructure in sqli
aa0c0 74 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f te.h. So all ro
aa0d0 75 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 utines,.** even
aa0e0 74 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 the public inter
aa0f0 66 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 face to SQLite,
aa100 63 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 can use a pointe
aa110 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 r to this struct
aa120 75 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 ure..** But this
aa130 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c file is the onl
aa140 79 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 y place where th
aa150 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 e internal detai
aa160 6c 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 ls of this.** st
aa170 72 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 ructure are know
aa180 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 n..**.** This st
aa190 72 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e ructure is defin
aa1a0 65 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 ed inside of vdb
aa1b0 65 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 eInt.h because i
aa1c0 74 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74 t uses substruct
aa1d0 75 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 ures.** (Mem) wh
aa1e0 69 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 ich are only def
aa1f0 69 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 ined there..*/.s
aa200 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f truct sqlite3_co
aa210 6e 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 ntext {. FuncDe
aa220 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 f *pFunc;
aa230 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 /* Pointer to fu
aa240 6e 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 nction informati
aa250 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 on. MUST BE FIR
aa260 53 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 ST */. VdbeFunc
aa270 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a *pVdbeFunc; /*
aa280 20 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 Auxilary data,
aa290 69 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 if created. */.
aa2a0 20 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 Mem s;
aa2b0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 /* The re
aa2c0 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 turn value is st
aa2d0 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d ored here */. M
aa2e0 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 em *pMem;
aa2f0 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 /* Memory c
aa300 65 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 ell used to stor
aa310 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 e aggregate cont
aa320 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 ext */. int isE
aa330 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f rror; /
aa340 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 * Error code ret
aa350 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e urned by the fun
aa360 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c ction. */. Coll
aa370 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 Seq *pColl;
aa380 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 /* Collating s
aa390 65 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f equence */.};../
aa3a0 2a 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 *.** A Set struc
aa3b0 74 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 ture is used for
aa3c0 20 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 quick testing t
aa3d0 6f 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 o see if a value
aa3e0 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 .** is part of a
aa3f0 20 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 small set. Set
aa400 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d s are used to im
aa410 70 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b plement code lik
aa420 65 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 e.** this:.**
aa430 20 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 x.y IN
aa440 28 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d ('hi','hoo','hum
aa450 27 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 ').*/.typedef st
aa460 72 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 ruct Set Set;.st
aa470 72 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 ruct Set {. Has
aa480 68 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 h hash;
aa490 20 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 /* A set is
aa4a0 6a 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c just a hash tabl
aa4b0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 e */. HashElem
aa4c0 2a 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a *prev; /*
aa4d0 20 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 Previously acce
aa4e0 73 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e ssed hash elemen
aa4f0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
aa500 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
aa510 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
aa520 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 . This structur
aa530 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 e contains the c
aa540 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65 omplete.** state
aa550 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 of the virtual
aa560 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 machine..**.** T
aa570 68 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 he "sqlite3_stmt
aa580 22 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e " structure poin
aa590 74 65 72 20 74 68 61 74 20 69 73 20 72 65 74 75 ter that is retu
aa5a0 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
aa5b0 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20 compile().** is
aa5c0 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 really a pointer
aa5d0 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 to an instance
aa5e0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
aa5f0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62 e..**.** The Vdb
aa600 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76 e.inVtabMethod v
aa610 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 ariable is set t
aa620 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 o non-zero for t
aa630 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a he duration of.*
aa640 2a 20 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 * any virtual ta
aa650 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63 ble method invoc
aa660 61 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74 ations made by t
aa670 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e he vdbe program.
aa680 20 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f It is.** set to
aa690 20 32 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20 2 for xDestroy
aa6a0 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64 method calls and
aa6b0 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 1 for all other
aa6c0 20 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a methods. This.*
aa6d0 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 * variable is us
aa6e0 65 64 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f ed for two purpo
aa6f0 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44 ses: to allow xD
aa700 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74 estroy methods t
aa710 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52 o execute.** "DR
aa720 4f 50 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d OP TABLE" statem
aa730 65 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76 ents and to prev
aa740 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73 ent some nasty s
aa750 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a ide effects of.*
aa760 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 * malloc failure
aa770 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 when SQLite is
aa780 69 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76 invoked recursiv
aa790 65 6c 79 20 62 79 20 61 20 76 69 72 74 75 61 6c ely by a virtual
aa7a0 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f table .** metho
aa7b0 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 d function..*/.s
aa7c0 74 72 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73 truct Vdbe {. s
aa7d0 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
aa7e0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
aa7f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
aa800 6e 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 n that owns this
aa810 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
aa820 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e 65 Vdbe *pPrev,*pNe
aa830 78 74 3b 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 xt; /* Linke
aa840 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 d list of VDBEs
aa850 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 with the same Vd
aa860 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e be.db */. int n
aa870 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Op;
aa880 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
aa890 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 instructions in
aa8a0 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 the program */.
aa8b0 20 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 int nOpAlloc;
aa8c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
aa8d0 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f er of slots allo
aa8e0 63 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 cated for aOp[]
aa8f0 2a 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 */. Op *aOp;
aa900 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
aa910 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
aa920 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e e virtual machin
aa930 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 e's program */.
aa940 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20 int nLabel;
aa950 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
aa960 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 er of labels use
aa970 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 d */. int nLabe
aa980 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f lAlloc; /
aa990 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 * Number of slot
aa9a0 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 s allocated in a
aa9b0 4c 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 Label[] */. int
aa9c0 20 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 *aLabel;
aa9d0 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f /* Space to
aa9e0 20 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73 hold the labels
aa9f0 20 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 */. Mem **apAr
aaa00 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a g; /*
aaa10 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 75 Arguments to cu
aaa20 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e rrently executin
aaa30 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 g user function
aaa40 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 */. Mem *aColNa
aaa50 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 me; /*
aaa60 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 Column names to
aaa70 72 65 74 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20 return */. Mem
aaa80 2a 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20 *pResultSet;
aaa90 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
aaaa0 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 o an array of re
aaab0 73 75 6c 74 73 20 2a 2f 0a 20 20 75 31 36 20 6e sults */. u16 n
aaac0 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 ResColumn;
aaad0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
aaae0 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 columns in one r
aaaf0 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ow of the result
aab00 20 73 65 74 20 2a 2f 0a 20 20 75 31 36 20 6e 43 set */. u16 nC
aab10 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 ursor;
aab20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
aab30 6c 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 lots in apCsr[]
aab40 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 */. VdbeCursor
aab50 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 **apCsr; /*
aab60 4f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 One element of t
aab70 68 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61 his array for ea
aab80 63 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a ch open cursor *
aab90 2f 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69 /. u8 errorActi
aaba0 6f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 on; /* R
aabb0 65 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74 ecovery action t
aabc0 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 o do in case of
aabd0 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 an error */. u8
aabe0 20 6f 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20 okVar;
aabf0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
aac00 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 azVar[] has bee
aac10 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f n initialized */
aac20 0a 20 20 75 31 36 20 6e 56 61 72 3b 20 20 20 20 . u16 nVar;
aac30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
aac40 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
aac50 69 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d in aVar[] */. M
aac60 65 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20 em *aVar;
aac70 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 /* Values
aac80 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69 for the OP_Vari
aac90 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a able opcode. */.
aaca0 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 char **azVar;
aacb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
aacc0 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a e of variables *
aacd0 2f 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 /. u32 magic;
aace0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
aacf0 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 agic number for
aad00 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 sanity checking
aad10 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 */. int nMem;
aad20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
aad30 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 Number of memory
aad40 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 locations curre
aad50 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a ntly allocated *
aad60 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 /. Mem *aMem;
aad70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
aad80 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 he memory locati
aad90 6f 6e 73 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 ons */. u32 cac
aada0 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 heCtr;
aadb0 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72 /* VdbeCursor r
aadc0 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74 ow cache generat
aadd0 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 ion counter */.
aade0 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 int pc;
aadf0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
aae00 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 program counter
aae10 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
aae20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
aae30 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 Value to return
aae40 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d */. char *zErrM
aae50 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 sg; /*
aae60 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 Error message wr
aae70 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 itten here */.
aae80 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 u8 explain;
aae90 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
aaea0 69 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 if EXPLAIN prese
aaeb0 6e 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e nt on SQL comman
aaec0 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 d */. u8 change
aaed0 43 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f CntOn; /
aaee0 2a 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 * True to update
aaef0 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
aaf00 74 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 ter */. u8 expi
aaf10 72 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 red;
aaf20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
aaf30 56 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 VM needs to be r
aaf40 65 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 ecompiled */. u
aaf50 38 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 8 minWriteFileFo
aaf60 72 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 rmat; /* Minimu
aaf70 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f m file format fo
aaf80 72 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 r writable datab
aaf90 61 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 ase files */. u
aafa0 38 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 8 inVtabMethod;
aafb0 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f /* See co
aafc0 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a mments above */.
aafd0 20 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 u8 usesStmtJou
aafe0 72 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 rnal; /* Tru
aaff0 65 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 e if uses a stat
ab000 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f ement journal */
ab010 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 . u8 readOnly;
ab020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
ab030 75 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 ue for read-only
ab040 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 statements */.
ab050 20 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b u8 isPrepareV2;
ab060 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
ab070 20 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 if prepared wit
ab080 68 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a h prepare_v2() *
ab090 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b /. int nChange;
ab0a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ab0b0 75 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e umber of db chan
ab0c0 67 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c ges made since l
ab0d0 61 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 ast reset */. i
ab0e0 6e 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 nt btreeMask;
ab0f0 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 /* Bitmas
ab100 6b 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65 k of db->aDb[] e
ab110 6e 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65 ntries reference
ab120 64 20 2a 2f 0a 20 20 69 36 34 20 73 74 61 72 74 d */. i64 start
ab130 54 69 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f Time; /
ab140 2a 20 54 69 6d 65 20 77 68 65 6e 20 71 75 65 72 * Time when quer
ab150 79 20 73 74 61 72 74 65 64 20 2d 20 75 73 65 64 y started - used
ab160 20 66 6f 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a for profiling *
ab170 2f 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 /. BtreeMutexAr
ab180 72 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 ray aMutex; /* A
ab190 6e 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 n array of Btree
ab1a0 20 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e used here and n
ab1b0 65 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a eeding locks */.
ab1c0 20 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 int aCounter[2
ab1d0 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 ]; /* Cou
ab1e0 6e 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 nters used by sq
ab1f0 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 lite3_stmt_statu
ab200 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a s() */. char *z
ab210 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Sql;
ab220 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
ab230 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 SQL statement th
ab240 61 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 69 at generated thi
ab250 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 s */. void *pFr
ab260 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ee; /
ab270 2a 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e * Free this when
ab280 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64 deleting the vd
ab290 62 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 be */. int iSta
ab2a0 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 tement;
ab2b0 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d /* Statement num
ab2c0 62 65 72 20 28 6f 72 20 30 20 69 66 20 68 61 73 ber (or 0 if has
ab2d0 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d 74 not opened stmt
ab2e0 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ) */.#ifdef SQLI
ab2f0 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20 TE_DEBUG. FILE
ab300 2a 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 *trace;
ab310 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 20 65 /* Write an e
ab320 78 65 63 75 74 69 6f 6e 20 74 72 61 63 65 20 68 xecution trace h
ab330 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c ere, if not NULL
ab340 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 64 62 */.#endif. Vdb
ab350 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 eFrame *pFrame;
ab360 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 66 /* Parent f
ab370 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 rame */. int nF
ab380 72 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 rame;
ab390 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
ab3a0 72 61 6d 65 73 20 69 6e 20 70 46 72 61 6d 65 20 rames in pFrame
ab3b0 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a list */.};../*.*
ab3c0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
ab3d0 61 72 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 are allowed valu
ab3e0 65 73 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 es for Vdbe.magi
ab3f0 63 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 c.*/.#define VDB
ab400 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20 E_MAGIC_INIT
ab410 20 30 78 32 36 62 63 65 61 61 35 20 20 20 20 2f 0x26bceaa5 /
ab420 2a 20 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42 * Building a VDB
ab430 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 E program */.#de
ab440 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f fine VDBE_MAGIC_
ab450 52 55 4e 20 20 20 20 20 20 30 78 62 64 66 32 30 RUN 0xbdf20
ab460 64 61 33 20 20 20 20 2f 2a 20 56 44 42 45 20 69 da3 /* VDBE i
ab470 73 20 72 65 61 64 79 20 74 6f 20 65 78 65 63 75 s ready to execu
ab480 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 te */.#define VD
ab490 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 BE_MAGIC_HALT
ab4a0 20 20 30 78 35 31 39 63 32 39 37 33 20 20 20 20 0x519c2973
ab4b0 2f 2a 20 56 44 42 45 20 68 61 73 20 63 6f 6d 70 /* VDBE has comp
ab4c0 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f 6e 20 leted execution
ab4d0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f */.#define VDBE_
ab4e0 4d 41 47 49 43 5f 44 45 41 44 20 20 20 20 20 30 MAGIC_DEAD 0
ab4f0 78 62 36 30 36 63 33 63 38 20 20 20 20 2f 2a 20 xb606c3c8 /*
ab500 54 68 65 20 56 44 42 45 20 68 61 73 20 62 65 65 The VDBE has bee
ab510 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f n deallocated */
ab520 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e ../*.** Function
ab530 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 prototypes.*/.S
ab540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
ab550 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 id sqlite3VdbeFr
ab560 65 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 2c eeCursor(Vdbe *,
ab570 20 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 VdbeCursor*);.v
ab580 6f 69 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f oid sqliteVdbePo
ab590 70 53 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 pStack(Vdbe*,int
ab5a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ab5b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
ab5c0 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 eCursorMoveto(Vd
ab5d0 62 65 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 beCursor*);.#if
ab5e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
ab5f0 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 EBUG) || defined
ab600 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 (VDBE_PROFILE).S
ab610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
ab620 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 id sqlite3VdbePr
ab630 69 6e 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 intOp(FILE*, int
ab640 2c 20 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 , Op*);.#endif.S
ab650 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 QLITE_PRIVATE u3
ab660 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 2 sqlite3VdbeSer
ab670 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b ialTypeLen(u32);
ab680 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ab690 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 u32 sqlite3VdbeS
ab6a0 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 erialType(Mem*,
ab6b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
ab6c0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
ab6d0 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e VdbeSerialPut(un
ab6e0 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e signed char*, in
ab6f0 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 t, Mem*, int);.S
ab700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 QLITE_PRIVATE u3
ab710 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 2 sqlite3VdbeSer
ab720 69 61 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 ialGet(const uns
ab730 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 igned char*, u32
ab740 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f , Mem*);.SQLITE_
ab750 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
ab760 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 ite3VdbeDeleteAu
ab770 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c xData(VdbeFunc*,
ab780 20 69 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 int);..int sqli
ab790 74 65 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 te2BtreeKeyCompa
ab7a0 72 65 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 re(BtCursor *, c
ab7b0 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 onst void *, int
ab7c0 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 , int, int *);.S
ab7d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ab7e0 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 t sqlite3VdbeIdx
ab7f0 4b 65 79 43 6f 6d 70 61 72 65 28 56 64 62 65 43 KeyCompare(VdbeC
ab800 75 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 ursor*,UnpackedR
ab810 65 63 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 ecord*,int*);.SQ
ab820 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ab830 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 sqlite3VdbeIdxR
ab840 6f 77 69 64 28 73 71 6c 69 74 65 33 2a 2c 20 42 owid(sqlite3*, B
ab850 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a tCursor *, i64 *
ab860 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ab870 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d E int sqlite3Mem
ab880 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 Compare(const Me
ab890 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 m*, const Mem*,
ab8a0 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b const CollSeq*);
ab8b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ab8c0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 int sqlite3VdbeE
ab8d0 78 65 63 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 xec(Vdbe*);.SQLI
ab8e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ab8f0 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 56 qlite3VdbeList(V
ab900 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
ab910 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ab920 33 56 64 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3VdbeHalt(Vdbe*)
ab930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ab940 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
ab950 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d ChangeEncoding(M
ab960 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 em *, int);.SQLI
ab970 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
ab980 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f qlite3VdbeMemToo
ab990 42 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 Big(Mem*);.SQLIT
ab9a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ab9b0 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 lite3VdbeMemCopy
ab9c0 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d (Mem*, const Mem
ab9d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
ab9e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
ab9f0 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 dbeMemShallowCop
aba00 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 y(Mem*, const Me
aba10 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 m*, int);.SQLITE
aba20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
aba30 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
aba40 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 (Mem*, Mem*);.SQ
aba50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
aba60 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
aba70 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a ulTerminate(Mem*
aba80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
aba90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
abaa0 65 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c eMemSetStr(Mem*,
abab0 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e const char*, in
abac0 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 t, u8, void(*)(v
abad0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 oid*));.SQLITE_P
abae0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
abaf0 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 te3VdbeMemSetInt
abb00 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 64(Mem*, i64);.S
abb10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
abb20 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
abb30 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c mSetDouble(Mem*,
abb40 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 double);.SQLITE
abb50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
abb60 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
abb70 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 ull(Mem*);.SQLIT
abb80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
abb90 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
abba0 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e ZeroBlob(Mem*,in
abbb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
abbc0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
abbd0 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 dbeMemSetRowSet(
abbe0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
abbf0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
abc00 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 3VdbeMemMakeWrit
abc10 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c eable(Mem*);.SQL
abc20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
abc30 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
abc40 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e ringify(Mem*, in
abc50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
abc60 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 TE i64 sqlite3Vd
abc70 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 beIntValue(Mem*)
abc80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
abc90 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
abca0 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65 MemIntegerify(Me
abcb0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
abcc0 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 ATE double sqlit
abcd0 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
abce0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
abcf0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
abd00 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 e3VdbeIntegerAff
abd10 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c inity(Mem*);.SQL
abd20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
abd30 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
abd40 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c alify(Mem*);.SQL
abd50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
abd60 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 sqlite3VdbeMemNu
abd70 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 merify(Mem*);.SQ
abd80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
abd90 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
abda0 72 6f 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f romBtree(BtCurso
abdb0 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d r*,int,int,int,M
abdc0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 em*);.SQLITE_PRI
abdd0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
abde0 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
abdf0 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f Mem *p);.SQLITE_
abe00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
abe10 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
abe20 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a seExternal(Mem *
abe30 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 p);.SQLITE_PRIVA
abe40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
abe50 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 beMemFinalize(Me
abe60 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 m*, FuncDef*);.S
abe70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
abe80 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
abe90 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3OpcodeName(int)
abea0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
abeb0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
abec0 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 OpcodeHasPropert
abed0 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c y(int, int);.SQL
abee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
abef0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
abf00 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e ow(Mem *pMem, in
abf10 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 t n, int preserv
abf20 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
abf30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
abf40 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 beCloseStatement
abf50 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 (Vdbe *, int);.S
abf60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
abf70 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 id sqlite3VdbeFr
abf80 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 72 ameDelete(VdbeFr
abf90 61 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ame*);.SQLITE_PR
abfa0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
abfb0 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 3VdbeFrameRestor
abfc0 65 28 56 64 62 65 46 72 61 6d 65 20 2a 29 3b 0a e(VdbeFrame *);.
abfd0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
abfe0 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 ABLE_MEMORY_MANA
abff0 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 GEMENT.SQLITE_PR
ac000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ac010 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 66 3VdbeReleaseBuff
ac020 65 72 73 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 ers(Vdbe *p);.#e
ac030 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
ac040 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
ac050 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 _CACHE.SQLITE_PR
ac060 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ac070 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 e3VdbeMutexArray
ac080 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 3b 0a Enter(Vdbe *p);.
ac090 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
ac0a0 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 qlite3VdbeMutexA
ac0b0 72 72 61 79 45 6e 74 65 72 28 70 29 0a 23 65 6e rrayEnter(p).#en
ac0c0 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 dif..SQLITE_PRIV
ac0d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ac0e0 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 dbeMemTranslate(
ac0f0 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65 Mem*, u8);.#ifde
ac100 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 f SQLITE_DEBUG.S
ac110 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
ac120 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ac130 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b PrintSql(Vdbe*);
ac140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac150 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
ac160 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
ac170 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 (Mem *pMem, char
ac180 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a *zBuf);.#endif.
ac190 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ac1a0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
ac1b0 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a mHandleBom(Mem *
ac1c0 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 pMem);..#ifndef
ac1d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
ac1e0 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 BLOB.SQLITE_PRIV
ac1f0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
ac200 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 3VdbeMemExpandBl
ac210 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73 65 ob(Mem *);.#else
ac220 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 . #define sqlit
ac230 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 e3VdbeMemExpandB
ac240 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b lob(x) SQLITE_OK
ac250 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
ac260 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56 44 42 /* !defined(_VDB
ac270 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a EINT_H_) */../**
ac280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
ac290 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a of vdbeInt.h **
ac2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ac2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
ac2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
ac2e0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
ac2f0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 75 74 66 left off in utf
ac300 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
ac310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 ***********/..#i
ac320 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 fndef SQLITE_AMA
ac330 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 LGAMATION./*.**
ac340 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f The following co
ac350 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 69 73 20 nstant value is
ac360 75 73 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 used by the SQLI
ac370 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e 64 TE_BIGENDIAN and
ac380 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c .** SQLITE_LITTL
ac390 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a EENDIAN macros..
ac3a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
ac3b0 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 E const int sqli
ac3c0 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6e 64 te3one = 1;.#end
ac3d0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41 if /* SQLITE_AMA
ac3e0 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a LGAMATION */../*
ac3f0 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70 20 .** This lookup
ac400 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f table is used to
ac410 20 68 65 6c 70 20 64 65 63 6f 64 65 20 74 68 65 help decode the
ac420 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a first byte of.*
ac430 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 55 * a multi-byte U
ac440 54 46 38 20 63 68 61 72 61 63 74 65 72 2e 0a 2a TF8 character..*
ac450 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 /.static const u
ac460 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c nsigned char sql
ac470 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 5d ite3Utf8Trans1[]
ac480 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 = {. 0x00, 0x0
ac490 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 1, 0x02, 0x03, 0
ac4a0 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c x04, 0x05, 0x06,
ac4b0 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 0x07,. 0x08, 0
ac4c0 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c x09, 0x0a, 0x0b,
ac4d0 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30 0x0c, 0x0d, 0x0
ac4e0 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 30 2c e, 0x0f,. 0x10,
ac4f0 20 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78 31 0x11, 0x12, 0x1
ac500 33 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20 30 3, 0x14, 0x15, 0
ac510 78 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 78 31 x16, 0x17,. 0x1
ac520 38 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20 30 8, 0x19, 0x1a, 0
ac530 78 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64 2c x1b, 0x1c, 0x1d,
ac540 20 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 20 30 0x1e, 0x1f,. 0
ac550 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c x00, 0x01, 0x02,
ac560 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 0x03, 0x04, 0x0
ac570 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 5, 0x06, 0x07,.
ac580 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30 0x08, 0x09, 0x0
ac590 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30 a, 0x0b, 0x0c, 0
ac5a0 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c x0d, 0x0e, 0x0f,
ac5b0 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 . 0x00, 0x01, 0
ac5c0 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c x02, 0x03, 0x04,
ac5d0 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 0x05, 0x06, 0x0
ac5e0 37 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 7,. 0x00, 0x01,
ac5f0 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 0x02, 0x03, 0x0
ac600 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 0, 0x01, 0x00, 0
ac610 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69 6e x00,.};...#defin
ac620 65 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 e WRITE_UTF8(zOu
ac630 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20 t, c) {
ac640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac650 20 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 30 30 \. if( c<0x000
ac660 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 80 ){
ac670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac680 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ac690 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 *zOut++ = (u8)(c
ac6a0 26 30 78 46 46 29 3b 20 20 20 20 20 20 20 20 20 &0xFF);
ac6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac6c0 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 \. }
ac6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac6f0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ac700 65 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30 38 else if( c<0x008
ac710 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 00 ){
ac720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac730 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 \. *zOut
ac740 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 38 29 ++ = 0xC0 + (u8)
ac750 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 20 20 ((c>>6)&0x1F);
ac760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ac770 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
ac780 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 80 + (u8)(c & 0x
ac790 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3F);
ac7a0 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 \. }
ac7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac7e0 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 \. else if( c<0
ac7f0 78 31 30 30 30 30 20 29 7b 20 20 20 20 20 20 20 x10000 ){
ac800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac810 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
ac820 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b 20 zOut++ = 0xE0 +
ac830 28 75 38 29 28 28 63 3e 3e 31 32 29 26 30 78 30 (u8)((c>>12)&0x0
ac840 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
ac850 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ac860 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 = 0x80 + (u8)((c
ac870 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 >>6) & 0x3F);
ac880 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ac890 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 *zOut++ = 0x80
ac8a0 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29 + (u8)(c & 0x3F)
ac8b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ac8c0 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 \. }else{
ac8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ac900 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 46 *zOut++ = 0xF
ac910 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 38 29 0 + (u8)((c>>18)
ac920 20 26 20 30 78 30 37 29 3b 20 20 20 20 20 20 20 & 0x07);
ac930 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
ac940 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 t++ = 0x80 + (u8
ac950 29 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33 46 )((c>>12) & 0x3F
ac960 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 5c ); \
ac970 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 . *zOut++ = 0
ac980 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 x80 + (u8)((c>>6
ac990 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 ) & 0x3F);
ac9a0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
ac9b0 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 Out++ = 0x80 + (
ac9c0 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 20 u8)(c & 0x3F);
ac9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac9e0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 \. }
ac9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca10 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 \.}..#
aca20 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 define WRITE_UTF
aca30 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 16LE(zOut, c) {
aca40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca60 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 \. if( c<=0x
aca70 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 FFFF ){
aca80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aca90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acaa0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
acab0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 zOut++ = (u8)(c&
acac0 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 0x00FF);
acad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
acaf0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
acb00 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 u8)((c>>8)&0x00F
acb10 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
acb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acb30 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 \. }else{
acb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acb70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
acb80 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
acb90 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29 ((c>>10)&0x003F)
acba0 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 + (((c-0x10000)
acbb0 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20 >>10)&0x00C0));
acbc0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
acbd0 20 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 (u8)(0x00D8 + (
acbe0 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 ((c-0x10000)>>18
acbf0 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 )&0x03));
acc00 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
acc10 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 ut++ = (u8)(c&0x
acc20 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 00FF);
acc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acc40 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
acc50 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
acc60 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e )(0x00DC + ((c>>
acc70 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 8)&0x03));
acc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acc90 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 \. }
acca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
accb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
accc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
accd0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 \.}..#d
acce0 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31 efine WRITE_UTF1
accf0 36 42 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 6BE(zOut, c) {
acd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 \. if( c<=0xF
acd30 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 FFF ){
acd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acd60 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
acd70 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e Out++ = (u8)((c>
acd80 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 20 20 >8)&0x00FF);
acd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
acdb0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
acdc0 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 8)(c&0x00FF);
acdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acdf0 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 \. }else{
ace00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ace10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ace20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ace30 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ace40 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 *zOut++ = (u8)(0
ace50 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31 x00D8 + (((c-0x1
ace60 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29 0000)>>18)&0x03)
ace70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ace80 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ace90 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 (u8)(((c>>10)&0x
acea0 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 003F) + (((c-0x1
aceb0 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 0000)>>10)&0x00C
acec0 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 0)); \. *zOu
aced0 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 t++ = (u8)(0x00D
acee0 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 C + ((c>>8)&0x03
acef0 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ));
acf00 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
acf10 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
acf20 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 (c&0x00FF);
acf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acf50 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 \. }
acf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acf90 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 \.}..#de
acfa0 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 4c fine READ_UTF16L
acfb0 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20 E(zIn, c){
acfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
acfe0 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49 6e \. c = (*zIn
acff0 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 ++);
ad000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad020 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 \. c
ad030 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 += ((*zIn++)<<8
ad040 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ad050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad070 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 \. if( c>=0x
ad080 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 D800 && c<0xE000
ad090 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ad0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad0b0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ad0c0 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e 2b int c2 = (*zIn+
ad0d0 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 +);
ad0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad100 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 \. c2 += (
ad110 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 (*zIn++)<<8);
ad120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad140 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ad150 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 c = (c2&0x03FF)
ad160 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c + ((c&0x003F)<<
ad170 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 10) + (((c&0x03C
ad180 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 0)+0x0040)<<10);
ad190 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 \. }
ad1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad1d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a \.}..
ad1e0 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 #define READ_UTF
ad1f0 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 16BE(zIn, c){
ad200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad220 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 28 \. c = ((
ad230 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 *zIn++)<<8);
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 20 20 20
ad260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad270 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b c += (*zIn++);
ad280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad2b0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e \. if( c>
ad2c0 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 =0xD800 && c<0xE
ad2d0 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 000 ){
ad2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad300 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28 2a int c2 = ((*
ad310 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 zIn++)<<8);
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 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b \. c2 +
ad350 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 = (*zIn++);
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 20 20 20 20 20 20
ad380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad390 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 c = (c2&0x03
ad3a0 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 FF) + ((c&0x003F
ad3b0 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 )<<10) + (((c&0x
ad3c0 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 03C0)+0x0040)<<1
ad3d0 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 0); \. }
ad3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad420 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 }../*.** Transla
ad430 74 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d te a single UTF-
ad440 38 20 63 68 61 72 61 63 74 65 72 2e 20 20 52 65 8 character. Re
ad450 74 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 65 turn the unicode
ad460 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 value..**.** Du
ad470 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f 6e ring translation
ad480 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 , assume that th
ad490 65 20 62 79 74 65 20 74 68 61 74 20 7a 54 65 72 e byte that zTer
ad4a0 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 61 m points.** is a
ad4b0 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 0x00..**.** Wri
ad4c0 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 te a pointer to
ad4d0 74 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64 20 the next unread
ad4e0 62 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a byte back into *
ad4f0 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f pzNext..**.** No
ad500 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55 tes On Invalid U
ad510 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 TF-8:.**.** *
ad520 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 This routine nev
ad530 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 69 er allows a 7-bi
ad540 74 20 63 68 61 72 61 63 74 65 72 20 28 30 78 30 t character (0x0
ad550 30 20 74 68 72 6f 75 67 68 20 30 78 37 66 29 20 0 through 0x7f)
ad560 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e 63 to.** be enc
ad570 6f 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69 2d oded as a multi-
ad580 62 79 74 65 20 63 68 61 72 61 63 74 65 72 2e 20 byte character.
ad590 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 20 Any multi-byte
ad5a0 63 68 61 72 61 63 74 65 72 20 74 68 61 74 0a 2a character that.*
ad5b0 2a 20 20 20 20 20 61 74 74 65 6d 70 74 73 20 74 * attempts t
ad5c0 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 o encode a value
ad5d0 20 62 65 74 77 65 65 6e 20 30 78 30 30 20 61 6e between 0x00 an
ad5e0 64 20 30 78 37 66 20 69 73 20 72 65 6e 64 65 72 d 0x7f is render
ad5f0 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a ed as 0xfffd..**
ad600 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 .** * This rou
ad610 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 tine never allow
ad620 73 20 61 20 55 54 46 31 36 20 73 75 72 72 6f 67 s a UTF16 surrog
ad630 61 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 ate value to be
ad640 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 20 encoded..**
ad650 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 If a multi-byte
ad660 63 68 61 72 61 63 74 65 72 20 61 74 74 65 6d 70 character attemp
ad670 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 ts to encode a v
ad680 61 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20 alue between.**
ad690 20 20 20 20 30 78 64 38 30 30 20 61 6e 64 20 30 0xd800 and 0
ad6a0 78 65 30 30 30 20 74 68 65 6e 20 69 74 20 69 73 xe000 then it is
ad6b0 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 rendered as 0xf
ad6c0 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 42 ffd..**.** * B
ad6d0 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67 ytes in the rang
ad6e0 65 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75 67 e of 0x80 throug
ad6f0 68 20 30 78 62 66 20 77 68 69 63 68 20 6f 63 63 h 0xbf which occ
ad700 75 72 20 61 73 20 74 68 65 20 66 69 72 73 74 0a ur as the first.
ad710 2a 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20 61 ** byte of a
ad720 20 63 68 61 72 61 63 74 65 72 20 61 72 65 20 69 character are i
ad730 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 73 69 nterpreted as si
ad740 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63 ngle-byte charac
ad750 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 20 ters.** and
ad760 72 65 6e 64 65 72 65 64 20 61 73 20 74 68 65 6d rendered as them
ad770 73 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f 75 selves even thou
ad780 67 68 20 74 68 65 79 20 61 72 65 20 74 65 63 68 gh they are tech
ad790 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 69 nically.** i
ad7a0 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 65 72 nvalid character
ad7b0 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 s..**.** * Thi
ad7c0 73 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70 74 s routine accept
ad7d0 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75 s an infinite nu
ad7e0 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e mber of differen
ad7f0 74 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 73 t UTF8 encodings
ad800 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 63 .** for unic
ad810 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38 30 20 ode values 0x80
ad820 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49 74 and greater. It
ad830 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f do not change o
ad840 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 20 ver-length.**
ad850 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 30 encodings to 0
ad860 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73 79 xfffd as some sy
ad870 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 2e stems recommend.
ad880 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 .*/.#define READ
ad890 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d _UTF8(zIn, zTerm
ad8a0 2c 20 63 29 20 20 20 20 20 20 20 20 20 20 20 20 , c)
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad8c0 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b . c = *(zIn++);
ad8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ad900 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 20 20 if( c>=0xc0 ){
ad910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad930 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 \. c
ad940 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 = sqlite3Utf8Tr
ad950 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20 20 ans1[c-0xc0];
ad960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad970 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c \. whil
ad980 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26 26 e( zIn!=zTerm &&
ad990 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d (*zIn & 0xc0)==
ad9a0 30 78 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 0x80 ){
ad9b0 20 20 20 5c 0a 20 20 20 20 20 20 63 20 3d 20 28 \. c = (
ad9c0 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20 c<<6) + (0x3f &
ad9d0 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20 20 *(zIn++));
ad9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9f0 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20 20 \. }
ada00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ada30 20 20 20 69 66 28 20 63 3c 30 78 38 30 20 20 20 if( c<0x80
ada40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ada60 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ada70 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 || (c&0xFFFF
ada80 46 38 30 30 29 3d 3d 30 78 44 38 30 30 20 20 20 F800)==0xD800
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 5c 0a 20 20 20 20 20 20 20 \.
adab0 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 || (c&0xFFFFFFF
adac0 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 E)==0xFFFE ){ c
adad0 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 20 20 = 0xFFFD; }
adae0 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54 45 \. }.SQLITE
adaf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
adb00 69 74 65 33 55 74 66 38 52 65 61 64 28 0a 20 20 ite3Utf8Read(.
adb10 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
adb20 68 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 20 har *zIn,
adb30 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 /* First byte of
adb40 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
adb50 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 */. const unsi
adb60 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e 65 gned char **pzNe
adb70 78 74 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66 xt /* Write f
adb80 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 55 irst byte past U
adb90 54 46 2d 38 20 63 68 61 72 20 68 65 72 65 20 2a TF-8 char here *
adba0 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 0a 20 /.){. int c;..
adbb0 20 2f 2a 20 53 61 6d 65 20 61 73 20 52 45 41 44 /* Same as READ
adbc0 5f 55 54 46 38 28 29 20 61 62 6f 76 65 20 62 75 _UTF8() above bu
adbd0 74 20 77 69 74 68 6f 75 74 20 74 68 65 20 7a 54 t without the zT
adbe0 65 72 6d 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 erm parameter..
adbf0 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 6f 75 ** For this rou
adc00 74 69 6e 65 2c 20 77 65 20 61 73 73 75 6d 65 20 tine, we assume
adc10 74 68 65 20 55 54 46 38 20 73 74 72 69 6e 67 20 the UTF8 string
adc20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 is always zero-t
adc30 65 72 6d 69 6e 61 74 65 64 2e 0a 20 20 2a 2f 0a erminated.. */.
adc40 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 0a c = *(zIn++);.
adc50 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b if( c>=0xc0 ){
adc60 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 . c = sqlite3
adc70 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 Utf8Trans1[c-0xc
adc80 30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 0];. while( (
adc90 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 *zIn & 0xc0)==0x
adca0 38 30 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 80 ){. c =
adcb0 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 (c<<6) + (0x3f &
adcc0 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 20 20 *(zIn++));.
adcd0 7d 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 }. if( c<0x80
adce0 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 . || (c&0
adcf0 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 xFFFFF800)==0xD8
add00 30 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 00. || (c
add10 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 &0xFFFFFFFE)==0x
add20 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 FFFE ){ c = 0xF
add30 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a FFD; }. }. *pz
add40 4e 65 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 72 65 Next = zIn;. re
add50 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a turn c;.}...../*
add60 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53 .** If the TRANS
add70 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f LATE_TRACE macro
add80 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 is defined, the
add90 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d value of each M
adda0 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64 em is.** printed
addb0 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68 on stderr on th
addc0 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f e way into and o
addd0 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 ut of sqlite3Vdb
adde0 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e eMemTranslate().
addf0 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20 .*/ ./* #define
ade00 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 TRANSLATE_TRACE
ade10 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 1 */..#ifndef SQ
ade20 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
ade30 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
ade40 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68 ne transforms th
ade50 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20 e internal text
ade60 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 encoding used by
ade70 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69 pMem to.** desi
ade80 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e redEnc. It is an
ade90 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74 error if the st
adea0 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20 ring is already
adeb0 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a of the desired.*
adec0 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69 * encoding, or i
aded0 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74 f *pMem does not
adee0 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e contain a strin
adef0 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 g value..*/.SQLI
adf00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
adf10 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 qlite3VdbeMemTra
adf20 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d nslate(Mem *pMem
adf30 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29 , u8 desiredEnc)
adf40 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 {. int len;
adf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf60 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 /* Maximum lengt
adf70 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69 h of output stri
adf80 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 ng in bytes */.
adf90 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
adfa0 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 zOut;
adfb0 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 /* Output
adfc0 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73 buffer */. uns
adfd0 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b igned char *zIn;
adfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adff0 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72 /* Input iter
ae000 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e ator */. unsign
ae010 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 ed char *zTerm;
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae030 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20 /* End of input
ae040 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
ae050 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 ar *z;
ae060 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 /* Ou
ae070 74 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f tput iterator */
ae080 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
ae090 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d c;.. assert( pM
ae0a0 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
ae0b0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
ae0c0 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
ae0d0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
ae0e0 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 em->flags&MEM_St
ae0f0 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
ae100 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65 Mem->enc!=desire
ae110 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74 dEnc );. assert
ae120 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29 ( pMem->enc!=0 )
ae130 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d ;. assert( pMem
ae140 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64 ->n>=0 );..#if d
ae150 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45 efined(TRANSLATE
ae160 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e _TRACE) && defin
ae170 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
ae180 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 . {. char zB
ae190 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c uf[100];. sql
ae1a0 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 ite3VdbeMemPrett
ae1b0 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75 yPrint(pMem, zBu
ae1c0 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 f);. fprintf(
ae1d0 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20 stderr, "INPUT:
ae1e0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 %s\n", zBuf);.
ae1f0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
ae200 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69 If the translati
ae210 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 on is between UT
ae220 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20 F-16 little and
ae230 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e big endian, then
ae240 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 . ** all that
ae250 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74 is required is t
ae260 6f 20 73 77 61 70 20 74 68 65 20 62 79 74 65 20 o swap the byte
ae270 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 65 order. This case
ae280 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a is handled. **
ae290 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f differently fro
ae2a0 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20 m the others..
ae2b0 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 */. if( pMem->e
ae2c0 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc!=SQLITE_UTF8
ae2d0 26 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53 && desiredEnc!=S
ae2e0 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 QLITE_UTF8 ){.
ae2f0 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69 u8 temp;. i
ae300 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 nt rc;. rc =
ae310 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 sqlite3VdbeMemMa
ae320 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d keWriteable(pMem
ae330 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
ae340 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
ae350 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
ae360 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 LITE_NOMEM );.
ae370 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ae380 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
ae390 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d zIn = (u8*)pM
ae3a0 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d em->z;. zTerm
ae3b0 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 = &zIn[pMem->n&
ae3c0 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ~1];. while(
ae3d0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 zIn<zTerm ){.
ae3e0 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a temp = *zIn;.
ae3f0 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a *zIn = *(z
ae400 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e In+1);. zIn
ae410 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b ++;. *zIn++
ae420 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 = temp;. }.
ae430 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 pMem->enc = d
ae440 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67 esiredEnc;. g
ae450 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75 oto translate_ou
ae460 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 t;. }.. /* Set
ae470 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69 len to the maxi
ae480 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 mum number of by
ae490 74 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 tes required in
ae4a0 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 the output buffe
ae4b0 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69 r. */. if( desi
ae4c0 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 redEnc==SQLITE_U
ae4d0 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 TF8 ){. /* Wh
ae4e0 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 en converting fr
ae4f0 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d om UTF-16, the m
ae500 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65 aximum growth re
ae510 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a sults from. *
ae520 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20 * translating a
ae530 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 2-byte character
ae540 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46 to a 4-byte UTF
ae550 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 -8 character..
ae560 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79 ** A single by
ae570 74 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66 te is required f
ae580 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74 or the output st
ae590 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d ring. ** nul-
ae5a0 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 terminator..
ae5b0 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26 */. pMem->n &
ae5c0 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 = ~1;. len =
ae5d0 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b pMem->n * 2 + 1;
ae5e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
ae5f0 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 When converting
ae600 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 from UTF-8 to U
ae610 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75 TF-16 the maximu
ae620 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73 m growth is caus
ae630 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61 ed. ** when a
ae640 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68 1-byte UTF-8 ch
ae650 61 72 61 63 74 65 72 20 69 73 20 74 72 61 6e 73 aracter is trans
ae660 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62 lated into a 2-b
ae670 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a yte UTF-16. *
ae680 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f * character. Two
ae690 20 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69 bytes are requi
ae6a0 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 red in the outpu
ae6b0 74 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65 t buffer for the
ae6c0 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d . ** nul-term
ae6d0 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 inator.. */.
ae6e0 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e len = pMem->n
ae6f0 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20 * 2 + 2;. }..
ae700 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70 /* Set zIn to p
ae710 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 72 oint at the star
ae720 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 t of the input b
ae730 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20 uffer and zTerm
ae740 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20 to point 1. **
ae750 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e byte past the en
ae760 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72 d.. **. ** Var
ae770 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65 iable zOut is se
ae780 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 t to point at th
ae790 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c e output buffer,
ae7a0 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a space obtained.
ae7b0 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 ** from sqlite
ae7c0 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f 3_malloc().. */
ae7d0 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d . zIn = (u8*)pM
ae7e0 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d em->z;. zTerm =
ae7f0 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a &zIn[pMem->n];.
ae800 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 zOut = sqlite3
ae810 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d DbMallocRaw(pMem
ae820 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66 ->db, len);. if
ae830 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72 ( !zOut ){. r
ae840 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
ae850 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f EM;. }. z = zO
ae860 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d ut;.. if( pMem-
ae870 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 >enc==SQLITE_UTF
ae880 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73 8 ){. if( des
ae890 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
ae8a0 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 UTF16LE ){.
ae8b0 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 /* UTF-8 -> UTF
ae8c0 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 -16 Little-endia
ae8d0 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 n */. while
ae8e0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
ae8f0 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 /* c = sq
ae900 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 lite3Utf8Read(zI
ae910 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 n, zTerm, (const
ae920 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a u8**)&zIn); */.
ae930 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 READ_UTF
ae940 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 8(zIn, zTerm, c)
ae950 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f ;. WRITE_
ae960 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 UTF16LE(z, c);.
ae970 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
ae980 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
ae990 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 desiredEnc==SQLI
ae9a0 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 TE_UTF16BE );.
ae9b0 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 /* UTF-8 ->
ae9c0 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 UTF-16 Big-endia
ae9d0 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 n */. while
ae9e0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
ae9f0 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71 /* c = sq
aea00 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 lite3Utf8Read(zI
aea10 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74 n, zTerm, (const
aea20 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a u8**)&zIn); */.
aea30 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 READ_UTF
aea40 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 8(zIn, zTerm, c)
aea50 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f ;. WRITE_
aea60 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 UTF16BE(z, c);.
aea70 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
aea80 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29 pMem->n = (int)
aea90 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20 (z - zOut);.
aeaa0 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 *z++ = 0;. }els
aeab0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 e{. assert( d
aeac0 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 esiredEnc==SQLIT
aead0 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 E_UTF8 );. if
aeae0 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c ( pMem->enc==SQL
aeaf0 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 ITE_UTF16LE ){.
aeb00 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c /* UTF-16 L
aeb10 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 ittle-endian ->
aeb20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 UTF-8 */. w
aeb30 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 hile( zIn<zTerm
aeb40 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f ){. READ_
aeb50 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b UTF16LE(zIn, c);
aeb60 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f . WRITE_
aeb70 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 UTF8(z, c);.
aeb80 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
aeb90 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 /* UTF-16 B
aeba0 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 ig-endian -> UTF
aebb0 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c -8 */. whil
aebc0 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a e( zIn<zTerm ){.
aebd0 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 READ_UTF
aebe0 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 16BE(zIn, c); .
aebf0 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 WRITE_UTF
aec00 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 8(z, c);. }
aec10 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d . }. pMem-
aec20 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a >n = (int)(z - z
aec30 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d Out);. }. *z =
aec40 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 0;. assert( (p
aec50 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45 Mem->n+(desiredE
aec60 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f nc==SQLITE_UTF8?
aec70 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 1:2))<=len );..
aec80 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
aec90 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
aeca0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pMem->flags &= ~
aecb0 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
aecc0 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a Dyn|MEM_Ephem);.
aecd0 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 pMem->enc = de
aece0 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d siredEnc;. pMem
aecf0 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f ->flags |= (MEM_
aed00 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20 Term|MEM_Dyn);.
aed10 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 pMem->z = (char
aed20 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e *)zOut;. pMem->
aed30 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e zMalloc = pMem->
aed40 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75 z;..translate_ou
aed50 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 t:.#if defined(T
aed60 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 RANSLATE_TRACE)
aed70 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 && defined(SQLIT
aed80 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 E_DEBUG). {.
aed90 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b char zBuf[100];
aeda0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
aedb0 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 MemPrettyPrint(p
aedc0 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 Mem, zBuf);.
aedd0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
aede0 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20 "OUTPUT: %s\n",
aedf0 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 zBuf);. }.#endi
aee00 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 f. return SQLIT
aee10 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
aee20 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
aee30 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72 ks for a byte-or
aee40 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20 der mark at the
aee50 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
aee60 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 .** UTF-16 stri
aee70 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d ng stored in *pM
aee80 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72 em. If one is pr
aee90 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d esent, it is rem
aeea0 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 oved and.** the
aeeb0 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 encoding of the
aeec0 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68 Mem adjusted. Th
aeed0 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 is routine does
aeee0 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79 not do any.** by
aeef0 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20 te-swapping, it
aef00 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e just sets Mem.en
aef10 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e c appropriately.
aef20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 .**.** The alloc
aef30 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64 ation (static, d
aef40 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64 ynamic etc.) and
aef50 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 encoding of the
aef60 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63 Mem may be.** c
aef70 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 66 hanged by this f
aef80 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 unction..*/.SQLI
aef90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
aefa0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e qlite3VdbeMemHan
aefb0 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d dleBom(Mem *pMem
aefc0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
aefd0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f LITE_OK;. u8 bo
aefe0 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 m = 0;.. assert
aeff0 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a ( pMem->n>=0 );.
af000 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 20 if( pMem->n>1
af010 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20 2a ){. u8 b1 = *
af020 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 (u8 *)pMem->z;.
af030 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28 75 u8 b2 = *(((u
af040 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31 8 *)pMem->z) + 1
af050 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 );. if( b1==0
af060 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46 20 xFE && b2==0xFF
af070 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 ){. bom = S
af080 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 QLITE_UTF16BE;.
af090 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31 3d }. if( b1=
af0a0 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46 =0xFF && b2==0xF
af0b0 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d E ){. bom =
af0c0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b SQLITE_UTF16LE;
af0d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 . }. }. .
af0e0 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72 if( bom ){. r
af0f0 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
af100 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
af110 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 pMem);. if( r
af120 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
af130 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d pMem->n -=
af140 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76 2;. memmov
af150 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d e(pMem->z, &pMem
af160 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 ->z[2], pMem->n)
af170 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b ;. pMem->z[
af180 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b pMem->n] = '\0';
af190 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 . pMem->z[p
af1a0 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27 Mem->n+1] = '\0'
af1b0 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
af1c0 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
af1d0 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 . pMem->enc
af1e0 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 = bom;. }.
af1f0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
af200 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
af210 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
af220 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 ./*.** pZ is a U
af230 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e 69 TF-8 encoded uni
af240 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20 code string. If
af250 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 74 68 nByte is less th
af260 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 an zero,.** retu
af270 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
af280 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 unicode charact
af290 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f 20 ers in pZ up to
af2a0 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 (but not includi
af2b0 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 ng).** the first
af2c0 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 20 6e 0x00 byte. If n
af2d0 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 73 Byte is not less
af2e0 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75 than zero, retu
af2f0 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 rn the.** number
af300 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 of unicode char
af310 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 66 69 acters in the fi
af320 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a 20 rst nByte of pZ
af330 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 (or up to .** th
af340 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 77 68 e first 0x00, wh
af350 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 ichever comes fi
af360 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rst)..*/.SQLITE_
af370 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
af380 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63 te3Utf8CharLen(c
af390 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 onst char *zIn,
af3a0 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e int nByte){. in
af3b0 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 t r = 0;. const
af3c0 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 u8 *z = (const
af3d0 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74 u8*)zIn;. const
af3e0 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66 u8 *zTerm;. if
af3f0 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20 ( nByte>=0 ){.
af400 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 zTerm = &z[nBy
af410 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 te];. }else{.
af420 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 zTerm = (const
af430 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20 u8*)(-1);. }.
af440 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72 assert( z<=zTer
af450 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a m );. while( *z
af460 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29 !=0 && z<zTerm )
af470 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 {. SQLITE_SKI
af480 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 72 P_UTF8(z);. r
af490 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ++;. }. return
af4a0 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74 r;.}../* This t
af4b0 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 est function is
af4c0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75 73 not currently us
af4d0 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61 ed by the automa
af4e0 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e 20 ted test-suite.
af4f0 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73 20 .** Hence it is
af500 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
af510 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e 0a n debug builds..
af520 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
af530 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 QLITE_TEST) && d
af540 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
af550 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 BUG)./*.** Trans
af560 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54 late UTF-8 to UT
af570 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 F-8..**.** This
af580 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f has the effect o
af590 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 f making sure th
af5a0 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 at the string is
af5b0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 well-formed.**
af5c0 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64 UTF-8. Miscoded
af5d0 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 characters are
af5e0 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 removed..**.** T
af5f0 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 he translation i
af600 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20 s done in-place
af610 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d 70 (since it is imp
af620 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 0a ossible for the.
af630 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d 38 ** correct UTF-8
af640 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 encoding to be
af650 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d 61 longer than a ma
af660 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67 lformed encoding
af670 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
af680 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
af690 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 Utf8To8(unsigned
af6a0 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 char *zIn){. u
af6b0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f nsigned char *zO
af6c0 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 ut = zIn;. unsi
af6d0 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72 gned char *zStar
af6e0 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 63 t = zIn;. u32 c
af6f0 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b ;.. while( zIn[
af700 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 0] ){. c = sq
af710 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49 lite3Utf8Read(zI
af720 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 n, (const u8**)&
af730 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 21 zIn);. if( c!
af740 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20 20 =0xfffd ){.
af750 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74 WRITE_UTF8(zOut
af760 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a , c);. }. }.
af770 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 72 *zOut = 0;. r
af780 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74 eturn (int)(zOut
af790 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 65 - zStart);.}.#e
af7a0 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
af7b0 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
af7c0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
af7d0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e UTF-16 string in
af7e0 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f the native enco
af7f0 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d ding into a UTF-
af800 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 8 string..** Mem
af810 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ory to hold the
af820 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20 UTF-8 string is
af830 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
af840 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64 lite3_malloc and
af850 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65 must.** be free
af860 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 d by the calling
af870 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
af880 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
af890 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e d if there is an
af8a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
af8b0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
af8c0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
af8d0 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74 e3Utf16to8(sqlit
af8e0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f e3 *db, const vo
af8f0 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65 id *z, int nByte
af900 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 ){. Mem m;. me
af910 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 mset(&m, 0, size
af920 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d of(m));. m.db =
af930 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 db;. sqlite3Vd
af940 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20 beMemSetStr(&m,
af950 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45 z, nByte, SQLITE
af960 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 _UTF16NATIVE, SQ
af970 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
af980 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
af990 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51 eEncoding(&m, SQ
af9a0 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 LITE_UTF8);. if
af9b0 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
af9c0 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ed ){. sqlite
af9d0 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
af9e0 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30 &m);. m.z = 0
af9f0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
afa00 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 (m.flags & MEM_T
afa10 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d erm)!=0 || db->m
afa20 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
afa30 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67 assert( (m.flag
afa40 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20 s & MEM_Str)!=0
afa50 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
afa60 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 led );. return
afa70 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 (m.flags & MEM_D
afa80 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73 yn)!=0 ? m.z : s
afa90 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
afaa0 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a b, m.z);.}../*.*
afab0 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d * Convert a UTF-
afac0 38 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65 20 8 string to the
afad0 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 UTF-16 encoding
afae0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 specified by par
afaf0 61 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20 41 ameter.** enc. A
afb00 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
afb10 6e 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 65 new string is re
afb20 74 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 20 turned, and the
afb30 76 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 0a value of *pnOut.
afb40 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ** is set to the
afb50 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 72 length of the r
afb60 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 eturned string i
afb70 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 6c n bytes. The cal
afb80 6c 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72 61 l should.** arra
afb90 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 nge to call sqli
afba0 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 74 te3DbFree() on t
afbb0 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e he returned poin
afbc0 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a 2a ter when it is.*
afbd0 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75 * no longer requ
afbe0 69 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 ired..** .** If
afbf0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 a malloc failure
afc00 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 occurs, NULL is
afc10 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 returned and th
afc20 65 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 e db.mallocFaile
afc30 64 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a 2a d.** flag set..*
afc40 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
afc50 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 4c ENABLE_STAT2.SQL
afc60 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
afc70 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f 31 *sqlite3Utf8to1
afc80 36 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 75 6(sqlite3 *db, u
afc90 38 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c 20 8 enc, char *z,
afca0 69 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f 75 int n, int *pnOu
afcb0 74 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d t){. Mem m;. m
afcc0 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a emset(&m, 0, siz
afcd0 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 eof(m));. m.db
afce0 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 = db;. sqlite3V
afcf0 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c dbeMemSetStr(&m,
afd00 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
afd10 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 F8, SQLITE_STATI
afd20 43 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 C);. if( sqlite
afd30 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 3VdbeMemTranslat
afd40 65 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20 20 e(&m, enc) ){.
afd50 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 assert( db->ma
afd60 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
afd70 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
afd80 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d 6d assert( m.z==m
afd90 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a 70 .zMalloc );. *p
afda0 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72 65 nOut = m.n;. re
afdb0 74 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 turn m.z;.}.#end
afdc0 69 66 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 if../*.** pZ is
afdd0 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 a UTF-16 encoded
afde0 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 20 unicode string
afdf0 61 74 20 6c 65 61 73 74 20 6e 43 68 61 72 20 63 at least nChar c
afe00 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 0a haracters long..
afe10 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
afe20 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
afe30 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 61 72 the first nChar
afe40 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 unicode charact
afe50 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e ers.** in pZ. n
afe60 43 68 61 72 20 6d 75 73 74 20 62 65 20 6e 6f 6e Char must be non
afe70 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 -negative..*/.SQ
afe80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
afe90 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74 sqlite3Utf16Byt
afea0 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20 eLen(const void
afeb0 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 29 *zIn, int nChar)
afec0 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 6e 73 {. int c;. uns
afed0 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 igned char const
afee0 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 *z = zIn;. int
afef0 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51 n = 0;. if( SQ
aff00 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
aff10 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 ==SQLITE_UTF16BE
aff20 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e 67 ){. /* Using
aff30 20 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 5f an "if (SQLITE_
aff40 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c UTF16NATIVE==SQL
aff50 49 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 6f ITE_UTF16BE)" co
aff60 6e 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 20 nstruct here.
aff70 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 72 ** and in other
aff80 20 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 66 parts of this f
aff90 69 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 61 ile means that a
affa0 74 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 6c t one branch wil
affb0 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20 l. ** not be
affc0 63 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 72 covered by cover
affd0 61 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 61 age testing on a
affe0 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e 20 ny single host.
afff0 42 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 20 But coverage.
b0000 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 ** will be comp
b0010 6c 65 74 65 20 69 66 20 74 68 65 20 74 65 73 74 lete if the test
b0020 73 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f 74 s are run on bot
b0030 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 h a little-endia
b0040 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 69 n and . ** bi
b0050 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 42 g-endian host. B
b0060 65 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 20 ecause both the
b0070 55 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 20 UTF16NATIVE and
b0080 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a 20 SQLITE_UTF16BE.
b0090 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 65 ** macros are
b00a0 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d constant at com
b00b0 70 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 6f pile time the co
b00c0 6d 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 72 mpiler can deter
b00d0 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 63 mine. ** whic
b00e0 68 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 65 h branch will be
b00f0 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 73 followed. It is
b0100 20 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 6d therefore assum
b0110 65 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 69 ed that no runti
b0120 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c 74 me. ** penalt
b0130 79 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 68 y is paid for th
b0140 69 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e is "if" statemen
b0150 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 t.. */. wh
b0160 69 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a ile( n<nChar ){.
b0170 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 READ_UTF16
b0180 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 BE(z, c);.
b0190 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c n++;. }. }el
b01a0 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e se{. while( n
b01b0 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 <nChar ){.
b01c0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c 20 READ_UTF16LE(z,
b01d0 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 c);. n++;.
b01e0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
b01f0 6e 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 n (int)(z-(unsig
b0200 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a ned char const *
b0210 29 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 )zIn);.}..#if de
b0220 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
b0230 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f T)./*.** This ro
b0240 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
b0250 66 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 from the TCL tes
b0260 74 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e t function "tran
b0270 73 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e slate_selftest".
b0280 0a 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 .** It checks th
b0290 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 at the primitive
b02a0 73 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e s for serializin
b02b0 67 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a g and deserializ
b02c0 69 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 ing.** character
b02d0 73 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 s in each encodi
b02e0 6e 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 ng are inverses
b02f0 6f 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a of each other..*
b0300 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b0310 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 void sqlite3Utf
b0320 53 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a SelfTest(void){.
b0330 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 unsigned int i
b0340 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 , t;. unsigned
b0350 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 char zBuf[20];.
b0360 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
b0370 7a 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e z;. int n;. un
b0380 73 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 signed int c;..
b0390 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 for(i=0; i<0x00
b03a0 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 110000; i++){.
b03b0 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 z = zBuf;.
b03c0 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 WRITE_UTF8(z, i)
b03d0 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 ;. n = (int)(
b03e0 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 z-zBuf);. ass
b03f0 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 ert( n>0 && n<=4
b0400 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 );. z[0] = 0
b0410 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a ;. z = zBuf;.
b0420 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 c = sqlite3U
b0430 74 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 tf8Read(z, (cons
b0440 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 t u8**)&z);.
b0450 74 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 t = i;. if( i
b0460 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 >=0xD800 && i<=0
b0470 78 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 xDFFF ) t = 0xFF
b0480 46 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 FD;. if( (i&0
b0490 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 xFFFFFFFE)==0xFF
b04a0 46 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b FE ) t = 0xFFFD;
b04b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d . assert( c==
b04c0 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
b04d0 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a (z-zBuf)==n );.
b04e0 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
b04f0 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b <0x00110000; i++
b0500 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 ){. if( i>=0x
b0510 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 D800 && i<0xE000
b0520 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
b0530 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 z = zBuf;. W
b0540 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 RITE_UTF16LE(z,
b0550 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 i);. n = (int
b0560 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 )(z-zBuf);. a
b0570 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c ssert( n>0 && n<
b0580 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d =4 );. z[0] =
b0590 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 0;. z = zBuf
b05a0 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 ;. READ_UTF16
b05b0 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73 LE(z, c);. as
b05c0 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 sert( c==i );.
b05d0 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 assert( (z-zBu
b05e0 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 f)==n );. }. f
b05f0 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 or(i=0; i<0x0011
b0600 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 0000; i++){.
b0610 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 if( i>=0xD800 &&
b0620 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74 i<0xE000 ) cont
b0630 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 inue;. z = zB
b0640 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54 uf;. WRITE_UT
b0650 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20 F16BE(z, i);.
b0660 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75 n = (int)(z-zBu
b0670 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 f);. assert(
b0680 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 n>0 && n<=4 );.
b0690 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 z[0] = 0;.
b06a0 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 z = zBuf;. R
b06b0 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 EAD_UTF16BE(z, c
b06c0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 );. assert( c
b06d0 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==i );. asser
b06e0 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 t( (z-zBuf)==n )
b06f0 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f ;. }.}.#endif /
b0700 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f * SQLITE_TEST */
b0710 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
b0720 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
b0730 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b0740 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a End of utf.c **
b0750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b0780 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
b0790 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c Begin file util
b07a0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
b07b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b07c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
b07d0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 ./*.** 2001 Sept
b07e0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 ember 15.**.** T
b07f0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
b0800 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
b0810 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
b0820 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
b0830 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
b0840 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
b0850 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
b0860 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
b0870 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
b0880 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
b0890 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
b08a0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
b08b0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
b08c0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
b08d0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
b08e0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
b08f0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
b0900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b0940 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 *******.** Utili
b0950 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 ty functions use
b0960 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c d throughout sql
b0970 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ite..**.** This
b0980 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 file contains fu
b0990 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f nctions for allo
b09a0 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 cating memory, c
b09b0 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 omparing.** stri
b09c0 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c ngs, and stuff l
b09d0 69 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2f 0a ike that..**.*/.
b09e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 #ifdef SQLITE_HA
b09f0 56 45 5f 49 53 4e 41 4e 0a 23 20 69 6e 63 6c 75 VE_ISNAN.# inclu
b0a00 64 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 65 6e 64 de <math.h>.#end
b0a10 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e if../*.** Routin
b0a20 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70 e needed to supp
b0a30 6f 72 74 20 74 68 65 20 74 65 73 74 63 61 73 65 ort the testcase
b0a40 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 () macro..*/.#if
b0a50 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 def SQLITE_COVER
b0a60 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f AGE_TEST.SQLITE_
b0a70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
b0a80 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 ite3Coverage(int
b0a90 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e x){. static in
b0aa0 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 64 t dummy = 0;. d
b0ab0 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 6e ummy += x;.}.#en
b0ac0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 dif../*.** Retur
b0ad0 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 6c n true if the fl
b0ae0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
b0af0 75 65 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d 62 ue is Not a Numb
b0b00 65 72 20 28 4e 61 4e 29 2e 0a 2a 2a 0a 2a 2a 20 er (NaN)..**.**
b0b10 55 73 65 20 74 68 65 20 6d 61 74 68 20 6c 69 62 Use the math lib
b0b20 72 61 72 79 20 69 73 6e 61 6e 28 29 20 66 75 6e rary isnan() fun
b0b30 63 74 69 6f 6e 20 69 66 20 63 6f 6d 70 69 6c 65 ction if compile
b0b40 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 48 41 d with SQLITE_HA
b0b50 56 45 5f 49 53 4e 41 4e 2e 0a 2a 2a 20 4f 74 68 VE_ISNAN..** Oth
b0b60 65 72 77 69 73 65 2c 20 77 65 20 68 61 76 65 20 erwise, we have
b0b70 6f 75 72 20 6f 77 6e 20 69 6d 70 6c 65 6d 65 6e our own implemen
b0b80 74 61 74 69 6f 6e 20 74 68 61 74 20 77 6f 72 6b tation that work
b0b90 73 20 6f 6e 20 6d 6f 73 74 20 73 79 73 74 65 6d s on most system
b0ba0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
b0bb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b0bc0 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b IsNaN(double x){
b0bd0 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 2f 2a 20 . int rc; /*
b0be0 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e The value return
b0bf0 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 */.#if !defined
b0c00 28 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e (SQLITE_HAVE_ISN
b0c10 41 4e 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 79 AN). /*. ** Sy
b0c20 73 74 65 6d 73 20 74 68 61 74 20 73 75 70 70 6f stems that suppo
b0c30 72 74 20 74 68 65 20 69 73 6e 61 6e 28 29 20 6c rt the isnan() l
b0c40 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 ibrary function
b0c50 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a should probably.
b0c60 20 20 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 ** make use of
b0c70 20 69 74 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 it by compiling
b0c80 20 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 48 with -DSQLITE_H
b0c90 41 56 45 5f 49 53 4e 41 4e 2e 20 20 42 75 74 20 AVE_ISNAN. But
b0ca0 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 66 6f 75 we have. ** fou
b0cb0 6e 64 20 74 68 61 74 20 6d 61 6e 79 20 73 79 73 nd that many sys
b0cc0 74 65 6d 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 tems do not have
b0cd0 20 61 20 77 6f 72 6b 69 6e 67 20 69 73 6e 61 6e a working isnan
b0ce0 28 29 20 66 75 6e 63 74 69 6f 6e 20 73 6f 0a 20 () function so.
b0cf0 20 2a 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 ** this impleme
b0d00 6e 74 61 74 69 6f 6e 20 69 73 20 70 72 6f 76 69 ntation is provi
b0d10 64 65 64 20 61 73 20 61 6e 20 61 6c 74 65 72 6e ded as an altern
b0d20 61 74 69 76 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a ative.. **. **
b0d30 20 54 68 69 73 20 4e 61 4e 20 74 65 73 74 20 73 This NaN test s
b0d40 6f 6d 65 74 69 6d 65 73 20 66 61 69 6c 73 20 69 ometimes fails i
b0d50 66 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47 43 f compiled on GC
b0d60 43 20 77 69 74 68 20 2d 66 66 61 73 74 2d 6d 61 C with -ffast-ma
b0d70 74 68 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68 65 20 th.. ** On the
b0d80 6f 74 68 65 72 20 68 61 6e 64 2c 20 74 68 65 20 other hand, the
b0d90 75 73 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d 61 use of -ffast-ma
b0da0 74 68 20 63 6f 6d 65 73 20 77 69 74 68 20 74 68 th comes with th
b0db0 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a e following. **
b0dc0 20 77 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a 0a 20 warning:. **.
b0dd0 20 2a 2a 20 20 20 20 20 20 54 68 69 73 20 6f 70 ** This op
b0de0 74 69 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d 61 74 tion [-ffast-mat
b0df0 68 5d 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 h] should never
b0e00 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 62 79 20 be turned on by
b0e10 61 6e 79 0a 20 20 2a 2a 20 20 20 20 20 20 2d 4f any. ** -O
b0e20 20 6f 70 74 69 6f 6e 20 73 69 6e 63 65 20 69 74 option since it
b0e30 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 69 can result in i
b0e40 6e 63 6f 72 72 65 63 74 20 6f 75 74 70 75 74 20 ncorrect output
b0e50 66 6f 72 20 70 72 6f 67 72 61 6d 73 0a 20 20 2a for programs. *
b0e60 2a 20 20 20 20 20 20 77 68 69 63 68 20 64 65 70 * which dep
b0e70 65 6e 64 20 6f 6e 20 61 6e 20 65 78 61 63 74 20 end on an exact
b0e80 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
b0e90 66 20 49 45 45 45 20 6f 72 20 49 53 4f 20 0a 20 f IEEE or ISO .
b0ea0 20 2a 2a 20 20 20 20 20 20 72 75 6c 65 73 2f 73 ** rules/s
b0eb0 70 65 63 69 66 69 63 61 74 69 6f 6e 73 20 66 6f pecifications fo
b0ec0 72 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73 r math functions
b0ed0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55 6e 64 65 .. **. ** Unde
b0ee0 72 20 4d 53 56 43 2c 20 74 68 69 73 20 4e 61 4e r MSVC, this NaN
b0ef0 20 74 65 73 74 20 6d 61 79 20 66 61 69 6c 20 69 test may fail i
b0f00 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 f compiled with
b0f10 61 20 66 6c 6f 61 74 69 6e 67 2d 0a 20 20 2a 2a a floating-. **
b0f20 20 70 6f 69 6e 74 20 70 72 65 63 69 73 69 6f 6e point precision
b0f30 20 6d 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e mode other than
b0f40 20 2f 66 70 3a 70 72 65 63 69 73 65 2e 20 20 46 /fp:precise. F
b0f50 72 6f 6d 20 74 68 65 20 4d 53 44 4e 20 0a 20 20 rom the MSDN .
b0f60 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e ** documentation
b0f70 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 :. **. **
b0f80 20 54 68 65 20 63 6f 6d 70 69 6c 65 72 20 5b 77 The compiler [w
b0f90 69 74 68 20 2f 66 70 3a 70 72 65 63 69 73 65 5d ith /fp:precise]
b0fa0 20 77 69 6c 6c 20 70 72 6f 70 65 72 6c 79 20 68 will properly h
b0fb0 61 6e 64 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e andle comparison
b0fc0 73 20 0a 20 20 2a 2a 20 20 20 20 20 20 69 6e 76 s . ** inv
b0fd0 6f 6c 76 69 6e 67 20 4e 61 4e 2e 20 46 6f 72 20 olving NaN. For
b0fe0 65 78 61 6d 70 6c 65 2c 20 78 20 21 3d 20 78 20 example, x != x
b0ff0 65 76 61 6c 75 61 74 65 73 20 74 6f 20 74 72 75 evaluates to tru
b1000 65 20 69 66 20 78 20 69 73 20 4e 61 4e 20 0a 20 e if x is NaN .
b1010 20 2a 2a 20 20 20 20 20 20 2e 2e 2e 0a 20 20 2a ** .... *
b1020 2f 0a 23 69 66 64 65 66 20 5f 5f 46 41 53 54 5f /.#ifdef __FAST_
b1030 4d 41 54 48 5f 5f 0a 23 20 65 72 72 6f 72 20 53 MATH__.# error S
b1040 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 77 QLite will not w
b1050 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 77 69 ork correctly wi
b1060 74 68 20 74 68 65 20 2d 66 66 61 73 74 2d 6d 61 th the -ffast-ma
b1070 74 68 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43 43 th option of GCC
b1080 2e 0a 23 65 6e 64 69 66 0a 20 20 76 6f 6c 61 74 ..#endif. volat
b1090 69 6c 65 20 64 6f 75 62 6c 65 20 79 20 3d 20 78 ile double y = x
b10a0 3b 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75 ;. volatile dou
b10b0 62 6c 65 20 7a 20 3d 20 79 3b 0a 20 20 72 63 20 ble z = y;. rc
b10c0 3d 20 28 79 21 3d 7a 29 3b 0a 23 65 6c 73 65 20 = (y!=z);.#else
b10d0 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 28 53 /* if defined(S
b10e0 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e QLITE_HAVE_ISNAN
b10f0 29 20 2a 2f 0a 20 20 72 63 20 3d 20 69 73 6e 61 ) */. rc = isna
b1100 6e 28 78 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 n(x);.#endif /*
b1110 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 SQLITE_HAVE_ISNA
b1120 4e 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 N */. testcase(
b1130 20 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 rc );. return
b1140 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d rc;.}../*.** Com
b1150 70 75 74 65 20 61 20 73 74 72 69 6e 67 20 6c 65 pute a string le
b1160 6e 67 74 68 20 74 68 61 74 20 69 73 20 6c 69 6d ngth that is lim
b1170 69 74 65 64 20 74 6f 20 77 68 61 74 20 63 61 6e ited to what can
b1180 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a be stored in.**
b1190 20 6c 6f 77 65 72 20 33 30 20 62 69 74 73 20 6f lower 30 bits o
b11a0 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65 f a 32-bit signe
b11b0 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a d integer..**.**
b11c0 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 The value retur
b11d0 6e 65 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 ned will never b
b11e0 65 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 72 e negative. Nor
b11f0 20 77 69 6c 6c 20 69 74 20 65 76 65 72 20 62 65 will it ever be
b1200 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e greater.** than
b1210 20 74 68 65 20 61 63 74 75 61 6c 20 6c 65 6e 67 the actual leng
b1220 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 th of the string
b1230 2e 20 20 46 6f 72 20 76 65 72 79 20 6c 6f 6e 67 . For very long
b1240 20 73 74 72 69 6e 67 73 20 28 67 72 65 61 74 65 strings (greate
b1250 72 0a 2a 2a 20 74 68 61 6e 20 31 47 69 42 29 20 r.** than 1GiB)
b1260 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
b1270 65 64 20 6d 69 67 68 74 20 62 65 20 6c 65 73 73 ed might be less
b1280 20 74 68 61 6e 20 74 68 65 20 74 72 75 65 20 73 than the true s
b1290 74 72 69 6e 67 20 6c 65 6e 67 74 68 2e 0a 2a 2f tring length..*/
b12a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b12b0 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65 int sqlite3Strle
b12c0 6e 33 30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a n30(const char *
b12d0 7a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 z){. const char
b12e0 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 *z2 = z;. if(
b12f0 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b z==0 ) return 0;
b1300 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 29 7b . while( *z2 ){
b1310 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 z2++; }. retur
b1320 6e 20 30 78 33 66 66 66 66 66 66 66 20 26 20 28 n 0x3fffffff & (
b1330 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 7d 0a int)(z2 - z);.}.
b1340 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d ./*.** Set the m
b1350 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 ost recent error
b1360 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 code and error
b1370 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 73 string for the s
b1380 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 qlite.** handle
b1390 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f 72 20 "db". The error
b13a0 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 22 code is set to "
b13b0 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a err_code"..**.**
b13c0 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 If it is not NU
b13d0 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d LL, string zForm
b13e0 61 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 at specifies the
b13f0 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a format of the.*
b1400 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 69 * error string i
b1410 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66 20 74 n the style of t
b1420 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63 74 69 he printf functi
b1430 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ons: The followi
b1440 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61 ng.** format cha
b1450 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f racters are allo
b1460 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 wed:.**.**
b1470 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 %s Insert a
b1480 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 string.**
b1490 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 %z A string
b14a0 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
b14b0 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a freed after use.
b14c0 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 ** %d
b14d0 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 Insert an intege
b14e0 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 r.** %T
b14f0 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e Insert a token
b1500 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 .** %S
b1510 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 Insert the firs
b1520 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 t element of a S
b1530 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f rcList.**.** zFo
b1540 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 74 72 rmat and any str
b1550 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 ing tokens that
b1560 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 73 follow it are as
b1570 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65 sumed to be.** e
b1580 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e ncoded in UTF-8.
b1590 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20 .**.** To clear
b15a0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
b15b0 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 65 error for sqlite
b15c0 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 71 handle "db", sq
b15d0 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68 lite3Error.** sh
b15e0 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 ould be called w
b15f0 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73 65 74 ith err_code set
b1600 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e to SQLITE_OK an
b1610 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a d zFormat set.**
b1620 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c to NULL..*/.SQL
b1630 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b1640 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71 sqlite3Error(sq
b1650 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 lite3 *db, int e
b1660 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63 rr_code, const c
b1670 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e har *zFormat, ..
b1680 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 .){. if( db &&
b1690 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62 (db->pErr || (db
b16a0 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 65 33 ->pErr = sqlite3
b16b0 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 30 ValueNew(db))!=0
b16c0 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72 ) ){. db->err
b16d0 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b Code = err_code;
b16e0 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 74 . if( zFormat
b16f0 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a ){. char *
b1700 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74 z;. va_list
b1710 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 74 ap;. va_st
b1720 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 art(ap, zFormat)
b1730 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 ;. z = sqli
b1740 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 te3VMPrintf(db,
b1750 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 zFormat, ap);.
b1760 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a va_end(ap);.
b1770 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c sqlite3Val
b1780 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 ueSetStr(db->pEr
b1790 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45 r, -1, z, SQLITE
b17a0 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 _UTF8, SQLITE_DY
b17b0 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73 NAMIC);. }els
b17c0 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
b17d0 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e ValueSetStr(db->
b17e0 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 pErr, 0, 0, SQLI
b17f0 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
b1800 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 STATIC);. }.
b1810 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 }.}../*.** Add
b1820 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
b1830 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 to pParse->zErr
b1840 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e Msg and incremen
b1850 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a t pParse->nErr..
b1860 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
b1870 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 72 formatting char
b1880 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77 acters are allow
b1890 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 ed:.**.** %
b18a0 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 s Insert a
b18b0 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25 string.** %
b18c0 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20 z A string
b18d0 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66 that should be f
b18e0 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a reed after use.*
b18f0 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49 * %d I
b1900 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72 nsert an integer
b1910 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20 .** %T
b1920 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a Insert a token.
b1930 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20 ** %S
b1940 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74 Insert the first
b1950 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72 element of a Sr
b1960 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73 cList.**.** This
b1970 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
b1980 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 70 6f be used to repo
b1990 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68 61 rt any error tha
b19a0 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74 0a t occurs whilst.
b19b0 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20 ** compiling an
b19c0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 69 SQL statement (i
b19d0 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 74 .e. within sqlit
b19e0 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20 54 e3_prepare()). T
b19f0 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67 he.** last thing
b1a00 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 the sqlite3_pre
b1a10 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 pare() function
b1a20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68 65 does is copy the
b1a30 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64 error.** stored
b1a40 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
b1a50 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 n into the datab
b1a60 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e 67 ase handle using
b1a70 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e sqlite3Error().
b1a80 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c .** Function sql
b1a90 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f 75 ite3Error() shou
b1aa0 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 69 6e ld be used durin
b1ab0 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65 63 g statement exec
b1ac0 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65 ution.** (sqlite
b1ad0 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e 0a 3_step() etc.)..
b1ae0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b1af0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 E void sqlite3Er
b1b00 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70 50 rorMsg(Parse *pP
b1b10 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 arse, const char
b1b20 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
b1b30 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
b1b40 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
b1b50 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61 Parse->db;. pPa
b1b60 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 73 rse->nErr++;. s
b1b70 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
b1b80 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 pParse->zErrMsg
b1b90 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 );. va_start(ap
b1ba0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50 , zFormat);. pP
b1bb0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 arse->zErrMsg =
b1bc0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
b1bd0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 db, zFormat, ap)
b1be0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a ;. va_end(ap);.
b1bf0 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 pParse->rc = S
b1c00 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a QLITE_ERROR;.}..
b1c10 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 /*.** Clear the
b1c20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
b1c30 20 70 50 61 72 73 65 2c 20 69 66 20 61 6e 79 0a pParse, if any.
b1c40 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b1c50 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 E void sqlite3Er
b1c60 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65 20 2a rorClear(Parse *
b1c70 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 pParse){. sqlit
b1c80 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d e3DbFree(pParse-
b1c90 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 >db, pParse->zEr
b1ca0 72 4d 73 67 29 3b 0a 20 20 70 50 61 72 73 65 2d rMsg);. pParse-
b1cb0 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
b1cc0 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d 20 30 pParse->nErr = 0
b1cd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
b1ce0 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 rt an SQL-style
b1cf0 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e quoted string in
b1d00 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 to a normal stri
b1d10 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a ng by removing.*
b1d20 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72 * the quote char
b1d30 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e acters. The con
b1d40 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 version is done
b1d50 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68 in-place. If th
b1d60 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 e.** input does
b1d70 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 not begin with a
b1d80 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 quote character
b1d90 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
b1da0 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f ine.** is a no-o
b1db0 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 p..**.** The inp
b1dc0 75 74 20 73 74 72 69 6e 67 20 6d 75 73 74 20 62 ut string must b
b1dd0 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 e zero-terminate
b1de0 64 2e 20 20 41 20 6e 65 77 20 7a 65 72 6f 2d 74 d. A new zero-t
b1df0 65 72 6d 69 6e 61 74 6f 72 0a 2a 2a 20 69 73 20 erminator.** is
b1e00 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 65 71 added to the deq
b1e10 75 6f 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a uoted string..**
b1e20 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 .** The return v
b1e30 61 6c 75 65 20 69 73 20 2d 31 20 69 66 20 6e 6f alue is -1 if no
b1e40 20 64 65 71 75 6f 74 69 6e 67 20 6f 63 63 75 72 dequoting occur
b1e50 73 20 6f 72 20 74 68 65 20 6c 65 6e 67 74 68 20 s or the length
b1e60 6f 66 20 74 68 65 0a 2a 2a 20 64 65 71 75 6f 74 of the.** dequot
b1e70 65 64 20 73 74 72 69 6e 67 2c 20 65 78 63 6c 75 ed string, exclu
b1e80 73 69 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f sive of the zero
b1e90 20 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66 20 terminator, if
b1ea0 64 65 71 75 6f 74 69 6e 67 20 64 6f 65 73 0a 2a dequoting does.*
b1eb0 2a 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 32 * occur..**.** 2
b1ec0 30 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 73 002-Feb-14: This
b1ed0 20 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 65 routine is exte
b1ee0 6e 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 4d nded to remove M
b1ef0 53 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a 2a S-Access style.*
b1f00 2a 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d 20 * brackets from
b1f10 61 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 72 around identifer
b1f20 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a s. For example:
b1f30 20 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 6f "[a-b-c]" beco
b1f40 6d 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a mes.** "a-b-c"..
b1f50 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b1f60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 65 71 E int sqlite3Deq
b1f70 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20 uote(char *z){.
b1f80 20 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 20 69 char quote;. i
b1f90 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a nt i, j;. if( z
b1fa0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b ==0 ) return -1;
b1fb0 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b . quote = z[0];
b1fc0 0a 20 20 73 77 69 74 63 68 28 20 71 75 6f 74 65 . switch( quote
b1fd0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c 27 ){. case '\'
b1fe0 27 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 ': break;. c
b1ff0 61 73 65 20 27 22 27 3a 20 20 20 62 72 65 61 6b ase '"': break
b2000 3b 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 20 ;. case '`':
b2010 20 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20 20 break;
b2020 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d /* For M
b2030 79 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 ySQL compatibili
b2040 74 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 ty */. case '
b2050 5b 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27 5d [': quote = ']
b2060 27 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 '; break; /* F
b2070 6f 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72 20 or MS SqlServer
b2080 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f compatibility */
b2090 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 . default:
b20a0 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a return -1;. }.
b20b0 20 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 for(i=1, j=0;
b20c0 41 4c 57 41 59 53 28 7a 5b 69 5d 29 3b 20 69 2b ALWAYS(z[i]); i+
b20d0 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d +){. if( z[i]
b20e0 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 ==quote ){.
b20f0 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f if( z[i+1]==quo
b2100 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b te ){. z[
b2110 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20 j++] = quote;.
b2120 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 i++;.
b2130 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
b2140 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
b2150 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
b2160 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 z[j++] = z[i];.
b2170 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 }. }. z[j]
b2180 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a 3b = 0;. return j;
b2190 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 6e .}../* Convenien
b21a0 74 20 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f 0a t short-hand */.
b21b0 23 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c #define UpperToL
b21c0 6f 77 65 72 20 73 71 6c 69 74 65 33 55 70 70 65 ower sqlite3Uppe
b21d0 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 rToLower../*.**
b21e0 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76 Some systems hav
b21f0 65 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74 e stricmp(). Ot
b2200 68 65 72 73 20 68 61 76 65 20 73 74 72 63 61 73 hers have strcas
b2210 65 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65 ecmp(). Because
b2220 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 .** there is no
b2230 63 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20 consistency, we
b2240 77 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20 will define our
b2250 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 own..*/.SQLITE_P
b2260 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b2270 65 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20 e3StrICmp(const
b2280 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e char *zLeft, con
b2290 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 st char *zRight)
b22a0 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 {. register uns
b22b0 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a igned char *a, *
b22c0 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e b;. a = (unsign
b22d0 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b ed char *)zLeft;
b22e0 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 . b = (unsigned
b22f0 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a char *)zRight;.
b2300 20 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26 while( *a!=0 &
b2310 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a & UpperToLower[*
b2320 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 a]==UpperToLower
b2330 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b [*b]){ a++; b++;
b2340 20 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65 }. return Uppe
b2350 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 rToLower[*a] - U
b2360 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b pperToLower[*b];
b2370 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
b2380 74 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 t sqlite3_strnic
b2390 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a mp(const char *z
b23a0 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 Left, const char
b23b0 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e 29 *zRight, int N)
b23c0 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73 {. register uns
b23d0 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a igned char *a, *
b23e0 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e b;. a = (unsign
b23f0 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b ed char *)zLeft;
b2400 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64 . b = (unsigned
b2410 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a char *)zRight;.
b2420 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 while( N-- > 0
b2430 20 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70 70 && *a!=0 && Upp
b2440 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55 erToLower[*a]==U
b2450 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29 pperToLower[*b])
b2460 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20 { a++; b++; }.
b2470 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20 3a return N<0 ? 0 :
b2480 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 UpperToLower[*a
b2490 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 ] - UpperToLower
b24a0 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 [*b];.}../*.** R
b24b0 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a 20 eturn TRUE if z
b24c0 69 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72 69 is a pure numeri
b24d0 63 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72 c string. Retur
b24e0 6e 20 46 41 4c 53 45 20 61 6e 64 20 6c 65 61 76 n FALSE and leav
b24f0 65 0a 2a 2a 20 2a 72 65 61 6c 6e 75 6d 20 75 6e e.** *realnum un
b2500 63 68 61 6e 67 65 64 20 69 66 20 74 68 65 20 73 changed if the s
b2510 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 61 tring contains a
b2520 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 68 69 ny character whi
b2530 63 68 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 61 72 ch is not.** par
b2540 74 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 0a 2a t of a number..*
b2550 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69 *.** If the stri
b2560 6e 67 20 69 73 20 70 75 72 65 20 6e 75 6d 65 72 ng is pure numer
b2570 69 63 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d ic, set *realnum
b2580 20 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 to TRUE if the
b2590 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 74 61 69 string.** contai
b25a0 6e 73 20 74 68 65 20 27 2e 27 20 63 68 61 72 61 ns the '.' chara
b25b0 63 74 65 72 20 6f 72 20 61 6e 20 22 45 2b 30 30 cter or an "E+00
b25c0 30 22 20 73 74 79 6c 65 20 65 78 70 6f 6e 65 6e 0" style exponen
b25d0 74 69 61 74 69 6f 6e 20 73 75 66 66 69 78 2e 0a tiation suffix..
b25e0 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 74 ** Otherwise set
b25f0 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 46 41 4c *realnum to FAL
b2600 53 45 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 6a SE. Note that j
b2610 75 73 74 20 62 65 63 61 75 65 20 2a 72 65 61 6c ust becaue *real
b2620 6e 75 6d 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20 num is.** false
b2630 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68 does not mean th
b2640 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 63 61 at the number ca
b2650 6e 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 6c n be successfull
b2660 79 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f y converted into
b2670 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 2d .** an integer -
b2680 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 6f 6f it might be too
b2690 20 62 69 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 big..**.** An e
b26a0 6d 70 74 79 20 73 74 72 69 6e 67 20 69 73 20 63 mpty string is c
b26b0 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e 75 onsidered non-nu
b26c0 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 meric..*/.SQLITE
b26d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b26e0 69 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e ite3IsNumber(con
b26f0 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 st char *z, int
b2700 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e 63 *realnum, u8 enc
b2710 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d 20 ){. int incr =
b2720 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 (enc==SQLITE_UTF
b2730 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65 6e 8?1:2);. if( en
b2740 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 c==SQLITE_UTF16B
b2750 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a E ) z++;. if( *
b2760 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b z=='-' || *z=='+
b2770 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 ' ) z += incr;.
b2780 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 if( !sqlite3Isd
b2790 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 igit(*z) ){.
b27a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
b27b0 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 2a 72 65 z += incr;. *re
b27c0 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68 69 alnum = 0;. whi
b27d0 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 le( sqlite3Isdig
b27e0 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 it(*z) ){ z += i
b27f0 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d ncr; }. if( *z=
b2800 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 2b 3d ='.' ){. z +=
b2810 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 incr;. if( !
b2820 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
b2830 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 z) ) return 0;.
b2840 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 while( sqlite
b2850 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 3Isdigit(*z) ){
b2860 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 z += incr; }.
b2870 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 *realnum = 1;.
b2880 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 }. if( *z=='e'
b2890 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 || *z=='E' ){.
b28a0 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 z += incr;.
b28b0 20 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c if( *z=='+' ||
b28c0 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 *z=='-' ) z +=
b28d0 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 73 incr;. if( !s
b28e0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
b28f0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 ) ) return 0;.
b2900 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
b2910 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a Isdigit(*z) ){ z
b2920 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 += incr; }.
b2930 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 *realnum = 1;.
b2940 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 }. return *z==0
b2950 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
b2960 74 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20 tring z[] is an
b2970 41 53 43 49 49 20 72 65 70 72 65 73 65 6e 74 61 ASCII representa
b2980 74 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e tion of a real n
b2990 75 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72 umber..** Conver
b29a0 74 20 74 68 69 73 20 73 74 72 69 6e 67 20 74 6f t this string to
b29b0 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a a double..**.**
b29c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73 This routine as
b29d0 73 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20 72 sumes that z[] r
b29e0 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 eally is a valid
b29f0 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 0a number. If it.
b2a00 2a 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 72 ** is not, the r
b2a10 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e esult is undefin
b2a20 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
b2a30 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 outine is used i
b2a40 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c 69 nstead of the li
b2a50 62 72 61 72 79 20 61 74 6f 66 28 29 20 66 75 6e brary atof() fun
b2a60 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a ction because.**
b2a70 20 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f the library ato
b2a80 66 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20 74 f() might want t
b2a90 6f 20 75 73 65 20 22 2c 22 20 61 73 20 74 68 65 o use "," as the
b2aa0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 decimal point i
b2ab0 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22 nstead.** of "."
b2ac0 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f depending on ho
b2ad0 77 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74 2e w locale is set.
b2ae0 20 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 But that would
b2af0 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a cause problems.
b2b00 2a 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20 ** for SQL. So
b2b10 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77 this routine alw
b2b20 61 79 73 20 75 73 65 73 20 22 2e 22 20 72 65 67 ays uses "." reg
b2b30 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c ardless of local
b2b40 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
b2b50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b2b60 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 AtoF(const char
b2b70 2a 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65 73 *z, double *pRes
b2b80 75 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 ult){.#ifndef SQ
b2b90 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
b2ba0 4e 47 5f 50 4f 49 4e 54 0a 20 20 63 6f 6e 73 74 NG_POINT. const
b2bb0 20 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20 char *zBegin =
b2bc0 7a 3b 0a 20 20 2f 2a 20 73 69 67 6e 20 2a 20 73 z;. /* sign * s
b2bd0 69 67 6e 69 66 69 63 61 6e 64 20 2a 20 28 31 30 ignificand * (10
b2be0 20 5e 20 28 65 73 69 67 6e 20 2a 20 65 78 70 6f ^ (esign * expo
b2bf0 6e 65 6e 74 29 29 20 2a 2f 0a 20 20 69 6e 74 20 nent)) */. int
b2c00 73 69 67 6e 20 3d 20 31 3b 20 20 20 2f 2a 20 73 sign = 1; /* s
b2c10 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61 ign of significa
b2c20 6e 64 20 2a 2f 0a 20 20 69 36 34 20 73 20 3d 20 nd */. i64 s =
b2c30 30 3b 20 20 20 20 20 20 2f 2a 20 73 69 67 6e 69 0; /* signi
b2c40 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 ficand */. int
b2c50 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 61 d = 0; /* a
b2c60 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 66 djust exponent f
b2c70 6f 72 20 73 68 69 66 74 69 6e 67 20 64 65 63 69 or shifting deci
b2c80 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 mal point */. i
b2c90 6e 74 20 65 73 69 67 6e 20 3d 20 31 3b 20 20 2f nt esign = 1; /
b2ca0 2a 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 * sign of expone
b2cb0 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 20 3d 20 nt */. int e =
b2cc0 30 3b 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 0; /* expon
b2cd0 65 6e 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 ent */. double
b2ce0 72 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 6e 44 result;. int nD
b2cf0 69 67 69 74 73 20 3d 20 30 3b 0a 0a 20 20 2f 2a igits = 0;.. /*
b2d00 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 73 70 skip leading sp
b2d10 61 63 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28 aces */. while(
b2d20 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
b2d30 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 2f 2a 20 *z) ) z++;. /*
b2d40 67 65 74 20 73 69 67 6e 20 6f 66 20 73 69 67 6e get sign of sign
b2d50 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 ificand */. if(
b2d60 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 *z=='-' ){.
b2d70 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7a sign = -1;. z
b2d80 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ++;. }else if(
b2d90 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a *z=='+' ){. z
b2da0 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b 69 ++;. }. /* ski
b2db0 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 65 73 p leading zeroes
b2dc0 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30 */. while( z[0
b2dd0 5d 3d 3d 27 30 27 20 29 20 7a 2b 2b 2c 20 6e 44 ]=='0' ) z++, nD
b2de0 69 67 69 74 73 2b 2b 3b 0a 0a 20 20 2f 2a 20 63 igits++;.. /* c
b2df0 6f 70 79 20 6d 61 78 20 73 69 67 6e 69 66 69 63 opy max signific
b2e00 61 6e 74 20 64 69 67 69 74 73 20 74 6f 20 73 69 ant digits to si
b2e10 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 77 gnificand */. w
b2e20 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 hile( sqlite3Isd
b2e30 69 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 28 igit(*z) && s<((
b2e40 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 29 LARGEST_INT64-9)
b2e50 2f 31 30 29 20 29 7b 0a 20 20 20 20 73 20 3d 20 /10) ){. s =
b2e60 73 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 s*10 + (*z - '0'
b2e70 29 3b 0a 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 );. z++, nDig
b2e80 69 74 73 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 its++;. }. /*
b2e90 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 66 69 skip non-signifi
b2ea0 63 61 6e 74 20 73 69 67 6e 69 66 69 63 61 6e 64 cant significand
b2eb0 20 64 69 67 69 74 73 0a 20 20 2a 2a 20 28 69 6e digits. ** (in
b2ec0 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 20 crease exponent
b2ed0 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 65 by d to shift de
b2ee0 63 69 6d 61 6c 20 6c 65 66 74 29 20 2a 2f 0a 20 cimal left) */.
b2ef0 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
b2f00 73 64 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b 2b sdigit(*z) ) z++
b2f10 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2b 2b , nDigits++, d++
b2f20 3b 0a 0a 20 20 2f 2a 20 69 66 20 64 65 63 69 6d ;.. /* if decim
b2f30 61 6c 20 70 6f 69 6e 74 20 69 73 20 70 72 65 73 al point is pres
b2f40 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d ent */. if( *z=
b2f50 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b ='.' ){. z++;
b2f60 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 69 67 . /* copy dig
b2f70 69 74 73 20 66 72 6f 6d 20 61 66 74 65 72 20 64 its from after d
b2f80 65 63 69 6d 61 6c 20 74 6f 20 73 69 67 6e 69 66 ecimal to signif
b2f90 69 63 61 6e 64 0a 20 20 20 20 2a 2a 20 28 64 65 icand. ** (de
b2fa0 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 20 crease exponent
b2fb0 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 65 by d to shift de
b2fc0 63 69 6d 61 6c 20 72 69 67 68 74 29 20 2a 2f 0a cimal right) */.
b2fd0 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
b2fe0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26 e3Isdigit(*z) &&
b2ff0 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54 s<((LARGEST_INT
b3000 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20 64-9)/10) ){.
b3010 20 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a s = s*10 + (*
b3020 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 z - '0');.
b3030 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 z++, nDigits++,
b3040 64 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f d--;. }. /
b3050 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 * skip non-signi
b3060 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 2a 2f ficant digits */
b3070 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 . while( sqli
b3080 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 te3Isdigit(*z) )
b3090 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b z++, nDigits++;
b30a0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 65 78 . }.. /* if ex
b30b0 70 6f 6e 65 6e 74 20 69 73 20 70 72 65 73 65 6e ponent is presen
b30c0 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 t */. if( *z=='
b30d0 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b e' || *z=='E' ){
b30e0 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 2f 2a . z++;. /*
b30f0 20 67 65 74 20 73 69 67 6e 20 6f 66 20 65 78 70 get sign of exp
b3100 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 onent */. if(
b3110 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 *z=='-' ){.
b3120 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 esign = -1;.
b3130 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c z++;. }el
b3140 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 se if( *z=='+' )
b3150 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 {. z++;.
b3160 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 }. /* copy d
b3170 69 67 69 74 73 20 74 6f 20 65 78 70 6f 6e 65 6e igits to exponen
b3180 74 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 t */. while(
b3190 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
b31a0 7a 29 20 29 7b 0a 20 20 20 20 20 20 65 20 3d 20 z) ){. e =
b31b0 65 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 e*10 + (*z - '0'
b31c0 29 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 );. z++;.
b31d0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 64 }. }.. /* ad
b31e0 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 62 79 just exponent by
b31f0 20 64 2c 20 61 6e 64 20 75 70 64 61 74 65 20 73 d, and update s
b3200 69 67 6e 20 2a 2f 0a 20 20 65 20 3d 20 28 65 2a ign */. e = (e*
b3210 65 73 69 67 6e 29 20 2b 20 64 3b 0a 20 20 69 66 esign) + d;. if
b3220 28 20 65 3c 30 20 29 20 7b 0a 20 20 20 20 65 73 ( e<0 ) {. es
b3230 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 65 20 ign = -1;. e
b3240 2a 3d 20 2d 31 3b 0a 20 20 7d 20 65 6c 73 65 20 *= -1;. } else
b3250 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 31 3b {. esign = 1;
b3260 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 30 20 . }.. /* if 0
b3270 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 significand */.
b3280 20 69 66 28 20 21 73 20 29 20 7b 0a 20 20 20 20 if( !s ) {.
b3290 2f 2a 20 49 6e 20 74 68 65 20 49 45 45 45 20 37 /* In the IEEE 7
b32a0 35 34 20 73 74 61 6e 64 61 72 64 2c 20 7a 65 72 54 standard, zer
b32b0 6f 20 69 73 20 73 69 67 6e 65 64 2e 0a 20 20 20 o is signed..
b32c0 20 2a 2a 20 41 64 64 20 74 68 65 20 73 69 67 6e ** Add the sign
b32d0 20 69 66 20 77 65 27 76 65 20 73 65 65 6e 20 61 if we've seen a
b32e0 74 20 6c 65 61 73 74 20 6f 6e 65 20 64 69 67 69 t least one digi
b32f0 74 20 2a 2f 0a 20 20 20 20 72 65 73 75 6c 74 20 t */. result
b3300 3d 20 28 73 69 67 6e 3c 30 20 26 26 20 6e 44 69 = (sign<0 && nDi
b3310 67 69 74 73 29 20 3f 20 2d 28 64 6f 75 62 6c 65 gits) ? -(double
b3320 29 30 20 3a 20 28 64 6f 75 62 6c 65 29 30 3b 0a )0 : (double)0;.
b3330 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f } else {. /
b3340 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64 * attempt to red
b3350 75 63 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a uce exponent */.
b3360 20 20 20 20 69 66 28 20 65 73 69 67 6e 3e 30 20 if( esign>0
b3370 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 ){. while(
b3380 73 3c 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 s<(LARGEST_INT64
b3390 2f 31 30 29 20 26 26 20 65 3e 30 20 29 20 65 2d /10) && e>0 ) e-
b33a0 2d 2c 73 2a 3d 31 30 3b 0a 20 20 20 20 7d 65 6c -,s*=10;. }el
b33b0 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 se{. while(
b33c0 20 21 28 73 25 31 30 29 20 26 26 20 65 3e 30 20 !(s%10) && e>0
b33d0 29 20 65 2d 2d 2c 73 2f 3d 31 30 3b 0a 20 20 20 ) e--,s/=10;.
b33e0 20 7d 0a 0a 20 20 20 20 2f 2a 20 61 64 6a 75 73 }.. /* adjus
b33f0 74 20 74 68 65 20 73 69 67 6e 20 6f 66 20 73 69 t the sign of si
b3400 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 20 gnificand */.
b3410 20 73 20 3d 20 73 69 67 6e 3c 30 20 3f 20 2d 73 s = sign<0 ? -s
b3420 20 3a 20 73 3b 0a 0a 20 20 20 20 2f 2a 20 69 66 : s;.. /* if
b3430 20 65 78 70 6f 6e 65 6e 74 2c 20 73 63 61 6c 65 exponent, scale
b3440 20 73 69 67 6e 69 66 69 63 61 6e 64 20 61 73 20 significand as
b3450 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 20 appropriate.
b3460 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 ** and store in
b3470 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 20 20 69 result. */. i
b3480 66 28 20 65 20 29 7b 0a 20 20 20 20 20 20 64 6f f( e ){. do
b3490 75 62 6c 65 20 73 63 61 6c 65 20 3d 20 31 2e 30 uble scale = 1.0
b34a0 3b 0a 20 20 20 20 20 20 2f 2a 20 61 74 74 65 6d ;. /* attem
b34b0 70 74 20 74 6f 20 68 61 6e 64 6c 65 20 65 78 74 pt to handle ext
b34c0 72 65 6d 65 6c 79 20 73 6d 61 6c 6c 2f 6c 61 72 remely small/lar
b34d0 67 65 20 6e 75 6d 62 65 72 73 20 62 65 74 74 65 ge numbers bette
b34e0 72 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 65 r */. if( e
b34f0 3e 33 30 37 20 26 26 20 65 3c 33 34 32 20 29 7b >307 && e<342 ){
b3500 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
b3510 65 25 33 30 38 20 29 20 7b 20 73 63 61 6c 65 20 e%308 ) { scale
b3520 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d 20 *= 1.0e+1; e -=
b3530 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 1; }. if(
b3540 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 esign<0 ){.
b3550 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 result = s
b3560 20 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 / scale;.
b3570 20 20 20 20 72 65 73 75 6c 74 20 2f 3d 20 31 2e result /= 1.
b3580 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20 0e+308;.
b3590 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
b35a0 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 63 result = s * sc
b35b0 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72 ale;. r
b35c0 65 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b 33 30 esult *= 1.0e+30
b35d0 38 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 8;. }.
b35e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
b35f0 20 20 2f 2a 20 31 2e 30 65 2b 32 32 20 69 73 20 /* 1.0e+22 is
b3600 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 77 65 the largest powe
b3610 72 20 6f 66 20 31 30 20 74 68 61 6e 20 63 61 6e r of 10 than can
b3620 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 be . **
b3630 72 65 70 72 65 73 65 6e 74 65 64 20 65 78 61 63 represented exac
b3640 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 tly. */.
b3650 77 68 69 6c 65 28 20 65 25 32 32 20 29 20 7b 20 while( e%22 ) {
b3660 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b scale *= 1.0e+1;
b3670 20 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 e -= 1; }.
b3680 20 20 20 77 68 69 6c 65 28 20 65 3e 30 20 29 20 while( e>0 )
b3690 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b { scale *= 1.0e+
b36a0 32 32 3b 20 65 20 2d 3d 20 32 32 3b 20 7d 0a 20 22; e -= 22; }.
b36b0 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67 6e if( esign
b36c0 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 <0 ){.
b36d0 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63 61 result = s / sca
b36e0 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 le;. }els
b36f0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 e{. res
b3700 75 6c 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 3b ult = s * scale;
b3710 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
b3720 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a }. } else {.
b3730 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 28 result = (
b3740 64 6f 75 62 6c 65 29 73 3b 0a 20 20 20 20 7d 0a double)s;. }.
b3750 20 20 7d 0a 0a 20 20 2f 2a 20 73 74 6f 72 65 20 }.. /* store
b3760 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 the result */.
b3770 2a 70 52 65 73 75 6c 74 20 3d 20 72 65 73 75 6c *pResult = resul
b3780 74 3b 0a 0a 20 20 2f 2a 20 72 65 74 75 72 6e 20 t;.. /* return
b3790 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
b37a0 74 65 72 73 20 75 73 65 64 20 2a 2f 0a 20 20 72 ters used */. r
b37b0 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 2d 20 eturn (int)(z -
b37c0 7a 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 0a 20 zBegin);.#else.
b37d0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 return sqlite3A
b37e0 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 6c 74 toi64(z, pResult
b37f0 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c );.#endif /* SQL
b3800 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
b3810 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a G_POINT */.}../*
b3820 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 .** Compare the
b3830 31 39 2d 63 68 61 72 61 63 74 65 72 20 73 74 72 19-character str
b3840 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73 74 ing zNum against
b3850 20 74 68 65 20 74 65 78 74 20 72 65 70 72 65 73 the text repres
b3860 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c 75 entation.** valu
b3870 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33 37 32 e 2^63: 9223372
b3880 30 33 36 38 35 34 37 37 35 38 30 38 2e 20 20 52 036854775808. R
b3890 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 eturn negative,
b38a0 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 zero, or positiv
b38b0 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73 20 e.** if zNum is
b38c0 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c less than, equal
b38d0 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 to, or greater
b38e0 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e 67 2e than the string.
b38f0 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 65 .**.** Unlike me
b3900 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75 74 mcmp() this rout
b3910 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 ine is guarantee
b3920 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 d to return the
b3930 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69 6e difference.** in
b3940 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 the values of t
b3950 68 65 20 6c 61 73 74 20 64 69 67 69 74 20 69 66 he last digit if
b3960 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 the only differ
b3970 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0a 2a ence is in the.*
b3980 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20 20 53 * last digit. S
b3990 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a o, for example,.
b39a0 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70 61 **.** compa
b39b0 72 65 32 70 6f 77 36 33 28 22 39 32 32 33 33 37 re2pow63("922337
b39c0 32 30 33 36 38 35 34 37 37 35 38 30 30 22 29 0a 2036854775800").
b39d0 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 **.** will retur
b39e0 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 n -8..*/.static
b39f0 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 int compare2pow6
b3a00 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 3(const char *zN
b3a10 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 um){. int c;.
b3a20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d 2c c = memcmp(zNum,
b3a30 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 "922337203685477
b3a40 35 38 30 22 2c 31 38 29 2a 31 30 3b 0a 20 20 69 580",18)*10;. i
b3a50 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 f( c==0 ){. c
b3a60 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38 = zNum[18] - '8
b3a70 27 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 ';. }. return
b3a80 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 c;.}.../*.** Ret
b3a90 75 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d urn TRUE if zNum
b3aa0 20 69 73 20 61 20 36 34 2d 62 69 74 20 73 69 67 is a 64-bit sig
b3ab0 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20 ned integer and
b3ac0 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c write.** the val
b3ad0 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ue of the intege
b3ae0 72 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49 r into *pNum. I
b3af0 66 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e f zNum is not an
b3b00 20 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69 integer.** or i
b3b10 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61 s an integer tha
b3b20 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 t is too large t
b3b30 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20 77 o be expressed w
b3b40 69 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20 ith 64 bits,.**
b3b50 74 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 then return fals
b3b60 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 e..**.** When th
b3b70 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f is routine was o
b3b80 72 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65 riginally writte
b3b90 6e 20 69 74 20 64 65 61 6c 74 20 77 69 74 68 20 n it dealt with
b3ba0 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e only.** 32-bit n
b3bb0 75 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61 74 umbers. At that
b3bc0 20 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75 time, it was mu
b3bd0 63 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 ch faster than t
b3be0 68 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62 he.** atoi() lib
b3bf0 72 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 rary routine in
b3c00 52 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53 RedHat 7.2..*/.S
b3c10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b3c20 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 t sqlite3Atoi64(
b3c30 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d const char *zNum
b3c40 2c 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20 , i64 *pNum){.
b3c50 69 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74 i64 v = 0;. int
b3c60 20 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63 neg;. int i, c
b3c70 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
b3c80 7a 53 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 28 zStart;. while(
b3c90 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 sqlite3Isspace(
b3ca0 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 2b 3b *zNum) ) zNum++;
b3cb0 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d . if( *zNum=='-
b3cc0 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 ' ){. neg = 1
b3cd0 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 ;. zNum++;.
b3ce0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d }else if( *zNum=
b3cf0 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 ='+' ){. neg
b3d00 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b = 0;. zNum++;
b3d10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 . }else{. ne
b3d20 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 53 74 g = 0;. }. zSt
b3d30 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 77 68 art = zNum;. wh
b3d40 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 ile( zNum[0]=='0
b3d50 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f ' ){ zNum++; } /
b3d60 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 61 64 * Skip over lead
b3d70 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 6b 65 ing zeros. Ticke
b3d80 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 6f 72 t #2454 */. for
b3d90 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d (i=0; (c=zNum[i]
b3da0 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 )>='0' && c<='9'
b3db0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 ; i++){. v =
b3dc0 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a v*10 + c - '0';.
b3dd0 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 6e 65 }. *pNum = ne
b3de0 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 69 66 g ? -v : v;. if
b3df0 28 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d 30 20 ( c!=0 || (i==0
b3e00 26 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 6d 29 && zStart==zNum)
b3e10 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 || i>19 ){.
b3e20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74 79 /* zNum is empty
b3e30 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e or contains non
b3e40 2d 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f 72 -numeric text or
b3e50 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a is longer. *
b3e60 2a 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 * than 19 digits
b3e70 20 28 74 68 75 73 20 67 75 61 72 61 6e 74 69 6e (thus guarantin
b3e80 67 20 74 68 61 74 20 69 74 20 69 73 20 74 6f 6f g that it is too
b3e90 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20 72 large) */. r
b3ea0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 eturn 0;. }else
b3eb0 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 if( i<19 ){.
b3ec0 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39 /* Less than 19
b3ed0 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b digits, so we k
b3ee0 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73 now that it fits
b3ef0 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20 in 64 bits */.
b3f00 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
b3f10 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39 2d else{. /* 19-
b3f20 64 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d 75 digit numbers mu
b3f30 73 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72 20 st be no larger
b3f40 74 68 61 6e 20 39 32 32 33 33 37 32 30 33 36 38 than 92233720368
b3f50 35 34 37 37 35 38 30 37 20 69 66 20 70 6f 73 69 54775807 if posi
b3f60 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20 39 tive. ** or 9
b3f70 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 2233720368547758
b3f80 30 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20 08 if negative.
b3f90 20 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33 33 Note that 92233
b3fa0 37 32 30 33 36 38 35 34 36 36 35 38 30 38 0a 20 72036854665808.
b3fb0 20 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20 2a ** is 2^63. *
b3fc0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d /. return com
b3fd0 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29 pare2pow63(zNum)
b3fe0 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a <neg;. }.}../*.
b3ff0 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 4e ** The string zN
b4000 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e um represents an
b4010 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 unsigned intege
b4020 72 2e 20 20 54 68 65 20 7a 4e 75 6d 20 73 74 72 r. The zNum str
b4030 69 6e 67 0a 2a 2a 20 63 6f 6e 73 69 73 74 73 20 ing.** consists
b4040 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 of one or more d
b4050 69 67 69 74 20 63 68 61 72 61 63 74 65 72 73 20 igit characters
b4060 61 6e 64 20 69 73 20 74 65 72 6d 69 6e 61 74 65 and is terminate
b4070 64 20 62 79 0a 2a 2a 20 61 20 7a 65 72 6f 20 63 d by.** a zero c
b4080 68 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 73 haracter. Any s
b4090 74 72 61 79 20 63 68 61 72 61 63 74 65 72 73 20 tray characters
b40a0 69 6e 20 7a 4e 75 6d 20 72 65 73 75 6c 74 20 69 in zNum result i
b40b0 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 62 n undefined.** b
b40c0 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 ehavior..**.** I
b40d0 66 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 69 f the unsigned i
b40e0 6e 74 65 67 65 72 20 74 68 61 74 20 7a 4e 75 6d nteger that zNum
b40f0 20 72 65 70 72 65 73 65 6e 74 73 20 77 69 6c 6c represents will
b4100 20 66 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 2d fit in a.** 64-
b4110 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
b4120 65 72 2c 20 72 65 74 75 72 6e 20 54 52 55 45 2e er, return TRUE.
b4130 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 Otherwise retu
b4140 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 rn FALSE..**.**
b4150 49 66 20 74 68 65 20 6e 65 67 46 6c 61 67 20 70 If the negFlag p
b4160 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 arameter is true
b4170 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 , that means tha
b4180 74 20 7a 4e 75 6d 20 72 65 61 6c 6c 79 20 72 65 t zNum really re
b4190 70 72 65 73 65 6e 74 73 0a 2a 2a 20 61 20 6e 65 presents.** a ne
b41a0 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 gative number.
b41b0 28 54 68 65 20 6c 65 61 64 69 6e 67 20 22 2d 22 (The leading "-"
b41c0 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d is omitted from
b41d0 20 7a 4e 75 6d 2e 29 20 20 54 68 69 73 0a 2a 2a zNum.) This.**
b41e0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 parameter is ne
b41f0 65 64 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e eded to determin
b4200 65 20 61 20 62 6f 75 6e 64 61 72 79 20 63 61 73 e a boundary cas
b4210 65 2e 20 20 41 20 73 74 72 69 6e 67 0a 2a 2a 20 e. A string.**
b4220 6f 66 20 22 39 32 32 33 33 37 33 30 33 36 38 35 of "922337303685
b4230 34 37 37 35 38 30 38 22 20 72 65 74 75 72 6e 73 4775808" returns
b4240 20 66 61 6c 73 65 20 69 66 20 6e 65 67 46 6c 61 false if negFla
b4250 67 20 69 73 20 66 61 6c 73 65 20 6f 72 20 74 72 g is false or tr
b4260 75 65 0a 2a 2a 20 69 66 20 6e 65 67 46 6c 61 67 ue.** if negFlag
b4270 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 is true..**.**
b4280 4c 65 61 64 69 6e 67 20 7a 65 72 6f 73 20 61 72 Leading zeros ar
b4290 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 e ignored..*/.SQ
b42a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b42b0 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 sqlite3FitsIn64
b42c0 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 Bits(const char
b42d0 2a 7a 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c *zNum, int negFl
b42e0 61 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 ag){. int i;.
b42f0 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 0a 20 20 int neg = 0;..
b4300 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 30 5d 3e assert( zNum[0]>
b4310 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 30 5d 3c ='0' && zNum[0]<
b4320 3d 27 39 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d 20 ='9' ); /* zNum
b4330 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 6e is an unsigned n
b4340 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 umber */.. if(
b4350 6e 65 67 46 6c 61 67 20 29 20 6e 65 67 20 3d 20 negFlag ) neg =
b4360 31 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 28 20 1-neg;. while(
b4370 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 20 *zNum=='0' ){.
b4380 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53 zNum++; /* S
b4390 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f kip leading zero
b43a0 73 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 34 s. Ticket #2454
b43b0 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d */. }. for(i=
b43c0 30 3b 20 7a 4e 75 6d 5b 69 5d 3b 20 69 2b 2b 29 0; zNum[i]; i++)
b43d0 7b 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 69 { assert( zNum[i
b43e0 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 69 ]>='0' && zNum[i
b43f0 5d 3c 3d 27 39 27 20 29 3b 20 7d 0a 20 20 69 66 ]<='9' ); }. if
b4400 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a ( i<19 ){. /*
b4410 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 Guaranteed to f
b4420 69 74 20 69 66 20 6c 65 73 73 20 74 68 61 6e 20 it if less than
b4430 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 19 digits */.
b4440 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c return 1;. }el
b4450 73 65 20 69 66 28 20 69 3e 31 39 20 29 7b 0a 20 se if( i>19 ){.
b4460 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 /* Guaranteed
b4470 20 74 6f 20 62 65 20 74 6f 6f 20 62 69 67 20 69 to be too big i
b4480 66 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 f greater than 1
b4490 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20 9 digits */.
b44a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 return 0;. }els
b44b0 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 61 72 e{. /* Compar
b44c0 65 20 61 67 61 69 6e 73 74 20 32 5e 36 33 2e 20 e against 2^63.
b44d0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f */. return co
b44e0 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d mpare2pow63(zNum
b44f0 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a )<neg;. }.}../*
b4500 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 72 65 70 72 .** If zNum repr
b4510 65 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 esents an intege
b4520 72 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 r that will fit
b4530 69 6e 20 33 32 2d 62 69 74 73 2c 20 74 68 65 6e in 32-bits, then
b4540 20 73 65 74 0a 2a 2a 20 2a 70 56 61 6c 75 65 20 set.** *pValue
b4550 74 6f 20 74 68 61 74 20 69 6e 74 65 67 65 72 20 to that integer
b4560 61 6e 64 20 72 65 74 75 72 6e 20 74 72 75 65 2e and return true.
b4570 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 Otherwise retu
b4580 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 rn false..**.**
b4590 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 Any non-numeric
b45a0 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20 characters that
b45b0 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d 20 61 following zNum a
b45c0 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 54 re ignored..** T
b45d0 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 his is different
b45e0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 41 74 6f from sqlite3Ato
b45f0 69 36 34 28 29 20 77 68 69 63 68 20 72 65 71 75 i64() which requ
b4600 69 72 65 73 20 74 68 65 0a 2a 2a 20 69 6e 70 75 ires the.** inpu
b4610 74 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 7a t number to be z
b4620 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a ero-terminated..
b4630 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b4640 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 E int sqlite3Get
b4650 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 Int32(const char
b4660 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70 56 61 *zNum, int *pVa
b4670 6c 75 65 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 lue){. sqlite_i
b4680 6e 74 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e nt64 v = 0;. in
b4690 74 20 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e 65 t i, c;. int ne
b46a0 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 4e 75 g = 0;. if( zNu
b46b0 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 m[0]=='-' ){.
b46c0 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e neg = 1;. zN
b46d0 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 um++;. }else if
b46e0 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27 20 29 ( zNum[0]=='+' )
b46f0 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 {. zNum++;.
b4700 7d 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b }. while( zNum[
b4710 30 5d 3d 3d 27 30 27 20 29 20 7a 4e 75 6d 2b 2b 0]=='0' ) zNum++
b4720 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 ;. for(i=0; i<1
b4730 31 20 26 26 20 28 63 20 3d 20 7a 4e 75 6d 5b 69 1 && (c = zNum[i
b4740 5d 20 2d 20 27 30 27 29 3e 3d 30 20 26 26 20 63 ] - '0')>=0 && c
b4750 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 <=9; i++){. v
b4760 20 3d 20 76 2a 31 30 20 2b 20 63 3b 0a 20 20 7d = v*10 + c;. }
b4770 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 6f 6e 67 65 .. /* The longe
b4780 73 74 20 64 65 63 69 6d 61 6c 20 72 65 70 72 65 st decimal repre
b4790 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 33 sentation of a 3
b47a0 32 20 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 2 bit integer is
b47b0 20 31 30 20 64 69 67 69 74 73 3a 0a 20 20 2a 2a 10 digits:. **
b47c0 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 . **
b47d0 20 20 31 32 33 34 35 36 37 38 39 30 0a 20 20 2a 1234567890. *
b47e0 2a 20 20 20 20 20 32 5e 33 31 20 2d 3e 20 32 31 * 2^31 -> 21
b47f0 34 37 34 38 33 36 34 38 0a 20 20 2a 2f 0a 20 20 47483648. */.
b4800 69 66 28 20 69 3e 31 30 20 29 7b 0a 20 20 20 20 if( i>10 ){.
b4810 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
b4820 69 66 28 20 76 2d 6e 65 67 3e 32 31 34 37 34 38 if( v-neg>214748
b4830 33 36 34 37 20 29 7b 0a 20 20 20 20 72 65 74 75 3647 ){. retu
b4840 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
b4850 6e 65 67 20 29 7b 0a 20 20 20 20 76 20 3d 20 2d neg ){. v = -
b4860 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c 75 65 v;. }. *pValue
b4870 20 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72 65 74 = (int)v;. ret
b4880 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 1;.}../*.**
b4890 54 68 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e The variable-len
b48a0 67 74 68 20 69 6e 74 65 67 65 72 20 65 6e 63 6f gth integer enco
b48b0 64 69 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c 6f ding is as follo
b48c0 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a ws:.**.** KEY:.*
b48d0 2a 20 20 20 20 20 20 20 20 20 41 20 3d 20 30 78 * A = 0x
b48e0 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 xxxxxx 7 bits
b48f0 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 of data and one
b4900 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 flag bit.**
b4910 20 20 20 20 20 42 20 3d 20 31 78 78 78 78 78 78 B = 1xxxxxx
b4920 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 x 7 bits of d
b4930 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 ata and one flag
b4940 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 bit.**
b4950 43 20 3d 20 78 78 78 78 78 78 78 78 20 20 20 20 C = xxxxxxxx
b4960 38 20 62 69 74 73 20 6f 66 20 64 61 74 61 0a 2a 8 bits of data.*
b4970 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41 *.** 7 bits - A
b4980 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41 .** 14 bits - BA
b4990 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42 .** 21 bits - BB
b49a0 41 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d 20 42 A.** 28 bits - B
b49b0 42 42 41 0a 2a 2a 20 33 35 20 62 69 74 73 20 2d BBA.** 35 bits -
b49c0 20 42 42 42 42 41 0a 2a 2a 20 34 32 20 62 69 74 BBBBA.** 42 bit
b49d0 73 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34 39 s - BBBBBA.** 49
b49e0 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 41 0a bits - BBBBBBA.
b49f0 2a 2a 20 35 36 20 62 69 74 73 20 2d 20 42 42 42 ** 56 bits - BBB
b4a00 42 42 42 42 41 0a 2a 2a 20 36 34 20 62 69 74 73 BBBBA.** 64 bits
b4a10 20 2d 20 42 42 42 42 42 42 42 42 43 0a 2a 2f 0a - BBBBBBBBC.*/.
b4a20 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 36 ./*.** Write a 6
b4a30 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 4-bit variable-l
b4a40 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f ength integer to
b4a50 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 memory starting
b4a60 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 at p[0]..** The
b4a70 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 length of data
b4a80 77 72 69 74 65 20 77 69 6c 6c 20 62 65 20 62 65 write will be be
b4a90 74 77 65 65 6e 20 31 20 61 6e 64 20 39 20 62 79 tween 1 and 9 by
b4aa0 74 65 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 tes. The number
b4ab0 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 72 69 .** of bytes wri
b4ac0 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64 tten is returned
b4ad0 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61 62 ..**.** A variab
b4ae0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le-length intege
b4af0 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 r consists of th
b4b00 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f e lower 7 bits o
b4b10 66 20 65 61 63 68 20 62 79 74 65 0a 2a 2a 20 66 f each byte.** f
b4b20 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61 or all bytes tha
b4b30 74 20 68 61 76 65 20 74 68 65 20 38 74 68 20 62 t have the 8th b
b4b40 69 74 20 73 65 74 20 61 6e 64 20 6f 6e 65 20 62 it set and one b
b4b50 79 74 65 20 77 69 74 68 20 74 68 65 20 38 74 68 yte with the 8th
b4b60 0a 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e 20 20 .** bit clear.
b4b70 45 78 63 65 70 74 2c 20 69 66 20 77 65 20 67 65 Except, if we ge
b4b80 74 20 74 6f 20 74 68 65 20 39 74 68 20 62 79 74 t to the 9th byt
b4b90 65 2c 20 69 74 20 73 74 6f 72 65 73 20 74 68 65 e, it stores the
b4ba0 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73 20 full.** 8 bits
b4bb0 61 6e 64 20 69 73 20 74 68 65 20 6c 61 73 74 20 and is the last
b4bc0 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f byte..*/.SQLITE_
b4bd0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b4be0 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73 te3PutVarint(uns
b4bf0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 igned char *p, u
b4c00 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c 20 64 v){. int i,
b4c10 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b 31 j, n;. u8 buf[1
b4c20 30 5d 3b 0a 20 20 69 66 28 20 76 20 26 20 28 28 0];. if( v & ((
b4c30 28 75 36 34 29 30 78 66 66 30 30 30 30 30 30 29 (u64)0xff000000)
b4c40 3c 3c 33 32 29 20 29 7b 0a 20 20 20 20 70 5b 38 <<32) ){. p[8
b4c50 5d 20 3d 20 28 75 38 29 76 3b 0a 20 20 20 20 76 ] = (u8)v;. v
b4c60 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28 >>= 8;. for(
b4c70 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b i=7; i>=0; i--){
b4c80 0a 20 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 75 . p[i] = (u
b4c90 38 29 28 28 76 20 26 20 30 78 37 66 29 20 7c 20 8)((v & 0x7f) |
b4ca0 30 78 38 30 29 3b 0a 20 20 20 20 20 20 76 20 3e 0x80);. v >
b4cb0 3e 3d 20 37 3b 0a 20 20 20 20 7d 0a 20 20 20 20 >= 7;. }.
b4cc0 72 65 74 75 72 6e 20 39 3b 0a 20 20 7d 20 20 20 return 9;. }
b4cd0 20 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b . n = 0;. do{
b4ce0 0a 20 20 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20 . buf[n++] =
b4cf0 28 75 38 29 28 28 76 20 26 20 30 78 37 66 29 20 (u8)((v & 0x7f)
b4d00 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 76 20 3e | 0x80);. v >
b4d10 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 >= 7;. }while(
b4d20 76 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30 5d v!=0 );. buf[0]
b4d30 20 26 3d 20 30 78 37 66 3b 0a 20 20 61 73 73 65 &= 0x7f;. asse
b4d40 72 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f rt( n<=9 );. fo
b4d50 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e r(i=0, j=n-1; j>
b4d60 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 =0; j--, i++){.
b4d70 20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d p[i] = buf[j]
b4d80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e ;. }. return n
b4d90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
b4da0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 66 61 73 routine is a fas
b4db0 74 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 73 ter version of s
b4dc0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 qlite3PutVarint(
b4dd0 29 20 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a 20 77 ) that only.** w
b4de0 6f 72 6b 73 20 66 6f 72 20 33 32 2d 62 69 74 20 orks for 32-bit
b4df0 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 positive integer
b4e00 73 20 61 6e 64 20 77 68 69 63 68 20 69 73 20 6f s and which is o
b4e10 70 74 69 6d 69 7a 65 64 20 66 6f 72 0a 2a 2a 20 ptimized for.**
b4e20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 the common case
b4e30 6f 66 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 of small integer
b4e40 73 2e 20 20 41 20 4d 41 43 52 4f 20 76 65 72 73 s. A MACRO vers
b4e50 69 6f 6e 2c 20 70 75 74 56 61 72 69 6e 74 33 32 ion, putVarint32
b4e60 2c 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64 ,.** is provided
b4e70 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 which inlines t
b4e80 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 he single-byte c
b4e90 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 ase. All code s
b4ea0 68 6f 75 6c 64 20 75 73 65 0a 2a 2a 20 74 68 65 hould use.** the
b4eb0 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 MACRO version a
b4ec0 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 s this function
b4ed0 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 assumes the sing
b4ee0 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 le-byte case has
b4ef0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e .** already been
b4f00 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c handled..*/.SQL
b4f10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b4f20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
b4f30 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 32(unsigned char
b4f40 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 23 69 66 *p, u32 v){.#if
b4f50 6e 64 65 66 20 70 75 74 56 61 72 69 6e 74 33 32 ndef putVarint32
b4f60 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 37 . if( (v & ~0x7
b4f70 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30 f)==0 ){. p[0
b4f80 5d 20 3d 20 76 3b 0a 20 20 20 20 72 65 74 75 72 ] = v;. retur
b4f90 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a n 1;. }.#endif.
b4fa0 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 33 66 if( (v & ~0x3f
b4fb0 66 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b ff)==0 ){. p[
b4fc0 30 5d 20 3d 20 28 75 38 29 28 28 76 3e 3e 37 29 0] = (u8)((v>>7)
b4fd0 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 70 5b | 0x80);. p[
b4fe0 31 5d 20 3d 20 28 75 38 29 28 76 20 26 20 30 78 1] = (u8)(v & 0x
b4ff0 37 66 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 7f);. return
b5000 32 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2;. }. return
b5010 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
b5020 28 70 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a (p, v);.}../*.**
b5030 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76 Read a 64-bit v
b5040 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 ariable-length i
b5050 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f nteger from memo
b5060 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 ry starting at p
b5070 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 [0]..** Return t
b5080 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
b5090 65 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 61 es read. The va
b50a0 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e lue is stored in
b50b0 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 *v..*/.SQLITE_P
b50c0 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 RIVATE u8 sqlite
b50d0 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 3GetVarint(const
b50e0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
b50f0 70 2c 20 75 36 34 20 2a 76 29 7b 0a 20 20 75 33 p, u64 *v){. u3
b5100 32 20 61 2c 62 2c 73 3b 0a 0a 20 20 61 20 3d 20 2 a,b,s;.. a =
b5110 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 *p;. /* a: p0 (
b5120 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
b5130 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
b5140 7b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 {. *v = a;.
b5150 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
b5160 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 . p++;. b = *p
b5170 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e ;. /* b: p1 (un
b5180 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 masked) */. if
b5190 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(b&0x80)). {.
b51a0 20 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20 a &= 0x7f;.
b51b0 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 a = a<<7;.
b51c0 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 a |= b;. *v
b51d0 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = a;. return
b51e0 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 2;. }.. p++;.
b51f0 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 a = a<<14;. a
b5200 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 |= *p;. /* a: p
b5210 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61 0<<14 | p2 (unma
b5220 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b5230 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (a&0x80)). {.
b5240 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 a &= (0x7f<<14
b5250 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 )|(0x7f);. b
b5260 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d &= 0x7f;. b =
b5270 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 b<<7;. a |=
b5280 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 b;. *v = a;.
b5290 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d return 3;. }
b52a0 0a 0a 20 20 2f 2a 20 43 53 45 31 20 66 72 6f 6d .. /* CSE1 from
b52b0 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d below */. a &=
b52c0 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b52d0 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d f);. p++;. b =
b52e0 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a b<<14;. b |= *
b52f0 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 p;. /* b: p1<<1
b5300 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 4 | p3 (unmasked
b5310 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 ) */. if (!(b&0
b5320 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20 x80)). {. b
b5330 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 &= (0x7f<<14)|(0
b5340 78 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 x7f);. /* mov
b5350 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20 ed CSE1 up */.
b5360 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c /* a &= (0x7f<
b5370 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a <14)|(0x7f); */.
b5380 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 a = a<<7;.
b5390 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 a |= b;. *v
b53a0 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e = a;. return
b53b0 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a 4;. }.. /* a:
b53c0 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 p0<<14 | p2 (ma
b53d0 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a sked) */. /* b:
b53e0 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e p1<<14 | p3 (un
b53f0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 masked) */. /*
b5400 31 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 1:save off p0<<2
b5410 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 1 | p1<<14 | p2<
b5420 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 <7 | p3 (masked)
b5430 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 */. /* moved C
b5440 53 45 31 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 SE1 up */. /* a
b5450 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
b5460 30 78 37 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 0x7f); */. b &=
b5470 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b5480 66 29 3b 0a 20 20 73 20 3d 20 61 3b 0a 20 20 2f f);. s = a;. /
b5490 2a 20 73 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 * s: p0<<14 | p2
b54a0 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 (masked) */..
b54b0 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 p++;. a = a<<14
b54c0 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. a |= *p;. /
b54d0 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 * a: p0<<28 | p2
b54e0 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 <<14 | p4 (unmas
b54f0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 ked) */. if (!(
b5500 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 a&0x80)). {.
b5510 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 /* we can skip
b5520 74 68 65 73 65 20 63 61 75 73 65 20 74 68 65 79 these cause they
b5530 20 77 65 72 65 20 28 65 66 66 65 63 74 69 76 65 were (effective
b5540 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69 ly) done above i
b5550 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a n calc'ing s */.
b5560 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 /* a &= (0x7
b5570 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 f<<28)|(0x7f<<14
b5580 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 )|(0x7f); */.
b5590 20 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c /* b &= (0x7f<<
b55a0 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 14)|(0x7f); */.
b55b0 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 b = b<<7;.
b55c0 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d a |= b;. s =
b55d0 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d s>>18;. *v =
b55e0 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 ((u64)s)<<32 |
b55f0 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 35 3b a;. return 5;
b5600 0a 20 20 7d 0a 0a 20 20 2f 2a 20 32 3a 73 61 76 . }.. /* 2:sav
b5610 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70 e off p0<<21 | p
b5620 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 1<<14 | p2<<7 |
b5630 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 p3 (masked) */.
b5640 20 73 20 3d 20 73 3c 3c 37 3b 0a 20 20 73 20 7c s = s<<7;. s |
b5650 3d 20 62 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c = b;. /* s: p0<
b5660 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 <21 | p1<<14 | p
b5670 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 2<<7 | p3 (maske
b5680 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 d) */.. p++;.
b5690 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c b = b<<14;. b |
b56a0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 = *p;. /* b: p1
b56b0 3c 3c 32 38 20 7c 20 70 33 3c 3c 31 34 20 7c 20 <<28 | p3<<14 |
b56c0 70 35 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p5 (unmasked) */
b56d0 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 . if (!(b&0x80)
b56e0 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 ). {. /* we
b56f0 63 61 6e 20 73 6b 69 70 20 74 68 69 73 20 63 61 can skip this ca
b5700 75 73 65 20 69 74 20 77 61 73 20 28 65 66 66 65 use it was (effe
b5710 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 ctively) done ab
b5720 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 ove in calc'ing
b5730 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d s */. /* b &=
b5740 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 (0x7f<<28)|(0x7
b5750 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a f<<14)|(0x7f); *
b5760 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 /. a &= (0x7f
b5770 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b5780 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 a = a<<7;.
b5790 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 a |= b;. s =
b57a0 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20 s>>18;. *v =
b57b0 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 ((u64)s)<<32 | a
b57c0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a ;. return 6;.
b57d0 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 }.. p++;. a
b57e0 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 = a<<14;. a |=
b57f0 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 32 3c 3c *p;. /* a: p2<<
b5800 32 38 20 7c 20 70 34 3c 3c 31 34 20 7c 20 70 36 28 | p4<<14 | p6
b5810 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b5820 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a if (!(a&0x80)).
b5830 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 {. a &= (0x
b5840 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 1f<<28)|(0x7f<<1
b5850 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
b5860 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
b5870 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62 0x7f);. b = b
b5880 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b <<7;. a |= b;
b5890 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 31 3b 0a . s = s>>11;.
b58a0 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 *v = ((u64)s
b58b0 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 )<<32 | a;. r
b58c0 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 0a 20 20 eturn 7;. }..
b58d0 2f 2a 20 43 53 45 32 20 66 72 6f 6d 20 62 65 6c /* CSE2 from bel
b58e0 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 ow */. a &= (0x
b58f0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b5900 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c p++;. b = b<<
b5910 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 14;. b |= *p;.
b5920 20 2f 2a 20 62 3a 20 70 33 3c 3c 32 38 20 7c 20 /* b: p3<<28 |
b5930 70 35 3c 3c 31 34 20 7c 20 70 37 20 28 75 6e 6d p5<<14 | p7 (unm
b5940 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b5950 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(b&0x80)). {.
b5960 20 20 20 62 20 26 3d 20 28 30 78 31 66 3c 3c 32 b &= (0x1f<<2
b5970 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
b5980 78 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76 x7f);. /* mov
b5990 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 ed CSE2 up */.
b59a0 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c /* a &= (0x7f<
b59b0 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a <14)|(0x7f); */.
b59c0 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 a = a<<7;.
b59d0 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 a |= b;. s
b59e0 3d 20 73 3e 3e 34 3b 0a 20 20 20 20 2a 76 20 3d = s>>4;. *v =
b59f0 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 ((u64)s)<<32 |
b5a00 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 38 3b a;. return 8;
b5a10 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 . }.. p++;. a
b5a20 20 3d 20 61 3c 3c 31 35 3b 0a 20 20 61 20 7c 3d = a<<15;. a |=
b5a30 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 34 3c *p;. /* a: p4<
b5a40 3c 32 39 20 7c 20 70 36 3c 3c 31 35 20 7c 20 70 <29 | p6<<15 | p
b5a50 38 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 8 (unmasked) */.
b5a60 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 . /* moved CSE2
b5a70 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d up */. /* a &=
b5a80 20 28 30 78 37 66 3c 3c 32 39 29 7c 28 30 78 37 (0x7f<<29)|(0x7
b5a90 66 3c 3c 31 35 29 7c 28 30 78 66 66 29 3b 20 2a f<<15)|(0xff); *
b5aa0 2f 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c /. b &= (0x7f<<
b5ab0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 62 20 14)|(0x7f);. b
b5ac0 3d 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20 62 = b<<8;. a |= b
b5ad0 3b 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a 20 ;.. s = s<<4;.
b5ae0 20 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62 20 b = p[-4];. b
b5af0 26 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20 62 &= 0x7f;. b = b
b5b00 3e 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a >>3;. s |= b;..
b5b10 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c *v = ((u64)s)<
b5b20 3c 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74 75 <32 | a;.. retu
b5b30 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn 9;.}../*.** R
b5b40 65 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72 ead a 32-bit var
b5b50 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 iable-length int
b5b60 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 eger from memory
b5b70 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30 starting at p[0
b5b80 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 ]..** Return the
b5b90 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
b5ba0 20 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75 read. The valu
b5bb0 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a e is stored in *
b5bc0 76 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 v..**.** If the
b5bd0 76 61 72 69 6e 74 20 73 74 6f 72 65 64 20 69 6e varint stored in
b5be0 20 70 5b 30 5d 20 69 73 20 6c 61 72 67 65 72 20 p[0] is larger
b5bf0 74 68 61 6e 20 63 61 6e 20 66 69 74 20 69 6e 20 than can fit in
b5c00 61 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e 65 a 32-bit unsigne
b5c10 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74 68 d.** integer, th
b5c20 65 6e 20 73 65 74 20 2a 76 20 74 6f 20 30 78 66 en set *v to 0xf
b5c30 66 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2a 20 41 fffffff..**.** A
b5c40 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 MACRO version,
b5c50 67 65 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 getVarint32, is
b5c60 70 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 provided which i
b5c70 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 nlines the .** s
b5c80 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e ingle-byte case.
b5c90 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c All code shoul
b5ca0 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 d use the MACRO
b5cb0 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 version as .** t
b5cc0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 his function ass
b5cd0 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d umes the single-
b5ce0 62 79 74 65 20 63 61 73 65 20 68 61 73 20 61 6c byte case has al
b5cf0 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c ready been handl
b5d00 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
b5d10 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 IVATE u8 sqlite3
b5d20 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 GetVarint32(cons
b5d30 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
b5d40 2a 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 *p, u32 *v){. u
b5d50 33 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 68 32 a,b;.. /* Th
b5d60 65 20 31 2d 62 79 74 65 20 63 61 73 65 2e 20 20 e 1-byte case.
b5d70 4f 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74 Overwhelmingly t
b5d80 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20 he most common.
b5d90 20 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0a Handled inline.
b5da0 20 20 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56 ** by the getV
b5db0 61 72 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a arin32() macro *
b5dc0 2f 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a /. a = *p;. /*
b5dd0 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 a: p0 (unmasked
b5de0 29 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 74 ) */.#ifndef get
b5df0 56 61 72 69 6e 74 33 32 0a 20 20 69 66 20 28 21 Varint32. if (!
b5e00 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (a&0x80)). {.
b5e10 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 /* Values betw
b5e20 65 65 6e 20 30 20 61 6e 64 20 31 32 37 20 2a 2f een 0 and 127 */
b5e30 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 . *v = a;.
b5e40 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 return 1;. }.#
b5e50 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 20 endif.. /* The
b5e60 32 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 2-byte case */.
b5e70 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a p++;. b = *p;.
b5e80 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 /* b: p1 (unma
b5e90 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b5ea0 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (b&0x80)). {.
b5eb0 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77 /* Values betw
b5ec0 65 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38 een 128 and 1638
b5ed0 33 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 78 3 */. a &= 0x
b5ee0 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 7f;. a = a<<7
b5ef0 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 ;. *v = a | b
b5f00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a ;. return 2;.
b5f10 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 2d }.. /* The 3-
b5f20 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 byte case */. p
b5f30 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b ++;. a = a<<14;
b5f40 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
b5f50 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 a: p0<<14 | p2
b5f60 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b5f70 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 if (!(a&0x80)).
b5f80 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 {. /* Values
b5f90 20 62 65 74 77 65 65 6e 20 31 36 33 38 34 20 61 between 16384 a
b5fa0 6e 64 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 20 nd 2097151 */.
b5fb0 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 a &= (0x7f<<14
b5fc0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 )|(0x7f);. b
b5fd0 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d &= 0x7f;. b =
b5fe0 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 b<<7;. *v =
b5ff0 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 a | b;. retur
b6000 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 n 3;. }.. /* A
b6010 20 33 32 2d 62 69 74 20 76 61 72 69 6e 74 20 69 32-bit varint i
b6020 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 s used to store
b6030 73 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e size information
b6040 20 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a 2a in btrees.. **
b6050 20 4f 62 6a 65 63 74 73 20 61 72 65 20 72 61 72 Objects are rar
b6060 65 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20 ely larger than
b6070 32 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 20 2MiB limit of a
b6080 33 2d 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 20 3-byte varint..
b6090 20 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 61 72 ** A 3-byte var
b60a0 69 6e 74 20 69 73 20 73 75 66 66 69 63 69 65 6e int is sufficien
b60b0 74 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 t, for example,
b60c0 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 73 69 to record the si
b60d0 7a 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 34 ze. ** of a 104
b60e0 38 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 6f 8569-byte BLOB o
b60f0 72 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a 20 r string.. **.
b6100 20 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 6f ** We only unro
b6110 6c 6c 20 74 68 65 20 66 69 72 73 74 20 31 2d 2c ll the first 1-,
b6120 20 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 65 2-, and 3- byte
b6130 20 63 61 73 65 73 2e 20 20 54 68 65 20 76 65 72 cases. The ver
b6140 79 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 67 y. ** rare larg
b6150 65 72 20 63 61 73 65 73 20 63 61 6e 20 62 65 20 er cases can be
b6160 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 73 handled by the s
b6170 6c 6f 77 65 72 20 36 34 2d 62 69 74 20 76 61 72 lower 64-bit var
b6180 69 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65 int. ** routine
b6190 2e 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 7b .. */.#if 1. {
b61a0 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 . u64 v64;.
b61b0 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d u8 n;.. p -
b61c0 3d 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c = 2;. n = sql
b61d0 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c ite3GetVarint(p,
b61e0 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 &v64);. asse
b61f0 72 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 20 rt( n>3 && n<=9
b6200 29 3b 0a 20 20 20 20 69 66 28 20 28 76 36 34 20 );. if( (v64
b6210 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 & SQLITE_MAX_U32
b6220 29 21 3d 76 36 34 20 29 7b 0a 20 20 20 20 20 20 )!=v64 ){.
b6230 2a 76 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b *v = 0xffffffff;
b6240 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
b6250 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b *v = (u32)v64;
b6260 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
b6270 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a n n;. }..#else.
b6280 20 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69 /* For followi
b6290 6e 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 6f ng code (kept fo
b62a0 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 63 r historical rec
b62b0 6f 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20 ord only) shows
b62c0 61 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 6e an. ** unrollin
b62d0 67 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e 64 g for the 3- and
b62e0 20 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20 63 4-byte varint c
b62f0 61 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64 65 ases. This code
b6300 20 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c is. ** slightl
b6310 79 20 66 61 73 74 65 72 2c 20 62 75 74 20 69 74 y faster, but it
b6320 20 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72 20 is also larger
b6330 61 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72 20 and much harder
b6340 74 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 20 to test.. */.
b6350 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 p++;. b = b<<14
b6360 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. b |= *p;. /
b6370 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 * b: p1<<14 | p3
b6380 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b6390 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a if (!(b&0x80)).
b63a0 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 {. /* Value
b63b0 73 20 62 65 74 77 65 65 6e 20 32 30 39 37 31 35 s between 209715
b63c0 32 20 61 6e 64 20 32 36 38 34 33 35 34 35 35 20 2 and 268435455
b63d0 2a 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 */. b &= (0x7
b63e0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b63f0 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 a &= (0x7f<<1
b6400 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 4)|(0x7f);. a
b6410 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 = a<<7;. *v
b6420 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 = a | b;. ret
b6430 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b urn 4;. }.. p+
b6440 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a +;. a = a<<14;.
b6450 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 a |= *p;. /*
b6460 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c a: p0<<28 | p2<<
b6470 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 14 | p4 (unmaske
b6480 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 d) */. if (!(a&
b6490 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 0x80)). {. /
b64a0 2a 20 57 61 6c 75 65 73 20 20 62 65 74 77 65 65 * Walues betwee
b64b0 6e 20 32 36 38 34 33 35 34 35 36 20 61 6e 64 20 n 268435456 and
b64c0 33 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a 20 34359738367 */.
b64d0 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32 a &= (0x1f<<2
b64e0 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 8)|(0x7f<<14)|(0
b64f0 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28 x7f);. b &= (
b6500 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 0x1f<<28)|(0x7f<
b6510 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 <14)|(0x7f);.
b6520 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a b = b<<7;. *
b6530 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 v = a | b;. r
b6540 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 eturn 5;. }..
b6550 2f 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72 /* We can only r
b6560 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 20 each this point
b6570 77 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20 63 when reading a c
b6580 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 0a orrupt database.
b6590 20 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74 ** file. In t
b65a0 68 61 74 20 63 61 73 65 20 77 65 20 61 72 65 20 hat case we are
b65b0 6e 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72 79 not in any hurry
b65c0 2e 20 20 55 73 65 20 74 68 65 20 28 72 65 6c 61 . Use the (rela
b65d0 74 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77 tively. ** slow
b65e0 29 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 ) general-purpos
b65f0 65 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 e sqlite3GetVari
b6600 6e 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20 nt() routine to
b6610 65 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a 2a extract the. **
b6620 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20 value. */. {.
b6630 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20 u64 v64;.
b6640 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 u8 n;.. p -=
b6650 34 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 4;. n = sqlit
b6660 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26 e3GetVarint(p, &
b6670 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74 v64);. assert
b6680 28 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b ( n>5 && n<=9 );
b6690 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 . *v = (u32)v
b66a0 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 64;. return n
b66b0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a ;. }.#endif.}..
b66c0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
b66d0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
b66e0 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e 65 that will be ne
b66f0 65 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 eded to store th
b6700 65 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69 e given.** 64-bi
b6710 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 t integer..*/.SQ
b6720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b6730 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 sqlite3VarintLe
b6740 6e 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 n(u64 v){. int
b6750 69 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 i = 0;. do{.
b6760 20 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 i++;. v >>=
b6770 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 7;. }while( v!=
b6780 30 20 26 26 20 41 4c 57 41 59 53 28 69 3c 39 29 0 && ALWAYS(i<9)
b6790 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a );. return i;.
b67a0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f }.../*.** Read o
b67b0 72 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d 62 r write a four-b
b67c0 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
b67d0 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f nteger value..*/
b67e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b67f0 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 62 u32 sqlite3Get4b
b6800 79 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 29 yte(const u8 *p)
b6810 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d {. return (p[0]
b6820 3c 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 <<24) | (p[1]<<1
b6830 36 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 6) | (p[2]<<8) |
b6840 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f p[3];.}.SQLITE_
b6850 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
b6860 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e 73 ite3Put4byte(uns
b6870 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 igned char *p, u
b6880 33 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 32 v){. p[0] =
b6890 28 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20 70 (u8)(v>>24);. p
b68a0 5b 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31 36 [1] = (u8)(v>>16
b68b0 29 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38 29 );. p[2] = (u8)
b68c0 28 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 3d (v>>8);. p[3] =
b68d0 20 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 (u8)v;.}....#if
b68e0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
b68f0 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 _OMIT_BLOB_LITER
b6900 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 AL) || defined(S
b6910 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 QLITE_HAS_CODEC)
b6920 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 ./*.** Translate
b6930 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f a single byte o
b6940 66 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e f Hex into an in
b6950 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72 teger..** This r
b6960 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b outine only work
b6970 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73 s if h really is
b6980 20 61 20 76 61 6c 69 64 20 68 65 78 61 64 65 63 a valid hexadec
b6990 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 imal.** characte
b69a0 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46 r: 0..9a..fA..F
b69b0 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 68 65 .*/.static u8 he
b69c0 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20 xToInt(int h){.
b69d0 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30 27 assert( (h>='0'
b69e0 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20 && h<='9') ||
b69f0 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66 (h>='a' && h<='f
b6a00 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26 ') || (h>='A' &
b6a10 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66 & h<='F') );.#if
b6a20 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 def SQLITE_ASCII
b6a30 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e . h += 9*(1&(h>
b6a40 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 >6));.#endif.#if
b6a50 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 def SQLITE_EBCDI
b6a60 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28 C. h += 9*(1&~(
b6a70 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20 h>>4));.#endif.
b6a80 20 72 65 74 75 72 6e 20 28 75 38 29 28 68 20 26 return (u8)(h &
b6a90 20 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 0xf);.}.#endif
b6aa0 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* !SQLITE_OMIT_
b6ab0 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 BLOB_LITERAL ||
b6ac0 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
b6ad0 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 */..#if !define
b6ae0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c d(SQLITE_OMIT_BL
b6af0 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 OB_LITERAL) || d
b6b00 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 efined(SQLITE_HA
b6b10 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43 S_CODEC)./*.** C
b6b20 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69 onvert a BLOB li
b6b30 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72 teral of the for
b6b40 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 69 6e m "x'hhhhhh'" in
b6b50 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a to its binary.**
b6b60 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20 value. Return
b6b70 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 a pointer to its
b6b80 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20 binary value.
b6b90 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
b6ba0 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75 e.** binary valu
b6bb0 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 e has been obtai
b6bc0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 ned from malloc
b6bd0 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 and must be free
b6be0 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c d by.** the call
b6bf0 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a ing routine..*/.
b6c00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
b6c10 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54 oid *sqlite3HexT
b6c20 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64 oBlob(sqlite3 *d
b6c30 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a b, const char *z
b6c40 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 , int n){. char
b6c50 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 69 *zBlob;. int i
b6c60 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 68 ;.. zBlob = (ch
b6c70 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 ar *)sqlite3DbMa
b6c80 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 20 llocRaw(db, n/2
b6c90 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 69 + 1);. n--;. i
b6ca0 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 20 f( zBlob ){.
b6cb0 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b for(i=0; i<n; i+
b6cc0 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 =2){. zBlob
b6cd0 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 6e [i/2] = (hexToIn
b6ce0 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 65 t(z[i])<<4) | he
b6cf0 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a xToInt(z[i+1]);.
b6d00 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 5b }. zBlob[
b6d10 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 i/2] = 0;. }.
b6d20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a return zBlob;.}.
b6d30 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 #endif /* !SQLIT
b6d40 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
b6d50 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 RAL || SQLITE_HA
b6d60 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a S_CODEC */.../*.
b6d70 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 71 ** Change the sq
b6d80 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d 20 lite.magic from
b6d90 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
b6da0 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 N to SQLITE_MAGI
b6db0 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 72 C_BUSY..** Retur
b6dc0 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d n an error (non-
b6dd0 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67 zero) if the mag
b6de0 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54 ic was not SQLIT
b6df0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 E_MAGIC_OPEN.**
b6e00 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
b6e10 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a e is called..**.
b6e20 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
b6e30 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 65 is called when e
b6e40 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 74 ntering an SQLit
b6e50 65 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c 49 e API. The SQLI
b6e60 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a TE_MAGIC_OPEN.**
b6e70 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 value indicates
b6e80 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 that the databa
b6e90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 se connection pa
b6ea0 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 41 50 ssed into the AP
b6eb0 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e 64 I is.** open and
b6ec0 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 73 is not being us
b6ed0 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68 ed by another th
b6ee0 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e 67 69 read. By changi
b6ef0 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 ng the value.**
b6f00 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f to SQLITE_MAGIC_
b6f10 42 55 53 59 20 77 65 20 69 6e 64 69 63 61 74 65 BUSY we indicate
b6f20 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63 that the connec
b6f30 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e 0a tion is in use..
b6f40 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 ** sqlite3Safety
b6f50 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c Off() below will
b6f60 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 change the valu
b6f70 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 e back to SQLITE
b6f80 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 _MAGIC_OPEN.** w
b6f90 68 65 6e 20 74 68 65 20 41 50 49 20 65 78 69 74 hen the API exit
b6fa0 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 s. .**.** This r
b6fb0 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74 74 65 outine is a atte
b6fc0 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20 69 66 mpt to detect if
b6fd0 20 74 77 6f 20 74 68 72 65 61 64 73 20 75 73 65 two threads use
b6fe0 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 6c the.** same sql
b6ff0 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 20 ite* pointer at
b7000 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 the same time.
b7010 54 68 65 72 65 20 69 73 20 61 20 72 61 63 65 20 There is a race
b7020 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 6f .** condition so
b7030 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 it is possible
b7040 74 68 61 74 20 74 68 65 20 65 72 72 6f 72 20 69 that the error i
b7050 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64 2e 0a s not detected..
b7060 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 20 74 ** But usually t
b7070 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c 20 he problem will
b7080 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20 72 65 be seen. The re
b7090 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e 0a sult will be an.
b70a0 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68 20 63 ** error which c
b70b0 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 an be used to de
b70c0 62 75 67 20 74 68 65 20 61 70 70 6c 69 63 61 74 bug the applicat
b70d0 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 75 ion that is.** u
b70e0 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 6f sing SQLite inco
b70f0 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 rrectly..**.** T
b7100 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49 66 20 icket #202: If
b7110 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f 74 db->magic is not
b7120 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 61 a valid open va
b7130 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65 20 6e lue, take care n
b7140 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 ot.** to modify
b7150 74 68 65 20 64 62 20 73 74 72 75 63 74 75 72 65 the db structure
b7160 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f 75 at all. It cou
b7170 6c 64 20 62 65 20 74 68 61 74 20 64 62 20 69 73 ld be that db is
b7180 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 6e a stale.** poin
b7190 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 ter. In other w
b71a0 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 62 ords, it could b
b71b0 65 20 74 68 61 74 20 74 68 65 72 65 20 68 61 73 e that there has
b71c0 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a 2a been a prior.**
b71d0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
b71e0 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 64 _close(db) and d
b71f0 62 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c b has been deall
b7200 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77 65 20 ocated. And we
b7210 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 do.** not want t
b7220 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64 65 61 o write into dea
b7230 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e llocated memory.
b7240 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
b7250 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 E_DEBUG.SQLITE_P
b7260 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
b7270 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 e3SafetyOn(sqlit
b7280 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 e3 *db){. if( d
b7290 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 b->magic==SQLITE
b72a0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a 20 _MAGIC_OPEN ){.
b72b0 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 db->magic = S
b72c0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 QLITE_MAGIC_BUSY
b72d0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 ;. assert( sq
b72e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
b72f0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 (db->mutex) );.
b7300 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
b7310 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 67 else if( db->mag
b7320 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 ic==SQLITE_MAGIC
b7330 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d _BUSY ){. db-
b7340 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f >magic = SQLITE_
b7350 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 MAGIC_ERROR;.
b7360 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 db->u1.isInterr
b7370 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 upted = 1;. }.
b7380 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e return 1;.}.#en
b7390 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 dif../*.** Chang
b73a0 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d e the magic from
b73b0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 SQLITE_MAGIC_BU
b73c0 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 SY to SQLITE_MAG
b73d0 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75 IC_OPEN..** Retu
b73e0 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e rn an error (non
b73f0 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 -zero) if the ma
b7400 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 gic was not SQLI
b7410 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a TE_MAGIC_BUSY.**
b7420 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
b7430 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f ne is called..*/
b7440 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
b7450 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
b7460 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
b7470 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 afetyOff(sqlite3
b7480 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d *db){. if( db-
b7490 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d >magic==SQLITE_M
b74a0 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 AGIC_BUSY ){.
b74b0 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c db->magic = SQL
b74c0 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a ITE_MAGIC_OPEN;.
b74d0 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
b74e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
b74f0 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 b->mutex) );.
b7500 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
b7510 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 se{. db->magi
b7520 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 c = SQLITE_MAGIC
b7530 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e _ERROR;. db->
b7540 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 u1.isInterrupted
b7550 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e = 1;. return
b7560 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 1;. }.}.#endif
b7570 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f ../*.** Check to
b7580 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 make sure we ha
b7590 76 65 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f ve a valid db po
b75a0 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73 inter. This tes
b75b0 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c t is not.** fool
b75c0 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65 proof but it doe
b75d0 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d s provide some m
b75e0 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63 easure of protec
b75f0 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 tion against.**
b7600 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e misuse of the in
b7610 74 65 72 66 61 63 65 20 73 75 63 68 20 61 73 20 terface such as
b7620 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f passing in db po
b7630 69 6e 74 65 72 73 20 74 68 61 74 20 61 72 65 0a inters that are.
b7640 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68 ** NULL or which
b7650 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 76 69 have been previ
b7660 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49 ously closed. I
b7670 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 f this routine r
b7680 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 6d eturns.** 1 it m
b7690 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 62 eans that the db
b76a0 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69 pointer is vali
b76b0 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 73 68 d and 0 if it sh
b76c0 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 64 ould not be.** d
b76d0 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72 20 ereferenced for
b76e0 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 65 any reason. The
b76f0 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
b7700 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 0a n should invoke.
b7710 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 ** SQLITE_MISUSE
b7720 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a immediately..**
b7730 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 .** sqlite3Safet
b7740 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 69 yCheckOk() requi
b7750 72 65 73 20 74 68 61 74 20 74 68 65 20 64 62 20 res that the db
b7760 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c 69 64 pointer be valid
b7770 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 71 for.** use. sq
b7780 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b lite3SafetyCheck
b7790 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f 77 SickOrOk() allow
b77a0 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72 20 74 s a db pointer t
b77b0 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a 2a hat failed to.**
b77c0 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 61 open properly a
b77d0 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20 66 6f nd is not fit fo
b77e0 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20 62 75 r general use bu
b77f0 74 20 77 68 69 63 68 20 63 61 6e 20 62 65 0a 2a t which can be.*
b7800 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61 72 67 * used as an arg
b7810 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 ument to sqlite3
b7820 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73 71 6c _errmsg() or sql
b7830 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a 2f ite3_close()..*/
b7840 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b7850 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 int sqlite3Safet
b7860 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 yCheckOk(sqlite3
b7870 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 *db){. u32 mag
b7880 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 ic;. if( db==0
b7890 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 61 ) return 0;. ma
b78a0 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b gic = db->magic;
b78b0 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 . if( magic!=SQ
b78c0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 LITE_MAGIC_OPEN
b78d0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
b78e0 45 42 55 47 0a 20 20 20 20 20 26 26 20 6d 61 67 EBUG. && mag
b78f0 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 ic!=SQLITE_MAGIC
b7900 5f 42 55 53 59 0a 23 65 6e 64 69 66 0a 20 20 29 _BUSY.#endif. )
b7910 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
b7920 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
b7930 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 51 4c urn 1;. }.}.SQL
b7940 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b7950 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 sqlite3SafetyChe
b7960 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74 ckSickOrOk(sqlit
b7970 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d e3 *db){. u32 m
b7980 61 67 69 63 3b 0a 20 20 6d 61 67 69 63 20 3d 20 agic;. magic =
b7990 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28 db->magic;. if(
b79a0 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d magic!=SQLITE_M
b79b0 41 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20 AGIC_SICK &&.
b79c0 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 magic!=SQLITE
b79d0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20 _MAGIC_OPEN &&.
b79e0 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 magic!=SQLI
b79f0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20 TE_MAGIC_BUSY )
b7a00 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 return 0;. retu
b7a10 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a rn 1;.}../******
b7a20 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
b7a30 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a util.c *********
b7a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7a60 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
b7a70 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
b7a80 69 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a ile hash.c *****
b7a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7ab0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
b7ac0 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 001 September 22
b7ad0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
b7ae0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
b7af0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
b7b00 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
b7b10 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
b7b20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
b7b30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
b7b40 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
b7b50 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
b7b60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
b7b70 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
b7b80 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
b7b90 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
b7ba0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
b7bb0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
b7bc0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
b7bd0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
b7be0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
b7bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b7c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
b7c30 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 ** This is the i
b7c40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
b7c50 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 generic hash-ta
b7c60 62 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20 bles.** used in
b7c70 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 SQLite..**.** $I
b7c80 64 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 38 d: hash.c,v 1.38
b7c90 20 32 30 30 39 2f 30 35 2f 30 39 20 32 33 3a 32 2009/05/09 23:2
b7ca0 39 3a 31 32 20 64 72 68 20 45 78 70 20 24 0a 2a 9:12 drh Exp $.*
b7cb0 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 /../* Turn bulk
b7cc0 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 memory into a ha
b7cd0 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 sh table object
b7ce0 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 by initializing
b7cf0 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 the.** fields of
b7d00 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74 the Hash struct
b7d10 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 ure..**.** "pNew
b7d20 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 " is a pointer t
b7d30 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 o the hash table
b7d40 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69 that is to be i
b7d50 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 nitialized..*/.S
b7d60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
b7d70 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e id sqlite3HashIn
b7d80 69 74 28 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a it(Hash *pNew){.
b7d90 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d assert( pNew!=
b7da0 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 0 );. pNew->fir
b7db0 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e st = 0;. pNew->
b7dc0 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 count = 0;. pNe
b7dd0 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 w->htsize = 0;.
b7de0 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d pNew->ht = 0;.}
b7df0 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 ../* Remove all
b7e00 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68 entries from a h
b7e10 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c ash table. Recl
b7e20 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a aim all memory..
b7e30 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 ** Call this rou
b7e40 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61 tine to delete a
b7e50 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74 hash table or t
b7e60 6f 20 72 65 73 65 74 20 61 20 68 61 73 68 20 74 o reset a hash t
b7e70 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 able.** to the e
b7e80 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 mpty state..*/.S
b7e90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
b7ea0 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c id sqlite3HashCl
b7eb0 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 ear(Hash *pH){.
b7ec0 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b HashElem *elem;
b7ed0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
b7ee0 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c looping over all
b7ef0 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 elements of the
b7f00 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 table */.. ass
b7f10 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 ert( pH!=0 );.
b7f20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 elem = pH->first
b7f30 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 ;. pH->first =
b7f40 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 0;. sqlite3_fre
b7f50 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d e(pH->ht);. pH-
b7f60 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 >ht = 0;. pH->h
b7f70 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 tsize = 0;. whi
b7f80 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 le( elem ){.
b7f90 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 HashElem *next_e
b7fa0 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 lem = elem->next
b7fb0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
b7fc0 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c ee(elem);. el
b7fd0 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a em = next_elem;.
b7fe0 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 }. pH->count
b7ff0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 = 0;.}../*.** Th
b8000 65 20 68 61 73 68 69 6e 67 20 66 75 6e 63 74 69 e hashing functi
b8010 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e on..*/.static un
b8020 73 69 67 6e 65 64 20 69 6e 74 20 73 74 72 48 61 signed int strHa
b8030 73 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a sh(const char *z
b8040 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 , int nKey){. i
b8050 6e 74 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 nt h = 0;. asse
b8060 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 rt( nKey>=0 );.
b8070 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 while( nKey > 0
b8080 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c ){. h = (h<
b8090 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 <3) ^ h ^ sqlite
b80a0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 3UpperToLower[(u
b80b0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b nsigned char)*z+
b80c0 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a +];. nKey--;.
b80d0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a }. return h;.
b80e0 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 }.../* Link pNew
b80f0 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 element into th
b8100 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e e hash table pH.
b8110 20 20 49 66 20 70 45 6e 74 72 79 21 3d 30 20 74 If pEntry!=0 t
b8120 68 65 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 hen also.** inse
b8130 72 74 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 rt pNew into the
b8140 20 70 45 6e 74 72 79 20 68 61 73 68 20 62 75 63 pEntry hash buc
b8150 6b 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ket..*/.static v
b8160 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e oid insertElemen
b8170 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20 t(. Hash *pH,
b8180 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
b8190 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 he complete hash
b81a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 table */. stru
b81b0 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 ct _ht *pEntry,
b81c0 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 /* The entry
b81d0 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 into which pNew
b81e0 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 is inserted */.
b81f0 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 HashElem *pNew
b8200 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
b8210 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 lement to be ins
b8220 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 erted */.){. Ha
b8230 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 shElem *pHead;
b8240 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c /* First el
b8250 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e ement already in
b8260 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 pEntry */. if(
b8270 20 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 pEntry ){. p
b8280 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 Head = pEntry->c
b8290 6f 75 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 ount ? pEntry->c
b82a0 68 61 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 hain : 0;. pE
b82b0 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 ntry->count++;.
b82c0 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e pEntry->chain
b82d0 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 = pNew;. }else
b82e0 7b 0a 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b {. pHead = 0;
b82f0 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 65 61 64 . }. if( pHead
b8300 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 ){. pNew->ne
b8310 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 xt = pHead;.
b8320 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 pNew->prev = pHe
b8330 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 ad->prev;. if
b8340 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b ( pHead->prev ){
b8350 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 pHead->prev->ne
b8360 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 xt = pNew; }.
b8370 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 else
b8380 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 { pH->first =
b8390 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 pNew; }. pHea
b83a0 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a d->prev = pNew;.
b83b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 }else{. pNe
b83c0 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 w->next = pH->fi
b83d0 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d rst;. if( pH-
b83e0 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 >first ){ pH->fi
b83f0 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 rst->prev = pNew
b8400 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 ; }. pNew->pr
b8410 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e ev = 0;. pH->
b8420 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 first = pNew;.
b8430 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 }.}.../* Resize
b8440 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 the hash table s
b8450 6f 20 74 68 61 74 20 69 74 20 63 61 6e 74 61 69 o that it cantai
b8460 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 ns "new_size" bu
b8470 63 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ckets..**.** The
b8480 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68 hash table migh
b8490 74 20 66 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 t fail to resize
b84a0 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c if sqlite3_mall
b84b0 6f 63 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a oc() fails or.**
b84c0 20 69 66 20 74 68 65 20 6e 65 77 20 73 69 7a 65 if the new size
b84d0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
b84e0 74 68 65 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a the prior size..
b84f0 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
b8500 66 20 74 68 65 20 72 65 73 69 7a 65 20 6f 63 63 f the resize occ
b8510 75 72 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 urs and false if
b8520 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 not..*/.static
b8530 69 6e 74 20 72 65 68 61 73 68 28 48 61 73 68 20 int rehash(Hash
b8540 2a 70 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e *pH, unsigned in
b8550 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 t new_size){. s
b8560 74 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 truct _ht *new_h
b8570 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
b8580 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 The new hash ta
b8590 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 ble */. HashEle
b85a0 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 m *elem, *next_e
b85b0 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c lem; /* For l
b85c0 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 ooping over exis
b85d0 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f ting elements */
b85e0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c ..#if SQLITE_MAL
b85f0 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 LOC_SOFT_LIMIT>0
b8600 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a . if( new_size*
b8610 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 sizeof(struct _h
b8620 74 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 t)>SQLITE_MALLOC
b8630 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 _SOFT_LIMIT ){.
b8640 20 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 new_size = SQ
b8650 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 LITE_MALLOC_SOFT
b8660 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 _LIMIT/sizeof(st
b8670 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 ruct _ht);. }.
b8680 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 if( new_size==p
b8690 48 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75 H->htsize ) retu
b86a0 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 rn 0;.#endif..
b86b0 2f 2a 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79 /* The inability
b86c0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 to allocates sp
b86d0 61 63 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 ace for a larger
b86e0 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 0a 20 hash table is.
b86f0 20 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 ** a performanc
b8700 65 20 68 69 74 20 62 75 74 20 69 74 20 69 73 20 e hit but it is
b8710 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72 72 6f not a fatal erro
b8720 72 2e 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a r. So mark the.
b8730 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 ** allocation
b8740 61 73 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a as a benign.. *
b8750 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e /. sqlite3Begin
b8760 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
b8770 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 new_ht = (stru
b8780 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 ct _ht *)sqlite3
b8790 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 Malloc( new_size
b87a0 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f *sizeof(struct _
b87b0 68 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ht) );. sqlite3
b87c0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
b87d0 29 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 );.. if( new_ht
b87e0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
b87f0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
b8800 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 H->ht);. pH->ht
b8810 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d = new_ht;. pH-
b8820 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 >htsize = new_si
b8830 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c ze = sqlite3Mall
b8840 6f 63 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 ocSize(new_ht)/s
b8850 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 izeof(struct _ht
b8860 29 3b 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f );. memset(new_
b8870 68 74 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a ht, 0, new_size*
b8880 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 sizeof(struct _h
b8890 74 29 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d t));. for(elem=
b88a0 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 pH->first, pH->f
b88b0 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c irst=0; elem; el
b88c0 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b em = next_elem){
b88d0 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e . unsigned in
b88e0 74 20 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c t h = strHash(el
b88f0 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e em->pKey, elem->
b8900 6e 4b 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 nKey) % new_size
b8910 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 ;. next_elem
b8920 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 = elem->next;.
b8930 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 insertElement(
b8940 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 pH, &new_ht[h],
b8950 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 elem);. }. ret
b8960 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 urn 1;.}../* Thi
b8970 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 s function (for
b8980 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c internal use onl
b8990 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c y) locates an el
b89a0 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 ement in an.** h
b89b0 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d ash table that m
b89c0 61 74 63 68 65 73 20 74 68 65 20 67 69 76 65 6e atches the given
b89d0 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 key. The hash
b89e0 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 for this key has
b89f0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e .** already been
b8a00 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 computed and is
b8a10 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 34 passed as the 4
b8a20 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f th parameter..*/
b8a30 0a 73 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d .static HashElem
b8a40 20 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 *findElementGiv
b8a50 65 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 enHash(. const
b8a60 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a Hash *pH, /*
b8a70 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65 The pH to be se
b8a80 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 arched */. cons
b8a90 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 t char *pKey,
b8aa0 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72 /* The key we ar
b8ab0 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 e searching for
b8ac0 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 */. int nKey,
b8ad0 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
b8ae0 73 20 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f s in key (not co
b8af0 75 6e 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d unting zero term
b8b00 69 6e 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 inator) */. uns
b8b10 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20 igned int h
b8b20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 /* The hash for
b8b30 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b this key. */.){
b8b40 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 . HashElem *ele
b8b50 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
b8b60 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f /* Used to loo
b8b70 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 p thru the eleme
b8b80 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 nt list */. int
b8b90 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 count;
b8ba0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
b8bb0 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 umber of element
b8bc0 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a s left to test *
b8bd0 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 /.. if( pH->ht
b8be0 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68 ){. struct _h
b8bf0 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d t *pEntry = &pH-
b8c00 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d >ht[h];. elem
b8c10 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e = pEntry->chain
b8c20 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 ;. count = pE
b8c30 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d ntry->count;. }
b8c40 65 6c 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d else{. elem =
b8c50 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 pH->first;.
b8c60 63 6f 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e count = pH->coun
b8c70 74 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 t;. }. while(
b8c80 63 6f 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 count-- && ALWAY
b8c90 53 28 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 S(elem) ){. i
b8ca0 66 28 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e f( elem->nKey==n
b8cb0 4b 65 79 20 26 26 20 73 71 6c 69 74 65 33 53 74 Key && sqlite3St
b8cc0 72 4e 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 rNICmp(elem->pKe
b8cd0 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 y,pKey,nKey)==0
b8ce0 29 7b 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){ . return
b8cf0 20 65 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 elem;. }.
b8d00 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 elem = elem->ne
b8d10 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e xt;. }. return
b8d20 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 0;.}../* Remove
b8d30 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 a single entry
b8d40 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 from the hash ta
b8d50 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e ble given a poin
b8d60 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 ter to that.** e
b8d70 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 lement and a has
b8d80 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 h on the element
b8d90 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 's key..*/.stati
b8da0 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 c void removeEle
b8db0 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 mentGivenHash(.
b8dc0 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 Hash *pH,
b8dd0 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e /* The pH con
b8de0 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a taining "elem" *
b8df0 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c /. HashElem* el
b8e00 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 em, /* The ele
b8e10 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 ment to be remov
b8e20 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a ed from the pH *
b8e30 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
b8e40 20 68 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61 h /* Hash va
b8e50 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d lue for the elem
b8e60 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 ent */.){. stru
b8e70 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a ct _ht *pEntry;.
b8e80 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 if( elem->prev
b8e90 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 ){. elem->pr
b8ea0 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d ev->next = elem-
b8eb0 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b >next; . }else{
b8ec0 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d . pH->first =
b8ed0 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d elem->next;. }
b8ee0 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 . if( elem->nex
b8ef0 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e t ){. elem->n
b8f00 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d ext->prev = elem
b8f10 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 ->prev;. }. if
b8f20 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 ( pH->ht ){.
b8f30 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 pEntry = &pH->ht
b8f40 5b 68 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 6e [h];. if( pEn
b8f50 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d try->chain==elem
b8f60 20 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 ){. pEntry
b8f70 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e ->chain = elem->
b8f80 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 next;. }.
b8f90 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b pEntry->count--;
b8fa0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 6e . assert( pEn
b8fb0 74 72 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b try->count>=0 );
b8fc0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 . }. sqlite3_f
b8fd0 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 ree( elem );. p
b8fe0 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 H->count--;. if
b8ff0 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 ( pH->count<=0 )
b9000 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 {. assert( pH
b9010 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 ->first==0 );.
b9020 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f assert( pH->co
b9030 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 unt==0 );. sq
b9040 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 70 lite3HashClear(p
b9050 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 H);. }.}../* At
b9060 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 tempt to locate
b9070 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 an element of th
b9080 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 e hash table pH
b9090 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 with a key.** th
b90a0 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c at matches pKey,
b90b0 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 nKey. Return th
b90c0 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 e data for this
b90d0 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 element if it is
b90e0 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 .** found, or NU
b90f0 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e LL if there is n
b9100 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 o match..*/.SQLI
b9110 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
b9120 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 *sqlite3HashFind
b9130 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c (const Hash *pH,
b9140 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 const char *pKe
b9150 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 y, int nKey){.
b9160 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 HashElem *elem;
b9170 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e /* The elemen
b9180 74 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b t that matches k
b9190 65 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ey */. unsigned
b91a0 20 69 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 int h; /* A
b91b0 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a hash on key */..
b91c0 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 assert( pH!=0
b91d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 );. assert( pKe
b91e0 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 y!=0 );. assert
b91f0 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 ( nKey>=0 );. i
b9200 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 f( pH->ht ){.
b9210 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65 h = strHash(pKe
b9220 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 y, nKey) % pH->h
b9230 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a tsize;. }else{.
b9240 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 h = 0;. }.
b9250 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d elem = findElem
b9260 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c entGivenHash(pH,
b9270 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b pKey, nKey, h);
b9280 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f . return elem ?
b9290 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b elem->data : 0;
b92a0 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e .}../* Insert an
b92b0 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 element into th
b92c0 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e e hash table pH.
b92d0 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 The key is pKe
b92e0 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 y,nKey.** and th
b92f0 65 20 64 61 74 61 20 69 73 20 22 64 61 74 61 22 e data is "data"
b9300 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c ..**.** If no el
b9310 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 ement exists wit
b9320 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 h a matching key
b9330 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 , then a new.**
b9340 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 element is creat
b9350 65 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 ed and NULL is r
b9360 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 eturned..**.** I
b9370 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e f another elemen
b9380 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 t already exists
b9390 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b with the same k
b93a0 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 ey, then the.**
b93b0 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 new data replace
b93c0 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 s the old data a
b93d0 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 nd the old data
b93e0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 is returned..**
b93f0 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 The key is not c
b9400 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e opied in this in
b9410 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 stance. If a ma
b9420 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e lloc fails, then
b9430 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 .** the new data
b9440 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
b9450 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
b9460 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a is unchanged..**
b9470 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 .** If the "data
b9480 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 " parameter to t
b9490 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
b94a0 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a NULL, then the.*
b94b0 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 * element corres
b94c0 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 ponding to "key"
b94d0 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d is removed from
b94e0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e the hash table.
b94f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b9500 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
b9510 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 HashInsert(Hash
b9520 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *pH, const char
b9530 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c *pKey, int nKey,
b9540 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 void *data){.
b9550 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 unsigned int h;
b9560 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 /* the has
b9570 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 h of the key mod
b9580 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73 ulo hash table s
b9590 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 ize */. HashEle
b95a0 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f m *elem; /
b95b0 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 * Used to loop t
b95c0 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 hru the element
b95d0 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c list */. HashEl
b95e0 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 em *new_elem;
b95f0 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 /* New element a
b9600 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a dded to the pH *
b9610 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 /.. assert( pH!
b9620 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
b9630 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 pKey!=0 );. ass
b9640 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a ert( nKey>=0 );.
b9650 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 if( pH->htsize
b9660 20 29 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 ){. h = strH
b9670 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 ash(pKey, nKey)
b9680 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 % pH->htsize;.
b9690 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 }else{. h = 0
b96a0 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 ;. }. elem = f
b96b0 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 indElementGivenH
b96c0 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 ash(pH,pKey,nKey
b96d0 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 ,h);. if( elem
b96e0 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 ){. void *old
b96f0 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 _data = elem->da
b9700 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 ta;. if( data
b9710 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d ==0 ){. rem
b9720 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 oveElementGivenH
b9730 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a ash(pH,elem,h);.
b9740 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
b9750 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 elem->data = da
b9760 74 61 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e ta;. elem->
b9770 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 pKey = pKey;.
b9780 20 20 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d assert(nKey==
b9790 65 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 elem->nKey);.
b97a0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c }. return ol
b97b0 64 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 d_data;. }. if
b97c0 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75 ( data==0 ) retu
b97d0 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d rn 0;. new_elem
b97e0 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 = (HashElem*)sq
b97f0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a lite3Malloc( siz
b9800 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b eof(HashElem) );
b9810 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d . if( new_elem=
b9820 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 =0 ) return data
b9830 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b ;. new_elem->pK
b9840 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 ey = pKey;. new
b9850 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b _elem->nKey = nK
b9860 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e ey;. new_elem->
b9870 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 data = data;. p
b9880 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 H->count++;. if
b9890 28 20 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 ( pH->count>=10
b98a0 26 26 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 && pH->count > 2
b98b0 2a 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 *pH->htsize ){.
b98c0 20 20 20 69 66 28 20 72 65 68 61 73 68 28 70 48 if( rehash(pH
b98d0 2c 20 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 , pH->count*2) )
b98e0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
b98f0 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a pH->htsize>0 );.
b9900 20 20 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 h = strHas
b9910 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 h(pKey, nKey) %
b9920 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 pH->htsize;.
b9930 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e }. }. if( pH->
b9940 68 74 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 ht ){. insert
b9950 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d Element(pH, &pH-
b9960 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d >ht[h], new_elem
b9970 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
b9980 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 insertElement(pH
b9990 2c 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a , 0, new_elem);.
b99a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
b99b0 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
b99c0 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 ** End of hash.c
b99d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
b99e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b99f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9a00 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
b9a10 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 ** Begin file op
b9a20 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a codes.c ********
b9a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b9a50 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 */./* Automatica
b9a60 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 lly generated.
b9a70 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f Do not edit */./
b9a80 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f * See the mkopco
b9a90 64 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66 dec.awk script f
b9aa0 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 or details. */.#
b9ab0 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
b9ac0 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 TE_OMIT_EXPLAIN)
b9ad0 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 || !defined(NDE
b9ae0 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 BUG) || defined(
b9af0 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c VDBE_PROFILE) ||
b9b00 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
b9b10 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 DEBUG).SQLITE_PR
b9b20 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
b9b30 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e *sqlite3OpcodeN
b9b40 61 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 ame(int i){. sta
b9b50 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
b9b60 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d const azName[] =
b9b70 20 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 { "?",. /*
b9b80 20 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 1 */ "VNext",.
b9b90 20 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22 /* 2 */ "
b9ba0 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 Affinity",.
b9bb0 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d /* 3 */ "Colum
b9bc0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20 n",. /* 4
b9bd0 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a */ "SetCookie",.
b9be0 20 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22 /* 5 */ "
b9bf0 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Seek",. /*
b9c00 20 36 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 22 6 */ "Sequence"
b9c10 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f ,. /* 7 */
b9c20 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 20 "Savepoint",.
b9c30 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 6f /* 8 */ "Ro
b9c40 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 wKey",. /*
b9c50 20 39 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 20 9 */ "SCopy",.
b9c60 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 4f /* 10 */ "O
b9c70 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 penWrite",.
b9c80 2f 2a 20 20 31 31 20 2a 2f 20 22 49 66 22 2c 0a /* 11 */ "If",.
b9c90 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22 /* 12 */ "
b9ca0 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f CollSeq",. /
b9cb0 2a 20 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 65 * 13 */ "OpenRe
b9cc0 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 ad",. /* 14
b9cd0 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20 */ "Expire",.
b9ce0 20 20 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 75 /* 15 */ "Au
b9cf0 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20 toCommit",.
b9d00 2f 2a 20 20 31 36 20 2a 2f 20 22 50 61 67 65 63 /* 16 */ "Pagec
b9d10 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ount",. /*
b9d20 31 37 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 17 */ "Integrity
b9d30 43 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 Ck",. /* 18
b9d40 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20 */ "Sort",.
b9d50 20 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 /* 19 */ "Not"
b9d60 2c 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f ,. /* 20 */
b9d70 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a "Copy",. /*
b9d80 20 20 32 31 20 2a 2f 20 22 54 72 61 63 65 22 2c 21 */ "Trace",
b9d90 0a 20 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 . /* 22 */
b9da0 22 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 "Function",.
b9db0 20 2f 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e 65 /* 23 */ "IfNe
b9dc0 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 g",. /* 24
b9dd0 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20 */ "Noop",.
b9de0 2f 2a 20 20 32 35 20 2a 2f 20 22 50 72 6f 67 72 /* 25 */ "Progr
b9df0 61 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 am",. /* 26
b9e00 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 */ "Return",.
b9e10 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 4e 65 /* 27 */ "Ne
b9e20 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a wRowid",. /*
b9e30 20 20 32 38 20 2a 2f 20 22 56 61 72 69 61 62 6c 28 */ "Variabl
b9e40 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 e",. /* 29
b9e50 2a 2f 20 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 */ "String",.
b9e60 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 52 65 61 /* 30 */ "Rea
b9e70 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 lAffinity",.
b9e80 20 2f 2a 20 20 33 31 20 2a 2f 20 22 56 52 65 6e /* 31 */ "VRen
b9e90 61 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 ame",. /* 3
b9ea0 32 20 2a 2f 20 22 50 61 72 73 65 53 63 68 65 6d 2 */ "ParseSchem
b9eb0 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 33 20 a",. /* 33
b9ec0 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 */ "VOpen",.
b9ed0 20 2f 2a 20 20 33 34 20 2a 2f 20 22 43 6c 6f 73 /* 34 */ "Clos
b9ee0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 e",. /* 35
b9ef0 2a 2f 20 22 43 72 65 61 74 65 49 6e 64 65 78 22 */ "CreateIndex"
b9f00 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f ,. /* 36 */
b9f10 20 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 "IsUnique",.
b9f20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 4e 6f 74 /* 37 */ "Not
b9f30 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 Found",. /*
b9f40 20 33 38 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 38 */ "Int64",.
b9f50 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 /* 39 */ "
b9f60 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 MustBeInt",.
b9f70 20 2f 2a 20 20 34 30 20 2a 2f 20 22 48 61 6c 74 /* 40 */ "Halt
b9f80 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 20 2a ",. /* 41 *
b9f90 2f 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 / "Rowid",.
b9fa0 2f 2a 20 20 34 32 20 2a 2f 20 22 49 64 78 4c 54 /* 42 */ "IdxLT
b9fb0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33 20 2a ",. /* 43 *
b9fc0 2f 20 22 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 / "AddImm",.
b9fd0 20 2f 2a 20 20 34 34 20 2a 2f 20 22 52 6f 77 44 /* 44 */ "RowD
b9fe0 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 ata",. /* 4
b9ff0 35 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 5 */ "MemMax",.
ba000 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4e /* 46 */ "N
ba010 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 otExists",.
ba020 2f 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75 62 /* 47 */ "Gosub
ba030 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a ",. /* 48 *
ba040 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 / "Integer",.
ba050 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 50 72 65 /* 49 */ "Pre
ba060 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 v",. /* 50
ba070 2a 2f 20 22 52 6f 77 53 65 74 52 65 61 64 22 2c */ "RowSetRead",
ba080 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 . /* 51 */
ba090 22 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20 "RowSetAdd",.
ba0a0 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 56 43 6f /* 52 */ "VCo
ba0b0 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 lumn",. /*
ba0c0 35 33 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62 53 */ "CreateTab
ba0d0 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34 le",. /* 54
ba0e0 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 20 */ "Last",.
ba0f0 20 2f 2a 20 20 35 35 20 2a 2f 20 22 53 65 65 6b /* 55 */ "Seek
ba100 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 Le",. /* 56
ba110 20 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22 */ "IncrVacuum"
ba120 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f ,. /* 57 */
ba130 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 "IdxRowid",.
ba140 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 52 65 73 /* 58 */ "Res
ba150 65 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f etCount",. /
ba160 2a 20 20 35 39 20 2a 2f 20 22 59 69 65 6c 64 22 * 59 */ "Yield"
ba170 2c 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 2a 2f ,. /* 60 */
ba180 20 22 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a "DropTrigger",.
ba190 20 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 /* 61 */ "
ba1a0 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 DropIndex",.
ba1b0 20 2f 2a 20 20 36 32 20 2a 2f 20 22 50 61 72 61 /* 62 */ "Para
ba1c0 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 33 20 m",. /* 63
ba1d0 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20 20 */ "IdxGE",.
ba1e0 20 2f 2a 20 20 36 34 20 2a 2f 20 22 49 64 78 44 /* 64 */ "IdxD
ba1f0 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 elete",. /*
ba200 20 36 35 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c 65 */ "Vacuum",
ba210 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 2a 2f 20 . /* 66 */
ba220 22 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 "Or",. /* 6
ba230 37 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 20 20 7 */ "And",.
ba240 20 2f 2a 20 20 36 38 20 2a 2f 20 22 49 66 4e 6f /* 68 */ "IfNo
ba250 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 39 20 t",. /* 69
ba260 2a 2f 20 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a */ "DropTable",.
ba270 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 22 /* 70 */ "
ba280 53 65 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a SeekLt",. /*
ba290 20 20 37 31 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 71 */ "IsNull"
ba2a0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32 20 2a 2f ,. /* 72 */
ba2b0 20 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 "NotNull",.
ba2c0 20 2f 2a 20 20 37 33 20 2a 2f 20 22 4e 65 22 2c /* 73 */ "Ne",
ba2d0 0a 20 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 . /* 74 */
ba2e0 22 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 "Eq",. /* 7
ba2f0 35 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 5 */ "Gt",.
ba300 2f 2a 20 20 37 36 20 2a 2f 20 22 4c 65 22 2c 0a /* 76 */ "Le",.
ba310 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22 /* 77 */ "
ba320 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 Lt",. /* 78
ba330 20 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f */ "Ge",. /
ba340 2a 20 20 37 39 20 2a 2f 20 22 4d 61 6b 65 52 65 * 79 */ "MakeRe
ba350 63 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 cord",. /*
ba360 38 30 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 80 */ "BitAnd",.
ba370 20 20 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22 /* 81 */ "
ba380 42 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 BitOr",. /*
ba390 20 38 32 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 82 */ "ShiftLef
ba3a0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 t",. /* 83
ba3b0 2a 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 2c */ "ShiftRight",
ba3c0 0a 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 . /* 84 */
ba3d0 22 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 "Add",. /*
ba3e0 38 35 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22 85 */ "Subtract"
ba3f0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f ,. /* 86 */
ba400 20 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 "Multiply",.
ba410 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 44 69 76 /* 87 */ "Div
ba420 69 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 ide",. /* 8
ba430 38 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 8 */ "Remainder"
ba440 2c 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f ,. /* 89 */
ba450 20 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 "Concat",.
ba460 2f 2a 20 20 39 30 20 2a 2f 20 22 52 65 73 75 6c /* 90 */ "Resul
ba470 74 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 tRow",. /*
ba480 39 31 20 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 91 */ "Delete",.
ba490 20 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22 /* 92 */ "
ba4a0 41 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 AggFinal",.
ba4b0 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e 6f /* 93 */ "BitNo
ba4c0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 20 t",. /* 94
ba4d0 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 20 */ "String8",.
ba4e0 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 43 6f /* 95 */ "Co
ba4f0 6d 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 mpare",. /*
ba500 20 39 36 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 96 */ "Goto",.
ba510 20 20 20 20 2f 2a 20 20 39 37 20 2a 2f 20 22 54 /* 97 */ "T
ba520 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 ableLock",.
ba530 2f 2a 20 20 39 38 20 2a 2f 20 22 43 6c 65 61 72 /* 98 */ "Clear
ba540 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a ",. /* 99 *
ba550 2f 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 / "VerifyCookie"
ba560 2c 0a 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f ,. /* 100 */
ba570 20 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20 20 "AggStep",.
ba580 20 2f 2a 20 31 30 31 20 2a 2f 20 22 54 72 61 6e /* 101 */ "Tran
ba590 73 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f saction",. /
ba5a0 2a 20 31 30 32 20 2a 2f 20 22 56 46 69 6c 74 65 * 102 */ "VFilte
ba5b0 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20 r",. /* 103
ba5c0 2a 2f 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 */ "VDestroy",.
ba5d0 20 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 4e /* 104 */ "N
ba5e0 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 ext",. /* 10
ba5f0 35 20 2a 2f 20 22 43 6f 75 6e 74 22 2c 0a 20 20 5 */ "Count",.
ba600 20 20 20 2f 2a 20 31 30 36 20 2a 2f 20 22 49 64 /* 106 */ "Id
ba610 78 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f xInsert",. /
ba620 2a 20 31 30 37 20 2a 2f 20 22 53 65 65 6b 47 65 * 107 */ "SeekGe
ba630 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a ",. /* 108 *
ba640 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 / "Insert",.
ba650 20 2f 2a 20 31 30 39 20 2a 2f 20 22 44 65 73 74 /* 109 */ "Dest
ba660 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 roy",. /* 11
ba670 30 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 65 0 */ "ReadCookie
ba680 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a ",. /* 111 *
ba690 2f 20 22 52 6f 77 53 65 74 54 65 73 74 22 2c 0a / "RowSetTest",.
ba6a0 20 20 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 /* 112 */ "
ba6b0 4c 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 LoadAnalysis",.
ba6c0 20 20 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 45 /* 113 */ "E
ba6d0 78 70 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a xplain",. /*
ba6e0 20 31 31 34 20 2a 2f 20 22 48 61 6c 74 49 66 4e 114 */ "HaltIfN
ba6f0 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 ull",. /* 11
ba700 35 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 6f 5 */ "OpenPseudo
ba710 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a ",. /* 116 *
ba720 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c / "OpenEphemeral
ba730 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a ",. /* 117 *
ba740 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f / "Null",. /
ba750 2a 20 31 31 38 20 2a 2f 20 22 4d 6f 76 65 22 2c * 118 */ "Move",
ba760 0a 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 . /* 119 */
ba770 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 "Blob",. /*
ba780 31 32 30 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c 120 */ "Rewind",
ba790 0a 20 20 20 20 20 2f 2a 20 31 32 31 20 2a 2f 20 . /* 121 */
ba7a0 22 53 65 65 6b 47 74 22 2c 0a 20 20 20 20 20 2f "SeekGt",. /
ba7b0 2a 20 31 32 32 20 2a 2f 20 22 56 42 65 67 69 6e * 122 */ "VBegin
ba7c0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20 2a ",. /* 123 *
ba7d0 2f 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20 20 / "VUpdate",.
ba7e0 20 20 2f 2a 20 31 32 34 20 2a 2f 20 22 49 66 5a /* 124 */ "IfZ
ba7f0 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 ero",. /* 12
ba800 35 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 0a 5 */ "VCreate",.
ba810 20 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 /* 126 */ "
ba820 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 Found",. /*
ba830 31 32 37 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 127 */ "IfPos",.
ba840 20 20 20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22 /* 128 */ "
ba850 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f NullRow",. /
ba860 2a 20 31 32 39 20 2a 2f 20 22 4a 75 6d 70 22 2c * 129 */ "Jump",
ba870 0a 20 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20 . /* 130 */
ba880 22 52 65 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 "Real",. /*
ba890 31 33 31 20 2a 2f 20 22 50 65 72 6d 75 74 61 74 131 */ "Permutat
ba8a0 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 ion",. /* 13
ba8b0 32 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 2 */ "NotUsed_13
ba8c0 32 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20 2",. /* 133
ba8d0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22 */ "NotUsed_133"
ba8e0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f ,. /* 134 */
ba8f0 20 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a "NotUsed_134",.
ba900 20 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 /* 135 */ "
ba910 4e 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 NotUsed_135",.
ba920 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f /* 136 */ "No
ba930 74 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 tUsed_136",.
ba940 20 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 /* 137 */ "NotU
ba950 73 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f sed_137",. /
ba960 2a 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 * 138 */ "NotUse
ba970 64 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 d_138",. /*
ba980 31 33 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 139 */ "NotUsed_
ba990 31 33 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 139",. /* 14
ba9a0 30 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 0 */ "NotUsed_14
ba9b0 30 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 0",. /* 141
ba9c0 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 */ "ToText",.
ba9d0 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 /* 142 */ "ToB
ba9e0 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 lob",. /* 14
ba9f0 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 3 */ "ToNumeric"
baa00 2c 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f ,. /* 144 */
baa10 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f "ToInt",. /
baa20 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c * 145 */ "ToReal
baa30 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e ",. };. return
baa40 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 azName[i];.}.#e
baa50 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ndif../*********
baa60 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 ***** End of opc
baa70 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a odes.c *********
baa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baaa0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
baab0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
baac0 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a os_os2.c ******
baad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
baaf0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 ****/./*.** 2006
bab00 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 Feb 14.**.** Th
bab10 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
bab20 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
bab30 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
bab40 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
bab50 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
bab60 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
bab70 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
bab80 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
bab90 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
baba0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
babb0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
babc0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
babd0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
babe0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
babf0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
bac00 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
bac10 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
bac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bac60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
bac70 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
bac80 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 ains code that i
bac90 73 20 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 s specific to OS
baca0 2f 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f /2..**.** $Id: o
bacb0 73 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32 s_os2.c,v 1.63 2
bacc0 30 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 008/12/10 19:26:
bacd0 32 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 24 drh Exp $.*/.
bace0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
bacf0 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 OS2../*.** A Not
bad00 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 e About Memory A
bad10 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a llocation:.**.**
bad20 20 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65 This driver use
bad30 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 s malloc()/free(
bad40 29 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 ) directly rathe
bad50 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 r than going thr
bad60 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 ough.** the SQLi
bad70 74 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 te-wrappers sqli
bad80 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c te3_malloc()/sql
bad90 69 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 ite3_free(). Th
bada0 6f 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 ose wrappers.**
badb0 61 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 are designed for
badc0 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 use on embedded
badd0 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d systems where m
bade0 65 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 emory is scarce
badf0 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 and.** malloc fa
bae00 69 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 ilures happen fr
bae10 65 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 equently. OS/2
bae20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c does not typical
bae30 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 ly run on.** emb
bae40 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 edded systems, a
bae50 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 nd when it does
bae60 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e the developers n
bae70 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 ormally have big
bae80 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 ger.** problems
bae90 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 to worry about t
baea0 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 han running out
baeb0 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 of memory. So t
baec0 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 here is not.** a
baed0 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 compelling need
baee0 20 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 to use the wrap
baef0 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 pers..**.** But
baf00 74 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 there is a good
baf10 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 reason to not us
baf20 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 e the wrappers.
baf30 20 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a If we use the.*
baf40 2a 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 * wrappers then
baf50 77 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 we will get simu
baf60 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 lated malloc() f
baf70 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 ailures within t
baf80 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 his.** driver.
baf90 41 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 And that causes
bafa0 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f all kinds of pro
bafb0 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 blems for our te
bafc0 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c sts. We.** coul
bafd0 64 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 d enhance SQLite
bafe0 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 to deal with si
baff0 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 mulated malloc f
bb000 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a ailures within.*
bb010 2a 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c * the OS driver,
bb020 20 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f but the code to
bb030 20 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 deal with those
bb040 20 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e failure would n
bb050 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 ot.** be exercis
bb060 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 ed on Linux (whi
bb070 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 ch does not need
bb080 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 to malloc() in
bb090 74 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 the driver).** a
bb0a0 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 nd so we would h
bb0b0 61 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 ave difficulty w
bb0c0 72 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 riting coverage
bb0d0 74 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a tests for that.*
bb0e0 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 * code. Better
bb0f0 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 to leave the cod
bb100 65 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e e out, we think.
bb110 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 .**.** The point
bb120 20 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 of this discuss
bb130 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 ion is as follow
bb140 73 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e s: When creatin
bb150 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 g a new.** OS la
bb160 79 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 yer for an embed
bb170 64 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 ded system, if y
bb180 6f 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 ou use this file
bb190 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a as an example,.
bb1a0 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 ** avoid the use
bb1b0 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 of malloc()/fre
bb1c0 65 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 e(). Those rout
bb1d0 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 ines work ok on
bb1e0 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 OS/2.** desktops
bb1f0 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c but not so well
bb200 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 in embedded sys
bb210 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 tems..*/../*.**
bb220 4d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 Macros used to d
bb230 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
bb240 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 or not to use t
bb250 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 hreads..*/.#if d
bb260 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 efined(SQLITE_TH
bb270 52 45 41 44 53 41 46 45 29 20 26 26 20 53 51 4c READSAFE) && SQL
bb280 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 ITE_THREADSAFE.#
bb290 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
bb2a0 53 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e S2_THREADS 1.#en
bb2b0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 dif../*.** Inclu
bb2c0 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 de code that is
bb2d0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 common to all os
bb2e0 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a _*.c files.*/./*
bb2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
bb300 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e clude os_common.
bb310 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
bb320 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a of os_os2.c ****
bb330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
bb340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
bb350 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d gin file os_comm
bb360 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
bb370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
bb390 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a .** 2004 May 22.
bb3a0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
bb3b0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
bb3c0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
bb3d0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
bb3e0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
bb3f0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
bb400 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
bb410 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
bb420 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
bb430 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
bb440 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
bb450 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
bb460 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
bb470 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
bb480 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
bb490 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
bb4a0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
bb4b0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
bb4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb500 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
bb510 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 ile contains mac
bb520 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 ros and a little
bb530 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 bit of code tha
bb540 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a t is common to.*
bb550 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 * all of the pla
bb560 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 tform-specific f
bb570 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e iles (os_*.c) an
bb580 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 d is #included i
bb590 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c nto those.** fil
bb5a0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 es..**.** This f
bb5b0 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 ile should be #i
bb5c0 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f ncluded by the o
bb5d0 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 s_*.c files only
bb5e0 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a . It is not a.*
bb5f0 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 * general purpos
bb600 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a e header file..*
bb610 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d *.** $Id: os_com
bb620 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 mon.h,v 1.38 200
bb630 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 9/02/24 18:40:50
bb640 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
bb650 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f $.*/.#ifndef _O
bb660 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 S_COMMON_H_.#def
bb670 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ine _OS_COMMON_H
bb680 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 _../*.** At leas
bb690 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 t two bugs have
bb6a0 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 slipped in becau
bb6b0 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 se we changed th
bb6c0 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a e MEMORY_DEBUG.*
bb6d0 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 * macro to SQLIT
bb6e0 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 E_DEBUG and some
bb6f0 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 older makefiles
bb700 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 have not yet ma
bb710 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 de the.** switch
bb720 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
bb730 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 code should cat
bb740 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ch this problem
bb750 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e at compile-time.
bb760 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 .*/.#ifdef MEMOR
bb770 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 Y_DEBUG.# error
bb780 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 "The MEMORY_DEBU
bb790 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c G macro is obsol
bb7a0 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 ete. Use SQLITE
bb7b0 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 _DEBUG instead."
bb7c0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
bb7d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
bb7e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
bb7f0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d sqlite3OSTrace =
bb800 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 0;.#define OSTR
bb810 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 ACE1(X)
bb820 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
bb830 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
bb840 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 gPrintf(X).#defi
bb850 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
bb860 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
bb870 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
bb880 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
bb890 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
bb8a0 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 ACE3(X,Y,Z)
bb8b0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
bb8c0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
bb8d0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 gPrintf(X,Y,Z).#
bb8e0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 define OSTRACE4(
bb8f0 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 X,Y,Z,A) if( s
bb900 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
bb910 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
bb920 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 tf(X,Y,Z,A).#def
bb930 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 ine OSTRACE5(X,Y
bb940 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 ,Z,A,B) if( sqli
bb950 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
bb960 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
bb970 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
bb980 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
bb990 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 Z,A,B,C) \. i
bb9a0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
bb9b0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
bb9c0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
bb9d0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
bb9e0 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
bb9f0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
bba00 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
bba10 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
bba20 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c Y,Z,A,B,C,D).#el
bba30 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 se.#define OSTRA
bba40 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f CE1(X).#define O
bba50 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 STRACE2(X,Y).#de
bba60 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
bba70 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Y,Z).#define OST
bba80 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 RACE4(X,Y,Z,A).#
bba90 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
bbaa0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
bbab0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
bbac0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
bbad0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
bbae0 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a A,B,C,D).#endif.
bbaf0 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f ./*.** Macros fo
bbb00 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 r performance tr
bbb10 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 acing. Normally
bbb20 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e turned off. On
bbb30 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 ly works.** on i
bbb40 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 486 hardware..*/
bbb50 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 .#ifdef SQLITE_P
bbb60 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 ERFORMANCE_TRACE
bbb70 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e ../* .** hwtime.
bbb80 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e h contains inlin
bbb90 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 e assembler code
bbba0 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e for implementin
bbbb0 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f g .** high-perfo
bbbc0 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f rmance timing ro
bbbd0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a utines..*/./****
bbbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
bbbf0 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 de hwtime.h in t
bbc00 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f he middle of os_
bbc10 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
bbc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
bbc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
bbc40 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a file hwtime.h *
bbc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
bbc80 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2008 May 27.**.
bbc90 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
bbca0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
bbcb0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
bbcc0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
bbcd0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
bbce0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
bbcf0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
bbd00 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
bbd10 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
bbd20 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
bbd30 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
bbd40 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
bbd50 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
bbd60 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
bbd70 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
bbd80 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
bbd90 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
bbda0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
bbdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbdf0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
bbe00 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
bbe10 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 asm code for re
bbe20 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 trieving "high-p
bbe30 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 erformance".** c
bbe40 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 ounters for x86
bbe50 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a class CPUs..**.*
bbe60 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c * $Id: hwtime.h,
bbe70 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 v 1.3 2008/08/01
bbe80 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 14:33:15 shane
bbe90 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
bbea0 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 _HWTIME_H_.#def
bbeb0 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a ine _HWTIME_H_..
bbec0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
bbed0 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 ing routine only
bbee0 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 works on pentiu
bbef0 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 m-class (or newe
bbf00 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a r) processors..*
bbf10 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 * It uses the RD
bbf20 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 TSC opcode to re
bbf30 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 ad the cycle cou
bbf40 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 nt value out of
bbf50 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 the.** processor
bbf60 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 and returns tha
bbf70 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 t value. This c
bbf80 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 an be used for h
bbf90 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 igh-res.** profi
bbfa0 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 ling..*/.#if (de
bbfb0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
bbfc0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f || defined(_MSC_
bbfd0 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 VER)) && \.
bbfe0 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 (defined(i386)
bbff0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 || defined(__i38
bc000 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 6__) || defined(
bc010 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 _M_IX86)).. #if
bc020 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f defined(__GNUC_
bc030 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f _).. __inline__
bc040 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
bc050 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
bc060 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 d){. unsigne
bc070 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 d int lo, hi;.
bc080 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
bc090 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
bc0a0 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d : "=a" (lo), "=
bc0b0 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 d" (hi));. r
bc0c0 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 eturn (sqlite_ui
bc0d0 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 nt64)hi << 32 |
bc0e0 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 lo;. }.. #elif
bc0f0 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
bc100 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 R).. __declspec
bc110 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 (naked) __inline
bc120 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f sqlite_uint64 _
bc130 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 _cdecl sqlite3Hw
bc140 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
bc150 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 __asm {.
bc160 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 rdtsc. r
bc170 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 et ; retur
bc180 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 n value at EDX:E
bc190 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 AX. }. }..
bc1a0 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 #endif..#elif (
bc1b0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
bc1c0 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 ) && defined(__x
bc1d0 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 86_64__)).. __i
bc1e0 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
bc1f0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
bc200 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
bc210 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 unsigned long v
bc220 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f al;. __asm_
bc230 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
bc240 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 "rdtsc" : "=A" (
bc250 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 val));. ret
bc260 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 urn val;. }. .#
bc270 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
bc280 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
bc290 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 ed(__ppc__))..
bc2a0 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
bc2b0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
bc2c0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
bc2d0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
bc2e0 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 g long retval;.
bc2f0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
bc300 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f ng junk;. _
bc310 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
bc320 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 e__ ("\n\.
bc330 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 1: mftb
bc340 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 u %1\n\.
bc350 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 mftb
bc360 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 %L0\n\.
bc370 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 mft
bc380 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 bu %0\n\.
bc390 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 cmp
bc3a0 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 w %0,%1\n\.
bc3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bc3c0 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 bne 1b".
bc3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 :
bc3e0 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 "=r" (retval), "
bc3f0 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 =r" (junk));.
bc400 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c return retval
bc410 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 ;. }..#else..
bc420 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c #error Need impl
bc430 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
bc440 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
bc450 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e r your platform.
bc460 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 .. /*. ** To c
bc470 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 ompile without i
bc480 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 mplementing sqli
bc490 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
bc4a0 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 your platform,.
bc4b0 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f ** you can remo
bc4c0 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 ve the above #er
bc4d0 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 ror and use the
bc4e0 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 following. ** s
bc4f0 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 tub function. Y
bc500 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d ou will lose tim
bc510 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 ing support for
bc520 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 many. ** of the
bc530 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 debugging and t
bc540 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 esting utilities
bc550 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 , but it should
bc560 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f at. ** least co
bc570 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 mpile and run..
bc580 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
bc590 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 TE sqlite_uint
bc5a0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
bc5b0 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 (void){ return (
bc5c0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
bc5d0 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 ); }..#endif..#e
bc5e0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
bc5f0 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a (_HWTIME_H_) */.
bc600 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bc610 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 End of hwtime.h
bc620 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
bc630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bc650 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bc660 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
bc670 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
bc680 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
bc690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bc6a0 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f ..static sqlite_
bc6b0 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a uint64 g_start;.
bc6c0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 static sqlite_ui
bc6d0 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a nt64 g_elapsed;.
bc6e0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 #define TIMER_ST
bc6f0 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 ART g_star
bc700 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 t=sqlite3Hwtime(
bc710 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f ).#define TIMER_
bc720 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c END g_el
bc730 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 apsed=sqlite3Hwt
bc740 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 ime()-g_start.#d
bc750 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
bc760 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 SED g_elapse
bc770 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 d.#else.#define
bc780 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 TIMER_START.#def
bc790 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 ine TIMER_END.#d
bc7a0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
bc7b0 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 SED ((sqlite
bc7c0 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 _uint64)0).#endi
bc7d0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 f../*.** If we c
bc7e0 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 ompile with the
bc7f0 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 SQLITE_TEST macr
bc800 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 o set, then the
bc810 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a following block.
bc820 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 ** of code will
bc830 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c give us the abil
bc840 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 ity to simulate
bc850 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 a disk I/O error
bc860 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 . This.** is us
bc870 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 ed for testing t
bc880 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 he I/O recovery
bc890 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 logic..*/.#ifdef
bc8a0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
bc8b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
bc8c0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
bc8d0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
bc8e0 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
bc8f0 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a of I/O Errors *
bc900 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
bc910 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
bc920 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 r_hardhit = 0;
bc930 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
bc940 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 of non-benign er
bc950 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 rors */.SQLITE_A
bc960 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
bc970 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 o_error_pending
bc980 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 = 0; /* C
bc990 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 ount down to fir
bc9a0 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a st I/O error */.
bc9b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
bc9c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bc9d0 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 persist = 0;
bc9e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 /* True if I
bc9f0 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 /O errors persis
bca00 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 t */.SQLITE_API
bca10 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
bca20 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b rror_benign = 0;
bca30 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
bca40 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 if errors are b
bca50 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f enign */.SQLITE_
bca60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
bca70 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
bca80 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 = 0;.SQLITE_API
bca90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 int sqlite3_dis
bcaa0 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 kfull = 0;.#defi
bcab0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
bcac0 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 orBenign(X) sqli
bcad0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
bcae0 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 ign=(X).#define
bcaf0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
bcb00 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 CODE) \. if( (
bcb10 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
bcb20 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 _persist && sqli
bcb30 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
bcb40 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 ) \. || sq
bcb50 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
bcb60 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 ending-- == 1 )
bcb70 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
bcb80 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 { local_ioerr()
bcb90 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 ; CODE; }.static
bcba0 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 void local_ioer
bcbb0 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 r(){. IOTRACE((
bcbc0 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 "IOERR\n"));. s
bcbd0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bcbe0 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 hit++;. if( !sq
bcbf0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
bcc00 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f enign ) sqlite3_
bcc10 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
bcc20 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 ++;.}.#define Si
bcc30 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
bcc40 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 ror(CODE) \. i
bcc50 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
bcc60 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c ull_pending ){ \
bcc70 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 . if( sqlite
bcc80 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
bcc90 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 ng == 1 ){ \.
bcca0 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 local_ioerr(
bccb0 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 ); \. sqli
bccc0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 te3_diskfull = 1
bccd0 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 ; \. sqlit
bcce0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 e3_io_error_hit
bccf0 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f = 1; \. CO
bcd00 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 DE; \. }else
bcd10 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 { \. sqlit
bcd20 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
bcd30 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 ing--; \. }
bcd40 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 \. }.#else.#de
bcd50 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
bcd60 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 rrorBenign(X).#d
bcd70 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
bcd80 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 Error(A).#define
bcd90 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
bcda0 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 lError(A).#endif
bcdb0 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 ../*.** When tes
bcdc0 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 ting, keep a cou
bcdd0 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 nt of the number
bcde0 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a of open files..
bcdf0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
bce00 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
bce10 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 int sqlite3_ope
bce20 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 n_file_count = 0
bce30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f ;.#define OpenCo
bce40 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 unter(X) sqlite
bce50 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 3_open_file_coun
bce60 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 t+=(X).#else.#de
bce70 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 fine OpenCounter
bce80 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 (X).#endif..#end
bce90 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
bcea0 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f OS_COMMON_H_) */
bceb0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
bcec0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d * End of os_comm
bced0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
bcee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf00 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
bcf10 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
bcf20 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
bcf30 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a n os_os2.c *****
bcf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bcf50 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 /../*.** The os2
bcf60 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 File structure i
bcf70 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 s subclass of sq
bcf80 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 lite3_file speci
bcf90 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 fic for the OS/2
bcfa0 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 .** protability
bcfb0 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 layer..*/.typede
bcfc0 66 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 f struct os2File
bcfd0 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 os2File;.struct
bcfe0 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e os2File {. con
bcff0 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
bd000 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 thods *pMethod;
bd010 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 /* Always the f
bd020 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 irst entry */.
bd030 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 HFILE h;
bd040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e /* Han
bd050 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e dle for accessin
bd060 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 g the file */.
bd070 63 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b char* pathToDel;
bd080 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
bd090 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c e of file to del
bd0a0 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 ete on close, NU
bd0b0 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 LL if not */. u
bd0c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 nsigned char loc
bd0d0 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 ktype; /* Type
bd0e0 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 of lock current
bd0f0 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 ly held on this
bd100 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 file */.};..#def
bd110 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ine LOCK_TIMEOUT
bd120 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 10L /* the defa
bd130 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 ult locking time
bd140 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a out */../*******
bd150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd190 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 ******.** The ne
bd1a0 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 xt group of rout
bd1b0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 ines implement t
bd1c0 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 he I/O methods s
bd1d0 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 pecified.** by t
bd1e0 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
bd1f0 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a thods object..**
bd200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
bd250 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c *.** Close a fil
bd260 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
bd270 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 os2Close( sqlit
bd280 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 e3_file *id ){.
bd290 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
bd2a0 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 ERROR;. os2File
bd2b0 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 *pFile;. if( i
bd2c0 64 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f d && (pFile = (o
bd2d0 73 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 s2File*)id) != 0
bd2e0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 ){. OSTRACE2
bd2f0 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 ( "CLOSE %d\n",
bd300 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 pFile->h );.
bd310 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 rc = DosClose( p
bd320 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 File->h );. p
bd330 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
bd340 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 NO_LOCK;. if
bd350 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 ( pFile->pathToD
bd360 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 el != NULL ){.
bd370 20 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 rc = DosForc
bd380 65 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 eDelete( (PSZ)pF
bd390 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 ile->pathToDel )
bd3a0 3b 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 ;. free( pF
bd3b0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 ile->pathToDel )
bd3c0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 ;. pFile->p
bd3d0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b athToDel = NULL;
bd3e0 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 . }. id =
bd3f0 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 0;. OpenCount
bd400 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 er( -1 );. }..
bd410 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f return rc == NO
bd420 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
bd430 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
bd440 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 R;.}../*.** Read
bd450 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c data from a fil
bd460 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e e into a buffer.
bd470 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
bd480 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 OK if all.** byt
bd490 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 es were read suc
bd4a0 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 cessfully and SQ
bd4b0 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e LITE_IOERR if an
bd4c0 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 ything goes.** w
bd4d0 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rong..*/.static
bd4e0 69 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 int os2Read(. s
bd4f0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
bd500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
bd510 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 * File to read f
bd520 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 rom */. void *p
bd530 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 Buf,
bd540 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
bd550 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 e content into t
bd560 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 his buffer */.
bd570 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 int amt,
bd580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd590 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
bd5a0 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 es to read */.
bd5b0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 sqlite3_int64 of
bd5c0 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 fset
bd5d0 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 /* Begin reading
bd5e0 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 at this offset
bd5f0 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 */.){. ULONG fi
bd600 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b leLocation = 0L;
bd610 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 . ULONG got;.
bd620 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d os2File *pFile =
bd630 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 (os2File*)id;.
bd640 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
bd650 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
bd660 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
bd670 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b TE_IOERR_READ );
bd680 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 . OSTRACE3( "RE
bd690 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 AD %d lock=%d\n"
bd6a0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
bd6b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
bd6c0 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 if( DosSetFileP
bd6d0 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 tr(pFile->h, off
bd6e0 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c set, FILE_BEGIN,
bd6f0 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 &fileLocation)
bd700 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 != NO_ERROR ){.
bd710 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
bd720 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 _IOERR;. }. if
bd730 28 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 ( DosRead( pFile
bd740 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 ->h, pBuf, amt,
bd750 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 &got ) != NO_ERR
bd760 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OR ){. return
bd770 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 SQLITE_IOERR_RE
bd780 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f AD;. }. if( go
bd790 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 t == (ULONG)amt
bd7a0 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ). return SQL
bd7b0 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b ITE_OK;. else {
bd7c0 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 . /* Unread p
bd7d0 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 ortions of the i
bd7e0 6e 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 nput buffer must
bd7f0 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 be zero-filled
bd800 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 */. memset(&(
bd810 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 (char*)pBuf)[got
bd820 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a ], 0, amt-got);.
bd830 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
bd840 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 E_IOERR_SHORT_RE
bd850 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a AD;. }.}../*.**
bd860 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d Write data from
bd870 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 a buffer into a
bd880 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 file. Return S
bd890 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
bd8a0 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f ess.** or some o
bd8b0 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 ther error code
bd8c0 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 on failure..*/.s
bd8d0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 tatic int os2Wri
bd8e0 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 te(. sqlite3_fi
bd8f0 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 le *id,
bd900 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f /* File to
bd910 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 write into */.
bd920 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 const void *pBu
bd930 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f,
bd940 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f /* The bytes to
bd950 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 be written */.
bd960 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 int amt,
bd970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
bd980 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
bd990 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
bd9a0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
bd9b0 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 offset
bd9c0 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f /* Offset into
bd9d0 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 the file to beg
bd9e0 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f in writing at */
bd9f0 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 .){. ULONG file
bda00 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 Location = 0L;.
bda10 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
bda20 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 ERROR;. ULONG w
bda30 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 rote;. os2File
bda40 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
bda50 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 e*)id;. assert(
bda60 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 id!=0 );. Simu
bda70 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
bda80 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
bda90 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 _WRITE );. Simu
bdaa0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f lateDiskfullErro
bdab0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
bdac0 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 _FULL );. OSTRA
bdad0 43 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c CE3( "WRITE %d l
bdae0 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 ock=%d\n", pFile
bdaf0 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
bdb00 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f type );. if( Do
bdb10 73 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c sSetFilePtr(pFil
bdb20 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 e->h, offset, FI
bdb30 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c LE_BEGIN, &fileL
bdb40 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 ocation) != NO_E
bdb50 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 RROR ){. retu
bdb60 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b rn SQLITE_IOERR;
bdb70 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 . }. assert( a
bdb80 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 mt>0 );. while(
bdb90 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 amt > 0 &&.
bdba0 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 ( rc = DosW
bdbb0 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 rite( pFile->h,
bdbc0 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 (PVOID)pBuf, amt
bdbd0 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 , &wrote ) ) ==
bdbe0 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 NO_ERROR &&.
bdbf0 20 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 wrote > 0.
bdc00 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 ){. amt -= w
bdc10 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d rote;. pBuf =
bdc20 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b &((char*)pBuf)[
bdc30 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 wrote];. }.. r
bdc40 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f eturn ( rc != NO
bdc50 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 _ERROR || amt >
bdc60 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 (int)wrote ) ? S
bdc70 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c QLITE_FULL : SQL
bdc80 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
bdc90 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 Truncate an ope
bdca0 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 n file to a spec
bdcb0 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 ified size.*/.st
bdcc0 61 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e atic int os2Trun
bdcd0 63 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 cate( sqlite3_fi
bdce0 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 le *id, i64 nByt
bdcf0 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 e ){. APIRET rc
bdd00 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f = NO_ERROR;. o
bdd10 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
bdd20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
bdd30 4f 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 OSTRACE3( "TRUNC
bdd40 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 ATE %d %lld\n",
bdd50 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 pFile->h, nByte
bdd60 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
bdd70 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
bdd80 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 ITE_IOERR_TRUNCA
bdd90 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 TE );. rc = Dos
bdda0 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 SetFileSize( pFi
bddb0 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a le->h, nByte );.
bddc0 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e return rc == N
bddd0 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 O_ERROR ? SQLITE
bdde0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
bddf0 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a RR_TRUNCATE;.}..
bde00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
bde10 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 ST./*.** Count t
bde20 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c he number of ful
bde30 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 lsyncs and norma
bde40 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 l syncs. This i
bde50 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a s used to test.*
bde60 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 * that syncs and
bde70 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f fullsyncs are o
bde80 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 ccuring at the r
bde90 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 ight times..*/.S
bdea0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
bdeb0 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 lite3_sync_count
bdec0 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 = 0;.SQLITE_API
bded0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c int sqlite3_ful
bdee0 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b lsync_count = 0;
bdef0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d .#endif../*.** M
bdf00 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 ake sure all wri
bdf10 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 tes to a particu
bdf20 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d lar file are com
bdf30 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a mitted to disk..
bdf40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
bdf50 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 2Sync( sqlite3_f
bdf60 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 ile *id, int fla
bdf70 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 gs ){. os2File
bdf80 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
bdf90 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 e*)id;. OSTRACE
bdfa0 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3( "SYNC %d lock
bdfb0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
bdfc0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
bdfd0 65 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 e );.#ifdef SQLI
bdfe0 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c TE_TEST. if( fl
bdff0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e ags & SQLITE_SYN
be000 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c C_FULL){. sql
be010 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
be020 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c unt++;. }. sql
be030 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b ite3_sync_count+
be040 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 +;.#endif. /* I
be050 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 f we compiled wi
be060 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f th the SQLITE_NO
be070 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e _SYNC flag, then
be080 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 syncing is a.
be090 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 ** no-op. */.#i
be0a0 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 fdef SQLITE_NO_S
be0b0 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 YNC. UNUSED_PAR
be0c0 41 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 AMETER(pFile);.
be0d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
be0e0 4b 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 K;.#else. retur
be0f0 6e 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 n DosResetBuffer
be100 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 ( pFile->h ) ==
be110 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 NO_ERROR ? SQLIT
be120 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f E_OK : SQLITE_IO
be130 45 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f ERR;.#endif.}../
be140 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 *.** Determine t
be150 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
be160 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 of a file in byt
be170 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 es.*/.static int
be180 20 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 os2FileSize( sq
be190 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
be1a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 sqlite3_int64 *p
be1b0 53 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 Size ){. APIRET
be1c0 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a rc = NO_ERROR;.
be1d0 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 FILESTATUS3 fs
be1e0 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d ts3FileInfo;. m
be1f0 65 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 emset(&fsts3File
be200 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Info, 0, sizeof(
be210 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b fsts3FileInfo));
be220 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
be230 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f );. SimulateIO
be240 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
be250 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 LITE_IOERR_FSTAT
be260 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 );. rc = DosQu
be270 65 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f eryFileInfo( ((o
be280 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 s2File*)id)->h,
be290 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 FIL_STANDARD, &f
be2a0 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 sts3FileInfo, si
be2b0 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 zeof(FILESTATUS3
be2c0 29 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d ) );. if( rc ==
be2d0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
be2e0 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 *pSize = fsts3F
be2f0 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a ileInfo.cbFile;.
be300 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
be310 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
be320 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
be330 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 _IOERR_FSTAT;.
be340 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 }.}../*.** Acqui
be350 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b re a reader lock
be360 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
be370 67 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 getReadLock( os2
be380 46 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 File *pFile ){.
be390 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 FILELOCK LockA
be3a0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rea,.
be3b0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 UnlockArea;. A
be3c0 50 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d PIRET res;. mem
be3d0 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 set(&LockArea, 0
be3e0 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 , sizeof(LockAre
be3f0 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 a));. memset(&U
be400 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 nlockArea, 0, si
be410 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 zeof(UnlockArea)
be420 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f );. LockArea.lO
be430 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 ffset = SHARED_F
be440 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 IRST;. LockArea
be450 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 .lRange = SHARED
be460 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 _SIZE;. UnlockA
be470 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
be480 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c ;. UnlockArea.l
be490 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 Range = 0L;. re
be4a0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f s = DosSetFileLo
be4b0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 cks( pFile->h, &
be4c0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 UnlockArea, &Loc
be4d0 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 kArea, LOCK_TIME
be4e0 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 OUT, 1L );. OST
be4f0 52 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c RACE3( "GETREADL
be500 4f 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 OCK %d res=%d\n"
be510 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
be520 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b );. return res;
be530 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 .}../*.** Undo a
be540 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 readlock.*/.sta
be550 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 tic int unlockRe
be560 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 adLock( os2File
be570 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 *id ){. FILELOC
be580 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 K LockArea,.
be590 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 UnlockA
be5a0 72 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 rea;. APIRET re
be5b0 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 s;. memset(&Loc
be5c0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
be5d0 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d (LockArea));. m
be5e0 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 emset(&UnlockAre
be5f0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c a, 0, sizeof(Unl
be600 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 ockArea));. Loc
be610 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 kArea.lOffset =
be620 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 0L;. LockArea.l
be630 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e Range = 0L;. Un
be640 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
be650 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
be660 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 . UnlockArea.lR
be670 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 ange = SHARED_SI
be680 5a 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 ZE;. res = DosS
be690 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d etFileLocks( id-
be6a0 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c >h, &UnlockArea,
be6b0 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b &LockArea, LOCK
be6c0 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a _TIMEOUT, 1L );.
be6d0 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c OSTRACE3( "UNL
be6e0 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c OCK-READLOCK fil
be6f0 65 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d e handle=%d res=
be700 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 %d?\n", id->h, r
be710 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 es );. return r
be720 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 es;.}../*.** Loc
be730 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
be740 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
be750 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
be760 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
be770 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
be780 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
be790 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
be7a0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
be7b0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
be7c0 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
be7d0 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
be7e0 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
be7f0 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
be800 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
be810 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
be820 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
be830 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
be840 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
be850 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
be860 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
be870 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
be880 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
be890 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
be8a0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
be8b0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
be8c0 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
be8d0 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
be8e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
be8f0 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
be900 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
be910 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
be920 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
be930 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
be940 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
be950 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
be960 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
be970 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
be980 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
be990 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
be9a0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
be9b0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
be9c0 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
be9d0 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 XCLUSIVE.**.** T
be9e0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
be9f0 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
bea00 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 lock. The os2U
bea10 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a nlock() routine.
bea20 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f ** erases all lo
bea30 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 cks at once and
bea40 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 returns us immed
bea50 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e iately to lockin
bea60 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 g level 0..** It
bea70 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
bea80 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f to lower the lo
bea90 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 cking level one
beaa0 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 step at a time.
beab0 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 You.** must go
beac0 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b straight to lock
bead0 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a ing level 0..*/.
beae0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f static int os2Lo
beaf0 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck( sqlite3_file
beb00 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
beb10 70 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d pe ){. int rc =
beb20 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
beb30 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
beb40 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 from subroutine
beb50 73 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 s */. APIRET re
beb60 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 s = NO_ERROR;
beb70 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e /* Result of an
beb80 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 OS/2 lock call
beb90 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b */. int newLock
beba0 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 type; /* S
bebb0 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 et pFile->lockty
bebc0 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 pe to this value
bebd0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 before exiting
bebe0 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 */. int gotPend
bebf0 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 ingLock = 0;/* T
bec00 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 rue if we acquir
bec10 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 ed a PENDING loc
bec20 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 k this time */.
bec30 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 FILELOCK LockA
bec40 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rea,.
bec50 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f UnlockArea;. o
bec60 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
bec70 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
bec80 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 memset(&LockArea
bec90 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b , 0, sizeof(Lock
beca0 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 Area));. memset
becb0 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c (&UnlockArea, 0,
becc0 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 sizeof(UnlockAr
becd0 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ea));. assert(
bece0 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 pFile!=0 );. OS
becf0 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 TRACE4( "LOCK %d
bed00 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 %d was %d\n", p
bed10 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
bed20 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 e, pFile->lockty
bed30 70 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 pe );.. /* If t
bed40 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 here is already
bed50 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 a lock of this t
bed60 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 ype or more rest
bed70 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 rictive on the.
bed80 20 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 ** os2File, do
bed90 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 nothing. Don't u
beda0 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a se the end_lock:
bedb0 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 exit path, as.
bedc0 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ** sqlite3_mute
bedd0 78 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 x_enter() hasn't
bede0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 been called yet
bedf0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 .. */. if( pFi
bee00 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f le->locktype>=lo
bee10 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 cktype ){. OS
bee20 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 TRACE3( "LOCK %d
bee30 20 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 %d ok (already
bee40 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d held)\n", pFile-
bee50 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a >h, locktype );.
bee60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
bee70 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
bee80 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f Make sure the lo
bee90 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 cking sequence i
beea0 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 s correct. */.
beeb0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e assert( pFile->
beec0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
beed0 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 K || locktype==S
beee0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
beef0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
bef00 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 !=PENDING_LOCK )
bef10 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
bef20 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c type!=RESERVED_L
bef30 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f OCK || pFile->lo
bef40 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
bef50 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 OCK );.. /* Loc
bef60 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f k the PENDING_LO
bef70 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 CK byte if we ne
bef80 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 ed to acquire a
bef90 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a PENDING lock or.
befa0 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f ** a SHARED lo
befb0 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 ck. If we are a
befc0 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 cquiring a SHARE
befd0 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 D lock, the acqu
befe0 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 isition of. **
beff0 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b the PENDING_LOCK
bf000 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 byte is tempora
bf010 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f ry.. */. newLo
bf020 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e cktype = pFile->
bf030 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 locktype;. if(
bf040 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
bf050 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c =NO_LOCK. |
bf060 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 | (locktype==EXC
bf070 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
bf080 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
bf090 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 RESERVED_LOCK).
bf0a0 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 ){. LockArea
bf0b0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 .lOffset = PENDI
bf0c0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 NG_BYTE;. Loc
bf0d0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 kArea.lRange = 1
bf0e0 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
bf0f0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
bf100 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
bf110 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 Range = 0L;..
bf120 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 /* wait longer
bf130 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 than LOCK_TIMEOU
bf140 54 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 T here not to ha
bf150 76 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 ve to try multip
bf160 6c 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 le times */.
bf170 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 res = DosSetFile
bf180 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
bf190 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
bf1a0 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 ockArea, 100L, 0
bf1b0 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 L );. if( res
bf1c0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
bf1d0 20 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 gotPending
bf1e0 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 Lock = 1;.
bf1f0 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 OSTRACE3( "LOCK
bf200 25 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 %d pending lock
bf210 62 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 boolean set. re
bf220 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e s=%d\n", pFile->
bf230 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a h, res );. }.
bf240 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 }.. /* Acquir
bf250 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a e a shared lock.
bf260 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
bf270 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
bf280 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 && res == NO_ER
bf290 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ROR ){. asser
bf2a0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
bf2b0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 pe==NO_LOCK );.
bf2c0 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 res = getRead
bf2d0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
bf2e0 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 if( res == NO_E
bf2f0 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 RROR ){. ne
bf300 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 wLocktype = SHAR
bf310 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 ED_LOCK;. }.
bf320 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f OSTRACE3( "LO
bf330 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68 CK %d acquire sh
bf340 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 ared lock. res=%
bf350 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
bf360 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a res );. }.. /*
bf370 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 Acquire a RESER
bf380 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 VED lock. */.
bf390 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 if( locktype==RE
bf3a0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 SERVED_LOCK && r
bf3b0 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
bf3c0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 {. assert( pF
bf3d0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
bf3e0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
bf3f0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
bf400 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 et = RESERVED_BY
bf410 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 TE;. LockArea
bf420 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
bf430 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
bf440 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
bf450 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
bf460 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 0L;. res =
bf470 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
bf480 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
bf490 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
bf4a0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
bf4b0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 , 0L );. if(
bf4c0 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
bf4d0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
bf4e0 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f type = RESERVED_
bf4f0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 LOCK;. }.
bf500 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 OSTRACE3( "LOCK
bf510 25 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72 %d acquire reser
bf520 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 ved lock. res=%d
bf530 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
bf540 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 es );. }.. /*
bf550 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e Acquire a PENDIN
bf560 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 G lock. */. if
bf570 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c ( locktype==EXCL
bf580 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 USIVE_LOCK && re
bf590 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
bf5a0 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 . newLocktype
bf5b0 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b = PENDING_LOCK;
bf5c0 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c . gotPendingL
bf5d0 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 ock = 0;. OST
bf5e0 52 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 RACE2( "LOCK %d
bf5f0 61 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 acquire pending
bf600 6c 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f lock. pending lo
bf610 63 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 ck boolean unset
bf620 2e 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 .\n", pFile->h )
bf630 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
bf640 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 ire an EXCLUSIVE
bf650 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
bf660 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
bf670 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 SIVE_LOCK && res
bf680 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
bf690 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c assert( pFil
bf6a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 e->locktype>=SHA
bf6b0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 RED_LOCK );.
bf6c0 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 res = unlockRead
bf6d0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
bf6e0 20 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 OSTRACE2( "unre
bf6f0 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 adlock = %d\n",
bf700 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 res );. LockA
bf710 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 rea.lOffset = SH
bf720 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 ARED_FIRST;.
bf730 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
bf740 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 = SHARED_SIZE;.
bf750 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
bf760 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
bf770 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
bf780 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 e = 0L;. res
bf790 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
bf7a0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
bf7b0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
bf7c0 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
bf7d0 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 T, 0L );. if(
bf7e0 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 res == NO_ERROR
bf7f0 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 ){. newLoc
bf800 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 ktype = EXCLUSIV
bf810 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 E_LOCK;. }els
bf820 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 e{. OSTRACE
bf830 32 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 2( "OS/2 error-c
bf840 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 ode = %d\n", res
bf850 20 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 );. getRea
bf860 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 dLock(pFile);.
bf870 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 }. OSTRACE3
bf880 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 ( "LOCK %d acqui
bf890 72 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 re exclusive loc
bf8a0 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 k. res=%d\n", p
bf8b0 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a File->h, res );.
bf8c0 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 }.. /* If we
bf8d0 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 are holding a PE
bf8e0 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 NDING lock that
bf8f0 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 ought to be rele
bf900 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 ased, then. **
bf910 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a release it now..
bf920 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 */. if( gotPe
bf930 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 ndingLock && loc
bf940 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
bf950 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b CK ){. int r;
bf960 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f . LockArea.lO
bf970 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
bf980 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 LockArea.lRange
bf990 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b = 0L;. Unlock
bf9a0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 Area.lOffset = P
bf9b0 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
bf9c0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e UnlockArea.lRan
bf9d0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d ge = 1L;. r =
bf9e0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
bf9f0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
bfa00 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
bfa10 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
bfa20 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
bfa30 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 ACE3( "LOCK %d u
bfa40 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 nlocking pending
bfa50 2f 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 /is shared. r=%d
bfa60 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
bfa70 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 );. }.. /* Up
bfa80 64 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f date the state o
bfa90 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 f the lock has h
bfaa0 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 eld in the file
bfab0 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a descriptor then.
bfac0 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 ** return the
bfad0 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 appropriate resu
bfae0 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 lt code.. */.
bfaf0 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 if( res == NO_ER
bfb00 52 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ROR ){. rc =
bfb10 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
bfb20 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 se{. OSTRACE4
bfb30 28 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 ( "LOCK FAILED %
bfb40 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 d trying for %d
bfb50 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 but got %d\n", p
bfb60 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
bfb70 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c locktype,
bfb80 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a newLocktype );.
bfb90 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
bfba0 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c BUSY;. }. pFil
bfbb0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 e->locktype = ne
bfbc0 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 wLocktype;. OST
bfbd0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 RACE3( "LOCK %d
bfbe0 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 now %d\n", pFile
bfbf0 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ->h, pFile->lock
bfc00 74 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e type );. return
bfc10 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
bfc20 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
bfc30 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
bfc40 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
bfc50 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
bfc60 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
bfc70 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
bfc80 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
bfc90 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
bfca0 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d , return.** non-
bfcb0 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 zero, otherwise
bfcc0 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 zero..*/.static
bfcd0 69 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 int os2CheckRese
bfce0 72 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 rvedLock( sqlite
bfcf0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
bfd00 2a 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 *pOut ){. int r
bfd10 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 = 0;. os2File
bfd20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c *pFile = (os2Fil
bfd30 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 e*)id;. assert(
bfd40 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 pFile!=0 );. i
bfd50 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
bfd60 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe>=RESERVED_LOC
bfd70 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a K ){. r = 1;.
bfd80 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 OSTRACE3( "T
bfd90 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
bfda0 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 d (local)\n", pF
bfdb0 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d ile->h, r );. }
bfdc0 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f else{. FILELO
bfdd0 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 CK LockArea,.
bfde0 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f Unlo
bfdf0 63 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 ckArea;. APIR
bfe00 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 ET rc = NO_ERROR
bfe10 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f ;. memset(&Lo
bfe20 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f ckArea, 0, sizeo
bfe30 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 f(LockArea));.
bfe40 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b memset(&Unlock
bfe50 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
bfe60 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 UnlockArea));.
bfe70 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
bfe80 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 et = RESERVED_BY
bfe90 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 TE;. LockArea
bfea0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 .lRange = 1L;.
bfeb0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
bfec0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
bfed0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
bfee0 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 = 0L;. rc =
bfef0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
bff00 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
bff10 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
bff20 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
bff30 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
bff40 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f CE3( "TEST WR-LO
bff50 43 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 CK %d lock reser
bff60 76 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e ved byte rc=%d\n
bff70 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 ", pFile->h, rc
bff80 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d );. if( rc ==
bff90 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
bffa0 20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 APIRET rcu =
bffb0 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 NO_ERROR; /* ret
bffc0 75 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c urn code for unl
bffd0 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 ocking */.
bffe0 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 LockArea.lOffset
bfff0 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 = 0L;. Loc
c0000 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
c0010 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 L;. UnlockA
c0020 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 rea.lOffset = RE
c0030 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 SERVED_BYTE;.
c0040 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 UnlockArea.lR
c0050 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 ange = 1L;.
c0060 20 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c rcu = DosSetFil
c0070 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 eLocks( pFile->h
c0080 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 , &UnlockArea, &
c0090 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 LockArea, LOCK_T
c00a0 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 IMEOUT, 0L );.
c00b0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 OSTRACE3( "T
c00c0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 EST WR-LOCK %d u
c00d0 6e 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 nlock reserved b
c00e0 79 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 yte r=%d\n", pFi
c00f0 6c 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 le->h, rcu );.
c0100 20 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 }. r = !(rc
c0110 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 == NO_ERROR);.
c0120 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 OSTRACE3( "TE
c0130 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
c0140 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 (remote)\n", pF
c0150 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d ile->h, r );. }
c0160 0a 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 . *pOut = r;.
c0170 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
c0180 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 ;.}../*.** Lower
c0190 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
c01a0 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 el on file descr
c01b0 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b iptor id to lock
c01c0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
c01d0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
c01e0 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
c01f0 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
c0200 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
c0210 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
c0220 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
c0230 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
c0240 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
c0250 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
c0260 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
c0270 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
c0280 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 * It is not poss
c0290 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f ible for this ro
c02a0 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 utine to fail if
c02b0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
c02c0 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f ment.** is NO_LO
c02d0 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f CK. If the seco
c02e0 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 nd argument is S
c02f0 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 HARED_LOCK then
c0300 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 this routine.**
c0310 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c might return SQL
c0320 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 ITE_IOERR;.*/.st
c0330 61 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f atic int os2Unlo
c0340 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck( sqlite3_file
c0350 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
c0360 70 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 pe ){. int type
c0370 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ;. os2File *pFi
c0380 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
c0390 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d d;. APIRET rc =
c03a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 SQLITE_OK;. AP
c03b0 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 IRET res = NO_ER
c03c0 52 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 ROR;. FILELOCK
c03d0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 LockArea,.
c03e0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 UnlockAre
c03f0 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 a;. memset(&Loc
c0400 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
c0410 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d (LockArea));. m
c0420 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 emset(&UnlockAre
c0430 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c a, 0, sizeof(Unl
c0440 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 ockArea));. ass
c0450 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b ert( pFile!=0 );
c0460 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
c0470 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype<=SHARED_LOCK
c0480 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 );. OSTRACE4(
c0490 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 "UNLOCK %d to %d
c04a0 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c was %d\n", pFil
c04b0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 e->h, locktype,
c04c0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
c04d0 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c );. type = pFil
c04e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 e->locktype;. i
c04f0 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 f( type>=EXCLUSI
c0500 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c VE_LOCK ){. L
c0510 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c0520 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 = 0L;. LockAr
c0530 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a ea.lRange = 0L;.
c0540 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c0550 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f Offset = SHARED_
c0560 46 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 FIRST;. Unloc
c0570 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 kArea.lRange = S
c0580 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 HARED_SIZE;.
c0590 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 res = DosSetFile
c05a0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
c05b0 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
c05c0 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 ockArea, LOCK_TI
c05d0 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 MEOUT, 0L );.
c05e0 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f OSTRACE3( "UNLO
c05f0 43 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 CK %d exclusive
c0600 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 lock res=%d\n",
c0610 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
c0620 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 . if( locktyp
c0630 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 e==SHARED_LOCK &
c0640 26 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 & getReadLock(pF
c0650 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 ile) != NO_ERROR
c0660 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 ){. /* Thi
c0670 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 s should never h
c0680 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c appen. We shoul
c0690 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 d always be able
c06a0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 to. ** rea
c06b0 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 cquire the read
c06c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 lock */. OS
c06d0 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 TRACE3( "UNLOCK
c06e0 25 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 %d to %d getRead
c06f0 4c 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 Lock() failed\n"
c0700 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
c0710 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 type );. rc
c0720 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
c0730 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 UNLOCK;. }.
c0740 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 }. if( type>=RE
c0750 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SERVED_LOCK ){.
c0760 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
c0770 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f set = 0L;. Lo
c0780 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c0790 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 0L;. UnlockAr
c07a0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 ea.lOffset = RES
c07b0 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 ERVED_BYTE;.
c07c0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
c07d0 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 e = 1L;. res
c07e0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
c07f0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
c0800 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
c0810 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
c0820 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 T, 0L );. OST
c0830 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 RACE3( "UNLOCK %
c0840 64 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 d reserved res=%
c0850 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
c0860 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 res );. }. if(
c0870 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f locktype==NO_LO
c0880 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 CK && type>=SHAR
c0890 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
c08a0 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c es = unlockReadL
c08b0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
c08c0 4f 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 OSTRACE5( "UNLOC
c08d0 4b 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 K %d is %d want
c08e0 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 %d res=%d\n", pF
c08f0 69 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f ile->h, type, lo
c0900 63 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 cktype, res );.
c0910 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 }. if( type>=P
c0920 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 ENDING_LOCK ){.
c0930 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 LockArea.lOff
c0940 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f set = 0L;. Lo
c0950 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c0960 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 0L;. UnlockAr
c0970 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e ea.lOffset = PEN
c0980 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 DING_BYTE;. U
c0990 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c09a0 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 1L;. res =
c09b0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
c09c0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
c09d0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
c09e0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
c09f0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
c0a00 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
c0a10 20 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c pending res=%d\
c0a20 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
c0a30 73 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 s );. }. pFile
c0a40 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
c0a50 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 ktype;. OSTRACE
c0a60 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 3( "UNLOCK %d no
c0a70 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e w %d\n", pFile->
c0a80 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 h, pFile->lockty
c0a90 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 pe );. return r
c0aa0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 c;.}../*.** Cont
c0ab0 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 rol and query of
c0ac0 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 the open file h
c0ad0 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 andle..*/.static
c0ae0 20 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 int os2FileCont
c0af0 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 rol(sqlite3_file
c0b00 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f *id, int op, vo
c0b10 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 id *pArg){. swi
c0b20 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 tch( op ){. c
c0b30 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
c0b40 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 _LOCKSTATE: {.
c0b50 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 *(int*)pArg
c0b60 3d 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 = ((os2File*)id)
c0b70 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 ->locktype;.
c0b80 20 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e OSTRACE3( "FCN
c0b90 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 TL_LOCKSTATE %d
c0ba0 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 lock=%d\n", ((os
c0bb0 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 2File*)id)->h, (
c0bc0 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c (os2File*)id)->l
c0bd0 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 ocktype );.
c0be0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c0bf0 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 K;. }. }. r
c0c00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
c0c10 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 OR;.}../*.** Ret
c0c20 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 urn the sector s
c0c30 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
c0c40 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 the underlying b
c0c50 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a lock device for.
c0c60 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ** the specified
c0c70 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 file. This is a
c0c80 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 lmost always 512
c0c90 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 bytes, but may
c0ca0 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 be.** larger for
c0cb0 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a some devices..*
c0cc0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 *.** SQLite code
c0cd0 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 assumes this fu
c0ce0 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 nction cannot fa
c0cf0 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 il. It also assu
c0d00 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 mes that.** if t
c0d10 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 wo files are cre
c0d20 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 ated in the same
c0d30 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 file-system dir
c0d40 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 ectory (i.e..**
c0d50 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 a database and i
c0d60 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 ts journal file)
c0d70 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 that the sector
c0d80 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 size will be th
c0d90 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f e.** same for bo
c0da0 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th..*/.static in
c0db0 74 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 t os2SectorSize(
c0dc0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
c0dd0 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 ){. return SQLI
c0de0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f TE_DEFAULT_SECTO
c0df0 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a R_SIZE;.}../*.**
c0e00 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 Return a vector
c0e10 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 of device chara
c0e20 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 cteristics..*/.s
c0e30 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 tatic int os2Dev
c0e40 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
c0e50 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
c0e60 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 *id){. return 0
c0e70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 ;.}.../*.** Char
c0e80 61 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 acter set conver
c0e90 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 sion objects use
c0ea0 64 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 d by conversion
c0eb0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 routines..*/.sta
c0ec0 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 tic UconvObject
c0ed0 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f ucUtf8 = NULL; /
c0ee0 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 * convert betwee
c0ef0 6e 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d n UTF-8 and UCS-
c0f00 32 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 2 */.static Ucon
c0f10 76 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 vObject uclCp =
c0f20 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 NULL; /* conver
c0f30 74 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 t between local
c0f40 63 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 codepage and UCS
c0f50 2d 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c -2 */../*.** Hel
c0f60 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 per function to
c0f70 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 initialize the c
c0f80 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 onversion object
c0f90 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 s from and to UT
c0fa0 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 F-8..*/.static v
c0fb0 6f 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a oid initUconvObj
c0fc0 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 ects( void ){.
c0fd0 69 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f if( UniCreateUco
c0fe0 6e 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c nvObject( UTF_8,
c0ff0 20 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c &ucUtf8 ) != UL
c1000 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 S_SUCCESS ).
c1010 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 ucUtf8 = NULL;.
c1020 20 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 if ( UniCreateU
c1030 63 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 convObject( (Uni
c1040 43 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 Char *)L"@path=y
c1050 65 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d es", &uclCp ) !=
c1060 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 ULS_SUCCESS ).
c1070 20 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b uclCp = NULL;
c1080 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 .}../*.** Helper
c1090 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 function to fre
c10a0 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e e the conversion
c10b0 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e objects from an
c10c0 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 d to UTF-8..*/.s
c10d0 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 tatic void freeU
c10e0 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 convObjects( voi
c10f0 64 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 d ){. if ( ucUt
c1100 66 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 f8 ). UniFree
c1110 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 UconvObject( ucU
c1120 74 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 tf8 );. if ( uc
c1130 6c 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 lCp ). UniFre
c1140 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 eUconvObject( uc
c1150 6c 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 lCp );. ucUtf8
c1160 3d 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 = NULL;. uclCp
c1170 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a = NULL;.}../*.**
c1180 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e Helper function
c1190 20 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d to convert UTF-
c11a0 38 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 8 filenames to l
c11b0 6f 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 ocal OS/2 codepa
c11c0 67 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 ge..** The two-s
c11d0 74 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 tep process: fir
c11e0 73 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 st convert the i
c11f0 6e 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 ncoming UTF-8 st
c1200 72 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 ring.** into UCS
c1210 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d -2 and then from
c1220 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 UCS-2 to the cu
c1230 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a rrent codepage..
c1240 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
c1250 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 char pointer has
c1260 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f to be freed..*/
c1270 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f .static char *co
c1280 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 nvertUtf8PathToC
c1290 70 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 p( const char *i
c12a0 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 n ){. UniChar t
c12b0 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 empPath[CCHMAXPA
c12c0 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 TH];. char *out
c12d0 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f = (char *)callo
c12e0 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 c( CCHMAXPATH, 1
c12f0 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 );.. if( !out
c1300 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c ). return NUL
c1310 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 L;.. if( !ucUtf
c1320 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 8 || !uclCp ).
c1330 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 initUconvObjec
c1340 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 ts();.. /* dete
c1350 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 rmine string for
c1360 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
c1370 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 of UTF-8 which i
c1380 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 s CP1208 */. if
c1390 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 ( UniStrToUcs( u
c13a0 63 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c cUtf8, tempPath,
c13b0 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 (char *)in, CCH
c13c0 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 MAXPATH ) != ULS
c13d0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 _SUCCESS ). r
c13e0 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 eturn out; /* if
c13f0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c conversion fail
c1400 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d s, return the em
c1410 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 pty string */..
c1420 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 /* conversion f
c1430 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 or current codep
c1440 61 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 age which can be
c1450 20 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 used for paths
c1460 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 */. UniStrFromU
c1470 63 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 cs( uclCp, out,
c1480 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 tempPath, CCHMAX
c1490 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 PATH );.. retur
c14a0 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n out;.}../*.**
c14b0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 Helper function
c14c0 74 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e to convert filen
c14d0 61 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 ames from local
c14e0 63 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d codepage to UTF-
c14f0 38 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 8..** The two-st
c1500 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 ep process: firs
c1510 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e t convert the in
c1520 63 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d coming codepage-
c1530 73 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 specific.** stri
c1540 6e 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e ng into UCS-2 an
c1550 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d d then from UCS-
c1560 32 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 2 to the codepag
c1570 65 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 e of UTF-8..** T
c1580 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 he returned char
c1590 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 pointer has to
c15a0 62 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 be freed..**.**
c15b0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
c15c0 20 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 non-static to b
c15d0 65 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 e able to use th
c15e0 69 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e is in shell.c an
c15f0 64 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 d.** similar app
c1600 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 lications that t
c1610 61 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 ake command line
c1620 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 arguments..*/.c
c1630 68 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 har *convertCpPa
c1640 74 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 thToUtf8( const
c1650 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e char *in ){. Un
c1660 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 iChar tempPath[C
c1670 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 CHMAXPATH];. ch
c1680 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 ar *out = (char
c1690 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 *)calloc( CCHMAX
c16a0 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 PATH, 1 );.. if
c16b0 28 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 ( !out ). ret
c16c0 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 urn NULL;.. if(
c16d0 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c !ucUtf8 || !ucl
c16e0 43 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f Cp ). initUco
c16f0 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 nvObjects();..
c1700 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f /* conversion fo
c1710 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 r current codepa
c1720 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 ge which can be
c1730 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a used for paths *
c1740 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f /. if( UniStrTo
c1750 55 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 Ucs( uclCp, temp
c1760 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e Path, (char *)in
c1770 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 , CCHMAXPATH ) !
c1780 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a = ULS_SUCCESS ).
c1790 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 return out;
c17a0 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e /* if conversion
c17b0 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 fails, return t
c17c0 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 he empty string
c17d0 2a 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 */.. /* determi
c17e0 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 ne string for th
c17f0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 e conversion of
c1800 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 UTF-8 which is C
c1810 50 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 P1208 */. UniSt
c1820 72 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 rFromUcs( ucUtf8
c1830 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c , out, tempPath,
c1840 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a CCHMAXPATH );..
c1850 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a return out;.}.
c1860 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 ./*.** This vect
c1870 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 or defines all t
c1880 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 he methods that
c1890 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 can operate on a
c18a0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c n.** sqlite3_fil
c18b0 65 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 e for os2..*/.st
c18c0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
c18d0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 e3_io_methods os
c18e0 32 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 2IoMethod = {.
c18f0 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1,
c1900 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
c1910 72 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c rsion */. os2Cl
c1920 6f 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a ose,. os2Read,.
c1930 20 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 os2Write,. os
c1940 32 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 2Truncate,. os2
c1950 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 Sync,. os2FileS
c1960 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a ize,. os2Lock,.
c1970 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f os2Unlock,. o
c1980 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c s2CheckReservedL
c1990 6f 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f ock,. os2FileCo
c19a0 6e 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 ntrol,. os2Sect
c19b0 6f 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 orSize,. os2Dev
c19c0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
c19d0 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a cs.};../********
c19e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c19f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1a20 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 ***.** Here ends
c1a30 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 the I/O methods
c1a40 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 that form the s
c1a50 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
c1a60 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 s object..**.**
c1a70 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f The next block o
c1a80 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 f code implement
c1a90 73 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 s the VFS method
c1aa0 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s..*************
c1ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c1ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c1af0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
c1b00 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
c1b10 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a name in zBuf. z
c1b20 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 Buf must be big
c1b30 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c enough to.** hol
c1b40 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 d at pVfs->mxPat
c1b50 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 hname characters
c1b60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c1b70 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 getTempname(int
c1b80 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
c1b90 20 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e ){. static con
c1ba0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
c1bb0 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 zChars[] =.
c1bc0 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f "abcdefghijklmno
c1bd0 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 pqrstuvwxyz".
c1be0 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e "ABCDEFGHIJKLMN
c1bf0 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 OPQRSTUVWXYZ".
c1c00 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a "0123456789";.
c1c10 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 int i, j;. ch
c1c20 61 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b ar zTempPathBuf[
c1c30 33 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 3];. PSZ zTempP
c1c40 61 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d ath = (PSZ)&zTem
c1c50 70 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 pPathBuf;. if(
c1c60 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 sqlite3_temp_dir
c1c70 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 ectory ){. zT
c1c80 65 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 empPath = sqlite
c1c90 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
c1ca0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
c1cb0 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 f( DosScanEnv( (
c1cc0 50 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 PSZ)"TEMP", &zTe
c1cd0 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 mpPath ) ){.
c1ce0 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 if( DosScanEnv
c1cf0 28 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a ( (PSZ)"TMP", &z
c1d00 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 TempPath ) ){.
c1d10 20 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 if( DosSca
c1d20 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 nEnv( (PSZ)"TMPD
c1d30 49 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 IR", &zTempPath
c1d40 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
c1d50 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d ULONG ulDriveNum
c1d60 20 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 = 0, ulDriveMap
c1d70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
c1d80 20 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 DosQueryCurrent
c1d90 44 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 Disk( &ulDriveNu
c1da0 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 m, &ulDriveMap )
c1db0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72 ;. spr
c1dc0 69 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 intf( (char*)zTe
c1dd0 6d 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 mpPath, "%c:", (
c1de0 63 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 char)( 'A' + ulD
c1df0 72 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b riveNum - 1 ) );
c1e00 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
c1e10 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f }. }. }. /
c1e20 2a 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 * Strip off a tr
c1e30 61 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f ailing slashes o
c1e40 72 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f r backslashes, o
c1e50 74 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c therwise we woul
c1e60 64 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c d get *. * mul
c1e70 74 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 tiple (back)slas
c1e80 68 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 hes which causes
c1e90 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 DosOpen() to fa
c1ea0 69 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 il.
c1eb0 20 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 *. * Trailing
c1ec0 20 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 spaces are not
c1ed0 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e allowed, either.
c1ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c1ef0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 */.
c1f00 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c j = sqlite3Strl
c1f10 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b en30(zTempPath);
c1f20 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 . while( j > 0
c1f30 26 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a && ( zTempPath[j
c1f40 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a -1] == '\\' || z
c1f50 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d TempPath[j-1] ==
c1f60 20 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 '/'.
c1f70 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d || zTem
c1f80 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 pPath[j-1] == '
c1f90 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a ' ) ){. j--;.
c1fa0 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b }. zTempPath[
c1fb0 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 j] = '\0';. if(
c1fc0 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 !sqlite3_temp_d
c1fd0 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
c1fe0 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 char *zTempPathU
c1ff0 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 TF = convertCpPa
c2000 74 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 thToUtf8( zTempP
c2010 61 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ath );. sqlit
c2020 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 e3_snprintf( nBu
c2030 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 f-30, zBuf,.
c2040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2050 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 "%s\\"SQLITE_T
c2060 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c EMP_FILE_PREFIX,
c2070 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b zTempPathUTF );
c2080 0a 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 . free( zTemp
c2090 50 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c PathUTF );. }el
c20a0 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f se{. sqlite3_
c20b0 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 snprintf( nBuf-3
c20c0 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 0, zBuf,.
c20d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
c20e0 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 %s\\"SQLITE_TEMP
c20f0 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 _FILE_PREFIX, zT
c2100 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 empPath );. }.
c2110 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c j = sqlite3Strl
c2120 65 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 en30( zBuf );.
c2130 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
c2140 73 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d ss( 20, &zBuf[j]
c2150 20 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 );. for( i = 0
c2160 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a ; i < 20; i++, j
c2170 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a ++ ){. zBuf[j
c2180 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 ] = (char)zChars
c2190 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 [ ((unsigned cha
c21a0 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 r)zBuf[j])%(size
c21b0 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b of(zChars)-1) ];
c21c0 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d . }. zBuf[j] =
c21d0 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 0;. OSTRACE2(
c21e0 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 "TEMP FILENAME:
c21f0 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 %s\n", zBuf );.
c2200 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c2210 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 K;.}.../*.** Tur
c2220 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 n a relative pat
c2230 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c hname into a ful
c2240 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 l pathname. Wri
c2250 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 te the full.** p
c2260 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 athname into zFu
c2270 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 ll[]. zFull[] w
c2280 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 ill be at least
c2290 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 pVfs->mxPathname
c22a0 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a .** bytes in siz
c22b0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
c22c0 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 os2FullPathname
c22d0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
c22e0 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 *pVfs,
c22f0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 /* Pointer to vf
c2300 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f s object */. co
c2310 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 nst char *zRelat
c2320 69 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 ive, /* Pos
c2330 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 sibly relative i
c2340 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 nput path */. i
c2350 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 nt nFull,
c2360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
c2370 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
c2380 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
c2390 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 char *zFull
c23a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c23b0 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a Output buffer *
c23c0 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 /.){. char *zRe
c23d0 6c 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 lativeCp = conve
c23e0 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 rtUtf8PathToCp(
c23f0 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 zRelative );. c
c2400 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d har zFullCp[CCHM
c2410 41 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a AXPATH] = "\0";.
c2420 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 char *zFullUTF
c2430 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 ;. APIRET rc =
c2440 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f DosQueryPathInfo
c2450 28 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 ( zRelativeCp, F
c2460 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 IL_QUERYFULLNAME
c2470 2c 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 , zFullCp,.
c2480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2490 20 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 CCHMA
c24a0 58 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 XPATH );. free(
c24b0 20 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a zRelativeCp );.
c24c0 20 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e zFullUTF = con
c24d0 76 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 vertCpPathToUtf8
c24e0 28 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 ( zFullCp );. s
c24f0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
c2500 20 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a nFull, zFull, z
c2510 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 FullUTF );. fre
c2520 65 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 e( zFullUTF );.
c2530 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f return rc == NO
c2540 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
c2550 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
c2560 52 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 R;.}.../*.** Ope
c2570 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 n a file..*/.sta
c2580 74 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 tic int os2Open(
c2590 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
c25a0 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
c25b0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a /* Not used */.
c25c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
c25d0 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ame,
c25e0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 /* Name of the f
c25f0 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ile */. sqlite3
c2600 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 _file *id,
c2610 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
c2620 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 the SQLite file
c2630 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 handle here */.
c2640 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 int flags,
c2650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c2660 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 * Open mode flag
c2670 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 s */. int *pOut
c2680 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 Flags
c2690 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 /* Status r
c26a0 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 eturn flags */.)
c26b0 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 {. HFILE h;. U
c26c0 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 LONG ulFileAttri
c26d0 62 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d bute = FILE_NORM
c26e0 41 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 AL;. ULONG ulOp
c26f0 65 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 enFlags = 0;. U
c2700 4c 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 LONG ulOpenMode
c2710 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a = 0;. os2File *
c2720 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
c2730 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 *)id;. APIRET r
c2740 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
c2750 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a ULONG ulAction;.
c2760 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b char *zNameCp;
c2770 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 . char zTmpname
c2780 5b 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 [CCHMAXPATH+1];
c2790 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 /* Buffer to
c27a0 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d hold name of tem
c27b0 70 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 p file */.. /*
c27c0 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
c27d0 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 gument to this f
c27e0 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c unction is NULL,
c27f0 20 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a generate a . *
c2800 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 * temporary file
c2810 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 name to use .
c2820 2a 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 */. if( !zName
c2830 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 ){. int rc =
c2840 67 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d getTempname(CCHM
c2850 41 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 AXPATH+1, zTmpna
c2860 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 me);. if( rc!
c2870 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
c2880 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
c2890 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d }. zName =
c28a0 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a zTmpname;. }..
c28b0 0a 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 . memset( pFile
c28c0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 , 0, sizeof(*pFi
c28d0 6c 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 le) );.. OSTRAC
c28e0 45 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 E2( "OPEN want %
c28f0 64 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a d\n", flags );..
c2900 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 if( flags & SQ
c2910 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
c2920 49 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 ITE ){. ulOpe
c2930 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 nMode |= OPEN_AC
c2940 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a CESS_READWRITE;.
c2950 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f OSTRACE1( "O
c2960 50 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e PEN read/write\n
c2970 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 " );. }else{.
c2980 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 ulOpenMode |=
c2990 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 OPEN_ACCESS_READ
c29a0 4f 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 ONLY;. OSTRAC
c29b0 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f E1( "OPEN read o
c29c0 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 nly\n" );. }..
c29d0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
c29e0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 ITE_OPEN_CREATE
c29f0 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 ){. ulOpenFla
c2a00 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f gs |= OPEN_ACTIO
c2a10 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 N_OPEN_IF_EXISTS
c2a20 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 | OPEN_ACTION_C
c2a30 52 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 REATE_IF_NEW;.
c2a40 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
c2a50 4e 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 N open new/creat
c2a60 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b e\n" );. }else{
c2a70 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 . ulOpenFlags
c2a80 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f |= OPEN_ACTION_
c2a90 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c OPEN_IF_EXISTS |
c2aa0 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 OPEN_ACTION_FAI
c2ab0 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 L_IF_NEW;. OS
c2ac0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 TRACE1( "OPEN op
c2ad0 65 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 en existing\n" )
c2ae0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 ;. }.. if( fla
c2af0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
c2b00 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 _MAIN_DB ){.
c2b10 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 ulOpenMode |= OP
c2b20 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e EN_SHARE_DENYNON
c2b30 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 E;. OSTRACE1(
c2b40 20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 "OPEN share rea
c2b50 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 d/write\n" );.
c2b60 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 }else{. ulOpe
c2b70 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 nMode |= OPEN_SH
c2b80 41 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 ARE_DENYWRITE;.
c2b90 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 OSTRACE1( "OP
c2ba0 45 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e EN share read on
c2bb0 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 ly\n" );. }..
c2bc0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
c2bd0 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
c2be0 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 CLOSE ){. cha
c2bf0 72 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 r pathUtf8[CCHMA
c2c00 58 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e XPATH];.#ifdef N
c2c10 44 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 DEBUG /* when de
c2c20 62 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 bugging we want
c2c30 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 to make sure it
c2c40 69 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 is deleted */.
c2c50 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 ulFileAttribut
c2c60 65 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b e = FILE_HIDDEN;
c2c70 0a 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 .#endif. os2F
c2c80 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 ullPathname( pVf
c2c90 73 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 s, zName, CCHMAX
c2ca0 50 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 PATH, pathUtf8 )
c2cb0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 ;. pFile->pat
c2cc0 68 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 hToDel = convert
c2cd0 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 Utf8PathToCp( pa
c2ce0 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 thUtf8 );. OS
c2cf0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 TRACE1( "OPEN hi
c2d00 64 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 dden/delete on c
c2d10 6c 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 lose file attrib
c2d20 75 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c utes\n" );. }el
c2d30 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 se{. pFile->p
c2d40 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b athToDel = NULL;
c2d50 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 . OSTRACE1( "
c2d60 4f 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 OPEN normal file
c2d70 20 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b attribute\n" );
c2d80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 . }.. /* alway
c2d90 73 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d s open in random
c2da0 20 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 access mode for
c2db0 20 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 possibly better
c2dc0 20 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 speed */. ulOp
c2dd0 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 enMode |= OPEN_F
c2de0 4c 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 LAGS_RANDOM;. u
c2df0 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 lOpenMode |= OPE
c2e00 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f N_FLAGS_FAIL_ON_
c2e10 45 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d ERROR;. ulOpenM
c2e20 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 ode |= OPEN_FLAG
c2e30 53 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 S_NOINHERIT;..
c2e40 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 zNameCp = conver
c2e50 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a tUtf8PathToCp( z
c2e60 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 Name );. rc = D
c2e70 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 osOpen( (PSZ)zNa
c2e80 6d 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 meCp,.
c2e90 20 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 &h,.
c2ea0 20 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 &ulAct
c2eb0 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ion,.
c2ec0 20 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 0L,.
c2ed0 20 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 ulFileA
c2ee0 74 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 ttribute,.
c2ef0 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e ulOpen
c2f00 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 Flags,.
c2f10 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 ulOpenMod
c2f20 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
c2f30 20 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 (PEAOP2)NULL
c2f40 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 );. free( zName
c2f50 43 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 Cp );. if( rc !
c2f60 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
c2f70 20 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 OSTRACE7( "OPE
c2f80 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 N Invalid handle
c2f90 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 rc=%d: zName=%s
c2fa0 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c , ulAction=%#lx,
c2fb0 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c ulAttr=%#lx, ul
c2fc0 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f Flags=%#lx, ulMo
c2fd0 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 de=%#lx\n",.
c2fe0 20 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e rc, zN
c2ff0 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 ame, ulAction, u
c3000 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 lFileAttribute,
c3010 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f ulOpenFlags, ulO
c3020 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 penMode );. i
c3030 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f f( pFile->pathTo
c3040 44 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 Del ). free
c3050 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 ( pFile->pathToD
c3060 65 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d el );. pFile-
c3070 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c >pathToDel = NUL
c3080 4c 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 L;. if( flags
c3090 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & SQLITE_OPEN_R
c30a0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 EADWRITE ){.
c30b0 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 OSTRACE2( "OPE
c30c0 4e 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e N %d Invalid han
c30d0 64 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 dle\n", ((flags
c30e0 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 | SQLITE_OPEN_RE
c30f0 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 ADONLY) & ~SQLIT
c3100 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
c3110 29 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ) );. retur
c3120 6e 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c n os2Open( pVfs,
c3130 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 zName, id,.
c3140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3150 20 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 ((flags | SQLI
c3160 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
c3170 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e ) & ~SQLITE_OPEN
c3180 5f 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 _READWRITE),.
c3190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c31a0 20 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a pOutFlags );.
c31b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
c31c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
c31d0 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 ANTOPEN;. }.
c31e0 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c }.. if( pOutFl
c31f0 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 ags ){. *pOut
c3200 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 Flags = flags &
c3210 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
c3220 57 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f WRITE ? SQLITE_O
c3230 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 PEN_READWRITE :
c3240 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
c3250 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 ONLY;. }.. pFi
c3260 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f le->pMethod = &o
c3270 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 s2IoMethod;. pF
c3280 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 ile->h = h;. Op
c3290 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 enCounter(+1);.
c32a0 20 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e OSTRACE3( "OPEN
c32b0 20 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 %d pOutFlags=%d
c32c0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
c32d0 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 OutFlags );. re
c32e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c32f0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
c3300 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a the named file..
c3310 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c3320 32 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 2Delete(. sqlit
c3330 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
c3340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3350 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e /* Not used on
c3360 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 os2 */. const
c3370 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
c3380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3390 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 /* Name of file
c33a0 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 to delete */.
c33b0 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 int syncDir
c33c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c33d0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 /* Not us
c33e0 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a ed on os2 */.){.
c33f0 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f APIRET rc = NO
c3400 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a _ERROR;. char *
c3410 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f zFilenameCp = co
c3420 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 nvertUtf8PathToC
c3430 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a p( zFilename );.
c3440 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
c3450 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
c3460 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b _IOERR_DELETE );
c3470 0a 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 . rc = DosDelet
c3480 65 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d e( (PSZ)zFilenam
c3490 65 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a eCp );. free( z
c34a0 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 FilenameCp );.
c34b0 4f 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 OSTRACE2( "DELET
c34c0 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 E \"%s\"\n", zFi
c34d0 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 lename );. retu
c34e0 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f rn rc == NO_ERRO
c34f0 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 R ? SQLITE_OK :
c3500 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c SQLITE_IOERR_DEL
c3510 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 ETE;.}../*.** Ch
c3520 65 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 eck the existanc
c3530 65 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 e and status of
c3540 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
c3550 63 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 c int os2Access(
c3560 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
c3570 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 pVfs, /*
c3580 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 Not used on os2
c3590 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
c35a0 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f *zFilename, /
c35b0 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 * Name of file t
c35c0 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 o check */. int
c35d0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
c35e0 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f /* Type o
c35f0 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f f test to make o
c3600 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 n this file */.
c3610 20 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 int *pOut
c3620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
c3630 69 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 ite results here
c3640 20 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 */.){. FILESTA
c3650 54 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 TUS3 fsts3Config
c3660 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 Info;. APIRET r
c3670 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
c3680 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 char *zFilenameC
c3690 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 p = convertUtf8P
c36a0 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 athToCp( zFilena
c36b0 6d 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 me );.. memset(
c36c0 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 &fsts3ConfigInf
c36d0 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 o, 0, sizeof(fst
c36e0 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b s3ConfigInfo) );
c36f0 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 . rc = DosQuery
c3700 50 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a PathInfo( (PSZ)z
c3710 46 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f FilenameCp, FIL_
c3720 53 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 STANDARD,.
c3730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3740 20 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 &fsts3ConfigI
c3750 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 nfo, sizeof(FILE
c3760 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 STATUS3) );. fr
c3770 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 ee( zFilenameCp
c3780 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 );. OSTRACE4( "
c3790 41 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 ACCESS fsts3Conf
c37a0 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d igInfo.attrFile=
c37b0 25 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 %d flags=%d rc=%
c37c0 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 d\n",.
c37d0 20 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 fsts3ConfigInf
c37e0 6f 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 o.attrFile, flag
c37f0 73 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 s, rc );. switc
c3800 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 h( flags ){.
c3810 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 case SQLITE_ACCE
c3820 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 SS_READ:. cas
c3830 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
c3840 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 EXISTS:. rc
c3850 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 = (rc == NO_ERR
c3860 4f 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 OR);. OSTRA
c3870 43 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 CE3( "ACCESS %s
c3880 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 access of read a
c3890 6e 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 nd exists rc=%d
c38a0 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 \n", zFilename,
c38b0 72 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 rc );. brea
c38c0 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 k;. case SQLI
c38d0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
c38e0 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 ITE:. rc =
c38f0 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 (rc == NO_ERROR)
c3900 20 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 && ( (fsts3Conf
c3910 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 igInfo.attrFile
c3920 26 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 & FILE_READONLY)
c3930 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f == 0 );. O
c3940 53 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 STRACE3( "ACCESS
c3950 20 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 %s access of re
c3960 61 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c ad/write rc=%d\
c3970 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 n", zFilename, r
c3980 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b c );. break
c3990 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 ;. default:.
c39a0 20 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49 assert( !"I
c39b0 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 nvalid flags arg
c39c0 75 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 ument" );. }.
c39d0 2a 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 *pOut = rc;. re
c39e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
c39f0 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }...#ifndef SQLI
c3a00 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
c3a10 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 ENSION./*.** Int
c3a20 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e erfaces for open
c3a30 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 ing a shared lib
c3a40 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e rary, finding en
c3a50 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 try points.** wi
c3a60 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 thin the shared
c3a70 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f library, and clo
c3a80 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 sing the shared
c3a90 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a library..*/./*.*
c3aa0 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 * Interfaces for
c3ab0 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 opening a share
c3ac0 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 d library, findi
c3ad0 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a ng entry points.
c3ae0 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 ** within the sh
c3af0 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e ared library, an
c3b00 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 d closing the sh
c3b10 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f ared library..*/
c3b20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 .static void *os
c3b30 32 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 2DlOpen(sqlite3_
c3b40 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 vfs *pVfs, const
c3b50 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
c3b60 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 ){. UCHAR loadE
c3b70 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 rr[256];. HMODU
c3b80 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 LE hmod;. APIRE
c3b90 54 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 T rc;. char *zF
c3ba0 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 ilenameCp = conv
c3bb0 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 ertUtf8PathToCp(
c3bc0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 zFilename);. rc
c3bd0 20 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 = DosLoadModule
c3be0 28 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 ((PSZ)loadErr, s
c3bf0 69 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 izeof(loadErr),
c3c00 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d zFilenameCp, &hm
c3c10 6f 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c od);. free(zFil
c3c20 65 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 enameCp);. retu
c3c30 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f rn rc != NO_ERRO
c3c40 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 R ? 0 : (void*)h
c3c50 6d 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e mod;.}./*.** A n
c3c60 6f 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 o-op since the e
c3c70 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
c3c80 75 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 urned on the Dos
c3c90 4c 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e LoadModule call.
c3ca0 0a 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 .** os2Dlopen re
c3cb0 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f turns zero if Do
c3cc0 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e sLoadModule is n
c3cd0 6f 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a ot successful..*
c3ce0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 /.static void os
c3cf0 32 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 2DlError(sqlite3
c3d00 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
c3d10 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
c3d20 4f 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a Out){./* no-op *
c3d30 2f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 /.}.static void
c3d40 2a 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 *os2DlSym(sqlite
c3d50 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
c3d60 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 d *pHandle, cons
c3d70 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 t char *zSymbol)
c3d80 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 {. PFN pfn;. A
c3d90 50 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d PIRET rc;. rc =
c3da0 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 DosQueryProcAdd
c3db0 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 r((HMODULE)pHand
c3dc0 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c le, 0L, zSymbol,
c3dd0 20 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 &pfn);. if( rc
c3de0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a != NO_ERROR ){.
c3df0 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 /* if the sy
c3e00 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 mbol itself was
c3e10 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 not found, searc
c3e20 68 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 h again for the
c3e30 73 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 same. * symb
c3e40 6f 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 ol with an extra
c3e50 20 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 underscore, tha
c3e60 74 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 t might be neede
c3e70 64 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 d depending.
c3e80 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e * on the callin
c3e90 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a g convention */.
c3ea0 20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f char _zSymbo
c3eb0 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 l[256] = "_";.
c3ec0 20 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 strncat(_zSymb
c3ed0 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 ol, zSymbol, 255
c3ee0 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 );. rc = DosQ
c3ef0 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d ueryProcAddr((HM
c3f00 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 ODULE)pHandle, 0
c3f10 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 L, _zSymbol, &pf
c3f20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e n);. }. return
c3f30 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 rc != NO_ERROR
c3f40 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e ? 0 : (void*)pfn
c3f50 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
c3f60 6f 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 os2DlClose(sqlit
c3f70 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f e3_vfs *pVfs, vo
c3f80 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 id *pHandle){.
c3f90 44 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 DosFreeModule((H
c3fa0 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b MODULE)pHandle);
c3fb0 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 .}.#else /* if S
c3fc0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
c3fd0 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 EXTENSION is def
c3fe0 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 ined: */. #defi
c3ff0 6e 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 ne os2DlOpen 0.
c4000 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 #define os2DlEr
c4010 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 ror 0. #define
c4020 6f 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 os2DlSym 0. #de
c4030 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 fine os2DlClose
c4040 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
c4050 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 Write up to nBu
c4060 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f f bytes of rando
c4070 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e mness into zBuf.
c4080 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c4090 73 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c s2Randomness(sql
c40a0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
c40b0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
c40c0 7a 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 zBuf ){. int n
c40d0 3d 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 = 0;.#if defined
c40e0 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 (SQLITE_TEST).
c40f0 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 n = nBuf;. mems
c4100 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 et(zBuf, 0, nBuf
c4110 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 );.#else. int s
c4120 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a izeofULong = siz
c4130 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 eof(ULONG);. if
c4140 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 ( (int)sizeof(DA
c4150 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 TETIME) <= nBuf
c4160 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 - n ){. DATET
c4170 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 IME x;. DosGe
c4180 74 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 tDateTime(&x);.
c4190 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
c41a0 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 n], &x, sizeof(x
c41b0 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a ));. n += siz
c41c0 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 eof(x);. }.. i
c41d0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c f( sizeofULong <
c41e0 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 = nBuf - n ){.
c41f0 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 PPIB ppib;.
c4200 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b DosGetInfoBlock
c4210 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a s(NULL, &ppib);.
c4220 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
c4230 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f [n], &ppib->pib_
c4240 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f ulpid, sizeofULo
c4250 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 ng);. n += si
c4260 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a zeofULong;. }..
c4270 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
c4280 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
c4290 0a 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a . PTIB ptib;.
c42a0 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c DosGetInfoBl
c42b0 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c ocks(&ptib, NULL
c42c0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
c42d0 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 Buf[n], &ptib->t
c42e0 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 ib_ptib2->tib2_u
c42f0 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e ltid, sizeofULon
c4300 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a g);. n += siz
c4310 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 eofULong;. }..
c4320 20 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 /* if we still
c4330 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 haven't filled t
c4340 68 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68 he buffer yet th
c4350 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c e following will
c4360 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 */. /* grab ev
c4370 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e erything once in
c4380 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 stead of making
c4390 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f several calls fo
c43a0 72 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 r a single item
c43b0 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 */. if( sizeofU
c43c0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e Long <= nBuf - n
c43d0 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c ){. ULONG ul
c43e0 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d SysInfo[QSV_MAX]
c43f0 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 ;. DosQuerySy
c4400 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 sInfo(1L, QSV_MA
c4410 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 X, ulSysInfo, si
c4420 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f zeofULong * QSV_
c4430 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 MAX);.. memcp
c4440 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 y(&zBuf[n], &ulS
c4450 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f ysInfo[QSV_MS_CO
c4460 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 UNT - 1], sizeof
c4470 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d ULong);. n +=
c4480 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 sizeofULong;..
c4490 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f if( sizeofULo
c44a0 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 ng <= nBuf - n )
c44b0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 {. memcpy(&
c44c0 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 zBuf[n], &ulSysI
c44d0 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e nfo[QSV_TIMER_IN
c44e0 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a TERVAL - 1], siz
c44f0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 eofULong);.
c4500 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e n += sizeofULon
c4510 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 g;. }. if(
c4520 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 sizeofULong <=
c4530 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 nBuf - n ){.
c4540 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
c4550 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 ], &ulSysInfo[QS
c4560 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c V_TIME_LOW - 1],
c4570 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 sizeofULong);.
c4580 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 n += sizeof
c4590 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 ULong;. }.
c45a0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
c45b0 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
c45c0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 memcpy(&zB
c45d0 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 uf[n], &ulSysInf
c45e0 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 o[QSV_TIME_HIGH
c45f0 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e - 1], sizeofULon
c4600 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 g);. n += s
c4610 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 izeofULong;.
c4620 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 }. if( sizeof
c4630 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 ULong <= nBuf -
c4640 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 n ){. memcp
c4650 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 y(&zBuf[n], &ulS
c4660 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 ysInfo[QSV_TOTAV
c4670 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a AILMEM - 1], siz
c4680 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 eofULong);.
c4690 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e n += sizeofULon
c46a0 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e g;. }. }.#en
c46b0 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b dif.. return n;
c46c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 .}../*.** Sleep
c46d0 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 for a little whi
c46e0 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 le. Return the
c46f0 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 amount of time s
c4700 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 lept..** The arg
c4710 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d ument is the num
c4720 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
c4730 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 nds we want to s
c4740 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 leep..** The ret
c4750 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 urn value is the
c4760 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f number of micro
c4770 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 seconds of sleep
c4780 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 actually.** req
c4790 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 uested from the
c47a0 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 underlying opera
c47b0 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e ting system, a n
c47c0 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d umber which.** m
c47d0 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20 ight be greater
c47e0 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
c47f0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 the argument, b
c4800 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 ut not less.** t
c4810 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 han the argument
c4820 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c4830 6f 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 os2Sleep( sqlite
c4840 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
c4850 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 microsec ){. D
c4860 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 osSleep( (micros
c4870 65 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 ec/1000) );. re
c4880 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d turn microsec;.}
c4890 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
c48a0 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 owing variable,
c48b0 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d if set to a non-
c48c0 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f zero value, beco
c48d0 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a mes the result.*
c48e0 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 * returned from
c48f0 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
c4900 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 Time(). This is
c4910 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e used for testin
c4920 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c g..*/.#ifdef SQL
c4930 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
c4940 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c4950 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 current_time = 0
c4960 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
c4970 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 Find the current
c4980 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 time (in Univer
c4990 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 sal Coordinated
c49a0 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 Time). Write th
c49b0 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d e.** current tim
c49c0 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 e and date as a
c49d0 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 Julian Day numbe
c49e0 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e r into *prNow an
c49f0 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 d.** return 0.
c4a00 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
c4a10 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 time and date ca
c4a20 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a nnot be found..*
c4a30 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 /.int os2Current
c4a40 54 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 Time( sqlite3_vf
c4a50 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 s *pVfs, double
c4a60 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 *prNow ){. doub
c4a70 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 le now;. SHORT
c4a80 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 minute; /* needs
c4a90 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 to be able to c
c4aa0 6f 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 ope with negativ
c4ab0 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 e timezone offse
c4ac0 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 t */. USHORT se
c4ad0 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 cond, hour,.
c4ae0 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c day, month,
c4af0 20 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d year;. DATETIM
c4b00 45 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 E dt;. DosGetDa
c4b10 74 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 teTime( &dt );.
c4b20 20 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 second = (USHOR
c4b30 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 T)dt.seconds;.
c4b40 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 minute = (SHORT)
c4b50 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e dt.minutes + dt.
c4b60 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 timezone;. hour
c4b70 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f = (USHORT)dt.ho
c4b80 75 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 urs;. day = (US
c4b90 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d HORT)dt.day;. m
c4ba0 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 onth = (USHORT)d
c4bb0 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 t.month;. year
c4bc0 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 = (USHORT)dt.yea
c4bd0 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 r;.. /* Calcula
c4be0 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a tions from http:
c4bf0 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c //www.astro.keel
c4c00 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 e.ac.uk/~rno/Ast
c4c10 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a ronomy/hjd.html.
c4c20 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e http://www.
c4c30 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 astro.keele.ac.u
c4c40 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 k/~rno/Astronomy
c4c50 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 /hjd-0.1.c */.
c4c60 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 /* Calculate the
c4c70 20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a Julian days */.
c4c80 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 now = day - 32
c4c90 30 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 076 +. 1461*(
c4ca0 79 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d year + 4800 + (m
c4cb0 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 onth - 14)/12)/4
c4cc0 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 +. 367*(mont
c4cd0 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d h - 2 - (month -
c4ce0 20 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 14)/12*12)/12 -
c4cf0 0a 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 . 3*((year +
c4d00 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 4900 + (month -
c4d10 31 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 14)/12)/100)/4;.
c4d20 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 . /* Add the fr
c4d30 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 actional hours,
c4d40 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 mins and seconds
c4d50 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f */. now += (ho
c4d60 75 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b ur + 12.0)/24.0;
c4d70 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 . now += minute
c4d80 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b /1440.0;. now +
c4d90 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 = second/86400.0
c4da0 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 ;. *prNow = now
c4db0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
c4dc0 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 TEST. if( sqlit
c4dd0 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
c4de0 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 ){. *prNow =
c4df0 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f sqlite3_current_
c4e00 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 time/86400.0 + 2
c4e10 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 440587.5;. }.#e
c4e20 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b ndif. return 0;
c4e30 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .}..static int o
c4e40 73 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 s2GetLastError(s
c4e50 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
c4e60 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
c4e70 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 *zBuf){. retur
c4e80 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e n 0;.}../*.** In
c4e90 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 itialize and dei
c4ea0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 nitialize the op
c4eb0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
c4ec0 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c nterface..*/.SQL
c4ed0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c4ee0 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 te3_os_init(void
c4ef0 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
c4f00 74 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d te3_vfs os2Vfs =
c4f10 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 {. 1,
c4f20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
c4f30 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a rsion */. siz
c4f40 65 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 eof(os2File),
c4f50 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 /* szOsFile */.
c4f60 20 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 CCHMAXPATH,
c4f70 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e /* mxPathn
c4f80 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
c4f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c4fa0 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f pNext */. "o
c4fb0 73 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 s2",
c4fc0 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 /* zName */.
c4fd0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
c4fe0 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 /* pAppData
c4ff0 2a 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c */.. os2Open,
c5000 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f /* xO
c5010 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 pen */. os2De
c5020 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a lete, /*
c5030 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 xDelete */.
c5040 6f 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 os2Access,
c5050 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f /* xAccess */
c5060 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 . os2FullPath
c5070 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c name, /* xFull
c5080 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 Pathname */.
c5090 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 os2DlOpen,
c50a0 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f /* xDlOpen */
c50b0 0a 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c . os2DlError,
c50c0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 /* xDlEr
c50d0 72 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c ror */. os2Dl
c50e0 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Sym, /*
c50f0 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f xDlSym */. o
c5100 73 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 s2DlClose,
c5110 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f /* xDlClose */
c5120 0a 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 . os2Randomne
c5130 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 ss, /* xRand
c5140 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 omness */. os
c5150 32 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 2Sleep,
c5160 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 /* xSleep */.
c5170 20 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 os2CurrentTime
c5180 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 , /* xCurrent
c5190 54 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 Time */. os2G
c51a0 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f etLastError /
c51b0 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 * xGetLastError
c51c0 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 */. };. sqlite
c51d0 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 3_vfs_register(&
c51e0 6f 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e os2Vfs, 1);. in
c51f0 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 itUconvObjects()
c5200 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
c5210 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 E_OK;.}.SQLITE_A
c5220 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
c5230 73 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 s_end(void){. f
c5240 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 reeUconvObjects(
c5250 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
c5260 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 TE_OK;.}..#endif
c5270 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 /* SQLITE_OS_OS
c5280 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2 */../*********
c5290 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f ***** End of os_
c52a0 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os2.c **********
c52b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c52c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c52d0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
c52e0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
c52f0 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a os_unix.c *****
c5300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5320 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
c5330 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 May 22.**.** Th
c5340 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
c5350 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
c5360 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
c5370 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
c5380 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
c5390 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
c53a0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
c53b0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
c53c0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
c53d0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
c53e0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
c53f0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
c5400 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
c5410 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
c5420 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
c5430 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
c5440 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
c5450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
c54a0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
c54b0 61 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 ains the VFS imp
c54c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 lementation for
c54d0 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 unix-like operat
c54e0 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 ing systems.** i
c54f0 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 nclude Linux, Ma
c5500 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c cOSX, *BSD, QNX,
c5510 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 VxWorks, AIX, H
c5520 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e PUX, and others.
c5530 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
c5540 20 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 actually severa
c5550 6c 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 l different VFS
c5560 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
c5570 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a in this file..**
c5580 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 The differences
c5590 20 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 are in the way
c55a0 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e that file lockin
c55b0 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 g is done. The
c55c0 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 default.** imple
c55d0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 mentation uses P
c55e0 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f osix Advisory Lo
c55f0 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 cks. Alternativ
c5600 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
c5610 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 s.** use flock()
c5620 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 , dot-files, var
c5630 69 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 ious proprietary
c5640 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 locking schemas
c5650 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 , or simply.** s
c5660 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 kip locking all
c5670 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 together..**.**
c5680 54 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 This source file
c5690 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e is organized in
c56a0 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 to divisions whe
c56b0 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 re the logic for
c56c0 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 various.** subf
c56d0 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 unctions is cont
c56e0 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 ained within the
c56f0 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 appropriate div
c5700 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a ision. PLEASE.*
c5710 2a 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 * KEEP THE STRUC
c5720 54 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c TURE OF THIS FIL
c5730 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 E INTACT. New c
c5740 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c ode should be pl
c5750 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 aced.** in the c
c5760 6f 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 orrect division
c5770 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c and should be cl
c5780 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a early labeled..*
c5790 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 *.** The layout
c57a0 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 of divisions is
c57b0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
c57c0 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 * * General-p
c57d0 75 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 urpose declarati
c57e0 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 ons and utility
c57f0 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 functions..**
c5800 2a 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 * Unique file I
c5810 44 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 D logic used by
c5820 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 VxWorks..** *
c5830 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 Various locking
c5840 20 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 primitive imple
c5850 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 mentations (all
c5860 65 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 except proxy loc
c5870 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b king):.** +
c5880 20 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 for Posix Advis
c5890 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 ory Locks.**
c58a0 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f + for no-op lo
c58b0 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f cks.** + fo
c58c0 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 r dot-file locks
c58d0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 .** + for f
c58e0 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a lock() locking.*
c58f0 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d * + for nam
c5900 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 ed semaphore loc
c5910 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 ks (VxWorks only
c5920 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 ).** + for
c5930 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c AFP filesystem l
c5940 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c ocks (MacOSX onl
c5950 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 y).** * sqlit
c5960 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 e3_file methods
c5970 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 not associated w
c5980 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 ith locking..**
c5990 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 * Definitions
c59a0 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d of sqlite3_io_m
c59b0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 ethods objects f
c59c0 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a or all locking.*
c59d0 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 * methods p
c59e0 6c 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e lus "finder" fun
c59f0 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 ctions for each
c5a00 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a locking method..
c5a10 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f ** * sqlite3_
c5a20 76 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 vfs method imple
c5a30 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 mentations..**
c5a40 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d * Locking prim
c5a50 69 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 itives for the p
c5a60 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e roxy uber-lockin
c5a70 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 g-method. (MacOS
c5a80 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 X only).** *
c5a90 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 Definitions of s
c5aa0 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
c5ab0 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 ts for all locki
c5ac0 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 ng methods.**
c5ad0 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e plus implemen
c5ae0 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 tations of sqlit
c5af0 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 e3_os_init() and
c5b00 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 sqlite3_os_end(
c5b10 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 )..*/.#if SQLITE
c5b20 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 _OS_UNIX
c5b30 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 /* This fi
c5b40 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e le is used on un
c5b50 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a ix only */../*.*
c5b60 2a 20 54 68 65 72 65 20 61 72 65 20 76 61 72 69 * There are vari
c5b70 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 ous methods for
c5b80 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 file locking use
c5b90 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 d for concurrenc
c5ba0 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a y.** control:.**
c5bb0 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c .** 1. POSIX l
c5bc0 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 ocking (the defa
c5bd0 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f ult),.** 2. No
c5be0 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 locking,.** 3
c5bf0 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 . Dot-file locki
c5c00 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 ng,.** 4. floc
c5c10 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 k() locking,.**
c5c20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67 5. AFP locking
c5c30 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 (OSX only),.**
c5c40 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58 6. Named POSIX
c5c50 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57 semaphores (VXW
c5c60 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 orks only),.**
c5c70 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 7. proxy lockin
c5c80 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a g. (OSX only).**
c5c90 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c .** Styles 4, 5,
c5ca0 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20 and 7 are only
c5cb0 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c available of SQL
c5cc0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
c5cd0 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64 NG_STYLE.** is d
c5ce0 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68 efined to 1. Th
c5cf0 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f e SQLITE_ENABLE_
c5d00 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c LOCKING_STYLE al
c5d10 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d so enables autom
c5d20 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f atic.** selectio
c5d30 6e 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72 n of the appropr
c5d40 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 iate locking sty
c5d50 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 le based on the
c5d60 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68 filesystem.** wh
c5d70 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ere the database
c5d80 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a is located. .*
c5d90 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
c5da0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
c5db0 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 KING_STYLE).# i
c5dc0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c f defined(__APPL
c5dd0 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 E__).# define
c5de0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
c5df0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 OCKING_STYLE 1.#
c5e00 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 else.# defi
c5e10 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ne SQLITE_ENABLE
c5e20 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 _LOCKING_STYLE 0
c5e30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 .# endif.#endif
c5e40 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 ../*.** Define t
c5e50 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72 he OS_VXWORKS pr
c5e60 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 e-processor macr
c5e70 6f 20 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69 o to 1 if buildi
c5e80 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b ng on .** vxwork
c5e90 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 s, or 0 otherwis
c5ea0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53 e..*/.#ifndef OS
c5eb0 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64 _VXWORKS.# if d
c5ec0 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20 efined(__RTP__)
c5ed0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f || defined(_WRS_
c5ee0 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66 KERNEL).# def
c5ef0 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31 ine OS_VXWORKS 1
c5f00 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 .# else.# de
c5f10 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 fine OS_VXWORKS
c5f20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 0.# endif.#endi
c5f30 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 f../*.** These #
c5f40 64 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 defines should e
c5f50 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 nable >2GB file
c5f60 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 support on Posix
c5f70 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 if the.** under
c5f80 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 lying operating
c5f90 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 system supports
c5fa0 69 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c it. If the OS l
c5fb0 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 acks.** large fi
c5fc0 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 le support, thes
c5fd0 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f e should be no-o
c5fe0 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 ps..**.** Large
c5ff0 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e file support can
c6000 20 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 be disabled usi
c6010 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f ng the -DSQLITE_
c6020 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 DISABLE_LFS swit
c6030 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d ch.** on the com
c6040 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 piler command li
c6050 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 ne. This is nec
c6060 65 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 essary if you ar
c6070 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f e compiling.** o
c6080 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 n a recent machi
c6090 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 ne (ex: RedHat 7
c60a0 2e 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 .2) but you want
c60b0 20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f your code to wo
c60c0 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 rk.** on an olde
c60d0 72 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 r machine (ex: R
c60e0 65 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 edHat 6.0). If
c60f0 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 you compile on R
c6100 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 edHat 7.2.** wit
c6110 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e hout this option
c6120 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e , LFS is enable.
c6130 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e But LFS does n
c6140 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 ot exist in the
c6150 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 kernel.** in Red
c6160 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 Hat 6.0, so the
c6170 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e code won't work.
c6180 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 Hence, for max
c6190 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 imum binary.** p
c61a0 6f 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 ortability you s
c61b0 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a hould omit LFS..
c61c0 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f **.** The previo
c61d0 75 73 20 70 61 72 61 67 72 61 70 68 20 77 61 73 us paragraph was
c61e0 20 77 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 written in 2005
c61f0 2e 20 20 28 54 68 69 73 20 70 61 72 61 67 72 61 . (This paragra
c6200 70 68 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a ph is written.**
c6210 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 on 2008-11-28.)
c6220 20 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c These days, all
c6230 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 Linux kernels s
c6240 75 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c upport large fil
c6250 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 es, so.** you sh
c6260 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65 ould probably le
c6270 61 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e ave LFS enabled.
c6280 20 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64 But some embed
c6290 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69 ded platforms mi
c62a0 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 ght.** lack LFS
c62b0 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 in which case th
c62c0 65 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 e SQLITE_DISABLE
c62d0 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74 _LFS macro might
c62e0 20 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c still be useful
c62f0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
c6300 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a ITE_DISABLE_LFS.
c6310 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f # define _LARGE_
c6320 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 FILE 1.# i
c6330 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 fndef _FILE_OFFS
c6340 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 ET_BITS.# defi
c6350 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f ne _FILE_OFFSET_
c6360 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a BITS 64.# endif.
c6370 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 # define _LARGEF
c6380 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e ILE_SOURCE 1.#en
c6390 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 dif../*.** stand
c63a0 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 ard include file
c63b0 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c s..*/.#include <
c63c0 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e sys/types.h>.#in
c63d0 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e clude <sys/stat.
c63e0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e h>.#include <fcn
c63f0 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c tl.h>.#include <
c6400 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 unistd.h>.#inclu
c6410 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a de <sys/time.h>.
c6420 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e #include <errno.
c6430 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 h>..#if SQLITE_E
c6440 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
c6450 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 YLE.# include <s
c6460 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 ys/ioctl.h>.# if
c6470 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 OS_VXWORKS.# i
c6480 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72 nclude <semaphor
c6490 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 e.h>.# include
c64a0 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73 <limits.h>.# els
c64b0 65 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 e.# include <sy
c64c0 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 s/file.h>.# inc
c64d0 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e lude <sys/param.
c64e0 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 h>.# include <s
c64f0 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e ys/mount.h>.# en
c6500 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 dif.#endif /* SQ
c6510 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
c6520 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a ING_STYLE */../*
c6530 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f .** If we are to
c6540 20 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c be thread-safe,
c6550 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 include the pth
c6560 72 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 reads header and
c6570 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 define.** the S
c6580 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 QLITE_UNIX_THREA
c6590 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 DS macro..*/.#if
c65a0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
c65b0 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 FE.# define SQLI
c65c0 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 TE_UNIX_THREADS
c65d0 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 1.#endif../*.**
c65e0 44 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 Default permissi
c65f0 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e ons when creatin
c6600 67 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a g a new file.*/.
c6610 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 #ifndef SQLITE_D
c6620 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d EFAULT_FILE_PERM
c6630 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 ISSIONS.# define
c6640 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
c6650 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 FILE_PERMISSIONS
c6660 20 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0644.#endif../*
c6670 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 . ** Default per
c6680 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 missions when cr
c6690 65 61 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 eating auto prox
c66a0 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 y dir. */.#ifnde
c66b0 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 f SQLITE_DEFAULT
c66c0 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 _PROXYDIR_PERMIS
c66d0 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 SIONS.# define S
c66e0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 QLITE_DEFAULT_PR
c66f0 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f OXYDIR_PERMISSIO
c6700 4e 53 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a NS 0755.#endif..
c6710 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 /*.** Maximum su
c6720 70 70 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e pported path-len
c6730 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 gth..*/.#define
c6740 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 MAX_PATHNAME 512
c6750 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 ../*.** Only set
c6760 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69 the lastErrno i
c6770 66 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 f the error code
c6780 20 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72 is a real error
c6790 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e and not .** a n
c67a0 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72 ormal expected r
c67b0 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 eturn code of SQ
c67c0 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c LITE_BUSY or SQL
c67d0 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e ITE_OK.*/.#defin
c67e0 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 e IS_LOCK_ERROR(
c67f0 78 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54 x) ((x != SQLIT
c6800 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53 E_OK) && (x != S
c6810 51 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f QLITE_BUSY)).../
c6820 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20 *.** Sometimes,
c6830 61 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e after a file han
c6840 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79 dle is closed by
c6850 20 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c SQLite, the fil
c6860 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 e descriptor.**
c6870 63 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 cannot be closed
c6880 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e immediately. In
c6890 20 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e these cases, in
c68a0 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66 stances of the f
c68b0 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 ollowing.** stru
c68c0 63 74 75 72 65 20 61 72 65 20 75 73 65 64 20 74 cture are used t
c68d0 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 o store the file
c68e0 20 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c descriptor whil
c68f0 65 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e e waiting for an
c6900 0a 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 .** opportunity
c6910 74 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20 to either close
c6920 6f 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a or reuse it..*/.
c6930 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55 typedef struct U
c6940 6e 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78 nixUnusedFd Unix
c6950 55 6e 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74 UnusedFd;.struct
c6960 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a UnixUnusedFd {.
c6970 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 int fd;
c6980 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
c6990 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
c69a0 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 o close */. int
c69b0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
c69c0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
c69d0 74 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69 this file descri
c69e0 70 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 ptor was opened
c69f0 77 69 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e with */. UnixUn
c6a00 75 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20 usedFd *pNext;
c6a10 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 /* Next unus
c6a20 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ed file descript
c6a30 6f 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20 or on same file
c6a40 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
c6a50 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 unixFile struct
c6a60 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 ure is subclass
c6a70 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 of sqlite3_file
c6a80 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 specific to the
c6a90 75 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c unix.** VFS impl
c6aa0 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a ementations..*/.
c6ab0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 typedef struct u
c6ac0 6e 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 nixFile unixFile
c6ad0 3b 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c ;.struct unixFil
c6ae0 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f e {. sqlite3_io
c6af0 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a _methods const *
c6b00 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 pMethod; /* Alw
c6b10 61 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e ays the first en
c6b20 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 try */. struct
c6b30 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 unixOpenCnt *pOp
c6b40 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 en; /* Inf
c6b50 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e o about all open
c6b60 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e fd's on this in
c6b70 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ode */. struct
c6b80 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c unixLockInfo *pL
c6b90 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 ock; /* Inf
c6ba0 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e o about locks on
c6bb0 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 this inode */.
c6bc0 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 int h;
c6bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c6be0 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
c6bf0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e scriptor */. in
c6c00 74 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 t dirfd;
c6c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
c6c20 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f * File descripto
c6c30 72 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 r for the direct
c6c40 6f 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ory */. unsigne
c6c50 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b d char locktype;
c6c60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
c6c70 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 type of lock he
c6c80 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f ld on this fd */
c6c90 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f . int lastErrno
c6ca0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c6cb0 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 /* The unix
c6cc0 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c errno from the l
c6cd0 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f ast I/O error */
c6ce0 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 . void *locking
c6cf0 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 Context;
c6d00 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 /* Locking s
c6d10 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 tyle specific st
c6d20 61 74 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 ate */. UnixUnu
c6d30 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 sedFd *pUnused;
c6d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 /* Pre
c6d50 2d 61 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 -allocated UnixU
c6d60 6e 75 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74 nusedFd */. int
c6d70 20 66 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20 fileFlags;
c6d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c6d90 20 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c Miscellanous fl
c6da0 61 67 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ags */.#if SQLIT
c6db0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
c6dc0 5f 53 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 _STYLE. int ope
c6dd0 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 nFlags;
c6de0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
c6df0 20 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 flags specified
c6e00 20 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 at open() */.#e
c6e10 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f ndif.#if SQLITE_
c6e20 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
c6e30 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
c6e40 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 . pthread_t tid
c6e50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
c6e60 20 20 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 /* The threa
c6e70 64 20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 d that "owns" th
c6e80 69 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 is unixFile */.#
c6e90 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 endif.#if OS_VXW
c6ea0 4f 52 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c ORKS. int isDel
c6eb0 65 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ete;
c6ec0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 /* Delet
c6ed0 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 e on close if tr
c6ee0 75 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 ue */. struct v
c6ef0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 xworksFileId *pI
c6f00 64 3b 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 d; /* Uniq
c6f10 75 65 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 ue file ID */.#e
c6f20 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 ndif.#ifndef NDE
c6f30 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 BUG. /* The nex
c6f40 74 20 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 t group of varia
c6f50 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f bles are used to
c6f60 20 74 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f track whether o
c6f70 72 20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 r not the. ** t
c6f80 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
c6f90 65 72 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 er in bytes 24-2
c6fa0 37 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 7 of database fi
c6fb0 6c 65 73 20 61 72 65 20 75 70 64 61 74 65 64 0a les are updated.
c6fc0 20 20 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e ** whenever an
c6fd0 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 y part of the da
c6fe0 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 tabase changes.
c6ff0 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 An assertion fa
c7000 75 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 ult will. ** oc
c7010 63 75 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 cur if a file is
c7020 20 75 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 updated without
c7030 20 61 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 also updating t
c7040 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 he transaction.
c7050 20 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 ** counter. Th
c7060 69 73 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 is test is made
c7070 74 6f 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f to avoid new pro
c7080 62 6c 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f blems similar to
c7090 20 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 the. ** one de
c70a0 73 63 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 scribed by ticke
c70b0 74 20 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 t #3584. . */.
c70c0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 unsigned char t
c70d0 72 61 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 ransCntrChng;
c70e0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 74 /* True if the t
c70f0 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 ransaction count
c7100 65 72 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 er changed */.
c7110 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 unsigned char db
c7120 55 70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f Update; /
c7130 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 * True if any pa
c7140 72 74 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 rt of database f
c7150 69 6c 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 ile changed */.
c7160 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 unsigned char i
c7170 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 nNormalWrite;
c7180 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 /* True if in a
c7190 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 normal write ope
c71a0 72 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 ration */.#endif
c71b0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
c71c0 45 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 EST. /* In test
c71d0 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 mode, increase
c71e0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 the size of this
c71f0 20 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74 structure a bit
c7200 20 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 so that . ** i
c7210 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e t is larger than
c7220 20 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73 the struct Cras
c7230 68 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e hFile defined in
c7240 20 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 test6.c.. */.
c7250 20 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 char aPadding[3
c7260 32 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2];.#endif.};../
c7270 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
c7280 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 ng macros define
c7290 20 62 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c bits in unixFil
c72a0 65 2e 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 e.fileFlags.*/.#
c72b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48 define SQLITE_WH
c72c0 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 OLE_FILE_LOCKING
c72d0 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73 0x0001 /* Us
c72e0 65 20 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 e whole-file loc
c72f0 6b 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 king */../*.** I
c7300 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 nclude code that
c7310 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c is common to al
c7320 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a l os_*.c files.*
c7330 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c7340 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d * Include os_com
c7350 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 mon.h in the mid
c7360 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 dle of os_unix.c
c7370 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c7380 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c7390 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
c73a0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
c73b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c73c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c73d0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
c73e0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
c73f0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
c7400 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
c7410 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
c7420 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
c7430 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
c7440 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
c7450 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
c7460 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
c7470 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
c7480 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
c7490 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
c74a0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
c74b0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
c74c0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
c74d0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
c74e0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
c74f0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
c7500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7540 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
c7550 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
c7560 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 macros and a li
c7570 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 ttle bit of code
c7580 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
c7590 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 to.** all of the
c75a0 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 platform-specif
c75b0 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 ic files (os_*.c
c75c0 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 ) and is #includ
c75d0 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a ed into those.**
c75e0 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 files..**.** Th
c75f0 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 is file should b
c7600 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 e #included by t
c7610 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 he os_*.c files
c7620 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 only. It is not
c7630 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 a.** general pu
c7640 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c rpose header fil
c7650 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 e..**.** $Id: os
c7660 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 _common.h,v 1.38
c7670 20 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 2009/02/24 18:4
c7680 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 0:50 danielk1977
c7690 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
c76a0 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a f _OS_COMMON_H_.
c76b0 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d #define _OS_COMM
c76c0 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 ON_H_../*.** At
c76d0 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 least two bugs h
c76e0 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 ave slipped in b
c76f0 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 ecause we change
c7700 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 d the MEMORY_DEB
c7710 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 UG.** macro to S
c7720 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 QLITE_DEBUG and
c7730 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 some older makef
c7740 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 iles have not ye
c7750 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 t made the.** sw
c7760 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f itch. The follo
c7770 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 wing code should
c7780 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 catch this prob
c7790 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 lem at compile-t
c77a0 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d ime..*/.#ifdef M
c77b0 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 EMORY_DEBUG.# er
c77c0 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f ror "The MEMORY_
c77d0 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f DEBUG macro is o
c77e0 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 bsolete. Use SQ
c77f0 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 LITE_DEBUG inste
c7800 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 ad.".#endif..#if
c7810 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
c7820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
c7830 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 int sqlite3OSTra
c7840 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 ce = 0;.#define
c7850 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 OSTRACE1(X)
c7860 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
c7870 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
c7880 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 DebugPrintf(X).#
c7890 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 define OSTRACE2(
c78a0 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 X,Y) if( s
c78b0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
c78c0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
c78d0 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 tf(X,Y).#define
c78e0 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 OSTRACE3(X,Y,Z)
c78f0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
c7900 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
c7910 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
c7920 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
c7930 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 CE4(X,Y,Z,A) i
c7940 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
c7950 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
c7960 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a Printf(X,Y,Z,A).
c7970 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 #define OSTRACE5
c7980 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 (X,Y,Z,A,B) if(
c7990 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
c79a0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
c79b0 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 ntf(X,Y,Z,A,B).#
c79c0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
c79d0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 X,Y,Z,A,B,C) \.
c79e0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 if(sqlite3OST
c79f0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 race) sqlite3Deb
c7a00 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
c7a10 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,B,C).#define OS
c7a20 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE7(X,Y,Z,A,B
c7a30 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 ,C,D) \. if(s
c7a40 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 qlite3OSTrace) s
c7a50 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c7a60 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 f(X,Y,Z,A,B,C,D)
c7a70 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
c7a80 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 STRACE1(X).#defi
c7a90 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
c7aa0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c7ab0 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 3(X,Y,Z).#define
c7ac0 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c OSTRACE4(X,Y,Z,
c7ad0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
c7ae0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 CE5(X,Y,Z,A,B).#
c7af0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
c7b00 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 X,Y,Z,A,B,C).#de
c7b10 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c fine OSTRACE7(X,
c7b20 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e Y,Z,A,B,C,D).#en
c7b30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f dif../*.** Macro
c7b40 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 s for performanc
c7b50 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d e tracing. Norm
c7b60 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e ally turned off.
c7b70 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 Only works.**
c7b80 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 on i486 hardware
c7b90 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
c7ba0 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 TE_PERFORMANCE_T
c7bb0 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 RACE../* .** hwt
c7bc0 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 ime.h contains i
c7bd0 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 nline assembler
c7be0 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 code for impleme
c7bf0 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 nting .** high-p
c7c00 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e erformance timin
c7c10 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f g routines..*/./
c7c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
c7c30 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 nclude hwtime.h
c7c40 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
c7c50 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
c7c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
c7c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
c7c80 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 egin file hwtime
c7c90 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
c7ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
c7cc0 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 *.** 2008 May 27
c7cd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
c7ce0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
c7cf0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
c7d00 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
c7d10 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
c7d20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
c7d30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
c7d40 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
c7d50 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
c7d60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
c7d70 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
c7d80 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
c7d90 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
c7da0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
c7db0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
c7dc0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
c7dd0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
c7de0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
c7df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7e30 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
c7e40 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e file contains in
c7e50 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f line asm code fo
c7e60 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 r retrieving "hi
c7e70 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a gh-performance".
c7e80 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 ** counters for
c7e90 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a x86 class CPUs..
c7ea0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d **.** $Id: hwtim
c7eb0 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 e.h,v 1.3 2008/0
c7ec0 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 8/01 14:33:15 sh
c7ed0 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 ane Exp $.*/.#if
c7ee0 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a ndef _HWTIME_H_.
c7ef0 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f #define _HWTIME_
c7f00 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f H_../*.** The fo
c7f10 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 llowing routine
c7f20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 only works on pe
c7f30 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 ntium-class (or
c7f40 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 newer) processor
c7f50 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 s..** It uses th
c7f60 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 e RDTSC opcode t
c7f70 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 o read the cycle
c7f80 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 count value out
c7f90 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 of the.** proce
c7fa0 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 ssor and returns
c7fb0 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 that value. Th
c7fc0 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 is can be used f
c7fd0 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 or high-res.** p
c7fe0 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 rofiling..*/.#if
c7ff0 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
c8000 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
c8010 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 MSC_VER)) && \.
c8020 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 (defined(i3
c8030 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 86) || defined(_
c8040 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 _i386__) || defi
c8050 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 ned(_M_IX86))..
c8060 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 #if defined(__G
c8070 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 NUC__).. __inli
c8080 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
c8090 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
c80a0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 (void){. uns
c80b0 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 igned int lo, hi
c80c0 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f ;. __asm__ _
c80d0 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 _volatile__ ("rd
c80e0 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 tsc" : "=a" (lo)
c80f0 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 , "=d" (hi));.
c8100 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 return (sqlit
c8110 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 e_uint64)hi << 3
c8120 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 2 | lo;. }.. #
c8130 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 elif defined(_MS
c8140 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c C_VER).. __decl
c8150 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e spec(naked) __in
c8160 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 line sqlite_uint
c8170 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 64 __cdecl sqlit
c8180 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
c8190 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 __asm {.
c81a0 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 rdtsc.
c81b0 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 ret ; r
c81c0 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 eturn value at E
c81d0 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 DX:EAX. }.
c81e0 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c }.. #endif..#el
c81f0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e if (defined(__GN
c8200 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 UC__) && defined
c8210 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 (__x86_64__))..
c8220 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
c8230 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
c8240 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
c8250 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
c8260 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f ng val;. __
c8270 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
c8280 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
c8290 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 A" (val));.
c82a0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d return val;. }
c82b0 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 . .#elif (define
c82c0 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 d(__GNUC__) && d
c82d0 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 efined(__ppc__))
c82e0 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
c82f0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
c8300 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
c8310 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
c8320 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 long long retva
c8330 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 l;. unsigne
c8340 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 d long junk;.
c8350 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
c8360 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 atile__ ("\n\.
c8370 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 1:
c8380 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 mftbu %1\n\.
c8390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c83a0 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 mftb %L0\n\.
c83b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c83c0 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 mftbu %0\n\.
c83d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c83e0 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e cmpw %0,%1\n
c83f0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c8400 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a bne 1b".
c8410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c8420 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c : "=r" (retval
c8430 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b ), "=r" (junk));
c8440 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 . return re
c8450 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 tval;. }..#else
c8460 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 .. #error Need
c8470 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
c8480 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 f sqlite3Hwtime(
c8490 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 ) for your platf
c84a0 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 orm... /*. **
c84b0 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f To compile witho
c84c0 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 ut implementing
c84d0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 sqlite3Hwtime()
c84e0 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 for your platfor
c84f0 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 m,. ** you can
c8500 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 remove the above
c8510 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 #error and use
c8520 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 the following.
c8530 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e ** stub function
c8540 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 . You will lose
c8550 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 timing support
c8560 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 for many. ** of
c8570 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 the debugging a
c8580 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 nd testing utili
c8590 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f ties, but it sho
c85a0 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 uld at. ** leas
c85b0 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 t compile and ru
c85c0 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 n.. */.SQLITE_P
c85d0 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f RIVATE sqlite_
c85e0 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 uint64 sqlite3Hw
c85f0 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 time(void){ retu
c8600 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 rn ((sqlite_uint
c8610 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 64)0); }..#endif
c8620 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 ..#endif /* !def
c8630 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 ined(_HWTIME_H_)
c8640 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
c8650 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 **** End of hwti
c8660 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a me.h ***********
c8670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8690 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c86a0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
c86b0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
c86c0 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 f in os_common.h
c86d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c86e0 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c ***/..static sql
c86f0 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 ite_uint64 g_sta
c8700 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 rt;.static sqlit
c8710 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 e_uint64 g_elaps
c8720 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 ed;.#define TIME
c8730 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f R_START g_
c8740 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 start=sqlite3Hwt
c8750 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 ime().#define TI
c8760 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 MER_END
c8770 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 g_elapsed=sqlite
c8780 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 3Hwtime()-g_star
c8790 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f t.#define TIMER_
c87a0 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c ELAPSED g_el
c87b0 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 apsed.#else.#def
c87c0 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a ine TIMER_START.
c87d0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e #define TIMER_EN
c87e0 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f D.#define TIMER_
c87f0 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71 ELAPSED ((sq
c8800 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 lite_uint64)0).#
c8810 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
c8820 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 we compile with
c8830 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 the SQLITE_TEST
c8840 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 macro set, then
c8850 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c the following bl
c8860 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 ock.** of code w
c8870 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 ill give us the
c8880 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c ability to simul
c8890 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 ate a disk I/O e
c88a0 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 rror. This.** i
c88b0 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
c88c0 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 ng the I/O recov
c88d0 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 ery logic..*/.#i
c88e0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
c88f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
c8900 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c8910 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 _hit = 0;
c8920 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
c8930 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f mber of I/O Erro
c8940 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 rs */.SQLITE_API
c8950 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
c8960 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 error_hardhit =
c8970 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 0; /* Num
c8980 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 ber of non-benig
c8990 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 n errors */.SQLI
c89a0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c89b0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
c89c0 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ing = 0;
c89d0 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f /* Count down to
c89e0 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 first I/O error
c89f0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
c8a00 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
c8a10 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b ror_persist = 0;
c8a20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
c8a30 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 if I/O errors pe
c8a40 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f rsist */.SQLITE_
c8a50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c8a60 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 io_error_benign
c8a70 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
c8a80 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 True if errors a
c8a90 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c re benign */.SQL
c8aa0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c8ab0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
c8ac0 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 ding = 0;.SQLITE
c8ad0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
c8ae0 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 _diskfull = 0;.#
c8af0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 define SimulateI
c8b00 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 OErrorBenign(X)
c8b10 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c8b20 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 _benign=(X).#def
c8b30 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
c8b40 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 ror(CODE) \. i
c8b50 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 f( (sqlite3_io_e
c8b60 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 rror_persist &&
c8b70 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c8b80 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c _hit) \. |
c8b90 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 | sqlite3_io_err
c8ba0 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 or_pending-- ==
c8bb0 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 1 ) \.
c8bc0 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 { local_ioe
c8bd0 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 rr(); CODE; }.st
c8be0 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f atic void local_
c8bf0 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 ioerr(){. IOTRA
c8c00 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b CE(("IOERR\n"));
c8c10 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 . sqlite3_io_er
c8c20 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 ror_hit++;. if(
c8c30 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 !sqlite3_io_err
c8c40 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 or_benign ) sqli
c8c50 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 te3_io_error_har
c8c60 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e dhit++;.}.#defin
c8c70 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 e SimulateDiskfu
c8c80 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a llError(CODE) \.
c8c90 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 if( sqlite3_d
c8ca0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 iskfull_pending
c8cb0 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 ){ \. if( sq
c8cc0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 lite3_diskfull_p
c8cd0 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c ending == 1 ){ \
c8ce0 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f . local_io
c8cf0 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 err(); \.
c8d00 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
c8d10 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 = 1; \. s
c8d20 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c8d30 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 hit = 1; \.
c8d40 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d CODE; \. }
c8d50 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 else{ \. s
c8d60 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
c8d70 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 pending--; \.
c8d80 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 } \. }.#else
c8d90 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
c8da0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 eIOErrorBenign(X
c8db0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
c8dc0 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 teIOError(A).#de
c8dd0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 fine SimulateDis
c8de0 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 kfullError(A).#e
c8df0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e ndif../*.** When
c8e00 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 testing, keep a
c8e10 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 count of the nu
c8e20 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c mber of open fil
c8e30 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 es..*/.#ifdef SQ
c8e40 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
c8e50 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
c8e60 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 _open_file_count
c8e70 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 = 0;.#define Op
c8e80 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 enCounter(X) sq
c8e90 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f lite3_open_file_
c8ea0 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 count+=(X).#else
c8eb0 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 .#define OpenCou
c8ec0 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a nter(X).#endif..
c8ed0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
c8ee0 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f ed(_OS_COMMON_H_
c8ef0 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
c8f00 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f ***** End of os_
c8f10 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
c8f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8f40 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
c8f50 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
c8f60 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
c8f70 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 ff in os_unix.c
c8f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8f90 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 ****/../*.** Def
c8fa0 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 ine various macr
c8fb0 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 os that are miss
c8fc0 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 ing from some sy
c8fd0 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 stems..*/.#ifnde
c8fe0 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 f O_LARGEFILE.#
c8ff0 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 define O_LARGEFI
c9000 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 LE 0.#endif.#ifd
c9010 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c ef SQLITE_DISABL
c9020 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f E_LFS.# undef O_
c9030 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 LARGEFILE.# defi
c9040 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 ne O_LARGEFILE 0
c9050 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
c9060 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 O_NOFOLLOW.# def
c9070 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 ine O_NOFOLLOW 0
c9080 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
c9090 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e O_BINARY.# defin
c90a0 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e e O_BINARY 0.#en
c90b0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 dif../*.** The D
c90c0 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e JGPP compiler en
c90d0 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 vironment looks
c90e0 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 mostly like Unix
c90f0 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b , but it.** lack
c9100 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 s the fcntl() sy
c9110 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 stem call. So r
c9120 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 edefine fcntl()
c9130 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a to be something.
c9140 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 ** that always s
c9150 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d ucceeds. This m
c9160 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e eans that lockin
c9170 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 g does not occur
c9180 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e under.** DJGPP.
c9190 20 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 But it is DOS
c91a0 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 - what did you e
c91b0 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 xpect?.*/.#ifdef
c91c0 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 __DJGPP__.# def
c91d0 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 ine fcntl(A,B,C)
c91e0 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
c91f0 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 The threadid ma
c9200 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 cro resolves to
c9210 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 the thread-id or
c9220 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 to 0. Used for
c9230 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 .** testing and
c9240 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a debugging only..
c9250 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 */.#if SQLITE_TH
c9260 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 READSAFE.#define
c9270 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61 threadid pthrea
c9280 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 d_self().#else.#
c9290 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 define threadid
c92a0 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
c92b0 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e Helper function
c92c0 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 s to obtain and
c92d0 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 relinquish the g
c92e0 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20 54 68 65 lobal mutex. The
c92f0 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 .** global mutex
c9300 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 74 is used to prot
c9310 65 63 74 20 74 68 65 20 75 6e 69 78 4f 70 65 6e ect the unixOpen
c9320 43 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 Cnt, unixLockInf
c9330 6f 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 o and.** vxworks
c9340 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 75 FileId objects u
c9350 73 65 64 20 62 79 20 74 68 69 73 20 66 69 6c 65 sed by this file
c9360 2c 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d , all of which m
c9370 61 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 ay be .** shared
c9380 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 by multiple thr
c9390 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 eads..**.** Func
c93a0 74 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65 tion unixMutexHe
c93b0 6c 64 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 ld() is used to
c93c0 61 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 assert() that th
c93d0 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0a e global mutex .
c93e0 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 ** is held when
c93f0 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 66 required. This f
c9400 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 unction is only
c9410 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 used as part of
c9420 61 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 61 assert() .** sta
c9430 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a tements. e.g..**
c9440 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4d .** unixEnterM
c9450 75 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 utex().** as
c9460 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 sert( unixMutexH
c9470 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 6e eld() );.** un
c9480 69 78 45 6e 74 65 72 4c 65 61 76 65 28 29 0a 2a ixEnterLeave().*
c9490 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e /.static void un
c94a0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 ixEnterMutex(voi
c94b0 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 d){. sqlite3_mu
c94c0 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 tex_enter(sqlite
c94d0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
c94e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
c94f0 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 MASTER));.}.stat
c9500 69 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 ic void unixLeav
c9510 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 eMutex(void){.
c9520 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
c9530 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 ave(sqlite3Mutex
c9540 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
c9550 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
c9560 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 4c ));.}.#ifdef SQL
c9570 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 ITE_DEBUG.static
c9580 20 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65 int unixMutexHe
c9590 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 74 ld(void) {. ret
c95a0 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 urn sqlite3_mute
c95b0 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 x_held(sqlite3Mu
c95c0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
c95d0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
c95e0 54 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a TER));.}.#endif.
c95f0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
c9600 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 DEBUG./*.** Help
c9610 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 er function for
c9620 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 61 printing out tra
c9630 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ce information f
c9640 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a rom debugging.**
c9650 20 62 69 6e 61 72 69 65 73 2e 20 54 68 69 73 20 binaries. This
c9660 72 65 74 75 72 6e 73 20 74 68 65 20 73 74 72 69 returns the stri
c9670 6e 67 20 72 65 70 72 65 73 65 74 61 74 69 6f 6e ng represetation
c9680 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 of the supplied
c9690 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b .** integer lock
c96a0 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 -type..*/.static
c96b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 const char *loc
c96c0 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c 6f ktypeName(int lo
c96d0 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 74 63 cktype){. switc
c96e0 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 h( locktype ){.
c96f0 20 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a case NO_LOCK:
c9700 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a return "NONE";.
c9710 20 20 20 20 63 61 73 65 20 53 48 41 52 45 44 5f case SHARED_
c9720 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 LOCK: return "SH
c9730 41 52 45 44 22 3b 0a 20 20 20 20 63 61 73 65 20 ARED";. case
c9740 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 RESERVED_LOCK: r
c9750 65 74 75 72 6e 20 22 52 45 53 45 52 56 45 44 22 eturn "RESERVED"
c9760 3b 0a 20 20 20 20 63 61 73 65 20 50 45 4e 44 49 ;. case PENDI
c9770 4e 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 NG_LOCK: return
c9780 22 50 45 4e 44 49 4e 47 22 3b 0a 20 20 20 20 63 "PENDING";. c
c9790 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f ase EXCLUSIVE_LO
c97a0 43 4b 3a 20 72 65 74 75 72 6e 20 22 45 58 43 4c CK: return "EXCL
c97b0 55 53 49 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 USIVE";. }. re
c97c0 74 75 72 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a turn "ERROR";.}.
c97d0 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
c97e0 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 QLITE_LOCK_TRACE
c97f0 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 ./*.** Print out
c9800 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
c9810 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f ut all locking o
c9820 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a perations..**.**
c9830 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
c9840 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62 6c used for troubl
c9850 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 eshooting locks
c9860 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 on multithreaded
c9870 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 .** platforms.
c9880 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c Enable by compil
c9890 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44 53 ing with the -DS
c98a0 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 QLITE_LOCK_TRACE
c98b0 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 .** command-line
c98c0 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 option on the c
c98d0 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20 63 ompiler. This c
c98e0 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a ode is normally.
c98f0 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a ** turned off..*
c9900 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 /.static int loc
c9910 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20 69 kTrace(int fd, i
c9920 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66 6c nt op, struct fl
c9930 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 ock *p){. char
c9940 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 *zOpName, *zType
c9950 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 ;. int s;. int
c9960 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 savedErrno;. i
c9970 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 f( op==F_GETLK )
c9980 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 {. zOpName =
c9990 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 "GETLK";. }else
c99a0 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b if( op==F_SETLK
c99b0 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 ){. zOpName
c99c0 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c = "SETLK";. }el
c99d0 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 se{. s = fcnt
c99e0 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 l(fd, op, p);.
c99f0 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 sqlite3DebugPr
c9a00 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e intf("fcntl unkn
c9a10 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 2c own %d %d %d\n",
c9a20 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 fd, op, s);.
c9a30 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 return s;. }.
c9a40 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d if( p->l_type==
c9a50 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a F_RDLCK ){. z
c9a60 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a Type = "RDLCK";.
c9a70 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c }else if( p->l
c9a80 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 _type==F_WRLCK )
c9a90 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 {. zType = "W
c9aa0 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 RLCK";. }else i
c9ab0 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f f( p->l_type==F_
c9ac0 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 UNLCK ){. zTy
c9ad0 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 pe = "UNLCK";.
c9ae0 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
c9af0 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 t( 0 );. }. as
c9b00 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 sert( p->l_whenc
c9b10 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 e==SEEK_SET );.
c9b20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f s = fcntl(fd, o
c9b30 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45 72 p, p);. savedEr
c9b40 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 rno = errno;. s
c9b50 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c9b60 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20 25 f("fcntl %d %d %
c9b70 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25 64 s %s %d %d %d %d
c9b80 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61 64 \n",. thread
c9b90 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c id, fd, zOpName,
c9ba0 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e zType, (int)p->
c9bb0 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70 2d l_start, (int)p-
c9bc0 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e >l_len,. (in
c9bd0 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a t)p->l_pid, s);.
c9be0 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 if( s==(-1) &&
c9bf0 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 op==F_SETLK &&
c9c00 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 (p->l_type==F_RD
c9c10 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 LCK || p->l_type
c9c20 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20 ==F_WRLCK) ){.
c9c30 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
c9c40 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 2;. l2 = *p;.
c9c50 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f fcntl(fd, F_
c9c60 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 GETLK, &l2);.
c9c70 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d if( l2.l_type==
c9c80 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 F_RDLCK ){.
c9c90 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 zType = "RDLCK"
c9ca0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
c9cb0 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c l2.l_type==F_WRL
c9cc0 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 CK ){. zTyp
c9cd0 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20 e = "WRLCK";.
c9ce0 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f }else if( l2.l_
c9cf0 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b type==F_UNLCK ){
c9d00 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 . zType = "
c9d10 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 UNLCK";. }els
c9d20 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e{. assert(
c9d30 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0 );. }.
c9d40 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
c9d50 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72 tf("fcntl-failur
c9d60 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 20 e-reason: %s %d
c9d70 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 %d %d\n",.
c9d80 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e zType, (int)l2.
c9d90 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 l_start, (int)l2
c9da0 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e .l_len, (int)l2.
c9db0 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72 l_pid);. }. er
c9dc0 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e 6f rno = savedErrno
c9dd0 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a ;. return s;.}.
c9de0 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c 6f #define fcntl lo
c9df0 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20 2f ckTrace.#endif /
c9e00 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 * SQLITE_LOCK_TR
c9e10 41 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 ACE */..../*.**
c9e20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 This routine tra
c9e30 6e 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61 nslates a standa
c9e40 72 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 rd POSIX errno c
c9e50 6f 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 ode into somethi
c9e60 6e 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 ng.** useful to
c9e70 74 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 the clients of t
c9e80 68 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74 he sqlite3 funct
c9e90 69 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61 ions. Specifica
c9ea0 6c 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e lly, it is.** in
c9eb0 74 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c tended to transl
c9ec0 61 74 65 20 61 20 76 61 72 69 65 74 79 20 6f 66 ate a variety of
c9ed0 20 22 74 72 79 20 61 67 61 69 6e 22 20 65 72 72 "try again" err
c9ee0 6f 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f ors into SQLITE_
c9ef0 42 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 BUSY.** and a va
c9f00 72 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65 riety of "please
c9f10 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 close the file
c9f20 64 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 descriptor NOW"
c9f30 65 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 errors into .**
c9f40 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 SQLITE_IOERR.**
c9f50 0a 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e .** Errors durin
c9f60 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e g initialization
c9f70 20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 of locks, or fi
c9f80 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 le system suppor
c9f90 74 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 t for locks,.**
c9fa0 73 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e should handle EN
c9fb0 4f 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 OLCK, ENOTSUP, E
c9fc0 4f 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61 OPNOTSUPP separa
c9fd0 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tely..*/.static
c9fe0 69 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 int sqliteErrorF
c9ff0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e romPosixError(in
ca000 74 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e t posixError, in
ca010 74 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b t sqliteIOErr) {
ca020 0a 20 20 73 77 69 74 63 68 20 28 70 6f 73 69 78 . switch (posix
ca030 45 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 Error) {. case
ca040 30 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 0: . return S
ca050 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 QLITE_OK;. .
ca060 20 63 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 case EAGAIN:.
ca070 63 61 73 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a case ETIMEDOUT:.
ca080 20 20 63 61 73 65 20 45 42 55 53 59 3a 0a 20 20 case EBUSY:.
ca090 63 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 case EINTR:. ca
ca0a0 73 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 se ENOLCK: .
ca0b0 20 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 /* random NFS r
ca0c0 65 74 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 etry error, unle
ca0d0 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 ss during file s
ca0e0 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 ystem support .
ca0f0 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 * introspect
ca100 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 ion, in which it
ca110 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 actually means
ca120 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a what it says */.
ca130 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
ca140 45 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 E_BUSY;. . c
ca150 61 73 65 20 45 41 43 43 45 53 3a 20 0a 20 20 20 ase EACCES: .
ca160 20 2f 2a 20 45 41 43 43 45 53 20 69 73 20 6c 69 /* EACCES is li
ca170 6b 65 20 45 41 47 41 49 4e 20 64 75 72 69 6e 67 ke EAGAIN during
ca180 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 locking operati
ca190 6f 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 ons, but not any
ca1a0 20 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 other time*/.
ca1b0 20 20 69 66 28 20 28 73 71 6c 69 74 65 49 4f 45 if( (sqliteIOE
ca1c0 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rr == SQLITE_IOE
ca1d0 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 RR_LOCK) || ..(s
ca1e0 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 qliteIOErr == SQ
ca1f0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
ca200 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 K) || ..(sqliteI
ca210 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 OErr == SQLITE_I
ca220 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a OERR_RDLOCK) ||.
ca230 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d .(sqliteIOErr ==
ca240 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 SQLITE_IOERR_CH
ca250 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 ECKRESERVEDLOCK)
ca260 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
ca270 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
ca280 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 }. /* else
ca290 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a fall through */.
ca2a0 20 20 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 case EPERM: .
ca2b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ca2c0 5f 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 _PERM;. . ca
ca2d0 73 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 se EDEADLK:.
ca2e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
ca2f0 45 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 ERR_BLOCKED;.
ca300 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 .#if EOPNOTSUPP
ca310 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 !=ENOTSUP. case
ca320 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 EOPNOTSUPP: .
ca330 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 /* something w
ca340 65 6e 74 20 74 65 72 72 69 62 6c 79 20 61 77 72 ent terribly awr
ca350 79 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 y, unless during
ca360 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 file system sup
ca370 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 port . * int
ca380 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 rospection, in w
ca390 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79 hich it actually
ca3a0 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 means what it s
ca3b0 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ays */.#endif.#i
ca3c0 66 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 fdef ENOTSUP. c
ca3d0 61 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 ase ENOTSUP: .
ca3e0 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c /* invalid fd,
ca3f0 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 unless during f
ca400 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f ile system suppo
ca410 72 74 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e rt introspection
ca420 2c 20 69 6e 20 77 68 69 63 68 20 0a 20 20 20 20 , in which .
ca430 20 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d * it actually m
ca440 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 eans what it say
ca450 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 s */.#endif. ca
ca460 73 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 se EIO:. case E
ca470 42 41 44 46 3a 0a 20 20 63 61 73 65 20 45 49 4e BADF:. case EIN
ca480 56 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 VAL:. case ENOT
ca490 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f CONN:. case ENO
ca4a0 44 45 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 DEV:. case ENXI
ca4b0 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 O:. case ENOENT
ca4c0 3a 0a 20 20 63 61 73 65 20 45 53 54 41 4c 45 3a :. case ESTALE:
ca4d0 0a 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a . case ENOSYS:.
ca4e0 20 20 20 20 2f 2a 20 74 68 65 73 65 20 73 68 6f /* these sho
ca4f0 75 6c 64 20 66 6f 72 63 65 20 74 68 65 20 63 6c uld force the cl
ca500 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 ient to close th
ca510 65 20 66 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e e file and recon
ca520 6e 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 nect */. . d
ca530 65 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 efault: . ret
ca540 75 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 3b urn sqliteIOErr;
ca550 0a 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a . }.}..../*****
ca560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ca5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
ca5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
ca5c0 69 6e 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 in Unique File I
ca5d0 44 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 D Utility Used B
ca5e0 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a y VxWorks ******
ca5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
ca600 4f 6e 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73 On most versions
ca610 20 6f 66 20 75 6e 69 78 2c 20 77 65 20 63 61 6e of unix, we can
ca620 20 67 65 74 20 61 20 75 6e 69 71 75 65 20 49 44 get a unique ID
ca630 20 66 6f 72 20 61 20 66 69 6c 65 20 62 79 20 63 for a file by c
ca640 6f 6e 63 61 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 oncatenating.**
ca650 74 68 65 20 64 65 76 69 63 65 20 6e 75 6d 62 65 the device numbe
ca660 72 20 61 6e 64 20 74 68 65 20 69 6e 6f 64 65 20 r and the inode
ca670 6e 75 6d 62 65 72 2e 20 20 42 75 74 20 74 68 69 number. But thi
ca680 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 s does not work
ca690 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f on VxWorks..** O
ca6a0 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69 n VxWorks, a uni
ca6b0 71 75 65 20 66 69 6c 65 20 69 64 20 6d 75 73 74 que file id must
ca6c0 20 62 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 be based on the
ca6d0 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e canonical filen
ca6e0 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 ame..**.** A poi
ca6f0 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
ca700 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
ca710 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 wing structure c
ca720 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 0a an be used as a.
ca730 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 ** unique file I
ca740 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e 20 20 45 D in VxWorks. E
ca750 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 ach instance of
ca760 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 this structure c
ca770 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 ontains.** a cop
ca780 79 20 6f 66 20 74 68 65 20 63 61 6e 6f 6e 69 63 y of the canonic
ca790 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 20 54 68 al filename. Th
ca7a0 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20 72 65 ere is also a re
ca7b0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 20 ference count.
ca7c0 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74 75 72 .** The structur
ca7d0 65 20 69 73 20 72 65 63 6c 61 69 6d 65 64 20 77 e is reclaimed w
ca7e0 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f hen the number o
ca7f0 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 69 74 f pointers to it
ca800 20 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 drops to.** zer
ca810 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 o..**.** There a
ca820 72 65 20 6e 65 76 65 72 20 76 65 72 79 20 6d 61 re never very ma
ca830 6e 79 20 66 69 6c 65 73 20 6f 70 65 6e 20 61 74 ny files open at
ca840 20 6f 6e 65 20 74 69 6d 65 20 61 6e 64 20 6c 6f one time and lo
ca850 6f 6b 75 70 73 20 61 72 65 20 6e 6f 74 0a 2a 2a okups are not.**
ca860 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 2d 63 a performance-c
ca870 72 69 74 69 63 61 6c 20 70 61 74 68 2c 20 73 6f ritical path, so
ca880 20 69 74 20 69 73 20 73 75 66 66 69 63 69 65 6e it is sufficien
ca890 74 20 74 6f 20 70 75 74 20 74 68 65 73 65 0a 2a t to put these.*
ca8a0 2a 20 73 74 72 75 63 74 75 72 65 73 20 6f 6e 20 * structures on
ca8b0 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a a linked list..*
ca8c0 2f 0a 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 /.struct vxworks
ca8d0 46 69 6c 65 49 64 20 7b 0a 20 20 73 74 72 75 63 FileId {. struc
ca8e0 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
ca8f0 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 *pNext; /* Next
ca900 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 68 in a list of th
ca910 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 em all */. int
ca920 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 nRef;
ca930 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ca940 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
ca950 73 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f s to this one */
ca960 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 . int nName;
ca970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ca980 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 /* Length of th
ca990 65 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 e zCanonicalName
ca9a0 5b 5d 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 [] string */. c
ca9b0 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e har *zCanonicalN
ca9c0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ame; /*
ca9d0 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 Canonical filena
ca9e0 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 me */.};..#if OS
ca9f0 5f 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 _VXWORKS./* .**
caa00 41 6c 6c 20 75 6e 69 71 75 65 20 66 69 6c 65 6e All unique filen
caa10 61 6d 65 73 20 61 72 65 20 68 65 6c 64 20 6f 6e ames are held on
caa20 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 a linked list h
caa30 65 61 64 65 64 20 62 79 20 74 68 69 73 0a 2a 2a eaded by this.**
caa40 20 76 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 variable:.*/.st
caa50 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77 6f atic struct vxwo
caa60 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 rksFileId *vxwor
caa70 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 30 3b 0a ksFileList = 0;.
caa80 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 20 ./*.** Simplify
caa90 61 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 a filename into
caaa0 69 74 73 20 63 61 6e 6f 6e 69 63 61 6c 20 66 6f its canonical fo
caab0 72 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 rm.** by making
caac0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 the following ch
caad0 61 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 anges:.**.** *
caae0 72 65 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72 61 removing any tra
caaf0 69 6c 69 6e 67 20 61 6e 64 20 64 75 70 6c 69 63 iling and duplic
cab00 61 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 ate /.** * conv
cab10 65 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 ert /./ into jus
cab20 74 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 t /.** * conver
cab30 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41 t /A/../ where A
cab40 20 69 73 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e is any simple n
cab50 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a ame into just /.
cab60 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 61 72 **.** Changes ar
cab70 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63 65 2e e made in-place.
cab80 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 65 77 Return the new
cab90 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a name length..**
caba0 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c .** The original
cabb0 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 20 filename is in
cabc0 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 z[0..n-1]. Retu
cabd0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
cabe0 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 69 .** characters i
cabf0 6e 20 74 68 65 20 73 69 6d 70 6c 69 66 69 65 64 n the simplified
cac00 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 name..*/.static
cac10 20 69 6e 74 20 76 78 77 6f 72 6b 73 53 69 6d 70 int vxworksSimp
cac20 6c 69 66 79 4e 61 6d 65 28 63 68 61 72 20 2a 7a lifyName(char *z
cac30 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 , int n){. int
cac40 69 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 28 20 6e i, j;. while( n
cac50 3e 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f >1 && z[n-1]=='/
cac60 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f ' ){ n--; }. fo
cac70 72 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b r(i=j=0; i<n; i+
cac80 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d +){. if( z[i]
cac90 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 =='/' ){. i
caca0 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 f( z[i+1]=='/' )
cacb0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 continue;.
cacc0 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 if( z[i+1]=='.'
cacd0 20 26 26 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 && i+2<n && z[i
cace0 2b 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 +2]=='/' ){.
cacf0 20 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 i += 1;.
cad00 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 continue;.
cad10 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
cad20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 z[i+1]=='.' && i
cad30 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d +3<n && z[i+2]==
cad40 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 '.' && z[i+3]=='
cad50 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 /' ){. wh
cad60 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d ile( j>0 && z[j-
cad70 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 1]!='/' ){ j--;
cad80 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e }. if( j>
cad90 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 0 ){ j--; }.
cada0 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 i += 2;.
cadb0 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 continue;.
cadc0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
cadd0 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 z[j++] = z[i];.
cade0 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 }. z[j] = 0;.
cadf0 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a return j;.}../*
cae00 0a 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69 71 75 .** Find a uniqu
cae10 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 74 68 e file ID for th
cae20 65 20 67 69 76 65 6e 20 61 62 73 6f 6c 75 74 65 e given absolute
cae30 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 74 75 pathname. Retu
cae40 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 rn.** a pointer
cae50 74 6f 20 74 68 65 20 76 78 77 6f 72 6b 73 46 69 to the vxworksFi
cae60 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 leId object. Th
cae70 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20 74 68 is pointer is th
cae80 65 20 75 6e 69 71 75 65 0a 2a 2a 20 66 69 6c 65 e unique.** file
cae90 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ID..**.** The n
caea0 52 65 66 20 66 69 65 6c 64 20 6f 66 20 74 68 65 Ref field of the
caeb0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f vxworksFileId o
caec0 62 6a 65 63 74 20 69 73 20 69 6e 63 72 65 6d 65 bject is increme
caed0 6e 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 74 nted before.** t
caee0 68 65 20 6f 62 6a 65 63 74 20 69 73 20 72 65 74 he object is ret
caef0 75 72 6e 65 64 2e 20 20 41 20 6e 65 77 20 76 78 urned. A new vx
caf00 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 worksFileId obje
caf10 63 74 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a ct is created.**
caf20 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 and added to th
caf30 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20 69 66 e global list if
caf40 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a necessary..**.*
caf50 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c * If a memory al
caf60 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f location error o
caf70 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 ccurs, return NU
caf80 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 LL..*/.static st
caf90 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
cafa0 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6e 64 46 Id *vxworksFindF
cafb0 69 6c 65 49 64 28 63 6f 6e 73 74 20 63 68 61 72 ileId(const char
cafc0 20 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 *zAbsoluteName)
cafd0 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 {. struct vxwor
cafe0 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 77 3b 20 ksFileId *pNew;
caff0 20 20 20 20 20 20 20 20 2f 2a 20 73 65 61 72 63 /* searc
cb000 68 20 6b 65 79 20 61 6e 64 20 6e 65 77 20 66 69 h key and new fi
cb010 6c 65 20 49 44 20 2a 2f 0a 20 20 73 74 72 75 63 le ID */. struc
cb020 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 t vxworksFileId
cb030 2a 70 43 61 6e 64 69 64 61 74 65 3b 20 20 20 2f *pCandidate; /
cb040 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 * For looping ov
cb050 65 72 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 er existing file
cb060 20 49 44 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b IDs */. int n;
cb070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
cb090 20 4c 65 6e 67 74 68 20 6f 66 20 7a 41 62 73 6f Length of zAbso
cb0a0 6c 75 74 65 4e 61 6d 65 20 73 74 72 69 6e 67 20 luteName string
cb0b0 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 41 */.. assert( zA
cb0c0 62 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d bsoluteName[0]==
cb0d0 27 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 28 69 6e '/' );. n = (in
cb0e0 74 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 t)strlen(zAbsolu
cb0f0 74 65 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 77 20 teName);. pNew
cb100 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
cb110 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 ( sizeof(*pNew)
cb120 2b 20 28 6e 2b 31 29 20 29 3b 0a 20 20 69 66 28 + (n+1) );. if(
cb130 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 pNew==0 ) retur
cb140 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 n 0;. pNew->zCa
cb150 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 28 63 nonicalName = (c
cb160 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 har*)&pNew[1];.
cb170 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 43 memcpy(pNew->zC
cb180 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 anonicalName, zA
cb190 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 bsoluteName, n+1
cb1a0 29 3b 0a 20 20 6e 20 3d 20 76 78 77 6f 72 6b 73 );. n = vxworks
cb1b0 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 70 4e 65 SimplifyName(pNe
cb1c0 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d w->zCanonicalNam
cb1d0 65 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 e, n);.. /* Sea
cb1e0 72 63 68 20 66 6f 72 20 61 6e 20 65 78 69 73 74 rch for an exist
cb1f0 69 6e 67 20 65 6e 74 72 79 20 74 68 61 74 20 6d ing entry that m
cb200 61 74 63 68 69 6e 67 20 74 68 65 20 63 61 6e 6f atching the cano
cb210 6e 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a nical name.. **
cb220 20 49 66 20 66 6f 75 6e 64 2c 20 69 6e 63 72 65 If found, incre
cb230 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e ment the referen
cb240 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 ce count and ret
cb250 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
cb260 0a 20 20 2a 2a 20 74 68 65 20 65 78 69 73 74 69 . ** the existi
cb270 6e 67 20 66 69 6c 65 20 49 44 2e 0a 20 20 2a 2f ng file ID.. */
cb280 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
cb290 78 28 29 3b 0a 20 20 66 6f 72 28 70 43 61 6e 64 x();. for(pCand
cb2a0 69 64 61 74 65 3d 76 78 77 6f 72 6b 73 46 69 6c idate=vxworksFil
cb2b0 65 4c 69 73 74 3b 20 70 43 61 6e 64 69 64 61 74 eList; pCandidat
cb2c0 65 3b 20 70 43 61 6e 64 69 64 61 74 65 3d 70 43 e; pCandidate=pC
cb2d0 61 6e 64 69 64 61 74 65 2d 3e 70 4e 65 78 74 29 andidate->pNext)
cb2e0 7b 0a 20 20 20 20 69 66 28 20 70 43 61 6e 64 69 {. if( pCandi
cb2f0 64 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a date->nName==n .
cb300 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 && memcmp(p
cb310 43 61 6e 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f Candidate->zCano
cb320 6e 69 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d nicalName, pNew-
cb330 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c >zCanonicalName,
cb340 20 6e 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 n)==0. ){.
cb350 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
cb360 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 e(pNew);.
cb370 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66 pCandidate->nRef
cb380 2b 2b 3b 0a 20 20 20 20 20 20 20 75 6e 69 78 4c ++;. unixL
cb390 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 eaveMutex();.
cb3a0 20 20 20 20 72 65 74 75 72 6e 20 70 43 61 6e 64 return pCand
cb3b0 69 64 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d idate;. }. }
cb3c0 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 .. /* No match
cb3d0 77 61 73 20 66 6f 75 6e 64 2e 20 20 57 65 20 77 was found. We w
cb3e0 69 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 77 20 66 ill make a new f
cb3f0 69 6c 65 20 49 44 20 2a 2f 0a 20 20 70 4e 65 77 ile ID */. pNew
cb400 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e ->nRef = 1;. pN
cb410 65 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 ew->nName = n;.
cb420 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 76 pNew->pNext = v
cb430 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 0a xworksFileList;.
cb440 20 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 vxworksFileLis
cb450 74 20 3d 20 70 4e 65 77 3b 0a 20 20 75 6e 69 78 t = pNew;. unix
cb460 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
cb470 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a return pNew;.}..
cb480 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 /*.** Decrement
cb490 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
cb4a0 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73 unt on a vxworks
cb4b0 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 FileId object.
cb4c0 46 72 65 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 Free.** the obje
cb4d0 63 74 20 77 68 65 6e 20 74 68 65 20 72 65 66 65 ct when the refe
cb4e0 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 rence count reac
cb4f0 68 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 hes zero..*/.sta
cb500 74 69 63 20 76 6f 69 64 20 76 78 77 6f 72 6b 73 tic void vxworks
cb510 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 73 74 ReleaseFileId(st
cb520 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
cb530 49 64 20 2a 70 49 64 29 7b 0a 20 20 75 6e 69 78 Id *pId){. unix
cb540 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
cb550 61 73 73 65 72 74 28 20 70 49 64 2d 3e 6e 52 65 assert( pId->nRe
cb560 66 3e 30 20 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 f>0 );. pId->nR
cb570 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 49 64 2d ef--;. if( pId-
cb580 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 >nRef==0 ){.
cb590 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 struct vxworksFi
cb5a0 6c 65 49 64 20 2a 2a 70 70 3b 0a 20 20 20 20 66 leId **pp;. f
cb5b0 6f 72 28 70 70 3d 26 76 78 77 6f 72 6b 73 46 69 or(pp=&vxworksFi
cb5c0 6c 65 4c 69 73 74 3b 20 2a 70 70 20 26 26 20 2a leList; *pp && *
cb5d0 70 70 21 3d 70 49 64 3b 20 70 70 20 3d 20 26 28 pp!=pId; pp = &(
cb5e0 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 7b 7d (*pp)->pNext)){}
cb5f0 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 70 . assert( *pp
cb600 3d 3d 70 49 64 20 29 3b 0a 20 20 20 20 2a 70 70 ==pId );. *pp
cb610 20 3d 20 70 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 = pId->pNext;.
cb620 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
cb630 70 49 64 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 pId);. }. unix
cb640 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a LeaveMutex();.}.
cb650 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 #endif /* OS_VXW
cb660 4f 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ORKS */./*******
cb670 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
cb680 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 Unique File ID U
cb690 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 tility Used By V
cb6a0 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a xWorks *********
cb6b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
cb6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb700 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a ******/.../*****
cb710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb750 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
cb760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb770 2a 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 64 76 69 ***** Posix Advi
cb780 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a sory Locking ***
cb790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
cb7b0 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c POSIX advisory l
cb7c0 6f 63 6b 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 ocks are broken
cb7d0 62 79 20 64 65 73 69 67 6e 2e 20 20 41 4e 53 49 by design. ANSI
cb7e0 20 53 54 44 20 31 30 30 33 2e 31 20 28 31 39 39 STD 1003.1 (199
cb7f0 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 6).** section 6.
cb800 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 5.2.2 lines 483
cb810 74 68 72 6f 75 67 68 20 34 39 30 20 73 70 65 63 through 490 spec
cb820 69 66 79 20 74 68 61 74 20 77 68 65 6e 20 61 20 ify that when a
cb830 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 process.** sets
cb840 6f 72 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b or clears a lock
cb850 2c 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e , that operation
cb860 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 overrides any p
cb870 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a rior locks set.*
cb880 2a 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 * by the same pr
cb890 6f 63 65 73 73 2e 20 20 49 74 20 64 6f 65 73 20 ocess. It does
cb8a0 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 20 73 not explicitly s
cb8b0 61 79 20 73 6f 2c 20 62 75 74 20 74 68 69 73 20 ay so, but this
cb8c0 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 implies.** that
cb8d0 69 74 20 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 it overrides loc
cb8e0 6b 73 20 73 65 74 20 62 79 20 74 68 65 20 73 61 ks set by the sa
cb8f0 6d 65 20 70 72 6f 63 65 73 73 20 75 73 69 6e 67 me process using
cb900 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 a different.**
cb910 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e file descriptor.
cb920 20 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 Consider this
cb930 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a test case:.**.**
cb940 20 20 20 20 20 20 20 69 6e 74 20 66 64 31 20 3d int fd1 =
cb950 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c open("./file1",
cb960 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c O_RDWR|O_CREAT,
cb970 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20 0644);.**
cb980 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e 28 int fd2 = open(
cb990 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57 "./file2", O_RDW
cb9a0 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 R|O_CREAT, 0644)
cb9b0 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 ;.**.** Suppose
cb9c0 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69 ./file1 and ./fi
cb9d0 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79 20 74 le2 are really t
cb9e0 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28 62 65 he same file (be
cb9f0 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 cause.** one is
cba00 61 20 68 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c a hard or symbol
cba10 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f ic link to the o
cba20 74 68 65 72 29 20 74 68 65 6e 20 69 66 20 79 6f ther) then if yo
cba30 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c u set.** an excl
cba40 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 usive lock on fd
cba50 31 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20 67 1, then try to g
cba60 65 74 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 et an exclusive
cba70 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 lock.** on fd2,
cba80 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75 it works. I wou
cba90 6c 64 20 68 61 76 65 20 65 78 70 65 63 74 65 64 ld have expected
cbaa0 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b the second lock
cbab0 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63 to.** fail sinc
cbac0 65 20 74 68 65 72 65 20 77 61 73 20 61 6c 72 65 e there was alre
cbad0 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 ady a lock on th
cbae0 65 20 66 69 6c 65 20 64 75 65 20 74 6f 20 66 64 e file due to fd
cbaf0 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f 1..** But not so
cbb00 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f . Since both lo
cbb10 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20 74 68 cks came from th
cbb20 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2c 20 e same process,
cbb30 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 the.** second ov
cbb40 65 72 72 69 64 65 73 20 74 68 65 20 66 69 72 73 errides the firs
cbb50 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 t, even though t
cbb60 68 65 79 20 77 65 72 65 20 6f 6e 20 64 69 66 66 hey were on diff
cbb70 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 erent.** file de
cbb80 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 scriptors opened
cbb90 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 20 66 69 on different fi
cbba0 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 le names..**.**
cbbb0 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 This means that
cbbc0 77 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f we cannot use PO
cbbd0 53 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e SIX locks to syn
cbbe0 63 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 chronize file ac
cbbf0 63 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f cess.** among co
cbc00 6d 70 65 74 69 6e 67 20 74 68 72 65 61 64 73 20 mpeting threads
cbc10 6f 66 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 of the same proc
cbc20 65 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b ess. POSIX lock
cbc30 73 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 s will work fine
cbc40 0a 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 .** to synchroni
cbc50 7a 65 20 61 63 63 65 73 73 20 66 6f 72 20 74 68 ze access for th
cbc60 72 65 61 64 73 20 69 6e 20 73 65 70 61 72 61 74 reads in separat
cbc70 65 20 70 72 6f 63 65 73 73 65 73 2c 20 62 75 74 e processes, but
cbc80 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 not.** threads
cbc90 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 within the same
cbca0 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 process..**.** T
cbcb0 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 o work around th
cbcc0 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 e problem, SQLit
cbcd0 65 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 e has to manage
cbce0 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 file locks inter
cbcf0 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 nally.** on its
cbd00 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 61 own. Whenever a
cbd10 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 69 73 new database is
cbd20 20 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76 65 opened, we have
cbd30 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 to find the.**
cbd40 73 70 65 63 69 66 69 63 20 69 6e 6f 64 65 20 6f specific inode o
cbd50 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
cbd60 69 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 20 69 ile (the inode i
cbd70 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 s determined by
cbd80 74 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e the.** st_dev an
cbd90 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 d st_ino fields
cbda0 6f 66 20 74 68 65 20 73 74 61 74 20 73 74 72 75 of the stat stru
cbdb0 63 74 75 72 65 20 74 68 61 74 20 66 73 74 61 74 cture that fstat
cbdc0 28 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 () fills in).**
cbdd0 61 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f and check for lo
cbde0 63 6b 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 cks already exis
cbdf0 74 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f ting on that ino
cbe00 64 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 de. When locks
cbe10 61 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f are.** created o
cbe20 72 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 r removed, we ha
cbe30 76 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 ve to look at ou
cbe40 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 r own internal r
cbe50 65 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 ecord of the.**
cbe60 6c 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 locks to see if
cbe70 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 68 another thread h
cbe80 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 as previously se
cbe90 74 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 t a lock on that
cbea0 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a same.** inode..
cbeb0 2a 2a 0a 2a 2a 20 28 41 73 69 64 65 3a 20 54 68 **.** (Aside: Th
cbec0 65 20 75 73 65 20 6f 66 20 69 6e 6f 64 65 20 6e e use of inode n
cbed0 75 6d 62 65 72 73 20 61 73 20 75 6e 69 71 75 65 umbers as unique
cbee0 20 49 44 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f IDs does not wo
cbef0 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a rk on VxWorks..*
cbf00 2a 20 46 6f 72 20 56 78 57 6f 72 6b 73 2c 20 77 * For VxWorks, w
cbf10 65 20 68 61 76 65 20 74 6f 20 75 73 65 20 74 68 e have to use th
cbf20 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 75 6e e alternative un
cbf30 69 71 75 65 20 49 44 20 73 79 73 74 65 6d 20 62 ique ID system b
cbf40 61 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e ased on.** canon
cbf50 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 61 6e ical filename an
cbf60 64 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e d implemented in
cbf70 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 64 69 the previous di
cbf80 76 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 vision.).**.** T
cbf90 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 he sqlite3_file
cbfa0 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 50 4f structure for PO
cbfb0 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 SIX is no longer
cbfc0 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 just an integer
cbfd0 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 file.** descrip
cbfe0 74 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 tor. It is now
cbff0 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 a structure that
cc000 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 74 65 67 holds the integ
cc010 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 er file.** descr
cc020 69 70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e iptor and a poin
cc030 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 74 75 ter to a structu
cc040 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 re that describe
cc050 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a s the internal.*
cc060 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63 * locks on the c
cc070 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f orresponding ino
cc080 64 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e de. There is on
cc090 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 e locking struct
cc0a0 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65 ure.** per inode
cc0b0 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61 6d 65 , so if the same
cc0c0 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65 64 inode is opened
cc0d0 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75 6e 69 twice, both uni
cc0e0 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 73 xFile structures
cc0f0 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 .** point to the
cc100 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 same locking st
cc110 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 6c 6f ructure. The lo
cc120 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 cking structure
cc130 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 66 65 72 keeps.** a refer
cc140 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f 20 77 ence count (so w
cc150 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e e will know when
cc160 20 74 6f 20 64 65 6c 65 74 65 20 69 74 29 20 61 to delete it) a
cc170 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69 nd a "cnt".** fi
cc180 65 6c 64 20 74 68 61 74 20 74 65 6c 6c 73 20 75 eld that tells u
cc190 73 20 69 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c s its internal l
cc1a0 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 63 6e 74 ock status. cnt
cc1b0 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a ==0 means the.**
cc1c0 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 file is unlocke
cc1d0 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e d. cnt==-1 mean
cc1e0 73 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 61 s the file has a
cc1f0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
cc200 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73 ..** cnt>0 means
cc210 20 74 68 65 72 65 20 61 72 65 20 63 6e 74 20 73 there are cnt s
cc220 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 hared locks on t
cc230 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 he file..**.** A
cc240 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f ny attempt to lo
cc250 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66 ck or unlock a f
cc260 69 6c 65 20 66 69 72 73 74 20 63 68 65 63 6b 73 ile first checks
cc270 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 the locking.**
cc280 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
cc290 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63 fcntl() system c
cc2a0 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f all is only invo
cc2b0 6b 65 64 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a ked to set a .**
cc2c0 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66 20 74 POSIX lock if t
cc2d0 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b he internal lock
cc2e0 20 73 74 72 75 63 74 75 72 65 20 74 72 61 6e 73 structure trans
cc2f0 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a itions between.*
cc300 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 * a locked and a
cc310 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65 n unlocked state
cc320 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 69 74 ..**.** But wait
cc330 3a 20 20 74 68 65 72 65 20 61 72 65 20 79 65 74 : there are yet
cc340 20 6d 6f 72 65 20 70 72 6f 62 6c 65 6d 73 20 77 more problems w
cc350 69 74 68 20 50 4f 53 49 58 20 61 64 76 69 73 6f ith POSIX adviso
cc360 72 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 ry locks..**.**
cc370 49 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 If you close a f
cc380 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
cc390 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 hat points to a
cc3a0 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 6c 6f file that has lo
cc3b0 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b cks,.** all lock
cc3c0 73 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74 s on that file t
cc3d0 68 61 74 20 61 72 65 20 6f 77 6e 65 64 20 62 79 hat are owned by
cc3e0 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f the current pro
cc3f0 63 65 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65 cess are.** rele
cc400 61 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61 ased. To work a
cc410 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c round this probl
cc420 65 6d 2c 20 65 61 63 68 20 75 6e 69 78 46 69 6c em, each unixFil
cc430 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 e structure cont
cc440 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 ains.** a pointe
cc450 72 20 74 6f 20 61 6e 20 75 6e 69 78 4f 70 65 6e r to an unixOpen
cc460 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 20 Cnt structure.
cc470 54 68 65 72 65 20 69 73 20 6f 6e 65 20 75 6e 69 There is one uni
cc480 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 xOpenCnt structu
cc490 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 re.** per open i
cc4a0 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 61 6e node, which mean
cc4b0 73 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 s that multiple
cc4c0 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 unixFile can poi
cc4d0 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a nt to a single.*
cc4e0 2a 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 20 * unixOpenCnt.
cc4f0 57 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 When an attempt
cc500 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 is made to close
cc510 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 an unixFile, if
cc520 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 there are.** ot
cc530 68 65 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 her unixFile ope
cc540 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 6e n on the same in
cc550 6f 64 65 20 74 68 61 74 20 61 72 65 20 68 6f 6c ode that are hol
cc560 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 ding locks, the
cc570 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 call.** to close
cc580 28 29 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 () the file desc
cc590 72 69 70 74 6f 72 20 69 73 20 64 65 66 65 72 72 riptor is deferr
cc5a0 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 ed until all of
cc5b0 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e the locks clear.
cc5c0 0a 2a 2a 20 54 68 65 20 75 6e 69 78 4f 70 65 6e .** The unixOpen
cc5d0 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 6b 65 Cnt structure ke
cc5e0 65 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 eps a list of fi
cc5f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 le descriptors t
cc600 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 hat need to.** b
cc610 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 e closed and tha
cc620 74 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 t list is walked
cc630 20 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 (and cleared) w
cc640 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 hen the last loc
cc650 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a k.** clears..**.
cc660 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70 ** Yet another p
cc670 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 roblem: LinuxTh
cc680 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 reads do not pla
cc690 79 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 y well with posi
cc6a0 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d x locks..**.** M
cc6b0 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f any older versio
cc6c0 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 ns of linux use
cc6d0 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 the LinuxThreads
cc6e0 20 6c 69 62 72 61 72 79 20 77 68 69 63 68 20 69 library which i
cc6f0 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 s.** not posix c
cc700 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 ompliant. Under
cc710 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61 LinuxThreads, a
cc720 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79 lock created by
cc730 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e thread.** A can
cc740 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 not be modified
cc750 6f 72 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 or overridden by
cc760 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 a different thr
cc770 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 ead B..** Only t
cc780 68 72 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 hread A can modi
cc790 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f fy the lock. Lo
cc7a0 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69 cking behavior i
cc7b0 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 s correct.** if
cc7c0 74 68 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 the appliation u
cc7d0 73 65 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61 ses the newer Na
cc7e0 74 69 76 65 20 50 6f 73 69 78 20 54 68 72 65 61 tive Posix Threa
cc7f0 64 20 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29 d Library (NPTL)
cc800 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 .** on linux - w
cc810 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 ith NPTL a lock
cc820 63 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61 created by threa
cc830 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 d A can override
cc840 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 locks.** in thr
cc850 65 61 64 20 42 2e 20 20 42 75 74 20 74 68 65 72 ead B. But ther
cc860 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b e is no way to k
cc870 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 now at compile-t
cc880 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 ime which.** thr
cc890 65 61 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69 eading library i
cc8a0 73 20 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53 s being used. S
cc8b0 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 o there is no wa
cc8c0 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 y to know at.**
cc8d0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 compile-time whe
cc8e0 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 ther or not thre
cc8f0 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 ad A can overrid
cc900 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 e locks on threa
cc910 64 20 42 2e 0a 2a 2a 20 57 65 20 68 61 76 65 20 d B..** We have
cc920 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 to do a run-time
cc930 20 63 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 check to discov
cc940 65 72 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 er the behavior
cc950 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e of the.** curren
cc960 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a t process..**.**
cc970 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 68 65 72 On systems wher
cc980 65 20 74 68 72 65 61 64 20 41 20 69 73 20 75 6e e thread A is un
cc990 61 62 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20 6c able to modify l
cc9a0 6f 63 6b 73 20 63 72 65 61 74 65 64 20 62 79 0a ocks created by.
cc9b0 2a 2a 20 74 68 72 65 61 64 20 42 2c 20 77 65 20 ** thread B, we
cc9c0 68 61 76 65 20 74 6f 20 6b 65 65 70 20 74 72 61 have to keep tra
cc9d0 63 6b 20 6f 66 20 77 68 69 63 68 20 74 68 72 65 ck of which thre
cc9e0 61 64 20 63 72 65 61 74 65 64 20 65 61 63 68 0a ad created each.
cc9f0 2a 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 20 ** lock. Hence
cca00 74 68 65 72 65 20 69 73 20 61 6e 20 65 78 74 72 there is an extr
cca10 61 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 6b a field in the k
cca20 65 79 20 74 6f 20 74 68 65 20 75 6e 69 78 4c 6f ey to the unixLo
cca30 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 ckInfo.** struct
cca40 75 72 65 20 74 6f 20 72 65 63 6f 72 64 20 74 68 ure to record th
cca50 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 is information.
cca60 20 41 6e 64 20 6f 6e 20 74 68 6f 73 65 20 73 79 And on those sy
cca70 73 74 65 6d 73 20 69 74 0a 2a 2a 20 69 73 20 69 stems it.** is i
cca80 6c 6c 65 67 61 6c 20 74 6f 20 62 65 67 69 6e 20 llegal to begin
cca90 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e a transaction in
ccaa0 20 6f 6e 65 20 74 68 72 65 61 64 20 61 6e 64 20 one thread and
ccab0 66 69 6e 69 73 68 20 69 74 0a 2a 2a 20 69 6e 20 finish it.** in
ccac0 61 6e 6f 74 68 65 72 2e 20 20 46 6f 72 20 74 68 another. For th
ccad0 69 73 20 6c 61 74 74 65 72 20 72 65 73 74 72 69 is latter restri
ccae0 63 74 69 6f 6e 2c 20 74 68 65 72 65 20 69 73 20 ction, there is
ccaf0 6e 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a no work-around..
ccb00 2a 2a 20 49 74 20 69 73 20 61 20 6c 69 6d 69 74 ** It is a limit
ccb10 61 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 68 ation of LinuxTh
ccb20 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a reads..*/../*.**
ccb30 20 53 65 74 20 6f 72 20 63 68 65 63 6b 20 74 68 Set or check th
ccb40 65 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 e unixFile.tid f
ccb50 69 65 6c 64 2e 20 20 54 68 69 73 20 66 69 65 6c ield. This fiel
ccb60 64 20 69 73 20 73 65 74 20 77 68 65 6e 20 61 6e d is set when an
ccb70 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 unixFile.** is
ccb80 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 first opened. A
ccb90 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 ll subsequent us
ccba0 65 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 es of the unixFi
ccbb0 6c 65 20 76 65 72 69 66 79 20 74 68 61 74 20 74 le verify that t
ccbc0 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 he.** same threa
ccbd0 64 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 6f d is operating o
ccbe0 6e 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 n the unixFile.
ccbf0 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 Some operating
ccc00 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f systems do.** no
ccc10 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f t allow locks to
ccc20 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 be overridden b
ccc30 79 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 y other threads
ccc40 61 6e 64 20 74 68 61 74 20 72 65 73 74 72 69 63 and that restric
ccc50 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 tion.** means th
ccc60 61 74 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 at sqlite3* data
ccc70 62 61 73 65 20 68 61 6e 64 6c 65 73 20 63 61 6e base handles can
ccc80 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 72 6f not be moved fro
ccc90 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 m one thread.**
ccca0 74 6f 20 61 6e 6f 74 68 65 72 20 77 68 69 6c 65 to another while
cccb0 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e locks are held.
cccc0 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 .**.** Version 3
cccd0 2e 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 35 .3.1 (2006-01-15
ccce0 29 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 61 6e ): unixFile can
cccf0 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f be moved from o
ccd00 6e 65 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 ne thread to.**
ccd10 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 another as long
ccd20 61 73 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e as we are runnin
ccd30 67 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 68 g on a system th
ccd40 61 74 20 73 75 70 70 6f 72 74 73 20 74 68 72 65 at supports thre
ccd50 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e ads.** overridin
ccd60 67 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f g each others lo
ccd70 63 6b 73 20 28 77 68 69 63 68 20 69 73 20 6e 6f cks (which is no
ccd80 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f w the most commo
ccd90 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f n behavior).** o
ccda0 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 r if no locks ar
ccdb0 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65 e held. But the
ccdc0 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 unixFile.pLock
ccdd0 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 field needs to b
ccde0 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 e.** recomputed
ccdf0 62 65 63 61 75 73 65 20 69 74 73 20 6b 65 79 20 because its key
cce00 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68 72 includes the thr
cce10 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65 ead-id. See the
cce20 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e .** transferOwn
cce30 65 72 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f ership() functio
cce40 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 n below for addi
cce50 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
cce60 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 on.*/.#if SQLITE
cce70 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
cce80 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
cce90 29 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 ).# define SET_T
ccea0 48 52 45 41 44 49 44 28 58 29 20 20 20 28 58 29 HREADID(X) (X)
cceb0 2d 3e 74 69 64 20 3d 20 70 74 68 72 65 61 64 5f ->tid = pthread_
ccec0 73 65 6c 66 28 29 0a 23 20 64 65 66 69 6e 65 20 self().# define
cced0 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 CHECK_THREADID(X
ccee0 29 20 28 74 68 72 65 61 64 73 4f 76 65 72 72 69 ) (threadsOverri
ccef0 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b deEachOthersLock
ccf00 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20 20 20 s==0 && \.
ccf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ccf20 20 20 20 20 20 20 21 70 74 68 72 65 61 64 5f 65 !pthread_e
ccf30 71 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c 20 70 qual((X)->tid, p
ccf40 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 29 0a thread_self())).
ccf50 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
ccf60 45 54 5f 54 48 52 45 41 44 49 44 28 58 29 0a 23 ET_THREADID(X).#
ccf70 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 define CHECK_TH
ccf80 52 45 41 44 49 44 28 58 29 20 30 0a 23 65 6e 64 READID(X) 0.#end
ccf90 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 if../*.** An ins
ccfa0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
ccfb0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
ccfc0 20 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b serves as the k
ccfd0 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f ey used.** to lo
ccfe0 63 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 cate a particula
ccff0 72 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 r unixOpenCnt st
cd000 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 ructure given it
cd010 73 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 0a 2a s inode. This.*
cd020 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 * is the same as
cd030 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 the unixLockKey
cd040 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
cd050 20 74 68 72 65 61 64 20 49 44 20 69 73 20 6f 6d thread ID is om
cd060 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 itted..*/.struct
cd070 20 75 6e 69 78 46 69 6c 65 49 64 20 7b 0a 20 20 unixFileId {.
cd080 64 65 76 5f 74 20 64 65 76 3b 20 20 20 20 20 20 dev_t dev;
cd090 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
cd0a0 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a evice number */.
cd0b0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
cd0c0 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 struct vxworksF
cd0d0 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 2f 2a 20 ileId *pId; /*
cd0e0 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 66 Unique file ID f
cd0f0 6f 72 20 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 or vxworks. */.#
cd100 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f else. ino_t ino
cd110 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
cd120 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 /* Inode numb
cd130 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a er */.#endif.};.
cd140 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
cd150 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
cd160 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65 ing structure se
cd170 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 rves as the key
cd180 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 used.** to locat
cd190 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 e a particular u
cd1a0 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 nixLockInfo stru
cd1b0 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 cture given its
cd1c0 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 inode..**.** If
cd1d0 74 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f threads cannot o
cd1e0 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 verride each oth
cd1f0 65 72 73 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 ers locks (Linux
cd200 54 68 72 65 61 64 73 29 2c 20 74 68 65 6e 20 77 Threads), then w
cd210 65 0a 2a 2a 20 73 65 74 20 74 68 65 20 75 6e 69 e.** set the uni
cd220 78 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 xLockKey.tid fie
cd230 6c 64 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 ld to the thread
cd240 20 49 44 2e 20 20 49 66 20 74 68 72 65 61 64 73 ID. If threads
cd250 20 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a can override.**
cd260 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 each others loc
cd270 6b 73 20 28 50 6f 73 69 78 20 61 6e 64 20 4e 50 ks (Posix and NP
cd280 54 4c 29 20 74 68 65 6e 20 74 69 64 20 69 73 20 TL) then tid is
cd290 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 always set to ze
cd2a0 72 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 20 6f 6d ro..** tid is om
cd2b0 69 74 74 65 64 20 69 66 20 77 65 20 63 6f 6d 70 itted if we comp
cd2c0 69 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72 65 ile without thre
cd2d0 61 64 69 6e 67 20 73 75 70 70 6f 72 74 20 6f 72 ading support or
cd2e0 20 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 on an OS.** oth
cd2f0 65 72 20 74 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a er than linux..*
cd300 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 /.struct unixLoc
cd310 6b 4b 65 79 20 7b 0a 20 20 73 74 72 75 63 74 20 kKey {. struct
cd320 75 6e 69 78 46 69 6c 65 49 64 20 66 69 64 3b 20 unixFileId fid;
cd330 20 2f 2a 20 55 6e 69 71 75 65 20 69 64 65 6e 74 /* Unique ident
cd340 69 66 69 65 72 20 66 6f 72 20 74 68 65 20 66 69 ifier for the fi
cd350 6c 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 le */.#if SQLITE
cd360 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
cd370 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
cd380 29 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 ). pthread_t ti
cd390 64 3b 20 20 2f 2a 20 54 68 72 65 61 64 20 49 44 d; /* Thread ID
cd3a0 20 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 of lock owner.
cd3b0 5a 65 72 6f 20 69 66 20 6e 6f 74 20 75 73 69 6e Zero if not usin
cd3c0 67 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 2a g LinuxThreads *
cd3d0 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a /.#endif.};../*.
cd3e0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
cd3f0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
cd400 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c structure is all
cd410 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 ocated for each
cd420 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 open.** inode.
cd430 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 Or, on LinuxThre
cd440 61 64 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e ads, there is on
cd450 65 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 e of these struc
cd460 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 tures for.** eac
cd470 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 h inode opened b
cd480 79 20 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a y each thread..*
cd490 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e *.** A single in
cd4a0 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c ode can have mul
cd4b0 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 tiple file descr
cd4c0 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 iptors, so each
cd4d0 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 unixFile.** stru
cd4e0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 cture contains a
cd4f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
cd500 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
cd510 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a object and this.
cd520 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 ** object keeps
cd530 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e a count of the n
cd540 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c umber of unixFil
cd550 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 e pointing to it
cd560 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 ..*/.struct unix
cd570 4c 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 LockInfo {. str
cd580 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 uct unixLockKey
cd590 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 20 2f 2a 20 lockKey; /*
cd5a0 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a The lookup key *
cd5b0 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 /. int cnt;
cd5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cd5d0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
cd5e0 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 SHARED locks he
cd5f0 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b ld */. int lock
cd600 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 type;
cd610 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
cd620 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 f SHARED_LOCK, R
cd630 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 ESERVED_LOCK etc
cd640 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b . */. int nRef;
cd650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cd660 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
cd670 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 of pointers to
cd680 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a this structure *
cd690 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c /. struct unixL
cd6a0 6f 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 ockInfo *pNext;
cd6b0 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 /* List of a
cd6c0 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ll unixLockInfo
cd6d0 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 objects */. str
cd6e0 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f uct unixLockInfo
cd6f0 20 2a 70 50 72 65 76 3b 20 20 20 20 20 2f 2a 20 *pPrev; /*
cd700 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c .... doubly l
cd710 69 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a inked */.};../*.
cd720 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
cd730 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
cd740 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c structure is all
cd750 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 ocated for each
cd760 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 open.** inode.
cd770 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6b This structure k
cd780 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68 eeps track of th
cd790 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b e number of lock
cd7a0 73 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f s on that.** ino
cd7b0 64 65 2e 20 20 49 66 20 61 20 63 6c 6f 73 65 20 de. If a close
cd7c0 69 73 20 61 74 74 65 6d 70 74 65 64 20 61 67 61 is attempted aga
cd7d0 69 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20 74 68 inst an inode th
cd7e0 61 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a at is holding.**
cd7f0 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73 locks, the clos
cd800 65 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e e is deferred un
cd810 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c til all locks cl
cd820 65 61 72 20 62 79 20 61 64 64 69 6e 67 20 74 68 ear by adding th
cd830 65 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 e.** file descri
cd840 70 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73 65 ptor to be close
cd850 64 20 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 d to the pending
cd860 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 list..**.** TOD
cd870 4f 3a 20 20 43 6f 6e 73 69 64 65 72 20 63 68 61 O: Consider cha
cd880 6e 67 69 6e 67 20 74 68 69 73 20 73 6f 20 74 68 nging this so th
cd890 61 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 at there is only
cd8a0 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 0a 2a a single file.*
cd8b0 2a 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 * descriptor for
cd8c0 20 65 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 2c each open file,
cd8d0 20 65 76 65 6e 20 77 68 65 6e 20 69 74 20 69 73 even when it is
cd8e0 20 6f 70 65 6e 65 64 20 6d 75 6c 74 69 70 6c 65 opened multiple
cd8f0 20 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 63 times..** The c
cd900 6c 6f 73 65 28 29 20 73 79 73 74 65 6d 20 63 61 lose() system ca
cd910 6c 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 ll would only oc
cd920 63 75 72 20 77 68 65 6e 20 74 68 65 20 6c 61 73 cur when the las
cd930 74 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 75 73 t database.** us
cd940 69 6e 67 20 74 68 65 20 66 69 6c 65 20 63 6c 6f ing the file clo
cd950 73 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 ses..*/.struct u
cd960 6e 69 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 73 nixOpenCnt {. s
cd970 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 truct unixFileId
cd980 20 66 69 6c 65 49 64 3b 20 20 20 2f 2a 20 54 68 fileId; /* Th
cd990 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a e lookup key */.
cd9a0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 int nRef;
cd9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
cd9c0 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 Number of point
cd9d0 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 ers to this stru
cd9e0 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e cture */. int n
cd9f0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 Lock;
cda00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
cda10 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 of outstanding
cda20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 55 locks */. UnixU
cda30 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 nusedFd *pUnused
cda40 3b 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 ; /* Unused
cda50 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
cda60 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 23 69 s to close */.#i
cda70 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 f OS_VXWORKS. s
cda80 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 em_t *pSem;
cda90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cdaa0 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 /* Named POSIX s
cdab0 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 emaphore */. ch
cdac0 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f ar aSemName[MAX_
cdad0 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 20 2f PATHNAME+2]; /
cdae0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 * Name of that s
cdaf0 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 emaphore */.#end
cdb00 69 66 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 if. struct unix
cdb10 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 OpenCnt *pNext,
cdb20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 *pPrev; /* Lis
cdb30 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 t of all unixOpe
cdb40 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a nCnt objects */.
cdb50 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 };../*.** Lists
cdb60 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 of all unixLockI
cdb70 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e nfo and unixOpen
cdb80 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 Cnt objects. Th
cdb90 65 73 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 ese used to be h
cdba0 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 ash.** tables.
cdbb0 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f But the number o
cdbc0 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 f objects is rar
cdbd0 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 ely more than a
cdbe0 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 dozen and.** nev
cdbf0 65 72 20 65 78 63 65 65 64 73 20 61 20 66 65 77 er exceeds a few
cdc00 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 thousand. And
cdc10 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e lookup is not on
cdc20 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 a critical.** p
cdc30 61 74 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 ath so a simple
cdc40 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c linked list will
cdc50 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 suffice..*/.sta
cdc60 74 69 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c tic struct unixL
cdc70 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 ockInfo *lockLis
cdc80 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 t = 0;.static st
cdc90 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 ruct unixOpenCnt
cdca0 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a *openList = 0;.
cdcb0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 ./*.** This vari
cdcc0 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 able remembers w
cdcd0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
cdce0 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 reads can overri
cdcf0 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a de each others.*
cdd00 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 * locks..**.**
cdd10 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 0: No. Threa
cdd20 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 ds cannot overri
cdd30 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c de each others l
cdd40 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 ocks. (LinuxThr
cdd50 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 eads).** 1:
cdd60 59 65 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 Yes. Threads ca
cdd70 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 n override each
cdd80 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 others locks. (
cdd90 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a Posix & NLPT).**
cdda0 20 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 -1: We don't
cddb0 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a know yet..**.**
cddc0 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 On some systems
cddd0 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d , we know at com
cdde0 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 pile-time if thr
cddf0 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 eads can overrid
cde00 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 e each.** others
cde10 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 locks. On thos
cde20 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 e systems, the S
cde30 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 QLITE_THREAD_OVE
cde40 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f RRIDE_LOCK macro
cde50 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 .** will be set
cde60 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 appropriately.
cde70 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 On other systems
cde80 2c 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 , we have to che
cde90 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 ck at.** runtime
cdea0 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 . On these latt
cdeb0 65 72 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 er systems, SQLT
cdec0 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 IE_THREAD_OVERRI
cded0 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e DE_LOCK is.** un
cdee0 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 defined..**.** T
cdef0 68 69 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 his variable nor
cdf00 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 mally has file s
cdf10 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 cope only. But
cdf20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 during testing,
cdf30 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 we make.** it a
cdf40 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 global so that t
cdf50 68 65 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e he test code can
cdf60 20 63 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 change its valu
cdf70 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 e in order to ve
cdf80 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 rify.** that the
cdf90 20 72 69 67 68 74 20 73 74 75 66 66 20 68 61 70 right stuff hap
cdfa0 70 65 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 pens in either c
cdfb0 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 ase..*/.#if SQLI
cdfc0 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
cdfd0 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 defined(__linux
cdfe0 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 __).# ifndef SQ
cdff0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 LITE_THREAD_OVER
ce000 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 RIDE_LOCK.# d
ce010 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 efine SQLITE_THR
ce020 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 EAD_OVERRIDE_LOC
ce030 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 K -1.# endif.#
ce040 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 ifdef SQLITE_TE
ce050 53 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 ST.int threadsOv
ce060 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
ce070 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 Locks = SQLITE_T
ce080 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c HREAD_OVERRIDE_L
ce090 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 OCK;.# else.sta
ce0a0 74 69 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f tic int threadsO
ce0b0 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 verrideEachOther
ce0c0 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f sLocks = SQLITE_
ce0d0 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f THREAD_OVERRIDE_
ce0e0 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 LOCK;.# endif.#
ce0f0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 endif../*.** Thi
ce100 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 s structure hold
ce110 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 s information pa
ce120 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 ssed into indivi
ce130 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 dual test.** thr
ce140 65 61 64 73 20 62 79 20 74 68 65 20 74 65 73 74 eads by the test
ce150 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 ThreadLockingBeh
ce160 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e avior() routine.
ce170 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 .*/.struct threa
ce180 64 54 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e dTestData {. in
ce190 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 t fd;
ce1a0 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 /* File to
ce1b0 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 be locked */. s
ce1c0 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b truct flock lock
ce1d0 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 ; /* The loc
ce1e0 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a king operation *
ce1f0 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 /. int result;
ce200 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
ce210 73 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b sult of the lock
ce220 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f ing operation */
ce230 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f .};..#if SQLITE_
ce240 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
ce250 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
ce260 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
ce270 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 tion is used as
ce280 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 the main routine
ce290 20 66 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 for a thread la
ce2a0 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 unched by.** tes
ce2b0 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 tThreadLockingBe
ce2c0 68 61 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 havior(). It tes
ce2d0 74 73 20 77 68 65 74 68 65 72 20 74 68 65 20 73 ts whether the s
ce2e0 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 hared-lock obtai
ce2f0 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 ned.** by the ma
ce300 69 6e 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 in thread in tes
ce310 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 tThreadLockingBe
ce320 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 havior() conflic
ce330 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 ts with a.** hyp
ce340 6f 74 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d othetical write-
ce350 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 lock obtained by
ce360 20 74 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 this thread on
ce370 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a the same file..*
ce380 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c *.** The write-l
ce390 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 ock is not actua
ce3a0 6c 6c 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 lly acquired, as
ce3b0 20 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 this is not pos
ce3c0 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 sible if .** the
ce3d0 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e file is open in
ce3e0 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 read-only mode
ce3f0 28 73 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 (see ticket #347
ce400 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 2)..*/ .static v
ce410 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 oid *threadLocki
ce420 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 ngTest(void *pAr
ce430 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 g){. struct thr
ce440 65 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 eadTestData *pDa
ce450 74 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 ta = (struct thr
ce460 65 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 eadTestData*)pAr
ce470 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 g;. pData->resu
ce480 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 lt = fcntl(pData
ce490 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 ->fd, F_GETLK, &
ce4a0 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 pData->lock);.
ce4b0 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 return pArg;.}.#
ce4c0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
ce4d0 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 THREADSAFE && de
ce4e0 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 fined(__linux__)
ce4f0 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 */...#if SQLITE
ce500 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
ce510 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
ce520 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f )./*.** This pro
ce530 63 65 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 cedure attempts
ce540 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 to determine whe
ce550 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 ther or not thre
ce560 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 ads.** can overr
ce570 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 ide each others
ce580 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 locks then sets
ce590 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f the .** threadsO
ce5a0 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 verrideEachOther
ce5b0 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 sLocks variable
ce5c0 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a appropriately..*
ce5d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 /.static void te
ce5e0 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 stThreadLockingB
ce5f0 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f ehavior(int fd_o
ce600 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a rig){. int fd;.
ce610 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 int rc;. stru
ce620 63 74 20 74 68 72 65 61 64 54 65 73 74 44 61 74 ct threadTestDat
ce630 61 20 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c a d;. struct fl
ce640 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 ock l;. pthread
ce650 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 _t t;.. fd = du
ce660 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 p(fd_orig);. if
ce670 28 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b ( fd<0 ) return;
ce680 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c . memset(&l, 0,
ce690 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c sizeof(l));. l
ce6a0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 .l_type = F_RDLC
ce6b0 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 K;. l.l_len = 1
ce6c0 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 ;. l.l_start =
ce6d0 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 0;. l.l_whence
ce6e0 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 = SEEK_SET;. rc
ce6f0 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 = fcntl(fd_orig
ce700 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a , F_SETLK, &l);.
ce710 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 if( rc!=0 ) re
ce720 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 turn;. memset(&
ce730 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 d, 0, sizeof(d))
ce740 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 ;. d.fd = fd;.
ce750 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 d.lock = l;. d
ce760 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 .lock.l_type = F
ce770 5f 57 52 4c 43 4b 3b 0a 20 20 69 66 28 20 70 74 _WRLCK;. if( pt
ce780 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 2c hread_create(&t,
ce790 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 0, threadLockin
ce7a0 67 54 65 73 74 2c 20 26 64 29 3d 3d 30 20 29 7b gTest, &d)==0 ){
ce7b0 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6a 6f 69 . pthread_joi
ce7c0 6e 28 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 63 n(t, 0);. }. c
ce7d0 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20 lose(fd);. if(
ce7e0 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65 d.result!=0 ) re
ce7f0 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f turn;. threadsO
ce800 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 verrideEachOther
ce810 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b sLocks = (d.lock
ce820 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b .l_type==F_UNLCK
ce830 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
ce840 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
ce850 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
ce860 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a nux__) */../*.**
ce870 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c Release a unixL
ce880 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 ockInfo structur
ce890 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c e previously all
ce8a0 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f ocated by findLo
ce8b0 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 ckInfo()..**.**
ce8c0 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 The mutex entere
ce8d0 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 d using the unix
ce8e0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e EnterMutex() fun
ce8f0 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 ction must be he
ce900 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 ld.** when this
ce910 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
ce920 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
ce930 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e id releaseLockIn
ce940 66 6f 28 73 74 72 75 63 74 20 75 6e 69 78 4c 6f fo(struct unixLo
ce950 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a ckInfo *pLock){.
ce960 20 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 assert( unixMu
ce970 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 texHeld() );. i
ce980 66 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 f( pLock ){.
ce990 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 pLock->nRef--;.
ce9a0 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 if( pLock->nR
ce9b0 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 ef==0 ){. i
ce9c0 66 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 f( pLock->pPrev
ce9d0 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
ce9e0 74 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d t( pLock->pPrev-
ce9f0 3e 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b >pNext==pLock );
cea00 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e . pLock->
cea10 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 pPrev->pNext = p
cea20 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
cea30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
cea40 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c 69 assert( lockLi
cea50 73 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 st==pLock );.
cea60 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 lockList =
cea70 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 pLock->pNext;.
cea80 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
cea90 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a pLock->pNext ){.
ceaa0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
ceab0 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 pLock->pNext->pP
ceac0 72 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 rev==pLock );.
cead0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 pLock->pNe
ceae0 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 xt->pPrev = pLoc
ceaf0 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 k->pPrev;.
ceb00 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
ceb10 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 free(pLock);.
ceb20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
ceb30 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4f 70 Release a unixOp
ceb40 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 enCnt structure
ceb50 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 previously alloc
ceb60 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b ated by findLock
ceb70 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 Info()..**.** Th
ceb80 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 e mutex entered
ceb90 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e using the unixEn
ceba0 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 terMutex() funct
cebb0 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 ion must be held
cebc0 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 .** when this fu
cebd0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
cebe0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
cebf0 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 releaseOpenCnt(
cec00 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
cec10 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61 73 nt *pOpen){. as
cec20 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 sert( unixMutexH
cec30 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70 eld() );. if( p
cec40 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65 Open ){. pOpe
cec50 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 n->nRef--;. i
cec60 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d f( pOpen->nRef==
cec70 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
cec80 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20 Open->pPrev ){.
cec90 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
ceca0 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 Open->pPrev->pNe
cecb0 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 xt==pOpen );.
cecc0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65 pOpen->pPre
cecd0 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e v->pNext = pOpen
cece0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
cecf0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 else{. as
ced00 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d sert( openList==
ced10 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 pOpen );.
ced20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 openList = pOpe
ced30 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 n->pNext;.
ced40 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 }. if( pOpe
ced50 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 n->pNext ){.
ced60 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 assert( pOpe
ced70 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d n->pNext->pPrev=
ced80 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 =pOpen );.
ced90 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e pOpen->pNext->
ceda0 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 pPrev = pOpen->p
cedb0 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 23 69 Prev;. }.#i
cedc0 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
cedd0 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f AFE && defined(_
cede0 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 20 20 20 20 _linux__).
cedf0 61 73 73 65 72 74 28 20 21 70 4f 70 65 6e 2d 3e assert( !pOpen->
cee00 70 55 6e 75 73 65 64 20 7c 7c 20 74 68 72 65 61 pUnused || threa
cee10 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
cee20 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 29 3b 0a hersLocks==0 );.
cee30 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a #endif.. /*
cee40 20 49 66 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 If pOpen->pUnus
cee50 65 64 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 ed is not null,
cee60 74 68 65 6e 20 6d 65 6d 6f 72 79 20 61 6e 64 20 then memory and
cee70 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 73 file-descriptors
cee80 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20 6c 65 . ** are le
cee90 61 6b 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 aked.. **.
ceea0 20 20 20 20 20 2a 2a 20 54 68 69 73 20 77 69 6c ** This wil
ceeb0 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 l only happen if
ceec0 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 74 68 72 , under Linuxthr
ceed0 65 61 64 73 2c 20 74 68 65 20 75 73 65 72 20 68 eads, the user h
ceee0 61 73 20 6f 70 65 6e 65 64 0a 20 20 20 20 20 20 as opened.
ceef0 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ** a transaction
cef00 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 2c 20 in one thread,
cef10 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74 6f then attempts to
cef20 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 close the datab
cef30 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e ase. ** han
cef40 64 6c 65 20 66 72 6f 6d 20 61 6e 6f 74 68 65 72 dle from another
cef50 20 74 68 72 65 61 64 20 28 77 69 74 68 6f 75 74 thread (without
cef60 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 69 6e 67 first unlocking
cef70 20 74 68 65 20 64 62 20 66 69 6c 65 29 2e 0a 20 the db file)..
cef80 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 ** This is
cef90 61 20 6d 69 73 75 73 65 2e 20 20 2a 2f 0a 20 20 a misuse. */.
cefa0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
cefb0 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 (pOpen);. }.
cefc0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 }.}../*.** Give
cefd0 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 n a file descrip
cefe0 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 6e 69 78 tor, locate unix
ceff0 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 LockInfo and uni
cf000 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 xOpenCnt structu
cf010 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 63 res that.** desc
cf020 72 69 62 65 73 20 74 68 61 74 20 66 69 6c 65 20 ribes that file
cf030 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 72 65 descriptor. Cre
cf040 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 20 ate new ones if
cf050 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 0a necessary. The.
cf060 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 ** return values
cf070 20 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74 might be uninit
cf080 69 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 72 ialized if an er
cf090 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a ror occurs..**.*
cf0a0 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 * The mutex ente
cf0b0 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e red using the un
cf0c0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 ixEnterMutex() f
cf0d0 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 unction must be
cf0e0 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 held.** when thi
cf0f0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
cf100 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 lled..**.** Retu
cf110 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 rn an appropriat
cf120 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f e error code..*/
cf130 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64 .static int find
cf140 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78 LockInfo(. unix
cf150 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 File *pFile,
cf160 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e /* Un
cf170 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c ix file with fil
cf180 65 20 64 65 73 63 20 75 73 65 64 20 69 6e 20 74 e desc used in t
cf190 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 he key */. stru
cf1a0 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ct unixLockInfo
cf1b0 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 **ppLock, /* Re
cf1c0 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63 turn the unixLoc
cf1d0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 kInfo structure
cf1e0 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 here */. struct
cf1f0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 unixOpenCnt **p
cf200 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 75 pOpen /* Retu
cf210 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 rn the unixOpenC
cf220 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65 72 nt structure her
cf230 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
cf240 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
cf250 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 73 74 /* Syst
cf260 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 63 em call return c
cf270 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b ode */. int fd;
cf280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cf290 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
cf2a0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 ile descriptor f
cf2b0 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74 or pFile */. st
cf2c0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 ruct unixLockKey
cf2d0 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 lockKey; /*
cf2e0 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 Lookup key for t
cf2f0 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 he unixLockInfo
cf300 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 structure */. s
cf310 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 truct unixFileId
cf320 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a fileId; /*
cf330 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 Lookup key for
cf340 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 the unixOpenCnt
cf350 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 75 struct */. stru
cf360 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 3b ct stat statbuf;
cf370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
cf380 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 w-level file inf
cf390 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 ormation */. st
cf3a0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 ruct unixLockInf
cf3b0 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 o *pLock = 0;/*
cf3c0 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4c 6f Candidate unixLo
cf3d0 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f ckInfo object */
cf3e0 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 . struct unixOp
cf3f0 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20 enCnt *pOpen;
cf400 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75 /* Candidate u
cf410 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 nixOpenCnt objec
cf420 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
cf430 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 unixMutexHeld()
cf440 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 );.. /* Get low
cf450 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 -level informati
cf460 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c on about the fil
cf470 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 e that we can us
cf480 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 ed to. ** creat
cf490 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 e a unique name
cf4a0 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 for the file..
cf4b0 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d */. fd = pFile-
cf4c0 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 >h;. rc = fstat
cf4d0 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a (fd, &statbuf);.
cf4e0 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 if( rc!=0 ){.
cf4f0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
cf500 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 rno = errno;.#if
cf510 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 def EOVERFLOW.
cf520 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 if( pFile->las
cf530 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f tErrno==EOVERFLO
cf540 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 W ) return SQLIT
cf550 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a E_NOLFS;.#endif.
cf560 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
cf570 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 E_IOERR;. }..#i
cf580 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 fdef __APPLE__.
cf590 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 /* On OS X on a
cf5a0 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 n msdos filesyst
cf5b0 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 em, the inode nu
cf5c0 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 mber is reported
cf5d0 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c . ** incorrectl
cf5e0 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 y for zero-size
cf5f0 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b files. See tick
cf600 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f et #3260. To wo
cf610 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 rk. ** around t
cf620 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 his problem (we
cf630 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 consider it a bu
cf640 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 g in OS X, not S
cf650 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 QLite). ** we a
cf660 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 lways increase t
cf670 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 he file size to
cf680 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 1 by writing a s
cf690 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 ingle byte. **
cf6a0 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 prior to accessi
cf6b0 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d ng the inode num
cf6c0 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 ber. The one by
cf6d0 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 te written is.
cf6e0 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 ** an ASCII 'S'
cf6f0 63 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20 character which
cf700 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 also happens to
cf710 62 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 be the first byt
cf720 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 e. ** in the he
cf730 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 ader of every SQ
cf740 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 Lite database.
cf750 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 In this way, if
cf760 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 there. ** is a
cf770 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 race condition s
cf780 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 uch that another
cf790 20 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 thread has alre
cf7a0 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 ady populated.
cf7b0 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 67 ** the first pag
cf7c0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
cf7d0 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 e, no damage is
cf7e0 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 done.. */. if(
cf7f0 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 statbuf.st_size
cf800 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
cf810 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 write(fd, "S", 1
cf820 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 );. if( rc!=1
cf830 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
cf840 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 SQLITE_IOERR;.
cf850 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73 }. rc = fs
cf860 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 tat(fd, &statbuf
cf870 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 );. if( rc!=0
cf880 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
cf890 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
cf8a0 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e no;. return
cf8b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 SQLITE_IOERR;.
cf8c0 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
cf8d0 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b . memset(&lockK
cf8e0 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f ey, 0, sizeof(lo
cf8f0 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b ckKey));. lockK
cf900 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61 ey.fid.dev = sta
cf910 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 tbuf.st_dev;.#if
cf920 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f OS_VXWORKS. lo
cf930 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 ckKey.fid.pId =
cf940 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 pFile->pId;.#els
cf950 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e e. lockKey.fid.
cf960 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 ino = statbuf.st
cf970 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 _ino;.#endif.#if
cf980 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
cf990 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f FE && defined(__
cf9a0 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 linux__). if( t
cf9b0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 hreadsOverrideEa
cf9c0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 chOthersLocks<0
cf9d0 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 ){. testThrea
cf9e0 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 dLockingBehavior
cf9f0 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b (fd);. }. lock
cfa00 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64 Key.tid = thread
cfa10 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 sOverrideEachOth
cfa20 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 ersLocks ? 0 : p
cfa30 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 thread_self();.#
cfa40 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d endif. fileId =
cfa50 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 lockKey.fid;.
cfa60 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b if( ppLock!=0 ){
cfa70 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 . pLock = loc
cfa80 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 kList;. while
cfa90 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d ( pLock && memcm
cfaa0 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f p(&lockKey, &pLo
cfab0 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a ck->lockKey, siz
cfac0 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b eof(lockKey)) ){
cfad0 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 . pLock = p
cfae0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
cfaf0 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b }. if( pLock
cfb00 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f ==0 ){. pLo
cfb10 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ck = sqlite3_mal
cfb20 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f loc( sizeof(*pLo
cfb30 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ck) );. if(
cfb40 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 pLock==0 ){.
cfb50 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
cfb60 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
cfb70 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f goto exit_findlo
cfb80 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a ckinfo;. }.
cfb90 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 pLock->loc
cfba0 6b 4b 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a kKey = lockKey;.
cfbb0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 pLock->nRe
cfbc0 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f f = 1;. pLo
cfbd0 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 ck->cnt = 0;.
cfbe0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
cfbf0 70 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c pe = 0;. pL
cfc00 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 ock->pNext = loc
cfc10 6b 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f kList;. pLo
cfc20 63 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 ck->pPrev = 0;.
cfc30 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73 if( lockLis
cfc40 74 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50 t ) lockList->pP
cfc50 72 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 rev = pLock;.
cfc60 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c lockList = pL
cfc70 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ock;. }else{.
cfc80 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65 pLock->nRe
cfc90 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a f++;. }. *
cfca0 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a ppLock = pLock;.
cfcb0 20 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e }. if( ppOpen
cfcc0 21 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e !=0 ){. pOpen
cfcd0 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20 = openList;.
cfce0 20 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26 while( pOpen &&
cfcf0 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c memcmp(&fileId,
cfd00 20 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c &pOpen->fileId,
cfd10 20 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29 sizeof(fileId))
cfd20 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 ){. pOpen
cfd30 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a = pOpen->pNext;.
cfd40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f }. if( pO
cfd50 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pen==0 ){.
cfd60 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f pOpen = sqlite3_
cfd70 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a malloc( sizeof(*
cfd80 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20 pOpen) );.
cfd90 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a if( pOpen==0 ){.
cfda0 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c releaseL
cfdb0 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a ockInfo(pLock);.
cfdc0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
cfdd0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
cfde0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e goto exit_fin
cfdf0 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 dlockinfo;.
cfe00 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 }. memset(
cfe10 70 4f 70 65 6e 2c 20 30 2c 20 73 69 7a 65 6f 66 pOpen, 0, sizeof
cfe20 28 2a 70 4f 70 65 6e 29 29 3b 0a 20 20 20 20 20 (*pOpen));.
cfe30 20 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 20 3d pOpen->fileId =
cfe40 20 66 69 6c 65 49 64 3b 0a 20 20 20 20 20 20 70 fileId;. p
cfe50 4f 70 65 6e 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a Open->nRef = 1;.
cfe60 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 pOpen->pNe
cfe70 78 74 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 xt = openList;.
cfe80 20 20 20 20 20 69 66 28 20 6f 70 65 6e 4c 69 73 if( openLis
cfe90 74 20 29 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 t ) openList->pP
cfea0 72 65 76 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 rev = pOpen;.
cfeb0 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f openList = pO
cfec0 70 65 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a pen;. }else{.
cfed0 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 pOpen->nRe
cfee0 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a f++;. }. *
cfef0 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a ppOpen = pOpen;.
cff00 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f }..exit_findlo
cff10 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e ckinfo:. return
cff20 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 rc;.}../*.** If
cff30 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c we are currentl
cff40 79 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 y in a different
cff50 20 74 68 72 65 61 64 20 74 68 61 6e 20 74 68 65 thread than the
cff60 20 74 68 72 65 61 64 20 74 68 61 74 20 74 68 65 thread that the
cff70 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 67 .** unixFile arg
cff80 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f ument belongs to
cff90 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 , then transfer
cffa0 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65 ownership of the
cffb0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65 unixFile.** ove
cffc0 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 r to the current
cffd0 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 thread..**.** A
cffe0 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c unixFile is onl
cfff0 79 20 6f 77 6e 65 64 20 62 79 20 61 20 74 68 72 y owned by a thr
d0000 65 61 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 74 ead on systems t
d0010 68 61 74 20 75 73 65 20 4c 69 6e 75 78 54 68 72 hat use LinuxThr
d0020 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 eads..**.** Owne
d0030 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69 rship transfer i
d0040 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 s only allowed i
d0050 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 f the unixFile i
d0060 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f s currently unlo
d0070 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 cked..** If the
d0080 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b unixFile is lock
d0090 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 ed and an owners
d00a0 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 hip is wrong, th
d00b0 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c en return.** SQL
d00c0 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c ITE_MISUSE. SQL
d00d0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
d00e0 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 ed if everything
d00f0 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 works..*/.#if S
d0100 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
d0110 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 && defined(__li
d0120 6e 75 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e nux__).static in
d0130 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 t transferOwners
d0140 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 hip(unixFile *pF
d0150 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ile){. int rc;.
d0160 20 20 70 74 68 72 65 61 64 5f 74 20 68 53 65 6c pthread_t hSel
d0170 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61 64 73 f;. if( threads
d0180 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
d0190 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f rsLocks ){. /
d01a0 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e * Ownership tran
d01b0 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 64 sfers not needed
d01c0 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65 6d 20 on this system
d01d0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 */. return SQ
d01e0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 LITE_OK;. }. h
d01f0 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 Self = pthread_s
d0200 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70 74 68 elf();. if( pth
d0210 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69 6c 65 read_equal(pFile
d0220 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20 29 7b ->tid, hSelf) ){
d0230 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 73 . /* We are s
d0240 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d 65 till in the same
d0250 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f thread */. O
d0260 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e STRACE1("No-tran
d0270 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72 65 61 sfer, same threa
d0280 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72 d\n");. retur
d0290 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d02a0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d02b0 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 cktype!=NO_LOCK
d02c0 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 6e ){. /* We can
d02d0 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 72 not change owner
d02e0 73 68 69 70 20 77 68 69 6c 65 20 77 65 20 61 72 ship while we ar
d02f0 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b e holding a lock
d0300 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 ! */. return
d0310 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
d0320 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 }. OSTRACE4("T
d0330 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69 ransfer ownershi
d0340 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 20 p of %d from %d
d0350 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 to %d\n",.
d0360 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 pFile->h,
d0370 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c pFile->tid, hSel
d0380 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64 f);. pFile->tid
d0390 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28 = hSelf;. if (
d03a0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20 pFile->pLock !=
d03b0 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65 NULL) {. rele
d03c0 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c aseLockInfo(pFil
d03d0 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 e->pLock);. r
d03e0 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f c = findLockInfo
d03f0 28 70 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e (pFile, &pFile->
d0400 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f pLock, 0);. O
d0410 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 STRACE5("LOCK
d0420 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73 %d is now %s(%s
d0430 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e ,%d)\n", pFile->
d0440 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f h,. lo
d0450 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 cktypeName(pFile
d0460 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 ->locktype),.
d0470 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 locktype
d0480 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 Name(pFile->pLoc
d0490 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46 k->locktype), pF
d04a0 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29 ile->pLock->cnt)
d04b0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
d04c0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 . } else {.
d04d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d04e0 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f ;. }.}.#else /
d04f0 2a 20 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f * if not SQLITE_
d0500 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 20 THREADSAFE */.
d0510 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72 /* On single-thr
d0520 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77 eaded builds, ow
d0530 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 nership transfer
d0540 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 is a no-op */.#
d0550 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72 define transfer
d0560 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c Ownership(X) SQL
d0570 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a ITE_OK.#endif /*
d0580 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
d0590 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 FE */.../*.** Th
d05a0 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
d05b0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
d05c0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
d05d0 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
d05e0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
d05f0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
d0600 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
d0610 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
d0620 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a , set *pResOut.*
d0630 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 * to a non-zero
d0640 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 value otherwise
d0650 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 *pResOut is set
d0660 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 to zero. The re
d0670 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 turn value.** is
d0680 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f set to SQLITE_O
d0690 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 K unless an I/O
d06a0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
d06b0 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e ing lock checkin
d06c0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
d06d0 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 unixCheckReserv
d06e0 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
d06f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
d0700 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 esOut){. int rc
d0710 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
d0720 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 int reserved = 0
d0730 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
d0740 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d0750 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 )id;.. Simulate
d0760 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
d0770 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 SQLITE_IOERR_CHE
d0780 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 CKRESERVEDLOCK;
d0790 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 );.. assert( pF
d07a0 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74 ile );. unixEnt
d07b0 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65 erMutex(); /* Be
d07c0 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f cause pFile->pLo
d07d0 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72 ck is shared acr
d07e0 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a oss threads */..
d07f0 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20 /* Check if a
d0800 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70 thread in this p
d0810 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63 rocess holds suc
d0820 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 h a lock */. if
d0830 28 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e ( pFile->pLock->
d0840 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
d0850 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 LOCK ){. rese
d0860 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 rved = 1;. }..
d0870 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 /* Otherwise se
d0880 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 e if some other
d0890 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 process holds it
d08a0 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f .. */.#ifndef _
d08b0 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 21 _DJGPP__. if( !
d08c0 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 reserved ){.
d08d0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 struct flock loc
d08e0 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 k;. lock.l_wh
d08f0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b ence = SEEK_SET;
d0900 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 . lock.l_star
d0910 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
d0920 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 E;. lock.l_le
d0930 6e 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e n = 1;. lock.
d0940 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b l_type = F_WRLCK
d0950 3b 0a 20 20 20 20 69 66 20 28 2d 31 20 3d 3d 20 ;. if (-1 ==
d0960 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 fcntl(pFile->h,
d0970 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 F_GETLK, &lock))
d0980 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 {. int tEr
d0990 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d09a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 rc = sqliteEr
d09b0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
d09c0 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
d09d0 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
d09e0 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 RVEDLOCK);.
d09f0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d0a00 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d0a10 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e } else if( lock.
d0a20 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20 l_type!=F_UNLCK
d0a30 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 ){. reserve
d0a40 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d d = 1;. }. }
d0a50 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69 .#endif. . uni
d0a60 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
d0a70 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20 OSTRACE4("TEST
d0a80 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 WR-LOCK %d %d %d
d0a90 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
d0aa0 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 c, reserved);..
d0ab0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 *pResOut = rese
d0ac0 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 rved;. return r
d0ad0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 c;.}../*.** Perf
d0ae0 6f 72 6d 20 61 20 66 69 6c 65 20 6c 6f 63 6b 69 orm a file locki
d0af0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 ng operation on
d0b00 61 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 a range of bytes
d0b10 20 69 6e 20 61 20 66 69 6c 65 2e 0a 2a 2a 20 54 in a file..** T
d0b20 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74 65 he "op" paramete
d0b30 72 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 r should be one
d0b40 6f 66 20 46 5f 52 44 4c 43 4b 2c 20 46 5f 57 52 of F_RDLCK, F_WR
d0b50 4c 43 4b 2c 20 6f 72 20 46 5f 55 4e 4c 43 4b 2e LCK, or F_UNLCK.
d0b60 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 .** Return 0 on
d0b70 73 75 63 63 65 73 73 20 6f 72 20 2d 31 20 66 6f success or -1 fo
d0b80 72 20 66 61 69 6c 75 72 65 2e 20 20 4f 6e 20 66 r failure. On f
d0b90 61 69 6c 75 72 65 2c 20 77 72 69 74 65 20 74 68 ailure, write th
d0ba0 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 e error.** code
d0bb0 69 6e 74 6f 20 2a 70 45 72 72 63 6f 64 65 2e 0a into *pErrcode..
d0bc0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
d0bd0 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c ITE_WHOLE_FILE_L
d0be0 4f 43 4b 49 4e 47 20 62 69 74 20 69 73 20 63 6c OCKING bit is cl
d0bf0 65 61 72 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 6c ear, then only l
d0c00 6f 63 6b 0a 2a 2a 20 74 68 65 20 72 61 6e 67 65 ock.** the range
d0c10 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 74 68 65 of bytes on the
d0c20 20 6c 6f 63 6b 69 6e 67 20 70 61 67 65 20 62 65 locking page be
d0c30 74 77 65 65 6e 20 53 48 41 52 45 44 5f 46 49 52 tween SHARED_FIR
d0c40 53 54 20 61 6e 64 0a 2a 2a 20 53 48 41 52 45 44 ST and.** SHARED
d0c50 5f 53 49 5a 45 2e 20 20 49 66 20 53 51 4c 49 54 _SIZE. If SQLIT
d0c60 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
d0c70 4b 49 4e 47 20 69 73 20 73 65 74 2c 20 74 68 65 KING is set, the
d0c80 6e 20 6c 6f 63 6b 20 61 6c 6c 0a 2a 2a 20 62 79 n lock all.** by
d0c90 74 65 73 20 66 72 6f 6d 20 30 20 75 70 20 74 6f tes from 0 up to
d0ca0 20 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 but not includi
d0cb0 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c ng PENDING_BYTE,
d0cc0 20 61 6e 64 20 61 6c 6c 20 62 79 74 65 73 0a 2a and all bytes.*
d0cd0 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 53 48 * that follow SH
d0ce0 41 52 45 44 5f 46 49 52 53 54 2e 0a 2a 2a 0a 2a ARED_FIRST..**.*
d0cf0 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
d0d00 2c 20 6f 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c , of SQLITE_WHOL
d0d10 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 E_FILE_LOCKING i
d0d20 66 20 66 61 6c 73 65 20 28 74 68 65 20 68 69 73 f false (the his
d0d30 74 6f 72 69 63 61 6c 0a 2a 2a 20 64 65 66 61 75 torical.** defau
d0d40 6c 74 20 63 61 73 65 29 20 74 68 65 6e 20 6f 6e lt case) then on
d0d50 6c 79 20 6c 6f 63 6b 20 61 20 73 6d 61 6c 6c 20 ly lock a small
d0d60 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 66 range of bytes f
d0d70 72 6f 6d 20 53 48 41 52 45 44 5f 46 49 52 53 54 rom SHARED_FIRST
d0d80 0a 2a 2a 20 74 68 72 6f 75 67 68 20 53 48 41 52 .** through SHAR
d0d90 45 44 5f 46 49 52 53 54 2b 53 48 41 52 45 44 5f ED_FIRST+SHARED_
d0da0 53 49 5a 45 2d 31 2e 20 20 42 75 74 20 69 66 20 SIZE-1. But if
d0db0 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c SQLITE_WHOLE_FIL
d0dc0 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 0a 2a 2a 20 E_LOCKING is.**
d0dd0 74 72 75 65 20 74 68 65 6e 20 6c 6f 63 6b 20 65 true then lock e
d0de0 76 65 72 79 20 62 79 74 65 20 69 6e 20 74 68 65 very byte in the
d0df0 20 66 69 6c 65 20 65 78 63 65 70 74 20 66 6f 72 file except for
d0e00 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 61 6e PENDING_BYTE an
d0e10 64 0a 2a 2a 20 52 45 53 45 52 56 45 44 5f 42 59 d.** RESERVED_BY
d0e20 54 45 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 TE..**.** SQLITE
d0e30 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b _WHOLE_FILE_LOCK
d0e40 49 4e 47 3d 74 72 75 65 20 6f 76 65 72 6c 61 70 ING=true overlap
d0e50 73 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 s SQLITE_WHOLE_F
d0e60 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 66 61 6c 73 ILE_LOCKING=fals
d0e70 65 0a 2a 2a 20 61 6e 64 20 73 6f 20 74 68 65 20 e.** and so the
d0e80 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 locking schemes
d0e90 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 20 are compatible.
d0ea0 20 4f 6e 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 One type of loc
d0eb0 6b 20 77 69 6c 6c 0a 2a 2a 20 65 66 66 65 63 74 k will.** effect
d0ec0 69 76 65 6c 79 20 65 78 63 6c 75 64 65 20 74 68 ively exclude th
d0ed0 65 20 6f 74 68 65 72 20 74 79 70 65 2e 20 20 54 e other type. T
d0ee0 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 he reason for us
d0ef0 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 ing the.** SQLIT
d0f00 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
d0f10 4b 49 4e 47 3d 74 72 75 65 20 69 73 20 74 68 61 KING=true is tha
d0f20 74 20 62 79 20 69 6e 64 69 63 61 74 69 6e 67 20 t by indicating
d0f30 74 68 65 20 66 75 6c 6c 20 72 61 6e 67 65 0a 2a the full range.*
d0f40 2a 20 6f 66 20 62 79 74 65 73 20 74 6f 20 62 65 * of bytes to be
d0f50 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e read or written
d0f60 2c 20 77 65 20 67 69 76 65 20 68 69 6e 74 73 20 , we give hints
d0f70 74 6f 20 4e 46 53 20 74 6f 20 68 65 6c 70 20 69 to NFS to help i
d0f80 74 0a 2a 2a 20 6d 61 69 6e 74 61 69 6e 20 63 61 t.** maintain ca
d0f90 63 68 65 20 63 6f 68 65 72 65 6e 63 79 2e 20 20 che coherency.
d0fa0 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
d0fb0 64 2c 20 77 68 6f 6c 65 20 66 69 6c 65 20 6c 6f d, whole file lo
d0fc0 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 73 6c 6f 77 cking.** is slow
d0fd0 65 72 2c 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 er, so we don't
d0fe0 77 61 6e 74 20 74 6f 20 75 73 65 20 69 74 20 65 want to use it e
d0ff0 78 63 65 70 74 20 66 6f 72 20 4e 46 53 2e 0a 2a xcept for NFS..*
d1000 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 61 6e /.static int ran
d1010 67 65 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65 20 geLock(unixFile
d1020 2a 70 46 69 6c 65 2c 20 69 6e 74 20 6f 70 2c 20 *pFile, int op,
d1030 69 6e 74 20 2a 70 45 72 72 63 6f 64 65 29 7b 0a int *pErrcode){.
d1040 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
d1050 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 ock;. int rc;.
d1060 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 6f lock.l_type = o
d1070 70 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 p;. lock.l_star
d1080 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 t = SHARED_FIRST
d1090 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 ;. lock.l_whenc
d10a0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
d10b0 69 66 28 20 28 70 46 69 6c 65 2d 3e 66 69 6c 65 if( (pFile->file
d10c0 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57 Flags & SQLITE_W
d10d0 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e HOLE_FILE_LOCKIN
d10e0 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 6c 6f 63 G)==0 ){. loc
d10f0 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44 k.l_len = SHARED
d1100 5f 53 49 5a 45 3b 0a 20 20 20 20 72 63 20 3d 20 _SIZE;. rc =
d1110 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 fcntl(pFile->h,
d1120 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b F_SETLK, &lock);
d1130 0a 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d . *pErrcode =
d1140 20 65 72 72 6e 6f 3b 0a 20 20 7d 65 6c 73 65 7b errno;. }else{
d1150 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 . lock.l_len
d1160 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 66 63 = 0;. rc = fc
d1170 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f ntl(pFile->h, F_
d1180 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 SETLK, &lock);.
d1190 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 *pErrcode = e
d11a0 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 4e 45 rrno;. if( NE
d11b0 56 45 52 28 6f 70 3d 3d 46 5f 55 4e 4c 43 4b 29 VER(op==F_UNLCK)
d11c0 20 7c 7c 20 72 63 21 3d 28 2d 31 29 20 29 7b 0a || rc!=(-1) ){.
d11d0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 lock.l_sta
d11e0 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 6c 6f rt = 0;. lo
d11f0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 50 45 4e 44 49 ck.l_len = PENDI
d1200 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 72 NG_BYTE;. r
d1210 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d c = fcntl(pFile-
d1220 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f >h, F_SETLK, &lo
d1230 63 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 41 ck);. if( A
d1240 4c 57 41 59 53 28 6f 70 21 3d 46 5f 55 4e 4c 43 LWAYS(op!=F_UNLC
d1250 4b 29 20 26 26 20 72 63 3d 3d 28 2d 31 29 20 29 K) && rc==(-1) )
d1260 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 72 72 63 {. *pErrc
d1270 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 ode = errno;.
d1280 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 lock.l_type
d1290 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 = F_UNLCK;.
d12a0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
d12b0 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b = SHARED_FIRST;
d12c0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
d12d0 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 len = 0;.
d12e0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
d12f0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d1300 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
d1310 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
d1320 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
d1330 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
d1340 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
d1350 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
d1360 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
d1370 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
d1380 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
d1390 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
d13a0 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
d13b0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
d13c0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
d13d0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
d13e0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
d13f0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
d1400 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
d1410 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
d1420 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
d1430 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
d1440 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
d1450 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
d1460 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
d1470 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
d1480 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
d1490 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
d14a0 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
d14b0 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
d14c0 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
d14d0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
d14e0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
d14f0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
d1500 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
d1510 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
d1520 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
d1530 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
d1540 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
d1550 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
d1560 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
d1570 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
d1580 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
d1590 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
d15a0 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d15b0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d15c0 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
d15d0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
d15e0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
d15f0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
d1600 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c ck. Use the sql
d1610 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a ite3OsUnlock().*
d1620 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 * routine to low
d1630 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 er a locking lev
d1640 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e el..*/.static in
d1650 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74 t unixLock(sqlit
d1660 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d1670 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a locktype){. /*
d1680 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 The following d
d1690 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70 escribes the imp
d16a0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
d16b0 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 he various locks
d16c0 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74 and. ** lock t
d16d0 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65 ransitions in te
d16e0 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58 rms of the POSIX
d16f0 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64 advisory shared
d1700 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20 and exclusive.
d1710 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69 ** lock primiti
d1720 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64 ves (called read
d1730 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65 -locks and write
d1740 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f -locks below, to
d1750 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66 avoid. ** conf
d1760 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74 usion with SQLit
d1770 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54 e lock names). T
d1780 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 he algorithms ar
d1790 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20 e complicated.
d17a0 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f ** slightly in o
d17b0 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61 rder to be compa
d17c0 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f tible with windo
d17d0 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c ws systems simul
d17e0 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 taneously. ** a
d17f0 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d ccessing the sam
d1800 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
d1810 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73 in case that is
d1820 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a ever required..
d1830 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c **. ** Symbol
d1840 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e s defined in os.
d1850 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20 h indentify the
d1860 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61 'pending byte' a
d1870 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64 nd the 'reserved
d1880 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63 . ** byte', eac
d1890 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61 h single bytes a
d18a0 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 t well known off
d18b0 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73 sets, and the 's
d18c0 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 hared byte. **
d18d0 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20 range', a range
d18e0 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20 of 510 bytes at
d18f0 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66 a well known off
d1900 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 set.. **. ** T
d1910 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45 o obtain a SHARE
d1920 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c D lock, a read-l
d1930 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ock is obtained
d1940 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a on the 'pending.
d1950 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20 ** byte'. If
d1960 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66 this is successf
d1970 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 ul, a random byt
d1980 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72 e from the 'shar
d1990 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e ed byte. ** ran
d19a0 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b ge' is read-lock
d19b0 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 ed and the lock
d19c0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 on the 'pending
d19d0 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a byte' released..
d19e0 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 **. ** A proc
d19f0 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 ess may only obt
d1a00 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c ain a RESERVED l
d1a10 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 ock after it has
d1a20 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a a SHARED lock..
d1a30 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20 ** A RESERVED
d1a40 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e lock is implemen
d1a50 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20 ted by grabbing
d1a60 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
d1a70 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76 the. ** 'reserv
d1a80 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a ed byte'. . **.
d1a90 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d ** A process m
d1aa0 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 ay only obtain a
d1ab0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66 PENDING lock af
d1ac0 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69 ter it has obtai
d1ad0 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45 ned a. ** SHARE
d1ae0 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e D lock. A PENDIN
d1af0 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d G lock is implem
d1b00 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69 ented by obtaini
d1b10 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a ng a write-lock.
d1b20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e ** on the 'pen
d1b30 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73 ding byte'. This
d1b40 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f ensures that no
d1b50 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b new SHARED lock
d1b60 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62 s can be. ** ob
d1b70 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73 tained, but exis
d1b80 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b ting SHARED lock
d1b90 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f s are allowed to
d1ba0 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63 persist. A proc
d1bb0 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f ess. ** does no
d1bc0 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e t have to obtain
d1bd0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
d1be0 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61 on the way to a
d1bf0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20 PENDING lock..
d1c00 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74 ** This propert
d1c10 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 y is used by the
d1c20 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72 algorithm for r
d1c30 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f olling back a jo
d1c40 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 urnal file. **
d1c50 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20 after a crash..
d1c60 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c **. ** An EXCL
d1c70 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61 USIVE lock, obta
d1c80 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e ined after a PEN
d1c90 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c DING lock is hel
d1ca0 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65 d, is. ** imple
d1cb0 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e mented by obtain
d1cc0 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
d1cd0 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27 on the entire '
d1ce0 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a shared byte. **
d1cf0 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61 range'. Since a
d1d00 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72 ll other locks r
d1d10 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f equire a read-lo
d1d20 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 ck on one of the
d1d30 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68 bytes. ** with
d1d40 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74 in this range, t
d1d50 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 his ensures that
d1d60 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 no other locks
d1d70 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a are held on the.
d1d80 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a ** database. .
d1d90 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65 **. ** The re
d1da0 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79 ason a single by
d1db0 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 te cannot be use
d1dc0 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 d instead of the
d1dd0 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 'shared byte.
d1de0 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61 ** range' is tha
d1df0 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 t some versions
d1e00 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f of windows do no
d1e10 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c t support read-l
d1e20 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f ocks. By. ** lo
d1e30 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62 cking a random b
d1e40 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65 yte from a range
d1e50 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41 , concurrent SHA
d1e60 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78 RED locks may ex
d1e70 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 ist. ** even if
d1e80 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69 the locking pri
d1e90 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61 mitive used is a
d1ea0 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f lways a write-lo
d1eb0 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72 ck.. */. int r
d1ec0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
d1ed0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d1ee0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d1ef0 3b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c ;. struct unixL
d1f00 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d ockInfo *pLock =
d1f10 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 pFile->pLock;.
d1f20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f struct flock lo
d1f30 63 6b 3b 0a 20 20 69 6e 74 20 73 20 3d 20 30 3b ck;. int s = 0;
d1f40 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 0a 0a . int tErrno;..
d1f50 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
d1f60 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 4c );. OSTRACE7("L
d1f70 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 OCK %d %s was
d1f80 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25 %s(%s,%d) pid=%
d1f90 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a d\n", pFile->h,.
d1fa0 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 locktypeNa
d1fb0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f me(locktype), lo
d1fc0 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 cktypeName(pFile
d1fd0 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 ->locktype),.
d1fe0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 locktypeName(
d1ff0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 pLock->locktype)
d2000 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 , pLock->cnt , g
d2010 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 etpid());.. /*
d2020 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 If there is alre
d2030 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 ady a lock of th
d2040 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 is type or more
d2050 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 restrictive on t
d2060 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 he. ** unixFile
d2070 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f , do nothing. Do
d2080 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f n't use the end_
d2090 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c lock: exit path,
d20a0 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 as. ** unixEnt
d20b0 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 erMutex() hasn't
d20c0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 been called yet
d20d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 .. */. if( pFi
d20e0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f le->locktype>=lo
d20f0 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 cktype ){. OS
d2100 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 TRACE3("LOCK
d2110 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 %d %s ok (alread
d2120 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c y held)\n", pFil
d2130 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 e->h,.
d2140 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c locktypeName(l
d2150 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 ocktype));. r
d2160 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d2170 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
d2180 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 sure the locking
d2190 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 sequence is cor
d21a0 72 65 63 74 2e 0a 20 20 2a 2a 20 20 28 31 29 20 rect.. ** (1)
d21b0 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72 We never move fr
d21c0 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61 om unlocked to a
d21d0 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20 74 nything higher t
d21e0 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e han shared lock.
d21f0 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74 . ** (2) SQLit
d2200 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74 e never explicit
d2210 6c 79 20 72 65 71 75 65 73 74 73 20 61 20 70 65 ly requests a pe
d2220 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 ndig lock.. **
d2230 20 28 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f (3) A shared lo
d2240 63 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c ck is always hel
d2250 64 20 77 68 65 6e 20 61 20 72 65 73 65 72 76 65 d when a reserve
d2260 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 lock is request
d2270 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ed.. */. asser
d2280 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
d2290 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c pe!=NO_LOCK || l
d22a0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
d22b0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
d22c0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 ( locktype!=PEND
d22d0 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 ING_LOCK );. as
d22e0 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d sert( locktype!=
d22f0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c RESERVED_LOCK ||
d2300 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d2310 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
d2320 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 .. /* This mute
d2330 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 x is needed beca
d2340 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b use pFile->pLock
d2350 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 is shared acros
d2360 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 s threads. */.
d2370 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
d2380 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 );.. /* Make su
d2390 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 re the current t
d23a0 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 hread owns the p
d23b0 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 File.. */. rc
d23c0 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 = transferOwners
d23d0 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 hip(pFile);. if
d23e0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
d23f0 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 ){. unixLeave
d2400 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 Mutex();. ret
d2410 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c urn rc;. }. pL
d2420 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f ock = pFile->pLo
d2430 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d ck;.. /* If som
d2440 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 e thread using t
d2450 68 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f his PID has a lo
d2460 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 ck via a differe
d2470 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a nt unixFile*. *
d2480 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 * handle that pr
d2490 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 ecludes the requ
d24a0 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 ested lock, retu
d24b0 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 rn BUSY.. */.
d24c0 69 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( (pFile->lock
d24d0 74 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 type!=pLock->loc
d24e0 6b 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 ktype && .
d24f0 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b (pLock->lock
d2500 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f type>=PENDING_LO
d2510 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 CK || locktype>S
d2520 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 HARED_LOCK)). )
d2530 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
d2540 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f E_BUSY;. goto
d2550 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a end_lock;. }..
d2560 20 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 /* If a SHARED
d2570 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 lock is request
d2580 65 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 ed, and some thr
d2590 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 ead using this P
d25a0 49 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 ID already. **
d25b0 68 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 has a SHARED or
d25c0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 RESERVED lock, t
d25d0 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 hen increment re
d25e0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 ference counts a
d25f0 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 nd. ** return S
d2600 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 QLITE_OK.. */.
d2610 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
d2620 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 HARED_LOCK && .
d2630 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 (pLock->loc
d2640 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
d2650 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 CK || pLock->loc
d2660 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f ktype==RESERVED_
d2670 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 LOCK) ){. ass
d2680 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 ert( locktype==S
d2690 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
d26a0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
d26b0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a >locktype==0 );.
d26c0 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
d26d0 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 k->cnt>0 );.
d26e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d26f0 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 = SHARED_LOCK;.
d2700 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b pLock->cnt++;
d2710 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 . pFile->pOpe
d2720 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 n->nLock++;.
d2730 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 goto end_lock;.
d2740 20 7d 0a 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 }... /* A PEND
d2750 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 ING lock is need
d2760 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 ed before acquir
d2770 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ing a SHARED loc
d2780 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a k and before. *
d2790 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 * acquiring an E
d27a0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 XCLUSIVE lock.
d27b0 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c For the SHARED l
d27c0 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 ock, the PENDING
d27d0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 will. ** be re
d27e0 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 6c leased.. */. l
d27f0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a ock.l_len = 1L;.
d2800 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 lock.l_whence
d2810 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 69 66 = SEEK_SET;. if
d2820 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
d2830 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c ED_LOCK . |
d2840 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 | (locktype==EXC
d2850 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
d2860 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 File->locktype<P
d2870 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 ENDING_LOCK). )
d2880 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 {. lock.l_typ
d2890 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 e = (locktype==S
d28a0 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c HARED_LOCK?F_RDL
d28b0 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20 CK:F_WRLCK);.
d28c0 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 lock.l_start =
d28d0 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 PENDING_BYTE;.
d28e0 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c s = fcntl(pFil
d28f0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 e->h, F_SETLK, &
d2900 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73 lock);. if( s
d2910 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 ==(-1) ){.
d2920 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d2930 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
d2940 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
d2950 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
d2960 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b ITE_IOERR_LOCK);
d2970 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f . if( IS_LO
d2980 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
d2990 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
d29a0 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d29b0 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 o;. }.
d29c0 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a goto end_lock;.
d29d0 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a }. }... /*
d29e0 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 If control gets
d29f0 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 to this point,
d2a00 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f then actually go
d2a10 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a ahead and make.
d2a20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 ** operating s
d2a30 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 ystem calls for
d2a40 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f the specified lo
d2a50 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c ck.. */. if( l
d2a60 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
d2a70 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 LOCK ){. asse
d2a80 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d rt( pLock->cnt==
d2a90 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
d2aa0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 pLock->locktype
d2ab0 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e ==0 );.. /* N
d2ac0 6f 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d ow get the read-
d2ad0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 73 20 3d 20 lock */. s =
d2ae0 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c rangeLock(pFile,
d2af0 20 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e F_RDLCK, &tErrn
d2b00 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 o);.. /* Drop
d2b10 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 the temporary P
d2b20 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 ENDING lock */.
d2b30 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
d2b40 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
d2b50 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
d2b60 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 1L;. lock.l_
d2b70 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a type = F_UNLCK;.
d2b80 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46 if( fcntl(pF
d2b90 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c ile->h, F_SETLK,
d2ba0 20 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 &lock)!=0 ){.
d2bb0 20 20 20 20 69 66 28 20 73 20 21 3d 20 2d 31 20 if( s != -1
d2bc0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
d2bd0 69 73 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 is could happen
d2be0 77 69 74 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d with a network m
d2bf0 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 ount */.
d2c00 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 tErrno = errno;
d2c10 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
d2c20 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d2c30 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d2c40 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
d2c50 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 OCK); . i
d2c60 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d2c70 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (rc) ){.
d2c80 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d2c90 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d2ca0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 }. g
d2cb0 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 oto end_lock;.
d2cc0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
d2cd0 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 if( s==(-1) ){.
d2ce0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d2cf0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d2d00 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d2d10 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a TE_IOERR_LOCK);.
d2d20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 if( IS_LOC
d2d30 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
d2d40 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d2d50 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d2d60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
d2d70 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 lse{. pFile
d2d80 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 ->locktype = SHA
d2d90 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 RED_LOCK;.
d2da0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c pFile->pOpen->nL
d2db0 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f ock++;. pLo
d2dc0 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 ck->cnt = 1;.
d2dd0 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c }. }else if( l
d2de0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
d2df0 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b VE_LOCK && pLock
d2e00 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f ->cnt>1 ){. /
d2e10 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e 67 20 * We are trying
d2e20 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 for an exclusive
d2e30 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 lock but anothe
d2e40 72 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 r thread in this
d2e50 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f . ** same pro
d2e60 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68 6f cess is still ho
d2e70 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c lding a shared l
d2e80 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d ock. */. rc =
d2e90 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
d2ea0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 }else{. /* Th
d2eb0 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f e request was fo
d2ec0 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 r a RESERVED or
d2ed0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
d2ee0 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 It is. ** as
d2ef0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 sumed that there
d2f00 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 is a SHARED or
d2f10 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 greater lock on
d2f20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 the file. **
d2f30 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a already.. */.
d2f40 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70 assert( 0!=p
d2f50 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
d2f60 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 ;. lock.l_typ
d2f70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 e = F_WRLCK;.
d2f80 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 switch( locktyp
d2f90 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 e ){. case
d2fa0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 RESERVED_LOCK:.
d2fb0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 lock.l_st
d2fc0 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 art = RESERVED_B
d2fd0 59 54 45 3b 0a 20 20 20 20 20 20 20 20 73 20 3d YTE;. s =
d2fe0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
d2ff0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d3000 3b 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f ;. tErrno
d3010 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d3020 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
d3030 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f ase EXCLUSIVE_LO
d3040 43 4b 3a 0a 20 20 20 20 20 20 20 20 73 20 3d 20 CK:. s =
d3050 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c rangeLock(pFile,
d3060 20 46 5f 57 52 4c 43 4b 2c 20 26 74 45 72 72 6e F_WRLCK, &tErrn
d3070 6f 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 o);. brea
d3080 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 k;. default
d3090 3a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 :. assert
d30a0 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 (0);. }. i
d30b0 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 f( s==(-1) ){.
d30c0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d30d0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d30e0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d30f0 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 E_IOERR_LOCK);.
d3100 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d3110 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d3120 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d3130 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d3140 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d3150 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 20 4e }. ..#ifndef N
d3160 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20 75 DEBUG. /* Set u
d3170 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f p the transactio
d3180 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 65 n-counter change
d3190 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73 20 checking flags
d31a0 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 69 when. ** transi
d31b0 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 53 tioning from a S
d31c0 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 45 52 HARED to a RESER
d31d0 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 63 VED lock. The c
d31e0 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 hange. ** from
d31f0 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 56 SHARED to RESERV
d3200 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 67 ED marks the beg
d3210 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 6d inning of a norm
d3220 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f 70 al. ** write op
d3230 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 68 eration (not a h
d3240 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot journal rollb
d3250 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ack).. */. if(
d3260 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 rc==SQLITE_OK.
d3270 20 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b && pFile->lock
d3280 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 type<=SHARED_LOC
d3290 4b 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 70 65 K. && locktype
d32a0 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a ==RESERVED_LOCK.
d32b0 20 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
d32c0 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20 transCntrChng =
d32d0 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62 0;. pFile->db
d32e0 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 20 Update = 0;.
d32f0 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 pFile->inNormalW
d3300 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65 rite = 1;. }.#e
d3310 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 63 3d ndif... if( rc=
d3320 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
d3330 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d3340 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
d3350 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 pLock->locktyp
d3360 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
d3370 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 }else if( lockty
d3380 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
d3390 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d CK ){. pFile-
d33a0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 >locktype = PEND
d33b0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c ING_LOCK;. pL
d33c0 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ock->locktype =
d33d0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 PENDING_LOCK;.
d33e0 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 }..end_lock:. u
d33f0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
d3400 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 . OSTRACE4("LOC
d3410 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 K %d %s %s\n"
d3420 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
d3430 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 typeName(locktyp
d3440 65 29 2c 20 0a 20 20 20 20 20 20 72 63 3d 3d 53 e), . rc==S
d3450 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 QLITE_OK ? "ok"
d3460 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 : "failed");. r
d3470 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d3480 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 66 69 6c ** Close all fil
d3490 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 63 e descriptors ac
d34a0 63 75 6d 75 61 74 65 64 20 69 6e 20 74 68 65 20 cumuated in the
d34b0 75 6e 69 78 4f 70 65 6e 43 6e 74 2d 3e 70 55 6e unixOpenCnt->pUn
d34c0 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 49 66 used list..** If
d34d0 20 61 6c 6c 20 73 75 63 68 20 66 69 6c 65 20 64 all such file d
d34e0 65 73 63 72 69 70 74 6f 72 73 20 61 72 65 20 63 escriptors are c
d34f0 6c 6f 73 65 64 20 77 69 74 68 6f 75 74 20 65 72 losed without er
d3500 72 6f 72 2c 20 74 68 65 20 6c 69 73 74 20 69 73 ror, the list is
d3510 0a 2a 2a 20 63 6c 65 61 72 65 64 20 61 6e 64 20 .** cleared and
d3520 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
d3530 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 ed..**.** Otherw
d3540 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 ise, if an error
d3550 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 73 75 occurs, then su
d3560 63 63 65 73 73 66 75 6c 6c 79 20 63 6c 6f 73 65 ccessfully close
d3570 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
d3580 72 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 72 65 r.** entries are
d3590 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 removed from th
d35a0 65 20 6c 69 73 74 2c 20 61 6e 64 20 53 51 4c 49 e list, and SQLI
d35b0 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 72 TE_IOERR_CLOSE r
d35c0 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 6e 6f 74 eturned. .** not
d35d0 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c deleted and SQL
d35e0 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 ITE_IOERR_CLOSE
d35f0 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74 returned..*/ .st
d3600 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 50 65 atic int closePe
d3610 6e 64 69 6e 67 46 64 73 28 75 6e 69 78 46 69 6c ndingFds(unixFil
d3620 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 e *pFile){. int
d3630 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d3640 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 . struct unixOp
d3650 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 enCnt *pOpen = p
d3660 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55 File->pOpen;. U
d3670 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 45 72 nixUnusedFd *pEr
d3680 72 6f 72 20 3d 20 30 3b 0a 20 20 55 6e 69 78 55 ror = 0;. UnixU
d3690 6e 75 73 65 64 46 64 20 2a 70 3b 0a 20 20 55 6e nusedFd *p;. Un
d36a0 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 78 ixUnusedFd *pNex
d36b0 74 3b 0a 20 20 66 6f 72 28 70 3d 70 4f 70 65 6e t;. for(p=pOpen
d36c0 2d 3e 70 55 6e 75 73 65 64 3b 20 70 3b 20 70 3d ->pUnused; p; p=
d36d0 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 pNext){. pNex
d36e0 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 t = p->pNext;.
d36f0 20 20 69 66 28 20 63 6c 6f 73 65 28 70 2d 3e 66 if( close(p->f
d3700 64 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c d) ){. pFil
d3710 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
d3720 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
d3730 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c SQLITE_IOERR_CL
d3740 4f 53 45 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e OSE;. p->pN
d3750 65 78 74 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 ext = pError;.
d3760 20 20 20 20 70 45 72 72 6f 72 20 3d 20 70 3b 0a pError = p;.
d3770 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d3780 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 sqlite3_free(p)
d3790 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f ;. }. }. pO
d37a0 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 pen->pUnused = p
d37b0 45 72 72 6f 72 3b 0a 20 20 72 65 74 75 72 6e 20 Error;. return
d37c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 rc;.}../*.** Add
d37d0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
d37e0 70 74 6f 72 20 75 73 65 64 20 62 79 20 66 69 6c ptor used by fil
d37f0 65 20 68 61 6e 64 6c 65 20 70 46 69 6c 65 20 74 e handle pFile t
d3800 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 o the correspond
d3810 69 6e 67 0a 2a 2a 20 70 55 6e 75 73 65 64 20 6c ing.** pUnused l
d3820 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ist..*/.static v
d3830 6f 69 64 20 73 65 74 50 65 6e 64 69 6e 67 46 64 oid setPendingFd
d3840 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
d3850 29 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 ){. struct unix
d3860 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d OpenCnt *pOpen =
d3870 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 pFile->pOpen;.
d3880 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 UnixUnusedFd *p
d3890 20 3d 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 = pFile->pUnuse
d38a0 64 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 d;. p->pNext =
d38b0 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b 0a pOpen->pUnused;.
d38c0 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 pOpen->pUnused
d38d0 20 3d 20 70 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 = p;. pFile->h
d38e0 20 3d 20 2d 31 3b 0a 20 20 70 46 69 6c 65 2d 3e = -1;. pFile->
d38f0 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a pUnused = 0;.}..
d3900 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 /*.** Lower the
d3910 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e locking level on
d3920 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d3930 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 pFile to lockty
d3940 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a pe. locktype.**
d3950 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
d3960 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 NO_LOCK or SHARE
d3970 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 D_LOCK..**.** If
d3980 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
d3990 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 el of the file d
d39a0 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 escriptor is alr
d39b0 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 eady at or below
d39c0 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 .** the requeste
d39d0 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c d locking level,
d39e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
d39f0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 a no-op..*/.sta
d3a00 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f tic int unixUnlo
d3a10 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d3a20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
d3a30 65 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a e){. unixFile *
d3a40 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d3a50 65 2a 29 69 64 3b 20 2f 2a 20 54 68 65 20 6f 70 e*)id; /* The op
d3a60 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 73 74 72 en file */. str
d3a70 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f uct unixLockInfo
d3a80 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a *pLock; /*
d3a90 20 53 74 72 75 63 74 75 72 65 20 64 65 73 63 72 Structure descr
d3aa0 69 62 69 6e 67 20 63 75 72 72 65 6e 74 20 6c 6f ibing current lo
d3ab0 63 6b 20 73 74 61 74 65 20 2a 2f 0a 20 20 73 74 ck state */. st
d3ac0 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b ruct flock lock;
d3ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
d3ae0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 * Information pa
d3af0 73 73 65 64 20 69 6e 74 6f 20 66 63 6e 74 6c 28 ssed into fcntl(
d3b00 29 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 ) */. int rc =
d3b10 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
d3b20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
d3b30 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 n code from this
d3b40 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 interface */.
d3b50 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 int h;
d3b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d3b70 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69 /* The underlyi
d3b80 6e 67 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ng file descript
d3b90 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 74 45 72 72 or */. int tErr
d3ba0 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 no;
d3bb0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f /* Erro
d3bc0 72 20 63 6f 64 65 20 66 72 6f 6d 20 73 79 73 74 r code from syst
d3bd0 65 6d 20 63 61 6c 6c 20 65 72 72 6f 72 73 20 2a em call errors *
d3be0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 /.. assert( pFi
d3bf0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 le );. OSTRACE7
d3c00 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 ("UNLOCK %d %d
d3c10 77 61 73 20 25 64 28 25 64 2c 25 64 29 20 70 69 was %d(%d,%d) pi
d3c20 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
d3c30 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 h, locktype,.
d3c40 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d3c50 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b pe, pFile->pLock
d3c60 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c ->locktype, pFil
d3c70 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 2c 20 67 e->pLock->cnt, g
d3c80 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 etpid());.. ass
d3c90 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 ert( locktype<=S
d3ca0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
d3cb0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
d3cc0 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype<=locktype ){
d3cd0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d3ce0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 TE_OK;. }. if(
d3cf0 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 CHECK_THREADID(
d3d00 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 pFile) ){. re
d3d10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
d3d20 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e SE;. }. unixEn
d3d30 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 68 20 terMutex();. h
d3d40 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 70 4c = pFile->h;. pL
d3d50 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f ock = pFile->pLo
d3d60 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c ck;. assert( pL
d3d70 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 29 3b 0a 20 ock->cnt!=0 );.
d3d80 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d3d90 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
d3da0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
d3db0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d pLock->locktype=
d3dc0 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 =pFile->locktype
d3dd0 20 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 );. Simulate
d3de0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 IOErrorBenign(1)
d3df0 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f ;. SimulateIO
d3e00 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a Error( h=(-1) ).
d3e10 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 SimulateIOEr
d3e20 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 0a 23 rorBenign(0);..#
d3e30 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
d3e40 20 20 2f 2a 20 57 68 65 6e 20 72 65 64 75 63 69 /* When reduci
d3e50 6e 67 20 61 20 6c 6f 63 6b 20 73 75 63 68 20 74 ng a lock such t
d3e60 68 61 74 20 6f 74 68 65 72 20 70 72 6f 63 65 73 hat other proces
d3e70 73 65 73 20 63 61 6e 20 73 74 61 72 74 0a 20 20 ses can start.
d3e80 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 ** reading the
d3e90 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
d3ea0 67 61 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20 gain, make sure
d3eb0 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 that the. **
d3ec0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e transaction coun
d3ed0 74 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20 ter was updated
d3ee0 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74 if any part of t
d3ef0 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
d3f00 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67 65 64 2e ** file changed.
d3f10 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 If the transac
d3f20 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 tion counter is
d3f30 6e 6f 74 20 75 70 64 61 74 65 64 2c 0a 20 20 20 not updated,.
d3f40 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 ** other connec
d3f50 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d tions to the sam
d3f60 65 20 66 69 6c 65 20 6d 69 67 68 74 20 6e 6f 74 e file might not
d3f70 20 72 65 61 6c 69 7a 65 20 74 68 61 74 0a 20 20 realize that.
d3f80 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 68 61 ** the file ha
d3f90 73 20 63 68 61 6e 67 65 64 20 61 6e 64 20 68 65 s changed and he
d3fa0 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 6b 6e nce might not kn
d3fb0 6f 77 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 ow to flush thei
d3fc0 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 r. ** cache.
d3fd0 20 54 68 65 20 75 73 65 20 6f 66 20 61 20 73 74 The use of a st
d3fe0 61 6c 65 20 63 61 63 68 65 20 63 61 6e 20 6c 65 ale cache can le
d3ff0 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 ad to database c
d4000 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 2a orruption.. *
d4010 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 /. assert( pF
d4020 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 ile->inNormalWri
d4030 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c te==0. |
d4040 7c 20 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 | pFile->dbUpdat
d4050 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c e==0. ||
d4060 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 pFile->transCnt
d4070 72 43 68 6e 67 3d 3d 31 20 29 3b 0a 20 20 20 20 rChng==1 );.
d4080 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 pFile->inNormalW
d4090 72 69 74 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 rite = 0;.#endif
d40a0 0a 0a 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 ... if( lockt
d40b0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d40c0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 61 ){. if( ra
d40d0 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46 ngeLock(pFile, F
d40e0 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29 _RDLCK, &tErrno)
d40f0 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 ==(-1) ){.
d4100 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d4110 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d4120 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d4130 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20 IOERR_RDLOCK);.
d4140 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f if( IS_LO
d4150 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
d4160 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
d4170 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d4180 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rno;. }.
d4190 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
d41a0 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a unlock;. }.
d41b0 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c }. lock.l
d41c0 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b _type = F_UNLCK;
d41d0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e . lock.l_when
d41e0 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
d41f0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 lock.l_start
d4200 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a = PENDING_BYTE;.
d4210 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d lock.l_len =
d4220 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20 50 45 2L; assert( PE
d4230 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 45 NDING_BYTE+1==RE
d4240 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b 0a 20 SERVED_BYTE );.
d4250 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 if( fcntl(h,
d4260 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 F_SETLK, &lock)!
d4270 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 70 =(-1) ){. p
d4280 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d Lock->locktype =
d4290 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
d42a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 74 }else{. t
d42b0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d42c0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d42d0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d42e0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d42f0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 TE_IOERR_UNLOCK)
d4300 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c ;. if( IS_L
d4310 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
d4320 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
d4330 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d4340 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 no;. }.
d4350 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 goto end_unloc
d4360 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 k;. }. }. i
d4370 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f f( locktype==NO_
d4380 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72 75 LOCK ){. stru
d4390 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
d43a0 70 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44 pOpen;.. /* D
d43b0 65 63 72 65 6d 65 6e 74 20 74 68 65 20 73 68 61 ecrement the sha
d43c0 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 red lock counter
d43d0 2e 20 20 52 65 6c 65 61 73 65 20 74 68 65 20 6c . Release the l
d43e0 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 ock using an.
d43f0 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 ** OS call only
d4400 20 77 68 65 6e 20 61 6c 6c 20 74 68 72 65 61 64 when all thread
d4410 73 20 69 6e 20 74 68 69 73 20 73 61 6d 65 20 70 s in this same p
d4420 72 6f 63 65 73 73 20 68 61 76 65 20 72 65 6c 65 rocess have rele
d4430 61 73 65 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 ased. ** the
d4440 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 lock.. */.
d4450 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 pLock->cnt--;.
d4460 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e if( pLock->cn
d4470 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f t==0 ){. lo
d4480 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e ck.l_type = F_UN
d4490 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e LCK;. lock.
d44a0 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
d44b0 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e SET;. lock.
d44c0 6c 5f 73 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c l_start = lock.l
d44d0 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 _len = 0L;.
d44e0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d44f0 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 Benign(1);.
d4500 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d4510 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 20 ( h=(-1) ).
d4520 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d4530 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 Benign(0);.
d4540 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f if( fcntl(h, F_
d4550 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 SETLK, &lock)!=(
d4560 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 -1) ){. p
d4570 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d Lock->locktype =
d4580 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 NO_LOCK;.
d4590 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74 }else{. t
d45a0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d45b0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
d45c0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d45d0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d45e0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
d45f0 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 K);. if(
d4600 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d4610 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
d4620 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d4630 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
d4640 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 }. pLoc
d4650 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f k->locktype = NO
d4660 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70 _LOCK;. p
d4670 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d4680 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 NO_LOCK;.
d4690 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
d46a0 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f Decrement the co
d46b0 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61 unt of locks aga
d46c0 69 6e 73 74 20 74 68 69 73 20 73 61 6d 65 20 66 inst this same f
d46d0 69 6c 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 20 ile. When the.
d46e0 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63 ** count reac
d46f0 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20 hes zero, close
d4700 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 20 64 any other file d
d4710 65 73 63 72 69 70 74 6f 72 73 20 77 68 6f 73 65 escriptors whose
d4720 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61 close. ** wa
d4730 73 20 64 65 66 65 72 72 65 64 20 62 65 63 61 75 s deferred becau
d4740 73 65 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e se of outstandin
d4750 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a g locks.. */.
d4760 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 46 69 6c pOpen = pFil
d4770 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 70 4f e->pOpen;. pO
d4780 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20 pen->nLock--;.
d4790 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d assert( pOpen-
d47a0 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 >nLock>=0 );.
d47b0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 if( pOpen->nLoc
d47c0 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e k==0 ){. in
d47d0 74 20 72 63 32 20 3d 20 63 6c 6f 73 65 50 65 6e t rc2 = closePen
d47e0 64 69 6e 67 46 64 73 28 70 46 69 6c 65 29 3b 0a dingFds(pFile);.
d47f0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
d4800 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
d4810 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 rc = rc2;.
d4820 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 09 }. }. }..
d4830 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 20 75 .end_unlock:. u
d4840 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
d4850 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
d4860 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f E_OK ) pFile->lo
d4870 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
d4880 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a e;. return rc;.
d4890 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
d48a0 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20 nction performs
d48b0 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68 65 the parts of the
d48c0 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70 "close file" op
d48d0 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d eration .** comm
d48e0 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e on to all lockin
d48f0 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63 6c g schemes. It cl
d4900 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74 6f oses the directo
d4910 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68 ry and file.** h
d4920 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79 20 andles, if they
d4930 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 73 are valid, and s
d4940 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f ets all fields o
d4950 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a f the unixFile.*
d4960 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 30 * structure to 0
d4970 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 6e ..**.** It is *n
d4980 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f ot* necessary to
d4990 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 20 hold the mutex
d49a0 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
d49b0 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 e is called,.**
d49c0 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 2e even on VxWorks.
d49d0 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c 20 62 A mutex will b
d49e0 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20 56 78 e acquired on Vx
d49f0 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a 2a 20 Works by the.**
d4a00 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 vxworksReleaseFi
d4a10 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65 2e 0a leId() routine..
d4a20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c */.static int cl
d4a30 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c 69 oseUnixFile(sqli
d4a40 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
d4a50 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d4a60 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d4a70 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b ;. if( pFile ){
d4a80 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e . if( pFile->
d4a90 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 dirfd>=0 ){.
d4aa0 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 int err = clos
d4ab0 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b e(pFile->dirfd);
d4ac0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29 . if( err )
d4ad0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
d4ae0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
d4af0 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 no;. retu
d4b00 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
d4b10 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 DIR_CLOSE;.
d4b20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
d4b30 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d 2d 31 3b pFile->dirfd=-1;
d4b40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d4b50 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e if( pFile->h>
d4b60 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 =0 ){. int
d4b70 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c err = close(pFil
d4b80 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 69 66 28 e->h);. if(
d4b90 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 err ){.
d4ba0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d4bb0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d4bc0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d4bd0 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 IOERR_CLOSE;.
d4be0 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 4f }. }.#if O
d4bf0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 69 66 S_VXWORKS. if
d4c00 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 29 7b 0a ( pFile->pId ){.
d4c10 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d if( pFile-
d4c20 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 >isDelete ){.
d4c30 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46 69 6c unlink(pFil
d4c40 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63 e->pId->zCanonic
d4c50 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d alName);. }
d4c60 0a 20 20 20 20 20 20 76 78 77 6f 72 6b 73 52 65 . vxworksRe
d4c70 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46 69 6c leaseFileId(pFil
d4c80 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 20 20 70 e->pId);. p
d4c90 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b 0a 20 File->pId = 0;.
d4ca0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
d4cb0 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20 OSTRACE2("CLOSE
d4cc0 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 %-3d\n", pFile
d4cd0 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 6e 43 6f ->h);. OpenCo
d4ce0 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20 20 73 unter(-1);. s
d4cf0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c qlite3_free(pFil
d4d00 65 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 20 20 e->pUnused);.
d4d10 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30 memset(pFile, 0
d4d20 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c , sizeof(unixFil
d4d30 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 e));. }. retur
d4d40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
d4d50 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
d4d60 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
d4d70 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69 t unixClose(sqli
d4d80 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
d4d90 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d4da0 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b _OK;. if( id ){
d4db0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
d4dc0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d4dd0 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e 69 78 55 *)id;. unixU
d4de0 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 nlock(id, NO_LOC
d4df0 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 K);. unixEnte
d4e00 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66 rMutex();. if
d4e10 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26 ( pFile->pOpen &
d4e20 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e & pFile->pOpen->
d4e30 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f nLock ){. /
d4e40 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f * If there are o
d4e50 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 utstanding locks
d4e60 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c , do not actuall
d4e70 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 y close the file
d4e80 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79 just. ** y
d4e90 65 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20 et because that
d4ea0 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73 would clear thos
d4eb0 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61 e locks. Instea
d4ec0 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a d, add the file.
d4ed0 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 ** descrip
d4ee0 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 70 55 tor to pOpen->pU
d4ef0 6e 75 73 65 64 20 6c 69 73 74 2e 20 20 49 74 20 nused list. It
d4f00 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 will be automati
d4f10 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 0a 20 20 cally closed .
d4f20 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 ** when the
d4f30 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65 last lock is cle
d4f40 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ared.. */.
d4f50 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46 setPendingF
d4f60 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a d(pFile);. }.
d4f70 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 releaseLockI
d4f80 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b nfo(pFile->pLock
d4f90 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 );. releaseOp
d4fa0 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 enCnt(pFile->pOp
d4fb0 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c en);. rc = cl
d4fc0 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b oseUnixFile(id);
d4fd0 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 . unixLeaveMu
d4fe0 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 tex();. }. ret
d4ff0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a urn rc;.}../****
d5000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
d5010 66 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69 f the posix advi
d5020 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d sory lock implem
d5030 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
d5040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
d5050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a *********/../***
d50a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d50b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d50c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d50d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d50e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
d50f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70 ********** No-op
d5110 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a Locking *******
d5120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
d5140 2a 20 4f 66 20 74 68 65 20 76 61 72 69 6f 75 73 * Of the various
d5150 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 locking impleme
d5160 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 6c 61 62 ntations availab
d5170 6c 65 2c 20 74 68 69 73 20 69 73 20 62 79 20 66 le, this is by f
d5180 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 6c 65 ar the.** simple
d5190 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 st: locking is
d51a0 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 74 74 ignored. No att
d51b0 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 empt is made to
d51c0 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 lock the databas
d51d0 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 72 65 e.** file for re
d51e0 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 ading or writing
d51f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 63 ..**.** This loc
d5200 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 70 70 king mode is app
d5210 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 ropriate for use
d5220 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 on read-only da
d5230 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 3a 20 tabases.** (ex:
d5240 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61 databases that a
d5250 72 65 20 62 75 72 6e 65 64 20 69 6e 74 6f 20 43 re burned into C
d5260 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 6d 70 D-ROM, for examp
d5270 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a 2a 20 le.) It can.**
d5280 61 6c 73 6f 20 62 65 20 75 73 65 64 20 69 66 20 also be used if
d5290 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
d52a0 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 78 74 employs some ext
d52b0 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 6d 20 ernal mechanism
d52c0 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 73 69 to.** prevent si
d52d0 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 65 73 multaneous acces
d52e0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 64 61 s of the same da
d52f0 74 61 62 61 73 65 20 62 79 20 74 77 6f 20 6f 72 tabase by two or
d5300 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 61 73 more.** databas
d5310 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 e connections.
d5320 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 73 But there is a s
d5330 65 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 20 64 erious risk of d
d5340 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 atabase.** corru
d5350 70 74 69 6f 6e 20 69 66 20 74 68 69 73 20 6c 6f ption if this lo
d5360 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 75 73 cking mode is us
d5370 65 64 20 69 6e 20 73 69 74 75 61 74 69 6f 6e 73 ed in situations
d5380 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 0a where multiple.
d5390 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ** database conn
d53a0 65 63 74 69 6f 6e 73 20 61 72 65 20 61 63 63 65 ections are acce
d53b0 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 ssing the same d
d53c0 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 atabase file at
d53d0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 6d 65 the same.** time
d53e0 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 and one or more
d53f0 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63 of those connec
d5400 74 69 6f 6e 73 20 61 72 65 20 77 72 69 74 69 6e tions are writin
d5410 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e g..*/..static in
d5420 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 t nolockCheckRes
d5430 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
d5440 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 3_file *NotUsed,
d5450 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
d5460 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
d5470 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a ER(NotUsed);. *
d5480 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72 pResOut = 0;. r
d5490 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d54a0 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f .}.static int no
d54b0 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 lockLock(sqlite3
d54c0 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 _file *NotUsed,
d54d0 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 int NotUsed2){.
d54e0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
d54f0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
d5500 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 sed2);. return
d5510 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 SQLITE_OK;.}.sta
d5520 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e tic int nolockUn
d5530 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
d5540 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 e *NotUsed, int
d5550 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 NotUsed2){. UNU
d5560 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
d5570 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
d5580 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
d5590 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
d55a0 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a Close the file..
d55b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f */.static int no
d55c0 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 lockClose(sqlite
d55d0 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 3_file *id) {.
d55e0 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 return closeUnix
d55f0 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a File(id);.}../**
d5600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5610 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 6f 2d * End of the no-
d5620 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e op lock implemen
d5630 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tation *********
d5640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
d5650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
d56a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d56b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d56c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d56d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d56e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d56f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5700 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 6f ******* Begin do
d5710 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 2a t-file Locking *
d5720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d5740 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c 65 20 .** The dotfile
d5750 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e locking implemen
d5760 74 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 tation uses the
d5770 65 78 69 73 74 61 6e 63 65 20 6f 66 20 73 65 70 existance of sep
d5780 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69 arate lock.** fi
d5790 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 les in order to
d57a0 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 control access t
d57b0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 o the database.
d57c0 20 54 68 69 73 20 77 6f 72 6b 73 20 6f 6e 20 6a This works on j
d57d0 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 65 76 65 ust.** about eve
d57e0 72 79 20 66 69 6c 65 73 79 73 74 65 6d 20 69 6d ry filesystem im
d57f0 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 74 20 74 aginable. But t
d5800 68 65 72 65 20 61 72 65 20 73 65 72 69 6f 75 73 here are serious
d5810 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a 2a downsides:.**.*
d5820 2a 20 20 20 20 28 31 29 20 20 54 68 65 72 65 20 * (1) There
d5830 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 72 72 65 is zero concurre
d5840 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 72 ncy. A single r
d5850 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 61 6c 6c eader blocks all
d5860 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 other.**
d5870 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72 connections fr
d5880 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 om reading or wr
d5890 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 iting the databa
d58a0 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 se..**.** (2)
d58b0 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e An application
d58c0 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20 crash or power
d58d0 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20 73 loss can leave s
d58e0 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0a tale lock files.
d58f0 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74 69 ** sitti
d5900 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20 6e ng around that n
d5910 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 eed to be cleare
d5920 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a d manually..**.*
d5930 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 * Nevertheless,
d5940 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61 6e 20 a dotlock is an
d5950 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b appropriate lock
d5960 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75 73 65 ing mode for use
d5970 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 20 if no.** other
d5980 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 locking strategy
d5990 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a is available..*
d59a0 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 *.** Dotfile loc
d59b0 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 20 63 72 king works by cr
d59c0 65 61 74 69 6e 67 20 61 20 66 69 6c 65 20 69 6e eating a file in
d59d0 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74 the same direct
d59e0 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a 20 64 61 ory as the.** da
d59f0 74 61 62 61 73 65 20 61 6e 64 20 77 69 74 68 20 tabase and with
d5a00 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 the same name bu
d5a10 74 20 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22 t with a ".lock"
d5a20 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 64 65 64 extension added
d5a30 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61 6e ..** The existan
d5a40 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 66 69 6c ce of a lock fil
d5a50 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58 43 e implies an EXC
d5a60 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41 6c LUSIVE lock. Al
d5a70 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a 20 l other lock.**
d5a80 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20 52 types (SHARED, R
d5a90 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e 47 ESERVED, PENDING
d5aa0 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e 74 ) are mapped int
d5ab0 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f 0a o EXCLUSIVE..*/.
d5ac0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 ./*.** The file
d5ad0 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f 20 suffix added to
d5ae0 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66 69 the data base fi
d5af0 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72 20 lename in order
d5b00 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a 2a to create the.**
d5b10 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 23 lock file..*/.#
d5b20 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53 define DOTLOCK_S
d5b30 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f UFFIX ".lock"../
d5b40 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
d5b50 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
d5b60 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
d5b70 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
d5b80 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
d5b90 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
d5ba0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
d5bb0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
d5bc0 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 is held, set *pR
d5bd0 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f esOut.** to a no
d5be0 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 n-zero value oth
d5bf0 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 erwise *pResOut
d5c00 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
d5c10 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
d5c20 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 e.** is set to S
d5c30 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 QLITE_OK unless
d5c40 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 an I/O error occ
d5c50 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 urs during lock
d5c60 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 checking..**.**
d5c70 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 In dotfile locki
d5c80 6e 67 2c 20 65 69 74 68 65 72 20 61 20 6c 6f 63 ng, either a loc
d5c90 6b 20 65 78 69 73 74 73 20 6f 72 20 69 74 20 64 k exists or it d
d5ca0 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20 oes not. So in
d5cb0 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f this.** variatio
d5cc0 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 65 72 76 n of CheckReserv
d5cd0 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f edLock(), *pResO
d5ce0 75 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 ut is set to tru
d5cf0 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a e if any lock.**
d5d00 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 is held on the
d5d10 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 65 20 69 file and false i
d5d20 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e f the file is un
d5d30 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 locked..*/.stati
d5d40 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65 c int dotlockChe
d5d50 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
d5d60 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d5d70 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b int *pResOut) {
d5d80 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d5d90 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 TE_OK;. int res
d5da0 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 erved = 0;. uni
d5db0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
d5dc0 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 unixFile*)id;..
d5dd0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d5de0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
d5df0 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
d5e00 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 VEDLOCK; );. .
d5e10 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d5e20 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 ;.. /* Check if
d5e30 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 a thread in thi
d5e40 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 s process holds
d5e50 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 such a lock */.
d5e60 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d5e70 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
d5e80 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 ){. /* Eithe
d5e90 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f r this connectio
d5ea0 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 n or some other
d5eb0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68 connection in th
d5ec0 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 0a 20 e same process.
d5ed0 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f ** holds a lo
d5ee0 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 ck on the file.
d5ef0 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 No need to chec
d5f00 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20 k further. */.
d5f10 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
d5f20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
d5f30 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 The lock is held
d5f40 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 if and only if
d5f50 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69 the lockfile exi
d5f60 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 sts */. const
d5f70 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 char *zLockFile
d5f80 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 = (const char*)
d5f90 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
d5fa0 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 73 65 72 ntext;. reser
d5fb0 76 65 64 20 3d 20 61 63 63 65 73 73 28 7a 4c 6f ved = access(zLo
d5fc0 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20 ckFile, 0)==0;.
d5fd0 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 }. OSTRACE4("T
d5fe0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
d5ff0 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d %d\n", pFile->
d6000 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 h, rc, reserved)
d6010 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 ;. *pResOut = r
d6020 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 eserved;. retur
d6030 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
d6040 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 ock the file wit
d6050 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 h the lock speci
d6060 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
d6070 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 r locktype - one
d6080 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f .** of the follo
d6090 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 wing:.**.**
d60a0 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a (1) SHARED_LOCK.
d60b0 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 ** (2) RESER
d60c0 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 VED_LOCK.**
d60d0 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b (3) PENDING_LOCK
d60e0 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c .** (4) EXCL
d60f0 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a USIVE_LOCK.**.**
d6100 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 Sometimes when
d6110 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c requesting one l
d6120 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 ock state, addit
d6130 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 ional lock state
d6140 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 s.** are inserte
d6150 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 d in between. T
d6160 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 he locking might
d6170 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 fail on one of
d6180 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 the later.** tra
d6190 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 nsitions leaving
d61a0 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 the lock state
d61b0 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 different from w
d61c0 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 hat it started b
d61d0 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 ut.** still shor
d61e0 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 t of its goal.
d61f0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 The following ch
d6200 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c art shows the al
d6210 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 lowed.** transit
d6220 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 ions and the ins
d6230 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 erted intermedia
d6240 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a te states:.**.**
d6250 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 UNLOCKED ->
d6260 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 SHARED.** SHA
d6270 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a RED -> RESERVED.
d6280 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
d6290 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 (PENDING) -> EXC
d62a0 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 LUSIVE.** RES
d62b0 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e ERVED -> (PENDIN
d62c0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
d62d0 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e ** PENDING ->
d62e0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a EXCLUSIVE.**.**
d62f0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 This routine wi
d6300 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 ll only increase
d6310 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 a lock. Use th
d6320 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 e sqlite3OsUnloc
d6330 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 k().** routine t
d6340 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e o lower a lockin
d6350 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 g level..**.** W
d6360 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b ith dotfile lock
d6370 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 20 6f ing, we really o
d6380 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 61 74 nly support stat
d6390 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 56 45 e (4): EXCLUSIVE
d63a0 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 61 63 ..** But we trac
d63b0 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b k the other lock
d63c0 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 65 72 ing levels inter
d63d0 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 nally..*/.static
d63e0 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b int dotlockLock
d63f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d6400 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
d6410 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 {. unixFile *p
d6420 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d6430 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a *)id;. int fd;.
d6440 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c char *zLockFil
d6450 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c e = (char *)pFil
d6460 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
d6470 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
d6480 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20 LITE_OK;... /*
d6490 49 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 6c If we have any l
d64a0 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f ock, then the lo
d64b0 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 64 79 20 ck file already
d64c0 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 77 65 20 exists. All we
d64d0 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 have. ** to do
d64e0 69 73 20 61 64 6a 75 73 74 20 6f 75 72 20 69 6e is adjust our in
d64f0 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 ternal record of
d6500 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e the lock level.
d6510 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
d6520 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f e->locktype > NO
d6530 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 _LOCK ){. pFi
d6540 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d6550 6f 63 6b 74 79 70 65 3b 0a 23 69 66 20 21 4f 53 ocktype;.#if !OS
d6560 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 2f 2a 20 _VXWORKS. /*
d6570 41 6c 77 61 79 73 20 75 70 64 61 74 65 20 74 68 Always update th
d6580 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e 20 74 e timestamp on t
d6590 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a 20 he old file */.
d65a0 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46 utimes(zLockF
d65b0 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64 ile, NULL);.#end
d65c0 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 if. return SQ
d65d0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a LITE_OK;. }. .
d65e0 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 /* grab an exc
d65f0 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 lusive lock */.
d6600 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b fd = open(zLock
d6610 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f File,O_RDONLY|O_
d6620 43 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30 CREAT|O_EXCL,060
d6630 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 0);. if( fd<0 )
d6640 7b 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 {. /* failed
d6650 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74 to open/create t
d6660 68 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65 he file, someone
d6670 20 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 73 else may have s
d6680 74 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a tolen the lock *
d6690 2f 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f /. int tErrno
d66a0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 = errno;. if
d66b0 28 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72 ( EEXIST == tErr
d66c0 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d no ){. rc =
d66d0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
d66e0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
d66f0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
d6700 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d6710 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d6720 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 OERR_LOCK);.
d6730 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
d6740 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 ROR(rc) ){.
d6750 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
d6760 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
d6770 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
d6780 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 0a return rc;. } .
d6790 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 20 if( close(fd)
d67a0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 ){. pFile->la
d67b0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
d67c0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d67d0 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 _IOERR_CLOSE;.
d67e0 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74 }. . /* got it
d67f0 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 , set the type a
d6800 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a nd return ok */.
d6810 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d6820 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 e = locktype;.
d6830 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
d6840 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
d6850 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
d6860 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
d6870 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
d6880 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
d6890 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
d68a0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
d68b0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
d68c0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
d68d0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
d68e0 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
d68f0 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
d6900 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
d6910 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
d6920 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
d6930 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 no-op..**.** Wh
d6940 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c en the locking l
d6950 65 76 65 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f evel reaches NO_
d6960 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 74 68 65 LOCK, delete the
d6970 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 lock file..*/.s
d6980 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 tatic int dotloc
d6990 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f kUnlock(sqlite3_
d69a0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
d69b0 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 cktype) {. unix
d69c0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d69d0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 63 nixFile*)id;. c
d69e0 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d har *zLockFile =
d69f0 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e (char *)pFile->
d6a00 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
d6a10 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
d6a20 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 );. OSTRACE5("
d6a30 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 UNLOCK %d %d wa
d6a40 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 s %d pid=%d\n",
d6a50 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
d6a60 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c pe,.. pFile->l
d6a70 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 ocktype, getpid(
d6a80 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f ));. assert( lo
d6a90 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
d6aa0 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e OCK );. . /* n
d6ab0 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 o-op if possible
d6ac0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d */. if( pFile-
d6ad0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 >locktype==lockt
d6ae0 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ype ){. retur
d6af0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d6b00 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 72 .. /* To downgr
d6b10 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20 73 ade to shared, s
d6b20 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75 72 imply update our
d6b30 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f 6e internal notion
d6b40 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63 of the. ** loc
d6b50 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65 65 k state. No nee
d6b60 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20 74 d to mess with t
d6b70 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e he file on disk.
d6b80 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
d6b90 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d6ba0 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
d6bb0 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 locktype = SHARE
d6bc0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 D_LOCK;. retu
d6bd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d6be0 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 6c }. . /* To ful
d6bf0 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 ly unlock the da
d6c00 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 74 tabase, delete t
d6c10 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a he lock file */.
d6c20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
d6c30 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 pe==NO_LOCK );.
d6c40 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f 63 if( unlink(zLoc
d6c50 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69 6e kFile) ){. in
d6c60 74 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 69 6e t rc = 0;. in
d6c70 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f t tErrno = errno
d6c80 3b 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54 ;. if( ENOENT
d6c90 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 != tErrno ){.
d6ca0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d6cb0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d6cc0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d6cd0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
d6ce0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49 . }. if( I
d6cf0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d6d00 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
d6d10 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d6d20 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 rno;. }. r
d6d30 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 eturn rc; . }.
d6d40 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d6d50 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 = NO_LOCK;. re
d6d60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d6d70 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 }../*.** Close a
d6d80 20 66 69 6c 65 2e 20 20 4d 61 6b 65 20 73 75 72 file. Make sur
d6d90 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 62 e the lock has b
d6da0 65 65 6e 20 72 65 6c 65 61 73 65 64 20 62 65 66 een released bef
d6db0 6f 72 65 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f 0a ore closing..*/.
d6dc0 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f static int dotlo
d6dd0 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f ckClose(sqlite3_
d6de0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e file *id) {. in
d6df0 74 20 72 63 3b 0a 20 20 69 66 28 20 69 64 20 29 t rc;. if( id )
d6e00 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a {. unixFile *
d6e10 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d6e20 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f 74 6c 6f e*)id;. dotlo
d6e30 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f ckUnlock(id, NO_
d6e40 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c 69 74 LOCK);. sqlit
d6e50 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c e3_free(pFile->l
d6e60 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a ockingContext);.
d6e70 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c 6f 73 65 }. rc = close
d6e80 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 UnixFile(id);.
d6e90 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a return rc;.}./**
d6ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6eb0 20 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74 2d End of the dot-
d6ec0 66 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d file lock implem
d6ed0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
d6ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
d6ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
d6f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d6f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6fa0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
d6fb0 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a lock Locking ***
d6fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
d6fe0 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 63 .** Use the floc
d6ff0 6b 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 k() system call
d7000 74 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69 to do file locki
d7010 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 ng..**.** flock(
d7020 29 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b ) locking is lik
d7030 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 e dot-file locki
d7040 6e 67 20 69 6e 20 74 68 61 74 20 74 68 65 20 76 ng in that the v
d7050 61 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67 arious.** fine-g
d7060 72 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 rain locking lev
d7070 65 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 79 els supported by
d7080 20 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c SQLite are coll
d7090 61 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20 apsed into.** a
d70a0 73 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 65 single exclusive
d70b0 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72 lock. In other
d70c0 20 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c 20 words, SHARED,
d70d0 52 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a RESERVED, and.**
d70e0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61 PENDING locks a
d70f0 72 65 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e re the same thin
d7100 67 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56 g as an EXCLUSIV
d7110 45 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a E lock. SQLite.
d7120 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77 ** still works w
d7130 68 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73 2c hen you do this,
d7140 20 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63 79 but concurrency
d7150 20 69 73 20 72 65 64 75 63 65 64 20 73 69 6e 63 is reduced sinc
d7160 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 e.** only a sing
d7170 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 62 le process can b
d7180 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 e reading the da
d7190 74 61 62 61 73 65 20 61 74 20 61 20 74 69 6d 65 tabase at a time
d71a0 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69 ..**.** Omit thi
d71b0 73 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51 4c s section if SQL
d71c0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
d71d0 4e 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72 6e NG_STYLE is turn
d71e0 65 64 20 6f 66 66 20 6f 72 20 69 66 0a 2a 2a 20 ed off or if.**
d71f0 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 56 58 compiling for VX
d7200 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 51 WORKS..*/.#if SQ
d7210 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
d7220 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 ING_STYLE && !OS
d7230 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20 _VXWORKS../*.**
d7240 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
d7250 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 cks if there is
d7260 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
d7270 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 held on the spec
d7280 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 ified.** file by
d7290 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 this or any oth
d72a0 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 er process. If s
d72b0 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 uch a lock is he
d72c0 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 ld, set *pResOut
d72d0 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 .** to a non-zer
d72e0 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 o value otherwis
d72f0 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 e *pResOut is se
d7300 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 t to zero. The
d7310 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
d7320 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 is set to SQLITE
d7330 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f _OK unless an I/
d7340 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 O error occurs d
d7350 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b uring lock check
d7360 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
d7370 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73 nt flockCheckRes
d7380 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
d7390 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
d73a0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 *pResOut){. int
d73b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d73c0 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 . int reserved
d73d0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 = 0;. unixFile
d73e0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d73f0 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d le*)id;. . Sim
d7400 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 ulateIOError( re
d7410 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
d7420 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c R_CHECKRESERVEDL
d7430 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 OCK; );. . ass
d7440 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
d7450 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 . /* Check if a
d7460 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 thread in this
d7470 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 process holds su
d7480 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 ch a lock */. i
d7490 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d74a0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 pe>SHARED_LOCK )
d74b0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d {. reserved =
d74c0 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 1;. }. . /*
d74d0 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 Otherwise see if
d74e0 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
d74f0 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f ess holds it. */
d7500 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 . if( !reserved
d7510 20 29 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d ){. /* attem
d7520 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f pt to get the lo
d7530 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 ck */. int lr
d7540 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d c = flock(pFile-
d7550 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f >h, LOCK_EX | LO
d7560 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 28 20 CK_NB);. if(
d7570 21 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a !lrc ){. /*
d7580 20 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20 75 got the lock, u
d7590 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20 20 nlock it */.
d75a0 20 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 lrc = flock(pF
d75b0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 ile->h, LOCK_UN)
d75c0 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c 72 63 ;. if ( lrc
d75d0 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 ) {. int
d75e0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d75f0 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 6c 6f . /* unlo
d7600 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 ck failed with a
d7610 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 n error */.
d7620 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 lrc = sqliteE
d7630 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d7640 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d7650 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b E_IOERR_UNLOCK);
d7660 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53 . if( IS
d7670 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 _LOCK_ERROR(lrc)
d7680 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 ){. pF
d7690 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d76a0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 tErrno;.
d76b0 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 rc = lrc;.
d76c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
d76d0 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
d76e0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
d76f0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73 65 rrno;. rese
d7700 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 rved = 1;.
d7710 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 /* someone else
d7720 6d 69 67 68 74 20 68 61 76 65 20 69 74 20 72 65 might have it re
d7730 73 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20 20 served */.
d7740 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f lrc = sqliteErro
d7750 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d7760 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d7770 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 20 OERR_LOCK); .
d7780 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d7790 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 RROR(lrc) ){.
d77a0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d77b0 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d77c0 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 rc = lrc
d77d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
d77e0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 }. OSTRACE4("
d77f0 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 TEST WR-LOCK %d
d7800 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d %d %d\n", pFile-
d7810 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 >h, rc, reserved
d7820 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );..#ifdef SQLIT
d7830 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c E_IGNORE_FLOCK_L
d7840 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 OCK_ERRORS. if(
d7850 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f (rc & SQLITE_IO
d7860 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 ERR) == SQLITE_I
d7870 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d OERR ){. rc =
d7880 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
d7890 72 65 73 65 72 76 65 64 3d 31 3b 0a 20 20 7d 0a reserved=1;. }.
d78a0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
d78b0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f _IGNORE_FLOCK_LO
d78c0 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 2a CK_ERRORS */. *
d78d0 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 pResOut = reserv
d78e0 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ed;. return rc;
d78f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
d7900 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
d7910 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
d7920 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
d7930 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
d7940 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
d7950 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
d7960 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
d7970 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
d7980 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
d7990 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
d79a0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
d79b0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
d79c0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
d79d0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
d79e0 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
d79f0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
d7a00 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
d7a10 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
d7a20 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
d7a30 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
d7a40 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
d7a50 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
d7a60 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
d7a70 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
d7a80 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
d7a90 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
d7aa0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
d7ab0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
d7ac0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
d7ad0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
d7ae0 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
d7af0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
d7b00 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
d7b10 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
d7b20 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
d7b30 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
d7b40 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
d7b50 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
d7b60 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
d7b70 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d7b80 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d7b90 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
d7ba0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 USIVE.**.** floc
d7bb0 6b 28 29 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 k() only really
d7bc0 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 support EXCLUSIV
d7bd0 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 E locks. We tra
d7be0 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a ck intermediate.
d7bf0 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 ** lock states i
d7c00 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 n the sqlite3_fi
d7c10 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75 le structure, bu
d7c20 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 t all locks SHAR
d7c30 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 ED or.** above a
d7c40 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 re really EXCLUS
d7c50 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 IVE locks and ex
d7c60 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 clude all other
d7c70 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a processes from.*
d7c80 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c * access the fil
d7c90 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
d7ca0 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 utine will only
d7cb0 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e increase a lock.
d7cc0 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 Use the sqlite
d7cd0 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 3OsUnlock().** r
d7ce0 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 outine to lower
d7cf0 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e a locking level.
d7d00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
d7d10 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 lockLock(sqlite3
d7d20 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c _file *id, int l
d7d30 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 ocktype) {. int
d7d40 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d7d50 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
d7d60 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
d7d70 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 id;.. assert( p
d7d80 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66 File );.. /* if
d7d90 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 we already have
d7da0 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 a lock, it is e
d7db0 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a xclusive. . **
d7dc0 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 Just adjust lev
d7dd0 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f el and punt on o
d7de0 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 utta here. */.
d7df0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if (pFile->lockt
d7e00 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b ype > NO_LOCK) {
d7e10 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
d7e20 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b type = locktype;
d7e30 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d7e40 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d7e50 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 /* grab an exclu
d7e60 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a sive lock */. .
d7e70 20 20 69 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c if (flock(pFil
d7e80 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 e->h, LOCK_EX |
d7e90 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 LOCK_NB)) {.
d7ea0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
d7eb0 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 no;. /* didn'
d7ec0 74 20 67 65 74 2c 20 6d 75 73 74 20 62 65 20 62 t get, must be b
d7ed0 75 73 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 usy */. rc =
d7ee0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d7ef0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d7f00 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c , SQLITE_IOERR_L
d7f10 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 OCK);. if( IS
d7f20 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d7f30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
d7f40 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d7f50 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c no;. }. } el
d7f60 73 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20 se {. /* got
d7f70 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 it, set the type
d7f80 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a and return ok *
d7f90 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 /. pFile->loc
d7fa0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
d7fb0 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 ;. }. OSTRACE4
d7fc0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
d7fd0 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %s\n", pFile->h,
d7fe0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f locktypeName(lo
d7ff0 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 cktype), .
d8000 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f rc==SQLITE_
d8010 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 OK ? "ok" : "fai
d8020 6c 65 64 22 29 3b 0a 23 69 66 64 65 66 20 53 51 led");.#ifdef SQ
d8030 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 LITE_IGNORE_FLOC
d8040 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 K_LOCK_ERRORS.
d8050 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54 45 if( (rc & SQLITE
d8060 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 _IOERR) == SQLIT
d8070 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 E_IOERR ){. r
d8080 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
d8090 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
d80a0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
d80b0 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a CK_LOCK_ERRORS *
d80c0 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d /. return rc;.}
d80d0 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 .../*.** Lower t
d80e0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
d80f0 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 on file descrip
d8100 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 tor pFile to loc
d8110 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 ktype. locktype
d8120 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 .** must be eith
d8130 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 er NO_LOCK or SH
d8140 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a ARED_LOCK..**.**
d8150 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 If the locking
d8160 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c level of the fil
d8170 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 e descriptor is
d8180 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 already at or be
d8190 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 low.** the reque
d81a0 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 sted locking lev
d81b0 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 el, this routine
d81c0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
d81d0 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b static int flock
d81e0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
d81f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
d8200 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 ktype) {. unixF
d8210 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d8220 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 ixFile*)id;. .
d8230 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d8240 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e ;. OSTRACE5("UN
d8250 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 LOCK %d %d was
d8260 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 %d pid=%d\n", pF
d8270 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
d8280 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69 ,. pFi
d8290 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 le->locktype, ge
d82a0 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 tpid());. asser
d82b0 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 t( locktype<=SHA
d82c0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 RED_LOCK );. .
d82d0 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 /* no-op if pos
d82e0 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 sible */. if( p
d82f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
d8300 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 locktype ){.
d8310 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d8320 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 ;. }. . /* sh
d8330 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 ared can just be
d8340 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 set because we
d8350 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 always have an e
d8360 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 xclusive */. if
d8370 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 (locktype==SHAR
d8380 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 ED_LOCK) {. p
d8390 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d83a0 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 locktype;. r
d83b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d83c0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c . }. . /* no,
d83d0 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e really, unlock.
d83e0 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 */. int rc = f
d83f0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c lock(pFile->h, L
d8400 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72 OCK_UN);. if (r
d8410 63 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20 c) {. int r,
d8420 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d8430 20 20 20 20 72 20 3d 20 73 71 6c 69 74 65 45 72 r = sqliteEr
d8440 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f rorFromPosixErro
d8450 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 r(tErrno, SQLITE
d8460 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a _IOERR_UNLOCK);.
d8470 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
d8480 45 52 52 4f 52 28 72 29 20 29 7b 0a 20 20 20 20 ERROR(r) ){.
d8490 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d84a0 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d84b0 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
d84c0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f _IGNORE_FLOCK_LO
d84d0 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20 69 66 CK_ERRORS. if
d84e0 28 20 28 72 20 26 20 53 51 4c 49 54 45 5f 49 4f ( (r & SQLITE_IO
d84f0 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 ERR) == SQLITE_I
d8500 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 20 72 20 OERR ){. r
d8510 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
d8520 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.#endif /* S
d8530 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
d8540 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a CK_LOCK_ERRORS *
d8550 2f 0a 20 20 20 20 0a 20 20 20 20 72 65 74 75 72 /. . retur
d8560 6e 20 72 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a n r;. } else {.
d8570 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d8580 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 ype = NO_LOCK;.
d8590 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d85a0 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a _OK;. }.}../*.*
d85b0 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a * Close a file..
d85c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c */.static int fl
d85d0 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 ockClose(sqlite3
d85e0 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 _file *id) {. i
d85f0 66 28 20 69 64 20 29 7b 0a 20 20 20 20 66 6c 6f f( id ){. flo
d8600 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f ckUnlock(id, NO_
d8610 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 LOCK);. }. ret
d8620 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c urn closeUnixFil
d8630 65 28 69 64 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 e(id);.}..#endif
d8640 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
d8650 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
d8660 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 20 2a 2f && !OS_VXWORK */
d8670 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
d8680 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 ****** End of th
d8690 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b 20 69 6d 70 e flock lock imp
d86a0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a lementation ****
d86b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d86c0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
d86d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d86e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d86f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8710 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
d8720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d8760 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
d8770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
d8780 6e 20 4e 61 6d 65 64 20 53 65 6d 61 70 68 6f 72 n Named Semaphor
d8790 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a e Locking ******
d87a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d87b0 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 **.**.** Named s
d87c0 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 emaphore locking
d87d0 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 is only support
d87e0 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a ed on VxWorks..*
d87f0 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c *.** Semaphore l
d8800 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 ocking is like d
d8810 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20 66 6c 6f 63 ot-lock and floc
d8820 6b 20 69 6e 20 74 68 61 74 20 69 74 20 72 65 61 k in that it rea
d8830 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 73 75 70 70 lly only.** supp
d8840 6f 72 74 73 20 45 58 43 4c 55 53 49 56 45 20 6c orts EXCLUSIVE l
d8850 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 20 ocking. Only a
d8860 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63 single process c
d8870 61 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 an read or write
d8880 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
d8890 20 66 69 6c 65 20 61 74 20 61 20 74 69 6d 65 2e file at a time.
d88a0 20 20 54 68 69 73 20 72 65 64 75 63 65 73 20 70 This reduces p
d88b0 6f 74 65 6e 74 69 61 6c 20 63 6f 6e 63 75 72 72 otential concurr
d88c0 65 6e 63 79 2c 20 62 75 74 0a 2a 2a 20 6d 61 6b ency, but.** mak
d88d0 65 73 20 74 68 65 20 6c 6f 63 6b 20 69 6d 70 6c es the lock impl
d88e0 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 63 68 20 ementation much
d88f0 65 61 73 69 65 72 2e 0a 2a 2f 0a 23 69 66 20 4f easier..*/.#if O
d8900 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a S_VXWORKS../*.**
d8910 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
d8920 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 ecks if there is
d8930 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
d8940 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 held on the spe
d8950 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 cified.** file b
d8960 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 y this or any ot
d8970 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 her process. If
d8980 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 such a lock is h
d8990 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 eld, set *pResOu
d89a0 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 t.** to a non-ze
d89b0 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 ro value otherwi
d89c0 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 se *pResOut is s
d89d0 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 et to zero. The
d89e0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a return value.**
d89f0 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 is set to SQLIT
d8a00 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 E_OK unless an I
d8a10 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 /O error occurs
d8a20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 during lock chec
d8a30 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 king..*/.static
d8a40 69 6e 74 20 73 65 6d 43 68 65 63 6b 52 65 73 65 int semCheckRese
d8a50 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 rvedLock(sqlite3
d8a60 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a _file *id, int *
d8a70 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 pResOut) {. int
d8a80 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
d8a90 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 . int reserved
d8aa0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 = 0;. unixFile
d8ab0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d8ac0 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c le*)id;.. Simul
d8ad0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
d8ae0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
d8af0 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 CHECKRESERVEDLOC
d8b00 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 K; );. . asser
d8b10 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f t( pFile );.. /
d8b20 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 * Check if a thr
d8b30 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 ead in this proc
d8b40 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 ess holds such a
d8b50 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 lock */. if( p
d8b60 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 File->locktype>S
d8b70 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
d8b80 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
d8b90 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 }. . /* Othe
d8ba0 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d rwise see if som
d8bb0 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 e other process
d8bc0 68 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 holds it. */. i
d8bd0 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a f( !reserved ){.
d8be0 20 20 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 sem_t *pSem
d8bf0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e = pFile->pOpen->
d8c00 70 53 65 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 pSem;. struct
d8c10 20 73 74 61 74 20 73 74 61 74 42 75 66 3b 0a 0a stat statBuf;..
d8c20 20 20 20 20 69 66 28 20 73 65 6d 5f 74 72 79 77 if( sem_tryw
d8c30 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b ait(pSem)==-1 ){
d8c40 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e . int tErrn
d8c50 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 o = errno;.
d8c60 20 69 66 28 20 45 41 47 41 49 4e 20 21 3d 20 74 if( EAGAIN != t
d8c70 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 Errno ){.
d8c80 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
d8c90 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
d8ca0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 tErrno, SQLITE_I
d8cb0 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 OERR_CHECKRESERV
d8cc0 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 EDLOCK);.
d8cd0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d8ce0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d8cf0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
d8d00 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c /* someone el
d8d10 73 65 20 68 61 73 20 74 68 65 20 6c 6f 63 6b 20 se has the lock
d8d20 77 68 65 6e 20 77 65 20 61 72 65 20 69 6e 20 4e when we are in N
d8d30 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 20 20 O_LOCK */.
d8d40 20 20 72 65 73 65 72 76 65 64 20 3d 20 28 70 46 reserved = (pF
d8d50 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 ile->locktype <
d8d60 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
d8d70 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
d8d80 0a 20 20 20 20 20 20 2f 2a 20 77 65 20 63 6f 75 . /* we cou
d8d90 6c 64 20 68 61 76 65 20 69 74 20 69 66 20 77 65 ld have it if we
d8da0 20 77 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 want it */.
d8db0 20 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 sem_post(pSem)
d8dc0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 ;. }. }. OS
d8dd0 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d TRACE4("TEST WR-
d8de0 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 LOCK %d %d %d\n"
d8df0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 , pFile->h, rc,
d8e00 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70 reserved);.. *p
d8e10 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 ResOut = reserve
d8e20 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a d;. return rc;.
d8e30 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
d8e40 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
d8e50 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
d8e60 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
d8e70 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
d8e80 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
d8e90 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
d8ea0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
d8eb0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
d8ec0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
d8ed0 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
d8ee0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
d8ef0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
d8f00 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
d8f10 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
d8f20 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
d8f30 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
d8f40 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
d8f50 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
d8f60 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
d8f70 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
d8f80 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
d8f90 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
d8fa0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
d8fb0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
d8fc0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
d8fd0 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
d8fe0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
d8ff0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
d9000 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
d9010 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
d9020 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
d9030 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
d9040 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
d9050 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
d9060 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
d9070 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
d9080 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
d9090 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
d90a0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
d90b0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
d90c0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
d90d0 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
d90e0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 SIVE.**.** Semap
d90f0 68 6f 72 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 20 hore locks only
d9100 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 really support E
d9110 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 XCLUSIVE locks.
d9120 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d We track interm
d9130 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 ediate.** lock s
d9140 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c tates in the sql
d9150 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 ite3_file struct
d9160 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 ure, but all loc
d9170 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 ks SHARED or.**
d9180 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79 above are really
d9190 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 EXCLUSIVE locks
d91a0 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c and exclude all
d91b0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 other processes
d91c0 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 from.** access
d91d0 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 the file..**.**
d91e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
d91f0 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 l only increase
d9200 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 a lock. Use the
d9210 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b sqlite3OsUnlock
d9220 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f ().** routine to
d9230 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 lower a locking
d9240 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 level..*/.stati
d9250 63 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71 c int semLock(sq
d9260 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d9270 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
d9280 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
d9290 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
d92a0 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 73 d;. int fd;. s
d92b0 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 em_t *pSem = pFi
d92c0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b le->pOpen->pSem;
d92d0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d92e0 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20 TE_OK;.. /* if
d92f0 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 we already have
d9300 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78 a lock, it is ex
d9310 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20 clusive. . **
d9320 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65 Just adjust leve
d9330 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75 l and punt on ou
d9340 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69 tta here. */. i
d9350 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f (pFile->lockty
d9360 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a pe > NO_LOCK) {.
d9370 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d9380 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
d9390 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
d93a0 4f 4b 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d OK;. goto sem
d93b0 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 _end_lock;. }.
d93c0 20 0a 20 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61 . /* lock sema
d93d0 70 68 6f 72 65 20 6e 6f 77 20 62 75 74 20 62 61 phore now but ba
d93e0 69 6c 20 6f 75 74 20 77 68 65 6e 20 61 6c 72 65 il out when alre
d93f0 61 64 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 ady locked. */.
d9400 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 if( sem_trywait
d9410 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 (pSem)==-1 ){.
d9420 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 rc = SQLITE_BU
d9430 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d SY;. goto sem
d9440 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a _end_lock;. }..
d9450 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 /* got it, set
d9460 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 the type and re
d9470 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 turn ok */. pFi
d9480 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d9490 6f 63 6b 74 79 70 65 3b 0a 0a 20 73 65 6d 5f 65 ocktype;.. sem_e
d94a0 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72 nd_lock:. retur
d94b0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
d94c0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
d94d0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
d94e0 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
d94f0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
d9500 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
d9510 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
d9520 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
d9530 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
d9540 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
d9550 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
d9560 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
d9570 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
d9580 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
d9590 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
d95a0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
d95b0 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
d95c0 20 73 65 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 semUnlock(sqlit
d95d0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d95e0 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
d95f0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d9600 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d9610 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 sem_t *pSem =
d9620 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 pFile->pOpen->pS
d9630 65 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 em;.. assert( p
d9640 46 69 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 File );. assert
d9650 28 20 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52 ( pSem );. OSTR
d9660 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 ACE5("UNLOCK %d
d9670 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 %d was %d pid=%
d9680 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
d9690 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 locktype,.. pF
d96a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 ile->locktype, g
d96b0 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 etpid());. asse
d96c0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 rt( locktype<=SH
d96d0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a ARED_LOCK );. .
d96e0 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f /* no-op if po
d96f0 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 ssible */. if(
d9700 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
d9710 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 =locktype ){.
d9720 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d9730 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 K;. }. . /* s
d9740 68 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 hared can just b
d9750 65 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 e set because we
d9760 20 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 always have an
d9770 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 exclusive */. i
d9780 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f (locktype==SHA
d9790 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 RED_LOCK) {.
d97a0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d97b0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 = locktype;.
d97c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d97d0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f ;. }. . /* no
d97e0 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e , really unlock.
d97f0 20 2a 2f 0a 20 20 69 66 20 28 20 73 65 6d 5f 70 */. if ( sem_p
d9800 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 20 ost(pSem)==-1 )
d9810 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 74 45 {. int rc, tE
d9820 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d9830 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d9840 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d9850 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d9860 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 IOERR_UNLOCK);.
d9870 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d9880 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
d9890 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d98a0 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d98b0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 }. return rc
d98c0 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e ; . }. pFile->
d98d0 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f locktype = NO_LO
d98e0 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c CK;. return SQL
d98f0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a ITE_OK;.}../*. *
d9900 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a * Close a file..
d9910 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 */.static int s
d9920 65 6d 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f emClose(sqlite3_
d9930 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 file *id) {. if
d9940 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 ( id ){. unix
d9950 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d9960 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
d9970 20 73 65 6d 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e semUnlock(id, N
d9980 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 61 73 73 O_LOCK);. ass
d9990 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
d99a0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
d99b0 28 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c ();. releaseL
d99c0 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 ockInfo(pFile->p
d99d0 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 Lock);. relea
d99e0 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d seOpenCnt(pFile-
d99f0 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 75 6e 69 >pOpen);. uni
d9a00 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
d9a10 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 closeUnixFile
d9a20 28 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 (id);. }. retu
d9a30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
d9a40 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
d9a50 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e WORKS */./*.** N
d9a60 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c amed semaphore l
d9a70 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 ocking is only a
d9a80 76 61 69 6c 61 62 6c 65 20 6f 6e 20 56 78 57 6f vailable on VxWo
d9a90 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a rks..**.********
d9aa0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 ******* End of t
d9ab0 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f he named semapho
d9ac0 72 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e re lock implemen
d9ad0 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tation *********
d9ae0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
d9af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9b30 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a ******/.../*****
d9b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
d9b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9ba0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 41 46 50 20 ***** Begin AFP
d9bb0 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a Locking ********
d9bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
d9be0 41 46 50 20 69 73 20 74 68 65 20 41 70 70 6c 65 AFP is the Apple
d9bf0 20 46 69 6c 69 6e 67 20 50 72 6f 74 6f 63 6f 6c Filing Protocol
d9c00 2e 20 20 41 46 50 20 69 73 20 61 20 6e 65 74 77 . AFP is a netw
d9c10 6f 72 6b 20 66 69 6c 65 73 79 73 74 65 6d 20 66 ork filesystem f
d9c20 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 41 70 70 6c 65 ound.** on Apple
d9c30 20 4d 61 63 69 6e 74 6f 73 68 20 63 6f 6d 70 75 Macintosh compu
d9c40 74 65 72 73 20 2d 20 62 6f 74 68 20 4f 53 39 20 ters - both OS9
d9c50 61 6e 64 20 4f 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 and OSX..**.** T
d9c60 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 6c 65 hird-party imple
d9c70 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 41 46 mentations of AF
d9c80 50 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e P are available.
d9c90 20 20 42 75 74 20 74 68 69 73 20 63 6f 64 65 20 But this code
d9ca0 68 65 72 65 0a 2a 2a 20 6f 6e 6c 79 20 77 6f 72 here.** only wor
d9cb0 6b 73 20 6f 6e 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 ks on OSX..*/..#
d9cc0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 if defined(__APP
d9cd0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
d9ce0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
d9cf0 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 TYLE./*.** The a
d9d00 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
d9d10 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
d9d20 69 6e 73 20 61 6c 6c 20 61 66 70 20 6c 6f 63 6b ins all afp lock
d9d30 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 0a specific state.
d9d40 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
d9d50 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 t afpLockingCont
d9d60 65 78 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f ext afpLockingCo
d9d70 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 61 66 ntext;.struct af
d9d80 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 pLockingContext
d9d90 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e {. unsigned lon
d9da0 67 20 6c 6f 6e 67 20 73 68 61 72 65 64 42 79 74 g long sharedByt
d9db0 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 e;. const char
d9dc0 2a 64 62 50 61 74 68 3b 20 20 20 20 20 20 20 20 *dbPath;
d9dd0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
d9de0 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f the open file */
d9df0 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 42 79 74 65 .};..struct Byte
d9e00 52 61 6e 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20 RangeLockPB2.{.
d9e10 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
d9e20 6f 6e 67 20 6f 66 66 73 65 74 3b 20 20 20 20 20 ong offset;
d9e30 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20 /* offset to
d9e40 66 69 72 73 74 20 62 79 74 65 20 74 6f 20 6c 6f first byte to lo
d9e50 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ck */. unsigned
d9e60 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 long long lengt
d9e70 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 62 72 h; /* nbr
d9e80 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 of bytes to loc
d9e90 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 k */. unsigned
d9ea0 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e long long retRan
d9eb0 67 65 53 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20 geStart; /* nbr
d9ec0 6f 66 20 31 73 74 20 62 79 74 65 20 6c 6f 63 6b of 1st byte lock
d9ed0 65 64 20 69 66 20 73 75 63 63 65 73 73 66 75 6c ed if successful
d9ee0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
d9ef0 68 61 72 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20 har unLockFlag;
d9f00 20 20 20 20 20 20 20 20 2f 2a 20 31 20 3d 20 75 /* 1 = u
d9f10 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20 nlock, 0 = lock
d9f20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
d9f30 61 72 20 73 74 61 72 74 45 6e 64 46 6c 61 67 3b ar startEndFlag;
d9f40 20 20 20 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20 /* 1=rel
d9f50 74 6f 20 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20 to end of fork,
d9f60 30 3d 72 65 6c 20 74 6f 20 73 74 61 72 74 20 2a 0=rel to start *
d9f70 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 /. int fd;
d9f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9f90 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65 /* file de
d9fa0 73 63 20 74 6f 20 61 73 73 6f 63 20 74 68 69 73 sc to assoc this
d9fb0 20 6c 6f 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b lock with */.};
d9fc0 0a 0a 23 64 65 66 69 6e 65 20 61 66 70 66 73 42 ..#define afpfsB
d9fd0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 yteRangeLock2FSC
d9fe0 54 4c 20 20 20 20 20 20 20 20 5f 49 4f 57 52 28 TL _IOWR(
d9ff0 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 'z', 23, struct
da000 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
da010 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 )../*.** This is
da020 20 61 20 75 74 69 6c 69 74 79 20 66 6f 72 20 73 a utility for s
da030 65 74 74 69 6e 67 20 6f 72 20 63 6c 65 61 72 69 etting or cleari
da040 6e 67 20 61 20 62 69 74 2d 72 61 6e 67 65 20 6c ng a bit-range l
da050 6f 63 6b 20 6f 6e 20 61 6e 0a 2a 2a 20 41 46 50 ock on an.** AFP
da060 20 66 69 6c 65 73 79 73 74 65 6d 2e 0a 2a 2a 20 filesystem..**
da070 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
da080 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c E_OK on success,
da090 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 6e 20 SQLITE_BUSY on
da0a0 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 failure..*/.stat
da0b0 69 63 20 69 6e 74 20 61 66 70 53 65 74 4c 6f 63 ic int afpSetLoc
da0c0 6b 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 k(. const char
da0d0 2a 70 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 *path,
da0e0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
da0f0 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 6c 6f he file to be lo
da100 63 6b 65 64 20 6f 72 20 75 6e 6c 6f 63 6b 65 64 cked or unlocked
da110 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a */. unixFile *
da120 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 pFile,
da130 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c /* Open fil
da140 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20 e descriptor on
da150 70 61 74 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e path */. unsign
da160 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 ed long long off
da170 73 65 74 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 set, /* Firs
da180 74 20 62 79 74 65 20 74 6f 20 62 65 20 6c 6f 63 t byte to be loc
da190 6b 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ked */. unsigne
da1a0 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 d long long leng
da1b0 74 68 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 th, /* Numbe
da1c0 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f r of bytes to lo
da1d0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 73 65 74 4c ck */. int setL
da1e0 6f 63 6b 46 6c 61 67 20 20 20 20 20 20 20 20 20 ockFlag
da1f0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
da200 6f 20 73 65 74 20 6c 6f 63 6b 2e 20 20 46 61 6c o set lock. Fal
da210 73 65 20 74 6f 20 63 6c 65 61 72 20 6c 6f 63 6b se to clear lock
da220 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 */.){. struct
da230 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
da240 20 70 62 3b 0a 20 20 69 6e 74 20 65 72 72 3b 0a pb;. int err;.
da250 20 20 0a 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 6c . pb.unLockFl
da260 61 67 20 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 67 ag = setLockFlag
da270 20 3f 20 30 20 3a 20 31 3b 0a 20 20 70 62 2e 73 ? 0 : 1;. pb.s
da280 74 61 72 74 45 6e 64 46 6c 61 67 20 3d 20 30 3b tartEndFlag = 0;
da290 0a 20 20 70 62 2e 6f 66 66 73 65 74 20 3d 20 6f . pb.offset = o
da2a0 66 66 73 65 74 3b 0a 20 20 70 62 2e 6c 65 6e 67 ffset;. pb.leng
da2b0 74 68 20 3d 20 6c 65 6e 67 74 68 3b 20 0a 20 20 th = length; .
da2c0 70 62 2e 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 pb.fd = pFile->h
da2d0 3b 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 36 28 ;. . OSTRACE6(
da2e0 22 41 46 50 53 45 54 4c 4f 43 4b 20 5b 25 73 5d "AFPSETLOCK [%s]
da2f0 20 66 6f 72 20 25 64 25 73 20 69 6e 20 72 61 6e for %d%s in ran
da300 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 5c 6e 22 2c ge %llx:%llx\n",
da310 20 0a 20 20 20 20 28 73 65 74 4c 6f 63 6b 46 6c . (setLockFl
da320 61 67 3f 22 4f 4e 22 3a 22 4f 46 46 22 29 2c 20 ag?"ON":"OFF"),
da330 70 46 69 6c 65 2d 3e 68 2c 20 28 70 62 2e 66 64 pFile->h, (pb.fd
da340 3d 3d 2d 31 3f 22 5b 74 65 73 74 76 61 6c 2d 31 ==-1?"[testval-1
da350 5d 22 3a 22 22 29 2c 0a 20 20 20 20 6f 66 66 73 ]":""),. offs
da360 65 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 65 et, length);. e
da370 72 72 20 3d 20 66 73 63 74 6c 28 70 61 74 68 2c rr = fsctl(path,
da380 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c afpfsByteRangeL
da390 6f 63 6b 32 46 53 43 54 4c 2c 20 26 70 62 2c 20 ock2FSCTL, &pb,
da3a0 30 29 3b 0a 20 20 69 66 20 28 20 65 72 72 3d 3d 0);. if ( err==
da3b0 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 -1 ) {. int r
da3c0 63 3b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e c;. int tErrn
da3d0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 4f o = errno;. O
da3e0 53 54 52 41 43 45 34 28 22 41 46 50 53 45 54 4c STRACE4("AFPSETL
da3f0 4f 43 4b 20 66 61 69 6c 65 64 20 74 6f 20 66 73 OCK failed to fs
da400 63 74 6c 28 29 20 27 25 73 27 20 25 64 20 25 73 ctl() '%s' %d %s
da410 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
da420 20 20 70 61 74 68 2c 20 74 45 72 72 6e 6f 2c 20 path, tErrno,
da430 73 74 72 65 72 72 6f 72 28 74 45 72 72 6e 6f 29 strerror(tErrno)
da440 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
da450 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b _IGNORE_AFP_LOCK
da460 5f 45 52 52 4f 52 53 0a 20 20 20 20 72 63 20 3d _ERRORS. rc =
da470 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 23 65 SQLITE_BUSY;.#e
da480 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 73 71 6c lse. rc = sql
da490 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
da4a0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 0a 20 xError(tErrno,.
da4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
da4c0 20 20 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f setLockFlag ?
da4d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
da4e0 43 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 CK : SQLITE_IOER
da4f0 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69 R_UNLOCK);.#endi
da500 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f f /* SQLITE_IGNO
da510 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f RE_AFP_LOCK_ERRO
da520 52 53 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 RS */. if( IS
da530 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
da540 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
da550 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
da560 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 no;. }. re
da570 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 turn rc;. } els
da580 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 e {. return S
da590 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a QLITE_OK;. }.}.
da5a0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
da5b0 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 ine checks if th
da5c0 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 ere is a RESERVE
da5d0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 D lock held on t
da5e0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 he specified.**
da5f0 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 file by this or
da600 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 any other proces
da610 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 s. If such a loc
da620 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a k is held, set *
da630 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 pResOut.** to a
da640 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f non-zero value o
da650 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 therwise *pResOu
da660 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f t is set to zero
da670 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 . The return va
da680 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f lue.** is set to
da690 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 SQLITE_OK unles
da6a0 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f s an I/O error o
da6b0 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 ccurs during loc
da6c0 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 k checking..*/.s
da6d0 74 61 74 69 63 20 69 6e 74 20 61 66 70 43 68 65 tatic int afpChe
da6e0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
da6f0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
da700 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
da710 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
da720 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 E_OK;. int rese
da730 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 rved = 0;. unix
da740 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
da750 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a nixFile*)id;. .
da760 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
da770 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
da780 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 _IOERR_CHECKRESE
da790 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a RVEDLOCK; );. .
da7a0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
da7b0 29 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 );. afpLockingC
da7c0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 ontext *context
da7d0 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e = (afpLockingCon
da7e0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c text *) pFile->l
da7f0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 ockingContext;.
da800 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 . /* Check if
da810 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 a thread in this
da820 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 process holds s
da830 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 uch a lock */.
da840 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
da850 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
da860 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 ){. reserved
da870 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a = 1;. }. . /*
da880 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 Otherwise see i
da890 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f f some other pro
da8a0 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 cess holds it..
da8b0 20 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 */. if( !rese
da8c0 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c rved ){. /* l
da8d0 6f 63 6b 20 74 68 65 20 52 45 53 45 52 56 45 44 ock the RESERVED
da8e0 20 62 79 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 byte */. int
da8f0 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc = afpSetLoc
da900 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
da910 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 h, pFile, RESERV
da920 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 20 ED_BYTE, 1,1);
da930 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f . if( SQLITE_
da940 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20 OK==lrc ){.
da950 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 65 /* if we succee
da960 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 68 ded in taking th
da970 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c e reserved lock,
da980 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65 unlock it to re
da990 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 74 store. ** t
da9a0 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 he original stat
da9b0 65 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d e */. lrc =
da9c0 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
da9d0 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
da9e0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 le, RESERVED_BYT
da9f0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 E, 1, 0);. }
daa00 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20 else {. /*
daa10 69 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 if we failed to
daa20 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 get the lock the
daa30 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d n someone else m
daa40 75 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 ust have it */.
daa50 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 reserved =
daa60 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
daa70 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
daa80 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d rc) ){. rc=
daa90 6c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 lrc;. }. }.
daaa0 20 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 . OSTRACE4("TE
daab0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
daac0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
daad0 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b , rc, reserved);
daae0 0a 20 20 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d . . *pResOut =
daaf0 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 reserved;. ret
dab00 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
dab10 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 Lock the file w
dab20 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 ith the lock spe
dab30 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 cified by parame
dab40 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f ter locktype - o
dab50 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c ne.** of the fol
dab60 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
dab70 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 (1) SHARED_LOC
dab80 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 K.** (2) RES
dab90 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 ERVED_LOCK.**
daba0 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f (3) PENDING_LO
dabb0 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 CK.** (4) EX
dabc0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a CLUSIVE_LOCK.**.
dabd0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 ** Sometimes whe
dabe0 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 n requesting one
dabf0 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 lock state, add
dac00 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 itional lock sta
dac10 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 tes.** are inser
dac20 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 ted in between.
dac30 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 The locking mig
dac40 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f ht fail on one o
dac50 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 f the later.** t
dac60 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 ransitions leavi
dac70 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 ng the lock stat
dac80 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d e different from
dac90 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 what it started
daca0 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 but.** still sh
dacb0 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e ort of its goal.
dacc0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
dacd0 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 chart shows the
dace0 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 allowed.** trans
dacf0 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 itions and the i
dad00 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 nserted intermed
dad10 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a iate states:.**.
dad20 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d ** UNLOCKED -
dad30 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 > SHARED.** S
dad40 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 HARED -> RESERVE
dad50 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
dad60 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
dad70 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 XCLUSIVE.** R
dad80 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 ESERVED -> (PEND
dad90 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
dada0 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 E.** PENDING
dadb0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a -> EXCLUSIVE.**.
dadc0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
dadd0 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 will only increa
dade0 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 se a lock. Use
dadf0 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c the sqlite3OsUnl
dae00 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 ock().** routine
dae10 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b to lower a lock
dae20 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 ing level..*/.st
dae30 61 74 69 63 20 69 6e 74 20 61 66 70 4c 6f 63 6b atic int afpLock
dae40 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
dae50 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
dae60 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
dae70 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 ITE_OK;. unixFi
dae80 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
dae90 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 xFile*)id;. afp
daea0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
daeb0 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f context = (afpLo
daec0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 ckingContext *)
daed0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
daee0 6e 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 65 ntext;. . asse
daef0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f rt( pFile );. O
daf00 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 STRACE5("LOCK
daf10 20 25 64 20 25 73 20 77 61 73 20 25 73 20 70 69 %d %s was %s pi
daf20 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
daf30 68 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63 6b h,. lock
daf40 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 typeName(locktyp
daf50 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 e), locktypeName
daf60 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 (pFile->locktype
daf70 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 ), getpid());..
daf80 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
daf90 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f already a lock o
dafa0 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d f this type or m
dafb0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 ore restrictive
dafc0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 on the. ** unix
dafd0 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 File, do nothing
dafe0 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 . Don't use the
daff0 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 afp_end_lock: ex
db000 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a it path, as. **
db010 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
db020 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 ) hasn't been ca
db030 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 lled yet.. */.
db040 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
db050 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 type>=locktype )
db060 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 {. OSTRACE3("
db070 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b LOCK %d %s ok
db080 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c (already held)\
db090 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
db0a0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
db0b0 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 eName(locktype))
db0c0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
db0d0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f ITE_OK;. }.. /
db0e0 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
db0f0 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 locking sequence
db100 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f is correct. */
db110 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
db120 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c ->locktype!=NO_L
db130 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d OCK || locktype=
db140 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
db150 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
db160 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b pe!=PENDING_LOCK
db170 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );. assert( lo
db180 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 cktype!=RESERVED
db190 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e _LOCK || pFile->
db1a0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
db1b0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a _LOCK );. . /*
db1c0 20 54 68 69 73 20 6d 75 74 65 78 20 69 73 20 6e This mutex is n
db1d0 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70 46 eeded because pF
db1e0 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 ile->pLock is sh
db1f0 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 ared across thre
db200 61 64 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 ads. */. unixE
db210 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 nterMutex();..
db220 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
db230 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 current thread
db240 6f 77 6e 73 20 74 68 65 20 70 46 69 6c 65 2e 0a owns the pFile..
db250 20 20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e */. rc = tran
db260 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46 sferOwnership(pF
db270 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d ile);. if( rc!=
db280 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
db290 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
db2a0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 );. return rc
db2b0 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20 ;. }. . /*
db2c0 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 A PENDING lock i
db2d0 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 s needed before
db2e0 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 acquiring a SHAR
db2f0 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f ED lock and befo
db300 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e re. ** acquirin
db310 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c g an EXCLUSIVE l
db320 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 ock. For the SH
db330 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 ARED lock, the P
db340 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a ENDING will. **
db350 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 be released..
db360 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
db370 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a e==SHARED_LOCK .
db380 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 || (lockty
db390 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
db3a0 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK && pFile->loc
db3b0 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f ktype<PENDING_LO
db3c0 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 CK). ){. int
db3d0 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 66 61 69 failed;. fai
db3e0 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b led = afpSetLock
db3f0 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
db400 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 , pFile, PENDING
db410 5f 42 59 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 _BYTE, 1, 1);.
db420 20 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a if (failed) {.
db430 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 rc = faile
db440 64 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 d;. goto af
db450 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 p_end_lock;.
db460 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 }. }. . /* If
db470 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f control gets to
db480 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 this point, the
db490 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 n actually go ah
db4a0 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a ead and make. *
db4b0 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 * operating syst
db4c0 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 em calls for the
db4d0 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e specified lock.
db4e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b . */. if( lock
db4f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
db500 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c K ){. int lk,
db510 20 6c 72 63 31 2c 20 6c 72 63 32 2c 20 6c 72 63 lrc1, lrc2, lrc
db520 31 45 72 72 6e 6f 3b 0a 20 20 20 20 0a 20 20 20 1Errno;. .
db530 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 /* Now get the
db540 72 65 61 64 2d 6c 6f 63 6b 20 53 48 41 52 45 44 read-lock SHARED
db550 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 2f 2a 20 _LOCK */. /*
db560 6e 6f 74 65 20 74 68 61 74 20 74 68 65 20 71 75 note that the qu
db570 61 6c 69 74 79 20 6f 66 20 74 68 65 20 72 61 6e ality of the ran
db580 64 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27 74 20 domness doesn't
db590 6d 61 74 74 65 72 20 74 68 61 74 20 6d 75 63 68 matter that much
db5a0 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e */. lk = ran
db5b0 64 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74 dom(); . cont
db5c0 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 20 ext->sharedByte
db5d0 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66 = (lk & 0x7fffff
db5e0 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45 ff)%(SHARED_SIZE
db5f0 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 63 31 20 - 1);. lrc1
db600 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e = afpSetLock(con
db610 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 text->dbPath, pF
db620 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 ile, .
db630 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63 6f 6e SHARED_FIRST+con
db640 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 text->sharedByte
db650 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 28 , 1, 1);. if(
db660 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
db670 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 72 rc1) ){. lr
db680 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c 65 2d c1Errno = pFile-
db690 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 >lastErrno;.
db6a0 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 }. /* Drop th
db6b0 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44 e temporary PEND
db6c0 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ING lock */.
db6d0 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc2 = afpSetLoc
db6e0 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
db6f0 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e h, pFile, PENDIN
db700 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 G_BYTE, 1, 0);.
db710 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 5f 4c . if( IS_L
db720 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 OCK_ERROR(lrc1)
db730 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d ) {. pFile-
db740 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63 >lastErrno = lrc
db750 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 1Errno;. rc
db760 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 20 67 = lrc1;. g
db770 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b oto afp_end_lock
db780 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 ;. } else if(
db790 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
db7a0 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 72 63 rc2) ){. rc
db7b0 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 20 67 = lrc2;. g
db7c0 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b oto afp_end_lock
db7d0 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 ;. } else if(
db7e0 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 45 5f lrc1 != SQLITE_
db7f0 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 63 20 OK ) {. rc
db800 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 65 6c = lrc1;. } el
db810 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 se {. pFile
db820 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 ->locktype = SHA
db830 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 RED_LOCK;.
db840 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c pFile->pOpen->nL
db850 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d ock++;. }. }
db860 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 else{. /* The
db870 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 request was for
db880 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 a RESERVED or E
db890 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 XCLUSIVE lock.
db8a0 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 It is. ** ass
db8b0 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 20 umed that there
db8c0 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 67 is a SHARED or g
db8d0 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 reater lock on t
db8e0 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 he file. ** a
db8f0 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 lready.. */.
db900 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 int failed =
db910 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 0;. assert( 0
db920 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 !=pFile->locktyp
db930 65 20 29 3b 0a 20 20 20 20 69 66 20 28 6c 6f 63 e );. if (loc
db940 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 52 56 45 ktype >= RESERVE
db950 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d D_LOCK && pFile-
db960 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 45 53 45 >locktype < RESE
db970 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 RVED_LOCK) {.
db980 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20 /* Acquire
db990 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
db9a0 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65 */. faile
db9b0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 d = afpSetLock(c
db9c0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
db9d0 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f pFile, RESERVED_
db9e0 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20 20 20 BYTE, 1,1);.
db9f0 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69 6c 65 }. if (!faile
dba00 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20 3d 3d d && locktype ==
dba10 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 EXCLUSIVE_LOCK)
dba20 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63 71 75 {. /* Acqu
dba30 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 ire an EXCLUSIVE
dba40 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 lock */.
dba50 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 . /* Remov
dba60 65 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 e the shared loc
dba70 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 k before trying
dba80 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65 27 6c the range. we'l
dba90 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20 20 20 l need to .
dbaa0 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73 68 20 ** reestablish
dbab0 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 the shared lock
dbac0 69 66 20 77 65 20 63 61 6e 27 74 20 67 65 74 20 if we can't get
dbad0 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 6b 0a 20 the afpUnlock.
dbae0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
dbaf0 28 20 21 28 66 61 69 6c 65 64 20 3d 20 61 66 70 ( !(failed = afp
dbb00 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d SetLock(context-
dbb10 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 >dbPath, pFile,
dbb20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 0a 20 SHARED_FIRST +.
dbb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dbb40 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d context-
dbb50 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 >sharedByte, 1,
dbb60 30 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 0)) ){. i
dbb70 6e 74 20 66 61 69 6c 65 64 32 20 3d 20 53 51 4c nt failed2 = SQL
dbb80 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 ITE_OK;.
dbb90 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20 /* now attemmpt
dbba0 74 6f 20 67 65 74 20 74 68 65 20 65 78 63 6c 75 to get the exclu
dbbb0 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20 sive lock range
dbbc0 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65 */. faile
dbbd0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 d = afpSetLock(c
dbbe0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 ontext->dbPath,
dbbf0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 pFile, SHARED_FI
dbc00 52 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 RST, .
dbc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dbc20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a 45 SHARED_SIZE
dbc30 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 1);. if
dbc40 28 20 66 61 69 6c 65 64 20 26 26 20 28 66 61 69 ( failed && (fai
dbc50 6c 65 64 32 20 3d 20 61 66 70 53 65 74 4c 6f 63 led2 = afpSetLoc
dbc60 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
dbc70 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 h, pFile, .
dbc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dbc90 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b SHARED_FIRST +
dbca0 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 context->shared
dbcb0 42 79 74 65 2c 20 31 2c 20 31 29 29 20 29 7b 0a Byte, 1, 1)) ){.
dbcc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e /* Can
dbcd0 27 74 20 72 65 65 73 74 61 62 6c 69 73 68 20 74 't reestablish t
dbce0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 he shared lock.
dbcf0 20 53 71 6c 69 74 65 20 63 61 6e 27 74 20 64 65 Sqlite can't de
dbd00 61 6c 2c 20 74 68 69 73 20 69 73 0a 20 20 20 20 al, this is.
dbd10 20 20 20 20 20 20 2a 2a 20 61 20 63 72 69 74 69 ** a criti
dbd20 63 61 6c 20 49 2f 4f 20 65 72 72 6f 72 0a 20 20 cal I/O error.
dbd30 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
dbd40 20 20 20 20 20 72 63 20 3d 20 28 28 66 61 69 6c rc = ((fail
dbd50 65 64 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 ed & SQLITE_IOER
dbd60 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 R) == SQLITE_IOE
dbd70 52 52 29 20 3f 20 66 61 69 6c 65 64 32 20 3a 20 RR) ? failed2 :
dbd80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
dbd90 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
dbda0 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 K;. got
dbdb0 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a o afp_end_lock;.
dbdc0 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 } .
dbdd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
dbde0 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20 rc = failed; .
dbdf0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
dbe00 69 66 28 20 66 61 69 6c 65 64 20 29 7b 0a 20 20 if( failed ){.
dbe10 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b rc = failed;
dbe20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 . }. }. .
dbe30 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
dbe40 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K ){. pFile->
dbe50 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
dbe60 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ype;. }else if(
dbe70 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
dbe80 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 SIVE_LOCK ){.
dbe90 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dbea0 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b = PENDING_LOCK;
dbeb0 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65 6e 64 5f . }. .afp_end_
dbec0 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 lock:. unixLeav
dbed0 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 eMutex();. OSTR
dbee0 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE4("LOCK %d
dbef0 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 %s %s\n", pFile
dbf00 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d ->h, locktypeNam
dbf10 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 e(locktype), .
dbf20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 rc==SQLIT
dbf30 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 E_OK ? "ok" : "f
dbf40 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 ailed");. retur
dbf50 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c n rc;.}../*.** L
dbf60 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
dbf70 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
dbf80 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
dbf90 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
dbfa0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
dbfb0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
dbfc0 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
dbfd0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
dbfe0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
dbff0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
dc000 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
dc010 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
dc020 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
dc030 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
dc040 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
dc050 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
dc060 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 afpUnlock(sqlit
dc070 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
dc080 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 locktype) {. i
dc090 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
dc0a0 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 K;. unixFile *p
dc0b0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
dc0c0 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 *)id;. afpLocki
dc0d0 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 ngContext *pCtx
dc0e0 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e = (afpLockingCon
dc0f0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c text *) pFile->l
dc100 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a ockingContext;..
dc110 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 assert( pFile
dc120 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 );. OSTRACE5("U
dc130 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 NLOCK %d %d was
dc140 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %d pid=%d\n", p
dc150 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
dc160 65 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c e,. pFil
dc170 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 e->locktype, get
dc180 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 pid());.. asser
dc190 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 t( locktype<=SHA
dc1a0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 RED_LOCK );. if
dc1b0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
dc1c0 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 e<=locktype ){.
dc1d0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
dc1e0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 _OK;. }. if( C
dc1f0 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 HECK_THREADID(pF
dc200 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ile) ){. retu
dc210 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 rn SQLITE_MISUSE
dc220 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65 ;. }. unixEnte
dc230 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 rMutex();. if(
dc240 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
dc250 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SHARED_LOCK ){.
dc260 20 20 20 0a 20 20 20 20 69 66 28 20 70 46 69 6c . if( pFil
dc270 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 e->locktype==EXC
dc280 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 LUSIVE_LOCK ){.
dc290 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 rc = afpSet
dc2a0 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 Lock(pCtx->dbPat
dc2b0 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 h, pFile, SHARED
dc2c0 5f 46 49 52 53 54 2c 20 53 48 41 52 45 44 5f 53 _FIRST, SHARED_S
dc2d0 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 IZE, 0);. i
dc2e0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
dc2f0 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 && locktype==SH
dc300 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
dc310 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d /* only re-
dc320 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 establish the sh
dc330 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63 ared lock if nec
dc340 65 73 73 61 72 79 20 2a 2f 0a 20 20 20 20 20 20 essary */.
dc350 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b int sharedLock
dc360 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 Byte = SHARED_FI
dc370 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 RST+pCtx->shared
dc380 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 72 63 Byte;. rc
dc390 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 = afpSetLock(pC
dc3a0 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c tx->dbPath, pFil
dc3b0 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 e, sharedLockByt
dc3c0 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 e, 1, 1);.
dc3d0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
dc3e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
dc3f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dc400 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 >=PENDING_LOCK )
dc410 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 {. rc = afp
dc420 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 SetLock(pCtx->db
dc430 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e Path, pFile, PEN
dc440 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 DING_BYTE, 1, 0)
dc450 3b 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28 ;. } . if(
dc460 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
dc470 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 & pFile->locktyp
dc480 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e>=RESERVED_LOCK
dc490 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
dc4a0 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e fpSetLock(pCtx->
dc4b0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 dbPath, pFile, R
dc4c0 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c ESERVED_BYTE, 1,
dc4d0 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 0);. }. }el
dc4e0 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d se if( locktype=
dc4f0 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 =NO_LOCK ){.
dc500 2f 2a 20 63 6c 65 61 72 20 74 68 65 20 73 68 61 /* clear the sha
dc510 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 red lock */.
dc520 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 int sharedLockBy
dc530 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 te = SHARED_FIRS
dc540 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 79 T+pCtx->sharedBy
dc550 74 65 3b 0a 20 20 20 20 72 63 20 3d 20 61 66 70 te;. rc = afp
dc560 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 SetLock(pCtx->db
dc570 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61 Path, pFile, sha
dc580 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20 redLockByte, 1,
dc590 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 0);. }.. if( r
dc5a0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
dc5b0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 if( locktype
dc5c0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ==NO_LOCK ){.
dc5d0 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 struct unixOp
dc5e0 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 enCnt *pOpen = p
dc5f0 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 File->pOpen;.
dc600 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d pOpen->nLock-
dc610 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 -;. assert(
dc620 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 pOpen->nLock>=0
dc630 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f );. if( pO
dc640 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b pen->nLock==0 ){
dc650 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6c . rc = cl
dc660 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46 osePendingFds(pF
dc670 69 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ile);. }.
dc680 20 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 }. }. unixLe
dc690 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 aveMutex();. if
dc6a0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
dc6b0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f ){. pFile->lo
dc6c0 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
dc6d0 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 e;. }. return
dc6e0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f rc;.}../*.** Clo
dc6f0 73 65 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 se a file & clea
dc700 6e 75 70 20 41 46 50 20 73 70 65 63 69 66 69 63 nup AFP specific
dc710 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
dc720 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
dc730 61 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 afpClose(sqlite3
dc740 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 _file *id) {. i
dc750 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 f( id ){. uni
dc760 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
dc770 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
dc780 20 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20 afpUnlock(id,
dc790 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e NO_LOCK);. un
dc7a0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
dc7b0 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 if( pFile->p
dc7c0 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 Open && pFile->p
dc7d0 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 Open->nLock ){.
dc7e0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 /* If there
dc7f0 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 are outstanding
dc800 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 locks, do not a
dc810 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 ctually close th
dc820 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 e file just.
dc830 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 ** yet because
dc840 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 that would clea
dc850 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 r those locks.
dc860 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 Instead, add the
dc870 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 file. ** d
dc880 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 escriptor to pOp
dc890 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49 en->aPending. I
dc8a0 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 t will be automa
dc8b0 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77 tically closed w
dc8c0 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 hen. ** the
dc8d0 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c last lock is cl
dc8e0 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a eared.. */.
dc8f0 20 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 setPending
dc900 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d Fd(pFile);. }
dc910 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e . releaseOpen
dc920 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e Cnt(pFile->pOpen
dc930 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
dc940 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 ree(pFile->locki
dc950 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 ngContext);.
dc960 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 closeUnixFile(id
dc970 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 );. unixLeave
dc980 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 Mutex();. }. r
dc990 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
dc9a0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 .}..#endif /* de
dc9b0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
dc9c0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
dc9d0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
dc9e0 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 */./*.** The cod
dc9f0 65 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 41 e above is the A
dca00 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e FP lock implemen
dca10 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64 tation. The cod
dca20 65 20 69 73 20 73 70 65 63 69 66 69 63 0a 2a 2a e is specific.**
dca30 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64 to MacOSX and d
dca40 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 oes not work on
dca50 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66 other unix platf
dca60 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e orms. No altern
dca70 61 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 61 69 ative.** is avai
dca80 6c 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 20 64 lable. If you d
dca90 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72 on't compile for
dcaa0 20 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 68 65 a mac, then the
dcab0 20 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56 "unix-afp".** V
dcac0 46 53 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 FS is not availa
dcad0 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ble..**.********
dcae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
dcaf0 64 20 6f 66 20 74 68 65 20 41 46 50 20 6c 6f 63 d of the AFP loc
dcb00 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e k implementation
dcb10 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
dcb20 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a *******.********
dcb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcb70 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a ******/.../*****
dcb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcbc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
dcbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c ********** Non-l
dcbe0 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 ocking sqlite3_f
dcbf0 69 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a ile methods ****
dcc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dcc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
dcc20 54 68 65 20 6e 65 78 74 20 64 69 76 69 73 69 6f The next divisio
dcc30 6e 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 n contains imple
dcc40 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 61 mentations for a
dcc50 6c 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 ll methods of th
dcc60 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 e .** sqlite3_fi
dcc70 6c 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 72 20 le object other
dcc80 74 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 than the locking
dcc90 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 20 6c methods. The l
dcca0 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64 ocking.** method
dccb0 73 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 69 s were defined i
dccc0 6e 20 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76 n divisions abov
dccd0 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d e (one locking m
dcce0 65 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76 ethod per.** div
dccf0 69 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d ision). Those m
dcd00 65 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20 ethods that are
dcd10 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f common to all lo
dcd20 63 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61 cking modes.** a
dcd30 72 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68 re gather togeth
dcd40 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76 er into this div
dcd50 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a ision..*/../*.**
dcd60 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 Seek to the off
dcd70 73 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68 set passed as th
dcd80 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
dcd90 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 t, then read cnt
dcda0 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 .** bytes into
dcdb0 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 pBuf. Return the
dcdc0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
dcdd0 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a actually read..
dcde0 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f **.** NB: If yo
dcdf0 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 u define USE_PRE
dce00 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 AD or USE_PREAD6
dce10 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 4, then it might
dce20 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 also.** be nece
dce30 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 ssary to define
dce40 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f _XOPEN_SOURCE to
dce50 20 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 be 500. This v
dce60 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e aries from.** on
dce70 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 e system to anot
dce80 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 her. Since SQLi
dce90 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 te does not defi
dcea0 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 ne USE_PREAD.**
dceb0 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 any any form by
dcec0 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c default, we will
dced0 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 not attempt to
dcee0 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f define _XOPEN_SO
dcef0 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 URCE..** See tic
dcf00 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 kets #2741 and #
dcf10 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 2681..**.** To a
dcf20 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68 void stomping th
dcf30 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e e errno value on
dcf40 20 61 20 66 61 69 6c 65 64 20 72 65 61 64 20 74 a failed read t
dcf50 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c he lastErrno val
dcf60 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66 ue.** is set bef
dcf70 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a ore returning..*
dcf80 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65 /.static int see
dcf90 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c kAndRead(unixFil
dcfa0 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 e *id, sqlite3_i
dcfb0 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69 nt64 offset, voi
dcfc0 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 d *pBuf, int cnt
dcfd0 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 ){. int got;.
dcfe0 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 i64 newOffset;.
dcff0 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 TIMER_START;.#i
dd000 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 f defined(USE_PR
dd010 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 72 65 EAD). got = pre
dd020 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 ad(id->h, pBuf,
dd030 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 cnt, offset);.
dd040 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
dd050 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c got = -1 );.#el
dd060 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 if defined(USE_P
dd070 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20 READ64). got =
dd080 70 72 65 61 64 36 34 28 69 64 2d 3e 68 2c 20 70 pread64(id->h, p
dd090 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
dd0a0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
dd0b0 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
dd0c0 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66 ;.#else. newOff
dd0d0 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e set = lseek(id->
dd0e0 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f h, offset, SEEK_
dd0f0 53 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 SET);. Simulate
dd100 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73 IOError( newOffs
dd110 65 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65 et-- );. if( ne
dd120 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20 wOffset!=offset
dd130 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66 ){. if( newOf
dd140 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 fset == -1 ){.
dd150 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
dd160 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d id)->lastErrno =
dd170 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73 errno;. }els
dd180 65 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 e{. ((unixF
dd190 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
dd1a0 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20 rno = 0;....
dd1b0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b }. return -1;
dd1c0 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61 . }. got = rea
dd1d0 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 d(id->h, pBuf, c
dd1e0 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49 nt);.#endif. TI
dd1f0 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67 MER_END;. if( g
dd200 6f 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e ot<0 ){. ((un
dd210 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
dd220 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
dd230 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 28 22 }. OSTRACE5("
dd240 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64 READ %-3d %5d
dd250 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 %7lld %llu\n",
dd260 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73 id->h, got, offs
dd270 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 et, TIMER_ELAPSE
dd280 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74 D);. return got
dd290 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
dd2a0 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 data from a file
dd2b0 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 into a buffer.
dd2c0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
dd2d0 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 K if all.** byte
dd2e0 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 s were read succ
dd2f0 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c essfully and SQL
dd300 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 ITE_IOERR if any
dd310 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 thing goes.** wr
dd320 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ong..*/.static i
dd330 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73 nt unixRead(. s
dd340 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
dd350 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 . void *pBuf,
dd360 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 . int amt,. sq
dd370 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
dd380 65 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 et.){. unixFile
dd390 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
dd3a0 69 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e 74 20 ile *)id;. int
dd3b0 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69 got;. assert( i
dd3c0 64 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 d );.. /* If th
dd3d0 69 73 20 69 73 20 61 20 64 61 74 61 62 61 73 65 is is a database
dd3e0 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 file (not a jou
dd3f0 72 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 rnal, master-jou
dd400 72 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a rnal or temp. *
dd410 2a 20 66 69 6c 65 29 2c 20 74 68 65 20 62 79 74 * file), the byt
dd420 65 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e es in the lockin
dd430 67 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e g range should n
dd440 65 76 65 72 20 62 65 20 72 65 61 64 20 6f 72 20 ever be read or
dd450 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 written. */. as
dd460 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e sert( pFile->pUn
dd470 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c used==0. |
dd480 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e | offset>=PENDIN
dd490 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20 G_BYTE+512.
dd4a0 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c || offset+amt<
dd4b0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 =PENDING_BYTE .
dd4c0 20 29 3b 0a 0a 20 20 67 6f 74 20 3d 20 73 65 65 );.. got = see
dd4d0 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 kAndRead(pFile,
dd4e0 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d offset, pBuf, am
dd4f0 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61 t);. if( got==a
dd500 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e mt ){. return
dd510 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
dd520 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b lse if( got<0 ){
dd530 0a 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e . /* lastErrn
dd540 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 o set by seekAnd
dd550 52 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 74 75 Read */. retu
dd560 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
dd570 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 READ;. }else{.
dd580 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
dd590 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e 6f 74 20 rno = 0; /* not
dd5a0 61 20 73 79 73 74 65 6d 20 65 72 72 6f 72 20 2a a system error *
dd5b0 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 /. /* Unread
dd5c0 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66 parts of the buf
dd5d0 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f fer must be zero
dd5e0 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d -filled */. m
dd5f0 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 emset(&((char*)p
dd600 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d Buf)[got], 0, am
dd610 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 t-got);. retu
dd620 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
dd630 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a SHORT_READ;. }.
dd640 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f }../*.** Seek to
dd650 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69 the offset in i
dd660 64 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72 d->offset then r
dd670 65 61 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e ead cnt bytes in
dd680 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75 to pBuf..** Retu
dd690 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
dd6a0 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 bytes actually
dd6b0 72 65 61 64 2e 20 20 55 70 64 61 74 65 20 74 68 read. Update th
dd6c0 65 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 e offset..**.**
dd6d0 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e To avoid stompin
dd6e0 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75 g the errno valu
dd6f0 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 77 72 e on a failed wr
dd700 69 74 65 20 74 68 65 20 6c 61 73 74 45 72 72 6e ite the lastErrn
dd710 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 o value.** is se
dd720 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 t before returni
dd730 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
dd740 74 20 73 65 65 6b 41 6e 64 57 72 69 74 65 28 75 t seekAndWrite(u
dd750 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 69 36 34 nixFile *id, i64
dd760 20 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74 20 76 offset, const v
dd770 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 oid *pBuf, int c
dd780 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a nt){. int got;.
dd790 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b i64 newOffset;
dd7a0 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a . TIMER_START;.
dd7b0 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f #if defined(USE_
dd7c0 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 PREAD). got = p
dd7d0 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 write(id->h, pBu
dd7e0 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b f, cnt, offset);
dd7f0 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55 .#elif defined(U
dd800 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f SE_PREAD64). go
dd810 74 20 3d 20 70 77 72 69 74 65 36 34 28 69 64 2d t = pwrite64(id-
dd820 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
dd830 66 66 73 65 74 29 3b 0a 23 65 6c 73 65 0a 20 20 ffset);.#else.
dd840 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 newOffset = lsee
dd850 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c k(id->h, offset,
dd860 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 69 66 SEEK_SET);. if
dd870 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 ( newOffset!=off
dd880 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e set ){. if( n
dd890 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 ewOffset == -1 )
dd8a0 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 {. ((unixFi
dd8b0 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 le*)id)->lastErr
dd8c0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
dd8d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75 }else{. ((u
dd8e0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 nixFile*)id)->la
dd8f0 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a stErrno = 0;....
dd900 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
dd910 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d -1;. }. got =
dd920 20 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 write(id->h, pB
dd930 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 uf, cnt);.#endif
dd940 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 . TIMER_END;.
dd950 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 if( got<0 ){.
dd960 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
dd970 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
dd980 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52 rno;. }.. OSTR
dd990 41 43 45 35 28 22 57 52 49 54 45 20 20 20 25 2d ACE5("WRITE %-
dd9a0 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 3d %5d %7lld %ll
dd9b0 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 u\n", id->h, got
dd9c0 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f , offset, TIMER_
dd9d0 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 ELAPSED);. retu
dd9e0 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a rn got;.}.../*.*
dd9f0 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f * Write data fro
dda00 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 m a buffer into
dda10 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 a file. Return
dda20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
dda30 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 cess.** or some
dda40 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 other error code
dda50 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a on failure..*/.
dda60 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57 static int unixW
dda70 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f rite(. sqlite3_
dda80 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e file *id, . con
dda90 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a st void *pBuf, .
ddaa0 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c int amt,. sql
ddab0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 ite3_int64 offse
ddac0 74 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 t .){. unixFile
ddad0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
ddae0 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77 ile*)id;. int w
ddaf0 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 rote = 0;. asse
ddb00 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 rt( id );. asse
ddb10 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20 rt( amt>0 );..
ddb20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 /* If this is a
ddb30 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e database file (n
ddb40 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 ot a journal, ma
ddb50 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 ster-journal or
ddb60 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c temp. ** file),
ddb70 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 the bytes in th
ddb80 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 e locking range
ddb90 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 should never be
ddba0 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e read or written.
ddbb0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 */. assert( pF
ddbc0 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a ile->pUnused==0.
ddbd0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 || offset
ddbe0 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 >=PENDING_BYTE+5
ddbf0 31 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 12. || off
ddc00 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 set+amt<=PENDING
ddc10 5f 42 59 54 45 20 0a 20 20 29 3b 0a 0a 23 69 66 _BYTE . );..#if
ddc20 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a ndef NDEBUG. /*
ddc30 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 If we are doing
ddc40 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 a normal write
ddc50 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69 to a database fi
ddc60 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 le (as opposed t
ddc70 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68 o. ** doing a h
ddc80 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
ddc90 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74 ack or a write t
ddca0 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65 o some file othe
ddcb0 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f r than a. ** no
ddcc0 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 rmal database fi
ddcd0 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20 le) then record
ddce0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 the fact that th
ddcf0 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 e database. **
ddd00 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66 has changed. If
ddd10 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
ddd20 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69 counter is modi
ddd30 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61 fied, record tha
ddd40 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e t. ** fact too.
ddd50 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
ddd60 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 e->inNormalWrite
ddd70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 ){. pFile->d
ddd80 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a bUpdate = 1; /*
ddd90 20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61 The database ha
ddda0 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 s been modified
dddb0 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65 */. if( offse
dddc0 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b t<=24 && offset+
dddd0 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20 amt>=27 ){.
ddde0 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63 int rc;. c
dddf0 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a har oldCntr[4];.
dde00 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f SimulateIO
dde10 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a ErrorBenign(1);.
dde20 20 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41 rc = seekA
dde30 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34 ndRead(pFile, 24
dde40 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20 , oldCntr, 4);.
dde50 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
dde60 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 rrorBenign(0);.
dde70 20 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c if( rc!=4 |
dde80 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72 | memcmp(oldCntr
dde90 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 , &((char*)pBuf)
ddea0 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21 [24-offset], 4)!
ddeb0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 =0 ){. pF
ddec0 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 ile->transCntrCh
dded0 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20 ng = 1; /* The
ddee0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e transaction coun
ddef0 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 ter has changed
ddf00 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d */. }. }
ddf10 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77 . }.#endif.. w
ddf20 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 hile( amt>0 && (
ddf30 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 wrote = seekAndW
ddf40 72 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 66 73 rite(pFile, offs
ddf50 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 29 3e et, pBuf, amt))>
ddf60 30 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 0 ){. amt -=
ddf70 77 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66 73 65 wrote;. offse
ddf80 74 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 t += wrote;.
ddf90 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 pBuf = &((char*)
ddfa0 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 pBuf)[wrote];.
ddfb0 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 }. SimulateIOEr
ddfc0 72 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d 31 29 ror(( wrote=(-1)
ddfd0 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 53 69 , amt=1 ));. Si
ddfe0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
ddff0 72 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c 20 61 ror(( wrote=0, a
de000 6d 74 3d 31 20 29 29 3b 0a 20 20 69 66 28 20 61 mt=1 ));. if( a
de010 6d 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 mt>0 ){. if(
de020 77 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20 20 20 wrote<0 ){.
de030 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65 /* lastErrno se
de040 74 20 62 79 20 73 65 65 6b 41 6e 64 57 72 69 74 t by seekAndWrit
de050 65 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 e */. retur
de060 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 n SQLITE_IOERR_W
de070 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b RITE;. }else{
de080 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
de090 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 stErrno = 0; /*
de0a0 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 not a system err
de0b0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 or */. retu
de0c0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a rn SQLITE_FULL;.
de0d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
de0e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
de0f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
de100 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 EST./*.** Count
de110 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 the number of fu
de120 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d llsyncs and norm
de130 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 al syncs. This
de140 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a is used to test.
de150 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e ** that syncs an
de160 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 d fullsyncs are
de170 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65 occurring at the
de180 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f right times..*/
de190 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
de1a0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 sqlite3_sync_cou
de1b0 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 nt = 0;.SQLITE_A
de1c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 PI int sqlite3_f
de1d0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 ullsync_count =
de1e0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
de1f0 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74 We do not trust
de200 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76 systems to prov
de210 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64 ide a working fd
de220 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65 atasync(). Some
de230 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64 do..** Others d
de240 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66 o no. To be saf
de250 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b e, we will stick
de260 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 65 with the (slowe
de270 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 49 r) fsync()..** I
de280 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 20 f you know that
de290 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 73 your system does
de2a0 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 79 support fdatasy
de2b0 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c 0a nc() correctly,.
de2c0 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 63 ** then simply c
de2d0 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 64 ompile with -Dfd
de2e0 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 6e atasync=fdatasyn
de2f0 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 c.*/.#if !define
de300 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 20 d(fdatasync) &&
de310 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 !defined(__linux
de320 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 61 __).# define fda
de330 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e tasync fsync.#en
de340 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e dif../*.** Defin
de350 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 e HAVE_FULLFSYNC
de360 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e to 0 or 1 depen
de370 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 ding on whether
de380 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f or not.** the F_
de390 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20 FULLFSYNC macro
de3a0 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46 is defined. F_F
de3b0 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72 ULLFSYNC is curr
de3c0 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76 ently.** only av
de3d0 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f ailable on Mac O
de3e0 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63 S X. But that c
de3f0 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a ould change..*/.
de400 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59 #ifdef F_FULLFSY
de410 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 NC.# define HAVE
de420 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c _FULLFSYNC 1.#el
de430 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45 se.# define HAVE
de440 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e _FULLFSYNC 0.#en
de450 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 dif.../*.** The
de460 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63 fsync() system c
de470 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 all does not wor
de480 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20 k as advertised
de490 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20 on many.** unix
de4a0 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f systems. The fo
de4b0 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72 llowing procedur
de4c0 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 e is an attempt
de4d0 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f to make.** it wo
de4e0 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a rk better..**.**
de4f0 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 The SQLITE_NO_S
de500 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c YNC macro disabl
de510 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e es all fsync()s.
de520 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c This is useful
de530 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20 .** for testing
de540 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20 when we want to
de550 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 run through the
de560 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b test suite quick
de570 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73 ly..** You are s
de580 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20 trongly advised
de590 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20 *not* to deploy
de5a0 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 with SQLITE_NO_S
de5b0 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 YNC.** enabled,
de5c0 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77 however, since w
de5d0 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 ith SQLITE_NO_SY
de5e0 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f NC enabled, an O
de5f0 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f S crash.** or po
de600 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c wer failure will
de610 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20 likely corrupt
de620 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
de630 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 e..**.** SQLite
de640 73 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c sets the dataOnl
de650 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 69 y flag if the si
de660 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 ze of the file i
de670 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 s unchanged..**
de680 54 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20 The idea behind
de690 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74 dataOnly is that
de6a0 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 it should only
de6b0 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63 write the file c
de6c0 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 73 ontent.** to dis
de6d0 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65 k, not the inode
de6e0 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64 . We only set d
de6f0 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66 ataOnly if the f
de700 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a 20 ile size is .**
de710 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20 unchanged since
de720 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 the file size is
de730 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 6f part of the ino
de740 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a 2a de. However, .*
de750 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c 73 * Ted Ts'o tells
de760 20 75 73 20 74 68 61 74 20 66 64 61 74 61 73 79 us that fdatasy
de770 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 77 nc() will also w
de780 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 69 rite the inode i
de790 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 69 f the.** file si
de7a0 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 ze has changed.
de7b0 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 64 The only real d
de7c0 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
de7d0 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a 2a n fdatasync().**
de7e0 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 65 and fsync(), Te
de7f0 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 74 d tells us, is t
de800 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 20 hat fdatasync()
de810 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 74 will not flush t
de820 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 74 he.** inode if t
de830 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e 65 he mtime or owne
de840 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 65 r or other inode
de850 20 61 74 74 72 69 62 75 74 65 73 20 68 61 76 65 attributes have
de860 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 20 changed..** We
de870 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 20 only care about
de880 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 6e the file size, n
de890 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 6c ot the other fil
de8a0 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 6f e attributes, so
de8b0 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 51 .** as far as SQ
de8c0 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e 65 Lite is concerne
de8d0 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 28 d, an fdatasync(
de8e0 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 71 ) is always adeq
de8f0 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 20 uate..** So, we
de900 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 61 always use fdata
de910 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 20 sync() if it is
de920 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 72 available, regar
de930 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 20 dless of.** the
de940 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 value of the dat
de950 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a 73 aOnly flag..*/.s
de960 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66 tatic int full_f
de970 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74 sync(int fd, int
de980 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64 fullSync, int d
de990 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20 ataOnly){. int
de9a0 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f rc;.. /* The fo
de9b0 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65 llowing "ifdef/e
de9c0 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b lif/else/" block
de9d0 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74 has the same st
de9e0 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20 ructure as. **
de9f0 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49 the one below. I
dea00 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20 t is replicated
dea10 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61 here solely to a
dea20 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20 void cluttering
dea30 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61 . ** up the rea
dea40 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 l code with the
dea50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
dea60 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a () macros.. */.
dea70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f #ifdef SQLITE_NO
dea80 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 _SYNC. UNUSED_P
dea90 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20 ARAMETER(fd);.
deaa0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
deab0 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e (fullSync);. UN
deac0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 USED_PARAMETER(d
dead0 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20 ataOnly);.#elif
deae0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20 HAVE_FULLFSYNC.
deaf0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
deb00 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c R(dataOnly);.#el
deb10 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
deb20 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b METER(fullSync);
deb30 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
deb40 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 TER(dataOnly);.#
deb50 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f endif.. /* Reco
deb60 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rd the number of
deb70 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 64 times that we d
deb80 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 o a normal fsync
deb90 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c () and . ** FUL
deba0 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20 LSYNC. This is
debb0 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 used during test
debc0 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 ing to verify th
debd0 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 at this procedur
debe0 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c e. ** gets call
debf0 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 ed with the corr
dec00 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 ect arguments..
dec10 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
dec20 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 6c E_TEST. if( ful
dec30 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 5f lSync ) sqlite3_
dec40 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b fullsync_count++
dec50 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 ;. sqlite3_sync
dec60 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 _count++;.#endif
dec70 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d .. /* If we com
dec80 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 piled with the S
dec90 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c QLITE_NO_SYNC fl
deca0 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 ag, then syncing
decb0 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 is a. ** no-op
decc0 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c . */.#ifdef SQL
decd0 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63 ITE_NO_SYNC. rc
dece0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 = SQLITE_OK;.#e
decf0 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 lif HAVE_FULLFSY
ded00 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 6e NC. if( fullSyn
ded10 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 63 c ){. rc = fc
ded20 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53 ntl(fd, F_FULLFS
ded30 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 YNC, 0);. }else
ded40 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 {. rc = 1;.
ded50 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 55 }. /* If the FU
ded60 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c 20 LLFSYNC failed,
ded70 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 74 fall back to att
ded80 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e 63 empting an fsync
ded90 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f 75 ().. ** It shou
deda0 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c ldn't be possibl
dedb0 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 20 e for fullfsync
dedc0 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 6c to fail on the l
dedd0 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 ocal . ** file
dede0 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 2c system (on OSX),
dedf0 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 69 so failure indi
dee00 63 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c 46 cates that FULLF
dee10 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 20 SYNC. ** isn't
dee20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 68 supported for th
dee30 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20 is file system.
dee40 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 66 So, attempt an f
dee50 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 28 sync . ** and (
dee60 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 20 for now) ignore
dee70 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20 the overhead of
dee80 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 63 a superfluous fc
dee90 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a 2a ntl call. . **
deea0 20 49 74 27 64 20 62 65 20 62 65 74 74 65 72 20 It'd be better
deeb0 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 73 to detect fullfs
deec0 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 65 ync support once
deed0 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a 2a and avoid . **
deee0 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c 20 the fcntl call
deef0 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 20 every time sync
def00 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f 0a is called.. */.
def10 20 20 69 66 28 20 72 63 20 29 20 72 63 20 3d 20 if( rc ) rc =
def20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c 73 fsync(fd);..#els
def30 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73 e . rc = fdatas
def40 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 5f ync(fd);.#if OS_
def50 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 VXWORKS. if( rc
def60 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d 45 ==-1 && errno==E
def70 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 63 NOTSUP ){. rc
def80 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 = fsync(fd);.
def90 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 }.#endif /* OS_V
defa0 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 66 XWORKS */.#endif
defb0 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 45 /* ifdef SQLITE
defc0 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 41 _NO_SYNC elif HA
defd0 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a VE_FULLFSYNC */.
defe0 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 4b . if( OS_VXWORK
deff0 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b 0a S && rc!= -1 ){.
df000 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 0a rc = 0;. }.
df010 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
df020 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
df030 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 all writes to a
df040 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 particular file
df050 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f are committed to
df060 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 disk..**.** If
df070 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e dataOnly==0 then
df080 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 69 both the file i
df090 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d 65 tself and its me
df0a0 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a 20 tadata (file.**
df0b0 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 6d size, access tim
df0c0 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e 63 e, etc) are sync
df0d0 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c 79 ed. If dataOnly
df0e0 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 !=0 then only th
df0f0 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 69 e.** file data i
df100 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 s synced..**.**
df110 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f Under Unix, also
df120 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
df130 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e the directory en
df140 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 try for the file
df150 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65 .** has been cre
df160 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e ated by fsync-in
df170 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 g the directory
df180 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
df190 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 65 e file..** If we
df1a0 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20 do not do this
df1b0 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 and we encounter
df1c0 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 a power failure
df1d0 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a , the directory.
df1e0 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ** entry for the
df1f0 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e journal might n
df200 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 20 77 ot exist after w
df210 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 6e e reboot. The n
df220 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f ext.** SQLite to
df230 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 access the file
df240 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74 will not know t
df250 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
df260 65 78 69 73 74 73 20 28 62 65 63 61 75 73 65 0a exists (because.
df270 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 ** the directory
df280 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a entry for the j
df290 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 72 ournal was never
df2a0 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 68 created) and th
df2b0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a e transaction.**
df2c0 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62 will not roll b
df2d0 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 6c ack - possibly l
df2e0 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 eading to databa
df2f0 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a se corruption..*
df300 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
df310 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 xSync(sqlite3_fi
df320 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 le *id, int flag
df330 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 s){. int rc;.
df340 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
df350 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
df360 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f 6e .. int isDataOn
df370 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c 49 ly = (flags&SQLI
df380 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 TE_SYNC_DATAONLY
df390 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c 73 );. int isFulls
df3a0 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 30 ync = (flags&0x0
df3b0 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f F)==SQLITE_SYNC_
df3c0 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 FULL;.. /* Chec
df3d0 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51 k that one of SQ
df3e0 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
df3f0 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73 or FULL was pas
df400 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 sed */. assert(
df410 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 (flags&0x0F)==SQ
df420 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
df430 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73 . || (flags
df440 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 &0x0F)==SQLITE_S
df450 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20 YNC_FULL. );..
df460 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 2c /* Unix cannot,
df470 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 6d but some system
df480 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c s may return SQL
df490 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 65 ITE_FULL from he
df4a0 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c 69 re. This. ** li
df4b0 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 68 ne is to test th
df4c0 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 73 at doing so does
df4d0 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 70 not cause any p
df4e0 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20 roblems.. */.
df4f0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
df500 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
df510 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20 LITE_FULL );..
df520 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
df530 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 4e . OSTRACE2("SYN
df540 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 C %-3d\n", pF
df550 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d 20 ile->h);. rc =
df560 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 full_fsync(pFile
df570 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 2c ->h, isFullsync,
df580 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 20 isDataOnly);.
df590 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
df5a0 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 rc=1 );. if( r
df5b0 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e c ){. pFile->
df5c0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
df5d0 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 o;. return SQ
df5e0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43 LITE_IOERR_FSYNC
df5f0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c ;. }. if( pFil
df600 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 e->dirfd>=0 ){.
df610 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20 int err;.
df620 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e OSTRACE4("DIRSYN
df630 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c C %-3d (have_ful
df640 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79 lfsync=%d fullsy
df650 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 nc=%d)\n", pFile
df660 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20 ->dirfd,.
df670 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53 HAVE_FULLFS
df680 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29 YNC, isFullsync)
df690 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
df6a0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 _DISABLE_DIRSYNC
df6b0 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65 . /* The dire
df6c0 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e ctory sync is on
df6d0 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20 ly attempted if
df6e0 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20 full_fsync is.
df6f0 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20 ** turned off
df700 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20 or unavailable.
df710 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63 If a full_fsync
df720 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c occurred above,
df730 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 . ** then the
df740 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 directory sync
df750 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a is superfluous..
df760 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 */. if( (
df770 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 !HAVE_FULLFSYNC
df780 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20 || !isFullsync)
df790 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 && full_fsync(pF
df7a0 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20 ile->dirfd,0,0)
df7b0 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 ){. /*.
df7c0 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72 ** We have r
df7d0 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65 eceived multiple
df7e0 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e reports of fsyn
df7f0 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20 c() returning.
df800 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77 ** errors w
df810 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64 hen applied to d
df820 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65 irectories on ce
df830 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65 rtain file syste
df840 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20 ms.. ** A
df850 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79 failed directory
df860 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62 sync is not a b
df870 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20 ig deal. So it
df880 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20 seems. **
df890 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65 better to ignore
df8a0 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63 the error. Tic
df8b0 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20 ket #1657.
df8c0 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 46 */. /* pF
df8d0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
df8e0 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20 errno; */.
df8f0 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 /* return SQLI
df900 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20 TE_IOERR; */.
df910 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 72 }.#endif. er
df920 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d r = close(pFile-
df930 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79 >dirfd); /* Only
df940 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e need to sync on
df950 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65 ce, so close the
df960 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 3d */. if( err=
df970 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 =0 ){
df980 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 20 /* directory
df990 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65 when we are done
df9a0 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 2d */. pFile-
df9b0 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 20 >dirfd = -1;.
df9c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 }else{. pF
df9d0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
df9e0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 errno;. rc
df9f0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
dfa00 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 7d DIR_CLOSE;. }
dfa10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
dfa20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 ;.}../*.** Trunc
dfa30 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 ate an open file
dfa40 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20 to a specified
dfa50 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69 size.*/.static i
dfa60 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28 nt unixTruncate(
dfa70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
dfa80 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 , i64 nByte){.
dfa90 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
dfaa0 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c 61 ( id );. Simula
dfab0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
dfac0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 n SQLITE_IOERR_T
dfad0 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20 RUNCATE );. rc
dfae0 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 6e = ftruncate(((un
dfaf0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 ixFile*)id)->h,
dfb00 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20 (off_t)nByte);.
dfb10 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 28 if( rc ){. (
dfb20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e (unixFile*)id)->
dfb30 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
dfb40 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 o;. return SQ
dfb50 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 LITE_IOERR_TRUNC
dfb60 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ATE;. }else{.
dfb70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
dfb80 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a OK;. }.}../*.**
dfb90 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 Determine the c
dfba0 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 urrent size of a
dfbb0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a file in bytes.*
dfbc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
dfbd0 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 xFileSize(sqlite
dfbe0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 3_file *id, i64
dfbf0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 *pSize){. int r
dfc00 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 c;. struct stat
dfc10 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 buf;. assert(
dfc20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74 id );. rc = fst
dfc30 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 at(((unixFile*)i
dfc40 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 d)->h, &buf);.
dfc50 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
dfc60 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 rc=1 );. if( r
dfc70 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e c!=0 ){. ((un
dfc80 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
dfc90 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
dfca0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
dfcb0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 E_IOERR_FSTAT;.
dfcc0 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75 }. *pSize = bu
dfcd0 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a f.st_size;.. /*
dfce0 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 When opening a
dfcf0 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61 zero-size databa
dfd00 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b se, the findLock
dfd10 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65 Info() procedure
dfd20 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73 . ** writes a s
dfd30 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20 ingle byte into
dfd40 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64 that file in ord
dfd50 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e er to work aroun
dfd60 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20 d a bug. ** in
dfd70 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 the OS-X msdos f
dfd80 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f ilesystem. In o
dfd90 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72 rder to avoid pr
dfda0 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65 oblems with uppe
dfdb0 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77 r. ** layers, w
dfdc0 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74 e need to report
dfdd0 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20 this file size
dfde0 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f as zero even tho
dfdf0 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72 ugh it is. ** r
dfe00 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65 eally 1. Ticke
dfe10 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 t #3260.. */.
dfe20 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20 if( *pSize==1 )
dfe30 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 *pSize = 0;...
dfe40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
dfe50 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f ;.}..#if SQLITE_
dfe60 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
dfe70 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 TYLE && defined(
dfe80 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a __APPLE__)./*.**
dfe90 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f Handler for pro
dfea0 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d xy-locking file-
dfeb0 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20 control verbs.
dfec0 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e Defined below in
dfed0 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 the.** proxying
dfee0 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f locking divisio
dfef0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 n..*/.static int
dff00 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f proxyFileContro
dff10 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c l(sqlite3_file*,
dff20 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 int,void*);.#end
dff30 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 if.../*.** Infor
dff40 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 mation and contr
dff50 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 ol of an open fi
dff60 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 le handle..*/.st
dff70 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c atic int unixFil
dff80 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
dff90 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f _file *id, int o
dffa0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a p, void *pArg){.
dffb0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
dffc0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
dffd0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a FCNTL_LOCKSTATE:
dffe0 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 {. *(int*)
dfff0 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c pArg = ((unixFil
e0000 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 e*)id)->locktype
e0010 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
e0020 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
e0030 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
e0040 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 LAST_ERRNO: {.
e0050 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 *(int*)pArg
e0060 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = ((unixFile*)id
e0070 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 )->lastErrno;.
e0080 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e0090 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e E_OK;. }.#ifn
e00a0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f def NDEBUG. /
e00b0 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c * The pager call
e00c0 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f s this method to
e00d0 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 signal that it
e00e0 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 has done. **
e00f0 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 a rollback and t
e0100 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
e0110 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e is therefore un
e0120 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20 changed and.
e0130 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69 ** it hence it i
e0140 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61 s OK for the tra
e0150 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20 nsaction change
e0160 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20 counter to be.
e0170 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a ** unchanged..
e0180 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 */. case
e0190 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f SQLITE_FCNTL_DB_
e01a0 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20 UNCHANGED: {.
e01b0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
e01c0 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 d)->dbUpdate = 0
e01d0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
e01e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
e01f0 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
e0200 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
e0210 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
e0220 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 d(__APPLE__).
e0230 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 case SQLITE_SET
e0240 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a _LOCKPROXYFILE:.
e0250 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
e0260 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c GET_LOCKPROXYFIL
e0270 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 E: {. retur
e0280 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 n proxyFileContr
e0290 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a ol(id,op,pArg);.
e02a0 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.#endif /*
e02b0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
e02c0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 CKING_STYLE && d
e02d0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f efined(__APPLE__
e02e0 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 ) */. }. retur
e02f0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
e0300 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
e0310 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
e0320 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 in bytes of the
e0330 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b underlying block
e0340 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 device for.** t
e0350 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c he specified fil
e0360 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 e. This is almos
e0370 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 t always 512 byt
e0380 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a es, but may be.*
e0390 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d * larger for som
e03a0 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a e devices..**.**
e03b0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 SQLite code ass
e03c0 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 umes this functi
e03d0 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 on cannot fail.
e03e0 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 It also assumes
e03f0 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 that.** if two f
e0400 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 iles are created
e0410 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c in the same fil
e0420 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f e-system directo
e0430 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 ry (i.e..** a da
e0440 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a tabase and its j
e0450 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 ournal file) tha
e0460 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a t the sector siz
e0470 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a e will be the.**
e0480 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a same for both..
e0490 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e */.static int un
e04a0 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c ixSectorSize(sql
e04b0 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
e04c0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
e04d0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e04e0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
e04f0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
e0500 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 _SIZE;.}../*.**
e0510 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 Return the devic
e0520 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
e0530 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 s for the file.
e0540 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 This is always 0
e0550 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 for unix..*/.st
e0560 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76 atic int unixDev
e0570 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
e0580 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
e0590 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 *NotUsed){. UNU
e05a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
e05b0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
e05c0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 0;.}../*.** Her
e05d0 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65 e ends the imple
e05e0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c mentation of all
e05f0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 sqlite3_file me
e0600 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a thods..**.******
e0610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0620 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c End sqlite3_fil
e0630 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a e Methods ******
e0640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0650 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
e0660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e0690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e06a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
e06b0 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 This division c
e06c0 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 ontains definiti
e06d0 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 ons of sqlite3_i
e06e0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
e06f0 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d s that.** implem
e0700 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65 ent various file
e0710 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
e0720 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f ies. It also co
e0730 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f ntains definitio
e0740 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72 ns.** of "finder
e0750 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20 " functions. A
e0760 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
e0770 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 is used to locat
e0780 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 e the appropriat
e0790 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f e.** sqlite3_io_
e07a0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66 methods object f
e07b0 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 or a particular
e07c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
e07d0 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20 The pAppData.**
e07e0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c field of the sql
e07f0 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a ite3_vfs VFS obj
e0800 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c ects are initial
e0810 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74 ized to be point
e0820 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f ers to.** the co
e0830 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e rrect finder-fun
e0840 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56 ction for that V
e0850 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 FS..**.** Most f
e0860 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 inder functions
e0870 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
e0880 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 to a fixed sqli
e0890 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a te3_io_methods.*
e08a0 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f * object. The o
e08b0 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20 nly interesting
e08c0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
e08d0 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e is autolockIoFin
e08e0 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f der, which.** lo
e08f0 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 oks at the files
e0900 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74 ystem type and t
e0910 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68 ries to guess th
e0920 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a e best locking.*
e0930 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20 * strategy from
e0940 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 that..**.** For
e0950 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46 finder-funtion F
e0960 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72 , two objects ar
e0970 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a e created:.**.**
e0980 20 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c (1) The real
e0990 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
e09a0 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22 named "FImpt()"
e09b0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41 ..**.** (2) A
e09c0 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65 constant pointe
e09d0 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 r to this functi
e09e0 6f 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 on named just "F
e09f0 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f "..**.**.** A po
e0a00 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 inter to the F p
e0a10 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 ointer is used a
e0a20 73 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 s the pAppData v
e0a30 61 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 alue for VFS.**
e0a40 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 objects. We hav
e0a50 65 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 e to do this ins
e0a60 74 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 tead of letting
e0a70 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a pAppData point.*
e0a80 2a 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 * directly at th
e0a90 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f e finder-functio
e0aa0 6e 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 n since C90 rule
e0ab0 73 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 s prevent a void
e0ac0 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 *.** from be cas
e0ad0 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f t into a functio
e0ae0 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a n pointer..**.**
e0af0 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 .** Each instanc
e0b00 65 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 e of this macro
e0b10 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 generates two ob
e0b20 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a jects:.**.** *
e0b30 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c A constant sql
e0b40 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e0b50 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 object call METH
e0b60 4f 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b OD that has lock
e0b70 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 ing.** meth
e0b80 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c ods CLOSE, LOCK,
e0b90 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f UNLOCK, CKRESLO
e0ba0 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 CK..**.** * A
e0bb0 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e n I/O method fin
e0bc0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c der function cal
e0bd0 6c 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 led FINDER that
e0be0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
e0bf0 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 r.** to the
e0c00 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 METHOD object i
e0c10 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 n the previous b
e0c20 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e ullet..*/.#defin
e0c30 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 e IOMETHODS(FIND
e0c40 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 ER, METHOD, CLOS
e0c50 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c E, LOCK, UNLOCK,
e0c60 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 CKLOCK)
e0c70 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 \.static
e0c80 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e0c90 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 _methods METHOD
e0ca0 3d 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 = {
e0cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0cc0 20 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 \. 1,
e0cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0ce0 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 /* iVersi
e0cf0 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 on */
e0d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0d10 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c \. CLOSE,
e0d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0d30 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 /* xClose
e0d40 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e0d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0d60 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61 \. unixRea
e0d70 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
e0d80 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f /* xRead */
e0d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0db0 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 \. unixWrit
e0dc0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
e0dd0 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f /* xWrite */
e0de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0e00 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 \. unixTrunc
e0e10 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ate,
e0e20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 /* xTruncate
e0e30 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e0e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0e50 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 \. unixSync,
e0e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0e70 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 /* xSync */
e0e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0ea0 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a \. unixFileSiz
e0eb0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
e0ec0 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f /* xFileSize */
e0ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e0ef0 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 . LOCK,
e0f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0f10 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 /* xLock */
e0f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e0f40 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 UNLOCK,
e0f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e0f60 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 * xUnlock */
e0f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e0f80 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e0f90 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 CKLOCK,
e0fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e0fb0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
e0fc0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ock */
e0fd0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e0fe0 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c unixFileControl
e0ff0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
e1000 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 xFileControl */
e1010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1020 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e1030 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 unixSectorSize,
e1040 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e1050 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 SectorSize */
e1060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1070 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 \. u
e1080 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 nixDeviceCharact
e1090 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 eristics /* xD
e10a0 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 eviceCapabilitie
e10b0 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 s */
e10c0 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 \.};
e10d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e10e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e10f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1110 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 \.static
e1120 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e1130 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 o_methods *FINDE
e1140 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 R##Impl(const ch
e1150 61 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 20 ar *z, unixFile
e1160 2a 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 53 45 *p){ \. UNUSE
e1170 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 D_PARAMETER(z);
e1180 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e1190 28 70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 (p);
e11a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e11b0 20 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e \. return
e11c0 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 &METHOD;
e11d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e11e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e11f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1200 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 \.}
e1210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1250 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e \.static con
e1260 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e1270 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 thods *(*const F
e1280 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 INDER)(const cha
e1290 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 29 20 r*,unixFile *p)
e12a0 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 \. = FINDE
e12b0 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 R##Impl;../*.**
e12c0 48 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 Here are all of
e12d0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d the sqlite3_io_m
e12e0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 ethods objects f
e12f0 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a or each of the.*
e1300 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 * locking strate
e1310 67 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 gies. Functions
e1320 20 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 that return poi
e1330 6e 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d nters to these m
e1340 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c ethods.** are al
e1350 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 so created..*/.I
e1360 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 OMETHODS(. posi
e1370 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 xIoFinder,
e1380 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
e1390 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
e13a0 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 . posixIoMethod
e13b0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
e13c0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e13d0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
e13e0 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 /. unixClose,
e13f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e1400 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
e1410 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 /. unixLock,
e1420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e1430 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
e1440 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 . unixUnlock,
e1450 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e1460 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
e1470 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 /. unixCheckRes
e1480 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a ervedLock /*
e1490 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
e14a0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
e14b0 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c IOMETHODS(. nol
e14c0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 ockIoFinder,
e14d0 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 /* Finder
e14e0 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
e14f0 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 /. nolockIoMeth
e1500 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ods, /*
e1510 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e1520 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ods object name
e1530 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 */. nolockClose
e1540 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e1550 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 * xClose method
e1560 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c */. nolockLock,
e1570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e1580 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a * xLock method *
e1590 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b /. nolockUnlock
e15a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
e15b0 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 xUnlock method
e15c0 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b */. nolockCheck
e15d0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f ReservedLock /
e15e0 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
e15f0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 Lock method */.)
e1600 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f .IOMETHODS(. do
e1610 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 tlockIoFinder,
e1620 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
e1630 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
e1640 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 */. dotlockIoMe
e1650 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f thods, /
e1660 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
e1670 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
e1680 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f */. dotlockClo
e1690 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 se,
e16a0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
e16b0 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 */. dotlockLoc
e16c0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
e16d0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
e16e0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f */. dotlockUnlo
e16f0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ck, /
e1700 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
e1710 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 */. dotlockChe
e1720 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 ckReservedLock
e1730 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e1740 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
e1750 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e )..#if SQLITE_EN
e1760 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e1770 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b LE && !OS_VXWORK
e1780 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66 S.IOMETHODS(. f
e1790 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 lockIoFinder,
e17a0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 /* Find
e17b0 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 er function name
e17c0 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 */. flockIoMet
e17d0 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 hods,
e17e0 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 /* sqlite3_io_me
e17f0 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d thods object nam
e1800 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 e */. flockClos
e1810 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
e1820 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f /* xClose metho
e1830 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b d */. flockLock
e1840 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1850 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 /* xLock method
e1860 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 */. flockUnloc
e1870 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
e1880 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f /* xUnlock metho
e1890 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63 d */. flockChec
e18a0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 kReservedLock
e18b0 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 /* xCheckReserv
e18c0 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f edLock method */
e18d0 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f .).#endif..#if O
e18e0 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 S_VXWORKS.IOMETH
e18f0 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64 ODS(. semIoFind
e1900 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 er,
e1910 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 /* Finder funct
e1920 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 ion name */. se
e1930 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 mIoMethods,
e1940 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 /* sqlit
e1950 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
e1960 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 ject name */. s
e1970 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 emClose,
e1980 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f /* xClo
e1990 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 se method */. s
e19a0 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 emLock,
e19b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
e19c0 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 k method */. se
e19d0 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 mUnlock,
e19e0 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f /* xUnlo
e19f0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 ck method */. s
e1a00 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c emCheckReservedL
e1a10 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 ock /* xChe
e1a20 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d ckReservedLock m
e1a30 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 ethod */.).#endi
e1a40 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f f..#if defined(_
e1a50 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
e1a60 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e1a70 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f NG_STYLE.IOMETHO
e1a80 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 DS(. afpIoFinde
e1a90 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
e1aa0 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
e1ab0 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 on name */. afp
e1ac0 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 IoMethods,
e1ad0 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
e1ae0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
e1af0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 ect name */. af
e1b00 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 pClose,
e1b10 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
e1b20 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 e method */. af
e1b30 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 pLock,
e1b40 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
e1b50 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 method */. afp
e1b60 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 Unlock,
e1b70 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
e1b80 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 k method */. af
e1b90 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f pCheckReservedLo
e1ba0 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 ck /* xChec
e1bb0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
e1bc0 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 thod */.).#endif
e1bd0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 68 6f ../*.** The "Who
e1be0 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 22 le File Locking"
e1bf0 20 66 69 6e 64 65 72 20 72 65 74 75 72 6e 73 20 finder returns
e1c00 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20 the same set of
e1c10 6d 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 74 68 methods as.** th
e1c20 65 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e 67 20 e posix locking
e1c30 66 69 6e 64 65 72 2e 20 20 42 75 74 20 69 74 20 finder. But it
e1c40 61 6c 73 6f 20 73 65 74 73 20 74 68 65 20 53 51 also sets the SQ
e1c50 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f LITE_WHOLE_FILE_
e1c60 4c 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 67 20 LOCKING.** flag
e1c70 74 6f 20 66 6f 72 63 65 20 74 68 65 20 70 6f 73 to force the pos
e1c80 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b ix advisory lock
e1c90 73 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 77 s to cover the w
e1ca0 68 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 65 61 hole file instea
e1cb0 64 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 20 73 d.** of just a s
e1cc0 6d 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 79 74 mall span of byt
e1cd0 65 73 20 6e 65 61 72 20 74 68 65 20 31 47 69 42 es near the 1GiB
e1ce0 20 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 6f 6c boundary. Whol
e1cf0 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 0a 2a e File Locking.*
e1d00 2a 20 69 73 20 75 73 65 66 75 6c 20 6f 6e 20 4e * is useful on N
e1d10 46 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c 65 73 FS-mounted files
e1d20 20 73 69 6e 63 65 20 69 74 20 68 65 6c 70 73 20 since it helps
e1d30 4e 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 NFS to maintain
e1d40 63 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 65 6e cache.** coheren
e1d50 63 79 2e 20 20 42 75 74 20 69 74 20 69 73 20 61 cy. But it is a
e1d60 20 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 6f 74 detriment to ot
e1d70 68 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 20 her filesystems
e1d80 73 69 6e 63 65 20 69 74 20 72 75 6e 73 0a 2a 2a since it runs.**
e1d90 20 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 slower..*/.stat
e1da0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
e1db0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 6f 73 _io_methods *pos
e1dc0 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70 ixWflIoFinderImp
e1dd0 6c 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a 2c 20 l(const char*z,
e1de0 75 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 20 55 unixFile*p){. U
e1df0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e1e00 7a 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 6c 61 z);. p->fileFla
e1e10 67 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c gs = SQLITE_WHOL
e1e20 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a E_FILE_LOCKING;.
e1e30 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 return &posixI
e1e40 6f 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 61 74 oMethods;.}.stat
e1e50 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
e1e60 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a _io_methods . *
e1e70 28 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 66 6c (*const posixWfl
e1e80 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 IoFinder)(const
e1e90 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a char*,unixFile *
e1ea0 70 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 6f 46 p) = posixWflIoF
e1eb0 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a inderImpl;../*.*
e1ec0 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b * The proxy lock
e1ed0 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 61 20 ing method is a
e1ee0 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 20 69 "super-method" i
e1ef0 6e 20 74 68 65 20 73 65 6e 73 65 20 74 68 61 74 n the sense that
e1f00 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63 it.** opens sec
e1f10 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 65 73 63 ondary file desc
e1f20 72 69 70 74 6f 72 73 20 66 6f 72 20 74 68 65 20 riptors for the
e1f30 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66 conch and lock f
e1f40 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 20 75 iles and.** it u
e1f50 73 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66 ses proxy, dot-f
e1f60 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c ile, AFP, and fl
e1f70 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65 ock() locking me
e1f80 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0a 2a thods on those.*
e1f90 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 * secondary file
e1fa0 73 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 s. For this rea
e1fb0 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f son, the divisio
e1fc0 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 n that implement
e1fd0 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69 s.** proxy locki
e1fe0 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75 ng is located mu
e1ff0 63 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 ch further down
e2000 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75 in the file. Bu
e2010 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 t we need.** to
e2020 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65 66 go ahead and def
e2030 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f ine the sqlite3_
e2040 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20 66 io_methods and f
e2050 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a inder function.*
e2060 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b * for proxy lock
e2070 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 77 65 ing here. So we
e2080 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 65 forward declare
e2090 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 the I/O methods
e20a0 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 ..*/.#if defined
e20b0 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
e20c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e20d0 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69 KING_STYLE.stati
e20e0 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 c int proxyClose
e20f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b (sqlite3_file*);
e2100 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
e2110 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 yLock(sqlite3_fi
e2120 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 le*, int);.stati
e2130 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 c int proxyUnloc
e2140 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c k(sqlite3_file*,
e2150 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69 6e int);.static in
e2160 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 t proxyCheckRese
e2170 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 rvedLock(sqlite3
e2180 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b 0a 49 _file*, int*);.I
e2190 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 6f 78 OMETHODS(. prox
e21a0 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 yIoFinder,
e21b0 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
e21c0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
e21d0 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 . proxyIoMethod
e21e0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
e21f0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e2200 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
e2210 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20 /. proxyClose,
e2220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e2230 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
e2240 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20 /. proxyLock,
e2250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e2260 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
e2270 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20 . proxyUnlock,
e2280 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2290 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
e22a0 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65 /. proxyCheckRe
e22b0 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a servedLock /*
e22c0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
e22d0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
e22e0 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 65 66 #endif...#if def
e22f0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
e2300 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
e2310 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f _LOCKING_STYLE./
e2320 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64 * .** This "find
e2330 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 er" function att
e2340 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69 empts to determi
e2350 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b ne the best lock
e2360 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a ing strategy .**
e2370 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 for the databas
e2380 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68 e file "filePath
e2390 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75 ". It then retu
e23a0 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f rns the sqlite3_
e23b0 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 io_methods.** ob
e23c0 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d ject that implem
e23d0 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65 ents that strate
e23e0 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 gy..**.** This i
e23f0 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c s for MacOSX onl
e2400 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e y..*/.static con
e2410 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e2420 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 thods *autolockI
e2430 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 oFinderImpl(. c
e2440 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50 onst char *fileP
e2450 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 ath, /* name
e2460 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
e2470 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 file */. unixFi
e2480 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 le *pNew
e2490 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20 /* open file
e24a0 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64 object for the d
e24b0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
e24c0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ){. static cons
e24d0 74 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 t struct Mapping
e24e0 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 {. const cha
e24f0 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 r *zFilesystem;
e2500 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2510 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 Filesystem type
e2520 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 name */. cons
e2530 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e2540 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 hods *pMethods;
e2550 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65 /* Appropriate
e2560 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 locking method
e2570 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 */. } aMap[] =
e2580 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20 {. { "hfs",
e2590 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 &posixIoMethod
e25a0 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22 s },. { "ufs"
e25b0 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 , &posixIoMet
e25c0 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61 hods },. { "a
e25d0 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 fpfs", &afpIoMe
e25e0 74 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 66 20 thods },.#ifdef
e25f0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 SQLITE_ENABLE_AF
e2600 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 P_LOCKING_SMB.
e2610 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 61 { "smbfs", &a
e2620 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 fpIoMethods },.#
e2630 65 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d 62 66 else. { "smbf
e2640 73 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d 65 74 s", &flockIoMet
e2650 68 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 hods },.#endif.
e2660 20 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 26 { "webdav", &
e2670 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 nolockIoMethods
e2680 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a },. { 0, 0 }.
e2690 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 };. int i;.
e26a0 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 struct statfs fs
e26b0 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 66 Info;. struct f
e26c0 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a lock lockInfo;..
e26d0 20 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 if( !filePath
e26e0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c ){. /* If fil
e26f0 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 ePath==NULL that
e2700 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 means we are de
e2710 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 aling with a tra
e2720 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 nsient file.
e2730 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 ** that does not
e2740 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b need to be lock
e2750 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 ed. */. retur
e2760 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f n &nolockIoMetho
e2770 64 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 ds;. }. if( st
e2780 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 26 atfs(filePath, &
e2790 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b fsInfo) != -1 ){
e27a0 0a 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f 2e . if( fsInfo.
e27b0 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44 f_flags & MNT_RD
e27c0 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 65 ONLY ){. re
e27d0 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 turn &nolockIoMe
e27e0 74 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20 thods;. }.
e27f0 20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69 for(i=0; aMap[i
e2800 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 69 ].zFilesystem; i
e2810 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 ++){. if( s
e2820 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 trcmp(fsInfo.f_f
e2830 73 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b stypename, aMap[
e2840 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d i].zFilesystem)=
e2850 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
e2860 74 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 turn aMap[i].pMe
e2870 74 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20 thods;. }.
e2880 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 }. }.. /* D
e2890 65 66 61 75 6c 74 20 63 61 73 65 2e 20 48 61 6e efault case. Han
e28a0 64 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 dles, amongst ot
e28b0 68 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a hers, "nfs".. *
e28c0 2a 20 54 65 73 74 20 62 79 74 65 2d 72 61 6e 67 * Test byte-rang
e28d0 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e e lock using fcn
e28e0 74 6c 28 29 2e 20 49 66 20 74 68 65 20 63 61 6c tl(). If the cal
e28f0 6c 20 73 75 63 63 65 65 64 73 2c 20 0a 20 20 2a l succeeds, . *
e2900 2a 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 * assume that th
e2910 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 e file-system su
e2920 70 70 6f 72 74 73 20 50 4f 53 49 58 20 73 74 79 pports POSIX sty
e2930 6c 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a le locks. . */.
e2940 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e lockInfo.l_len
e2950 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f = 1;. lockInfo
e2960 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 .l_start = 0;.
e2970 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 lockInfo.l_whenc
e2980 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
e2990 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 lockInfo.l_type
e29a0 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 = F_RDLCK;. if(
e29b0 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 fcntl(pNew->h,
e29c0 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e F_GETLK, &lockIn
e29d0 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 fo)!=-1 ) {.
e29e0 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 pNew->fileFlags
e29f0 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 = SQLITE_WHOLE_F
e2a00 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 20 20 ILE_LOCKING;.
e2a10 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f return &posixIo
e2a20 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 Methods;. }else
e2a30 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f {. return &do
e2a40 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a tlockIoMethods;.
e2a50 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e }.}.static con
e2a60 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e2a70 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 thods . *(*cons
e2a80 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 t autolockIoFind
e2a90 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c er)(const char*,
e2aa0 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 unixFile*) = aut
e2ab0 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 olockIoFinderImp
e2ac0 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 l;..#endif /* de
e2ad0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e2ae0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c && SQLITE_ENABL
e2af0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
e2b00 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 */..#if OS_VXWOR
e2b10 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 KS && SQLITE_ENA
e2b20 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e2b30 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 E./* .** This "f
e2b40 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 inder" function
e2b50 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 attempts to dete
e2b60 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c rmine the best l
e2b70 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 ocking strategy
e2b80 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 .** for the data
e2b90 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 base file "fileP
e2ba0 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 ath". It then r
e2bb0 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 eturns the sqlit
e2bc0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a e3_io_methods.**
e2bd0 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 object that imp
e2be0 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 lements that str
e2bf0 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ategy..**.** Thi
e2c00 73 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73 s is for VXWorks
e2c10 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 only..*/.static
e2c20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e2c30 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c o_methods *autol
e2c40 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 ockIoFinderImpl(
e2c50 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 . const char *f
e2c60 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e ilePath, /* n
e2c70 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
e2c80 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e ase file */. un
e2c90 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 ixFile *pNew
e2ca0 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70 /* the op
e2cb0 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a en file object *
e2cc0 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c /.){. struct fl
e2cd0 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 ock lockInfo;..
e2ce0 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 if( !filePath )
e2cf0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 {. /* If file
e2d00 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 Path==NULL that
e2d10 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 means we are dea
e2d20 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e ling with a tran
e2d30 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a sient file. *
e2d40 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 * that does not
e2d50 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 need to be locke
e2d60 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e d. */. return
e2d70 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &nolockIoMethod
e2d80 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 s;. }.. /* Tes
e2d90 74 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20 t if fcntl() is
e2da0 73 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73 supported and us
e2db0 65 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f e POSIX style lo
e2dc0 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 cks.. ** Otherw
e2dd0 69 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f ise fall back to
e2de0 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 the named semap
e2df0 68 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a hore method.. *
e2e00 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c /. lockInfo.l_l
e2e10 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e en = 1;. lockIn
e2e20 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a fo.l_start = 0;.
e2e30 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 lockInfo.l_whe
e2e40 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a nce = SEEK_SET;.
e2e50 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 lockInfo.l_typ
e2e60 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 e = F_RDLCK;. i
e2e70 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 f( fcntl(pNew->h
e2e80 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_GETLK, &lock
e2e90 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 Info)!=-1 ) {.
e2ea0 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 return &posixI
e2eb0 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 oMethods;. }els
e2ec0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73 e{. return &s
e2ed0 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d emIoMethods;. }
e2ee0 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 .}.static const
e2ef0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e2f00 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61 ds . *(*const a
e2f10 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 utolockIoFinder)
e2f20 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 (const char*,uni
e2f30 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f xFile*) = autolo
e2f40 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a ckIoFinderImpl;.
e2f50 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
e2f60 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f WORKS && SQLITE_
e2f70 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e2f80 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 TYLE */../*.** A
e2f90 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 n abstract type
e2fa0 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f for a pointer to
e2fb0 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e a IO method fin
e2fc0 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f der function:.*/
e2fd0 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 .typedef const s
e2fe0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e2ff0 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 s *(*finder_type
e3000 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e )(const char*,un
e3010 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a ixFile*);.../***
e3020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3060 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a *********.******
e3070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3080 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 ****** sqlite3_v
e3090 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a fs methods *****
e30a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e30b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
e30c0 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 is division cont
e30d0 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 ains the impleme
e30e0 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f ntation of metho
e30f0 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c ds on the.** sql
e3100 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e ite3_vfs object.
e3110 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 .*/../*.** Initi
e3120 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e alize the conten
e3130 74 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 ts of the unixFi
e3140 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 le structure poi
e3150 6e 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a nted to by pId..
e3160 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
e3170 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 llInUnixFile(.
e3180 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
e3190 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 s, /* Point
e31a0 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 er to vfs object
e31b0 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 */. int h,
e31c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e31d0 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 Open file descr
e31e0 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 iptor of file be
e31f0 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 ing opened */.
e3200 69 6e 74 20 64 69 72 66 64 2c 20 20 20 20 20 20 int dirfd,
e3210 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 /* Direc
e3220 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 tory file descri
e3230 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ptor */. sqlite
e3240 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 3_file *pId,
e3250 20 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 /* Write to th
e3260 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 e unixFile struc
e3270 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 ture here */. c
e3280 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
e3290 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f name, /* Name o
e32a0 66 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 f the file being
e32b0 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 opened */. int
e32c0 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 noLock,
e32d0 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63 /* Omit loc
e32e0 6b 69 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a king if true */.
e32f0 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 int isDelete
e3300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c /* Del
e3310 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 ete on close if
e3320 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e true */.){. con
e3330 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e3340 74 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53 thods *pLockingS
e3350 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65 tyle;. unixFile
e3360 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 *pNew = (unixFi
e3370 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 le *)pId;. int
e3380 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
e3390 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d . assert( pNew-
e33a0 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a >pLock==NULL );.
e33b0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
e33c0 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a pOpen==NULL );..
e33d0 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69 /* Parameter i
e33e0 73 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20 sDelete is only
e33f0 75 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e used on vxworks.
e3400 20 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78 Express this ex
e3410 70 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 68 plicitly . ** h
e3420 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 63 ere to prevent c
e3430 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 ompiler warnings
e3440 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61 about unused pa
e3450 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 rameters.. */.
e3460 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e3470 52 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a 20 20 R(isDelete);..
e3480 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20 OSTRACE3("OPEN
e3490 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c %-3d %s\n", h,
e34a0 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20 zFilename);
e34b0 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a . pNew->h = h;.
e34c0 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 pNew->dirfd =
e34d0 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52 dirfd;. SET_THR
e34e0 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 20 70 EADID(pNew);. p
e34f0 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d New->fileFlags =
e3500 20 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 0;..#if OS_VXWO
e3510 52 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20 RKS. pNew->pId
e3520 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c = vxworksFindFil
e3530 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a eId(zFilename);.
e3540 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d if( pNew->pId=
e3550 3d 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b =0 ){. noLock
e3560 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 = 1;. rc = S
e3570 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
e3580 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e .#endif.. if( n
e3590 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f oLock ){. pLo
e35a0 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f ckingStyle = &no
e35b0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 lockIoMethods;.
e35c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 }else{. pLoc
e35d0 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28 kingStyle = (**(
e35e0 66 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66 finder_type*)pVf
e35f0 73 2d 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69 s->pAppData)(zFi
e3600 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b 0a 23 lename, pNew);.#
e3610 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
e3620 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 _LOCKING_STYLE.
e3630 20 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c /* Cache zFil
e3640 65 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63 ename in the loc
e3650 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46 king context (AF
e3660 50 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76 P and dotlock ov
e3670 65 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20 erride) for.
e3680 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74 ** proxyLock act
e3690 69 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69 ivation is possi
e36a0 62 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78 ble (remote prox
e36b0 79 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62 y is based on db
e36c0 20 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46 name). ** zF
e36d0 69 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20 ilename remains
e36e0 76 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65 valid until file
e36f0 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73 is closed, to s
e3700 75 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e upport */. pN
e3710 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 ew->lockingConte
e3720 78 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c xt = (void*)zFil
e3730 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 ename;.#endif.
e3740 7d 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e }.. if( pLockin
e3750 67 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78 gStyle == &posix
e3760 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
e3770 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
e3780 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 );. rc = find
e3790 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 LockInfo(pNew, &
e37a0 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e pNew->pLock, &pN
e37b0 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 ew->pOpen);.
e37c0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
e37d0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 K ){. /* If
e37e0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65 an error occure
e37f0 64 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 d in findLockInf
e3800 6f 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 o(), close the f
e3810 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20 ile descriptor.
e3820 20 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 ** immediat
e3830 65 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65 ely, before rele
e3840 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e asing the mutex.
e3850 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 findLockInfo()
e3860 6d 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a may fail. *
e3870 2a 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69 * in two scenari
e3880 6f 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 os:. **.
e3890 20 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61 ** (a) A ca
e38a0 6c 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61 ll to fstat() fa
e38b0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20 iled.. **
e38c0 20 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61 (b) A malloc fa
e38d0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 iled.. **.
e38e0 20 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f ** Scenario
e38f0 20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63 (b) may only oc
e3900 63 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65 cur if the proce
e3910 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f ss is holding no
e3920 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 other. **
e3930 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 file descriptors
e3940 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d open on the sam
e3950 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 e file. If there
e3960 20 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65 were other file
e3970 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 . ** descri
e3980 70 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69 ptors on this fi
e3990 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c le, then no mall
e39a0 6f 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75 oc would be requ
e39b0 69 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a ired by. **
e39c0 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e findLockInfo().
e39d0 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
e39e0 63 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 74 case, it is quit
e39f0 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 0a e safe to close.
e3a00 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 ** handle
e3a10 68 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 61 h - as it is gua
e3a20 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f 20 ranteed that no
e3a30 70 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c 6c posix locks will
e3a40 20 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 be released.
e3a50 20 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 73 ** by doing s
e3a60 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 o.. **.
e3a70 20 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f ** If scenario
e3a80 20 28 61 29 20 63 61 75 73 65 64 20 74 68 65 20 (a) caused the
e3a90 65 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67 error then thing
e3aa0 73 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66 s are not so saf
e3ab0 65 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 e. The. **
e3ac0 69 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74 implicit assumpt
e3ad0 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 ion here is that
e3ae0 20 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c if fstat() fail
e3af0 73 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e s, things are in
e3b00 0a 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62 . ** such b
e3b10 61 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72 ad shape that dr
e3b20 6f 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72 opping a lock or
e3b30 20 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74 two doesn't mat
e3b40 74 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 20 ter much..
e3b50 2a 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 68 */. close(h
e3b60 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b );. h = -1;
e3b70 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c . }. unixL
e3b80 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
e3b90 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ..#if SQLITE_ENA
e3ba0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e3bb0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 E && defined(__A
e3bc0 50 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 PPLE__). else i
e3bd0 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 f( pLockingStyle
e3be0 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 == &afpIoMethod
e3bf0 73 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 s ){. /* AFP
e3c00 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 locking uses the
e3c10 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 file path so it
e3c20 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 needs to be inc
e3c30 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 luded in. **
e3c40 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f the afpLockingCo
e3c50 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 ntext.. */.
e3c60 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 afpLockingCont
e3c70 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 ext *pCtx;. p
e3c80 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 New->lockingCont
e3c90 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c ext = pCtx = sql
e3ca0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a ite3_malloc( siz
e3cb0 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 eof(*pCtx) );.
e3cc0 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b if( pCtx==0 ){
e3cd0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
e3ce0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 TE_NOMEM;. }e
e3cf0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 lse{. /* NB
e3d00 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 : zFilename exis
e3d10 74 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 ts and remains v
e3d20 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 alid until the f
e3d30 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 ile is closed.
e3d40 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 ** according
e3d50 20 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 to requirement
e3d60 46 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 F11141. So we d
e3d70 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 o not need to ma
e3d80 6b 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f ke a. ** co
e3d90 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 py of the filena
e3da0 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 me. */. pCt
e3db0 78 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c x->dbPath = zFil
e3dc0 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 ename;. sra
e3dd0 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 ndomdev();.
e3de0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
e3df0 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 );. rc = fi
e3e00 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c ndLockInfo(pNew,
e3e10 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f NULL, &pNew->pO
e3e20 70 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 pen);. unix
e3e30 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 LeaveMutex();
e3e40 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a . }. }.
e3e50 23 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 #endif.. else i
e3e60 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 f( pLockingStyle
e3e70 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 == &dotlockIoMe
e3e80 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
e3e90 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 Dotfile locking
e3ea0 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 uses the file pa
e3eb0 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 th so it needs t
e3ec0 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e o be included in
e3ed0 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c . ** the dotl
e3ee0 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 ockLockingContex
e3ef0 74 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 t . */. ch
e3f00 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 ar *zLockFile;.
e3f10 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 int nFilename
e3f20 3b 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 ;. nFilename
e3f30 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 = (int)strlen(zF
e3f40 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 ilename) + 6;.
e3f50 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 zLockFile = (c
e3f60 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 har *)sqlite3_ma
e3f70 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b lloc(nFilename);
e3f80 0a 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 . if( zLockFi
e3f90 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 le==0 ){. r
e3fa0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
e3fb0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
e3fc0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
e3fd0 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a ntf(nFilename, z
e3fe0 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 LockFile, "%s" D
e3ff0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a OTLOCK_SUFFIX, z
e4000 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d Filename);. }
e4010 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 . pNew->locki
e4020 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 ngContext = zLoc
e4030 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 kFile;. }..#if
e4040 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 OS_VXWORKS. els
e4050 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 e if( pLockingSt
e4060 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 yle == &semIoMet
e4070 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e hods ){. /* N
e4080 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c amed semaphore l
e4090 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 ocking uses the
e40a0 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 file path so it
e40b0 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 needs to be.
e40c0 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 ** included in t
e40d0 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e he semLockingCon
e40e0 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 text. */.
e40f0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
e4100 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c ;. rc = findL
e4110 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 ockInfo(pNew, &p
e4120 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 New->pLock, &pNe
e4130 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 w->pOpen);. i
e4140 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f f( (rc==SQLITE_O
e4150 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 K) && (pNew->pOp
e4160 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 en->pSem==NULL)
e4170 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a ){. char *z
e4180 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e SemName = pNew->
e4190 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b pOpen->aSemName;
e41a0 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 . int n;.
e41b0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
e41c0 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d intf(MAX_PATHNAM
e41d0 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25 E, zSemName, "/%
e41e0 73 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 s.sem",.
e41f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
e4200 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e New->pId->zCanon
e4210 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 icalName);.
e4220 20 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e for( n=1; zSemN
e4230 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 ame[n]; n++ ).
e4240 20 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 if( zSemNa
e4250 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 me[n]=='/' ) zSe
e4260 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a mName[n] = '_';.
e4270 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 pNew->pOpe
e4280 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 n->pSem = sem_op
e4290 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 en(zSemName, O_C
e42a0 52 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a REAT, 0666, 1);.
e42b0 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e if( pNew->
e42c0 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 pOpen->pSem == S
e42d0 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 EM_FAILED ){.
e42e0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
e42f0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 _NOMEM;.
e4300 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 pNew->pOpen->aSe
e4310 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b mName[0] = '\0';
e4320 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
e4330 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 unixLeaveMute
e4340 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a x();. }.#endif.
e4350 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 . pNew->lastE
e4360 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 rrno = 0;.#if OS
e4370 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 _VXWORKS. if( r
e4380 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
e4390 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 unlink(zFile
e43a0 6e 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c name);. isDel
e43b0 65 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 ete = 0;. }. p
e43c0 4e 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 New->isDelete =
e43d0 69 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 isDelete;.#endif
e43e0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
e43f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
e4400 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 dirfd>=0 ) close
e4410 28 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 (dirfd); /* sile
e4420 6e 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c nt leak if fail,
e4430 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f already in erro
e4440 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 3e 3d r */. if( h>=
e4450 30 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 0 ) close(h);.
e4460 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d }else{. pNew-
e4470 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b >pMethod = pLock
e4480 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70 ingStyle;. Op
e4490 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 enCounter(+1);.
e44a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
e44b0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 }../*.** Open a
e44c0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e44d0 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 to the directory
e44e0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 containing file
e44f0 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 zFilename..** I
e4500 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 f successful, *p
e4510 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 Fd is set to the
e4520 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 opened file des
e4530 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 criptor and.** S
e4540 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
e4550 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f rned. If an erro
e4560 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 r occurs, either
e4570 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a SQLITE_NOMEM.**
e4580 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f or SQLITE_CANTO
e4590 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 PEN is returned
e45a0 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 and *pFd is set
e45b0 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a to an undefined.
e45c0 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 ** value..**.**
e45d0 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 If SQLITE_OK is
e45e0 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61 returned, the ca
e45f0 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 ller is responsi
e4600 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a ble for closing.
e4610 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 ** the file desc
e4620 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e riptor *pFd usin
e4630 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 g close()..*/.st
e4640 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 atic int openDir
e4650 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 ectory(const cha
e4660 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e r *zFilename, in
e4670 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 t *pFd){. int i
e4680 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 i;. int fd = -1
e4690 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d ;. char zDirnam
e46a0 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 e[MAX_PATHNAME+1
e46b0 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e ];.. sqlite3_sn
e46c0 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e printf(MAX_PATHN
e46d0 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 AME, zDirname, "
e46e0 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b %s", zFilename);
e46f0 0a 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 . for(ii=(int)s
e4700 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b trlen(zDirname);
e4710 20 69 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d ii>1 && zDirnam
e4720 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d e[ii]!='/'; ii--
e4730 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b );. if( ii>0 ){
e4740 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 . zDirname[ii
e4750 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 ] = '\0';. fd
e4760 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 = open(zDirname
e4770 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e , O_RDONLY|O_BIN
e4780 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 ARY, 0);. if(
e4790 20 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 fd>=0 ){.#ifdef
e47a0 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 FD_CLOEXEC.
e47b0 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 fcntl(fd, F_SE
e47c0 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 TFD, fcntl(fd, F
e47d0 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f _GETFD, 0) | FD_
e47e0 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 CLOEXEC);.#endif
e47f0 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 . OSTRACE3(
e4800 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73 "OPENDIR %-3d %s
e4810 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d \n", fd, zDirnam
e4820 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
e4830 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 *pFd = fd;. ret
e4840 75 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 urn (fd>=0?SQLIT
e4850 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 E_OK:SQLITE_CANT
e4860 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 OPEN);.}../*.**
e4870 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 Create a tempora
e4880 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 ry file name in
e4890 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 zBuf. zBuf must
e48a0 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a be allocated.**
e48b0 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 by the calling
e48c0 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74 process and must
e48d0 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 be big enough t
e48e0 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a o hold at least.
e48f0 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e ** pVfs->mxPathn
e4900 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 ame bytes..*/.st
e4910 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 atic int getTemp
e4920 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 name(int nBuf, c
e4930 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 har *zBuf){. st
e4940 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
e4950 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 *azDirs[] = {.
e4960 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20 0,. 0,.
e4970 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20 "/var/tmp",.
e4980 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a "/usr/tmp",.
e4990 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20 "/tmp",.
e49a0 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74 ".",. };. st
e49b0 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
e49c0 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b ned char zChars[
e49d0 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 ] =. "abcdefg
e49e0 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 hijklmnopqrstuvw
e49f0 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 xyz". "ABCDEF
e4a00 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 GHIJKLMNOPQRSTUV
e4a10 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 WXYZ". "01234
e4a20 35 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 56789";. unsign
e4a30 65 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 ed int i, j;. s
e4a40 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a truct stat buf;.
e4a50 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
e4a60 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 ir = ".";.. /*
e4a70 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 It's odd to simu
e4a80 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 late an io-error
e4a90 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c here, but reall
e4aa0 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 y this is just.
e4ab0 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f ** using the io
e4ac0 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 -error infrastru
e4ad0 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 cture to test th
e4ae0 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 at SQLite handle
e4af0 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 s this. ** func
e4b00 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 tion failing. .
e4b10 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f */. SimulateIO
e4b20 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
e4b30 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 LITE_IOERR );..
e4b40 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c azDirs[0] = sql
e4b50 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 ite3_temp_direct
e4b60 6f 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20 ory;. if (NULL
e4b70 3d 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a == azDirs[1]) {.
e4b80 20 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20 azDirs[1] =
e4b90 67 65 74 65 6e 76 28 22 54 4d 50 44 49 52 22 29 getenv("TMPDIR")
e4ba0 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69 ;. }. . for(i
e4bb0 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 =0; i<sizeof(azD
e4bc0 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 irs)/sizeof(azDi
e4bd0 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 rs[0]); i++){.
e4be0 20 20 69 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d if( azDirs[i]=
e4bf0 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 =0 ) continue;.
e4c00 20 20 20 69 66 28 20 73 74 61 74 28 61 7a 44 69 if( stat(azDi
e4c10 72 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 20 63 rs[i], &buf) ) c
e4c20 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 ontinue;. if(
e4c30 20 21 53 5f 49 53 44 49 52 28 62 75 66 2e 73 74 !S_ISDIR(buf.st
e4c40 5f 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 _mode) ) continu
e4c50 65 3b 0a 20 20 20 20 69 66 28 20 61 63 63 65 73 e;. if( acces
e4c60 73 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 s(azDirs[i], 07)
e4c70 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
e4c80 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 zDir = azDirs[i
e4c90 5d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 ];. break;.
e4ca0 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 }.. /* Check th
e4cb0 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 at the output bu
e4cc0 66 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e ffer is large en
e4cd0 6f 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d ough for the tem
e4ce0 70 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a porary file . *
e4cf0 2a 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73 * name. If it is
e4d00 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c not, return SQL
e4d10 49 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a ITE_ERROR.. */.
e4d20 20 20 69 66 28 20 28 73 74 72 6c 65 6e 28 7a 44 if( (strlen(zD
e4d30 69 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c ir) + strlen(SQL
e4d40 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 ITE_TEMP_FILE_PR
e4d50 45 46 49 58 29 20 2b 20 31 37 29 20 3e 3d 20 28 EFIX) + 17) >= (
e4d60 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20 size_t)nBuf ){.
e4d70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e4d80 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 _ERROR;. }.. d
e4d90 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 o{. sqlite3_s
e4da0 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c nprintf(nBuf-17,
e4db0 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 zBuf, "%s/"SQLI
e4dc0 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
e4dd0 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 FIX, zDir);.
e4de0 6a 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 j = (int)strlen(
e4df0 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 zBuf);. sqlit
e4e00 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 e3_randomness(15
e4e10 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20 , &zBuf[j]);.
e4e20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20 for(i=0; i<15;
e4e30 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 i++, j++){.
e4e40 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 zBuf[j] = (char
e4e50 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 )zChars[ ((unsig
e4e60 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d ned char)zBuf[j]
e4e70 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 )%(sizeof(zChars
e4e80 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20 )-1) ];. }.
e4e90 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 zBuf[j] = 0;.
e4ea0 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73 73 28 }while( access(
e4eb0 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 zBuf,0)==0 );.
e4ec0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
e4ed0 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f ;.}..#if SQLITE_
e4ee0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e4ef0 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 TYLE && defined(
e4f00 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a __APPLE__)./*.**
e4f10 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e Routine to tran
e4f20 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 sform a unixFile
e4f30 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f into a proxy-lo
e4f40 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a cking unixFile..
e4f50 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
e4f60 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c n in the proxy-l
e4f70 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 ock division, bu
e4f80 74 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70 t used by unixOp
e4f90 65 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 en().** if SQLIT
e4fa0 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c E_PREFER_PROXY_L
e4fb0 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65 OCKING is define
e4fc0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
e4fd0 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 proxyTransformU
e4fe0 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 nixFile(unixFile
e4ff0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b *, const char*);
e5000 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 .#endif../*.** S
e5010 65 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 earch for an unu
e5020 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 sed file descrip
e5030 74 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 tor that was ope
e5040 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ned on the datab
e5050 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f ase .** file (no
e5060 74 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d t a journal or m
e5070 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 aster-journal fi
e5080 6c 65 29 20 69 64 65 6e 74 69 66 69 65 64 20 62 le) identified b
e5090 79 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50 y pathname.** zP
e50a0 61 74 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f ath with SQLITE_
e50b0 4f 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d OPEN_XXX flags m
e50c0 61 74 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61 atching those pa
e50d0 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f ssed as the seco
e50e0 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 nd.** argument t
e50f0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e o this function.
e5100 0a 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 .**.** Such a fi
e5110 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 le descriptor ma
e5120 79 20 65 78 69 73 74 20 69 66 20 61 20 64 61 74 y exist if a dat
e5130 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
e5140 20 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 was closed.** b
e5150 75 74 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 ut the associate
e5160 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
e5170 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 r could not be c
e5180 6c 6f 73 65 64 20 62 65 63 61 75 73 65 20 73 6f losed because so
e5190 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 me.** other file
e51a0 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e descriptor open
e51b0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c on the same fil
e51c0 65 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 e is holding a f
e51d0 69 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66 ile-lock..** Ref
e51e0 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69 er to comments i
e51f0 6e 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28 n the unixClose(
e5200 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74 ) function and t
e5210 68 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65 he lengthy comme
e5220 6e 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 nt.** describing
e5230 20 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 "Posix Advisory
e5240 20 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65 Locking" at the
e5250 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 66 start of this f
e5260 69 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74 ile for .** furt
e5270 68 65 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73 her details. Als
e5280 6f 2c 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e o, ticket #4018.
e5290 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74 .**.** If a suit
e52a0 61 62 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 able file descri
e52b0 70 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 ptor is found, t
e52c0 68 65 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e hen it is return
e52d0 65 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63 ed. If no.** suc
e52e0 68 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f h file descripto
e52f0 72 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31 r is located, -1
e5300 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
e5310 0a 73 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73 .static UnixUnus
e5320 65 64 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62 edFd *findReusab
e5330 6c 65 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20 leFd(const char
e5340 2a 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 *zPath, int flag
e5350 73 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 s){. UnixUnused
e5360 46 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b Fd *pUnused = 0;
e5370 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65 .. /* Do not se
e5380 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 arch for an unus
e5390 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ed file descript
e53a0 6f 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e or on vxworks. N
e53b0 6f 74 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 ot because. **
e53c0 76 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f vxworks would no
e53d0 74 20 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74 t benefit from t
e53e0 68 65 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69 he change (it mi
e53f0 67 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73 ght, we're not s
e5400 75 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62 ure),. ** but b
e5410 65 63 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f ecause no way to
e5420 20 74 65 73 74 20 69 74 20 69 73 20 63 75 72 72 test it is curr
e5430 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e ently available.
e5440 20 49 74 20 69 73 20 62 65 74 74 65 72 20 0a 20 It is better .
e5450 20 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 ** not to risk
e5460 62 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 breaking vxworks
e5470 20 73 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65 support for the
e5480 20 73 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e sake of such an
e5490 20 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66 obscure . ** f
e54a0 65 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20 eature. */.#if
e54b0 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 !OS_VXWORKS. st
e54c0 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b ruct stat sStat;
e54d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e54e0 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 /* Results of
e54f0 20 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a stat() call */.
e5500 0a 20 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63 . /* A stat() c
e5510 61 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72 all may fail for
e5520 20 76 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73 various reasons
e5530 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e . If this happen
e5540 73 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c s, it is. ** al
e5550 6d 6f 73 74 20 63 65 72 74 61 69 6e 20 74 68 61 most certain tha
e5560 74 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c t an open() call
e5570 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 on the same pat
e5580 68 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c h will also fail
e5590 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 .. ** For this
e55a0 72 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72 reason, if an er
e55b0 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 ror occurs in th
e55c0 65 20 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65 e stat() call he
e55d0 72 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69 re, it is. ** i
e55e0 67 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73 gnored and -1 is
e55f0 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 returned. The c
e5600 61 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74 aller will try t
e5610 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c o open a new fil
e5620 65 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f e. ** descripto
e5630 72 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 r on the same pa
e5640 74 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65 th, fail, and re
e5650 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f turn an error to
e5660 20 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20 SQLite.. **.
e5670 2a 2a 20 45 76 65 6e 20 69 66 20 61 20 73 75 62 ** Even if a sub
e5680 73 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63 sequent open() c
e5690 61 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64 all does succeed
e56a0 2c 20 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 , the consequenc
e56b0 65 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73 es of. ** not s
e56c0 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72 earching for a r
e56d0 65 73 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65 esusable file de
e56e0 73 63 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74 scriptor are not
e56f0 20 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 dire. */. if(
e5700 20 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 2c 20 0==stat(zPath,
e5710 26 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 73 &sStat) ){. s
e5720 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
e5730 74 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 75 63 t *pO;. struc
e5740 74 20 75 6e 69 78 46 69 6c 65 49 64 20 69 64 3b t unixFileId id;
e5750 0a 20 20 20 20 69 64 2e 64 65 76 20 3d 20 73 53 . id.dev = sS
e5760 74 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 20 20 tat.st_dev;.
e5770 69 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 2e 73 id.ino = sStat.s
e5780 74 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e 69 78 t_ino;.. unix
e5790 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
e57a0 20 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c 69 73 for(pO=openLis
e57b0 74 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d 70 28 t; pO && memcmp(
e57c0 26 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 49 64 &id, &pO->fileId
e57d0 2c 20 73 69 7a 65 6f 66 28 69 64 29 29 3b 20 70 , sizeof(id)); p
e57e0 4f 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 O=pO->pNext);.
e57f0 20 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 20 20 if( pO ){.
e5800 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a UnixUnusedFd *
e5810 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 *pp;. for(p
e5820 70 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 3b 20 p=&pO->pUnused;
e5830 2a 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c *pp && (*pp)->fl
e5840 61 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26 ags!=flags; pp=&
e5850 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b ((*pp)->pNext));
e5860 0a 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d . pUnused =
e5870 20 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 20 *pp;. if(
e5880 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20 pUnused ){.
e5890 20 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 64 *pp = pUnused
e58a0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
e58b0 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c . }. unixL
e58c0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
e58d0 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 66 .#endif /* if
e58e0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a !OS_VXWORKS */.
e58f0 20 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 64 return pUnused
e5900 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
e5910 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a the file zPath..
e5920 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c ** .** Previousl
e5930 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53 y, the SQLite OS
e5940 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65 layer used thre
e5950 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 e functions in p
e5960 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 lace of this.**
e5970 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 one:.**.** s
e5980 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 qlite3OsOpenRead
e5990 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 Write();.**
e59a0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 sqlite3OsOpenRea
e59b0 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 dOnly();.**
e59c0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 sqlite3OsOpenExc
e59d0 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 lusive();.**.**
e59e0 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 These calls corr
e59f0 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f espond to the fo
e5a00 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 llowing combinat
e5a10 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a ions of flags:.*
e5a20 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69 *.** ReadWri
e5a30 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41 te() -> (REA
e5a40 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29 DWRITE | CREATE)
e5a50 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 .** ReadOnly
e5a60 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44 () -> (READ
e5a70 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 ONLY) .** Op
e5a80 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e enExclusive() ->
e5a90 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 (READWRITE | CR
e5aa0 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45 EATE | EXCLUSIVE
e5ab0 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 ).**.** The old
e5ac0 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 OpenExclusive()
e5ad0 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 accepted a boole
e5ae0 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 an argument - "d
e5af0 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 elFlag". If.** t
e5b00 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61 rue, the file wa
e5b10 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 s configured to
e5b20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 be automatically
e5b30 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 deleted when th
e5b40 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 e.** file handle
e5b50 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 closed. To achi
e5b60 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66 eve the same eff
e5b70 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e ect using this n
e5b80 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 ew .** interface
e5b90 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45 , add the DELETE
e5ba0 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 ONCLOSE flag to
e5bb0 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20 those specified
e5bc0 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 above for .** Op
e5bd0 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a enExclusive()..*
e5be0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e5bf0 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 xOpen(. sqlite3
e5c00 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
e5c10 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 /* The VFS
e5c20 20 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 for which this
e5c30 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 is the xOpen met
e5c40 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 hod */. const c
e5c50 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 har *zPath,
e5c60 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d /* Pathnam
e5c70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 e of file to be
e5c80 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 opened */. sqli
e5c90 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c te3_file *pFile,
e5ca0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
e5cb0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e5cc0 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 to be filled in
e5cd0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 */. int flags,
e5ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e5cf0 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 /* Input flags
e5d00 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 to control the
e5d10 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 opening */. int
e5d20 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 *pOutFlags
e5d30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
e5d40 70 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e put flags return
e5d50 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 ed to SQLite cor
e5d60 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69 e */.){. unixFi
e5d70 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c le *p = (unixFil
e5d80 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74 e *)pFile;. int
e5d90 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 fd = -1;
e5da0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
e5db0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 ile descriptor r
e5dc0 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 eturned by open(
e5dd0 29 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 ) */. int dirfd
e5de0 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 = -1;
e5df0 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f /* Directo
e5e00 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ry file descript
e5e10 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e or */. int open
e5e20 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 Flags = 0;
e5e30 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
e5e40 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 to pass to open(
e5e50 29 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 ) */. int eType
e5e60 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 = flags&0xFFFFF
e5e70 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 F00; /* Type of
e5e80 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f file to open */
e5e90 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 . int noLock;
e5ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e5eb0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 /* True to omi
e5ec0 74 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 t locking primit
e5ed0 69 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ives */. int rc
e5ee0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
e5ef0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 /* Func
e5f00 74 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 tion Return Code
e5f10 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 */.. int isExc
e5f20 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 lusive = (flags
e5f30 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 & SQLITE_OPEN_E
e5f40 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 XCLUSIVE);. int
e5f50 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 isDelete =
e5f60 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f (flags & SQLITE_
e5f70 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f OPEN_DELETEONCLO
e5f80 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 SE);. int isCre
e5f90 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 ate = (flags
e5fa0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 & SQLITE_OPEN_C
e5fb0 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 REATE);. int is
e5fc0 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c Readonly = (fl
e5fd0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
e5fe0 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 N_READONLY);. i
e5ff0 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20 nt isReadWrite
e6000 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 = (flags & SQLIT
e6010 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
e6020 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 );.. /* If crea
e6030 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 ting a master or
e6040 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e main-file journ
e6050 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f al, this functio
e6060 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a n will open. **
e6070 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 a file-descript
e6080 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 or on the direct
e6090 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 ory too. The fir
e60a0 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 st time unixSync
e60b0 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 (). ** is calle
e60c0 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 d the directory
e60d0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e60e0 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 will be fsync()e
e60f0 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a d and close()d..
e6100 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 */. int isOpe
e6110 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 nDirectory = (is
e6120 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 Create && .
e6130 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f (eType==SQLITE_
e6140 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
e6150 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 NAL || eType==SQ
e6160 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
e6170 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 OURNAL). );..
e6180 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a /* If argument z
e6190 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 Path is a NULL p
e61a0 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e ointer, this fun
e61b0 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 ction is require
e61c0 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 d to open. ** a
e61d0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e temporary file.
e61e0 20 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72 Use this buffer
e61f0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 to store the fi
e6200 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f le name in.. */
e6210 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 . char zTmpname
e6220 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d [MAX_PATHNAME+1]
e6230 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
e6240 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a zName = zPath;..
e6250 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 /* Check the f
e6260 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 ollowing stateme
e6270 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 nts are true: .
e6280 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 **. ** (a) E
e6290 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 xactly one of th
e62a0 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 e READWRITE and
e62b0 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d READONLY flags m
e62c0 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 ust be set, and
e62d0 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 . ** (b) if C
e62e0 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 REATE is set, th
e62f0 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 en READWRITE mus
e6300 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 t also be set, a
e6310 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 nd. ** (c) if
e6320 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 EXCLUSIVE is se
e6330 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d t, then CREATE m
e6340 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e ust also be set.
e6350 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 . ** (d) if D
e6360 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 ELETEONCLOSE is
e6370 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 set, then CREATE
e6380 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 must also be se
e6390 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 t.. */. assert
e63a0 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 ((isReadonly==0
e63b0 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d || isReadWrite==
e63c0 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69 0) && (isReadWri
e63d0 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 te || isReadonly
e63e0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 ));. assert(isC
e63f0 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 reate==0 || isRe
e6400 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 adWrite);. asse
e6410 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d rt(isExclusive==
e6420 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 0 || isCreate);.
e6430 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 assert(isDelet
e6440 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 e==0 || isCreate
e6450 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 );.. /* The mai
e6460 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e n DB, main journ
e6470 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a al, and master j
e6480 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 ournal are never
e6490 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 automatically.
e64a0 20 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72 ** deleted. Nor
e64b0 20 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74 are they ever t
e64c0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 emporary files.
e64d0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 21 */. assert( (!
e64e0 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d isDelete && zNam
e64f0 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c e) || eType!=SQL
e6500 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 ITE_OPEN_MAIN_DB
e6510 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 );. assert( (!
e6520 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d isDelete && zNam
e6530 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c e) || eType!=SQL
e6540 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
e6550 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72 URNAL );. asser
e6560 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 t( (!isDelete &&
e6570 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 zName) || eType
e6580 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 !=SQLITE_OPEN_MA
e6590 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a STER_JOURNAL );.
e65a0 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 . /* Assert tha
e65b0 74 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 t the upper laye
e65c0 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 r has set one of
e65d0 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 the "file-type"
e65e0 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 flags. */. ass
e65f0 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 ert( eType==SQLI
e6600 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 TE_OPEN_MAIN_DB
e6610 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 || eType==S
e6620 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f QLITE_OPEN_TEMP_
e6630 44 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 DB . || eT
e6640 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
e6650 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c _MAIN_JOURNAL ||
e6660 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f eType==SQLITE_O
e6670 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c PEN_TEMP_JOURNAL
e6680 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 . || eTyp
e6690 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 e==SQLITE_OPEN_S
e66a0 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 UBJOURNAL || e
e66b0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
e66c0 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c N_MASTER_JOURNAL
e66d0 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 . || eTyp
e66e0 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 e==SQLITE_OPEN_T
e66f0 52 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b RANSIENT_DB. );
e6700 0a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c .. memset(p, 0,
e6710 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 sizeof(unixFile
e6720 29 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 ));.. if( eType
e6730 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
e6740 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e 69 IN_DB ){. Uni
e6750 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 xUnusedFd *pUnus
e6760 65 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 20 ed;. pUnused
e6770 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64 = findReusableFd
e6780 28 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b 0a (zName, flags);.
e6790 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 if( pUnused
e67a0 29 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 55 ){. fd = pU
e67b0 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 7d nused->fd;. }
e67c0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e 75 else{. pUnu
e67d0 73 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 sed = sqlite3_ma
e67e0 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e lloc(sizeof(*pUn
e67f0 75 73 65 64 29 29 3b 0a 20 20 20 20 20 20 69 66 used));. if
e6800 28 20 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 ( !pUnused ){.
e6810 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
e6820 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
e6830 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e }. }. p->
e6840 70 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65 pUnused = pUnuse
e6850 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 d;. }else if( !
e6860 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 zName ){. /*
e6870 49 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c If zName is NULL
e6880 2c 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 , the upper laye
e6890 72 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 r is requesting
e68a0 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a a temp file. */.
e68b0 20 20 20 20 61 73 73 65 72 74 28 69 73 44 65 6c assert(isDel
e68c0 65 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69 ete && !isOpenDi
e68d0 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63 rectory);. rc
e68e0 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d = getTempname(M
e68f0 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a AX_PATHNAME+1, z
e6900 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 Tmpname);. if
e6910 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
e6920 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
e6930 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e rc;. }. zN
e6940 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a ame = zTmpname;.
e6950 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d }.. /* Determ
e6960 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ine the value of
e6970 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d the flags param
e6980 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 50 eter passed to P
e6990 4f 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a 20 20 OSIX function.
e69a0 2a 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 73 65 ** open(). These
e69b0 20 6d 75 73 74 20 62 65 20 63 61 6c 63 75 6c 61 must be calcula
e69c0 74 65 64 20 65 76 65 6e 20 69 66 20 6f 70 65 6e ted even if open
e69d0 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 () is not called
e69e0 2c 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 20 6d , as. ** they m
e69f0 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 ay be stored as
e6a00 70 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 part of the file
e6a10 20 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 65 64 handle and used
e6a20 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 27 63 by the . ** 'c
e6a30 6f 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 6b 69 onch file' locki
e6a40 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 74 ng functions lat
e6a50 65 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 66 28 er on. */. if(
e6a60 20 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f isReadonly ) o
e6a70 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 penFlags |= O_RD
e6a80 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65 ONLY;. if( isRe
e6a90 61 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c adWrite ) openFl
e6aa0 61 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 ags |= O_RDWR;.
e6ab0 20 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 if( isCreate )
e6ac0 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 openFlags |=
e6ad0 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69 O_CREAT;. if( i
e6ae0 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65 sExclusive ) ope
e6af0 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 nFlags |= (O_EXC
e6b00 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 L|O_NOFOLLOW);.
e6b10 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f openFlags |= (O
e6b20 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e _LARGEFILE|O_BIN
e6b30 41 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 64 3c ARY);.. if( fd<
e6b40 30 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f 74 20 0 ){. mode_t
e6b50 6f 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 44 65 openMode = (isDe
e6b60 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 45 lete?0600:SQLITE
e6b70 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 _DEFAULT_FILE_PE
e6b80 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 RMISSIONS);.
e6b90 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c fd = open(zName,
e6ba0 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e openFlags, open
e6bb0 4d 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 Mode);. OSTRA
e6bc0 43 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 CE4("OPENX %-3
e6bd0 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c d %s 0%o\n", fd,
e6be0 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 zName, openFlag
e6bf0 73 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 s);. if( fd<0
e6c00 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 && errno!=EISDI
e6c10 52 20 26 26 20 69 73 52 65 61 64 57 72 69 74 65 R && isReadWrite
e6c20 20 26 26 20 21 69 73 45 78 63 6c 75 73 69 76 65 && !isExclusive
e6c30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 61 69 ){. /* Fai
e6c40 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 led to open the
e6c50 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72 file for read/wr
e6c60 69 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20 ite access. Try
e6c70 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 read-only. */.
e6c80 20 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 flags &= ~(S
e6c90 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
e6ca0 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
e6cb0 5f 43 52 45 41 54 45 29 3b 0a 20 20 20 20 20 20 _CREATE);.
e6cc0 6f 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f openFlags &= ~(O
e6cd0 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a _RDWR|O_CREAT);.
e6ce0 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 flags |= S
e6cf0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
e6d00 4e 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 NLY;. openF
e6d10 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 lags |= O_RDONLY
e6d20 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 6f 70 65 ;. fd = ope
e6d30 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 n(zName, openFla
e6d40 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 gs, openMode);.
e6d50 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 3c }. if( fd<
e6d60 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
e6d70 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
e6d80 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e . goto open
e6d90 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d _finished;. }
e6da0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 66 . }. assert( f
e6db0 64 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f d>=0 );. if( pO
e6dc0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a utFlags ){. *
e6dd0 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 pOutFlags = flag
e6de0 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d s;. }.. if( p-
e6df0 3e 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 >pUnused ){.
e6e00 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 20 3d p->pUnused->fd =
e6e10 20 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 fd;. p->pUnu
e6e20 73 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 sed->flags = fla
e6e30 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 gs;. }.. if( i
e6e40 73 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f sDelete ){.#if O
e6e50 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50 S_VXWORKS. zP
e6e60 61 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c ath = zName;.#el
e6e70 73 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e se. unlink(zN
e6e80 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d ame);.#endif. }
e6e90 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
e6ea0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e6eb0 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e . else{. p->
e6ec0 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e openFlags = open
e6ed0 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 Flags;. }.#endi
e6ee0 66 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44 f.. if( isOpenD
e6ef0 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 irectory ){.
e6f00 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f rc = openDirecto
e6f10 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64 ry(zPath, &dirfd
e6f20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
e6f30 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e6f40 20 20 2f 2a 20 49 74 20 69 73 20 73 61 66 65 20 /* It is safe
e6f50 74 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 20 74 to close fd at t
e6f60 68 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 61 75 his point, becau
e6f70 73 65 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 se it is guarant
e6f80 65 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a eed not. **
e6f90 20 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e 20 61 to be open on a
e6fa0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
e6fb0 49 66 20 69 74 20 77 65 72 65 20 6f 70 65 6e 20 If it were open
e6fc0 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 on a database fi
e6fd0 6c 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 le,. ** it
e6fe0 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 66 would not be saf
e6ff0 65 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 74 68 e to close as th
e7000 69 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 73 65 is would release
e7010 20 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 0a any locks held.
e7020 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 ** on the
e7030 66 69 6c 65 20 62 79 20 74 68 69 73 20 70 72 6f file by this pro
e7040 63 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 cess. */.
e7050 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 assert( eType!=S
e7060 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
e7070 44 42 20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 DB );. clos
e7080 65 28 66 64 29 3b 20 20 20 20 20 20 20 20 20 20 e(fd);
e7090 20 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c /* silently l
e70a0 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c 72 eak if fail, alr
e70b0 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f eady in error */
e70c0 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e . goto open
e70d0 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d _finished;. }
e70e0 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f . }..#ifdef FD_
e70f0 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 CLOEXEC. fcntl(
e7100 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e fd, F_SETFD, fcn
e7110 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 tl(fd, F_GETFD,
e7120 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 0) | FD_CLOEXEC)
e7130 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f ;.#endif.. noLo
e7140 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 ck = eType!=SQLI
e7150 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b TE_OPEN_MAIN_DB;
e7160 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 45 ..#if SQLITE_PRE
e7170 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e FER_PROXY_LOCKIN
e7180 47 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d 4e G. if( zPath!=N
e7190 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 26 ULL && !noLock &
e71a0 26 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 7b & pVfs->xOpen ){
e71b0 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 6f . char *envfo
e71c0 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 51 rce = getenv("SQ
e71d0 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 LITE_FORCE_PROXY
e71e0 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20 _LOCKING");.
e71f0 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30 int useProxy = 0
e7200 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 ;.. /* SQLITE
e7210 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 _FORCE_PROXY_LOC
e7220 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f KING==1 means fo
e7230 72 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70 rce always use p
e7240 72 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a 20 roxy, 0 means .
e7250 20 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 20 ** never use
e7260 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e proxy, NULL mean
e7270 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 20 s use proxy for
e7280 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20 non-local files
e7290 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66 only. */. if
e72a0 28 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c ( envforce!=NULL
e72b0 20 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f ){. usePro
e72c0 78 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72 xy = atoi(envfor
e72d0 63 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 ce)>0;. }else
e72e0 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 {. struct s
e72f0 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 tatfs fsInfo;.
e7300 20 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a if( statfs(z
e7310 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d Path, &fsInfo) =
e7320 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 = -1 ){.
e7330 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 /* In theory, th
e7340 65 20 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c e close(fd) call
e7350 20 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e is sub-optimal.
e7360 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 If the file ope
e7370 6e 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 ned. ** w
e7380 69 74 68 20 66 64 20 69 73 20 61 20 64 61 74 61 ith fd is a data
e7390 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74 base file, and t
e73a0 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 63 here are other c
e73b0 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a onnections open.
e73c0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 ** on th
e73d0 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 at file that are
e73e0 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 currently holdi
e73f0 6e 67 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b ng advisory lock
e7400 73 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20 s on it,.
e7410 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c ** then the cal
e7420 6c 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c l to close() wil
e7430 6c 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c l cancel those l
e7440 6f 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63 ocks. In practic
e7450 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 e,. ** we
e7460 27 72 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 're assuming tha
e7470 74 20 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e t statfs() doesn
e7480 27 74 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 't fail very oft
e7490 65 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20 en. At least.
e74a0 20 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c ** not whil
e74b0 65 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 e other file des
e74c0 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 criptors opened
e74d0 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 by the same proc
e74e0 65 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a ess on. *
e74f0 2a 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 * the same file
e7500 61 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f are working. */
e7510 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74 . p->last
e7520 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e7530 20 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64 if( dirfd
e7540 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 >=0 ){.
e7550 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f close(dirfd); /
e7560 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 * silently leak
e7570 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f if fail, in erro
e7580 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 r */. }.
e7590 20 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 close(fd)
e75a0 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 ; /* silently le
e75b0 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 ak if fail, in e
e75c0 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 rror */.
e75d0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
e75e0 52 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 20 R_ACCESS;.
e75f0 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 goto open_fini
e7600 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 shed;. }.
e7610 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 21 useProxy = !
e7620 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 (fsInfo.f_flags&
e7630 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 MNT_LOCAL);.
e7640 7d 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 6f }. if( usePro
e7650 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d xy ){. rc =
e7660 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 fillInUnixFile(
e7670 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c pVfs, fd, dirfd,
e7680 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e pFile, zPath, n
e7690 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 oLock, isDelete)
e76a0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
e76b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
e76c0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 rc = proxyT
e76d0 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 ransformUnixFile
e76e0 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c ((unixFile*)pFil
e76f0 65 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 e, ":auto:");.
e7700 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f }. goto
e7710 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a open_finished;.
e7720 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
e7730 0a 20 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 . . rc = fillI
e7740 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 nUnixFile(pVfs,
e7750 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 fd, dirfd, pFile
e7760 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c , zPath, noLock,
e7770 20 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 65 6e isDelete);.open
e7780 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 _finished:. if(
e7790 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
e77a0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 {. sqlite3_fr
e77b0 65 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b 0a ee(p->pUnused);.
e77c0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
e77d0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 .}.../*.** Delet
e77e0 65 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 e the file at zP
e77f0 61 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 ath. If the dirS
e7800 79 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 ync argument is
e7810 74 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a true, fsync().**
e7820 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 the directory a
e7830 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68 fter deleting th
e7840 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 e file..*/.stati
e7850 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 c int unixDelete
e7860 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
e7870 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a *NotUsed, /*
e7880 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 VFS containing
e7890 74 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c this as the xDel
e78a0 65 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ete method */.
e78b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
e78c0 68 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d h, /* Nam
e78d0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 e of file to be
e78e0 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 deleted */. int
e78f0 20 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20 dirSync
e7900 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 /* If tru
e7910 65 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63 e, fsync() direc
e7920 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 tory after delet
e7930 69 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 ing file */.){.
e7940 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
e7950 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 _OK;. UNUSED_PA
e7960 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e7970 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
e7980 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
e7990 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b E_IOERR_DELETE);
e79a0 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 . unlink(zPath)
e79b0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
e79c0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 _DISABLE_DIRSYNC
e79d0 0a 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 . if( dirSync )
e79e0 7b 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 {. int fd;.
e79f0 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 rc = openDirec
e7a00 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 tory(zPath, &fd)
e7a10 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
e7a20 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f LITE_OK ){.#if O
e7a30 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 S_VXWORKS.
e7a40 69 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d if( fsync(fd)==-
e7a50 31 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 1 ).#else.
e7a60 69 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a if( fsync(fd) ).
e7a70 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 #endif. {.
e7a80 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
e7a90 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 TE_IOERR_DIR_FSY
e7aa0 4e 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 NC;. }.
e7ab0 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26 if( close(fd)&
e7ac0 26 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 &!rc ){.
e7ad0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
e7ae0 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 R_DIR_CLOSE;.
e7af0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
e7b00 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 endif. return r
e7b10 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 c;.}../*.** Test
e7b20 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f the existance o
e7b30 66 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d f or access perm
e7b40 69 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 issions of file
e7b50 7a 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 zPath. The.** te
e7b60 73 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 st performed dep
e7b70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 ends on the valu
e7b80 65 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a e of flags:.**.*
e7b90 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 * SQLITE_ACC
e7ba0 45 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 ESS_EXISTS: Retu
e7bb0 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 rn 1 if the file
e7bc0 20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 exists.** S
e7bd0 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
e7be0 44 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 DWRITE: Return 1
e7bf0 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 if the file is
e7c00 72 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c read and writabl
e7c10 65 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 e..** SQLITE
e7c20 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 _ACCESS_READONLY
e7c30 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 : Return 1 if th
e7c40 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 e file is readab
e7c50 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 le..**.** Otherw
e7c60 69 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f ise return 0..*/
e7c70 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e7c80 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 Access(. sqlite
e7c90 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
e7ca0 20 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e /* The VFS con
e7cb0 74 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63 taining this xAc
e7cc0 63 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 cess method */.
e7cd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
e7ce0 74 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68 th, /* Path
e7cf0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 of the file to
e7d00 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 examine */. int
e7d10 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
e7d20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 /* What do
e7d30 77 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e we want to learn
e7d40 20 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68 about the zPath
e7d50 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 file? */. int
e7d60 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 *pResOut
e7d70 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 /* Write res
e7d80 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 ult boolean here
e7d90 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f */.){. int amo
e7da0 64 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 de = 0;. UNUSED
e7db0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
e7dc0 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 ed);. SimulateI
e7dd0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
e7de0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 QLITE_IOERR_ACCE
e7df0 53 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 SS; );. switch(
e7e00 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 flags ){. ca
e7e10 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
e7e20 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 _EXISTS:. a
e7e30 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 mode = F_OK;.
e7e40 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
e7e50 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 se SQLITE_ACCESS
e7e60 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 _READWRITE:.
e7e70 20 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 amode = W_OK|R
e7e80 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b _OK;. break
e7e90 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 ;. case SQLIT
e7ea0 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 E_ACCESS_READ:.
e7eb0 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f amode = R_O
e7ec0 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a K;. break;.
e7ed0 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 . default:.
e7ee0 20 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 assert(!"Inv
e7ef0 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d alid flags argum
e7f00 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 ent");. }. *pR
e7f10 65 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 esOut = (access(
e7f20 7a 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 zPath, amode)==0
e7f30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
e7f40 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a TE_OK;.}.../*.**
e7f50 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 Turn a relative
e7f60 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 pathname into a
e7f70 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 full pathname.
e7f80 54 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 The relative pat
e7f90 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 h.** is stored a
e7fa0 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 s a nul-terminat
e7fb0 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 ed string in the
e7fc0 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 buffer pointed
e7fd0 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 to by.** zPath.
e7fe0 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e .**.** zOut poin
e7ff0 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f ts to a buffer o
e8000 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 f at least sqlit
e8010 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d e3_vfs.mxPathnam
e8020 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 e bytes .** (in
e8030 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 this case, MAX_P
e8040 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 ATHNAME bytes).
e8050 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 The full-path is
e8060 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 written to.** t
e8070 68 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 his buffer befor
e8080 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a e returning..*/.
e8090 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 static int unixF
e80a0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 ullPathname(. s
e80b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
e80c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
e80d0 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f Pointer to vfs o
e80e0 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 bject */. const
e80f0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 char *zPath,
e8100 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 /* Poss
e8110 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e ibly relative in
e8120 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e put path */. in
e8130 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 t nOut,
e8140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
e8150 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 ize of output bu
e8160 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f ffer in bytes */
e8170 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 . char *zOut
e8180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8190 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 /* Output buffe
e81a0 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 r */.){.. /* It
e81b0 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 's odd to simula
e81c0 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 te an io-error h
e81d0 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 ere, but really
e81e0 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a this is just. *
e81f0 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 * using the io-e
e8200 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 rror infrastruct
e8210 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 ure to test that
e8220 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 SQLite handles
e8230 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 this. ** functi
e8240 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 on failing. This
e8250 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 function could
e8260 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 fail if, for exa
e8270 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 mple, the. ** c
e8280 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 urrent working d
e8290 69 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 irectory has bee
e82a0 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f n unlinked.. */
e82b0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
e82c0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
e82d0 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 E_ERROR );.. as
e82e0 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 sert( pVfs->mxPa
e82f0 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 thname==MAX_PATH
e8300 4e 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 NAME );. UNUSED
e8310 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
e8320 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 ;.. zOut[nOut-1
e8330 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 ] = '\0';. if(
e8340 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b zPath[0]=='/' ){
e8350 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
e8360 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 rintf(nOut, zOut
e8370 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a , "%s", zPath);.
e8380 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 }else{. int
e8390 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67 nCwd;. if( g
e83a0 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 etcwd(zOut, nOut
e83b0 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 -1)==0 ){.
e83c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 return SQLITE_CA
e83d0 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 NTOPEN;. }.
e83e0 20 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74 nCwd = (int)st
e83f0 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 rlen(zOut);.
e8400 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
e8410 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 (nOut-nCwd, &zOu
e8420 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 t[nCwd], "/%s",
e8430 7a 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 zPath);. }. re
e8440 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
e8450 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }...#ifndef SQLI
e8460 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
e8470 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 ENSION./*.** Int
e8480 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e erfaces for open
e8490 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 ing a shared lib
e84a0 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e rary, finding en
e84b0 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 try points.** wi
e84c0 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 thin the shared
e84d0 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f library, and clo
e84e0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 sing the shared
e84f0 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 library..*/.#inc
e8500 6c 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 lude <dlfcn.h>.s
e8510 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 tatic void *unix
e8520 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 DlOpen(sqlite3_v
e8530 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e fs *NotUsed, con
e8540 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
e8550 6d 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 me){. UNUSED_PA
e8560 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e8570 3b 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 ;. return dlope
e8580 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c n(zFilename, RTL
e8590 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f D_NOW | RTLD_GLO
e85a0 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 BAL);.}../*.** S
e85b0 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 QLite calls this
e85c0 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 function immedi
e85d0 61 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61 ately after a ca
e85e0 6c 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 ll to unixDlSym(
e85f0 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 ) or.** unixDlOp
e8600 65 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75 en() fails (retu
e8610 72 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 rns a null point
e8620 65 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 er). If a more d
e8630 65 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a etailed error.**
e8640 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 message is avai
e8650 6c 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69 lable, it is wri
e8660 74 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e tten to zBufOut.
e8670 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 If no error mes
e8680 73 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c sage.** is avail
e8690 61 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 able, zBufOut is
e86a0 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 left unmodified
e86b0 20 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73 and SQLite uses
e86c0 20 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 a default.** er
e86d0 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a ror message..*/.
e86e0 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 static void unix
e86f0 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f DlError(sqlite3_
e8700 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e vfs *NotUsed, in
e8710 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
e8720 75 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a ufOut){. char *
e8730 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 zErr;. UNUSED_P
e8740 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
e8750 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 );. unixEnterMu
e8760 74 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 tex();. zErr =
e8770 64 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 dlerror();. if(
e8780 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c zErr ){. sql
e8790 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 ite3_snprintf(nB
e87a0 75 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 uf, zBufOut, "%s
e87b0 22 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 ", zErr);. }.
e87c0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
e87d0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
e87e0 28 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 (*unixDlSym(sqli
e87f0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
e8800 2c 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 , void *p, const
e8810 20 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 char*zSym))(voi
e8820 64 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 d){. /* . ** G
e8830 43 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 CC with -pedanti
e8840 63 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 c-errors says th
e8850 61 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 at C90 does not
e8860 61 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f allow a void* to
e8870 20 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e be. ** cast in
e8880 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 to a pointer to
e8890 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 a function. And
e88a0 20 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 yet the library
e88b0 20 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 dlsym() routine
e88c0 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 . ** returns a
e88d0 76 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 void* which is r
e88e0 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 eally a pointer
e88f0 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 to a function.
e8900 53 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a So how do we. *
e8910 2a 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 * use dlsym() wi
e8920 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 th -pedantic-err
e8930 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 ors?. **. ** V
e8940 61 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 ariable x below
e8950 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 is defined to be
e8960 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
e8970 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a function taking.
e8980 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 ** parameters
e8990 76 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 void* and const
e89a0 63 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e char* and return
e89b0 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ing a pointer to
e89c0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a a function.. *
e89d0 2a 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 * We initialize
e89e0 78 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 x by assigning i
e89f0 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 t a pointer to t
e8a00 68 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 he dlsym() funct
e8a10 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 ion.. ** (That
e8a20 61 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 assignment requi
e8a30 72 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 res a cast.) Th
e8a40 65 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 en we call the f
e8a50 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a unction that. *
e8a60 2a 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 * x points to.
e8a70 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 . **. ** This
e8a80 77 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 work-around is u
e8a90 6e 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 nlikely to work
e8aa0 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 correctly on any
e8ab0 20 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 system where.
e8ac0 2a 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 ** you really ca
e8ad0 6e 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 nnot cast a func
e8ae0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 tion pointer int
e8af0 6f 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 o void*. But th
e8b00 65 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 en, on the. **
e8b10 6f 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 other hand, dlsy
e8b20 6d 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 m() will not wor
e8b30 6b 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 k on such a syst
e8b40 65 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 em either, so we
e8b50 20 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 have. ** not r
e8b60 65 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 eally lost anyth
e8b70 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 ing.. */. void
e8b80 20 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f (*(*x)(void*,co
e8b90 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 nst char*))(void
e8ba0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
e8bb0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
e8bc0 20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 x = (void(*(*)
e8bd0 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 (void*,const cha
e8be0 72 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d r*))(void))dlsym
e8bf0 3b 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 ;. return (*x)(
e8c00 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 p, zSym);.}.stat
e8c10 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c ic void unixDlCl
e8c20 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ose(sqlite3_vfs
e8c30 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a *NotUsed, void *
e8c40 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 pHandle){. UNUS
e8c50 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e8c60 55 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 Used);. dlclose
e8c70 28 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c (pHandle);.}.#el
e8c80 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f se /* if SQLITE_
e8c90 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
e8ca0 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 ION is defined:
e8cb0 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 */. #define uni
e8cc0 78 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 xDlOpen 0. #de
e8cd0 66 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 fine unixDlError
e8ce0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 0. #define uni
e8cf0 78 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 xDlSym 0. #de
e8d00 66 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 fine unixDlClose
e8d10 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
e8d20 20 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 Write nBuf byte
e8d30 73 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 s of random data
e8d40 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 to the supplied
e8d50 20 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f buffer zBuf..*/
e8d60 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e8d70 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 Randomness(sqlit
e8d80 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e8d90 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 int nBuf, char
e8da0 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 *zBuf){. UNUSED
e8db0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
e8dc0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 ed);. assert((s
e8dd0 69 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a ize_t)nBuf>=(siz
e8de0 65 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 eof(time_t)+size
e8df0 6f 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a of(int)));.. /*
e8e00 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 We have to init
e8e10 69 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 ialize zBuf to p
e8e20 72 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 revent valgrind
e8e30 66 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 from reporting.
e8e40 20 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 ** errors. The
e8e50 20 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 reports issued
e8e60 62 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 by valgrind are
e8e70 69 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 incorrect - we w
e8e80 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 ould. ** prefer
e8e90 20 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d that the random
e8ea0 6e 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 ness be increase
e8eb0 64 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 d by making use
e8ec0 6f 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e of the. ** unin
e8ed0 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 itialized space
e8ee0 69 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 in zBuf - but va
e8ef0 6c 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 lgrind errors te
e8f00 6e 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a nd to worry. **
e8f10 20 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 some users. Ra
e8f20 74 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c ther than argue,
e8f30 20 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 it seems easier
e8f40 20 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c just to initial
e8f50 69 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f ize. ** the who
e8f60 6c 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c le array and sil
e8f70 65 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 ence valgrind, e
e8f80 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e ven if that mean
e8f90 73 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 s less randomnes
e8fa0 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 s. ** in the ra
e8fb0 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a ndom seed.. **.
e8fc0 20 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e ** When testin
e8fd0 67 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 g, initializing
e8fe0 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 zBuf[] to zero i
e8ff0 73 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 s all we do. Th
e9000 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 at means. ** th
e9010 61 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 at we always use
e9020 20 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d the same random
e9030 20 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 number sequence
e9040 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 . This makes th
e9050 65 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 e. ** tests rep
e9060 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 eatable.. */.
e9070 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 memset(zBuf, 0,
e9080 6e 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 nBuf);.#if !defi
e9090 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
e90a0 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 . {. int pid
e90b0 2c 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f , fd;. fd = o
e90c0 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f pen("/dev/urando
e90d0 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 m", O_RDONLY);.
e90e0 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 if( fd<0 ){.
e90f0 20 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 time_t t;.
e9100 20 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 time(&t);.
e9110 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 memcpy(zBuf
e9120 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 , &t, sizeof(t))
e9130 3b 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 ;. pid = ge
e9140 74 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 tpid();. me
e9150 6d 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f mcpy(&zBuf[sizeo
e9160 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a f(t)], &pid, siz
e9170 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 eof(pid));.
e9180 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
e9190 74 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d t)+sizeof(pid)<=
e91a0 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a (size_t)nBuf );.
e91b0 20 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a nBuf = siz
e91c0 65 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 eof(t) + sizeof(
e91d0 70 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b pid);. }else{
e91e0 0a 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65 . nBuf = re
e91f0 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 ad(fd, zBuf, nBu
e9200 66 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 f);. close(
e9210 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 fd);. }. }.#
e9220 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e endif. return n
e9230 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 Buf;.}.../*.** S
e9240 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c leep for a littl
e9250 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e e while. Return
e9260 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 the amount of t
e9270 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 ime slept..** Th
e9280 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 e argument is th
e9290 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 e number of micr
e92a0 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 oseconds we want
e92b0 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 to sleep..** Th
e92c0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 e return value i
e92d0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
e92e0 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 microseconds of
e92f0 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a sleep actually.*
e9300 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d * requested from
e9310 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
e9320 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
e9330 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 , a number which
e9340 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 .** might be gre
e9350 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
e9360 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 al to the argume
e9370 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 nt, but not less
e9380 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 .** than the arg
e9390 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 ument..*/.static
e93a0 20 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73 int unixSleep(s
e93b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
e93c0 73 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 sed, int microse
e93d0 63 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 conds){.#if OS_V
e93e0 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 XWORKS. struct
e93f0 74 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 timespec sp;..
e9400 73 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 sp.tv_sec = micr
e9410 6f 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 oseconds / 10000
e9420 30 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 00;. sp.tv_nsec
e9430 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 = (microseconds
e9440 20 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30 % 1000000) * 10
e9450 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 00;. nanosleep(
e9460 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e &sp, NULL);. UN
e9470 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e9480 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
e9490 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a n microseconds;.
e94a0 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 #elif defined(HA
e94b0 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 VE_USLEEP) && HA
e94c0 56 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 VE_USLEEP. usle
e94d0 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 ep(microseconds)
e94e0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
e94f0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e9500 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 return microsec
e9510 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e onds;.#else. in
e9520 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 t seconds = (mic
e9530 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 roseconds+999999
e9540 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 )/1000000;. sle
e9550 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 ep(seconds);. U
e9560 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e9570 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 NotUsed);. retu
e9580 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 rn seconds*10000
e9590 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 00;.#endif.}../*
e95a0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
e95b0 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 g variable, if s
e95c0 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f et to a non-zero
e95d0 20 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 value, is inter
e95e0 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65 preted as.** the
e95f0 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e number of secon
e9600 64 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e ds since 1970 an
e9610 64 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 d is used to set
e9620 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a the result of.*
e9630 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 * sqlite3OsCurre
e9640 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 ntTime() during
e9650 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 testing..*/.#ifd
e9660 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
e9670 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e9680 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
e9690 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 me = 0; /* Fake
e96a0 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 system time in
e96b0 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 seconds since 19
e96c0 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 70. */.#endif../
e96d0 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 *.** Find the cu
e96e0 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 rrent time (in U
e96f0 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e niversal Coordin
e9700 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 ated Time). Wri
e9710 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e te the.** curren
e9720 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 t time and date
e9730 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 as a Julian Day
e9740 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e number into *prN
e9750 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e ow and.** return
e9760 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 0. Return 1 if
e9770 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 the time and da
e9780 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 te cannot be fou
e9790 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nd..*/.static in
e97a0 74 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d t unixCurrentTim
e97b0 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e e(sqlite3_vfs *N
e97c0 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a otUsed, double *
e97d0 70 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69 prNow){.#if defi
e97e0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
e97f0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a FLOATING_POINT).
e9800 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 time_t t;. ti
e9810 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 me(&t);. *prNow
e9820 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e = (((sqlite3_in
e9830 74 36 34 29 74 29 2f 38 36 34 30 20 2b 20 32 34 t64)t)/8640 + 24
e9840 34 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 6c 69 405875)/10;.#eli
e9850 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 f defined(NO_GET
e9860 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b TOD). time_t t;
e9870 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a . time(&t);. *
e9880 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e prNow = t/86400.
e9890 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 0 + 2440587.5;.#
e98a0 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a elif OS_VXWORKS.
e98b0 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 struct timespe
e98c0 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f c sNow;. clock_
e98d0 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 gettime(CLOCK_RE
e98e0 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a ALTIME, &sNow);.
e98f0 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 *prNow = 24405
e9900 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 87.5 + sNow.tv_s
e9910 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f ec/86400.0 + sNo
e9920 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 w.tv_nsec/864000
e9930 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 00000000.0;.#els
e9940 65 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76 e. struct timev
e9950 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 al sNow;. getti
e9960 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 meofday(&sNow, 0
e9970 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 );. *prNow = 24
e9980 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 40587.5 + sNow.t
e9990 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 v_sec/86400.0 +
e99a0 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 sNow.tv_usec/864
e99b0 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 00000000.0;.#end
e99c0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
e99d0 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c E_TEST. if( sql
e99e0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d ite3_current_tim
e99f0 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 e ){. *prNow
e9a00 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e = sqlite3_curren
e9a10 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b t_time/86400.0 +
e9a20 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 2440587.5;. }.
e9a30 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f #endif. UNUSED_
e9a40 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e9a50 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a d);. return 0;.
e9a60 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 }../*.** We adde
e9a70 64 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 d the xGetLastEr
e9a80 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 ror() method wit
e9a90 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 h the intention
e9aa0 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 of providing.**
e9ab0 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c better low-level
e9ac0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
e9ad0 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 when operating-s
e9ae0 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 ystem problems c
e9af0 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 ome up.** during
e9b00 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f SQLite operatio
e9b10 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 n. But so far,
e9b20 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 none of that has
e9b30 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 been implemente
e9b40 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 d.** in the core
e9b50 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 . So this routi
e9b60 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c ne is never call
e9b70 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 ed. For now, it
e9b80 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 is merely.** a
e9b90 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f place-holder..*/
e9ba0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e9bb0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c GetLastError(sql
e9bc0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 ite3_vfs *NotUse
e9bd0 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c d, int NotUsed2,
e9be0 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 char *NotUsed3)
e9bf0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
e9c00 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e9c10 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e9c20 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 R(NotUsed2);. U
e9c30 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e9c40 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 NotUsed3);. ret
e9c50 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a urn 0;.}../*.***
e9c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9c70 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c ***** End of sql
e9c80 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 ite3_vfs methods
e9c90 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
e9ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
e9cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
e9d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
e9d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9d60 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 ******** Begin P
e9d70 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a roxy Locking ***
e9d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e9d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
e9da0 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e .** Proxy lockin
e9db0 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 g is a "uber-loc
e9dc0 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 king-method" in
e9dd0 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20 this sense: It
e9de0 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 uses the.** othe
e9df0 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 r locking method
e9e00 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c s on secondary l
e9e10 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 ock files. Prox
e9e20 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a y locking is a.*
e9e30 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 * meta-layer ove
e9e40 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69 r top of the pri
e9e50 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 mitive locking i
e9e60 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 mplemented above
e9e70 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 . For.** this r
e9e80 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 eason, the divis
e9e90 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ion that impleme
e9ea0 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 nts of proxy loc
e9eb0 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 king is deferred
e9ec0 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 .** until late i
e9ed0 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 65 n the file (here
e9ee0 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 ) after all of t
e9ef0 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 he other I/O met
e9f00 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 hods have.** bee
e9f10 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 n defined - so t
e9f20 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 hat the primitiv
e9f30 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 e locking method
e9f40 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a s are available.
e9f50 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74 ** as services t
e9f60 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 o help with the
e9f70 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
e9f80 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e f proxy locking.
e9f90 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 .**.****.**.** T
e9fa0 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 he default locki
e9fb0 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51 ng schemes in SQ
e9fc0 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 61 Lite use byte-ra
e9fd0 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 nge locks on the
e9fe0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c .** database fil
e9ff0 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 e to coordinate
ea000 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 safe, concurrent
ea010 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 access by multi
ea020 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 ple readers.** a
ea030 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 70 nd writers [http
ea040 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f ://sqlite.org/lo
ea050 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 ckingv3.html].
ea060 54 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f The five file lo
ea070 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 cking.** states
ea080 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 (UNLOCKED, PENDI
ea090 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 NG, SHARED, RESE
ea0a0 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29 RVED, EXCLUSIVE)
ea0b0 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 are implemented
ea0c0 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61 .** as POSIX rea
ea0d0 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 d & write locks
ea0e0 6f 76 65 72 20 66 69 78 65 64 20 73 65 74 20 6f over fixed set o
ea0f0 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 f locations (via
ea100 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 fsctl),.** on A
ea110 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 FP and SMB only
ea120 65 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 exclusive byte-r
ea130 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 ange locks are a
ea140 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63 vailable via fsc
ea150 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 tl.** with _IOWR
ea160 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 ('z', 23, struct
ea170 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 ByteRangeLockPB
ea180 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20 2) to track the
ea190 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a same 5 states..*
ea1a0 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 * To simulate a
ea1b0 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 F_RDLCK on the s
ea1c0 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 hared range, on
ea1d0 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 AFP a randomly s
ea1e0 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 elected.** addre
ea1f0 73 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 64 ss in the shared
ea200 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 range is taken
ea210 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63 for a SHARED loc
ea220 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a k, the entire.**
ea230 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 shared range is
ea240 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 taken for an EX
ea250 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a CLUSIVE lock):.*
ea260 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e *.** PENDIN
ea270 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 78 G_BYTE 0x
ea280 34 30 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a 40000000.. ..*
ea290 2a 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f * RESERVED_
ea2a0 42 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30 BYTE 0x400
ea2b0 30 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 00001.** SH
ea2c0 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20 ARED_RANGE
ea2d0 20 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20 0x40000002 ->
ea2e0 30 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 0x40000200.**.**
ea2f0 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c This works well
ea300 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 on the local fi
ea310 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73 le system, but s
ea320 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30 hows a nearly 10
ea330 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 0x.** slowdown i
ea340 6e 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e n read performan
ea350 63 65 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73 ce on AFP becaus
ea360 65 20 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74 e the AFP client
ea370 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 disables.** the
ea380 20 72 65 61 64 20 63 61 63 68 65 20 77 68 65 6e read cache when
ea390 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b byte-range lock
ea3a0 73 20 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20 s are present.
ea3b0 45 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61 Enabling the rea
ea3c0 64 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73 d.** cache expos
ea3d0 65 73 20 61 20 63 61 63 68 65 20 63 6f 68 65 72 es a cache coher
ea3e0 65 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 ency problem tha
ea3f0 74 20 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 t is present on
ea400 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 all OS X.** supp
ea410 6f 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 orted network fi
ea420 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 le systems. NFS
ea430 20 61 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62 and AFP both ob
ea440 73 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f serve the.** clo
ea450 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e se-to-open seman
ea460 74 69 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e tics for ensurin
ea470 67 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 g cache coherenc
ea480 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 y.** [http://nfs
ea490 2e 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 .sourceforge.net
ea4a0 2f 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 /#faq_a8], which
ea4b0 20 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74 does not effect
ea4c0 69 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 ively.** address
ea4d0 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 the requirement
ea4e0 73 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 s for concurrent
ea4f0 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 database access
ea500 20 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 by multiple.**
ea510 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 readers and writ
ea520 65 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 ers.** [http://w
ea530 77 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 ww.nabble.com/SQ
ea540 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 Lite-on-NFS-cach
ea550 65 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 e-coherency-td15
ea560 36 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 655701.html]..**
ea570 0a 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74 .** To address t
ea580 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 he performance a
ea590 6e 64 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e nd cache coheren
ea5a0 63 79 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79 cy issues, proxy
ea5b0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a file locking.**
ea5c0 20 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 changes the way
ea5d0 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 database access
ea5e0 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 is controlled b
ea5f0 79 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 y limiting acces
ea600 73 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 s to a.** single
ea610 20 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 host at a time
ea620 61 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 and moving file
ea630 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 locks off of the
ea640 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a database file.*
ea650 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f * and onto a pro
ea660 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c xy file on the l
ea670 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d ocal file system
ea680 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 . .**.**.** Usi
ea690 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a ng proxy locks.*
ea6a0 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
ea6b0 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 ---.**.** C APIs
ea6c0 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f .**.** sqlite3_
ea6d0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c file_control(db,
ea6e0 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f dbname, SQLITE_
ea6f0 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c SET_LOCKPROXYFIL
ea700 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 E,.**
ea710 20 20 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f <pro
ea720 78 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 xy_path> | ":aut
ea730 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 o:");.** sqlite
ea740 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 3_file_control(d
ea750 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 b, dbname, SQLIT
ea760 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_GET_LOCKPROXYF
ea770 49 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 ILE, &<proxy_pat
ea780 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 h>);.**.**.** SQ
ea790 4c 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 L pragmas.**.**
ea7a0 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 PRAGMA [databas
ea7b0 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 e.]lock_proxy_fi
ea7c0 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 le=<proxy_path>
ea7d0 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 | :auto:.** PRA
ea7e0 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c GMA [database.]l
ea7f0 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a ock_proxy_file.*
ea800 2a 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20 *.** Specifying
ea810 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 ":auto:" means t
ea820 68 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 hat if there is
ea830 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 a conch file wit
ea840 68 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 h a matching.**
ea850 68 6f 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74 host ID in it, t
ea860 68 65 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e he proxy path in
ea870 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
ea880 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 will be used, ot
ea890 68 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f herwise.** a pro
ea8a0 78 79 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e xy path based on
ea8b0 20 74 68 65 20 75 73 65 72 27 73 20 74 65 6d 70 the user's temp
ea8c0 20 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e dir.** (via con
ea8d0 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f fstr(_CS_DARWIN_
ea8e0 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e USER_TEMP_DIR,..
ea8f0 2e 29 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64 .)) will be used
ea900 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 and the.** actu
ea910 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 al proxy file na
ea920 6d 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 me is generated
ea930 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e from the name an
ea940 64 20 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a d path of the.**
ea950 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
ea960 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a For example:.**
ea970 0a 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61 .** For da
ea980 74 61 62 61 73 65 20 70 61 74 68 20 22 2f 55 73 tabase path "/Us
ea990 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a ers/me/foo.db" .
ea9a0 2a 2a 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63 ** The loc
ea9b0 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22 k path will be "
ea9c0 3c 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 <tmpdir>/sqlitep
ea9d0 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f locks/_Users_me_
ea9e0 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a foo.db:auto:").*
ea9f0 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b *.** Once a lock
eaa00 20 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 proxy is config
eaa10 75 72 65 64 20 66 6f 72 20 61 20 64 61 74 61 62 ured for a datab
eaa20 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 ase connection,
eaa30 69 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 it can not.** be
eaa40 20 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 removed, howeve
eaa50 72 20 69 74 20 6d 61 79 20 62 65 20 73 77 69 74 r it may be swit
eaa60 63 68 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 ched to a differ
eaa70 65 6e 74 20 70 72 6f 78 79 20 70 61 74 68 20 76 ent proxy path v
eaa80 69 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 ia.** the above
eaa90 41 50 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 APIs (assuming t
eaaa0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 he conch file is
eaab0 20 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 not being held
eaac0 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f by another.** co
eaad0 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 nnection or proc
eaae0 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 ess). .**.**.**
eaaf0 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e How proxy lockin
eab00 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d g works.** -----
eab10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
eab20 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 --.**.** Proxy f
eab30 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 ile locking reli
eab40 65 73 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 es primarily on
eab50 74 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69 two new supporti
eab60 6e 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a ng files: .**.**
eab70 20 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 * conch file
eab80 20 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73 to limit access
eab90 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
eaba0 20 66 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c file to a singl
eabb0 65 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 e host.** a
eabc0 74 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 t a time.**.**
eabd0 20 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74 * proxy file t
eabe0 6f 20 61 63 74 20 61 73 20 61 20 70 72 6f 78 79 o act as a proxy
eabf0 20 66 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72 for the advisor
eac00 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 y locks normally
eac10 0a 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f .** taken o
eac20 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a n the database.*
eac30 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 *.** The conch f
eac40 69 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70 ile - to use a p
eac50 72 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 roxy file, sqlit
eac60 65 20 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f e must first "ho
eac70 6c 64 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a ld the conch".**
eac80 20 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 by taking an sq
eac90 6c 69 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 lite-style share
eaca0 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f d lock on the co
eacb0 6e 63 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e nch file, readin
eacc0 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 g the.** content
eacd0 73 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 s and comparing
eace0 74 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 the host's uniqu
eacf0 65 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 e host ID (see b
ead00 65 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a elow) and lock.*
ead10 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61 * proxy path aga
ead20 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20 inst the values
ead30 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f stored in the co
ead40 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 nch. The conch
ead50 66 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 file is.** store
ead60 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 d in the same di
ead70 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 20 64 rectory as the d
ead80 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
ead90 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a the file name.*
eada0 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61 * is patterned a
eadb0 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73 fter the databas
eadc0 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 e file name as "
eadd0 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d .<databasename>-
eade0 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 conch"..** If th
eadf0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 e conch file doe
eae00 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 s not exist, or
eae10 69 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f it's contents do
eae20 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a not match the.*
eae30 2a 20 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 * host ID and/or
eae40 20 70 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65 proxy path, the
eae50 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 n the lock is es
eae60 63 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78 calated to an ex
eae70 63 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 clusive.** lock
eae80 61 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69 and the conch fi
eae90 6c 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 le contents is u
eaea0 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 pdated with the
eaeb0 68 6f 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78 host ID and prox
eaec0 79 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68 y.** path and th
eaed0 65 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 e lock is downgr
eaee0 61 64 65 64 20 74 6f 20 61 20 73 68 61 72 65 64 aded to a shared
eaef0 20 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 lock again. If
eaf00 20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 the conch.** is
eaf10 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 held by another
eaf20 20 70 72 6f 63 65 73 73 20 28 77 69 74 68 20 61 process (with a
eaf30 20 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 shared lock), t
eaf40 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 he exclusive loc
eaf50 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 k.** will fail a
eaf60 6e 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 nd SQLITE_BUSY i
eaf70 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
eaf80 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 * The proxy file
eaf90 20 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 - a single-byte
eafa0 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61 file used for a
eafb0 6c 6c 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65 ll advisory file
eafc0 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c locks.** normal
eafd0 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 ly taken on the
eafe0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
eaff0 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 This allows for
eb000 20 73 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a safe sharing.**
eb010 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
eb020 20 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 file for multip
eb030 6c 65 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 le readers and w
eb040 72 69 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61 riters on the sa
eb050 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 me.** host (the
eb060 63 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68 conch ensures th
eb070 61 74 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20 at they all use
eb080 74 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c the same local l
eb090 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a ock file)..**.**
eb0a0 20 54 68 65 72 65 20 69 73 20 61 20 74 68 69 72 There is a thir
eb0b0 64 20 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 d file - the hos
eb0c0 74 20 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64 t ID file - used
eb0d0 20 61 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 as a persistent
eb0e0 20 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 record.** of a
eb0f0 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 unique identifie
eb100 72 20 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 r for the host,
eb110 61 20 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75 a 128-byte uniqu
eb120 65 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a e host id file.*
eb130 2a 20 69 6e 20 74 68 65 20 70 61 74 68 20 64 65 * in the path de
eb140 66 69 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53 fined by the HOS
eb150 54 49 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64 TIDPATH macro (d
eb160 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a efault value is.
eb170 2a 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 ** /Library/Cach
eb180 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 es/.com.apple.sq
eb190 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 liteConchHostId)
eb1a0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 ..**.** Requesti
eb1b0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 ng the lock prox
eb1c0 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 y does not immed
eb1d0 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20 iately take the
eb1e0 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 conch, it is.**
eb1f0 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 only taken when
eb200 74 68 65 20 66 69 72 73 74 20 72 65 71 75 65 73 the first reques
eb210 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 t to lock databa
eb220 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e se file is made.
eb230 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 .** This match
eb240 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 es the semantics
eb250 20 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 6f of the traditio
eb260 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 nal locking beha
eb270 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f vior, where.** o
eb280 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 pening a connect
eb290 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 ion to a databas
eb2a0 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 e file does not
eb2b0 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 take a lock on i
eb2c0 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 t..** The shared
eb2d0 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 lock and an ope
eb2e0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
eb2f0 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 r are maintained
eb300 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 until .** the c
eb310 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 onnection to the
eb320 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f database is clo
eb330 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sed. .**.** The
eb340 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 proxy file and t
eb350 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 he lock file are
eb360 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 never deleted s
eb370 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 o they only need
eb380 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65 .** to be create
eb390 64 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 d the first time
eb3a0 20 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a they are used..
eb3b0 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 **.** Configurat
eb3c0 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d ion options.** -
eb3d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
eb3e0 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 ----.**.** SQLI
eb3f0 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f TE_PREFER_PROXY_
eb400 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 LOCKING.**.**
eb410 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c Database fil
eb420 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e es accessed on n
eb430 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 on-local file sy
eb440 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 stems are.**
eb450 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 automatically
eb460 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 configured for
eb470 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c proxy locking, l
eb480 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a ock files are.**
eb490 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74 named aut
eb4a0 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 omatically using
eb4b0 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 the same logic
eb4c0 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47 as.** PRAG
eb4d0 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 MA lock_proxy_fi
eb4e0 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 le=":auto:".**
eb4f0 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 .** SQLITE_PR
eb500 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 OXY_DEBUG.**.**
eb510 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 Enables th
eb520 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 e logging of err
eb530 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 or messages duri
eb540 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a ng host id file.
eb550 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76 ** retriev
eb560 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a al and creation.
eb570 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 **.** HOSTIDPAT
eb580 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 H.**.** Ov
eb590 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 errides the defa
eb5a0 75 6c 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65 ult host ID file
eb5b0 20 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a path location.*
eb5c0 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 *.** LOCKPROXYD
eb5d0 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f IR.**.** O
eb5e0 76 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 verrides the def
eb5f0 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75 ault directory u
eb600 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f sed for lock pro
eb610 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a xy files that.**
eb620 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64 are named
eb630 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 automatically v
eb640 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 ia the ":auto:"
eb650 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 setting.**.** S
eb660 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 QLITE_DEFAULT_PR
eb670 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f OXYDIR_PERMISSIO
eb680 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 NS.**.** P
eb690 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 ermissions to us
eb6a0 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 e when creating
eb6b0 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20 a directory for
eb6c0 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 storing the.**
eb6d0 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 lock proxy
eb6e0 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 files, only used
eb6f0 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 when LOCKPROXYD
eb700 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a IR is not set..*
eb710 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 * .** .**
eb720 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f As mentioned abo
eb730 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 ve, when compile
eb740 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52 d with SQLITE_PR
eb750 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 EFER_PROXY_LOCKI
eb760 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 NG,.** setting t
eb770 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 he environment v
eb780 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 ariable SQLITE_F
eb790 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 ORCE_PROXY_LOCKI
eb7a0 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 NG to 1 will.**
eb7b0 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b force proxy lock
eb7c0 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 ing to be used f
eb7d0 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 or every databas
eb7e0 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 e file opened, a
eb7f0 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 nd 0.** will for
eb800 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f ce automatic pro
eb810 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 xy locking to be
eb820 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c disabled for al
eb830 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 l database.** fi
eb840 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79 20 63 les (explicity c
eb850 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54 alling the SQLIT
eb860 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_SET_LOCKPROXYF
eb870 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a ILE pragma or.**
eb880 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e sqlite_file_con
eb890 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 trol API is not
eb8a0 61 66 66 65 63 74 65 64 20 62 79 20 53 51 4c 49 affected by SQLI
eb8b0 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c TE_FORCE_PROXY_L
eb8c0 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a OCKING)..*/../*.
eb8d0 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 ** Proxy locking
eb8e0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
eb8f0 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f le on MacOSX .*/
eb900 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 .#if defined(__A
eb910 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 PPLE__) && SQLIT
eb920 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
eb930 5f 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 _STYLE..#ifdef S
eb940 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 QLITE_TEST./* si
eb950 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 mulate multiple
eb960 68 6f 73 74 73 20 62 79 20 63 72 65 61 74 69 6e hosts by creatin
eb970 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 g unique hostid
eb980 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 file paths */.SQ
eb990 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
eb9a0 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 ite3_hostid_num
eb9b0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
eb9c0 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b ** The proxyLock
eb9d0 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 ingContext has t
eb9e0 68 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 he path and file
eb9f0 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 structures for
eba00 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 the remote .** a
eba10 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 nd local proxy f
eba20 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 iles in it.*/.ty
eba30 70 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f pedef struct pro
eba40 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
eba50 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
eba60 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f text;.struct pro
eba70 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
eba80 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 {. unixFile *c
eba90 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 onchFile;
ebaa0 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 /* Open conch
ebab0 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a file */. char *
ebac0 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 conchFilePath;
ebad0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
ebae0 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 f the conch file
ebaf0 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a */. unixFile *
ebb00 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 lockProxy;
ebb10 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 /* Open proxy
ebb20 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 lock file */.
ebb30 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 char *lockProxyP
ebb40 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ath; /*
ebb50 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 Name of the prox
ebb60 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 y lock file */.
ebb70 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 char *dbPath;
ebb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ebb90 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 Name of the ope
ebba0 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 n file */. int
ebbb0 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 conchHeld;
ebbc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
ebbd0 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 if the conch is
ebbe0 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 currently held
ebbf0 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f */. void *oldLo
ebc00 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 ckingContext;
ebc10 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f /* Original lo
ebc20 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 ckingcontext to
ebc30 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 restore on close
ebc40 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f */. sqlite3_io
ebc50 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a _methods const *
ebc60 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 pOldMethod;
ebc70 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 /* Original I/O
ebc80 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 methods for clos
ebc90 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 e */.};../* HOST
ebca0 49 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c IDLEN and CONCHL
ebcb0 45 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 EN both include
ebcc0 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 73 74 space for the st
ebcd0 72 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 ring .** termina
ebce0 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 ting nul .*/.#de
ebcf0 66 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 fine HOSTIDLEN
ebd00 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 128.#defi
ebd10 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 ne CONCHLEN
ebd20 20 20 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e (MAXPATHLEN
ebd30 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 +HOSTIDLEN+1).#i
ebd40 66 6e 64 65 66 20 48 4f 53 54 49 44 50 41 54 48 fndef HOSTIDPATH
ebd50 0a 23 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44 .# define HOSTID
ebd60 50 41 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62 PATH "/Lib
ebd70 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d rary/Caches/.com
ebd80 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e .apple.sqliteCon
ebd90 63 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 chHostId".#endif
ebda0 0a 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 ../* basically a
ebdb0 20 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e copy of unixRan
ebdc0 64 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66 domness with dif
ebdd0 66 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 ferent.** test b
ebde0 65 68 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e ehavior built in
ebdf0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 */.static int p
ebe00 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 roxyGenerateHost
ebe10 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 ID(char *pHostID
ebe20 29 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64 ){. int pid, fd
ebe30 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 , len;. unsigne
ebe40 64 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 d char *key = (u
ebe50 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 nsigned char *)p
ebe60 48 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d HostID;. . mem
ebe70 73 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 set(key, 0, HOST
ebe80 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 IDLEN);. len =
ebe90 30 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 0;. fd = open("
ebea0 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f /dev/urandom", O
ebeb0 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 _RDONLY);. if(
ebec0 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e fd>=0 ){. len
ebed0 20 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c = read(fd, key,
ebee0 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 HOSTIDLEN);.
ebef0 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 close(fd); /* s
ebf00 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 ilently leak the
ebf10 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 fd if it fails
ebf20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e */. }. if( len
ebf30 20 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a < HOSTIDLEN ){.
ebf40 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 time_t t;.
ebf50 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 time(&t);.
ebf60 6d 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 memcpy(key, &t,
ebf70 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 sizeof(t));.
ebf80 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a pid = getpid();.
ebf90 20 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b memcpy(&key[
ebfa0 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 sizeof(t)], &pid
ebfb0 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a , sizeof(pid));.
ebfc0 20 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 }. .#ifdef MA
ebfd0 4b 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44 KE_PRETTY_HOSTID
ebfe0 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a . {. int i;.
ebff0 20 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 /* filter th
ec000 65 20 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69 e bytes into pri
ec010 6e 74 61 62 6c 65 20 61 73 63 69 69 20 63 68 61 ntable ascii cha
ec020 72 61 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 racters and NUL
ec030 74 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 terminate */.
ec040 20 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d key[(HOSTIDLEN-
ec050 31 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 1)] = 0x00;.
ec060 66 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 for( i=0; i<(HOS
ec070 54 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 TIDLEN-1); i++ )
ec080 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
ec090 20 63 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 char pa = key[i
ec0a0 5d 26 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66 ]&0x7F;. if
ec0b0 28 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 ( pa<0x20 ){.
ec0c0 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b key[i] = (k
ec0d0 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 ey[i]&0x80 == 0x
ec0e0 38 30 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 80) ? pa+0x40 :
ec0f0 70 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d pa+0x20;. }
ec100 65 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37 else if( pa==0x7
ec110 46 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 F ){. key
ec120 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 [i] = (key[i]&0x
ec130 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 80 == 0x80) ? pa
ec140 3d 30 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b =0x20 : pa+0x7E;
ec150 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
ec160 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
ec170 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
ec180 0a 2f 2a 20 77 72 69 74 65 73 20 74 68 65 20 68 ./* writes the h
ec190 6f 73 74 20 69 64 20 70 61 74 68 20 74 6f 20 70 ost id path to p
ec1a0 61 74 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 ath, path should
ec1b0 20 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 be an pre-alloc
ec1c0 61 74 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 ated buffer.** w
ec1d0 69 74 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65 ith enough space
ec1e0 20 66 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a for a path .*/.
ec1f0 73 74 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78 static void prox
ec200 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 63 yGetHostIDPath(c
ec210 68 61 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f har *path, size_
ec220 74 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 t len){. strlcp
ec230 79 28 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41 y(path, HOSTIDPA
ec240 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 TH, len);.#ifdef
ec250 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 SQLITE_TEST. i
ec260 66 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 f( sqlite3_hosti
ec270 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 d_num>0 ){. c
ec280 68 61 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 har suffix[2] =
ec290 22 31 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b "1";. suffix[
ec2a0 30 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 0] = suffix[0] +
ec2b0 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f sqlite3_hostid_
ec2c0 6e 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 num;. strlcat
ec2d0 28 70 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c (path, suffix, l
ec2e0 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a en);. }.#endif.
ec2f0 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 OSTRACE3("GETH
ec300 4f 53 54 49 44 50 41 54 48 20 20 25 73 20 70 69 OSTIDPATH %s pi
ec310 64 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 d=%d\n", path, g
ec320 65 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 etpid());.}../*
ec330 67 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 20 get the host ID
ec340 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f from a sqlite ho
ec350 73 74 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64 stid file stored
ec360 20 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 in the .** user
ec370 2d 73 70 65 63 69 66 69 63 20 74 6d 70 20 64 69 -specific tmp di
ec380 72 65 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20 rectory, create
ec390 74 68 65 20 49 44 20 69 66 20 69 74 27 73 20 6e the ID if it's n
ec3a0 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 ot there already
ec3b0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
ec3c0 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63 proxyGetHostID(c
ec3d0 68 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e har *pHostID, in
ec3e0 74 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e t *pError){. in
ec3f0 74 20 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74 t fd;. char pat
ec400 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a h[MAXPATHLEN]; .
ec410 20 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 size_t len;.
ec420 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b int rc=SQLITE_OK
ec430 3b 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73 ;.. proxyGetHos
ec440 74 49 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41 tIDPath(path, MA
ec450 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 XPATHLEN);. /*
ec460 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 74 68 try to create th
ec470 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 e host ID file,
ec480 69 66 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 if it already ex
ec490 69 73 74 73 20 72 65 61 64 20 74 68 65 20 63 6f ists read the co
ec4a0 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d ntents */. fd =
ec4b0 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 open(path, O_CR
ec4c0 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 EAT|O_WRONLY|O_E
ec4d0 58 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 XCL, 0644);. if
ec4e0 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e ( fd<0 ){. in
ec4f0 74 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a t err=errno;....
ec500 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 if( err!=EEX
ec510 49 53 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51 IST ){.#ifdef SQ
ec520 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 LITE_PROXY_DEBUG
ec530 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 /* set the sqli
ec540 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 te error message
ec550 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 instead */.
ec560 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
ec570 2c 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 , "sqlite error
ec580 63 72 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44 creating host ID
ec590 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c file %s: %s\n",
ec5a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
ec5b0 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 ath, strerror(er
ec5c0 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 r));.#endif.
ec5d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ec5e0 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 PERM;. }.
ec5f0 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 /* couldn't crea
ec600 74 65 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61 te the file, rea
ec610 64 20 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a d it instead */.
ec620 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 fd = open(pa
ec630 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 th, O_RDONLY|O_E
ec640 58 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64 XCL);. if( fd
ec650 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c <0 ){.#ifdef SQL
ec660 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 ITE_PROXY_DEBUG
ec670 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 /* set the sqlit
ec680 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
ec690 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 instead */.
ec6a0 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f int err = errno
ec6b0 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 ;. fprintf(
ec6c0 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 stderr, "sqlite
ec6d0 65 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f error opening ho
ec6e0 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 st ID file %s: %
ec6f0 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
ec700 20 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 path, strerr
ec710 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 or(err));.#endif
ec720 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
ec730 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d LITE_PERM;. }
ec740 0a 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 . len = pread
ec750 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f (fd, pHostID, HO
ec760 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 STIDLEN, 0);.
ec770 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 if( len<0 ){.
ec780 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 *pError = er
ec790 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rno;. rc =
ec7a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 SQLITE_IOERR_REA
ec7b0 44 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 D;. }else if(
ec7c0 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 len<HOSTIDLEN )
ec7d0 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 {. *pError
ec7e0 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
ec7f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
ec800 52 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 RT_READ;. }.
ec810 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a close(fd); /*
ec820 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 silently leak t
ec830 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c he fd if it fail
ec840 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 s */. OSTRACE
ec850 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 72 65 3("GETHOSTID re
ec860 61 64 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c ad %s pid=%d\n",
ec870 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 pHostID, getpid
ec880 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ());. return
ec890 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 rc;. }else{.
ec8a0 20 2f 2a 20 77 65 27 72 65 20 63 72 65 61 74 69 /* we're creati
ec8b0 6e 67 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 ng the host ID f
ec8c0 69 6c 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f ile (use a rando
ec8d0 6d 20 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65 m string of byte
ec8e0 73 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 s) */. proxyG
ec8f0 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28 70 48 enerateHostID(pH
ec900 6f 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 ostID);. len
ec910 3d 20 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f = pwrite(fd, pHo
ec920 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c stID, HOSTIDLEN,
ec930 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 0);. if( len
ec940 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 <0 ){. *pEr
ec950 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 ror = errno;.
ec960 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
ec970 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 OERR_WRITE;.
ec980 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f }else if( len<HO
ec990 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 STIDLEN ){.
ec9a0 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 *pError = 0;.
ec9b0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
ec9c0 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 IOERR_WRITE;.
ec9d0 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 }. close(fd)
ec9e0 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 ; /* silently le
ec9f0 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20 ak the fd if it
eca00 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 fails */. OST
eca10 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 RACE3("GETHOSTID
eca20 20 20 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25 wrote %s pid=%
eca30 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 d\n", pHostID, g
eca40 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 etpid());. re
eca50 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a turn rc;. }.}..
eca60 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
eca70 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 GetLockPath(cons
eca80 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 t char *dbPath,
eca90 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a char *lPath, siz
ecaa0 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 e_t maxLen){. i
ecab0 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 nt len;. int db
ecac0 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 Len;. int i;..#
ecad0 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 ifdef LOCKPROXYD
ecae0 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 IR. len = strlc
ecaf0 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 py(lPath, LOCKPR
ecb00 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b OXYDIR, maxLen);
ecb10 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f .#else.# ifdef _
ecb20 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 CS_DARWIN_USER_T
ecb30 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 EMP_DIR. {.
ecb40 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 confstr(_CS_DARW
ecb50 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 IN_USER_TEMP_DIR
ecb60 2c 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 , lPath, maxLen)
ecb70 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c ;. len = strl
ecb80 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 cat(lPath, "sqli
ecb90 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 teplocks", maxLe
ecba0 6e 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 n);. if( mkdi
ecbb0 72 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f r(lPath, SQLITE_
ecbc0 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 DEFAULT_PROXYDIR
ecbd0 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b _PERMISSIONS) ){
ecbe0 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 . /* if mkd
ecbf0 69 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 ir fails, handle
ecc00 20 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 as lock file cr
ecc10 65 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a eation failure *
ecc20 2f 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 /.# ifdef SQLIT
ecc30 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 6e E_DEBUG. in
ecc40 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 t err = errno;.
ecc50 20 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 if( err!=EE
ecc60 58 49 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20 XIST ){.
ecc70 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
ecc80 22 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 "proxyGetLockPat
ecc90 68 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 h: mkdir(%s,0%o)
ecca0 20 65 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c error %d %s\n",
eccb0 20 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 lPath,.
eccc0 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 SQLITE_D
eccd0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
ecce0 50 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 PERMISSIONS, err
eccf0 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 , strerror(err))
ecd00 3b 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 ;. }.# end
ecd10 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 if. }else{.
ecd20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 OSTRACE3("GE
ecd30 54 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 TLOCKPATH mkdir
ecd40 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c %s pid=%d\n", l
ecd50 50 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b Path, getpid());
ecd60 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a . }. . }.
ecd70 23 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 # else. len = s
ecd80 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f trlcpy(lPath, "/
ecd90 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a tmp/", maxLen);.
ecda0 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a # endif.#endif..
ecdb0 20 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d if( lPath[len-
ecdc0 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 1]!='/' ){. l
ecdd0 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 en = strlcat(lPa
ecde0 74 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 th, "/", maxLen)
ecdf0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 ;. }. . /* tr
ece00 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70 ansform the db p
ece10 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20 ath to a unique
ece20 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 cache name */.
ece30 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 dbLen = (int)str
ece40 6c 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66 len(dbPath);. f
ece50 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e or( i=0; i<dbLen
ece60 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 && (i+len+7)<ma
ece70 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 xLen; i++){.
ece80 63 68 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b char c = dbPath[
ece90 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b i];. lPath[i+
ecea0 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f len] = (c=='/')?
eceb0 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 '_':c;. }. lPa
ecec0 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a th[i+len]='\0';.
eced0 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c strlcat(lPath,
ecee0 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 ":auto:", maxLe
ecef0 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c n);. return SQL
ecf00 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
ecf10 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 56 46 Create a new VF
ecf20 53 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f S file descripto
ecf30 72 20 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d r (stored in mem
ecf40 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
ecf50 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c m.** sqlite3_mal
ecf60 6c 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 loc) and open th
ecf70 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 e file named "pa
ecf80 74 68 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20 th" in the file
ecf90 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a descriptor..**.*
ecfa0 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 * The caller is
ecfb0 72 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 responsible not
ecfc0 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 only for closing
ecfd0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
ecfe0 70 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f ptor.** but also
ecff0 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 for freeing the
ed000 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 memory associat
ed010 65 64 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 ed with the file
ed020 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a descriptor..*/.
ed030 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
ed040 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63 CreateUnixFile(c
ed050 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c onst char *path,
ed060 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 unixFile **ppFi
ed070 6c 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 le) {. unixFile
ed080 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 66 6c *pNew;. int fl
ed090 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 ags = SQLITE_OPE
ed0a0 4e 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 54 45 N_MAIN_DB|SQLITE
ed0b0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c _OPEN_CREATE|SQL
ed0c0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
ed0d0 54 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 TE;. int rc = S
ed0e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 QLITE_OK;. sqli
ed0f0 74 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 te3_vfs dummyVfs
ed100 3b 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69 ;.. pNew = (uni
ed110 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f xFile *)sqlite3_
ed120 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e malloc(sizeof(un
ed130 69 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20 ixFile));. if(
ed140 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74 !pNew ){. ret
ed150 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
ed160 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 ;. }. memset(p
ed170 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 New, 0, sizeof(u
ed180 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 2f 2a nixFile));.. /*
ed190 20 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 28 29 Call unixOpen()
ed1a0 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 72 6f to open the pro
ed1b0 78 79 20 66 69 6c 65 2e 20 54 68 65 20 66 6c 61 xy file. The fla
ed1c0 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 6e 69 gs passed to uni
ed1d0 78 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 75 67 xOpen(). ** sug
ed1e0 67 65 73 74 20 74 68 61 74 20 74 68 65 20 66 69 gest that the fi
ed1f0 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 le being opened
ed200 69 73 20 61 20 22 6d 61 69 6e 20 64 61 74 61 62 is a "main datab
ed210 61 73 65 22 2e 20 54 68 69 73 20 69 73 0a 20 20 ase". This is.
ed220 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 ** necessary as
ed230 6f 74 68 65 72 20 66 69 6c 65 20 74 79 70 65 73 other file types
ed240 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 do not necessar
ed250 69 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f 63 6b ily support lock
ed260 69 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 ing. It. ** is
ed270 62 65 74 74 65 72 20 74 6f 20 75 73 65 20 75 6e better to use un
ed280 69 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 61 64 ixOpen() instead
ed290 20 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 of opening the
ed2a0 66 69 6c 65 20 64 69 72 65 63 74 6c 79 20 77 69 file directly wi
ed2b0 74 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2c 20 th. ** open(),
ed2c0 61 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 73 65 as unixOpen() se
ed2d0 74 73 20 75 70 20 74 68 65 20 76 61 72 69 6f 75 ts up the variou
ed2e0 73 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 65 71 s mechanisms req
ed2f0 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 6d 61 uired to. ** ma
ed300 6b 65 20 73 75 72 65 20 61 20 63 61 6c 6c 20 74 ke sure a call t
ed310 6f 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 20 6e o close() does n
ed320 6f 74 20 63 61 75 73 65 20 74 68 65 20 73 79 73 ot cause the sys
ed330 74 65 6d 20 74 6f 20 64 69 73 63 61 72 64 0a 20 tem to discard.
ed340 20 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 ** POSIX locks
ed350 70 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 20 2a prematurely.. *
ed360 2a 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 6d 70 *. ** It is imp
ed370 6f 72 74 61 6e 74 20 74 68 61 74 20 74 68 65 20 ortant that the
ed380 78 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f 66 20 xOpen member of
ed390 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 70 the VFS object p
ed3a0 61 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a 20 75 assed to . ** u
ed3b0 6e 69 78 4f 70 65 6e 28 29 20 69 73 20 4e 55 4c nixOpen() is NUL
ed3c0 4c 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 75 6e L. This tells un
ed3d0 69 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 72 79 ixOpen() may try
ed3e0 20 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f 78 79 to open a proxy
ed3f0 2d 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f 72 20 -file . ** for
ed400 74 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 20 28 the proxy-file (
ed410 63 72 65 61 74 69 6e 67 20 61 20 70 6f 74 65 6e creating a poten
ed420 74 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 6c 6f tial infinite lo
ed430 6f 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 6d 6d op).. */. dumm
ed440 79 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20 yVfs.pAppData =
ed450 28 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b (void*)&autolock
ed460 49 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 6d 6d IoFinder;. dumm
ed470 79 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 3b 0a yVfs.xOpen = 0;.
ed480 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e 28 rc = unixOpen(
ed490 26 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 68 2c &dummyVfs, path,
ed4a0 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a (sqlite3_file *
ed4b0 29 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 26 66 )pNew, flags, &f
ed4c0 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d lags);. if( rc=
ed4d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 66 =SQLITE_OK && (f
ed4e0 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e lags&SQLITE_OPEN
ed4f0 5f 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a 20 20 _READONLY) ){.
ed500 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 2d pNew->pMethod-
ed510 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 >xClose((sqlite3
ed520 5f 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b 0a 20 _file *)pNew);.
ed530 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
ed540 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 ANTOPEN;. }..
ed550 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
ed560 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 K ){. sqlite3
ed570 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 _free(pNew);.
ed580 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 0a pNew = 0;. }..
ed590 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 77 *ppFile = pNew
ed5a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
ed5b0 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63 ../* takes the c
ed5c0 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61 onch by taking a
ed5d0 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 shared lock and
ed5e0 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e read the conten
ed5f0 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a ts conch, if .**
ed600 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e lockPath is non
ed610 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20 -NULL, the host
ed620 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 ID and lock file
ed630 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68 path must match
ed640 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f . A NULL .** lo
ed650 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61 ckPath means tha
ed660 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69 t the lockPath i
ed670 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 n the conch file
ed680 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66 will be used if
ed690 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44 the .** host ID
ed6a0 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65 s match, or a ne
ed6b0 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c w lock path will
ed6c0 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75 be generated au
ed6d0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 tomatically .**
ed6e0 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 and written to t
ed6f0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a he conch file..*
ed700 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
ed710 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78 xyTakeConch(unix
ed720 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 File *pFile){.
ed730 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
ed740 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
ed750 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
ed760 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
ed770 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20 gContext; . .
ed780 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 if( pCtx->conchH
ed790 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 eld>0 ){. ret
ed7a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
ed7b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 }else{. unix
ed7c0 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 File *conchFile
ed7d0 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c = pCtx->conchFil
ed7e0 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74 e;. char test
ed7f0 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b Value[CONCHLEN];
ed800 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56 . char conchV
ed810 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a alue[CONCHLEN];.
ed820 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74 char lockPat
ed830 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 h[MAXPATHLEN];.
ed840 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61 char *tLockPa
ed850 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 th = NULL;. i
ed860 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
ed870 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52 K;. int readR
ed880 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
ed890 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73 int syncPerms
ed8a0 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41 = 0;.. OSTRA
ed8b0 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 CE4("TAKECONCH
ed8c0 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 %d for %s pid=%d
ed8d0 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e \n", conchFile->
ed8e0 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 h,.
ed8f0 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 (pCtx->lockProxy
ed900 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 Path ? pCtx->loc
ed910 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 kProxyPath : ":a
ed920 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 uto:"), getpid()
ed930 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e );.. rc = con
ed940 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d chFile->pMethod-
ed950 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f >xLock((sqlite3_
ed960 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c file*)conchFile,
ed970 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
ed980 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
ed990 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e E_OK ){. in
ed9a0 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 t pError = 0;.
ed9b0 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56 memset(testV
ed9c0 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 alue, 0, CONCHLE
ed9d0 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20 N); /* conch is
ed9e0 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20 fixed size */.
ed9f0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65 rc = proxyGe
eda00 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75 tHostID(testValu
eda10 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20 e, &pError);.
eda20 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 if( (rc&0xff)
eda30 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 ==SQLITE_IOERR )
eda40 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
eda50 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72 >lastErrno = pEr
eda60 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ror;. }.
eda70 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 if( pCtx->loc
eda80 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
eda90 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 strlcpy(&t
edaa0 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c estValue[HOSTIDL
edab0 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 EN], pCtx->lockP
edac0 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 roxyPath, MAXPAT
edad0 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 HLEN);. }.
edae0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
edaf0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
edb00 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b goto end_tak
edb10 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 econch;. }.
edb20 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20 . readRc =
edb30 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65 unixRead((sqlite
edb40 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 3_file *)conchFi
edb50 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 le, conchValue,
edb60 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 CONCHLEN, 0);.
edb70 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 if( readRc!=SQ
edb80 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
edb90 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69 _READ ){. i
edba0 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 f( readRc!=SQLIT
edbb0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
edbc0 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 if( (rc&0xff)==S
edbd0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 QLITE_IOERR ){.
edbe0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e pFile->
edbf0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 lastErrno = conc
edc00 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f hFile->lastErrno
edc10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
edc20 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b rc = readRc;
edc30 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
edc40 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 d_takeconch;.
edc50 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66 }. /* if
edc60 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64 the conch has d
edc70 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20 ata compare the
edc80 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20 contents */.
edc90 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 if( !pCtx->loc
edca0 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
edcb0 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 /* for aut
edcc0 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f o-named local lo
edcd0 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 ck file, just ch
edce0 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 eck the host ID
edcf0 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 and we'll.
edd00 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f ** use the lo
edd10 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 cal lock file pa
edd20 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64 th that's alread
edd30 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20 y in there */.
edd40 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d if( !memcm
edd50 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e p(testValue, con
edd60 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c chValue, HOSTIDL
edd70 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EN) ){.
edd80 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68 tLockPath = (ch
edd90 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65 ar *)&conchValue
edda0 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 [HOSTIDLEN];.
eddb0 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
eddc0 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 takeconch;.
eddd0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
edde0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27 {. /* we'
eddf0 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68 ve got the conch
ede00 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d if conchValue m
ede10 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20 atches our path
ede20 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20 and host ID */.
ede30 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 if( !memc
ede40 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f mp(testValue, co
ede50 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c nchValue, CONCHL
ede60 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 EN) ){.
ede70 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f goto end_takeco
ede80 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 nch;. }.
ede90 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
edea0 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f {. /* a sho
edeb0 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65 rt read means we
edec0 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74 're "creating" t
eded0 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74 he conch (even t
edee0 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a hough it could .
edef0 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65 ** have be
edf00 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e en user-interven
edf10 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71 tion), if we acq
edf20 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69 uire the exclusi
edf30 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a ve lock,. *
edf40 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d * we'll try to m
edf50 61 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74 atch the current
edf60 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73 on-disk permiss
edf70 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61 ions of the data
edf80 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 base. */.
edf90 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 syncPerms =
edfa0 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 1;. }. .
edfb0 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63 /* either conc
edfc0 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64 h was emtpy or d
edfd0 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20 idn't match */.
edfe0 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f if( !pCtx->lo
edff0 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
ee000 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63 proxyGetLoc
ee010 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61 kPath(pCtx->dbPa
ee020 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 th, lockPath, MA
ee030 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 XPATHLEN);.
ee040 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63 tLockPath = loc
ee050 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72 kPath;. str
ee060 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b lcpy(&testValue[
ee070 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b HOSTIDLEN], lock
ee080 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e Path, MAXPATHLEN
ee090 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 );. }. .
ee0a0 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63 /* update conc
ee0b0 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20 h with host and
ee0c0 70 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20 path (this will
ee0d0 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72 fail if other pr
ee0e0 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61 ocess. ** ha
ee0f0 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 s a shared lock
ee100 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20 already) */.
ee110 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e rc = conchFile->
ee120 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 pMethod->xLock((
ee130 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
ee140 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49 nchFile, EXCLUSI
ee150 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 VE_LOCK);. if
ee160 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
ee170 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e ){. rc = un
ee180 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33 ixWrite((sqlite3
ee190 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c _file *)conchFil
ee1a0 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f e, testValue, CO
ee1b0 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 NCHLEN, 0);.
ee1c0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
ee1d0 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73 _OK && syncPerms
ee1e0 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 ){. stru
ee1f0 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 ct stat buf;.
ee200 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66 int err = f
ee210 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 stat(pFile->h, &
ee220 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 buf);. if
ee230 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 ( err==0 ){.
ee240 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20 /* try to
ee250 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61 match the databa
ee260 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69 se file permissi
ee270 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c ons, ignore fail
ee280 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ure */.#ifndef S
ee290 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 QLITE_PROXY_DEBU
ee2a0 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d G. fchm
ee2b0 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c od(conchFile->h,
ee2c0 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 buf.st_mode);.#
ee2d0 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69 else. i
ee2e0 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 f( fchmod(conchF
ee2f0 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d ile->h, buf.st_m
ee300 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ode)!=0 ){.
ee310 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 int code
ee320 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 = errno;.
ee330 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
ee340 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 err, "fchmod %o
ee350 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25 FAILED with %d %
ee360 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
ee370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee380 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 buf.st_mode,
ee390 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 code, strerror(c
ee3a0 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 ode));.
ee3b0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
ee3c0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
ee3d0 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f derr, "fchmod %o
ee3e0 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66 SUCCEDED\n",buf
ee3f0 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20 .st_mode);.
ee400 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
ee410 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
ee420 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f int code = errno
ee430 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 ;. fpri
ee440 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41 ntf(stderr, "STA
ee450 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74 T FAILED[%d] wit
ee460 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20 h %d %s\n", .
ee470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee480 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65 err, code
ee490 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29 , strerror(code)
ee4a0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 );.#endif.
ee4b0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
ee4c0 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d }. conchFile-
ee4d0 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
ee4e0 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
ee4f0 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 )conchFile, SHAR
ee500 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 ED_LOCK);. .end
ee510 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20 _takeconch:.
ee520 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 OSTRACE2("TRANSP
ee530 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c ROXY: CLOSE %d\
ee540 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 n", pFile->h);.
ee550 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
ee560 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f E_OK && pFile->o
ee570 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 penFlags ){.
ee580 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d if( pFile->h>=
ee590 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49 0 ){.#ifdef STRI
ee5a0 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 CT_CLOSE_ERROR.
ee5b0 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 if( close
ee5c0 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 (pFile->h) ){.
ee5d0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
ee5e0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f astErrno = errno
ee5f0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
ee600 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
ee610 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d CLOSE;. }
ee620 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63 .#else. c
ee630 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20 lose(pFile->h);
ee640 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b /* silently leak
ee650 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23 fd if fail */.#
ee660 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 endif. }.
ee670 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d pFile->h = -
ee680 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20 1;. int fd
ee690 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50 = open(pCtx->dbP
ee6a0 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e ath, pFile->open
ee6b0 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 Flags,.
ee6c0 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 SQLIT
ee6d0 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 E_DEFAULT_FILE_P
ee6e0 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 ERMISSIONS);.
ee6f0 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 OSTRACE2("TRA
ee700 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25 NSPROXY: OPEN %
ee710 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20 d\n", fd);.
ee720 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 if( fd>=0 ){.
ee730 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d pFile->h =
ee740 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 fd;. }else
ee750 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c {. rc=SQL
ee760 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a ITE_CANTOPEN; /*
ee770 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72 SQLITE_BUSY? pr
ee780 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c oxyTakeConch cal
ee790 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 led.
ee7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ee7b0 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e during lockin
ee7c0 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 g */. }.
ee7d0 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 }. if( rc==S
ee7e0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74 QLITE_OK && !pCt
ee7f0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a x->lockProxy ){.
ee800 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68 char *path
ee810 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74 = tLockPath ? t
ee820 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d LockPath : pCtx-
ee830 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a >lockProxyPath;.
ee840 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65 /* ACS: Ne
ee850 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 ed to make a cop
ee860 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69 y of path someti
ee870 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 mes */. rc
ee880 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 = proxyCreateUni
ee890 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74 xFile(path, &pCt
ee8a0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 x->lockProxy);.
ee8b0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
ee8c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
ee8d0 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 pCtx->conchH
ee8e0 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 eld = 1;..
ee8f0 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b if( tLockPath ){
ee900 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c . pCtx->l
ee910 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 ockProxyPath = s
ee920 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 qlite3DbStrDup(0
ee930 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 , tLockPath);.
ee940 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e if( pCtx->
ee950 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 lockProxy->pMeth
ee960 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 od == &afpIoMeth
ee970 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ods ){.
ee980 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e ((afpLockingCon
ee990 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 text *)pCtx->loc
ee9a0 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 kProxy->lockingC
ee9b0 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20 ontext)->dbPath
ee9c0 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 =.
ee9d0 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 pCtx->loc
ee9e0 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 kProxyPath;.
ee9f0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
eea00 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
eea10 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 conchFile->pMet
eea20 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 hod->xUnlock((sq
eea30 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 lite3_file*)conc
eea40 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b hFile, NO_LOCK);
eea50 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 . }. OSTRA
eea60 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 CE3("TAKECONCH
eea70 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 %d %s\n", conchF
eea80 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 ile->h, rc==SQLI
eea90 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c TE_OK?"ok":"fail
eeaa0 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ed");. return
eeab0 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a rc;. }.}../*.*
eeac0 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73 * If pFile holds
eead0 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e a lock on a con
eeae0 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65 ch file, then re
eeaf0 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e lease that lock.
eeb00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
eeb10 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 roxyReleaseConch
eeb20 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
eeb30 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
eeb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eeb50 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72 /* Subroutine r
eeb60 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
eeb70 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
eeb80 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54 ext *pCtx; /* T
eeb90 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 he locking conte
eeba0 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 xt for the proxy
eebb0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 lock */. unixF
eebc0 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 ile *conchFile;
eebd0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
eebe0 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 f the conch file
eebf0 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70 */.. pCtx = (p
eec00 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
eec10 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b xt *)pFile->lock
eec20 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f ingContext;. co
eec30 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e nchFile = pCtx->
eec40 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54 conchFile;. OST
eec50 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f RACE4("RELEASECO
eec60 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 NCH %d for %s p
eec70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 id=%d\n", conchF
eec80 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
eec90 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 (pCtx->lockPr
eeca0 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e oxyPath ? pCtx->
eecb0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 lockProxyPath :
eecc0 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20 ":auto:"), .
eecd0 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 getpid())
eece0 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 ;. pCtx->conchH
eecf0 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 eld = 0;. rc =
eed00 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 conchFile->pMeth
eed10 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c od->xUnlock((sql
eed20 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 ite3_file*)conch
eed30 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a File, NO_LOCK);.
eed40 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45 OSTRACE3("RELE
eed50 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c ASECONCH %d %s\
eed60 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 n", conchFile->h
eed70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 ,. (rc
eed80 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f ==SQLITE_OK ? "o
eed90 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b k" : "failed"));
eeda0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
eedb0 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 ./*.** Given the
eedc0 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 name of a datab
eedd0 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74 ase file, comput
eede0 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74 e the name of it
eedf0 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a s conch file..**
eee00 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68 Store the conch
eee10 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d filename in mem
eee20 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
eee30 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
eee40 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f ()..** Make *pCo
eee50 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f nchPath point to
eee60 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20 the new name.
eee70 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
eee80 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f on success.** o
eee90 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 r SQLITE_NOMEM i
eeea0 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 f unable to obta
eeeb0 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a in memory..**.**
eeec0 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 The caller is r
eeed0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 esponsible for e
eeee0 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 nsuring that the
eeef0 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 allocated memor
eef00 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76 y.** space is ev
eef10 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a entually freed..
eef20 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74 **.** *pConchPat
eef30 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c h is set to NULL
eef40 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c if a memory all
eef50 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 ocation error oc
eef60 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 curs..*/.static
eef70 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43 int proxyCreateC
eef80 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61 onchPathname(cha
eef90 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 r *dbPath, char
eefa0 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20 **pConchPath){.
eefb0 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
eefc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
eefd0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
eefe0 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69 /. int len = (i
eeff0 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 nt)strlen(dbPath
ef000 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 ); /* Length of
ef010 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d database filenam
ef020 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20 e - dbPath */.
ef030 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b char *conchPath;
ef040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
ef050 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 buffer in which
ef060 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f to construct co
ef070 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f nch name */.. /
ef080 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 * Allocate space
ef090 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66 for the conch f
ef0a0 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74 ilename and init
ef0b0 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20 ialize the name
ef0c0 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 to. ** the name
ef0d0 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c of the original
ef0e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
ef0f0 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61 */ . *pConchPa
ef100 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d th = conchPath =
ef110 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
ef120 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 _malloc(len + 8)
ef130 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 ;. if( conchPat
ef140 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 h==0 ){. retu
ef150 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
ef160 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f . }. memcpy(co
ef170 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c nchPath, dbPath,
ef180 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a len+1);. . /*
ef190 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e now insert a ".
ef1a0 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 " before the las
ef1b0 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f t / character */
ef1c0 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 . for( i=(len-1
ef1d0 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a ); i>=0; i-- ){.
ef1e0 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 if( conchPat
ef1f0 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 h[i]=='/' ){.
ef200 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72 i++;. br
ef210 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
ef220 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e conchPath[i]='.
ef230 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c ';. while ( i<l
ef240 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50 en ){. conchP
ef250 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b ath[i+1]=dbPath[
ef260 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d i];. i++;. }
ef270 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68 .. /* append th
ef280 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69 e "-conch" suffi
ef290 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f x to the file */
ef2a0 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68 . memcpy(&conch
ef2b0 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e Path[i+1], "-con
ef2c0 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72 ch", 7);. asser
ef2d0 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63 t( (int)strlen(c
ef2e0 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e onchPath) == len
ef2f0 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 +7 );.. return
ef300 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
ef310 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20 * Takes a fully
ef320 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79 configured proxy
ef330 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75 locking-style u
ef340 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69 nix file and swi
ef350 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 tches.** the loc
ef360 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 al lock file pat
ef370 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 h .*/.static int
ef380 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 switchLockProxy
ef390 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 Path(unixFile *p
ef3a0 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 File, const char
ef3b0 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 *path) {. prox
ef3c0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
ef3d0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
ef3e0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 ckingContext*)pF
ef3f0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
ef400 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64 ext;. char *old
ef410 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 Path = pCtx->loc
ef420 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e kProxyPath;. in
ef430 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
ef440 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e ;.. if( pFile->
ef450 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 locktype!=NO_LOC
ef460 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
ef470 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
ef480 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 .. /* nothing
ef490 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61 to do if the pa
ef4a0 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 th is NULL, :aut
ef4b0 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68 o: or matches th
ef4c0 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20 e existing path
ef4d0 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c */. if( !path |
ef4e0 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 | path[0]=='\0'
ef4f0 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c || !strcmp(path,
ef500 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 ":auto:") ||.
ef510 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73 (oldPath && !s
ef520 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20 trncmp(oldPath,
ef530 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e path, MAXPATHLEN
ef540 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e )) ){. return
ef550 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
ef560 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c lse{. unixFil
ef570 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 e *lockProxy = p
ef580 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
ef590 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 pCtx->lockPr
ef5a0 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 oxy=NULL;. pC
ef5b0 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 tx->conchHeld =
ef5c0 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 0;. if( lockP
ef5d0 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 roxy!=NULL ){.
ef5e0 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 rc=lockProxy
ef5f0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 ->pMethod->xClos
ef600 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 e((sqlite3_file
ef610 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 *)lockProxy);.
ef620 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
ef630 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 urn rc;. sq
ef640 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 lite3_free(lockP
ef650 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 roxy);. }.
ef660 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c sqlite3_free(ol
ef670 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 dPath);. pCtx
ef680 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
ef690 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
ef6a0 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a p(0, path);. }.
ef6b0 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
ef6c0 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 }../*.** pFile i
ef6d0 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 s a file that ha
ef6e0 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 s been opened by
ef6f0 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63 a prior xOpen c
ef700 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20 all. dbPath.**
ef710 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 is a string buff
ef720 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50 er at least MAXP
ef730 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74 ATHLEN+1 charact
ef740 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a ers in size..**.
ef750 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
ef760 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d find the filenam
ef770 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
ef780 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74 h pFile and writ
ef790 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50 es it.** int dbP
ef7a0 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ath..*/.static i
ef7b0 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74 nt proxyGetDbPat
ef7c0 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69 hForUnixFile(uni
ef7d0 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68 xFile *pFile, ch
ef7e0 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66 ar *dbPath){.#if
ef7f0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
ef800 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d __). if( pFile-
ef810 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 >pMethod == &afp
ef820 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
ef830 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65 /* afp style ke
ef840 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 eps a reference
ef850 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69 to the db path i
ef860 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66 n the filePath f
ef870 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 ield . ** of
ef880 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 the struct */.
ef890 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 assert( (int)s
ef8a0 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 trlen((char*)pFi
ef8b0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
ef8c0 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 xt)<=MAXPATHLEN
ef8d0 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 );. strcpy(db
ef8e0 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 Path, ((afpLocki
ef8f0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
ef900 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
ef910 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d t)->dbPath);. }
ef920 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66 else.#endif. if
ef930 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 ( pFile->pMethod
ef940 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 == &dotlockIoMe
ef950 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
ef960 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75 dot lock style u
ef970 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 ses the locking
ef980 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 context to store
ef990 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 the dot lock.
ef9a0 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a ** file path *
ef9b0 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 /. int len =
ef9c0 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70 strlen((char *)p
ef9d0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
ef9e0 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44 text) - strlen(D
ef9f0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a OTLOCK_SUFFIX);.
efa00 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74 memcpy(dbPat
efa10 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 h, (char *)pFile
efa20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
efa30 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 , len + 1);. }e
efa40 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 lse{. /* all
efa50 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65 other styles use
efa60 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e the locking con
efa70 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 text to store th
efa80 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a e db file path *
efa90 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 /. assert( st
efaa0 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c rlen((char*)pFil
efab0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
efac0 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 t)<=MAXPATHLEN )
efad0 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 ;. strcpy(dbP
efae0 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 ath, (char *)pFi
efaf0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
efb00 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 xt);. }. retur
efb10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
efb20 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61 /*.** Takes an a
efb30 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e lready filled in
efb40 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61 unix file and a
efb50 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20 lters it so all
efb60 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a file locking .**
efb70 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d will be perform
efb80 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 ed on the local
efb90 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e proxy lock file.
efba0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
efbb0 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72 fields.** are pr
efbc0 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c eserved in the l
efbd0 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73 ocking context s
efbe0 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 o that they can
efbf0 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 be restored and
efc00 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72 .** the unix str
efc10 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20 ucture properly
efc20 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c cleaned up at cl
efc30 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e ose time:.** ->
efc40 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a lockingContext.*
efc50 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a * ->pMethod.*/.
efc60 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
efc70 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c TransformUnixFil
efc80 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c e(unixFile *pFil
efc90 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 e, const char *p
efca0 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f ath) {. proxyLo
efcb0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
efcc0 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74 tx;. char dbPat
efcd0 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b h[MAXPATHLEN+1];
efce0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
efcf0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
efd00 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c ile */. char *l
efd10 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 ockPath=NULL;.
efd20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
efd30 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69 OK;. . if( pFi
efd40 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f le->locktype!=NO
efd50 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 _LOCK ){. ret
efd60 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b urn SQLITE_BUSY;
efd70 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44 . }. proxyGetD
efd80 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 bPathForUnixFile
efd90 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b (pFile, dbPath);
efda0 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 . if( !path ||
efdb0 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c path[0]=='\0' ||
efdc0 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 !strcmp(path, "
efdd0 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20 :auto:") ){.
efde0 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 lockPath=NULL;.
efdf0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b }else{. lock
efe00 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74 Path=(char *)pat
efe10 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 h;. }. . OSTR
efe20 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59 ACE4("TRANSPROXY
efe30 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d %d for %s pid=
efe40 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
efe50 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63 . (loc
efe60 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68 kPath ? lockPath
efe70 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 : ":auto:"), ge
efe80 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78 tpid());.. pCtx
efe90 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
efea0 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 c( sizeof(*pCtx)
efeb0 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d );. if( pCtx==
efec0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
efed0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
efee0 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c }. memset(pCtx,
efef0 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 0, sizeof(*pCtx
eff00 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78 ));.. rc = prox
eff10 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 yCreateConchPath
eff20 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43 name(dbPath, &pC
eff30 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 tx->conchFilePat
eff40 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 h);. if( rc==SQ
eff50 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
eff60 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 c = proxyCreateU
eff70 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f nixFile(pCtx->co
eff80 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43 nchFilePath, &pC
eff90 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a tx->conchFile);.
effa0 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d } . if( rc==
effb0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 SQLITE_OK && loc
effc0 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74 kPath ){. pCt
effd0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
effe0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
efff0 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b up(0, lockPath);
f0000 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d . }.. if( rc==
f0010 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
f0020 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 /* all memory i
f0030 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f s allocated, pro
f0040 78 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 xys are created
f0050 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 and assigned, .
f0060 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 ** switch the
f0070 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
f0080 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 and pMethod the
f0090 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f n return.. */
f00a0 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 . pCtx->dbPat
f00b0 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 h = sqlite3DbStr
f00c0 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a Dup(0, dbPath);.
f00d0 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 pCtx->oldLoc
f00e0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 kingContext = pF
f00f0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
f0100 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ext;. pFile->
f0110 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d lockingContext =
f0120 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d pCtx;. pCtx-
f0130 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46 >pOldMethod = pF
f0140 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 ile->pMethod;.
f0150 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 pFile->pMethod
f0160 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f = &proxyIoMetho
f0170 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ds;. }else{.
f0180 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 if( pCtx->conch
f0190 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72 File ){ . r
f01a0 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 c = pCtx->conchF
f01b0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 ile->pMethod->xC
f01c0 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 lose((sqlite3_fi
f01d0 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68 le *)pCtx->conch
f01e0 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 File);. if(
f01f0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f0200 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
f0210 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 ree(pCtx->conchF
f0220 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ile);. }.
f0230 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
f0240 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 x->conchFilePath
f0250 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ); . sqlite3_
f0260 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a free(pCtx);. }.
f0270 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e OSTRACE3("TRAN
f0280 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22 SPROXY %d %s\n"
f0290 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 , pFile->h,.
f02a0 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 (rc==SQLI
f02b0 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 TE_OK ? "ok" : "
f02c0 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 failed"));. ret
f02d0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
f02e0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 * This routine h
f02f0 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 andles sqlite3_f
f0300 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 ile_control() ca
f0310 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65 lls that are spe
f0320 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 cific.** to prox
f0330 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 y locking..*/.st
f0340 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 atic int proxyFi
f0350 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
f0360 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
f0370 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b op, void *pArg){
f0380 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b . switch( op ){
f0390 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
f03a0 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _GET_LOCKPROXYFI
f03b0 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 LE: {. unix
f03c0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
f03d0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 nixFile*)id;.
f03e0 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d if( pFile->pM
f03f0 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 ethod == &proxyI
f0400 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
f0410 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 proxyLocking
f0420 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
f0430 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
f0440 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 text*)pFile->loc
f0450 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 kingContext;.
f0460 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f proxyTakeCo
f0470 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 nch(pFile);.
f0480 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f if( pCtx->lo
f0490 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 ckProxyPath ){.
f04a0 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 *(const
f04b0 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 char **)pArg =
f04c0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
f04d0 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ath;. }el
f04e0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 se{. *(
f04f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 const char **)pA
f0500 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f rg = ":auto: (no
f0510 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 t held)";.
f0520 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 }. } else
f0530 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e {. *(con
f0540 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 st char **)pArg
f0550 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a = NULL;. }.
f0560 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
f0570 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 ITE_OK;. }.
f0580 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 case SQLITE_SE
f0590 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a T_LOCKPROXYFILE:
f05a0 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c {. unixFil
f05b0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
f05c0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 File*)id;.
f05d0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
f05e0 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 OK;. int is
f05f0 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46 ProxyStyle = (pF
f0600 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 ile->pMethod ==
f0610 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 &proxyIoMethods)
f0620 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67 ;. if( pArg
f0630 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 ==NULL || (const
f0640 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 char *)pArg==0
f0650 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 ){. if( i
f0660 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 sProxyStyle ){.
f0670 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e /* turn
f0680 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 off proxy locki
f0690 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 ng - not support
f06a0 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ed */.
f06b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
f06c0 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f R /*SQLITE_PROTO
f06d0 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 COL? SQLITE_MISU
f06e0 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d SE?*/;. }
f06f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
f0700 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 /* turn off prox
f0710 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 y locking - alre
f0720 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a ady off - NOOP *
f0730 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d /. rc =
f0740 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
f0750 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
f0760 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 e{. const
f0770 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68 char *proxyPath
f0780 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a = (const char *
f0790 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69 )pArg;. i
f07a0 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 f( isProxyStyle
f07b0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f ){. pro
f07c0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f07d0 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20 *pCtx = .
f07e0 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b (proxyLock
f07f0 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c ingContext*)pFil
f0800 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f0810 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 t;. if(
f0820 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22 !strcmp(pArg, "
f0830 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 :auto:") .
f0840 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c || (pCtx->l
f0850 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a ockProxyPath &&.
f0860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 !
f0870 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f strncmp(pCtx->lo
f0880 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f ckProxyPath, pro
f0890 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c xyPath, MAXPATHL
f08a0 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29 EN)). )
f08b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
f08c0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
f08d0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
f08e0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
f08f0 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 switchLockProxyP
f0900 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 ath(pFile, proxy
f0910 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 Path);.
f0920 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
f0930 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 {. /* t
f0940 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c urn on proxy fil
f0950 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 e locking */.
f0960 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 rc = prox
f0970 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 yTransformUnixFi
f0980 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 le(pFile, proxyP
f0990 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a ath);. }.
f09a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
f09b0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
f09c0 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
f09d0 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b assert( 0 );
f09e0 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 /* The call as
f09f0 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 sures that only
f0a00 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72 valid opcodes ar
f0a10 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a e sent */. }.
f0a20 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 }. /*NOTREACH
f0a30 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 ED*/. return SQ
f0a40 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f LITE_ERROR;.}../
f0a50 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 *.** Within this
f0a60 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70 division (the p
f0a70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 roxying locking
f0a80 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 implementation)
f0a90 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a the procedures.*
f0aa0 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 * above this poi
f0ab0 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 nt are all utili
f0ac0 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d ties. The lock-
f0ad0 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20 related methods
f0ae0 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d of the.** proxy-
f0af0 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f locking sqlite3_
f0b00 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 io_method object
f0b10 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a follow..*/.../*
f0b20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
f0b30 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
f0b40 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
f0b50 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
f0b60 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
f0b70 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
f0b80 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
f0b90 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
f0ba0 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 s held, set *pRe
f0bb0 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e sOut.** to a non
f0bc0 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 -zero value othe
f0bd0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 rwise *pResOut i
f0be0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 s set to zero.
f0bf0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
f0c00 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
f0c10 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 LITE_OK unless a
f0c20 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 n I/O error occu
f0c30 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 rs during lock c
f0c40 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
f0c50 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 ic int proxyChec
f0c60 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
f0c70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
f0c80 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a int *pResOut) {.
f0c90 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
f0ca0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
f0cb0 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 d;. int rc = pr
f0cc0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 oxyTakeConch(pFi
f0cd0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 le);. if( rc==S
f0ce0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
f0cf0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f0d00 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
f0d10 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f0d20 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
f0d30 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e gContext;. un
f0d40 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 ixFile *proxy =
f0d50 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b pCtx->lockProxy;
f0d60 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 . return prox
f0d70 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 y->pMethod->xChe
f0d80 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 ckReservedLock((
f0d90 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 sqlite3_file*)pr
f0da0 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 oxy, pResOut);.
f0db0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
f0dc0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
f0dd0 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
f0de0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
f0df0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
f0e00 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
f0e10 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
f0e20 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
f0e30 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
f0e40 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
f0e50 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
f0e60 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
f0e70 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
f0e80 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
f0e90 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
f0ea0 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
f0eb0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
f0ec0 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
f0ed0 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
f0ee0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
f0ef0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
f0f00 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
f0f10 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
f0f20 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
f0f30 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
f0f40 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
f0f50 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
f0f60 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
f0f70 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
f0f80 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
f0f90 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
f0fa0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
f0fb0 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
f0fc0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
f0fd0 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
f0fe0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
f0ff0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
f1000 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
f1010 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
f1020 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
f1030 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
f1040 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
f1050 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
f1060 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
f1070 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
f1080 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
f1090 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
f10a0 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 k. Use the sqli
f10b0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a te3OsUnlock().**
f10c0 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 routine to lowe
f10d0 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 r a locking leve
f10e0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 l..*/.static int
f10f0 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 proxyLock(sqlit
f1100 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
f1110 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
f1120 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
f1130 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
f1140 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 int rc = proxy
f1150 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 TakeConch(pFile)
f1160 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
f1170 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f TE_OK ){. pro
f1180 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f1190 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c *pCtx = (proxyL
f11a0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
f11b0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f11c0 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 ntext;. unixF
f11d0 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 ile *proxy = pCt
f11e0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 x->lockProxy;.
f11f0 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d rc = proxy->pM
f1200 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 ethod->xLock((sq
f1210 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 lite3_file*)prox
f1220 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 y, locktype);.
f1230 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
f1240 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 e = proxy->lockt
f1250 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ype;. }. retur
f1260 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
f1270 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e Lower the lockin
f1280 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 g level on file
f1290 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 descriptor pFile
f12a0 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c to locktype. l
f12b0 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 ocktype.** must
f12c0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 be either NO_LOC
f12d0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b K or SHARED_LOCK
f12e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c ..**.** If the l
f12f0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 ocking level of
f1300 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
f1310 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 tor is already a
f1320 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 t or below.** th
f1330 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b e requested lock
f1340 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 ing level, this
f1350 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d routine is a no-
f1360 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e op..*/.static in
f1370 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 t proxyUnlock(sq
f1380 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
f1390 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a int locktype) {.
f13a0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
f13b0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
f13c0 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 d;. int rc = pr
f13d0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 oxyTakeConch(pFi
f13e0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 le);. if( rc==S
f13f0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
f1400 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f1410 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
f1420 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f1430 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
f1440 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e gContext;. un
f1450 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 ixFile *proxy =
f1460 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b pCtx->lockProxy;
f1470 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d . rc = proxy-
f1480 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
f1490 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
f14a0 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 )proxy, locktype
f14b0 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f );. pFile->lo
f14c0 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e cktype = proxy->
f14d0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 locktype;. }.
f14e0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
f14f0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 .** Close a file
f1500 20 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 that uses proxy
f1510 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 locks..*/.stati
f1520 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 c int proxyClose
f1530 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
f1540 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b d) {. if( id ){
f1550 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
f1560 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
f1570 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c *)id;. proxyL
f1580 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 ockingContext *p
f1590 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b Ctx = (proxyLock
f15a0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 ingContext *)pFi
f15b0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f15c0 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 xt;. unixFile
f15d0 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 *lockProxy = pC
f15e0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 tx->lockProxy;.
f15f0 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e unixFile *con
f1600 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 chFile = pCtx->c
f1610 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e onchFile;. in
f1620 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
f1630 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c ;. . if( l
f1640 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 ockProxy ){.
f1650 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 rc = lockProxy
f1660 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f ->pMethod->xUnlo
f1670 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
f1680 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f *)lockProxy, NO_
f1690 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 LOCK);. if(
f16a0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f16b0 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b . rc = lock
f16c0 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e Proxy->pMethod->
f16d0 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f xClose((sqlite3_
f16e0 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 file*)lockProxy)
f16f0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
f1700 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
f1710 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c sqlite3_free(l
f1720 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 ockProxy);.
f1730 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
f1740 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
f1750 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b if( conchFile ){
f1760 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d . if( pCtx-
f1770 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 >conchHeld ){.
f1780 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 rc = proxy
f1790 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 ReleaseConch(pFi
f17a0 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 le);. if(
f17b0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
f17c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
f17d0 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 c = conchFile->p
f17e0 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 Method->xClose((
f17f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f sqlite3_file*)co
f1800 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 nchFile);.
f1810 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
f1820 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 rc;. sqlite
f1830 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 3_free(conchFile
f1840 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
f1850 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e ite3_free(pCtx->
f1860 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a lockProxyPath);.
f1870 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
f1880 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 (pCtx->conchFile
f1890 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 Path);. sqlit
f18a0 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62 e3_free(pCtx->db
f18b0 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65 Path);. /* re
f18c0 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e store the origin
f18d0 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 al locking conte
f18e0 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 xt and pMethod t
f18f0 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a hen close it */.
f1900 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 pFile->locki
f1910 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 ngContext = pCtx
f1920 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 ->oldLockingCont
f1930 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e ext;. pFile->
f1940 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e pMethod = pCtx->
f1950 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20 pOldMethod;.
f1960 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 sqlite3_free(pCt
f1970 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 x);. return p
f1980 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 File->pMethod->x
f1990 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20 Close(id);. }.
f19a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
f19b0 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f K;.}....#endif /
f19c0 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c * defined(__APPL
f19d0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
f19e0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
f19f0 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 YLE */./*.** The
f1a00 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73 proxy locking s
f1a10 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64 tyle is intended
f1a20 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46 for use with AF
f1a30 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a P filesystems..*
f1a40 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20 * And since AFP
f1a50 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 is only supporte
f1a60 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65 d on MacOSX, the
f1a70 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 proxy locking i
f1a80 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69 s also.** restri
f1a90 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a cted to MacOSX..
f1aa0 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ** .**.*********
f1ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f1ac0 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b f the proxy lock
f1ad0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
f1ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1af0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
f1b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f1b40 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e *****/../*.** In
f1b50 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 itialize the ope
f1b60 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e rating system in
f1b70 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 terface..**.** T
f1b80 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 his routine regi
f1b90 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d sters all VFS im
f1ba0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f plementations fo
f1bb0 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 r unix-like oper
f1bc0 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73 ating.** systems
f1bd0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c . This routine,
f1be0 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 and the sqlite3
f1bf0 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e _os_end() routin
f1c00 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a e that follows,.
f1c10 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 ** should be the
f1c20 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69 only routines i
f1c30 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74 n this file that
f1c40 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f are visible fro
f1c50 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73 m other.** files
f1c60 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
f1c70 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f tine is called o
f1c80 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74 nce during SQLit
f1c90 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e e initialization
f1ca0 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e and by a.** sin
f1cb0 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 gle thread. The
f1cc0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
f1cd0 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 on and mutex sub
f1ce0 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74 systems have not
f1cf0 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20 .** necessarily
f1d00 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 been initialized
f1d10 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
f1d20 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e ne is called, an
f1d30 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f d so they.** sho
f1d40 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e uld not be used.
f1d50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
f1d60 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e nt sqlite3_os_in
f1d70 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 it(void){ . /*
f1d80 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . ** The follow
f1d90 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 ing macro define
f1da0 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72 s an initializer
f1db0 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f for an sqlite3_
f1dc0 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a vfs object.. **
f1dd0 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 The name of the
f1de0 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54 VFS is NAME. T
f1df0 68 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61 he pAppData is a
f1e00 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f pointer to a po
f1e10 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 inter. ** to th
f1e20 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 e "finder" funct
f1e30 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20 ion. (pAppData
f1e40 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
f1e50 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73 a pointer becaus
f1e60 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30 e. ** silly C90
f1e70 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20 rules prohibit
f1e80 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69 a void* from bei
f1e90 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e ng cast to a fun
f1ea0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20 ction pointer.
f1eb0 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76 ** and so we hav
f1ec0 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 e to go through
f1ed0 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 the intermediate
f1ee0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69 pointer to avoi
f1ef0 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 d problems. **
f1f00 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77 when compiling w
f1f10 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 ith -pedantic-er
f1f20 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20 rors on GCC.).
f1f30 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44 **. ** The FIND
f1f40 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 ER parameter to
f1f50 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68 this macro is th
f1f60 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f e name of the po
f1f70 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a inter to the. *
f1f80 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f * finder-functio
f1f90 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66 n. The finder-f
f1fa0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
f1fb0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
f1fc0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f . ** sqlite_io_
f1fd0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74 methods object t
f1fe0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 hat implements t
f1ff0 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69 he desired locki
f2000 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 ng. ** behavior
f2010 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69 s. See the divi
f2020 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20 sion above that
f2030 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d contains the IOM
f2040 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72 ETHODS. ** macr
f2050 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69 o for addition i
f2060 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 nformation on fi
f2070 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a nder-functions..
f2080 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 **. ** Most f
f2090 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65 inders simply re
f20a0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
f20b0 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 o a fixed sqlite
f20c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 3_io_methods. *
f20d0 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74 * object. But t
f20e0 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 he "autolockIoFi
f20f0 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20 nder" available
f2100 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61 on MacOSX does a
f2110 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 little. ** mor
f2120 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20 e than that; it
f2130 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c looks at the fil
f2140 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61 esystem type tha
f2150 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a t hosts the . *
f2160 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 * database file
f2170 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f and tries to cho
f2180 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d ose an locking m
f2190 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74 ethod appropriat
f21a0 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20 e for. ** that
f21b0 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e filesystem time.
f21c0 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 . */. #define
f21d0 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c UNIXVFS(VFSNAME,
f21e0 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20 FINDER) {
f21f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2200 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20 \. 1,
f2210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
f2220 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 iVersion */
f2230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2240 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 \. sizeof(uni
f2250 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73 xFile), /* s
f2260 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20 zOsFile */
f2270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f2280 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 MAX_PATHNAME
f2290 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 , /* mxP
f22a0 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 athname */
f22b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f22c0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
f22d0 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 /* pNext
f22e0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f22f0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f2300 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 VFSNAME,
f2310 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a /* zName *
f2320 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f2330 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76 \. (v
f2340 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20 oid*)&FINDER,
f2350 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 /* pAppData
f2360 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f2370 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f2380 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 Open,
f2390 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20 /* xOpen */
f23a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f23b0 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65 \. unixDe
f23c0 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 lete,
f23d0 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20 /* xDelete */
f23e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f23f0 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65 \. unixAcce
f2400 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ss, /*
f2410 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20 xAccess */
f2420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2430 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61 \. unixFullPa
f2440 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78 thname, /* x
f2450 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20 FullPathname */
f2460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f2470 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 unixDlOpen,
f2480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
f2490 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 Open */
f24a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f24b0 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20 unixDlError,
f24c0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 /* xDlEr
f24d0 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ror */
f24e0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f24f0 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20 unixDlSym,
f2500 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 /* xDlSym
f2510 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f2520 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
f2530 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 ixDlClose,
f2540 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 /* xDlClose
f2550 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f2560 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f2570 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 Randomness,
f2580 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 /* xRandomness
f2590 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f25a0 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c \. unixSl
f25b0 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 eep,
f25c0 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20 /* xSleep */
f25d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f25e0 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72 \. unixCurr
f25f0 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a entTime, /*
f2600 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f xCurrentTime */
f2610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2620 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73 \. unixGetLas
f2630 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78 tError /* x
f2640 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20 GetLastError */
f2650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f2660 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 }.. /*. ** A
f2670 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 ll default VFSes
f2680 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f for unix are co
f2690 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 ntained in the f
f26a0 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a ollowing array..
f26b0 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 **. ** Note t
f26c0 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f hat the sqlite3_
f26d0 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 vfs.pNext field
f26e0 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 of the VFS objec
f26f0 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 t is modified.
f2700 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 ** by the SQLite
f2710 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 core when the V
f2720 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 FS is registered
f2730 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 . So the follow
f2740 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 ing. ** array c
f2750 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a annot be const..
f2760 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 */. static sq
f2770 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d lite3_vfs aVfs[]
f2780 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f = {.#if SQLITE_
f2790 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
f27a0 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f TYLE && (OS_VXWO
f27b0 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f RKS || defined(_
f27c0 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55 _APPLE__)). U
f27d0 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 NIXVFS("unix",
f27e0 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b autolock
f27f0 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 IoFinder ),.#els
f2800 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 e. UNIXVFS("u
f2810 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 nix", p
f2820 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a osixIoFinder ),.
f2830 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 #endif. UNIXV
f2840 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 FS("unix-none",
f2850 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 nolockIoFind
f2860 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 er ),. UNIXVF
f2870 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 S("unix-dotfile"
f2880 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 , dotlockIoFind
f2890 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 er ),. UNIXVF
f28a0 53 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 20 20 S("unix-wfl",
f28b0 20 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e posixWflIoFin
f28c0 64 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 der ),.#if OS_VX
f28d0 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 WORKS. UNIXVF
f28e0 53 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d S("unix-namedsem
f28f0 22 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 ", semIoFinder )
f2900 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c ,.#endif.#if SQL
f2910 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
f2920 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 NG_STYLE. UNI
f2930 58 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 XVFS("unix-posix
f2940 22 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e ", posixIoFin
f2950 64 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56 der ),.#if !OS_V
f2960 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 XWORKS. UNIXV
f2970 46 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c FS("unix-flock",
f2980 20 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 flockIoFinde
f2990 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64 r ),.#endif.#end
f29a0 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e if.#if SQLITE_EN
f29b0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
f29c0 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f LE && defined(__
f29d0 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 APPLE__). UNI
f29e0 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c XVFS("unix-afp",
f29f0 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65 afpIoFinde
f2a00 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
f2a10 28 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20 ("unix-proxy",
f2a20 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20 proxyIoFinder
f2a30 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 ),.#endif. };.
f2a40 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b unsigned int i;
f2a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
f2a60 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 p counter */..
f2a70 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 /* Register all
f2a80 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e VFSes defined in
f2a90 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61 the aVfs[] arra
f2aa0 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 y */. for(i=0;
f2ab0 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f i<(sizeof(aVfs)/
f2ac0 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 sizeof(sqlite3_v
f2ad0 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 fs)); i++){.
f2ae0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
f2af0 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69 ster(&aVfs[i], i
f2b00 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ==0);. }. retu
f2b10 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d rn SQLITE_OK; .}
f2b20 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e ../*.** Shutdown
f2b30 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
f2b40 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e ystem interface.
f2b50 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72 .**.** Some oper
f2b60 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69 ating systems mi
f2b70 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73 ght need to do s
f2b80 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74 ome cleanup in t
f2b90 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 his routine,.**
f2ba0 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d to release dynam
f2bb0 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 ically allocated
f2bc0 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e objects. But n
f2bd0 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54 ot on unix..** T
f2be0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
f2bf0 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e no-op for unix.
f2c00 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
f2c10 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nt sqlite3_os_en
f2c20 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 d(void){ . retu
f2c30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d rn SQLITE_OK; .}
f2c40 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c . .#endif /* SQL
f2c50 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a ITE_OS_UNIX */..
f2c60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
f2c70 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 End of os_unix.c
f2c80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
f2c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
f2cb0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
f2cc0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69 Begin file os_wi
f2cd0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
f2ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
f2d00 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
f2d10 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
f2d20 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
f2d30 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
f2d40 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
f2d50 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
f2d60 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
f2d70 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
f2d80 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
f2d90 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
f2da0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
f2db0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
f2dc0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
f2dd0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
f2de0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
f2df0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
f2e00 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
f2e10 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
f2e20 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
f2e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2e70 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
f2e80 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
f2e90 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63 ode that is spec
f2ea0 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e ific to windows.
f2eb0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
f2ec0 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 S_WIN
f2ed0 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 /* This file
f2ee0 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e is used for win
f2ef0 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f dows only */.../
f2f00 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 *.** A Note Abou
f2f10 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 t Memory Allocat
f2f20 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion:.**.** This
f2f30 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c driver uses mall
f2f40 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 oc()/free() dire
f2f50 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e ctly rather than
f2f60 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a going through.*
f2f70 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 * the SQLite-wra
f2f80 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 ppers sqlite3_ma
f2f90 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 lloc()/sqlite3_f
f2fa0 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 ree(). Those wr
f2fb0 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 appers.** are de
f2fc0 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f signed for use o
f2fd0 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
f2fe0 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 ms where memory
f2ff0 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a is scarce and.**
f3000 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
f3010 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 happen frequent
f3020 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 ly. Win32 does
f3030 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 not typically ru
f3040 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 n on.** embedded
f3050 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 systems, and wh
f3060 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 en it does the d
f3070 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c evelopers normal
f3080 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a ly have bigger.*
f3090 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f * problems to wo
f30a0 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 rry about than r
f30b0 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 unning out of me
f30c0 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 mory. So there
f30d0 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 is not.** a comp
f30e0 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 elling need to u
f30f0 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e se the wrappers.
f3100 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 .**.** But there
f3110 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f is a good reaso
f3120 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 n to not use the
f3130 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 wrappers. If w
f3140 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 e use the.** wra
f3150 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 ppers then we wi
f3160 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 ll get simulated
f3170 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 malloc() failur
f3180 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a es within this.*
f3190 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 * driver. And t
f31a0 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b hat causes all k
f31b0 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 inds of problems
f31c0 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 for our tests.
f31d0 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 We.** could enh
f31e0 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 ance SQLite to d
f31f0 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 eal with simulat
f3200 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ed malloc failur
f3210 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 es within.** the
f3220 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 OS driver, but
f3230 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c the code to deal
f3240 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c with those fail
f3250 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a ure would not.**
f3260 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e be exercised on
f3270 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f Linux (which do
f3280 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
f3290 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 alloc() in the d
f32a0 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f river).** and so
f32b0 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 we would have d
f32c0 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e ifficulty writin
f32d0 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 g coverage tests
f32e0 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 for that.** cod
f32f0 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 e. Better to le
f3300 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 ave the code out
f3310 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a , we think..**.*
f3320 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 * The point of t
f3330 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 his discussion i
f3340 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 s as follows: W
f3350 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e hen creating a n
f3360 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 ew.** OS layer f
f3370 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 or an embedded s
f3380 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 ystem, if you us
f3390 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 e this file as a
f33a0 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 n example,.** av
f33b0 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d oid the use of m
f33c0 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 alloc()/free().
f33d0 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 Those routines
f33e0 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f work ok on windo
f33f0 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 ws.** desktops b
f3400 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 ut not so well i
f3410 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 n embedded syste
f3420 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 ms..*/..#include
f3430 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 <winbase.h>..#i
f3440 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a fdef __CYGWIN__.
f3450 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 # include <sys/c
f3460 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a ygwin.h>.#endif.
f3470 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 ./*.** Macros us
f3480 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
f3490 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
f34a0 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a o use threads..*
f34b0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 /.#if defined(TH
f34c0 52 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 READSAFE) && THR
f34d0 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 EADSAFE.# define
f34e0 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 SQLITE_W32_THRE
f34f0 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a ADS 1.#endif../*
f3500 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 .** Include code
f3510 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 that is common
f3520 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 to all os_*.c fi
f3530 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a les.*/./********
f3540 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f ****** Include o
f3550 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 s_common.h in th
f3560 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 e middle of os_w
f3570 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a in.c ***********
f3580 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
f3590 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
f35a0 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a e os_common.h **
f35b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f35c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f35d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
f35e0 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 4 May 22.**.** T
f35f0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
f3600 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
f3610 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
f3620 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
f3630 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
f3640 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
f3650 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
f3660 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
f3670 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
f3680 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
f3690 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
f36a0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
f36b0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
f36c0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
f36d0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
f36e0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
f36f0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
f3700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
f3750 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
f3760 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 tains macros and
f3770 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 a little bit of
f3780 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f code that is co
f3790 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f mmon to.** all o
f37a0 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 f the platform-s
f37b0 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f pecific files (o
f37c0 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 s_*.c) and is #i
f37d0 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f ncluded into tho
f37e0 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a se.** files..**.
f37f0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f ** This file sho
f3800 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 uld be #included
f3810 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 by the os_*.c f
f3820 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 iles only. It i
f3830 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 s not a.** gener
f3840 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 al purpose heade
f3850 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 r file..**.** $I
f3860 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 d: os_common.h,v
f3870 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34 1.38 2009/02/24
f3880 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c 18:40:50 daniel
f3890 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 k1977 Exp $.*/.#
f38a0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f ifndef _OS_COMMO
f38b0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 N_H_.#define _OS
f38c0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a _COMMON_H_../*.*
f38d0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 * At least two b
f38e0 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 ugs have slipped
f38f0 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 in because we c
f3900 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 hanged the MEMOR
f3910 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f Y_DEBUG.** macro
f3920 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 to SQLITE_DEBUG
f3930 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 and some older
f3940 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e makefiles have n
f3950 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a ot yet made the.
f3960 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 ** switch. The
f3970 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 following code s
f3980 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 hould catch this
f3990 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 problem at comp
f39a0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 ile-time..*/.#if
f39b0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 def MEMORY_DEBUG
f39c0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 .# error "The ME
f39d0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f MORY_DEBUG macro
f39e0 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 is obsolete. U
f39f0 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 se SQLITE_DEBUG
f3a00 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 instead.".#endif
f3a10 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
f3a20 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
f3a30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
f3a40 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 OSTrace = 0;.#de
f3a50 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 fine OSTRACE1(X)
f3a60 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
f3a70 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
f3a80 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
f3a90 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 (X).#define OSTR
f3aa0 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 ACE2(X,Y)
f3ab0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
f3ac0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
f3ad0 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 gPrintf(X,Y).#de
f3ae0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
f3af0 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c Y,Z) if( sql
f3b00 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
f3b10 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
f3b20 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
f3b30 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 OSTRACE4(X,Y,Z,A
f3b40 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f ) if( sqlite3O
f3b50 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
f3b60 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
f3b70 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Z,A).#define OST
f3b80 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 RACE5(X,Y,Z,A,B)
f3b90 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
f3ba0 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
f3bb0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
f3bc0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
f3bd0 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
f3be0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
f3bf0 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
f3c00 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
f3c10 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 Y,Z,A,B,C).#defi
f3c20 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c ne OSTRACE7(X,Y,
f3c30 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 Z,A,B,C,D) \.
f3c40 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 if(sqlite3OSTra
f3c50 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 ce) sqlite3Debug
f3c60 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
f3c70 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 ,C,D).#else.#def
f3c80 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a ine OSTRACE1(X).
f3c90 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 #define OSTRACE2
f3ca0 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 (X,Y).#define OS
f3cb0 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 TRACE3(X,Y,Z).#d
f3cc0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 efine OSTRACE4(X
f3cd0 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 ,Y,Z,A).#define
f3ce0 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 OSTRACE5(X,Y,Z,A
f3cf0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
f3d00 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
f3d10 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
f3d20 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
f3d30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
f3d40 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f Macros for perfo
f3d50 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 rmance tracing.
f3d60 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 Normally turned
f3d70 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b off. Only work
f3d80 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 s.** on i486 har
f3d90 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 dware..*/.#ifdef
f3da0 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 SQLITE_PERFORMA
f3db0 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a NCE_TRACE../* .*
f3dc0 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 * hwtime.h conta
f3dd0 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d ins inline assem
f3de0 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d bler code for im
f3df0 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 plementing .** h
f3e00 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 igh-performance
f3e10 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e timing routines.
f3e20 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
f3e30 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 *** Include hwti
f3e40 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 me.h in the midd
f3e50 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e le of os_common.
f3e60 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
f3e70 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
f3e80 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
f3e90 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
f3ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3ec0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d **/./*.** 2008 M
f3ed0 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 27.**.** The
f3ee0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
f3ef0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
f3f00 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
f3f10 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
f3f20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
f3f30 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
f3f40 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
f3f50 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
f3f60 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
f3f70 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
f3f80 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
f3f90 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
f3fa0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
f3fb0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
f3fc0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
f3fd0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
f3fe0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
f3ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4030 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
f4040 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
f4050 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f ns inline asm co
f4060 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e de for retrievin
f4070 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 g "high-performa
f4080 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 nce".** counters
f4090 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 for x86 class C
f40a0 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 PUs..**.** $Id:
f40b0 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 hwtime.h,v 1.3 2
f40c0 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 008/08/01 14:33:
f40d0 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 15 shane Exp $.*
f40e0 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d /.#ifndef _HWTIM
f40f0 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 E_H_.#define _HW
f4100 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 TIME_H_../*.** T
f4110 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
f4120 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tine only works
f4130 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 on pentium-class
f4140 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 (or newer) proc
f4150 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 essors..** It us
f4160 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 es the RDTSC opc
f4170 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 ode to read the
f4180 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 cycle count valu
f4190 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 e out of the.**
f41a0 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 processor and re
f41b0 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 turns that value
f41c0 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 . This can be u
f41d0 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 sed for high-res
f41e0 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a .** profiling..*
f41f0 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f /.#if (defined(_
f4200 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 _GNUC__) || defi
f4210 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 ned(_MSC_VER)) &
f4220 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e & \. (defin
f4230 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 ed(i386) || defi
f4240 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c ned(__i386__) ||
f4250 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 defined(_M_IX86
f4260 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 )).. #if define
f4270 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f d(__GNUC__).. _
f4280 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
f4290 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
f42a0 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
f42b0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c unsigned int l
f42c0 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 o, hi;. __as
f42d0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
f42e0 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 ("rdtsc" : "=a"
f42f0 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 (lo), "=d" (hi)
f4300 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 );. return (
f4310 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 sqlite_uint64)hi
f4320 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d << 32 | lo;. }
f4330 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 .. #elif define
f4340 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f d(_MSC_VER).. _
f4350 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 _declspec(naked)
f4360 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 __inline sqlite
f4370 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 _uint64 __cdecl
f4380 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
f4390 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 id){. __asm
f43a0 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a {. rdtsc.
f43b0 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 ret
f43c0 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 ; return value
f43d0 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 at EDX:EAX.
f43e0 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 }. }.. #endif
f43f0 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 ..#elif (defined
f4400 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 (__GNUC__) && de
f4410 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f fined(__x86_64__
f4420 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f )).. __inline__
f4430 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
f4440 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
f4450 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e d){. unsign
f4460 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 ed long val;.
f4470 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
f4480 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
f4490 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a : "=A" (val));.
f44a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c return val
f44b0 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 ;. }. .#elif (d
f44c0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
f44d0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 && defined(__pp
f44e0 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e c__)).. __inlin
f44f0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
f4500 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
f4510 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
f4520 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
f4530 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e retval;. un
f4540 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b signed long junk
f4550 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 ;. __asm__
f4560 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c __volatile__ ("\
f4570 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 n\. 1:
f4580 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c mftbu %1\
f4590 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
f45a0 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 mftb %L0
f45b0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
f45c0 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 mftbu %0
f45d0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
f45e0 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 cmpw %0
f45f0 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 ,%1\n\.
f4600 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 bne
f4610 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 1b".
f4620 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 : "=r" (r
f4630 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 etval), "=r" (ju
f4640 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 nk));. retu
f4650 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a rn retval;. }..
f4660 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 #else.. #error
f4670 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 Need implementat
f4680 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 ion of sqlite3Hw
f4690 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
f46a0 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a platform... /*.
f46b0 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 ** To compile
f46c0 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e without implemen
f46d0 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 ting sqlite3Hwti
f46e0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
f46f0 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 atform,. ** you
f4700 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 can remove the
f4710 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 above #error and
f4720 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 use the followi
f4730 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e ng. ** stub fun
f4740 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c ction. You will
f4750 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 lose timing sup
f4760 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 port for many.
f4770 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 ** of the debugg
f4780 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 ing and testing
f4790 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 utilities, but i
f47a0 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a t should at. **
f47b0 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 least compile a
f47c0 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c nd run.. */.SQL
f47d0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 ITE_PRIVATE sq
f47e0 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
f47f0 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
f4800 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 return ((sqlite
f4810 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 _uint64)0); }..#
f4820 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a endif..#endif /*
f4830 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d !defined(_HWTIM
f4840 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a E_H_) */../*****
f4850 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
f4860 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a hwtime.h ******
f4870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4890 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
f48a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
f48b0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
f48c0 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d ft off in os_com
f48d0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mon.h **********
f48e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 ********/..stati
f48f0 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 c sqlite_uint64
f4900 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 g_start;.static
f4910 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f sqlite_uint64 g_
f4920 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 elapsed;.#define
f4930 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 TIMER_START
f4940 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 g_start=sqlit
f4950 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 e3Hwtime().#defi
f4960 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 ne TIMER_END
f4970 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 g_elapsed=s
f4980 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 qlite3Hwtime()-g
f4990 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 _start.#define T
f49a0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 IMER_ELAPSED
f49b0 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 g_elapsed.#else
f49c0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 .#define TIMER_S
f49d0 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d TART.#define TIM
f49e0 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 ER_END.#define T
f49f0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 IMER_ELAPSED
f4a00 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ((sqlite_uint64
f4a10 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a )0).#endif../*.*
f4a20 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 * If we compile
f4a30 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
f4a40 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 TEST macro set,
f4a50 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 then the followi
f4a60 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 ng block.** of c
f4a70 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 ode will give us
f4a80 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 the ability to
f4a90 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 simulate a disk
f4aa0 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 I/O error. This
f4ab0 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 .** is used for
f4ac0 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 testing the I/O
f4ad0 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a recovery logic..
f4ae0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
f4af0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
f4b00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f int sqlite3_io_
f4b10 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 error_hit = 0;
f4b20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
f4b30 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f al number of I/O
f4b40 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 Errors */.SQLIT
f4b50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
f4b60 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 3_io_error_hardh
f4b70 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f it = 0; /
f4b80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d * Number of non-
f4b90 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f benign errors */
f4ba0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f4bb0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
f4bc0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 _pending = 0;
f4bd0 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f /* Count do
f4be0 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 wn to first I/O
f4bf0 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f error */.SQLITE_
f4c00 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
f4c10 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 io_error_persist
f4c20 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
f4c30 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f True if I/O erro
f4c40 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 rs persist */.SQ
f4c50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
f4c60 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 ite3_io_error_be
f4c70 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 nign = 0;
f4c80 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 /* True if err
f4c90 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a ors are benign *
f4ca0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
f4cb0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
f4cc0 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 l_pending = 0;.S
f4cd0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
f4ce0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d lite3_diskfull =
f4cf0 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 0;.#define Simu
f4d00 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
f4d10 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f n(X) sqlite3_io_
f4d20 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 error_benign=(X)
f4d30 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 .#define Simulat
f4d40 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 eIOError(CODE)
f4d50 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 \. if( (sqlite3
f4d60 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 _io_error_persis
f4d70 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f t && sqlite3_io_
f4d80 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 error_hit) \.
f4d90 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 || sqlite3_i
f4da0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d o_error_pending-
f4db0 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 - == 1 ) \.
f4dc0 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 { loca
f4dd0 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b l_ioerr(); CODE;
f4de0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c }.static void l
f4df0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 ocal_ioerr(){.
f4e00 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c IOTRACE(("IOERR\
f4e10 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f n"));. sqlite3_
f4e20 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a io_error_hit++;.
f4e30 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 if( !sqlite3_i
f4e40 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 o_error_benign )
f4e50 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
f4e60 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 r_hardhit++;.}.#
f4e70 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 define SimulateD
f4e80 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 iskfullError(COD
f4e90 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 E) \. if( sqli
f4ea0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e te3_diskfull_pen
f4eb0 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 ding ){ \. i
f4ec0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
f4ed0 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 ull_pending == 1
f4ee0 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 ){ \. loc
f4ef0 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 al_ioerr(); \.
f4f00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 sqlite3_dis
f4f10 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 kfull = 1; \.
f4f20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 sqlite3_io_e
f4f30 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a rror_hit = 1; \.
f4f40 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 CODE; \.
f4f50 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 }else{ \.
f4f60 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b sqlite3_disk
f4f70 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 full_pending--;
f4f80 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a \. } \. }.
f4f90 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 #else.#define Si
f4fa0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e mulateIOErrorBen
f4fb0 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 ign(X).#define S
f4fc0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 imulateIOError(A
f4fd0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
f4fe0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
f4ff0 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a A).#endif../*.**
f5000 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b When testing, k
f5010 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 eep a count of t
f5020 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 he number of ope
f5030 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 n files..*/.#ifd
f5040 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
f5050 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
f5060 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f lite3_open_file_
f5070 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 count = 0;.#defi
f5080 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 ne OpenCounter(X
f5090 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f ) sqlite3_open_
f50a0 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a file_count+=(X).
f50b0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 #else.#define Op
f50c0 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e enCounter(X).#en
f50d0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
f50e0 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d defined(_OS_COMM
f50f0 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a ON_H_) */../****
f5100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f5110 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a f os_common.h **
f5120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f5150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
f5160 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
f5170 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69 eft off in os_wi
f5180 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
f5190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
f51a0 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 * Some microsoft
f51b0 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 compilers lack
f51c0 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e this definition.
f51d0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 .*/.#ifndef INVA
f51e0 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 LID_FILE_ATTRIBU
f51f0 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 TES.# define INV
f5200 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
f5210 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29 UTES ((DWORD)-1)
f5220 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 .#endif../*.**
f5230 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 Determine if we
f5240 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 are dealing with
f5250 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 WindowsCE - whi
f5260 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a ch has a much.**
f5270 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f reduced API..*/
f5280 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
f5290 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72 INCE.# define Ar
f52a0 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 eFileApisANSI()
f52b0 31 0a 23 20 64 65 66 69 6e 65 20 47 65 74 44 69 1.# define GetDi
f52c0 73 6b 46 72 65 65 53 70 61 63 65 57 28 29 20 30 skFreeSpaceW() 0
f52d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
f52e0 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76 inCE lacks nativ
f52f0 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69 e support for fi
f5300 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65 le locking so we
f5310 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74 have to fake it
f5320 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f .** with some co
f5330 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a de of our own..*
f5340 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
f5350 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74 WINCE.typedef st
f5360 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b ruct winceLock {
f5370 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b . int nReaders;
f5380 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
f5390 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 of reader locks
f53a0 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 obtained */. B
f53b0 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20 OOL bPending;
f53c0 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 /* Indicates
f53d0 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 a pending lock h
f53e0 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 as been obtained
f53f0 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65 */. BOOL bRese
f5400 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64 rved; /* Ind
f5410 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65 icates a reserve
f5420 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 d lock has been
f5430 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f obtained */. BO
f5440 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20 OL bExclusive;
f5450 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 /* Indicates a
f5460 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
f5470 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e has been obtain
f5480 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 ed */.} winceLoc
f5490 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a k;.#endif../*.**
f54a0 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72 The winFile str
f54b0 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63 ucture is a subc
f54c0 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f lass of sqlite3_
f54d0 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74 file* specific t
f54e0 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70 o the win32.** p
f54f0 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72 ortability layer
f5500 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
f5510 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46 uct winFile winF
f5520 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46 ile;.struct winF
f5530 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 ile {. const sq
f5540 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
f5550 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 *pMethod;/* Mus
f5560 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 t be first */.
f5570 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20 HANDLE h;
f5580 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c /* Handl
f5590 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 e for accessing
f55a0 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e the file */. un
f55b0 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b signed char lock
f55c0 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66 type; /* Type of
f55d0 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 lock currently
f55e0 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c held on this fil
f55f0 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61 e */. short sha
f5600 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f redLockByte; /
f5610 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 * Randomly chose
f5620 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61 n byte used as a
f5630 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a shared lock */.
f5640 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e DWORD lastErrn
f5650 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 o; /* The
f5660 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 66 Windows errno f
f5670 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f rom the last I/O
f5680 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f 52 error */. DWOR
f5690 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 D sectorSize;
f56a0 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69 /* Sector si
f56b0 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65 ze of the device
f56c0 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a 23 file is on */.#
f56d0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f56e0 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c CE. WCHAR *zDel
f56f0 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 eteOnClose; /*
f5700 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
f5710 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73 delete when clos
f5720 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 ing */. HANDLE
f5730 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 hMutex;
f5740 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74 /* Mutex used t
f5750 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
f5760 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 to shared lock
f5770 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53 */ . HANDLE hS
f5780 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f hared; /
f5790 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 * Shared memory
f57a0 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72 segment used for
f57b0 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 locking */. wi
f57c0 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 nceLock local;
f57d0 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f /* Locks o
f57e0 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 btained by this
f57f0 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46 instance of winF
f5800 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f ile */. winceLo
f5810 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20 ck *shared;
f5820 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65 /* Global share
f5830 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f d lock memory fo
f5840 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23 r the file */.#
f5850 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 endif.};../*.**
f5860 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 70 Forward prototyp
f5870 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
f5880 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 t getSectorSize(
f5890 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
f58a0 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 *pVfs,. cons
f58b0 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
f58c0 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 e /* UTF-8 f
f58d0 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a ile name */.);..
f58e0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
f58f0 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20 ing variable is
f5900 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f (normally) set o
f5910 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68 nce and never ch
f5920 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66 anges.** thereaf
f5930 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73 ter. It records
f5940 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65 whether the ope
f5950 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 rating system is
f5960 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e Win95.** or Win
f5970 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f NT..**.** 0: O
f5980 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
f5990 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 unknown..** 1:
f59a0 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 Operating syste
f59b0 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 m is Win95..** 2
f59c0 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 : Operating sy
f59d0 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a stem is WinNT..*
f59e0 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f *.** In order to
f59f0 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74 facilitate test
f5a00 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73 ing on a WinNT s
f5a10 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20 ystem, the test
f5a20 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d fixture.** can m
f5a30 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73 anually set this
f5a40 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65 value to 1 to e
f5a50 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68 mulate Win98 beh
f5a60 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 avior..*/.#ifdef
f5a70 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
f5a80 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f5a90 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b te3_os_type = 0;
f5aa0 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e .#else.static in
f5ab0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 t sqlite3_os_typ
f5ac0 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f e = 0;.#endif../
f5ad0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
f5ae0 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 (non-zero) if w
f5af0 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e e are running un
f5b00 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b der WinNT, Win2K
f5b10 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 , WinXP,.** or W
f5b20 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 inCE. Return fa
f5b30 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 lse (zero) for W
f5b40 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 in95, Win98, or
f5b50 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 WinME..**.** Her
f5b60 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 e is an interest
f5b70 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a ing observation:
f5b80 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 Win95, Win98,
f5b90 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a and WinME lack.*
f5ba0 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 * the LockFileEx
f5bb0 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 () API. But we
f5bc0 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 can still static
f5bd0 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 ally link agains
f5be0 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 t that.** API as
f5bf0 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 long as we don'
f5c00 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 72 t call it when r
f5c10 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f unning Win95/98/
f5c20 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a ME. A call to.*
f5c30 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * this routine i
f5c40 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
f5c50 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 ine if the host
f5c60 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f is Win95/98/ME o
f5c70 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 r.** WinNT/2K/XP
f5c80 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c so that we will
f5c90 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 know whether or
f5ca0 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 not we can safe
f5cb0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c ly call.** the L
f5cc0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
f5cd0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
f5ce0 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 S_WINCE.# define
f5cf0 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c isNT() (1).#el
f5d00 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 se. static int
f5d10 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 isNT(void){.
f5d20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 if( sqlite3_os_t
f5d30 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ype==0 ){.
f5d40 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 OSVERSIONINFO sI
f5d50 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f nfo;. sInfo
f5d60 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f .dwOSVersionInfo
f5d70 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 Size = sizeof(sI
f5d80 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 nfo);. GetV
f5d90 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 ersionEx(&sInfo)
f5da0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
f5db0 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e os_type = sInfo.
f5dc0 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 dwPlatformId==VE
f5dd0 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 R_PLATFORM_WIN32
f5de0 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 _NT ? 2 : 1;.
f5df0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 }. return sq
f5e00 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 lite3_os_type==2
f5e10 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
f5e20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 SQLITE_OS_WINCE
f5e30 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 */../*.** Conver
f5e40 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 t a UTF-8 string
f5e50 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e to microsoft un
f5e60 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e icode (UTF-16?).
f5e70 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f .**.** Space to
f5e80 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e hold the return
f5e90 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 ed string is obt
f5ea0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
f5eb0 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 c..*/.static WCH
f5ec0 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64 AR *utf8ToUnicod
f5ed0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 e(const char *zF
f5ee0 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 ilename){. int
f5ef0 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a nChar;. WCHAR *
f5f00 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a zWideFilename;..
f5f10 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 nChar = MultiB
f5f20 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 yteToWideChar(CP
f5f30 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e _UTF8, 0, zFilen
f5f40 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 ame, -1, NULL, 0
f5f50 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 );. zWideFilena
f5f60 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 me = malloc( nCh
f5f70 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46 ar*sizeof(zWideF
f5f80 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 ilename[0]) );.
f5f90 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61 if( zWideFilena
f5fa0 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 me==0 ){. ret
f5fb0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 urn 0;. }. nCh
f5fc0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f ar = MultiByteTo
f5fd0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 WideChar(CP_UTF8
f5fe0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
f5ff0 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d -1, zWideFilenam
f6000 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28 e, nChar);. if(
f6010 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 nChar==0 ){.
f6020 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e free(zWideFilen
f6030 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46 ame);. zWideF
f6040 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d ilename = 0;. }
f6050 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46 . return zWideF
f6060 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ilename;.}../*.*
f6070 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 * Convert micros
f6080 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55 oft unicode to U
f6090 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 TF-8. Space to
f60a0 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 hold the returne
f60b0 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f d string is.** o
f60c0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c btained from mal
f60d0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
f60e0 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f char *unicodeTo
f60f0 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52 Utf8(const WCHAR
f6100 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 *zWideFilename)
f6110 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 {. int nByte;.
f6120 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f6130 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 ;.. nByte = Wid
f6140 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 eCharToMultiByte
f6150 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 (CP_UTF8, 0, zWi
f6160 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 deFilename, -1,
f6170 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 0, 0, 0, 0);. z
f6180 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f Filename = mallo
f6190 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 c( nByte );. if
f61a0 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 ( zFilename==0 )
f61b0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
f61c0 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 }. nByte = Wi
f61d0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 deCharToMultiByt
f61e0 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 e(CP_UTF8, 0, zW
f61f0 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c ideFilename, -1,
f6200 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 zFilename, nByt
f6210 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
f6220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6230 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 0, 0);. if( nB
f6240 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 yte == 0 ){.
f6250 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b free(zFilename);
f6260 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d . zFilename =
f6270 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
f6280 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f zFilename;.}../
f6290 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20 *.** Convert an
f62a0 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d ansi string to m
f62b0 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
f62c0 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a , based on the.*
f62d0 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 * current codepa
f62e0 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 ge settings for
f62f0 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a file apis..** .*
f6300 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 * Space to hold
f6310 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 the returned str
f6320 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a ing is obtained.
f6330 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a ** from malloc..
f6340 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 */.static WCHAR
f6350 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63 *mbcsToUnicode(c
f6360 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
f6370 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 name){. int nBy
f6380 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62 te;. WCHAR *zMb
f6390 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e csFilename;. in
f63a0 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 t codepage = Are
f63b0 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f FileApisANSI() ?
f63c0 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d CP_ACP : CP_OEM
f63d0 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d CP;.. nByte = M
f63e0 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 ultiByteToWideCh
f63f0 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 ar(codepage, 0,
f6400 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e zFilename, -1, N
f6410 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43 ULL,0)*sizeof(WC
f6420 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c HAR);. zMbcsFil
f6430 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 ename = malloc(
f6440 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 nByte*sizeof(zMb
f6450 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 csFilename[0]) )
f6460 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c ;. if( zMbcsFil
f6470 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 ename==0 ){.
f6480 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
f6490 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74 nByte = MultiByt
f64a0 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65 eToWideChar(code
f64b0 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 page, 0, zFilena
f64c0 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c me, -1, zMbcsFil
f64d0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20 ename, nByte);.
f64e0 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b if( nByte==0 ){
f64f0 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46 . free(zMbcsF
f6500 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d ilename);. zM
f6510 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b bcsFilename = 0;
f6520 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d . }. return zM
f6530 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a bcsFilename;.}..
f6540 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 /*.** Convert mi
f6550 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 crosoft unicode
f6560 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61 to multibyte cha
f6570 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62 racter string, b
f6580 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 ased on the.** u
f6590 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70 ser's Ansi codep
f65a0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 age..**.** Space
f65b0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 to hold the ret
f65c0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 urned string is
f65d0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
f65e0 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 malloc()..*/.st
f65f0 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f atic char *unico
f6600 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57 deToMbcs(const W
f6610 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e CHAR *zWideFilen
f6620 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ame){. int nByt
f6630 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 e;. char *zFile
f6640 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65 name;. int code
f6650 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70 page = AreFileAp
f6660 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43 isANSI() ? CP_AC
f6670 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 P : CP_OEMCP;..
f6680 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 nByte = WideCha
f6690 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 rToMultiByte(cod
f66a0 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 epage, 0, zWideF
f66b0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 ilename, -1, 0,
f66c0 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 0, 0, 0);. zFil
f66d0 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 ename = malloc(
f66e0 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a nByte );. if( z
f66f0 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 Filename==0 ){.
f6700 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
f6710 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 . nByte = WideC
f6720 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 harToMultiByte(c
f6730 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 odepage, 0, zWid
f6740 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a eFilename, -1, z
f6750 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c Filename, nByte,
f6760 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
f6770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
f6780 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 , 0);. if( nByt
f6790 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 e == 0 ){. fr
f67a0 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 ee(zFilename);.
f67b0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 zFilename = 0
f67c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
f67d0 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a Filename;.}../*.
f67e0 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69 ** Convert multi
f67f0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 byte character s
f6800 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20 tring to UTF-8.
f6810 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
f6820 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 he.** returned s
f6830 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 tring is obtaine
f6840 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e d from malloc().
f6850 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
f6860 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e har *sqlite3_win
f6870 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 32_mbcs_to_utf8(
f6880 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
f6890 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a ename){. char *
f68a0 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20 zFilenameUtf8;.
f68b0 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 WCHAR *zTmpWide
f68c0 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 ;.. zTmpWide =
f68d0 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46 mbcsToUnicode(zF
f68e0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 ilename);. if(
f68f0 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 zTmpWide==0 ){.
f6900 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
f6910 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 . zFilenameUtf8
f6920 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 = unicodeToUtf8
f6930 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 (zTmpWide);. fr
f6940 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 ee(zTmpWide);.
f6950 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 return zFilename
f6960 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 Utf8;.}../*.** C
f6970 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20 onvert UTF-8 to
f6980 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 multibyte charac
f6990 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61 ter string. Spa
f69a0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a ce to hold the .
f69b0 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 ** returned stri
f69c0 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 ng is obtained f
f69d0 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f rom malloc()..*/
f69e0 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74 .static char *ut
f69f0 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63 f8ToMbcs(const c
f6a00 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b har *zFilename){
f6a10 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 . char *zFilena
f6a20 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20 meMbcs;. WCHAR
f6a30 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54 *zTmpWide;.. zT
f6a40 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55 mpWide = utf8ToU
f6a50 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 nicode(zFilename
f6a60 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64 );. if( zTmpWid
f6a70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 e==0 ){. retu
f6a80 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c rn 0;. }. zFil
f6a90 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63 enameMbcs = unic
f6aa0 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69 odeToMbcs(zTmpWi
f6ab0 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70 de);. free(zTmp
f6ac0 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 Wide);. return
f6ad0 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d zFilenameMbcs;.}
f6ae0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
f6af0 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a WINCE./*********
f6b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f6b40 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e .** This section
f6b50 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 contains code f
f6b60 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a or WinCE only..*
f6b70 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 /./*.** WindowsC
f6b80 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 E does not have
f6b90 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75 a localtime() fu
f6ba0 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61 nction. So crea
f6bb0 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75 te a.** substitu
f6bc0 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d te..*/.struct tm
f6bd0 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74 *__cdecl localt
f6be0 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74 ime(const time_t
f6bf0 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20 *t).{. static
f6c00 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46 struct tm y;. F
f6c10 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d ILETIME uTm, lTm
f6c20 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70 ;. SYSTEMTIME p
f6c30 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e Tm;. sqlite3_in
f6c40 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d t64 t64;. t64 =
f6c50 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36 *t;. t64 = (t6
f6c60 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29 4 + 11644473600)
f6c70 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d *10000000;. uTm
f6c80 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d .dwLowDateTime =
f6c90 20 28 44 57 4f 52 44 29 28 74 36 34 20 26 20 30 (DWORD)(t64 & 0
f6ca0 78 46 46 46 46 46 46 46 46 29 3b 0a 20 20 75 54 xFFFFFFFF);. uT
f6cb0 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65 m.dwHighDateTime
f6cc0 3d 20 28 44 57 4f 52 44 29 28 74 36 34 20 3e 3e = (DWORD)(t64 >>
f6cd0 20 33 32 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65 32);. FileTime
f6ce0 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28 ToLocalFileTime(
f6cf0 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 &uTm,&lTm);. Fi
f6d00 6c 65 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 69 leTimeToSystemTi
f6d10 6d 65 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 me(&lTm,&pTm);.
f6d20 20 79 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d y.tm_year = pTm
f6d30 2e 77 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 20 .wYear - 1900;.
f6d40 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e y.tm_mon = pTm.
f6d50 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e wMonth - 1;. y.
f6d60 74 6d 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 44 tm_wday = pTm.wD
f6d70 61 79 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d ayOfWeek;. y.tm
f6d80 5f 6d 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 _mday = pTm.wDay
f6d90 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 ;. y.tm_hour =
f6da0 70 54 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74 pTm.wHour;. y.t
f6db0 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e m_min = pTm.wMin
f6dc0 75 74 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20 ute;. y.tm_sec
f6dd0 3d 20 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 = pTm.wSecond;.
f6de0 20 72 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f return &y;.}../
f6df0 2a 20 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 65 * This will neve
f6e00 72 20 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 74 r be called, but
f6e10 20 64 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65 defined to make
f6e20 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c the code compil
f6e30 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 74 e */.#define Get
f6e40 54 65 6d 70 50 61 74 68 41 28 61 2c 62 29 0a 0a TempPathA(a,b)..
f6e50 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 #define LockFile
f6e60 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 (a,b,c,d,e)
f6e70 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 winceLockFile(
f6e80 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a &a, b, c, d, e).
f6e90 23 64 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 #define UnlockFi
f6ea0 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 le(a,b,c,d,e)
f6eb0 20 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c winceUnlockFil
f6ec0 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 e(&a, b, c, d, e
f6ed0 29 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 ).#define LockFi
f6ee0 6c 65 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 leEx(a,b,c,d,e,f
f6ef0 29 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c ) winceLockFil
f6f00 65 45 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c eEx(&a, b, c, d,
f6f10 20 65 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 20 e, f)..#define
f6f20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c HANDLE_TO_WINFIL
f6f30 45 28 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 26 E(a) (winFile*)&
f6f40 28 28 63 68 61 72 2a 29 61 29 5b 2d 28 69 6e 74 ((char*)a)[-(int
f6f50 29 6f 66 66 73 65 74 6f 66 28 77 69 6e 46 69 6c )offsetof(winFil
f6f60 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 e,h)]../*.** Acq
f6f70 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 uire a lock on t
f6f80 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f 0a 73 he handle h.*/.s
f6f90 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65 tatic void wince
f6fa0 4d 75 74 65 78 41 63 71 75 69 72 65 28 48 41 4e MutexAcquire(HAN
f6fb0 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f 52 44 DLE h){. DWORD
f6fc0 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20 7b 0a dwErr;. do {.
f6fd0 20 20 20 20 20 64 77 45 72 72 20 3d 20 57 61 69 dwErr = Wai
f6fe0 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74 tForSingleObject
f6ff0 28 68 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0a 20 (h, INFINITE);.
f7000 20 20 7d 20 77 68 69 6c 65 20 28 64 77 45 72 72 } while (dwErr
f7010 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 5f != WAIT_OBJECT_
f7020 30 20 26 26 20 64 77 45 72 72 20 21 3d 20 57 41 0 && dwErr != WA
f7030 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0a 7d IT_ABANDONED);.}
f7040 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
f7050 20 6c 6f 63 6b 20 61 63 71 75 69 72 65 64 20 62 lock acquired b
f7060 79 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 y winceMutexAcqu
f7070 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 ire().*/.#define
f7080 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 winceMutexRelea
f7090 73 65 28 68 29 20 52 65 6c 65 61 73 65 4d 75 74 se(h) ReleaseMut
f70a0 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 ex(h)../*.** Cre
f70b0 61 74 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e ate the mutex an
f70c0 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 d shared memory
f70d0 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 used for locking
f70e0 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 in the file.**
f70f0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 descriptor pFile
f7100 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 .*/.static BOOL
f7110 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28 winceCreateLock(
f7120 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
f7130 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c 65 20 2a ename, winFile *
f7140 70 46 69 6c 65 29 7b 0a 20 20 57 43 48 41 52 20 pFile){. WCHAR
f7150 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41 52 20 2a *zTok;. WCHAR *
f7160 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 6f 55 6e zName = utf8ToUn
f7170 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 icode(zFilename)
f7180 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69 74 20 3d ;. BOOL bInit =
f7190 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20 49 6e 69 TRUE;.. /* Ini
f71a0 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61 tialize the loca
f71b0 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 20 l lockdata */.
f71c0 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70 46 69 6c ZeroMemory(&pFil
f71d0 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a 65 6f 66 e->local, sizeof
f71e0 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b (pFile->local));
f71f0 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 .. /* Replace t
f7200 68 65 20 62 61 63 6b 73 6c 61 73 68 65 73 20 66 he backslashes f
f7210 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 rom the filename
f7220 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 65 20 69 and lowercase i
f7230 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72 69 76 65 t. ** to derive
f7240 20 61 20 6d 75 74 65 78 20 6e 61 6d 65 2e 20 2a a mutex name. *
f7250 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68 61 72 4c /. zTok = CharL
f7260 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 owerW(zName);.
f7270 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b for (;*zTok;zTok
f7280 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a 54 ++){. if (*zT
f7290 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f ok == '\\') *zTo
f72a0 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20 k = '_';. }..
f72b0 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 74 /* Create/open t
f72c0 68 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 2a he named mutex *
f72d0 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 /. pFile->hMute
f72e0 78 20 3d 20 43 72 65 61 74 65 4d 75 74 65 78 57 x = CreateMutexW
f72f0 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e (NULL, FALSE, zN
f7300 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46 69 ame);. if (!pFi
f7310 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 le->hMutex){.
f7320 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
f7330 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 o = GetLastError
f7340 28 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4e 61 ();. free(zNa
f7350 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 me);. return
f7360 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a FALSE;. }.. /*
f7370 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74 Acquire the mut
f7380 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e ex before contin
f7390 75 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4d uing */. winceM
f73a0 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c utexAcquire(pFil
f73b0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a 20 e->hMutex);. .
f73c0 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e 61 /* Since the na
f73d0 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75 74 mes of named mut
f73e0 65 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65 73 exes, semaphores
f73f0 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73 20 , file mappings
f7400 65 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63 61 etc are . ** ca
f7410 73 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74 61 se-sensitive, ta
f7420 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 ke advantage of
f7430 74 68 61 74 20 62 79 20 75 70 70 65 72 63 61 73 that by uppercas
f7440 69 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e 61 ing the mutex na
f7450 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69 6e me. ** and usin
f7460 67 20 74 68 61 74 20 61 73 20 74 68 65 20 73 68 g that as the sh
f7470 61 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e 67 ared filemapping
f7480 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 name.. */. Ch
f7490 61 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b arUpperW(zName);
f74a0 0a 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 . pFile->hShare
f74b0 64 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d 61 d = CreateFileMa
f74c0 70 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48 ppingW(INVALID_H
f74d0 41 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c ANDLE_VALUE, NUL
f74e0 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 L,.
f74f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7500 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f 52 PAGE_R
f7510 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a EADWRITE, 0, siz
f7520 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a eof(winceLock),.
f7530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7550 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 20 zName);
f7560 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c 61 .. /* Set a fla
f7570 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 g that indicates
f7580 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73 74 we're the first
f7590 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6d to create the m
f75a0 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 2a emory so it . *
f75b0 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 69 * must be zero-i
f75c0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 nitialized */.
f75d0 69 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f 72 if (GetLastError
f75e0 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 45 () == ERROR_ALRE
f75f0 41 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20 20 ADY_EXISTS){.
f7600 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b 0a bInit = FALSE;.
f7610 20 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61 6d }.. free(zNam
f7620 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 e);.. /* If we
f7630 73 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61 6b succeeded in mak
f7640 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6d ing the shared m
f7650 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d 61 emory handle, ma
f7660 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28 70 p it. */. if (p
f7670 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b 0a File->hShared){.
f7680 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
f7690 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 d = (winceLock*)
f76a0 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 MapViewOfFile(pF
f76b0 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a 20 ile->hShared, .
f76c0 20 20 20 20 20 20 20 20 20 20 20 20 46 49 4c 45 FILE
f76d0 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f 4d _MAP_READ|FILE_M
f76e0 41 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c 20 AP_WRITE, 0, 0,
f76f0 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b sizeof(winceLock
f7700 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 61 ));. /* If ma
f7710 70 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63 6c pping failed, cl
f7720 6f 73 65 20 74 68 65 20 73 68 61 72 65 64 20 6d ose the shared m
f7730 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e 64 emory handle and
f7740 20 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20 20 erase it */.
f7750 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68 61 if (!pFile->sha
f7760 72 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c red){. pFil
f7770 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
f7780 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
f7790 20 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 CloseHandle
f77a0 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 (pFile->hShared)
f77b0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 ;. pFile->h
f77c0 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b 0a 20 Shared = NULL;.
f77d0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
f77e0 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 f shared memory
f77f0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 72 65 could not be cre
f7800 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65 ated, then close
f7810 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20 66 the mutex and f
f7820 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 ail */. if (pFi
f7830 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d 20 4e le->hShared == N
f7840 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63 65 4d ULL){. winceM
f7850 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c utexRelease(pFil
f7860 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 e->hMutex);.
f7870 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c CloseHandle(pFil
f7880 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 e->hMutex);.
f7890 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 pFile->hMutex =
f78a0 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e NULL;. return
f78b0 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 FALSE;. }. .
f78c0 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
f78d0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 he shared memory
f78e0 20 69 66 20 77 65 27 72 65 20 73 75 70 70 6f 73 if we're suppos
f78f0 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66 20 28 62 ed to */. if (b
f7900 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a 65 72 6f Init) {. Zero
f7910 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d 3e 73 68 Memory(pFile->sh
f7920 61 72 65 64 2c 20 73 69 7a 65 6f 66 28 77 69 6e ared, sizeof(win
f7930 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d 0a 0a 20 ceLock));. }..
f7940 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 winceMutexRelea
f7950 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 se(pFile->hMutex
f7960 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 52 55 45 );. return TRUE
f7970 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 ;.}../*.** Destr
f7980 6f 79 20 74 68 65 20 70 61 72 74 20 6f 66 20 77 oy the part of w
f7990 69 6e 46 69 6c 65 20 74 68 61 74 20 64 65 61 6c inFile that deal
f79a0 73 20 77 69 74 68 20 77 69 6e 63 65 20 6c 6f 63 s with wince loc
f79b0 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ks.*/.static voi
f79c0 64 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f d winceDestroyLo
f79d0 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c ck(winFile *pFil
f79e0 65 29 7b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d e){. if (pFile-
f79f0 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 2f 2a >hMutex){. /*
f7a00 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74 Acquire the mut
f7a10 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d ex */. winceM
f7a20 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c utexAcquire(pFil
f7a30 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 20 e->hMutex);..
f7a40 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
f7a50 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 6c 64 20 g blocks should
f7a60 70 72 6f 62 61 62 6c 79 20 61 73 73 65 72 74 20 probably assert
f7a70 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 62 in debug mode, b
f7a80 75 74 20 74 68 65 79 0a 20 20 20 20 20 20 20 61 ut they. a
f7a90 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 20 69 6e re to cleanup in
f7aa0 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20 case any locks
f7ab0 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e 20 2a 2f remained open */
f7ac0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f7ad0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b local.nReaders){
f7ae0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 . pFile->sh
f7af0 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d ared->nReaders -
f7b00 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 -;. }. if
f7b10 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 (pFile->local.bR
f7b20 65 73 65 72 76 65 64 29 7b 0a 20 20 20 20 20 20 eserved){.
f7b30 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f7b40 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45 Reserved = FALSE
f7b50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 ;. }. if (
f7b60 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 pFile->local.bPe
f7b70 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 nding){. pF
f7b80 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 ile->shared->bPe
f7b90 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 nding = FALSE;.
f7ba0 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 }. if (pFi
f7bb0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 le->local.bExclu
f7bc0 73 69 76 65 29 7b 0a 20 20 20 20 20 20 70 46 69 sive){. pFi
f7bd0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 le->shared->bExc
f7be0 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a lusive = FALSE;.
f7bf0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 }.. /* De
f7c00 2d 72 65 66 65 72 65 6e 63 65 20 61 6e 64 20 63 -reference and c
f7c10 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 20 6f 66 lose our copy of
f7c20 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f the shared memo
f7c30 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 20 ry handle */.
f7c40 20 55 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c 65 UnmapViewOfFile
f7c50 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 3b (pFile->shared);
f7c60 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 . CloseHandle
f7c70 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 (pFile->hShared)
f7c80 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e 65 20 77 ;.. /* Done w
f7c90 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 2a 2f ith the mutex */
f7ca0 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 . winceMutexR
f7cb0 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d elease(pFile->hM
f7cc0 75 74 65 78 29 3b 20 20 20 20 0a 20 20 20 20 43 utex); . C
f7cd0 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 loseHandle(pFile
f7ce0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 70 ->hMutex);. p
f7cf0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e File->hMutex = N
f7d00 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a ULL;. }.}../* .
f7d10 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ** An implementa
f7d20 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b tion of the Lock
f7d30 46 69 6c 65 28 29 20 41 50 49 20 6f 66 20 77 69 File() API of wi
f7d40 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a ndows for wince.
f7d50 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 */.static BOOL w
f7d60 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 0a 20 20 inceLockFile(.
f7d70 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a HANDLE *phFile,.
f7d80 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 DWORD dwFileOf
f7d90 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 fsetLow,. DWORD
f7da0 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67 dwFileOffsetHig
f7db0 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 h,. DWORD nNumb
f7dc0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
f7dd0 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d ow,. DWORD nNum
f7de0 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b berOfBytesToLock
f7df0 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c High.){. winFil
f7e00 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c e *pFile = HANDL
f7e10 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46 E_TO_WINFILE(phF
f7e20 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 ile);. BOOL bRe
f7e30 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 turn = FALSE;..
f7e40 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
f7e50 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 R(dwFileOffsetHi
f7e60 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 gh);. UNUSED_PA
f7e70 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f RAMETER(nNumberO
f7e80 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 fBytesToLockHigh
f7e90 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 );.. if (!pFile
f7ea0 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e ->hMutex) return
f7eb0 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 TRUE;. winceMu
f7ec0 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 texAcquire(pFile
f7ed0 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a ->hMutex);.. /*
f7ee0 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 6c Wanting an excl
f7ef0 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 usive lock? */.
f7f00 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 if (dwFileOffse
f7f10 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 tLow == (DWORD)S
f7f20 48 41 52 45 44 5f 46 49 52 53 54 0a 20 20 20 20 HARED_FIRST.
f7f30 20 20 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 && nNumberOfB
f7f40 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d ytesToLockLow ==
f7f50 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53 (DWORD)SHARED_S
f7f60 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28 70 46 IZE){. if (pF
f7f70 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 ile->shared->nRe
f7f80 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20 70 46 aders == 0 && pF
f7f90 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 ile->shared->bEx
f7fa0 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 clusive == 0){.
f7fb0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f7fc0 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 red->bExclusive
f7fd0 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 20 70 = TRUE;. p
f7fe0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 File->local.bExc
f7ff0 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 lusive = TRUE;.
f8000 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 bReturn =
f8010 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a TRUE;. }. }.
f8020 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 61 . /* Want a rea
f8030 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a 2f 0a d-only lock? */.
f8040 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c else if (dwFil
f8050 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 eOffsetLow == (D
f8060 57 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 WORD)SHARED_FIRS
f8070 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 T &&.
f8080 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f8090 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 LockLow == 1){.
f80a0 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 if (pFile->sh
f80b0 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 ared->bExclusive
f80c0 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 20 70 46 == 0){. pF
f80d0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 ile->local.nRead
f80e0 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66 ers ++;. if
f80f0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e (pFile->local.n
f8100 52 65 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20 Readers == 1){.
f8110 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 pFile->sh
f8120 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b ared->nReaders +
f8130 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 +;. }.
f8140 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b bReturn = TRUE;
f8150 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
f8160 20 57 61 6e 74 20 61 20 70 65 6e 64 69 6e 67 20 Want a pending
f8170 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 lock? */. else
f8180 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 if (dwFileOffset
f8190 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 50 45 Low == (DWORD)PE
f81a0 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 6e 4e NDING_BYTE && nN
f81b0 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f umberOfBytesToLo
f81c0 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 ckLow == 1){.
f81d0 20 2f 2a 20 49 66 20 6e 6f 20 70 65 6e 64 69 6e /* If no pendin
f81e0 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 g lock has been
f81f0 61 63 71 75 69 72 65 64 2c 20 74 68 65 6e 20 61 acquired, then a
f8200 63 71 75 69 72 65 20 69 74 20 2a 2f 0a 20 20 20 cquire it */.
f8210 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 if (pFile->shar
f8220 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 3d 20 ed->bPending ==
f8230 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 0) {. pFile
f8240 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 ->shared->bPendi
f8250 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 ng = TRUE;.
f8260 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 pFile->local.bP
f8270 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 ending = TRUE;.
f8280 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 bReturn = T
f8290 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a RUE;. }. }..
f82a0 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 73 65 /* Want a rese
f82b0 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 rved lock? */.
f82c0 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f else if (dwFileO
f82d0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f ffsetLow == (DWO
f82e0 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 54 45 RD)RESERVED_BYTE
f82f0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 && nNumberOfByt
f8300 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 esToLockLow == 1
f8310 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 ){. if (pFile
f8320 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 ->shared->bReser
f8330 76 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 ved == 0) {.
f8340 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f8350 3e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 >bReserved = TRU
f8360 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e E;. pFile->
f8370 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 local.bReserved
f8380 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52 = TRUE;. bR
f8390 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 eturn = TRUE;.
f83a0 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 }. }.. wince
f83b0 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 MutexRelease(pFi
f83c0 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 le->hMutex);. r
f83d0 65 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d eturn bReturn;.}
f83e0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 ../*.** An imple
f83f0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
f8400 20 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20 UnlockFile API
f8410 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 of windows for w
f8420 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 ince.*/.static B
f8430 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 OOL winceUnlockF
f8440 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 ile(. HANDLE *p
f8450 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 hFile,. DWORD d
f8460 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a wFileOffsetLow,.
f8470 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 DWORD dwFileOf
f8480 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 fsetHigh,. DWOR
f8490 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 D nNumberOfBytes
f84a0 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 ToUnlockLow,. D
f84b0 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 WORD nNumberOfBy
f84c0 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a tesToUnlockHigh.
f84d0 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 ){. winFile *pF
f84e0 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f ile = HANDLE_TO_
f84f0 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b WINFILE(phFile);
f8500 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 . BOOL bReturn
f8510 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 = FALSE;.. UNUS
f8520 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77 46 ED_PARAMETER(dwF
f8530 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b 0a ileOffsetHigh);.
f8540 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
f8550 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 ER(nNumberOfByte
f8560 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 29 3b 0a sToUnlockHigh);.
f8570 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 . if (!pFile->h
f8580 4d 75 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 Mutex) return TR
f8590 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 UE;. winceMutex
f85a0 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 Acquire(pFile->h
f85b0 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 52 65 Mutex);.. /* Re
f85c0 6c 65 61 73 69 6e 67 20 61 20 72 65 61 64 65 72 leasing a reader
f85d0 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 65 78 63 6c lock or an excl
f85e0 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 usive lock */.
f85f0 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 if (dwFileOffset
f8600 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 Low == (DWORD)SH
f8610 41 52 45 44 5f 46 49 52 53 54 29 7b 0a 20 20 20 ARED_FIRST){.
f8620 20 2f 2a 20 44 69 64 20 77 65 20 68 61 76 65 20 /* Did we have
f8630 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
f8640 6b 3f 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 k? */. if (pF
f8650 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c ile->local.bExcl
f8660 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 61 73 usive){. as
f8670 73 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 79 sert(nNumberOfBy
f8680 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d tesToUnlockLow =
f8690 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f = (DWORD)SHARED_
f86a0 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 70 46 69 SIZE);. pFi
f86b0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 le->local.bExclu
f86c0 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 sive = FALSE;.
f86d0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
f86e0 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 d->bExclusive =
f86f0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 FALSE;. bRe
f8700 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 turn = TRUE;.
f8710 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 }.. /* Did w
f8720 65 20 6a 75 73 74 20 68 61 76 65 20 61 20 72 65 e just have a re
f8730 61 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 ader lock? */.
f8740 20 20 65 6c 73 65 20 69 66 20 28 70 46 69 6c 65 else if (pFile
f8750 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 ->local.nReaders
f8760 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
f8770 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f8780 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 UnlockLow == (DW
f8790 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 20 ORD)SHARED_SIZE
f87a0 7c 7c 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 || nNumberOfByte
f87b0 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 sToUnlockLow ==
f87c0 31 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 1);. pFile-
f87d0 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 >local.nReaders
f87e0 2d 2d 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 --;. if (pF
f87f0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 ile->local.nRead
f8800 65 72 73 20 3d 3d 20 30 29 0a 20 20 20 20 20 20 ers == 0).
f8810 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
f8820 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 >shared->nReader
f8830 73 20 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 s --;. }.
f8840 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 bReturn = TR
f8850 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 UE;. }. }..
f8860 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 /* Releasing a
f8870 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a pending lock */.
f8880 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c else if (dwFil
f8890 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 eOffsetLow == (D
f88a0 57 4f 52 44 29 50 45 4e 44 49 4e 47 5f 42 59 54 WORD)PENDING_BYT
f88b0 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 E && nNumberOfBy
f88c0 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d tesToUnlockLow =
f88d0 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 = 1){. if (pF
f88e0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 ile->local.bPend
f88f0 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c ing){. pFil
f8900 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e e->local.bPendin
f8910 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 g = FALSE;.
f8920 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
f8930 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 bPending = FALSE
f8940 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 ;. bReturn
f8950 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
f8960 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 }. /* Releasing
f8970 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b a reserved lock
f8980 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
f8990 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f89a0 3d 20 28 44 57 4f 52 44 29 52 45 53 45 52 56 45 = (DWORD)RESERVE
f89b0 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 D_BYTE && nNumbe
f89c0 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b rOfBytesToUnlock
f89d0 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 Low == 1){. i
f89e0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e f (pFile->local.
f89f0 62 52 65 73 65 72 76 65 64 29 20 7b 0a 20 20 20 bReserved) {.
f8a00 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e pFile->local.
f8a10 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 bReserved = FALS
f8a20 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e E;. pFile->
f8a30 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 shared->bReserve
f8a40 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 d = FALSE;.
f8a50 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b bReturn = TRUE;
f8a60 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 . }. }.. wi
f8a70 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 nceMutexRelease(
f8a80 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
f8a90 20 20 72 65 74 75 72 6e 20 62 52 65 74 75 72 6e return bReturn
f8aa0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d ;.}../*.** An im
f8ab0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
f8ac0 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 the LockFileEx()
f8ad0 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 API of windows
f8ae0 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 for wince.*/.sta
f8af0 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f tic BOOL winceLo
f8b00 63 6b 46 69 6c 65 45 78 28 0a 20 20 48 41 4e 44 ckFileEx(. HAND
f8b10 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 LE *phFile,. DW
f8b20 4f 52 44 20 64 77 46 6c 61 67 73 2c 0a 20 20 44 ORD dwFlags,. D
f8b30 57 4f 52 44 20 64 77 52 65 73 65 72 76 65 64 2c WORD dwReserved,
f8b40 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 . DWORD nNumber
f8b50 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 OfBytesToLockLow
f8b60 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 ,. DWORD nNumbe
f8b70 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 rOfBytesToLockHi
f8b80 67 68 2c 0a 20 20 4c 50 4f 56 45 52 4c 41 50 50 gh,. LPOVERLAPP
f8b90 45 44 20 6c 70 4f 76 65 72 6c 61 70 70 65 64 0a ED lpOverlapped.
f8ba0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
f8bb0 4d 45 54 45 52 28 64 77 52 65 73 65 72 76 65 64 METER(dwReserved
f8bc0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
f8bd0 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 METER(nNumberOfB
f8be0 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b ytesToLockHigh);
f8bf0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 .. /* If the ca
f8c00 6c 6c 65 72 20 77 61 6e 74 73 20 61 20 73 68 61 ller wants a sha
f8c10 72 65 64 20 72 65 61 64 20 6c 6f 63 6b 2c 20 66 red read lock, f
f8c20 6f 72 77 61 72 64 20 74 68 69 73 20 63 61 6c 6c orward this call
f8c30 0a 20 20 2a 2a 20 74 6f 20 77 69 6e 63 65 4c 6f . ** to winceLo
f8c40 63 6b 46 69 6c 65 20 2a 2f 0a 20 20 69 66 20 28 ckFile */. if (
f8c50 6c 70 4f 76 65 72 6c 61 70 70 65 64 2d 3e 4f 66 lpOverlapped->Of
f8c60 66 73 65 74 20 3d 3d 20 28 44 57 4f 52 44 29 53 fset == (DWORD)S
f8c70 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 HARED_FIRST &&.
f8c80 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d 20 dwFlags ==
f8c90 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d 62 1 &&. nNumb
f8ca0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
f8cb0 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 ow == (DWORD)SHA
f8cc0 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72 RED_SIZE){. r
f8cd0 65 74 75 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 eturn winceLockF
f8ce0 69 6c 65 28 70 68 46 69 6c 65 2c 20 53 48 41 52 ile(phFile, SHAR
f8cf0 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 31 2c 20 ED_FIRST, 0, 1,
f8d00 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0);. }. return
f8d10 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 FALSE;.}./*.**
f8d20 45 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63 69 End of the speci
f8d30 61 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e 63 al code for winc
f8d40 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.**************
f8d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
f8d90 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
f8da0 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f E_OS_WINCE */../
f8db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
f8e00 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 The next group
f8e10 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c of routines impl
f8e20 65 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 ement the I/O me
f8e30 74 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a thods specified.
f8e40 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 ** by the sqlite
f8e50 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
f8e60 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ect..***********
f8e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f8eb0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 ***/../*.** Clos
f8ec0 65 20 61 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 e a file..**.**
f8ed0 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74 It is reported t
f8ee0 68 61 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 hat an attempt t
f8ef0 6f 20 63 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65 o close a handle
f8f00 20 6d 69 67 68 74 20 73 6f 6d 65 74 69 6d 65 73 might sometimes
f8f10 0a 2a 2a 20 66 61 69 6c 2e 20 20 54 68 69 73 20 .** fail. This
f8f20 69 73 20 61 20 76 65 72 79 20 75 6e 72 65 61 73 is a very unreas
f8f30 6f 6e 61 62 6c 65 20 72 65 73 75 6c 74 2c 20 62 onable result, b
f8f40 75 74 20 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f ut windows is no
f8f50 74 6f 72 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 torious.** for b
f8f60 65 69 6e 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c eing unreasonabl
f8f70 65 20 73 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f e so I do not do
f8f80 75 62 74 20 74 68 61 74 20 69 74 20 6d 69 67 68 ubt that it migh
f8f90 74 20 68 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a t happen. If.**
f8fa0 20 74 68 65 20 63 6c 6f 73 65 20 66 61 69 6c 73 the close fails
f8fb0 2c 20 77 65 20 70 61 75 73 65 20 66 6f 72 20 31 , we pause for 1
f8fc0 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 00 milliseconds
f8fd0 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 20 20 and try again.
f8fe0 41 73 0a 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58 As.** many as MX
f8ff0 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 61 _CLOSE_ATTEMPT a
f9000 74 74 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 ttempts to close
f9010 20 74 68 65 20 68 61 6e 64 6c 65 20 61 72 65 20 the handle are
f9020 6d 61 64 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67 made before.** g
f9030 69 76 69 6e 67 20 75 70 20 61 6e 64 20 72 65 74 iving up and ret
f9040 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e urning an error.
f9050 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 .*/.#define MX_C
f9060 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 33 0a 73 LOSE_ATTEMPT 3.s
f9070 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f tatic int winClo
f9080 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
f9090 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 *id){. int rc,
f90a0 63 6e 74 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 cnt = 0;. winFi
f90b0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
f90c0 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 File*)id;.. ass
f90d0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
f90e0 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20 OSTRACE2("CLOSE
f90f0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 %d\n", pFile->h)
f9100 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72 63 20 3d ;. do{. rc =
f9110 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 CloseHandle(pFi
f9120 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68 69 6c 65 le->h);. }while
f9130 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b 63 6e 74 ( rc==0 && ++cnt
f9140 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 < MX_CLOSE_ATTE
f9150 4d 50 54 20 26 26 20 28 53 6c 65 65 70 28 31 30 MPT && (Sleep(10
f9160 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20 53 51 0), 1) );.#if SQ
f9170 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 64 LITE_OS_WINCE.#d
f9180 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45 4c 45 efine WINCE_DELE
f9190 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a TION_ATTEMPTS 3.
f91a0 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f winceDestroyLo
f91b0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 ck(pFile);. if(
f91c0 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f pFile->zDeleteO
f91d0 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20 69 6e nClose ){. in
f91e0 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77 t cnt = 0;. w
f91f0 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20 20 hile(.
f9200 20 44 65 6c 65 74 65 46 69 6c 65 57 28 70 46 69 DeleteFileW(pFi
f9210 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f le->zDeleteOnClo
f9220 73 65 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26 se)==0. &
f9230 26 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75 & GetFileAttribu
f9240 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c tesW(pFile->zDel
f9250 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66 eteOnClose)!=0xf
f9260 66 66 66 66 66 66 66 20 0a 20 20 20 20 20 20 20 fffffff .
f9270 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 49 4e 43 && cnt++ < WINC
f9280 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d E_DELETION_ATTEM
f9290 50 54 53 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 PTS. ){.
f92a0 20 20 53 6c 65 65 70 28 31 30 30 29 3b 20 20 2f Sleep(100); /
f92b0 2a 20 57 61 69 74 20 61 20 6c 69 74 74 6c 65 20 * Wait a little
f92c0 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 61 67 before trying ag
f92d0 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 ain */. }.
f92e0 20 66 72 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65 free(pFile->zDe
f92f0 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 leteOnClose);.
f9300 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70 65 6e 43 }.#endif. OpenC
f9310 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 72 65 ounter(-1);. re
f9320 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49 54 45 turn rc ? SQLITE
f9330 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
f9340 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d RR;.}../*.** Som
f9350 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70 e microsoft comp
f9360 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73 20 ilers lack this
f9370 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 definition..*/.#
f9380 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f 53 ifndef INVALID_S
f9390 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a ET_FILE_POINTER.
f93a0 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 44 # define INVALID
f93b0 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 _SET_FILE_POINTE
f93c0 52 20 28 28 44 57 4f 52 44 29 2d 31 29 0a 23 65 R ((DWORD)-1).#e
f93d0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 ndif../*.** Read
f93e0 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c data from a fil
f93f0 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e e into a buffer.
f9400 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
f9410 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 OK if all.** byt
f9420 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 es were read suc
f9430 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 cessfully and SQ
f9440 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e LITE_IOERR if an
f9450 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 ything goes.** w
f9460 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rong..*/.static
f9470 69 6e 74 20 77 69 6e 52 65 61 64 28 0a 20 20 73 int winRead(. s
f9480 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
f9490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
f94a0 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a e to read from *
f94b0 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 /. void *pBuf,
f94c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
f94d0 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 * Write content
f94e0 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 into this buffer
f94f0 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 */. int amt,
f9500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f9510 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
f9520 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 tes to read */.
f9530 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f sqlite3_int64 o
f9540 66 66 73 65 74 20 20 20 20 20 20 20 2f 2a 20 42 ffset /* B
f9550 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 egin reading at
f9560 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 this offset */.)
f9570 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 {. LONG upperBi
f9580 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66 ts = (LONG)((off
f9590 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 66 set>>32) & 0x7ff
f95a0 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c fffff);. LONG l
f95b0 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 owerBits = (LONG
f95c0 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66 66 )(offset & 0xfff
f95d0 66 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 fffff);. DWORD
f95e0 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 rc;. winFile *p
f95f0 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
f9600 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 )id;. DWORD err
f9610 6f 72 3b 0a 20 20 44 57 4f 52 44 20 67 6f 74 3b or;. DWORD got;
f9620 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d .. assert( id!=
f9630 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 0 );. SimulateI
f9640 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 OError(return SQ
f9650 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 29 LITE_IOERR_READ)
f9660 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 ;. OSTRACE3("RE
f9670 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 AD %d lock=%d\n"
f9680 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c , pFile->h, pFil
f9690 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 e->locktype);.
f96a0 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e rc = SetFilePoin
f96b0 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f ter(pFile->h, lo
f96c0 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 42 werBits, &upperB
f96d0 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 its, FILE_BEGIN)
f96e0 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 ;. if( rc==INVA
f96f0 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 LID_SET_FILE_POI
f9700 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d 47 NTER && (error=G
f9710 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21 3d etLastError())!=
f9720 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
f9730 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
f9740 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 = error;. re
f9750 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c turn SQLITE_FULL
f9760 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 65 61 ;. }. if( !Rea
f9770 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 dFile(pFile->h,
f9780 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c pBuf, amt, &got,
f9790 20 30 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 0) ){. pFile
f97a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 ->lastErrno = Ge
f97b0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
f97c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f97d0 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a IOERR_READ;. }.
f97e0 20 20 69 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52 if( got==(DWOR
f97f0 44 29 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 D)amt ){. ret
f9800 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
f9810 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55 }else{. /* U
f9820 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 nread parts of t
f9830 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 he buffer must b
f9840 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f e zero-filled */
f9850 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 . memset(&((c
f9860 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c har*)pBuf)[got],
f9870 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 0, amt-got);.
f9880 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
f9890 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
f98a0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 ;. }.}../*.** W
f98b0 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61 rite data from a
f98c0 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66 buffer into a f
f98d0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c ile. Return SQL
f98e0 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
f98f0 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 s.** or some oth
f9900 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e er error code on
f9910 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 failure..*/.sta
f9920 74 69 63 20 69 6e 74 20 77 69 6e 57 72 69 74 65 tic int winWrite
f9930 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
f9940 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f 2a *id, /*
f9950 20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69 File to write i
f9960 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 nto */. const v
f9970 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 oid *pBuf,
f9980 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 /* The bytes
f9990 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f to be written */
f99a0 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 . int amt,
f99b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f99c0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
f99d0 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 to write */. sq
f99e0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
f99f0 65 74 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 et /* Offse
f9a00 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 t into the file
f9a10 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67 to begin writing
f9a20 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 at */.){. LONG
f9a30 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f upperBits = (LO
f9a40 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 29 NG)((offset>>32)
f9a50 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a & 0x7fffffff);.
f9a60 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 LONG lowerBits
f9a70 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74 = (LONG)(offset
f9a80 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a & 0xffffffff);.
f9a90 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 DWORD rc;. wi
f9aa0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 nFile *pFile = (
f9ab0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 winFile*)id;. D
f9ac0 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 20 44 57 WORD error;. DW
f9ad0 4f 52 44 20 77 72 6f 74 65 20 3d 20 30 3b 0a 0a ORD wrote = 0;..
f9ae0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
f9af0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
f9b00 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 rror(return SQLI
f9b10 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 29 3b TE_IOERR_WRITE);
f9b20 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 . SimulateDiskf
f9b30 75 6c 6c 45 72 72 6f 72 28 72 65 74 75 72 6e 20 ullError(return
f9b40 53 51 4c 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 20 SQLITE_FULL);.
f9b50 4f 53 54 52 41 43 45 33 28 22 57 52 49 54 45 20 OSTRACE3("WRITE
f9b60 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 %d lock=%d\n", p
f9b70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e File->h, pFile->
f9b80 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 locktype);. rc
f9b90 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 = SetFilePointer
f9ba0 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 (pFile->h, lower
f9bb0 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 Bits, &upperBits
f9bc0 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 , FILE_BEGIN);.
f9bd0 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 if( rc==INVALID
f9be0 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 _SET_FILE_POINTE
f9bf0 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c R && (error=GetL
f9c00 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f astError())!=NO_
f9c10 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 ERROR ){. pFi
f9c20 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
f9c30 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 error;. retur
f9c40 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 n SQLITE_FULL;.
f9c50 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 }. assert( amt
f9c60 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20 >0 );. while(.
f9c70 20 20 20 20 61 6d 74 3e 30 0a 20 20 20 20 20 26 amt>0. &
f9c80 26 20 28 72 63 20 3d 20 57 72 69 74 65 46 69 6c & (rc = WriteFil
f9c90 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 e(pFile->h, pBuf
f9ca0 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30 , amt, &wrote, 0
f9cb0 29 29 21 3d 30 0a 20 20 20 20 20 26 26 20 77 72 ))!=0. && wr
f9cc0 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61 ote>0. ){. a
f9cd0 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 mt -= wrote;.
f9ce0 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a pBuf = &((char*
f9cf0 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 )pBuf)[wrote];.
f9d00 20 7d 0a 20 20 69 66 28 20 21 72 63 20 7c 7c 20 }. if( !rc ||
f9d10 61 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65 20 29 amt>(int)wrote )
f9d20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
f9d30 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
f9d40 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 Error();. ret
f9d50 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b urn SQLITE_FULL;
f9d60 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
f9d70 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
f9d80 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 * Truncate an op
f9d90 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 en file to a spe
f9da0 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 cified size.*/.s
f9db0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 54 72 75 tatic int winTru
f9dc0 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 ncate(sqlite3_fi
f9dd0 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f le *id, sqlite3_
f9de0 69 6e 74 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 int64 nByte){.
f9df0 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d LONG upperBits =
f9e00 20 28 4c 4f 4e 47 29 28 28 6e 42 79 74 65 3e 3e (LONG)((nByte>>
f9e10 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66 32) & 0x7fffffff
f9e20 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 );. LONG lowerB
f9e30 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 79 its = (LONG)(nBy
f9e40 74 65 20 26 20 30 78 66 66 66 66 66 66 66 66 29 te & 0xffffffff)
f9e50 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 ;. DWORD rc;.
f9e60 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
f9e70 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 (winFile*)id;.
f9e80 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 DWORD error;..
f9e90 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
f9ea0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 ;. OSTRACE3("TR
f9eb0 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e UNCATE %d %lld\n
f9ec0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 ", pFile->h, nBy
f9ed0 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 te);. SimulateI
f9ee0 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 OError(return SQ
f9ef0 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 LITE_IOERR_TRUNC
f9f00 41 54 45 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 ATE);. rc = Set
f9f10 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c FilePointer(pFil
f9f20 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c e->h, lowerBits,
f9f30 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c &upperBits, FIL
f9f40 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 E_BEGIN);. if(
f9f50 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f rc==INVALID_SET_
f9f60 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 FILE_POINTER &&
f9f70 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 (error=GetLastEr
f9f80 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 ror())!=NO_ERROR
f9f90 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
f9fa0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 astErrno = error
f9fb0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
f9fc0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 ITE_IOERR_TRUNCA
f9fd0 54 45 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 74 TE;. }. /* Set
f9fe0 45 6e 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66 EndOfFile will f
f9ff0 61 69 6c 20 69 66 20 6e 42 79 74 65 20 69 73 20 ail if nByte is
fa000 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 69 66 negative */. if
fa010 28 20 21 53 65 74 45 6e 64 4f 66 46 69 6c 65 28 ( !SetEndOfFile(
fa020 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 pFile->h) ){.
fa030 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
fa040 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 o = GetLastError
fa050 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ();. return S
fa060 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e QLITE_IOERR_TRUN
fa070 43 41 54 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 CATE;. }. retu
fa080 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
fa090 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
fa0a0 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 EST./*.** Count
fa0b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 the number of fu
fa0c0 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d llsyncs and norm
fa0d0 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 al syncs. This
fa0e0 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a is used to test.
fa0f0 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e ** that syncs an
fa100 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 d fullsyncs are
fa110 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 occuring at the
fa120 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a right times..*/.
fa130 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
fa140 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e qlite3_sync_coun
fa150 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 t = 0;.SQLITE_AP
fa160 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 I int sqlite3_fu
fa170 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 llsync_count = 0
fa180 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
fa190 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 Make sure all wr
fa1a0 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 ites to a partic
fa1b0 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f ular file are co
fa1c0 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e mmitted to disk.
fa1d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
fa1e0 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 inSync(sqlite3_f
fa1f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 ile *id, int fla
fa200 67 73 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c gs){.#ifndef SQL
fa210 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 69 ITE_NO_SYNC. wi
fa220 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 nFile *pFile = (
fa230 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 winFile*)id;..
fa240 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
fa250 0a 20 20 4f 53 54 52 41 43 45 33 28 22 53 59 4e . OSTRACE3("SYN
fa260 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c C %d lock=%d\n",
fa270 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 pFile->h, pFile
fa280 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 23 65 6c ->locktype);.#el
fa290 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 se. UNUSED_PARA
fa2a0 4d 45 54 45 52 28 69 64 29 3b 0a 23 65 6e 64 69 METER(id);.#endi
fa2b0 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 f.#ifndef SQLITE
fa2c0 5f 54 45 53 54 0a 20 20 55 4e 55 53 45 44 5f 50 _TEST. UNUSED_P
fa2d0 41 52 41 4d 45 54 45 52 28 66 6c 61 67 73 29 3b ARAMETER(flags);
fa2e0 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 66 6c 61 .#else. if( fla
fa2f0 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 gs & SQLITE_SYNC
fa300 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c _FULL ){. sql
fa310 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
fa320 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c unt++;. }. sql
fa330 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b ite3_sync_count+
fa340 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 +;.#endif. /* I
fa350 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 f we compiled wi
fa360 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f th the SQLITE_NO
fa370 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e _SYNC flag, then
fa380 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 syncing is a.
fa390 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 ** no-op. */.#i
fa3a0 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 fdef SQLITE_NO_S
fa3b0 59 4e 43 0a 20 20 20 20 72 65 74 75 72 6e 20 53 YNC. return S
fa3c0 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a QLITE_OK;.#else.
fa3d0 20 20 69 66 28 20 46 6c 75 73 68 46 69 6c 65 42 if( FlushFileB
fa3e0 75 66 66 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 uffers(pFile->h)
fa3f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
fa400 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
fa410 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 e{. pFile->la
fa420 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 stErrno = GetLas
fa430 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 tError();. re
fa440 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 turn SQLITE_IOER
fa450 52 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a R;. }.#endif.}.
fa460 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 ./*.** Determine
fa470 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a the current siz
fa480 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 e of a file in b
fa490 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ytes.*/.static i
fa4a0 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28 73 nt winFileSize(s
fa4b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fa4c0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a sqlite3_int64 *
fa4d0 70 53 69 7a 65 29 7b 0a 20 20 44 57 4f 52 44 20 pSize){. DWORD
fa4e0 75 70 70 65 72 42 69 74 73 3b 0a 20 20 44 57 4f upperBits;. DWO
fa4f0 52 44 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 RD lowerBits;.
fa500 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
fa510 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 (winFile*)id;.
fa520 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20 DWORD error;..
fa530 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
fa540 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
fa550 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
fa560 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a E_IOERR_FSTAT);.
fa570 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 47 65 lowerBits = Ge
fa580 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d tFileSize(pFile-
fa590 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 3b >h, &upperBits);
fa5a0 0a 20 20 69 66 28 20 20 20 28 6c 6f 77 65 72 42 . if( (lowerB
fa5b0 69 74 73 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 its == INVALID_F
fa5c0 49 4c 45 5f 53 49 5a 45 29 0a 20 20 20 20 20 26 ILE_SIZE). &
fa5d0 26 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c & ((error = GetL
fa5e0 61 73 74 45 72 72 6f 72 28 29 29 20 21 3d 20 4e astError()) != N
fa5f0 4f 5f 45 52 52 4f 52 29 20 29 0a 20 20 7b 0a 20 O_ERROR) ). {.
fa600 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
fa610 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 rno = error;.
fa620 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
fa630 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a OERR_FSTAT;. }.
fa640 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28 73 71 *pSize = (((sq
fa650 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70 70 65 lite3_int64)uppe
fa660 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f rBits)<<32) + lo
fa670 77 65 72 42 69 74 73 3b 0a 20 20 72 65 74 75 72 werBits;. retur
fa680 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
fa690 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 /*.** LOCKFILE_F
fa6a0 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 AIL_IMMEDIATELY
fa6b0 69 73 20 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 is undefined on
fa6c0 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20 73 79 73 some Windows sys
fa6d0 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tems..*/.#ifndef
fa6e0 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 LOCKFILE_FAIL_I
fa6f0 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20 64 65 66 MMEDIATELY.# def
fa700 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 ine LOCKFILE_FAI
fa710 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a L_IMMEDIATELY 1.
fa720 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 #endif../*.** Ac
fa730 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20 6c quire a reader l
fa740 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e ock..** Differen
fa750 74 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 61 t API routines a
fa760 72 65 20 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 re called depend
fa770 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f ing on whether o
fa780 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 r not this.** is
fa790 20 57 69 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e Win95 or WinNT.
fa7a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
fa7b0 65 74 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 etReadLock(winFi
fa7c0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e le *pFile){. in
fa7d0 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e t res;. if( isN
fa7e0 54 28 29 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c T() ){. OVERL
fa7f0 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20 APPED ovlp;.
fa800 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 53 48 ovlp.Offset = SH
fa810 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 ARED_FIRST;.
fa820 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68 20 ovlp.OffsetHigh
fa830 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45 = 0;. ovlp.hE
fa840 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 vent = 0;. re
fa850 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70 s = LockFileEx(p
fa860 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c File->h, LOCKFIL
fa870 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 E_FAIL_IMMEDIATE
fa880 4c 59 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 LY,.
fa890 20 20 20 20 20 20 20 20 20 30 2c 20 53 48 41 52 0, SHAR
fa8a0 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c ED_SIZE, 0, &ovl
fa8b0 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 p);./* isNT() is
fa8c0 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
fa8d0 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
fa8e0 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
fa8f0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 executed. .*/.#i
fa900 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
fa910 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 E==0. }else{.
fa920 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71 int lk;. sq
fa930 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
fa940 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b (sizeof(lk), &lk
fa950 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 );. pFile->sh
fa960 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 aredLockByte = (
fa970 73 68 6f 72 74 29 28 28 6c 6b 20 26 20 30 78 37 short)((lk & 0x7
fa980 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44 fffffff)%(SHARED
fa990 5f 53 49 5a 45 20 2d 20 31 29 29 3b 0a 20 20 20 _SIZE - 1));.
fa9a0 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 res = LockFile(
fa9b0 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fa9c0 5f 46 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68 _FIRST+pFile->sh
fa9d0 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c aredLockByte, 0,
fa9e0 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 1, 0);.#endif.
fa9f0 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 }. if( res ==
faa00 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 0 ){. pFile->
faa10 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c lastErrno = GetL
faa20 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a astError();. }.
faa30 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a return res;.}.
faa40 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 ./*.** Undo a re
faa50 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 adlock.*/.static
faa60 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c int unlockReadL
faa70 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 ock(winFile *pFi
faa80 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a le){. int res;.
faa90 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
faaa0 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b res = Unlock
faab0 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 File(pFile->h, S
faac0 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 HARED_FIRST, 0,
faad0 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b SHARED_SIZE, 0);
faae0 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
faaf0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
fab00 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
fab10 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
fab20 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 cuted. .*/.#if S
fab30 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
fab40 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 0. }else{. r
fab50 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 es = UnlockFile(
fab60 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fab70 5f 46 49 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e _FIRST + pFile->
fab80 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 sharedLockByte,
fab90 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0, 1, 0);.#endif
faba0 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d . }. if( res =
fabb0 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 = 0 ){. pFile
fabc0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 ->lastErrno = Ge
fabd0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
fabe0 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a }. return res;.
fabf0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
fac00 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
fac10 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
fac20 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
fac30 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
fac40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
fac50 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
fac60 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
fac70 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
fac80 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
fac90 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
faca0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
facb0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
facc0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
facd0 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
face0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
facf0 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
fad00 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
fad10 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
fad20 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
fad30 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
fad40 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
fad50 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
fad60 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
fad70 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
fad80 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
fad90 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
fada0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
fadb0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
fadc0 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
fadd0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
fade0 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
fadf0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
fae00 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
fae10 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
fae20 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
fae30 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
fae40 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
fae50 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
fae60 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
fae70 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
fae80 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
fae90 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
faea0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
faeb0 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
faec0 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
faed0 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63 k. The winUnloc
faee0 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 k() routine.** e
faef0 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 rases all locks
faf00 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 at once and retu
faf10 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 rns us immediate
faf20 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 ly to locking le
faf30 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 vel 0..** It is
faf40 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
faf50 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e lower the lockin
faf60 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 g level one step
faf70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 at a time. You
faf80 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 .** must go stra
faf90 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 ight to locking
fafa0 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 level 0..*/.stat
fafb0 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 ic int winLock(s
fafc0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fafd0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a int locktype){.
fafe0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
faff0 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75 E_OK; /* Retu
fb000 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 rn code from sub
fb010 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e routines */. in
fb020 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20 t res = 1;
fb030 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f /* Result o
fb040 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b f a windows lock
fb050 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e call */. int n
fb060 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20 ewLocktype;
fb070 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e /* Set pFile->
fb080 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73 locktype to this
fb090 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78 value before ex
fb0a0 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67 iting */. int g
fb0b0 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 otPendingLock =
fb0c0 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 0;/* True if we
fb0d0 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49 acquired a PENDI
fb0e0 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d NG lock this tim
fb0f0 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a e */. winFile *
fb100 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
fb110 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 *)id;. DWORD er
fb120 72 6f 72 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a ror = NO_ERROR;.
fb130 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 . assert( id!=0
fb140 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 );. OSTRACE5("
fb150 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 25 LOCK %d %d was %
fb160 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20 d(%d)\n",.
fb170 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f pFile->h, lo
fb180 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c cktype, pFile->l
fb190 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e ocktype, pFile->
fb1a0 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b sharedLockByte);
fb1b0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
fb1c0 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 is already a loc
fb1d0 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f k of this type o
fb1e0 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 r more restricti
fb1f0 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 4f ve on the. ** O
fb200 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e sFile, do nothin
fb210 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 g. Don't use the
fb220 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 end_lock: exit
fb230 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71 path, as. ** sq
fb240 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65 lite3OsEnterMute
fb250 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 x() hasn't been
fb260 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f called yet.. */
fb270 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
fb280 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 cktype>=locktype
fb290 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
fb2a0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
fb2b0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 /* Make sure th
fb2c0 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e e locking sequen
fb2d0 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 ce is correct.
fb2e0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 */. assert( pFi
fb2f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f le->locktype!=NO
fb300 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 _LOCK || locktyp
fb310 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
fb320 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
fb330 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f type!=PENDING_LO
fb340 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
fb350 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 locktype!=RESERV
fb360 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 ED_LOCK || pFile
fb370 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ->locktype==SHAR
fb380 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a ED_LOCK );.. /*
fb390 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49 4e Lock the PENDIN
fb3a0 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20 77 G_LOCK byte if w
fb3b0 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69 72 e need to acquir
fb3c0 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b e a PENDING lock
fb3d0 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 52 45 or. ** a SHARE
fb3e0 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 20 61 D lock. If we a
fb3f0 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53 re acquiring a S
fb400 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 HARED lock, the
fb410 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20 acquisition of.
fb420 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e 47 5f ** the PENDING_
fb430 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 74 65 6d LOCK byte is tem
fb440 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e porary.. */. n
fb450 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 46 69 ewLocktype = pFi
fb460 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 le->locktype;.
fb470 69 66 28 20 20 20 28 70 46 69 6c 65 2d 3e 6c 6f if( (pFile->lo
fb480 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 cktype==NO_LOCK)
fb490 0a 20 20 20 20 20 7c 7c 20 28 20 20 20 28 6c 6f . || ( (lo
fb4a0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
fb4b0 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20 20 E_LOCK).
fb4c0 20 26 26 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b && (pFile->lock
fb4d0 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c type==RESERVED_L
fb4e0 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 OCK)). ){. i
fb4f0 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 nt cnt = 3;.
fb500 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 while( cnt-->0 &
fb510 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c & (res = LockFil
fb520 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 e(pFile->h, PEND
fb530 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 ING_BYTE, 0, 1,
fb540 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 0))==0 ){.
fb550 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 73 20 74 /* Try 3 times t
fb560 6f 20 67 65 74 20 74 68 65 20 70 65 6e 64 69 6e o get the pendin
fb570 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e g lock. The pen
fb580 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 ding lock might
fb590 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 be. ** held
fb5a0 20 62 79 20 61 6e 6f 74 68 65 72 20 72 65 61 64 by another read
fb5b0 65 72 20 70 72 6f 63 65 73 73 20 77 68 6f 20 77 er process who w
fb5c0 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 74 20 6d ill release it m
fb5d0 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 omentarily..
fb5e0 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 */. OSTRA
fb5f0 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 CE2("could not g
fb600 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 et a PENDING loc
fb610 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e k. cnt=%d\n", cn
fb620 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 65 70 28 t);. Sleep(
fb630 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 1);. }. go
fb640 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 tPendingLock = r
fb650 65 73 3b 0a 20 20 20 20 69 66 28 20 21 72 65 73 es;. if( !res
fb660 20 29 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 ){. error
fb670 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
fb680 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
fb690 2a 20 41 63 71 75 69 72 65 20 61 20 73 68 61 72 * Acquire a shar
fb6a0 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 ed lock. */. i
fb6b0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
fb6c0 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 RED_LOCK && res
fb6d0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
fb6e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
fb6f0 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 NO_LOCK );. r
fb700 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b es = getReadLock
fb710 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 (pFile);. if(
fb720 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 res ){. ne
fb730 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 wLocktype = SHAR
fb740 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c ED_LOCK;. }el
fb750 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 se{. error
fb760 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
fb770 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
fb780 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 * Acquire a RESE
fb790 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 RVED lock. */.
fb7a0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 if( locktype==R
fb7b0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 ESERVED_LOCK &&
fb7c0 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 res ){. asser
fb7d0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
fb7e0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
fb7f0 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 );. res = Loc
fb800 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fb810 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 RESERVED_BYTE, 0
fb820 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 , 1, 0);. if(
fb830 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 res ){. ne
fb840 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45 wLocktype = RESE
fb850 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d RVED_LOCK;. }
fb860 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f else{. erro
fb870 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 r = GetLastError
fb880 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 ();. }. }..
fb890 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45 /* Acquire a PE
fb8a0 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a NDING lock. */.
fb8b0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
fb8c0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 EXCLUSIVE_LOCK &
fb8d0 26 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65 77 & res ){. new
fb8e0 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 Locktype = PENDI
fb8f0 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 NG_LOCK;. got
fb900 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b PendingLock = 0;
fb910 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
fb920 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 re an EXCLUSIVE
fb930 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
fb940 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
fb950 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 IVE_LOCK && res
fb960 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
fb970 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d File->locktype>=
fb980 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 SHARED_LOCK );.
fb990 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 res = unlockR
fb9a0 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a eadLock(pFile);.
fb9b0 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 75 6e OSTRACE2("un
fb9c0 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 readlock = %d\n"
fb9d0 2c 20 72 65 73 29 3b 0a 20 20 20 20 72 65 73 20 , res);. res
fb9e0 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 = LockFile(pFile
fb9f0 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
fba00 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a T, 0, SHARED_SIZ
fba10 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 E, 0);. if( r
fba20 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c es ){. newL
fba30 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53 ocktype = EXCLUS
fba40 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 IVE_LOCK;. }e
fba50 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 lse{. error
fba60 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
fba70 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 );. OSTRACE
fba80 32 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20 2("error-code =
fba90 25 64 5c 6e 22 2c 20 65 72 72 6f 72 29 3b 0a 20 %d\n", error);.
fbaa0 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 6b getReadLock
fbab0 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 (pFile);. }.
fbac0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 }.. /* If we a
fbad0 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e re holding a PEN
fbae0 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f DING lock that o
fbaf0 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 ught to be relea
fbb00 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 sed, then. ** r
fbb10 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 elease it now..
fbb20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e */. if( gotPen
fbb30 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b dingLock && lock
fbb40 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
fbb50 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 K ){. UnlockF
fbb60 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 ile(pFile->h, PE
fbb70 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 NDING_BYTE, 0, 1
fbb80 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 , 0);. }.. /*
fbb90 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74 65 Update the state
fbba0 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 of the lock has
fbbb0 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c held in the fil
fbbc0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 e descriptor the
fbbd0 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 n. ** return th
fbbe0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 e appropriate re
fbbf0 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a sult code.. */.
fbc00 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 if( res ){.
fbc10 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
fbc20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53 . }else{. OS
fbc30 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46 41 49 TRACE4("LOCK FAI
fbc40 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66 6f LED %d trying fo
fbc50 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64 5c r %d but got %d\
fbc60 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 n", pFile->h,.
fbc70 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 locktyp
fbc80 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 29 3b e, newLocktype);
fbc90 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
fbca0 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 Errno = error;.
fbcb0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
fbcc0 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 USY;. }. pFile
fbcd0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 ->locktype = (u8
fbce0 29 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 )newLocktype;.
fbcf0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
fbd00 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
fbd10 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
fbd20 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
fbd30 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
fbd40 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
fbd50 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
fbd60 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
fbd70 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
fbd80 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a s held, return.*
fbd90 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 * non-zero, othe
fbda0 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 rwise zero..*/.s
fbdb0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 68 65 tatic int winChe
fbdc0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 ckReservedLock(s
fbdd0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fbde0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a int *pResOut){.
fbdf0 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46 int rc;. winF
fbe00 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 ile *pFile = (wi
fbe10 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 nFile*)id;.. as
fbe20 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
fbe30 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
fbe40 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c type>=RESERVED_L
fbe50 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 OCK ){. rc =
fbe60 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 1;. OSTRACE3(
fbe70 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
fbe80 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 %d (local)\n",
fbe90 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 pFile->h, rc);.
fbea0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
fbeb0 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d LockFile(pFile-
fbec0 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 >h, RESERVED_BYT
fbed0 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 E, 0, 1, 0);.
fbee0 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
fbef0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c UnlockFile(pFil
fbf00 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 e->h, RESERVED_B
fbf10 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 YTE, 0, 1, 0);.
fbf20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 21 72 }. rc = !r
fbf30 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 c;. OSTRACE3(
fbf40 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
fbf50 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c %d (remote)\n",
fbf60 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a pFile->h, rc);.
fbf70 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d }. *pResOut =
fbf80 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 rc;. return SQ
fbf90 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
fbfa0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b * Lower the lock
fbfb0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c ing level on fil
fbfc0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20 e descriptor id
fbfd0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
fbfe0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
fbff0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
fc000 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
fc010 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
fc020 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
fc030 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
fc040 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
fc050 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
fc060 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
fc070 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
fc080 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
fc090 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e p..**.** It is n
fc0a0 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 ot possible for
fc0b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 this routine to
fc0c0 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f fail if the seco
fc0d0 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 nd argument.** i
fc0e0 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 s NO_LOCK. If t
fc0f0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
fc100 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 nt is SHARED_LOC
fc110 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 K then this rout
fc120 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 ine.** might ret
fc130 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fc140 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ;.*/.static int
fc150 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 winUnlock(sqlite
fc160 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
fc170 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 locktype){. int
fc180 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65 type;. winFile
fc190 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
fc1a0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 le*)id;. int rc
fc1b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
fc1c0 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 assert( pFile!=0
fc1d0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );. assert( lo
fc1e0 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
fc1f0 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 OCK );. OSTRACE
fc200 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 5("UNLOCK %d to
fc210 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e 22 %d was %d(%d)\n"
fc220 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
fc230 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20 type,.
fc240 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c pFile->locktype,
fc250 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f pFile->sharedLo
fc260 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 65 20 ckByte);. type
fc270 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 = pFile->locktyp
fc280 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 45 e;. if( type>=E
fc290 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b XCLUSIVE_LOCK ){
fc2a0 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 . UnlockFile(
fc2b0 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fc2c0 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 _FIRST, 0, SHARE
fc2d0 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 D_SIZE, 0);.
fc2e0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
fc2f0 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65 ARED_LOCK && !ge
fc300 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 tReadLock(pFile)
fc310 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 ){. /* Thi
fc320 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 s should never h
fc330 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c appen. We shoul
fc340 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 d always be able
fc350 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 to. ** rea
fc360 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 cquire the read
fc370 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63 lock */. rc
fc380 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
fc390 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 UNLOCK;. }.
fc3a0 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 }. if( type>=RE
fc3b0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 SERVED_LOCK ){.
fc3c0 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 UnlockFile(pF
fc3d0 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 ile->h, RESERVED
fc3e0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b _BYTE, 0, 1, 0);
fc3f0 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 . }. if( lockt
fc400 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 ype==NO_LOCK &&
fc410 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 type>=SHARED_LOC
fc420 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 K ){. unlockR
fc430 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a eadLock(pFile);.
fc440 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d }. if( type>=
fc450 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a PENDING_LOCK ){.
fc460 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 UnlockFile(p
fc470 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 File->h, PENDING
fc480 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b _BYTE, 0, 1, 0);
fc490 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f . }. pFile->lo
fc4a0 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6c 6f 63 cktype = (u8)loc
fc4b0 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 ktype;. return
fc4c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e rc;.}../*.** Con
fc4d0 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f trol and query o
fc4e0 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 f the open file
fc4f0 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 handle..*/.stati
fc500 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e c int winFileCon
fc510 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c trol(sqlite3_fil
fc520 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 e *id, int op, v
fc530 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 oid *pArg){. sw
fc540 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 itch( op ){.
fc550 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 case SQLITE_FCNT
fc560 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 L_LOCKSTATE: {.
fc570 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 *(int*)pArg
fc580 20 3d 20 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 = ((winFile*)id
fc590 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 )->locktype;.
fc5a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fc5b0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 _OK;. }. c
fc5c0 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f ase SQLITE_LAST_
fc5d0 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a ERRNO: {. *
fc5e0 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 69 6e (int*)pArg = (in
fc5f0 74 29 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 t)((winFile*)id)
fc600 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 ->lastErrno;.
fc610 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fc620 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 _OK;. }. }.
fc630 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
fc640 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 RROR;.}../*.** R
fc650 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 eturn the sector
fc660 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f size in bytes o
fc670 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 f the underlying
fc680 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f block device fo
fc690 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 r.** the specifi
fc6a0 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 ed file. This is
fc6b0 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 almost always 5
fc6c0 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 12 bytes, but ma
fc6d0 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 y be.** larger f
fc6e0 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e or some devices.
fc6f0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f .**.** SQLite co
fc700 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 de assumes this
fc710 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 function cannot
fc720 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 fail. It also as
fc730 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 sumes that.** if
fc740 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 two files are c
fc750 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 reated in the sa
fc760 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 me file-system d
fc770 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a irectory (i.e..*
fc780 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 * a database and
fc790 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c its journal fil
fc7a0 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 74 e) that the sect
fc7b0 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 or size will be
fc7c0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 the.** same for
fc7d0 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 both..*/.static
fc7e0 69 6e 74 20 77 69 6e 53 65 63 74 6f 72 53 69 7a int winSectorSiz
fc7f0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
fc800 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 id){. assert( i
fc810 64 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e d!=0 );. return
fc820 20 28 69 6e 74 29 28 28 28 77 69 6e 46 69 6c 65 (int)(((winFile
fc830 2a 29 69 64 29 2d 3e 73 65 63 74 6f 72 53 69 7a *)id)->sectorSiz
fc840 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 e);.}../*.** Ret
fc850 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 urn a vector of
fc860 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 device character
fc870 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 istics..*/.stati
fc880 63 20 69 6e 74 20 77 69 6e 44 65 76 69 63 65 43 c int winDeviceC
fc890 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 haracteristics(s
fc8a0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
fc8b0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
fc8c0 45 54 45 52 28 69 64 29 3b 0a 20 20 72 65 74 75 ETER(id);. retu
fc8d0 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn 0;.}../*.** T
fc8e0 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e his vector defin
fc8f0 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f es all the metho
fc900 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 ds that can oper
fc910 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c ate on an.** sql
fc920 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 77 69 ite3_file for wi
fc930 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 n32..*/.static c
fc940 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
fc950 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74 methods winIoMet
fc960 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 hod = {. 1,
fc970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fc980 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 /* iVersion
fc990 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 */. winClose,.
fc9a0 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57 winRead,. winW
fc9b0 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63 rite,. winTrunc
fc9c0 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a ate,. winSync,.
fc9d0 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 winFileSize,.
fc9e0 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 winLock,. winU
fc9f0 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63 nlock,. winChec
fca00 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 kReservedLock,.
fca10 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c winFileControl,
fca20 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 . winSectorSize
fca30 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43 68 61 ,. winDeviceCha
fca40 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a racteristics.};.
fca50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
fca60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fca70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fca80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fca90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
fcaa0 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 49 Here ends the I
fcab0 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 /O methods that
fcac0 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 33 form the sqlite3
fcad0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
fcae0 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 ct..**.** The ne
fcaf0 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 xt block of code
fcb00 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
fcb10 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a VFS methods..***
fcb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fcb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fcb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fcb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fcb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
fcb70 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d * Convert a UTF-
fcb80 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 8 filename into
fcb90 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20 74 68 whatever form th
fcba0 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 e underlying.**
fcbb0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
fcbc0 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73 wants filenames
fcbd0 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 68 in. Space to h
fcbe0 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 0a 2a old the result.*
fcbf0 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 * is obtained fr
fcc00 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 om malloc and mu
fcc10 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 st be freed by t
fcc20 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 he calling.** fu
fcc30 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 nction..*/.stati
fcc40 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55 c void *convertU
fcc50 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 tf8Filename(cons
fcc60 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
fcc70 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e e){. void *zCon
fcc80 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20 69 66 verted = 0;. if
fcc90 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 ( isNT() ){.
fcca0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66 zConverted = utf
fccb0 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 8ToUnicode(zFile
fccc0 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 name);./* isNT()
fccd0 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f is 1 if SQLITE_
fcce0 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 OS_WINCE==1, so
fccf0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 this else is nev
fcd00 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f er executed. .*/
fcd10 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
fcd20 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b INCE==0. }else{
fcd30 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 . zConverted
fcd40 3d 20 75 74 66 38 54 6f 4d 62 63 73 28 7a 46 69 = utf8ToMbcs(zFi
fcd50 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a lename);.#endif.
fcd60 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 20 }. /* caller
fcd70 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75 74 20 will handle out
fcd80 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 of memory */. r
fcd90 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74 65 64 eturn zConverted
fcda0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 ;.}../*.** Creat
fcdb0 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 e a temporary fi
fcdc0 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e le name in zBuf.
fcdd0 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62 zBuf must be b
fcde0 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 ig enough to.**
fcdf0 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 hold at pVfs->mx
fce00 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63 74 Pathname charact
fce10 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ers..*/.static i
fce20 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 nt getTempname(i
fce30 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
fce40 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20 63 Buf){. static c
fce50 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 har zChars[] =.
fce60 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c "abcdefghijkl
fce70 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a mnopqrstuvwxyz".
fce80 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b "ABCDEFGHIJK
fce90 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22 LMNOPQRSTUVWXYZ"
fcea0 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38 39 . "0123456789
fceb0 22 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 20 6a ";. size_t i, j
fcec0 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 61 ;. char zTempPa
fced0 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a th[MAX_PATH+1];.
fcee0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 if( sqlite3_te
fcef0 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a mp_directory ){.
fcf00 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
fcf10 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 intf(MAX_PATH-30
fcf20 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 , zTempPath, "%s
fcf30 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f ", sqlite3_temp_
fcf40 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65 directory);. }e
fcf50 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29 20 29 lse if( isNT() )
fcf60 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c {. char *zMul
fcf70 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20 7a 57 ti;. WCHAR zW
fcf80 69 64 65 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 idePath[MAX_PATH
fcf90 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61 ];. GetTempPa
fcfa0 74 68 57 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c thW(MAX_PATH-30,
fcfb0 20 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20 20 zWidePath);.
fcfc0 20 7a 4d 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64 zMulti = unicod
fcfd0 65 54 6f 55 74 66 38 28 7a 57 69 64 65 50 61 74 eToUtf8(zWidePat
fcfe0 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c h);. if( zMul
fcff0 74 69 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ti ){. sqli
fd000 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 te3_snprintf(MAX
fd010 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50 _PATH-30, zTempP
fd020 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74 ath, "%s", zMult
fd030 69 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 7a i);. free(z
fd040 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73 Multi);. }els
fd050 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
fd060 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
fd070 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 }./* isNT() is
fd080 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 1 if SQLITE_OS_
fd090 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 WINCE==1, so thi
fd0a0 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 s else is never
fd0b0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 executed. .** Si
fd0c0 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 nce the ASCII ve
fd0d0 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 rsion of these W
fd0e0 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f indows API do no
fd0f0 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 t exist for WINC
fd100 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 E,.** it's impor
fd110 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 tant to not refe
fd120 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 rence them for W
fd130 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a INCE builds..*/.
fd140 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
fd150 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a NCE==0. }else{.
fd160 20 20 20 20 63 68 61 72 20 2a 7a 55 74 66 38 3b char *zUtf8;
fd170 0a 20 20 20 20 63 68 61 72 20 7a 4d 62 63 73 50 . char zMbcsP
fd180 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 ath[MAX_PATH];.
fd190 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 41 28 GetTempPathA(
fd1a0 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 4d 62 MAX_PATH-30, zMb
fd1b0 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a 55 74 csPath);. zUt
fd1c0 66 38 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e f8 = sqlite3_win
fd1d0 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 32_mbcs_to_utf8(
fd1e0 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20 zMbcsPath);.
fd1f0 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20 20 20 if( zUtf8 ){.
fd200 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
fd210 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c ntf(MAX_PATH-30,
fd220 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22 zTempPath, "%s"
fd230 2c 20 7a 55 74 66 38 29 3b 0a 20 20 20 20 20 20 , zUtf8);.
fd240 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20 20 free(zUtf8);.
fd250 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 }else{. re
fd260 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
fd270 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a M;. }.#endif.
fd280 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 }. for(i=sqli
fd290 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d te3Strlen30(zTem
fd2a0 70 50 61 74 68 29 3b 20 69 3e 30 20 26 26 20 7a pPath); i>0 && z
fd2b0 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 TempPath[i-1]=='
fd2c0 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 \\'; i--){}. zT
fd2d0 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a empPath[i] = 0;.
fd2e0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
fd2f0 74 66 28 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 tf(nBuf-30, zBuf
fd300 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
fd310 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 "%s\\"SQLIT
fd320 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
fd330 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a IX, zTempPath);.
fd340 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 j = sqlite3Str
fd350 6c 65 6e 33 30 28 7a 42 75 66 29 3b 0a 20 20 73 len30(zBuf);. s
fd360 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
fd370 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b s(20, &zBuf[j]);
fd380 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30 . for(i=0; i<20
fd390 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 ; i++, j++){.
fd3a0 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 zBuf[j] = (char
fd3b0 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 )zChars[ ((unsig
fd3c0 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d ned char)zBuf[j]
fd3d0 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 )%(sizeof(zChars
fd3e0 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 )-1) ];. }. zB
fd3f0 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 uf[j] = 0;. OST
fd400 52 41 43 45 32 28 22 54 45 4d 50 20 46 49 4c 45 RACE2("TEMP FILE
fd410 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 NAME: %s\n", zBu
fd420 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c f);. return SQL
fd430 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a ITE_OK; .}../*.*
fd440 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c * The return val
fd450 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45 72 72 ue of getLastErr
fd460 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f orMsg.** is zero
fd470 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 if the error me
fd480 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 68 ssage fits in th
fd490 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e e buffer, or non
fd4a0 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 -zero.** otherwi
fd4b0 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61 se (if the messa
fd4c0 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 ge was truncated
fd4d0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
fd4e0 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 getLastErrorMsg
fd4f0 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 (int nBuf, char
fd500 2a 7a 42 75 66 29 7b 0a 20 20 44 57 4f 52 44 20 *zBuf){. DWORD
fd510 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 error = GetLastE
fd520 72 72 6f 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c rror();..#if SQL
fd530 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73 ITE_OS_WINCE. s
fd540 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
fd550 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 nBuf, zBuf, "OsE
fd560 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22 2c rror 0x%x (%u)",
fd570 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a error, error);.
fd580 23 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61 #else. /* Forma
fd590 74 4d 65 73 73 61 67 65 20 72 65 74 75 72 6e 73 tMessage returns
fd5a0 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 0 on failure.
fd5b0 4f 74 68 65 72 77 69 73 65 20 69 74 0a 20 20 2a Otherwise it. *
fd5c0 2a 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 * returns the nu
fd5d0 6d 62 65 72 20 6f 66 20 54 43 48 41 52 73 20 77 mber of TCHARs w
fd5e0 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 ritten to the ou
fd5f0 74 70 75 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 tput. ** buffer
fd600 2c 20 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20 , excluding the
fd610 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c terminating null
fd620 20 63 68 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 char.. */. if
fd630 20 28 21 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 (!FormatMessage
fd640 41 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 A(FORMAT_MESSAGE
fd650 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20 _FROM_SYSTEM,.
fd660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd670 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 NULL,.
fd680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd690 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 error,.
fd6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a 0,.
fd6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd6c0 20 20 20 20 20 20 7a 42 75 66 2c 0a 20 20 20 20 zBuf,.
fd6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd6e0 20 20 6e 42 75 66 2d 31 2c 0a 20 20 20 20 20 20 nBuf-1,.
fd6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd700 30 29 29 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 0)). {. sqli
fd710 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
fd720 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f f, zBuf, "OsErro
fd730 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 r 0x%x (%u)", er
fd740 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d ror, error);. }
fd750 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 .#endif.. retur
fd760 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 n 0;.}../*.** Op
fd770 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 en a file..*/.st
fd780 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e atic int winOpen
fd790 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
fd7a0 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a *pVfs, /*
fd7b0 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 Not used */. c
fd7c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
fd7d0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 , /* Name
fd7e0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55 54 of the file (UT
fd7f0 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 F-8) */. sqlite
fd800 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 3_file *id,
fd810 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
fd820 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e SQLite file han
fd830 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e dle here */. in
fd840 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 t flags,
fd850 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 /* Open
fd860 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 mode flags */.
fd870 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 int *pOutFlags
fd880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
fd890 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73 tus return flags
fd8a0 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20 */.){. HANDLE
fd8b0 68 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65 73 h;. DWORD dwDes
fd8c0 69 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44 57 iredAccess;. DW
fd8d0 4f 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 3b ORD dwShareMode;
fd8e0 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 74 . DWORD dwCreat
fd8f0 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a ionDisposition;.
fd900 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 41 DWORD dwFlagsA
fd910 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 30 ndAttributes = 0
fd920 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ;.#if SQLITE_OS_
fd930 57 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 54 65 WINCE. int isTe
fd940 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 mp = 0;.#endif.
fd950 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
fd960 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
fd970 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 void *zConvert
fd980 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
fd990 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 /* Filename
fd9a0 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a in OS encoding *
fd9b0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
fd9c0 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d zUtf8Name = zNam
fd9d0 65 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d e; /* Filenam
fd9e0 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 e in UTF-8 encod
fd9f0 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 ing */. char zT
fda00 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b mpname[MAX_PATH+
fda10 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 75 1]; /* Bu
fda20 66 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 65 ffer used to cre
fda30 61 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d ate temp filenam
fda40 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
fda50 69 64 21 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45 id!=0 );. UNUSE
fda60 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
fda70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
fda80 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
fda90 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
fdaa0 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 is NULL, genera
fdab0 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f te a . ** tempo
fdac0 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 rary file name t
fdad0 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 o use . */. if
fdae0 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a ( !zUtf8Name ){.
fdaf0 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74 int rc = get
fdb00 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 Tempname(MAX_PAT
fdb10 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a H+1, zTmpname);.
fdb20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
fdb30 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
fdb40 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
fdb50 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 zUtf8Name =
fdb60 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 zTmpname;. }..
fdb70 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 /* Convert the
fdb80 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 filename to the
fdb90 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e system encoding.
fdba0 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 */. zConverted
fdbb0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 = convertUtf8Fi
fdbc0 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 lename(zUtf8Name
fdbd0 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 );. if( zConver
fdbe0 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ted==0 ){. re
fdbf0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
fdc00 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c M;. }.. if( fl
fdc10 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
fdc20 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 N_READWRITE ){.
fdc30 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 dwDesiredAcce
fdc40 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 ss = GENERIC_REA
fdc50 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54 D | GENERIC_WRIT
fdc60 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 E;. }else{.
fdc70 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20 dwDesiredAccess
fdc80 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a = GENERIC_READ;.
fdc90 20 20 7d 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f }. /* SQLITE_
fdca0 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 69 OPEN_EXCLUSIVE i
fdcb0 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 s used to make s
fdcc0 75 72 65 20 74 68 61 74 20 61 20 6e 65 77 20 66 ure that a new f
fdcd0 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 63 72 65 ile is . ** cre
fdce0 61 74 65 64 2e 20 53 51 4c 69 74 65 20 64 6f 65 ated. SQLite doe
fdcf0 73 6e 27 74 20 75 73 65 20 69 74 20 74 6f 20 69 sn't use it to i
fdd00 6e 64 69 63 61 74 65 20 22 65 78 63 6c 75 73 69 ndicate "exclusi
fdd10 76 65 20 61 63 63 65 73 73 22 20 0a 20 20 2a 2a ve access" . **
fdd20 20 61 73 20 69 74 20 69 73 20 75 73 75 61 6c 6c as it is usuall
fdd30 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0a 20 20 y understood..
fdd40 2a 2f 0a 20 20 61 73 73 65 72 74 28 21 28 66 6c */. assert(!(fl
fdd50 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 ags & SQLITE_OPE
fdd60 4e 5f 45 58 43 4c 55 53 49 56 45 29 20 7c 7c 20 N_EXCLUSIVE) ||
fdd70 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f (flags & SQLITE_
fdd80 4f 50 45 4e 5f 43 52 45 41 54 45 29 29 3b 0a 20 OPEN_CREATE));.
fdd90 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
fdda0 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
fddb0 56 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65 VE ){. /* Cre
fddc0 61 74 65 73 20 61 20 6e 65 77 20 66 69 6c 65 2c ates a new file,
fddd0 20 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f 65 73 only if it does
fdde0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 not already exi
fddf0 73 74 2e 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 st. */. /* If
fde00 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 the file exists
fde10 2c 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f 0a 20 , it fails. */.
fde20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 dwCreationDis
fde30 70 6f 73 69 74 69 6f 6e 20 3d 20 43 52 45 41 54 position = CREAT
fde40 45 5f 4e 45 57 3b 0a 20 20 7d 65 6c 73 65 20 69 E_NEW;. }else i
fde50 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
fde60 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b E_OPEN_CREATE ){
fde70 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 65 78 69 . /* Open exi
fde80 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f 72 20 63 sting file, or c
fde90 72 65 61 74 65 20 69 66 20 69 74 20 64 6f 65 73 reate if it does
fdea0 6e 27 74 20 65 78 69 73 74 20 2a 2f 0a 20 20 20 n't exist */.
fdeb0 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f dwCreationDispo
fdec0 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c sition = OPEN_AL
fded0 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 WAYS;. }else{.
fdee0 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69 /* Opens a fi
fdef0 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 65 le, only if it e
fdf00 78 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 64 77 xists. */. dw
fdf10 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
fdf20 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 ion = OPEN_EXIST
fdf30 49 4e 47 3b 0a 20 20 7d 0a 20 20 64 77 53 68 61 ING;. }. dwSha
fdf40 72 65 4d 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48 reMode = FILE_SH
fdf50 41 52 45 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f ARE_READ | FILE_
fdf60 53 48 41 52 45 5f 57 52 49 54 45 3b 0a 20 20 69 SHARE_WRITE;. i
fdf70 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
fdf80 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
fdf90 4c 4f 53 45 20 29 7b 0a 23 69 66 20 53 51 4c 49 LOSE ){.#if SQLI
fdfa0 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20 TE_OS_WINCE.
fdfb0 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 dwFlagsAndAttrib
fdfc0 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 utes = FILE_ATTR
fdfd0 49 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a 20 20 IBUTE_HIDDEN;.
fdfe0 20 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 65 isTemp = 1;.#e
fdff0 6c 73 65 0a 20 20 20 20 64 77 46 6c 61 67 73 41 lse. dwFlagsA
fe000 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 ndAttributes = F
fe010 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 54 45 ILE_ATTRIBUTE_TE
fe020 4d 50 4f 52 41 52 59 0a 20 20 20 20 20 20 20 20 MPORARY.
fe030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe040 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 41 54 | FILE_AT
fe050 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e 0a 20 TRIBUTE_HIDDEN.
fe060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 |
fe080 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45 FILE_FLAG_DELETE
fe090 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69 _ON_CLOSE;.#endi
fe0a0 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 f. }else{. d
fe0b0 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 wFlagsAndAttribu
fe0c0 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 tes = FILE_ATTRI
fe0d0 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d BUTE_NORMAL;. }
fe0e0 0a 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 66 72 . /* Reports fr
fe0f0 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 74 20 om the internet
fe100 61 72 65 20 74 68 61 74 20 70 65 72 66 6f 72 6d are that perform
fe110 61 6e 63 65 20 69 73 20 61 6c 77 61 79 73 0a 20 ance is always.
fe120 20 2a 2a 20 62 65 74 74 65 72 20 69 66 20 46 49 ** better if FI
fe130 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41 LE_FLAG_RANDOM_A
fe140 43 43 45 53 53 20 69 73 20 75 73 65 64 2e 20 20 CCESS is used.
fe150 54 69 63 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f Ticket #2699. */
fe160 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
fe170 49 4e 43 45 0a 20 20 64 77 46 6c 61 67 73 41 6e INCE. dwFlagsAn
fe180 64 41 74 74 72 69 62 75 74 65 73 20 7c 3d 20 46 dAttributes |= F
fe190 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f ILE_FLAG_RANDOM_
fe1a0 41 43 43 45 53 53 3b 0a 23 65 6e 64 69 66 0a 20 ACCESS;.#endif.
fe1b0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
fe1c0 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c h = CreateFil
fe1d0 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 eW((WCHAR*)zConv
fe1e0 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 erted,. dw
fe1f0 44 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 DesiredAccess,.
fe200 20 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 dwShareMod
fe210 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a e,. NULL,.
fe220 20 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f dwCreatio
fe230 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 nDisposition,.
fe240 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 dwFlagsAndA
fe250 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 ttributes,.
fe260 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a NULL. );./*
fe270 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
fe280 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
fe290 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
fe2a0 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
fe2b0 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 ed. .** Since th
fe2c0 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 e ASCII version
fe2d0 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 of these Windows
fe2e0 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 API do not exis
fe2f0 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 t for WINCE,.**
fe300 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 it's important t
fe310 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 o not reference
fe320 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 them for WINCE b
fe330 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 uilds..*/.#if SQ
fe340 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 LITE_OS_WINCE==0
fe350 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 . }else{. h
fe360 3d 20 43 72 65 61 74 65 46 69 6c 65 41 28 28 63 = CreateFileA((c
fe370 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c har*)zConverted,
fe380 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72 65 . dwDesire
fe390 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20 dAccess,.
fe3a0 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20 dwShareMode,.
fe3b0 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 NULL,.
fe3c0 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f dwCreationDispo
fe3d0 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64 sition,. d
fe3e0 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 wFlagsAndAttribu
fe3f0 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c tes,. NULL
fe400 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20 . );.#endif.
fe410 20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56 41 }. if( h==INVA
fe420 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 LID_HANDLE_VALUE
fe430 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f ){. free(zCo
fe440 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 69 66 nverted);. if
fe450 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
fe460 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
fe470 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
fe480 77 69 6e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e winOpen(pVfs, zN
fe490 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20 20 20 20 ame, id, .
fe4a0 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 7c 53 ((flags|S
fe4b0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
fe4c0 4e 4c 59 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45 NLY)&~SQLITE_OPE
fe4d0 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 20 70 4f N_READWRITE), pO
fe4e0 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65 utFlags);. }e
fe4f0 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
fe500 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 n SQLITE_CANTOPE
fe510 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 N;. }. }. i
fe520 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a f( pOutFlags ){.
fe530 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 if( flags &
fe540 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
fe550 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 2a WRITE ){. *
fe560 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49 pOutFlags = SQLI
fe570 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
fe580 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 E;. }else{.
fe590 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d *pOutFlags =
fe5a0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
fe5b0 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d DONLY;. }. }
fe5c0 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c . memset(pFile,
fe5d0 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 0, sizeof(*pFil
fe5e0 65 29 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d e));. pFile->pM
fe5f0 65 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65 ethod = &winIoMe
fe600 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 thod;. pFile->h
fe610 20 3d 20 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c = h;. pFile->l
fe620 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52 astErrno = NO_ER
fe630 52 4f 52 3b 0a 20 20 70 46 69 6c 65 2d 3e 73 65 ROR;. pFile->se
fe640 63 74 6f 72 53 69 7a 65 20 3d 20 67 65 74 53 65 ctorSize = getSe
fe650 63 74 6f 72 53 69 7a 65 28 70 56 66 73 2c 20 7a ctorSize(pVfs, z
fe660 55 74 66 38 4e 61 6d 65 29 3b 0a 23 69 66 20 53 Utf8Name);.#if S
fe670 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 QLITE_OS_WINCE.
fe680 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 28 53 if( (flags & (S
fe690 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
fe6a0 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
fe6b0 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 _MAIN_DB)) ==.
fe6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 (SQ
fe6d0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
fe6e0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ITE|SQLITE_OPEN_
fe6f0 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 MAIN_DB).
fe700 26 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c && !winceCreateL
fe710 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 ock(zName, pFile
fe720 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 ). ){. Close
fe730 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 Handle(h);. f
fe740 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
fe750 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
fe760 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d TE_CANTOPEN;. }
fe770 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b . if( isTemp ){
fe780 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c . pFile->zDel
fe790 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f eteOnClose = zCo
fe7a0 6e 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 nverted;. }else
fe7b0 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 .#endif. {.
fe7c0 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
fe7d0 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e ;. }. OpenCoun
fe7e0 74 65 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 ter(+1);. retur
fe7f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
fe800 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 /*.** Delete the
fe810 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a named file..**.
fe820 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e ** Note that win
fe830 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c dows does not al
fe840 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65 low a file to be
fe850 20 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 deleted if some
fe860 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 other.** proces
fe870 73 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 s has it open.
fe880 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75 Sometimes a viru
fe890 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 s scanner or ind
fe8a0 65 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a exing program.**
fe8b0 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 will open a jou
fe8c0 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c rnal file shortl
fe8d0 79 20 61 66 74 65 72 20 69 74 20 69 73 20 63 72 y after it is cr
fe8e0 65 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 eated in order t
fe8f0 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 o do.** whatever
fe900 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 it does. While
fe910 20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63 this other proc
fe920 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 ess is holding t
fe930 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c he.** file open,
fe940 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 we will be unab
fe950 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e le to delete it.
fe960 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 To work around
fe970 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d this.** problem
fe980 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d , we delay 100 m
fe990 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 illiseconds and
fe9a0 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67 try to delete ag
fe9b0 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d ain. Up.** to M
fe9c0 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d X_DELETION_ATTEM
fe9d0 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 PTs deletion att
fe9e0 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62 65 empts are run be
fe9f0 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 fore giving.** u
fea00 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 p and returning
fea10 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 an error..*/.#de
fea20 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e fine MX_DELETION
fea30 5f 41 54 54 45 4d 50 54 53 20 35 0a 73 74 61 74 _ATTEMPTS 5.stat
fea40 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 74 65 ic int winDelete
fea50 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
fea60 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 *pVfs,
fea70 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 /* Not used on w
fea80 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 in32 */. const
fea90 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
feaa0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
feab0 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 file to delete
feac0 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 */. int syncDir
fead0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
feae0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 /* Not used on
feaf0 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e win32 */.){. in
feb00 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f t cnt = 0;. DWO
feb10 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 65 RD rc;. DWORD e
feb20 72 72 6f 72 20 3d 20 30 3b 0a 20 20 76 6f 69 64 rror = 0;. void
feb30 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 *zConverted = c
feb40 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 onvertUtf8Filena
feb50 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 me(zFilename);.
feb60 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
feb70 52 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 R(pVfs);. UNUSE
feb80 44 5f 50 41 52 41 4d 45 54 45 52 28 73 79 6e 63 D_PARAMETER(sync
feb90 44 69 72 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e Dir);. if( zCon
feba0 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 verted==0 ){.
febb0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
febc0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 OMEM;. }. Simu
febd0 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 lateIOError(retu
febe0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
febf0 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 69 DELETE);. if( i
fec00 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b sNT() ){. do{
fec10 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c . DeleteFil
fec20 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a eW(zConverted);.
fec30 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 }while( (
fec40 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 ((rc = GetFile
fec50 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f 6e AttributesW(zCon
fec60 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 verted)) != INVA
fec70 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 LID_FILE_ATTRIBU
fec80 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 TES).
fec90 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d || ((error =
feca0 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 GetLastError())
fecb0 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 == ERROR_ACCESS
fecc0 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 _DENIED)).
fecd0 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c && (++cnt <
fece0 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 MX_DELETION_ATT
fecf0 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 EMPTS).
fed00 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 && (Sleep(100)
fed10 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 , 1) );./* isNT(
fed20 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
fed30 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
fed40 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
fed50 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
fed60 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 * Since the ASCI
fed70 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 I version of the
fed80 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 se Windows API d
fed90 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 o not exist for
feda0 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 WINCE,.** it's i
fedb0 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 mportant to not
fedc0 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 reference them f
fedd0 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e or WINCE builds.
fede0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
fedf0 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c S_WINCE==0. }el
fee00 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 se{. do{.
fee10 20 20 44 65 6c 65 74 65 46 69 6c 65 41 28 7a 43 DeleteFileA(zC
fee20 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d onverted);. }
fee30 77 68 69 6c 65 28 20 20 20 28 20 20 20 28 28 72 while( ( ((r
fee40 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69 c = GetFileAttri
fee50 62 75 74 65 73 41 28 7a 43 6f 6e 76 65 72 74 65 butesA(zConverte
fee60 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46 d)) != INVALID_F
fee70 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a ILE_ATTRIBUTES).
fee80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
fee90 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c | ((error = GetL
feea0 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45 astError()) == E
feeb0 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49 RROR_ACCESS_DENI
feec0 45 44 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 ED)).
feed0 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44 && (++cnt < MX_D
feee0 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 ELETION_ATTEMPTS
feef0 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 ). &&
fef00 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20 (Sleep(100), 1)
fef10 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
fef20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
fef30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 44 45 ;. OSTRACE2("DE
fef40 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 LETE \"%s\"\n",
fef50 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65 zFilename);. re
fef60 74 75 72 6e 20 28 20 20 20 28 72 63 20 3d 3d 20 turn ( (rc ==
fef70 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
fef80 52 49 42 55 54 45 53 29 20 0a 20 20 20 20 20 20 RIBUTES) .
fef90 20 20 20 20 26 26 20 28 65 72 72 6f 72 20 3d 3d && (error ==
fefa0 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f ERROR_FILE_NOT_
fefb0 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c 49 54 45 FOUND)) ? SQLITE
fefc0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
fefd0 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a RR_DELETE;.}../*
fefe0 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 78 .** Check the ex
feff0 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 74 istance and stat
ff000 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f us of a file..*/
ff010 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 41 .static int winA
ff020 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 ccess(. sqlite3
ff030 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
ff040 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 /* Not used
ff050 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f on win32 */. co
ff060 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
ff070 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ame, /* Name
ff080 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63 of file to chec
ff090 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 k */. int flags
ff0a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
ff0b0 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73 /* Type of tes
ff0c0 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 t to make on thi
ff0d0 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 s file */. int
ff0e0 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 *pResOut
ff0f0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 /* OUT: R
ff100 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 20 44 57 esult */.){. DW
ff110 4f 52 44 20 61 74 74 72 3b 0a 20 20 69 6e 74 20 ORD attr;. int
ff120 72 63 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a rc = 0;. void *
ff130 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e zConverted = con
ff140 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 vertUtf8Filename
ff150 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 (zFilename);. U
ff160 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
ff170 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f pVfs);. if( zCo
ff180 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 nverted==0 ){.
ff190 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ff1a0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 NOMEM;. }. if(
ff1b0 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 61 isNT() ){. a
ff1c0 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 ttr = GetFileAtt
ff1d0 72 69 62 75 74 65 73 57 28 28 57 43 48 41 52 2a ributesW((WCHAR*
ff1e0 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a )zConverted);./*
ff1f0 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 isNT() is 1 if
ff200 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
ff210 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 =1, so this else
ff220 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 is never execut
ff230 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 ed. .** Since th
ff240 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 e ASCII version
ff250 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 of these Windows
ff260 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 API do not exis
ff270 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 t for WINCE,.**
ff280 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 it's important t
ff290 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 o not reference
ff2a0 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 them for WINCE b
ff2b0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 uilds..*/.#if SQ
ff2c0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 LITE_OS_WINCE==0
ff2d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 74 . }else{. at
ff2e0 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 tr = GetFileAttr
ff2f0 69 62 75 74 65 73 41 28 28 63 68 61 72 2a 29 7a ibutesA((char*)z
ff300 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 Converted);.#end
ff310 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 if. }. free(zC
ff320 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 73 77 69 onverted);. swi
ff330 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 tch( flags ){.
ff340 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
ff350 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 CESS_READ:. c
ff360 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ase SQLITE_ACCES
ff370 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 S_EXISTS:.
ff380 72 63 20 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c rc = attr!=INVAL
ff390 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 ID_FILE_ATTRIBUT
ff3a0 45 53 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ES;. break;
ff3b0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
ff3c0 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 _ACCESS_READWRIT
ff3d0 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 61 E:. rc = (a
ff3e0 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52 49 ttr & FILE_ATTRI
ff3f0 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 3d 3d BUTE_READONLY)==
ff400 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0;. break;.
ff410 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
ff420 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 assert(!"Inva
ff430 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 lid flags argume
ff440 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 nt");. }. *pRe
ff450 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 sOut = rc;. ret
ff460 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
ff470 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 .../*.** Turn a
ff480 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d relative pathnam
ff490 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 e into a full pa
ff4a0 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 thname. Write t
ff4b0 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e he full.** pathn
ff4c0 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e ame into zOut[].
ff4d0 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65 zOut[] will be
ff4e0 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e at least pVfs->
ff4f0 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 mxPathname.** by
ff500 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a tes in size..*/.
ff510 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 75 static int winFu
ff520 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 llPathname(. sq
ff530 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
ff540 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
ff550 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 ointer to vfs ob
ff560 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ject */. const
ff570 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c char *zRelative,
ff580 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 /* Possi
ff590 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 bly relative inp
ff5a0 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 ut path */. int
ff5b0 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 nFull,
ff5c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
ff5d0 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
ff5e0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
ff5f0 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 char *zFull
ff600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff610 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 /* Output buffer
ff620 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 20 64 65 */.){. .#if de
ff630 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f fined(__CYGWIN__
ff640 29 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ). UNUSED_PARAM
ff650 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 63 ETER(nFull);. c
ff660 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75 ygwin_conv_to_fu
ff670 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52 ll_win32_path(zR
ff680 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b elative, zFull);
ff690 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
ff6a0 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 _OK;.#endif..#if
ff6b0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ff6c0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ff6d0 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 2f 2a TER(nFull);. /*
ff6e0 20 57 69 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f WinCE has no co
ff6f0 6e 63 65 70 74 20 6f 66 20 61 20 72 65 6c 61 74 ncept of a relat
ff700 69 76 65 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 ive pathname, or
ff710 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a so I am told. *
ff720 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 /. sqlite3_snpr
ff730 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 intf(pVfs->mxPat
ff740 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 hname, zFull, "%
ff750 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a s", zRelative);.
ff760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
ff770 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 OK;.#endif..#if
ff780 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 !SQLITE_OS_WINCE
ff790 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 && !defined(__C
ff7a0 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e YGWIN__). int n
ff7b0 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 Byte;. void *zC
ff7c0 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63 68 61 72 onverted;. char
ff7d0 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 53 45 44 *zOut;. UNUSED
ff7e0 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c _PARAMETER(nFull
ff7f0 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 );. zConverted
ff800 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
ff810 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29 ename(zRelative)
ff820 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 ;. if( isNT() )
ff830 7b 0a 20 20 20 20 57 43 48 41 52 20 2a 7a 54 65 {. WCHAR *zTe
ff840 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 mp;. nByte =
ff850 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57 GetFullPathNameW
ff860 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 ((WCHAR*)zConver
ff870 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 ted, 0, 0, 0) +
ff880 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 3;. zTemp = m
ff890 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a alloc( nByte*siz
ff8a0 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b eof(zTemp[0]) );
ff8b0 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d . if( zTemp==
ff8c0 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 0 ){. free(
ff8d0 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 zConverted);.
ff8e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
ff8f0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 _NOMEM;. }.
ff900 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d GetFullPathNam
ff910 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 eW((WCHAR*)zConv
ff920 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 erted, nByte, zT
ff930 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 emp, 0);. fre
ff940 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
ff950 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 6f 64 zOut = unicod
ff960 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a eToUtf8(zTemp);.
ff970 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b free(zTemp);
ff980 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 ./* isNT() is 1
ff990 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ff9a0 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 CE==1, so this e
ff9b0 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 lse is never exe
ff9c0 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 cuted. .** Since
ff9d0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 the ASCII versi
ff9e0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 on of these Wind
ff9f0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 ows API do not e
ffa00 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a xist for WINCE,.
ffa10 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e ** it's importan
ffa20 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e t to not referen
ffa30 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 ce them for WINC
ffa40 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 E builds..*/.#if
ffa50 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ffa60 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ==0. }else{.
ffa70 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a 20 20 char *zTemp;.
ffa80 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c nByte = GetFul
ffa90 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72 lPathNameA((char
ffaa0 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c *)zConverted, 0,
ffab0 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20 0, 0) + 3;.
ffac0 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 zTemp = malloc(
ffad0 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65 nByte*sizeof(zTe
ffae0 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 mp[0]) );. if
ffaf0 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 ( zTemp==0 ){.
ffb00 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
ffb10 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ted);. retu
ffb20 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
ffb30 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75 . }. GetFu
ffb40 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 llPathNameA((cha
ffb50 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e r*)zConverted, n
ffb60 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b Byte, zTemp, 0);
ffb70 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 . free(zConve
ffb80 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 rted);. zOut
ffb90 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f = sqlite3_win32_
ffba0 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65 mbcs_to_utf8(zTe
ffbb0 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 mp);. free(zT
ffbc0 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d emp);.#endif. }
ffbd0 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20 . if( zOut ){.
ffbe0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
ffbf0 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 ntf(pVfs->mxPath
ffc00 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 name, zFull, "%s
ffc10 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72 ", zOut);. fr
ffc20 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65 ee(zOut);. re
ffc30 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
ffc40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
ffc50 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
ffc60 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a ;. }.#endif.}..
ffc70 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 65 /*.** Get the se
ffc80 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65 ctor size of the
ffc90 20 64 65 76 69 63 65 20 75 73 65 64 20 74 6f 20 device used to
ffca0 73 74 6f 72 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a store.** file..*
ffcb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
ffcc0 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20 SectorSize(.
ffcd0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
ffce0 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 s,. const cha
ffcf0 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20 r *zRelative
ffd00 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e /* UTF-8 file n
ffd10 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52 ame */.){. DWOR
ffd20 44 20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 D bytesPerSector
ffd30 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c = SQLITE_DEFAUL
ffd40 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 T_SECTOR_SIZE;.
ffd50 20 2f 2a 20 47 65 74 44 69 73 6b 46 72 65 65 53 /* GetDiskFreeS
ffd60 70 61 63 65 20 69 73 20 6e 6f 74 20 73 75 70 70 pace is not supp
ffd70 6f 72 74 65 64 20 75 6e 64 65 72 20 57 49 4e 43 orted under WINC
ffd80 45 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f E */.#if SQLITE_
ffd90 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 OS_WINCE. UNUSE
ffda0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
ffdb0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
ffdc0 4d 45 54 45 52 28 7a 52 65 6c 61 74 69 76 65 29 METER(zRelative)
ffdd0 3b 0a 23 65 6c 73 65 0a 20 20 63 68 61 72 20 7a ;.#else. char z
ffde0 46 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54 Fullpath[MAX_PAT
ffdf0 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a H+1];. int rc;.
ffe00 20 20 44 57 4f 52 44 20 64 77 52 65 74 20 3d 20 DWORD dwRet =
ffe10 30 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 75 6d 0;. DWORD dwDum
ffe20 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57 my;.. /*. ** W
ffe30 65 20 6e 65 65 64 20 74 6f 20 67 65 74 20 74 68 e need to get th
ffe40 65 20 66 75 6c 6c 20 70 61 74 68 20 6e 61 6d 65 e full path name
ffe50 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 20 2a of the file. *
ffe60 2a 20 74 6f 20 67 65 74 20 74 68 65 20 64 72 69 * to get the dri
ffe70 76 65 20 6c 65 74 74 65 72 20 74 6f 20 6c 6f 6f ve letter to loo
ffe80 6b 20 75 70 20 74 68 65 20 73 65 63 74 6f 72 0a k up the sector.
ffe90 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a ** size.. */.
ffea0 20 20 72 63 20 3d 20 77 69 6e 46 75 6c 6c 50 61 rc = winFullPa
ffeb0 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 52 65 thname(pVfs, zRe
ffec0 6c 61 74 69 76 65 2c 20 4d 41 58 5f 50 41 54 48 lative, MAX_PATH
ffed0 2c 20 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20 , zFullpath);.
ffee0 69 66 28 20 72 63 20 3d 3d 20 53 51 4c 49 54 45 if( rc == SQLITE
ffef0 5f 4f 4b 20 29 0a 20 20 7b 0a 20 20 20 20 76 6f _OK ). {. vo
fff00 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d id *zConverted =
fff10 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 convertUtf8File
fff20 6e 61 6d 65 28 7a 46 75 6c 6c 70 61 74 68 29 3b name(zFullpath);
fff30 0a 20 20 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 . if( zConver
fff40 74 65 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ted ){. if(
fff50 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 20 isNT() ){.
fff60 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 /* trim path
fff70 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 to just drive re
fff80 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 ference */.
fff90 20 20 20 57 43 48 41 52 20 2a 70 20 3d 20 7a 43 WCHAR *p = zC
fffa0 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 20 20 onverted;.
fffb0 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a for(;*p;p++){.
fffc0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 if( *p
fffd0 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 20 20 == '\\' ){.
fffe0 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30 *p = '\0
ffff0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 ';. b
10000 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
10001 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
10002 20 20 20 20 64 77 52 65 74 20 3d 20 47 65 74 44 dwRet = GetD
10003 69 73 6b 46 72 65 65 53 70 61 63 65 57 28 28 57 iskFreeSpaceW((W
10004 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
10005 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
10006 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10007 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 &dwDummy,.
10008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10009 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1000a 26 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 2c &bytesPerSector,
1000b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1000c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1000d 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20 &dwDummy,.
1000e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1000f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
10010 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 dwDummy);.
10011 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f }else{. /
10012 2a 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a * trim path to j
10013 75 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65 ust drive refere
10014 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 nce */. C
10015 48 41 52 20 2a 70 20 3d 20 28 43 48 41 52 20 2a HAR *p = (CHAR *
10016 29 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 )zConverted;.
10017 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b for(;*p;p++
10018 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
10019 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 *p == '\\' ){.
1001a 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20 *p =
1001b 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '\0';.
1001c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1001d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1001e 20 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47 dwRet = G
1001f 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 41 etDiskFreeSpaceA
10020 28 28 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 ((CHAR*)zConvert
10021 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ed,.
10022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10023 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a &dwDummy,.
10024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10025 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10026 20 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f &bytesPerSecto
10027 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 r,.
10028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10029 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 &dwDummy,.
1002a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1002b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1002c 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 &dwDummy);.
1002d 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65 28 7a }. free(z
1002e 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
1002f 7d 0a 20 20 20 20 69 66 28 20 21 64 77 52 65 74 }. if( !dwRet
10030 20 29 7b 0a 20 20 20 20 20 20 62 79 74 65 73 50 ){. bytesP
10031 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 erSector = SQLIT
10032 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
10033 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d _SIZE;. }. }
10034 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
10035 20 28 69 6e 74 29 20 62 79 74 65 73 50 65 72 53 (int) bytesPerS
10036 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 6e 64 ector; .}..#ifnd
10037 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
10038 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a OAD_EXTENSION./*
10039 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 .** Interfaces f
1003a 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 or opening a sha
1003b 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e red library, fin
1003c 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 ding entry point
1003d 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 s.** within the
1003e 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 shared library,
1003f 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 and closing the
10040 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a shared library..
10041 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 */./*.** Interfa
10042 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 ces for opening
10043 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 a shared library
10044 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 , finding entry
10045 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e points.** within
10046 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
10047 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 ary, and closing
10048 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
10049 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ary..*/.static v
1004a 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73 oid *winDlOpen(s
1004b 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1004c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
1004d 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44 ilename){. HAND
1004e 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 LE h;. void *zC
1004f 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 onverted = conve
10050 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a rtUtf8Filename(z
10051 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55 Filename);. UNU
10052 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 SED_PARAMETER(pV
10053 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 fs);. if( zConv
10054 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 erted==0 ){.
10055 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
10056 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
10057 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 h = LoadLibrar
10058 79 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 yW((WCHAR*)zConv
10059 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 erted);./* isNT(
1005a 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
1005b 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
1005c 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
1005d 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
1005e 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 * Since the ASCI
1005f 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 I version of the
10060 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 se Windows API d
10061 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 o not exist for
10062 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 WINCE,.** it's i
10063 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 mportant to not
10064 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 reference them f
10065 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e or WINCE builds.
10066 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
10067 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c S_WINCE==0. }el
10068 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 se{. h = Load
10069 4c 69 62 72 61 72 79 41 28 28 63 68 61 72 2a 29 LibraryA((char*)
1006a 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e zConverted);.#en
1006b 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a dif. }. free(z
1006c 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65 Converted);. re
1006d 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d turn (void*)h;.}
1006e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e .static void win
1006f 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f DlError(sqlite3_
10070 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
10071 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f Buf, char *zBufO
10072 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ut){. UNUSED_PA
10073 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 RAMETER(pVfs);.
10074 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 getLastErrorMsg
10075 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 29 3b (nBuf, zBufOut);
10076 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53 .}.void (*winDlS
10077 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ym(sqlite3_vfs *
10078 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e pVfs, void *pHan
10079 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 dle, const char
1007a 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 *zSymbol))(void)
1007b 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
1007c 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 20 ETER(pVfs);.#if
1007d 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
1007e 20 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f 63 /* The GetProc
1007f 41 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 69 AddressA() routi
10080 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ne is only avail
10081 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 2a able on wince. *
10082 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 /. return (void
10083 28 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f (*)(void))GetPro
10084 63 41 64 64 72 65 73 73 41 28 28 48 41 4e 44 4c cAddressA((HANDL
10085 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62 E)pHandle, zSymb
10086 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 ol);.#else. /*
10087 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 6f 77 All other window
10088 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 70 65 s platforms expe
10089 63 74 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 ct GetProcAddres
1008a 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a s() to take. **
1008b 20 61 6e 20 41 6e 73 69 20 73 74 72 69 6e 67 20 an Ansi string
1008c 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 regardless of th
1008d 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 74 69 e _UNICODE setti
1008e 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 ng */. return (
1008f 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 void(*)(void))Ge
10090 74 50 72 6f 63 41 64 64 72 65 73 73 28 28 48 41 tProcAddress((HA
10091 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 NDLE)pHandle, zS
10092 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d ymbol);.#endif.}
10093 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65 .void winDlClose
10094 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
10095 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c fs, void *pHandl
10096 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 e){. UNUSED_PAR
10097 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
10098 46 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e FreeLibrary((HAN
10099 44 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a DLE)pHandle);.}.
1009a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 #else /* if SQLI
1009b 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
1009c 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 ENSION is define
1009d 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 d: */. #define
1009e 77 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 winDlOpen 0. #
1009f 64 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f define winDlErro
100a0 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 r 0. #define wi
100a1 6e 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 nDlSym 0. #de
100a2 66 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20 fine winDlClose
100a3 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
100a4 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 Write up to nBu
100a5 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f f bytes of rando
100a6 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e mness into zBuf.
100a7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
100a8 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c inRandomness(sql
100a9 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
100aa 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
100ab 7a 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d zBuf){. int n =
100ac 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 0;. UNUSED_PAR
100ad 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 AMETER(pVfs);.#i
100ae 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
100af 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 _TEST). n = nBu
100b0 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 f;. memset(zBuf
100b1 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 , 0, nBuf);.#els
100b2 65 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 53 e. if( sizeof(S
100b3 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66 YSTEMTIME)<=nBuf
100b4 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 4d -n ){. SYSTEM
100b5 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 53 TIME x;. GetS
100b6 79 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20 ystemTime(&x);.
100b7 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
100b8 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 n], &x, sizeof(x
100b9 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a ));. n += siz
100ba 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66 eof(x);. }. if
100bb 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c ( sizeof(DWORD)<
100bc 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 =nBuf-n ){. D
100bd 57 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 75 WORD pid = GetCu
100be 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29 rrentProcessId()
100bf 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 ;. memcpy(&zB
100c0 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a uf[n], &pid, siz
100c1 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 6e eof(pid));. n
100c2 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 3b += sizeof(pid);
100c3 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f . }. if( sizeo
100c4 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e f(DWORD)<=nBuf-n
100c5 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 6e ){. DWORD cn
100c6 74 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e 74 t = GetTickCount
100c7 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 ();. memcpy(&
100c8 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73 zBuf[n], &cnt, s
100c9 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20 izeof(cnt));.
100ca 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74 n += sizeof(cnt
100cb 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a );. }. if( siz
100cc 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45 eof(LARGE_INTEGE
100cd 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 R)<=nBuf-n ){.
100ce 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 20 LARGE_INTEGER
100cf 69 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 66 i;. QueryPerf
100d0 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26 ormanceCounter(&
100d1 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 i);. memcpy(&
100d2 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a zBuf[n], &i, siz
100d3 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b eof(i));. n +
100d4 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d = sizeof(i);. }
100d5 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
100d6 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c n;.}.../*.** Sl
100d7 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 eep for a little
100d8 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 while. Return
100d9 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 the amount of ti
100da 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61 me slept..*/.sta
100db 74 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70 tic int winSleep
100dc 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
100dd 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 fs, int microsec
100de 29 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72 ){. Sleep((micr
100df 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b osec+999)/1000);
100e0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
100e1 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 TER(pVfs);. ret
100e2 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 urn ((microsec+9
100e3 39 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 99)/1000)*1000;.
100e4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
100e5 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c lowing variable,
100e6 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e if set to a non
100e7 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 -zero value, bec
100e8 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a omes the result.
100e9 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d ** returned from
100ea 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
100eb 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 tTime(). This i
100ec 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
100ed 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ng..*/.#ifdef SQ
100ee 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
100ef 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
100f0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 _current_time =
100f1 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
100f2 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e Find the curren
100f3 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 t time (in Unive
100f4 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 rsal Coordinated
100f5 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 Time). Write t
100f6 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 he.** current ti
100f7 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 me and date as a
100f8 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 Julian Day numb
100f9 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 er into *prNow a
100fa 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 nd.** return 0.
100fb 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
100fc 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 time and date c
100fd 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a annot be found..
100fe 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e */.int winCurren
100ff 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 tTime(sqlite3_vf
10100 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 s *pVfs, double
10101 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 *prNow){. FILET
10102 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c IME ft;. /* FIL
10103 45 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 ETIME structure
10104 69 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 is a 64-bit valu
10105 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 e representing t
10106 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 he number of .
10107 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 100-nanosecon
10108 64 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 d intervals sinc
10109 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 e January 1, 160
1010a 31 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 1 (= JD 2305813.
1010b 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 5). . */. sqli
1010c 74 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 57 3b te3_int64 timeW;
1010d 20 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 79 73 /* Whole days
1010e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e */. sqlite3_in
1010f 74 36 34 20 74 69 6d 65 46 3b 20 20 20 2f 2a 20 t64 timeF; /*
10110 46 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 73 20 Fractional Days
10111 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 */.. /* Number
10112 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e of 100-nanosecon
10113 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 61 d intervals in a
10114 20 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f 0a 20 single day */.
10115 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
10116 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 lite3_int64 ntuP
10117 65 72 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 erDay = . 1
10118 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 0000000*(sqlite3
10119 5f 69 6e 74 36 34 29 38 36 34 30 30 3b 0a 0a 20 _int64)86400;..
1011a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30 /* Number of 10
1011b 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74 0-nanosecond int
1011c 65 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 20 6f ervals in half o
1011d 66 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 74 61 f a day */. sta
1011e 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
1011f 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 48 61 3_int64 ntuPerHa
10120 6c 66 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31 lfDay = . 1
10121 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33 0000000*(sqlite3
10122 5f 69 6e 74 36 34 29 34 33 32 30 30 3b 0a 0a 20 _int64)43200;..
10123 20 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76 /* 2^32 - to av
10124 6f 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e oid use of LL an
10125 64 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63 d warnings in gc
10126 63 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f c */. static co
10127 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 nst sqlite3_int6
10128 34 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20 4 max32BitValue
10129 3d 20 0a 20 20 20 20 20 20 28 73 71 6c 69 74 65 = . (sqlite
1012a 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 3_int64)20000000
1012b 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 00 + (sqlite3_in
1012c 74 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b t64)2000000000 +
1012d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
1012e 32 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 66 20 294967296;..#if
1012f 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
10130 20 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69 6d SYSTEMTIME tim
10131 65 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 e;. GetSystemTi
10132 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f 2a 20 me(&time);. /*
10133 69 66 20 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 if SystemTimeToF
10134 69 6c 65 54 69 6d 65 28 29 20 66 61 69 6c 73 2c ileTime() fails,
10135 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f it returns zero
10136 2e 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 73 74 . */. if (!Syst
10137 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65 emTimeToFileTime
10138 28 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a 20 20 (&time,&ft)){.
10139 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
1013a 23 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65 #else. GetSyste
1013b 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28 mTimeAsFileTime(
1013c 20 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20 &ft );.#endif.
1013d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1013e 52 28 70 56 66 73 29 3b 0a 20 20 74 69 6d 65 57 R(pVfs);. timeW
1013f 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e = (((sqlite3_in
10140 74 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 74 t64)ft.dwHighDat
10141 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 56 eTime)*max32BitV
10142 61 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 65 33 alue) + (sqlite3
10143 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 44 _int64)ft.dwLowD
10144 61 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d 65 46 ateTime;. timeF
10145 20 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 50 65 = timeW % ntuPe
10146 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f rDay; /
10147 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 * fractional day
10148 73 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e s (100-nanosecon
10149 64 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d ds) */. timeW =
1014a 20 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 72 44 timeW / ntuPerD
1014b 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ay; /*
1014c 77 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20 whole days */.
1014d 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 timeW = timeW +
1014e 32 33 30 35 38 31 33 3b 20 20 20 20 20 20 20 20 2305813;
1014f 20 20 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 /* add whole
10150 20 64 61 79 73 20 28 66 72 6f 6d 20 32 33 30 35 days (from 2305
10151 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 813.5) */. time
10152 46 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 75 50 F = timeF + ntuP
10153 65 72 48 61 6c 66 44 61 79 3b 20 20 20 20 20 20 erHalfDay;
10154 2f 2a 20 61 64 64 20 68 61 6c 66 20 61 20 64 61 /* add half a da
10155 79 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e y (from 2305813.
10156 35 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 5) */. timeW =
10157 74 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 2f 6e timeW + (timeF/n
10158 74 75 50 65 72 44 61 79 29 3b 20 20 2f 2a 20 61 tuPerDay); /* a
10159 64 64 20 77 68 6f 6c 65 20 64 61 79 20 69 66 20 dd whole day if
1015a 68 61 6c 66 20 64 61 79 20 6d 61 64 65 20 6f 6e half day made on
1015b 65 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74 e */. timeF = t
1015c 69 6d 65 46 20 25 20 6e 74 75 50 65 72 44 61 79 imeF % ntuPerDay
1015d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f ; /* co
1015e 6d 70 75 74 65 20 6e 65 77 20 66 72 61 63 74 69 mpute new fracti
1015f 6f 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 20 2a onal days */. *
10160 70 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29 prNow = (double)
10161 74 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 6c 65 timeW + ((double
10162 29 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 6c 65 )timeF / (double
10163 29 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 69 66 )ntuPerDay);.#if
10164 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
10165 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 if( sqlite3_cu
10166 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 rrent_time ){.
10167 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 6f 75 *prNow = ((dou
10168 62 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 72 72 ble)sqlite3_curr
10169 65 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f 75 62 ent_time + (doub
1016a 6c 65 29 34 33 32 30 30 29 20 2f 20 28 64 6f 75 le)43200) / (dou
1016b 62 6c 65 29 38 36 34 30 30 20 2b 20 28 64 6f 75 ble)86400 + (dou
1016c 62 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 20 7d ble)2440587;. }
1016d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
1016e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 0;.}../*.** The
1016f 20 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68 idea is that th
10170 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b is function work
10171 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61 s like a combina
10172 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61 tion of.** GetLa
10173 73 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f stError() and Fo
10174 72 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e rmatMessage() on
10175 20 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72 windows (or err
10176 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72 no and.** strerr
10177 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e or_r() on unix).
10178 20 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 After an error
10179 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 is returned by a
1017a 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e n OS.** function
1017b 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 , SQLite calls t
1017c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 his function wit
1017d 68 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20 h zBuf pointing
1017e 74 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f to.** a buffer o
1017f 66 20 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68 f nBuf bytes. Th
10180 65 20 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c e OS layer shoul
10181 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a d populate the.*
10182 2a 20 62 75 66 66 65 72 20 77 69 74 68 20 61 20 * buffer with a
10183 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 nul-terminated U
10184 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72 TF-8 encoded err
10185 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65 or message.** de
10186 73 63 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73 scribing the las
10187 74 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61 t IO error to ha
10188 76 65 20 6f 63 63 75 72 72 65 64 20 77 69 74 68 ve occurred with
10189 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a in the calling.*
1018a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 * thread..**.**
1018b 49 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 If the error mes
1018c 73 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 sage is too larg
1018d 65 20 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69 e for the suppli
1018e 65 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74 ed buffer,.** it
1018f 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 should be trunc
10190 61 74 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e ated. The return
10191 20 76 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61 value of xGetLa
10192 73 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65 stError.** is ze
10193 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 ro if the error
10194 6d 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 message fits in
10195 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e the buffer, or n
10196 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 on-zero.** other
10197 77 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 wise (if the mes
10198 73 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 sage was truncat
10199 65 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f ed). If non-zero
1019a 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a is returned,.**
1019b 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20 then it is not
1019c 6e 65 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63 necessary to inc
1019d 6c 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72 lude the nul-ter
1019e 6d 69 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65 minator characte
1019f 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70 r.** in the outp
101a0 75 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a ut buffer..**.**
101a1 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61 Not supplying a
101a2 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
101a3 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76 will have no adv
101a4 65 72 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f erse effect.** o
101a5 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20 n SQLite. It is
101a6 66 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20 fine to have an
101a7 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 implementation t
101a8 68 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74 hat never.** ret
101a9 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 urns an error me
101aa 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 ssage:.**.** i
101ab 6e 74 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 nt xGetLastError
101ac 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
101ad 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 fs, int nBuf, ch
101ae 61 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20 ar *zBuf){.**
101af 20 20 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d assert(zBuf[0]
101b0 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20 =='\0');.**
101b1 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d return 0;.** }
101b2 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69 .**.** However i
101b3 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 f an error messa
101b4 67 65 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 ge is supplied,
101b5 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72 it will be incor
101b6 70 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71 porated.** by sq
101b7 6c 69 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72 lite into the er
101b8 72 6f 72 20 6d 65 73 73 61 67 65 20 61 76 61 69 ror message avai
101b9 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 lable to the use
101ba 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 r using.** sqlit
101bb 65 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73 e3_errmsg(), pos
101bc 73 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20 sibly making IO
101bd 65 72 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f errors easier to
101be 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69 debug..*/.stati
101bf 63 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74 c int winGetLast
101c0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 Error(sqlite3_vf
101c1 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 s *pVfs, int nBu
101c2 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
101c3 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
101c4 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75 ER(pVfs);. retu
101c5 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d rn getLastErrorM
101c6 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a sg(nBuf, zBuf);.
101c7 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
101c8 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 ize and deinitia
101c9 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 lize the operati
101ca 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
101cb 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ace..*/.SQLITE_A
101cc 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
101cd 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 s_init(void){.
101ce 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 static sqlite3_v
101cf 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 fs winVfs = {.
101d0 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 1,
101d1 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e /* iVersion
101d2 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 */. sizeof(w
101d3 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a inFile), /* sz
101d4 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 OsFile */. MA
101d5 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 X_PATH,
101d6 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a /* mxPathname *
101d7 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 /. 0,
101d8 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 /* pNex
101d9 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 t */. "win32"
101da 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a , /* z
101db 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 Name */. 0,
101dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
101dd 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a * pAppData */. .
101de 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 winOpen,
101df 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 /* xOpen
101e0 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 */. winDelete
101e1 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 , /* xDe
101e2 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 lete */. winA
101e3 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f ccess, /
101e4 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 * xAccess */.
101e5 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 winFullPathname
101e6 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 , /* xFullPath
101e7 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 name */. winD
101e8 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f lOpen, /
101e9 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 * xDlOpen */.
101ea 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 winDlError,
101eb 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 /* xDlError
101ec 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c */. winDlSym,
101ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
101ee 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c Sym */. winDl
101ef 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Close, /*
101f0 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 xDlClose */.
101f1 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 winRandomness,
101f2 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 /* xRandomne
101f3 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 ss */. winSle
101f4 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ep, /*
101f5 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 xSleep */. wi
101f6 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 nCurrentTime,
101f7 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 /* xCurrentTime
101f8 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 */. winGetLa
101f9 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 stError /* xG
101fa 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 etLastError */.
101fb 20 7d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 };.. sqlite3_v
101fc 66 73 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e fs_register(&win
101fd 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 Vfs, 1);. retur
101fe 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
101ff 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
10200 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f qlite3_os_end(vo
10201 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 id){ . return S
10202 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e QLITE_OK;.}..#en
10203 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 dif /* SQLITE_OS
10204 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a _WIN */../******
10205 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
10206 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_win.c *******
10207 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10208 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10209 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
1020a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
1020b 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a ile bitvec.c ***
1020c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1020d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1020e 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
1020f 30 30 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 008 February 16.
10210 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
10211 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
10212 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
10213 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
10214 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
10215 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
10216 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
10217 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
10218 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
10219 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
1021a 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
1021b 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
1021c 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
1021d 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
1021e 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
1021f 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
10220 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
10221 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
10222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10223 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10225 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
10226 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c * This file impl
10227 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 ements an object
10228 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 that represents
10229 20 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a a fixed-length.
1022a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 ** bitmap. Bits
1022b 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 are numbered st
1022c 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a arting with 1..*
1022d 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 *.** A bitmap is
1022e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 used to record
1022f 77 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 which pages of a
10230 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 database file h
10231 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 ave been.** jour
10232 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 nalled during a
10233 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 transaction, or
10234 77 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65 which pages have
10235 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 the "dont-write
10236 22 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 ".** property.
10237 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 Usually only a f
10238 65 77 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 ew pages are mee
10239 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 t either conditi
1023a 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 on..** So the bi
1023b 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 tmap is usually
1023c 73 70 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c sparse and has l
1023d 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a ow cardinality..
1023e 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 ** But sometimes
1023f 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 (for example wh
10240 65 6e 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 en during a DROP
10241 20 6f 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c of a large tabl
10242 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c e) most.** or al
10243 6c 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 l of the pages i
10244 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e n a database can
10245 20 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e get journalled.
10246 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 In those cases
10247 2c 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 , .** the bitmap
10248 20 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 becomes dense w
10249 69 74 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 ith high cardina
1024a 6c 69 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 lity. The algor
1024b 69 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 ithm needs .** t
1024c 6f 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 o handle both ca
1024d 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 ses well..**.**
1024e 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 The size of the
1024f 62 69 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 bitmap is fixed
10250 77 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 when the object
10251 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a is created..**.*
10252 2a 20 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 * All bits are c
10253 6c 65 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 lear when the bi
10254 74 6d 61 70 20 69 73 20 63 72 65 61 74 65 64 2e tmap is created.
10255 20 20 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 Individual bit
10256 73 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 s.** may be set
10257 6f 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 or cleared one a
10258 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 t a time..**.**
10259 54 65 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 Test operations
1025a 61 72 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 are about 100 ti
1025b 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 mes more common
1025c 74 68 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 that set operati
1025d 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 ons..** Clear op
1025e 65 72 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 erations are exc
1025f 65 65 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 eedingly rare.
10260 54 68 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c There are usuall
10261 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 y between.** 5 a
10262 6e 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 nd 500 set opera
10263 74 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 tions per Bitvec
10264 20 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 object, though
10265 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 the number of se
10266 74 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 ts can.** someti
10267 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 mes grow into te
10268 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 ns of thousands
10269 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 or larger. The
1026a 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 size of the.** B
1026b 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 itvec object is
1026c 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
1026d 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
1026e 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a ase file at the.
1026f 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 ** start of a tr
10270 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 ansaction, and i
10271 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c s thus usually l
10272 65 73 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 ess than a few t
10273 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 housand,.** but
10274 63 61 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 can be as large
10275 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 as 2 billion for
10276 20 61 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 a really big da
10277 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 tabase..**.** @(
10278 23 29 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63 #) $Id: bitvec.c
10279 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f ,v 1.17 2009/07/
1027a 32 35 20 31 37 3a 33 33 3a 32 36 20 64 72 68 20 25 17:33:26 drh
1027b 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a Exp $.*/../* Siz
1027c 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 e of the Bitvec
1027d 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 74 structure in byt
1027e 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 es. */.#define B
1027f 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20 ITVEC_SZ
10280 28 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a 31 (sizeof(void*)*1
10281 32 38 29 20 20 2f 2a 20 35 31 32 20 6f 6e 20 33 28) /* 512 on 3
10282 32 62 69 74 2e 20 20 31 30 32 34 20 6f 6e 20 36 2bit. 1024 on 6
10283 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e 4bit */../* Roun
10284 64 20 74 68 65 20 75 6e 69 6f 6e 20 73 69 7a 65 d the union size
10285 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61 down to the nea
10286 72 65 73 74 20 70 6f 69 6e 74 65 72 20 62 6f 75 rest pointer bou
10287 6e 64 61 72 79 2c 20 73 69 6e 63 65 20 74 68 61 ndary, since tha
10288 74 27 73 20 68 6f 77 20 0a 2a 2a 20 69 74 20 77 t's how .** it w
10289 69 6c 6c 20 62 65 20 61 6c 69 67 6e 65 64 20 77 ill be aligned w
1028a 69 74 68 69 6e 20 74 68 65 20 42 69 74 76 65 63 ithin the Bitvec
1028b 20 73 74 72 75 63 74 2e 20 2a 2f 0a 23 64 65 66 struct. */.#def
1028c 69 6e 65 20 42 49 54 56 45 43 5f 55 53 49 5a 45 ine BITVEC_USIZE
1028d 20 20 20 20 20 28 28 28 42 49 54 56 45 43 5f 53 (((BITVEC_S
1028e 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 75 33 32 29 Z-(3*sizeof(u32)
1028f 29 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 ))/sizeof(Bitvec
10290 2a 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 65 *))*sizeof(Bitve
10291 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 65 20 6f 66 c*))../* Type of
10292 20 74 68 65 20 61 72 72 61 79 20 22 65 6c 65 6d the array "elem
10293 65 6e 74 22 20 66 6f 72 20 74 68 65 20 62 69 74 ent" for the bit
10294 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 69 map representati
10295 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 20 62 on. .** Should b
10296 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2c 20 e a power of 2,
10297 61 6e 64 20 69 64 65 61 6c 6c 79 2c 20 65 76 65 and ideally, eve
10298 6e 6c 79 20 64 69 76 69 64 65 20 69 6e 74 6f 20 nly divide into
10299 42 49 54 56 45 43 5f 55 53 49 5a 45 2e 20 0a 2a BITVEC_USIZE. .*
1029a 2a 20 53 65 74 74 69 6e 67 20 74 68 69 73 20 74 * Setting this t
1029b 6f 20 74 68 65 20 22 6e 61 74 75 72 61 6c 20 77 o the "natural w
1029c 6f 72 64 22 20 73 69 7a 65 20 6f 66 20 79 6f 75 ord" size of you
1029d 72 20 43 50 55 20 6d 61 79 20 69 6d 70 72 6f 76 r CPU may improv
1029e 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 e.** performance
1029f 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 . */.#define BIT
102a0 56 45 43 5f 54 45 4c 45 4d 20 20 20 20 20 75 38 VEC_TELEM u8
102a1 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69 74 ./* Size, in bit
102a2 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70 s, of the bitmap
102a3 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 64 65 element. */.#de
102a4 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 45 4c fine BITVEC_SZEL
102a5 45 4d 20 20 20 20 38 0a 2f 2a 20 4e 75 6d 62 65 EM 8./* Numbe
102a6 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e r of elements in
102a7 20 61 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e a bitmap array.
102a8 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 */.#define BITV
102a9 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20 28 42 49 EC_NELEM (BI
102aa 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f TVEC_USIZE/sizeo
102ab 66 28 42 49 54 56 45 43 5f 54 45 4c 45 4d 29 29 f(BITVEC_TELEM))
102ac 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 ./* Number of bi
102ad 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 70 ts in the bitmap
102ae 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 array. */.#defi
102af 6e 65 20 42 49 54 56 45 43 5f 4e 42 49 54 20 20 ne BITVEC_NBIT
102b0 20 20 20 20 28 42 49 54 56 45 43 5f 4e 45 4c 45 (BITVEC_NELE
102b1 4d 2a 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 29 M*BITVEC_SZELEM)
102b2 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 ../* Number of u
102b3 33 32 20 76 61 6c 75 65 73 20 69 6e 20 68 61 73 32 values in has
102b4 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 65 66 h table. */.#def
102b5 69 6e 65 20 42 49 54 56 45 43 5f 4e 49 4e 54 20 ine BITVEC_NINT
102b6 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 (BITVEC_USI
102b7 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 29 0a ZE/sizeof(u32)).
102b8 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 /* Maximum numbe
102b9 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
102ba 68 61 73 68 20 74 61 62 6c 65 20 62 65 66 6f 72 hash table befor
102bb 65 20 0a 2a 2a 20 73 75 62 2d 64 69 76 69 64 69 e .** sub-dividi
102bc 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e ng and re-hashin
102bd 67 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 g. */.#define BI
102be 54 56 45 43 5f 4d 58 48 41 53 48 20 20 20 20 28 TVEC_MXHASH (
102bf 42 49 54 56 45 43 5f 4e 49 4e 54 2f 32 29 0a 2f BITVEC_NINT/2)./
102c0 2a 20 48 61 73 68 69 6e 67 20 66 75 6e 63 74 69 * Hashing functi
102c1 6f 6e 20 66 6f 72 20 74 68 65 20 61 48 61 73 68 on for the aHash
102c2 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e representation.
102c3 0a 2a 2a 20 45 6d 70 69 72 69 63 61 6c 20 74 65 .** Empirical te
102c4 73 74 69 6e 67 20 73 68 6f 77 65 64 20 74 68 61 sting showed tha
102c5 74 20 74 68 65 20 2a 33 37 20 6d 75 6c 74 69 70 t the *37 multip
102c6 6c 69 65 72 20 0a 2a 2a 20 28 61 6e 20 61 72 62 lier .** (an arb
102c7 69 74 72 61 72 79 20 70 72 69 6d 65 29 69 6e 20 itrary prime)in
102c8 74 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f the hash functio
102c9 6e 20 70 72 6f 76 69 64 65 64 20 0a 2a 2a 20 6e n provided .** n
102ca 6f 20 66 65 77 65 72 20 63 6f 6c 6c 69 73 69 6f o fewer collisio
102cb 6e 73 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d 6f ns than the no-o
102cc 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 p *1. */.#define
102cd 20 42 49 54 56 45 43 5f 48 41 53 48 28 58 29 20 BITVEC_HASH(X)
102ce 20 20 28 28 28 58 29 2a 31 29 25 42 49 54 56 45 (((X)*1)%BITVE
102cf 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 66 69 6e 65 C_NINT)..#define
102d0 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20 20 20 BITVEC_NPTR
102d1 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f (BITVEC_USIZE/
102d2 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20 2a 29 sizeof(Bitvec *)
102d3 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d ).../*.** A bitm
102d4 61 70 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ap is an instanc
102d5 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
102d6 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
102d7 0a 2a 2a 20 54 68 69 73 20 62 69 74 6d 61 70 20 .** This bitmap
102d8 72 65 63 6f 72 64 73 20 74 68 65 20 65 78 69 73 records the exis
102d9 74 61 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 tance of zero or
102da 20 6d 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 69 more bits.** wi
102db 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 th values betwee
102dc 6e 20 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 69 n 1 and iSize, i
102dd 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 nclusive..**.**
102de 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 There are three
102df 70 6f 73 73 69 62 6c 65 20 72 65 70 72 65 73 65 possible represe
102e0 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
102e1 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 53 bitmap..** If iS
102e2 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 ize<=BITVEC_NBIT
102e3 2c 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e , then Bitvec.u.
102e4 61 42 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 73 aBitmap[] is a s
102e5 74 72 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d 61 traight.** bitma
102e6 70 2e 20 20 54 68 65 20 6c 65 61 73 74 20 73 69 p. The least si
102e7 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 69 73 gnificant bit is
102e8 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 bit 1..**.** If
102e9 20 69 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42 iSize>BITVEC_NB
102ea 49 54 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d IT and iDivisor=
102eb 3d 30 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 =0 then Bitvec.u
102ec 2e 61 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 61 .aHash[] is.** a
102ed 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 hash table that
102ee 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f will hold up to
102ef 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64 BITVEC_MXHASH d
102f0 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0a istinct values..
102f1 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
102f2 20 74 68 65 20 76 61 6c 75 65 20 69 20 69 73 20 the value i is
102f3 72 65 64 69 72 65 63 74 65 64 20 69 6e 74 6f 20 redirected into
102f4 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f 4e 50 one of BITVEC_NP
102f5 54 52 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 70 TR.** sub-bitmap
102f6 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 s pointed to by
102f7 42 69 74 76 65 63 2e 75 2e 61 70 53 75 62 5b 5d Bitvec.u.apSub[]
102f8 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d 61 . Each subbitma
102f9 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 20 p.** handles up
102fa 74 6f 20 69 44 69 76 69 73 6f 72 20 73 65 70 61 to iDivisor sepa
102fb 72 61 74 65 20 76 61 6c 75 65 73 20 6f 66 20 69 rate values of i
102fc 2e 20 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c 64 . apSub[0] hold
102fd 73 0a 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 77 s.** values betw
102fe 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73 een 1 and iDivis
102ff 6f 72 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 6f or. apSub[1] ho
10300 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 lds values betwe
10301 65 6e 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b 31 en.** iDivisor+1
10302 20 61 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 2e and 2*iDivisor.
10303 20 20 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 73 apSub[N] holds
10304 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a values between.
10305 2a 2a 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 20 ** N*iDivisor+1
10306 61 6e 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 73 and (N+1)*iDivis
10307 6f 72 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 or. Each subbit
10308 6d 61 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a 65 map is normalize
10309 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 61 d.** to hold dea
1030a 6c 20 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 l with values be
1030b 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 tween 1 and iDiv
1030c 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 isor..*/.struct
1030d 42 69 74 76 65 63 20 7b 0a 20 20 75 33 32 20 69 Bitvec {. u32 i
1030e 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d 61 Size; /* Ma
1030f 78 69 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 2e ximum bit index.
10310 20 20 4d 61 78 20 69 53 69 7a 65 20 69 73 20 34 Max iSize is 4
10311 2c 32 39 34 2c 39 36 37 2c 32 39 36 2e 20 2a 2f ,294,967,296. */
10312 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 20 20 . u32 nSet;
10313 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
10314 62 69 74 73 20 74 68 61 74 20 61 72 65 20 73 65 bits that are se
10315 74 20 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66 t - only valid f
10316 6f 72 20 61 48 61 73 68 0a 20 20 20 20 20 20 20 or aHash.
10317 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 6c ** el
10318 65 6d 65 6e 74 2e 20 20 4d 61 78 20 69 73 20 42 ement. Max is B
10319 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72 ITVEC_NINT. For
1031a 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 BITVEC_SZ of 51
1031b 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2,.
1031c 20 20 20 20 20 2a 2a 20 74 68 69 73 20 77 6f 75 ** this wou
1031d 6c 64 20 62 65 20 31 32 35 2e 20 2a 2f 0a 20 20 ld be 125. */.
1031e 75 33 32 20 69 44 69 76 69 73 6f 72 3b 20 20 20 u32 iDivisor;
1031f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 /* Number of bit
10320 73 20 68 61 6e 64 6c 65 64 20 62 79 20 65 61 63 s handled by eac
10321 68 20 61 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e h apSub[] entry.
10322 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
10323 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20 /* Should
10324 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20 65 6c >=0 for apSub el
10325 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 ement. */.
10326 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
10327 61 78 20 69 44 69 76 69 73 6f 72 20 69 73 20 6d ax iDivisor is m
10328 61 78 28 75 33 32 29 20 2f 20 42 49 54 56 45 43 ax(u32) / BITVEC
10329 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0a 20 _NPTR + 1. */.
1032a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1032b 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56 45 43 /* For a BITVEC
1032c 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68 69 73 _SZ of 512, this
1032d 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33 35 39 would be 34,359
1032e 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e ,739. */. union
1032f 20 7b 0a 20 20 20 20 42 49 54 56 45 43 5f 54 45 {. BITVEC_TE
10330 4c 45 4d 20 61 42 69 74 6d 61 70 5b 42 49 54 56 LEM aBitmap[BITV
10331 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a EC_NELEM]; /*
10332 20 42 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e Bitmap represen
10333 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 33 tation */. u3
10334 32 20 61 48 61 73 68 5b 42 49 54 56 45 43 5f 4e 2 aHash[BITVEC_N
10335 49 4e 54 5d 3b 20 20 20 20 20 20 2f 2a 20 48 61 INT]; /* Ha
10336 73 68 20 74 61 62 6c 65 20 72 65 70 72 65 73 65 sh table represe
10337 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 42 ntation */. B
10338 69 74 76 65 63 20 2a 61 70 53 75 62 5b 42 49 54 itvec *apSub[BIT
10339 56 45 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 52 VEC_NPTR]; /* R
1033a 65 63 75 72 73 69 76 65 20 72 65 70 72 65 73 65 ecursive represe
1033b 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75 ntation */. } u
1033c 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 ;.};../*.** Crea
1033d 74 65 20 61 20 6e 65 77 20 62 69 74 6d 61 70 20 te a new bitmap
1033e 6f 62 6a 65 63 74 20 61 62 6c 65 20 74 6f 20 68 object able to h
1033f 61 6e 64 6c 65 20 62 69 74 73 20 62 65 74 77 65 andle bits betwe
10340 65 6e 20 30 20 61 6e 64 20 69 53 69 7a 65 2c 0a en 0 and iSize,.
10341 2a 2a 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 52 ** inclusive. R
10342 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
10343 74 6f 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 to the new objec
10344 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 t. Return NULL
10345 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 if .** malloc fa
10346 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ils..*/.SQLITE_P
10347 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 RIVATE Bitvec *s
10348 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
10349 74 65 28 75 33 32 20 69 53 69 7a 65 29 7b 0a 20 te(u32 iSize){.
1034a 20 42 69 74 76 65 63 20 2a 70 3b 0a 20 20 61 73 Bitvec *p;. as
1034b 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a 70 29 sert( sizeof(*p)
1034c 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b 0a 20 ==BITVEC_SZ );.
1034d 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c p = sqlite3Mall
1034e 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a ocZero( sizeof(*
1034f 70 29 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b p) );. if( p ){
10350 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 . p->iSize =
10351 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 iSize;. }. ret
10352 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
10353 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
10354 74 68 65 20 69 2d 74 68 20 62 69 74 20 69 73 20 the i-th bit is
10355 73 65 74 2e 20 20 52 65 74 75 72 6e 20 74 72 75 set. Return tru
10356 65 20 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 e or false..** I
10357 66 20 70 20 69 73 20 4e 55 4c 4c 20 28 69 66 20 f p is NULL (if
10358 74 68 65 20 62 69 74 6d 61 70 20 68 61 73 20 6e the bitmap has n
10359 6f 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 29 ot been created)
1035a 20 6f 72 20 69 66 0a 2a 2a 20 69 20 69 73 20 6f or if.** i is o
1035b 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 ut of range, the
1035c 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a n return false..
1035d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1035e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 E int sqlite3Bit
1035f 76 65 63 54 65 73 74 28 42 69 74 76 65 63 20 2a vecTest(Bitvec *
10360 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 69 66 28 p, u32 i){. if(
10361 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
10362 3b 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69 53 69 ;. if( i>p->iSi
10363 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74 ze || i==0 ) ret
10364 75 72 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 urn 0;. i--;.
10365 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 while( p->iDivis
10366 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 or ){. u32 bi
10367 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f n = i/p->iDiviso
10368 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e r;. i = i%p->
10369 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 iDivisor;. p
1036a 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e = p->u.apSub[bin
1036b 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b ];. if (!p) {
1036c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
1036d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
1036e 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 p->iSize<=BITVE
1036f 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 72 65 C_NBIT ){. re
10370 74 75 72 6e 20 28 70 2d 3e 75 2e 61 42 69 74 6d turn (p->u.aBitm
10371 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c ap[i/BITVEC_SZEL
10372 45 4d 5d 20 26 20 28 31 3c 3c 28 69 26 28 42 49 EM] & (1<<(i&(BI
10373 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 TVEC_SZELEM-1)))
10374 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a )!=0;. } else{.
10375 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 54 56 u32 h = BITV
10376 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 EC_HASH(i++);.
10377 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 while( p->u.aH
10378 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 ash[h] ){.
10379 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 if( p->u.aHash[h
1037a 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 31 3b ]==i ) return 1;
1037b 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b 31 29 . h = (h+1)
1037c 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54 3b 0a % BITVEC_NINT;.
1037d 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
1037e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0;. }.}../*.**
1037f 20 53 65 74 20 74 68 65 20 69 2d 74 68 20 62 69 Set the i-th bi
10380 74 2e 20 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 t. Return 0 on
10381 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65 success and an e
10382 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 rror code if.**
10383 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 anything goes wr
10384 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ong..**.** This
10385 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 63 61 routine might ca
10386 75 73 65 20 73 75 62 2d 62 69 74 6d 61 70 73 20 use sub-bitmaps
10387 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e to be allocated.
10388 20 20 46 61 69 6c 69 6e 67 0a 2a 2a 20 74 6f 20 Failing.** to
10389 67 65 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 6e get the memory n
1038a 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 eeded to hold th
1038b 65 20 73 75 62 2d 62 69 74 6d 61 70 20 69 73 20 e sub-bitmap is
1038c 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 74 68 61 74 the only.** that
1038d 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 20 77 69 can go wrong wi
1038e 74 68 20 61 6e 20 69 6e 73 65 72 74 2c 20 61 73 th an insert, as
1038f 73 75 6d 69 6e 67 20 70 20 61 6e 64 20 69 20 61 suming p and i a
10390 72 65 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 re valid..**.**
10391 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 The calling func
10392 74 69 6f 6e 20 6d 75 73 74 20 65 6e 73 75 72 65 tion must ensure
10393 20 74 68 61 74 20 70 20 69 73 20 61 20 76 61 6c that p is a val
10394 69 64 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 id Bitvec object
10395 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 .** and that the
10396 20 76 61 6c 75 65 20 66 6f 72 20 22 69 22 20 69 value for "i" i
10397 73 20 77 69 74 68 69 6e 20 72 61 6e 67 65 20 6f s within range o
10398 66 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a f the Bitvec obj
10399 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ect..** Otherwis
1039a 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 e the behavior i
1039b 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
1039c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1039d 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 nt sqlite3Bitvec
1039e 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75 Set(Bitvec *p, u
1039f 33 32 20 69 29 7b 0a 20 20 75 33 32 20 68 3b 0a 32 i){. u32 h;.
103a0 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
103a1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
103a2 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a assert( i>0 );.
103a3 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e assert( i<=p->
103a4 69 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a iSize );. i--;.
103a5 20 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a while((p->iSiz
103a6 65 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 29 e > BITVEC_NBIT)
103a7 20 26 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 29 && p->iDivisor)
103a8 20 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d {. u32 bin =
103a9 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a i/p->iDivisor;.
103aa 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 i = i%p->iDi
103ab 76 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 visor;. if( p
103ac 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d ->u.apSub[bin]==
103ad 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 2e 0 ){. p->u.
103ae 61 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c apSub[bin] = sql
103af 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
103b0 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b ( p->iDivisor );
103b1 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e . if( p->u.
103b2 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 apSub[bin]==0 )
103b3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
103b4 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 MEM;. }. p
103b5 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 = p->u.apSub[bi
103b6 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d n];. }. if( p-
103b7 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e >iSize<=BITVEC_N
103b8 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e BIT ){. p->u.
103b9 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 aBitmap[i/BITVEC
103ba 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c _SZELEM] |= 1 <<
103bb 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c (i&(BITVEC_SZEL
103bc 45 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75 EM-1));. retu
103bd 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
103be 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48 }. h = BITVEC_H
103bf 41 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69 ASH(i++);. /* i
103c0 66 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 61 f there wasn't a
103c1 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c hash collision,
103c2 20 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27 and this doesn'
103c3 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65 t */. /* comple
103c4 74 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61 tely fill the ha
103c5 73 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64 sh, then just ad
103c6 64 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a d it without */.
103c7 20 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f /* worring abo
103c8 75 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 ut sub-dividing
103c9 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 and re-hashing.
103ca 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61 */. if( !p->u.a
103cb 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 Hash[h] ){. i
103cc 66 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56 f (p->nSet<(BITV
103cd 45 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20 EC_NINT-1)) {.
103ce 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f goto bitvec_
103cf 73 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65 set_end;. } e
103d0 6c 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f lse {. goto
103d1 20 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 bitvec_set_reha
103d2 73 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 sh;. }. }.
103d3 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63 /* there was a c
103d4 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20 ollision, check
103d5 74 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61 to see if it's a
103d6 6c 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69 lready */. /* i
103d7 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20 n hash, if not,
103d8 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70 try to find a sp
103d9 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64 ot for it */. d
103da 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 o {. if( p->u
103db 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 .aHash[h]==i ) r
103dc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
103dd 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 . h++;. if
103de 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 ( h>=BITVEC_NINT
103df 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68 ) h = 0;. } wh
103e0 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b ile( p->u.aHash[
103e1 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69 h] );. /* we di
103e2 64 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20 dn't find it in
103e3 74 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69 the hash. h poi
103e4 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 nts to the first
103e5 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62 */. /* availab
103e6 6c 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68 le free spot. ch
103e7 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 eck to see if th
103e8 69 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a is is going to *
103e9 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20 /. /* make our
103ea 68 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e hash too "full".
103eb 20 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f */.bitvec_set_
103ec 72 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d rehash:. if( p-
103ed 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58 >nSet>=BITVEC_MX
103ee 48 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69 HASH ){. unsi
103ef 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 gned int j;.
103f0 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20 int rc;. u32
103f1 2a 61 69 56 61 6c 75 65 73 20 3d 20 73 71 6c 69 *aiValues = sqli
103f2 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 te3StackAllocRaw
103f3 28 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e (0, sizeof(p->u.
103f4 61 48 61 73 68 29 29 3b 0a 20 20 20 20 69 66 28 aHash));. if(
103f5 20 61 69 56 61 6c 75 65 73 3d 3d 30 20 29 7b 0a aiValues==0 ){.
103f6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
103f7 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
103f8 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 else{. memc
103f9 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e py(aiValues, p->
103fa 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 u.aHash, sizeof(
103fb 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 p->u.aHash));.
103fc 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e memset(p->u.
103fd 61 70 53 75 62 2c 20 30 2c 20 73 69 7a 65 6f 66 apSub, 0, sizeof
103fe 28 70 2d 3e 75 2e 61 70 53 75 62 29 29 3b 0a 20 (p->u.apSub));.
103ff 20 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f 72 p->iDivisor
10400 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20 42 = (p->iSize + B
10401 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29 2f ITVEC_NPTR - 1)/
10402 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20 20 BITVEC_NPTR;.
10403 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
10404 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a itvecSet(p, i);.
10405 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
10406 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b <BITVEC_NINT; j+
10407 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +){. if(
10408 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63 aiValues[j] ) rc
10409 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 |= sqlite3Bitve
1040a 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73 cSet(p, aiValues
1040b 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 [j]);. }.
1040c 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b sqlite3Stack
1040d 46 72 65 65 28 30 2c 20 61 69 56 61 6c 75 65 73 Free(0, aiValues
1040e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1040f 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 62 69 rc;. }. }.bi
10410 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 20 tvec_set_end:.
10411 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d 3e p->nSet++;. p->
10412 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a u.aHash[h] = i;.
10413 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10414 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 OK;.}../*.** Cle
10415 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e ar the i-th bit.
10416 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 6d 75 73 74 .**.** pBuf must
10417 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f be a pointer to
10418 20 61 74 20 6c 65 61 73 74 20 42 49 54 56 45 43 at least BITVEC
10419 5f 53 5a 20 62 79 74 65 73 20 6f 66 20 74 65 6d _SZ bytes of tem
1041a 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 0a 2a porary storage.*
1041b 2a 20 74 68 61 74 20 42 69 74 76 65 63 43 6c 65 * that BitvecCle
1041c 61 72 20 63 61 6e 20 75 73 65 20 74 6f 20 72 65 ar can use to re
1041d 62 75 69 6c 74 20 69 74 73 20 68 61 73 68 20 74 built its hash t
1041e 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f able..*/.SQLITE_
1041f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
10420 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 ite3BitvecClear(
10421 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 Bitvec *p, u32 i
10422 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 , void *pBuf){.
10423 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
10424 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e rn;. assert( i>
10425 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 0 );. i--;. wh
10426 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 ile( p->iDivisor
10427 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 ){. u32 bin
10428 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b = i/p->iDivisor;
10429 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 . i = i%p->iD
1042a 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 ivisor;. p =
1042b 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b p->u.apSub[bin];
1042c 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20 . if (!p) {.
1042d 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
1042e 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e }. }. if( p->
1042f 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 iSize<=BITVEC_NB
10430 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 IT ){. p->u.a
10431 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f Bitmap[i/BITVEC_
10432 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 3c SZELEM] &= ~(1 <
10433 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 < (i&(BITVEC_SZE
10434 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 7d 65 6c 73 LEM-1)));. }els
10435 65 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 e{. unsigned
10436 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32 20 2a int j;. u32 *
10437 61 69 56 61 6c 75 65 73 20 3d 20 70 42 75 66 3b aiValues = pBuf;
10438 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 . memcpy(aiVa
10439 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 lues, p->u.aHash
1043a 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 , sizeof(p->u.aH
1043b 61 73 68 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 ash));. memse
1043c 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c t(p->u.aHash, 0,
1043d 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 sizeof(p->u.aHa
1043e 73 68 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65 sh));. p->nSe
1043f 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a t = 0;. for(j
10440 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e =0; j<BITVEC_NIN
10441 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 T; j++){. i
10442 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 f( aiValues[j] &
10443 26 20 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 & aiValues[j]!=(
10444 69 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 i+1) ){.
10445 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 u32 h = BITVEC_H
10446 41 53 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d ASH(aiValues[j]-
10447 31 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 1);. p->n
10448 53 65 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 Set++;. w
10449 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 hile( p->u.aHash
1044a 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 [h] ){.
1044b 20 68 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 h++;.
1044c 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 if( h>=BITVEC_NI
1044d 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 NT ) h = 0;.
1044e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d }. p-
1044f 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 69 >u.aHash[h] = ai
10450 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20 Values[j];.
10451 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a }. }. }.}..
10452 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 /*.** Destroy a
10453 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 bitmap object.
10454 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f Reclaim all memo
10455 72 79 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ry used..*/.SQLI
10456 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
10457 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
10458 74 72 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b troy(Bitvec *p){
10459 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
1045a 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 turn;. if( p->i
1045b 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 Divisor ){. u
1045c 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 nsigned int i;.
1045d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 for(i=0; i<BI
1045e 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b TVEC_NPTR; i++){
1045f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 . sqlite3Bi
10460 74 76 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 tvecDestroy(p->u
10461 2e 61 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 .apSub[i]);.
10462 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f }. }. sqlite3_
10463 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a free(p);.}../*.*
10464 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c * Return the val
10465 75 65 20 6f 66 20 74 68 65 20 69 53 69 7a 65 20 ue of the iSize
10466 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 69 66 parameter specif
10467 69 65 64 20 77 68 65 6e 20 42 69 74 76 65 63 20 ied when Bitvec
10468 2a 70 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 *p.** was create
10469 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1046a 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
1046b 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 BitvecSize(Bitve
1046c 63 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 c *p){. return
1046d 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 p->iSize;.}..#if
1046e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1046f 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a _BUILTIN_TEST./*
10470 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 .** Let V[] be a
10471 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73 69 67 n array of unsig
10472 6e 65 64 20 63 68 61 72 61 63 74 65 72 73 20 73 ned characters s
10473 75 66 66 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c ufficient to hol
10474 64 0a 2a 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 d.** up to N bit
10475 73 2e 20 20 4c 65 74 20 49 20 62 65 20 61 6e 20 s. Let I be an
10476 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 integer between
10477 30 20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 0 and N. 0<=I<N
10478 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 66 6f ..** Then the fo
10479 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 llowing macros c
1047a 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 an be used to se
1047b 74 2c 20 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 t, clear, or tes
1047c 74 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 t.** individual
1047d 62 69 74 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a bits within V..*
1047e 2f 0a 23 64 65 66 69 6e 65 20 53 45 54 42 49 54 /.#define SETBIT
1047f 28 56 2c 49 29 20 20 20 20 20 20 56 5b 49 3e 3e (V,I) V[I>>
10480 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 3] |= (1<<(I&7))
10481 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52 42 49 .#define CLEARBI
10482 54 28 56 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 T(V,I) V[I>>3
10483 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 ] &= ~(1<<(I&7))
10484 0a 23 64 65 66 69 6e 65 20 54 45 53 54 42 49 54 .#define TESTBIT
10485 28 56 2c 49 29 20 20 20 20 20 28 56 5b 49 3e 3e (V,I) (V[I>>
10486 33 5d 26 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 3]&(1<<(I&7)))!=
10487 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0../*.** This ro
10488 75 74 69 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 utine runs an ex
10489 74 65 6e 73 69 76 65 20 74 65 73 74 20 6f 66 20 tensive test of
1048a 74 68 65 20 42 69 74 76 65 63 20 63 6f 64 65 2e the Bitvec code.
1048b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 .**.** The input
1048c 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 is an array of
1048d 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 61 63 integers that ac
1048e 74 73 20 61 73 20 61 20 70 72 6f 67 72 61 6d 0a ts as a program.
1048f 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 42 ** to test the B
10490 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e 74 65 itvec. The inte
10491 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64 65 73 gers are opcodes
10492 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 followed.** by
10493 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 0, 1, or 3 opera
10494 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f nds, depending o
10495 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 n the opcode. A
10496 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 nother.** opcode
10497 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 follows immedia
10498 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 6c tely after the l
10499 61 73 74 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a ast operand..**.
1049a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 36 20 6f ** There are 6 o
1049b 70 63 6f 64 65 73 20 6e 75 6d 62 65 72 65 64 20 pcodes numbered
1049c 66 72 6f 6d 20 30 20 74 68 72 6f 75 67 68 20 35 from 0 through 5
1049d 2e 20 20 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 . 0 is the.** "
1049e 68 61 6c 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 halt" opcode and
1049f 20 63 61 75 73 65 73 20 74 68 65 20 74 65 73 74 causes the test
104a0 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 to end..**.**
104a1 20 20 30 20 20 20 20 20 20 20 20 20 20 48 61 6c 0 Hal
104a2 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 t and return the
104a3 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 number of error
104a4 73 0a 2a 2a 20 20 20 20 31 20 4e 20 53 20 58 20 s.** 1 N S X
104a5 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 62 65 Set N bits be
104a6 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61 ginning with S a
104a7 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 nd incrementing
104a8 62 79 20 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 by X.** 2 N S
104a9 20 58 20 20 20 20 43 6c 65 61 72 20 4e 20 62 69 X Clear N bi
104aa 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 ts beginning wit
104ab 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e h S and incremen
104ac 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 ting by X.**
104ad 33 20 4e 20 20 20 20 20 20 20 20 53 65 74 20 4e 3 N Set N
104ae 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e randomly chosen
104af 20 62 69 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 bits.** 4 N
104b0 20 20 20 20 20 20 20 43 6c 65 61 72 20 4e 20 72 Clear N r
104b1 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 andomly chosen b
104b2 69 74 73 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 its.** 5 N S
104b3 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 X Set N bits
104b4 66 72 6f 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 from S increment
104b5 20 58 20 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 X in array only
104b6 2c 20 6e 6f 74 20 69 6e 20 62 69 74 76 65 63 0a , not in bitvec.
104b7 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 **.** The opcode
104b8 73 20 31 20 74 68 72 6f 75 67 68 20 34 20 70 65 s 1 through 4 pe
104b9 72 66 6f 72 6d 20 73 65 74 20 61 6e 64 20 63 6c rform set and cl
104ba 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 ear operations a
104bb 72 65 20 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 re performed.**
104bc 6f 6e 20 62 6f 74 68 20 61 20 42 69 74 76 65 63 on both a Bitvec
104bd 20 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 object and on a
104be 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 linear array of
104bf 20 62 69 74 73 20 6f 62 74 61 69 6e 65 64 20 66 bits obtained f
104c0 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f rom malloc..** O
104c1 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e pcode 5 works on
104c2 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 the linear arra
104c3 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 y only, not on t
104c4 68 65 20 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 he Bitvec..** Op
104c5 63 6f 64 65 20 35 20 69 73 20 75 73 65 64 20 74 code 5 is used t
104c6 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 69 o deliberately i
104c7 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20 69 6e nduce a fault in
104c8 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e order to.** con
104c9 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f 72 20 firm that error
104ca 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e detection works.
104cb 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f .**.** At the co
104cc 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 nclusion of the
104cd 74 65 73 74 20 74 68 65 20 6c 69 6e 65 61 72 20 test the linear
104ce 61 72 72 61 79 20 69 73 20 63 6f 6d 70 61 72 65 array is compare
104cf 64 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65 d.** against the
104d0 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 Bitvec object.
104d1 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e If there are an
104d2 79 20 64 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a y differences,.*
104d3 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 * an error is re
104d4 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 79 turned. If they
104d5 20 61 72 65 20 74 68 65 20 73 61 6d 65 2c 20 7a are the same, z
104d6 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e ero is returned.
104d7 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f .**.** If a memo
104d8 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 ry allocation er
104d9 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 ror occurs, retu
104da 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rn -1..*/.SQLITE
104db 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
104dc 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69 ite3BitvecBuilti
104dd 6e 54 65 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e nTest(int sz, in
104de 74 20 2a 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 t *aOp){. Bitve
104df 63 20 2a 70 42 69 74 76 65 63 20 3d 20 30 3b 0a c *pBitvec = 0;.
104e0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
104e1 2a 70 56 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 *pV = 0;. int r
104e2 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c c = -1;. int i,
104e3 20 6e 78 2c 20 70 63 2c 20 6f 70 3b 0a 20 20 76 nx, pc, op;. v
104e4 6f 69 64 20 2a 70 54 6d 70 53 70 61 63 65 3b 0a oid *pTmpSpace;.
104e5 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 . /* Allocate t
104e6 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65 20 he Bitvec to be
104e7 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69 6e tested and a lin
104e8 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 20 2a ear array of. *
104e9 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 61 73 * bits to act as
104ea 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 2a the reference *
104eb 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 73 71 /. pBitvec = sq
104ec 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
104ed 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 3d 20 e( sz );. pV =
104ee 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 sqlite3_malloc(
104ef 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 3b 0a (sz+7)/8 + 1 );.
104f0 20 20 70 54 6d 70 53 70 61 63 65 20 3d 20 73 71 pTmpSpace = sq
104f1 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 42 49 54 lite3_malloc(BIT
104f2 56 45 43 5f 53 5a 29 3b 0a 20 20 69 66 28 20 70 VEC_SZ);. if( p
104f3 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 3d Bitvec==0 || pV=
104f4 3d 30 20 7c 7c 20 70 54 6d 70 53 70 61 63 65 3d =0 || pTmpSpace=
104f5 3d 30 20 20 29 20 67 6f 74 6f 20 62 69 74 76 65 =0 ) goto bitve
104f6 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28 c_end;. memset(
104f7 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20 pV, 0, (sz+7)/8
104f8 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 4e 55 4c 4c + 1);.. /* NULL
104f9 20 70 42 69 74 76 65 63 20 74 65 73 74 73 20 2a pBitvec tests *
104fa 2f 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 /. sqlite3Bitve
104fb 63 53 65 74 28 30 2c 20 31 29 3b 0a 20 20 73 71 cSet(0, 1);. sq
104fc 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 lite3BitvecClear
104fd 28 30 2c 20 31 2c 20 70 54 6d 70 53 70 61 63 65 (0, 1, pTmpSpace
104fe 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 );.. /* Run the
104ff 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 63 program */. pc
10500 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28 = 0;. while( (
10501 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30 op = aOp[pc])!=0
10502 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 ){. switch(
10503 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 op ){. case
10504 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32 1:. case 2
10505 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 20 :. case 5:
10506 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 34 {. nx = 4
10507 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 61 4f ;. i = aO
10508 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 20 p[pc+2] - 1;.
10509 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d 20 2b aOp[pc+2] +
1050a 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 20 = aOp[pc+3];.
1050b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1050c 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 33 }. case 3
1050d 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 20 :. case 4:
1050e 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 . default:
1050f 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 32 {. nx = 2
10510 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
10511 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 3_randomness(siz
10512 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 20 20 eof(i), &i);.
10513 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
10514 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
10515 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 20 ( (--aOp[pc+1])
10516 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a 20 20 > 0 ) nx = 0;.
10517 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 20 pc += nx;.
10518 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 66 66 i = (i & 0x7ffff
10519 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 66 28 fff)%sz;. if(
1051a 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 7b 0a (op & 1)!=0 ){.
1051b 20 20 20 20 20 20 53 45 54 42 49 54 28 70 56 2c SETBIT(pV,
1051c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 (i+1));. i
1051d 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 20 20 f( op!=5 ){.
1051e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 if( sqlite3B
1051f 69 74 76 65 63 53 65 74 28 70 42 69 74 76 65 63 itvecSet(pBitvec
10520 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 62 69 , i+1) ) goto bi
10521 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 20 20 tvec_end;.
10522 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
10523 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 2c 20 CLEARBIT(pV,
10524 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 73 71 (i+1));. sq
10525 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 lite3BitvecClear
10526 28 70 42 69 74 76 65 63 2c 20 69 2b 31 2c 20 70 (pBitvec, i+1, p
10527 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 7d TmpSpace);. }
10528 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 . }.. /* Test
10529 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 to make sure the
1052a 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65 78 linear array ex
1052b 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74 68 actly matches th
1052c 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f 62 e. ** Bitvec ob
1052d 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 69 74 ject. Start wit
1052e 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e h the assumption
1052f 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a 20 20 that they do.
10530 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30 29 ** match (rc==0)
10531 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f 20 . Change rc to
10532 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64 69 non-zero if a di
10533 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a 20 69 screpancy. ** i
10534 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 s found.. */.
10535 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 rc = sqlite3Bitv
10536 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20 73 71 ecTest(0,0) + sq
10537 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
10538 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 0a 20 pBitvec, sz+1).
10539 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74 + sqlit
1053a 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 e3BitvecTest(pBi
1053b 74 76 65 63 2c 20 30 29 0a 20 20 20 20 20 20 20 tvec, 0).
1053c 20 20 20 2b 20 28 73 71 6c 69 74 65 33 42 69 74 + (sqlite3Bit
1053d 76 65 63 53 69 7a 65 28 70 42 69 74 76 65 63 29 vecSize(pBitvec)
1053e 20 2d 20 73 7a 29 3b 0a 20 20 66 6f 72 28 69 3d - sz);. for(i=
1053f 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 7b 0a 1; i<=sz; i++){.
10540 20 20 20 20 69 66 28 20 20 28 54 45 53 54 42 49 if( (TESTBI
10541 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 74 65 T(pV,i))!=sqlite
10542 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 74 3BitvecTest(pBit
10543 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 20 20 vec,i) ){.
10544 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 62 72 rc = i;. br
10545 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a eak;. }. }..
10546 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63 61 /* Free alloca
10547 74 65 64 20 73 74 72 75 63 74 75 72 65 20 2a 2f ted structure */
10548 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 20 73 .bitvec_end:. s
10549 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70 qlite3_free(pTmp
1054a 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65 Space);. sqlite
1054b 33 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 3_free(pV);. sq
1054c 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
1054d 6f 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 oy(pBitvec);. r
1054e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
1054f 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
10550 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a T_BUILTIN_TEST *
10551 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
10552 2a 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 ** End of bitvec
10553 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
10554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10556 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
10557 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 ** Begin file pc
10558 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ache.c *********
10559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1055a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1055b 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 */./*.** 2008 Au
1055c 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 gust 05.**.** Th
1055d 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1055e 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1055f 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
10560 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
10561 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
10562 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
10563 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
10564 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
10565 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
10566 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
10567 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
10568 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
10569 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1056a 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1056b 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1056c 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1056d 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1056e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1056f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10571 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10572 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
10573 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 ile implements t
10574 68 61 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a hat page cache..
10575 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
10576 70 63 61 63 68 65 2e 63 2c 76 20 31 2e 34 37 20 pcache.c,v 1.47
10577 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36 2009/07/25 11:46
10578 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :49 danielk1977
10579 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 Exp $.*/../*.**
1057a 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 20 A complete page
1057b 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 cache is an inst
1057c 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
1057d 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 ucture..*/.struc
1057e 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 48 t PCache {. PgH
1057f 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 69 dr *pDirty, *pDi
10580 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 20 rtyTail;
10581 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74 /* List of dirt
10582 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 6f y pages in LRU o
10583 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 20 rder */. PgHdr
10584 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20 *pSynced;
10585 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10586 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 67 Last synced pag
10587 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 20 e in dirty page
10588 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 list */. int nR
10589 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
1058a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1058b 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 Number of refer
1058c 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a 20 enced pages */.
1058d 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 20 int nMax;
1058e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1058f 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
10590 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f ed cache size */
10591 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 . int szPage;
10592 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10593 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
10594 66 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 f every page in
10595 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 20 this cache */.
10596 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20 20 int szExtra;
10597 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10598 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 /* Size of e
10599 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 20 65 xtra space for e
1059a 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e ach page */. in
1059b 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 t bPurgeable;
1059c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1059d 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 /* True if pag
1059e 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e es are on backin
1059f 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 g store */. int
105a0 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 (*xStress)(void
105a1 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20 *,PgHdr*);
105a2 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 /* Call to try
105a3 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 61 make a page clea
105a4 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 n */. void *pSt
105a5 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 ress;
105a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
105a7 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 gument to xStres
105a8 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 s */. sqlite3_p
105a9 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 cache *pCache;
105aa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6c /* Pl
105ab 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f uggable cache mo
105ac 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 72 20 dule */. PgHdr
105ad 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 20 20 *pPage1;
105ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
105af 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 Reference to pa
105b0 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ge 1 */.};../*.*
105b1 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 73 * Some of the as
105b2 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 69 6e sert() macros in
105b3 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 20 74 this code are t
105b4 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 6f 20 oo expensive to
105b5 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 72 69 run.** even duri
105b6 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 67 67 ng normal debugg
105b7 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d 20 6f ing. Use them o
105b8 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 6c 6f nly rarely on lo
105b9 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 74 65 ng-running.** te
105ba 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 68 65 sts. Enable the
105bb 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65 72 expensive asser
105bc 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 ts using the.**
105bd 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f -DSQLITE_ENABLE_
105be 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
105bf 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 =1 compile-time
105c0 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65 option..*/.#ifde
105c1 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
105c2 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
105c3 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 .# define expens
105c4 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 20 61 ive_assert(X) a
105c5 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 0a 23 ssert(X).#else.#
105c6 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 69 76 define expensiv
105c7 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 6e 64 e_assert(X).#end
105c8 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
105c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
105ca 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20 4c ******* Linked L
105cb 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20 2a ist Management *
105cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
105cd 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e ***/..#if !defin
105ce 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64 65 ed(NDEBUG) && de
105cf 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 fined(SQLITE_ENA
105d0 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
105d1 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 65 63 SERT)./*.** Chec
105d2 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 63 68 k that the pCach
105d3 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 69 61 e->pSynced varia
105d4 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 72 65 ble is set corre
105d5 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a 20 69 ctly. If it.** i
105d6 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 66 61 s not, either fa
105d7 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f 72 20 il an assert or
105d8 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f 74 68 return zero. Oth
105d9 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0a 2a erwise, return.*
105da 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 69 73 * non-zero. This
105db 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e is only used in
105dc 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64 debugging build
105dd 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a s, as follows:.*
105de 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 76 65 *.** expensive
105df 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 _assert( pcacheC
105e0 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 heckSynced(pCach
105e1 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 e) );.*/.static
105e2 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 6b 53 int pcacheCheckS
105e3 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a 70 43 ynced(PCache *pC
105e4 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a ache){. PgHdr *
105e5 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 p;. for(p=pCach
105e6 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70 e->pDirtyTail; p
105e7 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 !=pCache->pSynce
105e8 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 50 72 d; p=p->pDirtyPr
105e9 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ev){. assert(
105ea 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e p->nRef || (p->
105eb 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
105ec 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a 20 20 _SYNC) );. }.
105ed 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c 7c 20 return (p==0 ||
105ee 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e 66 p->nRef || (p->f
105ef 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
105f0 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 65 6e SYNC)==0);.}.#en
105f1 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20 26 dif /* !NDEBUG &
105f2 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
105f3 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 EXPENSIVE_ASSERT
105f4 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 */../*.** Remov
105f5 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72 6f e page pPage fro
105f6 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 64 69 m the list of di
105f7 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 73 74 rty pages..*/.st
105f8 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
105f9 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c RemoveFromDirtyL
105fa 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 67 65 ist(PgHdr *pPage
105fb 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d ){. PCache *p =
105fc 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a pPage->pCache;.
105fd 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
105fe 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c 7c 20 ->pDirtyNext ||
105ff 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 pPage==p->pDirty
10600 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 Tail );. assert
10601 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 ( pPage->pDirtyP
10602 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d rev || pPage==p-
10603 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 2f 2a >pDirty );.. /*
10604 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61 63 Update the PCac
10605 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72 69 he1.pSynced vari
10606 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72 able if necessar
10607 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 y. */. if( p->p
10608 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 29 7b Synced==pPage ){
10609 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 79 6e . PgHdr *pSyn
1060a 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 ced = pPage->pDi
1060b 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 68 69 rtyPrev;. whi
1060c 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 20 28 le( pSynced && (
1060d 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 26 50 pSynced->flags&P
1060e 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 GHDR_NEED_SYNC)
1060f 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 65 64 ){. pSynced
10610 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 69 72 = pSynced->pDir
10611 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 tyPrev;. }.
10612 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70 p->pSynced = p
10613 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 20 69 Synced;. }.. i
10614 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 f( pPage->pDirty
10615 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 61 67 Next ){. pPag
10616 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 e->pDirtyNext->p
10617 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61 67 DirtyPrev = pPag
10618 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 e->pDirtyPrev;.
10619 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
1061a 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 rt( pPage==p->pD
1061b 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 20 20 irtyTail );.
1061c 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 p->pDirtyTail =
1061d 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
1061e 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 v;. }. if( pPa
1061f 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 29 ge->pDirtyPrev )
10620 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 {. pPage->pDi
10621 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 79 4e rtyPrev->pDirtyN
10622 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 ext = pPage->pDi
10623 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 rtyNext;. }else
10624 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
10625 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 age==p->pDirty )
10626 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 ;. p->pDirty
10627 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e = pPage->pDirtyN
10628 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 ext;. }. pPage
10629 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 30 ->pDirtyNext = 0
1062a 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 ;. pPage->pDirt
1062b 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 65 78 yPrev = 0;.. ex
1062c 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 pensive_assert(
1062d 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 pcacheCheckSynce
1062e 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a d(p) );.}../*.**
1062f 20 41 64 64 20 70 61 67 65 20 70 50 61 67 65 20 Add page pPage
10630 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 to the head of t
10631 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 28 50 he dirty list (P
10632 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 69 73 Cache1.pDirty is
10633 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 67 65 set to.** pPage
10634 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
10635 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 d pcacheAddToDir
10636 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 tyList(PgHdr *pP
10637 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a age){. PCache *
10638 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 p = pPage->pCach
10639 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 e;.. assert( pP
1063a 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3d age->pDirtyNext=
1063b 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 44 69 =0 && pPage->pDi
1063c 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 70 2d rtyPrev==0 && p-
1063d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 20 29 >pDirty!=pPage )
1063e 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 ;.. pPage->pDir
1063f 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 tyNext = p->pDir
10640 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d ty;. if( pPage-
10641 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 >pDirtyNext ){.
10642 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
10643 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44 ->pDirtyNext->pD
10644 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a 20 irtyPrev==0 );.
10645 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 pPage->pDirty
10646 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76 Next->pDirtyPrev
10647 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 = pPage;. }.
10648 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 67 p->pDirty = pPag
10649 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 44 69 e;. if( !p->pDi
1064a 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 20 70 rtyTail ){. p
1064b 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 70 ->pDirtyTail = p
1064c 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Page;. }. if(
1064d 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 20 30 !p->pSynced && 0
1064e 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 ==(pPage->flags&
1064f 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
10650 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63 ){. p->pSync
10651 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a ed = pPage;. }.
10652 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
10653 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 rt( pcacheCheckS
10654 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f ynced(p) );.}../
10655 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72 6f *.** Wrapper aro
10656 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62 6c und the pluggabl
10657 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e 20 e caches xUnpin
10658 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20 63 method. If the c
10659 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e 67 ache is.** being
1065a 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d used for an in-
1065b 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c memory database,
1065c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
1065d 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
1065e 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
1065f 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29 7b Unpin(PgHdr *p){
10660 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 . PCache *pCach
10661 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 e = p->pCache;.
10662 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
10663 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 rgeable ){. i
10664 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b f( p->pgno==1 ){
10665 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70 . pCache->p
10666 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 7d Page1 = 0;. }
10667 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
10668 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10669 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70 xUnpin(pCache->p
1066a 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a 20 20 Cache, p, 0);.
1066b 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../**********
1066c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1066d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1066e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 ********* Genera
1066f 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a 2a l Interfaces ***
10670 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61 ***.**.** Initia
10671 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f 77 lize and shutdow
10672 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 n the page cache
10673 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69 74 subsystem. Neit
10674 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a 2a her of these .**
10675 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 74 functions are t
10676 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 51 hreadsafe..*/.SQ
10677 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
10678 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e sqlite3PcacheIn
10679 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b 0a itialize(void){.
1067a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
1067b 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
1067c 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 .xInit==0 ){.
1067d 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 sqlite3PCacheSe
1067e 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a tDefault();. }.
1067f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
10680 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
10681 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 74 65 che.xInit(sqlite
10682 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
10683 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a 53 51 ache.pArg);.}.SQ
10684 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
10685 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
10686 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 hutdown(void){.
10687 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 if( sqlite3Glob
10688 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10689 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 20 xShutdown ){.
1068a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
1068b 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 nfig.pcache.xShu
1068c 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f tdown(sqlite3Glo
1068d 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
1068e 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f .pArg);. }.}../
1068f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
10690 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 size in bytes of
10691 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 a PCache object
10692 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10693 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
10694 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 7b cacheSize(void){
10695 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 50 return sizeof(P
10696 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a Cache); }../*.**
10697 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 50 43 Create a new PC
10698 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 74 6f ache object. Sto
10699 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 68 6f rage space to ho
1069a 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a 2a 2a ld the object.**
1069b 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
1069c 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 n allocated and
1069d 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 20 is passed in as
1069e 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e 20 0a the p pointer. .
1069f 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64 69 ** The caller di
106a0 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 63 68 scovers how much
106a1 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 6f 20 space needs to
106a2 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 be allocated by
106a3 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 .** calling sqli
106a4 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 2e te3PcacheSize().
106a5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
106a6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
106a7 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74 cacheOpen(. int
106a8 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20 szPage,
106a9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
106aa 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 e of every page
106ab 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 */. int szExtra
106ac 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
106ad 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 /* Extra space
106ae 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
106af 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 each page */.
106b0 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 int bPurgeable,
106b1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
106b2 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72 True if pages ar
106b3 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f e on backing sto
106b4 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 re */. int (*xS
106b5 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 tress)(void*,PgH
106b6 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f 20 dr*),/* Call to
106b7 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 try to make page
106b8 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 s clean */. voi
106b9 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 d *pStress,
106ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
106bb 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73 ument to xStress
106bc 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 20 */. PCache *p
106bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106be 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 /* Preallocat
106bf 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 ed space for the
106c0 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20 20 PCache */.){.
106c1 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a memset(p, 0, siz
106c2 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a 20 20 eof(PCache));.
106c3 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 p->szPage = szPa
106c4 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72 61 ge;. p->szExtra
106c5 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 70 2d = szExtra;. p-
106c6 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 62 50 >bPurgeable = bP
106c7 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e 78 urgeable;. p->x
106c8 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 73 73 Stress = xStress
106c9 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 20 3d ;. p->pStress =
106ca 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 6e pStress;. p->n
106cb 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a 2f 2a Max = 100;.}../*
106cc 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 .** Change the p
106cd 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43 61 age size for PCa
106ce 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 che object. The
106cf 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 caller must ensu
106d0 72 65 20 74 68 61 74 20 74 68 65 72 65 0a 2a 2a re that there.**
106d1 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 are no outstand
106d2 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e ing page referen
106d3 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 66 75 ces when this fu
106d4 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
106d5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
106d6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
106d7 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 7a PcacheSetPageSiz
106d8 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 e(PCache *pCache
106d9 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b 0a 20 , int szPage){.
106da 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d assert( pCache-
106db 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 61 63 >nRef==0 && pCac
106dc 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 29 3b he->pDirty==0 );
106dd 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 . if( pCache->p
106de 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c Cache ){. sql
106df 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
106e0 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 .pcache.xDestroy
106e1 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 (pCache->pCache)
106e2 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43 ;. pCache->pC
106e3 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 ache = 0;. }.
106e4 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 3d pCache->szPage =
106e5 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a szPage;.}../*.*
106e6 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 * Try to obtain
106e7 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
106e8 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cache..*/.SQLITE
106e9 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
106ea 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
106eb 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 . PCache *pCach
106ec 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 e, /* Obta
106ed 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d in the page from
106ee 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 this cache */.
106ef 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 Pgno pgno,
106f0 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
106f1 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e 20 umber to obtain
106f2 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65 46 */. int createF
106f3 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49 66 lag, /* If
106f4 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70 61 true, create pa
106f5 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f ge if it does no
106f6 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79 20 t exist already
106f7 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70 50 */. PgHdr **ppP
106f8 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57 72 age /* Wr
106f9 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65 72 ite the page her
106fa 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20 e */.){. PgHdr
106fb 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e *pPage = 0;. in
106fc 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 61 73 t eCreate;.. as
106fd 73 65 72 74 28 20 70 43 61 63 68 65 21 3d 30 20 sert( pCache!=0
106fe 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 72 65 );. assert( cre
106ff 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 63 72 ateFlag==1 || cr
10700 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b 0a 20 eateFlag==0 );.
10701 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 20 assert( pgno>0
10702 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
10703 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 pluggable cache
10704 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
10705 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 ) has not been a
10706 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 61 llocated,. ** a
10707 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a llocate it now..
10708 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 63 */. if( !pCac
10709 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63 72 he->pCache && cr
1070a 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 eateFlag ){.
1070b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
1070c 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 p;. int nByte
1070d 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 43 ;. nByte = pC
1070e 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70 ache->szPage + p
1070f 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b Cache->szExtra +
10710 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b 0a sizeof(PgHdr);.
10711 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 p = sqlite3G
10712 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 lobalConfig.pcac
10713 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74 65 he.xCreate(nByte
10714 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 , pCache->bPurge
10715 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21 able);. if( !
10716 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 p ){. retur
10717 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
10718 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
10719 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
1071a 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 ache.xCachesize(
1071b 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 p, pCache->nMax)
1071c 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43 ;. pCache->pC
1071d 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 ache = p;. }..
1071e 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61 74 eCreate = creat
1071f 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 21 70 eFlag * (1 + (!p
10720 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c Cache->bPurgeabl
10721 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 44 e || !pCache->pD
10722 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 70 43 irty));. if( pC
10723 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a ache->pCache ){.
10724 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 pPage = sqli
10725 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
10726 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 pcache.xFetch(pC
10727 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 ache->pCache, pg
10728 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20 20 no, eCreate);.
10729 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 20 }.. if( !pPage
1072a 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29 7b && eCreate==1 ){
1072b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b . PgHdr *pPg;
1072c 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 20 .. /* Find a
1072d 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 77 72 dirty page to wr
1072e 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63 79 ite-out and recy
1072f 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20 74 cle. First try t
10730 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a 2a o find a . **
10731 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 page that does
10732 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a 6f not require a jo
10733 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 20 urnal-sync (one
10734 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44 5f with PGHDR_NEED_
10735 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65 61 SYNC. ** clea
10736 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 68 61 red), but if tha
10737 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
10738 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e 79 e settle for any
10739 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 75 other . ** u
1073a 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 74 nreferenced dirt
1073b 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 y page.. */.
1073c 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 expensive_ass
1073d 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b ert( pcacheCheck
1073e 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 Synced(pCache) )
1073f 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 ;. for(pPg=pC
10740 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 0a ache->pSynced; .
10741 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20 28 pPg && (
10742 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 50 pPg->nRef || (pP
10743 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
10744 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 20 EED_SYNC)); .
10745 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 44 pPg=pPg->pD
10746 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b 0a irtyPrev. );.
10747 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a if( !pPg ){.
10748 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43 for(pPg=pC
10749 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c ache->pDirtyTail
1074a 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52 ; pPg && pPg->nR
1074b 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69 ef; pPg=pPg->pDi
1074c 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a rtyPrev);. }.
1074d 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 if( pPg ){.
1074e 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 int rc;.
1074f 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e rc = pCache->
10750 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e xStress(pCache->
10751 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20 pStress, pPg);.
10752 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
10753 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 ITE_OK && rc!=SQ
10754 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 LITE_BUSY ){.
10755 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
10756 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
10757 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74 pPage = sqlit
10758 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
10759 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 61 cache.xFetch(pCa
1075a 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e che->pCache, pgn
1075b 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 o, 2);. }.. if
1075c 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 69 ( pPage ){. i
1075d 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74 61 f( !pPage->pData
1075e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 ){. memset
1075f 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f (pPage, 0, sizeo
10760 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 63 68 f(PgHdr) + pCach
10761 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 20 e->szExtra);.
10762 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 61 pPage->pExtra
10763 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67 65 = (void*)&pPage
10764 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 [1];. pPage
10765 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 20 ->pData = (void
10766 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 61 67 *)&((char *)pPag
10767 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 29 e)[sizeof(PgHdr)
10768 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 + pCache->szExt
10769 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 ra];. pPage
1076a 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 ->pCache = pCach
1076b 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e e;. pPage->
1076c 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 pgno = pgno;.
1076d 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
1076e 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43 Page->pCache==pC
1076f 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73 65 ache );. asse
10770 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d rt( pPage->pgno=
10771 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73 73 =pgno );. ass
10772 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78 74 ert( pPage->pExt
10773 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50 61 ra==(void *)&pPa
10774 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69 66 ge[1] );.. if
10775 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65 66 ( 0==pPage->nRef
10776 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 ){. pCache
10777 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a ->nRef++;. }.
10778 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66 2b pPage->nRef+
10779 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d +;. if( pgno=
1077a 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 =1 ){. pCac
1077b 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 he->pPage1 = pPa
1077c 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ge;. }. }.
1077d 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b *ppPage = pPage;
1077e 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 . return (pPage
1077f 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 29 20 ==0 && eCreate)
10780 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a ? SQLITE_NOMEM :
10781 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
10782 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 *.** Decrement t
10783 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
10784 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 66 nt on a page. If
10785 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c 65 the page is cle
10786 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65 an and the.** re
10787 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 72 ference count dr
10788 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 69 ops to 0, then i
10789 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c 65 t is made elible
1078a 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e 0a for recycling..
1078b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1078c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
1078d 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 acheRelease(PgHd
1078e 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 r *p){. assert(
1078f 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 p->nRef>0 );.
10790 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 p->nRef--;. if(
10791 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 p->nRef==0 ){.
10792 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 PCache *pCach
10793 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 e = p->pCache;.
10794 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d pCache->nRef-
10795 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e 66 -;. if( (p->f
10796 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
10797 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63 )==0 ){. pc
10798 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 20 acheUnpin(p);.
10799 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f }else{. /
1079a 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 * Move the page
1079b 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 to the head of t
1079c 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20 2a he dirty list. *
1079d 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 52 65 /. pcacheRe
1079e 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73 moveFromDirtyLis
1079f 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 t(p);. pcac
107a0 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74 heAddToDirtyList
107a1 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
107a2 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73 65 ../*.** Increase
107a3 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
107a4 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c 69 ount of a suppli
107a5 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a 2f ed page by 1..*/
107a6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
107a7 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
107a8 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 29 7b heRef(PgHdr *p){
107a9 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52 65 . assert(p->nRe
107aa 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b f>0);. p->nRef+
107ab 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 +;.}../*.** Drop
107ac 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
107ad 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d 75 cache. There mu
107ae 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e st be exactly on
107af 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 e reference to t
107b0 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69 73 he.** page. This
107b1 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 function delete
107b2 73 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65 s that reference
107b3 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 72 65 , so after it re
107b4 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61 67 turns the.** pag
107b5 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 e pointed to by
107b6 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f p is invalid..*/
107b7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
107b8 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
107b9 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 29 heDrop(PgHdr *p)
107ba 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 {. PCache *pCac
107bb 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d he;. assert( p-
107bc 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66 >nRef==1 );. if
107bd 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 ( p->flags&PGHDR
107be 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70 63 _DIRTY ){. pc
107bf 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 acheRemoveFromDi
107c0 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a rtyList(p);. }.
107c1 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 pCache = p->pC
107c2 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d 3e ache;. pCache->
107c3 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d nRef--;. if( p-
107c4 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 >pgno==1 ){.
107c5 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d pCache->pPage1 =
107c6 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 0;. }. sqlite
107c7 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
107c8 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 ache.xUnpin(pCac
107c9 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 31 he->pCache, p, 1
107ca 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 );.}../*.** Make
107cb 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 sure the page i
107cc 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 s marked as dirt
107cd 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 64 y. If it isn't d
107ce 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a 2a irty already,.**
107cf 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a make it so..*/.
107d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
107d1 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
107d2 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72 eMakeDirty(PgHdr
107d3 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 73 *p){. p->flags
107d4 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 5f &= ~PGHDR_DONT_
107d5 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 28 WRITE;. assert(
107d6 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 p->nRef>0 );.
107d7 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 73 if( 0==(p->flags
107d8 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20 & PGHDR_DIRTY)
107d9 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 ){. p->flags
107da 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a |= PGHDR_DIRTY;.
107db 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 pcacheAddToD
107dc 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20 irtyList( p);.
107dd 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 }.}../*.** Make
107de 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73 sure the page is
107df 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e marked as clean
107e0 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c . If it isn't cl
107e1 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 ean already,.**
107e2 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 make it so..*/.S
107e3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
107e4 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
107e5 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 MakeClean(PgHdr
107e6 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 *p){. if( (p->f
107e7 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 lags & PGHDR_DIR
107e8 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 TY) ){. pcach
107e9 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 eRemoveFromDirty
107ea 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e List(p);. p->
107eb 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52 flags &= ~(PGHDR
107ec 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45 _DIRTY|PGHDR_NEE
107ed 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 D_SYNC);. if(
107ee 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 p->nRef==0 ){.
107ef 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e pcacheUnpin
107f0 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
107f1 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65 ../*.** Make eve
107f2 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 ry page in the c
107f3 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 ache clean..*/.S
107f4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
107f5 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
107f6 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20 CleanAll(PCache
107f7 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 *pCache){. PgHd
107f8 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28 r *p;. while( (
107f9 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 p = pCache->pDir
107fa 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 ty)!=0 ){. sq
107fb 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
107fc 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a lean(p);. }.}..
107fd 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 /*.** Clear the
107fe 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
107ff 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69 flag from all di
10800 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51 rty pages..*/.SQ
10801 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
10802 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 d sqlite3PcacheC
10803 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 learSyncFlags(PC
10804 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
10805 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 PgHdr *p;. for
10806 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 (p=pCache->pDirt
10807 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 y; p; p=p->pDirt
10808 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66 yNext){. p->f
10809 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e lags &= ~PGHDR_N
1080a 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 EED_SYNC;. }.
1080b 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 pCache->pSynced
1080c 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 = pCache->pDirty
1080d 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 Tail;.}../*.** C
1080e 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e hange the page n
1080f 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 20 umber of page p
10810 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a to newPgno. .*/.
10811 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10812 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
10813 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 eMove(PgHdr *p,
10814 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 Pgno newPgno){.
10815 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 PCache *pCache
10816 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61 = p->pCache;. a
10817 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 ssert( p->nRef>0
10818 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65 );. assert( ne
10819 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c wPgno>0 );. sql
1081a 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
1081b 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70 .pcache.xRekey(p
1081c 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 Cache->pCache, p
1081d 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67 , p->pgno, newPg
1081e 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d no);. p->pgno =
1081f 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20 newPgno;. if(
10820 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f (p->flags&PGHDR_
10821 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c DIRTY) && (p->fl
10822 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
10823 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63 YNC) ){. pcac
10824 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
10825 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63 yList(p);. pc
10826 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 acheAddToDirtyLi
10827 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a st(p);. }.}../*
10828 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 .** Drop every c
10829 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 ache entry whose
1082a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 page number is
1082b 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 greater than "pg
1082c 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c no". The.** call
1082d 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 er must ensure t
1082e 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f hat there are no
1082f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 outstanding ref
10830 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 erences to any p
10831 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68 ages.** other th
10832 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61 an page 1 with a
10833 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65 page number gre
10834 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a ater than pgno..
10835 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 **.** If there i
10836 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
10837 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 20 page 1 and the
10838 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70 pgno parameter p
10839 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a assed to this.**
1083a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20 function is 0,
1083b 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 72 then the data ar
1083c 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ea associated wi
1083d 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 72 th page 1 is zer
1083e 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20 oed, but.** the
1083f 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e page object is n
10840 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 ot dropped..*/.S
10841 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
10842 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
10843 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 20 Truncate(PCache
10844 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67 *pCache, Pgno pg
10845 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 no){. if( pCach
10846 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 e->pCache ){.
10847 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 PgHdr *p;. P
10848 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20 gHdr *pNext;.
10849 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 for(p=pCache->p
1084a 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78 Dirty; p; p=pNex
1084b 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20 t){. pNext
1084c 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b = p->pDirtyNext;
1084d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67 . if( p->pg
1084e 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 no>pgno ){.
1084f 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c assert( p->fl
10850 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 ags&PGHDR_DIRTY
10851 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
10852 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
10853 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n(p);. }.
10854 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f }. if( pgno
10855 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 ==0 && pCache->p
10856 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d Page1 ){. m
10857 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50 emset(pCache->pP
10858 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20 age1->pData, 0,
10859 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b pCache->szPage);
1085a 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b . pgno = 1;
1085b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1085c 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
1085d 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28 cache.xTruncate(
1085e 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
1085f 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a pgno+1);. }.}..
10860 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61 /*.** Close a ca
10861 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 che..*/.SQLITE_P
10862 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
10863 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 te3PcacheClose(P
10864 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
10865 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 if( pCache->pC
10866 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 ache ){. sqli
10867 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
10868 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28 pcache.xDestroy(
10869 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b pCache->pCache);
1086a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 . }.}../* .** D
1086b 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 iscard the conte
1086c 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 nts of the cache
1086d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1086e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1086f 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63 PcacheClear(PCac
10870 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 73 he *pCache){. s
10871 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e qlite3PcacheTrun
10872 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 3b cate(pCache, 0);
10873 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 .}../*.** Merge
10874 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 67 two lists of pag
10875 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 es connected by
10876 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 67 pDirty and in pg
10877 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f 20 no order..** Do
10878 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 20 not both fixing
10879 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20 70 the pDirtyPrev p
1087a 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 ointers..*/.stat
1087b 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65 ic PgHdr *pcache
1087c 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 50 MergeDirtyList(P
1087d 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20 gHdr *pA, PgHdr
1087e 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 65 *pB){. PgHdr re
1087f 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 sult, *pTail;.
10880 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 3b pTail = &result;
10881 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 . while( pA &&
10882 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41 pB ){. if( pA
10883 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f 20 ->pgno<pB->pgno
10884 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e ){. pTail->
10885 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 20 pDirty = pA;.
10886 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a 20 pTail = pA;.
10887 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 44 pA = pA->pD
10888 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b irty;. }else{
10889 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44 . pTail->pD
1088a 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20 irty = pB;.
1088b 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20 pTail = pB;.
1088c 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72 pB = pB->pDir
1088d 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ty;. }. }.
1088e 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 54 if( pA ){. pT
1088f 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 ail->pDirty = pA
10890 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 ;. }else if( pB
10891 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70 ){. pTail->p
10892 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d 65 Dirty = pB;. }e
10893 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e lse{. pTail->
10894 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d 0a pDirty = 0;. }.
10895 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 2e return result.
10896 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pDirty;.}../*.**
10897 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 6f Sort the list o
10898 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e f pages in accen
10899 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 67 ding order by pg
1089a 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a 2a no. Pages are.*
1089b 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 * connected by p
1089c 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 20 Dirty pointers.
1089d 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76 20 The pDirtyPrev
1089e 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a 20 pointers are.**
1089f 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 69 corrupted by thi
108a0 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 53 69 s sort..**.** Si
108a1 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e 6f 74 nce there cannot
108a2 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 32 5e be more than 2^
108a3 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 67 65 31 distinct page
108a4 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 2c s in a database,
108a5 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e 6f 74 .** there cannot
108a6 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 33 31 be more than 31
108a7 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 72 65 buckets require
108a8 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 20 73 d by the merge s
108a9 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 65 78 orter..** One ex
108aa 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 61 64 tra bucket is ad
108ab 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 65 ded to catch ove
108ac 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 73 6f rflow in case so
108ad 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 72 20 mething.** ever
108ae 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65 20 changes to make
108af 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e the previous sen
108b0 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74 2e tence incorrect.
108b1 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53 4f .*/.#define N_SO
108b2 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a 73 74 RT_BUCKET 32.st
108b3 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 atic PgHdr *pcac
108b4 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 heSortDirtyList(
108b5 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50 PgHdr *pIn){. P
108b6 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42 gHdr *a[N_SORT_B
108b7 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 69 6e UCKET], *p;. in
108b8 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61 2c t i;. memset(a,
108b9 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b 0a 0, sizeof(a));.
108ba 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 7b 0a while( pIn ){.
108bb 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20 20 p = pIn;.
108bc 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74 79 pIn = p->pDirty
108bd 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20 ;. p->pDirty
108be 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 = 0;. for(i=0
108bf 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 4f 52 ; ALWAYS(i<N_SOR
108c0 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 2b 2b T_BUCKET-1); i++
108c1 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69 ){. if( a[i
108c2 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ]==0 ){.
108c3 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 a[i] = p;.
108c4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
108c5 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20 else{. p
108c6 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 = pcacheMergeDir
108c7 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b tyList(a[i], p);
108c8 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 . a[i] =
108c9 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
108ca 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 69 . if( NEVER(i
108cb 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d ==N_SORT_BUCKET-
108cc 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 1) ){. /* T
108cd 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 72 o get here, ther
108ce 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28 e need to be 2^(
108cf 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 65 N_SORT_BUCKET) e
108d0 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 20 20 lements in.
108d1 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c 69 ** the input li
108d2 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 69 73 st. But that is
108d3 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20 impossible..
108d4 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d */. a[i]
108d5 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 = pcacheMergeDi
108d6 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 rtyList(a[i], p)
108d7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 ;. }. }. p
108d8 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d = a[0];. for(i=
108d9 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 1; i<N_SORT_BUCK
108da 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 ET; i++){. p
108db 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72 = pcacheMergeDir
108dc 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b tyList(p, a[i]);
108dd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
108de 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
108df 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64 a list of all d
108e0 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 irty pages in th
108e1 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20 e cache, sorted
108e2 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a by page number..
108e3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
108e4 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33 E PgHdr *sqlite3
108e5 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 PcacheDirtyList(
108e6 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
108e7 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 . PgHdr *p;. f
108e8 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 or(p=pCache->pDi
108e9 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 rty; p; p=p->pDi
108ea 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d rtyNext){. p-
108eb 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69 >pDirty = p->pDi
108ec 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 rtyNext;. }. r
108ed 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74 eturn pcacheSort
108ee 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 68 65 DirtyList(pCache
108ef 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a ->pDirty);.}../*
108f0 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 .** Return the
108f1 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
108f2 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 referenced pages
108f3 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 63 held by the cac
108f4 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 he..*/.SQLITE_PR
108f5 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
108f6 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
108f7 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
108f8 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 . return pCache
108f9 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ->nRef;.}../*.**
108fa 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
108fb 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 er of references
108fc 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 75 70 to the page sup
108fd 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 plied as an argu
108fe 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ment..*/.SQLITE_
108ff 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
10900 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 te3PcachePageRef
10901 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b count(PgHdr *p){
10902 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 . return p->nRe
10903 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 f;.}../* .** Ret
10904 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 urn the total nu
10905 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
10906 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 the cache..*/.S
10907 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
10908 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 t sqlite3PcacheP
10909 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20 agecount(PCache
1090a 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 *pCache){. int
1090b 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28 nPage = 0;. if(
1090c 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 pCache->pCache
1090d 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73 ){. nPage = s
1090e 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
1090f 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63 ig.pcache.xPagec
10910 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61 ount(pCache->pCa
10911 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 che);. }. retu
10912 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 rn nPage;.}..#if
10913 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
10914 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75 /*.** Get the su
10915 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 ggested cache-si
10916 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c ze value..*/.SQL
10917 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
10918 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 sqlite3PcacheGet
10919 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 Cachesize(PCache
1091a 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74 *pCache){. ret
1091b 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 urn pCache->nMax
1091c 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1091d 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 65 73 * Set the sugges
1091e 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76 ted cache-size v
1091f 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alue..*/.SQLITE_
10920 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
10921 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63 ite3PcacheSetCac
10922 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 hesize(PCache *p
10923 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67 Cache, int mxPag
10924 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d e){. pCache->nM
10925 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69 ax = mxPage;. i
10926 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 f( pCache->pCach
10927 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
10928 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
10929 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 che.xCachesize(p
1092a 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d Cache->pCache, m
1092b 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 xPage);. }.}..#
1092c 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
1092d 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c E_CHECK_PAGES) |
1092e 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
1092f 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 46 6f _DEBUG)./*.** Fo
10930 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 r all dirty page
10931 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 s currently in t
10932 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 he cache, invoke
10933 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
10934 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 * callback. This
10935 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 is only used if
10936 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43 the SQLITE_CHEC
10937 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73 K_PAGES macro is
10938 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a .** defined..*/.
10939 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1093a 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
1093b 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 eIterateDirty(PC
1093c 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f ache *pCache, vo
1093d 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 id (*xIter)(PgHd
1093e 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a r *)){. PgHdr *
1093f 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 pDirty;. for(pD
10940 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 irty=pCache->pDi
10941 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69 rty; pDirty; pDi
10942 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 rty=pDirty->pDir
10943 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74 tyNext){. xIt
10944 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a er(pDirty);. }.
10945 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a }.#endif../*****
10946 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
10947 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a pcache.c ******
10948 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10949 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1094a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1094b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1094c 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a file pcache1.c *
1094d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1094e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1094f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
10950 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 2008 November 05
10951 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
10952 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
10953 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
10954 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
10955 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
10956 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
10957 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
10958 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
10959 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1095a 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1095b 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1095c 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1095d 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1095e 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1095f 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
10960 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
10961 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
10962 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
10963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10964 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10965 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10966 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
10967 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
10968 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 implements the d
10969 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 efault page cach
1096a 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
1096b 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 (the.** sqlite3
1096c 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 _pcache interfac
1096d 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 e). It also cont
1096e 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 ains part of the
1096f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
10970 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 ** of the SQLITE
10971 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
10972 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 E and sqlite3_re
10973 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 lease_memory() f
10974 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 eatures..** If t
10975 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 he default page
10976 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 cache implementa
10977 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 tion is override
10978 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 n, then neither
10979 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 of.** these two
1097a 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61 features are ava
1097b 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 ilable..**.** @(
1097c 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e #) $Id: pcache1.
1097d 63 2c 76 20 31 2e 31 39 20 32 30 30 39 2f 30 37 c,v 1.19 2009/07
1097e 2f 31 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68 /17 11:44:07 drh
1097f 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 74 79 70 65 Exp $.*/...type
10980 64 65 66 20 73 74 72 75 63 74 20 50 43 61 63 68 def struct PCach
10981 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79 70 65 e1 PCache1;.type
10982 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 def struct PgHdr
10983 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65 64 65 1 PgHdr1;.typede
10984 66 20 73 74 72 75 63 74 20 50 67 46 72 65 65 73 f struct PgFrees
10985 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74 3b 0a lot PgFreeslot;.
10986 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 ./* Pointers to
10987 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 structures of th
10988 69 73 20 74 79 70 65 20 61 72 65 20 63 61 73 74 is type are cast
10989 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 61 73 and returned as
1098a 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 6c 69 .** opaque sqli
1098b 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 6e 64 te3_pcache* hand
1098c 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43 les.*/.struct PC
1098d 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 61 63 ache1 {. /* Cac
1098e 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e he configuration
1098f 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 61 67 parameters. Pag
10990 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 29 20 e size (szPage)
10991 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 62 6c and the purgeabl
10992 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 50 75 e. ** flag (bPu
10993 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 65 74 rgeable) are set
10994 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 65 20 when the cache
10995 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d 61 78 is created. nMax
10996 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 6d 6f may be . ** mo
10997 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 74 69 dified at any ti
10998 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 me by a call to
10999 74 68 65 20 70 63 61 63 68 65 31 43 61 63 68 65 the pcache1Cache
1099a 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0a 20 Size() method..
1099b 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d ** The global m
1099c 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c utex must be hel
1099d 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 6e 67 d when accessing
1099e 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 69 6e nMax.. */. in
1099f 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 t szPage;
109a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
109a1 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c /* Size of all
109a2 6f 63 61 74 65 64 20 70 61 67 65 73 20 69 6e 20 ocated pages in
109a3 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 bytes */. int b
109a4 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20 Purgeable;
109a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
109a6 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 65 20 * True if cache
109a7 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a 2f 0a is purgeable */.
109a8 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
109a9 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 Min;
109aa 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d /* Minimum
109ab 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
109ac 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 75 reserved */. u
109ad 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 61 78 nsigned int nMax
109ae 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
109af 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64 /* Configured
109b0 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20 76 61 "cache_size" va
109b1 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 61 73 lue */.. /* Has
109b2 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c 20 70 h table of all p
109b3 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 ages. The follow
109b4 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 6d 61 ing variables ma
109b5 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65 73 73 y only be access
109b6 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 ed. ** when the
109b7 20 61 63 63 65 73 73 6f 72 20 69 73 20 68 6f 6c accessor is hol
109b8 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61 6c 20 ding the global
109b9 6d 75 74 65 78 20 28 73 65 65 20 70 63 61 63 68 mutex (see pcach
109ba 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 20 0a e1EnterMutex() .
109bb 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68 65 31 ** and pcache1
109bc 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e 0a 20 LeaveMutex())..
109bd 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
109be 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20 nt nRecyclable;
109bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
109c0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
109c1 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a the LRU list */.
109c2 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
109c3 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
109c4 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
109c5 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
109c6 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 75 6e n apHash */. un
109c7 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68 signed int nHash
109c8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
109c9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
109ca 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d lots in apHash[]
109cb 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 2a 61 */. PgHdr1 **a
109cc 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 pHash;
109cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
109ce 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74 h table for fast
109cf 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20 2a lookup by key *
109d0 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e /.. unsigned in
109d1 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20 20 20 t iMaxKey;
109d2 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 /* Larg
109d3 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73 69 6e est key seen sin
109d4 63 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 2a ce xTruncate() *
109d5 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 /.};../*.** Each
109d6 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 73 20 cache entry is
109d7 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 represented by a
109d8 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
109d9 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 e following .**
109da 73 74 72 75 63 74 75 72 65 2e 20 41 20 62 75 66 structure. A buf
109db 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e 70 43 fer of PgHdr1.pC
109dc 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62 79 74 ache->szPage byt
109dd 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 es is allocated
109de 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62 65 66 .** directly bef
109df 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 74 75 ore this structu
109e0 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 73 65 re in memory (se
109e1 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 4f 5f e the PGHDR1_TO_
109e2 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 72 6f PAGE() .** macro
109e3 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 72 75 below)..*/.stru
109e4 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 75 6e ct PgHdr1 {. un
109e5 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 3b signed int iKey;
109e6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
109e7 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 65 20 Key value (page
109e8 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 67 48 number) */. PgH
109e9 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 dr1 *pNext;
109ea 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
109eb 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 62 6c ext in hash tabl
109ec 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 43 61 e chain */. PCa
109ed 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 20 20 che1 *pCache;
109ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
109ef 61 63 68 65 20 74 68 61 74 20 63 75 72 72 65 6e ache that curren
109f0 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 70 61 tly owns this pa
109f1 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a ge */. PgHdr1 *
109f2 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 20 20 pLruNext;
109f3 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 /* Next i
109f4 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e n LRU list of un
109f5 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a pinned pages */.
109f6 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 50 72 PgHdr1 *pLruPr
109f7 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ev;
109f8 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 6e 20 /* Previous in
109f9 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 69 LRU list of unpi
109fa 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a 7d 3b nned pages */.};
109fb 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 6c 6f ../*.** Free slo
109fc 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f 63 61 ts in the alloca
109fd 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 76 69 tor used to divi
109fe 64 65 20 75 70 20 74 68 65 20 62 75 66 66 65 72 de up the buffer
109ff 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e 67 0a provided using.
10a00 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f ** the SQLITE_CO
10a01 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 6d NFIG_PAGECACHE m
10a02 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 72 echanism..*/.str
10a03 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 7b uct PgFreeslot {
10a04 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 . PgFreeslot *p
10a05 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 66 Next; /* Next f
10a06 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a ree slot */.};..
10a07 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 61 74 /*.** Global dat
10a08 61 20 75 73 65 64 20 62 79 20 74 68 69 73 20 63 a used by this c
10a09 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ache..*/.static
10a0a 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 SQLITE_WSD struc
10a0b 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 20 7b t PCacheGlobal {
10a0c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
10a0d 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 *mutex;
10a0e 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 69 63 /* static
10a0f 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 54 41 mutex MUTEX_STA
10a10 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 69 6e TIC_LRU */.. in
10a11 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 20 20 t nMaxPage;
10a12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10a13 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 61 78 /* Sum of nMax
10a14 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61 62 Page for purgeab
10a15 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 le caches */. i
10a16 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 20 20 nt nMinPage;
10a17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10a18 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 69 /* Sum of nMi
10a19 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61 nPage for purgea
10a1a 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 ble caches */.
10a1b 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67 65 int nCurrentPage
10a1c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10a1d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
10a1e 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 73 purgeable pages
10a1f 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 allocated */.
10a20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 61 64 PgHdr1 *pLruHead
10a21 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 20 20 , *pLruTail;
10a22 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 74 20 /* LRU list
10a23 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 of unpinned page
10a24 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 69 61 s */.. /* Varia
10a25 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 bles related to
10a26 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
10a27 47 45 43 41 43 48 45 20 73 65 74 74 69 6e 67 73 GECACHE settings
10a28 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 6c 6f . */. int szSlo
10a29 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
10a2a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
10a2b 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 65 20 ze of each free
10a2c 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a slot */. void *
10a2d 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 20 20 pStart, *pEnd;
10a2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10a2f 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 65 63 Bounds of pagec
10a30 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 6e 67 ache malloc rang
10a31 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 6c 6f e */. PgFreeslo
10a32 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 20 t *pFree;
10a33 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 /* Fr
10a34 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 20 2a ee page blocks *
10a35 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 /. int isInit;
10a36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10a37 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
10a38 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a if initialized *
10a39 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b 0a 0a /.} pcache1_g;..
10a3a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 20 69 /*.** All code i
10a3b 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 6f 75 n this file shou
10a3c 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 67 6c ld access the gl
10a3d 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 61 obal structure a
10a3e 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a 2a 20 bove via the.**
10a3f 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 22 2e alias "pcache1".
10a40 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 This ensures th
10a41 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 6c 61 at the WSD emula
10a42 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68 65 tion is used whe
10a43 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 n.** compiling f
10a44 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 or systems that
10a45 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 do not support r
10a46 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 65 66 eal WSD..*/.#def
10a47 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 4c 4f ine pcache1 (GLO
10a48 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 63 68 BAL(struct PCach
10a49 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 65 31 eGlobal, pcache1
10a4a 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e _g))../*.** When
10a4b 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63 74 a PgHdr1 struct
10a4c 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ure is allocated
10a4d 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 , the associated
10a4e 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 0a PCache1.szPage.
10a4f 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 ** bytes of data
10a50 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 69 72 are located dir
10a51 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69 74 20 ectly before it
10a52 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 2e 20 in memory (i.e.
10a53 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 69 7a the total.** siz
10a54 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 e of the allocat
10a55 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 50 67 ion is sizeof(Pg
10a56 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e 73 7a Hdr1)+PCache1.sz
10a57 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 65 0a Page byte). The.
10a58 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 ** PGHDR1_TO_PAG
10a59 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 E() macro takes
10a5a 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 a pointer to a P
10a5b 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20 gHdr1 structure
10a5c 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d 65 6e as.** an argumen
10a5d 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 t and returns a
10a5e 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 pointer to the a
10a5f 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 6b 20 ssociated block
10a60 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 79 74 of szPage.** byt
10a61 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 4f 5f es. The PAGE_TO_
10a62 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f 20 64 PGHDR1() macro d
10a63 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 74 65 oes the opposite
10a64 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69 : its argument i
10a65 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 s.** a pointer t
10a66 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 7a 50 o a block of szP
10a67 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 age bytes of dat
10a68 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 72 6e a and the return
10a69 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 70 value is.** a p
10a6a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 73 ointer to the as
10a6b 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 31 20 sociated PgHdr1
10a6c 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
10a6d 20 20 20 61 73 73 65 72 74 28 20 50 47 48 44 52 assert( PGHDR
10a6e 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 5f 54 1_TO_PAGE(PAGE_T
10a6f 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c O_PGHDR1(pCache,
10a70 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a 23 64 X))==X );.*/.#d
10a71 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54 4f 5f efine PGHDR1_TO_
10a72 50 41 47 45 28 70 29 20 20 20 20 28 76 6f 69 64 PAGE(p) (void
10a73 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20 2d 20 *)(((char*)p) -
10a74 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 p->pCache->szPag
10a75 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 5f e).#define PAGE_
10a76 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70 29 20 TO_PGHDR1(c, p)
10a77 28 50 67 48 64 72 31 2a 29 28 28 28 63 68 61 72 (PgHdr1*)(((char
10a78 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61 67 65 *)p) + c->szPage
10a79 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 )../*.** Macros
10a7a 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c 65 61 to enter and lea
10a7b 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 ve the global LR
10a7c 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 65 66 U mutex..*/.#def
10a7d 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74 65 72 ine pcache1Enter
10a7e 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f Mutex() sqlite3_
10a7f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63 mutex_enter(pcac
10a80 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 66 69 he1.mutex).#defi
10a81 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d ne pcache1LeaveM
10a82 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d utex() sqlite3_m
10a83 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 63 68 utex_leave(pcach
10a84 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a e1.mutex)../****
10a85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
10a8a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63 ***** Page Alloc
10a8b 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e ation/SQLITE_CON
10a8c 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c 61 74 FIG_PCACHE Relat
10a8d 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a ed Functions ***
10a8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
10a8f 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
10a90 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69 n is called duri
10a91 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ng initializatio
10a92 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 62 75 n if a static bu
10a93 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 70 70 ffer is .** supp
10a94 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f 72 20 lied to use for
10a95 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 62 the page-cache b
10a96 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 53 51 y passing the SQ
10a97 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 LITE_CONFIG_PAGE
10a98 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 74 6f CACHE.** verb to
10a99 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
10a9a 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70 42 75 ). Parameter pBu
10a9b 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 f points to an a
10a9c 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65 0a llocation large.
10a9d 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e ** enough to con
10a9e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65 72 73 tain 'n' buffers
10a9f 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73 20 65 of 'sz' bytes e
10aa0 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ach..*/.SQLITE_P
10aa1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
10aa2 74 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53 te3PCacheBufferS
10aa3 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75 66 2c etup(void *pBuf,
10aa4 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b int sz, int n){
10aa5 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 69 . if( pcache1.i
10aa6 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50 67 46 sInit ){. PgF
10aa7 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 reeslot *p;.
10aa8 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 sz = ROUNDDOWN8(
10aa9 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 sz);. pcache1
10aaa 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20 .szSlot = sz;.
10aab 20 20 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 pcache1.pStart
10aac 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70 63 61 = pBuf;. pca
10aad 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 3b 0a che1.pFree = 0;.
10aae 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29 while( n-- )
10aaf 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50 67 46 {. p = (PgF
10ab0 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a 20 reeslot*)pBuf;.
10ab1 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 p->pNext =
10ab2 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 pcache1.pFree;.
10ab3 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72 pcache1.pFr
10ab4 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 ee = p;. pB
10ab5 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 uf = (void*)&((c
10ab6 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d 3b 0a har*)pBuf)[sz];.
10ab7 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
10ab8 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b 0a 20 1.pEnd = pBuf;.
10ab9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c }.}../*.** Mall
10aba 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 oc function used
10abb 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c within this fil
10abc 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 e to allocate sp
10abd 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 ace from the buf
10abe 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 fer.** configure
10abf 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f d using sqlite3_
10ac0 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f config(SQLITE_CO
10ac1 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 NFIG_PAGECACHE)
10ac2 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a option. If no .*
10ac3 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 65 78 * such buffer ex
10ac4 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 69 73 ists or there is
10ac5 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 20 69 no space left i
10ac6 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e 63 74 n it, this funct
10ac7 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 62 61 ion falls .** ba
10ac8 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c ck to sqlite3Mal
10ac9 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
10aca 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 41 6c void *pcache1Al
10acb 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a loc(int nByte){.
10acc 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 void *p;. ass
10acd 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
10ace 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e ex_held(pcache1.
10acf 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
10ad0 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 2e 73 nByte<=pcache1.s
10ad1 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 65 31 zSlot && pcache1
10ad2 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 61 73 .pFree ){. as
10ad3 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 sert( pcache1.is
10ad4 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20 3d 20 Init );. p =
10ad5 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65 (PgHdr1 *)pcache
10ad6 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 1.pFree;. pca
10ad7 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 63 61 che1.pFree = pca
10ad8 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e 65 78 che1.pFree->pNex
10ad9 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 t;. sqlite3St
10ada 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 atusSet(SQLITE_S
10adb 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
10adc 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a 20 20 SIZE, nByte);.
10add 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
10ade 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
10adf 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c _PAGECACHE_USED,
10ae0 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 1);. }else{..
10ae1 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /* Allocate a
10ae2 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 69 6e new buffer usin
10ae3 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 2e g sqlite3Malloc.
10ae4 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f Before doing so
10ae5 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 20 2a , exit the. *
10ae6 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 65 20 * global pcache
10ae7 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f 63 6b mutex and unlock
10ae8 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 the pager-cache
10ae9 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 2e 20 object pCache.
10aea 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a 2a 20 This is . **
10aeb 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 61 so that if the a
10aec 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 ttempt to alloca
10aed 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 te a new buffer
10aee 63 61 75 73 65 73 20 74 68 65 20 74 68 65 20 0a causes the the .
10aef 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 ** configure
10af0 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 6d 69 d soft-heap-limi
10af1 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 65 64 t to be breached
10af2 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 6f 73 , it will be pos
10af3 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a 2a 20 sible to. **
10af4 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 66 reclaim memory f
10af5 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 2d 63 rom this pager-c
10af6 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ache.. */.
10af7 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10af8 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 ex();. p = sq
10af9 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 lite3Malloc(nByt
10afa 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 e);. pcache1E
10afb 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
10afc 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 if( p ){.
10afd 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 65 33 int sz = sqlite3
10afe 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 MallocSize(p);.
10aff 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 sqlite3Stat
10b00 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 usAdd(SQLITE_STA
10b01 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 TUS_PAGECACHE_OV
10b02 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 20 20 ERFLOW, sz);.
10b03 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
10b04 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 p;.}../*.** Free
10b05 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 an allocated bu
10b06 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 ffer obtained fr
10b07 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 om pcache1Alloc(
10b08 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
10b09 64 20 70 63 61 63 68 65 31 46 72 65 65 28 76 6f d pcache1Free(vo
10b0a 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 id *p){. assert
10b0b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
10b0c 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74 held(pcache1.mut
10b0d 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d ex) );. if( p==
10b0e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 0 ) return;. if
10b0f 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 53 74 ( p>=pcache1.pSt
10b10 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 65 31 art && p<pcache1
10b11 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 67 46 .pEnd ){. PgF
10b12 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 3b 0a reeslot *pSlot;.
10b13 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
10b14 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
10b15 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 US_PAGECACHE_USE
10b16 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 6c 6f D, -1);. pSlo
10b17 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f 74 2a t = (PgFreeslot*
10b18 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d 3e 70 )p;. pSlot->p
10b19 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 Next = pcache1.p
10b1a 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65 Free;. pcache
10b1b 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f 74 3b 1.pFree = pSlot;
10b1c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e . }else{. in
10b1d 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 t iSize = sqlite
10b1e 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 3MallocSize(p);.
10b1f 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
10b20 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
10b21 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 US_PAGECACHE_OVE
10b22 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a RFLOW, -iSize);.
10b23 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
10b24 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
10b25 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 * Allocate a new
10b26 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 6e 69 page object ini
10b27 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 74 65 tially associate
10b28 64 20 77 69 74 68 20 63 61 63 68 65 20 70 43 61 d with cache pCa
10b29 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 che..*/.static P
10b2a 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 41 6c gHdr1 *pcache1Al
10b2b 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 31 20 locPage(PCache1
10b2c 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20 *pCache){. int
10b2d 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 50 nByte = sizeof(P
10b2e 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 65 2d gHdr1) + pCache-
10b2f 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69 64 20 >szPage;. void
10b30 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31 41 6c *pPg = pcache1Al
10b31 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 50 67 loc(nByte);. Pg
10b32 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28 20 70 Hdr1 *p;. if( p
10b33 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20 50 41 Pg ){. p = PA
10b34 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 GE_TO_PGHDR1(pCa
10b35 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20 20 69 che, pPg);. i
10b36 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 f( pCache->bPurg
10b37 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 eable ){. p
10b38 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 cache1.nCurrentP
10b39 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d age++;. }. }
10b3a 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 30 3b else{. p = 0;
10b3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
10b3c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
10b3d 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6c 6c page object all
10b3e 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 68 65 ocated by pcache
10b3f 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a 2a 2a 1AllocPage()..**
10b40 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 .** The pointer
10b41 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 is allowed to be
10b42 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 NULL, which is
10b43 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 69 74 prudent. But it
10b44 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 74 68 turns out.** th
10b45 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 at the current i
10b46 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 mplementation ha
10b47 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 20 63 ppens to never c
10b48 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 all this routine
10b49 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 .** with a NULL
10b4a 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 20 6d pointer, so we m
10b4b 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 65 73 ark the NULL tes
10b4c 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 29 2e t with ALWAYS().
10b4d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10b4e 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10b4f 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 66 PgHdr1 *p){. if
10b50 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a 20 ( ALWAYS(p) ){.
10b51 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 68 65 if( p->pCache
10b52 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a ->bPurgeable ){.
10b53 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43 pcache1.nC
10b54 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a 20 20 urrentPage--;.
10b55 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 31 46 }. pcache1F
10b56 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 ree(PGHDR1_TO_PA
10b57 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f GE(p));. }.}../
10b58 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63 *.** Malloc func
10b59 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 51 4c tion used by SQL
10b5a 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 ite to obtain sp
10b5b 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66 ace from the buf
10b5c 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 0a 2a fer configured.*
10b5d 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f * using sqlite3_
10b5e 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f config(SQLITE_CO
10b5f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20 NFIG_PAGECACHE)
10b60 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 73 75 option. If no su
10b61 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 78 69 ch buffer.** exi
10b62 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69 sts, this functi
10b63 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f on falls back to
10b64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 sqlite3Malloc()
10b65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
10b66 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
10b67 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20 3PageMalloc(int
10b68 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a sz){. void *p;.
10b69 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10b6a 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 63 61 tex();. p = pca
10b6b 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b 0a 20 che1Alloc(sz);.
10b6c 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10b6d 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ex();. return p
10b6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
10b6f 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 an allocated buf
10b70 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 6f fer obtained fro
10b71 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c m sqlite3PageMal
10b72 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 loc()..*/.SQLITE
10b73 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
10b74 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 6f lite3PageFree(vo
10b75 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 68 65 id *p){. pcache
10b76 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10b77 20 70 63 61 63 68 65 31 46 72 65 65 28 70 29 3b pcache1Free(p);
10b78 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
10b79 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a utex();.}../****
10b7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
10b7f 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6d ***** General Im
10b80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 75 6e plementation Fun
10b81 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ctions *********
10b82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
10b84 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
10b85 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 73 n is used to res
10b86 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62 ize the hash tab
10b87 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 le used by the c
10b88 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a 20 61 ache passed.** a
10b89 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
10b8a 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
10b8b 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 global mutex mus
10b8c 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74 t be held when t
10b8d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
10b8e 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 called..*/.stati
10b8f 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 65 73 c int pcache1Res
10b90 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 31 20 izeHash(PCache1
10b91 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 2a 2a *p){. PgHdr1 **
10b92 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65 apNew;. unsigne
10b93 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 75 6e d int nNew;. un
10b94 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 0a 20 signed int i;..
10b95 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
10b96 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 _mutex_held(pcac
10b97 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 he1.mutex) );..
10b98 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 73 68 nNew = p->nHash
10b99 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 3c 32 *2;. if( nNew<2
10b9a 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 20 3d 56 ){. nNew =
10b9b 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 256;. }.. pca
10b9c 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10b9d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 ;. if( p->nHash
10b9e 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e ){ sqlite3Begin
10b9f 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 BenignMalloc();
10ba0 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 67 48 }. apNew = (PgH
10ba1 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d dr1 **)sqlite3_m
10ba2 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 67 48 alloc(sizeof(PgH
10ba3 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20 dr1 *)*nNew);.
10ba4 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20 if( p->nHash ){
10ba5 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
10ba6 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 70 63 Malloc(); }. pc
10ba7 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10ba8 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 20 29 );. if( apNew )
10ba9 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4e {. memset(apN
10baa 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 67 ew, 0, sizeof(Pg
10bab 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 Hdr1 *)*nNew);.
10bac 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d for(i=0; i<p-
10bad 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a 20 20 >nHash; i++){.
10bae 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 PgHdr1 *pPag
10baf 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72 31 20 e;. PgHdr1
10bb0 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70 48 61 *pNext = p->apHa
10bb1 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77 68 69 sh[i];. whi
10bb2 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70 4e 65 le( (pPage = pNe
10bb3 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 xt)!=0 ){.
10bb4 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
10bb5 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25 = pPage->iKey %
10bb6 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70 nNew;. p
10bb7 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e Next = pPage->pN
10bb8 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61 ext;. pPa
10bb9 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 4e 65 ge->pNext = apNe
10bba 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 61 70 w[h];. ap
10bbb 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a New[h] = pPage;.
10bbc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
10bbd 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
10bbe 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 20 70 ->apHash);. p
10bbf 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e 65 77 ->apHash = apNew
10bc0 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 20 3d ;. p->nHash =
10bc1 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 72 65 nNew;. }.. re
10bc2 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 68 20 turn (p->apHash
10bc3 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 ? SQLITE_OK : SQ
10bc4 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a LITE_NOMEM);.}..
10bc5 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
10bc6 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 74 65 ion is used inte
10bc7 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f 76 65 rnally to remove
10bc8 20 74 68 65 20 70 61 67 65 20 70 50 61 67 65 20 the page pPage
10bc9 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 6c 6f from the .** glo
10bca 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 69 66 bal LRU list, if
10bcb 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 2e 20 is part of it.
10bcc 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20 If pPage is not
10bcd 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 part of the glob
10bce 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 2c 20 al.** LRU list,
10bcf 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 then this functi
10bd0 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
10bd1 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 *.** The global
10bd2 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 mutex must be he
10bd3 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ld when this fun
10bd4 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
10bd5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10bd6 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 50 pcache1PinPage(P
10bd7 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a 20 gHdr1 *pPage){.
10bd8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
10bd9 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 _mutex_held(pcac
10bda 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 he1.mutex) );.
10bdb 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70 50 if( pPage && (pP
10bdc 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c age->pLruNext ||
10bdd 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 31 2e pPage==pcache1.
10bde 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 pLruTail) ){.
10bdf 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 if( pPage->pLru
10be0 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 50 Prev ){. pP
10be1 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70 age->pLruPrev->p
10be2 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 65 2d LruNext = pPage-
10be3 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d >pLruNext;. }
10be4 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
10be5 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 20 pLruNext ){.
10be6 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 pPage->pLruNex
10be7 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50 t->pLruPrev = pP
10be8 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20 age->pLruPrev;.
10be9 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61 }. if( pca
10bea 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d 3d 70 che1.pLruHead==p
10beb 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63 Page ){. pc
10bec 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 3d ache1.pLruHead =
10bed 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10bee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
10bef 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
10bf0 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 ==pPage ){.
10bf1 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 pcache1.pLruTai
10bf2 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 l = pPage->pLruP
10bf3 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 rev;. }. p
10bf4 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d Page->pLruNext =
10bf5 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 0;. pPage->p
10bf6 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 LruPrev = 0;.
10bf7 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e pPage->pCache->
10bf8 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b 0a 20 nRecyclable--;.
10bf9 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d }.}.../*.** Rem
10bfa 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75 70 ove the page sup
10bfb 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 plied as an argu
10bfc 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68 61 ment from the ha
10bfd 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 50 43 sh table .** (PC
10bfe 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 74 72 ache1.apHash str
10bff 75 63 74 75 72 65 29 20 74 68 61 74 20 69 74 20 ucture) that it
10c00 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f is currently sto
10c01 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 red in..**.** Th
10c02 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d e global mutex m
10c03 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e ust be held when
10c04 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
10c05 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
10c06 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 tic void pcache1
10c07 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 50 RemoveFromHash(P
10c08 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a 20 gHdr1 *pPage){.
10c09 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b unsigned int h;
10c0a 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 . PCache1 *pCac
10c0b 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 he = pPage->pCac
10c0c 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a 70 he;. PgHdr1 **p
10c0d 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 65 2d p;.. h = pPage-
10c0e 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e >iKey % pCache->
10c0f 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 70 3d nHash;. for(pp=
10c10 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b &pCache->apHash[
10c11 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 67 65 h]; (*pp)!=pPage
10c12 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 ; pp=&(*pp)->pNe
10c13 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 2a 70 xt);. *pp = (*p
10c14 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 70 43 p)->pNext;.. pC
10c15 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 7d ache->nPage--;.}
10c16 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 ../*.** If there
10c17 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 6d are currently m
10c18 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 65 2e ore than pcache.
10c19 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20 61 nMaxPage pages a
10c1a 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a 2a llocated, try.**
10c1b 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67 65 to recycle page
10c1c 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 s to reduce the
10c1d 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65 64 number allocated
10c1e 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 78 50 to pcache.nMaxP
10c1f 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 age..*/.static v
10c20 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 6f 72 oid pcache1Enfor
10c21 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 29 7b ceMaxPage(void){
10c22 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10c23 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10c24 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10c25 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 65 31 while( pcache1
10c26 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70 63 .nCurrentPage>pc
10c27 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 26 ache1.nMaxPage &
10c28 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 & pcache1.pLruTa
10c29 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31 il ){. PgHdr1
10c2a 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e 70 4c *p = pcache1.pL
10c2b 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 63 ruTail;. pcac
10c2c 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20 he1PinPage(p);.
10c2d 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65 pcache1Remove
10c2e 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 FromHash(p);.
10c2f 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 pcache1FreePage
10c30 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a (p);. }.}../*.*
10c31 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 70 61 * Discard all pa
10c32 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 20 70 ges from cache p
10c33 43 61 63 68 65 20 77 69 74 68 20 61 20 70 61 67 Cache with a pag
10c34 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 76 61 e number (key va
10c35 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 65 72 lue) .** greater
10c36 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
10c37 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 o iLimit. Any pi
10c38 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 74 20 nned pages that
10c39 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 63 72 meet this .** cr
10c3a 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 69 6e iteria are unpin
10c3b 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 79 20 ned before they
10c3c 61 72 65 20 64 69 73 63 61 72 64 65 64 2e 0a 2a are discarded..*
10c3d 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 *.** The global
10c3e 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 mutex must be he
10c3f 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ld when this fun
10c40 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
10c41 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10c42 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 pcache1TruncateU
10c43 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 65 31 nsafe(. PCache1
10c44 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 6e 73 *pCache, . uns
10c45 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 igned int iLimit
10c46 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 .){. TESTONLY(
10c47 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 unsigned int nP
10c48 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20 20 20 age = 0; )
10c49 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73 65 72 /* Used to asser
10c4a 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20 t pCache->nPage
10c4b 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20 is correct */.
10c4c 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a unsigned int h;.
10c4d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
10c4e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 3_mutex_held(pca
10c4f 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 che1.mutex) );.
10c50 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 61 63 for(h=0; h<pCac
10c51 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b 29 7b he->nHash; h++){
10c52 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a 70 70 . PgHdr1 **pp
10c53 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 = &pCache->apHa
10c54 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 48 64 sh[h]; . PgHd
10c55 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 77 r1 *pPage;. w
10c56 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 2a hile( (pPage = *
10c57 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 pp)!=0 ){.
10c58 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3e if( pPage->iKey>
10c59 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 =iLimit ){.
10c5a 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 pCache->nPage
10c5b 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 20 --;. *pp
10c5c 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a = pPage->pNext;.
10c5d 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 50 pcache1P
10c5e 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 inPage(pPage);.
10c5f 20 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 pcache1Fr
10c60 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 eePage(pPage);.
10c61 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
10c62 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d pp = &pPage-
10c63 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
10c64 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67 65 2b TESTONLY( nPage+
10c65 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20 20 20 +; ). }.
10c66 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 }. }. assert(
10c67 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3d 3d pCache->nPage==
10c68 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a nPage );.}../***
10c69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
10c6e 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 ****** sqlite3_p
10c6f 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 2a 2a cache Methods **
10c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
10c73 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10c74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10c75 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 74 20 e3_pcache.xInit
10c76 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 method..*/.stati
10c77 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 6e 69 c int pcache1Ini
10c78 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
10c79 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
10c7a 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
10c7b 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 assert( pcache1
10c7c 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a 20 20 .isInit==0 );.
10c7d 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c memset(&pcache1,
10c7e 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 0, sizeof(pcach
10c7f 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 e1));. if( sqli
10c80 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
10c81 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 bCoreMutex ){.
10c82 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65 78 20 pcache1.mutex
10c83 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f = sqlite3_mutex_
10c84 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 alloc(SQLITE_MUT
10c85 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 3b 0a EX_STATIC_LRU);.
10c86 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e 69 73 }. pcache1.is
10c87 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65 74 75 Init = 1;. retu
10c88 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
10c89 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10c8a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10c8b 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 68 75 ite3_pcache.xShu
10c8c 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a tdown method..**
10c8d 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73 Note that the s
10c8e 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c 6c 6f tatic mutex allo
10c8f 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74 20 64 cated in xInit d
10c90 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 oes .** not need
10c91 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f to be freed..*/
10c92 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10c93 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76 6f 69 che1Shutdown(voi
10c94 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
10c95 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
10c96 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65 NotUsed);. asse
10c97 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e rt( pcache1.isIn
10c98 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 it!=0 );. memse
10c99 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20 73 t(&pcache1, 0, s
10c9a 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29 3b izeof(pcache1));
10c9b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
10c9c 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
10c9d 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 sqlite3_pcache.x
10c9e 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a Create method..*
10c9f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 *.** Allocate a
10ca0 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 new cache..*/.st
10ca1 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61 atic sqlite3_pca
10ca2 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 65 61 che *pcache1Crea
10ca3 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 te(int szPage, i
10ca4 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 7b 0a nt bPurgeable){.
10ca5 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
10ca6 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d 20 28 e;.. pCache = (
10ca7 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 74 65 PCache1 *)sqlite
10ca8 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 3_malloc(sizeof(
10ca9 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28 PCache1));. if(
10caa 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6d pCache ){. m
10cab 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 30 2c emset(pCache, 0,
10cac 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 29 sizeof(PCache1)
10cad 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 73 );. pCache->s
10cae 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a zPage = szPage;.
10caf 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 75 72 pCache->bPur
10cb0 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 67 65 geable = (bPurge
10cb1 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20 able ? 1 : 0);.
10cb2 20 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c if( bPurgeabl
10cb3 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 e ){. pCach
10cb4 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 e->nMin = 10;.
10cb5 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 pcache1Enter
10cb6 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 70 Mutex();. p
10cb7 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 cache1.nMinPage
10cb8 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b += pCache->nMin;
10cb9 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 4c 65 . pcache1Le
10cba 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 aveMutex();.
10cbb 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 }. }. return (
10cbc 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
10cbd 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a )pCache;.}../*.*
10cbe 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
10cbf 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
10cc0 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a pcache.xCachesiz
10cc1 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a e method. .**.**
10cc2 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 Configure the c
10cc3 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20 ache_size limit
10cc4 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a for a cache..*/.
10cc5 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10cc6 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 71 6c he1Cachesize(sql
10cc7 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 ite3_pcache *p,
10cc8 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61 int nMax){. PCa
10cc9 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 che1 *pCache = (
10cca 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 69 PCache1 *)p;. i
10ccb 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 f( pCache->bPurg
10ccc 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 eable ){. pca
10ccd 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10cce 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 4d ;. pcache1.nM
10ccf 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20 axPage += (nMax
10cd0 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b - pCache->nMax);
10cd1 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 . pCache->nMa
10cd2 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63 x = nMax;. pc
10cd3 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 ache1EnforceMaxP
10cd4 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 63 68 age();. pcach
10cd5 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a e1LeaveMutex();.
10cd6 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 }.}../*.** Imp
10cd7 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10cd8 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
10cd9 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d 65 74 e.xPagecount met
10cda 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 hod. .*/.static
10cdb 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 65 63 int pcache1Pagec
10cdc 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 63 61 ount(sqlite3_pca
10cdd 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e che *p){. int n
10cde 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10cdf 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28 Mutex();. n = (
10ce0 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e (PCache1 *)p)->n
10ce1 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 31 4c Page;. pcache1L
10ce2 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
10ce3 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn n;.}../*.*
10ce4 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
10ce5 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
10ce6 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 6d 65 pcache.xFetch me
10ce7 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 thod. .**.** Fet
10ce8 63 68 20 61 20 70 61 67 65 20 62 79 20 6b 65 79 ch a page by key
10ce9 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 value..**.** Wh
10cea 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e ether or not a n
10ceb 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 20 61 ew page may be a
10cec 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 llocated by this
10ced 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64 function depend
10cee 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75 s on.** the valu
10cef 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46 e of the createF
10cf0 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20 20 30 lag argument. 0
10cf1 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 61 6c means do not al
10cf2 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 locate a new.**
10cf3 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73 20 61 page. 1 means a
10cf4 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 llocate a new pa
10cf5 67 65 20 69 66 20 73 70 61 63 65 20 69 73 20 65 ge if space is e
10cf6 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e asily available.
10cf7 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20 74 6f 2 .** means to
10cf8 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61 72 64 try really hard
10cf9 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e to allocate a n
10cfa 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 46 ew page..**.** F
10cfb 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65 61 62 or a non-purgeab
10cfc 6c 65 20 63 61 63 68 65 20 28 61 20 63 61 63 68 le cache (a cach
10cfd 65 20 75 73 65 64 20 61 73 20 74 68 65 20 73 74 e used as the st
10cfe 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69 6e 2d orage for an in-
10cff 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 memory.** databa
10d00 73 65 29 20 74 68 65 72 65 20 69 73 20 72 65 61 se) there is rea
10d01 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65 6e 63 lly no differenc
10d02 65 20 62 65 74 77 65 65 6e 20 63 72 65 61 74 65 e between create
10d03 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20 20 53 Flag 1 and 2. S
10d04 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 o.** the calling
10d05 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61 63 68 function (pcach
10d06 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65 72 20 e.c) will never
10d07 68 61 76 65 20 61 20 63 72 65 61 74 65 46 6c 61 have a createFla
10d08 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61 20 6e g of 1 on.** a n
10d09 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61 63 68 on-purgable cach
10d0a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 e..**.** There a
10d0b 72 65 20 74 68 72 65 65 20 64 69 66 66 65 72 65 re three differe
10d0c 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20 74 6f nt approaches to
10d0d 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61 63 65 obtaining space
10d0e 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a 2a 20 for a page,.**
10d0f 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 depending on the
10d10 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61 6d 65 value of parame
10d11 74 65 72 20 63 72 65 61 74 65 46 6c 61 67 20 28 ter createFlag (
10d12 77 68 69 63 68 20 6d 61 79 20 62 65 20 30 2c 20 which may be 0,
10d13 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a 20 20 1 or 2)..**.**
10d14 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 1. Regardless o
10d15 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 63 f the value of c
10d16 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65 20 63 reateFlag, the c
10d17 61 63 68 65 20 69 73 20 73 65 61 72 63 68 65 64 ache is searched
10d18 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20 20 20 for a .**
10d19 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 71 75 copy of the requ
10d1a 65 73 74 65 64 20 70 61 67 65 2e 20 49 66 20 6f ested page. If o
10d1b 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69 74 20 ne is found, it
10d1c 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
10d1d 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65 61 74 ** 2. If creat
10d1e 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74 68 65 eFlag==0 and the
10d1f 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
10d20 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 eady in the cach
10d21 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 20 20 e, NULL is.**
10d22 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a returned..**.
10d23 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65 61 74 ** 3. If creat
10d24 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e 64 20 eFlag is 1, and
10d25 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
10d26 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 already in the c
10d27 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20 61 6e ache,.** an
10d28 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 20 74 d if either of t
10d29 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
10d2a 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 4e 55 true, return NU
10d2b 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 LL:.**.**
10d2c 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f (a) the number o
10d2d 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62 f pages pinned b
10d2e 79 20 74 68 65 20 63 61 63 68 65 20 69 73 20 67 y the cache is g
10d2f 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20 reater than.**
10d30 20 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31 PCache1
10d31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 20 20 .nMax, or.**
10d32 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d 62 65 (b) the numbe
10d33 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 r of pages pinne
10d34 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 d by the cache i
10d35 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a s greater than.*
10d36 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 * the
10d37 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 20 sum of nMax for
10d38 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 all purgeable ca
10d39 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65 20 73 ches, less the s
10d3a 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 um of .**
10d3b 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 6c 6c nMin for all
10d3c 20 6f 74 68 65 72 20 70 75 72 67 65 61 62 6c 65 other purgeable
10d3d 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 caches. .**.**
10d3e 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f 66 20 4. If none of
10d3f 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 the first three
10d40 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 6c 79 conditions apply
10d41 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 20 69 and the cache i
10d42 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 20 20 s marked.**
10d43 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c 20 61 as purgeable, a
10d44 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 nd if one of the
10d45 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 following is tr
10d46 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 ue:.**.**
10d47 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f (a) The number o
10d48 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 f pages allocate
10d49 64 20 66 6f 72 20 74 68 65 20 63 61 63 68 65 20 d for the cache
10d4a 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 20 is already .**
10d4b 20 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31 PCache1
10d4c 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 .nMax, or.**.**
10d4d 20 20 20 20 20 20 28 62 29 20 54 68 65 20 6e 75 (b) The nu
10d4e 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c mber of pages al
10d4f 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c 6c 20 located for all
10d50 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 purgeable caches
10d51 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 is.**
10d52 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c 20 74 already equal t
10d53 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 o or greater tha
10d54 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 n the sum of nMa
10d55 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 20 20 x for all.**
10d56 20 20 20 20 20 20 20 70 75 72 67 65 61 62 6c 65 purgeable
10d57 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a 20 20 caches,.**.**
10d58 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74 then attempt
10d59 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 to recycle a pa
10d5a 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 55 20 ge from the LRU
10d5b 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73 20 74 list. If it is t
10d5c 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 20 20 he right.**
10d5d 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 74 68 size, return th
10d5e 65 20 72 65 63 79 63 6c 65 64 20 62 75 66 66 65 e recycled buffe
10d5f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 72 r. Otherwise, fr
10d60 65 65 20 74 68 65 20 62 75 66 66 65 72 20 61 6e ee the buffer an
10d61 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 65 65 d.** procee
10d62 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a 2a 2a d to step 5. .**
10d63 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 77 69 .** 5. Otherwi
10d64 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 se, allocate and
10d65 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20 70 61 return a new pa
10d66 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 ge buffer..*/.st
10d67 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 atic void *pcach
10d68 65 31 46 65 74 63 68 28 73 71 6c 69 74 65 33 5f e1Fetch(sqlite3_
10d69 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 pcache *p, unsig
10d6a 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 69 6e ned int iKey, in
10d6b 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b 0a 20 t createFlag){.
10d6c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50 unsigned int nP
10d6d 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 65 31 inned;. PCache1
10d6e 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 *pCache = (PCac
10d6f 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 he1 *)p;. PgHdr
10d70 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 1 *pPage = 0;..
10d71 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d assert( pCache-
10d72 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 63 >bPurgeable || c
10d73 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29 3b 0a reateFlag!=1 );.
10d74 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10d75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63 72 65 tex();. if( cre
10d76 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c ateFlag==1 ) sql
10d77 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
10d78 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a 20 53 alloc();.. /* S
10d79 65 61 72 63 68 20 74 68 65 20 68 61 73 68 20 74 earch the hash t
10d7a 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78 69 73 able for an exis
10d7b 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 ting entry. */.
10d7c 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 48 61 if( pCache->nHa
10d7d 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e 73 69 sh>0 ){. unsi
10d7e 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 gned int h = iKe
10d7f 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 y % pCache->nHas
10d80 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61 67 65 h;. for(pPage
10d81 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b =pCache->apHash[
10d82 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61 67 65 h]; pPage&&pPage
10d83 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20 70 50 ->iKey!=iKey; pP
10d84 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65 78 74 age=pPage->pNext
10d85 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 );. }.. if( pP
10d86 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 age || createFla
10d87 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63 g==0 ){. pcac
10d88 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 he1PinPage(pPage
10d89 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63 );. goto fetc
10d8a 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a h_out;. }.. /*
10d8b 20 53 74 65 70 20 33 20 6f 66 20 68 65 61 64 65 Step 3 of heade
10d8c 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a 20 20 r comment. */.
10d8d 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63 68 65 nPinned = pCache
10d8e 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63 68 65 ->nPage - pCache
10d8f 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a 20 ->nRecyclable;.
10d90 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d if( createFlag=
10d91 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 =1 && (.
10d92 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63 68 65 nPinned>=(pcache
10d93 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61 63 68 1.nMaxPage+pCach
10d94 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 31 2e e->nMin-pcache1.
10d95 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20 20 7c nMinPage). |
10d96 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 61 63 | nPinned>=(pCac
10d97 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f 20 31 he->nMax * 9 / 1
10d98 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f 74 0). )){. got
10d99 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d o fetch_out;. }
10d9a 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e .. if( pCache->
10d9b 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e 6e nPage>=pCache->n
10d9c 48 61 73 68 20 26 26 20 70 63 61 63 68 65 31 52 Hash && pcache1R
10d9d 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 68 65 esizeHash(pCache
10d9e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65 ) ){. goto fe
10d9f 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 tch_out;. }..
10da0 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20 74 /* Step 4. Try t
10da1 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67 65 o recycle a page
10da2 20 62 75 66 66 65 72 20 69 66 20 61 70 70 72 6f buffer if appro
10da3 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28 priate. */. if(
10da4 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 pCache->bPurgea
10da5 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 2e 70 ble && pcache1.p
10da6 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 20 20 LruTail && (.
10da7 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 (pCache->nPage
10da8 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78 +1>=pCache->nMax
10da9 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75 ) || pcache1.nCu
10daa 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 63 68 rrentPage>=pcach
10dab 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 29 29 e1.nMaxPage. ))
10dac 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 63 {. pPage = pc
10dad 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a ache1.pLruTail;.
10dae 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 pcache1Remov
10daf 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29 eFromHash(pPage)
10db0 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69 6e ;. pcache1Pin
10db1 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
10db2 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 61 63 if( pPage->pCac
10db3 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 61 63 he->szPage!=pCac
10db4 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a 20 20 he->szPage ){.
10db5 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 pcache1FreeP
10db6 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
10db7 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 pPage = 0;.
10db8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 }else{. pc
10db9 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 ache1.nCurrentPa
10dba 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e 70 43 ge -= (pPage->pC
10dbb 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
10dbc 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 - pCache->bPurg
10dbd 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 eable);. }.
10dbe 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20 }.. /* Step 5.
10dbf 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 67 65 If a usable page
10dc0 20 62 75 66 66 65 72 20 68 61 73 20 73 74 69 6c buffer has stil
10dc1 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64 l not been found
10dc2 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20 , . ** attempt
10dc3 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 to allocate a ne
10dc4 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69 w one. . */. i
10dc5 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20 f( !pPage ){.
10dc6 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31 pPage = pcache1
10dc7 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 68 65 AllocPage(pCache
10dc8 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 );. }.. if( pP
10dc9 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 69 67 age ){. unsig
10dca 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79 ned int h = iKey
10dcb 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 % pCache->nHash
10dcc 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 ;. pCache->nP
10dcd 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65 age++;. pPage
10dce 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20 ->iKey = iKey;.
10dcf 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 pPage->pNext
10dd0 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 = pCache->apHash
10dd1 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e [h];. pPage->
10dd2 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65 3b pCache = pCache;
10dd3 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 . pPage->pLru
10dd4 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50 Prev = 0;. pP
10dd5 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 age->pLruNext =
10dd6 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20 2a 2a 0;. *(void **
10dd7 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 )(PGHDR1_TO_PAGE
10dd8 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a 20 20 (pPage)) = 0;.
10dd9 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 pCache->apHash
10dda 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d [h] = pPage;. }
10ddb 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 20 69 ..fetch_out:. i
10ddc 66 28 20 70 50 61 67 65 20 26 26 20 69 4b 65 79 f( pPage && iKey
10ddd 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 >pCache->iMaxKey
10dde 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e ){. pCache->
10ddf 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0a iMaxKey = iKey;.
10de0 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61 74 65 }. if( create
10de1 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65 Flag==1 ) sqlite
10de2 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
10de3 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 ();. pcache1Lea
10de4 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 veMutex();. ret
10de5 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50 47 48 urn (pPage ? PGH
10de6 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67 DR1_TO_PAGE(pPag
10de7 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a e) : 0);.}.../*.
10de8 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
10de9 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
10dea 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 20 6d _pcache.xUnpin m
10deb 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72 ethod..**.** Mar
10dec 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e 70 69 k a page as unpi
10ded 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65 20 66 nned (eligible f
10dee 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 or asynchronous
10def 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73 recycling)..*/.s
10df0 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10df1 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65 33 5f e1Unpin(sqlite3_
10df2 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69 64 20 pcache *p, void
10df3 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73 65 55 *pPg, int reuseU
10df4 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43 61 63 nlikely){. PCac
10df5 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 he1 *pCache = (P
10df6 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 Cache1 *)p;. Pg
10df7 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 50 41 Hdr1 *pPage = PA
10df8 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 GE_TO_PGHDR1(pCa
10df9 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20 20 61 che, pPg);. . a
10dfa 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 43 ssert( pPage->pC
10dfb 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29 3b 0a ache==pCache );.
10dfc 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10dfd 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 tex();.. /* It
10dfe 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63 is an error to c
10dff 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
10e00 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 n if the page is
10e01 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a 20 70 already . ** p
10e02 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 art of the globa
10e03 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 2a 2f l LRU list.. */
10e04 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
10e05 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 26 26 ->pLruPrev==0 &&
10e06 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10e07 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
10e08 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 pcache1.pLruHea
10e09 64 21 3d 70 50 61 67 65 20 26 26 20 70 63 61 63 d!=pPage && pcac
10e0a 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d 70 50 he1.pLruTail!=pP
10e0b 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 72 65 age );.. if( re
10e0c 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c 20 70 useUnlikely || p
10e0d 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 cache1.nCurrentP
10e0e 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 78 age>pcache1.nMax
10e0f 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 61 63 Page ){. pcac
10e10 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 he1RemoveFromHas
10e11 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63 h(pPage);. pc
10e12 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50 ache1FreePage(pP
10e13 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 age);. }else{.
10e14 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 70 61 /* Add the pa
10e15 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c ge to the global
10e16 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 6d 61 LRU list. Norma
10e17 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20 69 73 lly, the page is
10e18 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 2a 2a added to. **
10e19 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65 the head of the
10e1a 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61 67 65 list (last page
10e1b 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 64 29 to be recycled)
10e1c 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 . However, if th
10e1d 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 65 55 e . ** reuseU
10e1e 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 61 73 nlikely flag pas
10e1f 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 sed to this func
10e20 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 68 tion is true, th
10e21 65 20 70 61 67 65 20 69 73 20 61 64 64 65 64 0a e page is added.
10e22 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 61 ** to the ta
10e23 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28 il of the list (
10e24 66 69 72 73 74 20 70 61 67 65 20 74 6f 20 62 65 first page to be
10e25 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 20 20 recycled)..
10e26 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 63 68 */. if( pcach
10e27 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b 0a 20 e1.pLruHead ){.
10e28 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 pcache1.pLr
10e29 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 76 20 uHead->pLruPrev
10e2a 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70 = pPage;. p
10e2b 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d Page->pLruNext =
10e2c 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 pcache1.pLruHea
10e2d 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 d;. pcache1
10e2e 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67 .pLruHead = pPag
10e2f 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 e;. }else{.
10e30 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 pcache1.pLru
10e31 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20 Tail = pPage;.
10e32 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 pcache1.pLru
10e33 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 Head = pPage;.
10e34 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e }. pCache->
10e35 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20 nRecyclable++;.
10e36 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 }.. pcache1Lea
10e37 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a veMutex();.}../*
10e38 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10e39 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10e3a 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 79 20 3_pcache.xRekey
10e3b 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 method. .*/.stat
10e3c 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 52 ic void pcache1R
10e3d 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 5f ekey(. sqlite3_
10e3e 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 6f 69 pcache *p,. voi
10e3f 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 67 6e d *pPg,. unsign
10e40 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 20 75 ed int iOld,. u
10e41 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e 65 77 nsigned int iNew
10e42 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 .){. PCache1 *p
10e43 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 Cache = (PCache1
10e44 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a *)p;. PgHdr1 *
10e45 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f 5f pPage = PAGE_TO_
10e46 50 47 48 44 52 31 28 70 43 61 63 68 65 2c 20 70 PGHDR1(pCache, p
10e47 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a Pg);. PgHdr1 **
10e48 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 pp;. unsigned i
10e49 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 74 28 nt h; . assert(
10e4a 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f pPage->iKey==iO
10e4b 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ld );. assert(
10e4c 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 pPage->pCache==p
10e4d 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63 61 63 Cache );.. pcac
10e4e 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
10e4f 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 43 61 .. h = iOld%pCa
10e50 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 70 che->nHash;. pp
10e51 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61 = &pCache->apHa
10e52 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 28 20 sh[h];. while(
10e53 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 7b 0a (*pp)!=pPage ){.
10e54 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d pp = &(*pp)-
10e55 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 >pNext;. }. *p
10e56 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 p = pPage->pNext
10e57 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 70 43 ;.. h = iNew%pC
10e58 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 ache->nHash;. p
10e59 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 Page->iKey = iNe
10e5a 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 w;. pPage->pNex
10e5b 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 t = pCache->apHa
10e5c 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 65 2d sh[h];. pCache-
10e5d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 >apHash[h] = pPa
10e5e 67 65 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 78 52 ge;.. /* The xR
10e5f 65 6b 65 79 28 29 20 69 6e 74 65 72 66 61 63 65 ekey() interface
10e60 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 74 6f is only used to
10e61 20 6d 6f 76 65 20 70 61 67 65 73 20 65 61 72 6c move pages earl
10e62 69 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 ier in the. **
10e63 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 69 database file (i
10e64 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 76 65 20 n order to move
10e65 61 6c 6c 20 66 72 65 65 20 70 61 67 65 73 20 74 all free pages t
10e66 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
10e67 0a 20 20 2a 2a 20 66 69 6c 65 20 77 68 65 72 65 . ** file where
10e68 20 74 68 65 79 20 63 61 6e 20 62 65 20 74 72 75 they can be tru
10e69 6e 63 61 74 65 64 20 6f 66 66 2e 29 20 20 48 65 ncated off.) He
10e6a 6e 63 65 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 nce, it is not p
10e6b 6f 73 73 69 62 6c 65 0a 20 20 2a 2a 20 66 6f 72 ossible. ** for
10e6c 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 6e 75 the new page nu
10e6d 6d 62 65 72 20 74 6f 20 62 65 20 67 72 65 61 74 mber to be great
10e6e 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 er than the larg
10e6f 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 est previously.
10e70 20 2a 2a 20 66 65 74 63 68 65 64 20 70 61 67 65 ** fetched page
10e71 2e 20 20 42 75 74 20 77 65 20 72 65 74 61 69 6e . But we retain
10e72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
10e73 65 73 74 20 69 6e 20 63 61 73 65 20 78 52 65 6b est in case xRek
10e74 65 79 28 29 0a 20 20 2a 2a 20 62 65 67 69 6e 73 ey(). ** begins
10e75 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 64 to be used in d
10e76 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 69 6e ifferent ways in
10e77 20 74 68 65 20 66 75 74 75 72 65 2e 0a 20 20 2a the future.. *
10e78 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 4e /. if( NEVER(iN
10e79 65 77 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b ew>pCache->iMaxK
10e7a 65 79 29 20 29 7b 0a 20 20 20 20 70 43 61 63 68 ey) ){. pCach
10e7b 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65 e->iMaxKey = iNe
10e7c 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 w;. }.. pcache
10e7d 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 1LeaveMutex();.}
10e7e 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
10e7f 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 tation of the sq
10e80 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 54 72 lite3_pcache.xTr
10e81 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a uncate method. .
10e82 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c **.** Discard al
10e83 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 l unpinned pages
10e84 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 69 in the cache wi
10e85 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 th a page number
10e86 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 equal to.** or
10e87 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 61 72 greater than par
10e88 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 2e 20 41 ameter iLimit. A
10e89 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 ny pinned pages
10e8a 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 with a page numb
10e8b 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f er.** equal to o
10e8c 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69 r greater than i
10e8d 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 6c 69 63 Limit are implic
10e8e 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a itly unpinned..*
10e8f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
10e90 61 63 68 65 31 54 72 75 6e 63 61 74 65 28 73 71 ache1Truncate(sq
10e91 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c lite3_pcache *p,
10e92 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c unsigned int iL
10e93 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 68 65 31 imit){. PCache1
10e94 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 *pCache = (PCac
10e95 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 he1 *)p;. pcach
10e96 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a e1EnterMutex();.
10e97 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43 if( iLimit<=pC
10e98 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b ache->iMaxKey ){
10e99 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e . pcache1Trun
10e9a 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 68 cateUnsafe(pCach
10e9b 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 e, iLimit);.
10e9c 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 pCache->iMaxKey
10e9d 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a = iLimit-1;. }.
10e9e 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
10e9f 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 tex();.}../*.**
10ea0 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
10ea1 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 f the sqlite3_pc
10ea2 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 ache.xDestroy me
10ea3 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 thod. .**.** Des
10ea4 74 72 6f 79 20 61 20 63 61 63 68 65 20 61 6c 6c troy a cache all
10ea5 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70 63 61 ocated using pca
10ea6 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f che1Create()..*/
10ea7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10ea8 63 68 65 31 44 65 73 74 72 6f 79 28 73 71 6c 69 che1Destroy(sqli
10ea9 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a te3_pcache *p){.
10eaa 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 PCache1 *pCach
10eab 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 e = (PCache1 *)p
10eac 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10ead 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68 Mutex();. pcach
10eae 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65 e1TruncateUnsafe
10eaf 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 70 (pCache, 0);. p
10eb0 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 cache1.nMaxPage
10eb1 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b -= pCache->nMax;
10eb2 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 . pcache1.nMinP
10eb3 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e age -= pCache->n
10eb4 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e Min;. pcache1En
10eb5 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b 0a forceMaxPage();.
10eb6 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 pcache1LeaveMu
10eb7 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 tex();. sqlite3
10eb8 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70 _free(pCache->ap
10eb9 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 Hash);. sqlite3
10eba 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b 0a 7d _free(pCache);.}
10ebb 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
10ebc 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
10ebd 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a during initializ
10ebe 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69 ation (sqlite3_i
10ebf 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a nitialize()) to.
10ec0 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65 20 64 ** install the d
10ec1 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62 6c 65 efault pluggable
10ec2 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61 cache module, a
10ec3 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73 65 72 ssuming the user
10ec4 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 has not.** alre
10ec5 61 64 79 20 70 72 6f 76 69 64 65 64 20 61 6e 20 ady provided an
10ec6 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a alternative..*/.
10ec7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10ec8 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 oid sqlite3PCach
10ec9 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 eSetDefault(void
10eca 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
10ecb 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
10ecc 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 ds defaultMethod
10ecd 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 s = {. 0,
10ece 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ecf 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20 /* pArg */.
10ed0 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c 20 20 pcache1Init,
10ed1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 49 /* xI
10ed2 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 nit */. pcach
10ed3 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 20 20 e1Shutdown,
10ed4 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e /* xShutdown
10ed5 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43 */. pcache1C
10ed6 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 reate,
10ed7 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f 0a 20 /* xCreate */.
10ed8 20 20 20 70 63 61 63 68 65 31 43 61 63 68 65 73 pcache1Caches
10ed9 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 ize, /* x
10eda 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20 Cachesize */.
10edb 20 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e pcache1Pagecoun
10edc 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 50 61 t, /* xPa
10edd 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70 gecount */. p
10ede 63 61 63 68 65 31 46 65 74 63 68 2c 20 20 20 20 cache1Fetch,
10edf 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65 74 63 /* xFetc
10ee0 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 h */. pcache1
10ee1 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20 20 20 Unpin,
10ee2 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 /* xUnpin */.
10ee3 20 20 20 70 63 61 63 68 65 31 52 65 6b 65 79 2c pcache1Rekey,
10ee4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
10ee5 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70 63 61 Rekey */. pca
10ee6 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20 20 20 che1Truncate,
10ee7 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 /* xTrunca
10ee8 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 te */. pcache
10ee9 31 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 1Destroy
10eea 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a /* xDestroy *
10eeb 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 /. };. sqlite3
10eec 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 _config(SQLITE_C
10eed 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20 26 64 ONFIG_PCACHE, &d
10eee 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a efaultMethods);.
10eef 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
10ef0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d _ENABLE_MEMORY_M
10ef1 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 ANAGEMENT./*.**
10ef2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
10ef3 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 called to free
10ef4 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 superfluous dyna
10ef5 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 mically allocate
10ef6 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 d memory.** held
10ef7 20 62 79 20 74 68 65 20 70 61 67 65 72 20 73 79 by the pager sy
10ef8 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 stem. Memory in
10ef9 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74 use by any SQLit
10efa 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 e pager allocate
10efb 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72 d.** by the curr
10efc 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62 ent thread may b
10efd 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 e sqlite3_free()
10efe 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 ed..**.** nReq i
10eff 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
10f00 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 bytes of memory
10f01 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20 74 required. Once t
10f02 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20 his much has.**
10f03 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c 20 74 been released, t
10f04 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
10f05 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 6e 20 rns. The return
10f06 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74 value is the tot
10f07 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 al number .** of
10f08 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
10f09 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 released..*/.SQ
10f0a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
10f0b 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
10f0c 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 leaseMemory(int
10f0d 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72 nReq){. int nFr
10f0e 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 63 ee = 0;. if( pc
10f0f 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 ache1.pStart==0
10f10 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70 ){. PgHdr1 *p
10f11 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 ;. pcache1Ent
10f12 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 77 erMutex();. w
10f13 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c hile( (nReq<0 ||
10f14 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20 nFree<nReq) &&
10f15 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 54 (p=pcache1.pLruT
10f16 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 ail) ){. nF
10f17 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 ree += sqlite3Ma
10f18 6c 6c 6f 63 53 69 7a 65 28 50 47 48 44 52 31 5f llocSize(PGHDR1_
10f19 54 4f 5f 50 41 47 45 28 70 29 29 3b 0a 20 20 20 TO_PAGE(p));.
10f1a 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 pcache1PinPag
10f1b 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 e(p);. pcac
10f1c 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 he1RemoveFromHas
10f1d 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63 h(p);. pcac
10f1e 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0a he1FreePage(p);.
10f1f 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
10f20 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
10f21 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65 }. return nFre
10f22 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 e;.}.#endif /* S
10f23 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
10f24 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a ORY_MANAGEMENT *
10f25 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
10f26 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 _TEST./*.** This
10f27 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 function is use
10f28 64 20 62 79 20 74 65 73 74 20 70 72 6f 63 65 64 d by test proced
10f29 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63 74 20 ures to inspect
10f2a 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 the internal sta
10f2b 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f te.** of the glo
10f2c 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 bal cache..*/.SQ
10f2d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
10f2e 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
10f2f 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43 tats(. int *pnC
10f30 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 urrent, /*
10f31 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 OUT: Total numbe
10f32 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65 r of pages cache
10f33 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 d */. int *pnMa
10f34 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f x, /* O
10f35 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d UT: Global maxim
10f36 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f um cache size */
10f37 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 . int *pnMin,
10f38 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 /* OUT:
10f39 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31 2e 6e Sum of PCache1.n
10f3a 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61 62 6c Min for purgeabl
10f3b 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e e caches */. in
10f3c 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 t *pnRecyclable
10f3d 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c /* OUT: Total
10f3e 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10f3f 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 available for r
10f40 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 ecycling */.){.
10f41 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e PgHdr1 *p;. in
10f42 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 t nRecyclable =
10f43 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61 63 68 0;. for(p=pcach
10f44 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70 3b 20 e1.pLruHead; p;
10f45 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a p=p->pLruNext){.
10f46 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b nRecyclable+
10f47 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72 +;. }. *pnCurr
10f48 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e 6e 43 ent = pcache1.nC
10f49 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 urrentPage;. *p
10f4a 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31 2e 6e nMax = pcache1.n
10f4b 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 MaxPage;. *pnMi
10f4c 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e n = pcache1.nMin
10f4d 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 Page;. *pnRecyc
10f4e 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 lable = nRecycla
10f4f 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ble;.}.#endif../
10f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
10f51 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e 63 20 nd of pcache1.c
10f52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
10f55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
10f56 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 egin file rowset
10f57 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
10f58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
10f5a 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62 *.** 2008 Decemb
10f5b 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 er 3.**.** The a
10f5c 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
10f5d 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
10f5e 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
10f5f 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
10f60 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
10f61 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
10f62 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
10f63 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
10f64 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
10f65 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
10f66 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
10f67 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
10f68 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
10f69 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
10f6a 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
10f6b 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
10f6c 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
10f6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10f71 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d ***.**.** This m
10f72 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 odule implements
10f73 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 63 61 an object we ca
10f74 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a ll a "RowSet"..*
10f75 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 *.** The RowSet
10f76 6f 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c object is a coll
10f77 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 ection of rowids
10f78 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 . Rowids.** are
10f79 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 inserted into t
10f7a 68 65 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 he RowSet in an
10f7b 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e arbitrary order.
10f7c 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e Inserts.** can
10f7d 20 62 65 20 69 6e 74 65 72 6d 69 78 65 64 20 77 be intermixed w
10f7e 69 74 68 20 74 65 73 74 73 20 74 6f 20 73 65 65 ith tests to see
10f7f 20 69 66 20 61 20 67 69 76 65 6e 20 72 6f 77 69 if a given rowi
10f80 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 d has been.** pr
10f81 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 eviously inserte
10f82 64 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 d into the RowSe
10f83 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 t..**.** After a
10f84 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66 ll inserts are f
10f85 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70 inished, it is p
10f86 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61 ossible to extra
10f87 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e ct the.** elemen
10f88 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74 ts of the RowSet
10f89 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 in sorted order
10f8a 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78 74 . Once this ext
10f8b 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 raction.** proce
10f8c 73 73 20 68 61 73 20 73 74 61 72 74 65 64 2c 20 ss has started,
10f8d 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 no new elements
10f8e 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e may be inserted.
10f8f 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 .**.** Hence, th
10f90 65 20 70 72 69 6d 69 74 69 76 65 20 6f 70 65 72 e primitive oper
10f91 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 ations for a Row
10f92 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 Set are:.**.**
10f93 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 CREATE.** I
10f94 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 NSERT.** TEST
10f95 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a .** SMALLEST.
10f96 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a ** DESTROY.**
10f97 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 45 20 61 .** The CREATE a
10f98 6e 64 20 44 45 53 54 52 4f 59 20 70 72 69 6d 69 nd DESTROY primi
10f99 74 69 76 65 73 20 61 72 65 20 74 68 65 20 63 6f tives are the co
10f9a 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 65 nstructor and de
10f9b 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 structor,.** obv
10f9c 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 iously. The INS
10f9d 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 61 64 ERT primitive ad
10f9e 64 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 ds a new element
10f9f 20 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a to the RowSet..
10fa0 2a 2a 20 54 45 53 54 20 63 68 65 63 6b 73 20 74 ** TEST checks t
10fa1 6f 20 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d o see if an elem
10fa2 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 ent is already i
10fa3 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53 n the RowSet. S
10fa4 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 MALLEST.** extra
10fa5 63 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61 cts the least va
10fa6 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 lue from the Row
10fa7 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 Set..**.** The I
10fa8 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 NSERT primitive
10fa9 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 might allocate a
10faa 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 dditional memory
10fab 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 . Memory is.**
10fac 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 68 75 allocated in chu
10fad 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 nks so most INSE
10fae 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 RTs do no alloca
10faf 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 73 20 tion. There is
10fb0 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 an .** upper bou
10fb1 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f nd on the size o
10fb2 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f f allocated memo
10fb3 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 ry. No memory i
10fb4 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c s freed.** until
10fb5 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 DESTROY..**.**
10fb6 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 The TEST primiti
10fb7 76 65 20 69 6e 63 6c 75 64 65 73 20 61 20 22 62 ve includes a "b
10fb8 61 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 atch" number. T
10fb9 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 he TEST primitiv
10fba 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 e.** will only s
10fbb 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 ee elements that
10fbc 20 77 65 72 65 20 69 6e 73 65 72 74 65 64 20 62 were inserted b
10fbd 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 63 efore the last c
10fbe 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 hange.** in the
10fbf 62 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 batch number. I
10fc0 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 n other words, i
10fc1 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 f an INSERT occu
10fc2 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 rs between.** tw
10fc3 6f 20 54 45 53 54 73 20 77 68 65 72 65 20 74 68 o TESTs where th
10fc4 65 20 54 45 53 54 73 20 68 61 76 65 20 74 68 65 e TESTs have the
10fc5 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 62 6d same batch nubm
10fc6 65 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 er, then the.**
10fc7 76 61 6c 75 65 20 61 64 64 65 64 20 62 79 20 74 value added by t
10fc8 68 65 20 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e he INSERT will n
10fc9 6f 74 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f ot be visible to
10fca 20 74 68 65 20 73 65 63 6f 6e 64 20 54 45 53 54 the second TEST
10fcb 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 61 6c ..** The initial
10fcc 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 batch number is
10fcd 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 zero, so if the
10fce 20 76 65 72 79 20 66 69 72 73 74 20 54 45 53 54 very first TEST
10fcf 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e contains.** a n
10fd0 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 on-zero batch nu
10fd1 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 mber, it will se
10fd2 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 e all prior INSE
10fd3 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e RTs..**.** No IN
10fd4 53 45 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73 SERTs may occurs
10fd5 20 61 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 after a SMALLES
10fd6 54 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e T. An assertion
10fd7 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a will fail if.**
10fd8 20 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74 that is attempt
10fd9 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ed..**.** The co
10fda 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 st of an INSERT
10fdb 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 is roughly const
10fdc 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 ant. (Sometime
10fdd 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 new memory.** ha
10fde 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 s to be allocate
10fdf 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 d on an INSERT.)
10fe0 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 The cost of a
10fe1 54 45 53 54 20 77 69 74 68 20 61 20 6e 65 77 0a TEST with a new.
10fe2 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 ** batch number
10fe3 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 is O(NlogN) wher
10fe4 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e N is the numbe
10fe5 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e r of elements in
10fe6 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 the RowSet..**
10fe7 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 The cost of a TE
10fe8 53 54 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d ST using the sam
10fe9 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 e batch number i
10fea 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 s O(logN). The
10feb 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 cost.** of the f
10fec 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 irst SMALLEST is
10fed 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f O(NlogN). Seco
10fee 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e nd and subsequen
10fef 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 t SMALLEST.** pr
10ff0 69 6d 69 74 69 76 65 73 20 61 72 65 20 63 6f 6e imitives are con
10ff1 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 stant time. The
10ff2 20 63 6f 73 74 20 6f 66 20 44 45 53 54 52 4f 59 cost of DESTROY
10ff3 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 is O(N)..**.**
10ff4 54 68 65 72 65 20 69 73 20 61 6e 20 61 64 64 65 There is an adde
10ff5 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 d cost of O(N) w
10ff6 68 65 6e 20 73 77 69 74 63 68 69 6e 67 20 62 65 hen switching be
10ff7 74 77 65 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a tween TEST and.*
10ff8 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 * SMALLEST primi
10ff9 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 tives..**.** $Id
10ffa 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 2e 37 : rowset.c,v 1.7
10ffb 20 32 30 30 39 2f 30 35 2f 32 32 20 30 31 3a 30 2009/05/22 01:0
10ffc 30 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a 0:13 drh Exp $.*
10ffd 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 65 74 /.../*.** Target
10ffe 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f 63 61 size for alloca
10fff 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a 2f 0a tion chunks..*/.
11000 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 41 #define ROWSET_A
11001 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 20 31 LLOCATION_SIZE 1
11002 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 024../*.** The n
11003 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20 umber of rowset
11004 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f entries per allo
11005 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f cation chunk..*/
11006 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f .#define ROWSET_
11007 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20 ENTRY_PER_CHUNK
11008 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
11009 20 20 20 20 20 20 20 20 20 20 28 28 52 4f 57 53 ((ROWS
1100a 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 ET_ALLOCATION_SI
1100b 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72 ZE-8)/sizeof(str
1100c 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29 uct RowSetEntry)
1100d 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e )../*.** Each en
1100e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20 try in a RowSet
1100f 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f is an instance o
11010 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
11011 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 object..*/.struc
11012 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 7b 20 t RowSetEntry {
11013 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 36 . i6
11014 34 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 4 v;
11015 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
11016 4f 57 49 44 20 76 61 6c 75 65 20 66 6f 72 20 74 OWID value for t
11017 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 his entry */. s
11018 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
11019 79 20 2a 70 52 69 67 68 74 3b 20 20 20 2f 2a 20 y *pRight; /*
1101a 52 69 67 68 74 20 73 75 62 74 72 65 65 20 28 6c Right subtree (l
1101b 61 72 67 65 72 20 65 6e 74 72 69 65 73 29 20 6f arger entries) o
1101c 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 r list */. stru
1101d 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
1101e 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66 pLeft; /* Lef
1101f 74 20 73 75 62 74 72 65 65 20 28 73 6d 61 6c 6c t subtree (small
11020 65 72 20 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d er entries) */.}
11021 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45 ;../*.** RowSetE
11022 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 61 72 65 ntry objects are
11023 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61 allocated in la
11024 72 67 65 20 63 68 75 6e 6b 73 20 28 69 6e 73 74 rge chunks (inst
11025 61 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 ances of the.**
11026 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
11027 75 72 65 29 20 74 6f 20 72 65 64 75 63 65 20 6d ure) to reduce m
11028 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
11029 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 65 0a overhead. The.
1102a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 6b 65 ** chunks are ke
1102b 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c pt on a linked l
1102c 69 73 74 20 73 6f 20 74 68 61 74 20 74 68 65 79 ist so that they
1102d 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61 can be dealloca
1102e 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ted.** when the
1102f 52 6f 77 53 65 74 20 69 73 20 64 65 73 74 72 6f RowSet is destro
11030 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 yed..*/.struct R
11031 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73 owSetChunk {. s
11032 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e truct RowSetChun
11033 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20 k *pNextChunk;
11034 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 /* Next ch
11035 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74 unk on list of t
11036 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72 hem all */. str
11037 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
11038 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e aEntry[ROWSET_EN
11039 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20 TRY_PER_CHUNK];
1103a 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74 /* Allocated ent
1103b 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ries */.};../*.*
1103c 2a 20 41 20 52 6f 77 53 65 74 20 69 6e 20 61 6e * A RowSet in an
1103d 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
1103e 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
1103f 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79 ture..**.** A ty
11040 70 65 64 65 66 20 6f 66 20 74 68 69 73 20 73 74 pedef of this st
11041 72 75 63 74 75 72 65 20 69 66 20 66 6f 75 6e 64 ructure if found
11042 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e in sqliteInt.h.
11043 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65 .*/.struct RowSe
11044 74 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 t {. struct Row
11045 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b SetChunk *pChunk
11046 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 ; /* List of
11047 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61 all chunk alloca
11048 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74 tions */. sqlit
11049 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 e3 *db;
1104a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1104b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1104c 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 tion */. struct
1104d 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 RowSetEntry *pE
1104e 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74 ntry; /* List
1104f 20 6f 66 20 65 6e 74 72 69 65 73 20 75 73 69 6e of entries usin
11050 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 73 74 g pRight */. st
11051 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11052 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 *pLast; /*
11053 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 Last entry on th
11054 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f e pEntry list */
11055 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
11056 45 6e 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20 Entry *pFresh;
11057 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e /* Source of n
11058 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73 ew entry objects
11059 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 */. struct Row
1105a 53 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b SetEntry *pTree;
1105b 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74 /* Binary t
1105c 72 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a ree of entries *
1105d 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20 /. u16 nFresh;
1105e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1105f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
11060 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73 objects on pFres
11061 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74 h */. u8 isSort
11062 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
11063 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
11064 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74 65 pEntry is sorte
11065 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 63 68 d */. u8 iBatch
11066 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11067 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 /* Current
11068 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 2a 2f insert batch */
11069 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 .};../*.** Turn
1106a 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f bulk memory into
1106b 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 a RowSet object
1106c 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 . N bytes of me
1106d 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69 mory.** are avai
1106e 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e lable at pSpace.
1106f 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72 The db pointer
11070 20 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65 is used as a me
11071 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 mory context.**
11072 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65 for any subseque
11073 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 nt allocations t
11074 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 hat need to occu
11075 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 r..** Return a p
11076 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
11077 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e w RowSet object.
11078 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62 .**.** It must b
11079 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 e the case that
1107a 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 N is sufficient
1107b 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74 to make a Rowset
1107c 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20 . If not.** an
1107d 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
1107e 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 occurs..** .** I
1107f 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68 f N is larger th
11080 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20 an the minimum,
11081 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20 use the surplus
11082 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a as an initial.**
11083 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65 allocation of e
11084 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65 ntries available
11085 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a to be filled..*
11086 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
11087 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33 RowSet *sqlite3
11088 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74 RowSetInit(sqlit
11089 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53 e3 *db, void *pS
1108a 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69 pace, unsigned i
1108b 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20 nt N){. RowSet
1108c 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20 *p;. assert( N
1108d 3e 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 >= ROUND8(sizeof
1108e 28 2a 70 29 29 20 29 3b 0a 20 20 70 20 3d 20 70 (*p)) );. p = p
1108f 53 70 61 63 65 3b 0a 20 20 70 2d 3e 70 43 68 75 Space;. p->pChu
11090 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 64 62 20 nk = 0;. p->db
11091 3d 20 64 62 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 = db;. p->pEntr
11092 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 y = 0;. p->pLas
11093 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 t = 0;. p->pTre
11094 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 65 e = 0;. p->pFre
11095 73 68 20 3d 20 28 73 74 72 75 63 74 20 52 6f 77 sh = (struct Row
11096 53 65 74 45 6e 74 72 79 2a 29 28 52 4f 55 4e 44 SetEntry*)(ROUND
11097 38 28 73 69 7a 65 6f 66 28 2a 70 29 29 20 2b 20 8(sizeof(*p)) +
11098 28 63 68 61 72 2a 29 70 29 3b 0a 20 20 70 2d 3e (char*)p);. p->
11099 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 28 28 nFresh = (u16)((
1109a 4e 20 2d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f N - ROUND8(sizeo
1109b 66 28 2a 70 29 29 29 2f 73 69 7a 65 6f 66 28 73 f(*p)))/sizeof(s
1109c 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
1109d 79 29 29 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 y));. p->isSort
1109e 65 64 20 3d 20 31 3b 0a 20 20 70 2d 3e 69 42 61 ed = 1;. p->iBa
1109f 74 63 68 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 tch = 0;. retur
110a0 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 n p;.}../*.** De
110a1 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63 68 75 allocate all chu
110a2 6e 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77 53 65 nks from a RowSe
110a3 74 2e 20 20 54 68 69 73 20 66 72 65 65 73 20 61 t. This frees a
110a4 6c 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74 0a 2a ll memory that.*
110a5 2a 20 74 68 65 20 52 6f 77 53 65 74 20 68 61 73 * the RowSet has
110a6 20 61 6c 6c 6f 63 61 74 65 64 20 6f 76 65 72 20 allocated over
110a7 69 74 73 20 6c 69 66 65 74 69 6d 65 2e 20 20 54 its lifetime. T
110a8 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a his routine is.*
110a9 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 * the destructor
110aa 20 66 6f 72 20 74 68 65 20 52 6f 77 53 65 74 2e for the RowSet.
110ab 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
110ac 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 TE void sqlite3R
110ad 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65 owSetClear(RowSe
110ae 74 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 74 20 t *p){. struct
110af 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 RowSetChunk *pCh
110b0 75 6e 6b 2c 20 2a 70 4e 65 78 74 43 68 75 6e 6b unk, *pNextChunk
110b1 3b 0a 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d 70 ;. for(pChunk=p
110b2 2d 3e 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b ->pChunk; pChunk
110b3 3b 20 70 43 68 75 6e 6b 20 3d 20 70 4e 65 78 74 ; pChunk = pNext
110b4 43 68 75 6e 6b 29 7b 0a 20 20 20 20 70 4e 65 78 Chunk){. pNex
110b5 74 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d tChunk = pChunk-
110b6 3e 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 20 >pNextChunk;.
110b7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
110b8 2d 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b 0a 20 ->db, pChunk);.
110b9 20 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d }. p->pChunk =
110ba 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 20 0;. p->nFresh
110bb 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 79 = 0;. p->pEntry
110bc 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 = 0;. p->pLast
110bd 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 65 = 0;. p->pTree
110be 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 = 0;. p->isSor
110bf 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a ted = 1;.}../*.*
110c0 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 76 * Insert a new v
110c1 61 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f 77 53 alue into a RowS
110c2 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 et..**.** The ma
110c3 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
110c4 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
110c5 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 73 65 connection is se
110c6 74 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 t if a.** memory
110c7 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
110c8 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
110c9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
110ca 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 3RowSetInsert(Ro
110cb 77 53 65 74 20 2a 70 2c 20 69 36 34 20 72 6f 77 wSet *p, i64 row
110cc 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f id){. struct Ro
110cd 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 wSetEntry *pEntr
110ce 79 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 65 y; /* The new e
110cf 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 ntry */. struct
110d0 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c RowSetEntry *pL
110d1 61 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 6c 61 ast; /* The la
110d2 73 74 20 70 72 69 6f 72 20 65 6e 74 72 79 20 2a st prior entry *
110d3 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 /. assert( p!=0
110d4 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 );. if( p->nFr
110d5 65 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 esh==0 ){. st
110d6 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b ruct RowSetChunk
110d7 20 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77 *pNew;. pNew
110d8 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
110d9 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a ocRaw(p->db, siz
110da 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20 eof(*pNew));.
110db 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a if( pNew==0 ){.
110dc 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
110dd 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e }. pNew->pN
110de 65 78 74 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 43 extChunk = p->pC
110df 68 75 6e 6b 3b 0a 20 20 20 20 70 2d 3e 70 43 68 hunk;. p->pCh
110e0 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 unk = pNew;.
110e1 70 2d 3e 70 46 72 65 73 68 20 3d 20 70 4e 65 77 p->pFresh = pNew
110e2 2d 3e 61 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d ->aEntry;. p-
110e3 3e 6e 46 72 65 73 68 20 3d 20 52 4f 57 53 45 54 >nFresh = ROWSET
110e4 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b _ENTRY_PER_CHUNK
110e5 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d ;. }. pEntry =
110e6 20 70 2d 3e 70 46 72 65 73 68 2b 2b 3b 0a 20 20 p->pFresh++;.
110e7 70 2d 3e 6e 46 72 65 73 68 2d 2d 3b 0a 20 20 70 p->nFresh--;. p
110e8 45 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77 69 64 Entry->v = rowid
110e9 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 67 ;. pEntry->pRig
110ea 68 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73 74 20 ht = 0;. pLast
110eb 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20 69 66 = p->pLast;. if
110ec 28 20 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 69 ( pLast ){. i
110ed 66 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 26 f( p->isSorted &
110ee 26 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74 2d 3e & rowid<=pLast->
110ef 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 v ){. p->is
110f0 53 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 Sorted = 0;.
110f1 7d 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70 52 69 }. pLast->pRi
110f2 67 68 74 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20 ght = pEntry;.
110f3 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
110f4 74 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 t( p->pEntry==0
110f5 29 3b 20 2f 2a 20 46 69 72 65 73 20 69 66 20 49 ); /* Fires if I
110f6 4e 53 45 52 54 20 61 66 74 65 72 20 53 4d 41 4c NSERT after SMAL
110f7 4c 45 53 54 20 2a 2f 0a 20 20 20 20 70 2d 3e 70 LEST */. p->p
110f8 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a Entry = pEntry;.
110f9 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d }. p->pLast =
110fa 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a pEntry;.}../*.*
110fb 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74 * Merge two list
110fc 73 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 s of RowSetEntry
110fd 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d 6f 76 objects. Remov
110fe 65 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a e duplicates..**
110ff 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 6c 69 .** The input li
11100 73 74 73 20 61 72 65 20 63 6f 6e 6e 65 63 74 65 sts are connecte
11101 64 20 76 69 61 20 70 52 69 67 68 74 20 70 6f 69 d via pRight poi
11102 6e 74 65 72 73 20 61 6e 64 20 61 72 65 20 0a 2a nters and are .*
11103 2a 20 61 73 73 75 6d 65 64 20 74 6f 20 65 61 63 * assumed to eac
11104 68 20 61 6c 72 65 61 64 79 20 62 65 20 69 6e 20 h already be in
11105 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2f sorted order..*/
11106 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52 .static struct R
11107 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 owSetEntry *rowS
11108 65 74 4d 65 72 67 65 28 0a 20 20 73 74 72 75 63 etMerge(. struc
11109 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
1110a 41 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 73 A, /* First s
1110b 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20 62 65 orted list to be
1110c 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 73 74 72 merged */. str
1110d 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
1110e 2a 70 42 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e *pB /* Secon
1110f 64 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f d sorted list to
11110 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 29 7b be merged */.){
11111 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
11112 45 6e 74 72 79 20 68 65 61 64 3b 0a 20 20 73 74 Entry head;. st
11113 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11114 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 54 61 69 *pTail;.. pTai
11115 6c 20 3d 20 26 68 65 61 64 3b 0a 20 20 77 68 69 l = &head;. whi
11116 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b 0a le( pA && pB ){.
11117 20 20 20 20 61 73 73 65 72 74 28 20 70 41 2d 3e assert( pA->
11118 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 41 2d pRight==0 || pA-
11119 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 2d 3e >v<=pA->pRight->
1111a 76 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 v );. assert(
1111b 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c pB->pRight==0 |
1111c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 | pB->v<=pB->pRi
1111d 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 69 66 ght->v );. if
1111e 28 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 29 7b ( pA->v<pB->v ){
1111f 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 . pTail->pR
11120 69 67 68 74 20 3d 20 70 41 3b 0a 20 20 20 20 20 ight = pA;.
11121 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 pA = pA->pRight
11122 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 ;. pTail =
11123 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 pTail->pRight;.
11124 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d }else if( pB-
11125 3e 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 20 20 >v<pA->v ){.
11126 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 pTail->pRight
11127 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42 20 3d = pB;. pB =
11128 20 70 42 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 pB->pRight;.
11129 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c pTail = pTail
1112a 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 ->pRight;. }e
1112b 6c 73 65 7b 0a 20 20 20 20 20 20 70 41 20 3d 20 lse{. pA =
1112c 70 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 pA->pRight;.
1112d 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 20 29 }. }. if( pA )
1112e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41 {. assert( pA
1112f 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 ->pRight==0 || p
11130 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 A->v<=pA->pRight
11131 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c ->v );. pTail
11132 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20 ->pRight = pA;.
11133 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
11134 72 74 28 20 70 42 3d 3d 30 20 7c 7c 20 70 42 2d rt( pB==0 || pB-
11135 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42 >pRight==0 || pB
11136 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d ->v<=pB->pRight-
11137 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c 2d >v );. pTail-
11138 3e 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 >pRight = pB;.
11139 7d 0a 20 20 72 65 74 75 72 6e 20 68 65 61 64 2e }. return head.
1113a 70 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pRight;.}../*.**
1113b 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d 65 6e Sort all elemen
1113c 74 73 20 6f 6e 20 74 68 65 20 70 45 6e 74 72 79 ts on the pEntry
1113d 20 6c 69 73 74 20 6f 66 20 74 68 65 20 52 6f 77 list of the Row
1113e 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 69 Set into ascendi
1113f 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 74 ng order..*/ .st
11140 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 atic void rowSet
11141 53 6f 72 74 28 52 6f 77 53 65 74 20 2a 70 29 7b Sort(RowSet *p){
11142 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
11143 69 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 i;. struct RowS
11144 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b etEntry *pEntry;
11145 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
11146 45 6e 74 72 79 20 2a 61 42 75 63 6b 65 74 5b 34 Entry *aBucket[4
11147 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 0];.. assert( p
11148 2d 3e 69 73 53 6f 72 74 65 64 3d 3d 30 20 29 3b ->isSorted==0 );
11149 0a 20 20 6d 65 6d 73 65 74 28 61 42 75 63 6b 65 . memset(aBucke
1114a 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 42 75 t, 0, sizeof(aBu
1114b 63 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c 65 28 cket));. while(
1114c 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 p->pEntry ){.
1114d 20 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 pEntry = p->pE
1114e 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e 70 45 6e ntry;. p->pEn
1114f 74 72 79 20 3d 20 70 45 6e 74 72 79 2d 3e 70 52 try = pEntry->pR
11150 69 67 68 74 3b 0a 20 20 20 20 70 45 6e 74 72 79 ight;. pEntry
11151 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 ->pRight = 0;.
11152 20 20 66 6f 72 28 69 3d 30 3b 20 61 42 75 63 6b for(i=0; aBuck
11153 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 et[i]; i++){.
11154 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 pEntry = rowS
11155 65 74 4d 65 72 67 65 28 61 42 75 63 6b 65 74 5b etMerge(aBucket[
11156 69 5d 2c 20 70 45 6e 74 72 79 29 3b 0a 20 20 20 i], pEntry);.
11157 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 aBucket[i] =
11158 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 42 75 0;. }. aBu
11159 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e 74 72 79 cket[i] = pEntry
1115a 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d ;. }. pEntry =
1115b 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
1115c 3c 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29 <sizeof(aBucket)
1115d 2f 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 5b /sizeof(aBucket[
1115e 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 0]); i++){. p
1115f 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 Entry = rowSetMe
11160 72 67 65 28 70 45 6e 74 72 79 2c 20 61 42 75 63 rge(pEntry, aBuc
11161 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 ket[i]);. }. p
11162 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 ->pEntry = pEntr
11163 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20 y;. p->pLast =
11164 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64 0;. p->isSorted
11165 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 = 1;.}.../*.**
11166 54 68 65 20 69 6e 70 75 74 2c 20 70 49 6e 2c 20 The input, pIn,
11167 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65 is a binary tree
11168 20 28 6f 72 20 73 75 62 74 72 65 65 29 20 6f 66 (or subtree) of
11169 20 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a RowSetEntry obj
1116a 65 63 74 73 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 ects..** Convert
1116b 20 74 68 69 73 20 74 72 65 65 20 69 6e 74 6f 20 this tree into
1116c 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 63 6f a linked list co
1116d 6e 6e 65 63 74 65 64 20 62 79 20 74 68 65 20 70 nnected by the p
1116e 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 0a 2a Right pointers.*
1116f 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 70 6f 69 * and return poi
11170 6e 74 65 72 73 20 74 6f 20 74 68 65 20 66 69 72 nters to the fir
11171 73 74 20 61 6e 64 20 6c 61 73 74 20 65 6c 65 6d st and last elem
11172 65 6e 74 73 20 6f 66 20 74 68 65 20 6e 65 77 20 ents of the new
11173 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 list..*/.static
11174 76 6f 69 64 20 72 6f 77 53 65 74 54 72 65 65 54 void rowSetTreeT
11175 6f 4c 69 73 74 28 0a 20 20 73 74 72 75 63 74 20 oList(. struct
11176 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 49 6e RowSetEntry *pIn
11177 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f , /* Roo
11178 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74 t of the input t
11179 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ree */. struct
1117a 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 RowSetEntry **pp
1117b 46 69 72 73 74 2c 20 20 20 20 2f 2a 20 57 72 69 First, /* Wri
1117c 74 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 6f te head of the o
1117d 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 20 utput list here
1117e 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 */. struct RowS
1117f 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c 61 73 74 etEntry **ppLast
11180 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
11181 61 69 6c 20 6f 66 20 74 68 65 20 6f 75 74 70 75 ail of the outpu
11182 74 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 t list here */.)
11183 7b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 21 {. assert( pIn!
11184 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 2d =0 );. if( pIn-
11185 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 74 >pLeft ){. st
11186 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11187 20 2a 70 3b 0a 20 20 20 20 72 6f 77 53 65 74 54 *p;. rowSetT
11188 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 reeToList(pIn->p
11189 4c 65 66 74 2c 20 70 70 46 69 72 73 74 2c 20 26 Left, ppFirst, &
1118a 70 29 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 p);. p->pRigh
1118b 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 65 6c 73 65 t = pIn;. }else
1118c 7b 0a 20 20 20 20 2a 70 70 46 69 72 73 74 20 3d {. *ppFirst =
1118d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 pIn;. }. if(
1118e 70 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 pIn->pRight ){.
1118f 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c rowSetTreeToL
11190 69 73 74 28 70 49 6e 2d 3e 70 52 69 67 68 74 2c ist(pIn->pRight,
11191 20 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c 20 70 &pIn->pRight, p
11192 70 4c 61 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b pLast);. }else{
11193 0a 20 20 20 20 2a 70 70 4c 61 73 74 20 3d 20 70 . *ppLast = p
11194 49 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 In;. }. assert
11195 28 20 28 2a 70 70 4c 61 73 74 29 2d 3e 70 52 69 ( (*ppLast)->pRi
11196 67 68 74 3d 3d 30 20 29 3b 0a 7d 0a 0a 0a 2f 2a ght==0 );.}.../*
11197 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f .** Convert a so
11198 72 74 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65 rted list of ele
11199 6d 65 6e 74 73 20 28 63 6f 6e 6e 65 63 74 65 64 ments (connected
1119a 20 62 79 20 70 52 69 67 68 74 29 20 69 6e 74 6f by pRight) into
1119b 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 74 72 65 a binary.** tre
1119c 65 20 77 69 74 68 20 64 65 70 74 68 20 6f 66 20 e with depth of
1119d 69 44 65 70 74 68 2e 20 20 41 20 64 65 70 74 68 iDepth. A depth
1119e 20 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 65 20 of 1 means the
1119f 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 tree contains a
111a0 73 69 6e 67 6c 65 0a 2a 2a 20 6e 6f 64 65 20 74 single.** node t
111a1 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 68 65 aken from the he
111a2 61 64 20 6f 66 20 2a 70 70 4c 69 73 74 2e 20 20 ad of *ppList.
111a3 41 20 64 65 70 74 68 20 6f 66 20 32 20 6d 65 61 A depth of 2 mea
111a4 6e 73 20 61 20 74 72 65 65 20 77 69 74 68 0a 2a ns a tree with.*
111a5 2a 20 74 68 72 65 65 20 6e 6f 64 65 73 2e 20 20 * three nodes.
111a6 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a And so forth..**
111a7 0a 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e 79 20 .** Use as many
111a8 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65 entries from the
111a9 20 69 6e 70 75 74 20 6c 69 73 74 20 61 73 20 72 input list as r
111aa 65 71 75 69 72 65 64 20 61 6e 64 20 75 70 64 61 equired and upda
111ab 74 65 20 74 68 65 0a 2a 2a 20 2a 70 70 4c 69 73 te the.** *ppLis
111ac 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 t to point to th
111ad 65 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 6e 74 e unused element
111ae 73 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 20 20 s of the list.
111af 49 66 20 74 68 65 20 69 6e 70 75 74 0a 2a 2a 20 If the input.**
111b0 6c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f list contains to
111b1 6f 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 2c 20 o few elements,
111b2 74 68 65 6e 20 63 6f 6e 73 74 72 75 63 74 20 61 then construct a
111b3 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74 72 65 n incomplete tre
111b4 65 0a 2a 2a 20 61 6e 64 20 6c 65 61 76 65 20 2a e.** and leave *
111b5 70 70 4c 69 73 74 20 73 65 74 20 74 6f 20 4e 55 ppList set to NU
111b6 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e LL..**.** Return
111b7 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
111b8 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 63 6f e root of the co
111b9 6e 73 74 72 75 63 74 65 64 20 62 69 6e 61 72 79 nstructed binary
111ba 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 tree..*/.static
111bb 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
111bc 74 72 79 20 2a 72 6f 77 53 65 74 4e 44 65 65 70 try *rowSetNDeep
111bd 54 72 65 65 28 0a 20 20 73 74 72 75 63 74 20 52 Tree(. struct R
111be 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c owSetEntry **ppL
111bf 69 73 74 2c 0a 20 20 69 6e 74 20 69 44 65 70 74 ist,. int iDept
111c0 68 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f h.){. struct Ro
111c1 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20 wSetEntry *p;
111c2 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66 /* Root of
111c3 20 74 68 65 20 6e 65 77 20 74 72 65 65 20 2a 2f the new tree */
111c4 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
111c5 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 Entry *pLeft;
111c6 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 /* Left subtre
111c7 65 20 2a 2f 0a 20 20 69 66 28 20 2a 70 70 4c 69 e */. if( *ppLi
111c8 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 st==0 ){. ret
111c9 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 urn 0;. }. if(
111ca 20 69 44 65 70 74 68 3d 3d 31 20 29 7b 0a 20 20 iDepth==1 ){.
111cb 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20 p = *ppList;.
111cc 20 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e *ppList = p->
111cd 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 pRight;. p->p
111ce 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 Left = p->pRight
111cf 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
111d0 20 70 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 74 20 p;. }. pLeft
111d1 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 = rowSetNDeepTre
111d2 65 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 e(ppList, iDepth
111d3 2d 31 29 3b 0a 20 20 70 20 3d 20 2a 70 70 4c 69 -1);. p = *ppLi
111d4 73 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 st;. if( p==0 )
111d5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 {. return pLe
111d6 66 74 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 65 ft;. }. p->pLe
111d7 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 2a 70 ft = pLeft;. *p
111d8 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 pList = p->pRigh
111d9 74 3b 0a 20 20 70 2d 3e 70 52 69 67 68 74 20 3d t;. p->pRight =
111da 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 rowSetNDeepTree
111db 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 2d (ppList, iDepth-
111dc 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 1);. return p;.
111dd 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
111de 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f a sorted list o
111df 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74 6f 20 f elements into
111e0 61 20 62 69 6e 61 72 79 20 74 72 65 65 2e 20 4d a binary tree. M
111e1 61 6b 65 20 74 68 65 20 74 72 65 65 0a 2a 2a 20 ake the tree.**
111e2 61 73 20 64 65 65 70 20 61 73 20 69 74 20 6e 65 as deep as it ne
111e3 65 64 73 20 74 6f 20 62 65 20 69 6e 20 6f 72 64 eds to be in ord
111e4 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 er to contain th
111e5 65 20 65 6e 74 69 72 65 20 6c 69 73 74 2e 0a 2a e entire list..*
111e6 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 /.static struct
111e7 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 RowSetEntry *row
111e8 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 73 74 SetListToTree(st
111e9 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
111ea 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 *pList){. int
111eb 69 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 iDepth;
111ec 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 74 68 /* Depth of th
111ed 65 20 74 72 65 65 20 73 6f 20 66 61 72 20 2a 2f e tree so far */
111ee 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
111ef 45 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 20 20 Entry *p;
111f0 2f 2a 20 43 75 72 72 65 6e 74 20 74 72 65 65 20 /* Current tree
111f1 72 6f 6f 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 root */. struct
111f2 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c RowSetEntry *pL
111f3 65 66 74 3b 20 20 20 2f 2a 20 4c 65 66 74 20 73 eft; /* Left s
111f4 75 62 74 72 65 65 20 2a 2f 0a 0a 20 20 61 73 73 ubtree */.. ass
111f5 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b ert( pList!=0 );
111f6 0a 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 20 . p = pList;.
111f7 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68 pList = p->pRigh
111f8 74 3b 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20 t;. p->pLeft =
111f9 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 p->pRight = 0;.
111fa 20 66 6f 72 28 69 44 65 70 74 68 3d 31 3b 20 70 for(iDepth=1; p
111fb 4c 69 73 74 3b 20 69 44 65 70 74 68 2b 2b 29 7b List; iDepth++){
111fc 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 3b 0a . pLeft = p;.
111fd 20 20 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 p = pList;.
111fe 20 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 pList = p->pR
111ff 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 4c 65 ight;. p->pLe
11200 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 20 20 ft = pLeft;.
11201 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53 p->pRight = rowS
11202 65 74 4e 44 65 65 70 54 72 65 65 28 26 70 4c 69 etNDeepTree(&pLi
11203 73 74 2c 20 69 44 65 70 74 68 29 3b 0a 20 20 7d st, iDepth);. }
11204 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
11205 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 /*.** Convert th
11206 65 20 6c 69 73 74 20 69 6e 20 70 2d 3e 70 45 6e e list in p->pEn
11207 74 72 79 20 69 6e 74 6f 20 61 20 73 6f 72 74 65 try into a sorte
11208 64 20 6c 69 73 74 20 69 66 20 69 74 20 69 73 20 d list if it is
11209 6e 6f 74 0a 2a 2a 20 73 6f 72 74 65 64 20 61 6c not.** sorted al
1120a 72 65 61 64 79 2e 20 20 49 66 20 74 68 65 72 65 ready. If there
1120b 20 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 is a binary tre
1120c 65 20 6f 6e 20 70 2d 3e 70 54 72 65 65 2c 20 74 e on p->pTree, t
1120d 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 69 hen.** convert i
1120e 74 20 69 6e 74 6f 20 61 20 6c 69 73 74 20 74 6f t into a list to
1120f 6f 20 61 6e 64 20 6d 65 72 67 65 20 69 74 20 69 o and merge it i
11210 6e 74 6f 20 74 68 65 20 70 2d 3e 70 45 6e 74 72 nto the p->pEntr
11211 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 y list..*/.stati
11212 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54 6f 4c c void rowSetToL
11213 69 73 74 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a ist(RowSet *p){.
11214 20 20 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74 if( !p->isSort
11215 65 64 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 ed ){. rowSet
11216 53 6f 72 74 28 70 29 3b 0a 20 20 7d 0a 20 20 69 Sort(p);. }. i
11217 66 28 20 70 2d 3e 70 54 72 65 65 20 29 7b 0a 20 f( p->pTree ){.
11218 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 struct RowSet
11219 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 2a 70 Entry *pHead, *p
1121a 54 61 69 6c 3b 0a 20 20 20 20 72 6f 77 53 65 74 Tail;. rowSet
1121b 54 72 65 65 54 6f 4c 69 73 74 28 70 2d 3e 70 54 TreeToList(p->pT
1121c 72 65 65 2c 20 26 70 48 65 61 64 2c 20 26 70 54 ree, &pHead, &pT
1121d 61 69 6c 29 3b 0a 20 20 20 20 70 2d 3e 70 54 72 ail);. p->pTr
1121e 65 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 ee = 0;. p->p
1121f 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65 Entry = rowSetMe
11220 72 67 65 28 70 2d 3e 70 45 6e 74 72 79 2c 20 70 rge(p->pEntry, p
11221 48 65 61 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a Head);. }.}../*
11222 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 .** Extract the
11223 73 6d 61 6c 6c 65 73 74 20 65 6c 65 6d 65 6e 74 smallest element
11224 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74 from the RowSet
11225 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65 ..** Write the e
11226 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 2a 70 52 6f lement into *pRo
11227 77 69 64 2e 20 20 52 65 74 75 72 6e 20 31 20 6f wid. Return 1 o
11228 6e 20 73 75 63 63 65 73 73 2e 20 20 52 65 74 75 n success. Retu
11229 72 6e 0a 2a 2a 20 30 20 69 66 20 74 68 65 20 52 rn.** 0 if the R
1122a 6f 77 53 65 74 20 69 73 20 61 6c 72 65 61 64 79 owSet is already
1122b 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 66 empty..**.** Af
1122c 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
1122d 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 has been called
1122e 2c 20 74 68 65 20 73 71 6c 69 74 65 33 52 6f 77 , the sqlite3Row
1122f 53 65 74 49 6e 73 65 72 74 28 29 0a 2a 2a 20 72 SetInsert().** r
11230 6f 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74 20 62 outine may not b
11231 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 2e 20 e called again.
11232 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
11233 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
11234 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74 owSetNext(RowSet
11235 20 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77 69 64 *p, i64 *pRowid
11236 29 7b 0a 20 20 72 6f 77 53 65 74 54 6f 4c 69 73 ){. rowSetToLis
11237 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 t(p);. if( p->p
11238 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a 70 52 Entry ){. *pR
11239 6f 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 72 79 owid = p->pEntry
1123a 2d 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74 ->v;. p->pEnt
1123b 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e ry = p->pEntry->
1123c 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20 pRight;. if(
1123d 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29 7b 0a p->pEntry==0 ){.
1123e 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 sqlite3Row
1123f 53 65 74 43 6c 65 61 72 28 70 29 3b 0a 20 20 20 SetClear(p);.
11240 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b }. return 1;
11241 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
11242 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 0;. }.}../
11243 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 *.** Check to se
11244 65 20 69 66 20 65 6c 65 6d 65 6e 74 20 69 52 6f e if element iRo
11245 77 69 64 20 77 61 73 20 69 6e 73 65 72 74 65 64 wid was inserted
11246 20 69 6e 74 6f 20 74 68 65 20 74 68 65 20 72 6f into the the ro
11247 77 73 65 74 20 61 73 0a 2a 2a 20 70 61 72 74 20 wset as.** part
11248 6f 66 20 61 6e 79 20 69 6e 73 65 72 74 20 62 61 of any insert ba
11249 74 63 68 20 70 72 69 6f 72 20 74 6f 20 69 42 61 tch prior to iBa
1124a 74 63 68 2e 20 20 52 65 74 75 72 6e 20 31 20 6f tch. Return 1 o
1124b 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 r 0..*/.SQLITE_P
1124c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1124d 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 e3RowSetTest(Row
1124e 53 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 75 38 Set *pRowSet, u8
1124f 20 69 42 61 74 63 68 2c 20 73 71 6c 69 74 65 33 iBatch, sqlite3
11250 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a _int64 iRowid){.
11251 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11252 6e 74 72 79 20 2a 70 3b 0a 20 20 69 66 28 20 69 ntry *p;. if( i
11253 42 61 74 63 68 21 3d 70 52 6f 77 53 65 74 2d 3e Batch!=pRowSet->
11254 69 42 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66 iBatch ){. if
11255 28 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 ( pRowSet->pEntr
11256 79 20 29 7b 0a 20 20 20 20 20 20 72 6f 77 53 65 y ){. rowSe
11257 74 54 6f 4c 69 73 74 28 70 52 6f 77 53 65 74 29 tToList(pRowSet)
11258 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d ;. pRowSet-
11259 3e 70 54 72 65 65 20 3d 20 72 6f 77 53 65 74 4c >pTree = rowSetL
1125a 69 73 74 54 6f 54 72 65 65 28 70 52 6f 77 53 65 istToTree(pRowSe
1125b 74 2d 3e 70 45 6e 74 72 79 29 3b 0a 20 20 20 20 t->pEntry);.
1125c 20 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 pRowSet->pEntr
1125d 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 52 6f y = 0;. pRo
1125e 77 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20 30 3b wSet->pLast = 0;
1125f 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 6f 77 53 . }. pRowS
11260 65 74 2d 3e 69 42 61 74 63 68 20 3d 20 69 42 61 et->iBatch = iBa
11261 74 63 68 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 tch;. }. p = p
11262 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 3b 0a 20 RowSet->pTree;.
11263 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 while( p ){.
11264 20 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64 if( p->v<iRowid
11265 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d ){. p = p-
11266 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c >pRight;. }el
11267 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52 6f 77 se if( p->v>iRow
11268 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 id ){. p =
11269 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 65 p->pLeft;. }e
1126a 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 lse{. retur
1126b 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
1126c 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
1126d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1126e 64 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a d of rowset.c **
1126f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11271 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
11272 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
11273 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63 gin file pager.c
11274 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
11275 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11276 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
11277 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
11278 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
11279 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
1127a 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
1127b 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
1127c 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
1127d 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
1127e 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
1127f 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
11280 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
11281 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
11282 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
11283 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
11284 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
11285 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
11286 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
11287 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
11288 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
11289 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
1128a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1128b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1128c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1128d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1128e 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ****.** This is
1128f 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
11290 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 on of the page c
11291 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f ache subsystem o
11292 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a r "pager"..** .*
11293 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75 * The pager is u
11294 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20 sed to access a
11295 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69 database disk fi
11296 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e le. It implemen
11297 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d ts.** atomic com
11298 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b mit and rollback
11299 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65 through the use
1129a 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 of a journal fi
1129b 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65 le that.** is se
1129c 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20 parate from the
1129d 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
1129e 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69 The pager also i
1129f 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a mplements file.*
112a0 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65 * locking to pre
112a1 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73 vent two process
112a2 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 es from writing
112a3 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
112a4 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74 e.** file simult
112a5 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65 aneously, or one
112a6 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65 process from re
112a7 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 ading the databa
112a8 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74 se while.** anot
112a9 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a her is writing..
112aa 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
112ab 70 61 67 65 72 2e 63 2c 76 20 31 2e 36 32 39 20 pager.c,v 1.629
112ac 32 30 30 39 2f 30 38 2f 31 30 20 31 37 3a 34 38 2009/08/10 17:48
112ad 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :57 drh Exp $.*/
112ae 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
112af 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a OMIT_DISKIO../*.
112b0 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 72 ** Macros for tr
112b1 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 20 oubleshooting.
112b2 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 Normally turned
112b3 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 off.*/.#if 0.int
112b4 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 61 sqlite3PagerTra
112b5 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 ce=1; /* True t
112b6 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 o enable tracing
112b7 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 */.#define sqli
112b8 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 70 te3DebugPrintf p
112b9 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41 rintf.#define PA
112ba 47 45 52 54 52 41 43 45 28 58 29 20 20 20 20 20 GERTRACE(X)
112bb 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 if( sqlite3Pager
112bc 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33 Trace ){ sqlite3
112bd 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20 7d DebugPrintf X; }
112be 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50 .#else.#define P
112bf 41 47 45 52 54 52 41 43 45 28 58 29 0a 23 65 6e AGERTRACE(X).#en
112c0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
112c1 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63 ollowing two mac
112c2 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 74 ros are used wit
112c3 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 41 hin the PAGERTRA
112c4 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76 CE() macros abov
112c5 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75 e.** to print ou
112c6 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f t file-descripto
112c7 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52 rs. .**.** PAGER
112c8 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f 69 ID() takes a poi
112c9 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 nter to a Pager
112ca 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 struct as its ar
112cb 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 61 gument. The.** a
112cc 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d 64 ssociated file-d
112cd 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 74 escriptor is ret
112ce 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 4c urned. FILEHANDL
112cf 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 73 EID() takes an s
112d0 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 73 qlite3_file.** s
112d1 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67 truct as its arg
112d2 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e ument..*/.#defin
112d3 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28 69 e PAGERID(p) ((i
112d4 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 66 nt)(p->fd)).#def
112d5 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 44 ine FILEHANDLEID
112d6 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a 0a (fd) ((int)fd)..
112d7 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63 /*.** The page c
112d8 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 20 ache as a whole
112d9 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e 65 is always in one
112da 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
112db 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a 0a g.** states:.**.
112dc 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f 43 ** PAGER_UNLOC
112dd 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67 K The pag
112de 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 63 e cache is not c
112df 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e 67 urrently reading
112e0 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 or .**
112e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 72 wr
112e2 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 iting the databa
112e3 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 20 se file. There
112e4 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 is no.**
112e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
112e6 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f ata held in memo
112e7 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 ry. This is the
112e8 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 20 initial.**
112e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112ea 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 state..**.**
112eb 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 20 PAGER_SHARED
112ec 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61 The page ca
112ed 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 74 che is reading t
112ee 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 he database..**
112ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112f0 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 73 Writing is
112f1 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e 20 not permitted.
112f2 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a There can be.**
112f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
112f4 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 20 multiple
112f5 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69 6e readers accessin
112f6 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 g the same datab
112f7 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ase.**
112f8 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c fil
112f9 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 e at the same ti
112fa 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 me..**.** PAGE
112fb 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 R_RESERVED
112fc 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 73 This process has
112fd 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64 61 reserved the da
112fe 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 69 tabase for writi
112ff 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ng.**
11300 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74 20 but
11301 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 has not yet made
11302 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 4f any changes. O
11303 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 0a nly one process.
11304 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
11305 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74 69 at a ti
11306 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 74 me can reserve t
11307 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
11308 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 e original.**
11309 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1130a 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c database fil
1130b 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 6d e has not been m
1130c 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 72 odified so other
1130d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1130e 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 73 proces
1130f 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 ses may still be
11310 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e 2d reading the on-
11311 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 disk.**
11312 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61 da
11313 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
11314 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c 55 ** PAGER_EXCLU
11315 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61 67 SIVE The pag
11316 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74 69 e cache is writi
11317 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ng the database.
11318 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
11319 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73 73 Access
1131a 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 is exclusive.
1131b 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 No other process
1131c 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 es or.**
1131d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 t
1131e 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 65 hreads can be re
1131f 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 ading or writing
11320 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20 while one.**
11321 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11322 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 77 process is w
11323 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 riting..**.**
11324 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20 PAGER_SYNCED
11325 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d 6f The pager mo
11326 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 74 ves to this stat
11327 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 43 e from PAGER_EXC
11328 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 20 LUSIVE.**
11329 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1132a 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 20 after all dirty
1132b 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 pages have been
1132c 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a written to the.*
1132d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
1132e 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 database
1132f 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69 file and the fi
11330 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e 63 le has been sync
11331 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 ed to.**
11332 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
11333 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 65 isk. All that re
11334 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 74 mains to do is t
11335 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20 o remove or.**
11336 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11337 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 68 truncate th
11338 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 e journal file a
11339 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 nd the transacti
1133a 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 on .**
1133b 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 6c wil
1133c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a l be committed..
1133d 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63 **.** The page c
1133e 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 6e ache comes up in
1133f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 PAGER_UNLOCK.
11340 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 The first time a
11341 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 .** sqlite3Pager
11342 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 68 Get() occurs, th
11343 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 e state transiti
11344 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41 ons to PAGER_SHA
11345 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 6c RED..** After al
11346 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 l pages have bee
11347 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 n released using
11348 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e 72 sqlite_page_unr
11349 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 61 ef(),.** the sta
1134a 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62 te transitions b
1134b 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e 4c ack to PAGER_UNL
1134c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 20 OCK. The first
1134d 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 6c time.** that sql
1134e 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 ite3PagerWrite()
1134f 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 is called, the
11350 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e state transition
11351 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 45 s to.** PAGER_RE
11352 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 74 SERVED. (Note t
11353 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 hat sqlite3Pager
11354 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c 79 Write() can only
11355 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e be.** called on
11356 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 an outstanding
11357 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e 73 page which means
11358 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 that the pager
11359 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 41 must.** be in PA
1135a 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f 72 GER_SHARED befor
1135b 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e 73 e it transitions
1135c 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 56 to PAGER_RESERV
1135d 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 45 ED.).** PAGER_RE
1135e 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 61 SERVED means tha
1135f 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f 70 t there is an op
11360 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 en rollback jour
11361 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 6e nal..** The tran
11362 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 5f sition to PAGER_
11363 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 73 EXCLUSIVE occurs
11364 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e before any chan
11365 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 ges.** are made
11366 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
11367 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 69 file, though wri
11368 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 tes to the rollb
11369 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f ack.** journal o
1136a 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 20 ccurs with just
1136b 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 PAGER_RESERVED.
1136c 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 65 After an sqlite
1136d 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 3PagerRollback()
1136e 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 61 .** or sqlite3Pa
1136f 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 gerCommitPhaseTw
11370 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 63 o(), the state c
11371 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 41 an go back to PA
11372 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 6f GER_SHARED,.** o
11373 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 74 r it can stay at
11374 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
11375 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 78 if we are in ex
11376 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 6d clusive access m
11377 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ode..*/.#define
11378 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 PAGER_UNLOCK
11379 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45 0.#define PAGE
1137a 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 20 R_SHARED 1
1137b 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 41 /* same as SHA
1137c 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 RED_LOCK */.#def
1137d 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 56 ine PAGER_RESERV
1137e 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 6d ED 2 /* sam
1137f 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c 4f e as RESERVED_LO
11380 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 CK */.#define PA
11381 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 GER_EXCLUSIVE
11382 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 45 4 /* same as E
11383 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f XCLUSIVE_LOCK */
11384 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53 .#define PAGER_S
11385 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f 2a YNCED 5../*
11386 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 .** A macro used
11387 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68 for invoking th
11388 65 20 63 6f 64 65 63 20 69 66 20 74 68 65 72 65 e codec if there
11389 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65 is one.*/.#ifde
1138a 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 f SQLITE_HAS_COD
1138b 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 EC.# define CODE
1138c 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c 0a C1(P,D,N,X,E) \.
1138d 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65 if( P->xCode
1138e 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 50 c && P->xCodec(P
1138f 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 3d ->pCodec,D,N,X)=
11390 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65 66 =0 ){ E; }.# def
11391 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e ine CODEC2(P,D,N
11392 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69 66 ,X,E,O) \. if
11393 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20 29 ( P->xCodec==0 )
11394 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d 65 { O=(char*)D; }e
11395 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20 28 4f lse \. if( (O
11396 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64 =(char*)(P->xCod
11397 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e ec(P->pCodec,D,N
11398 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20 7d ,X)))==0 ){ E; }
11399 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
1139a 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 CODEC1(P,D,N,X,E
1139b 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a ) /* NO-OP */.
1139c 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 # define CODEC2(
1139d 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d 28 P,D,N,X,E,O) O=(
1139e 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a 0a char*)D.#endif..
1139f 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 /*.** The maximu
113a0 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f 72 m allowed sector
113a1 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49 66 size. 64KiB. If
113a2 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 7a 65 the xSectorsize
113a3 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72 65 () method .** re
113a4 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 6c 61 turns a value la
113a5 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 rger than this,
113a6 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f then MAX_SECTOR_
113a7 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 6e 73 SIZE is used ins
113a8 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63 6f tead..** This co
113a9 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c 79 20 uld conceivably
113aa 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 6f 6e cause corruption
113ab 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f 77 following a pow
113ac 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a 2a er failure on.**
113ad 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 2e 20 such a system.
113ae 54 68 69 73 20 69 73 20 63 75 72 72 65 6e 74 6c This is currentl
113af 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 y an undocumente
113b0 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66 d limit..*/.#def
113b1 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 ine MAX_SECTOR_S
113b2 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a 0a IZE 0x10000../*.
113b3 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
113b4 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
113b5 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c structure is all
113b6 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 ocated for each
113b7 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65 70 6f active.** savepo
113b8 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d 65 6e int and statemen
113b9 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e t transaction in
113ba 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41 6c 6c the system. All
113bb 20 73 75 63 68 20 73 74 72 75 63 74 75 72 65 73 such structures
113bc 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 .** are stored i
113bd 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 n the Pager.aSav
113be 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20 epoint[] array,
113bf 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63 61 74 which is allocat
113c0 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a 65 ed and.** resize
113c1 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52 d using sqlite3R
113c2 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 ealloc()..**.**
113c3 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 When a savepoint
113c4 20 69 73 20 63 72 65 61 74 65 64 2c 20 74 68 65 is created, the
113c5 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e PagerSavepoint.
113c6 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 6c 64 iHdrOffset field
113c7 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2e is.** set to 0.
113c8 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 If a journal-he
113c9 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 ader is written
113ca 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a 6f into the main jo
113cb 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20 74 urnal while.** t
113cc 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 he savepoint is
113cd 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69 48 64 active, then iHd
113ce 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 rOffset is set t
113cf 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 o the byte offse
113d0 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c t .** immediatel
113d1 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 y following the
113d2 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65 63 last journal rec
113d3 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e 74 6f ord written into
113d4 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 the main.** jou
113d5 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68 65 20 rnal before the
113d6 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e 20 journal-header.
113d7 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64 This is required
113d8 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f 69 6e during savepoin
113d9 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28 73 t.** rollback (s
113da 65 65 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b ee pagerPlayback
113db 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a 2f Savepoint())..*/
113dc 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
113dd 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 50 PagerSavepoint P
113de 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a 73 agerSavepoint;.s
113df 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70 truct PagerSavep
113e0 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f 66 oint {. i64 iOf
113e1 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 fset;
113e2 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 69 6e /* Startin
113e3 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 6e g offset in main
113e4 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 journal */. i6
113e5 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 20 20 4 iHdrOffset;
113e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
113e7 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69 74 e above */. Bit
113e8 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f 69 6e vec *pInSavepoin
113e9 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 t; /* Set
113ea 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 69 of pages in thi
113eb 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 s savepoint */.
113ec 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 20 Pgno nOrig;
113ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
113ee 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65 72 Original number
113ef 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66 69 6c of pages in fil
113f0 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75 62 e */. Pgno iSub
113f1 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
113f2 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
113f3 66 69 72 73 74 20 72 65 63 6f 72 64 20 69 6e 20 first record in
113f4 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 7d sub-journal */.}
113f5 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e 20 ;../*.** A open
113f6 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e page cache is an
113f7 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
113f8 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
113f9 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 65 72 72 43 ture..**.** errC
113fa 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 65 ode.**.** Page
113fb 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 62 65 r.errCode may be
113fc 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 49 set to SQLITE_I
113fd 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 OERR, SQLITE_COR
113fe 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 20 20 6f 72 RUPT, or.** or
113ff 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e SQLITE_FULL. On
11400 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 ce one of the fi
11401 72 73 74 20 74 68 72 65 65 20 65 72 72 6f 72 73 rst three errors
11402 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 72 73 occurs, it pers
11403 69 73 74 73 0a 2a 2a 20 20 20 61 6e 64 20 69 73 ists.** and is
11404 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 68 65 returned as the
11405 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 72 79 result of every
11406 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 50 49 major pager API
11407 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20 20 call. The.**
11408 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 65 74 SQLITE_FULL ret
11409 75 72 6e 20 63 6f 64 65 20 69 73 20 73 6c 69 67 urn code is slig
1140a 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 2e 20 htly different.
1140b 49 74 20 70 65 72 73 69 73 74 73 20 6f 6e 6c 79 It persists only
1140c 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 20 20 until the.**
1140d 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 6c 20 next successful
1140e 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 72 66 rollback is perf
1140f 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 61 67 ormed on the pag
11410 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 0a er cache. Also,.
11411 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c ** SQLITE_FULL
11412 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74 does not affect
11413 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 the sqlite3Page
11414 72 47 65 74 28 29 20 61 6e 64 20 73 71 6c 69 74 rGet() and sqlit
11415 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 0a e3PagerLookup().
11416 2a 2a 20 20 20 41 50 49 73 2c 20 74 68 65 79 20 ** APIs, they
11417 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75 73 65 may still be use
11418 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a d successfully..
11419 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 56 61 6c 69 **.** dbSizeVali
1141a 64 2c 20 64 62 53 69 7a 65 2c 20 64 62 4f 72 69 d, dbSize, dbOri
1141b 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53 69 7a gSize, dbFileSiz
1141c 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 6e 61 67 69 e.**.** Managi
1141d 6e 67 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ng the size of t
1141e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1141f 20 69 6e 20 70 61 67 65 73 20 69 73 20 61 20 6c in pages is a l
11420 69 74 74 6c 65 20 63 6f 6d 70 6c 69 63 61 74 65 ittle complicate
11421 64 2e 0a 2a 2a 20 20 20 54 68 65 20 76 61 72 69 d..** The vari
11422 61 62 6c 65 20 50 61 67 65 72 2e 64 62 53 69 7a able Pager.dbSiz
11423 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e e contains the n
11424 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 umber of pages t
11425 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
11426 0a 2a 2a 20 20 20 69 6d 61 67 65 20 63 75 72 72 .** image curr
11427 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 2e 20 ently contains.
11428 41 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 As the database
11429 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 20 73 image grows or s
1142a 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a 20 20 hrinks this.**
1142b 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 70 64 variable is upd
1142c 61 74 65 64 2e 20 54 68 65 20 76 61 72 69 61 62 ated. The variab
1142d 6c 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 le Pager.dbFileS
1142e 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ize contains the
1142f 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 6f 66 20 number.** of
11430 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
11431 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 abase file. This
11432 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e may be differen
11433 74 20 66 72 6f 6d 20 50 61 67 65 72 2e 64 62 53 t from Pager.dbS
11434 69 7a 65 0a 2a 2a 20 20 20 69 66 20 73 6f 6d 65 ize.** if some
11435 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e pages have been
11436 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 appended to the
11437 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 database image
11438 62 75 74 20 6e 6f 74 20 79 65 74 20 77 72 69 74 but not yet writ
11439 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 20 66 72 6f ten.** out fro
1143a 6d 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 74 m the cache to t
1143b 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6f he actual file o
1143c 6e 20 64 69 73 6b 2e 20 4f 72 20 69 66 20 74 68 n disk. Or if th
1143d 65 20 69 6d 61 67 65 20 68 61 73 20 62 65 65 6e e image has been
1143e 0a 2a 2a 20 20 20 74 72 75 6e 63 61 74 65 64 20 .** truncated
1143f 62 79 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 by an incrementa
11440 6c 2d 76 61 63 75 75 6d 20 6f 70 65 72 61 74 69 l-vacuum operati
11441 6f 6e 2e 20 54 68 65 20 50 61 67 65 72 2e 64 62 on. The Pager.db
11442 4f 72 69 67 53 69 7a 65 20 76 61 72 69 61 62 6c OrigSize variabl
11443 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 69 6e 73 20 e.** contains
11444 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
11445 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
11446 61 73 65 20 69 6d 61 67 65 20 77 68 65 6e 20 74 ase image when t
11447 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20 20 he current.**
11448 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 transaction was
11449 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 6f 6e 74 opened. The cont
1144a 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68 72 65 ents of all thre
1144b 65 20 6f 66 20 74 68 65 73 65 20 76 61 72 69 61 e of these varia
1144c 62 6c 65 73 20 69 73 0a 2a 2a 20 20 20 6f 6e 6c bles is.** onl
1144d 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 y guaranteed to
1144e 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 74 68 be correct if th
1144f 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 72 2e e boolean Pager.
11450 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 20 74 dbSizeValid is t
11451 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 rue..**.** TOD
11452 4f 3a 20 55 6e 64 65 72 20 77 68 61 74 20 63 6f O: Under what co
11453 6e 64 69 74 69 6f 6e 73 20 69 73 20 64 62 53 69 nditions is dbSi
11454 7a 65 56 61 6c 69 64 20 73 65 74 3f 20 43 6c 65 zeValid set? Cle
11455 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 63 68 61 6e ared?.**.** chan
11456 67 65 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a geCountDone.**.*
11457 2a 20 20 20 54 68 69 73 20 62 6f 6f 6c 65 61 6e * This boolean
11458 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 variable is use
11459 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 d to make sure t
1145a 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 2d 63 hat the change-c
1145b 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 20 28 74 68 ounter .** (th
1145c 65 20 34 2d 62 79 74 65 20 68 65 61 64 65 72 20 e 4-byte header
1145d 66 69 65 6c 64 20 61 74 20 62 79 74 65 20 6f 66 field at byte of
1145e 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 64 fset 24 of the d
1145f 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 69 73 atabase file) is
11460 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 70 64 61 74 .** not updat
11461 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 ed more often th
11462 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 2a an necessary. .*
11463 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 73 65 74 *.** It is set
11464 20 74 6f 20 74 72 75 65 20 77 68 65 6e 20 74 68 to true when th
11465 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
11466 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 field is update
11467 64 2c 20 77 68 69 63 68 20 0a 2a 2a 20 20 20 63 d, which .** c
11468 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 an only happen i
11469 66 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c f an exclusive l
1146a 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 ock is held on t
1146b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1146c 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c 65 ..** It is cle
1146d 61 72 65 64 20 28 73 65 74 20 74 6f 20 66 61 6c ared (set to fal
1146e 73 65 29 20 77 68 65 6e 65 76 65 72 20 61 6e 20 se) whenever an
1146f 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 exclusive lock i
11470 73 20 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 75 69 s .** relinqui
11471 73 68 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 shed on the data
11472 62 61 73 65 20 66 69 6c 65 2e 20 45 61 63 68 20 base file. Each
11473 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69 time a transacti
11474 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2c on is committed,
11475 0a 2a 2a 20 20 20 54 68 65 20 63 68 61 6e 67 65 .** The change
11476 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69 CountDone flag i
11477 73 20 69 6e 73 70 65 63 74 65 64 2e 20 49 66 20 s inspected. If
11478 69 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 it is true, the
11479 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 70 64 work of.** upd
1147a 61 74 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65 ating the change
1147b 2d 63 6f 75 6e 74 65 72 20 69 73 20 6f 6d 69 74 -counter is omit
1147c 74 65 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 ted for the curr
1147d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ent transaction.
1147e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 6d 65 .**.** This me
1147f 63 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74 68 chanism means th
11480 61 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 at when running
11481 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
11482 65 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e, a connection
11483 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79 20 .** need only
11484 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
11485 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c 20 e-counter once,
11486 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 72 for the first tr
11487 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 63 ansaction.** c
11488 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 ommitted..**.**
11489 64 62 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a 2a 2a dbModified.**.**
1148a 20 20 20 54 68 65 20 64 62 4d 6f 64 69 66 69 65 The dbModifie
1148b 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68 d flag is set wh
1148c 65 6e 65 76 65 72 20 61 20 64 61 74 61 62 61 73 enever a databas
1148d 65 20 70 61 67 65 20 69 73 20 64 69 72 74 69 65 e page is dirtie
1148e 64 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c d..** It is cl
1148f 65 61 72 65 64 20 61 74 20 74 68 65 20 65 6e 64 eared at the end
11490 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73 61 63 of each transac
11491 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 74 tion..**.** It
11492 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 63 6f is used when co
11493 6d 6d 69 74 74 69 6e 67 20 6f 72 20 6f 74 68 65 mmitting or othe
11494 72 77 69 73 65 20 65 6e 64 69 6e 67 20 61 20 74 rwise ending a t
11495 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 0a 2a ransaction. If.*
11496 2a 20 20 20 74 68 65 20 64 62 4d 6f 64 69 66 69 * the dbModifi
11497 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 ed flag is clear
11498 20 74 68 65 6e 20 6c 65 73 73 20 77 6f 72 6b 20 then less work
11499 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 2e 0a has to be done..
1149a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 53 74 61 **.** journalSta
1149b 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 rted.**.** Thi
1149c 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68 s flag is set wh
1149d 65 6e 65 76 65 72 20 74 68 65 20 74 68 65 20 6d enever the the m
1149e 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 ain journal is s
1149f 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 ynced. .**.**
114a0 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 The point of thi
114a1 73 20 66 6c 61 67 20 69 73 20 74 68 61 74 20 69 s flag is that i
114a2 74 20 6d 75 73 74 20 62 65 20 73 65 74 20 61 66 t must be set af
114a3 74 65 72 20 74 68 65 20 0a 2a 2a 20 20 20 66 69 ter the .** fi
114a4 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 rst journal head
114a5 65 72 20 69 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 er in a journal
114a6 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 file has been sy
114a7 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a nced to disk..**
114a8 20 20 20 41 66 74 65 72 20 74 68 69 73 20 68 61 After this ha
114a9 73 20 68 61 70 70 65 6e 65 64 2c 20 6e 65 77 20 s happened, new
114aa 70 61 67 65 73 20 61 70 70 65 6e 64 65 64 20 74 pages appended t
114ab 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a o the database .
114ac 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 6e 65 65 64 ** do not need
114ad 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f the PGHDR_NEED_
114ae 53 59 4e 43 20 66 6c 61 67 20 73 65 74 2c 20 61 SYNC flag set, a
114af 73 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6e 65 s they do not ne
114b0 65 64 0a 2a 2a 20 20 20 74 6f 20 77 61 69 74 20 ed.** to wait
114b1 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79 for a journal sy
114b2 6e 63 20 62 65 66 6f 72 65 20 74 68 65 79 20 63 nc before they c
114b3 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 an be written ou
114b4 74 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 64 61 t to.** the da
114b5 74 61 62 61 73 65 20 66 69 6c 65 20 28 73 65 65 tabase file (see
114b6 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 5f function pager_
114b7 77 72 69 74 65 28 29 29 2e 0a 2a 2a 20 20 20 0a write())..** .
114b8 2a 2a 20 73 65 74 4d 61 73 74 65 72 0a 2a 2a 0a ** setMaster.**.
114b9 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 62 ** This variab
114ba 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e le is used to en
114bb 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 61 sure that the ma
114bc 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
114bd 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 28 69 66 20 e name.** (if
114be 61 6e 79 29 20 69 73 20 6f 6e 6c 79 20 77 72 69 any) is only wri
114bf 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f tten into the jo
114c0 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 63 65 2e urnal file once.
114c1 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 6e 20 63 6f .**.** When co
114c2 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61 6e 73 mmitting a trans
114c3 61 63 74 69 6f 6e 2c 20 74 68 65 20 6d 61 73 74 action, the mast
114c4 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
114c5 6e 61 6d 65 20 28 69 66 20 61 6e 79 29 0a 2a 2a name (if any).**
114c6 20 20 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 may be writte
114c7 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e n into the journ
114c8 61 6c 20 66 69 6c 65 20 77 68 69 6c 65 20 74 68 al file while th
114c9 65 20 70 61 67 65 72 20 69 73 20 73 74 69 6c 6c e pager is still
114ca 20 69 6e 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52 in.** PAGER_R
114cb 45 53 45 52 56 45 44 20 73 74 61 74 65 20 28 73 ESERVED state (s
114cc 65 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e ee CommitPhaseOn
114cd 65 28 29 20 66 6f 72 20 74 68 65 20 61 63 74 69 e() for the acti
114ce 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 20 74 68 65 on). It.** the
114cf 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 75 70 n attempts to up
114d0 67 72 61 64 65 20 74 6f 20 61 6e 20 65 78 63 6c grade to an excl
114d1 75 73 69 76 65 20 6c 6f 63 6b 2e 20 49 66 20 74 usive lock. If t
114d2 68 69 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 20 his attempt.**
114d3 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c fails, then SQL
114d4 49 54 45 5f 42 55 53 59 20 6d 61 79 20 62 65 20 ITE_BUSY may be
114d5 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
114d6 75 73 65 72 20 61 6e 64 20 74 68 65 20 75 73 65 user and the use
114d7 72 0a 2a 2a 20 20 20 6d 61 79 20 61 74 74 65 6d r.** may attem
114d8 70 74 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 pt to commit the
114d9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 67 61 transaction aga
114da 69 6e 20 6c 61 74 65 72 20 28 63 61 6c 6c 69 6e in later (callin
114db 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 74 50 68 61 g.** CommitPha
114dc 73 65 4f 6e 65 28 29 20 61 67 61 69 6e 29 2e 20 seOne() again).
114dd 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 73 65 This flag is use
114de 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 d to ensure that
114df 20 74 68 65 20 0a 2a 2a 20 20 20 6d 61 73 74 65 the .** maste
114e0 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 r journal name i
114e1 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 74 s only written t
114e2 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
114e3 6c 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 le the first.**
114e4 20 20 74 69 6d 65 20 43 6f 6d 6d 69 74 50 68 61 time CommitPha
114e5 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c 6c 65 seOne() is calle
114e6 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 79 d..**.** doNotSy
114e7 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 nc.**.** This
114e8 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 variable is set
114e9 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20 73 and cleared by s
114ea 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
114eb 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 65 64 53 79 ()..**.** needSy
114ec 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 4f 3a nc.**.** TODO:
114ed 20 49 74 20 6d 69 67 68 74 20 62 65 20 65 61 73 It might be eas
114ee 69 65 72 20 74 6f 20 73 65 74 20 74 68 69 73 20 ier to set this
114ef 76 61 72 69 61 62 6c 65 20 69 6e 20 77 72 69 74 variable in writ
114f0 65 4a 6f 75 72 6e 61 6c 48 64 72 28 29 0a 2a 2a eJournalHdr().**
114f1 20 20 20 61 6e 64 20 77 72 69 74 65 4d 61 73 74 and writeMast
114f2 65 72 4a 6f 75 72 6e 61 6c 28 29 20 6f 6e 6c 79 erJournal() only
114f3 2e 20 43 68 61 6e 67 65 20 69 74 73 20 6d 65 61 . Change its mea
114f4 6e 69 6e 67 20 74 6f 20 22 75 6e 73 79 6e 63 65 ning to "unsynce
114f5 64 20 64 61 74 61 0a 2a 2a 20 20 20 68 61 73 20 d data.** has
114f6 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 been written to
114f7 74 68 65 20 6a 6f 75 72 6e 61 6c 22 2e 0a 2a 2a the journal"..**
114f8 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 .** subjInMemory
114f9 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 69 73 .**.** This is
114fa 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 a boolean varia
114fb 62 6c 65 2e 20 49 66 20 74 72 75 65 2c 20 74 68 ble. If true, th
114fc 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 en any required
114fd 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 sub-journal.**
114fe 20 69 73 20 6f 70 65 6e 65 64 20 61 73 20 61 6e is opened as an
114ff 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
11500 61 6c 20 66 69 6c 65 2e 20 49 66 20 66 61 6c 73 al file. If fals
11501 65 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d 6f 72 e, then in-memor
11502 79 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75 72 6e y.** sub-journ
11503 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 als are only use
11504 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 d for in-memory
11505 70 61 67 65 72 20 66 69 6c 65 73 2e 0a 2a 2f 0a pager files..*/.
11506 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0a 20 struct Pager {.
11507 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
11508 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 fs; /*
11509 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 OS functions to
1150a 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a 20 20 use for IO */.
1150b 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 u8 exclusiveMode
1150c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 ; /* B
1150d 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 20 oolean. True if
1150e 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58 locking_mode==EX
1150f 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38 20 CLUSIVE */. u8
11510 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 journalMode;
11511 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 20 6f /* On o
11512 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55 52 f the PAGER_JOUR
11513 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 65 73 NALMODE_* values
11514 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f 75 72 */. u8 useJour
11515 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 nal;
11516 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c 6c 62 /* Use a rollb
11517 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20 74 ack journal on t
11518 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 his file */. u8
11519 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 20 20 noReadlock;
1151a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 /* Do
1151b 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 6f 62 not bother to ob
1151c 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 20 2a tain readlocks *
1151d 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b 20 20 /. u8 noSync;
1151e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1151f 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 20 74 /* Do not sync t
11520 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74 72 he journal if tr
11521 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c 6c 53 ue */. u8 fullS
11522 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 ync;
11523 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 61 20 /* Do extra
11524 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a 6f 75 syncs of the jou
11525 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 74 6e rnal for robustn
11526 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e 63 ess */. u8 sync
11527 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 _flags;
11528 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 /* One of S
11529 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 59 YNC_NORMAL or SY
1152a 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 38 20 NC_FULL */. u8
1152b 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 20 tempFile;
1152c 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c /* zFil
1152d 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70 6f ename is a tempo
1152e 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75 rary file */. u
1152f 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 8 readOnly;
11530 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
11531 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f 6e ue for a read-on
11532 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 ly database */.
11533 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 20 u8 memDb;
11534 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11535 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74 20 True to inhibit
11536 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a all file I/O */.
11537 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
11538 69 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 ing block contai
11539 6e 73 20 74 68 6f 73 65 20 63 6c 61 73 73 20 6d ns those class m
1153a 65 6d 62 65 72 73 20 74 68 61 74 20 61 72 65 20 embers that are
1153b 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a dynamically. **
1153c 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e 67 modified during
1153d 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f normal operatio
1153e 6e 73 2e 20 54 68 65 20 6f 74 68 65 72 20 76 61 ns. The other va
1153f 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 riables in this
11540 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 61 structure. ** a
11541 72 65 20 65 69 74 68 65 72 20 63 6f 6e 73 74 61 re either consta
11542 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 nt throughout th
11543 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 e lifetime of th
11544 65 20 70 61 67 65 72 2c 20 6f 72 20 65 6c 73 65 e pager, or else
11545 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 . ** used to st
11546 6f 72 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ore configuratio
11547 6e 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 n parameters tha
11548 74 20 61 66 66 65 63 74 20 74 68 65 20 77 61 79 t affect the way
11549 20 74 68 65 20 70 61 67 65 72 20 0a 20 20 2a 2a the pager . **
1154a 20 6f 70 65 72 61 74 65 73 2e 0a 20 20 2a 2a 0a operates.. **.
1154b 20 20 2a 2a 20 54 68 65 20 27 73 74 61 74 65 27 ** The 'state'
1154c 20 76 61 72 69 61 62 6c 65 20 69 73 20 64 65 73 variable is des
1154d 63 72 69 62 65 64 20 69 6e 20 6d 6f 72 65 20 64 cribed in more d
1154e 65 74 61 69 6c 20 61 6c 6f 6e 67 20 77 69 74 68 etail along with
1154f 20 74 68 65 0a 20 20 2a 2a 20 64 65 73 63 72 69 the. ** descri
11550 70 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 ptions of the va
11551 6c 75 65 73 20 69 74 20 6d 61 79 20 74 61 6b 65 lues it may take
11552 20 2d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 - PAGER_UNLOCK
11553 65 74 63 2e 20 4d 61 6e 79 20 6f 66 20 74 68 65 etc. Many of the
11554 0a 20 20 2a 2a 20 6f 74 68 65 72 20 76 61 72 69 . ** other vari
11555 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 62 6c ables in this bl
11556 6f 63 6b 20 61 72 65 20 64 65 73 63 72 69 62 65 ock are describe
11557 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 d in the comment
11558 20 64 69 72 65 63 74 6c 79 20 0a 20 20 2a 2a 20 directly . **
11559 61 62 6f 76 65 20 74 68 69 73 20 63 6c 61 73 73 above this class
1155a 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20 2a definition.. *
1155b 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20 20 20 /. u8 state;
1155c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1155d 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2c /* PAGER_UNLOCK,
1155e 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53 45 52 _SHARED, _RESER
1155f 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 VED, etc. */. u
11560 38 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 20 20 8 dbModified;
11561 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
11562 75 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 ue if there are
11563 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74 any changes to t
11564 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 6e 65 he Db */. u8 ne
11565 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 edSync;
11566 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
11567 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69 73 20 f an fsync() is
11568 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20 6a 6f needed on the jo
11569 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 6a 6f urnal */. u8 jo
1156a 75 72 6e 61 6c 53 74 61 72 74 65 64 3b 20 20 20 urnalStarted;
1156b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
1156c 66 20 68 65 61 64 65 72 20 6f 66 20 6a 6f 75 72 f header of jour
1156d 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 2a 2f nal is synced */
1156e 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f 75 6e . u8 changeCoun
1156f 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 2f tDone; /
11570 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e 63 72 * Set after incr
11571 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 68 61 ementing the cha
11572 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 nge-counter */.
11573 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b 20 20 u8 setMaster;
11574 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11575 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e 61 True if a m-j na
11576 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 me has been writ
11577 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20 ten to jrnl */.
11578 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20 20 u8 doNotSync;
11579 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1157a 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20 74 Boolean. While t
1157b 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69 6c rue, do not spil
1157c 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 l the cache */.
1157d 20 75 38 20 64 62 53 69 7a 65 56 61 6c 69 64 3b u8 dbSizeValid;
1157e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1157f 53 65 74 20 77 68 65 6e 20 64 62 53 69 7a 65 20 Set when dbSize
11580 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20 is correct */.
11581 75 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b u8 subjInMemory;
11582 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
11583 72 75 65 20 74 6f 20 75 73 65 20 69 6e 2d 6d 65 rue to use in-me
11584 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c mory sub-journal
11585 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 53 69 s */. Pgno dbSi
11586 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
11587 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
11588 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
11589 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 abase */. Pgno
1158a 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 20 20 dbOrigSize;
1158b 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a 65 /* dbSize
1158c 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 72 72 before the curr
1158d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1158e 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 6c 65 */. Pgno dbFile
1158f 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 Size;
11590 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
11591 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 ges in the datab
11592 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e ase file */. in
11593 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20 t errCode;
11594 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
11595 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69 6e 64 of several kind
11596 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 s of errors */.
11597 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20 20 int nRec;
11598 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11599 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c 6c 65 64 Pages journalled
1159a 20 73 69 6e 63 65 20 6c 61 73 74 20 6a 2d 68 65 since last j-he
1159b 61 64 65 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a ader written */.
1159c 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74 3b u32 cksumInit;
1159d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1159e 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76 61 Quasi-random va
1159f 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76 65 lue added to eve
115a0 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20 ry checksum */.
115a1 20 75 33 32 20 6e 53 75 62 52 65 63 3b 20 20 20 u32 nSubRec;
115a2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
115a3 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64 Number of record
115a4 73 20 77 72 69 74 74 65 6e 20 74 6f 20 73 75 62 s written to sub
115a5 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 42 69 -journal */. Bi
115a6 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 6e 61 6c tvec *pInJournal
115a7 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 ; /* One
115a8 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 70 61 bit for each pa
115a9 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 ge in the databa
115aa 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c se file */. sql
115ab 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 20 20 ite3_file *fd;
115ac 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
115ad 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 descriptor for
115ae 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 database */. sq
115af 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b lite3_file *jfd;
115b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c /* Fil
115b1 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 e descriptor for
115b2 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f main journal */
115b3 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
115b4 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 20 20 2f *sjfd; /
115b5 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f * File descripto
115b6 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 r for sub-journa
115b7 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e l */. i64 journ
115b8 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 alOff;
115b9 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 77 72 /* Current wr
115ba 69 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74 68 ite offset in th
115bb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a e journal file *
115bc 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 /. i64 journalH
115bd 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dr;
115be 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 74 /* Byte offset t
115bf 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72 6e o previous journ
115c0 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 50 al header */. P
115c1 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 61 agerSavepoint *a
115c2 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20 41 72 Savepoint; /* Ar
115c3 72 61 79 20 6f 66 20 61 63 74 69 76 65 20 73 61 ray of active sa
115c4 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e vepoints */. in
115c5 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 t nSavepoint;
115c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
115c7 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
115c8 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 in aSavepoint[]
115c9 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 6c 65 */. char dbFile
115ca 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 20 20 Vers[16];
115cb 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 65 6e /* Changes when
115cc 65 76 65 72 20 64 61 74 61 62 61 73 65 20 66 69 ever database fi
115cd 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 le changes */.
115ce 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 u32 sectorSize;
115cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
115d0 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69 ssumed sector si
115d1 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 ze during rollba
115d2 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 20 6e 45 78 ck */.. u16 nEx
115d3 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 tra;
115d4 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69 73 /* Add this
115d5 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20 65 many bytes to e
115d6 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 ach in-memory pa
115d7 67 65 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 73 ge */. i16 nRes
115d8 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 20 erve;
115d9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
115da 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 61 74 unused bytes at
115db 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 end of each pag
115dc 65 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46 6c e */. u32 vfsFl
115dd 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
115de 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20 /* Flags for
115df 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 sqlite3_vfs.xOpe
115e0 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 n() */. int pag
115e1 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 eSize;
115e2 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
115e3 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61 67 f bytes in a pag
115e4 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 e */. Pgno mxPg
115e5 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 no;
115e6 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c /* Maximum al
115e7 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74 68 lowed size of th
115e8 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
115e9 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b char *zFilename;
115ea 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
115eb 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
115ec 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 ase file */. ch
115ed 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 ar *zJournal;
115ee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
115ef 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
115f0 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 28 file */. int (
115f1 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76 *xBusyHandler)(v
115f2 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 74 69 oid*); /* Functi
115f3 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 on to call when
115f4 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a busy */. void *
115f5 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 3b pBusyHandlerArg;
115f6 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 /* Context
115f7 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 78 42 argument for xB
115f8 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 23 69 usyHandler */.#i
115f9 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
115fa 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e 4d 69 . int nHit, nMi
115fb 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ss; /
115fc 2a 20 43 61 63 68 65 20 68 69 74 73 20 61 6e 64 * Cache hits and
115fd 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20 69 6e missing */. in
115fe 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74 65 3b t nRead, nWrite;
115ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
11600 61 62 61 73 65 20 70 61 67 65 73 20 72 65 61 64 abase pages read
11601 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65 6e 64 /written */.#end
11602 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 if. void (*xRei
11603 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a 29 3b niter)(DbPage*);
11604 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f /* Call this ro
11605 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61 utine when reloa
11606 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23 69 ding pages */.#i
11607 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f fdef SQLITE_HAS_
11608 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a 28 2a CODEC. void *(*
11609 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f xCodec)(void*,vo
1160a 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f id*,Pgno,int); /
1160b 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e * Routine for en
1160c 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20 2a /decoding data *
1160d 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 /. void (*xCode
1160e 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a cSizeChng)(void*
1160f 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e 6f ,int,int); /* No
11610 74 69 66 79 20 6f 66 20 70 61 67 65 20 73 69 7a tify of page siz
11611 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 76 e changes */. v
11612 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65 oid (*xCodecFree
11613 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 )(void*);
11614 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 /* Destruc
11615 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f 64 65 tor for the code
11616 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f c */. void *pCo
11617 64 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 dec;
11618 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 /* First argu
11619 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 2e 2e ment to xCodec..
1161a 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 65 6e . methods */.#en
1161b 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 6d 70 dif. char *pTmp
1161c 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 Space;
1161d 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 53 /* Pager.pageS
1161e 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 ize bytes of spa
1161f 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a ce for tmp use *
11620 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53 /. i64 journalS
11621 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 izeLimit;
11622 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 66 6f /* Size limit fo
11623 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 r persistent jou
11624 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 rnal files */.
11625 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 65 3b PCache *pPCache;
11626 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
11627 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 63 ointer to page c
11628 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ache object */.
11629 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 sqlite3_backup
1162a 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 2f 2a 20 *pBackup; /*
1162b 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73 74 20 Pointer to list
1162c 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63 6b 75 of ongoing backu
1162d 70 20 70 72 6f 63 65 73 73 65 73 20 2a 2f 0a 7d p processes */.}
1162e 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c ;../*.** The fol
1162f 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 lowing global va
11630 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63 6f 75 riables hold cou
11631 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72 0a 2a nters used for.*
11632 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 * testing purpos
11633 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73 65 20 es only. These
11634 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e 6f 74 variables do not
11635 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61 20 6e exist in.** a n
11636 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69 6c 64 on-testing build
11637 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c . These variabl
11638 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72 65 61 es are not threa
11639 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 64 65 d-safe..*/.#ifde
1163a 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
1163b 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1163c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 ite3_pager_readd
1163d 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20 b_count = 0;
1163e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c /* Number of ful
1163f 6c 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f l pages read fro
11640 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 m DB */.SQLITE_A
11641 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 PI int sqlite3_p
11642 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75 ager_writedb_cou
11643 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d nt = 0; /* Num
11644 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 ber of full page
11645 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 42 20 s written to DB
11646 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
11647 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f t sqlite3_pager_
11648 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d 20 30 writej_count = 0
11649 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f ; /* Number o
1164a 66 20 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 f pages written
1164b 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 20 to journal */.#
1164c 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 define PAGER_INC
1164d 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73 65 0a R(v) v++.#else.
1164e 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 # define PAGER_I
1164f 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a 0a 0a NCR(v).#endif...
11650 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66 ./*.** Journal f
11651 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74 68 20 iles begin with
11652 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
11653 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54 68 65 gic string. The
11654 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f 62 74 data.** was obt
11655 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65 76 2f ained from /dev/
11656 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73 20 75 random. It is u
11657 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20 73 61 sed only as a sa
11658 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a nity check..**.*
11659 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 * Since version
1165a 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75 72 6e 2.8.0, the journ
1165b 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74 61 69 al format contai
1165c 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 61 ns additional sa
1165d 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 nity.** checking
1165e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 information. I
1165f 66 20 74 68 65 20 70 6f 77 65 72 20 66 61 69 6c f the power fail
11660 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 s while the jour
11661 6e 61 6c 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20 nal is being.**
11662 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d 72 61 written, semi-ra
11663 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64 61 74 ndom garbage dat
11664 61 20 6d 69 67 68 74 20 61 70 70 65 61 72 20 69 a might appear i
11665 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a n the journal.**
11666 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f 77 65 file after powe
11667 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e 20 20 r is restored.
11668 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 If an attempt is
11669 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f then made.** to
1166a 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 6e 61 roll the journa
1166b 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 l back, the data
1166c 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20 63 6f base could be co
1166d 72 72 75 70 74 65 64 2e 20 20 54 68 65 20 61 64 rrupted. The ad
1166e 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69 ditional.** sani
1166f 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61 74 61 ty checking data
11670 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74 is an attempt t
11671 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 67 o discover the g
11672 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a arbage in the.**
11673 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 67 6e journal and ign
11674 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ore it..**.** Th
11675 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e e sanity checkin
11676 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f g information fo
11677 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72 6e 61 r the new journa
11678 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69 73 74 l format consist
11679 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69 74 s.** of a 32-bit
1167a 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61 63 checksum on eac
1167b 68 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 20 h page of data.
1167c 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20 63 6f The checksum co
1167d 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74 68 65 vers both.** the
1167e 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 page number and
1167f 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 61 67 the pPager->pag
11680 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 eSize bytes of d
11681 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 65 ata for the page
11682 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d 20 ..** This cksum
11683 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 is initialized t
11684 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e 64 6f o a 32-bit rando
11685 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 70 70 m value that app
11686 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6a ears in the.** j
11687 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 67 68 ournal file righ
11688 74 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64 t after the head
11689 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d 20 er. The random
1168a 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 20 69 initializer is i
1168b 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 65 63 mportant,.** bec
1168c 61 75 73 65 20 67 61 72 62 61 67 65 20 64 61 74 ause garbage dat
1168d 61 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61 a that appears a
1168e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a t the end of a j
1168f 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65 6c 79 ournal is likely
11690 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20 77 61 .** data that wa
11691 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65 72 20 s once in other
11692 66 69 6c 65 73 20 74 68 61 74 20 68 61 76 65 20 files that have
11693 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74 65 64 now been deleted
11694 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67 61 72 . If the.** gar
11695 62 61 67 65 20 64 61 74 61 20 63 61 6d 65 20 66 bage data came f
11696 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 65 20 rom an obsolete
11697 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 journal file, th
11698 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69 67 68 e checksums migh
11699 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63 74 2e t.** be correct.
1169a 20 20 42 75 74 20 62 79 20 69 6e 69 74 69 61 6c But by initial
1169b 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63 6b 73 izing the checks
1169c 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76 61 6c um to random val
1169d 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 64 ue which.** is d
1169e 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 76 65 ifferent for eve
1169f 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 20 6d ry journal, we m
116a0 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72 69 73 inimize that ris
116a1 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e k..*/.static con
116a2 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
116a3 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d aJournalMagic[]
116a4 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30 78 64 = {. 0xd9, 0xd
116a5 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c 20 30 5, 0x05, 0xf9, 0
116a6 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36 33 2c x20, 0xa1, 0x63,
116a7 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 0xd7,.};../*.**
116a8 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 The size of the
116a9 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 72 65 of each page re
116aa 63 6f 72 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 cord in the jour
116ab 6e 61 6c 20 69 73 20 67 69 76 65 6e 20 62 79 0a nal is given by.
116ac 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** the following
116ad 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69 macro..*/.#defi
116ae 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a ne JOURNAL_PG_SZ
116af 28 70 50 61 67 65 72 29 20 20 28 28 70 50 61 67 (pPager) ((pPag
116b0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 2b 20 er->pageSize) +
116b1 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 8)../*.** The jo
116b2 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 69 7a urnal header siz
116b3 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 e for this pager
116b4 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c . This is usuall
116b5 79 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20 73 y the same .** s
116b6 69 7a 65 20 61 73 20 61 20 73 69 6e 67 6c 65 20 ize as a single
116b7 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 65 65 disk sector. See
116b8 20 61 6c 73 6f 20 73 65 74 53 65 63 74 6f 72 53 also setSectorS
116b9 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e ize()..*/.#defin
116ba 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a e JOURNAL_HDR_SZ
116bb 28 70 50 61 67 65 72 29 20 28 70 50 61 67 65 72 (pPager) (pPager
116bc 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a 0a 2f ->sectorSize)../
116bd 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 4d *.** The macro M
116be 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 66 20 EMDB is true if
116bf 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 we are dealing w
116c0 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 ith an in-memory
116c1 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 57 65 database..** We
116c2 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 6d 61 do this as a ma
116c3 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 20 74 cro so that if t
116c4 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d he SQLITE_OMIT_M
116c5 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 69 73 EMORYDB macro is
116c6 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 61 6c set,.** the val
116c7 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 6c 6c ue of MEMDB will
116c8 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 20 61 be a constant a
116c9 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 nd the compiler
116ca 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a will optimize.**
116cb 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 20 77 out code that w
116cc 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 63 75 ould never execu
116cd 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 te..*/.#ifdef SQ
116ce 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 LITE_OMIT_MEMORY
116cf 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 DB.# define MEMD
116d0 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 B 0.#else.# defi
116d1 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72 2d ne MEMDB pPager-
116d2 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a 0a 2f >memDb.#endif../
116d3 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d *.** The maximum
116d4 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d 62 legal page numb
116d5 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20 31 29 er is (2^31 - 1)
116d6 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 ..*/.#define PAG
116d7 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31 34 37 ER_MAX_PGNO 2147
116d8 34 38 33 36 34 37 0a 0a 23 69 66 6e 64 65 66 20 483647..#ifndef
116d9 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20 55 73 NDEBUG ./*.** Us
116da 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73 73 age:.**.** ass
116db 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 ert( assert_page
116dc 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20 r_state(pPager)
116dd 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 );.*/.static int
116de 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 assert_pager_st
116df 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ate(Pager *pPage
116e0 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 74 65 6d 70 r){.. /* A temp
116e1 2d 66 69 6c 65 20 69 73 20 61 6c 77 61 79 73 20 -file is always
116e2 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 in PAGER_EXCLUSI
116e3 56 45 20 6f 72 20 50 41 47 45 52 5f 53 59 4e 43 VE or PAGER_SYNC
116e4 45 44 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 61 ED state. */. a
116e5 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 ssert( pPager->t
116e6 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 70 50 empFile==0 || pP
116e7 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
116e8 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a ER_EXCLUSIVE );.
116e9 0a 20 20 2f 2a 20 54 68 65 20 63 68 61 6e 67 65 . /* The change
116ea 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69 CountDone flag i
116eb 73 20 61 6c 77 61 79 73 20 73 65 74 20 66 6f 72 s always set for
116ec 20 74 65 6d 70 2d 66 69 6c 65 73 20 2a 2f 0a 20 temp-files */.
116ed 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
116ee 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 >tempFile==0 ||
116ef 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
116f0 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20 72 65 untDone );.. re
116f1 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 turn 1;.}.#endif
116f2 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
116f3 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 65 63 rue if it is nec
116f4 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 essary to write
116f5 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20 74 page *pPg into t
116f6 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a he sub-journal..
116f7 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73 20 ** A page needs
116f8 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e to be written in
116f9 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e to the sub-journ
116fa 61 6c 20 69 66 20 74 68 65 72 65 20 65 78 69 73 al if there exis
116fb 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d 6f 72 ts one.** or mor
116fc 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 e open savepoint
116fd 73 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a 2a 0a s for which:.**.
116fe 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 2d ** * The page-
116ff 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74 number is less t
11700 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
11701 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 6e PagerSavepoint.n
11702 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a Orig, and.** *
11703 20 54 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 The bit corresp
11704 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61 onding to the pa
11705 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 ge-number is not
11706 20 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 20 50 set in.** P
11707 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 agerSavepoint.pI
11708 6e 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 nSavepoint..*/.s
11709 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a 52 65 tatic int subjRe
1170a 71 75 69 72 65 73 50 61 67 65 28 50 67 48 64 72 quiresPage(PgHdr
1170b 20 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f 20 70 *pPg){. Pgno p
1170c 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b gno = pPg->pgno;
1170d 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
1170e 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
1170f 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
11710 3d 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e 6e 53 =0; i<pPager->nS
11711 61 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 7b 0a avepoint; i++){.
11712 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 PagerSavepoi
11713 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d nt *p = &pPager-
11714 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d 3b 0a >aSavepoint[i];.
11715 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69 67 if( p->nOrig
11716 3e 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71 6c >=pgno && 0==sql
11717 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
11718 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20 ->pInSavepoint,
11719 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 72 pgno) ){. r
1171a 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
1171b 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
1171c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1171d 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20 rue if the page
1171e 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 is already in th
1171f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
11720 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
11721 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 64 geInJournal(PgHd
11722 72 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 r *pPg){. retur
11723 6e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 n sqlite3BitvecT
11724 65 73 74 28 70 50 67 2d 3e 70 50 61 67 65 72 2d est(pPg->pPager-
11725 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 >pInJournal, pPg
11726 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ->pgno);.}../*.*
11727 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 20 * Read a 32-bit
11728 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65 integer from the
11729 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63 given file desc
1172a 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20 74 riptor. Store t
1172b 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 68 he integer.** th
1172c 61 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a 70 at is read in *p
1172d 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51 4c Res. Return SQL
1172e 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 ITE_OK if everyt
1172f 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20 hing worked, or
11730 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 an.** error code
11731 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f is something go
11732 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 es wrong..**.**
11733 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20 73 All values are s
11734 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61 73 tored on disk as
11735 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f 0a big-endian..*/.
11736 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 33 static int read3
11737 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69 2bits(sqlite3_fi
11738 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 le *fd, i64 offs
11739 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b 0a et, u32 *pRes){.
1173a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
1173b 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63 20 ac[4];. int rc
1173c 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
1173d 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28 61 fd, ac, sizeof(a
1173e 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 69 c), offset);. i
1173f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
11740 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 ){. *pRes =
11741 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 sqlite3Get4byte(
11742 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ac);. }. retur
11743 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 n rc;.}../*.** W
11744 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e rite a 32-bit in
11745 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74 72 teger into a str
11746 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62 69 ing buffer in bi
11747 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f 72 g-endian byte or
11748 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 der..*/.#define
11749 70 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 20 put32bits(A,B)
1174a 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 sqlite3Put4byte(
1174b 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a (u8*)A,B)../*.**
1174c 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 Write a 32-bit
1174d 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68 65 integer into the
1174e 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63 given file desc
1174f 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 riptor. Return
11750 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 SQLITE_OK.** on
11751 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 success or an er
11752 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 ror code is some
11753 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
11754 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11755 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c 69 write32bits(sqli
11756 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 te3_file *fd, i6
11757 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76 61 4 offset, u32 va
11758 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34 5d l){. char ac[4]
11759 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61 63 ;. put32bits(ac
1175a 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e , val);. return
1175b 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
1175c 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73 65 fd, ac, 4, offse
1175d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 t);.}../*.** The
1175e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
1175f 73 20 6d 61 63 72 6f 20 69 73 20 61 20 66 69 6c s macro is a fil
11760 65 20 64 65 73 63 72 69 70 74 6f 72 20 28 74 79 e descriptor (ty
11761 70 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a pe sqlite3_file*
11762 29 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 )..** Return 0 i
11763 66 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e f it is not open
11764 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 28 62 , or non-zero (b
11765 75 74 20 6e 6f 74 20 31 29 20 69 66 20 69 74 20 ut not 1) if it
11766 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 is..**.** This i
11767 73 20 73 6f 20 74 68 61 74 20 65 78 70 72 65 73 s so that expres
11768 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 77 72 69 sions can be wri
11769 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 tten as:.**.**
1176a 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
1176b 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e 2e 0a er->jfd) ){ ....
1176c 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 **.** instead of
1176d 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 70 50 61 .**.** if( pPa
1176e 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f ger->jfd->pMetho
1176f 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23 64 65 ds ){ ....*/.#de
11770 66 69 6e 65 20 69 73 4f 70 65 6e 28 70 46 64 29 fine isOpen(pFd)
11771 20 28 28 70 46 64 29 2d 3e 70 4d 65 74 68 6f 64 ((pFd)->pMethod
11772 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 6c s)../*.** If fil
11773 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c 20 63 e pFd is open, c
11774 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c all sqlite3OsUnl
11775 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f 0a ock() on it..*/.
11776 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 6e 6c static int osUnl
11777 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
11778 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f 63 6b *pFd, int eLock
11779 29 7b 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e ){. if( !isOpen
1177a 28 70 46 64 29 20 29 7b 0a 20 20 20 20 72 65 74 (pFd) ){. ret
1177b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1177c 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 }. return sqli
1177d 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 64 2c te3OsUnlock(pFd,
1177e 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a eLock);.}../*.*
1177f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
11780 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 determines wheth
11781 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 er or not the at
11782 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d omic-write optim
11783 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62 ization.** can b
11784 65 20 75 73 65 64 20 77 69 74 68 20 74 68 69 73 e used with this
11785 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 74 69 pager. The opti
11786 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 mization can be
11787 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 used if:.**.**
11788 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 (a) the value re
11789 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 76 69 turned by OsDevi
1178a 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
1178b 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 74 68 s() indicates th
1178c 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 61 74 at.** a dat
1178d 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 20 62 abase page may b
1178e 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 e written atomic
1178f 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 28 62 ally, and.** (b
11790 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 ) the value retu
11791 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f 72 rned by OsSector
11792 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20 74 Size() is less t
11793 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 han or equal.**
11794 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 67 65 to the page
11795 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 size..**.** The
11796 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 optimization is
11797 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65 6e 61 also always ena
11798 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 bled for tempora
11799 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69 73 0a ry files. It is.
1179a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63 ** an error to c
1179b 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f all this functio
1179c 6e 20 69 66 20 70 50 61 67 65 72 20 69 73 20 6f n if pPager is o
1179d 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e 2d 6d pened on an in-m
1179e 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 73 emory.** databas
1179f 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
117a0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e optimization can
117a1 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30 20 69 not be used, 0 i
117a2 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69 s returned. If i
117a3 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c 0a 2a t can be used,.*
117a4 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 * then the value
117a5 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 returned is the
117a6 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 size of the jou
117a7 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 69 rnal file when i
117a8 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 72 6f t.** contains ro
117a9 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f 72 20 llback data for
117aa 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 exactly one page
117ab 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
117ac 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 TE_ENABLE_ATOMIC
117ad 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20 69 6e _WRITE.static in
117ae 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 t jrnlBufferSize
117af 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
117b0 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 . assert( !MEMD
117b1 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 B );. if( !pPag
117b2 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a er->tempFile ){.
117b3 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20 20 20 int dc;
117b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
117b5 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 /* Device
117b6 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 characteristics
117b7 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65 63 74 */. int nSect
117b8 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 or;
117b9 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74 /* Sect
117ba 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 69 or size */. i
117bb 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 nt szPage;
117bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
117bd 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a 2f /* Page size */
117be 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 .. assert( is
117bf 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
117c0 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73 71 6c );. dc = sql
117c1 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 ite3OsDeviceChar
117c2 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 acteristics(pPag
117c3 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e 53 65 er->fd);. nSe
117c4 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d 3e 73 ctor = pPager->s
117c5 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 73 ectorSize;. s
117c6 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e zPage = pPager->
117c7 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20 20 61 pageSize;.. a
117c8 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 ssert(SQLITE_IOC
117c9 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35 AP_ATOMIC512==(5
117ca 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61 73 73 12>>8));. ass
117cb 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 ert(SQLITE_IOCAP
117cc 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 _ATOMIC64K==(655
117cd 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69 66 28 36>>8));. if(
117ce 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54 45 5f 0==(dc&(SQLITE_
117cf 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 73 7a IOCAP_ATOMIC|(sz
117d0 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e 53 65 Page>>8)) || nSe
117d1 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29 7b 0a ctor>szPage) ){.
117d2 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
117d3 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
117d4 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f urn JOURNAL_HDR_
117d5 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a 4f 55 SZ(pPager) + JOU
117d6 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 RNAL_PG_SZ(pPage
117d7 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a r);.}.#endif../*
117d8 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 48 .** If SQLITE_CH
117d9 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66 ECK_PAGES is def
117da 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 6f 20 ined then we do
117db 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 65 63 some sanity chec
117dc 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 king.** on the c
117dd 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 61 73 ache using a has
117de 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 h function. Thi
117df 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 s is used for te
117e0 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62 sting.** and deb
117e1 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a ugging only..*/.
117e2 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 #ifdef SQLITE_CH
117e3 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20 ECK_PAGES./*.**
117e4 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 74 20 Return a 32-bit
117e5 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 67 65 hash of the page
117e6 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 2e data for pPage.
117e7 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 .*/.static u32 p
117e8 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 69 6e ager_datahash(in
117e9 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 6e 65 t nByte, unsigne
117ea 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 7b 0a d char *pData){.
117eb 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 3b 0a u32 hash = 0;.
117ec 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
117ed 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b 2b =0; i<nByte; i++
117ee 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 28 68 ){. hash = (h
117ef 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61 74 ash*1039) + pDat
117f0 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 a[i];. }. retu
117f1 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 74 69 rn hash;.}.stati
117f2 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 67 65 c u32 pager_page
117f3 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 67 hash(PgHdr *pPag
117f4 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 67 e){. return pag
117f5 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67 er_datahash(pPag
117f6 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 65 53 e->pPager->pageS
117f7 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 ize, (unsigned c
117f8 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 44 61 har *)pPage->pDa
117f9 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f ta);.}.static vo
117fa 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67 id pager_set_pag
117fb 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 ehash(PgHdr *pPa
117fc 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e 70 61 ge){. pPage->pa
117fd 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70 geHash = pager_p
117fe 61 67 65 68 61 73 68 28 70 50 61 67 65 29 3b 0a agehash(pPage);.
117ff 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 }../*.** The CHE
11800 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 61 CK_PAGE macro ta
11801 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 73 20 kes a PgHdr* as
11802 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 an argument. If
11803 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 SQLITE_CHECK_PAG
11804 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 ES.** is defined
11805 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73 20 , and NDEBUG is
11806 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 not defined, an
11807 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
11808 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 61 nt checks.** tha
11809 74 20 74 68 65 20 70 61 67 65 20 69 73 20 65 69 t the page is ei
1180a 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 73 74 ther dirty or st
1180b 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65 20 ill matches the
1180c 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65 2d calculated page-
1180d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 hash..*/.#define
1180e 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63 CHECK_PAGE(x) c
1180f 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61 74 heckPage(x).stat
11810 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67 ic void checkPag
11811 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 e(PgHdr *pPg){.
11812 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
11813 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
11814 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70 61 assert( !pPg->pa
11815 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65 72 geHash || pPager
11816 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 20 20 ->errCode.
11817 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 || (pPg->flags&P
11818 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20 70 GHDR_DIRTY) || p
11819 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70 61 Pg->pageHash==pa
1181a 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 ger_pagehash(pPg
1181b 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23 64 ) );.}..#else.#d
1181c 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74 61 efine pager_data
1181d 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64 65 hash(X,Y) 0.#de
1181e 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65 68 fine pager_pageh
1181f 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 69 6e ash(X) 0.#defin
11820 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 0a e CHECK_PAGE(x).
11821 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 #endif /* SQLIT
11822 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 2a 2f E_CHECK_PAGES */
11823 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 ../*.** When thi
11824 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 s is called the
11825 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
11826 20 70 61 67 65 72 20 70 50 61 67 65 72 20 6d 75 pager pPager mu
11827 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 54 st be open..** T
11828 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 his function att
11829 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 61 20 empts to read a
1182a 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
1182b 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 ile name from th
1182c 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 e .** end of the
1182d 20 66 69 6c 65 20 61 6e 64 2c 20 69 66 20 73 75 file and, if su
1182e 63 63 65 73 73 66 75 6c 2c 20 63 6f 70 69 65 73 ccessful, copies
1182f 20 69 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 it into memory
11830 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 62 79 20 supplied .** by
11831 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65 65 20 the caller. See
11832 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 77 comments above w
11833 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 riteMasterJourna
11834 6c 28 29 20 66 6f 72 20 74 68 65 20 66 6f 72 6d l() for the form
11835 61 74 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 at.** used to st
11836 6f 72 65 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 ore a master jou
11837 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 61 rnal file name a
11838 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a t the end of a j
11839 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a ournal file..**.
1183a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20 ** zMaster must
1183b 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 point to a buffe
1183c 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d r of at least nM
1183d 61 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f aster bytes allo
1183e 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 cated by.** the
1183f 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 6f caller. This sho
11840 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f 76 uld be sqlite3_v
11841 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20 fs.mxPathname+1
11842 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72 65 (to ensure there
11843 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 70 is.** enough sp
11844 61 63 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 ace to write the
11845 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11846 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d 61 name). If the ma
11847 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 ster journal.**
11848 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 name in the jour
11849 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 68 nal is longer th
1184a 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 an nMaster bytes
1184b 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a 2a (including a.**
1184c 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 nul-terminator)
1184d 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 68 , then this is h
1184e 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f 20 andled as if no
1184f 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
11850 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 ame.** were pres
11851 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ent in the journ
11852 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d al..**.** If a m
11853 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
11854 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65 le name is prese
11855 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 nt at the end of
11856 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 the journal.**
11857 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 file, then it is
11858 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 copied into the
11859 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 buffer pointed
1185a 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 2e 20 41 to by zMaster. A
1185b 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 .** nul-terminat
1185c 6f 72 20 62 79 74 65 20 69 73 20 61 70 70 65 6e or byte is appen
1185d 64 65 64 20 74 6f 20 74 68 65 20 62 75 66 66 65 ded to the buffe
1185e 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 r following the
1185f 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 master.** journa
11860 6c 20 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a 2a 0a l file name..**.
11861 2a 2a 20 49 66 20 69 74 20 69 73 20 64 65 74 65 ** If it is dete
11862 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6d rmined that no m
11863 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
11864 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65 le name is prese
11865 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 65 72 5b 30 nt .** zMaster[0
11866 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e ] is set to 0 an
11867 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 d SQLITE_OK retu
11868 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 rned..**.** If a
11869 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 n error occurs w
1186a 68 69 6c 65 20 72 65 61 64 69 6e 67 20 66 72 6f hile reading fro
1186b 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 m the journal fi
1186c 6c 65 2c 20 61 6e 20 53 51 4c 69 74 65 0a 2a 2a le, an SQLite.**
1186d 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
1186e 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 eturned..*/.stat
1186f 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 74 65 ic int readMaste
11870 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 rJournal(sqlite3
11871 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63 68 _file *pJrnl, ch
11872 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75 33 32 ar *zMaster, u32
11873 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 nMaster){. int
11874 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
11875 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
11876 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 33 32 20 n code */. u32
11877 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 len;
11878 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
11879 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6d 61 73 in bytes of mas
1187a 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
1187b 20 2a 2f 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 */. i64 szJ;
1187c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1187d 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 69 /* Total size i
1187e 6e 20 62 79 74 65 73 20 6f 66 20 6a 6f 75 72 6e n bytes of journ
1187f 61 6c 20 66 69 6c 65 20 70 4a 72 6e 6c 20 2a 2f al file pJrnl */
11880 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 . u32 cksum;
11881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11882 20 4d 4a 20 63 68 65 63 6b 73 75 6d 20 76 61 6c MJ checksum val
11883 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6a 6f 75 ue read from jou
11884 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b rnal */. u32 u;
11885 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11886 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 /* Unsigned
11887 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f loop counter */
11888 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
11889 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 2f 2a aMagic[8]; /*
1188a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c A buffer to hol
1188b 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64 d the magic head
1188c 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 5b er */. zMaster[
1188d 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 69 66 0] = '\0';.. if
1188e 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
1188f 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
11890 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a 4a Size(pJrnl, &szJ
11891 29 29 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31 36 0a )). || szJ<16.
11892 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 || SQLITE_OK!
11893 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 =(rc = read32bit
11894 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c s(pJrnl, szJ-16,
11895 20 26 6c 65 6e 29 29 0a 20 20 20 7c 7c 20 6c 65 &len)). || le
11896 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a 20 20 20 7c n>=nMaster . |
11897 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 | SQLITE_OK!=(rc
11898 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a = read32bits(pJ
11899 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b rnl, szJ-12, &ck
1189a 73 75 6d 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 sum)). || SQLI
1189b 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c TE_OK!=(rc = sql
1189c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c ite3OsRead(pJrnl
1189d 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a 4a , aMagic, 8, szJ
1189e 2d 38 29 29 0a 20 20 20 7c 7c 20 6d 65 6d 63 6d -8)). || memcm
1189f 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e p(aMagic, aJourn
118a0 61 6c 4d 61 67 69 63 2c 20 38 29 0a 20 20 20 7c alMagic, 8). |
118a1 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 | SQLITE_OK!=(rc
118a2 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
118a3 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c (pJrnl, zMaster,
118a4 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e len, szJ-16-len
118a5 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 )). ){. retu
118a6 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
118a7 20 53 65 65 20 69 66 20 74 68 65 20 63 68 65 63 See if the chec
118a8 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 68 65 ksum matches the
118a9 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
118aa 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75 3d name */. for(u=
118ab 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b 0a 0; u<len; u++){.
118ac 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d 61 cksum -= zMa
118ad 73 74 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20 20 69 ster[u];. }. i
118ae 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 f( cksum ){.
118af 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63 6b 73 /* If the checks
118b0 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64 20 75 um doesn't add u
118b1 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 20 6d p, then one or m
118b2 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73 6b 20 ore of the disk
118b3 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a 20 63 sectors. ** c
118b4 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 ontaining the ma
118b5 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
118b6 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75 70 74 ename is corrupt
118b7 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73 0a 20 ed. This means.
118b8 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65 6c 79 ** definitely
118b9 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f 20 6a roll back, so j
118ba 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ust return SQLIT
118bb 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72 74 20 E_OK and report
118bc 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a 20 6d a (nul). ** m
118bd 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 aster-journal fi
118be 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f 0a 20 lename.. */.
118bf 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 7d 0a len = 0;. }.
118c0 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 3d zMaster[len] =
118c1 20 27 5c 30 27 3b 0a 20 20 20 0a 20 20 72 65 74 '\0';. . ret
118c2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
118c3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
118c4 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 he offset of the
118c5 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 sector boundary
118c6 20 61 74 20 6f 72 20 69 6d 6d 65 64 69 61 74 65 at or immediate
118c7 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 ly .** following
118c8 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 70 50 the value in pP
118c9 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
118ca 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 73 65 63 , assuming a sec
118cb 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 tor .** size of
118cc 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
118cd 7a 65 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 ze bytes..**.**
118ce 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 6f 72 i.e for a sector
118cf 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a size of 512:.**
118d0 0a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f 75 72 .** Pager.jour
118d1 6e 61 6c 4f 66 66 20 20 20 20 20 20 20 20 20 20 nalOff
118d2 52 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 Return value.**
118d3 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
118d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
118d5 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 30 ---------.** 0
118d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118d7 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 20 20 0.**
118d8 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20 512
118d9 20 20 20 20 20 20 20 20 20 20 35 31 32 0a 2a 2a 512.**
118da 20 20 20 31 30 30 20 20 20 20 20 20 20 20 20 20 100
118db 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 32 512
118dc 0a 2a 2a 20 20 20 32 30 30 30 20 20 20 20 20 20 .** 2000
118dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118de 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74 2048.** .*/.stat
118df 69 63 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 64 ic i64 journalHd
118e0 72 4f 66 66 73 65 74 28 50 61 67 65 72 20 2a 70 rOffset(Pager *p
118e1 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f 66 Pager){. i64 of
118e2 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 20 fset = 0;. i64
118e3 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 c = pPager->jour
118e4 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 20 nalOff;. if( c
118e5 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 ){. offset =
118e6 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f 48 ((c-1)/JOURNAL_H
118e7 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 DR_SZ(pPager) +
118e8 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 1) * JOURNAL_HDR
118e9 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d _SZ(pPager);. }
118ea 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 . assert( offse
118eb 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a t%JOURNAL_HDR_SZ
118ec 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20 (pPager)==0 );.
118ed 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e assert( offset>
118ee 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =c );. assert(
118ef 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e (offset-c)<JOURN
118f0 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
118f1 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f 66 ) );. return of
118f2 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 fset;.}../*.** T
118f3 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
118f4 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 must be open whe
118f5 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
118f6 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a is called..**.**
118f7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
118f8 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 s a no-op if the
118f9 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
118fa 73 20 6e 6f 74 20 62 65 65 6e 20 77 72 69 74 74 s not been writt
118fb 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 68 69 6e 20 en to.** within
118fc 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e the current tran
118fd 73 61 63 74 69 6f 6e 20 28 69 2e 65 2e 20 69 66 saction (i.e. if
118fe 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 Pager.journalOf
118ff 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 f==0)..**.** If
11900 64 6f 54 72 75 6e 63 61 74 65 20 69 73 20 6e 6f doTruncate is no
11901 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 65 20 50 61 n-zero or the Pa
11902 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c ger.journalSizeL
11903 69 6d 69 74 20 76 61 72 69 61 62 6c 65 20 69 73 imit variable is
11904 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2c 20 74 68 .** set to 0, th
11905 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68 65 20 en truncate the
11906 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 journal file to
11907 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 zero bytes in si
11908 7a 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a ze. Otherwise,.*
11909 2a 20 7a 65 72 6f 20 74 68 65 20 32 38 2d 62 79 * zero the 28-by
1190a 74 65 20 68 65 61 64 65 72 20 61 74 20 74 68 65 te header at the
1190b 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f start of the jo
1190c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 6e 20 65 urnal file. In e
1190d 69 74 68 65 72 20 63 61 73 65 2c 20 0a 2a 2a 20 ither case, .**
1190e 69 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 if the pager is
1190f 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d not in no-sync m
11910 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f ode, sync the jo
11911 75 72 6e 61 6c 20 66 69 6c 65 20 69 6d 6d 65 64 urnal file immed
11912 69 61 74 65 6c 79 20 0a 2a 2a 20 61 66 74 65 72 iately .** after
11913 20 77 72 69 74 69 6e 67 20 6f 72 20 74 72 75 6e writing or trun
11914 63 61 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a cating it..**.**
11915 20 49 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 If Pager.journa
11916 6c 53 69 7a 65 4c 69 6d 69 74 20 69 73 20 73 65 lSizeLimit is se
11917 74 20 74 6f 20 61 20 70 6f 73 69 74 69 76 65 2c t to a positive,
11918 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c non-zero value,
11919 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e and.** followin
1191a 67 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e g the truncation
1191b 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65 73 63 or zeroing desc
1191c 72 69 62 65 64 20 61 62 6f 76 65 20 74 68 65 20 ribed above the
1191d 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 size of the .**
1191e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 journal file in
1191f 62 79 74 65 73 20 69 73 20 6c 61 72 67 65 72 20 bytes is larger
11920 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2c than this value,
11921 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74 then truncate t
11922 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 he.** journal fi
11923 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f 75 72 le to Pager.jour
11924 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62 79 74 nalSizeLimit byt
11925 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 es. The journal
11926 66 69 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 file does.** not
11927 20 6e 65 65 64 20 74 6f 20 62 65 20 73 79 6e 63 need to be sync
11928 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 ed following thi
11929 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a s operation..**.
1192a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f ** If an IO erro
1192b 72 20 6f 63 63 75 72 73 2c 20 61 62 61 6e 64 6f r occurs, abando
1192c 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 n processing and
1192d 20 72 65 74 75 72 6e 20 74 68 65 20 49 4f 20 65 return the IO e
1192e 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 4f 74 rror code..** Ot
1192f 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 herwise, return
11930 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 SQLITE_OK..*/.st
11931 61 74 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f 75 atic int zeroJou
11932 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 rnalHdr(Pager *p
11933 50 61 67 65 72 2c 20 69 6e 74 20 64 6f 54 72 75 Pager, int doTru
11934 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 20 72 63 ncate){. int rc
11935 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
11936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11937 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
11938 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
11939 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
1193a 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 Pager->jfd) );.
1193b 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 if( pPager->jou
1193c 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 63 rnalOff ){. c
1193d 6f 6e 73 74 20 69 36 34 20 69 4c 69 6d 69 74 20 onst i64 iLimit
1193e 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
1193f 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 2f lSizeLimit; /
11940 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 * Local cache of
11941 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 20 49 4f 54 jsl */.. IOT
11942 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 44 52 20 RACE(("JZEROHDR
11943 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a %p\n", pPager)).
11944 20 20 20 20 69 66 28 20 64 6f 54 72 75 6e 63 61 if( doTrunca
11945 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d 30 20 te || iLimit==0
11946 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
11947 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 lite3OsTruncate(
11948 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b pPager->jfd, 0);
11949 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1194a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 static const c
1194b 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d 20 har zeroHdr[28]
1194c 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 72 63 20 = {0};. rc
1194d 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 = sqlite3OsWrite
1194e 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 65 (pPager->jfd, ze
1194f 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a 65 roHdr, sizeof(ze
11950 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 20 20 roHdr), 0);.
11951 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
11952 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61 67 LITE_OK && !pPag
11953 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 er->noSync ){.
11954 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
11955 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a OsSync(pPager->j
11956 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f fd, SQLITE_SYNC_
11957 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72 2d DATAONLY|pPager-
11958 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 >sync_flags);.
11959 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 }.. /* At t
1195a 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 74 72 his point the tr
1195b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d ansaction is com
1195c 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 20 77 mitted but the w
1195d 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 20 2a rite lock . *
1195e 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c 64 20 * is still held
1195f 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20 on the file. If
11960 74 68 65 72 65 20 69 73 20 61 20 73 69 7a 65 20 there is a size
11961 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 65 64 limit configured
11962 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 for . ** the
11963 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 persistent jour
11964 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 nal and the jour
11965 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74 nal file current
11966 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 65 ly consumes more
11967 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 74 68 . ** space th
11968 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 61 6c an that limit al
11969 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e 63 61 lows for, trunca
1196a 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 72 65 te it now. There
1196b 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 is no need.
1196c 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 20 66 ** to sync the f
1196d 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 ile following th
1196e 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 is operation..
1196f 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
11970 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4c =SQLITE_OK && iL
11971 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 imit>0 ){.
11972 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 72 63 i64 sz;. rc
11973 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 = sqlite3OsFile
11974 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 Size(pPager->jfd
11975 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 69 66 , &sz);. if
11976 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
11977 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b 0a && sz>iLimit ){.
11978 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
11979 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
1197a 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 6d Pager->jfd, iLim
1197b 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 it);. }.
1197c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1197d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 rc;.}../*.** The
1197e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 journal file mu
1197f 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 st be open when
11980 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
11981 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 called. A journa
11982 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f 55 l.** header (JOU
11983 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 RNAL_HDR_SZ byte
11984 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e s) is written in
11985 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
11986 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63 75 ile at the.** cu
11987 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 0a rrent location..
11988 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 **.** The format
11989 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c for the journal
1198a 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f header is as fo
1198b 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 79 llows:.** - 8 by
1198c 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e 74 tes: Magic ident
1198d 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 ifying journal f
1198e 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 79 ormat..** - 4 by
1198f 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72 tes: Number of r
11990 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61 ecords in journa
11991 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63 l, or -1 no-sync
11992 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a 20 mode is on..**
11993 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64 6f - 4 bytes: Rando
11994 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66 6f m number used fo
11995 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a 20 r page hash..**
11996 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74 69 - 4 bytes: Initi
11997 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 65 al database page
11998 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 62 count..** - 4 b
11999 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69 7a ytes: Sector siz
1199a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72 e used by the pr
1199b 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 65 ocess that wrote
1199c 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a 2a this journal..*
1199d 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61 74 * - 4 bytes: Dat
1199e 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e abase page size.
1199f 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64 .** .** Followed
119a0 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 by (JOURNAL_HDR
119a1 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20 _SZ - 28) bytes
119a2 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 2e of unused space.
119a3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
119a4 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50 riteJournalHdr(P
119a5 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
119a6 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
119a7 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
119a8 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
119a9 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ode */. char *z
119aa 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d Header = pPager-
119ab 3e 70 54 6d 70 53 70 61 63 65 3b 20 20 2f 2a 20 >pTmpSpace; /*
119ac 54 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65 20 Temporary space
119ad 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 68 65 used to build he
119ae 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 48 ader */. u32 nH
119af 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e eader = pPager->
119b0 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a pageSize; /*
119b1 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 Size of buffer
119b2 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 48 pointed to by zH
119b3 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e eader */. u32 n
119b4 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 Write;
119b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
119b6 2a 20 42 79 74 65 73 20 6f 66 20 68 65 61 64 65 * Bytes of heade
119b7 72 20 73 65 63 74 6f 72 20 77 72 69 74 74 65 6e r sector written
119b8 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 */. int ii;
119b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
119bb 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 p counter */..
119bc 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
119bd 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20 Pager->jfd) );
119be 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66 /* Journal f
119bf 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e ile must be open
119c0 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 48 65 61 . */.. if( nHea
119c1 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f der>JOURNAL_HDR_
119c2 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 SZ(pPager) ){.
119c3 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f 55 52 nHeader = JOUR
119c4 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
119c5 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 r);. }.. /* If
119c6 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76 there are activ
119c7 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 e savepoints and
119c8 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 65 72 any of them wer
119c9 65 20 63 72 65 61 74 65 64 20 0a 20 20 2a 2a 20 e created . **
119ca 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20 72 since the most r
119cb 65 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 68 65 ecent journal he
119cc 61 64 65 72 20 77 61 73 20 77 72 69 74 74 65 6e ader was written
119cd 2c 20 75 70 64 61 74 65 20 74 68 65 20 0a 20 20 , update the .
119ce 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e ** PagerSavepoin
119cf 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 t.iHdrOffset fie
119d0 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 lds now.. */.
119d1 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 for(ii=0; ii<pPa
119d2 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
119d3 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ii++){. if(
119d4 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
119d5 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 nt[ii].iHdrOffse
119d6 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 50 t==0 ){. pP
119d7 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
119d8 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 74 20 [ii].iHdrOffset
119d9 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 = pPager->journa
119da 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a lOff;. }. }.
119db 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e . pPager->journ
119dc 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d 3e alHdr = pPager->
119dd 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 journalOff = jou
119de 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 rnalHdrOffset(pP
119df 61 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20 ager);.. /* .
119e0 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 ** Write the nRe
119e1 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e 75 c Field - the nu
119e2 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 63 mber of page rec
119e3 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 ords that follow
119e4 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 6e this. ** journ
119e5 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d 61 al header. Norma
119e6 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72 69 lly, zero is wri
119e7 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61 6c tten to this val
119e8 75 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e ue at this time.
119e9 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65 20 . ** After the
119ea 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 64 65 records are adde
119eb 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c d to the journal
119ec 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 (and the journa
119ed 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a 20 l synced, . **
119ee 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 if in full-sync
119ef 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f 20 mode), the zero
119f0 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 is overwritten w
119f1 69 74 68 20 74 68 65 20 74 72 75 65 20 6e 75 6d ith the true num
119f2 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63 6f ber. ** of reco
119f3 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f 75 rds (see syncJou
119f4 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 20 rnal()).. **.
119f5 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74 65 ** A faster alte
119f6 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77 72 rnative is to wr
119f7 69 74 65 20 30 78 46 46 46 46 46 46 46 46 20 74 ite 0xFFFFFFFF t
119f8 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 o the nRec field
119f9 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64 . When. ** read
119fa 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ing the journal
119fb 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c 73 this value tells
119fc 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75 6d SQLite to assum
119fd 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 e that the. **
119fe 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 rest of the jour
119ff 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e nal file contain
11a00 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63 s valid page rec
11a01 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 75 6d ords. This assum
11a02 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 61 ption. ** is da
11a03 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20 61 ngerous, as if a
11a04 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65 failure occurre
11a05 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 6e 67 d whilst writing
11a06 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a to the journal.
11a07 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61 79 ** file it may
11a08 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67 61 contain some ga
11a09 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 65 72 rbage data. Ther
11a0a 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e 61 72 e are two scenar
11a0b 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 74 ios. ** where t
11a0c 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 65 20 his risk can be
11a0d 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 20 ignored:. **.
11a0e 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20 ** * When the
11a0f 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d 73 pager is in no-s
11a10 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 70 ync mode. Corrup
11a11 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 20 tion can follow
11a12 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 72 a. ** power
11a13 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69 73 failure in this
11a14 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a 20 20 case anyway..
11a15 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e **. ** * When
11a16 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 the SQLITE_IOCA
11a17 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 66 6c P_SAFE_APPEND fl
11a18 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 ag is set. This
11a19 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a 20 guarantees. **
11a1a 20 20 20 20 74 68 61 74 20 67 61 72 62 61 67 65 that garbage
11a1b 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20 61 data is never a
11a1c 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6a ppended to the j
11a1d 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 2a ournal file.. *
11a1e 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 /. assert( isOp
11a1f 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c en(pPager->fd) |
11a20 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 | pPager->noSync
11a21 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 );. if( (pPage
11a22 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28 70 r->noSync) || (p
11a23 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
11a24 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
11a25 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20 20 LMODE_MEMORY).
11a26 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44 65 || (sqlite3OsDe
11a27 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
11a28 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 26 ics(pPager->fd)&
11a29 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 SQLITE_IOCAP_SAF
11a2a 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b 0a E_APPEND) . ){.
11a2b 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64 memcpy(zHead
11a2c 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 er, aJournalMagi
11a2d 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e c, sizeof(aJourn
11a2e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 20 20 70 alMagic));. p
11a2f 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
11a30 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
11a31 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 66 66 lMagic)], 0xffff
11a32 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a ffff);. }else{.
11a33 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 65 61 64 memset(zHead
11a34 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 4a er, 0, sizeof(aJ
11a35 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 29 3b ournalMagic)+4);
11a36 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 72 . }.. /* The r
11a37 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 73 68 andom check-hash
11a38 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a 2f 20 initialiser */
11a39 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f . sqlite3_rando
11a3a 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 50 61 mness(sizeof(pPa
11a3b 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 2c ger->cksumInit),
11a3c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 &pPager->cksumI
11a3d 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 69 74 nit);. put32bit
11a3e 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f s(&zHeader[sizeo
11a3f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
11a40 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 +4], pPager->cks
11a41 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 54 68 umInit);. /* Th
11a42 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 62 61 e initial databa
11a43 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75 74 se size */. put
11a44 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 32bits(&zHeader[
11a45 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d sizeof(aJournalM
11a46 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 65 72 agic)+8], pPager
11a47 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0a 20 ->dbOrigSize);.
11a48 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 64 20 /* The assumed
11a49 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f 72 20 sector size for
11a4a 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a 2f 0a this process */.
11a4b 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 put32bits(&zHe
11a4c 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 ader[sizeof(aJou
11a4d 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d 2c 20 rnalMagic)+12],
11a4e 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
11a4f 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 ze);.. /* The p
11a50 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75 age size */. pu
11a51 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 t32bits(&zHeader
11a52 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c [sizeof(aJournal
11a53 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 61 67 Magic)+16], pPag
11a54 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a er->pageSize);..
11a55 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69 6e /* Initializin
11a56 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 g the tail of th
11a57 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 e buffer is not
11a58 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76 65 72 necessary. Ever
11a59 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f 72 6b ything. ** work
11a5a 73 20 66 69 6e 64 20 69 66 20 74 68 65 20 66 6f s find if the fo
11a5b 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74 28 29 llowing memset()
11a5c 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 is omitted. Bu
11a5d 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 0a 20 t initializing.
11a5e 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 70 ** the memory p
11a5f 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69 6e 64 revents valgrind
11a60 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e 69 6e from complainin
11a61 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77 69 6c g, so we are wil
11a62 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 61 6b ling to. ** tak
11a63 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 e the performanc
11a64 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 6d 65 e hit.. */. me
11a65 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b 73 69 mset(&zHeader[si
11a66 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11a67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20 20 20 ic)+20], 0,.
11a68 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28 73 69 nHeader-(si
11a69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11a6a 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f 2a 20 ic)+20));.. /*
11a6b 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 69 73 In theory, it is
11a6c 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 79 20 only necessary
11a6d 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 38 20 to write the 28
11a6e 62 79 74 65 73 20 74 68 61 74 20 74 68 65 20 0a bytes that the .
11a6f 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 ** journal hea
11a70 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 6f 20 der consumes to
11a71 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11a72 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e 63 72 here. Then incr
11a73 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a 2a 20 ement the . **
11a74 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 Pager.journalOff
11a75 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a 4f 55 variable by JOU
11a76 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f 20 74 RNAL_HDR_SZ so t
11a77 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a 20 20 hat the next .
11a78 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77 72 69 ** record is wri
11a79 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f 6c 6c tten to the foll
11a7a 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28 6c 65 owing sector (le
11a7b 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e 20 74 aving a gap in t
11a7c 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 68 61 he file. ** tha
11a7d 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c 69 63 t will be implic
11a7e 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e 20 62 itly filled in b
11a7f 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a 2a 0a y the OS).. **.
11a80 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69 74 20 ** However it
11a81 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f 76 65 has been discove
11a82 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f 6d 65 red that on some
11a83 20 73 79 73 74 65 6d 73 20 74 68 69 73 20 70 61 systems this pa
11a84 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a 2a 20 ttern can . **
11a85 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 be significantly
11a86 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63 6f 6e slower than con
11a87 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74 69 6e tiguously writin
11a88 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 g data to the fi
11a89 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66 le,. ** even if
11a8a 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78 70 6c that means expl
11a8b 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67 20 64 icitly writing d
11a8c 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b ata to the block
11a8d 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55 52 4e of . ** (JOURN
11a8e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20 AL_HDR_SZ - 28)
11a8f 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20 bytes that will
11a90 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53 6f 20 not be used. So
11a91 74 68 61 74 20 69 73 20 77 68 61 74 0a 20 20 2a that is what. *
11a92 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20 2a 2a * is done. . **
11a93 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70 20 69 . ** The loop i
11a94 73 20 72 65 71 75 69 72 65 64 20 68 65 72 65 20 s required here
11a95 69 6e 20 63 61 73 65 20 74 68 65 20 73 65 63 74 in case the sect
11a96 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 67 65 or-size is large
11a97 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a r than the . **
11a98 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73 database page s
11a99 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65 20 7a ize. Since the z
11a9a 48 65 61 64 65 72 20 62 75 66 66 65 72 20 69 73 Header buffer is
11a9b 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61 67 65 only Pager.page
11a9c 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65 73 20 Size. ** bytes
11a9d 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20 74 68 in size, more th
11a9e 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73 an one call to s
11a9f 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 20 qlite3OsWrite()
11aa0 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 64 0a may be required.
11aa1 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 74 65 ** to populate
11aa2 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 72 the entire jour
11aa3 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63 74 6f nal header secto
11aa4 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 6e r.. */ . for(n
11aa5 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51 4c Write=0; rc==SQL
11aa6 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c 4a ITE_OK&&nWrite<J
11aa7 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
11aa8 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d 6e ager); nWrite+=n
11aa9 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f 54 Header){. IOT
11aaa 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 25 RACE(("JHDR %p %
11aab 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 lld %d\n", pPage
11aac 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e r, pPager->journ
11aad 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29 29 alHdr, nHeader))
11aae 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
11aaf 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3OsWrite(pPager-
11ab0 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 6e >jfd, zHeader, n
11ab1 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d 3e Header, pPager->
11ab2 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 20 journalOff);.
11ab3 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11ab4 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b 0a Off += nHeader;.
11ab5 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
11ab6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a ;.}../*.** The j
11ab7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
11ab8 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 be open when th
11ab9 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20 is is called. A
11aba 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
11abb 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f ile.** (JOURNAL_
11abc 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73 HDR_SZ bytes) is
11abd 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 63 read from the c
11abe 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 urrent location
11abf 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a in the journal.*
11ac0 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72 72 * file. The curr
11ac1 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 ent location in
11ac2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11ac3 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 is given by.**
11ac4 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11ac5 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 ff. See comments
11ac6 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 above function
11ac7 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
11ac8 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 63 72 ) for.** a descr
11ac9 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f iption of the jo
11aca 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f 72 urnal header for
11acb 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 mat..**.** If th
11acc 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 64 e header is read
11acd 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 2a successfully, *
11ace 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 pNRec is set to
11acf 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a the number of.**
11ad0 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 6f page records fo
11ad1 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 61 llowing this hea
11ad2 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 7a 65 der and *pDbSize
11ad3 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 is set to the s
11ad4 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 ize of the.** da
11ad5 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74 68 tabase before th
11ad6 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 e transaction be
11ad7 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41 gan, in pages. A
11ad8 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 lso, pPager->cks
11ad9 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74 umInit.** is set
11ada 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65 to the value re
11adb 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ad from the jour
11adc 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49 nal header. SQLI
11add 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
11ade 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 d.** in this cas
11adf 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
11ae0 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 journal header f
11ae1 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 ile appears to b
11ae2 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c e corrupted, SQL
11ae3 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72 ITE_DONE is.** r
11ae4 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 4e 52 eturned and *pNR
11ae5 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a 65 20 ec and *PDbSize
11ae6 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 are undefined.
11ae7 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 If JOURNAL_HDR_S
11ae8 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f Z bytes.** canno
11ae9 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 t be read from t
11aea 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11aeb 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 an error code is
11aec 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
11aed 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75 atic int readJou
11aee 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72 rnalHdr(. Pager
11aef 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 *pPager,
11af0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 /* Pager
11af1 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 object */. int
11af2 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 6a 6f isHot,. i64 jo
11af3 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 20 20 urnalSize,
11af4 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
11af5 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 72 6e f the open journ
11af6 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 al file in bytes
11af7 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 65 63 */. u32 *pNRec
11af8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11af9 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 /* OUT: Value
11afa 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6e read from the n
11afb 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 20 75 Rec field */. u
11afc 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 20 20 32 *pDbSize
11afd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
11afe 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f 72 69 UT: Value of ori
11aff 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73 ginal database s
11b00 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a ize field */.){.
11b01 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
11b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11b03 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
11b04 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
11b05 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 20 20 aMagic[8];
11b06 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 /* A buffer to h
11b07 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 old the magic he
11b08 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 ader */. i64 iH
11b09 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 drOff;
11b0a 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
11b0b 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 of journal head
11b0c 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f er being read */
11b0d 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 .. assert( isOp
11b0e 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
11b0f 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e ); /* Journ
11b10 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 al file must be
11b11 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41 open. */.. /* A
11b12 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a 6f 75 dvance Pager.jou
11b13 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 20 73 rnalOff to the s
11b14 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 tart of the next
11b15 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 65 0a sector. If the.
11b16 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ** journal fil
11b17 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 e is too small f
11b18 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 61 or there to be a
11b19 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 20 61 header stored a
11b1a 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e t this. ** poin
11b1b 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 t, return SQLITE
11b1c 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70 50 _DONE.. */. pP
11b1d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11b1e 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 = journalHdrOff
11b1f 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 set(pPager);. i
11b20 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
11b21 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44 alOff+JOURNAL_HD
11b22 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e 20 6a R_SZ(pPager) > j
11b23 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20 ournalSize ){.
11b24 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
11b25 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 64 72 DONE;. }. iHdr
11b26 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f Off = pPager->jo
11b27 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 urnalOff;.. /*
11b28 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 72 73 Read in the firs
11b29 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 t 8 bytes of the
11b2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e journal header.
11b2b 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 If they do not
11b2c 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 20 20 match. ** the
11b2d 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 6f 75 magic string fou
11b2e 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 nd at the start
11b2f 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20 of each journal
11b30 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e 0a 20 header, return.
11b31 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e ** SQLITE_DONE.
11b32 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
11b33 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 61 occurs, return a
11b34 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 n error code. Ot
11b35 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 70 72 herwise,. ** pr
11b36 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 oceed.. */. if
11b37 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 72 4f ( isHot || iHdrO
11b38 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 ff!=pPager->jour
11b39 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 72 63 nalHdr ){. rc
11b3a 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 = sqlite3OsRead
11b3b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d (pPager->jfd, aM
11b3c 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61 agic, sizeof(aMa
11b3d 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 3b 0a gic), iHdrOff);.
11b3e 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
11b3f 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
11b40 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d }. if( mem
11b41 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 cmp(aMagic, aJou
11b42 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f rnalMagic, sizeo
11b43 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29 7b f(aMagic))!=0 ){
11b44 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
11b45 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d LITE_DONE;. }
11b46 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 . }.. /* Read
11b47 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20 the first three
11b48 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 6f 66 32-bit fields of
11b49 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
11b4a 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a 20 20 der: The nRec.
11b4b 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 63 68 ** field, the ch
11b4c 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c 69 7a ecksum-initializ
11b4d 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 er and the datab
11b4e 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 65 20 ase size at the
11b4f 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68 start. ** of th
11b50 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 52 e transaction. R
11b51 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
11b52 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ode if anything
11b53 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f goes wrong.. */
11b54 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b . if( SQLITE_OK
11b55 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 !=(rc = read32bi
11b56 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
11b57 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 65 63 iHdrOff+8, pNRec
11b58 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f )). || SQLITE_
11b59 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
11b5a 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11b5b 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 26 70 , iHdrOff+12, &p
11b5c 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 Pager->cksumInit
11b5d 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f )). || SQLITE_
11b5e 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
11b5f 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11b60 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 70 44 , iHdrOff+16, pD
11b61 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 20 20 bSize)). ){.
11b62 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
11b63 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a . if( pPager->j
11b64 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a ournalOff==0 ){.
11b65 20 20 20 20 75 33 32 20 69 50 61 67 65 53 69 7a u32 iPageSiz
11b66 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
11b67 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 66 69 /* Page-size fi
11b68 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 eld of journal h
11b69 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 eader */. u32
11b6a 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 20 20 iSectorSize;
11b6b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 /* Sec
11b6c 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f tor-size field o
11b6d 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 f journal header
11b6e 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 61 67 */. u16 iPag
11b6f 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 20 20 eSize16;
11b70 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 /* Copy of
11b71 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 36 2d iPageSize in 16-
11b72 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a bit variable */.
11b73 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 . /* Read the
11b74 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 page-size and s
11b75 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e ector-size journ
11b76 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64 73 al header fields
11b77 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c . */. if( SQL
11b78 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
11b79 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d ad32bits(pPager-
11b7a 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 30 >jfd, iHdrOff+20
11b7b 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29 29 , &iSectorSize))
11b7c 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f . || SQLITE_
11b7d 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
11b7e 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 bits(pPager->jfd
11b7f 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 69 , iHdrOff+24, &i
11b80 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20 29 PageSize)). )
11b81 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
11b82 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a c;. }.. /*
11b83 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 Check that the
11b84 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f 6d values read from
11b85 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 the page-size a
11b86 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 66 nd sector-size f
11b87 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 72 65 ields. ** are
11b88 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 54 within range. T
11b89 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 27 2c o be 'in range',
11b8a 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e 65 65 both values nee
11b8b 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 0a d to be a power.
11b8c 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 67 72 ** of two gr
11b8d 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 eater than or eq
11b8e 75 61 6c 20 74 6f 20 35 31 32 2c 20 61 6e 64 20 ual to 512, and
11b8f 6e 6f 74 20 67 72 65 61 74 65 72 20 74 68 61 6e not greater than
11b90 20 74 68 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 their . ** r
11b91 65 73 70 65 63 74 69 76 65 20 63 6f 6d 70 69 6c espective compil
11b92 65 20 74 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c e time maximum l
11b93 69 6d 69 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 imits.. */.
11b94 20 20 69 66 28 20 69 50 61 67 65 53 69 7a 65 3c if( iPageSize<
11b95 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20 512
11b96 20 20 20 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 || iSectorS
11b97 69 7a 65 3c 35 31 32 0a 20 20 20 20 20 7c 7c 20 ize<512. ||
11b98 69 50 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 iPageSize>SQLITE
11b99 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 7c _MAX_PAGE_SIZE |
11b9a 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41 | iSectorSize>MA
11b9b 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 20 20 X_SECTOR_SIZE.
11b9c 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53 69 7a || ((iPageSiz
11b9d 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65 29 21 e-1)&iPageSize)!
11b9e 3d 30 20 20 20 7c 7c 20 28 28 69 53 65 63 74 6f =0 || ((iSecto
11b9f 72 53 69 7a 65 2d 31 29 26 69 53 65 63 74 6f 72 rSize-1)&iSector
11ba0 53 69 7a 65 29 21 3d 30 20 0a 20 20 20 20 29 7b Size)!=0 . ){
11ba1 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
11ba2 20 65 69 74 68 65 72 20 74 68 65 20 70 61 67 65 either the page
11ba3 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 6f 72 2d -size or sector-
11ba4 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 size in the jour
11ba5 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 0a 20 nal-header is .
11ba6 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c 69 64 2c ** invalid,
11ba7 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63 65 73 then the proces
11ba8 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 65 s that wrote the
11ba9 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 journal-header
11baa 6d 75 73 74 20 68 61 76 65 20 0a 20 20 20 20 20 must have .
11bab 20 2a 2a 20 63 72 61 73 68 65 64 20 62 65 66 6f ** crashed befo
11bac 72 65 20 74 68 65 20 68 65 61 64 65 72 20 77 61 re the header wa
11bad 73 20 73 79 6e 63 65 64 2e 20 49 6e 20 74 68 69 s synced. In thi
11bae 73 20 63 61 73 65 20 73 74 6f 70 20 72 65 61 64 s case stop read
11baf 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 ing . ** th
11bb0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 e journal file h
11bb1 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ere.. */.
11bb2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
11bb3 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 E_DONE;. }..
11bb4 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 /* Update the
11bb5 20 70 61 67 65 2d 73 69 7a 65 20 74 6f 20 6d 61 page-size to ma
11bb6 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 72 65 tch the value re
11bb7 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 ad from the jour
11bb8 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a 20 55 73 65 nal. . ** Use
11bb9 20 61 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 a testcase() ma
11bba 63 72 6f 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 cro to make sure
11bbb 20 74 68 61 74 20 6d 61 6c 6c 6f 63 20 66 61 69 that malloc fai
11bbc 6c 75 72 65 20 77 69 74 68 69 6e 20 0a 20 20 20 lure within .
11bbd 20 2a 2a 20 50 61 67 65 72 53 65 74 50 61 67 65 ** PagerSetPage
11bbe 73 69 7a 65 28 29 20 69 73 20 74 65 73 74 65 64 size() is tested
11bbf 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 50 61 .. */. iPa
11bc0 67 65 53 69 7a 65 31 36 20 3d 20 28 75 31 36 29 geSize16 = (u16)
11bc1 69 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 iPageSize;. r
11bc2 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
11bc3 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 67 SetPagesize(pPag
11bc4 65 72 2c 20 26 69 50 61 67 65 53 69 7a 65 31 36 er, &iPageSize16
11bc5 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63 , -1);. testc
11bc6 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ase( rc!=SQLITE_
11bc7 4f 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 OK );. assert
11bc8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
11bc9 7c 7c 20 69 50 61 67 65 53 69 7a 65 31 36 3d 3d || iPageSize16==
11bca 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 20 29 (u16)iPageSize )
11bcb 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 ;.. /* Update
11bcc 20 74 68 65 20 61 73 73 75 6d 65 64 20 73 65 63 the assumed sec
11bcd 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 tor-size to matc
11bce 68 20 74 68 65 20 76 61 6c 75 65 20 75 73 65 64 h the value used
11bcf 20 62 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 by . ** the
11bd0 70 72 6f 63 65 73 73 20 74 68 61 74 20 63 72 65 process that cre
11bd1 61 74 65 64 20 74 68 69 73 20 6a 6f 75 72 6e 61 ated this journa
11bd2 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f 75 72 6e l. If this journ
11bd3 61 6c 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 72 al was. ** cr
11bd4 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 eated by a proce
11bd5 73 73 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 ss other than th
11bd6 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 74 68 69 is one, then thi
11bd7 73 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 2a 2a s routine. **
11bd8 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 is being called
11bd9 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 from within pag
11bda 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e 20 54 er_playback(). T
11bdb 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 0a 20 he local value.
11bdc 20 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 2e 73 ** of Pager.s
11bdd 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 65 73 ectorSize is res
11bde 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 6e 64 tored at the end
11bdf 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 6e 65 of that routine
11be0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 .. */. pPa
11be1 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 ger->sectorSize
11be2 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20 = iSectorSize;.
11be3 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f }.. pPager->jo
11be4 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52 urnalOff += JOUR
11be5 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
11be6 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b r);. return rc;
11be7 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 .}.../*.** Write
11be8 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6d 61 the supplied ma
11be9 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d ster journal nam
11bea 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e e into the journ
11beb 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 al file for page
11bec 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 20 74 r.** pPager at t
11bed 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 he current locat
11bee 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 72 20 ion. The master
11bef 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73 journal name mus
11bf0 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a 2a 2a t be the last.**
11bf1 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e 20 74 thing written t
11bf2 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 o a journal file
11bf3 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 . If the pager i
11bf4 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d s in full-sync m
11bf5 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 ode, the.** jour
11bf6 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
11bf7 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 64 20 tor is advanced
11bf8 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 to the next sect
11bf9 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 66 6f or boundary befo
11bfa 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 69 re.** anything i
11bfb 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 20 66 s written. The f
11bfc 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 ormat is:.**.**
11bfd 20 20 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47 + 4 bytes: PAG
11bfe 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 ER_MJ_PGNO..**
11bff 20 2b 20 4e 20 62 79 74 65 73 3a 20 4d 61 73 74 + N bytes: Mast
11c00 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e er journal filen
11c01 61 6d 65 20 69 6e 20 75 74 66 2d 38 2e 0a 2a 2a ame in utf-8..**
11c02 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4e 20 + 4 bytes: N
11c03 28 6c 65 6e 67 74 68 20 6f 66 20 6d 61 73 74 65 (length of maste
11c04 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 r journal name i
11c05 6e 20 62 79 74 65 73 2c 20 6e 6f 20 6e 75 6c 2d n bytes, no nul-
11c06 74 65 72 6d 69 6e 61 74 6f 72 29 2e 0a 2a 2a 20 terminator)..**
11c07 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4d 61 73 + 4 bytes: Mas
11c08 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
11c09 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 20 checksum..**
11c0a 2b 20 38 20 62 79 74 65 73 3a 20 61 4a 6f 75 72 + 8 bytes: aJour
11c0b 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a nalMagic[]..**.*
11c0c 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 * The master jou
11c0d 72 6e 61 6c 20 70 61 67 65 20 63 68 65 63 6b 73 rnal page checks
11c0e 75 6d 20 69 73 20 74 68 65 20 73 75 6d 20 6f 66 um is the sum of
11c0f 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68 the bytes in th
11c10 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 e master.** jour
11c11 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 65 72 65 20 nal name, where
11c12 65 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74 each byte is int
11c13 65 72 70 72 65 74 65 64 20 61 73 20 61 20 73 69 erpreted as a si
11c14 67 6e 65 64 20 38 2d 62 69 74 20 69 6e 74 65 67 gned 8-bit integ
11c15 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 er..**.** If zMa
11c16 73 74 65 72 20 69 73 20 61 20 4e 55 4c 4c 20 70 ster is a NULL p
11c17 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 73 20 66 ointer (occurs f
11c18 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 or a single data
11c19 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e base transaction
11c1a 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c ), .** this call
11c1b 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
11c1c 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 static int write
11c1d 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 50 61 MasterJournal(Pa
11c1e 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e ger *pPager, con
11c1f 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 st char *zMaster
11c20 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 ){. int rc;
11c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c22 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
11c23 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d code */. int nM
11c24 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 aster;
11c25 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
11c26 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a ngth of string z
11c27 4d 61 73 74 65 72 20 2a 2f 0a 20 20 69 36 34 20 Master */. i64
11c28 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 iHdrOff;
11c29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11c2a 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 65 72 Offset of header
11c2b 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 in journal file
11c2c 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c 53 69 */. i64 jrnlSi
11c2d 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
11c2e 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
11c2f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f f journal file o
11c30 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 75 33 32 20 n disk */. u32
11c31 63 6b 73 75 6d 20 3d 20 30 3b 20 20 20 20 20 20 cksum = 0;
11c32 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11c33 43 68 65 63 6b 73 75 6d 20 6f 66 20 73 74 72 69 Checksum of stri
11c34 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 0a 20 ng zMaster */..
11c35 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 7c 7c if( !zMaster ||
11c36 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
11c37 65 72 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 2d er. || pPager-
11c38 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
11c39 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
11c3a 4d 45 4d 4f 52 59 20 0a 20 20 20 7c 7c 20 70 50 MEMORY . || pP
11c3b 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
11c3c 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
11c3d 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 29 7b 0a 20 MODE_OFF . ){.
11c3e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
11c3f 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 _OK;. }. pPage
11c40 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 31 r->setMaster = 1
11c41 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 ;. assert( isOp
11c42 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
11c43 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 );.. /* Calcula
11c44 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20 69 6e te the length in
11c45 20 62 79 74 65 73 20 61 6e 64 20 74 68 65 20 63 bytes and the c
11c46 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61 73 74 hecksum of zMast
11c47 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d 61 73 er */. for(nMas
11c48 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72 5b 6e ter=0; zMaster[n
11c49 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74 65 72 Master]; nMaster
11c4a 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b ++){. cksum +
11c4b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65 = zMaster[nMaste
11c4c 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 r];. }.. /* If
11c4d 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f in full-sync mo
11c4e 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20 74 de, advance to t
11c4f 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65 63 he next disk sec
11c50 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74 69 tor before writi
11c51 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73 74 ng. ** the mast
11c52 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e er journal name.
11c53 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73 65 This is in case
11c54 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 the previous pa
11c55 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 ge written to.
11c56 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 ** the journal h
11c57 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
11c58 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 synced.. */. i
11c59 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 f( pPager->fullS
11c5a 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 67 65 ync ){. pPage
11c5b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 r->journalOff =
11c5c 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 journalHdrOffset
11c5d 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 (pPager);. }.
11c5e 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72 iHdrOff = pPager
11c5f 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 ->journalOff;..
11c60 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6d 61 /* Write the ma
11c61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64 61 74 ster journal dat
11c62 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 a to the end of
11c63 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11c64 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 72 72 . If. ** an err
11c65 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 or occurs, retur
11c66 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 n the error code
11c67 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a to the caller..
11c68 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20 21 3d */. if( (0 !=
11c69 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 (rc = write32bi
11c6a 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
11c6b 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52 5f 4d iHdrOff, PAGER_M
11c6c 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 29 29 J_PGNO(pPager)))
11c6d 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 ). || (0 != (r
11c6e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
11c6f 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
11c70 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 zMaster, nMaster
11c71 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29 0a 20 , iHdrOff+4))).
11c72 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d || (0 != (rc =
11c73 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 write32bits(pPa
11c74 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 ger->jfd, iHdrOf
11c75 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e 4d 61 f+4+nMaster, nMa
11c76 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20 28 30 ster))). || (0
11c77 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 != (rc = write3
11c78 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
11c79 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 d, iHdrOff+4+nMa
11c7a 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29 29 29 ster+4, cksum)))
11c7b 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 . || (0 != (rc
11c7c 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
11c7d 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 e(pPager->jfd, a
11c7e 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 2c JournalMagic, 8,
11c7f 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 iHdrOff+4+nMast
11c80 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20 20 20 er+8))). ){.
11c81 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
11c82 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11c83 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74 65 72 lOff += (nMaster
11c84 2b 32 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e +20);. pPager->
11c85 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50 61 67 needSync = !pPag
11c86 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20 20 2f er->noSync;.. /
11c87 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
11c88 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74 2d 6a s in peristent-j
11c89 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74 68 65 ournal mode, the
11c8a 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c 20 0a n the physical .
11c8b 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c ** journal-fil
11c8c 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70 61 73 e may extend pas
11c8d 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
11c8e 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 master-journal
11c8f 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 38 20 name. ** and 8
11c90 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63 20 64 bytes of magic d
11c91 61 74 61 20 6a 75 73 74 20 77 72 69 74 74 65 6e ata just written
11c92 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 68 to the file. Th
11c93 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61 6e 67 is is . ** dang
11c94 65 72 6f 75 73 20 62 65 63 61 75 73 65 20 74 68 erous because th
11c95 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c 62 61 e code to rollba
11c96 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ck a hot-journal
11c97 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 file. ** will
11c98 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 66 not be able to f
11c99 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72 2d 6a ind the master-j
11c9a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f 20 64 ournal name to d
11c9b 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 etermine . ** w
11c9c 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
11c9d 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 e journal is hot
11c9e 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 73 . . **. ** Eas
11c9f 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20 64 6f iest thing to do
11ca0 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61 72 69 in this scenari
11ca1 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61 74 65 o is to truncate
11ca2 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 the journal .
11ca3 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65 20 72 ** file to the r
11ca4 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a 20 20 equired size..
11ca5 2a 2f 20 0a 20 20 69 66 28 20 53 51 4c 49 54 45 */ . if( SQLITE
11ca6 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK==(rc = sqlit
11ca7 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
11ca8 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 ger->jfd, &jrnlS
11ca9 69 7a 65 29 29 0a 20 20 20 26 26 20 6a 72 6e 6c ize)). && jrnl
11caa 53 69 7a 65 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 Size>pPager->jou
11cab 72 6e 61 6c 4f 66 66 0a 20 20 29 7b 0a 20 20 20 rnalOff. ){.
11cac 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 rc = sqlite3OsT
11cad 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
11cae 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 jfd, pPager->jou
11caf 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 7d 0a 20 20 rnalOff);. }.
11cb0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
11cb1 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 65 20 .** Find a page
11cb2 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c in the hash tabl
11cb3 65 20 67 69 76 65 6e 20 69 74 73 20 70 61 67 65 e given its page
11cb4 20 6e 75 6d 62 65 72 2e 20 52 65 74 75 72 6e 0a number. Return.
11cb5 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
11cb6 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55 4c 4c the page or NULL
11cb7 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 if the requeste
11cb8 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 0a 2a d page is not .*
11cb9 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 6d 65 6d * already in mem
11cba 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 ory..*/.static P
11cbb 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b gHdr *pager_look
11cbc 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 up(Pager *pPager
11cbd 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 , Pgno pgno){.
11cbe 50 67 48 64 72 20 2a 70 3b 20 20 20 20 20 20 20 PgHdr *p;
11cbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cc0 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 /* Return valu
11cc1 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 74 20 69 73 e */.. /* It is
11cc2 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f not possible fo
11cc3 72 20 61 20 63 61 6c 6c 20 74 6f 20 50 63 61 63 r a call to Pcac
11cc4 68 65 46 65 74 63 68 28 29 20 77 69 74 68 20 63 heFetch() with c
11cc5 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 74 6f 0a reateFlag==0 to.
11cc6 20 20 2a 2a 20 66 61 69 6c 2c 20 73 69 6e 63 65 ** fail, since
11cc7 20 6e 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 61 no attempt to a
11cc8 6c 6c 6f 63 61 74 65 20 64 79 6e 61 6d 69 63 20 llocate dynamic
11cc9 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 65 20 6d memory will be m
11cca 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 ade.. */. (voi
11ccb 64 29 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 d)sqlite3PcacheF
11ccc 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 etch(pPager->pPC
11ccd 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 ache, pgno, 0, &
11cce 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a p);. return p;.
11ccf 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 }../*.** Unless
11cd0 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11cd1 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 64 69 73 error-state, dis
11cd2 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f card all in-memo
11cd3 72 79 20 70 61 67 65 73 2e 20 49 66 0a 2a 2a 20 ry pages. If.**
11cd4 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11cd5 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 68 65 error-state, the
11cd6 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 n this call is a
11cd7 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f no-op..**.** TO
11cd8 44 4f 3a 20 57 68 79 20 63 61 6e 20 77 65 20 6e DO: Why can we n
11cd9 6f 74 20 72 65 73 65 74 20 74 68 65 20 70 61 67 ot reset the pag
11cda 65 72 20 77 68 69 6c 65 20 69 6e 20 65 72 72 6f er while in erro
11cdb 72 20 73 74 61 74 65 3f 0a 2a 2f 0a 73 74 61 74 r state?.*/.stat
11cdc 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65 ic void pager_re
11cdd 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 set(Pager *pPage
11cde 72 29 7b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 r){. if( SQLITE
11cdf 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d 3e 65 72 72 _OK==pPager->err
11ce0 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 Code ){. sqli
11ce1 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 te3BackupRestart
11ce2 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 (pPager->pBackup
11ce3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 );. sqlite3Pc
11ce4 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 65 72 acheClear(pPager
11ce5 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20 ->pPCache);.
11ce6 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
11ce7 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a lid = 0;. }.}..
11ce8 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 73 /*.** Free all s
11ce9 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65 tructures in the
11cea 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e Pager.aSavepoin
11ceb 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 73 65 t[] array and se
11cec 74 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65 72 2e t both.** Pager.
11ced 61 53 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 50 aSavepoint and P
11cee 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 20 ager.nSavepoint
11cef 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65 20 74 to zero. Close t
11cf0 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a he sub-journal.*
11cf1 2a 20 69 66 20 69 74 20 69 73 20 6f 70 65 6e 20 * if it is open
11cf2 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73 and the pager is
11cf3 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76 not in exclusiv
11cf4 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 e mode..*/.stati
11cf5 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 41 6c c void releaseAl
11cf6 6c 53 61 76 65 70 6f 69 6e 74 73 28 50 61 67 65 lSavepoints(Page
11cf7 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
11cf8 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 t ii;
11cf9 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 /* Iterator
11cfa 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f for looping thro
11cfb 75 67 68 20 50 61 67 65 72 2e 61 53 61 76 65 70 ugh Pager.aSavep
11cfc 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f 72 28 69 69 oint */. for(ii
11cfd 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e =0; ii<pPager->n
11cfe 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 Savepoint; ii++)
11cff 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 {. sqlite3Bit
11d00 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 vecDestroy(pPage
11d01 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 r->aSavepoint[ii
11d02 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b ].pInSavepoint);
11d03 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 . }. if( !pPag
11d04 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
11d05 65 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4d 65 e || sqlite3IsMe
11d06 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d mJournal(pPager-
11d07 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 73 71 >sjfd) ){. sq
11d08 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
11d09 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a ger->sjfd);. }.
11d0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
11d0b 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
11d0c 74 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 61 53 t);. pPager->aS
11d0d 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 avepoint = 0;.
11d0e 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 pPager->nSavepoi
11d0f 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 nt = 0;. pPager
11d10 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 7d ->nSubRec = 0;.}
11d11 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
11d12 62 69 74 20 6e 75 6d 62 65 72 20 70 67 6e 6f 20 bit number pgno
11d13 69 6e 20 74 68 65 20 50 61 67 65 72 53 61 76 65 in the PagerSave
11d14 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 point.pInSavepoi
11d15 6e 74 20 0a 2a 2a 20 62 69 74 76 65 63 73 20 6f nt .** bitvecs o
11d16 66 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 f all open savep
11d17 6f 69 6e 74 73 2e 20 52 65 74 75 72 6e 20 53 51 oints. Return SQ
11d18 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 LITE_OK if succe
11d19 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49 ssful.** or SQLI
11d1a 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 TE_NOMEM if a ma
11d1b 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 lloc failure occ
11d1c 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 urs..*/.static i
11d1d 6e 74 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e nt addToSavepoin
11d1e 74 42 69 74 76 65 63 73 28 50 61 67 65 72 20 2a tBitvecs(Pager *
11d1f 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e pPager, Pgno pgn
11d20 6f 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 o){. int ii;
11d21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d22 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
11d23 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
11d24 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f LITE_OK; /
11d25 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f * Result code */
11d26 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 .. for(ii=0; ii
11d27 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f <pPager->nSavepo
11d28 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 int; ii++){.
11d29 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a PagerSavepoint *
11d2a 70 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61 p = &pPager->aSa
11d2b 76 65 70 6f 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 vepoint[ii];.
11d2c 20 69 66 28 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f if( pgno<=p->nO
11d2d 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72 63 20 rig ){. rc
11d2e 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 |= sqlite3Bitvec
11d2f 53 65 74 28 70 2d 3e 70 49 6e 53 61 76 65 70 6f Set(p->pInSavepo
11d30 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 int, pgno);.
11d31 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d testcase( rc==
11d32 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a SQLITE_NOMEM );.
11d33 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
11d34 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 ==SQLITE_OK || r
11d35 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
11d36 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 );. }. }. r
11d37 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
11d38 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 ** Unlock the da
11d39 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 tabase file. Thi
11d3a 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 s function is a
11d3b 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70 61 67 no-op if the pag
11d3c 65 72 0a 2a 2a 20 69 73 20 69 6e 20 65 78 63 6c er.** is in excl
11d3d 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a usive mode..**.*
11d3e 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
11d3f 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 65 s currently in e
11d40 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69 73 63 rror state, disc
11d41 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ard the contents
11d42 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61 63 68 of .** the cach
11d43 65 20 61 6e 64 20 72 65 73 65 74 20 74 68 65 20 e and reset the
11d44 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 Pager structure
11d45 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 2e 20 internal state.
11d46 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 If there is.** a
11d47 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 n open journal-f
11d48 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6e 65 ile, then the ne
11d49 78 74 20 74 69 6d 65 20 61 20 73 68 61 72 65 64 xt time a shared
11d4a 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 -lock is obtaine
11d4b 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61 67 65 d.** on the page
11d4c 72 20 66 69 6c 65 20 28 62 79 20 74 68 69 73 20 r file (by this
11d4d 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f or any other pro
11d4e 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c 20 62 cess), it will b
11d4f 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20 e.** treated as
11d50 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e a hot-journal an
11d51 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a d rolled back..*
11d52 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 /.static void pa
11d53 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72 ger_unlock(Pager
11d54 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 *pPager){. if(
11d55 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 !pPager->exclus
11d56 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69 iveMode ){. i
11d57 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
11d58 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
11d59 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
11d5a 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63 6c 6f /* Always clo
11d5b 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 se the journal f
11d5c 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70 69 6e ile when droppin
11d5d 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c g the database l
11d5e 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 ock.. ** Othe
11d5f 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72 20 63 rwise, another c
11d60 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6a onnection with j
11d61 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 ournal_mode=dele
11d62 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a 2a 20 te might. **
11d63 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20 delete the file
11d64 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 out from under u
11d65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 s.. */. sq
11d66 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 lite3OsClose(pPa
11d67 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 73 ger->jfd);. s
11d68 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 qlite3BitvecDest
11d69 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a roy(pPager->pInJ
11d6a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61 ournal);. pPa
11d6b 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 ger->pInJournal
11d6c 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 = 0;. release
11d6d 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 AllSavepoints(pP
11d6e 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 ager);.. /* I
11d6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e f the file is un
11d70 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 62 6f 64 79 locked, somebody
11d71 20 65 6c 73 65 20 6d 69 67 68 74 20 63 68 61 6e else might chan
11d72 67 65 20 69 74 2e 20 54 68 65 0a 20 20 20 20 2a ge it. The. *
11d73 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 * values stored
11d74 69 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 20 in Pager.dbSize
11d75 65 74 63 2e 20 6d 69 67 68 74 20 62 65 63 6f 6d etc. might becom
11d76 65 20 69 6e 76 61 6c 69 64 20 69 66 0a 20 20 20 e invalid if.
11d77 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73 ** this happens
11d78 2e 20 54 4f 44 4f 3a 20 52 65 61 6c 6c 79 2c 20 . TODO: Really,
11d79 74 68 69 73 20 64 6f 65 73 6e 27 74 20 6e 65 65 this doesn't nee
11d7a 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 64 0a d to be cleared.
11d7b 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 74 68 65 ** until the
11d7c 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
11d7d 63 68 65 63 6b 20 66 61 69 6c 73 20 69 6e 20 50 check fails in P
11d7e 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29 agerSharedLock()
11d7f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 .. */. pPa
11d80 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
11d81 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20 3d 20 = 0;.. rc =
11d82 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d osUnlock(pPager-
11d83 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 >fd, NO_LOCK);.
11d84 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
11d85 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f pPager->errCo
11d86 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 de = rc;. }.
11d87 20 20 20 49 4f 54 52 41 43 45 28 28 22 55 4e 4c IOTRACE(("UNL
11d88 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 OCK %p\n", pPage
11d89 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66 20 50 r)).. /* If P
11d8a 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20 ager.errCode is
11d8b 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 set, the content
11d8c 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63 s of the pager c
11d8d 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a 20 ache cannot be.
11d8e 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e ** trusted. N
11d8f 6f 77 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ow that the page
11d90 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b r file is unlock
11d91 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ed, the contents
11d92 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 of the. ** c
11d93 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 63 ache can be disc
11d94 61 72 64 65 64 20 61 6e 64 20 74 68 65 20 65 72 arded and the er
11d95 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c 79 20 ror code safely
11d96 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a cleared.. */.
11d97 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
11d98 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 errCode ){.
11d99 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
11d9a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 OK ){. pP
11d9b 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 ager->errCode =
11d9c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
11d9d 20 7d 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72 }. pager_r
11d9e 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 eset(pPager);.
11d9f 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 72 2d }.. pPager-
11da0 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
11da1 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
11da2 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
11da3 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f UNLOCK;. }.}../
11da4 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
11da5 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c on should be cal
11da6 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f 45 52 led when an IOER
11da7 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20 46 55 R, CORRUPT or FU
11da8 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61 79 20 LL error.** may
11da9 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 20 54 have occurred. T
11daa 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
11dab 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 t is a pointer t
11dac 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a 2a 20 o the pager .**
11dad 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 20 73 structure, the s
11dae 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f 72 2d econd the error-
11daf 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 62 65 code about to be
11db0 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 70 returned by a p
11db1 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66 75 6e ager .** API fun
11db2 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65 ction. The value
11db3 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 20 63 returned is a c
11db4 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e opy of the secon
11db5 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 d argument .** t
11db6 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e o this function.
11db7 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 .**.** If the s
11db8 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
11db9 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 s SQLITE_IOERR,
11dba 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 SQLITE_CORRUPT,
11dbb 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a or SQLITE_FULL.*
11dbc 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 63 6f * the error beco
11dbd 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 2e 20 mes persistent.
11dbe 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 69 73 Until the persis
11dbf 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 6c 65 ten error is cle
11dc0 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 71 75 ared,.** subsequ
11dc1 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 6f 6e ent API calls on
11dc2 20 74 68 69 73 20 50 61 67 65 72 20 77 69 6c 6c this Pager will
11dc3 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 65 74 immediately ret
11dc4 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a urn the same .**
11dc5 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a error code..**.
11dc6 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e 74 20 ** A persistent
11dc7 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 73 20 error indicates
11dc8 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 that the content
11dc9 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 s of the pager-c
11dca 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20 ache .** cannot
11dcb 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 69 73 be trusted. This
11dcc 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 63 6c state can be cl
11dcd 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c 65 74 eared by complet
11dce 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 20 0a ely discarding .
11dcf 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ** the contents
11dd0 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 of the pager-cac
11dd1 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 he. If a transac
11dd2 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 65 20 tion was active
11dd3 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 72 73 when.** the pers
11dd4 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f 63 63 istent error occ
11dd5 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 urred, then the
11dd6 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
11dd7 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 may need.** to
11dd8 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f 20 72 be replayed to r
11dd9 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65 estore the conte
11dda 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 nts of the datab
11ddb 61 73 65 20 66 69 6c 65 20 28 61 73 20 69 66 0a ase file (as if.
11ddc 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68 6f 74 ** it were a hot
11ddd 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a 73 74 -journal)..*/.st
11dde 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 atic int pager_e
11ddf 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 61 67 rror(Pager *pPag
11de0 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 69 er, int rc){. i
11de1 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 30 78 nt rc2 = rc & 0x
11de2 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 ff;. assert( rc
11de3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 ==SQLITE_OK || !
11de4 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65 72 MEMDB );. asser
11de5 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 65 72 t(. pPager
11de6 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 ->errCode==SQLIT
11de7 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 E_FULL ||.
11de8 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11de9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 ==SQLITE_OK ||.
11dea 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 65 (pPager->e
11deb 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 3d 3d rrCode & 0xff)==
11dec 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 29 SQLITE_IOERR. )
11ded 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c ;. if( rc2==SQL
11dee 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63 32 3d ITE_FULL || rc2=
11def 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b =SQLITE_IOERR ){
11df0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 . pPager->err
11df1 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d 0a 20 Code = rc;. }.
11df2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
11df3 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20 72 *.** Execute a r
11df4 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 72 61 ollback if a tra
11df5 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
11df6 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 ve and unlock th
11df7 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 e .** database f
11df8 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ile. .**.** If t
11df9 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72 he pager has alr
11dfa 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 eady entered the
11dfb 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f error state, do
11dfc 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a 2a 2a not attempt .**
11dfd 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 61 74 the rollback at
11dfe 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e 73 74 this time. Inst
11dff 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c 6f 63 ead, pager_unloc
11e00 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 54 k() is called. T
11e01 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 he.** call to pa
11e02 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77 69 6c ger_unlock() wil
11e03 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 69 6e l discard all in
11e04 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c 20 75 -memory pages, u
11e05 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64 61 74 nlock.** the dat
11e06 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 63 abase file and c
11e07 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73 lear the error s
11e08 74 61 74 65 2e 20 49 66 20 74 68 69 73 20 6d 65 tate. If this me
11e09 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 72 ans that.** ther
11e0a 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e e is a hot-journ
11e0b 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 al left in the f
11e0c 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 65 20 ile-system, the
11e0d 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a next connection.
11e0e 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 73 ** to obtain a s
11e0f 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 hared lock on th
11e10 65 20 70 61 67 65 72 20 28 77 68 69 63 68 20 6d e pager (which m
11e11 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65 29 20 ay be this one)
11e12 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69 74 20 will.** roll it
11e13 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 back..**.** If t
11e14 68 65 20 70 61 67 65 72 20 68 61 73 20 6e 6f 74 he pager has not
11e15 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 already entered
11e16 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
11e17 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72 0a 2a , but an IO or.*
11e18 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f * malloc error o
11e19 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 20 72 ccurs during a r
11e1a 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 74 68 ollback, then th
11e1b 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66 20 63 is will itself c
11e1c 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 61 67 ause .** the pag
11e1d 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 65 20 er to enter the
11e1e 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57 68 69 error state. Whi
11e1f 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65 61 72 ch will be clear
11e20 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c ed by the.** cal
11e21 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 l to pager_unloc
11e22 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69 62 65 k(), as describe
11e23 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 d above..*/.stat
11e24 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c ic void pagerUnl
11e25 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 ockAndRollback(P
11e26 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
11e27 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 if( pPager->err
11e28 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code==SQLITE_OK
11e29 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 && pPager->state
11e2a 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 >=PAGER_RESERVED
11e2b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
11e2c 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
11e2d 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 ();. sqlite3P
11e2e 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50 61 agerRollback(pPa
11e2f 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ger);. sqlite
11e30 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
11e31 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 5f ();. }. pager_
11e32 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a unlock(pPager);.
11e33 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
11e34 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 72 61 utine ends a tra
11e35 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72 61 6e nsaction. A tran
11e36 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75 61 6c saction is usual
11e37 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a 2a 20 ly ended by .**
11e38 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49 54 20 either a COMMIT
11e39 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20 6f 70 or a ROLLBACK op
11e3a 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 72 6f eration. This ro
11e3b 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63 61 6c utine may be cal
11e3c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20 72 6f led .** after ro
11e3d 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f 74 2d llback of a hot-
11e3e 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66 20 61 journal, or if a
11e3f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 n error occurs w
11e40 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 hile opening.**
11e41 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11e42 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 or writing the
11e43 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75 72 6e very first journ
11e44 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61 0a 2a al-header of a.*
11e45 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 * database trans
11e46 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 49 action..** .** I
11e47 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 f the pager is i
11e48 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6f n PAGER_SHARED o
11e49 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 r PAGER_UNLOCK s
11e4a 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a tate when this.*
11e4b 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c * routine is cal
11e4c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d led, it is a no-
11e4d 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51 4c 49 op (returns SQLI
11e4e 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 TE_OK)..**.** Ot
11e4f 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 63 74 herwise, any act
11e50 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 ive savepoints a
11e51 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a re released..**.
11e52 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 ** If the journa
11e53 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20 l file is open,
11e54 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 6e 61 then it is "fina
11e55 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 20 6a lized". Once a j
11e56 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c 65 20 ournal .** file
11e57 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a has been finaliz
11e58 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 ed it is not pos
11e59 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 74 20 sible to use it
11e5a 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 0a to roll back a .
11e5b 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ** transaction.
11e5c 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 20 63 Nor will it be c
11e5d 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 onsidered to be
11e5e 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 79 a hot-journal by
11e5f 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e 79 20 this.** or any
11e60 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 other database c
11e61 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 63 74 onnection. Exact
11e62 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e 61 6c ly how a journal
11e63 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a is finalized.**
11e64 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 depends on whet
11e65 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 70 her or not the p
11e66 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 ager is running
11e67 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 in exclusive mod
11e68 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 75 72 e and.** the cur
11e69 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 rent journal-mod
11e6a 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c e (Pager.journal
11e6b 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 73 20 Mode value), as
11e6c 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 follows:.**.**
11e6d 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 journalMode==ME
11e6e 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 MORY.** Jour
11e6f 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
11e70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 63 6c tor is simply cl
11e71 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 74 72 osed. This destr
11e72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 20 69 oys an .** i
11e73 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c n-memory journal
11e74 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 ..**.** journa
11e75 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 45 0a lMode==TRUNCATE.
11e76 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 ** Journal f
11e77 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 ile is truncated
11e78 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 to zero bytes i
11e79 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 n size..**.**
11e7a 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 journalMode==PER
11e7b 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 65 20 SIST.** The
11e7c 66 69 72 73 74 20 32 38 20 62 79 74 65 73 20 6f first 28 bytes o
11e7d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
11e7e 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e 20 54 le are zeroed. T
11e7f 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 73 0a his invalidates.
11e80 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 73 74 ** the first
11e81 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
11e82 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64 in the file, and
11e83 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 69 72 hence the entir
11e84 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 e journal.**
11e85 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 6c 69 file. An invali
11e86 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 d journal file c
11e87 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 annot be rolled
11e88 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f back..**.** jo
11e89 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54 urnalMode==DELET
11e8a 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a 6f 75 E.** The jou
11e8b 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f rnal file is clo
11e8c 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 20 sed and deleted
11e8d 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 using sqlite3OsD
11e8e 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 elete()..**.**
11e8f 20 20 20 49 66 20 74 68 65 20 70 61 67 65 72 20 If the pager
11e90 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 is running in ex
11e91 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 clusive mode, th
11e92 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 69 6e is method of fin
11e93 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 20 74 alizing.** t
11e94 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11e95 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20 49 is never used. I
11e96 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 20 6a nstead, if the j
11e97 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a ournalMode is.**
11e98 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e 64 20 DELETE and
11e99 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11e9a 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
11e9b 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 63 72 the method descr
11e9c 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 20 20 ibed under.**
11e9d 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 journalMode==P
11e9e 45 52 53 49 53 54 20 69 73 20 75 73 65 64 20 69 ERSIST is used i
11e9f 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66 nstead..**.** Af
11ea0 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ter the journal
11ea1 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 69 66 is finalized, if
11ea2 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f 6e 2d running in non-
11ea3 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 exclusive mode,
11ea4 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d 6f 76 the.** pager mov
11ea5 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 es to PAGER_SHAR
11ea6 45 44 20 73 74 61 74 65 20 28 61 6e 64 20 64 6f ED state (and do
11ea7 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c 6f 63 wngrades the loc
11ea8 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 k on the.** data
11ea9 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f 72 64 base file accord
11eaa 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 ingly)..**.** If
11eab 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 the pager is ru
11eac 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 nning in exclusi
11ead 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73 20 69 ve mode and is i
11eae 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 73 n PAGER_SYNCED s
11eaf 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f 76 65 tate,.** it move
11eb0 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 s to PAGER_EXCLU
11eb1 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61 SIVE. No locks a
11eb2 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20 77 68 re downgraded wh
11eb3 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a 2a 2a en running in.**
11eb4 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e exclusive mode.
11eb5 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
11eb6 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
11eb7 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e no error occurs.
11eb8 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
11eb9 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20 61 6e urs during.** an
11eba 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70 65 72 y of the IO oper
11ebb 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61 6c 69 ations to finali
11ebc 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ze the journal f
11ebd 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 ile or unlock th
11ebe 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 68 e.** database th
11ebf 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 en the IO error
11ec0 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
11ec1 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 49 66 to the user. If
11ec2 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61 74 69 the .** operati
11ec3 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 on to finalize t
11ec4 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11ec5 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 20 fails, then the
11ec6 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20 74 72 code still.** tr
11ec7 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 ies to unlock th
11ec8 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
11ec9 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 if not in exclus
11eca 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74 68 65 ive mode. If the
11ecb 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 .** unlock opera
11ecc 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20 77 65 tion fails as we
11ecd 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 ll, then the fir
11ece 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 st error code re
11ecf 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 lated.** to the
11ed0 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e 63 6f first error enco
11ed1 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a 6f 75 untered (the jou
11ed2 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f rnal finalizatio
11ed3 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72 65 74 n one) is.** ret
11ed4 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
11ed5 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 5f 74 int pager_end_t
11ed6 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65 72 ransaction(Pager
11ed7 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 68 61 *pPager, int ha
11ed8 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 sMaster){. int
11ed9 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
11eda 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f /* Error co
11edb 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 de from journal
11edc 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 70 65 finalization ope
11edd 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ration */. int
11ede 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc2 = SQLITE_OK;
11edf 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f /* Error co
11ee0 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c 65 20 de from db file
11ee1 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e unlock operation
11ee2 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 67 65 */.. if( pPage
11ee3 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52 r->state<PAGER_R
11ee4 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 72 ESERVED ){. r
11ee5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
11ee6 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 41 6c . }. releaseAl
11ee7 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67 lSavepoints(pPag
11ee8 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 er);.. assert(
11ee9 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
11eea 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 70 fd) || pPager->p
11eeb 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a InJournal==0 );.
11eec 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
11eed 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a 20 20 ger->jfd) ){..
11eee 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20 74 68 /* Finalize th
11eef 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
11ef0 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
11ef1 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 e3IsMemJournal(p
11ef2 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
11ef3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
11ef4 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
11ef5 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
11ef6 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 ODE_MEMORY );.
11ef7 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
11ef8 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b se(pPager->jfd);
11ef9 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
11efa 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
11efb 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
11efc 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 29 LMODE_TRUNCATE )
11efd 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 {. if( pPag
11efe 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d er->journalOff==
11eff 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 0 ){. rc
11f00 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
11f01 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
11f02 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11f03 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
11f04 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 >jfd, 0);.
11f05 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e }. pPager->
11f06 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
11f07 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
11f08 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 urnalStarted = 0
11f09 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
11f0a 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
11f0b 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c 20 70 eMode . || p
11f0c 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
11f0d 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
11f0e 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 LMODE_PERSIST.
11f0f 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
11f10 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 70 zeroJournalHdr(p
11f11 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 65 72 Pager, hasMaster
11f12 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 );. pager_e
11f13 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
11f14 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e ;. pPager->
11f15 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a journalOff = 0;.
11f16 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
11f17 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 urnalStarted = 0
11f18 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
11f19 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 /* This branc
11f1a 68 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 65 h may be execute
11f1b 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a 6f 75 d with Pager.jou
11f1c 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 rnalMode==MEMORY
11f1d 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61 20 68 if. ** a h
11f1e 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6a ot-journal was j
11f1f 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e ust rolled back.
11f20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
11f21 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 e journal.
11f22 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 ** file should b
11f23 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c e closed and del
11f24 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 63 6f eted. If this co
11f25 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 nnection writes
11f26 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 to. ** the
11f27 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69 database file, i
11f28 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75 73 69 t will do so usi
11f29 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 ng an in-memory
11f2a 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f 0a 20 20 20 journal. */.
11f2b 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
11f2c 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
11f2d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
11f2e 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20 20 20 E_DELETE .
11f2f 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e || pPager->
11f30 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 journalMode==PAG
11f31 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d ER_JOURNALMODE_M
11f32 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 29 3b 0a EMORY . );.
11f33 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
11f34 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 lose(pPager->jfd
11f35 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 );. if( !pP
11f36 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 ager->tempFile )
11f37 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
11f38 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 qlite3OsDelete(p
11f39 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61 Pager->pVfs, pPa
11f3a 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 ger->zJournal, 0
11f3b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
11f3c 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
11f3d 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 CHECK_PAGES.
11f3e 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 sqlite3PcacheIte
11f3f 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 rateDirty(pPager
11f40 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72 ->pPCache, pager
11f41 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a _set_pagehash);.
11f42 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69 #endif.. sqli
11f43 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c te3PcacheCleanAl
11f44 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 l(pPager->pPCach
11f45 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 e);. sqlite3B
11f46 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
11f47 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 ger->pInJournal)
11f48 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 ;. pPager->pI
11f49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 nJournal = 0;.
11f4a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d pPager->nRec =
11f4b 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 0;. }.. if( !
11f4c 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
11f4d 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32 eMode ){. rc2
11f4e 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 = osUnlock(pPag
11f4f 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c er->fd, SHARED_L
11f50 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72 OCK);. pPager
11f51 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f ->state = PAGER_
11f52 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67 SHARED;. pPag
11f53 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 er->changeCountD
11f54 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 one = 0;. }else
11f55 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
11f56 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 te==PAGER_SYNCED
11f57 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
11f58 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 state = PAGER_EX
11f59 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70 CLUSIVE;. }. p
11f5a 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
11f5b 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e = 0;. pPager->
11f5c 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 needSync = 0;.
11f5d 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
11f5e 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f ed = 0;.. /* TO
11f5f 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69 DO: Is this opti
11f60 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20 mal? Why is the
11f61 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61 db size invalida
11f62 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77 ted here . ** w
11f63 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
11f64 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c file is not unl
11f65 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67 ocked? */. pPag
11f66 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d er->dbOrigSize =
11f67 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 0;. sqlite3Pca
11f68 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67 cheTruncate(pPag
11f69 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61 er->pPCache, pPa
11f6a 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 ger->dbSize);.
11f6b 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 if( !MEMDB ){.
11f6c 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
11f6d 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a Valid = 0;. }..
11f6e 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 return (rc==SQ
11f6f 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b LITE_OK?rc2:rc);
11f70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 .}../*.** Parame
11f71 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70 ter aData must p
11f72 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 oint to a buffer
11f73 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65 of pPager->page
11f74 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 Size bytes.** of
11f75 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61 data. Compute a
11f76 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63 nd return a chec
11f77 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74 ksum based ont t
11f78 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
11f79 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64 he .** page of d
11f7a 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72 ata and the curr
11f7b 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61 ent value of pPa
11f7c 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a ger->cksumInit..
11f7d 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f **.** This is no
11f7e 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75 t a real checksu
11f7f 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20 m. It is really
11f80 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20 just the sum of
11f81 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 the .** random i
11f82 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50 nitial value (pP
11f83 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 ager->cksumInit)
11f84 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68 and every 200th
11f85 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 byte.** of the
11f86 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74 page data, start
11f87 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66 ing with byte of
11f88 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61 fset (pPager->pa
11f89 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20 geSize%200)..**
11f8a 45 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74 Each byte is int
11f8b 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38 erpreted as an 8
11f8c 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e -bit unsigned in
11f8d 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 teger..**.** Cha
11f8e 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c nging the formul
11f8f 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 a used to comput
11f90 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20 e this checksum
11f91 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a results in an.**
11f92 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f incompatible jo
11f93 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 urnal file forma
11f94 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72 t..**.** If jour
11f95 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f nal corruption o
11f96 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70 ccurs due to a p
11f97 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68 ower failure, th
11f98 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a e most likely .*
11f99 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68 * scenario is th
11f9a 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68 at one end or th
11f9b 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72 e other of the r
11f9c 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68 ecord will be ch
11f9d 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73 anged. .** It is
11f9e 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c much less likel
11f9f 79 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65 y that the two e
11fa0 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e nds of the journ
11fa1 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 al record will b
11fa2 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64 e.** correct and
11fa3 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63 the middle be c
11fa4 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74 orrupt. Thus, t
11fa5 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73 his "checksum" s
11fa6 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68 cheme,.** though
11fa7 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65 fast and simple
11fa8 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f , catches the mo
11fa9 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 stly likely kind
11faa 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a of corruption..
11fab 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 */.static u32 pa
11fac 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20 ger_cksum(Pager
11fad 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75 *pPager, const u
11fae 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32 8 *aData){. u32
11faf 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d cksum = pPager-
11fb0 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 >cksumInit;
11fb1 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 /* Checksum
11fb2 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 value to return
11fb3 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61 */. int i = pPa
11fb4 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30 ger->pageSize-20
11fb5 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 0; /* L
11fb6 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
11fb7 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 while( i>0 ){.
11fb8 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74 cksum += aDat
11fb9 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32 a[i];. i -= 2
11fba 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 00;. }. return
11fbb 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cksum;.}../*.**
11fbc 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70 Read a single p
11fbd 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20 age from either
11fbe 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11fbf 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d (if isMainJrnl=
11fc0 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 =1) or.** from t
11fc1 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 he sub-journal (
11fc2 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 if isMainJrnl==0
11fc3 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74 ) and playback t
11fc4 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65 hat page..** The
11fc5 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20 page begins at
11fc6 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20 offset *pOffset
11fc7 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 into the file. T
11fc8 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76 he *pOffset.** v
11fc9 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65 alue is increase
11fca 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f d to the start o
11fcb 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 f the next page
11fcc 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a in the journal..
11fcd 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e **.** The isMain
11fce 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75 Jrnl flag is tru
11fcf 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 e if this is the
11fd0 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a main rollback j
11fd1 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 ournal and.** fa
11fd2 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74 lse for the stat
11fd3 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 ement journal.
11fd4 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 The main rollbac
11fd5 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a k journal uses.*
11fd6 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68 * checksums - th
11fd7 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
11fd8 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a nal does not..**
11fd9 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 .** If the page
11fda 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 number of the pa
11fdb 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66 ge record read f
11fdc 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f rom the (sub-)jo
11fdd 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 urnal file.** is
11fde 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
11fdf 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
11fe0 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c of Pager.dbSize,
11fe1 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69 then playback i
11fe2 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64 s.** skipped and
11fe3 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
11fe4 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
11fe5 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55 pDone is not NU
11fe6 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61 LL, then it is a
11fe7 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73 record of pages
11fe8 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61 that have alrea
11fe9 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65 dy.** been playe
11fea 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20 d back. If the
11feb 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74 page at *pOffset
11fec 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
11fed 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a n played back.**
11fee 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70 (if the corresp
11fef 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74 onding pDone bit
11ff0 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b is set) then sk
11ff1 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e ip the playback.
11ff2 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 .** Make sure th
11ff3 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72 e pDone bit corr
11ff4 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 esponding to the
11ff5 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69 *pOffset page i
11ff6 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74 s set.** prior t
11ff7 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a o returning..**.
11ff8 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72 ** If the page r
11ff9 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73 ecord is success
11ffa 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20 fully read from
11ffb 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 the (sub-)journa
11ffc 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c l file.** and pl
11ffd 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20 ayed back, then
11ffe 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
11fff 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 urned. If an IO
12000 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 error occurs.**
12001 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68 while reading th
12002 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68 e record from th
12003 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 e (sub-)journal
12004 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72 file or while wr
12005 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 iting.** to the
12006 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 database file, t
12007 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 hen the IO error
12008 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
12009 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73 d. If data.** is
1200a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 successfully re
1200b 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 ad from the (sub
1200c 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 -)journal file b
1200d 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 ut appears to be
1200e 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53 .** corrupted, S
1200f 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 QLITE_DONE is re
12010 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20 turned. Data is
12011 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75 considered corru
12012 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63 pted in.** two c
12013 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a ircumstances:.**
12014 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 .** * If the
12015 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62 record page-numb
12016 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30 er is illegal (0
12017 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e or PAGER_MJ_PGN
12018 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66 O), or.** * If
12019 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62 the record is b
1201a 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b eing rolled back
1201b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a from the main j
1201c 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 ournal file.**
1201d 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b and the check
1201e 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e sum field does n
1201f 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63 ot match the rec
12020 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a ord content..**.
12021 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68 ** Neither of th
12022 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f ese two scenario
12023 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64 s are possible d
12024 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e uring a savepoin
12025 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a t rollback..**.*
12026 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 * If this is a s
12027 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 avepoint rollbac
12028 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d k, then memory m
12029 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79 ay have to be dy
1202a 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c namically.** all
1202b 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66 ocated by this f
1202c 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 unction. If this
1202d 20 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64 is the case and
1202e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 an allocation f
1202f 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f ails,.** SQLITE_
12030 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 NOMEM is returne
12031 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
12032 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
12033 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65 one_page(. Page
12034 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 r *pPager,
12035 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
12036 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61 pager being pla
12037 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 6e yed back */. in
12038 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20 t isMainJrnl,
12039 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
1203a 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c -> main journal
1203b 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e . 0 -> sub-journ
1203c 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 al. */. int isU
1203d 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20 nsync,
1203e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
1203f 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 75 f reading from u
12040 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f 75 nsynced main jou
12041 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a 70 rnal */. i64 *p
12042 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 Offset,
12043 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
12044 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70 t of record to p
12045 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 layback */. int
12046 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 20 isSavepnt,
12047 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
12048 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69 ue for a savepoi
12049 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 nt rollback */.
1204a 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 20 Bitvec *pDone
1204b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1204c 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65 * Bitvec of page
1204d 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64 s already played
1204e 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e back */.){. in
1204f 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 t rc;. PgHdr *p
12050 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Pg;
12051 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 /* An exis
12052 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 ting page in the
12053 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f cache */. Pgno
12054 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
12055 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
12056 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
12057 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 a page in journa
12058 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d l */. u32 cksum
12059 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1205a 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d /* Checksum
1205b 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 used for sanity
1205c 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 checking */. u
1205d 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 8 *aData;
1205e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1205f 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 Temporary storag
12060 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a e for the page *
12061 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
12062 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 *jfd;
12063 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 /* The file de
12064 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 scriptor for the
12065 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f journal file */
12066 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d .. assert( (isM
12067 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 ainJrnl&~1)==0 )
12068 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e ; /* isMain
12069 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a Jrnl is 0 or 1 *
1206a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53 /. assert( (isS
1206b 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b avepnt&~1)==0 );
1206c 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65 /* isSave
1206d 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f pnt is 0 or 1 */
1206e 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69 . assert( isMai
1206f 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 nJrnl || pDone )
12070 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61 ; /* pDone a
12071 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75 lways used on su
12072 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 b-journals */.
12073 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e assert( isSavepn
12074 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b t || pDone==0 );
12075 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65 /* pDone neve
12076 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 r used on non-sa
12077 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 vepoint */.. aD
12078 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 65 ata = (u8*)pPage
12079 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 r->pTmpSpace;.
1207a 61 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b assert( aData );
1207b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 /* Temp
1207c 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61 storage must ha
1207d 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
1207e 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20 allocated */..
1207f 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65 /* Read the page
12080 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65 number and page
12081 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a data from the j
12082 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f ournal or sub-jo
12083 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e urnal. ** file.
12084 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
12085 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c code to the cal
12086 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72 ler if an IO err
12087 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a or occurs.. */.
12088 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72 jfd = isMainJr
12089 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64 nl ? pPager->jfd
1208a 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b : pPager->sjfd;
1208b 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 . rc = read32bi
1208c 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74 ts(jfd, *pOffset
1208d 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 , &pgno);. if(
1208e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
1208f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 return rc;. rc
12090 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
12091 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67 jfd, aData, pPag
12092 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 2a er->pageSize, (*
12093 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 69 pOffset)+4);. i
12094 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12095 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
12096 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 67 *pOffset += pPag
12097 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34 er->pageSize + 4
12098 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b + isMainJrnl*4;
12099 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 .. /* Sanity ch
1209a 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61 ecking on the pa
1209b 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72 ge. This is mor
1209c 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 e important that
1209d 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 I originally.
1209e 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20 ** thought. If
1209f 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 a power failure
120a0 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 occurs while the
120a1 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e journal is bein
120a2 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 g written,. **
120a3 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69 it could cause i
120a4 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62 nvalid data to b
120a5 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
120a6 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20 he journal. We
120a7 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74 need to. ** det
120a8 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64 ect this invalid
120a9 20 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68 data (with high
120aa 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e probability) an
120ab 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a d ignore it.. *
120ac 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 /. if( pgno==0
120ad 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d || pgno==PAGER_M
120ae 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 J_PGNO(pPager) )
120af 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 {. assert( !i
120b0 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 sSavepnt );.
120b1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
120b2 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 NE;. }. if( pg
120b3 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d no>(Pgno)pPager-
120b4 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 74 >dbSize || sqlit
120b5 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44 6f e3BitvecTest(pDo
120b6 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 ne, pgno) ){.
120b7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
120b8 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4d K;. }. if( isM
120b9 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72 ainJrnl ){. r
120ba 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a c = read32bits(j
120bb 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d 34 fd, (*pOffset)-4
120bc 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69 , &cksum);. i
120bd 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
120be 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 61 c;. if( !isSa
120bf 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f 63 vepnt && pager_c
120c0 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61 ksum(pPager, aDa
120c1 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 ta)!=cksum ){.
120c2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
120c3 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 E_DONE;. }.
120c4 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26 }.. if( pDone &
120c5 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 & (rc = sqlite3B
120c6 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 itvecSet(pDone,
120c7 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f pgno))!=SQLITE_O
120c8 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
120c9 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 rc;. }.. asser
120ca 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
120cb 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 ==PAGER_RESERVED
120cc 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 || pPager->stat
120cd 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 e>=PAGER_EXCLUSI
120ce 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 VE );.. /* If t
120cf 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 52 he pager is in R
120d0 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74 ESERVED state, t
120d1 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62 hen there must b
120d2 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73 e a copy of this
120d3 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 . ** page in th
120d4 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 e pager cache. I
120d5 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74 n this case just
120d6 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65 update the page
120d7 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f r cache,. ** no
120d8 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 t the database f
120d9 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 ile. The page is
120da 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72 left marked dir
120db 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e ty in this case.
120dc 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 . **. ** An ex
120dd 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 ception to the a
120de 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68 bove rule: If th
120df 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e e database is in
120e0 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 no-sync mode.
120e1 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73 ** and a page is
120e2 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e moved during an
120e3 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 incremental vac
120e4 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67 uum then the pag
120e5 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 e may. ** not b
120e6 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 e in the pager c
120e7 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 ache. Later: if
120e8 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f a malloc() or IO
120e9 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 error occurs.
120ea 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 ** during a Move
120eb 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 page() call, the
120ec 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e n the page may n
120ed 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 ot be in the cac
120ee 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 he. ** either.
120ef 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e So the condition
120f0 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 described in th
120f1 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70 e above paragrap
120f2 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 h is not. ** as
120f3 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a sert()able.. **
120f4 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c . ** If in EXCL
120f5 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 USIVE state, the
120f6 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 20 n we update the
120f7 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69 pager cache if i
120f8 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e t exists. ** an
120f9 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e d the main file.
120fa 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65 The page is the
120fb 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 n marked not dir
120fc 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 ty.. **. ** Ti
120fd 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 65 cket #1171: The
120fe 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e statement journ
120ff 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e al might contain
12100 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 page content th
12101 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 at is. ** diffe
12102 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61 rent from the pa
12103 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 ge content at th
12104 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 e start of the t
12105 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a ransaction.. **
12106 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65 This occurs whe
12107 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61 6e n a page is chan
12108 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65 ged prior to the
12109 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 74 start of a stat
1210a 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 ement. ** then
1210b 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69 changed again wi
1210c 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 thin the stateme
1210d 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e nt. When rollin
1210e 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20 g back such a.
1210f 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20 ** statement we
12110 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74 must not write t
12111 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 o the original d
12112 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77 atabase unless w
12113 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 e know. ** for
12114 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69 certain that ori
12115 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 ginal page conte
12116 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 69 nts are synced i
12117 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c nto the main rol
12118 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e lback. ** journ
12119 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 al. Otherwise,
1211a 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 a power loss mig
1211b 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 ht leave modifie
1211c 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20 d data in the.
1211d 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
1211e 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 without an entr
1211f 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 y in the rollbac
12120 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 k journal that c
12121 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 an. ** restore
12122 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
12123 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 its original for
12124 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f m. Two conditio
12125 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 ns must be. **
12126 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 69 met before writi
12127 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
12128 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68 se files. (1) th
12129 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 e database must
1212a 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 be. ** locked.
1212b 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 (2) we know tha
1212c 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 t the original p
1212d 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 age content is f
1212e 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a ully synced. **
1212f 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 in the main jou
12130 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 61 rnal either beca
12131 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73 20 use the page is
12132 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 not in cache or
12133 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 else. ** the pa
12134 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 ge is marked as
12135 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a needSync==0.. *
12136 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 *. ** 2008-04-1
12137 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74 4: When attempt
12138 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20 ing to vacuum a
12139 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
1213a 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 file, it. ** i
1213b 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 s possible to fa
1213c 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f il a statement o
1213d 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68 61 n a database tha
1213e 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 t does not yet e
1213f 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f xist.. ** Do no
12140 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 t attempt to wri
12141 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20 66 te if database f
12142 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 ile has never be
12143 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a en opened.. */.
12144 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f pPg = pager_lo
12145 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e okup(pPager, pgn
12146 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 o);. assert( pP
12147 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 g || !MEMDB );.
12148 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50 4c PAGERTRACE(("PL
12149 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25 AYBACK %d page %
1214a 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73 5c d hash(%08x) %s\
1214b 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
1214c 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 PAGERID(pPage
1214d 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f r), pgno, pager_
1214e 64 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d datahash(pPager-
1214f 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61 >pageSize, aData
12150 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ),.
12151 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d (isMainJrnl?"m
12152 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75 ain-journal":"su
12153 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29 b-journal"). ))
12154 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 2d ;. if( (pPager-
12155 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 >state>=PAGER_EX
12156 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 28 CLUSIVE). && (
12157 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50 pPg==0 || 0==(pP
12158 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
12159 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26 EED_SYNC)). &&
1215a 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
1215b 66 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73 fd). && !isUns
1215c 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34 ync. ){. i64
1215d 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29 ofst = (pgno-1)
1215e 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 *(i64)pPager->pa
1215f 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d geSize;. rc =
12160 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
12161 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74 pPager->fd, aDat
12162 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 a, pPager->pageS
12163 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20 ize, ofst);.
12164 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d if( pgno>pPager-
12165 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 >dbFileSize ){.
12166 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 pPager->dbF
12167 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a ileSize = pgno;.
12168 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 }. if( pP
12169 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 29 7b ager->pBackup ){
1216a 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50 . CODEC1(pP
1216b 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e ager, aData, pgn
1216c 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f o, 3, rc=SQLITE_
1216d 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 73 71 NOMEM);. sq
1216e 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 lite3BackupUpdat
1216f 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 e(pPager->pBacku
12170 70 2c 20 70 67 6e 6f 2c 20 61 44 61 74 61 29 3b p, pgno, aData);
12171 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50 . CODEC1(pP
12172 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e ager, aData, pgn
12173 6f 2c 20 30 2c 20 72 63 3d 53 51 4c 49 54 45 5f o, 0, rc=SQLITE_
12174 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 20 20 NOMEM);. }.
12175 7d 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 }else if( !isMai
12176 6e 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 nJrnl && pPg==0
12177 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 ){. /* If thi
12178 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 s is a rollback
12179 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 of a savepoint a
1217a 6e 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 nd data was not
1217b 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a written to. *
1217c 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 * the database a
1217d 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e nd the page is n
1217e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 ot in-memory, th
1217f 65 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 ere is a potenti
12180 61 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 al. ** proble
12181 6d 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 m. When the page
12182 20 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 is next fetched
12183 20 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c by the b-tree l
12184 61 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a ayer, it . **
12185 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 will be read fr
12186 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
12187 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 file, which may
12188 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 or may not be .
12189 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a ** current. .
1218a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
1218b 65 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 ere are a couple
1218c 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 of different wa
1218d 79 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 ys this can happ
1218e 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 en. All are quit
1218f 65 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 e. ** obscure
12190 2e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 . When running i
12191 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f n synchronous mo
12192 64 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c de, this can onl
12193 79 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a y happen . **
12194 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
12195 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 on the free-list
12196 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
12197 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
12198 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f , then. ** po
12199 70 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f pulated, then mo
1219a 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 ved using sqlite
1219b 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 3PagerMovepage()
1219c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1219d 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
1219e 74 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d to add an in-mem
1219f 6f 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 ory page to the
121a0 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 cache containing
121a1 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 . ** the data
121a2 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 just read from
121a3 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e the sub-journal.
121a4 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 Mark the page a
121a5 73 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 s dirty . **
121a6 61 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 and if the pager
121a7 20 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 requires a jour
121a8 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d nal-sync, then m
121a9 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 ark the page as
121aa 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e . ** requirin
121ab 67 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 g a journal-sync
121ac 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 before it is wr
121ad 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 itten.. */.
121ae 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 assert( isSave
121af 70 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 pnt );. if( (
121b0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
121b1 72 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c rAcquire(pPager,
121b2 20 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 pgno, &pPg, 1))
121b3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
121b4 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
121b5 20 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 }. pPg->f
121b6 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e lags &= ~PGHDR_N
121b7 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 EED_READ;. sq
121b8 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 lite3PcacheMakeD
121b9 69 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 irty(pPg);. }.
121ba 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 if( pPg ){.
121bb 2f 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c /* No page shoul
121bc 64 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 d ever be explic
121bd 69 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b itly rolled back
121be 20 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c that is in use,
121bf 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 except. ** f
121c0 6f 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20 or page 1 which
121c1 69 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69 is held in use i
121c2 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
121c3 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a the lock on the.
121c4 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
121c5 61 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20 active. However
121c6 73 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 20 such a page may
121c7 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 be rolled back a
121c8 73 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a s a result. *
121c9 2a 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c * of an internal
121ca 20 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 error resulting
121cb 20 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 in an automatic
121cc 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 call to. **
121cd 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c sqlite3PagerRoll
121ce 62 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 back().. */.
121cf 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a void *pData;.
121d0 20 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d pData = pPg-
121d1 3e 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 >pData;. memc
121d2 70 79 28 70 44 61 74 61 2c 20 61 44 61 74 61 2c py(pData, aData,
121d3 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
121d4 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e e);. pPager->
121d5 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b 0a xReiniter(pPg);.
121d6 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 if( isMainJr
121d7 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70 6e nl && (!isSavepn
121d8 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70 t || *pOffset<=p
121d9 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 Pager->journalHd
121da 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 r) ){. /* I
121db 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f f the contents o
121dc 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72 65 f this page were
121dd 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66 just restored f
121de 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 20 rom the main .
121df 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 ** journal f
121e0 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f ile, then its co
121e1 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73 ntent must be as
121e2 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e 20 they were when
121e3 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72 the . ** tr
121e4 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66 69 ansaction was fi
121e5 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74 rst opened. In t
121e6 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e 20 his case we can
121e7 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20 20 mark the page.
121e8 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e 2c ** as clean,
121e9 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69 6c since there wil
121ea 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f 20 l be no need to
121eb 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 write it out to
121ec 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 the.. **.
121ed 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 ** There is
121ee 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f one exception to
121ef 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74 this rule. If t
121f0 68 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 he page is being
121f1 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a rolled. **
121f2 20 62 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66 back as part of
121f3 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72 a savepoint (or
121f4 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c statement) roll
121f5 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20 back from an .
121f6 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20 ** unsynced
121f7 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d portion of the m
121f8 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
121f9 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 , then it is not
121fa 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74 safe. ** t
121fb 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 o mark the page
121fc 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69 as clean. This i
121fd 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e s because markin
121fe 67 20 74 68 65 20 70 61 67 65 20 61 73 0a 20 20 g the page as.
121ff 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c ** clean wil
12200 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 44 l clear the PGHD
12201 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
12202 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65 . Since the page
12203 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 is. ** alr
12204 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 eady in the jour
12205 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64 nal file (record
12206 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a ed in Pager.pInJ
12207 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20 ournal) and.
12208 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e ** the PGHDR_N
12209 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73 EED_SYNC flag is
1220a 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65 cleared, if the
1220b 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e page is written
1220c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 to. ** aga
1220d 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 in within this t
1220e 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77 ransaction, it w
1220f 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73 ill be marked as
12210 20 64 69 72 74 79 20 62 75 74 0a 20 20 20 20 20 dirty but.
12211 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45 ** the PGHDR_NE
12212 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c ED_SYNC flag wil
12213 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74 l not be set. It
12214 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65 could then pote
12215 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a ntially. **
12216 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 be written out
12217 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
12218 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74 e file before it
12219 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 s journal file.
1221a 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20 ** segment
1221b 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20 is synced. If a
1221c 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 75 72 crash occurs dur
1221d 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 ing or following
1221e 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20 this,. **
1221f 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 database corrupt
12220 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20 ion may ensue..
12221 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 */. sq
12222 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
12223 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 7d lean(pPg);. }
12224 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
12225 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 HECK_PAGES. p
12226 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 Pg->pageHash = p
12227 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
12228 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f g);.#endif. /
12229 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 70 61 * If this was pa
1222a 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f ge 1, then resto
1222b 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 re the value of
1222c 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 Pager.dbFileVers
1222d 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 .. ** Do this
1222e 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f before any deco
1222f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 ding. */. if(
12230 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 pgno==1 ){.
12231 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 memcpy(&pPager
12232 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 ->dbFileVers, &(
12233 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c (u8*)pData)[24],
12234 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 sizeof(pPager->d
12235 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 bFileVers));.
12236 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 }.. /* Decod
12237 65 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 20 e the page just
12238 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a read from disk *
12239 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 61 /. CODEC1(pPa
1223a 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d ger, pData, pPg-
1223b 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c >pgno, 3, rc=SQL
1223c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 ITE_NOMEM);.
1223d 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c sqlite3PcacheRel
1223e 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 ease(pPg);. }.
1223f 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
12240 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a *.** Parameter z
12241 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e 61 Master is the na
12242 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a me of a master j
12243 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 ournal file. A s
12244 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a ingle journal.**
12245 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 72 file that refer
12246 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65 red to the maste
12247 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 r journal file h
12248 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c as just been rol
12249 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 led back..** Thi
1224a 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
1224b 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 if it is possib
1224c 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 le to delete the
1224d 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1224e 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 file,.** and doe
1224f 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a s so if it is..*
12250 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d *.** Argument zM
12251 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 aster may point
12252 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 to Pager.pTmpSpa
12253 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66 ce. So that buff
12254 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 er is not .** av
12255 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 ailable for use
12256 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 within this func
12257 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e tion..**.** When
12258 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
12259 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 l file is create
1225a 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 74 d, it is populat
1225b 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 ed with the name
1225c 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 s .** of all of
1225d 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 its child journa
1225e 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 6e ls, one after an
1225f 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 64 other, formatted
12260 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e as utf-8 .** en
12261 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 20 coded text. The
12262 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 6c end of each chil
12263 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 d journal file i
12264 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 20 s marked with a
12265 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 .** nul-terminat
12266 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e 20 or byte (0x00).
12267 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 20 i.e. the entire
12268 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61 contents of a ma
12269 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 ster journal.**
1226a 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e 73 file for a trans
1226b 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67 action involving
1226c 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 6d two databases m
1226d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20 ight be:.**.**
1226e 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64 "/home/bill/a.d
1226f 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f b-journal\x00/ho
12270 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75 me/bill/b.db-jou
12271 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20 rnal\x00".**.**
12272 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c A master journal
12273 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62 file may only b
12274 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 61 e deleted once a
12275 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 ll of its child
12276 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76 .** journals hav
12277 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 e been rolled ba
12278 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ck..**.** This f
12279 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 68 unction reads th
1227a 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
1227b 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c e master-journal
1227c 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d file into .** m
1227d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20 emory and loops
1227e 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 20 through each of
1227f 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 the child journa
12280 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20 l names. For.**
12281 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e each child journ
12282 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 66 al, it checks if
12283 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74 :.**.** * if t
12284 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c he child journal
12285 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 20 exists, and if
12286 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65 so.** * if the
12287 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63 child journal c
12288 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 65 ontains a refere
12289 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f nce to master jo
1228a 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69 urnal .** fi
1228b 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a le zMaster.**.**
1228c 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 72 If a child jour
1228d 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 nal can be found
1228e 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 6f that matches bo
1228f 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 72 th of the criter
12290 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69 ia.** above, thi
12291 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
12292 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 ns without doing
12293 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 72 anything. Other
12294 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73 wise, if.** no s
12295 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 uch child journa
12296 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 l can be found,
12297 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20 file zMaster is
12298 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 deleted from.**
12299 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
1229a 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44 using sqlite3OsD
1229b 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 elete()..**.** I
1229c 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 69 f an IO error wi
1229d 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69 thin this functi
1229e 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 on, an error cod
1229f 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 e is returned. T
122a0 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 his.** function
122a1 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79 allocates memory
122a2 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 by calling sqli
122a3 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 te3Malloc(). If
122a4 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a an allocation.**
122a5 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e fails, SQLITE_N
122a6 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 OMEM is returned
122a7 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 . Otherwise, if
122a8 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20 no IO or malloc
122a9 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72 errors .** occur
122aa 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
122ab 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
122ac 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 69 ODO: This functi
122ad 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73 on allocates a s
122ae 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d ingle block of m
122af 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a emory to load.**
122b0 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 the entire cont
122b1 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74 ents of the mast
122b2 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
122b3 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a This could be.*
122b4 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69 * a couple of ki
122b5 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d 20 lobytes or so -
122b6 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67 potentially larg
122b7 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65 er than the page
122b8 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 .** size..*/.st
122b9 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64 atic int pager_d
122ba 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a elmaster(Pager *
122bb 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68 pPager, const ch
122bc 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 ar *zMaster){.
122bd 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
122be 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 s = pPager->pVfs
122bf 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 ;. int rc;
122c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
122c1 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
122c2 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
122c3 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d pMaster; /* M
122c4 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a alloc'd master-j
122c5 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 ournal file desc
122c6 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 riptor */. sqli
122c7 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e te3_file *pJourn
122c8 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 al; /* Malloc'
122c9 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20 d child-journal
122ca 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
122cb 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 */. char *zMast
122cc 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f erJournal = 0; /
122cd 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61 * Contents of ma
122ce 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
122cf 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74 e */. i64 nMast
122d0 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 erJournal;
122d1 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74 /* Size of mast
122d2 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
122d3 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 */.. /* Allocat
122d4 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 68 e space for both
122d5 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 6e the pJournal an
122d6 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 64 d pMaster file d
122d7 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a 2a escriptors.. **
122d8 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
122d9 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 open the master
122da 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 journal file for
122db 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 reading.. */.
122dc 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69 pMaster = (sqli
122dd 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 te3_file *)sqlit
122de 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66 e3MallocZero(pVf
122df 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29 s->szOsFile * 2)
122e0 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28 ;. pJournal = (
122e1 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28 sqlite3_file *)(
122e2 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20 ((u8 *)pMaster)
122e3 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 + pVfs->szOsFile
122e4 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65 );. if( !pMaste
122e5 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 r ){. rc = SQ
122e6 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 LITE_NOMEM;. }e
122e7 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 lse{. const i
122e8 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49 nt flags = (SQLI
122e9 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
122ea 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 |SQLITE_OPEN_MAS
122eb 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 TER_JOURNAL);.
122ec 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
122ed 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74 Open(pVfs, zMast
122ee 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61 er, pMaster, fla
122ef 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 gs, 0);. }. if
122f0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
122f1 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 ) goto delmaster
122f2 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71 _out;.. rc = sq
122f3 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
122f4 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 pMaster, &nMaste
122f5 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 rJournal);. if(
122f6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
122f7 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
122f8 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73 out;.. if( nMas
122f9 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a terJournal>0 ){.
122fa 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e char *zJourn
122fb 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d al;. char *zM
122fc 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 20 asterPtr = 0;.
122fd 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72 int nMasterPtr
122fe 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e = pVfs->mxPathn
122ff 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c ame+1;.. /* L
12300 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d oad the entire m
12301 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
12302 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 le into space ob
12303 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 20 tained from.
12304 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ** sqlite3_mallo
12305 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 20 c() and pointed
12306 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f 75 to by zMasterJou
12307 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 rnal. . */.
12308 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c zMasterJournal
12309 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
1230a 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 ((int)nMasterJou
1230b 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74 rnal + nMasterPt
1230c 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20 r + 1);. if(
1230d 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 !zMasterJournal
1230e 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
1230f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
12310 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
12311 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
12312 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d zMasterPtr = &zM
12313 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 asterJournal[nMa
12314 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d 3b 0a sterJournal+1];.
12315 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12316 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20 OsRead(pMaster,
12317 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20 zMasterJournal,
12318 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72 (int)nMasterJour
12319 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 nal, 0);. if(
1231a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1231b 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f goto delmaster_
1231c 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 out;. zMaster
1231d 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a Journal[nMasterJ
1231e 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a 20 20 ournal] = 0;..
1231f 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61 zJournal = zMa
12320 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 sterJournal;.
12321 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61 while( (zJourna
12322 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c l-zMasterJournal
12323 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c )<nMasterJournal
12324 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78 ){. int ex
12325 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d ists;. rc =
12326 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
12327 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c (pVfs, zJournal,
12328 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
12329 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b XISTS, &exists);
1232a 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
1232b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1232c 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
1232d 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a er_out;. }.
1232e 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73 if( exists
1232f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f ){. /* O
12330 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 ne of the journa
12331 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ls pointed to by
12332 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
12333 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20 nal exists..
12334 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61 ** Open it a
12335 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70 nd check if it p
12336 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73 oints at the mas
12337 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a ter journal. If.
12338 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72 ** so, r
12339 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65 eturn without de
1233a 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65 leting the maste
1233b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a r journal file..
1233c 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1233d 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 int c;.
1233e 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 int flags = (S
1233f 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
12340 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f NLY|SQLITE_OPEN_
12341 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 MAIN_JOURNAL);.
12342 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
12343 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
12344 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e zJournal, pJourn
12345 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 al, flags, 0);.
12346 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
12347 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
12348 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
12349 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 ster_out;.
1234a 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20 }.. rc
1234b 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
1234c 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d nal(pJournal, zM
1234d 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65 asterPtr, nMaste
1234e 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73 rPtr);. s
1234f 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a qlite3OsClose(pJ
12350 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20 ournal);.
12351 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12352 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
12353 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f goto delmaster_o
12354 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 ut;. }..
12355 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74 c = zMast
12356 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73 erPtr[0]!=0 && s
12357 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72 trcmp(zMasterPtr
12358 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20 , zMaster)==0;.
12359 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a if( c ){.
1235a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 /* We
1235b 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f have a match. Do
1235c 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 not delete the
1235d 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
1235e 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ile. */.
1235f 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 goto delmaster
12360 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a _out;. }.
12361 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a }. zJ
12362 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74 ournal += (sqlit
12363 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72 e3Strlen30(zJour
12364 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 nal)+1);. }.
12365 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c }. . rc = sql
12366 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 ite3OsDelete(pVf
12367 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a s, zMaster, 0);.
12368 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a .delmaster_out:.
12369 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75 if( zMasterJou
1236a 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 rnal ){. sqli
1236b 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72 te3_free(zMaster
1236c 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a Journal);. } .
1236d 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b if( pMaster ){
1236e 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c . sqlite3OsCl
1236f 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 ose(pMaster);.
12370 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 assert( !isOpe
12371 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20 n(pJournal) );.
12372 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 }. sqlite3_fre
12373 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 e(pMaster);. re
12374 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
12375 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
12376 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e is used to chan
12377 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69 ge the actual si
12378 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
12379 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74 se .** file in t
1237a 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20 he file-system.
1237b 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e This only happen
1237c 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e s when committin
1237d 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c g a transaction,
1237e 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 .** or rolling b
1237f 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f ack a transactio
12380 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c n (including rol
12381 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d ling back a hot-
12382 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20 journal)..**.**
12383 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 If the main data
12384 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 base file is not
12385 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63 open, or an exc
12386 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e lusive lock is n
12387 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73 ot.** held, this
12388 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
12389 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c o-op. Otherwise,
1238a 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1238b 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e file is.** chan
1238c 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67 ged to nPage pag
1238d 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72 es (nPage*pPager
1238e 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 ->pageSize bytes
1238f 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a ). If the file.*
12390 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72 * on disk is cur
12391 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 rently larger th
12392 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20 an nPage pages,
12393 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53 then use the VFS
12394 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20 .** xTruncate()
12395 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61 method to trunca
12396 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c te it..**.** Or,
12397 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20 it might might
12398 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 be the case that
12399 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 the file on dis
1239a 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 k is smaller tha
1239b 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65 n .** nPage page
1239c 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e s. Some operatin
1239d 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65 g system impleme
1239e 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74 ntations can get
1239f 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a confused if .**
123a0 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e you try to trun
123a1 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73 cate a file to s
123a2 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73 ome size that is
123a3 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20 larger than it
123a4 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73 .** currently is
123a5 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73 , so detect this
123a6 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20 case and write
123a7 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79 a single zero by
123a8 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e te to .** the en
123a9 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c d of the new fil
123aa 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a e instead..**.**
123ab 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
123ac 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
123ad 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 . If an IO error
123ae 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f occurs while mo
123af 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64 difying.** the d
123b0 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65 atabase file, re
123b1 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 turn the error c
123b2 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ode to the calle
123b3 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
123b4 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 pager_truncate(
123b5 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
123b6 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e gno nPage){. in
123b7 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
123b8 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e ;. if( pPager->
123b9 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 state>=PAGER_EXC
123ba 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e LUSIVE && isOpen
123bb 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a (pPager->fd) ){.
123bc 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 i64 currentS
123bd 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 ize, newSize;.
123be 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74 /* TODO: Is it
123bf 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67 safe to use Pag
123c0 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65 er.dbFileSize he
123c1 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 re? */. rc =
123c2 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a sqlite3OsFileSiz
123c3 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63 e(pPager->fd, &c
123c4 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20 urrentSize);.
123c5 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65 newSize = pPage
123c6 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34 r->pageSize*(i64
123c7 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20 )nPage;. if(
123c8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
123c9 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65 currentSize!=ne
123ca 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69 wSize ){. i
123cb 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e f( currentSize>n
123cc 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 ewSize ){.
123cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
123ce 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d Truncate(pPager-
123cf 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20 >fd, newSize);.
123d0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
123d1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
123d2 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
123d3 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69 fd, "", 1, newSi
123d4 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ze-1);. }.
123d5 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
123d6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
123d7 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 pPager->dbFile
123d8 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 Size = nPage;.
123d9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
123da 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
123db 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 /*.** Set the va
123dc 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72 lue of the Pager
123dd 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69 .sectorSize vari
123de 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76 able for the giv
123df 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65 en.** pager base
123e0 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72 d on the value r
123e1 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 eturned by the x
123e2 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f SectorSize metho
123e3 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e d.** of the open
123e4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
123e5 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 The sector size
123e6 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65 will be used use
123e7 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 d .** to determi
123e8 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20 ne the size and
123e9 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75 alignment of jou
123ea 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20 rnal header and
123eb 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e .** master journ
123ec 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 al pointers with
123ed 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e in created journ
123ee 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 al files..**.**
123ef 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 For temporary fi
123f0 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76 les the effectiv
123f1 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 e sector size is
123f2 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 always 512 byte
123f3 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 s..**.** Otherwi
123f4 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70 se, for non-temp
123f5 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65 orary files, the
123f6 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f effective secto
123f7 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65 r size is.** the
123f8 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
123f9 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69 by the xSectorSi
123fa 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e ze() method roun
123fb 64 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 66 ded up to 512 if
123fc 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74 .** it is less t
123fd 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 6e han 512, or roun
123fe 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f ded down to MAX_
123ff 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69 SECTOR_SIZE if i
12400 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 t.** is greater
12401 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f than MAX_SECTOR_
12402 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 SIZE..*/.static
12403 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 69 void setSectorSi
12404 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
12405 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f ){. assert( isO
12406 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
12407 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 || pPager->tempF
12408 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 ile );.. if( !p
12409 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
1240a 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 ){. /* Sector
1240b 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 size doesn't ma
1240c 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 tter for tempora
1240d 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 ry files. Also,
1240e 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 the file. **
1240f 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 may not have bee
12410 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e n opened yet, in
12411 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20 which case the
12412 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 OsSectorSize().
12413 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 ** call will
12414 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f segfault.. */
12415 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
12416 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 torSize = sqlite
12417 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 3OsSectorSize(pP
12418 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 ager->fd);. }.
12419 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 if( pPager->sec
1241a 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20 torSize<512 ){.
1241b 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f pPager->secto
1241c 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d rSize = 512;. }
1241d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
1241e 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 ectorSize>MAX_SE
1241f 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20 CTOR_SIZE ){.
12420 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43 assert( MAX_SEC
12421 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b TOR_SIZE>=512 );
12422 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 . pPager->sec
12423 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45 torSize = MAX_SE
12424 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d CTOR_SIZE;. }.}
12425 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b ../*.** Playback
12426 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 the journal and
12427 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68 thus restore th
12428 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
12429 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 to.** the state
1242a 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65 it was in before
1242b 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69 we started maki
1242c 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a ng changes. .**
1242d 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 .** The journal
1242e 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61 file format is a
1242f 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a s follows: .**.*
12430 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70 * (1) 8 byte p
12431 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f refix. A copy o
12432 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b f aJournalMagic[
12433 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79 ]..** (2) 4 by
12434 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
12435 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
12436 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c he number of val
12437 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a id page records.
12438 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20 ** in the
12439 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69 journal. If thi
1243a 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66 s value is 0xfff
1243b 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70 fffff, then comp
1243c 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 ute the.**
1243d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 number of page
1243e 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 records from the
1243f 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a journal size..*
12440 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62 * (3) 4 byte b
12441 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
12442 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69 r which is the i
12443 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72 nitial value for
12444 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73 the .** s
12445 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a anity checksum..
12446 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20 ** (4) 4 byte
12447 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 integer which is
12448 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
12449 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65 ages to truncate
1244a 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 the.** da
1244b 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67 tabase to during
1244c 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 a rollback..**
1244d 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69 67 (5) 4 byte big
1244e 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 -endian integer
1244f 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65 63 which is the sec
12450 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 68 tor size. The h
12451 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 69 eader.** i
12452 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 s this many byte
12453 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 s in size..** (
12454 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6) 4 byte big-e
12455 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 ndian integer wh
12456 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65 20 ich is the page
12457 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 34 case..** (7) 4
12458 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 byte integer wh
12459 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 ich is the numbe
1245a 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
1245b 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
1245c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e 20 .** name.
1245d 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 62 The value may b
1245e 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 65 e zero (indicate
1245f 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e that there is n
12460 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 o master.**
12461 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 20 journal.).**
12462 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 20 (8) N bytes of
12463 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
12464 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e 61 al name. The na
12465 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d 74 me will be nul-t
12466 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 erminated.**
12467 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 20 and might be
12468 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 65 shorter than the
12469 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d value read from
1246a 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 69 (5). If the fi
1246b 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 rst byte.**
1246c 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 of the name is
1246d 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 65 \000 then there
1246e 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f is no master jo
1246f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 74 urnal. The mast
12470 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72 er.** jour
12471 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f 72 nal name is stor
12472 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 20 ed in UTF-8..**
12473 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f (9) Zero or mo
12474 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63 re pages instanc
12475 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c es, each as foll
12476 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b ows:.** +
12477 20 20 34 20 62 79 74 65 20 70 61 67 65 20 6e 75 4 byte page nu
12478 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 mber..**
12479 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 + pPager->pageS
1247a 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 ize bytes of dat
1247b 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 a..** +
1247c 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a 4 byte checksum.
1247d 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70 **.** When we sp
1247e 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e eak of the journ
1247f 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65 al header, we me
12480 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 69 an the first 8 i
12481 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45 tems above..** E
12482 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ach entry in the
12483 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69 journal is an i
12484 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 39 nstance of the 9
12485 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43 th item..**.** C
12486 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72 all the value fr
12487 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75 om the second bu
12488 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52 llet "nRec". nR
12489 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ec is the number
1248a 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67 of.** valid pag
1248b 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 e entries in the
1248c 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f journal. In mo
1248d 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61 st cases, you ca
1248e 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a n compute the.**
1248f 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66 value of nRec f
12490 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 rom the size of
12491 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12492 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 65 . But if a powe
12493 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63 r.** failure occ
12494 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 20 urred while the
12495 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e journal was bein
12496 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f g written, it co
12497 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 uld be the.** ca
12498 73 65 20 74 68 61 74 20 74 68 65 20 73 69 7a 65 se that the size
12499 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
1249a 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79 file had already
1249b 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20 been increased
1249c 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61 but.** the extra
1249d 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74 entries had not
1249e 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 66 yet made it saf
1249f 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e ely to disk. In
124a0 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a such a case,.**
124a1 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 the value of nR
124a2 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d ec computed from
124a3 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77 the file size w
124a4 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67 ould be too larg
124a5 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20 e. For.** that
124a6 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79 reason, we alway
124a7 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 76 s use the nRec v
124a8 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 64 alue in the head
124a9 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 er..**.** If the
124aa 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 30 nRec value is 0
124ab 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 61 xffffffff it mea
124ac 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f ns that nRec sho
124ad 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a uld be computed.
124ae 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 ** from the file
124af 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c size. This val
124b0 75 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 ue is used when
124b1 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 73 the user selects
124b2 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 the.** no-sync
124b3 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a option for the j
124b4 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 ournal. A power
124b5 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c failure could l
124b6 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f ead to corruptio
124b7 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 n.** in this cas
124b8 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e e. But for thin
124b9 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72 gs like temporar
124ba 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77 y table (which w
124bb 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 ill be.** delete
124bc 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72 d when the power
124bd 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 65 is restored) we
124be 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a don't care. .*
124bf 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65 *.** If the file
124c0 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a opened as the j
124c1 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
124c2 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 ot a well-formed
124c3 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
124c4 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20 then all pages
124c5 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 up to the first
124c6 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 61 corrupted page a
124c7 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 re rolled.** bac
124c8 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69 k (or no pages i
124c9 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 f the journal he
124ca 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65 ader is corrupte
124cb 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 d). The journal
124cc 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20 file.** is then
124cd 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49 deleted and SQLI
124ce 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20 TE_OK returned,
124cf 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f just as if no co
124d0 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20 rruption had.**
124d1 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 been encountered
124d2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f ..**.** If an I/
124d3 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 O or malloc() er
124d4 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 ror occurs, the
124d5 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 journal-file is
124d6 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 not deleted.** a
124d7 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 nd an error code
124d8 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
124d9 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61 .** The isHot pa
124da 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65 rameter indicate
124db 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72 s that we are tr
124dc 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b ying to rollback
124dd 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68 a journal.** th
124de 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f at might be a ho
124df 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 t journal. Or,
124e0 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 it could be that
124e1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
124e2 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 65 .** preserved be
124e3 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c cause of JOURNAL
124e4 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20 MODE_PERSIST or
124e5 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e JOURNALMODE_TRUN
124e6 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20 CATE..** If the
124e7 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 journal really i
124e8 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65 s hot, reset the
124e9 20 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69 pager cache pri
124ea 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61 or rolling.** ba
124eb 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20 ck any content.
124ec 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
124ed 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73 is merely persis
124ee 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69 tent, no reset i
124ef 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a s.** needed..*/.
124f0 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 static int pager
124f1 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20 _playback(Pager
124f2 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48 *pPager, int isH
124f3 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 ot){. sqlite3_v
124f4 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 fs *pVfs = pPage
124f5 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73 r->pVfs;. i64 s
124f6 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 zJ;
124f7 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
124f8 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
124f9 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 in bytes */. u3
124fa 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20 2 nRec;
124fb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
124fc 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74 of Records in t
124fd 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 he journal */.
124fe 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 u32 u;
124ff 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 /* Unsi
12500 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 gned loop counte
12501 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 r */. Pgno mxPg
12502 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
12503 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f /* Size of the o
12504 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20 riginal file in
12505 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 pages */. int r
12506 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
12507 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f /* Result co
12508 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 de of a subrouti
12509 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 ne */. int res
1250a 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 1;
1250b 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e /* Value return
1250c 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41 ed by sqlite3OsA
1250d 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61 ccess() */. cha
1250e 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 r *zMaster = 0;
1250f 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
12510 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
12511 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20 file if any */.
12512 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65 int needPagerRe
12513 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 set; /* Tru
12514 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 20 e to reset page
12515 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70 prior to first p
12516 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a age rollback */.
12517 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 . /* Figure out
12518 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 how many record
12519 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 s are in the jou
1251a 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 rnal. Abort ear
1251b 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a ly if. ** the j
1251c 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e ournal is empty.
1251d 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1251e 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
1251f 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 fd) );. rc = sq
12520 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
12521 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a pPager->jfd, &sz
12522 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 J);. if( rc!=SQ
12523 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d LITE_OK || szJ==
12524 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 0 ){. goto en
12525 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a d_playback;. }.
12526 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6d . /* Read the m
12527 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
12528 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 me from the jour
12529 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 72 nal, if it is pr
1252a 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61 esent.. ** If a
1252b 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1252c 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65 file name is spe
1252d 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 20 cified, but the
1252e 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a file is not. **
1252f 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b present on disk
12530 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
12531 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e al is not hot an
12532 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 d does not need
12533 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65 to be. ** playe
12534 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a d back.. **. *
12535 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61 * TODO: Technica
12536 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e lly the followin
12537 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 65 g is an error be
12538 63 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 73 cause it assumes
12539 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 65 that. ** buffe
1253a 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 r Pager.pTmpSpac
1253b 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d 65 e is (mxPathname
1253c 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 72 +1) bytes or lar
1253d 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20 ger. i.e. that.
1253e 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 67 ** (pPager->pag
1253f 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d eSize >= pPager-
12540 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d >pVfs->mxPathnam
12541 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75 e+1). Using os_u
12542 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50 nix.c,. ** mxP
12543 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c 20 athname is 512,
12544 77 68 69 63 68 20 69 73 20 74 68 65 20 73 61 6d which is the sam
12545 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d e as the minimum
12546 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65 allowable value
12547 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 69 . ** for pageSi
12548 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74 ze.. */. zMast
12549 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d er = pPager->pTm
1254a 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72 pSpace;. rc = r
1254b 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c eadMasterJournal
1254c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d (pPager->jfd, zM
1254d 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 aster, pPager->p
1254e 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
1254f 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 1);. if( rc==SQ
12550 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 LITE_OK && zMast
12551 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 20 er[0] ){. rc
12552 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 = sqlite3OsAcces
12553 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c s(pVfs, zMaster,
12554 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
12555 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 XISTS, &res);.
12556 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b }. zMaster = 0;
12557 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
12558 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a E_OK || !res ){.
12559 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 goto end_pla
1255a 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61 yback;. }. pPa
1255b 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
1255c 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 72 = 0;. needPager
1255d 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a Reset = isHot;..
1255e 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 /* This loop t
1255f 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72 erminates either
12560 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 72 when a readJour
12561 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 2a nalHdr() or . *
12562 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b * pager_playback
12563 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c 6c _one_page() call
12564 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f returns SQLITE_
12565 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72 DONE or an IO er
12566 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73 ror . ** occurs
12567 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 . . */. while(
12568 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 1 ){. int is
12569 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 Unsync = 0;..
1256a 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78 /* Read the nex
1256b 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
1256c 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
1256d 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 l file. If ther
1256e 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 e are. ** not
1256f 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c 65 enough bytes le
12570 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ft in the journa
12571 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d l file for a com
12572 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f 72 plete header, or
12573 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 6f . ** it is co
12574 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 20 rrupted, then a
12575 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 20 process must of
12576 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69 failed while wri
12577 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20 ting it.. **
12578 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e This indicates n
12579 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64 othing more need
1257a 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 s to be rolled b
1257b 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ack.. */.
1257c 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c rc = readJournal
1257d 48 64 72 28 70 50 61 67 65 72 2c 20 69 73 48 6f Hdr(pPager, isHo
1257e 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 t, szJ, &nRec, &
1257f 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 mxPg);. if( r
12580 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 c!=SQLITE_OK ){
12581 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
12582 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 QLITE_DONE ){.
12583 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
12584 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 E_OK;. }.
12585 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 goto end_pla
12586 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 yback;. }..
12587 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 /* If nRec is
12588 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 0xffffffff, then
12589 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 this journal wa
1258a 73 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 s created by a p
1258b 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f rocess. ** wo
1258c 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 rking in no-sync
1258d 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e mode. This mean
1258e 73 20 74 68 61 74 20 74 68 65 20 72 65 73 74 20 s that the rest
1258f 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 of the journal.
12590 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 ** file consi
12591 73 74 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68 sts of pages, th
12592 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 ere are no more
12593 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e journal headers.
12594 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 Compute. **
12595 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 the value of nRe
12596 63 20 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20 c based on this
12597 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 assumption..
12598 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d */. if( nRec=
12599 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20 =0xffffffff ){.
1259a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
1259b 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d ger->journalOff=
1259c 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 =JOURNAL_HDR_SZ(
1259d 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 pPager) );.
1259e 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 nRec = (int)((s
1259f 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 zJ - JOURNAL_HDR
125a0 5f 53 5a 28 70 50 61 67 65 72 29 29 2f 4a 4f 55 _SZ(pPager))/JOU
125a1 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 RNAL_PG_SZ(pPage
125a2 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 r));. }..
125a3 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 20 /* If nRec is 0
125a4 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 62 61 63 and this rollbac
125a5 6b 20 69 73 20 6f 66 20 61 20 74 72 61 6e 73 61 k is of a transa
125a6 63 74 69 6f 6e 20 63 72 65 61 74 65 64 20 62 79 ction created by
125a7 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f this. ** pro
125a8 63 65 73 73 20 61 6e 64 20 69 66 20 74 68 69 73 cess and if this
125a9 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 68 65 is the final he
125aa 61 64 65 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 ader in the jour
125ab 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 6d 65 61 nal, then it mea
125ac 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 ns. ** that t
125ad 68 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 his part of the
125ae 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e journal was bein
125af 67 20 66 69 6c 6c 65 64 20 62 75 74 20 68 61 73 g filled but has
125b0 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a 20 20 not yet been.
125b1 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 ** synced to d
125b2 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 20 74 68 isk. Compute th
125b3 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
125b4 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72 s based on the r
125b5 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 emaining. **
125b6 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 size of the file
125b7 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
125b8 54 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 6f The third term o
125b9 66 20 74 68 65 20 74 65 73 74 20 77 61 73 20 61 f the test was a
125ba 64 64 65 64 20 74 6f 20 66 69 78 20 74 69 63 6b dded to fix tick
125bb 65 74 20 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a et #2565.. **
125bc 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 When rolling ba
125bd 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c ck a hot journal
125be 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61 79 73 , nRec==0 always
125bf 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
125c0 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e next. ** chun
125c1 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c k of the journal
125c2 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70 contains zero p
125c3 61 67 65 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 ages to be rolle
125c4 64 20 62 61 63 6b 2e 20 20 42 75 74 0a 20 20 20 d back. But.
125c5 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 ** when doing a
125c6 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68 ROLLBACK and th
125c7 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20 e nRec==0 chunk
125c8 69 73 20 74 68 65 20 6c 61 73 74 20 63 68 75 6e is the last chun
125c9 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 k in. ** the
125ca 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e journal, it mean
125cb 73 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e s that the journ
125cc 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e al might contain
125cd 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20 additional.
125ce 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65 ** pages that ne
125cf 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 ed to be rolled
125d0 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20 74 68 back and that th
125d1 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
125d2 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 s . ** should
125d3 20 62 65 20 63 6f 6d 70 75 74 65 64 20 62 61 73 be computed bas
125d4 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ed on the journa
125d5 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20 l file size..
125d6 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 */. if( nRec
125d7 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 26 ==0 && !isHot &&
125d8 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
125d9 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 >journalHdr+JOUR
125da 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
125db 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r)==pPager->jour
125dc 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 nalOff ){.
125dd 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a nRec = (int)((sz
125de 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 J - pPager->jour
125df 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 nalOff) / JOURNA
125e0 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 L_PG_SZ(pPager))
125e1 3b 0a 20 20 20 20 20 20 69 73 55 6e 73 79 6e 63 ;. isUnsync
125e2 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 = 1;. }..
125e3 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 /* If this is t
125e4 68 65 20 66 69 72 73 74 20 68 65 61 64 65 72 20 he first header
125e5 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f read from the jo
125e6 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 urnal, truncate
125e7 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 the. ** datab
125e8 61 73 65 20 66 69 6c 65 20 62 61 63 6b 20 74 6f ase file back to
125e9 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 its original si
125ea 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ze.. */. i
125eb 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
125ec 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 alOff==JOURNAL_H
125ed 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b DR_SZ(pPager) ){
125ee 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
125ef 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 r_truncate(pPage
125f0 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 r, mxPg);.
125f1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
125f2 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
125f3 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
125f4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 }. pP
125f5 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d ager->dbSize = m
125f6 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 xPg;. }..
125f7 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c /* Copy original
125f8 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74 68 pages out of th
125f9 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 e journal and ba
125fa 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 ck into the .
125fb 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
125fc 65 20 61 6e 64 2f 6f 72 20 70 61 67 65 20 63 61 e and/or page ca
125fd 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 che.. */.
125fe 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b for(u=0; u<nRec;
125ff 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 u++){. if(
12600 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 needPagerReset
12601 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 ){. pager
12602 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a _reset(pPager);.
12603 20 20 20 20 20 20 20 20 6e 65 65 64 50 61 67 65 needPage
12604 72 52 65 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 rReset = 0;.
12605 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70 }. rc = p
12606 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e ager_playback_on
12607 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 31 2c e_page(pPager,1,
12608 69 73 55 6e 73 79 6e 63 2c 26 70 50 61 67 65 72 isUnsync,&pPager
12609 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 30 ->journalOff,0,0
1260a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1260b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1260c 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1260d 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 LITE_DONE ){.
1260e 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
1260f 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 TE_OK;.
12610 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12611 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 Off = szJ;.
12612 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
12613 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
12614 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 /* If we ar
12615 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c e unable to roll
12616 62 61 63 6b 2c 20 71 75 69 74 20 61 6e 64 20 72 back, quit and r
12617 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 0a eturn the error.
12618 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 ** cod
12619 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 e. This will ca
1261a 75 73 65 20 74 68 65 20 70 61 67 65 72 20 74 6f use the pager to
1261b 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 enter the error
1261c 20 73 74 61 74 65 0a 20 20 20 20 20 20 20 20 20 state.
1261d 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f 20 66 ** so that no f
1261e 75 72 74 68 65 72 20 68 61 72 6d 20 77 69 6c 6c urther harm will
1261f 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 72 68 61 be done. Perha
12620 70 73 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20 ps the next.
12621 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 ** process
12622 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 to come along w
12623 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 72 ill be able to r
12624 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 61 74 61 ollback the data
12625 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 base..
12626 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 */. got
12627 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
12628 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
12629 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a }. }. }. /*
1262a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 NOTREACHED*/. a
1262b 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 ssert( 0 );..end
1262c 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 _playback:. /*
1262d 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c Following a roll
1262e 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61 back, the databa
1262f 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 se file should b
12630 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 e back in its or
12631 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 iginal. ** stat
12632 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 e prior to the s
12633 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e tart of the tran
12634 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f saction, so invo
12635 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 ke the. ** SQLI
12636 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 TE_FCNTL_DB_UNCH
12637 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 ANGED file-contr
12638 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 ol method to dis
12639 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 able the. ** as
1263a 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 68 65 sertion that the
1263b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 transaction cou
1263c 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 69 65 nter was modifie
1263d 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 d.. */. assert
1263e 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 64 (. pPager->fd
1263f 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c ->pMethods==0 ||
12640 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 . sqlite3OsFi
12641 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 leControl(pPager
12642 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54 ->fd,SQLITE_FCNT
12643 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30 L_DB_UNCHANGED,0
12644 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 )>=SQLITE_OK. )
12645 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 ;.. /* If this
12646 70 6c 61 79 62 61 63 6b 20 69 73 20 68 61 70 70 playback is happ
12647 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 69 63 61 ening automatica
12648 6c 6c 79 20 61 73 20 61 20 72 65 73 75 6c 74 20 lly as a result
12649 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a 20 20 2a of an IO or . *
1264a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74 * malloc error t
1264b 68 61 74 20 6f 63 63 75 72 72 65 64 20 61 66 74 hat occurred aft
1264c 65 72 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f er the change-co
1264d 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74 65 unter was update
1264e 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 65 66 6f d but . ** befo
1264f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 re the transacti
12650 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 on was committed
12651 2c 20 74 68 65 6e 20 74 68 65 20 63 68 61 6e 67 , then the chang
12652 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2a 20 e-counter . **
12653 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 61 79 modification may
12654 20 6a 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 just have been
12655 72 65 76 65 72 74 65 64 2e 20 49 66 20 74 68 69 reverted. If thi
12656 73 20 68 61 70 70 65 6e 73 20 69 6e 20 65 78 63 s happens in exc
12657 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 6d 6f 64 lusive . ** mod
12658 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 65 e, then subseque
12659 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 nt transactions
1265a 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65 performed by the
1265b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c connection will
1265c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 61 74 65 not. ** update
1265d 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
1265e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68 69 73 ter at all. This
1265f 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63 61 63 may lead to cac
12660 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79 he inconsistency
12661 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 66 . ** problems f
12662 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 or other process
12663 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 es at some point
12664 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 20 in the future.
12665 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e So, just. ** in
12666 20 63 61 73 65 20 74 68 69 73 20 68 61 73 20 68 case this has h
12667 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 72 20 74 appened, clear t
12668 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f he changeCountDo
12669 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a ne flag now.. *
1266a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e /. pPager->chan
1266b 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 geCountDone = pP
1266c 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a ager->tempFile;.
1266d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1266e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73 E_OK ){. zMas
1266f 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 ter = pPager->pT
12670 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 63 20 mpSpace;. rc
12671 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 = readMasterJour
12672 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c nal(pPager->jfd,
12673 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 zMaster, pPager
12674 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 ->pVfs->mxPathna
12675 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 73 74 63 me+1);. testc
12676 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ase( rc!=SQLITE_
12677 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 OK );. }. if(
12678 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
12679 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f . rc = pager_
1267a 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 end_transaction(
1267b 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b pPager, zMaster[
1267c 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74 0]!='\0');. t
1267d 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c estcase( rc!=SQL
1267e 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 ITE_OK );. }.
1267f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12680 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 K && zMaster[0]
12681 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a && res ){. /*
12682 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20 If there was a
12683 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 master journal a
12684 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nd this routine
12685 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 63 will return succ
12686 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 ess,. ** see
12687 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c if it is possibl
12688 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 e to delete the
12689 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a master journal..
1268a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
1268b 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 pager_delmaster(
1268c 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 pPager, zMaster)
1268d 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
1268e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
1268f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 . }.. /* The P
12690 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 ager.sectorSize
12691 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76 variable may hav
12692 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 20 77 e been updated w
12693 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a hile rolling. *
12694 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c * back a journal
12695 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 created by a pr
12696 6f 63 65 73 73 20 77 69 74 68 20 61 20 64 69 66 ocess with a dif
12697 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69 ferent sector si
12698 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 ze. ** value. R
12699 65 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 63 eset it to the c
1269a 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72 orrect value for
1269b 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 this process..
1269c 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53 */. setSectorS
1269d 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 ize(pPager);. r
1269e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1269f 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73 61 76 65 ** Playback save
126a0 70 6f 69 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 point pSavepoint
126a1 2e 20 4f 72 2c 20 69 66 20 70 53 61 76 65 70 6f . Or, if pSavepo
126a2 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 int==NULL, then
126a3 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 playback.** the
126a4 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f entire master jo
126a5 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 urnal file. The
126a6 63 61 73 65 20 70 53 61 76 65 70 6f 69 6e 74 3d case pSavepoint=
126a7 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 =NULL occurs whe
126a8 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b n .** a ROLLBACK
126a9 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 TO command is i
126aa 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 nvoked on a SAVE
126ab 50 4f 49 4e 54 20 74 68 61 74 20 69 73 20 61 20 POINT that is a
126ac 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 transaction .**
126ad 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a savepoint..**.**
126ae 20 57 68 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 When pSavepoint
126af 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 is not NULL (me
126b0 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e aning a non-tran
126b1 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e saction savepoin
126b2 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 t is .** being r
126b3 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74 68 65 olled back), the
126b4 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 n the rollback c
126b5 6f 6e 73 69 73 74 73 20 6f 66 20 75 70 20 74 6f onsists of up to
126b6 20 74 68 72 65 65 20 73 74 61 67 65 73 2c 0a 2a three stages,.*
126b7 2a 20 70 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 * performed in t
126b8 68 65 20 6f 72 64 65 72 20 73 70 65 63 69 66 69 he order specifi
126b9 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 ed:.**.** * Pa
126ba 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 20 62 ges are played b
126bb 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 ack from the mai
126bc 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 n journal starti
126bd 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 ng at byte.**
126be 20 20 6f 66 66 73 65 74 20 50 61 67 65 72 53 61 offset PagerSa
126bf 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 vepoint.iOffset
126c0 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 and continuing t
126c1 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 o .** PagerS
126c2 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
126c3 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 set, or to the e
126c4 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a nd of the main j
126c5 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 ournal.** fi
126c6 6c 65 20 69 66 20 50 61 67 65 72 53 61 76 65 70 le if PagerSavep
126c7 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 oint.iHdrOffset
126c8 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 is zero..**.**
126c9 20 2a 20 49 66 20 50 61 67 65 72 53 61 76 65 70 * If PagerSavep
126ca 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 oint.iHdrOffset
126cb 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
126cc 6e 20 70 61 67 65 73 20 61 72 65 20 70 6c 61 79 n pages are play
126cd 65 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 ed.** back s
126ce 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 tarting from the
126cf 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
126d0 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c immediately foll
126d1 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 owing .** Pa
126d2 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 gerSavepoint.iHd
126d3 72 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 65 rOffset to the e
126d4 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a nd of the main j
126d5 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a ournal file..**.
126d6 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 65 ** * Pages are
126d7 20 74 68 65 6e 20 70 6c 61 79 65 64 20 62 61 63 then played bac
126d8 6b 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a k from the sub-j
126d9 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 ournal file, sta
126da 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 rting.** wit
126db 68 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 h the PagerSavep
126dc 6f 69 6e 74 2e 69 53 75 62 52 65 63 20 61 6e 64 oint.iSubRec and
126dd 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 continuing to t
126de 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 he end of.**
126df 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
126e0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 e..**.** Through
126e1 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b out the rollback
126e2 20 70 72 6f 63 65 73 73 2c 20 65 61 63 68 20 74 process, each t
126e3 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 72 6f ime a page is ro
126e4 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a lled back, the.*
126e5 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 * corresponding
126e6 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 61 20 bit is set in a
126e7 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 bitvec structure
126e8 20 28 76 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 (variable pDone
126e9 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 in the.** imple
126ea 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 mentation below)
126eb 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 . This is used t
126ec 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 20 o ensure that a
126ed 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 page is only.**
126ee 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 rolled back the
126ef 66 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 73 first time it is
126f0 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 encountered in
126f1 65 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a either journal..
126f2 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 6f **.** If pSavepo
126f3 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 int is NULL, the
126f4 6e 20 70 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 n pages are only
126f5 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f played back fro
126f6 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f m the main.** jo
126f7 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 urnal file. Ther
126f8 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 e is no need for
126f9 20 61 20 62 69 74 76 65 63 20 69 6e 20 74 68 69 a bitvec in thi
126fa 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e s case..**.** In
126fb 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 62 65 either case, be
126fc 66 6f 72 65 20 70 6c 61 79 62 61 63 6b 20 63 6f fore playback co
126fd 6d 6d 65 6e 63 65 73 20 74 68 65 20 50 61 67 65 mmences the Page
126fe 72 2e 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c r.dbSize variabl
126ff 65 0a 2a 2a 20 69 73 20 72 65 73 65 74 20 74 6f e.** is reset to
12700 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 the value that
12701 69 74 20 68 65 6c 64 20 61 74 20 74 68 65 20 73 it held at the s
12702 74 61 72 74 20 6f 66 20 74 68 65 20 73 61 76 65 tart of the save
12703 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 point .** (or tr
12704 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 ansaction). No p
12705 61 67 65 20 77 69 74 68 20 61 20 70 61 67 65 2d age with a page-
12706 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 number greater t
12707 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 0a 2a han this value.*
12708 2a 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b * is played back
12709 2e 20 49 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f . If one is enco
1270a 75 6e 74 65 72 65 64 20 69 74 20 69 73 20 73 69 untered it is si
1270b 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f mply skipped..*/
1270c 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
1270d 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 rPlaybackSavepoi
1270e 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
1270f 2c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 , PagerSavepoint
12710 20 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 *pSavepoint){.
12711 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 i64 szJ;
12712 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 66 66 /* Eff
12713 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 ective size of t
12714 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
12715 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 */. i64 iHdrOff
12716 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
12717 20 45 6e 64 20 6f 66 20 66 69 72 73 74 20 73 65 End of first se
12718 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f gment of main-jo
12719 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f urnal records */
1271a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1271b 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 TE_OK; /* R
1271c 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
1271d 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 Bitvec *pDone =
1271e 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76 0; /* Bitv
1271f 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70 61 67 ec to ensure pag
12720 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f es played back o
12721 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 nly once */.. a
12722 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
12723 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 tate>=PAGER_SHAR
12724 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f ED );.. /* Allo
12725 63 61 74 65 20 61 20 62 69 74 76 65 63 20 74 6f cate a bitvec to
12726 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68 use to store th
12727 65 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 72 e set of pages r
12728 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 olled back */.
12729 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 if( pSavepoint )
1272a 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 73 71 {. pDone = sq
1272b 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
1272c 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f e(pSavepoint->nO
1272d 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 21 70 rig);. if( !p
1272e 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 72 65 Done ){. re
1272f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
12730 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 M;. }. }..
12731 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 /* Set the datab
12732 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20 74 6f ase size back to
12733 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 77 61 the value it wa
12734 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 61 76 s before the sav
12735 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69 epoint . ** bei
12736 6e 67 20 72 65 76 65 72 74 65 64 20 77 61 73 20 ng reverted was
12737 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 opened.. */. p
12738 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
12739 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 pSavepoint ? pSa
1273a 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a vepoint->nOrig :
1273b 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
1273c 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70 ize;.. /* Use p
1273d 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
1273e 66 20 61 73 20 74 68 65 20 65 66 66 65 63 74 69 f as the effecti
1273f 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d ve size of the m
12740 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a ain rollback. *
12741 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 * journal. The
12742 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 67 68 actual file migh
12743 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e t be larger than
12744 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41 this in. ** PA
12745 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
12746 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 47 45 TRUNCATE or PAGE
12747 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 R_JOURNALMODE_PE
12748 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e 79 74 RSIST. But anyt
12749 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 20 70 hing. ** past p
1274a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
1274b 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20 f is off-limits
1274c 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a to us.. */. sz
1274d 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 J = pPager->jour
1274e 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 nalOff;.. /* Be
1274f 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 gin by rolling b
12750 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f 6d ack records from
12751 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
12752 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 l starting at.
12753 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e ** PagerSavepoin
12754 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f t.iOffset and co
12755 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 ntinuing to the
12756 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 next journal hea
12757 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 der.. ** There
12758 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 73 might be records
12759 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 in the main jou
1275a 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 61 rnal that have a
1275b 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a page number. *
1275c 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 * greater than t
1275d 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 he current datab
1275e 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 72 ase size (pPager
1275f 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 68 ->dbSize) but th
12760 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 ose. ** will be
12761 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 74 skipped automat
12762 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 61 ically. Pages a
12763 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f 6e re added to pDon
12764 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 61 e as they. ** a
12765 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a re played back..
12766 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 */. if( pSave
12767 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 48 64 point ){. iHd
12768 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e rOff = pSavepoin
12769 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3f 20 t->iHdrOffset ?
1276a 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 pSavepoint->iHdr
1276b 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 Offset : szJ;.
1276c 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
1276d 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e lOff = pSavepoin
1276e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 t->iOffset;.
1276f 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 while( rc==SQLIT
12770 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e E_OK && pPager->
12771 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f journalOff<iHdrO
12772 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ff ){. rc =
12773 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f pager_playback_
12774 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c one_page(pPager,
12775 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 1, 0, &pPager->
12776 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 journalOff, 1, p
12777 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Done);. }.
12778 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
12779 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 65 ITE_DONE );. }e
1277a 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d lse{. pPager-
1277b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b >journalOff = 0;
1277c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 . }.. /* Conti
1277d 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b nue rolling back
1277e 20 72 65 63 6f 72 64 73 20 6f 75 74 20 6f 66 20 records out of
1277f 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c the main journal
12780 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a starting at. *
12781 2a 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 * the first jour
12782 6e 61 6c 20 68 65 61 64 65 72 20 73 65 65 6e 20 nal header seen
12783 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75 and continuing u
12784 6e 74 69 6c 20 74 68 65 20 65 66 66 65 63 74 69 ntil the effecti
12785 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 ve end. ** of t
12786 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
12787 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20 file. Continue
12788 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 to skip out-of-r
12789 61 6e 67 65 20 70 61 67 65 73 20 61 6e 64 0a 20 ange pages and.
1278a 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61 64 64 ** continue add
1278b 69 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 ing pages rolled
1278c 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a back to pDone..
1278d 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 */. while( rc
1278e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
1278f 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
12790 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32 f<szJ ){. u32
12791 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
12792 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
12793 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a 52 65 63 */. u32 nJRec
12794 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d = 0; /* Num
12795 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 ber of Journal R
12796 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 ecords */. u3
12797 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 2 dummy;. rc
12798 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 = readJournalHdr
12799 28 70 50 61 67 65 72 2c 20 30 2c 20 73 7a 4a 2c (pPager, 0, szJ,
1279a 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d 79 29 &nJRec, &dummy)
1279b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
1279c 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
1279d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 .. /*. **
1279e 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a 6f 75 The "pPager->jou
1279f 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f rnalHdr+JOURNAL_
127a0 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d HDR_SZ(pPager)==
127a1 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
127a2 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73 74 20 ff". ** test
127a3 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 69 is related to ti
127a4 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53 65 65 cket #2565. See
127a5 20 74 68 65 20 64 69 73 63 75 73 73 69 6f 6e 20 the discussion
127a6 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 in the. ** pa
127a7 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 66 ger_playback() f
127a8 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 unction for addi
127a9 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
127aa 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 on.. */. i
127ab 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20 f( nJRec==0 .
127ac 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 && pPager->jou
127ad 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f rnalHdr+JOURNAL_
127ae 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d HDR_SZ(pPager)==
127af 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
127b0 66 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ff. ){.
127b1 6e 4a 52 65 63 20 3d 20 28 75 33 32 29 28 28 73 nJRec = (u32)((s
127b2 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 zJ - pPager->jou
127b3 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 4c rnalOff)/JOURNAL
127b4 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b _PG_SZ(pPager));
127b5 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
127b6 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f i=0; rc==SQLITE_
127b7 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63 20 26 OK && ii<nJRec &
127b8 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
127b9 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 7b lOff<szJ; ii++){
127ba 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
127bb 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
127bc 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20 30 age(pPager, 1, 0
127bd 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e , &pPager->journ
127be 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29 alOff, 1, pDone)
127bf 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
127c0 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 rt( rc!=SQLITE_D
127c1 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 ONE );. }. ass
127c2 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
127c3 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f OK || pPager->jo
127c4 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 3b urnalOff==szJ );
127c5 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 .. /* Finally,
127c6 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 73 20 rollback pages
127c7 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 from the sub-jou
127c8 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 61 74 rnal. Page that
127c9 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 76 69 were. ** previ
127ca 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 ously rolled bac
127cb 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69 k out of the mai
127cc 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61 n journal (and a
127cd 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e re hence in pDon
127ce 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 e). ** will be
127cf 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d 6f 66 skipped. Out-of
127d0 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 72 65 -range pages are
127d1 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e 0a 20 also skipped..
127d2 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 */. if( pSavep
127d3 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 32 20 oint ){. u32
127d4 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ii; /
127d5 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
127d6 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 /. i64 offset
127d7 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 = pSavepoint->i
127d8 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72 SubRec*(4+pPager
127d9 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 ->pageSize);.
127da 20 66 6f 72 28 69 69 3d 70 53 61 76 65 70 6f 69 for(ii=pSavepoi
127db 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 63 3d nt->iSubRec; rc=
127dc 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 =SQLITE_OK && ii
127dd 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 <pPager->nSubRec
127de 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 ; ii++){. a
127df 73 73 65 72 74 28 20 6f 66 66 73 65 74 3d 3d 69 ssert( offset==i
127e0 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 i*(4+pPager->pag
127e1 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 20 20 eSize) );.
127e2 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 rc = pager_playb
127e3 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 ack_one_page(pPa
127e4 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66 66 73 ger, 0, 0, &offs
127e5 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 et, 1, pDone);.
127e6 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
127e7 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc!=SQLITE_DONE
127e8 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 );. }.. sqlit
127e9 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
127ea 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72 63 pDone);. if( rc
127eb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
127ec 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
127ed 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d alOff = szJ;. }
127ee 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
127ef 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
127f0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
127f1 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 of in-memory pa
127f2 67 65 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c ges that are all
127f3 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f owed..*/.SQLITE_
127f4 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
127f5 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 ite3PagerSetCach
127f6 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 esize(Pager *pPa
127f7 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 ger, int mxPage)
127f8 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 {. sqlite3Pcach
127f9 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 eSetCachesize(pP
127fa 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d ager->pPCache, m
127fb 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a xPage);.}../*.**
127fc 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75 Adjust the robu
127fd 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61 stness of the da
127fe 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65 tabase to damage
127ff 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 due to OS crash
12800 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 es.** or power f
12801 61 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67 ailures by chang
12802 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ing the number o
12803 66 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20 f syncs()s when
12804 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 writing.** the r
12805 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
12806 20 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 There are thre
12807 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 e levels:.**.**
12808 20 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c OFF sql
12809 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20 ite3OsSync() is
1280a 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 never called. T
1280b 68 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75 his is the defau
1280c 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 lt.**
1280d 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 for temporary
1280e 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66 and transient f
1280f 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e iles..**.** N
12810 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 ORMAL The jou
12811 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f rnal is synced o
12812 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65 nce before write
12813 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a s begin on the.*
12814 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 * d
12815 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 69 atabase. This i
12816 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 s normally adequ
12817 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 ate protection,
12818 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 but.**
12819 20 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 65 it is theore
1281a 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 tically possible
1281b 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e , though very un
1281c 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 likely,.**
1281d 20 20 20 20 20 20 20 20 74 68 61 74 20 61 6e 20 that an
1281e 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72 inopertune power
1281f 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c failure could l
12820 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c eave the journal
12821 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
12822 20 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 63 in a state whic
12823 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61 h would cause da
12824 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 mage to the data
12825 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 base.**
12826 20 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 20 when it is
12827 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
12828 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20 ** FULL
12829 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 The journal is s
1282a 79 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 6f ynced twice befo
1282b 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20 re writes begin
1282c 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 on the.**
1282d 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 database
1282e 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74 (with some addit
1282f 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
12830 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65 n - the nRec fie
12831 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 ld.**
12832 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 of the journa
12833 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67 l header - being
12834 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77 written in betw
12835 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 een the two.**
12836 20 20 20 20 20 20 20 20 20 20 20 20 73 79 6e 63 sync
12837 73 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d s). If we assum
12838 65 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 61 e that writing a
12839 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1283a 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 single disk sec
1283b 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 tor is atomic, t
1283c 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72 hen this mode pr
1283d 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 ovides.**
1283e 20 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 65 assurance
1283f 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 that the journa
12840 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f l will not be co
12841 72 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a rrupted to the.*
12842 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 * p
12843 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20 oint of causing
12844 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 damage to the da
12845 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f tabase during ro
12846 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 llback..**.** Nu
12847 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 73 meric values ass
12848 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
12849 73 65 20 73 74 61 74 65 73 20 61 72 65 20 4f 46 se states are OF
1284a 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a F==1, NORMAL=2,.
1284b 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a ** and FULL=3..*
1284c 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1284d 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 _OMIT_PAGER_PRAG
1284e 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 MAS.SQLITE_PRIVA
1284f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
12850 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 agerSetSafetyLev
12851 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 el(Pager *pPager
12852 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 , int level, int
12853 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 bFullFsync){.
12854 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d pPager->noSync =
12855 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 (level==1 || p
12856 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 Pager->tempFile)
12857 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d ?1:0;. pPager-
12858 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76 >fullSync = (lev
12859 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 72 el==3 && !pPager
1285a 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 ->tempFile) ?1:0
1285b 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 ;. pPager->sync
1285c 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46 _flags = (bFullF
1285d 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 sync?SQLITE_SYNC
1285e 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e _FULL:SQLITE_SYN
1285f 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 C_NORMAL);. if(
12860 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
12861 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 ) pPager->needSy
12862 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 nc = 0;.}.#endif
12863 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
12864 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 owing global var
12865 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 iable is increme
12866 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 nted whenever th
12867 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 e library.** att
12868 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 empts to open a
12869 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 temporary file.
1286a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f This informatio
1286b 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a n is used for.**
1286c 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 testing and ana
1286d 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f lysis only. .*/
1286e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1286f 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
12870 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 nt sqlite3_opent
12871 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 emp_count = 0;.#
12872 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 endif../*.** Ope
12873 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 n a temporary fi
12874 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 le..**.** Write
12875 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
12876 74 6f 72 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e tor into *pFile.
12877 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
12878 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a K on success .**
12879 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 or some other e
1287a 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 rror code if we
1287b 66 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c fail. The OS wil
1287c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 l automatically
1287d 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 74 .** delete the t
1287e 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 77 68 emporary file wh
1287f 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e en it is closed.
12880 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 .**.** The flags
12881 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 56 passed to the V
12882 46 53 20 6c 61 79 65 72 20 78 4f 70 65 6e 28 29 FS layer xOpen()
12883 20 63 61 6c 6c 20 61 72 65 20 74 68 6f 73 65 20 call are those
12884 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 specified.** by
12885 70 61 72 61 6d 65 74 65 72 20 76 66 73 46 6c 61 parameter vfsFla
12886 67 73 20 4f 52 65 64 20 77 69 74 68 20 74 68 65 gs ORed with the
12887 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
12888 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 * SQLITE_OPE
12889 4e 5f 52 45 41 44 57 52 49 54 45 0a 2a 2a 20 20 N_READWRITE.**
1288a 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 SQLITE_OPEN_C
1288b 52 45 41 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c REATE.** SQL
1288c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
1288d 56 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 VE.** SQLITE
1288e 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
1288f 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e OSE.*/.static in
12890 74 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 t pagerOpentemp(
12891 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
12892 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 , /* The
12893 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a pager object */.
12894 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
12895 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 pFile, /* Write
12896 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
12897 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 ptor here */. i
12898 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 nt vfsFlags
12899 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 /* Flags pa
1289a 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 ssed through to
1289b 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 the VFS */.){.
1289c 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
1289d 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
1289e 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 code */..#ifdef
1289f 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 SQLITE_TEST. sq
128a0 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 lite3_opentemp_c
128a1 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 ount++; /* Used
128a2 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 for testing and
128a3 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a analysis only *
128a4 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 46 /.#endif.. vfsF
128a5 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f lags |= SQLITE_
128a6 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c OPEN_READWRITE |
128a7 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 SQLITE_OPEN_CRE
128a8 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 ATE |.
128a9 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 SQLITE_OPEN_EX
128aa 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45 CLUSIVE | SQLITE
128ab 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
128ac 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 OSE;. rc = sqli
128ad 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 te3OsOpen(pPager
128ae 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 ->pVfs, 0, pFile
128af 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a , vfsFlags, 0);.
128b0 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
128b1 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 LITE_OK || isOpe
128b2 6e 28 70 46 69 6c 65 29 20 29 3b 0a 20 20 72 65 n(pFile) );. re
128b3 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
128b4 2a 20 53 65 74 20 74 68 65 20 62 75 73 79 20 68 * Set the busy h
128b5 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e andler function.
128b6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 .**.** The pager
128b7 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73 invokes the bus
128b8 79 2d 68 61 6e 64 6c 65 72 20 69 66 20 73 71 6c y-handler if sql
128b9 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74 ite3OsLock() ret
128ba 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f urns .** SQLITE_
128bb 42 55 53 59 20 77 68 65 6e 20 74 72 79 69 6e 67 BUSY when trying
128bc 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d to upgrade from
128bd 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48 no-lock to a SH
128be 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72 ARED lock,.** or
128bf 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 when trying to
128c0 75 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52 upgrade from a R
128c1 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 ESERVED lock to
128c2 61 6e 20 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a an EXCLUSIVE .**
128c3 20 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a lock. It does *
128c4 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 not* invoke the
128c5 62 75 73 79 20 68 61 6e 64 6c 65 72 20 77 68 65 busy handler whe
128c6 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d n upgrading from
128c7 0a 2a 2a 20 53 48 41 52 45 44 20 74 6f 20 52 45 .** SHARED to RE
128c8 53 45 52 56 45 44 2c 20 6f 72 20 77 68 65 6e 20 SERVED, or when
128c9 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 20 53 upgrading from S
128ca 48 41 52 45 44 20 74 6f 20 45 58 43 4c 55 53 49 HARED to EXCLUSI
128cb 56 45 0a 2a 2a 20 28 77 68 69 63 68 20 6f 63 63 VE.** (which occ
128cc 75 72 73 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a urs during hot-j
128cd 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 ournal rollback)
128ce 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a . Summary:.**.**
128cf 20 20 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 Transition
128d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
128d1 20 20 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 | Invokes x
128d2 42 75 73 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20 BusyHandler.**
128d3 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
128d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
128d5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
128d6 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e ---------.** N
128d7 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20 O_LOCK ->
128d8 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 SHARED_LOCK
128d9 20 7c 20 59 65 73 0a 2a 2a 20 20 20 53 48 41 52 | Yes.** SHAR
128da 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53 ED_LOCK -> RES
128db 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20 ERVED_LOCK |
128dc 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c No.** SHARED_L
128dd 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55 53 49 OCK -> EXCLUSI
128de 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a VE_LOCK | No.*
128df 2a 20 20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 * RESERVED_LOC
128e0 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c K -> EXCLUSIVE_L
128e1 4f 43 4b 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a OCK | Yes.**.*
128e2 2a 20 49 66 20 74 68 65 20 62 75 73 79 2d 68 61 * If the busy-ha
128e3 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 ndler callback r
128e4 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c eturns non-zero,
128e5 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a the lock is .**
128e6 20 72 65 74 72 69 65 64 2e 20 49 66 20 69 74 20 retried. If it
128e7 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 returns zero, th
128e8 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55 en the SQLITE_BU
128e9 53 59 20 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72 SY error is.** r
128ea 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 eturned to the c
128eb 61 6c 6c 65 72 20 6f 66 20 74 68 65 20 70 61 67 aller of the pag
128ec 65 72 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e er API function.
128ed 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
128ee 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
128ef 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c agerSetBusyhandl
128f0 65 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 er(. Pager *pPa
128f1 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 ger,
128f2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
128f3 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 ger object */.
128f4 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c int (*xBusyHandl
128f5 65 72 29 28 76 6f 69 64 20 2a 29 2c 20 20 20 20 er)(void *),
128f6 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
128f7 74 6f 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 to busy-handler
128f8 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f function */. vo
128f9 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 id *pBusyHandler
128fa 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 Arg
128fb 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
128fc 6f 20 70 61 73 73 20 74 6f 20 78 42 75 73 79 48 o pass to xBusyH
128fd 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 20 0a 20 andler */.){ .
128fe 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 pPager->xBusyHa
128ff 6e 64 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e ndler = xBusyHan
12900 64 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e dler;. pPager->
12901 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 pBusyHandlerArg
12902 3d 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 = pBusyHandlerAr
12903 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f g;.}../*.** Repo
12904 72 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 rt the current p
12905 61 67 65 20 73 69 7a 65 20 61 6e 64 20 6e 75 6d age size and num
12906 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20 ber of reserved
12907 62 79 74 65 73 20 62 61 63 6b 0a 2a 2a 20 74 6f bytes back.** to
12908 20 74 68 65 20 63 6f 64 65 63 2e 0a 2a 2f 0a 23 the codec..*/.#
12909 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 ifdef SQLITE_HAS
1290a 5f 43 4f 44 45 43 0a 73 74 61 74 69 63 20 76 6f _CODEC.static vo
1290b 69 64 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 id pagerReportSi
1290c 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
1290d 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d ){. if( pPager-
1290e 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 >xCodecSizeChng
1290f 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 78 ){. pPager->x
12910 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 28 70 50 CodecSizeChng(pP
12911 61 67 65 72 2d 3e 70 43 6f 64 65 63 2c 20 70 50 ager->pCodec, pP
12912 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a ager->pageSize,.
12913 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12914 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 (int)
12915 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 pPager->nReserve
12916 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 );. }.}.#else.#
12917 20 64 65 66 69 6e 65 20 70 61 67 65 72 52 65 70 define pagerRep
12918 6f 72 74 53 69 7a 65 28 58 29 20 20 20 20 20 2f ortSize(X) /
12919 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 65 20 64 6f * No-op if we do
1291a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 20 63 not support a c
1291b 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a odec */.#endif..
1291c 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
1291d 20 70 61 67 65 20 73 69 7a 65 20 75 73 65 64 20 page size used
1291e 62 79 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a by the Pager obj
1291f 65 63 74 2e 20 54 68 65 20 6e 65 77 20 70 61 67 ect. The new pag
12920 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 20 70 61 e size .** is pa
12921 73 73 65 64 20 69 6e 20 2a 70 50 61 67 65 53 69 ssed in *pPageSi
12922 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ze..**.** If the
12923 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 pager is in the
12924 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 error state whe
12925 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
12926 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a is called, it.**
12927 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 is a no-op. The
12928 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
12929 69 73 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 is the error sta
1292a 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 28 69 te error code (i
1292b 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 .e. .** one of S
1292c 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c QLITE_IOERR, SQL
1292d 49 54 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 53 ITE_CORRUPT or S
1292e 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a QLITE_FULL)..**.
1292f 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 ** Otherwise, if
12930 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c all of the foll
12931 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a owing are true:.
12932 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65 **.** * the ne
12933 77 20 70 61 67 65 20 73 69 7a 65 20 28 76 61 6c w page size (val
12934 75 65 20 6f 66 20 2a 70 50 61 67 65 53 69 7a 65 ue of *pPageSize
12935 29 20 69 73 20 76 61 6c 69 64 20 28 61 20 70 6f ) is valid (a po
12936 77 65 72 20 0a 2a 2a 20 20 20 20 20 6f 66 20 74 wer .** of t
12937 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 wo between 512 a
12938 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 nd SQLITE_MAX_PA
12939 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c 75 73 69 GE_SIZE, inclusi
1293a 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 ve), and.**.**
1293b 20 2a 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 * there are no
1293c 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
1293d 20 72 65 66 65 72 65 6e 63 65 73 2c 20 61 6e 64 references, and
1293e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 .**.** * the d
1293f 61 74 61 62 61 73 65 20 69 73 20 65 69 74 68 65 atabase is eithe
12940 72 20 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f r not an in-memo
12941 72 79 20 64 61 74 61 62 61 73 65 20 6f 72 20 69 ry database or i
12942 74 20 69 73 0a 2a 2a 20 20 20 20 20 61 6e 20 69 t is.** an i
12943 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
12944 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 e that currently
12945 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 7a 65 72 consists of zer
12946 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 o pages..**.** t
12947 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 6f 62 hen the pager ob
12948 6a 65 63 74 20 70 61 67 65 20 73 69 7a 65 20 69 ject page size i
12949 73 20 73 65 74 20 74 6f 20 2a 70 50 61 67 65 53 s set to *pPageS
1294a 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ize..**.** If th
1294b 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 63 e page size is c
1294c 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 69 hanged, then thi
1294d 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 20 s function uses
1294e 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 6c 6c sqlite3PagerMall
1294f 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 6f 62 74 61 oc() .** to obta
12950 69 6e 20 61 20 6e 65 77 20 50 61 67 65 72 2e 70 in a new Pager.p
12951 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 72 2e TmpSpace buffer.
12952 20 49 66 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 If this allocat
12953 69 6f 6e 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20 ion attempt .**
12954 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f fails, SQLITE_NO
12955 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 MEM is returned
12956 61 6e 64 20 74 68 65 20 70 61 67 65 20 73 69 7a and the page siz
12957 65 20 72 65 6d 61 69 6e 73 20 75 6e 63 68 61 6e e remains unchan
12958 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20 ged. .** In all
12959 6f 74 68 65 72 20 63 61 73 65 73 2c 20 53 51 4c other cases, SQL
1295a 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
1295b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
1295c 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f page size is no
1295d 74 20 63 68 61 6e 67 65 64 2c 20 65 69 74 68 65 t changed, eithe
1295e 72 20 62 65 63 61 75 73 65 20 6f 6e 65 20 6f 66 r because one of
1295f 20 74 68 65 20 65 6e 75 6d 65 72 61 74 65 64 0a the enumerated.
12960 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 ** conditions ab
12961 6f 76 65 20 69 73 20 6e 6f 74 20 74 72 75 65 2c ove is not true,
12962 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69 the pager was i
12963 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 n error state wh
12964 65 6e 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 en this.** funct
12965 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 ion was called,
12966 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20 6d or because the m
12967 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
12968 20 61 74 74 65 6d 70 74 20 66 61 69 6c 65 64 2c attempt failed,
12969 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 50 61 67 65 .** then *pPage
1296a 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 Size is set to t
1296b 68 65 20 6f 6c 64 2c 20 72 65 74 61 69 6e 65 64 he old, retained
1296c 20 70 61 67 65 20 73 69 7a 65 20 62 65 66 6f 72 page size befor
1296d 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a e returning..*/.
1296e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1296f 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
12970 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 etPagesize(Pager
12971 20 2a 70 50 61 67 65 72 2c 20 75 31 36 20 2a 70 *pPager, u16 *p
12972 50 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 PageSize, int nR
12973 65 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 eserve){. int r
12974 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 c = pPager->errC
12975 6f 64 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d ode;.. if( rc==
12976 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
12977 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 3d 20 u16 pageSize =
12978 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 *pPageSize;.
12979 61 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65 assert( pageSize
1297a 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 ==0 || (pageSize
1297b 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a >=512 && pageSiz
1297c 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 e<=SQLITE_MAX_PA
1297d 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 20 20 GE_SIZE) );.
1297e 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d if( (pPager->mem
1297f 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d Db==0 || pPager-
12980 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20 >dbSize==0).
12981 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 && sqlite3Pcach
12982 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 eRefCount(pPager
12983 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 ->pPCache)==0 .
12984 20 20 20 20 26 26 20 70 61 67 65 53 69 7a 65 20 && pageSize
12985 26 26 20 70 61 67 65 53 69 7a 65 21 3d 70 50 61 && pageSize!=pPa
12986 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20 ger->pageSize .
12987 20 20 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 ){. char
12988 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a *pNew = (char *
12989 29 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c )sqlite3PageMall
1298a 6f 63 28 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 oc(pageSize);.
1298b 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b if( !pNew ){
1298c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
1298d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
1298e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1298f 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 pager_reset(pPa
12990 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 70 50 ger);. pP
12991 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d ager->pageSize =
12992 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 pageSize;.
12993 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 sqlite3PageFr
12994 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 ee(pPager->pTmpS
12995 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20 20 70 pace);. p
12996 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
12997 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 = pNew;.
12998 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 sqlite3PcacheSe
12999 74 50 61 67 65 53 69 7a 65 28 70 50 61 67 65 72 tPageSize(pPager
1299a 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 53 ->pPCache, pageS
1299b 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ize);. }.
1299c 20 20 7d 0a 20 20 20 20 2a 70 50 61 67 65 53 69 }. *pPageSi
1299d 7a 65 20 3d 20 28 75 31 36 29 70 50 61 67 65 72 ze = (u16)pPager
1299e 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 ->pageSize;.
1299f 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29 if( nReserve<0 )
129a0 20 6e 52 65 73 65 72 76 65 20 3d 20 70 50 61 67 nReserve = pPag
129a1 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b 0a 20 20 er->nReserve;.
129a2 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 assert( nReser
129a3 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 ve>=0 && nReserv
129a4 65 3c 31 30 30 30 20 29 3b 0a 20 20 20 20 70 50 e<1000 );. pP
129a5 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 20 3d ager->nReserve =
129a6 20 28 69 31 36 29 6e 52 65 73 65 72 76 65 3b 0a (i16)nReserve;.
129a7 20 20 20 20 70 61 67 65 72 52 65 70 6f 72 74 53 pagerReportS
129a8 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ize(pPager);. }
129a9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
129aa 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
129ab 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22 pointer to the "
129ac 74 65 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20 temporary page"
129ad 62 75 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65 buffer held inte
129ae 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65 rnally.** by the
129af 20 70 61 67 65 72 2e 20 20 54 68 69 73 20 69 73 pager. This is
129b0 20 61 20 62 75 66 66 65 72 20 74 68 61 74 20 69 a buffer that i
129b1 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 s big enough to
129b2 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69 hold the.** enti
129b3 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 re content of a
129b4 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 database page.
129b5 54 68 69 73 20 62 75 66 66 65 72 20 69 73 20 75 This buffer is u
129b6 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a sed internally.*
129b7 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 * during rollbac
129b8 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76 k and will be ov
129b9 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e 65 76 erwritten whenev
129ba 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a er a rollback.**
129bb 20 6f 63 63 75 72 73 2e 20 20 42 75 74 20 6f 74 occurs. But ot
129bc 68 65 72 20 6d 6f 64 75 6c 65 73 20 61 72 65 20 her modules are
129bd 66 72 65 65 20 74 6f 20 75 73 65 20 69 74 20 74 free to use it t
129be 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a oo, as long as.*
129bf 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61 * no rollbacks a
129c0 72 65 20 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f re happening..*/
129c1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
129c2 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
129c3 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 erTempSpace(Page
129c4 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
129c5 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d turn pPager->pTm
129c6 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a pSpace;.}../*.**
129c7 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 Attempt to set
129c8 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 the maximum data
129c9 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 20 base page count
129ca 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 if mxPage is pos
129cb 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 itive. .** Make
129cc 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20 6d 78 no changes if mx
129cd 50 61 67 65 20 69 73 20 7a 65 72 6f 20 6f 72 20 Page is zero or
129ce 6e 65 67 61 74 69 76 65 2e 20 20 41 6e 64 20 6e negative. And n
129cf 65 76 65 72 20 72 65 64 75 63 65 20 74 68 65 0a ever reduce the.
129d0 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 ** maximum page
129d1 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 20 count below the
129d2 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 current size of
129d3 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a the database..**
129d4 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f .** Regardless o
129d5 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e f mxPage, return
129d6 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 the current max
129d7 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e imum page count.
129d8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
129d9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
129da 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 gerMaxPageCount(
129db 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
129dc 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66 nt mxPage){. if
129dd 28 20 6d 78 50 61 67 65 3e 30 20 29 7b 0a 20 20 ( mxPage>0 ){.
129de 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f pPager->mxPgno
129df 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20 = mxPage;. }.
129e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
129e1 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 ecount(pPager, 0
129e2 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 );. return pPag
129e3 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f er->mxPgno;.}../
129e4 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
129e5 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e ng set of routin
129e6 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 es are used to d
129e7 69 73 61 62 6c 65 20 74 68 65 20 73 69 6d 75 6c isable the simul
129e8 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f ated.** I/O erro
129e9 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20 54 68 r mechanism. Th
129ea 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
129eb 20 75 73 65 64 20 74 6f 20 61 76 6f 69 64 20 73 used to avoid s
129ec 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f imulated.** erro
129ed 72 73 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 rs in places whe
129ee 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 re we do not car
129ef 65 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a e about errors..
129f0 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53 **.** Unless -DS
129f1 51 4c 49 54 45 5f 54 45 53 54 3d 31 20 69 73 20 QLITE_TEST=1 is
129f2 75 73 65 64 2c 20 74 68 65 73 65 20 72 6f 75 74 used, these rout
129f3 69 6e 65 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d ines are all no-
129f4 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 ops.** and gener
129f5 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a ate no code..*/.
129f6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
129f7 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 ST.SQLITE_API ex
129f8 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 tern int sqlite3
129f9 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e _io_error_pendin
129fa 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 g;.SQLITE_API ex
129fb 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 tern int sqlite3
129fc 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73 _io_error_hit;.s
129fd 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f tatic int saved_
129fe 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c cnt;.void disabl
129ff 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
12a00 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 rrors(void){. s
12a01 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 aved_cnt = sqlit
12a02 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
12a03 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 ing;. sqlite3_i
12a04 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 o_error_pending
12a05 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 = -1;.}.void ena
12a06 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
12a07 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 _errors(void){.
12a08 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
12a09 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65 r_pending = save
12a0a 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 d_cnt;.}.#else.#
12a0b 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f define disable_
12a0c 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 simulated_io_err
12a0d 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 ors().# define e
12a0e 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
12a0f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 io_errors().#end
12a10 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 if../*.** Read t
12a11 68 65 20 66 69 72 73 74 20 4e 20 62 79 74 65 73 he first N bytes
12a12 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e from the beginn
12a13 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ing of the file
12a14 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 into memory.** t
12a15 68 61 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73 hat pDest points
12a16 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 to. .**.** If t
12a17 68 65 20 70 61 67 65 72 20 77 61 73 20 6f 70 65 he pager was ope
12a18 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e 73 69 65 ned on a transie
12a19 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61 nt file (zFilena
12a1a 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f me==""), or.** o
12a1b 70 65 6e 65 64 20 6f 6e 20 61 20 66 69 6c 65 20 pened on a file
12a1c 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 less than N byte
12a1d 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f s in size, the o
12a1e 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 0a utput buffer is.
12a1f 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 20 53 51 ** zeroed and SQ
12a20 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 LITE_OK returned
12a21 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 6c 65 20 . The rationale
12a22 66 6f 72 20 74 68 69 73 20 69 73 20 74 68 61 74 for this is that
12a23 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 this .** functi
12a24 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 on is used to re
12a25 61 64 20 64 61 74 61 62 61 73 65 20 68 65 61 64 ad database head
12a26 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 77 20 74 ers, and a new t
12a27 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a ransient or.** z
12a28 65 72 6f 20 73 69 7a 65 64 20 64 61 74 61 62 61 ero sized databa
12a29 73 65 20 68 61 73 20 61 20 68 65 61 64 65 72 20 se has a header
12a2a 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 20 65 6e than consists en
12a2b 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 6f 65 73 tirely of zeroes
12a2c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 49 ..**.** If any I
12a2d 4f 20 65 72 72 6f 72 20 61 70 61 72 74 20 66 72 O error apart fr
12a2e 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f om SQLITE_IOERR_
12a2f 53 48 4f 52 54 5f 52 45 41 44 20 69 73 20 65 6e SHORT_READ is en
12a30 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a 20 74 68 countered,.** th
12a31 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
12a32 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
12a33 63 61 6c 6c 65 72 20 61 6e 64 20 74 68 65 20 63 caller and the c
12a34 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a ontents of the.*
12a35 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 * output buffer
12a36 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 undefined..*/.SQ
12a37 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
12a38 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 sqlite3PagerRea
12a39 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 67 65 dFileheader(Page
12a3a 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 4e r *pPager, int N
12a3b 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 , unsigned char
12a3c 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 *pDest){. int r
12a3d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
12a3e 20 6d 65 6d 73 65 74 28 70 44 65 73 74 2c 20 30 memset(pDest, 0
12a3f 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 , N);. assert(
12a40 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
12a41 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 d) || pPager->te
12a42 6d 70 46 69 6c 65 20 29 3b 0a 20 20 69 66 28 20 mpFile );. if(
12a43 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
12a44 64 29 20 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 d) ){. IOTRAC
12a45 45 28 28 22 44 42 48 44 52 20 25 70 20 30 20 25 E(("DBHDR %p 0 %
12a46 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e 29 d\n", pPager, N)
12a47 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ). rc = sqlit
12a48 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
12a49 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30 >fd, pDest, N, 0
12a4a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
12a4b 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
12a4c 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 T_READ ){.
12a4d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
12a4e 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
12a4f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12a50 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c Return the total
12a51 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
12a52 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
12a53 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 file associated
12a54 20 0a 2a 2a 20 77 69 74 68 20 70 50 61 67 65 72 .** with pPager
12a55 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 . Normally, this
12a56 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 61 is calculated a
12a57 73 20 28 3c 64 62 20 66 69 6c 65 20 73 69 7a 65 s (<db file size
12a58 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 3e 29 2e 0a >/<page-size>)..
12a59 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 ** However, if t
12a5a 68 65 20 66 69 6c 65 20 69 73 20 62 65 74 77 65 he file is betwe
12a5b 65 6e 20 31 20 61 6e 64 20 3c 70 61 67 65 2d 73 en 1 and <page-s
12a5c 69 7a 65 3e 20 62 79 74 65 73 20 69 6e 20 73 69 ize> bytes in si
12a5d 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 68 69 ze, then .** thi
12a5e 73 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 s is considered
12a5f 61 20 31 20 70 61 67 65 20 66 69 6c 65 2e 0a 2a a 1 page file..*
12a60 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 *.** If the page
12a61 72 20 69 73 20 69 6e 20 65 72 72 6f 72 20 73 74 r is in error st
12a62 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 ate when this fu
12a63 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
12a64 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 72 , then the.** er
12a65 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 ror state error
12a66 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
12a67 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 6c 65 66 and *pnPage lef
12a68 74 20 75 6e 63 68 61 6e 67 65 64 2e 20 4f 72 2c t unchanged. Or,
12a69 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 6c 65 20 .** if the file
12a6a 73 79 73 74 65 6d 20 68 61 73 20 74 6f 20 62 65 system has to be
12a6b 20 71 75 65 72 69 65 64 20 66 6f 72 20 74 68 65 queried for the
12a6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c size of the fil
12a6d 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 71 75 65 e and.** the que
12a6e 72 79 20 61 74 74 65 6d 70 74 20 72 65 74 75 72 ry attempt retur
12a6f 6e 73 20 61 6e 20 49 4f 20 65 72 72 6f 72 2c 20 ns an IO error,
12a70 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
12a71 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a e is returned.**
12a72 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20 and *pnPage is
12a73 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a left unchanged..
12a74 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
12a75 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 if everything i
12a76 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 s successful, th
12a77 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 en SQLITE_OK is
12a78 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 returned.** and
12a79 2a 70 6e 50 61 67 65 20 69 73 20 73 65 74 20 74 *pnPage is set t
12a7a 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
12a7b 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 pages in the dat
12a7c 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 abase..*/.SQLITE
12a7d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
12a7e 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
12a7f 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nt(Pager *pPager
12a80 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a , int *pnPage){.
12a81 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 Pgno nPage;
12a82 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
12a83 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76 alue to return v
12a84 69 61 20 2a 70 6e 50 61 67 65 20 2a 2f 0a 0a 20 ia *pnPage */..
12a85 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 /* If the pager
12a86 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
12a87 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 he error state,
12a88 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
12a89 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 code. */. if(
12a8a 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
12a8b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 ){. return pP
12a8c 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 ager->errCode;.
12a8d 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 }.. /* Determi
12a8e 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ne the number of
12a8f 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 pages in the fi
12a90 6c 65 2e 20 53 74 6f 72 65 20 74 68 69 73 20 69 le. Store this i
12a91 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a 20 20 69 66 n nPage. */. if
12a92 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
12a93 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 6e 50 61 Valid ){. nPa
12a94 67 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 ge = pPager->dbS
12a95 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ize;. }else{.
12a96 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
12a97 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 /* Err
12a98 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f or returned by O
12a99 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 20 sFileSize() */.
12a9a 20 20 20 69 36 34 20 6e 20 3d 20 30 3b 20 20 20 i64 n = 0;
12a9b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
12a9c 6c 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 le size in bytes
12a9d 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 46 returned by OsF
12a9e 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 0a 20 20 ileSize() */..
12a9f 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e assert( isOpen
12aa0 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 (pPager->fd) ||
12aa1 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
12aa2 20 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 );. if( isOp
12aa3 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 26 en(pPager->fd) &
12aa4 26 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71 & (0 != (rc = sq
12aa5 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
12aa6 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29 pPager->fd, &n))
12aa7 29 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72 ) ){. pager
12aa8 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 _error(pPager, r
12aa9 63 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e c);. return
12aaa 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 rc;. }. i
12aab 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67 f( n>0 && n<pPag
12aac 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a er->pageSize ){.
12aad 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 31 3b nPage = 1;
12aae 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
12aaf 20 20 6e 50 61 67 65 20 3d 20 28 50 67 6e 6f 29 nPage = (Pgno)
12ab0 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e 70 61 67 (n / pPager->pag
12ab1 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 eSize);. }.
12ab2 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 if( pPager->st
12ab3 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 ate!=PAGER_UNLOC
12ab4 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 K ){. pPage
12ab5 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 r->dbSize = nPag
12ab6 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d e;. pPager-
12ab7 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 >dbFileSize = nP
12ab8 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 age;. pPage
12ab9 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d r->dbSizeValid =
12aba 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 1;. }. }..
12abb 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 /* If the curre
12abc 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 nt number of pag
12abd 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69 es in the file i
12abe 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 s greater than t
12abf 68 65 20 0a 20 20 2a 2a 20 63 6f 6e 66 69 67 75 he . ** configu
12ac0 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 red maximum page
12ac1 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72 65 61 r number, increa
12ac2 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64 20 6c se the allowed l
12ac3 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74 68 61 imit so. ** tha
12ac4 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e 20 62 t the file can b
12ac5 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 e read.. */. i
12ac6 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65 72 2d f( nPage>pPager-
12ac7 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 70 >mxPgno ){. p
12ac8 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 Pager->mxPgno =
12ac9 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d (Pgno)nPage;. }
12aca 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f .. /* Set the o
12acb 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 61 utput variable a
12acc 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 nd return SQLITE
12acd 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 20 70 6e 50 _OK */. if( pnP
12ace 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 6e 50 61 age ){. *pnPa
12acf 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a ge = nPage;. }.
12ad0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
12ad1 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 OK;.}.../*.** Tr
12ad2 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f y to obtain a lo
12ad3 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f 63 6b 74 ck of type lockt
12ad4 79 70 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ype on the datab
12ad5 61 73 65 20 66 69 6c 65 2e 20 49 66 0a 2a 2a 20 ase file. If.**
12ad6 61 20 73 69 6d 69 6c 61 72 20 6f 72 20 67 72 65 a similar or gre
12ad7 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 61 6c 72 ater lock is alr
12ad8 65 61 64 79 20 68 65 6c 64 2c 20 74 68 69 73 20 eady held, this
12ad9 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
12ada 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e -op.** (returnin
12adb 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65 g SQLITE_OK imme
12adc 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 diately)..**.**
12add 4f 74 68 65 72 77 69 73 65 2c 20 61 74 74 65 6d Otherwise, attem
12ade 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 pt to obtain the
12adf 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73 71 6c 69 lock using sqli
12ae0 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 76 te3OsLock(). Inv
12ae1 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62 75 73 79 oke .** the busy
12ae2 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 callback if the
12ae3 20 6c 6f 63 6b 20 69 73 20 63 75 72 72 65 6e 74 lock is current
12ae4 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 ly not available
12ae5 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20 75 6e 74 . Repeat .** unt
12ae6 69 6c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c il the busy call
12ae7 62 61 63 6b 20 72 65 74 75 72 6e 73 20 66 61 6c back returns fal
12ae8 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 se or until the
12ae9 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f attempt to .** o
12aea 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 73 btain the lock s
12aeb 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 ucceeds..**.** R
12aec 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
12aed 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 on success and a
12aee 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 n error code if
12aef 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e we cannot obtain
12af0 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66 .** the lock. If
12af1 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f 62 74 the lock is obt
12af2 61 69 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c ained successful
12af3 6c 79 2c 20 73 65 74 20 74 68 65 20 50 61 67 65 ly, set the Page
12af4 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76 61 72 69 r.state .** vari
12af5 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 able to locktype
12af6 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
12af7 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
12af8 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c pager_wait_on_l
12af9 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 ock(Pager *pPage
12afa 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 r, int locktype)
12afb 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
12afc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12afd 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
12afe 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a rn code */.. /*
12aff 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76 61 6c The OS lock val
12b00 75 65 73 20 6d 75 73 74 20 62 65 20 74 68 65 20 ues must be the
12b01 73 61 6d 65 20 61 73 20 74 68 65 20 50 61 67 65 same as the Page
12b02 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 2a 2f r lock values */
12b03 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 . assert( PAGER
12b04 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 45 44 5f _SHARED==SHARED_
12b05 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
12b06 28 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 ( PAGER_RESERVED
12b07 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 ==RESERVED_LOCK
12b08 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 );. assert( PAG
12b09 45 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d 45 58 ER_EXCLUSIVE==EX
12b0a 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a CLUSIVE_LOCK );.
12b0b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c . /* If the fil
12b0c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75 e is currently u
12b0d 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74 68 65 nlocked then the
12b0e 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 75 6e size must be un
12b0f 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73 65 72 known */. asser
12b10 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
12b11 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c >=PAGER_SHARED |
12b12 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 | pPager->dbSize
12b13 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 0a 20 20 2f Valid==0 );.. /
12b14 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69 * Check that thi
12b15 73 20 69 73 20 65 69 74 68 65 72 20 61 20 6e 6f s is either a no
12b16 2d 6f 70 20 28 62 65 63 61 75 73 65 20 74 68 65 -op (because the
12b17 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 requested lock
12b18 69 73 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 is . ** already
12b19 20 68 65 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f 66 held, or one of
12b1a 20 74 68 65 20 74 72 61 6e 73 69 73 74 69 6f 6e the transistion
12b1b 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79 2d s that the busy-
12b1c 68 61 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d 61 79 handler. ** may
12b1d 20 62 65 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 be invoked duri
12b1e 6e 67 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f ng, according to
12b1f 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f the comment abo
12b20 76 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 ve. ** sqlite3P
12b21 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c agerSetBusyhandl
12b22 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 er().. */. ass
12b23 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73 74 ert( (pPager->st
12b24 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a 20 ate>=locktype).
12b25 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 || (pPager
12b26 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 ->state==PAGER_U
12b27 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74 79 70 NLOCK && locktyp
12b28 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 29 e==PAGER_SHARED)
12b29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 . || (pPag
12b2a 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
12b2b 5f 52 45 53 45 52 56 45 44 20 26 26 20 6c 6f 63 _RESERVED && loc
12b2c 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 45 58 43 ktype==PAGER_EXC
12b2d 4c 55 53 49 56 45 29 0a 20 20 29 3b 0a 0a 20 20 LUSIVE). );..
12b2e 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 if( pPager->stat
12b2f 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 e>=locktype ){.
12b30 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
12b31 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
12b32 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 do {. rc =
12b33 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 sqlite3OsLock(pP
12b34 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74 79 ager->fd, lockty
12b35 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 pe);. }while(
12b36 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
12b37 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75 73 && pPager->xBus
12b38 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72 2d yHandler(pPager-
12b39 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 >pBusyHandlerArg
12b3a 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ) );. if( rc=
12b3b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12b3c 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
12b3d 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 e = (u8)locktype
12b3e 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 ;. IOTRACE(
12b3f 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c ("LOCK %p %d\n",
12b40 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70 pPager, locktyp
12b41 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e)). }. }.
12b42 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
12b43 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 61 73 73 .** Function ass
12b44 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 ertTruncateConst
12b45 72 61 69 6e 74 28 70 50 61 67 65 72 29 20 63 68 raint(pPager) ch
12b46 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 20 6f 66 ecks that one of
12b47 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 the .** followi
12b48 6e 67 20 69 73 20 74 72 75 65 20 66 6f 72 20 61 ng is true for a
12b49 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63 ll dirty pages c
12b4a 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 urrently in the
12b4b 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a 2a 0a 2a page-cache:.**.*
12b4c 2a 20 20 20 61 29 20 54 68 65 20 70 61 67 65 20 * a) The page
12b4d 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74 number is less t
12b4e 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
12b4f 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
12b50 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 65 6e 74 .** current
12b51 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c database image,
12b52 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 0a 2a 2a in pages, OR.**
12b53 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68 65 20 .** b) if the
12b54 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 65 72 page content wer
12b55 65 20 77 72 69 74 74 65 6e 20 61 74 20 74 68 69 e written at thi
12b56 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75 6c 64 s time, it would
12b57 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 62 65 20 not.** be
12b58 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 necessary to wri
12b59 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 te the current c
12b5a 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20 74 68 ontent out to th
12b5b 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a e sub-journal.**
12b5c 20 20 20 20 20 20 28 61 73 20 64 65 74 65 72 6d (as determ
12b5d 69 6e 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e ined by function
12b5e 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
12b5f 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 e())..**.** If t
12b60 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 61 73 73 he condition ass
12b61 65 72 74 65 64 20 62 79 20 74 68 69 73 20 66 75 erted by this fu
12b62 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e 6f 74 20 nction were not
12b63 74 72 75 65 2c 20 61 6e 64 20 74 68 65 0a 2a 2a true, and the.**
12b64 20 64 69 72 74 79 20 70 61 67 65 20 77 65 72 65 dirty page were
12b65 20 74 6f 20 62 65 20 64 69 73 63 61 72 64 65 64 to be discarded
12b66 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 from the cache
12b67 76 69 61 20 74 68 65 20 70 61 67 65 72 53 74 72 via the pagerStr
12b68 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 ess().** routine
12b69 2c 20 70 61 67 65 72 53 74 72 65 73 73 28 29 20 , pagerStress()
12b6a 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 74 65 20 would not write
12b6b 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 the current page
12b6c 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 74 content to.** t
12b6d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12b6e 2e 20 49 66 20 61 20 73 61 76 65 70 6f 69 6e 74 . If a savepoint
12b6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 65 72 transaction wer
12b70 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 e rolled back af
12b71 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 61 70 70 ter.** this happ
12b72 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 72 65 63 ened, the correc
12b73 74 20 62 65 68 61 76 69 6f 75 72 20 77 6f 75 6c t behaviour woul
12b74 64 20 62 65 20 74 6f 20 72 65 73 74 6f 72 65 20 d be to restore
12b75 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 the current.** c
12b76 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 ontent of the pa
12b77 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 73 69 6e ge. However, sin
12b78 63 65 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 20 ce this content
12b79 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 is not present i
12b7a 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 68 65 20 n either.** the
12b7b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 72 database file or
12b7c 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 6f 66 20 the portion of
12b7d 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
12b7e 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 73 75 62 rnal and .** sub
12b7f 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 65 64 20 -journal rolled
12b80 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 65 6e 74 back the content
12b81 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 could not be re
12b82 73 74 6f 72 65 64 20 61 6e 64 20 74 68 65 0a 2a stored and the.*
12b83 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 * database image
12b84 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 20 63 6f would become co
12b85 72 72 75 70 74 2e 20 49 74 20 69 73 20 74 68 65 rrupt. It is the
12b86 72 65 66 6f 72 65 20 66 6f 72 74 75 6e 61 74 65 refore fortunate
12b87 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 73 20 63 that .** this c
12b88 69 72 63 75 6d 73 74 61 6e 63 65 20 63 61 6e 6e ircumstance cann
12b89 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a 23 69 66 ot arise..*/.#if
12b8a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
12b8b 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 76 6f DEBUG).static vo
12b8c 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 id assertTruncat
12b8d 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28 50 67 eConstraintCb(Pg
12b8e 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 Hdr *pPg){. ass
12b8f 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 ert( pPg->flags&
12b90 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 PGHDR_DIRTY );.
12b91 20 61 73 73 65 72 74 28 20 21 73 75 62 6a 52 65 assert( !subjRe
12b92 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20 quiresPage(pPg)
12b93 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 || pPg->pgno<=pP
12b94 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a g->pPager->dbSiz
12b95 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f e );.}.static vo
12b96 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 id assertTruncat
12b97 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 67 65 eConstraint(Page
12b98 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 71 r *pPager){. sq
12b99 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61 lite3PcacheItera
12b9a 74 65 44 69 72 74 79 28 70 50 61 67 65 72 2d 3e teDirty(pPager->
12b9b 70 50 43 61 63 68 65 2c 20 61 73 73 65 72 74 54 pPCache, assertT
12b9c 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e runcateConstrain
12b9d 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 tCb);.}.#else.#
12b9e 64 65 66 69 6e 65 20 61 73 73 65 72 74 54 72 75 define assertTru
12b9f 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 ncateConstraint(
12ba0 70 50 61 67 65 72 29 0a 23 65 6e 64 69 66 0a 0a pPager).#endif..
12ba1 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 /*.** Truncate t
12ba2 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 he in-memory dat
12ba3 61 62 61 73 65 20 66 69 6c 65 20 69 6d 61 67 65 abase file image
12ba4 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 73 2e to nPage pages.
12ba5 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 This .** functi
12ba6 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 on does not actu
12ba7 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 ally modify the
12ba8 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e database file on
12ba9 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75 disk. It .** ju
12baa 73 74 20 73 65 74 73 20 74 68 65 20 69 6e 74 65 st sets the inte
12bab 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68 rnal state of th
12bac 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 73 e pager object s
12bad 6f 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 74 o that the .** t
12bae 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62 runcation will b
12baf 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68 65 20 e done when the
12bb0 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
12bb1 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
12bb2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
12bb3 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
12bb4 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 PagerTruncateIma
12bb5 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ge(Pager *pPager
12bb6 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 , Pgno nPage){.
12bb7 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
12bb8 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a >dbSizeValid );.
12bb9 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12bba 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67 65 20 ->dbSize>=nPage
12bbb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
12bbc 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
12bbd 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 R_RESERVED );.
12bbe 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
12bbf 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 nPage;. assert
12bc0 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 TruncateConstrai
12bc1 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f nt(pPager);.}../
12bc2 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 *.** Shutdown th
12bc3 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 46 e page cache. F
12bc4 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 ree all memory a
12bc5 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c nd close all fil
12bc6 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 es..**.** If a t
12bc7 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69 ransaction was i
12bc8 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20 n progress when
12bc9 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
12bca 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 called, that.**
12bcb 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 transaction is r
12bcc 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c olled back. All
12bcd 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 outstanding pag
12bce 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 es are invalidat
12bcf 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 ed.** and their
12bd0 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e memory is freed.
12bd1 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f Any attempt to
12bd2 20 75 73 65 20 61 20 70 61 67 65 20 61 73 73 6f use a page asso
12bd3 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 ciated.** with t
12bd4 68 69 73 20 70 61 67 65 20 63 61 63 68 65 20 61 his page cache a
12bd5 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 fter this functi
12bd6 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 on returns will
12bd7 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 likely.** result
12bd8 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a in a coredump..
12bd9 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
12bda 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65 ion always succe
12bdb 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61 eds. If a transa
12bdc 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 ction is active
12bdd 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 an attempt.** is
12bde 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 made to roll it
12bdf 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 back. If an err
12be0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
12be1 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a the rollback .*
12be2 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 * a hot journal
12be3 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74 may be left in t
12be4 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75 he filesystem bu
12be5 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65 t no error is re
12be6 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 turned.** to the
12be7 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 caller..*/.SQLI
12be8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
12be9 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 qlite3PagerClose
12bea 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b (Pager *pPager){
12beb 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c . disable_simul
12bec 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 ated_io_errors()
12bed 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e ;. sqlite3Begin
12bee 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
12bef 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 pPager->errCod
12bf0 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d e = 0;. pPager-
12bf1 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d >exclusiveMode =
12bf2 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65 0;. pager_rese
12bf3 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 t(pPager);. if(
12bf4 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 61 MEMDB ){. pa
12bf5 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 ger_unlock(pPage
12bf6 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 r);. }else{.
12bf7 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e 6a 6f /* Set Pager.jo
12bf8 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 20 66 urnalHdr to -1 f
12bf9 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74 20 6f or the benefit o
12bfa 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 79 f the pager_play
12bfb 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a 20 63 back() . ** c
12bfc 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 62 65 all which may be
12bfd 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 68 69 made from withi
12bfe 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 n pagerUnlockAnd
12bff 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 20 69 Rollback(). If i
12c00 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 t. ** is not
12c01 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75 6e 73 -1, then the uns
12c02 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 ynced portion of
12c03 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c an open journal
12c04 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 2a 2a file may. **
12c05 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 be played back
12c06 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 into the databas
12c07 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 e. If a power fa
12c08 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 ilure occurs whi
12c09 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 le. ** this i
12c0a 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 68 65 s happening, the
12c0b 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65 database may be
12c0c 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 come corrupt..
12c0d 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d */. pPager-
12c0e 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 2d 31 >journalHdr = -1
12c0f 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 ;. pagerUnloc
12c10 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 kAndRollback(pPa
12c11 67 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 ger);. }. sqli
12c12 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
12c13 6f 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73 oc();. enable_s
12c14 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
12c15 72 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52 41 rs();. PAGERTRA
12c16 43 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 CE(("CLOSE %d\n"
12c17 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 , PAGERID(pPager
12c18 29 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 )));. IOTRACE((
12c19 22 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50 "CLOSE %p\n", pP
12c1a 61 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65 33 ager)). sqlite3
12c1b 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e OsClose(pPager->
12c1c 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 fd);. sqlite3Pa
12c1d 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 geFree(pPager->p
12c1e 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c TmpSpace);. sql
12c1f 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 ite3PcacheClose(
12c20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
12c21 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
12c22 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 _HAS_CODEC. if(
12c23 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 pPager->xCodecF
12c24 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 43 ree ) pPager->xC
12c25 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72 2d odecFree(pPager-
12c26 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64 69 66 >pCodec);.#endif
12c27 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 .. assert( !pPa
12c28 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 ger->aSavepoint
12c29 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a && !pPager->pInJ
12c2a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 ournal );. asse
12c2b 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 rt( !isOpen(pPag
12c2c 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 4f er->jfd) && !isO
12c2d 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 pen(pPager->sjfd
12c2e 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f ) );.. sqlite3_
12c2f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 free(pPager);.
12c30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12c31 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 ;.}..#if !define
12c32 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 d(NDEBUG) || def
12c33 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
12c34 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 )./*.** Return t
12c35 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 he page number f
12c36 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a or page pPg..*/.
12c37 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 SQLITE_PRIVATE P
12c38 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 gno sqlite3Pager
12c39 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67 Pagenumber(DbPag
12c3a 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72 e *pPg){. retur
12c3b 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 n pPg->pgno;.}.#
12c3c 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 endif../*.** Inc
12c3d 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 rement the refer
12c3e 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 ence count for p
12c3f 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49 age pPg..*/.SQLI
12c40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
12c41 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 sqlite3PagerRef(
12c42 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 DbPage *pPg){.
12c43 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
12c44 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 (pPg);.}../*.**
12c45 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c Sync the journal
12c46 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 . In other words
12c47 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 , make sure all
12c48 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68 the pages that h
12c49 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 ave.** been writ
12c4a 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ten to the journ
12c4b 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79 al have actually
12c4c 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72 reached the sur
12c4d 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 face of the.** d
12c4e 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 20 72 isk and can be r
12c4f 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 65 estored in the e
12c50 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d 6a 6f vent of a hot-jo
12c51 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a urnal rollback..
12c52 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 61 67 **.** If the Pag
12c53 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 er.needSync flag
12c54 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 68 65 is not set, the
12c55 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
12c56 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f is a.** no-op. O
12c57 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 63 therwise, the ac
12c58 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 64 tions required d
12c59 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75 epend on the jou
12c5a 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64 rnal-mode.** and
12c5b 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61 72 the device char
12c5c 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 acteristics of t
12c5d 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 he the file-syst
12c5e 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a em, as follows:.
12c5f 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 **.** * If the
12c60 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
12c61 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f an in-memory jo
12c62 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61 urnal file, no a
12c63 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20 ction need.**
12c64 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a be taken..**.*
12c65 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65 2c * * Otherwise,
12c66 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20 64 if the device d
12c67 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 oes not support
12c68 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44 20 the SAFE_APPEND
12c69 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20 property,.**
12c6a 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66 then the nRec f
12c6b 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 ield of the most
12c6c 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74 65 recently writte
12c6d 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 n journal header
12c6e 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61 74 .** is updat
12c6f 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 ed to contain th
12c70 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72 e number of jour
12c71 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61 74 nal records that
12c72 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 65 have.** bee
12c73 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77 n written follow
12c74 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20 70 ing it. If the p
12c75 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69 6e ager is operatin
12c76 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a g in full-sync.*
12c77 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e * mode, then
12c78 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12c79 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66 6f e is synced befo
12c7a 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69 73 re this field is
12c7b 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 updated..**.**
12c7c 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69 63 * If the devic
12c7d 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f e does not suppo
12c7e 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49 41 rt the SEQUENTIA
12c7f 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65 6e L property, then
12c80 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c .** journal
12c81 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e file is synced.
12c82 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73 .**.** Or, in ps
12c83 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a eudo-code:.**.**
12c84 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d if( NOT <in-m
12c85 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29 emory journal> )
12c86 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 {.** if( NOT
12c87 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a SAFE_APPEND ){.
12c88 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66 75 ** if( <fu
12c89 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20 ll-sync mode> )
12c8a 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 xSync(<journal f
12c8b 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20 ile>);.**
12c8c 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69 65 <update nRec fie
12c8d 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a ld>.** } .**
12c8e 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45 51 if( NOT SEQ
12c8f 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 28 UENTIAL ) xSync(
12c90 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b <journal file>);
12c91 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 .** }.**.** Th
12c92 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 e Pager.needSync
12c93 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 20 62 flag is never b
12c94 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 6f 72 e set for tempor
12c95 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 61 6e ary files, or an
12c96 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 61 74 y.** file operat
12c97 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d ing in no-sync m
12c98 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 79 6e ode (Pager.noSyn
12c99 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 c set to non-zer
12c9a 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 o)..**.** If suc
12c9b 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 72 6f cessful, this ro
12c9c 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65 utine clears the
12c9d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
12c9e 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 20 0a flag of every .
12c9f 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c ** page currentl
12ca0 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 y held in memory
12ca1 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
12ca2 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 g SQLITE_OK. If
12ca3 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 69 an IO.** error i
12ca4 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 s encountered, t
12ca5 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 hen the IO error
12ca6 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
12ca7 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e d to the caller.
12ca8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
12ca9 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 yncJournal(Pager
12caa 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 *pPager){. if(
12cab 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
12cac 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
12cad 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 !pPager->tempFi
12cae 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 le );. if( pP
12caf 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
12cb0 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e!=PAGER_JOURNAL
12cb1 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 MODE_MEMORY ){.
12cb2 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 int rc;
12cb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12cb4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
12cb5 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20 urn code */.
12cb6 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 63 20 const int iDc
12cb7 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 = sqlite3OsDevic
12cb8 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
12cb9 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 (pPager->fd);.
12cba 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 assert( isOp
12cbb 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
12cbc 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d );.. if( 0=
12cbd 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 =(iDc&SQLITE_IOC
12cbe 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20 AP_SAFE_APPEND)
12cbf 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
12cc0 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77 is block deals w
12cc1 69 74 68 20 61 6e 20 6f 62 73 63 75 72 65 20 70 ith an obscure p
12cc2 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 65 20 6c roblem. If the l
12cc3 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 ast connection.
12cc4 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77 ** that w
12cc5 72 6f 74 65 20 74 6f 20 74 68 69 73 20 64 61 74 rote to this dat
12cc6 61 62 61 73 65 20 77 61 73 20 6f 70 65 72 61 74 abase was operat
12cc7 69 6e 67 20 69 6e 20 70 65 72 73 69 73 74 65 6e ing in persisten
12cc8 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 t-journal.
12cc9 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 ** mode, then
12cca 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12ccb 20 6d 61 79 20 61 74 20 74 68 69 73 20 70 6f 69 may at this poi
12ccc 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 6c nt actually be l
12ccd 61 72 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a arger. **
12cce 20 74 68 61 6e 20 50 61 67 65 72 2e 6a 6f 75 72 than Pager.jour
12ccf 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e 20 49 66 nalOff bytes. If
12cd0 20 74 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20 the next thing
12cd1 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 in the journal.
12cd2 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 68 ** file h
12cd3 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61 20 6a appens to be a j
12cd4 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 28 77 ournal-header (w
12cd5 72 69 74 74 65 6e 20 61 73 20 70 61 72 74 20 6f ritten as part o
12cd6 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a f the. **
12cd7 20 70 72 65 76 69 6f 75 73 20 63 6f 6e 6e 65 63 previous connec
12cd8 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f tions transactio
12cd9 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 73 68 20 n), and a crash
12cda 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c 75 72 65 or power-failure
12cdb 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 63 63 . ** occ
12cdc 75 72 73 20 61 66 74 65 72 20 6e 52 65 63 20 69 urs after nRec i
12cdd 73 20 75 70 64 61 74 65 64 20 62 75 74 20 62 65 s updated but be
12cde 66 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 fore this connec
12cdf 74 69 6f 6e 20 77 72 69 74 65 73 20 0a 20 20 20 tion writes .
12ce0 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67 ** anything
12ce1 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a 6f 75 else to the jou
12ce2 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 63 6f rnal file (or co
12ce3 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61 63 6b mmits/rolls back
12ce4 20 69 74 73 20 0a 20 20 20 20 20 20 20 20 2a 2a its . **
12ce5 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 74 transaction), t
12ce6 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79 20 62 hen SQLite may b
12ce7 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64 20 77 ecome confused w
12ce8 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20 0a 20 hen doing the .
12ce9 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f ** hot-jo
12cea 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 66 urnal rollback f
12ceb 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76 65 72 ollowing recover
12cec 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c 20 62 y. It may roll b
12ced 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 ack all.
12cee 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6e 6e 65 ** of this conne
12cef 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 74 68 65 ctions data, the
12cf0 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 72 6f 6c n proceed to rol
12cf1 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 20 6f 6c ling back the ol
12cf2 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 75 d,. ** ou
12cf3 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 61 20 74 t-of-date data t
12cf4 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 74 2e 20 hat follows it.
12cf5 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 Database corrupt
12cf6 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a ion.. **.
12cf7 20 20 20 20 20 20 20 20 2a 2a 20 54 6f 20 77 6f ** To wo
12cf8 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 2c 20 rk around this,
12cf9 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 if the journal f
12cfa 69 6c 65 20 64 6f 65 73 20 61 70 70 65 61 72 20 ile does appear
12cfb 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 20 to contain.
12cfc 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 68 65 ** a valid he
12cfd 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 50 ader following P
12cfe 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 2c ager.journalOff,
12cff 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 30 78 then write a 0x
12d00 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79 00. ** by
12d01 74 65 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 te to the start
12d02 6f 66 20 69 74 20 74 6f 20 70 72 65 76 65 6e 74 of it to prevent
12d03 20 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 72 it from being r
12d04 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 20 20 20 ecognized..
12d05 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
12d06 20 56 61 72 69 61 62 6c 65 20 69 4e 65 78 74 48 Variable iNextH
12d07 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 drOffset is set
12d08 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 61 74 to the offset at
12d09 20 77 68 69 63 68 20 74 68 69 73 0a 20 20 20 20 which this.
12d0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 61 74 ** problemat
12d0b 69 63 20 68 65 61 64 65 72 20 77 69 6c 6c 20 6f ic header will o
12d0c 63 63 75 72 2c 20 69 66 20 69 74 20 65 78 69 73 ccur, if it exis
12d0d 74 73 2e 20 61 4d 61 67 69 63 20 69 73 20 75 73 ts. aMagic is us
12d0e 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 ed . ** a
12d0f 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 62 75 s a temporary bu
12d10 66 66 65 72 20 74 6f 20 69 6e 73 70 65 63 74 20 ffer to inspect
12d11 74 68 65 20 66 69 72 73 74 20 63 6f 75 70 6c 65 the first couple
12d12 20 6f 66 20 62 79 74 65 73 20 6f 66 0a 20 20 20 of bytes of.
12d13 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 74 65 ** the pote
12d14 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c 20 68 65 ntial journal he
12d15 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f ader.. */
12d16 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65 . i64 iNe
12d17 78 74 48 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 xtHdrOffset;.
12d18 20 20 20 20 20 75 38 20 61 4d 61 67 69 63 5b 38 u8 aMagic[8
12d19 5d 3b 0a 09 75 38 20 7a 48 65 61 64 65 72 5b 73 ];..u8 zHeader[s
12d1a 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
12d1b 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d 65 6d 63 70 gic)+4];...memcp
12d1c 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 y(zHeader, aJour
12d1d 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 nalMagic, sizeof
12d1e 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 (aJournalMagic))
12d1f 3b 0a 09 70 75 74 33 32 62 69 74 73 28 26 7a 48 ;..put32bits(&zH
12d20 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
12d21 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 70 50 urnalMagic)], pP
12d22 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20 ager->nRec);..
12d23 20 20 20 20 20 20 69 4e 65 78 74 48 64 72 4f 66 iNextHdrOf
12d24 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 fset = journalHd
12d25 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b rOffset(pPager);
12d26 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
12d27 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 lite3OsRead(pPag
12d28 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c er->jfd, aMagic,
12d29 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 73 8, iNextHdrOffs
12d2a 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 et);. if(
12d2b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
12d2c 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67 & 0==memcmp(aMag
12d2d 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 ic, aJournalMagi
12d2e 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20 20 c, 8) ){.
12d2f 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
12d30 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b u8 zerobyte = 0;
12d31 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
12d32 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
12d33 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72 Pager->jfd, &zer
12d34 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 48 obyte, 1, iNextH
12d35 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 drOffset);.
12d36 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
12d37 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc!=SQLITE_OK &
12d38 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 & rc!=SQLITE_IOE
12d39 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b RR_SHORT_READ ){
12d3a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
12d3b 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a n rc;. }.
12d3c 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 . /* Writ
12d3d 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 e the nRec value
12d3e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 into the journa
12d3f 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49 l file header. I
12d40 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 f in. **
12d41 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 full-synchronous
12d42 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 mode, sync the
12d43 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 journal first. T
12d44 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 his ensures that
12d45 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 . ** all
12d46 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20 data has really
12d47 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65 66 hit the disk bef
12d48 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64 61 ore nRec is upda
12d49 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 ted to mark.
12d4a 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63 ** it as a c
12d4b 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c andidate for rol
12d4c 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a lback.. *
12d4d 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 *. ** Thi
12d4e 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 s is not require
12d4f 64 20 69 66 20 74 68 65 20 70 65 72 73 69 73 74 d if the persist
12d50 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72 ent media suppor
12d51 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a ts the. *
12d52 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 * SAFE_APPEND pr
12d53 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 65 20 operty. Because
12d54 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 20 in this case it
12d55 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
12d56 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 . ** for
12d57 67 61 72 62 61 67 65 20 64 61 74 61 20 74 6f 20 garbage data to
12d58 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 be appended to t
12d59 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 he file, the nRe
12d5a 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20 c field.
12d5b 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 ** is populated
12d5c 77 69 74 68 20 30 78 46 46 46 46 46 46 46 46 20 with 0xFFFFFFFF
12d5d 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c when the journal
12d5e 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74 header is writt
12d5f 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e en. ** an
12d60 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f d never needs to
12d61 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20 be updated..
12d62 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
12d63 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c if( pPager->full
12d64 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26 Sync && 0==(iDc&
12d65 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 SQLITE_IOCAP_SEQ
12d66 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 UENTIAL) ){.
12d67 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
12d68 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 (("SYNC journal
12d69 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 of %d\n", PAGERI
12d6a 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
12d6b 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 IOTRACE((
12d6c 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 "JSYNC %p\n", pP
12d6d 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20 ager)).
12d6e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 rc = sqlite3OsS
12d6f 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ync(pPager->jfd,
12d70 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
12d71 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ags);.
12d72 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12d73 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
12d74 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12d75 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 IOTRACE(("JHDR
12d76 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 70 50 61 67 %p %lld\n", pPag
12d77 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 er, pPager->jour
12d78 6e 61 6c 48 64 72 29 29 3b 0a 20 20 20 20 20 20 nalHdr));.
12d79 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
12d7a 57 72 69 74 65 28 0a 20 20 20 20 20 20 20 20 20 Write(.
12d7b 20 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 pPager->jfd,
12d7c 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 zHeader, sizeof(
12d7d 7a 48 65 61 64 65 72 29 2c 20 70 50 61 67 65 72 zHeader), pPager
12d7e 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 0a 09 29 3b ->journalHdr..);
12d7f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
12d80 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
12d81 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
12d82 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44 if( 0==(iD
12d83 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 c&SQLITE_IOCAP_S
12d84 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 EQUENTIAL) ){.
12d85 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 PAGERTRACE
12d86 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 (("SYNC journal
12d87 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 of %d\n", PAGERI
12d88 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
12d89 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a IOTRACE(("J
12d8a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 SYNC %p\n", pPag
12d8b 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20 er)). rc
12d8c 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 = sqlite3OsSync(
12d8d 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 pPager->jfd, pPa
12d8e 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c ger->sync_flags|
12d8f 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61 . (pPa
12d90 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d ger->sync_flags=
12d91 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c =SQLITE_SYNC_FUL
12d92 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 L?SQLITE_SYNC_DA
12d93 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 TAONLY:0).
12d94 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 );. if(
12d95 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
12d96 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
12d97 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
12d98 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
12d99 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 63 le was just succ
12d9a 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e essfully synced.
12d9b 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 53 Set Pager.needS
12d9c 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a ync . ** to z
12d9d 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 68 ero and clear th
12d9e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e e PGHDR_NEED_SYN
12d9f 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61 C flag on all pa
12da0 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 gess.. */.
12da1 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
12da2 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 c = 0;. pPage
12da3 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 r->journalStarte
12da4 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 d = 1;. sqlit
12da5 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e e3PcacheClearSyn
12da6 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e 70 cFlags(pPager->p
12da7 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 PCache);. }..
12da8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12da9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 ;.}../*.** The a
12daa 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 66 rgument is the f
12dab 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 irst in a linked
12dac 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 list of dirty p
12dad 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a ages connected.*
12dae 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e 70 * by the PgHdr.p
12daf 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20 54 Dirty pointer. T
12db0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 his function wri
12db1 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66 20 tes each one of
12db2 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 the.** in-memory
12db3 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69 pages in the li
12db4 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 st to the databa
12db5 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72 67 se file. The arg
12db6 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20 ument may.** be
12db7 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74 69 NULL, representi
12db8 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73 74 ng an empty list
12db9 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
12dba 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a his function is.
12dbb 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a ** a no-op..**.*
12dbc 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 74 * The pager must
12dbd 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 61 hold at least a
12dbe 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 77 RESERVED lock w
12dbf 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
12dc0 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 n.** is called.
12dc1 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 61 Before writing a
12dc2 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64 nything to the d
12dc3 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
12dc4 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70 is lock.** is up
12dc5 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43 graded to an EXC
12dc6 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 LUSIVE lock. If
12dc7 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 the lock cannot
12dc8 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20 be obtained,.**
12dc9 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
12dca 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 eturned and no d
12dcb 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 ata is written t
12dcc 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
12dcd 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 ile..** .** If t
12dce 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74 65 he pager is a te
12dcf 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61 6e mp-file pager an
12dd0 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c d the actual fil
12dd1 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a e-system file.**
12dd2 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e is not yet open
12dd3 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20 , it is created
12dd4 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 and opened befor
12dd5 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a 2a e any data is .*
12dd6 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a * written out..*
12dd7 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f *.** Once the lo
12dd8 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67 72 ck has been upgr
12dd9 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65 63 aded and, if nec
12dda 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c 65 essary, the file
12ddb 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20 opened,.** the
12ddc 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65 pages are writte
12ddd 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 n out to the dat
12dde 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c 69 abase file in li
12ddf 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69 6e st order. Writin
12de0 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 73 g.** a page is s
12de1 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65 65 kipped if it mee
12de2 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 ts either of the
12de3 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 following crite
12de4 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 ria:.**.** * T
12de5 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 he page number i
12de6 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 50 s greater than P
12de7 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a ager.dbSize, or.
12de8 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44 52 ** * The PGHDR
12de9 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 _DONT_WRITE flag
12dea 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 70 is set on the p
12deb 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 age..**.** If wr
12dec 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67 65 iting out a page
12ded 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61 causes the data
12dee 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f base file to gro
12def 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 w, Pager.dbFileS
12df0 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74 65 ize.** is update
12df1 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49 d accordingly. I
12df2 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69 74 f page 1 is writ
12df3 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74 68 ten out, then th
12df4 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a 2a e value cached.*
12df5 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69 6c * in Pager.dbFil
12df6 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61 74 eVers[] is updat
12df7 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 ed to match the
12df8 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65 64 new value stored
12df9 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 in.** the datab
12dfa 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
12dfb 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 If everything is
12dfc 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c successful, SQL
12dfd 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
12dfe 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 ed. If an IO err
12dff 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61 or .** occurs, a
12e00 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
12e01 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c is returned. Or,
12e02 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 56 if the EXCLUSIV
12e03 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a E lock cannot.**
12e04 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 51 be obtained, SQ
12e05 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 LITE_BUSY is ret
12e06 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
12e07 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 int pager_write
12e08 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 20 _pagelist(PgHdr
12e09 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72 *pList){. Pager
12e0a 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 *pPager;
12e0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12e0c 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
12e0d 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
12e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12e0f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
12e10 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 urn code */.. i
12e11 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d f( NEVER(pList==
12e12 30 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 0) ) return SQLI
12e13 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20 TE_OK;. pPager
12e14 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b = pList->pPager;
12e15 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 .. /* At this p
12e16 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62 oint there may b
12e17 65 20 65 69 74 68 65 72 20 61 20 52 45 53 45 52 e either a RESER
12e18 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 VED or EXCLUSIVE
12e19 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a lock on the. *
12e1a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e * database file.
12e1b 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 If there is alr
12e1c 65 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49 56 eady an EXCLUSIV
12e1d 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c E lock, the foll
12e1e 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 20 owing. ** call
12e1f 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a is a no-op.. **
12e20 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65 . ** Moving the
12e21 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 53 45 52 lock from RESER
12e22 56 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45 VED to EXCLUSIVE
12e23 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 actually involv
12e24 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 es going. ** th
12e25 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 72 6d 65 rough an interme
12e26 64 69 61 74 65 20 73 74 61 74 65 20 50 45 4e 44 diate state PEND
12e27 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 49 4e 47 ING. A PENDING
12e28 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 73 20 6e lock prevents n
12e29 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 72 73 20 ew. ** readers
12e2a 66 72 6f 6d 20 61 74 74 61 63 68 69 6e 67 20 74 from attaching t
12e2b 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 o the database b
12e2c 75 74 20 69 73 20 75 6e 73 75 66 66 69 63 69 65 ut is unsufficie
12e2d 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a nt for us to. *
12e2e 2a 20 77 72 69 74 65 2e 20 20 54 68 65 20 69 64 * write. The id
12e2f 65 61 20 6f 66 20 61 20 50 45 4e 44 49 4e 47 20 ea of a PENDING
12e30 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 65 76 65 lock is to preve
12e31 6e 74 20 6e 65 77 20 72 65 61 64 65 72 73 20 66 nt new readers f
12e32 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 rom. ** coming
12e33 69 6e 20 77 68 69 6c 65 20 77 65 20 77 61 69 74 in while we wait
12e34 20 66 6f 72 20 65 78 69 73 74 69 6e 67 20 72 65 for existing re
12e35 61 64 65 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a aders to clear..
12e36 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20 **. ** While
12e37 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
12e38 74 68 65 20 52 45 53 45 52 56 45 44 20 73 74 61 the RESERVED sta
12e39 74 65 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c te, the original
12e3a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 database file.
12e3b 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ** is unchanged
12e3c 20 61 6e 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c and we can roll
12e3d 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76 back without hav
12e3e 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 ing to playback
12e3f 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c the. ** journal
12e40 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e into the origin
12e41 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 al database file
12e42 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 61 6e 73 . Once we trans
12e43 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 ition to. ** EX
12e44 43 4c 55 53 49 56 45 2c 20 69 74 20 6d 65 61 6e CLUSIVE, it mean
12e45 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 s the database f
12e46 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 68 61 ile has been cha
12e47 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c nged and any rol
12e48 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 lback. ** will
12e49 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 require a journa
12e4a 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f l playback.. */
12e4b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
12e4c 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
12e4d 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 72 63 RESERVED );. rc
12e4e 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e = pager_wait_on
12e4f 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 _lock(pPager, EX
12e50 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 0a CLUSIVE_LOCK);..
12e51 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 /* If the file
12e52 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 is a temp-file
12e53 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e has not yet been
12e54 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74 opened, open it
12e55 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a 20 69 73 now. It. ** is
12e56 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f not possible fo
12e57 72 20 72 63 20 74 6f 20 62 65 20 6f 74 68 65 72 r rc to be other
12e58 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 than SQLITE_OK
12e59 69 66 20 74 68 69 73 20 62 72 61 6e 63 68 0a 20 if this branch.
12e5a 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c 20 61 73 ** is taken, as
12e5b 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c pager_wait_on_l
12e5c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 ock() is a no-op
12e5d 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 2e for temp-files.
12e5e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f . */. if( !isO
12e5f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
12e60 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
12e61 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
12e62 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b && rc==SQLITE_OK
12e63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 );. rc = pag
12e64 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 erOpentemp(pPage
12e65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 r, pPager->fd, p
12e66 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 29 Pager->vfsFlags)
12e67 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 ;. }.. while(
12e68 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
12e69 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 50 67 pList ){. Pg
12e6a 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73 74 2d no pgno = pList-
12e6b 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49 >pgno;.. /* I
12e6c 66 20 74 68 65 72 65 20 61 72 65 20 64 69 72 74 f there are dirt
12e6d 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70 y pages in the p
12e6e 61 67 65 20 63 61 63 68 65 20 77 69 74 68 20 70 age cache with p
12e6f 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72 65 61 age numbers grea
12e70 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 ter. ** than
12e71 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 Pager.dbSize, th
12e72 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 65 33 is means sqlite3
12e73 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 PagerTruncateIma
12e74 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 20 ge() was called
12e75 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74 to. ** make t
12e76 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20 he file smaller
12e77 28 70 72 65 73 75 6d 61 62 6c 79 20 62 79 20 61 (presumably by a
12e78 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64 65 29 uto-vacuum code)
12e79 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20 . Do not write.
12e7a 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 68 20 70 ** any such p
12e7b 61 67 65 73 20 74 6f 20 74 68 65 20 66 69 6c 65 ages to the file
12e7c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
12e7d 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 77 72 69 Also, do not wri
12e7e 74 65 20 6f 75 74 20 61 6e 79 20 70 61 67 65 20 te out any page
12e7f 74 68 61 74 20 68 61 73 20 74 68 65 20 50 47 48 that has the PGH
12e80 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c DR_DONT_WRITE fl
12e81 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 20 28 73 ag. ** set (s
12e82 65 74 20 62 79 20 73 71 6c 69 74 65 33 50 61 67 et by sqlite3Pag
12e83 65 72 44 6f 6e 74 57 72 69 74 65 28 29 29 2e 0a erDontWrite())..
12e84 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
12e85 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53 gno<=pPager->dbS
12e86 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c 69 73 74 ize && 0==(pList
12e87 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 4f ->flags&PGHDR_DO
12e88 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a 20 20 20 NT_WRITE) ){.
12e89 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 i64 offset =
12e8a 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 (pgno-1)*(i64)pP
12e8b 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 ager->pageSize;
12e8c 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 77 /* Offset to w
12e8d 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 63 68 rite */. ch
12e8e 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 ar *pData;
12e8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12e90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12e91 44 61 74 61 20 74 6f 20 77 72 69 74 65 20 2a 2f Data to write */
12e92 20 20 20 20 0a 0a 20 20 20 20 20 20 2f 2a 20 45 .. /* E
12e93 6e 63 6f 64 65 20 74 68 65 20 64 61 74 61 62 61 ncode the databa
12e94 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 4f 44 45 se */. CODE
12e95 43 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74 C2(pPager, pList
12e96 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36 ->pData, pgno, 6
12e97 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
12e98 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 29 3b 0a 0a NOMEM, pData);..
12e99 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6f /* Write o
12e9a 75 74 20 74 68 65 20 70 61 67 65 20 64 61 74 61 ut the page data
12e9b 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 . */. rc =
12e9c 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
12e9d 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 Pager->fd, pData
12e9e 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
12e9f 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 ze, offset);..
12ea0 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 31 /* If page 1
12ea1 20 77 61 73 20 6a 75 73 74 20 77 72 69 74 74 65 was just writte
12ea2 6e 2c 20 75 70 64 61 74 65 20 50 61 67 65 72 2e n, update Pager.
12ea3 64 62 46 69 6c 65 56 65 72 73 20 74 6f 20 6d 61 dbFileVers to ma
12ea4 74 63 68 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 tch. ** the
12ea5 20 76 61 6c 75 65 20 6e 6f 77 20 73 74 6f 72 65 value now store
12ea6 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 d in the databas
12ea7 65 20 66 69 6c 65 2e 20 49 66 20 77 72 69 74 69 e file. If writi
12ea8 6e 67 20 74 68 69 73 20 0a 20 20 20 20 20 20 2a ng this . *
12ea9 2a 20 70 61 67 65 20 63 61 75 73 65 64 20 74 68 * page caused th
12eaa 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
12eab 74 6f 20 67 72 6f 77 2c 20 75 70 64 61 74 65 20 to grow, update
12eac 64 62 46 69 6c 65 53 69 7a 65 2e 20 0a 20 20 20 dbFileSize. .
12ead 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
12eae 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 pgno==1 ){.
12eaf 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 memcpy(&pPage
12eb0 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 r->dbFileVers, &
12eb1 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a 65 6f pData[24], sizeo
12eb2 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 f(pPager->dbFile
12eb3 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a Vers));. }.
12eb4 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 if( pgno>p
12eb5 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a Pager->dbFileSiz
12eb6 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61 e ){. pPa
12eb7 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
12eb8 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a = pgno;. }.
12eb9 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 . /* Update
12eba 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 6a 65 any backup obje
12ebb 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 65 20 cts copying the
12ebc 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73 contents of this
12ebd 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 pager. */.
12ebe 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 sqlite3BackupUp
12ebf 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 date(pPager->pBa
12ec0 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a ckup, pgno, (u8*
12ec1 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 29 3b 0a )pList->pData);.
12ec2 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 . PAGERTRAC
12ec3 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 61 67 E(("STORE %d pag
12ec4 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c e %d hash(%08x)\
12ec5 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
12ec6 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 PAGERID(p
12ec7 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 Pager), pgno, pa
12ec8 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 ger_pagehash(pLi
12ec9 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 4f 54 st)));. IOT
12eca 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 70 20 RACE(("PGOUT %p
12ecb 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 %d\n", pPager, p
12ecc 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41 47 gno));. PAG
12ecd 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f ER_INCR(sqlite3_
12ece 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f pager_writedb_co
12ecf 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41 47 45 unt);. PAGE
12ed0 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e R_INCR(pPager->n
12ed1 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 Write);. }els
12ed2 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 e{. PAGERTR
12ed3 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 25 64 ACE(("NOSTORE %d
12ed4 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 page %d\n", PAG
12ed5 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 ERID(pPager), pg
12ed6 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 no));. }.#ifd
12ed7 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f ef SQLITE_CHECK_
12ed8 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 74 2d PAGES. pList-
12ed9 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 >pageHash = page
12eda 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73 74 r_pagehash(pList
12edb 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4c );.#endif. pL
12edc 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 ist = pList->pDi
12edd 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 rty;. }.. retu
12ede 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12edf 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72 64 20 Append a record
12ee0 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 of the current s
12ee1 74 61 74 65 20 6f 66 20 70 61 67 65 20 70 50 67 tate of page pPg
12ee2 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 to the sub-jour
12ee3 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 20 74 nal. .** It is t
12ee4 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73 70 6f he callers respo
12ee5 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75 73 65 nsibility to use
12ee6 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
12ee7 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a 2a 2a e() to check .**
12ee8 20 74 68 61 74 20 69 74 20 69 73 20 72 65 61 6c that it is real
12ee9 6c 79 20 72 65 71 75 69 72 65 64 20 62 65 66 6f ly required befo
12eea 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 re calling this
12eeb 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
12eec 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 If successful, s
12eed 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 72 65 et the bit corre
12eee 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 67 2d sponding to pPg-
12eef 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 69 74 >pgno in the bit
12ef0 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 vecs.** for all
12ef1 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 open savepoints
12ef2 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 before returning
12ef3 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
12ef4 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 ction returns SQ
12ef5 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 LITE_OK if every
12ef6 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 thing is success
12ef7 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 ful, an IO.** er
12ef8 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68 65 20 ror code if the
12ef9 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
12efa 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 to the sub-jour
12efb 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a nal fails, or .*
12efc 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 * SQLITE_NOMEM i
12efd 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 f a malloc fails
12efe 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67 20 61 while setting a
12eff 20 62 69 74 20 69 6e 20 61 20 73 61 76 65 70 6f bit in a savepo
12f00 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e 0a 2a int.** bitvec..*
12f01 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 62 /.static int sub
12f02 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 48 64 journalPage(PgHd
12f03 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 20 72 r *pPg){. int r
12f04 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
12f05 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
12f06 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pPg->pPager;.
12f07 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 if( isOpen(pPage
12f08 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 r->sjfd) ){.
12f09 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 void *pData = pP
12f0a 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 69 36 g->pData;. i6
12f0b 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67 65 4 offset = pPage
12f0c 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b 70 50 r->nSubRec*(4+pP
12f0d 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b ager->pageSize);
12f0e 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 . char *pData
12f0f 32 3b 0a 0a 20 20 20 20 43 4f 44 45 43 32 28 70 2;.. CODEC2(p
12f10 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 Pager, pData, pP
12f11 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 g->pgno, 7, retu
12f12 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c rn SQLITE_NOMEM,
12f13 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20 50 41 pData2);. PA
12f14 47 45 52 54 52 41 43 45 28 28 22 53 54 4d 54 2d GERTRACE(("STMT-
12f15 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 JOURNAL %d page
12f16 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 %d\n", PAGERID(p
12f17 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e Pager), pPg->pgn
12f18 6f 29 29 3b 0a 20 20 0a 20 20 20 20 61 73 73 65 o));. . asse
12f19 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 rt( pageInJourna
12f1a 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 l(pPg) || pPg->p
12f1b 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 gno>pPager->dbOr
12f1c 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 72 63 igSize );. rc
12f1d 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
12f1e 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 Pager->sjfd, off
12f1f 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b set, pPg->pgno);
12f20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
12f21 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12f22 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
12f23 69 74 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 ite(pPager->sjfd
12f24 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 , pData2, pPager
12f25 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 ->pageSize, offs
12f26 65 74 2b 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d et+4);. }. }
12f27 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
12f28 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 E_OK ){. pPag
12f29 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 er->nSubRec++;.
12f2a 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
12f2b 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20 r->nSavepoint>0
12f2c 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 64 64 54 );. rc = addT
12f2d 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 oSavepointBitvec
12f2e 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 s(pPager, pPg->p
12f2f 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 gno);. }. retu
12f30 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
12f31 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
12f32 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 s called by the
12f33 70 63 61 63 68 65 20 6c 61 79 65 72 20 77 68 65 pcache layer whe
12f34 6e 20 69 74 20 68 61 73 20 72 65 61 63 68 65 64 n it has reached
12f35 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 some.** soft me
12f36 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 68 65 20 mory limit. The
12f37 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
12f38 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
12f39 20 50 61 67 65 72 20 6f 62 6a 65 63 74 0a 2a 2a Pager object.**
12f3a 20 28 63 61 73 74 20 61 73 20 61 20 76 6f 69 64 (cast as a void
12f3b 2a 29 2e 20 54 68 65 20 70 61 67 65 72 20 69 73 *). The pager is
12f3c 20 61 6c 77 61 79 73 20 27 70 75 72 67 65 61 62 always 'purgeab
12f3d 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d le' (not an in-m
12f3e 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 73 emory.** databas
12f3f 65 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 e). The second a
12f40 72 67 75 6d 65 6e 74 20 69 73 20 61 20 72 65 66 rgument is a ref
12f41 65 72 65 6e 63 65 20 74 6f 20 61 20 70 61 67 65 erence to a page
12f42 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 63 75 72 that is .** cur
12f43 72 65 6e 74 6c 79 20 64 69 72 74 79 20 62 75 74 rently dirty but
12f44 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64 has no outstand
12f45 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 2e 20 ing references.
12f46 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 The page.** is a
12f47 6c 77 61 79 73 20 61 73 73 6f 63 69 61 74 65 64 lways associated
12f48 20 77 69 74 68 20 74 68 65 20 50 61 67 65 72 20 with the Pager
12f49 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 61 73 object passed as
12f4a 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 61 the first .** a
12f4b 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 rgument..**.** T
12f4c 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 66 he job of this f
12f4d 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 6d 61 unction is to ma
12f4e 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62 79 20 ke pPg clean by
12f4f 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f 6e 74 writing its cont
12f50 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 74 ents.** out to t
12f51 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12f52 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 20 54 , if possible. T
12f53 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76 65 20 his may involve
12f54 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a 2a 20 6a syncing the.** j
12f55 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a ournal file. .**
12f56 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 .** If successfu
12f57 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 l, sqlite3Pcache
12f58 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 63 MakeClean() is c
12f59 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70 61 67 alled on the pag
12f5a 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f e and.** SQLITE_
12f5b 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 OK returned. If
12f5c 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 an IO error occu
12f5d 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 20 rs while trying
12f5e 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 70 to make the.** p
12f5f 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 65 20 49 age clean, the I
12f60 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
12f61 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68 65 returned. If the
12f62 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 0a page cannot be.
12f63 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e 20 66 6f ** made clean fo
12f64 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 72 65 61 r some other rea
12f65 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f son, but no erro
12f66 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 53 r occurs, then S
12f67 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 QLITE_OK.** is r
12f68 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
12f69 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 e3PcacheMakeClea
12f6a 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 n() is not calle
12f6b 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
12f6c 20 70 61 67 65 72 53 74 72 65 73 73 28 76 6f 69 pagerStress(voi
12f6d 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70 50 67 d *p, PgHdr *pPg
12f6e 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
12f6f 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29 70 3b er = (Pager *)p;
12f70 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
12f71 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 TE_OK;.. assert
12f72 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 ( pPg->pPager==p
12f73 50 61 67 65 72 20 29 3b 0a 20 20 61 73 73 65 72 Pager );. asser
12f74 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 t( pPg->flags&PG
12f75 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 0a 20 20 HDR_DIRTY );..
12f76 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79 6e 63 /* The doNotSync
12f77 20 66 6c 61 67 20 69 73 20 73 65 74 20 62 79 20 flag is set by
12f78 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 the sqlite3Pager
12f79 57 72 69 74 65 28 29 20 66 75 6e 63 74 69 6f 6e Write() function
12f7a 20 77 68 69 6c 65 20 69 74 0a 20 20 2a 2a 20 69 while it. ** i
12f7b 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 61 20 s journalling a
12f7c 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f set of two or mo
12f7d 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 re database page
12f7e 73 20 74 68 61 74 20 61 72 65 20 73 74 6f 72 65 s that are store
12f7f 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 61 d. ** on the sa
12f80 6d 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 me disk sector.
12f81 53 79 6e 63 69 6e 67 20 74 68 65 20 6a 6f 75 72 Syncing the jour
12f82 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 nal is not allow
12f83 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68 ed while. ** th
12f84 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 is is happening
12f85 61 73 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 as it is importa
12f86 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d 65 6d 62 nt that all memb
12f87 65 72 73 20 6f 66 20 73 75 63 68 20 61 0a 20 20 ers of such a.
12f88 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 ** set of pages
12f89 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20 64 69 are synced to di
12f8a 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53 6f 2c sk together. So,
12f8b 20 69 66 20 74 68 65 20 70 61 67 65 20 74 68 69 if the page thi
12f8c 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 s function. **
12f8d 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b is trying to mak
12f8e 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72 65 71 e clean will req
12f8f 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 73 uire a journal s
12f90 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f 4e 6f ync and the doNo
12f91 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20 tSync. ** flag
12f92 69 73 20 73 65 74 2c 20 72 65 74 75 72 6e 20 77 is set, return w
12f93 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 ithout doing any
12f94 74 68 69 6e 67 2e 20 54 68 65 20 70 63 61 63 68 thing. The pcach
12f95 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a 20 20 2a e layer will. *
12f96 2a 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 67 * just have to g
12f97 6f 20 61 68 65 61 64 20 61 6e 64 20 61 6c 6c 6f o ahead and allo
12f98 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 cate a new page
12f99 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f buffer instead o
12f9a 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e 67 20 70 f. ** reusing p
12f9b 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 69 Pg.. **. ** Si
12f9c 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20 milarly, if the
12f9d 70 61 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 pager has alread
12f9e 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 y entered the er
12f9f 72 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f ror state, do no
12fa0 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20 77 72 t. ** try to wr
12fa1 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ite the contents
12fa2 20 6f 66 20 70 50 67 20 74 6f 20 64 69 73 6b 2e of pPg to disk.
12fa3 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 . */. if( NEVE
12fa4 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 R(pPager->errCod
12fa5 65 29 0a 20 20 20 7c 7c 20 28 70 50 61 67 65 72 e). || (pPager
12fa6 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 26 26 20 70 ->doNotSync && p
12fa7 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
12fa8 4e 45 45 44 5f 53 59 4e 43 29 0a 20 20 29 7b 0a NEED_SYNC). ){.
12fa9 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
12faa 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
12fab 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c Sync the journal
12fac 20 66 69 6c 65 20 69 66 20 72 65 71 75 69 72 65 file if require
12fad 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d d. */. if( pPg-
12fae 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
12faf 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 72 63 D_SYNC ){. rc
12fb0 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 = syncJournal(p
12fb1 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
12fb2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
12fb3 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e pPager->fullSyn
12fb4 63 20 26 26 20 0a 20 20 20 20 20 20 21 28 70 50 c && . !(pP
12fb5 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
12fb6 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c e==PAGER_JOURNAL
12fb7 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26 26 0a MODE_MEMORY) &&.
12fb8 20 20 20 20 20 20 21 28 73 71 6c 69 74 65 33 4f !(sqlite3O
12fb9 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 sDeviceCharacter
12fba 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 istics(pPager->f
12fbb 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f d)&SQLITE_IOCAP_
12fbc 53 41 46 45 5f 41 50 50 45 4e 44 29 0a 20 20 20 SAFE_APPEND).
12fbd 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 ){. pPager
12fbe 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 ->nRec = 0;.
12fbf 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 rc = writeJour
12fc0 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a nalHdr(pPager);.
12fc1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
12fc2 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 If the page numb
12fc3 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 er of this page
12fc4 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
12fc5 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
12fc6 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 of. ** the data
12fc7 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74 20 6d base image, it m
12fc8 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 77 72 ay need to be wr
12fc9 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 itten to the sub
12fca 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54 -journal.. ** T
12fcb 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 74 his is because t
12fcc 68 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 he call to pager
12fcd 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 _write_pagelist(
12fce 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74 ) below will not
12fcf 0a 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 77 . ** actually w
12fd0 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 rite data to the
12fd1 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61 file in this ca
12fd2 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f se.. **. ** Co
12fd3 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f nsider the follo
12fd4 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66 wing sequence of
12fd5 20 65 76 65 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20 events:. **.
12fd6 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a ** BEGIN;. **
12fd7 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 <journal pa
12fd8 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 3c ge X>. ** <
12fd9 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e 0a 20 modify page X>.
12fda 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e ** SAVEPOIN
12fdb 54 20 73 70 3b 0a 20 20 2a 2a 20 20 20 20 20 20 T sp;. **
12fdc 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62 61 73 <shrink databas
12fdd 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61 67 65 e file to Y page
12fde 73 3e 0a 20 20 2a 2a 20 20 20 20 20 20 20 70 61 s>. ** pa
12fdf 67 65 72 53 74 72 65 73 73 28 70 61 67 65 20 58 gerStress(page X
12fe0 29 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 ). ** ROLLB
12fe1 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a ACK TO sp;. **.
12fe2 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 74 ** If (X>Y), t
12fe3 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72 53 74 hen when pagerSt
12fe4 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64 20 70 ress is called p
12fe5 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62 age X will not b
12fe6 65 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 6f e written. ** o
12fe7 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
12fe8 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c se file, but wil
12fe9 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f l be dropped fro
12fea 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 m the cache. The
12feb 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e n,. ** followin
12fec 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 g the "ROLLBACK
12fed 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65 6e 74 TO sp" statement
12fee 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 58 , reading page X
12fef 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 2a 2a 20 will read. **
12ff0 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 data from the da
12ff1 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 tabase file. Thi
12ff2 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 63 6f s will be the co
12ff3 70 79 20 6f 66 20 70 61 67 65 20 58 20 61 73 20 py of page X as
12ff4 69 74 0a 20 20 2a 2a 20 77 61 73 20 77 68 65 6e it. ** was when
12ff5 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
12ff6 20 73 74 61 72 74 65 64 2c 20 6e 6f 74 20 61 73 started, not as
12ff7 20 69 74 20 77 61 73 20 77 68 65 6e 20 22 53 41 it was when "SA
12ff8 56 45 50 4f 49 4e 54 20 73 70 22 0a 20 20 2a 2a VEPOINT sp". **
12ff9 20 77 61 73 20 65 78 65 63 75 74 65 64 2e 0a 20 was executed..
12ffa 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 6f 6c **. ** The sol
12ffb 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 69 74 ution is to writ
12ffc 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 e the current da
12ffd 74 61 20 66 6f 72 20 70 61 67 65 20 58 20 69 6e ta for page X in
12ffe 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 to the . ** sub
12fff 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f -journal file no
13000 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 w (if it is not
13001 61 6c 72 65 61 64 79 20 74 68 65 72 65 29 2c 20 already there),
13002 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 0a so that it will.
13003 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 65 64 ** be restored
13004 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20 to its current
13005 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 22 value when the "
13006 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 ROLLBACK TO sp"
13007 69 73 20 0a 20 20 2a 2a 20 65 78 65 63 75 74 65 is . ** execute
13008 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 d.. */. if( NE
13009 56 45 52 28 0a 20 20 20 20 20 20 72 63 3d 3d 53 VER(. rc==S
1300a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 67 2d QLITE_OK && pPg-
1300b 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 >pgno>pPager->db
1300c 53 69 7a 65 20 26 26 20 73 75 62 6a 52 65 71 75 Size && subjRequ
1300d 69 72 65 73 50 61 67 65 28 70 50 67 29 0a 20 20 iresPage(pPg).
1300e 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 75 ) ){. rc = su
1300f 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 bjournalPage(pPg
13010 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 );. }.. /* Wri
13011 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 te the contents
13012 6f 66 20 74 68 65 20 70 61 67 65 20 6f 75 74 20 of the page out
13013 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
13014 66 69 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72 file. */. if( r
13015 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13016 20 20 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20 pPg->pDirty
13017 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 = 0;. rc = pa
13018 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 ger_write_pageli
13019 73 74 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 st(pPg);. }..
1301a 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 /* Mark the page
1301b 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 as clean. */.
1301c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1301d 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52 K ){. PAGERTR
1301e 41 43 45 28 28 22 53 54 52 45 53 53 20 25 64 20 ACE(("STRESS %d
1301f 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 page %d\n", PAGE
13020 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 RID(pPager), pPg
13021 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 ->pgno));. sq
13022 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 lite3PcacheMakeC
13023 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a lean(pPg);. }..
13024 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 return pager_e
13025 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
13026 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f ;.}.../*.** Allo
13027 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c cate and initial
13028 69 7a 65 20 61 20 6e 65 77 20 50 61 67 65 72 20 ize a new Pager
13029 6f 62 6a 65 63 74 20 61 6e 64 20 70 75 74 20 61 object and put a
1302a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a pointer to it.*
1302b 2a 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 20 54 * in *ppPager. T
1302c 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64 20 he pager should
1302d 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 66 72 eventually be fr
1302e 65 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 69 eed by passing i
1302f 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 t.** to sqlite3P
13030 61 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a agerClose()..**.
13031 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65 ** The zFilename
13032 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
13033 20 70 61 74 68 20 74 6f 20 74 68 65 20 64 61 74 path to the dat
13034 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 abase file to op
13035 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e en..** If zFilen
13036 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e ame is NULL then
13037 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65 a randomly-name
13038 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 d temporary file
13039 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 is created.** a
1303a 6e 64 20 75 73 65 64 20 61 73 20 74 68 65 20 66 nd used as the f
1303b 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64 ile to be cached
1303c 2e 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 . Temporary file
1303d 73 20 61 72 65 20 62 65 20 64 65 6c 65 74 65 64 s are be deleted
1303e 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c .** automaticall
1303f 79 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 y when they are
13040 63 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 6c 65 closed. If zFile
13041 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 name is ":memory
13042 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20 :" then .** all
13043 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 information is h
13044 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 49 74 eld in cache. It
13045 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65 is never writte
13046 6e 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54 n to disk. .** T
13047 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 his can be used
13048 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 to implement an
13049 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
1304a 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 se..**.** The nE
1304b 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 73 xtra parameter s
1304c 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d pecifies the num
1304d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
1304e 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a space allocated.
1304f 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65 61 ** along with ea
13050 63 68 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 ch page referenc
13051 65 2e 20 54 68 69 73 20 73 70 61 63 65 20 69 73 e. This space is
13052 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 available to th
13053 65 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 74 68 e user.** via th
13054 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 e sqlite3PagerGe
13055 74 45 78 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a tExtra() API..**
13056 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 .** The flags ar
13057 67 75 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 gument is used t
13058 6f 20 73 70 65 63 69 66 79 20 70 72 6f 70 65 72 o specify proper
13059 74 69 65 73 20 74 68 61 74 20 61 66 66 65 63 74 ties that affect
1305a 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f the.** operatio
1305b 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72 2e 20 n of the pager.
1305c 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 It should be pas
1305d 73 65 64 20 73 6f 6d 65 20 62 69 74 77 69 73 65 sed some bitwise
1305e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20 combination.**
1305f 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4f 4d 49 of the PAGER_OMI
13060 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41 T_JOURNAL and PA
13061 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 GER_NO_READLOCK
13062 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 flags..**.** The
13063 20 76 66 73 46 6c 61 67 73 20 70 61 72 61 6d 65 vfsFlags parame
13064 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 73 6b ter is a bitmask
13065 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 to pass to the
13066 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a flags parameter.
13067 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 28 ** of the xOpen(
13068 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 ) method of the
13069 73 75 70 70 6c 69 65 64 20 56 46 53 20 77 68 65 supplied VFS whe
1306a 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e n opening files.
1306b 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 .**.** If the p
1306c 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73 20 61 ager object is a
1306d 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 llocated and the
1306e 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 20 specified file
1306f 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65 opened .** succe
13070 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f ssfully, SQLITE_
13071 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 OK is returned a
13072 6e 64 20 2a 70 70 50 61 67 65 72 20 73 65 74 20 nd *ppPager set
13073 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 to point to.** t
13074 68 65 20 6e 65 77 20 70 61 67 65 72 20 6f 62 6a he new pager obj
13075 65 63 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ect. If an error
13076 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 67 65 occurs, *ppPage
13077 72 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c r is set to NULL
13078 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 63 6f .** and error co
13079 64 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 de returned. Thi
1307a 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 s function may r
1307b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1307c 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 EM.** (sqlite3Ma
1307d 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 64 20 74 lloc() is used t
1307e 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 o allocate memor
1307f 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f y), SQLITE_CANTO
13080 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f PEN or .** vario
13081 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 us SQLITE_IO_XXX
13082 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 errors..*/.SQLI
13083 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
13084 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 qlite3PagerOpen(
13085 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
13086 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 pVfs, /* T
13087 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20 he virtual file
13088 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f system to use */
13089 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 . Pager **ppPag
1308a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f er, /* O
1308b 55 54 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50 UT: Return the P
1308c 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 68 ager structure h
1308d 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ere */. const c
1308e 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
1308f 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
13090 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
13091 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 o open */. int
13092 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 nExtra,
13093 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79 /* Extra by
13094 74 65 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61 tes append to ea
13095 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 ch in-memory pag
13096 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 e */. int flags
13097 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
13098 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c /* flags control
13099 6c 69 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a ling this file *
1309a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 /. int vfsFlags
1309b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
1309c 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 flags passed thr
1309d 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f ough to sqlite3_
1309e 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 vfs.xOpen() */.
1309f 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29 void (*xReinit)
130a0 28 44 62 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e (DbPage*) /* Fun
130a1 63 74 69 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69 ction to reiniti
130a2 61 6c 69 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29 alize pages */.)
130a3 7b 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 {. u8 *pPtr;.
130a4 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
130a5 30 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 0; /* Page
130a6 72 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f r object to allo
130a7 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 cate and return
130a8 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 */. int rc = SQ
130a9 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a LITE_OK; /*
130aa 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
130ab 20 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d int tempFile =
130ac 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
130ad 75 65 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 ue for temp file
130ae 73 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f s (incl. in-memo
130af 72 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 ry files) */. i
130b0 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 nt memDb = 0;
130b1 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
130b2 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e if this is an in
130b3 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a -memory file */.
130b4 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d int readOnly =
130b5 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 0; /* Tr
130b6 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 ue if this is a
130b7 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a read-only file *
130b8 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 /. int journalF
130b9 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 ileSize; /*
130ba 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 Bytes to allocat
130bb 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e e for each journ
130bc 61 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 al fd */. char
130bd 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 *zPathname = 0;
130be 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 /* Full path
130bf 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c to database fil
130c0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 e */. int nPath
130c1 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 name = 0;
130c2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
130c3 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 es in zPathname
130c4 2a 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 */. int useJour
130c5 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 nal = (flags & P
130c6 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 AGER_OMIT_JOURNA
130c7 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 L)==0; /* False
130c8 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 to omit journal
130c9 2a 2f 0a 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c */. int noReadl
130ca 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26 20 50 ock = (flags & P
130cb 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b AGER_NO_READLOCK
130cc 29 21 3d 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 )!=0; /* True t
130cd 6f 20 6f 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b o omit read-lock
130ce 20 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65 */. int pcache
130cf 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63 Size = sqlite3Pc
130d0 61 63 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20 acheSize();
130d1 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c /* Bytes to al
130d2 6c 6f 63 61 74 65 20 66 6f 72 20 50 43 61 63 68 locate for PCach
130d3 65 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50 61 67 e */. u16 szPag
130d4 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 eDflt = SQLITE_D
130d5 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
130d6 3b 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 ; /* Default pa
130d7 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a ge size */.. /*
130d8 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 Figure out how
130d9 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 much space is re
130da 71 75 69 72 65 64 20 66 6f 72 20 65 61 63 68 20 quired for each
130db 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e journal file-han
130dc 64 6c 65 0a 20 20 2a 2a 20 28 74 68 65 72 65 20 dle. ** (there
130dd 61 72 65 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c are two of them,
130de 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
130df 6c 20 61 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f l and the sub-jo
130e0 75 72 6e 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a urnal). This. *
130e1 2a 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d * is the maximum
130e2 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 space required
130e3 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 for an in-memory
130e4 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
130e5 6e 64 6c 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61 ndle . ** and a
130e6 20 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c regular journal
130e7 20 66 69 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f file-handle. No
130e8 74 65 20 74 68 61 74 20 61 20 22 72 65 67 75 6c te that a "regul
130e9 61 72 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c ar journal-handl
130ea 65 22 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 61 e". ** may be a
130eb 20 77 72 61 70 70 65 72 20 63 61 70 61 62 6c 65 wrapper capable
130ec 20 6f 66 20 63 61 63 68 69 6e 67 20 74 68 65 20 of caching the
130ed 66 69 72 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66 first portion of
130ee 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a the journal. *
130ef 2a 20 66 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79 * file in memory
130f0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 to implement th
130f1 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f e atomic-write o
130f2 70 74 69 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65 ptimization (see
130f3 20 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20 66 69 . ** source fi
130f4 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20 le journal.c)..
130f5 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
130f6 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 3JournalSize(pVf
130f7 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 s)>sqlite3MemJou
130f8 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 rnalSize() ){.
130f9 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a journalFileSiz
130fa 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 e = ROUND8(sqlit
130fb 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 e3JournalSize(pV
130fc 66 73 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 fs));. }else{.
130fd 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 journalFileSi
130fe 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 ze = ROUND8(sqli
130ff 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a te3MemJournalSiz
13100 65 28 29 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 e());. }.. /*
13101 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76 Set the output v
13102 61 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20 ariable to NULL
13103 69 6e 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72 in case an error
13104 20 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70 occurs. */. *p
13105 70 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f pPager = 0;.. /
13106 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 * Compute and st
13107 6f 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74 ore the full pat
13108 68 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f hname in an allo
13109 63 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69 cated buffer poi
1310a 6e 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 nted. ** to by
1310b 7a 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 zPathname, lengt
1310c 68 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c h nPathname. Or,
1310d 20 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65 if this is a te
1310e 6d 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 mporary file,.
1310f 2a 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 ** leave both nP
13110 61 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 athname and zPat
13111 68 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a hname set to 0..
13112 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 */. if( zFile
13113 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d name && zFilenam
13114 65 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 e[0] ){. nPat
13115 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 hname = pVfs->mx
13116 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 Pathname+1;.
13117 7a 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 zPathname = sqli
13118 74 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e te3Malloc(nPathn
13119 61 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 ame*2);. if(
1311a 7a 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a zPathname==0 ){.
1311b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1311c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d ITE_NOMEM;. }
1311d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1311e 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 OMIT_MEMORYDB.
1311f 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69 if( strcmp(zFi
13120 6c 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a lename,":memory:
13121 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d ")==0 ){. m
13122 65 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 emDb = 1;.
13123 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 zPathname[0] = 0
13124 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 ;. }else.#end
13125 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 7a if. {. z
13126 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b Pathname[0] = 0;
13127 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 6e /* Make sure in
13128 69 74 69 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 itialized even i
13129 66 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 f FullPathname()
1312a 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20 fails */.
1312b 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 rc = sqlite3OsFu
1312c 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c llPathname(pVfs,
1312d 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 zFilename, nPat
1312e 68 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 hname, zPathname
1312f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50 );. }.. nP
13130 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 athname = sqlite
13131 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 3Strlen30(zPathn
13132 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ame);. if( rc
13133 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e ==SQLITE_OK && n
13134 50 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d Pathname+8>pVfs-
13135 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 >mxPathname ){.
13136 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
13137 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65 nch is taken whe
13138 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 n the journal pa
13139 74 68 20 72 65 71 75 69 72 65 64 20 62 79 0a 20 th required by.
1313a 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 ** the data
1313b 62 61 73 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 base being opene
1313c 64 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 d will be more t
1313d 68 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 han pVfs->mxPath
1313e 6e 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 name. ** by
1313f 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 tes in length. T
13140 68 69 73 20 6d 65 61 6e 73 20 74 68 65 20 64 61 his means the da
13141 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 tabase cannot be
13142 20 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a opened,. *
13143 2a 20 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 * as it will not
13144 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 be possible to
13145 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c open the journal
13146 20 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 file or even.
13147 20 20 20 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 ** check for
13148 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 a hot-journal b
13149 65 66 6f 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 efore reading..
1314a 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 */. rc
1314b 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 = SQLITE_CANTOP
1314c 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 EN;. }. if
1314d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1314e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1314f 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 _free(zPathname)
13150 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
13151 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 c;. }. }..
13152 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f /* Allocate memo
13153 72 79 20 66 6f 72 20 74 68 65 20 50 61 67 65 72 ry for the Pager
13154 20 73 74 72 75 63 74 75 72 65 2c 20 50 43 61 63 structure, PCac
13155 68 65 20 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20 he object, the.
13156 20 2a 2a 20 74 68 72 65 65 20 66 69 6c 65 20 64 ** three file d
13157 65 73 63 72 69 70 74 6f 72 73 2c 20 74 68 65 20 escriptors, the
13158 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 database file na
13159 6d 65 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e me and the journ
1315a 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61 al . ** file na
1315b 6d 65 2e 20 54 68 65 20 6c 61 79 6f 75 74 20 69 me. The layout i
1315c 6e 20 6d 65 6d 6f 72 79 20 69 73 20 61 73 20 66 n memory is as f
1315d 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a ollows:. **. *
1315e 2a 20 20 20 20 20 50 61 67 65 72 20 6f 62 6a 65 * Pager obje
1315f 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ct
13160 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 50 61 (sizeof(Pa
13161 67 65 72 29 20 62 79 74 65 73 29 0a 20 20 2a 2a ger) bytes). **
13162 20 20 20 20 20 50 43 61 63 68 65 20 6f 62 6a 65 PCache obje
13163 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ct
13164 20 20 20 20 20 28 73 71 6c 69 74 65 33 50 63 61 (sqlite3Pca
13165 63 68 65 53 69 7a 65 28 29 20 62 79 74 65 73 29 cheSize() bytes)
13166 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61 . ** Databa
13167 73 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 se file handle
13168 20 20 20 20 20 20 20 20 20 20 28 70 56 66 73 2d (pVfs-
13169 3e 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 29 >szOsFile bytes)
1316a 0a 20 20 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f . ** Sub-jo
1316b 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c urnal file handl
1316c 65 20 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e e (journ
1316d 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 alFileSize bytes
1316e 29 0a 20 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20 ). ** Main
1316f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e journal file han
13170 64 6c 65 20 20 20 20 20 20 20 20 28 6a 6f 75 72 dle (jour
13171 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 nalFileSize byte
13172 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 s). ** Data
13173 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 20 base file name
13174 20 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61 (nPa
13175 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 29 0a thname+1 bytes).
13176 20 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c ** Journal
13177 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 file name
13178 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e (nPathn
13179 61 6d 65 2b 38 2b 31 20 62 79 74 65 73 29 0a 20 ame+8+1 bytes).
1317a 20 2a 2f 0a 20 20 70 50 74 72 20 3d 20 28 75 38 */. pPtr = (u8
1317b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 *)sqlite3Malloc
1317c 5a 65 72 6f 28 0a 20 20 20 20 52 4f 55 4e 44 38 Zero(. ROUND8
1317d 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 (sizeof(*pPager)
1317e 29 20 2b 20 20 20 20 20 20 2f 2a 20 50 61 67 65 ) + /* Page
1317f 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 r structure */.
13180 20 20 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65 ROUND8(pcache
13181 53 69 7a 65 29 20 2b 20 20 20 20 20 20 20 20 20 Size) +
13182 20 20 2f 2a 20 50 43 61 63 68 65 20 6f 62 6a 65 /* PCache obje
13183 63 74 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 ct */. ROUND8
13184 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 (pVfs->szOsFile)
13185 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 + /* The
13186 6d 61 69 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a main db file */.
13187 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 journalFileS
13188 69 7a 65 20 2a 20 32 20 2b 20 20 20 20 20 20 20 ize * 2 +
13189 20 20 20 2f 2a 20 54 68 65 20 74 77 6f 20 6a 6f /* The two jo
1318a 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 20 0a urnal files */ .
1318b 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 nPathname +
1318c 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 1 +
1318d 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 /* zFilename
1318e 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 */. nPathname
1318f 20 2b 20 38 20 2b 20 31 20 20 20 20 20 20 20 20 + 8 + 1
13190 20 20 20 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61 /* zJourna
13191 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 61 73 73 65 l */. );. asse
13192 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
13193 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 54 45 5f LIGNMENT(SQLITE_
13194 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f 75 72 6e INT_TO_PTR(journ
13195 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 29 3b 0a alFileSize)) );.
13196 20 20 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20 if( !pPtr ){.
13197 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
13198 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 zPathname);.
13199 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1319a 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 MEM;. }. pPage
1319b 72 20 3d 20 20 20 20 20 20 20 20 20 20 20 20 20 r =
1319c 20 28 50 61 67 65 72 2a 29 28 70 50 74 72 29 3b (Pager*)(pPtr);
1319d 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 . pPager->pPCac
1319e 68 65 20 3d 20 20 20 20 28 50 43 61 63 68 65 2a he = (PCache*
1319f 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 )(pPtr += ROUND8
131a0 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 (sizeof(*pPager)
131a1 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64 ));. pPager->fd
131a2 20 3d 20 20 20 28 73 71 6c 69 74 65 33 5f 66 69 = (sqlite3_fi
131a3 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 le*)(pPtr += ROU
131a4 4e 44 38 28 70 63 61 63 68 65 53 69 7a 65 29 29 ND8(pcacheSize))
131a5 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 ;. pPager->sjfd
131a6 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 = (sqlite3_file
131a7 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 *)(pPtr += ROUND
131a8 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 8(pVfs->szOsFile
131a9 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 ));. pPager->jf
131aa 64 20 3d 20 20 28 73 71 6c 69 74 65 33 5f 66 69 d = (sqlite3_fi
131ab 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 le*)(pPtr += jou
131ac 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 rnalFileSize);.
131ad 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
131ae 6d 65 20 3d 20 20 20 20 28 63 68 61 72 2a 29 28 me = (char*)(
131af 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 pPtr += journalF
131b0 69 6c 65 53 69 7a 65 29 3b 0a 20 20 61 73 73 65 ileSize);. asse
131b1 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
131b2 4c 49 47 4e 4d 45 4e 54 28 70 50 61 67 65 72 2d LIGNMENT(pPager-
131b3 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 >jfd) );.. /* F
131b4 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 67 65 72 ill in the Pager
131b5 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50 .zFilename and P
131b6 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75 ager.zJournal bu
131b7 66 66 65 72 73 2c 20 69 66 20 72 65 71 75 69 72 ffers, if requir
131b8 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61 ed. */. if( zPa
131b9 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 70 50 thname ){. pP
131ba 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d ager->zJournal =
131bb 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 72 20 (char*)(pPtr
131bc 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 += nPathname + 1
131bd 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 );. memcpy(pP
131be 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c ager->zFilename,
131bf 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 zPathname, nPat
131c0 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 hname);. memc
131c1 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 py(pPager->zJour
131c2 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 nal, zPathname,
131c3 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 nPathname);.
131c4 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e memcpy(&pPager->
131c5 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 zJournal[nPathna
131c6 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c me], "-journal",
131c7 20 38 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 8);. if( pPa
131c8 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 5b 30 ger->zFilename[0
131c9 5d 3d 3d 30 20 29 20 70 50 61 67 65 72 2d 3e 7a ]==0 ) pPager->z
131ca 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d 20 30 3b 0a Journal[0] = 0;.
131cb 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
131cc 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d (zPathname);. }
131cd 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 66 73 20 . pPager->pVfs
131ce 3d 20 70 56 66 73 3b 0a 20 20 70 50 61 67 65 72 = pVfs;. pPager
131cf 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 76 66 73 ->vfsFlags = vfs
131d0 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65 Flags;.. /* Ope
131d1 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 n the pager file
131d2 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 .. */. if( zFi
131d3 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e lename && zFilen
131d4 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d 44 62 ame[0] && !memDb
131d5 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 6f 75 74 ){. int fout
131d6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
131d7 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 /* VFS
131d8 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64 20 62 flags returned b
131d9 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 20 y xOpen() */.
131da 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
131db 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 pen(pVfs, pPager
131dc 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 50 61 ->zFilename, pPa
131dd 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 6c 61 67 ger->fd, vfsFlag
131de 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 20 72 s, &fout);. r
131df 65 61 64 4f 6e 6c 79 20 3d 20 28 66 6f 75 74 26 eadOnly = (fout&
131e0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
131e1 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 ONLY);.. /* I
131e2 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 73 f the file was s
131e3 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e uccessfully open
131e4 65 64 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 ed for read/writ
131e5 65 20 61 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a e access,. **
131e6 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c choose a defaul
131e7 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63 t page size in c
131e8 61 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63 ase we have to c
131e9 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a reate the. **
131ea 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
131eb 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 The default page
131ec 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d 61 78 size is the max
131ed 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a imum of:. **.
131ee 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 51 4c 49 ** + SQLI
131ef 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f TE_DEFAULT_PAGE_
131f0 53 49 5a 45 2c 0a 20 20 20 20 2a 2a 20 20 20 20 SIZE,. **
131f1 2b 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 + The value retu
131f2 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f rned by sqlite3O
131f3 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20 sSectorSize().
131f4 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 6c 61 ** + The la
131f5 72 67 65 73 74 20 70 61 67 65 20 73 69 7a 65 20 rgest page size
131f6 74 68 61 74 20 63 61 6e 20 62 65 20 77 72 69 74 that can be writ
131f7 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2e 0a ten atomically..
131f8 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
131f9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
131fa 21 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 !readOnly ){.
131fb 20 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 setSectorSize
131fc 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
131fd 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 44 45 assert(SQLITE_DE
131fe 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c FAULT_PAGE_SIZE<
131ff 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 =SQLITE_MAX_DEFA
13200 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29 3b 0a ULT_PAGE_SIZE);.
13201 20 20 20 20 20 20 69 66 28 20 73 7a 50 61 67 65 if( szPage
13202 44 66 6c 74 3c 70 50 61 67 65 72 2d 3e 73 65 63 Dflt<pPager->sec
13203 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 torSize ){.
13204 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 if( pPager->s
13205 65 63 74 6f 72 53 69 7a 65 3e 53 51 4c 49 54 45 ectorSize>SQLITE
13206 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
13207 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 E_SIZE ){.
13208 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d szPageDflt =
13209 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 SQLITE_MAX_DEFA
1320a 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 0a 20 ULT_PAGE_SIZE;.
1320b 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1320c 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 szPageDf
1320d 6c 74 20 3d 20 28 75 31 36 29 70 50 61 67 65 72 lt = (u16)pPager
1320e 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 ->sectorSize;.
1320f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
13210 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
13211 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 ABLE_ATOMIC_WRIT
13212 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 E. {.
13213 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 int iDc = sqli
13214 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 te3OsDeviceChara
13215 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65 cteristics(pPage
13216 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 r->fd);.
13217 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 int ii;.
13218 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f assert(SQLITE_IO
13219 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 CAP_ATOMIC512==(
1321a 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 512>>8));.
1321b 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f assert(SQLITE_
1321c 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d IOCAP_ATOMIC64K=
1321d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 =(65536>>8));.
1321e 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c assert(SQL
1321f 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f ITE_MAX_DEFAULT_
13220 50 41 47 45 5f 53 49 5a 45 3c 3d 36 35 35 33 36 PAGE_SIZE<=65536
13221 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 );. for(i
13222 69 3d 73 7a 50 61 67 65 44 66 6c 74 3b 20 69 69 i=szPageDflt; ii
13223 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 <=SQLITE_MAX_DEF
13224 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 AULT_PAGE_SIZE;
13225 69 69 3d 69 69 2a 32 29 7b 0a 20 20 20 20 20 20 ii=ii*2){.
13226 20 20 20 20 69 66 28 20 69 44 63 26 28 53 51 4c if( iDc&(SQL
13227 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
13228 7c 28 69 69 3e 3e 38 29 29 20 29 7b 0a 20 20 20 |(ii>>8)) ){.
13229 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 szPageD
1322a 66 6c 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 flt = ii;.
1322b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
1322c 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
1322d 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1322e 20 20 2f 2a 20 49 66 20 61 20 74 65 6d 70 6f 72 /* If a tempor
1322f 61 72 79 20 66 69 6c 65 20 69 73 20 72 65 71 75 ary file is requ
13230 65 73 74 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 ested, it is not
13231 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 69 61 74 opened immediat
13232 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 6e 20 74 ely.. ** In t
13233 68 69 73 20 63 61 73 65 20 77 65 20 61 63 63 65 his case we acce
13234 70 74 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 pt the default p
13235 61 67 65 20 73 69 7a 65 20 61 6e 64 20 64 65 6c age size and del
13236 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 20 20 20 ay actually.
13237 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 ** opening the f
13238 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 66 69 ile until the fi
13239 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f 73 57 72 rst call to OsWr
1323a 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 ite().. **.
1323b 20 20 2a 2a 20 54 68 69 73 20 62 72 61 6e 63 68 ** This branch
1323c 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 66 6f 72 is also run for
1323d 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
1323e 74 61 62 61 73 65 2e 20 41 6e 20 69 6e 2d 6d 65 tabase. An in-me
1323f 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 61 74 61 mory. ** data
13240 62 61 73 65 20 69 73 20 74 68 65 20 73 61 6d 65 base is the same
13241 20 61 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 as a temp-file
13242 74 68 61 74 20 69 73 20 6e 65 76 65 72 20 77 72 that is never wr
13243 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 20 20 20 itten out to.
13244 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 75 73 65 ** disk and use
13245 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 s an in-memory r
13246 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
13247 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 74 65 6d . */ . tem
13248 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 20 20 70 pFile = 1;. p
13249 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 Pager->state = P
1324a 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a AGER_EXCLUSIVE;.
1324b 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 readOnly = (
1324c 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 45 5f vfsFlags&SQLITE_
1324d 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a OPEN_READONLY);.
1324e 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f }.. /* The fo
1324f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 20 llowing call to
13250 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
13251 28 29 20 73 65 72 76 65 73 20 74 6f 20 73 65 74 () serves to set
13252 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 0a 20 the value of .
13253 20 2a 2a 20 50 61 67 65 72 2e 70 61 67 65 53 69 ** Pager.pageSi
13254 7a 65 20 61 6e 64 20 74 6f 20 61 6c 6c 6f 63 61 ze and to alloca
13255 74 65 20 74 68 65 20 50 61 67 65 72 2e 70 54 6d te the Pager.pTm
13256 70 53 70 61 63 65 20 62 75 66 66 65 72 2e 0a 20 pSpace buffer..
13257 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 */. if( rc==SQ
13258 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 LITE_OK ){. a
13259 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6d ssert( pPager->m
1325a 65 6d 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20 72 emDb==0 );. r
1325b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1325c 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 67 SetPagesize(pPag
1325d 65 72 2c 20 26 73 7a 50 61 67 65 44 66 6c 74 2c er, &szPageDflt,
1325e 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61 -1);. testca
1325f 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f se( rc!=SQLITE_O
13260 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 K );. }.. /* I
13261 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
13262 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6f 66 red in either of
13263 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76 the blocks abov
13264 65 2c 20 66 72 65 65 20 74 68 65 20 0a 20 20 2a e, free the . *
13265 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 * Pager structur
13266 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 e and close the
13267 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 file.. */. if(
13268 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13269 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 {. assert( !p
1326a 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
1326b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f );. sqlite3O
1326c 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 sClose(pPager->f
1326d 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f d);. sqlite3_
1326e 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 free(pPager);.
1326f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
13270 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a .. /* Initializ
13271 65 20 74 68 65 20 50 43 61 63 68 65 20 6f 62 6a e the PCache obj
13272 65 63 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ect. */. assert
13273 28 20 6e 45 78 74 72 61 3c 31 30 30 30 20 29 3b ( nExtra<1000 );
13274 0a 20 20 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e . nExtra = ROUN
13275 44 38 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 D8(nExtra);. sq
13276 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 lite3PcacheOpen(
13277 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 szPageDflt, nExt
13278 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 ra, !memDb,.
13279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1327a 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65 !memDb?pagerStre
1327b 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 ss:0, (void *)pP
1327c 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 ager, pPager->pP
1327d 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 Cache);.. PAGER
1327e 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20 TRACE(("OPEN %d
1327f 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c %s\n", FILEHANDL
13280 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c EID(pPager->fd),
13281 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
13282 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 me));. IOTRACE(
13283 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c ("OPEN %p %s\n",
13284 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d pPager, pPager-
13285 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 >zFilename))..
13286 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e pPager->useJourn
13287 61 6c 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 al = (u8)useJour
13288 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e nal;. pPager->n
13289 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 oReadlock = (noR
1328a 65 61 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f eadlock && readO
1328b 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 nly) ?1:0;. /*
1328c 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e pPager->stmtOpen
1328d 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
1328e 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 ager->stmtInUse
1328f 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 = 0; */. /* pPa
13290 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a ger->nRef = 0; *
13291 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 /. pPager->dbSi
13292 7a 65 56 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 zeValid = (u8)me
13293 6d 44 62 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 mDb;. /* pPager
13294 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 ->stmtSize = 0;
13295 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e */. /* pPager->
13296 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a stmtJSize = 0; *
13297 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e /. /* pPager->n
13298 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 Page = 0; */. p
13299 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 Pager->mxPgno =
1329a 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1329b 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 COUNT;. /* pPag
1329c 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
1329d 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 R_UNLOCK; */. a
1329e 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
1329f 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c tate == (tempFil
132a0 65 20 3f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 e ? PAGER_EXCLUS
132a1 49 56 45 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f IVE : PAGER_UNLO
132a2 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 CK) );. /* pPag
132a3 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b er->errMask = 0;
132a4 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 */. pPager->te
132a5 6d 70 46 69 6c 65 20 3d 20 28 75 38 29 74 65 6d mpFile = (u8)tem
132a6 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 pFile;. assert(
132a7 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 tempFile==PAGER
132a8 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 _LOCKINGMODE_NOR
132a9 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c MAL . |
132aa 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 | tempFile==PAGE
132ab 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 R_LOCKINGMODE_EX
132ac 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73 CLUSIVE );. ass
132ad 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 ert( PAGER_LOCKI
132ae 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
132af 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d ==1 );. pPager-
132b0 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d >exclusiveMode =
132b1 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a (u8)tempFile; .
132b2 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 pPager->change
132b3 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 CountDone = pPag
132b4 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 er->tempFile;.
132b5 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 pPager->memDb =
132b6 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 (u8)memDb;. pPa
132b7 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 ger->readOnly =
132b8 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 (u8)readOnly;.
132b9 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 /* pPager->needS
132ba 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 61 73 ync = 0; */. as
132bb 73 65 72 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c sert( useJournal
132bc 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 || pPager->temp
132bd 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 67 65 72 File );. pPager
132be 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61 67 65 ->noSync = pPage
132bf 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 r->tempFile;. p
132c0 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 Pager->fullSync
132c1 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 = pPager->noSync
132c2 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 65 72 2d ?0:1;. pPager-
132c3 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 53 51 >sync_flags = SQ
132c4 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c LITE_SYNC_NORMAL
132c5 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 ;. /* pPager->p
132c6 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 First = 0; */.
132c7 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73 /* pPager->pFirs
132c8 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a 2f 0a tSynced = 0; */.
132c9 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 4c 61 /* pPager->pLa
132ca 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 st = 0; */. pPa
132cb 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20 28 75 ger->nExtra = (u
132cc 31 36 29 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 16)nExtra;. pPa
132cd 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 ger->journalSize
132ce 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 Limit = SQLITE_D
132cf 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 EFAULT_JOURNAL_S
132d0 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 IZE_LIMIT;. ass
132d1 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
132d2 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46 er->fd) || tempF
132d3 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 65 63 74 ile );. setSect
132d4 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a orSize(pPager);.
132d5 20 20 69 66 28 20 21 75 73 65 4a 6f 75 72 6e 61 if( !useJourna
132d6 6c 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d l ){. pPager-
132d7 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 >journalMode = P
132d8 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
132d9 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65 20 69 66 _OFF;. }else if
132da 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 70 ( memDb ){. p
132db 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
132dc 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e de = PAGER_JOURN
132dd 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20 ALMODE_MEMORY;.
132de 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e }. /* pPager->
132df 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 30 xBusyHandler = 0
132e0 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 ; */. /* pPager
132e1 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 ->pBusyHandlerAr
132e2 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 g = 0; */. pPag
132e3 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 er->xReiniter =
132e4 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a 20 6d 65 xReinit;. /* me
132e5 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 48 61 mset(pPager->aHa
132e6 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 50 sh, 0, sizeof(pP
132e7 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b 20 2a ager->aHash)); *
132e8 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 70 /. *ppPager = p
132e9 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 Pager;. return
132ea 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a SQLITE_OK;.}....
132eb 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
132ec 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 ion is called af
132ed 74 65 72 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e ter transitionin
132ee 67 20 66 72 6f 6d 20 50 41 47 45 52 5f 55 4e 4c g from PAGER_UNL
132ef 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f OCK to.** PAGER_
132f0 53 48 41 52 45 44 20 73 74 61 74 65 2e 20 49 74 SHARED state. It
132f1 20 74 65 73 74 73 20 69 66 20 74 68 65 72 65 20 tests if there
132f2 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c is a hot journal
132f3 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 present in.** t
132f4 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 he file-system f
132f5 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 or the given pag
132f6 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61 er. A hot journa
132f7 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 0a 2a l is one that .*
132f8 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c * needs to be pl
132f9 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63 6f 72 ayed back. Accor
132fa 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66 75 6e ding to this fun
132fb 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a 6f 75 ction, a hot-jou
132fc 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 65 78 69 rnal.** file exi
132fd 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f sts if the follo
132fe 77 69 6e 67 20 63 72 69 74 65 72 69 61 20 61 72 wing criteria ar
132ff 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a e met:.**.** *
13300 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
13301 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 e exists in the
13302 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 61 6e 64 file system, and
13303 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 72 6f 63 65 .** * No proce
13304 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52 ss holds a RESER
13305 56 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c VED or greater l
13306 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
13307 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 0a 2a 2a ase file, and.**
13308 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 61 73 * The databas
13309 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20 69 73 e file itself is
1330a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 20 greater than 0
1330b 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 61 bytes in size, a
1330c 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 66 69 nd.** * The fi
1330d 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 rst byte of the
1330e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 journal file exi
1330f 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74 20 30 sts and is not 0
13310 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 x00..**.** If th
13311 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
13312 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
13313 69 6c 65 20 69 73 20 30 20 62 75 74 20 61 20 6a ile is 0 but a j
13314 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 65 ournal file.** e
13315 78 69 73 74 73 2c 20 74 68 61 74 20 69 73 20 70 xists, that is p
13316 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64 20 6a robably an old j
13317 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76 65 72 ournal left over
13318 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a from a prior.**
13319 20 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74 database with t
1331a 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 49 6e he same name. In
1331b 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a this case the j
1331c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 0a 2a ournal file is.*
1331d 2a 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20 75 * just deleted u
1331e 73 69 6e 67 20 4f 73 44 65 6c 65 74 65 2c 20 2a sing OsDelete, *
1331f 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74 pExists is set t
13320 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f o 0 and SQLITE_O
13321 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 K.** is returned
13322 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
13323 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 tine does not ch
13324 65 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 eck if there is
13325 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
13326 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 61 74 20 filename.** at
13327 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 the end of the f
13328 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 ile. If there is
13329 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73 74 65 , and that maste
1332a 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a r journal file.*
1332b 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 * does not exist
1332c 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e , then the journ
1332d 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 al file is not r
1332e 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20 74 68 eally hot. In th
1332f 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 69 73 20 is.** case this
13330 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 routine will ret
13331 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 urn a false-posi
13332 74 69 76 65 2e 20 54 68 65 20 70 61 67 65 72 5f tive. The pager_
13333 70 6c 61 79 62 61 63 6b 28 29 0a 2a 2a 20 72 6f playback().** ro
13334 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73 63 6f utine will disco
13335 76 65 72 20 74 68 61 74 20 74 68 65 20 6a 6f 75 ver that the jou
13336 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 rnal file is not
13337 20 72 65 61 6c 6c 79 20 68 6f 74 20 61 6e 64 20 really hot and
13338 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c .** will not rol
13339 6c 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a l it back. .**.*
1333a 2a 20 49 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e * If a hot-journ
1333b 61 6c 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 al file is found
1333c 20 74 6f 20 65 78 69 73 74 2c 20 2a 70 45 78 69 to exist, *pExi
1333d 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 31 20 sts is set to 1
1333e 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f and .** SQLITE_O
1333f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 6e K returned. If n
13340 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 o hot-journal fi
13341 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 2a le is present, *
13342 70 45 78 69 73 74 73 20 69 73 0a 2a 2a 20 73 65 pExists is.** se
13343 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 t to 0 and SQLIT
13344 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 E_OK returned. I
13345 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 f an IO error oc
13346 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e curs while tryin
13347 67 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e g.** to determin
13348 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 e whether or not
13349 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 a hot-journal f
1334a 69 6c 65 20 65 78 69 73 74 73 2c 20 74 68 65 20 ile exists, the
1334b 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 IO error.** code
1334c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
1334d 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 2a 70 the value of *p
1334e 45 78 69 73 74 73 20 69 73 20 75 6e 64 65 66 69 Exists is undefi
1334f 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ned..*/.static i
13350 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c nt hasHotJournal
13351 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
13352 69 6e 74 20 2a 70 45 78 69 73 74 73 29 7b 0a 20 int *pExists){.
13353 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 sqlite3_vfs * c
13354 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 onst pVfs = pPag
13355 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 er->pVfs;. int
13356 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
13357 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
13358 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e urn code */. in
13359 74 20 65 78 69 73 74 73 3b 20 20 20 20 20 20 20 t exists;
1335a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1335b 72 75 65 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c rue if a journal
1335c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74 file is present
1335d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1335e 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 Pager!=0 );. as
1335f 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 sert( pPager->us
13360 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 eJournal );. as
13361 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
13362 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 61 73 ger->fd) );. as
13363 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 sert( !isOpen(pP
13364 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 ager->jfd) );.
13365 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13366 73 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 state <= PAGER_S
13367 48 41 52 45 44 20 29 3b 0a 0a 20 20 2a 70 45 78 HARED );.. *pEx
13368 69 73 74 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d ists = 0;. rc =
13369 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
1336a 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
1336b 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f Journal, SQLITE_
1336c 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 ACCESS_EXISTS, &
1336d 65 78 69 73 74 73 29 3b 0a 20 20 69 66 28 20 72 exists);. if( r
1336e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
1336f 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 69 6e exists ){. in
13370 74 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 t locked;
13371 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
13372 65 20 69 66 20 73 6f 6d 65 20 70 72 6f 63 65 73 e if some proces
13373 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56 s holds a RESERV
13374 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 ED lock */..
13375 2f 2a 20 52 61 63 65 20 63 6f 6e 64 69 74 69 6f /* Race conditio
13376 6e 20 68 65 72 65 3a 20 20 41 6e 6f 74 68 65 72 n here: Another
13377 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 68 process might h
13378 61 76 65 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67 ave been holding
13379 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 the. ** the
1337a 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e RESERVED lock an
1337b 64 20 68 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c d have a journal
1337c 20 6f 70 65 6e 20 61 74 20 74 68 65 20 73 71 6c open at the sql
1337d 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 0a ite3OsAccess() .
1337e 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 ** call abov
1337f 65 2c 20 62 75 74 20 74 68 65 6e 20 64 65 6c 65 e, but then dele
13380 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 te the journal a
13381 6e 64 20 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b nd drop the lock
13382 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77 before. ** w
13383 65 20 67 65 74 20 74 6f 20 74 68 65 20 66 6f 6c e get to the fol
13384 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 lowing sqlite3Os
13385 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
13386 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20 74 68 k() call. If th
13387 61 74 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65 at. ** is the
13388 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f 75 74 case, this rout
13389 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e 6b 20 ine might think
1338a 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a there is a hot j
1338b 6f 75 72 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20 ournal when.
1338c 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65 72 65 ** in fact there
1338d 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20 is none. This
1338e 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66 61 6c results in a fal
1338f 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68 69 63 se-positive whic
13390 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 h will. ** be
13391 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74 dealt with by t
13392 68 65 20 70 6c 61 79 62 61 63 6b 20 72 6f 75 74 he playback rout
13393 69 6e 65 2e 20 20 54 69 63 6b 65 74 20 23 33 38 ine. Ticket #38
13394 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 83.. */. r
13395 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 68 65 c = sqlite3OsChe
13396 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 ckReservedLock(p
13397 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b Pager->fd, &lock
13398 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d ed);. if( rc=
13399 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c =SQLITE_OK && !l
1339a 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 69 ocked ){. i
1339b 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20 nt nPage;..
1339c 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 73 69 /* Check the si
1339d 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
1339e 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74 20 63 se file. If it c
1339f 6f 6e 73 69 73 74 73 20 6f 66 20 30 20 70 61 67 onsists of 0 pag
133a0 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 es,. ** the
133a1 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 n delete the jou
133a2 72 6e 61 6c 20 66 69 6c 65 2e 20 53 65 65 20 74 rnal file. See t
133a3 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e he header commen
133a4 74 20 61 62 6f 76 65 20 66 6f 72 20 0a 20 20 20 t above for .
133a5 20 20 20 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e ** the reason
133a6 69 6e 67 20 68 65 72 65 2e 20 20 44 65 6c 65 74 ing here. Delet
133a7 65 20 74 68 65 20 6f 62 73 6f 6c 65 74 65 20 6a e the obsolete j
133a8 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e 64 65 ournal file unde
133a9 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 52 45 53 r. ** a RES
133aa 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 76 ERVED lock to av
133ab 6f 69 64 20 72 61 63 65 20 63 6f 6e 64 69 74 69 oid race conditi
133ac 6f 6e 73 20 61 6e 64 20 74 6f 20 61 76 6f 69 64 ons and to avoid
133ad 20 76 69 6f 6c 61 74 69 6e 67 0a 20 20 20 20 20 violating.
133ae 20 2a 2a 20 5b 48 33 33 30 32 30 5d 2e 0a 20 20 ** [H33020]..
133af 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 */. rc
133b0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 = sqlite3PagerPa
133b1 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 gecount(pPager,
133b2 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 &nPage);. i
133b3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
133b4 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
133b5 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 nPage==0 ){.
133b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 sqlite3Beg
133b7 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
133b8 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
133b9 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 sqlite3OsLock(pP
133ba 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 ager->fd, RESERV
133bb 45 44 5f 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45 ED_LOCK)==SQLITE
133bc 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
133bd 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 sqlite3OsDele
133be 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d te(pVfs, pPager-
133bf 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 >zJournal, 0);.
133c0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
133c1 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 e3OsUnlock(pPage
133c2 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f r->fd, SHARED_LO
133c3 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d CK);. }
133c4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
133c5 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f e3EndBenignMallo
133c6 63 28 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c c();. }el
133c7 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a se{. /*
133c8 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
133c9 65 20 65 78 69 73 74 73 20 61 6e 64 20 6e 6f 20 e exists and no
133ca 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e other connection
133cb 20 68 61 73 20 61 20 72 65 73 65 72 76 65 64 0a has a reserved.
133cc 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 ** or
133cd 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 greater lock on
133ce 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
133cf 65 2e 20 4e 6f 77 20 63 68 65 63 6b 20 74 68 61 e. Now check tha
133d0 74 20 74 68 65 72 65 20 69 73 0a 20 20 20 20 20 t there is.
133d1 20 20 20 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 ** at least
133d2 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 6f 20 62 79 one non-zero by
133d3 74 65 73 20 61 74 20 74 68 65 20 73 74 61 72 74 tes at the start
133d4 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
133d5 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20 file..
133d6 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 2c 20 ** If there is,
133d7 74 68 65 6e 20 77 65 20 63 6f 6e 73 69 64 65 72 then we consider
133d8 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 74 6f this journal to
133d9 20 62 65 20 68 6f 74 2e 20 49 66 20 6e 6f 74 2c be hot. If not,
133da 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 . ** i
133db 74 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 t can be ignored
133dc 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
133dd 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d int f =
133de 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
133df 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 DONLY|SQLITE_OPE
133e0 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a N_MAIN_JOURNAL;.
133e1 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
133e2 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
133e3 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 s, pPager->zJour
133e4 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 nal, pPager->jfd
133e5 2c 20 66 2c 20 26 66 29 3b 0a 20 20 20 20 20 20 , f, &f);.
133e6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
133e7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
133e8 20 20 20 20 20 75 38 20 66 69 72 73 74 20 3d 20 u8 first =
133e9 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 0;. r
133ea 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
133eb 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28 d(pPager->jfd, (
133ec 76 6f 69 64 20 2a 29 26 66 69 72 73 74 2c 20 31 void *)&first, 1
133ed 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
133ee 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
133ef 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
133f0 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 D ){.
133f1 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
133f2 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d K;. }
133f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
133f4 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 ite3OsClose(pPag
133f5 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 er->jfd);.
133f6 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d *pExists =
133f7 20 28 66 69 72 73 74 21 3d 30 29 3b 0a 20 20 20 (first!=0);.
133f8 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
133f9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 rc==SQLITE_CANT
133fa 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 OPEN ){.
133fb 20 20 20 20 2f 2a 20 49 66 20 77 65 20 63 61 6e /* If we can
133fc 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 72 6f 6c not open the rol
133fd 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 lback journal fi
133fe 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 le in order to s
133ff 65 65 20 69 66 0a 20 20 20 20 20 20 20 20 20 20 ee if.
13400 20 20 2a 2a 20 69 74 73 20 68 61 73 20 61 20 7a ** its has a z
13401 65 72 6f 20 68 65 61 64 65 72 2c 20 74 68 61 74 ero header, that
13402 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 6f might be due to
13403 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 2c 20 6f an I/O error, o
13404 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a r. **
13405 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 75 65 it might be due
13406 20 74 6f 20 74 68 65 20 72 61 63 65 20 63 6f 6e to the race con
13407 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 dition described
13408 20 61 62 6f 76 65 20 61 6e 64 20 69 6e 0a 20 20 above and in.
13409 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 69 63 ** tic
1340a 6b 65 74 20 23 33 38 38 33 2e 20 20 45 69 74 68 ket #3883. Eith
1340b 65 72 20 77 61 79 2c 20 61 73 73 75 6d 65 20 74 er way, assume t
1340c 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
1340d 69 73 20 68 6f 74 2e 0a 20 20 20 20 20 20 20 20 is hot..
1340e 20 20 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 ** This migh
1340f 74 20 62 65 20 61 20 66 61 6c 73 65 20 70 6f 73 t be a false pos
13410 69 74 69 76 65 2e 20 20 42 75 74 20 69 66 20 69 itive. But if i
13411 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 t is, then the.
13412 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 75 ** au
13413 74 6f 6d 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20 tomatic journal
13414 70 6c 61 79 62 61 63 6b 20 61 6e 64 20 72 65 63 playback and rec
13415 6f 76 65 72 79 20 6d 65 63 68 61 6e 69 73 6d 20 overy mechanism
13416 77 69 6c 6c 20 64 65 61 6c 0a 20 20 20 20 20 20 will deal.
13417 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 69 74 ** with it
13418 20 75 6e 64 65 72 20 61 6e 20 45 58 43 4c 55 53 under an EXCLUS
13419 49 56 45 20 6c 6f 63 6b 20 77 68 65 72 65 20 77 IVE lock where w
1341a 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f e do not need to
1341b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
1341c 77 6f 72 72 79 20 73 6f 20 6d 75 63 68 20 77 69 worry so much wi
1341d 74 68 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f th race conditio
1341e 6e 73 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 ns..
1341f 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a */. *
13420 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20 20 20 pExists = 1;.
13421 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 rc = SQ
13422 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 LITE_OK;.
13423 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
13424 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
13425 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
13426 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 ../*.** Read the
13427 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 content for pag
13428 65 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 e pPg out of the
13429 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
1342a 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d nd into .** pPg-
1342b 3e 70 44 61 74 61 2e 20 41 20 73 68 61 72 65 64 >pData. A shared
1342c 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72 lock or greater
1342d 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 6f 6e must be held on
1342e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
1342f 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69 file before thi
13430 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
13431 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 lled..**.** If p
13432 61 67 65 20 31 20 69 73 20 72 65 61 64 2c 20 74 age 1 is read, t
13433 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 hen the value of
13434 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 Pager.dbFileVer
13435 73 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a s[] is set to.**
13436 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 the value read
13437 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
13438 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e file..**.** If
13439 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
1343a 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 49 4f urs, then the IO
1343b 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
1343c 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
1343d 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 ..** Otherwise,
1343e 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
1343f 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
13440 20 69 6e 74 20 72 65 61 64 44 62 50 61 67 65 28 int readDbPage(
13441 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 PgHdr *pPg){. P
13442 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 ager *pPager = p
13443 50 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 Pg->pPager; /* P
13444 61 67 65 72 20 6f 62 6a 65 63 74 20 61 73 73 6f ager object asso
13445 63 69 61 74 65 64 20 77 69 74 68 20 70 61 67 65 ciated with page
13446 20 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 pPg */. Pgno p
13447 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b gno = pPg->pgno;
13448 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
13449 75 6d 62 65 72 20 74 6f 20 72 65 61 64 20 2a 2f umber to read */
1344a 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
1344b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1344c 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
1344d 2f 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b /. i64 iOffset;
1344e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1344f 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 /* Byte offset
13450 6f 66 20 66 69 6c 65 20 74 6f 20 72 65 61 64 20 of file to read
13451 66 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 from */.. asser
13452 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
13453 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 26 >=PAGER_SHARED &
13454 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 & !MEMDB );. as
13455 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 sert( isOpen(pPa
13456 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 69 ger->fd) );.. i
13457 66 28 20 4e 45 56 45 52 28 21 69 73 4f 70 65 6e f( NEVER(!isOpen
13458 28 70 50 61 67 65 72 2d 3e 66 64 29 29 20 29 7b (pPager->fd)) ){
13459 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
1345a 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
1345b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d . memset(pPg-
1345c 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 >pData, 0, pPage
1345d 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 r->pageSize);.
1345e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1345f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f 66 66 73 65 OK;. }. iOffse
13460 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 t = (pgno-1)*(i6
13461 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 4)pPager->pageSi
13462 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 ze;. rc = sqlit
13463 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
13464 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c >fd, pPg->pData,
13465 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
13466 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 69 e, iOffset);. i
13467 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f f( rc==SQLITE_IO
13468 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 ERR_SHORT_READ )
13469 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1346a 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 E_OK;. }. if(
1346b 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 75 pgno==1 ){. u
1346c 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20 3d 20 8 *dbFileVers =
1346d 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 61 74 &((u8*)pPg->pDat
1346e 61 29 5b 32 34 5d 3b 0a 20 20 20 20 6d 65 6d 63 a)[24];. memc
1346f 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 py(&pPager->dbFi
13470 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 65 leVers, dbFileVe
13471 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 rs, sizeof(pPage
13472 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b r->dbFileVers));
13473 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 . }. CODEC1(pP
13474 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 ager, pPg->pData
13475 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 63 20 3d 20 , pgno, 3, rc =
13476 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 0a SQLITE_NOMEM);..
13477 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c PAGER_INCR(sql
13478 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64 ite3_pager_readd
13479 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 b_count);. PAGE
1347a 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e R_INCR(pPager->n
1347b 52 65 61 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 Read);. IOTRACE
1347c 28 28 22 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 (("PGIN %p %d\n"
1347d 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 , pPager, pgno))
1347e 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 ;. PAGERTRACE((
1347f 22 46 45 54 43 48 20 25 64 20 70 61 67 65 20 25 "FETCH %d page %
13480 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c d hash(%08x)\n",
13481 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
13482 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
13483 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 pgno, pager_pag
13484 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 ehash(pPg)));..
13485 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
13486 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
13487 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 on is called to
13488 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 20 obtain a shared
13489 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
1348a 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74 base file..** It
1348b 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63 is illegal to c
1348c 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 all sqlite3Pager
1348d 41 63 71 75 69 72 65 28 29 20 75 6e 74 69 6c 20 Acquire() until
1348e 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
1348f 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ion.** has been
13490 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 61 6c successfully cal
13491 6c 65 64 2e 20 49 66 20 61 20 73 68 61 72 65 64 led. If a shared
13492 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 -lock is already
13493 20 68 65 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68 held when.** th
13494 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
13495 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e alled, it is a n
13496 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 o-op..**.** The
13497 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74 following operat
13498 69 6f 6e 73 20 61 72 65 20 61 6c 73 6f 20 70 65 ions are also pe
13499 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20 rformed by this
1349a 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
1349b 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67 65 1) If the page
1349c 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 r is currently i
1349d 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 n PAGER_UNLOCK s
1349e 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65 tate (no lock he
1349f 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 ld.** on th
134a0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 e database file)
134a1 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 , then an attemp
134a2 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 74 t is made to obt
134a3 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53 48 ain a.** SH
134a4 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ARED lock on the
134a5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
134a6 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 Immediately afte
134a7 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 r obtaining.**
134a8 20 20 20 20 74 68 65 20 53 48 41 52 45 44 20 6c the SHARED l
134a9 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73 79 ock, the file-sy
134aa 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20 stem is checked
134ab 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 for a hot-journa
134ac 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63 68 l,.** which
134ad 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 is played back
134ae 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c if present. Foll
134af 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f owing any hot-jo
134b0 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72 urnal .** r
134b1 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e ollback, the con
134b2 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 tents of the cac
134b3 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 65 64 he are validated
134b4 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 by checking.**
134b5 20 20 20 20 20 74 68 65 20 27 63 68 61 6e 67 65 the 'change
134b6 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64 20 -counter' field
134b7 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
134b8 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64 0a file header and.
134b9 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 64 65 ** discarde
134ba 64 20 69 66 20 74 68 65 79 20 61 72 65 20 66 6f d if they are fo
134bb 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 und to be invali
134bc 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 d..**.** 2) If
134bd 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75 the pager is ru
134be 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 nning in exclusi
134bf 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65 ve-mode, and the
134c0 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 re are currently
134c1 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73 .** no outs
134c2 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
134c3 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 2c es to any pages,
134c4 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 65 and is in the e
134c5 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20 rror state,.**
134c6 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 65 then an atte
134c7 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 mpt is made to c
134c8 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73 lear the error s
134c9 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64 69 tate by discardi
134ca 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63 ng.** the c
134cb 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 ontents of the p
134cc 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72 6f age cache and ro
134cd 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f lling back any o
134ce 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 pen journal.**
134cf 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 file..**.**
134d0 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e If the operation
134d1 20 64 65 73 63 72 69 62 65 64 20 62 79 20 28 32 described by (2
134d2 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 61 ) above is not a
134d3 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 69 66 ttempted, and if
134d4 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 69 73 the.** pager is
134d5 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 in an error sta
134d6 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 te other than SQ
134d7 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74 LITE_FULL when t
134d8 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a his is called,.*
134d9 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 * the error stat
134da 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
134db 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 73 20 returned. It is
134dc 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 61 permitted to rea
134dd 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 d the.** databas
134de 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 54 45 e when in SQLITE
134df 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 61 74 _FULL error stat
134e0 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 e..**.** Otherwi
134e1 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e se, if everythin
134e2 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c g is successful,
134e3 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
134e4 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a turned. If an.**
134e5 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
134e6 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74 while locking t
134e7 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 68 65 he database, che
134e8 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d cking for a hot-
134e9 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 journal.** file
134ea 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 or rolling back
134eb 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 a journal file,
134ec 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
134ed 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
134ee 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
134ef 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
134f0 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 rSharedLock(Page
134f1 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e r *pPager){. in
134f2 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
134f3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
134f4 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
134f5 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72 */. int isError
134f6 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20 Reset = 0;
134f7 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
134f8 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20 66 72 if recovering fr
134f9 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65 20 2a om error state *
134fa 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 /.. /* This rou
134fb 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c tine is only cal
134fc 6c 65 64 20 66 72 6f 6d 20 62 2d 74 72 65 65 20 led from b-tree
134fd 61 6e 64 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68 and only when th
134fe 65 72 65 20 61 72 65 20 6e 6f 0a 20 20 2a 2a 20 ere are no. **
134ff 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 outstanding page
13500 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 s */. assert( s
13501 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
13502 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
13503 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 69 66 ache)==0 );. if
13504 28 20 4e 45 56 45 52 28 4d 45 4d 44 42 20 26 26 ( NEVER(MEMDB &&
13505 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
13506 29 20 29 7b 20 72 65 74 75 72 6e 20 70 50 61 67 ) ){ return pPag
13507 65 72 2d 3e 65 72 72 43 6f 64 65 3b 20 7d 0a 0a er->errCode; }..
13508 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 /* If this dat
13509 61 62 61 73 65 20 69 73 20 69 6e 20 61 6e 20 65 abase is in an e
1350a 72 72 6f 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20 rror-state, now
1350b 69 73 20 61 20 63 68 61 6e 63 65 20 74 6f 20 63 is a chance to c
1350c 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 lear. ** the er
1350d 72 6f 72 2e 20 44 69 73 63 61 72 64 20 74 68 65 ror. Discard the
1350e 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
1350f 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64 pager-cache and
13510 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 61 rollback. ** a
13511 6e 79 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 ny hot journal i
13512 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 n the file-syste
13513 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 m.. */. if( pP
13514 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b ager->errCode ){
13515 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 . if( isOpen(
13516 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 pPager->jfd) ||
13517 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
13518 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 72 6f ){. isErro
13519 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 20 20 rReset = 1;.
1351a 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 }. pPager->er
1351b 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f rCode = SQLITE_O
1351c 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 65 73 K;. pager_res
1351d 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a et(pPager);. }.
1351e 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
1351f 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f tate==PAGER_UNLO
13520 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 65 73 CK || isErrorRes
13521 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 et ){. sqlite
13522 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 3_vfs * const pV
13523 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 fs = pPager->pVf
13524 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74 s;. int isHot
13525 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 Journal = 0;.
13526 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 assert( !MEMDB
13527 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
13528 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 qlite3PcacheRefC
13529 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 ount(pPager->pPC
1352a 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 ache)==0 );.
1352b 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 if( pPager->noRe
1352c 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 adlock ){.
1352d 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
1352e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 20 readOnly );.
1352f 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
13530 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a = PAGER_SHARED;.
13531 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13532 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 rc = pager_wait
13533 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c _on_lock(pPager,
13534 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 SHARED_LOCK);.
13535 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13536 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13537 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13538 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 ->state==PAGER_U
13539 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20 NLOCK );.
1353a 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 return pager_er
1353b 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b ror(pPager, rc);
1353c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1353d 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
1353e 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45 44 r->state>=SHARED
1353f 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a _LOCK );.. /*
13540 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 If a journal fi
13541 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 20 74 le exists, and t
13542 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 45 52 here is no RESER
13543 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a VED lock on the.
13544 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
13545 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 65 69 file, then it ei
13546 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 ther needs to be
13547 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 72 20 played back or
13548 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a deleted.. */.
13549 20 20 20 20 69 66 28 20 21 69 73 45 72 72 6f 72 if( !isError
1354a 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20 61 Reset ){. a
1354b 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
1354c 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 48 tate <= PAGER_SH
1354d 41 52 45 44 20 29 3b 0a 20 20 20 20 20 20 72 63 ARED );. rc
1354e 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c = hasHotJournal
1354f 28 70 50 61 67 65 72 2c 20 26 69 73 48 6f 74 4a (pPager, &isHotJ
13550 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 ournal);. i
13551 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13552 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
13553 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d failed;. }
13554 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 . }. if( i
13555 73 45 72 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 sErrorReset || i
13556 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 sHotJournal ){.
13557 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 /* Get an E
13558 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e XCLUSIVE lock on
13559 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1355a 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e le. At this poin
1355b 74 20 69 74 20 69 73 0a 20 20 20 20 20 20 2a 2a t it is. **
1355c 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 important that
1355d 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
1355e 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 is not obtained
1355f 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 74 68 on the way to th
13560 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 e. ** EXCLU
13561 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 SIVE lock. If it
13562 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 were, another p
13563 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65 rocess might ope
13564 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 n the. ** d
13565 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65 atabase file, de
13566 74 65 63 74 20 74 68 65 20 52 45 53 45 52 56 45 tect the RESERVE
13567 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 D lock, and conc
13568 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a 20 20 lude that the.
13569 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 ** database
1356a 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20 is safe to read
1356b 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65 while this proce
1356c 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c ss is still roll
1356d 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 2a ing the . *
1356e 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61 * hot-journal ba
1356f 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 ck.. ** .
13570 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74 ** Because t
13571 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 he intermediate
13572 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 RESERVED lock is
13573 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20 not requested,
13574 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 any. ** oth
13575 65 72 20 70 72 6f 63 65 73 73 20 61 74 74 65 6d er process attem
13576 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73 73 20 pting to access
13577 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13578 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20 e will get to .
13579 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 ** this poi
1357a 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61 nt in the code a
1357b 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 61 69 nd fail to obtai
1357c 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 n its own EXCLUS
1357d 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20 IVE lock .
1357e 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ** on the databa
1357f 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a se file.. *
13580 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 /. if( pPag
13581 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 er->state<EXCLUS
13582 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 IVE_LOCK ){.
13583 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13584 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 OsLock(pPager->f
13585 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 d, EXCLUSIVE_LOC
13586 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 K);. if(
13587 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13588 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
13589 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
1358a 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 er, rc);.
1358b 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
1358c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1358d 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
1358e 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 = PAGER_EXCLUSIV
1358f 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 E;. }. .
13590 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a /* Open the j
13591 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f ournal for read/
13592 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 68 write access. Th
13593 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e is is because in
13594 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 . ** exclu
13595 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 sive-access mode
13596 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 the file descri
13597 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 ptor will be kep
13598 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 t open and.
13599 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73 65 ** possibly use
1359a 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 d for a transact
1359b 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e ion later on. On
1359c 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 some systems, t
1359d 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 he. ** OsTr
1359e 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 75 73 uncate() call us
1359f 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d ed in exclusive-
135a0 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f access mode also
135a1 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20 requires.
135a2 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 ** a read/write
135a3 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 file handle..
135a4 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
135a5 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
135a6 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 jfd) ){.
135a7 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 20 int res;.
135a8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
135a9 63 63 65 73 73 28 70 56 66 73 2c 70 50 61 67 65 ccess(pVfs,pPage
135aa 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 r->zJournal,SQLI
135ab 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
135ac 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 ,&res);.
135ad 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
135ae 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 K ){. i
135af 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 f( res ){.
135b0 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d int fout =
135b1 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0;.
135b2 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f int f = SQLITE_O
135b3 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 PEN_READWRITE|SQ
135b4 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a LITE_OPEN_MAIN_J
135b5 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 OURNAL;.
135b6 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 assert( !pPa
135b7 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
135b8 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
135b9 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 = sqlite3OsOpen(
135ba 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a pVfs, pPager->zJ
135bb 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e ournal, pPager->
135bc 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a jfd, f, &fout);.
135bd 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 asse
135be 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f rt( rc!=SQLITE_O
135bf 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 K || isOpen(pPag
135c0 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 er->jfd) );.
135c1 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
135c2 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 SQLITE_OK && fou
135c3 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 t&SQLITE_OPEN_RE
135c4 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 ADONLY ){.
135c5 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
135c6 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 ITE_CANTOPEN;.
135c7 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
135c8 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 te3OsClose(pPage
135c9 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 r->jfd);.
135ca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
135cb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
135cc 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f /* If the jo
135cd 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 urnal does not e
135ce 78 69 73 74 2c 20 69 74 20 75 73 75 61 6c 6c 79 xist, it usually
135cf 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65 means that some
135d0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a . **
135d1 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
135d2 6e 20 6d 61 6e 61 67 65 64 20 74 6f 20 67 65 74 n managed to get
135d3 20 69 6e 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20 in and roll it
135d4 62 61 63 6b 20 62 65 66 6f 72 65 20 0a 20 20 20 back before .
135d5 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 ** this
135d6 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74 61 connection obta
135d7 69 6e 65 64 20 74 68 65 20 65 78 63 6c 75 73 69 ined the exclusi
135d8 76 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e 20 4f ve lock above. O
135d9 72 2c 20 69 74 20 0a 20 20 20 20 20 20 20 20 20 r, it .
135da 20 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e 20 74 ** may mean t
135db 68 61 74 20 74 68 65 20 70 61 67 65 72 20 77 61 hat the pager wa
135dc 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 2d 73 s in the error-s
135dd 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 20 tate when this.
135de 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 ** fu
135df 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 nction was calle
135e0 64 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 d and the journa
135e1 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 l file does not
135e2 65 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 20 20 exist. */.
135e3 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 rc = page
135e4 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
135e5 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 n(pPager, 0);.
135e6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
135e7 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
135e8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
135e9 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
135ea 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 oto failed;.
135eb 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f }.. /* TO
135ec 44 4f 3a 20 57 68 79 20 61 72 65 20 74 68 65 73 DO: Why are thes
135ed 65 20 63 6c 65 61 72 65 64 20 68 65 72 65 3f 20 e cleared here?
135ee 49 73 20 69 74 20 6e 65 63 65 73 73 61 72 79 3f Is it necessary?
135ef 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 67 65 72 */. pPager
135f0 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
135f1 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 = 0;. pPag
135f2 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
135f3 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
135f4 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b ->setMaster = 0;
135f5 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a . pPager->j
135f6 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 ournalHdr = 0;.
135f7 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 . /* Playba
135f8 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 ck and delete th
135f9 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 e journal. Drop
135fa 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72 the database wr
135fb 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 ite. ** loc
135fc 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 65 20 k and reacquire
135fd 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 the read lock. P
135fe 75 72 67 65 20 74 68 65 20 63 61 63 68 65 20 62 urge the cache b
135ff 65 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 efore. ** p
13600 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20 laying back the
13601 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 hot-journal so t
13602 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 hat we don't end
13603 20 75 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a up with. *
13604 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e * an inconsisten
13605 74 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a t cache.. *
13606 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 /. if( isOp
13607 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
13608 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
13609 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 pager_playback(p
1360a 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 Pager, 1);.
1360b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1360c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1360d 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 rc = pager_err
1360e 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a or(pPager, rc);.
1360f 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 goto f
13610 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d ailed;. }
13611 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
13612 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e ssert( (pPager->
13613 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 state==PAGER_SHA
13614 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 20 RED).
13615 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c || (pPager->excl
13616 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61 usiveMode && pPa
13617 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 ger->state>PAGER
13618 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 29 _SHARED). )
13619 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
1361a 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 pPager->pBackup
1361b 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68 || sqlite3Pcach
1361c 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 ePagecount(pPage
1361d 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b r->pPCache)>0 ){
1361e 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68 . /* The sh
1361f 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 ared-lock has ju
13620 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 64 st been acquired
13621 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
13622 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 file. ** a
13623 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 nd there are alr
13624 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 68 eady pages in th
13625 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20 e cache (from a
13626 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a previous. *
13627 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 * read or write
13628 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 transaction). C
13629 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 heck to see if t
1362a 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
1362b 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f ** has been mo
1362c 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 20 dified. If the
1362d 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 61 database has cha
1362e 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a nged, flush the.
1362f 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a ** cache..
13630 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
13631 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67 * Database chang
13632 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62 es is detected b
13633 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 y looking at 15
13634 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a bytes beginning.
13635 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 ** at offs
13636 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66 et 24 into the f
13637 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 ile. The first
13638 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 79 4 of these 16 by
13639 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a tes are. **
1363a 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 a 32-bit counte
1363b 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d r that is increm
1363c 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20 ented with each
1363d 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 change. The.
1363e 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 ** other byte
1363f 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c s change randoml
13640 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c 65 y with each file
13641 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 change when.
13642 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 ** a codec is
13643 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a in use.. *
13644 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 * . ** Ther
13645 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67 e is a vanishing
13646 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 ly small chance
13647 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 69 that a change wi
13648 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 ll not be .
13649 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54 ** detected. T
1364a 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 he chance of an
1364b 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67 undetected chang
1364c 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 e is so small th
1364d 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63 at. ** it c
1364e 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e an be neglected.
1364f 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
13650 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b char dbFileVers[
13651 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 sizeof(pPager->d
13652 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20 bFileVers)];.
13653 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 sqlite3PagerP
13654 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
13655 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 0);.. if(
13656 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 pPager->errCode
13657 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
13658 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
13659 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 . goto fa
1365a 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 iled;. }..
1365b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 assert( pPa
1365c 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
1365d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 );. if( pP
1365e 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 ager->dbSize>0 )
1365f 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 {. IOTRAC
13660 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 25 64 E(("CKVERS %p %d
13661 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a \n", pPager, siz
13662 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 eof(dbFileVers))
13663 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
13664 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 sqlite3OsRead(pP
13665 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c ager->fd, &dbFil
13666 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 eVers, sizeof(db
13667 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a FileVers), 24);.
13668 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
13669 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1366a 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c goto fail
1366b 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ed;. }.
1366c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1366d 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 memset(dbFile
1366e 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Vers, 0, sizeof(
1366f 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 dbFileVers));.
13670 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 }.. if(
13671 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e memcmp(pPager->
13672 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 dbFileVers, dbFi
13673 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 leVers, sizeof(d
13674 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29 bFileVers))!=0 )
13675 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f {. pager_
13676 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
13677 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
13678 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13679 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c >exclusiveMode |
1367a 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d | pPager->state=
1367b 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b =PAGER_SHARED );
1367c 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 . }.. failed:.
1367d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1367e 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 OK ){. /* pag
1367f 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 er_unlock() is a
13680 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 no-op for exclu
13681 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e sive mode and in
13682 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
13683 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f s. */. pager_
13684 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a unlock(pPager);.
13685 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13686 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
13687 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
13688 20 68 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 has reached zer
13689 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 o, rollback any
1368a 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 active.** transa
1368b 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b ction and unlock
1368c 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a the pager..**.*
1368d 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 * Except, in loc
1368e 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 king_mode=EXCLUS
1368f 49 56 45 20 77 68 65 6e 20 74 68 65 72 65 20 69 IVE when there i
13690 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a s nothing to in.
13691 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** the rollback
13692 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c journal, the unl
13693 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f ock is not perfo
13694 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65 20 69 rmed and there i
13695 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 s.** nothing to
13696 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 rollback, so thi
13697 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
13698 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 o-op..*/ .static
13699 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 void pagerUnloc
1369a 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20 kIfUnused(Pager
1369b 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 *pPager){. if(
1369c 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 (sqlite3PcacheRe
1369d 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
1369e 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26 PCache)==0). &
1369f 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c & (!pPager->excl
136a0 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 usiveMode || pPa
136a1 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e ger->journalOff>
136a2 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67 0) . ){. pag
136a3 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 erUnlockAndRollb
136a4 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d ack(pPager);. }
136a5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 .}../*.** Acquir
136a6 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f e a reference to
136a7 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 67 6e page number pgn
136a8 6f 20 69 6e 20 70 61 67 65 72 20 70 50 61 67 65 o in pager pPage
136a9 72 20 28 61 20 70 61 67 65 0a 2a 2a 20 72 65 66 r (a page.** ref
136aa 65 72 65 6e 63 65 20 68 61 73 20 74 79 70 65 20 erence has type
136ab 44 62 50 61 67 65 2a 29 2e 20 49 66 20 74 68 65 DbPage*). If the
136ac 20 72 65 71 75 65 73 74 65 64 20 72 65 66 65 72 requested refer
136ad 65 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75 63 63 ence is .** succ
136ae 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 essfully obtaine
136af 64 2c 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 d, it is copied
136b0 74 6f 20 2a 70 70 50 61 67 65 20 61 6e 64 20 53 to *ppPage and S
136b1 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
136b2 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
136b3 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 requested page i
136b4 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 s already in the
136b5 20 63 61 63 68 65 2c 20 69 74 20 69 73 20 72 65 cache, it is re
136b6 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 turned. .** Othe
136b7 72 77 69 73 65 2c 20 61 20 6e 65 77 20 70 61 67 rwise, a new pag
136b8 65 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f e object is allo
136b9 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 cated and popula
136ba 74 65 64 20 77 69 74 68 20 64 61 74 61 0a 2a 2a ted with data.**
136bb 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 read from the d
136bc 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e atabase file. In
136bd 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 some cases, the
136be 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 6d pcache module m
136bf 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 20 6e 6f 74 ay.** choose not
136c0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e to allocate a n
136c1 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 ew page object a
136c2 6e 64 20 6d 61 79 20 72 65 75 73 65 20 61 6e 20 nd may reuse an
136c3 65 78 69 73 74 69 6e 67 0a 2a 2a 20 6f 62 6a 65 existing.** obje
136c4 63 74 20 77 69 74 68 20 6e 6f 20 6f 75 74 73 74 ct with no outst
136c5 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 anding reference
136c6 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 74 s..**.** The ext
136c7 72 61 20 64 61 74 61 20 61 70 70 65 6e 64 65 64 ra data appended
136c8 20 74 6f 20 61 20 70 61 67 65 20 69 73 20 61 6c to a page is al
136c9 77 61 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 ways initialized
136ca 20 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 0a 2a to zeros the .*
136cb 2a 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 70 * first time a p
136cc 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e age is loaded in
136cd 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68 to memory. If th
136ce 65 20 70 61 67 65 20 72 65 71 75 65 73 74 65 64 e page requested
136cf 20 69 73 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 is .** already
136d0 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 68 65 in the cache whe
136d1 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
136d2 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 is called, then
136d3 74 68 65 20 65 78 74 72 61 0a 2a 2a 20 64 61 74 the extra.** dat
136d4 61 20 69 73 20 6c 65 66 74 20 61 73 20 69 74 20 a is left as it
136d5 77 61 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 was when the pag
136d6 65 20 6f 62 6a 65 63 74 20 77 61 73 20 6c 61 73 e object was las
136d7 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 t used..**.** If
136d8 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d the database im
136d9 61 67 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 age is smaller t
136da 68 61 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 han the requeste
136db 64 20 70 61 67 65 20 6f 72 20 69 66 20 61 20 0a d page or if a .
136dc 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 ** non-zero valu
136dd 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 e is passed as t
136de 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 70 61 72 he noContent par
136df 61 6d 65 74 65 72 20 61 6e 64 20 74 68 65 20 0a ameter and the .
136e0 2a 2a 20 72 65 71 75 65 73 74 65 64 20 70 61 67 ** requested pag
136e1 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 e is not already
136e2 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 stored in the c
136e3 61 63 68 65 2c 20 74 68 65 6e 20 6e 6f 20 0a 2a ache, then no .*
136e4 2a 20 61 63 74 75 61 6c 20 64 69 73 6b 20 72 65 * actual disk re
136e5 61 64 20 6f 63 63 75 72 73 2e 20 49 6e 20 74 68 ad occurs. In th
136e6 69 73 20 63 61 73 65 20 74 68 65 20 6d 65 6d 6f is case the memo
136e7 72 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20 ry image of the
136e8 0a 2a 2a 20 70 61 67 65 20 69 73 20 69 6e 69 74 .** page is init
136e9 69 61 6c 69 7a 65 64 20 74 6f 20 61 6c 6c 20 7a ialized to all z
136ea 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 eros. .**.** If
136eb 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 noContent is tru
136ec 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 e, it means that
136ed 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 we do not care
136ee 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e about the conten
136ef 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 ts.** of the pag
136f0 65 2e 20 54 68 69 73 20 6f 63 63 75 72 73 20 69 e. This occurs i
136f1 6e 20 74 77 6f 20 73 65 70 65 72 61 74 65 20 73 n two seperate s
136f2 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a 0a 2a 2a 20 cenarios:.**.**
136f3 20 20 61 29 20 57 68 65 6e 20 72 65 61 64 69 6e a) When readin
136f4 67 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 g a free-list le
136f5 61 66 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 af page from the
136f6 20 64 61 74 61 62 61 73 65 2c 20 61 6e 64 0a 2a database, and.*
136f7 2a 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e 20 61 *.** b) When a
136f8 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 62 65 savepoint is be
136f9 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 ing rolled back
136fa 61 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6c and we need to l
136fb 6f 61 64 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65 oad.** a ne
136fc 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 w page into the
136fd 63 61 63 68 65 20 74 6f 20 70 6f 70 75 6c 61 74 cache to populat
136fe 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61 20 e with the data
136ff 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f read.** fro
13700 6d 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 m the savepoint
13701 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 journal..**.** I
13702 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 f noContent is t
13703 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 rue, then the da
13704 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 7a ta returned is z
13705 65 72 6f 65 64 20 69 6e 73 74 65 61 64 20 6f 66 eroed instead of
13706 0a 2a 2a 20 62 65 69 6e 67 20 72 65 61 64 20 66 .** being read f
13707 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
13708 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 . Additionally,
13709 74 68 65 20 62 69 74 73 20 63 6f 72 72 65 73 70 the bits corresp
1370a 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 67 6e onding.** to pgn
1370b 6f 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f o in Pager.pInJo
1370c 75 72 6e 61 6c 20 28 62 69 74 76 65 63 20 6f 66 urnal (bitvec of
1370d 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 77 pages already w
1370e 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a ritten to the.**
1370f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 61 journal file) a
13710 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 nd the PagerSave
13711 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 point.pInSavepoi
13712 6e 74 20 62 69 74 76 65 63 73 20 6f 66 20 61 6e nt bitvecs of an
13713 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 76 65 70 6f y open.** savepo
13714 69 6e 74 73 20 61 72 65 20 73 65 74 2e 20 54 68 ints are set. Th
13715 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65 20 is means if the
13716 70 61 67 65 20 69 73 20 6d 61 64 65 20 77 72 69 page is made wri
13717 74 61 62 6c 65 20 61 74 20 61 6e 79 0a 2a 2a 20 table at any.**
13718 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 point in the fut
13719 75 72 65 2c 20 75 73 69 6e 67 20 61 20 63 61 6c ure, using a cal
1371a 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 l to sqlite3Page
1371b 72 57 72 69 74 65 28 29 2c 20 69 74 73 20 63 6f rWrite(), its co
1371c 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 6c 6c 20 6e ntents.** will n
1371d 6f 74 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e ot be journaled.
1371e 20 54 68 69 73 20 73 61 76 65 73 20 49 4f 2e 0a This saves IO..
1371f 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 69 73 **.** The acquis
13720 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c ition might fail
13721 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 65 61 for several rea
13722 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61 sons. In all ca
13723 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f ses,.** an appro
13724 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 priate error cod
13725 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e e is returned an
13726 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74 d *ppPage is set
13727 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 to NULL..**.**
13728 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 See also sqlite3
13729 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 PagerLookup().
1372a 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e Both this routin
1372b 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 e and Lookup() a
1372c 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e ttempt.** to fin
1372d 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20 d a page in the
1372e 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 in-memory cache
1372f 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 20 70 first. If the p
13730 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
13731 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c dy.** in memory,
13732 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67 6f this routine go
13733 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72 65 es to disk to re
13734 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 61 73 ad it in whereas
13735 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73 Lookup().** jus
13736 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 54 68 t returns 0. Th
13737 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 is routine acqui
13738 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 res a read-lock
13739 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 the first time i
1373a 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f 20 74 t.** has to go t
1373b 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c o disk, and coul
1373c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20 d also playback
1373d 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69 an old journal i
1373e 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 f necessary..**
1373f 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e Since Lookup() n
13740 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 69 73 ever goes to dis
13741 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 73 20 k, it never has
13742 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c 6f 63 to deal with loc
13743 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c ks.** or journal
13744 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 files..*/.SQLIT
13745 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13746 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 lite3PagerAcquir
13747 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 e(. Pager *pPag
13748 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 er, /* The
13749 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68 pager open on th
1374a 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1374b 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 */. Pgno pgno,
1374c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
1374d 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 68 number to fetch
1374e 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a 70 */. DbPage **p
1374f 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69 pPage, /* Wri
13750 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 te a pointer to
13751 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f the page here */
13752 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 . int noContent
13753 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 /* Do not
13754 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 20 bother reading
13755 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73 content from dis
13756 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a k if true */.){.
13757 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 int rc;. PgHd
13758 72 20 2a 70 50 67 3b 0a 0a 20 20 61 73 73 65 72 r *pPg;.. asser
13759 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f t( assert_pager_
1375a 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b state(pPager) );
1375b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1375c 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 55 r->state>PAGER_U
1375d 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 NLOCK );.. if(
1375e 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 pgno==0 ){. r
1375f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
13760 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a RUPT_BKPT;. }..
13761 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 /* If the page
13762 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f r is in the erro
13763 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20 r state, return
13764 61 6e 20 65 72 72 6f 72 20 69 6d 6d 65 64 69 61 an error immedia
13765 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 4f 74 68 65 tely. . ** Othe
13766 72 77 69 73 65 2c 20 72 65 71 75 65 73 74 20 74 rwise, request t
13767 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 he page from the
13768 20 50 43 61 63 68 65 20 6c 61 79 65 72 2e 20 2a PCache layer. *
13769 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e /. if( pPager->
1376a 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f errCode!=SQLITE_
1376b 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 OK && pPager->er
1376c 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 rCode!=SQLITE_FU
1376d 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 LL ){. rc = p
1376e 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
1376f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
13770 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 = sqlite3PcacheF
13771 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 etch(pPager->pPC
13772 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 70 ache, pgno, 1, p
13773 70 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 pPage);. }.. i
13774 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13775 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 ){. /* Eithe
13776 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 r the call to sq
13777 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
13778 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 () returned an e
13779 72 72 6f 72 20 6f 72 20 74 68 65 0a 20 20 20 20 rror or the.
1377a 2a 2a 20 70 61 67 65 72 20 77 61 73 20 61 6c 72 ** pager was alr
1377b 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f eady in the erro
1377c 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 r-state when thi
1377d 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 s function was c
1377e 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 53 65 alled.. ** Se
1377f 74 20 70 50 67 20 74 6f 20 30 20 61 6e 64 20 6a t pPg to 0 and j
13780 75 6d 70 20 74 6f 20 74 68 65 20 65 78 63 65 70 ump to the excep
13781 74 69 6f 6e 20 68 61 6e 64 6c 65 72 2e 20 20 2a tion handler. *
13782 2f 0a 20 20 20 20 70 50 67 20 3d 20 30 3b 0a 20 /. pPg = 0;.
13783 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 goto pager_ac
13784 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 7d 0a 20 quire_err;. }.
13785 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 61 67 assert( (*ppPag
13786 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 e)->pgno==pgno )
13787 3b 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 ;. assert( (*pp
13788 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d 70 Page)->pPager==p
13789 50 61 67 65 72 20 7c 7c 20 28 2a 70 70 50 61 67 Pager || (*ppPag
1378a 65 29 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b e)->pPager==0 );
1378b 0a 0a 20 20 69 66 28 20 28 2a 70 70 50 61 67 65 .. if( (*ppPage
1378c 29 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 )->pPager ){.
1378d 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 /* In this case
1378e 20 74 68 65 20 70 63 61 63 68 65 20 61 6c 72 65 the pcache alre
1378f 61 64 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 ady contains an
13790 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 6f 70 79 initialized copy
13791 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 of. ** the p
13792 61 67 65 2e 20 52 65 74 75 72 6e 20 77 69 74 68 age. Return with
13793 6f 75 74 20 66 75 72 74 68 65 72 20 61 64 6f 2e out further ado.
13794 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
13795 20 70 67 6e 6f 3c 3d 50 41 47 45 52 5f 4d 41 58 pgno<=PAGER_MAX
13796 5f 50 47 4e 4f 20 26 26 20 70 67 6e 6f 21 3d 50 _PGNO && pgno!=P
13797 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
13798 67 65 72 29 20 29 3b 0a 20 20 20 20 50 41 47 45 ger) );. PAGE
13799 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e R_INCR(pPager->n
1379a 48 69 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e Hit);. return
1379b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d SQLITE_OK;.. }
1379c 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 else{. /* The
1379d 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73 pager cache has
1379e 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70 created a new p
1379f 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 age. Its content
137a0 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a needs to . *
137a1 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 * be initialized
137a2 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d . */. int nM
137a3 61 78 3b 0a 0a 20 20 20 20 50 41 47 45 52 5f 49 ax;.. PAGER_I
137a4 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73 NCR(pPager->nMis
137a5 73 29 3b 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 s);. pPg = *p
137a6 70 50 61 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e pPage;. pPg->
137a7 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b pPager = pPager;
137a8 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 .. /* The max
137a9 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72 imum page number
137aa 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e is 2^31. Return
137ab 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
137ac 69 66 20 61 20 70 61 67 65 0a 20 20 20 20 2a 2a if a page. **
137ad 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 number greater
137ae 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 74 68 than this, or th
137af 65 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67 e unused locking
137b0 2d 70 61 67 65 2c 20 69 73 20 72 65 71 75 65 73 -page, is reques
137b1 74 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ted. */. if(
137b2 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 pgno>PAGER_MAX_P
137b3 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 GNO || pgno==PAG
137b4 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
137b5 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d r) ){. rc =
137b6 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
137b7 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f BKPT;. goto
137b8 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 pager_acquire_e
137b9 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 rr;. }.. r
137ba 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
137bb 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 Pagecount(pPager
137bc 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20 69 66 , &nMax);. if
137bd 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
137be 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 ){. goto pa
137bf 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b ger_acquire_err;
137c0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
137c1 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c nMax<(int)pgno |
137c2 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f 43 6f 6e | MEMDB || noCon
137c3 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 69 66 tent ){. if
137c4 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d ( pgno>pPager->m
137c5 78 50 67 6e 6f 20 29 7b 0a 09 72 63 20 3d 20 53 xPgno ){..rc = S
137c6 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 09 67 6f 74 QLITE_FULL;..got
137c7 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f o pager_acquire_
137c8 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 err;. }.
137c9 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65 6e 74 if( noContent
137ca 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 ){. /* F
137cb 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20 74 68 ailure to set th
137cc 65 20 62 69 74 73 20 69 6e 20 74 68 65 20 49 6e e bits in the In
137cd 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65 63 74 Journal bit-vect
137ce 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20 ors is benign..
137cf 20 20 20 20 20 20 20 2a 2a 20 49 74 20 6d 65 72 ** It mer
137d0 65 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 77 ely means that w
137d1 65 20 6d 69 67 68 74 20 64 6f 20 73 6f 6d 65 20 e might do some
137d2 65 78 74 72 61 20 77 6f 72 6b 20 74 6f 20 6a 6f extra work to jo
137d3 75 72 6e 61 6c 20 61 20 0a 20 20 20 20 20 20 20 urnal a .
137d4 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f ** page that do
137d5 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 es not need to b
137d6 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 20 4e 65 e journaled. Ne
137d7 76 65 72 74 68 65 6c 65 73 73 2c 20 62 65 20 73 vertheless, be s
137d8 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ure . **
137d9 74 6f 20 74 65 73 74 20 74 68 65 20 63 61 73 65 to test the case
137da 20 77 68 65 72 65 20 61 20 6d 61 6c 6c 6f 63 20 where a malloc
137db 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 error occurs whi
137dc 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 73 65 74 le trying to set
137dd 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 62 . ** a b
137de 69 74 20 69 6e 20 61 20 62 69 74 20 76 65 63 74 it in a bit vect
137df 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 or.. */.
137e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 sqlite3Be
137e1 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 ginBenignMalloc(
137e2 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
137e3 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f gno<=pPager->dbO
137e4 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 rigSize ){.
137e5 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 TESTONLY( r
137e6 63 20 3d 20 29 20 73 71 6c 69 74 65 33 42 69 74 c = ) sqlite3Bit
137e7 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 vecSet(pPager->p
137e8 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e 6f 29 InJournal, pgno)
137e9 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74 ;. test
137ea 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 case( rc==SQLITE
137eb 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 _NOMEM );.
137ec 20 20 7d 0a 20 20 20 20 20 20 20 20 54 45 53 54 }. TEST
137ed 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 61 64 64 ONLY( rc = ) add
137ee 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 ToSavepointBitve
137ef 63 73 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 cs(pPager, pgno)
137f0 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
137f1 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e se( rc==SQLITE_N
137f2 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 OMEM );.
137f3 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
137f4 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
137f5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d }else{. m
137f6 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 emset(pPg->pData
137f7 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 , 0, pPager->pag
137f8 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a eSize);. }.
137f9 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
137fa 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 ZERO %p %d\n", p
137fb 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 Pager, pgno));.
137fc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
137fd 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50 61 assert( pPg->pPa
137fe 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 ger==pPager );.
137ff 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62 rc = readDb
13800 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 Page(pPg);.
13801 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13802 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
13803 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 to pager_acquire
13804 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 _err;. }.
13805 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
13806 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 E_CHECK_PAGES.
13807 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 pPg->pageHash
13808 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 = pager_pagehash
13809 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 (pPg);.#endif.
1380a 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }.. return SQLI
1380b 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 72 5f 61 63 TE_OK;..pager_ac
1380c 71 75 69 72 65 5f 65 72 72 3a 0a 20 20 61 73 73 quire_err:. ass
1380d 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
1380e 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 67 20 OK );. if( pPg
1380f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 ){. sqlite3Pc
13810 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b 0a 20 acheDrop(pPg);.
13811 20 7d 0a 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b }. pagerUnlock
13812 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29 IfUnused(pPager)
13813 3b 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30 ;.. *ppPage = 0
13814 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
13815 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 ../*.** Acquire
13816 61 20 70 61 67 65 20 69 66 20 69 74 20 69 73 20 a page if it is
13817 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 69 already in the i
13818 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 n-memory cache.
13819 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20 Do.** not read
1381a 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 64 69 the page from di
1381b 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f sk. Return a po
1381c 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 inter to the pag
1381d 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 74 68 e,.** or 0 if th
1381e 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e e page is not in
1381f 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 20 72 65 cache. Also, re
13820 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 0a 2a turn 0 if the .*
13821 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41 * pager is in PA
13822 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 GER_UNLOCK state
13823 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
13824 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a ion is called,.*
13825 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 67 65 * or if the page
13826 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 r is in an error
13827 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 state other tha
13828 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a n SQLITE_FULL..*
13829 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 *.** See also sq
1382a 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 2e lite3PagerGet().
1382b 20 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 The difference
1382c 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f between this ro
1382d 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 6c utine.** and sql
1382e 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 69 ite3PagerGet() i
1382f 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 77 69 s that _get() wi
13830 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 ll go to the dis
13831 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 69 6e k and read.** in
13832 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68 65 the page if the
13833 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
13834 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20 20 eady in cache.
13835 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 This routine.**
13836 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 returns NULL if
13837 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 the page is not
13838 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20 61 in cache or if a
13839 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 disk I/O error
1383a 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61 70 .** has ever hap
1383b 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pened..*/.SQLITE
1383c 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20 _PRIVATE DbPage
1383d 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f *sqlite3PagerLoo
1383e 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 kup(Pager *pPage
1383f 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 r, Pgno pgno){.
13840 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30 3b PgHdr *pPg = 0;
13841 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13842 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 r!=0 );. assert
13843 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 20 61 ( pgno!=0 );. a
13844 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 ssert( pPager->p
13845 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 PCache!=0 );. a
13846 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
13847 74 61 74 65 20 3e 20 50 41 47 45 52 5f 55 4e 4c tate > PAGER_UNL
13848 4f 43 4b 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 OCK );. sqlite3
13849 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67 PcacheFetch(pPag
1384a 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e er->pPCache, pgn
1384b 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 72 o, 0, &pPg);. r
1384c 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a eturn pPg;.}../*
1384d 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 61 .** Release a pa
1384e 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a ge reference..**
1384f 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65 .** If the numbe
13850 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
13851 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f 70 to the page drop
13852 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 to zero, then t
13853 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64 he.** page is ad
13854 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c ded to the LRU l
13855 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72 ist. When all r
13856 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c eferences to all
13857 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 65 pages.** are re
13858 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61 leased, a rollba
13859 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68 ck occurs and th
1385a 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 e lock on the da
1385b 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d tabase is.** rem
1385c 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oved..*/.SQLITE_
1385d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1385e 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 ite3PagerUnref(D
1385f 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 69 bPage *pPg){. i
13860 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 61 f( pPg ){. Pa
13861 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 ger *pPager = pP
13862 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 73 g->pPager;. s
13863 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 qlite3PcacheRele
13864 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 61 ase(pPg);. pa
13865 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 gerUnlockIfUnuse
13866 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d d(pPager);. }.}
13867 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d ../*.** If the m
13868 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
13869 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
1386a 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 72 65 n opened, ensure
1386b 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 75 62 that the.** sub
1386c 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 -journal file is
1386d 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 74 68 open too. If th
1386e 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 e main journal i
1386f 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 74 s not open,.** t
13870 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
13871 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 a no-op..**.** S
13872 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
13873 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69 rned if everythi
13874 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69 6e ng goes accordin
13875 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 41 g to plan. .** A
13876 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 n SQLITE_IOERR_X
13877 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 XX error code is
13878 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 63 returned if a c
13879 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 all to .** sqlit
1387a 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 6c 73 e3OsOpen() fails
1387b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1387c 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 50 openSubJournal(P
1387d 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
1387e 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1387f 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 _OK;. if( isOpe
13880 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 n(pPager->jfd) &
13881 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 & !isOpen(pPager
13882 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 ->sjfd) ){. i
13883 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
13884 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
13885 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
13886 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a || pPager->subj
13887 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20 InMemory ){.
13888 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 sqlite3MemJour
13889 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e nalOpen(pPager->
1388a 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 sjfd);. }else
1388b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
1388c 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65 erOpentemp(pPage
1388d 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c r, pPager->sjfd,
1388e 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 SQLITE_OPEN_SUB
1388f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a JOURNAL);. }.
13890 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13891 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
13892 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
13893 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f d at the start o
13894 66 20 65 76 65 72 79 20 77 72 69 74 65 20 74 72 f every write tr
13895 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 ansaction..** Th
13896 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 ere must already
13897 20 62 65 20 61 20 52 45 53 45 52 56 45 44 20 6f be a RESERVED o
13898 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b r EXCLUSIVE lock
13899 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1389a 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 .** file when t
1389b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
1389c 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 alled..**.** Ope
1389d 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
1389e 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 le for pager pPa
1389f 67 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20 ger and write a
138a0 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a journal header.*
138a1 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f * to the start o
138a2 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61 f it. If there a
138a3 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f re active savepo
138a4 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73 ints, open the s
138a5 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73 ub-journal.** as
138a6 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 well. This func
138a7 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 tion is only use
138a8 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e d when the journ
138a9 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67 al file is being
138aa 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77 .** opened to w
138ab 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 rite a rollback
138ac 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 61 log for a transa
138ad 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74 ction. It is not
138ae 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f used .** when o
138af 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75 pening a hot jou
138b0 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c rnal file to rol
138b1 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a l it back..**.**
138b2 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
138b3 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20 file is already
138b4 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 20 open (as it may
138b5 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 be in exclusive
138b6 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74 mode),.** then t
138b7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73 his function jus
138b8 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 6e t writes a journ
138b9 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 al header to the
138ba 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a start of the.**
138bb 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 69 already open fi
138bc 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 le. .**.** Wheth
138bd 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f er or not the jo
138be 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 urnal file is op
138bf 65 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e ened by this fun
138c0 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61 ction, the.** Pa
138c1 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 ger.pInJournal b
138c2 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20 itvec structure
138c3 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a is allocated..**
138c4 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
138c5 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 E_OK if everythi
138c6 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c ng is successful
138c7 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 . Otherwise, ret
138c8 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e urn .** SQLITE_N
138c9 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 65 OMEM if the atte
138ca 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 mpt to allocate
138cb 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c Pager.pInJournal
138cc 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61 fails, or .** a
138cd 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
138ce 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 if opening or wr
138cf 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 iting the journa
138d0 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f l file fails..*/
138d1 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 .static int page
138d2 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 r_open_journal(P
138d3 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
138d4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
138d5 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
138d6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
138d7 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
138d8 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f sqlite3_vfs * co
138d9 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 nst pVfs = pPage
138da 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f r->pVfs; /* Lo
138db 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 73 cal cache of vfs
138dc 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 pointer */.. a
138dd 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
138de 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 tate>=PAGER_RESE
138df 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74 RVED );. assert
138e0 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 ( pPager->useJou
138e1 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 rnal );. assert
138e2 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
138e3 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 lMode!=PAGER_JOU
138e4 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a RNALMODE_OFF );.
138e5 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
138e6 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
138e7 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 61 6c );. . /* If al
138e8 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 ready in the err
138e9 6f 72 20 73 74 61 74 65 2c 20 74 68 69 73 20 66 or state, this f
138ea 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d unction is a no-
138eb 6f 70 2e 20 20 42 75 74 20 6f 6e 0a 20 20 2a 2a op. But on. **
138ec 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
138ed 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
138ee 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 66 never called if
138ef 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 we are already
138f0 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 in. ** an error
138f1 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28 state. */. if(
138f2 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 NEVER(pPager->e
138f3 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e rrCode) ) return
138f4 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
138f5 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 ;.. /* TODO: Is
138f6 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73 73 69 it really possi
138f7 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72 65 20 ble to get here
138f8 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c 69 64 with dbSizeValid
138f9 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20 20 2a ==0? If not,. *
138fa 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 50 61 * the call to Pa
138fb 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 63 gerPagecount() c
138fc 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e 0a 20 an be removed..
138fd 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 */. testcase(
138fe 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 pPager->dbSizeVa
138ff 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 lid==0 );. sqli
13900 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
13901 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 t(pPager, 0);..
13902 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
13903 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 nal = sqlite3Bit
13904 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65 72 vecCreate(pPager
13905 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 ->dbSize);. if(
13906 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
13907 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 nal==0 ){. re
13908 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
13909 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 M;. }.. /* Ope
1390a 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
1390b 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 le if it is not
1390c 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 2a 2f already open. */
1390d 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 . if( !isOpen(p
1390e 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 Pager->jfd) ){.
1390f 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a if( pPager->j
13910 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
13911 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
13912 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 73 71 MORY ){. sq
13913 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f lite3MemJournalO
13914 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
13915 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
13916 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 const int fla
13917 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 20 20 gs =
13918 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 6c /* VFS fl
13919 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f 75 72 ags to open jour
1391a 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 nal file */.
1391b 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
1391c 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 READWRITE|SQLITE
1391d 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 _OPEN_CREATE|.
1391e 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 74 (pPager->t
1391f 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 20 20 empFile ? .
13920 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 (SQLITE_OPE
13921 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 7c N_DELETEONCLOSE|
13922 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 SQLITE_OPEN_TEMP
13923 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 20 20 _JOURNAL):.
13924 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 (SQLITE_OPE
13925 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a N_MAIN_JOURNAL).
13926 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66 64 65 );.#ifde
13927 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
13928 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 ATOMIC_WRITE.
13929 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a rc = sqlite3J
1392a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 20 ournalOpen(.
1392b 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61 67 pVfs, pPag
1392c 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 er->zJournal, pP
1392d 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 ager->jfd, flags
1392e 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 , jrnlBufferSize
1392f 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20 29 (pPager). )
13930 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 72 63 ;.#else. rc
13931 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
13932 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
13933 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d Journal, pPager-
13934 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b >jfd, flags, 0);
13935 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 .#endif. }.
13936 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 assert( rc!=SQ
13937 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 LITE_OK || isOpe
13938 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 n(pPager->jfd) )
13939 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72 69 ;. }... /* Wri
1393a 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 te the first jou
1393b 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 rnal header to t
1393c 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
1393d 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a 20 74 and open . ** t
1393e 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 he sub-journal i
1393f 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a f necessary.. *
13940 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
13941 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 TE_OK ){. /*
13942 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66 20 61 TODO: Check if a
13943 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 65 20 ll of these are
13944 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64 2e really required.
13945 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e */. pPager->
13946 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50 61 dbOrigSize = pPa
13947 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 ger->dbSize;.
13948 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13949 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 Started = 0;.
1394a 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
1394b 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 c = 0;. pPage
1394c 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 r->nRec = 0;.
1394d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
1394e 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 Off = 0;. pPa
1394f 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d ger->setMaster =
13950 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
13951 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a journalHdr = 0;.
13952 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f rc = writeJo
13953 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 urnalHdr(pPager)
13954 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d ;. }. if( rc==
13955 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 SQLITE_OK && pPa
13956 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 ger->nSavepoint
13957 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e ){. rc = open
13958 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 SubJournal(pPage
13959 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 r);. }.. if( r
1395a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1395b 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
1395c 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d cDestroy(pPager-
1395d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 >pInJournal);.
1395e 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 pPager->pInJou
1395f 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 rnal = 0;. }.
13960 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
13961 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 69 74 .** Begin a writ
13962 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e e-transaction on
13963 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 the specified p
13964 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 ager object. If
13965 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e a .** write-tran
13966 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 saction has alre
13967 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c ady been opened,
13968 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13969 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
1396a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 61 If the exFlag a
1396b 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 rgument is false
1396c 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 61 , then acquire a
1396d 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52 56 t least a RESERV
1396e 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68 ED.** lock on th
1396f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
13970 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 72 If exFlag is tr
13971 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 ue, then acquire
13972 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 6e 20 at least.** an
13973 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
13974 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
13975 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 s already held,
13976 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 66 no locking .** f
13977 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 62 65 unctions need be
13978 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 called..**.** I
13979 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 f this is not a
1397a 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 6e 2d temporary or in-
1397b 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e 64 2c memory file and,
1397c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1397d 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 e is .** opened
1397e 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65 if it has not be
1397f 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f 72 20 en already. For
13980 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
13981 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 0a 2a , the opening .*
13982 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * of the journal
13983 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 65 file is deferre
13984 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 73 d until there is
13985 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65 64 20 an actual need
13986 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74 6f 20 to .** write to
13987 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 4f 44 the journal. TOD
13988 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20 74 65 O: Why handle te
13989 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 64 69 mporary files di
1398a 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a 2a 2a fferently?.**.**
1398b 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 If the journal
1398c 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 28 file is opened (
1398d 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 or if it is alre
1398e 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65 6e 20 ady open), then
1398f 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 a.** journal-hea
13990 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 74 der is written t
13991 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69 o the start of i
13992 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 t..**.** If the
13993 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 subjInMemory arg
13994 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 ument is non-zer
13995 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75 62 2d o, then any sub-
13996 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64 0a 2a journal opened.*
13997 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72 * within this tr
13998 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 ansaction will b
13999 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e 20 69 e opened as an i
1399a 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e 20 54 n-memory file. T
1399b 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20 65 66 his.** has no ef
1399c 66 65 63 74 20 69 66 20 74 68 65 20 73 75 62 2d fect if the sub-
1399d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 65 61 journal is alrea
1399e 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20 69 74 dy opened (as it
1399f 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a 2a 20 may be when.**
139a0 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 running in exclu
139a1 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 69 66 sive mode) or if
139a2 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
139a3 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 does not requir
139a4 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e e a.** sub-journ
139a5 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 6a 49 al. If the subjI
139a6 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e 74 nMemory argument
139a7 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 is zero, then a
139a8 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 73 ny required.** s
139a9 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 69 6d ub-journal is im
139aa 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d 65 6d plemented in-mem
139ab 6f 72 79 20 69 66 20 70 50 61 67 65 72 20 69 73 ory if pPager is
139ac 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
139ad 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72 20 75 tabase, .** or u
139ae 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 72 79 sing a temporary
139af 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73 65 2e file otherwise.
139b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
139b1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
139b2 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 20 2a gerBegin(Pager *
139b3 70 50 61 67 65 72 2c 20 69 6e 74 20 65 78 46 6c pPager, int exFl
139b4 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 6e 4d 65 ag, int subjInMe
139b5 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 72 63 20 mory){. int rc
139b6 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 = SQLITE_OK;. a
139b7 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
139b8 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f tate!=PAGER_UNLO
139b9 43 4b 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e CK );. pPager->
139ba 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 3d 20 28 subjInMemory = (
139bb 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b u8)subjInMemory;
139bc 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
139bd 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 tate==PAGER_SHAR
139be 45 44 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ED ){. assert
139bf 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ( pPager->pInJou
139c0 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61 rnal==0 );. a
139c1 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26 ssert( !MEMDB &&
139c2 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 !pPager->tempFi
139c3 6c 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 le );.. /* Ob
139c4 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 tain a RESERVED
139c5 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
139c6 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 base file. If th
139c7 65 20 65 78 46 6c 61 67 20 70 61 72 61 6d 65 74 e exFlag paramet
139c8 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 74 72 75 er. ** is tru
139c9 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69 61 74 e, then immediat
139ca 65 6c 79 20 75 70 67 72 61 64 65 20 74 68 69 73 ely upgrade this
139cb 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 to an EXCLUSIVE
139cc 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20 20 2a lock. The. *
139cd 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 * busy-handler c
139ce 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65 20 75 allback can be u
139cf 73 65 64 20 77 68 65 6e 20 75 70 67 72 61 64 69 sed when upgradi
139d0 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c 55 53 ng to the EXCLUS
139d1 49 56 45 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 2c IVE. ** lock,
139d2 20 62 75 74 20 6e 6f 74 20 77 68 65 6e 20 6f 62 but not when ob
139d3 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45 53 45 taining the RESE
139d4 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a RVED lock.. *
139d5 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
139d6 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d e3OsLock(pPager-
139d7 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f >fd, RESERVED_LO
139d8 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d CK);. if( rc=
139d9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
139da 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
139db 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56 e = PAGER_RESERV
139dc 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20 65 78 ED;. if( ex
139dd 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 Flag ){.
139de 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f rc = pager_wait_
139df 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 on_lock(pPager,
139e0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b EXCLUSIVE_LOCK);
139e1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
139e2 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 /* If the re
139e3 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 77 65 72 quired locks wer
139e4 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f e successfully o
139e5 62 74 61 69 6e 65 64 2c 20 6f 70 65 6e 20 74 68 btained, open th
139e6 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a e journal. **
139e7 20 66 69 6c 65 20 61 6e 64 20 77 72 69 74 65 20 file and write
139e8 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 the first journa
139e9 6c 2d 68 65 61 64 65 72 20 74 6f 20 69 74 2e 0a l-header to it..
139ea 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 */. if( r
139eb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
139ec 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
139ed 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode!=PAGER_JOURN
139ee 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 ALMODE_OFF ){.
139ef 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f rc = pager_o
139f0 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 pen_journal(pPag
139f1 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c er);. }. }el
139f2 73 65 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 se if( isOpen(pP
139f3 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 ager->jfd) && pP
139f4 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
139f5 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ==0 ){. /* Th
139f6 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 is happens when
139f7 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e the pager was in
139f8 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 exclusive-acces
139f9 73 20 6d 6f 64 65 20 74 68 65 20 6c 61 73 74 0a s mode the last.
139fa 20 20 20 20 2a 2a 20 74 69 6d 65 20 61 20 28 72 ** time a (r
139fb 65 61 64 20 6f 72 20 77 72 69 74 65 29 20 74 72 ead or write) tr
139fc 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 73 75 ansaction was su
139fd 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c ccessfully concl
139fe 75 64 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 uded. ** by t
139ff 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 his connection.
13a00 49 6e 73 74 65 61 64 20 6f 66 20 64 65 6c 65 74 Instead of delet
13a01 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ing the journal
13a02 66 69 6c 65 20 69 74 20 77 61 73 20 0a 20 20 20 file it was .
13a03 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e ** kept open an
13a04 64 20 65 69 74 68 65 72 20 77 61 73 20 74 72 75 d either was tru
13a05 6e 63 61 74 65 64 20 74 6f 20 30 20 62 79 74 65 ncated to 0 byte
13a06 73 20 6f 72 20 69 74 73 20 68 65 61 64 65 72 20 s or its header
13a07 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77 was. ** overw
13a08 72 69 74 74 65 6e 20 77 69 74 68 20 7a 65 72 6f ritten with zero
13a09 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 s.. */. as
13a0a 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52 sert( pPager->nR
13a0b 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 ec==0 );. ass
13a0c 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 4f ert( pPager->dbO
13a0d 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 rigSize==0 );.
13a0e 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13a0f 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
13a10 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 );. rc = page
13a11 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 r_open_journal(p
13a12 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 50 Pager);. }.. P
13a13 41 47 45 52 54 52 41 43 45 28 28 22 54 52 41 4e AGERTRACE(("TRAN
13a14 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50 SACTION %d\n", P
13a15 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 AGERID(pPager)))
13a16 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f ;. assert( !isO
13a17 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
13a18 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
13a19 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 63 21 3d nalOff>0 || rc!=
13a1a 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 SQLITE_OK );. i
13a1b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13a1c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
13a1d 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 !pPager->dbModif
13a1e 69 65 64 20 29 3b 0a 20 20 20 20 2f 2a 20 49 67 ied );. /* Ig
13a1f 6e 6f 72 65 20 61 6e 79 20 49 4f 20 65 72 72 6f nore any IO erro
13a20 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 69 r that occurs wi
13a21 74 68 69 6e 20 70 61 67 65 72 5f 65 6e 64 5f 74 thin pager_end_t
13a22 72 61 6e 73 61 63 74 69 6f 6e 28 29 2e 20 54 68 ransaction(). Th
13a23 65 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 e. ** purpose
13a24 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73 of this call is
13a25 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 69 6e to reset the in
13a26 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 ternal state of
13a27 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a the pager. **
13a28 20 73 75 62 2d 73 79 73 74 65 6d 2e 20 49 74 20 sub-system. It
13a29 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 69 doesn't matter i
13a2a 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 f the journal-fi
13a2b 6c 65 20 69 73 20 6e 6f 74 20 70 72 6f 70 65 72 le is not proper
13a2c 6c 79 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 ly. ** finali
13a2d 7a 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e zed at this poin
13a2e 74 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 6e t (since it is n
13a2f 6f 74 20 61 20 76 61 6c 69 64 20 6a 6f 75 72 6e ot a valid journ
13a30 61 6c 20 66 69 6c 65 20 61 6e 79 77 61 79 29 2e al file anyway).
13a31 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 61 67 65 . */. page
13a32 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
13a33 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 n(pPager, 0);.
13a34 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
13a35 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73 ../*.** Mark a s
13a36 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 20 ingle data page
13a37 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 as writeable. Th
13a38 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 e page is writte
13a39 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d n into the .** m
13a3a 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 ain journal or s
13a3b 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65 ub-journal as re
13a3c 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 70 quired. If the p
13a3d 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 age is written i
13a3e 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 nto.** one of th
13a3f 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20 e journals, the
13a40 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 corresponding bi
13a41 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 t is set in the
13a42 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 .** Pager.pInJou
13a43 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 20 rnal bitvec and
13a44 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 the PagerSavepoi
13a45 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 nt.pInSavepoint
13a46 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e bitvecs.** of an
13a47 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 y open savepoint
13a48 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 s as appropriate
13a49 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
13a4a 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 64 pager_write(PgHd
13a4b 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 20 r *pPg){. void
13a4c 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 *pData = pPg->pD
13a4d 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 ata;. Pager *pP
13a4e 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
13a4f 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 er;. int rc = S
13a50 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 QLITE_OK;.. /*
13a51 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
13a52 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c 65 73 not called unles
13a53 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 s a transaction
13a54 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
13a55 0a 20 20 2a 2a 20 73 74 61 72 74 65 64 2e 0a 20 . ** started..
13a56 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
13a57 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 ager->state>=PAG
13a58 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 0a ER_RESERVED );..
13a59 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 /* If an error
13a5a 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 69 6f has been previo
13a5b 75 73 6c 79 20 64 65 74 65 63 74 65 64 2c 20 77 usly detected, w
13a5c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a e should not be.
13a5d 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 ** calling thi
13a5e 73 20 72 6f 75 74 69 6e 65 2e 20 20 52 65 70 65 s routine. Repe
13a5f 61 74 20 74 68 65 20 65 72 72 6f 72 20 66 6f 72 at the error for
13a60 20 72 6f 62 75 73 74 6e 65 73 73 2e 0a 20 20 2a robustness.. *
13a61 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 /. if( NEVER(pP
13a62 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 ager->errCode) )
13a63 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
13a64 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 >errCode;.. /*
13a65 48 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 Higher-level rou
13a66 74 69 6e 65 73 20 6e 65 76 65 72 20 63 61 6c 6c tines never call
13a67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
13a68 66 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f f database is no
13a69 74 0a 20 20 2a 2a 20 77 72 69 74 61 62 6c 65 2e t. ** writable.
13a6a 20 20 42 75 74 20 63 68 65 63 6b 20 61 6e 79 77 But check anyw
13a6b 61 79 2c 20 6a 75 73 74 20 66 6f 72 20 72 6f 62 ay, just for rob
13a6c 75 73 74 6e 65 73 73 2e 20 2a 2f 0a 20 20 69 66 ustness. */. if
13a6d 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e ( NEVER(pPager->
13a6e 72 65 61 64 4f 6e 6c 79 29 20 29 20 72 65 74 75 readOnly) ) retu
13a6f 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a rn SQLITE_PERM;.
13a70 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67 . assert( !pPag
13a71 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 3b er->setMaster );
13a72 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45 28 70 .. CHECK_PAGE(p
13a73 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 Pg);.. /* Mark
13a74 74 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74 the page as dirt
13a75 79 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 y. If the page
13a76 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
13a77 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 74 6f written. ** to
13a78 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65 the journal the
13a79 6e 20 77 65 20 63 61 6e 20 72 65 74 75 72 6e 20 n we can return
13a7a 72 69 67 68 74 20 61 77 61 79 2e 0a 20 20 2a 2f right away.. */
13a7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
13a7c 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a MakeDirty(pPg);.
13a7d 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f 75 72 if( pageInJour
13a7e 6e 61 6c 28 70 50 67 29 20 26 26 20 21 73 75 62 nal(pPg) && !sub
13a7f 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 jRequiresPage(pP
13a80 67 29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 g) ){. pPager
13a81 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 ->dbModified = 1
13a82 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 ;. }else{..
13a83 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69 /* If we get thi
13a84 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 s far, it means
13a85 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65 that the page ne
13a86 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a eds to be. **
13a87 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
13a88 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 transaction jour
13a89 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b 65 63 6b nal or the ckeck
13a8a 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 point journal.
13a8b 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e 0a 20 20 ** or both..
13a8c 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 48 69 67 68 **. ** High
13a8d 65 72 20 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 er level routine
13a8e 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c s should have al
13a8f 72 65 61 64 79 20 73 74 61 72 74 65 64 20 61 20 ready started a
13a90 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 20 transaction,.
13a91 20 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20 ** which means
13a92 74 68 65 79 20 68 61 76 65 20 61 63 71 75 69 72 they have acquir
13a93 65 64 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 ed the necessary
13a94 20 6c 6f 63 6b 73 20 61 6e 64 20 6f 70 65 6e 65 locks and opene
13a95 64 0a 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62 d. ** a rollb
13a96 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 6f ack journal. Do
13a97 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61 uble-check to ma
13a98 6b 65 73 20 73 75 72 65 20 74 68 69 73 20 69 73 kes sure this is
13a99 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20 2a the case.. *
13a9a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
13a9b 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 61 e3PagerBegin(pPa
13a9c 67 65 72 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e ger, 0, pPager->
13a9d 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 subjInMemory);.
13a9e 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 if( NEVER(rc!
13a9f 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 =SQLITE_OK) ){.
13aa0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
13aa1 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 }. if( !i
13aa2 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
13aa3 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f d) && pPager->jo
13aa4 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 urnalMode!=PAGER
13aa5 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
13aa6 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
13aa7 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 ( pPager->useJou
13aa8 72 6e 61 6c 20 29 3b 0a 20 20 20 20 20 20 72 63 rnal );. rc
13aa9 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f = pager_open_jo
13aaa 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 urnal(pPager);.
13aab 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13aac 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 ITE_OK ) return
13aad 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 rc;. }. pP
13aae 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
13aaf 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 = 1;. . /*
13ab0 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 The transaction
13ab1 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73 journal now exis
13ab2 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 61 ts and we have a
13ab3 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e 0a RESERVED or an.
13ab4 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 ** EXCLUSIVE
13ab5 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69 lock on the mai
13ab6 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e n database file.
13ab7 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 Write the curr
13ab8 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 20 ent page to.
13ab9 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ** the transacti
13aba 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74 on journal if it
13abb 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c is not there al
13abc 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 ready.. */.
13abd 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a 6f 75 if( !pageInJou
13abe 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69 73 4f rnal(pPg) && isO
13abf 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
13ac0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ){. if( pP
13ac1 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d g->pgno<=pPager-
13ac2 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 >dbOrigSize ){.
13ac3 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75 6d u32 cksum
13ac4 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a ;. char *
13ac5 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 20 20 pData2;..
13ac6 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 6e 65 /* We should ne
13ac7 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 68 65 ver write to the
13ac8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 journal file th
13ac9 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 20 20 e page that.
13aca 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 ** contains
13acb 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 the database loc
13acc 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ks. The followi
13acd 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 66 69 ng assert verifi
13ace 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 es. ** th
13acf 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 2a 2f at we do not. */
13ad0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
13ad1 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 45 pPg->pgno!=PAGE
13ad2 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
13ad3 29 20 29 3b 0a 20 20 20 20 20 20 20 20 43 4f 44 ) );. COD
13ad4 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 61 74 EC2(pPager, pDat
13ad5 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c a, pPg->pgno, 7,
13ad6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
13ad7 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 OMEM, pData2);.
13ad8 20 20 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 cksum = p
13ad9 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 ager_cksum(pPage
13ada 72 2c 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b r, (u8*)pData2);
13adb 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 . rc = wr
13adc 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 ite32bits(pPager
13add 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a ->jfd, pPager->j
13ade 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e ournalOff, pPg->
13adf 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 pgno);. i
13ae0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13ae1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
13ae2 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
13ae3 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 e(pPager->jfd, p
13ae4 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 Data2, pPager->p
13ae5 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 ageSize,.
13ae6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13ae7 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
13ae8 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a ournalOff + 4);.
13ae9 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
13aea 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 ->journalOff +=
13aeb 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
13aec 2b 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 +4;. }.
13aed 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
13aee 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13aef 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 rc = write3
13af0 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 2bits(pPager->jf
13af1 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e d, pPager->journ
13af2 61 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 alOff, cksum);.
13af3 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
13af4 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 >journalOff += 4
13af5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
13af6 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f IOTRACE(("JO
13af7 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 UT %p %d %lld %d
13af8 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 \n", pPager, pPg
13af9 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 ->pgno, .
13afa 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 pPager
13afb 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 ->journalOff, pP
13afc 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 ager->pageSize))
13afd 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 5f ;. PAGER_
13afe 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 INCR(sqlite3_pag
13aff 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 er_writej_count)
13b00 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 ;. PAGERT
13b01 52 41 43 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 RACE(("JOURNAL %
13b02 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 d page %d needSy
13b03 6e 63 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 nc=%d hash(%08x)
13b04 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
13b05 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 PAGERID(pPager
13b06 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 ), pPg->pgno, .
13b07 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50 ((pP
13b08 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e g->flags&PGHDR_N
13b09 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 EED_SYNC)?1:0),
13b0a 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
13b0b 50 67 29 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 Pg)));..
13b0c 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f /* Even if an IO
13b0d 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 or diskfull err
13b0e 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c or occurred whil
13b0f 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 e journalling th
13b10 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 e. ** pag
13b11 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 e in the block a
13b12 62 6f 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65 bove, set the ne
13b13 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 ed-sync flag for
13b14 20 74 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20 the page..
13b15 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c ** Otherwise,
13b16 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 when the transa
13b17 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 ction is rolled
13b18 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 back, the logic
13b19 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c in. ** pl
13b1a 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 ayback_one_page(
13b1b 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 ) will think tha
13b1c 74 20 74 68 65 20 70 61 67 65 20 6e 65 65 64 73 t the page needs
13b1d 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a to be restored.
13b1e 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 ** in th
13b1f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
13b20 20 41 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72 And if an IO er
13b21 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
13b22 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 doing so,.
13b23 20 20 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 ** then corru
13b24 70 74 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 ption may follow
13b25 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
13b26 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 if( !pPager
13b27 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 ->noSync ){.
13b28 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 pPg->flags
13b29 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 |= PGHDR_NEED_S
13b2a 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70 YNC;. p
13b2b 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
13b2c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a = 1;. }..
13b2d 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 /* An er
13b2e 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 ror has occurred
13b2f 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 writing to the
13b30 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 journal file. Th
13b31 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 e . ** tr
13b32 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 ansaction will b
13b33 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 e rolled back by
13b34 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65 the layer above
13b35 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
13b36 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13b37 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13b38 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
13b39 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
13b3a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b pPager->nRec++
13b3b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
13b3c 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 ( pPager->pInJou
13b3d 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 rnal!=0 );.
13b3e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
13b3f 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d itvecSet(pPager-
13b40 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 >pInJournal, pPg
13b41 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 ->pgno);.
13b42 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 testcase( rc==S
13b43 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
13b44 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 assert( r
13b45 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
13b46 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
13b47 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 7c );. rc |
13b48 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 = addToSavepoint
13b49 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 Bitvecs(pPager,
13b4a 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 pPg->pgno);.
13b4b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
13b4c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
13b4d 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
13b4e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
13b4f 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
13b50 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
13b51 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13b52 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e if( !pPager->
13b53 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26 journalStarted &
13b54 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e & !pPager->noSyn
13b55 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 c ){. p
13b56 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 Pg->flags |= PGH
13b57 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
13b58 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
13b59 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
13b5a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13b5b 50 41 47 45 52 54 52 41 43 45 28 28 22 41 50 50 PAGERTRACE(("APP
13b5c 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e END %d page %d n
13b5d 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 eedSync=%d\n",.
13b5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
13b5f 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 AGERID(pPager),
13b60 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 pPg->pgno,.
13b61 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d ((pPg-
13b62 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
13b63 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a D_SYNC)?1:0)));.
13b64 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
13b65 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 . /* If the s
13b66 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c tatement journal
13b67 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 is open and the
13b68 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 page is not in
13b69 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 it,. ** then
13b6a 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e write the curren
13b6b 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 73 74 t page to the st
13b6c 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e atement journal.
13b6d 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20 Note that.
13b6e 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ** the statement
13b6f 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 journal format
13b70 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65 differs from the
13b71 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 standard journa
13b72 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 l format. **
13b73 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73 in that it omits
13b74 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61 the checksums a
13b75 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20 nd the header..
13b76 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 */. if( su
13b77 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 bjRequiresPage(p
13b78 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Pg) ){. rc
13b79 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 = subjournalPage
13b7a 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d (pPg);. }. }
13b7b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 .. /* Update th
13b7c 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
13b7d 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f and return.. */
13b7e 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13b7f 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
13b80 53 48 41 52 45 44 20 29 3b 0a 20 20 69 66 28 20 SHARED );. if(
13b81 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 pPager->dbSize<p
13b82 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 Pg->pgno ){.
13b83 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
13b84 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a pPg->pgno;. }.
13b85 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
13b86 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 /*.** Mark a dat
13b87 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61 a page as writea
13b88 62 6c 65 2e 20 54 68 69 73 20 72 6f 75 74 69 6e ble. This routin
13b89 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
13b8a 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 before .** maki
13b8b 6e 67 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 ng changes to a
13b8c 70 61 67 65 2e 20 54 68 65 20 63 61 6c 6c 65 72 page. The caller
13b8d 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 must check the
13b8e 72 65 74 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a return value .**
13b8f 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
13b90 6e 20 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c n and be careful
13b91 20 6e 6f 74 20 74 6f 20 63 68 61 6e 67 65 20 61 not to change a
13b92 6e 79 20 70 61 67 65 20 64 61 74 61 20 75 6e 6c ny page data unl
13b93 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 ess .** this rou
13b94 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c tine returns SQL
13b95 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 ITE_OK..**.** Th
13b96 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 e difference bet
13b97 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 ween this functi
13b98 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69 on and pager_wri
13b99 74 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69 te() is that thi
13b9a 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c s.** function al
13b9b 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 so deals with th
13b9c 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 e special case w
13b9d 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 here 2 or more p
13b9e 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 ages.** fit on a
13b9f 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 single disk sec
13ba0 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73 tor. In this cas
13ba1 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e e all co-residen
13ba2 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 t pages.** must
13ba3 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 have been writte
13ba4 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
13ba5 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74 file before ret
13ba6 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 urning..**.** If
13ba7 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
13ba8 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f , SQLITE_NOMEM o
13ba9 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f r an IO error co
13baa 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a de is returned.*
13bab 2a 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 * as appropriate
13bac 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
13bad 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 ITE_OK..*/.SQLIT
13bae 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
13baf 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
13bb0 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 29 DbPage *pDbPage)
13bb1 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
13bb2 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 ITE_OK;.. PgHdr
13bb3 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b *pPg = pDbPage;
13bb4 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
13bb5 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
13bb6 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 Pgno nPagePerS
13bb7 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d ector = (pPager-
13bb8 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 >sectorSize/pPag
13bb9 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a er->pageSize);..
13bba 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 53 65 if( nPagePerSe
13bbb 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 ctor>1 ){. Pg
13bbc 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 no nPageCount;
13bbd 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
13bbe 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
13bbf 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c in database fil
13bc0 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 e */. Pgno pg
13bc1 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
13bc2 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 /* First page
13bc3 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 of the sector p
13bc4 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e Pg is located on
13bc5 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 . */. int nPa
13bc6 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
13bc7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
13bc8 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 20 61 pages starting a
13bc9 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c t pg1 to journal
13bca 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 */. int ii;
13bcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13bcc 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
13bcd 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65 r */. int nee
13bce 64 53 79 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 dSync = 0;
13bcf 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e /* True if an
13bd0 79 20 70 61 67 65 20 68 61 73 20 50 47 48 44 52 y page has PGHDR
13bd1 5f 4e 45 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 _NEED_SYNC */..
13bd2 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f /* Set the do
13bd3 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 74 6f 20 NotSync flag to
13bd4 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 1. This is becau
13bd5 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c se we cannot all
13bd6 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 ow a journal.
13bd7 20 2a 2a 20 68 65 61 64 65 72 20 74 6f 20 62 65 ** header to be
13bd8 20 77 72 69 74 74 65 6e 20 62 65 74 77 65 65 6e written between
13bd9 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75 72 6e the pages journ
13bda 61 6c 65 64 20 62 79 20 74 68 69 73 20 66 75 6e aled by this fun
13bdb 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ction.. */.
13bdc 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 assert( !MEMDB
13bdd 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
13bde 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e pPager->doNotSyn
13bdf 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 67 c==0 );. pPag
13be0 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 er->doNotSync =
13be1 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 1;.. /* This
13be2 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20 74 68 trick assumes th
13be3 61 74 20 62 6f 74 68 20 74 68 65 20 70 61 67 65 at both the page
13be4 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 -size and sector
13be5 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a -size are. **
13be6 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 65 an integer powe
13be7 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74 73 20 r of 2. It sets
13be8 76 61 72 69 61 62 6c 65 20 70 67 31 20 74 6f 20 variable pg1 to
13be9 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20 the identifier.
13bea 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 ** of the fir
13beb 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73 st page of the s
13bec 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 ector pPg is loc
13bed 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a ated on.. */.
13bee 20 20 20 20 70 67 31 20 3d 20 28 28 70 50 67 2d pg1 = ((pPg-
13bef 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61 >pgno-1) & ~(nPa
13bf0 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 29 20 gePerSector-1))
13bf1 2b 20 31 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 + 1;.. sqlite
13bf2 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
13bf3 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a 29 26 pPager, (int *)&
13bf4 6e 50 61 67 65 43 6f 75 6e 74 29 3b 0a 20 20 20 nPageCount);.
13bf5 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e if( pPg->pgno>n
13bf6 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 PageCount ){.
13bf7 20 20 20 6e 50 61 67 65 20 3d 20 28 70 50 67 2d nPage = (pPg-
13bf8 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a >pgno - pg1)+1;.
13bf9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 }else if( (p
13bfa 67 31 2b 6e 50 61 67 65 50 65 72 53 65 63 74 6f g1+nPagePerSecto
13bfb 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20 r-1)>nPageCount
13bfc 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d ){. nPage =
13bfd 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67 nPageCount+1-pg
13bfe 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
13bff 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 nPage = nPag
13c00 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 20 20 20 ePerSector;.
13c01 7d 0a 20 20 20 20 61 73 73 65 72 74 28 6e 50 61 }. assert(nPa
13c02 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 ge>0);. asser
13c03 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f t(pg1<=pPg->pgno
13c04 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 28 70 );. assert((p
13c05 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70 g1+nPage)>pPg->p
13c06 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 gno);.. for(i
13c07 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 i=0; ii<nPage &&
13c08 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc==SQLITE_OK;
13c09 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e ii++){. Pgn
13c0a 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20 o pg = pg1+ii;.
13c0b 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 PgHdr *pPag
13c0c 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 67 3d e;. if( pg=
13c0d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 =pPg->pgno || !s
13c0e 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
13c0f 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
13c10 6e 61 6c 2c 20 70 67 29 20 29 7b 0a 20 20 20 20 nal, pg) ){.
13c11 20 20 20 20 69 66 28 20 70 67 21 3d 50 41 47 45 if( pg!=PAGE
13c12 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 R_MJ_PGNO(pPager
13c13 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 ) ){. r
13c14 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13c15 47 65 74 28 70 50 61 67 65 72 2c 20 70 67 2c 20 Get(pPager, pg,
13c16 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 &pPage);.
13c17 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13c18 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
13c19 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 rc = pager_w
13c1a 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 rite(pPage);.
13c1b 20 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 if( pPa
13c1c 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f ge->flags&PGHDR_
13c1d 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 NEED_SYNC ){.
13c1e 20 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 needS
13c1f 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ync = 1;.
13c20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 assert(pP
13c21 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b ager->needSync);
13c22 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
13c23 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
13c24 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 e3PagerUnref(pPa
13c25 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ge);. }
13c26 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13c27 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 61 67 }else if( (pPag
13c28 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 e = pager_lookup
13c29 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30 (pPager, pg))!=0
13c2a 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
13c2b 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 pPage->flags&PGH
13c2c 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a DR_NEED_SYNC ){.
13c2d 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 needSy
13c2e 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 nc = 1;.
13c2f 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
13c30 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 3PagerUnref(pPag
13c31 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
13c32 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
13c33 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
13c34 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 flag is set for
13c35 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 any of the nPag
13c36 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 e pages . **
13c37 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c starting at pg1,
13c38 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 then it needs t
13c39 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c o be set for all
13c3a 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73 of them. Becaus
13c3b 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 e. ** writing
13c3c 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65 to any of these
13c3d 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d 61 79 nPage pages may
13c3e 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65 damage the othe
13c3f 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a rs, the. ** j
13c40 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
13c41 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 contain sync()e
13c42 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 d copies of all
13c43 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 of them. ** b
13c44 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65 efore any of the
13c45 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e m can be written
13c46 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 out to the data
13c47 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a base file.. *
13c48 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 /. if( rc==SQ
13c49 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 65 64 53 LITE_OK && needS
13c4a 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ync ){. ass
13c4b 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26 20 70 ert( !MEMDB && p
13c4c 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 Pager->noSync==0
13c4d 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 );. for(ii
13c4e 3d 30 3b 20 69 69 3c 6e 50 61 67 65 3b 20 69 69 =0; ii<nPage; ii
13c4f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 67 48 ++){. PgH
13c50 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 67 65 dr *pPage = page
13c51 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c r_lookup(pPager,
13c52 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 20 20 pg1+ii);.
13c53 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 if( pPage ){.
13c54 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e pPage->
13c55 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e flags |= PGHDR_N
13c56 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 EED_SYNC;.
13c57 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
13c58 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 Unref(pPage);.
13c59 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
13c5a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61 assert(pPa
13c5b 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a ger->needSync);.
13c5c 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 }.. asser
13c5d 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 t( pPager->doNot
13c5e 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 Sync==1 );. p
13c5f 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 Pager->doNotSync
13c60 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
13c61 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 rc = pager_wr
13c62 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ite(pDbPage);.
13c63 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
13c64 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 ../*.** Return T
13c65 52 55 45 20 69 66 20 74 68 65 20 70 61 67 65 20 RUE if the page
13c66 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 given in the arg
13c67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f ument was previo
13c68 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 74 usly passed.** t
13c69 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 o sqlite3PagerWr
13c6a 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72 ite(). In other
13c6b 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 words, return T
13c6c 52 55 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0a RUE if it is ok.
13c6d 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 ** to change the
13c6e 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 content of the
13c6f 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 page..*/.#ifndef
13c70 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 NDEBUG.SQLITE_P
13c71 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13c72 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
13c73 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b le(DbPage *pPg){
13c74 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 66 . return pPg->f
13c75 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
13c76 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
13c77 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 * A call to this
13c78 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 routine tells t
13c79 68 65 20 70 61 67 65 72 20 74 68 61 74 20 69 74 he pager that it
13c7a 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
13c7b 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74 68 y to.** write th
13c7c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e e information on
13c7d 20 70 61 67 65 20 70 50 67 20 62 61 63 6b 20 74 page pPg back t
13c7e 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76 65 6e o the disk, even
13c7f 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74 20 though.** that
13c80 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 6d 61 page might be ma
13c81 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 rked as dirty.
13c82 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f This happens, fo
13c83 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a r example, when.
13c84 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61 73 20 ** the page has
13c85 62 65 65 6e 20 61 64 64 65 64 20 61 73 20 61 20 been added as a
13c86 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72 65 65 leaf of the free
13c87 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74 73 0a list and so its.
13c88 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c 6f ** content no lo
13c89 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a 2a nger matters..**
13c8a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 6e .** The overlyin
13c8b 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 65 72 g software layer
13c8c 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 calls this rout
13c8d 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 20 ine when all of
13c8e 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20 74 the data.** on t
13c8f 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 69 73 he given page is
13c90 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70 61 67 unused. The pag
13c91 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70 61 67 er marks the pag
13c92 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a 2a 2a e as clean so.**
13c93 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f that it does no
13c94 74 20 67 65 74 20 77 72 69 74 74 65 6e 20 74 6f t get written to
13c95 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 disk..**.** Tes
13c96 74 73 20 73 68 6f 77 20 74 68 61 74 20 74 68 69 ts show that thi
13c97 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 s optimization c
13c98 61 6e 20 71 75 61 64 72 75 70 6c 65 20 74 68 65 an quadruple the
13c99 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67 65 20 speed of large
13c9a 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65 72 61 .** DELETE opera
13c9b 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tions..*/.SQLITE
13c9c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
13c9d 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
13c9e 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b ite(PgHdr *pPg){
13c9f 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
13ca0 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a = pPg->pPager;.
13ca1 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 if( (pPg->flag
13ca2 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26 s&PGHDR_DIRTY) &
13ca3 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 & pPager->nSavep
13ca4 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 50 oint==0 ){. P
13ca5 41 47 45 52 54 52 41 43 45 28 28 22 44 4f 4e 54 AGERTRACE(("DONT
13ca6 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 20 6f _WRITE page %d o
13ca7 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e 70 67 f %d\n", pPg->pg
13ca8 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 no, PAGERID(pPag
13ca9 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54 52 41 er)));. IOTRA
13caa 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20 25 64 CE(("CLEAN %p %d
13cab 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 \n", pPager, pPg
13cac 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70 50 67 ->pgno)). pPg
13cad 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
13cae 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23 69 66 _DONT_WRITE;.#if
13caf 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
13cb0 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e _PAGES. pPg->
13cb1 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 pageHash = pager
13cb2 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a _pagehash(pPg);.
13cb3 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a #endif. }.}../*
13cb4 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
13cb5 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e is called to in
13cb6 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 crement the valu
13cb7 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
13cb8 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 6e 67 e file .** chang
13cb9 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f 72 65 e-counter, store
13cba 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 62 69 d as a 4-byte bi
13cbb 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
13cbc 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a 2a 2a starting at .**
13cbd 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 byte offset 24
13cbe 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c of the pager fil
13cbf 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
13cc0 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 isDirectMode fla
13cc1 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 g is zero, then
13cc2 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20 this is done by
13cc3 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 calling .** sqli
13cc4 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
13cc5 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 on page 1, then
13cc6 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f modifying the co
13cc7 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a ntents of the.**
13cc8 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74 page data. In t
13cc9 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c his case the fil
13cca 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65 e will be update
13ccb 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 d when the curre
13ccc 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f nt.** transactio
13ccd 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a n is committed..
13cce 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65 **.** The isDire
13ccf 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20 ctMode flag may
13cd0 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f only be non-zero
13cd1 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 if the library
13cd2 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 was compiled.**
13cd3 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f with the SQLITE_
13cd4 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 ENABLE_ATOMIC_WR
13cd5 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 ITE macro define
13cd6 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c d. In this case,
13cd7 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20 .** if isDirect
13cd8 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
13cd9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13cda 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64 ile is updated d
13cdb 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72 irectly.** by wr
13cdc 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64 iting an updated
13cdd 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65 version of page
13cde 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 1 using a call
13cdf 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 to the .** sqlit
13ce0 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63 e3OsWrite() func
13ce1 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
13ce2 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 int pager_incr_c
13ce3 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67 hangecounter(Pag
13ce4 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
13ce5 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20 isDirectMode){.
13ce6 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
13ce7 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61 _OK;.. /* Decla
13ce8 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a re and initializ
13ce9 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 e constant integ
13cea 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49 er 'isDirect'. I
13ceb 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69 f the. ** atomi
13cec 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
13ced 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20 tion is enabled
13cee 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74 in this build, t
13cef 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a hen isDirect. *
13cf0 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 * is initialized
13cf1 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 to the value pa
13cf2 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69 ssed as the isDi
13cf3 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74 rectMode paramet
13cf4 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 er. ** to this
13cf5 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 function. Otherw
13cf6 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79 ise, it is alway
13cf7 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20 s set to zero..
13cf8 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65 **. ** The ide
13cf9 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65 a is that if the
13cfa 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 atomic-write op
13cfb 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f timization is no
13cfc 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61 t. ** enabled a
13cfd 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 t compile time,
13cfe 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e the compiler can
13cff 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20 omit the tests
13d00 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63 of. ** 'isDirec
13d01 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c t' below, as wel
13d02 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65 l as the block e
13d03 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20 nclosed in the.
13d04 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63 ** "if( isDirec
13d05 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a t )" condition..
13d06 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c */.#ifndef SQL
13d07 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 ITE_ENABLE_ATOMI
13d08 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 6e 65 C_WRITE.# define
13d09 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0a 20 DIRECT_MODE 0.
13d0a 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63 assert( isDirec
13d0b 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e tMode==0 );. UN
13d0c 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 USED_PARAMETER(i
13d0d 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65 sDirectMode);.#e
13d0e 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 49 52 lse.# define DIR
13d0f 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 65 63 ECT_MODE isDirec
13d10 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a 20 20 tMode.#endif..
13d11 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
13d12 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 state>=PAGER_RES
13d13 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21 ERVED );. if( !
13d14 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
13d15 75 6e 74 44 6f 6e 65 20 26 26 20 41 4c 57 41 59 untDone && ALWAY
13d16 53 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 S(pPager->dbSize
13d17 3e 30 29 20 29 7b 0a 20 20 20 20 50 67 48 64 72 >0) ){. PgHdr
13d18 20 2a 70 50 67 48 64 72 3b 20 20 20 20 20 20 20 *pPgHdr;
13d19 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 /* Refe
13d1a 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 rence to page 1
13d1b 2a 2f 0a 20 20 20 20 75 33 32 20 63 68 61 6e 67 */. u32 chang
13d1c 65 5f 63 6f 75 6e 74 65 72 3b 20 20 20 20 20 20 e_counter;
13d1d 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 /* Initial
13d1e 76 61 6c 75 65 20 6f 66 20 63 68 61 6e 67 65 2d value of change-
13d1f 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f counter field */
13d20 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 .. assert( !p
13d21 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
13d22 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 && isOpen(pPager
13d23 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a ->fd) );.. /*
13d24 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20 Open page 1 of
13d25 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69 the file for wri
13d26 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 ting. */. rc
13d27 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
13d28 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50 t(pPager, 1, &pP
13d29 67 48 64 72 29 3b 0a 20 20 20 20 61 73 73 65 72 gHdr);. asser
13d2a 74 28 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20 t( pPgHdr==0 ||
13d2b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
13d2c 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 .. /* If page
13d2d 20 6f 6e 65 20 77 61 73 20 66 65 74 63 68 65 64 one was fetched
13d2e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 successfully, a
13d2f 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e nd this function
13d30 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f is not. ** o
13d31 70 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65 perating in dire
13d32 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 ct-mode, make pa
13d33 67 65 20 31 20 77 72 69 74 61 62 6c 65 2e 20 20 ge 1 writable.
13d34 57 68 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 20 20 When not in .
13d35 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64 65 2c ** direct mode,
13d36 20 70 61 67 65 20 31 20 69 73 20 61 6c 77 61 79 page 1 is alway
13d37 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 20 s held in cache
13d38 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 50 61 and hence the Pa
13d39 67 65 72 47 65 74 28 29 0a 20 20 20 20 2a 2a 20 gerGet(). **
13d3a 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79 73 20 above is always
13d3b 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68 65 6e successful - hen
13d3c 63 65 20 74 68 65 20 41 4c 57 41 59 53 20 6f 6e ce the ALWAYS on
13d3d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e 0a rc==SQLITE_OK..
13d3e 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
13d3f 44 49 52 45 43 54 5f 4d 4f 44 45 20 26 26 20 41 DIRECT_MODE && A
13d40 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45 LWAYS(rc==SQLITE
13d41 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 63 _OK) ){. rc
13d42 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
13d43 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20 20 rite(pPgHdr);.
13d44 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d }.. if( rc=
13d45 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13d46 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 /* Increment
13d47 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 74 20 the value just
13d48 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 69 read and write i
13d49 74 20 62 61 63 6b 20 74 6f 20 62 79 74 65 20 32 t back to byte 2
13d4a 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 6e 4. */. chan
13d4b 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c ge_counter = sql
13d4c 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75 38 ite3Get4byte((u8
13d4d 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 *)pPager->dbFile
13d4e 56 65 72 73 29 3b 0a 20 20 20 20 20 20 63 68 61 Vers);. cha
13d4f 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20 nge_counter++;.
13d50 20 20 20 20 20 70 75 74 33 32 62 69 74 73 28 28 put32bits((
13d51 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d 3e 70 (char*)pPgHdr->p
13d52 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 65 Data)+24, change
13d53 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 20 20 _counter);..
13d54 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20 /* If running
13d55 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 in direct mode,
13d56 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e write the conten
13d57 74 73 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20 ts of page 1 to
13d58 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 the file. */.
13d59 20 20 20 69 66 28 20 44 49 52 45 43 54 5f 4d 4f if( DIRECT_MO
13d5a 44 45 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f DE ){. co
13d5b 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d nst void *zBuf =
13d5c 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a pPgHdr->pData;.
13d5d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
13d5e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 pPager->dbFileSi
13d5f 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 ze>0 );.
13d60 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
13d61 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ite(pPager->fd,
13d62 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 zBuf, pPager->pa
13d63 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 geSize, 0);.
13d64 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
13d65 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
13d66 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 pPager->chang
13d67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a eCountDone = 1;.
13d68 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13d69 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
13d6a 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 Pager->changeCou
13d6b 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 ntDone = 1;.
13d6c 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f }. }.. /
13d6d 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 70 61 * Release the pa
13d6e 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f ge reference. */
13d6f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
13d70 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a rUnref(pPgHdr);.
13d71 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13d72 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 .}../*.** Sync t
13d73 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 74 6f he pager file to
13d74 20 64 69 73 6b 2e 20 54 68 69 73 20 69 73 20 61 disk. This is a
13d75 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d 6d 65 no-op for in-me
13d76 6d 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20 6f 72 mory files.** or
13d77 20 70 61 67 65 73 20 77 69 74 68 20 74 68 65 20 pages with the
13d78 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66 6c 61 Pager.noSync fla
13d79 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 g set..**.** If
13d7a 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 63 successful, or c
13d7b 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 72 alled on a pager
13d7c 20 66 6f 72 20 77 68 69 63 68 20 69 74 20 69 73 for which it is
13d7d 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 0a 2a a no-op, this.*
13d7e 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 * function retur
13d7f 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 ns SQLITE_OK. Ot
13d80 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f 20 65 herwise, an IO e
13d81 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
13d82 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
13d83 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
13d84 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 61 ite3PagerSync(Pa
13d85 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
13d86 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
13d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13d88 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
13d89 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ode */. assert(
13d8a 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28 !MEMDB );. if(
13d8b 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 pPager->noSync
13d8c 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
13d8d 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
13d8e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13d8f 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 OsSync(pPager->f
13d90 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f d, pPager->sync_
13d91 66 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 flags);. }. re
13d92 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
13d93 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 * Sync the datab
13d94 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 ase file for the
13d95 20 70 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a pager pPager. z
13d96 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f Master points to
13d97 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 the name.** of
13d98 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
13d99 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c file that shoul
13d9a 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 d be written int
13d9b 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c o the individual
13d9c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
13d9d 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 . zMaster may be
13d9e 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 NULL, which is
13d9f 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 6e interpreted as n
13da0 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 o master.** jour
13da1 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 nal (a single da
13da2 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 tabase transacti
13da3 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 on)..**.** This
13da4 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 routine ensures
13da5 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 that:.**.** *
13da6 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
13da7 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
13da8 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20 is updated,.**
13da9 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 * the journal
13daa 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73 is synced (unles
13dab 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 s the atomic-wri
13dac 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 te optimization
13dad 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a is used),.** *
13dae 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
13daf 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20 are written to
13db0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13db1 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 e, .** * the d
13db2 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 atabase file is
13db3 74 72 75 6e 63 61 74 65 64 20 28 69 66 20 72 65 truncated (if re
13db4 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 quired), and.**
13db5 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 * the database
13db6 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a file synced. .*
13db7 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 *.** The only th
13db8 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73 ing that remains
13db9 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 to commit the t
13dba 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f ransaction is to
13dbb 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 finalize .** (d
13dbc 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20 elete, truncate
13dbd 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73 or zero the firs
13dbe 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a t part of) the j
13dbf 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 ournal file (or
13dc0 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d .** delete the m
13dc1 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
13dc2 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 64 29 le if specified)
13dc3 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
13dc4 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 t if zMaster==NU
13dc5 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f LL, this does no
13dc6 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 t overwrite a pr
13dc7 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 evious value.**
13dc8 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c passed to an sql
13dc9 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 ite3PagerCommitP
13dca 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a haseOne() call..
13dcb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e **.** If the fin
13dcc 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e al parameter - n
13dcd 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c oSync - is true,
13dce 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 then the databa
13dcf 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a se file itself.*
13dd0 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e * is not synced.
13dd1 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 The caller must
13dd2 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 call sqlite3Pag
13dd3 65 72 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c erSync() directl
13dd4 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 y to.** sync the
13dd5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 database file b
13dd6 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f efore calling Co
13dd7 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 mmitPhaseTwo() t
13dd8 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 o delete the.**
13dd9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 journal file in
13dda 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 this case..*/.SQ
13ddb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13ddc 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
13ddd 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50 mitPhaseOne(. P
13dde 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 ager *pPager,
13ddf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13de0 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a * Pager object *
13de1 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
13de2 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20 20 20 zMaster,
13de3 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 /* If not NU
13de4 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a LL, the master j
13de5 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 ournal name */.
13de6 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20 int noSync
13de7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13de8 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 /* True to omit
13de9 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68 the xSync on th
13dea 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a e db file */.){.
13deb 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
13dec 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
13ded 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
13dee 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 64 62 */.. /* The db
13def 4f 72 69 67 53 69 7a 65 20 69 73 20 6e 65 76 65 OrigSize is neve
13df0 72 20 73 65 74 20 69 66 20 6a 6f 75 72 6e 61 6c r set if journal
13df1 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20 20 61 _mode=OFF */. a
13df2 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a ssert( pPager->j
13df3 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 ournalMode!=PAGE
13df4 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 R_JOURNALMODE_OF
13df5 46 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 4f F || pPager->dbO
13df6 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 0a 20 rigSize==0 );..
13df7 20 2f 2a 20 49 66 20 61 20 70 72 69 6f 72 20 65 /* If a prior e
13df8 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74 rror occurred, t
13df9 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 his routine shou
13dfa 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 ld not be called
13dfb 2e 20 20 52 4f 4c 4c 42 41 43 4b 0a 20 20 2a 2a . ROLLBACK. **
13dfc 20 69 73 20 74 68 65 20 61 70 70 72 6f 70 72 69 is the appropri
13dfd 61 74 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 ate response to
13dfe 61 6e 20 65 72 72 6f 72 2c 20 6e 6f 74 20 43 4f an error, not CO
13dff 4d 4d 49 54 2e 20 20 47 75 61 72 64 20 61 67 61 MMIT. Guard aga
13e00 69 6e 73 74 0a 20 20 2a 2a 20 63 6f 64 69 6e 67 inst. ** coding
13e01 20 65 72 72 6f 72 73 20 62 79 20 72 65 70 65 61 errors by repea
13e02 74 69 6e 67 20 74 68 65 20 70 72 69 6f 72 20 65 ting the prior e
13e03 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 4e rror. */. if( N
13e04 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 EVER(pPager->err
13e05 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e 20 70 Code) ) return p
13e06 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a Pager->errCode;.
13e07 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
13e08 44 41 54 41 42 41 53 45 20 53 59 4e 43 3a 20 46 DATABASE SYNC: F
13e09 69 6c 65 3d 25 73 20 7a 4d 61 73 74 65 72 3d 25 ile=%s zMaster=%
13e0a 73 20 6e 53 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a s nSize=%d\n", .
13e0b 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 46 pPager->zF
13e0c 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 ilename, zMaster
13e0d 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 , pPager->dbSize
13e0e 29 29 3b 0a 0a 20 20 69 66 28 20 4d 45 4d 44 42 ));.. if( MEMDB
13e0f 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f && pPager->dbMo
13e10 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 2f 2a dified ){. /*
13e11 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 If this is an i
13e12 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 n-memory db, or
13e13 6e 6f 20 70 61 67 65 73 20 68 61 76 65 20 62 65 no pages have be
13e14 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f en written to, o
13e15 72 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 66 75 r this. ** fu
13e16 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 nction has alrea
13e17 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 dy been called,
13e18 69 74 20 69 73 20 6d 6f 73 74 6c 79 20 61 20 6e it is mostly a n
13e19 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 65 72 2c 20 o-op. However,
13e1a 61 6e 79 0a 20 20 20 20 2a 2a 20 62 61 63 6b 75 any. ** backu
13e1b 70 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6e 65 p in progress ne
13e1c 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 eds to be restar
13e1d 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
13e1e 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 sqlite3BackupRes
13e1f 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 tart(pPager->pBa
13e20 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 ckup);. }else i
13e21 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
13e22 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 !=PAGER_SYNCED &
13e23 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 & pPager->dbModi
13e24 66 69 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 fied ){.. /*
13e25 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c The following bl
13e26 6f 63 6b 20 75 70 64 61 74 65 73 20 74 68 65 20 ock updates the
13e27 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20 change-counter.
13e28 45 78 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20 Exactly how it.
13e29 20 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 ** does this
13e2a 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 depends on wheth
13e2b 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 er or not the at
13e2c 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 omic-update opti
13e2d 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 mization. **
13e2e 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 was enabled at c
13e2f 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 ompile time, and
13e30 20 69 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 if this transac
13e31 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a tion meets the .
13e32 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 ** runtime c
13e33 72 69 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 riteria to use t
13e34 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 he operation: .
13e35 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 **. **
13e36 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 * The file-syste
13e37 6d 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 m supports the a
13e38 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 tomic-write prop
13e39 65 72 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 erty for. **
13e3a 20 20 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 blocks of s
13e3b 69 7a 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 ize page-size, a
13e3c 6e 64 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 nd . ** *
13e3d 54 68 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e This commit is n
13e3e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c ot part of a mul
13e3f 74 69 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74 ti-file transact
13e40 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 ion, and. **
13e41 20 20 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 * Exactly one
13e42 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d page has been m
13e43 6f 64 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72 odified and stor
13e44 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c e in the journal
13e45 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 file.. **.
13e46 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 ** If the opti
13e47 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 mization was not
13e48 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 enabled at comp
13e49 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 ile time, then t
13e4a 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f he. ** pager_
13e4b 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 incr_changecount
13e4c 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 er() function is
13e4d 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 called to updat
13e4e 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 e the change.
13e4f 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27 ** counter in '
13e50 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20 indirect-mode'.
13e51 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 If the optimizat
13e52 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ion is compiled
13e53 69 6e 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73 in but. ** is
13e54 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20 not applicable
13e55 74 6f 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 to this transact
13e56 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 ion, call sqlite
13e57 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 3JournalCreate()
13e58 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 . ** to make
13e59 73 75 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c sure the journal
13e5a 20 66 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c file has actual
13e5b 6c 79 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c ly been created,
13e5c 20 74 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a then call. *
13e5d 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 * pager_incr_cha
13e5e 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 ngecounter() to
13e5f 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
13e60 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64 e-counter in ind
13e61 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64 irect. ** mod
13e62 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a e. . **. *
13e63 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 * Otherwise, if
13e64 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e the optimization
13e65 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 is both enabled
13e66 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c and applicable,
13e67 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c . ** then cal
13e68 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 l pager_incr_cha
13e69 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 ngecounter() to
13e6a 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 update the chang
13e6b 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a e-counter. **
13e6c 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64 in 'direct' mod
13e6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 e. In this case
13e6e 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
13e6f 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20 will never be.
13e70 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f ** created fo
13e71 72 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 r this transacti
13e72 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 on.. */.#ifde
13e73 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
13e74 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 ATOMIC_WRITE.
13e75 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20 PgHdr *pPg;.
13e76 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
13e77 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 pPager->jfd) ||
13e78 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
13e79 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
13e7a 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 ALMODE_OFF );.
13e7b 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26 if( !zMaster &
13e7c 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d & isOpen(pPager-
13e7d 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26 20 70 >jfd) . && p
13e7e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
13e7f 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a f==jrnlBufferSiz
13e80 65 28 70 50 61 67 65 72 29 20 0a 20 20 20 20 20 e(pPager) .
13e81 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a && pPager->dbSiz
13e82 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c e>=pPager->dbFil
13e83 65 53 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30 eSize. && (0
13e84 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33 ==(pPg = sqlite3
13e85 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 PcacheDirtyList(
13e86 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
13e87 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69 ) || 0==pPg->pDi
13e88 72 74 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 rty). ){.
13e89 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
13e8a 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 db file change c
13e8b 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65 20 64 ounter via the d
13e8c 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68 irect-write meth
13e8d 6f 64 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a od. The . *
13e8e 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c * following call
13e8f 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 will modify the
13e90 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 in-memory repre
13e91 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67 sentation of pag
13e92 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f e 1 . ** to
13e93 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 include the upd
13e94 61 74 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e ated change coun
13e95 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 ter and then wri
13e96 74 65 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 te page 1 .
13e97 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 ** directly to
13e98 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13e99 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 e. Because of th
13e9a 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a e atomic-write .
13e9b 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74 ** propert
13e9c 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 y of the host fi
13e9d 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20 le-system, this
13e9e 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a is safe.. *
13e9f 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 /. rc = pag
13ea0 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f er_incr_changeco
13ea1 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 unter(pPager, 1)
13ea2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
13ea3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a rc = sqlite3J
13ea4 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61 ournalCreate(pPa
13ea5 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 ger->jfd);.
13ea6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13ea7 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
13ea8 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 = pager_incr_ch
13ea9 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 angecounter(pPag
13eaa 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a er, 0);. }.
13eab 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 }.#else.
13eac 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f rc = pager_incr_
13ead 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 changecounter(pP
13eae 61 67 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66 ager, 0);.#endif
13eaf 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
13eb0 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f ITE_OK ) goto co
13eb1 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 mmit_phase_one_e
13eb2 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 xit;.. /* If
13eb3 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
13eb4 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 has made the da
13eb5 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 tabase smaller,
13eb6 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 then all pages.
13eb7 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63 ** being disc
13eb8 61 72 64 65 64 20 62 79 20 74 68 65 20 74 72 75 arded by the tru
13eb9 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 ncation must be
13eba 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
13ebb 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 ournal. ** fi
13ebc 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c le. This can onl
13ebd 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f y happen in auto
13ebe 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20 -vacuum mode..
13ebf 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f **. ** Befo
13ec0 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 70 re reading the p
13ec1 61 67 65 73 20 77 69 74 68 20 70 61 67 65 20 6e ages with page n
13ec2 75 6d 62 65 72 73 20 6c 61 72 67 65 72 20 74 68 umbers larger th
13ec3 61 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 an the . ** c
13ec4 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 urrent value of
13ec5 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65 Pager.dbSize, se
13ec6 74 20 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f t dbSize back to
13ec7 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a the value. *
13ec8 2a 20 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61 * that it took a
13ec9 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
13eca 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 he transaction.
13ecb 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20 Otherwise, the.
13ecc 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 ** calls to s
13ecd 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 qlite3PagerGet()
13ece 20 72 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70 return zeroed p
13ecf 61 67 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 ages instead of
13ed0 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 . ** reading
13ed1 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 data from the da
13ed2 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 tabase file..
13ed3 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 **. ** When
13ed4 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d 4f 46 journal_mode==OF
13ed5 46 20 74 68 65 20 64 62 4f 72 69 67 53 69 7a 65 F the dbOrigSize
13ed6 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2c is always zero,
13ed7 20 73 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 so this. **
13ed8 62 6c 6f 63 6b 20 6e 65 76 65 72 20 72 75 6e 73 block never runs
13ed9 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 if journal_mode
13eda 3d 4f 46 46 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 =OFF.. */.#if
13edb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
13edc 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
13edd 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 if( pPager->dbSi
13ede 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 ze<pPager->dbOri
13edf 67 53 69 7a 65 20 0a 20 20 20 20 20 26 26 20 41 gSize . && A
13ee0 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f LWAYS(pPager->jo
13ee1 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 urnalMode!=PAGER
13ee2 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
13ee3 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 50 ). ){. P
13ee4 67 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20 gno i;
13ee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13ee6 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 /* Iter
13ee7 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f ator variable */
13ee8 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e . const Pgn
13ee9 6f 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f o iSkip = PAGER_
13eea 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b MJ_PGNO(pPager);
13eeb 20 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b /* Pending lock
13eec 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 63 page */. c
13eed 6f 6e 73 74 20 50 67 6e 6f 20 64 62 53 69 7a 65 onst Pgno dbSize
13eee 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a = pPager->dbSiz
13eef 65 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 e; /* Data
13ef0 62 61 73 65 20 69 6d 61 67 65 20 73 69 7a 65 20 base image size
13ef1 2a 2f 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 */ . pPager
13ef2 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 ->dbSize = pPage
13ef3 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 r->dbOrigSize;.
13ef4 20 20 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69 for( i=dbSi
13ef5 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d ze+1; i<=pPager-
13ef6 3e 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b >dbOrigSize; i++
13ef7 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
13ef8 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 !sqlite3BitvecTe
13ef9 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f st(pPager->pInJo
13efa 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d urnal, i) && i!=
13efb 69 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 20 iSkip ){.
13efc 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b PgHdr *pPage;
13efd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13efe 50 61 67 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 Page to journal
13eff 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 */. rc
13f00 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
13f01 74 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 t(pPager, i, &pP
13f02 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
13f03 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
13f04 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f K ) goto commit_
13f05 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
13f06 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
13f07 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
13f08 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
13f09 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
13f0a 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 nref(pPage);.
13f0b 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
13f0c 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 QLITE_OK ) goto
13f0d 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 commit_phase_one
13f0e 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d _exit;. }
13f0f 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 . } .
13f10 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d pPager->dbSize =
13f11 20 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23 dbSize;. }.#
13f12 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72 endif.. /* Wr
13f13 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ite the master j
13f14 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f ournal name into
13f15 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
13f16 65 2e 20 49 66 20 61 20 6d 61 73 74 65 72 20 0a e. If a master .
13f17 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 ** journal f
13f18 69 6c 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72 ile name has alr
13f19 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 eady been writte
13f1a 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
13f1b 20 66 69 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f file, . ** o
13f1c 72 20 69 66 20 7a 4d 61 73 74 65 72 20 69 73 20 r if zMaster is
13f1d 4e 55 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 NULL (no master
13f1e 6a 6f 75 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 journal), then t
13f1f 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f his call is a no
13f20 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 -op.. */.
13f21 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72 rc = writeMaster
13f22 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 Journal(pPager,
13f23 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 zMaster);. if
13f24 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13f25 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 ) goto commit_ph
13f26 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 ase_one_exit;..
13f27 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a /* Sync the j
13f28 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
13f29 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 the atomic-updat
13f2a 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
13f2b 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 s being. ** u
13f2c 73 65 64 2c 20 74 68 69 73 20 63 61 6c 6c 20 77 sed, this call w
13f2d 69 6c 6c 20 6e 6f 74 20 63 72 65 61 74 65 20 74 ill not create t
13f2e 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13f2f 6f 72 20 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20 or perform any.
13f30 20 20 20 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20 ** real IO..
13f31 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
13f32 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 yncJournal(pPage
13f33 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d r);. if( rc!=
13f34 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
13f35 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e commit_phase_on
13f36 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 e_exit;.. /*
13f37 57 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 Write all dirty
13f38 70 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 pages to the dat
13f39 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 abase file. */.
13f3a 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 rc = pager_wr
13f3b 69 74 65 5f 70 61 67 65 6c 69 73 74 28 73 71 6c ite_pagelist(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 3b 0a 20 20 20 20 69 66 28 20 72 che));. if( r
13f3f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
13f40 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
13f41 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 !=SQLITE_IOERR_B
13f42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 LOCKED );.
13f43 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 goto commit_phas
13f44 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 e_one_exit;.
13f45 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 }. sqlite3Pca
13f46 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 cheCleanAll(pPag
13f47 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 er->pPCache);..
13f48 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c /* If the fil
13f49 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 e on disk is not
13f4a 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 the same size a
13f4b 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 s the database i
13f4c 6d 61 67 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 mage,. ** the
13f4d 6e 20 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e n use pager_trun
13f4e 63 61 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 cate to grow or
13f4f 73 68 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 shrink the file
13f50 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 here.. */.
13f51 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 if( pPager->dbS
13f52 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64 62 46 ize!=pPager->dbF
13f53 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 ileSize ){.
13f54 20 50 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61 Pgno nNew = pPa
13f55 67 65 72 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70 ger->dbSize - (p
13f56 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 Pager->dbSize==P
13f57 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 AGER_MJ_PGNO(pPa
13f58 67 65 72 29 29 3b 0a 20 20 20 20 20 20 61 73 73 ger));. ass
13f59 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 ert( pPager->sta
13f5a 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
13f5b 49 56 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 IVE );. rc
13f5c 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 = pager_truncate
13f5d 28 70 50 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a (pPager, nNew);.
13f5e 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
13f5f 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 LITE_OK ) goto c
13f60 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
13f61 65 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 exit;. }..
13f62 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e /* Finally, syn
13f63 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 c the database f
13f64 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ile. */. if(
13f65 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 !pPager->noSync
13f66 26 26 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 && !noSync ){.
13f67 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13f68 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 OsSync(pPager->f
13f69 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f d, pPager->sync_
13f6a 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 flags);. }.
13f6b 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 53 59 IOTRACE(("DBSY
13f6c 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 NC %p\n", pPager
13f6d 29 29 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e )).. pPager->
13f6e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59 state = PAGER_SY
13f6f 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69 NCED;. }..commi
13f70 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 t_phase_one_exit
13f71 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d :. return rc;.}
13f72 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 .../*.** When th
13f73 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
13f74 61 6c 6c 65 64 2c 20 74 68 65 20 64 61 74 61 62 alled, the datab
13f75 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 ase file has bee
13f76 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 n completely.**
13f77 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65 updated to refle
13f78 63 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d ct the changes m
13f79 61 64 65 20 62 79 20 74 68 65 20 63 75 72 72 65 ade by the curre
13f7a 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 nt transaction a
13f7b 6e 64 0a 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 nd.** synced to
13f7c 64 69 73 6b 2e 20 54 68 65 20 6a 6f 75 72 6e 61 disk. The journa
13f7d 6c 20 66 69 6c 65 20 73 74 69 6c 6c 20 65 78 69 l file still exi
13f7e 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2d sts in the file-
13f7f 73 79 73 74 65 6d 20 0a 2a 2a 20 74 68 6f 75 67 system .** thoug
13f80 68 2c 20 61 6e 64 20 69 66 20 61 20 66 61 69 6c h, and if a fail
13f81 75 72 65 20 6f 63 63 75 72 73 20 61 74 20 74 68 ure occurs at th
13f82 69 73 20 70 6f 69 6e 74 20 69 74 20 77 69 6c 6c is point it will
13f83 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 eventually.** b
13f84 65 20 75 73 65 64 20 61 73 20 61 20 68 6f 74 2d e used as a hot-
13f85 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 journal and the
13f86 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
13f87 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e ion rolled back.
13f88 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
13f89 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 tion finalizes t
13f8a 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c he journal file,
13f8b 20 65 69 74 68 65 72 20 62 79 20 64 65 6c 65 74 either by delet
13f8c 69 6e 67 2c 20 0a 2a 2a 20 74 72 75 6e 63 61 74 ing, .** truncat
13f8d 69 6e 67 20 6f 72 20 70 61 72 74 69 61 6c 6c 79 ing or partially
13f8e 20 7a 65 72 6f 69 6e 67 20 69 74 2c 20 73 6f 20 zeroing it, so
13f8f 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 that it cannot b
13f90 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 68 e used .** for h
13f91 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
13f92 61 63 6b 2e 20 4f 6e 63 65 20 74 68 69 73 20 69 ack. Once this i
13f93 73 20 64 6f 6e 65 20 74 68 65 20 74 72 61 6e 73 s done the trans
13f94 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 72 72 action is.** irr
13f95 65 76 6f 63 61 62 6c 79 20 63 6f 6d 6d 69 74 74 evocably committ
13f96 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ed..**.** If an
13f97 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e error occurs, an
13f98 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
13f99 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 s returned and t
13f9a 68 65 20 70 61 67 65 72 0a 2a 2a 20 6d 6f 76 65 he pager.** move
13f9b 73 20 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 s into the error
13f9c 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 69 73 state. Otherwis
13f9d 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 e, SQLITE_OK is
13f9e 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
13f9f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
13fa0 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
13fa1 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72 itPhaseTwo(Pager
13fa2 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 *pPager){. int
13fa3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
13fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fa5 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
13fa6 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 */.. /* This r
13fa7 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f outine should no
13fa8 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 61 t be called if a
13fa9 20 70 72 69 6f 72 20 65 72 72 6f 72 20 68 61 73 prior error has
13faa 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 2a 2a 20 occurred.. **
13fab 42 75 74 20 69 66 20 28 64 75 65 20 74 6f 20 61 But if (due to a
13fac 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 65 6c coding error el
13fad 73 65 77 68 65 72 65 20 69 6e 20 74 68 65 20 73 sewhere in the s
13fae 79 73 74 65 6d 29 20 69 74 20 64 6f 65 73 20 67 ystem) it does g
13faf 65 74 0a 20 20 2a 2a 20 63 61 6c 6c 65 64 2c 20 et. ** called,
13fb0 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 just return the
13fb1 73 61 6d 65 20 65 72 72 6f 72 20 63 6f 64 65 20 same error code
13fb2 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e without doing an
13fb3 79 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 ything. */. if(
13fb4 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 NEVER(pPager->e
13fb5 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e rrCode) ) return
13fb6 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
13fb7 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e ;.. /* This fun
13fb8 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 ction should not
13fb9 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 be called if th
13fba 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 e pager is not i
13fbb 6e 20 61 74 20 6c 65 61 73 74 0a 20 20 2a 2a 20 n at least. **
13fbc 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 PAGER_RESERVED s
13fbd 74 61 74 65 2e 20 41 6e 64 20 69 6e 64 65 65 64 tate. And indeed
13fbe 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 64 6f SQLite never do
13fbf 65 73 20 74 68 69 73 2e 20 42 75 74 20 69 74 20 es this. But it
13fc0 69 73 0a 20 20 2a 2a 20 6e 69 63 65 20 74 6f 20 is. ** nice to
13fc1 68 61 76 65 20 74 68 69 73 20 64 65 66 65 6e 73 have this defens
13fc2 69 76 65 20 74 65 73 74 20 68 65 72 65 20 61 6e ive test here an
13fc3 79 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 yway.. */. if(
13fc4 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 73 NEVER(pPager->s
13fc5 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 tate<PAGER_RESER
13fc6 56 45 44 29 20 29 20 72 65 74 75 72 6e 20 53 51 VED) ) return SQ
13fc7 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f LITE_ERROR;.. /
13fc8 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f * An optimizatio
13fc9 6e 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61 n. If the databa
13fca 73 65 20 77 61 73 20 6e 6f 74 20 61 63 74 75 61 se was not actua
13fcb 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 64 75 72 lly modified dur
13fcc 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 72 ing. ** this tr
13fcd 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 70 ansaction, the p
13fce 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 ager is running
13fcf 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 in exclusive-mod
13fd0 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 73 e and is. ** us
13fd1 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 20 6a ing persistent j
13fd2 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 68 ournals, then th
13fd3 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
13fd4 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a no-op.. **. *
13fd5 2a 20 54 68 65 20 73 74 61 72 74 20 6f 66 20 74 * The start of t
13fd6 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13fd7 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 currently contai
13fd8 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 72 ns a single jour
13fd9 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72 nal . ** header
13fda 20 77 69 74 68 20 74 68 65 20 6e 52 65 63 20 66 with the nRec f
13fdb 69 65 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 49 ield set to 0. I
13fdc 66 20 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c f such a journal
13fdd 20 69 73 20 75 73 65 64 20 61 73 0a 20 20 2a 2a is used as. **
13fde 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64 a hot-journal d
13fdf 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 uring hot-journa
13fe0 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68 l rollback, 0 ch
13fe1 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61 anges will be ma
13fe2 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 de. ** to the d
13fe3 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f atabase file. So
13fe4 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
13fe5 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f d to zero the jo
13fe6 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 urnal . ** head
13fe7 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 er. Since the pa
13fe8 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73 ger is in exclus
13fe9 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20 ive mode, there
13fea 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20 is no need. **
13feb 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 6b to drop any lock
13fec 73 20 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a 20 s either.. */.
13fed 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d if( pPager->dbM
13fee 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 20 70 50 odified==0 && pP
13fef 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
13ff0 6f 64 65 20 0a 20 20 20 26 26 20 70 50 61 67 65 ode . && pPage
13ff1 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
13ff2 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
13ff3 45 5f 50 45 52 53 49 53 54 0a 20 20 29 7b 0a 20 E_PERSIST. ){.
13ff4 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13ff5 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a r->journalOff==J
13ff6 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
13ff7 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72 65 74 ager) );. ret
13ff8 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
13ff9 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 }.. PAGERTRACE
13ffa 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c (("COMMIT %d\n",
13ffb 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
13ffc 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 ));. assert( pP
13ffd 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
13ffe 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 4d 45 4d ER_SYNCED || MEM
13fff 44 42 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 64 DB || !pPager->d
14000 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 72 bModified );. r
14001 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 c = pager_end_tr
14002 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 ansaction(pPager
14003 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 , pPager->setMas
14004 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 ter);. return p
14005 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
14006 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a r, rc);.}../*.**
14007 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 Rollback all ch
14008 61 6e 67 65 73 2e 20 54 68 65 20 64 61 74 61 62 anges. The datab
14009 61 73 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 ase falls back t
1400a 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d o PAGER_SHARED m
1400b 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ode..**.** This
1400c 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d function perform
1400d 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a s two tasks:.**.
1400e 2a 2a 20 20 20 31 29 20 49 74 20 72 6f 6c 6c 73 ** 1) It rolls
1400f 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 back the journa
14010 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e l file, restorin
14011 67 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 g all database f
14012 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 ile and .**
14013 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 in-memory cache
14014 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 73 74 pages to the st
14015 61 74 65 20 74 68 65 79 20 77 65 72 65 20 69 6e ate they were in
14016 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 when the transa
14017 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 77 61 ction.** wa
14018 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0a 2a 2a s opened, and.**
14019 20 20 20 32 29 20 49 74 20 66 69 6e 61 6c 69 7a 2) It finaliz
1401a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 es the journal f
1401b 69 6c 65 2c 20 73 6f 20 74 68 61 74 20 69 74 20 ile, so that it
1401c 69 73 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 20 is not used for
1401d 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c hot.** roll
1401e 62 61 63 6b 20 61 74 20 61 6e 79 20 70 6f 69 6e back at any poin
1401f 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e t in the future.
14020 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74 .**.** subject t
14021 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
14022 71 75 61 6c 69 66 69 63 61 74 69 6f 6e 73 3a 0a qualifications:.
14023 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 6a **.** * If the j
14024 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
14025 6f 74 20 79 65 74 20 6f 70 65 6e 20 77 68 65 6e ot yet open when
14026 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
14027 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 74 s called,.** t
14028 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20 69 73 20 hen only (2) is
14029 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e 20 74 68 performed. In th
1402a 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 is case there is
1402b 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 no journal file
1402c 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 62 61 .** to roll ba
1402d 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 69 ck..**.** * If i
1402e 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 n an error state
1402f 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 other than SQLI
14030 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 74 61 TE_FULL, then ta
14031 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a 20 20 20 sk (1) is .**
14032 70 65 72 66 6f 72 6d 65 64 2e 20 49 66 20 73 75 performed. If su
14033 63 63 65 73 73 66 75 6c 2c 20 74 61 73 6b 20 28 ccessful, task (
14034 32 29 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 2). Regardless o
14035 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 0a 2a 2a f the outcome.**
14036 20 20 20 6f 66 20 65 69 74 68 65 72 2c 20 74 68 of either, th
14037 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 e error state er
14038 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
14039 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c rned to the call
1403a 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 65 69 er.** (i.e. ei
1403b 74 68 65 72 20 53 51 4c 49 54 45 5f 49 4f 45 52 ther SQLITE_IOER
1403c 52 20 6f 72 20 53 51 4c 49 54 45 5f 43 4f 52 52 R or SQLITE_CORR
1403d 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 UPT)..**.** * If
1403e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
1403f 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 PAGER_RESERVED
14040 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 state, then atte
14041 6d 70 74 20 28 31 29 2e 20 57 68 65 74 68 65 72 mpt (1). Whether
14042 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20 28 31 29 .** or not (1)
14043 20 69 73 20 73 75 63 63 75 73 73 66 75 6c 2c 20 is succussful,
14044 61 6c 73 6f 20 61 74 74 65 6d 70 74 20 28 32 29 also attempt (2)
14045 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c . If successful,
14046 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 53 51 4c return.** SQL
14047 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 ITE_OK. Otherwis
14048 65 2c 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 e, enter the err
14049 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 or state and ret
1404a 75 72 6e 20 74 68 65 20 66 69 72 73 74 20 0a 2a urn the first .*
1404b 2a 20 20 20 65 72 72 6f 72 20 63 6f 64 65 20 65 * error code e
1404c 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a 2a 2a 0a ncountered. .**.
1404d 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 ** In this cas
1404e 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 e there is no ch
1404f 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 64 61 ance that the da
14050 74 61 62 61 73 65 20 77 61 73 20 77 72 69 74 74 tabase was writt
14051 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 6f 20 en to. .** So
14052 69 73 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c is safe to final
14053 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ize the journal
14054 66 69 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 file even if the
14055 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 20 20 playback .**
14056 28 6f 70 65 72 61 74 69 6f 6e 20 31 29 20 66 61 (operation 1) fa
14057 69 6c 65 64 2e 20 48 6f 77 65 76 65 72 20 74 68 iled. However th
14058 65 20 70 61 67 65 72 20 6d 75 73 74 20 65 6e 74 e pager must ent
14059 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 er the error sta
1405a 74 65 0a 2a 2a 20 20 20 61 73 20 74 68 65 20 63 te.** as the c
1405b 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69 ontents of the i
1405c 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 61 n-memory cache a
1405d 72 65 20 6e 6f 77 20 73 75 73 70 65 63 74 2e 0a re now suspect..
1405e 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c 79 2c **.** * Finally,
1405f 20 69 66 20 69 6e 20 50 41 47 45 52 5f 45 58 43 if in PAGER_EXC
14060 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 LUSIVE state, th
14061 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 en attempt (1).
14062 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74 65 6d 70 Only.** attemp
14063 74 20 28 32 29 20 69 66 20 28 31 29 20 69 73 20 t (2) if (1) is
14064 73 75 63 63 65 73 73 66 75 6c 2e 20 52 65 74 75 successful. Retu
14065 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 rn SQLITE_OK if
14066 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 20 successful,.**
14067 20 6f 74 68 65 72 77 69 73 65 20 65 6e 74 65 72 otherwise enter
14068 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
14069 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 and return the
1406a 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 error code from
1406b 74 68 65 20 0a 2a 2a 20 20 20 66 61 69 6c 69 6e the .** failin
1406c 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a g operation..**.
1406d 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 ** In this cas
1406e 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 e the database f
1406f 69 6c 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 ile may have bee
14070 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 53 6f n written to. So
14071 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 70 6c 61 if the.** pla
14072 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 yback operation
14073 64 69 64 20 6e 6f 74 20 73 75 63 63 65 65 64 20 did not succeed
14074 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 it would not be
14075 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65 safe to finalize
14076 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 .** the journa
14077 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65 65 64 73 l file. It needs
14078 20 74 6f 20 62 65 20 6c 65 66 74 20 69 6e 20 74 to be left in t
14079 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 he file-system s
1407a 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73 6f 6d 65 o that.** some
1407b 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 other process c
1407c 61 6e 20 75 73 65 20 69 74 20 74 6f 20 72 65 73 an use it to res
1407d 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 tore the databas
1407e 65 20 73 74 61 74 65 20 28 62 79 0a 2a 2a 20 20 e state (by.**
1407f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c hot-journal rol
14080 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c 49 54 lback)..*/.SQLIT
14081 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
14082 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 lite3PagerRollba
14083 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
14084 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
14085 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
14086 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
14087 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41 urn code */. PA
14088 47 45 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42 GERTRACE(("ROLLB
14089 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 ACK %d\n", PAGER
1408a 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
1408b 69 66 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d if( !pPager->dbM
1408c 6f 64 69 66 69 65 64 20 7c 7c 20 21 69 73 4f 70 odified || !isOp
1408d 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
1408e 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 ){. rc = page
1408f 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f r_end_transactio
14090 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 n(pPager, pPager
14091 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 ->setMaster);.
14092 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 }else if( pPager
14093 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 ->errCode && pPa
14094 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 ger->errCode!=SQ
14095 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 LITE_FULL ){.
14096 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
14097 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
14098 49 56 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67 IVE ){. pag
14099 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 er_playback(pPag
1409a 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 er, 0);. }.
1409b 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 rc = pPager->e
1409c 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b rrCode;. }else{
1409d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
1409e 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 >state==PAGER_RE
1409f 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 20 20 SERVED ){.
140a0 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 int rc2;. r
140a1 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 c = pager_playba
140a2 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 ck(pPager, 0);.
140a3 20 20 20 20 20 72 63 32 20 3d 20 70 61 67 65 72 rc2 = pager
140a4 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
140a5 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d (pPager, pPager-
140a6 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 20 >setMaster);.
140a7 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
140a8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
140a9 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 rc = rc2;.
140aa 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
140ab 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c rc = pager_pl
140ac 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 ayback(pPager, 0
140ad 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 );. }.. if
140ae 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 ( !MEMDB ){.
140af 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
140b0 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d Valid = 0;. }
140b1 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65 .. /* If an e
140b2 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
140b3 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 ng a ROLLBACK, w
140b4 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 e can no longer
140b5 74 72 75 73 74 20 74 68 65 20 70 61 67 65 72 0a trust the pager.
140b6 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 53 6f ** cache. So
140b7 20 63 61 6c 6c 20 70 61 67 65 72 5f 65 72 72 6f call pager_erro
140b8 72 28 29 20 6f 6e 20 74 68 65 20 77 61 79 20 6f r() on the way o
140b9 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 65 ut to make any e
140ba 72 72 6f 72 20 0a 20 20 20 20 2a 2a 20 70 65 72 rror . ** per
140bb 73 69 73 74 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a sistent.. */.
140bc 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 rc = pager_e
140bd 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
140be 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
140bf 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
140c0 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 64 rn TRUE if the d
140c1 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 atabase file is
140c2 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79 opened read-only
140c3 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a . Return FALSE.
140c4 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 62 61 ** if the databa
140c5 73 65 20 69 73 20 28 69 6e 20 74 68 65 6f 72 79 se is (in theory
140c6 29 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 ) writable..*/.S
140c7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 QLITE_PRIVATE u8
140c8 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 sqlite3PagerIsr
140c9 65 61 64 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 eadonly(Pager *p
140ca 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
140cb 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c pPager->readOnl
140cc 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 y;.}../*.** Retu
140cd 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
140ce 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
140cf 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c he pager..*/.SQL
140d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
140d1 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
140d2 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 ount(Pager *pPag
140d3 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 er){. return sq
140d4 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
140d5 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
140d6 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 che);.}../*.** R
140d7 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
140d8 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 of references t
140d9 6f 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 o the specified
140da 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f page..*/.SQLITE_
140db 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
140dc 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
140dd 6f 75 6e 74 28 44 62 50 61 67 65 20 2a 70 50 61 ount(DbPage *pPa
140de 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 ge){. return sq
140df 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 lite3PcachePageR
140e0 65 66 63 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a efcount(pPage);.
140e1 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }..#ifdef SQLITE
140e2 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 _TEST./*.** This
140e3 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
140e4 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 for testing and
140e5 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a analysis only..
140e6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
140e7 45 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 E int *sqlite3Pa
140e8 67 65 72 53 74 61 74 73 28 50 61 67 65 72 20 2a gerStats(Pager *
140e9 70 50 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69 pPager){. stati
140ea 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 c int a[11];. a
140eb 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 [0] = sqlite3Pca
140ec 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 cheRefCount(pPag
140ed 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 er->pPCache);.
140ee 61 5b 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 a[1] = sqlite3Pc
140ef 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 achePagecount(pP
140f0 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
140f1 20 20 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 a[2] = sqlite3
140f2 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73 69 PcacheGetCachesi
140f3 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 ze(pPager->pPCac
140f4 68 65 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 he);. a[3] = pP
140f5 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
140f6 64 20 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72 d ? (int) pPager
140f7 2d 3e 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 ->dbSize : -1;.
140f8 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[4] = pPager->
140f9 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 state;. a[5] =
140fa 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
140fb 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 . a[6] = pPager
140fc 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d ->nHit;. a[7] =
140fd 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a pPager->nMiss;.
140fe 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 a[8] = 0; /*
140ff 55 73 65 64 20 74 6f 20 62 65 20 70 50 61 67 65 Used to be pPage
14100 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b r->nOvfl */. a[
14101 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 9] = pPager->nRe
14102 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 ad;. a[10] = pP
14103 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 ager->nWrite;.
14104 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64 return a;.}.#end
14105 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e if../*.** Return
14106 20 74 72 75 65 20 69 66 20 74 68 69 73 20 69 73 true if this is
14107 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 an in-memory pa
14108 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ger..*/.SQLITE_P
14109 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1410a 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 e3PagerIsMemdb(P
1410b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
1410c 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d return MEMDB;.}
1410d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 ../*.** Check th
1410e 61 74 20 74 68 65 72 65 20 61 72 65 20 61 74 20 at there are at
1410f 6c 65 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 least nSavepoint
14110 20 73 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e savepoints open
14111 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a . If there are.*
14112 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 * currently less
14113 20 74 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 than nSavepoint
14114 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 s open, then ope
14115 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 n one or more sa
14116 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d vepoints.** to m
14117 61 6b 65 20 75 70 20 74 68 65 20 64 69 66 66 65 ake up the diffe
14118 72 65 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75 rence. If the nu
14119 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e mber of savepoin
1411a 74 73 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a ts is already.**
1411b 20 65 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 equal to nSavep
1411c 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 oint, then this
1411d 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
1411e 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 -op..**.** If a
1411f 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
14120 6e 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f n fails, SQLITE_
14121 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 NOMEM is returne
14122 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a d. If an error .
14123 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 ** occurs while
14124 6f 70 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d opening the sub-
14125 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 journal file, th
14126 65 6e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 en an IO error c
14127 6f 64 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e ode is.** return
14128 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 ed. Otherwise, S
14129 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c QLITE_OK..*/.SQL
1412a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1412b 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e sqlite3PagerOpen
1412c 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 Savepoint(Pager
1412d 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 *pPager, int nSa
1412e 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 vepoint){. int
1412f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
14130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14131 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
14132 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 code */. int nC
14133 75 72 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d urrent = pPager-
14134 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 >nSavepoint;
14135 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e /* Current n
14136 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 umber of savepoi
14137 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53 nts */.. if( nS
14138 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e avepoint>nCurren
14139 74 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 t && pPager->use
1413a 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 Journal ){. i
1413b 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
1413c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1413d 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
1413e 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 or variable */.
1413f 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e PagerSavepoin
14140 74 20 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20 t *aNew;
14141 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
14142 77 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 w Pager.aSavepoi
14143 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20 nt array */..
14144 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 72 65 /* Either there
14145 20 69 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f is no active jo
14146 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62 urnal or the sub
14147 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e -journal is open
14148 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 or . ** the
14149 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79 journal is alway
1414a 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f s stored in memo
1414b 72 79 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 ry */. assert
1414c 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 ( pPager->nSavep
1414d 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65 oint==0 || isOpe
1414e 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 n(pPager->sjfd)
1414f 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ||. p
14150 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
14151 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
14152 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a LMODE_MEMORY );.
14153 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 . /* Grow the
14154 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e Pager.aSavepoin
14155 74 20 61 72 72 61 79 20 75 73 69 6e 67 20 72 65 t array using re
14156 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20 alloc(). Return
14157 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20 SQLITE_NOMEM.
14158 20 2a 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 ** if the alloc
14159 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68 ation fails. Oth
1415a 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65 erwise, zero the
1415b 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20 new portion in
1415c 63 61 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d case a . ** m
1415d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 alloc failure oc
1415e 63 75 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c curs while popul
1415f 61 74 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20 ating it in the
14160 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65 for(...) loop be
14161 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 low.. */.
14162 61 4e 65 77 20 3d 20 28 50 61 67 65 72 53 61 76 aNew = (PagerSav
14163 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33 epoint *)sqlite3
14164 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20 Realloc(.
14165 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f pPager->aSavepo
14166 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65 int, sizeof(Page
14167 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76 rSavepoint)*nSav
14168 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20 epoint. );.
14169 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 if( !aNew ){.
1416a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1416b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
1416c 20 20 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77 memset(&aNew
1416d 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28 [nCurrent], 0, (
1416e 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72 nSavepoint-nCurr
1416f 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61 ent) * sizeof(Pa
14170 67 65 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a gerSavepoint));.
14171 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 pPager->aSav
14172 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20 epoint = aNew;.
14173 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 pPager->nSave
14174 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69 point = nSavepoi
14175 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 nt;.. /* Popu
14176 6c 61 74 65 20 74 68 65 20 50 61 67 65 72 53 61 late the PagerSa
14177 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 vepoint structur
14178 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 es just allocate
14179 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 d. */. for(ii
1417a 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 =nCurrent; ii<nS
1417b 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b avepoint; ii++){
1417c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1417d 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
1417e 69 64 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 id );. aNew
1417f 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61 [ii].nOrig = pPa
14180 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 ger->dbSize;.
14181 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 if( isOpen(pP
14182 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 41 4c ager->jfd) && AL
14183 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75 WAYS(pPager->jou
14184 72 6e 61 6c 4f 66 66 3e 30 29 20 29 7b 0a 20 20 rnalOff>0) ){.
14185 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 aNew[ii].i
14186 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d Offset = pPager-
14187 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 >journalOff;.
14188 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
14189 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 aNew[ii].iOffs
1418a 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 et = JOURNAL_HDR
1418b 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 _SZ(pPager);.
1418c 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b }. aNew[
1418d 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50 ii].iSubRec = pP
1418e 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20 ager->nSubRec;.
1418f 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 aNew[ii].pI
14190 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c nSavepoint = sql
14191 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
14192 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 (pPager->dbSize)
14193 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 ;. if( !aNe
14194 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 w[ii].pInSavepoi
14195 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 nt ){. re
14196 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
14197 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d M;. }. }
14198 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 .. /* Open th
14199 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 e sub-journal, i
1419a 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 f it is not alre
1419b 61 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 ady opened. */.
1419c 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a rc = openSubJ
1419d 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a ournal(pPager);.
1419e 20 20 20 20 61 73 73 65 72 74 54 72 75 6e 63 61 assertTrunca
1419f 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 teConstraint(pPa
141a0 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 ger);. }.. ret
141a1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
141a2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
141a3 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c s called to roll
141a4 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 back or release
141a5 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70 (commit) a savep
141a6 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76 oint..** The sav
141a7 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73 epoint to releas
141a8 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 e or rollback ne
141a9 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f ed not be the mo
141aa 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20 st recently .**
141ab 63 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e created savepoin
141ac 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 t..**.** Paramet
141ad 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20 er op is always
141ae 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 either SAVEPOINT
141af 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56 _ROLLBACK or SAV
141b0 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a EPOINT_RELEASE..
141b1 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56 45 ** If it is SAVE
141b2 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 POINT_RELEASE, t
141b3 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20 hen release and
141b4 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 destroy the save
141b5 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e point with.** in
141b6 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 dex iSavepoint.
141b7 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 If it is SAVEPOI
141b8 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 NT_ROLLBACK, the
141b9 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 n rollback all c
141ba 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68 hanges.** that h
141bb 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e ave occurred sin
141bc 63 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ce the specified
141bd 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63 savepoint was c
141be 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 reated..**.** Th
141bf 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 e savepoint to r
141c0 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 ollback or relea
141c1 73 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 se is identified
141c2 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a by parameter .*
141c3 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20 * iSavepoint. A
141c4 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 value of 0 means
141c5 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 to operate on t
141c6 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 he outermost sav
141c7 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66 epoint.** (the f
141c8 69 72 73 74 20 63 72 65 61 74 65 64 29 2e 20 41 irst created). A
141c9 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72 value of (Pager
141ca 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d .nSavepoint-1) m
141cb 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20 eans operate.**
141cc 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 on the most rece
141cd 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 61 76 ntly created sav
141ce 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65 epoint. If iSave
141cf 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 point is greater
141d0 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e than.** (Pager.
141d1 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74 nSavepoint-1), t
141d2 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
141d3 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a n is a no-op..**
141d4 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76 .** If a negativ
141d5 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 e value is passe
141d6 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 d to this functi
141d7 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 on, then the cur
141d8 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 rent.** transact
141d9 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 ion is rolled ba
141da 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 ck. This is diff
141db 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67 erent to calling
141dc 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 .** sqlite3Page
141dd 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61 rRollback() beca
141de 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f use this functio
141df 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69 n does not termi
141e0 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e nate.** the tran
141e1 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63 saction or unloc
141e2 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 k the database,
141e3 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73 it just restores
141e4 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 the .** content
141e5 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 s of the databas
141e6 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 e to its origina
141e7 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 l state. .**.**
141e8 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c In any case, all
141e9 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68 savepoints with
141ea 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 an index greate
141eb 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e r than iSavepoin
141ec 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f t .** are destro
141ed 79 65 64 2e 20 49 66 20 74 68 69 73 20 69 73 20 yed. If this is
141ee 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74 a release operat
141ef 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49 ion (op==SAVEPOI
141f0 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20 NT_RELEASE),.**
141f1 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 then savepoint i
141f2 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73 Savepoint is als
141f3 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a o destroyed..**.
141f4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
141f5 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 may return SQLI
141f6 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65 TE_NOMEM if a me
141f7 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
141f8 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20 fails,.** or an
141f9 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 IO error code if
141fa 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
141fb 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e urs while rollin
141fc 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76 g back a .** sav
141fd 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72 epoint. If no er
141fe 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49 rors occur, SQLI
141ff 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
14200 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 d..*/ .SQLITE_PR
14201 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
14202 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 3PagerSavepoint(
14203 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
14204 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 nt op, int iSave
14205 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 point){. int rc
14206 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
14207 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 assert( op==SAV
14208 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c EPOINT_RELEASE |
14209 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f | op==SAVEPOINT_
1420a 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 73 ROLLBACK );. as
1420b 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74 sert( iSavepoint
1420c 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 >=0 || op==SAVEP
1420d 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b OINT_ROLLBACK );
1420e 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70 6f 69 .. if( iSavepoi
1420f 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 nt<pPager->nSave
14210 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 point ){. int
14211 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
14212 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 /* Iterator vari
14213 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 able */. int
14214 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 2f nNew; /
14215 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d 61 * Number of rema
14216 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 73 ining savepoints
14217 20 61 66 74 65 72 20 74 68 69 73 20 6f 70 2e 20 after this op.
14218 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 */.. /* Figur
14219 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 73 e out how many s
1421a 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 73 avepoints will s
1421b 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65 20 61 till be active a
1421c 66 74 65 72 20 74 68 69 73 0a 20 20 20 20 2a 2a fter this. **
1421d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f 72 operation. Stor
1421e 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 6e 20 e this value in
1421f 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65 65 20 nNew. Then free
14220 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 resources associ
14221 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 74 ated . ** wit
14222 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 h any savepoints
14223 20 74 68 61 74 20 61 72 65 20 64 65 73 74 72 6f that are destro
14224 79 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 yed by this oper
14225 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 ation.. */.
14226 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f nNew = iSavepo
14227 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 50 int + (op==SAVEP
14228 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a OINT_ROLLBACK);.
14229 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 3b for(ii=nNew;
1422a 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 ii<pPager->nSav
1422b 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 epoint; ii++){.
1422c 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
1422d 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 ecDestroy(pPager
1422e 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d ->aSavepoint[ii]
1422f 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a .pInSavepoint);.
14230 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 }. pPager
14231 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e ->nSavepoint = n
14232 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 New;.. /* If
14233 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 this is a rollba
14234 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c ck operation, pl
14235 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 69 ayback the speci
14236 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a fied savepoint..
14237 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 ** If this i
14238 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69 s a temp-file, i
14239 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 t is possible th
1423a 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 at the journal f
1423b 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e ile has. ** n
1423c 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e ot yet been open
1423d 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
1423e 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 6e there have been
1423f 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 no changes to.
14240 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ** the databa
14241 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20 se file, so the
14242 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 playback operati
14243 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 on can be skippe
14244 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 d.. */. if
14245 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f ( op==SAVEPOINT_
14246 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f 70 ROLLBACK && isOp
14247 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
14248 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 53 61 ){. PagerSa
14249 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f vepoint *pSavepo
1424a 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 3f int = (nNew==0)?
1424b 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 0:&pPager->aSave
1424c 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a 20 point[nNew-1];.
1424d 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 rc = pagerP
1424e 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 laybackSavepoint
1424f 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 70 6f (pPager, pSavepo
14250 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 int);. asse
14251 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f rt(rc!=SQLITE_DO
14252 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 NE);. }. .
14253 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
14254 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65 a release of the
14255 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70 outermost savep
14256 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a oint, truncate .
14257 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a ** the sub-j
14258 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62 ournal to zero b
14259 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f ytes in size. */
1425a 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30 . if( nNew==0
1425b 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e && op==SAVEPOIN
1425c 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69 73 4f T_RELEASE && isO
1425d 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 pen(pPager->sjfd
1425e 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ) ){. asser
1425f 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
14260 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 );. rc = s
14261 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
14262 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 (pPager->sjfd, 0
14263 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d );. pPager-
14264 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 20 >nSubRec = 0;.
14265 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
14266 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
14267 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 turn the full pa
14268 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 thname of the da
14269 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a tabase file..*/.
1426a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
1426b 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
1426c 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 e3PagerFilename(
1426d 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
1426e 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
1426f 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f >zFilename;.}../
14270 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
14271 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f VFS structure fo
14272 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a r the pager..*/.
14273 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
14274 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 onst sqlite3_vfs
14275 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 *sqlite3PagerVf
14276 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 s(Pager *pPager)
14277 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
14278 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a r->pVfs;.}../*.*
14279 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c * Return the fil
1427a 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 e handle for the
1427b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
1427c 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
1427d 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 h the pager. Th
1427e 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 is might return
1427f 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65 NULL if the file
14280 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 has.** not yet
14281 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a been opened..*/.
14282 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
14283 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c qlite3_file *sql
14284 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 ite3PagerFile(Pa
14285 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
14286 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66 return pPager->f
14287 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 d;.}../*.** Retu
14288 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 rn the full path
14289 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 name of the jour
1428a 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c nal file..*/.SQL
1428b 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
1428c 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 t char *sqlite3P
1428d 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 agerJournalname(
1428e 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
1428f 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
14290 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a >zJournal;.}../*
14291 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
14292 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c 6c 73 if fsync() calls
14293 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 66 6f are disabled fo
14294 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 52 r this pager. R
14295 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 eturn FALSE.** i
14296 66 20 66 73 79 6e 63 28 29 73 20 61 72 65 20 65 f fsync()s are e
14297 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79 xecuted normally
14298 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14299 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
1429a 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 agerNosync(Pager
1429b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 *pPager){. ret
1429c 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 urn pPager->noSy
1429d 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 nc;.}..#ifdef SQ
1429e 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f LITE_HAS_CODEC./
1429f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 72 65 74 72 *.** Set or retr
142a0 69 65 76 65 20 74 68 65 20 63 6f 64 65 63 20 66 ieve the codec f
142a1 6f 72 20 74 68 69 73 20 70 61 67 65 72 0a 2a 2f or this pager.*/
142a2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
142a3 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 ite3PagerSetCode
142a4 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 c(. Pager *pPag
142a5 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 er,. void *(*xC
142a6 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 odec)(void*,void
142a7 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 *,Pgno,int),. v
142a8 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65 oid (*xCodecSize
142a9 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c Chng)(void*,int,
142aa 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 int),. void (*x
142ab 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a CodecFree)(void*
142ac 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 ),. void *pCode
142ad 63 0a 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 c.){. if( pPage
142ae 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 29 20 r->xCodecFree )
142af 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 pPager->xCodecFr
142b0 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 ee(pPager->pCode
142b1 63 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 c);. pPager->xC
142b2 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a 20 odec = xCodec;.
142b3 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 pPager->xCodecS
142b4 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64 65 63 izeChng = xCodec
142b5 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50 61 67 SizeChng;. pPag
142b6 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 3d er->xCodecFree =
142b7 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20 20 70 xCodecFree;. p
142b8 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20 3d 20 Pager->pCodec =
142b9 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65 72 52 pCodec;. pagerR
142ba 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72 eportSize(pPager
142bb 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 );.}.static void
142bc 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *sqlite3PagerGe
142bd 74 43 6f 64 65 63 28 50 61 67 65 72 20 2a 70 50 tCodec(Pager *pP
142be 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
142bf 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 3b 0a pPager->pCodec;.
142c0 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 }.#endif..#ifnde
142c1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
142c2 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d TOVACUUM./*.** M
142c3 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50 67 ove the page pPg
142c4 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e to location pgn
142c5 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a o in the file..*
142c6 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 *.** There must
142c7 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73 be no references
142c8 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 72 65 to the page pre
142c9 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 viously located
142ca 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63 at.** pgno (whic
142cb 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64 h we call pPgOld
142cc 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 70 61 ) though that pa
142cd 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f ge is allowed to
142ce 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e be.** in cache.
142cf 20 20 49 66 20 74 68 65 20 70 61 67 65 20 70 72 If the page pr
142d0 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 eviously located
142d1 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20 at pgno is not
142d2 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68 already.** in th
142d3 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e e rollback journ
142d4 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75 al, it is not pu
142d5 74 20 74 68 65 72 65 20 62 79 20 62 79 20 74 68 t there by by th
142d6 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a is routine..**.*
142d7 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20 * References to
142d8 74 68 65 20 70 61 67 65 20 70 50 67 20 72 65 6d the page pPg rem
142d9 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74 ain valid. Updat
142da 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d ing any.** meta-
142db 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 data associated
142dc 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64 with pPg (i.e. d
142dd 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 ata stored in th
142de 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a e nExtra bytes.*
142df 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e * allocated alon
142e0 67 20 77 69 74 68 20 74 68 65 20 70 61 67 65 29 g with the page)
142e1 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 is the responsi
142e2 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 bility of the ca
142e3 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 ller..**.** A tr
142e4 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
142e5 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 74 68 e active when th
142e6 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
142e7 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f lled. It used to
142e8 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 be.** required
142e9 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74 that a statement
142ea 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 transaction was
142eb 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74 not active, but
142ec 20 74 68 69 73 20 72 65 73 74 72 69 63 74 69 6f this restrictio
142ed 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65 n.** has been re
142ee 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 49 4e moved (CREATE IN
142ef 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76 DEX needs to mov
142f0 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 61 20 e a page when a
142f1 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 statement.** tra
142f2 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
142f3 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ve)..**.** If th
142f4 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e e fourth argumen
142f5 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 20 t, isCommit, is
142f6 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 non-zero, then t
142f7 68 69 73 20 70 61 67 65 20 69 73 20 62 65 69 6e his page is bein
142f8 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 61 g.** moved as pa
142f9 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 rt of a database
142fa 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e 20 reorganization
142fb 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 just before the
142fc 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 transaction .**
142fd 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74 is being committ
142fe 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
142ff 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 , it is guarante
14300 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 ed that the data
14301 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 50 base page .** pP
14302 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 6c 6c g refers to will
14303 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 20 not be written
14304 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 to again within
14305 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e this transaction
14306 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
14307 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e ction may return
14308 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 SQLITE_NOMEM or
14309 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
1430a 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a e if an error.**
1430b 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 77 69 occurs. Otherwi
1430c 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53 se, it returns S
1430d 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c QLITE_OK..*/.SQL
1430e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1430f 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 sqlite3PagerMove
14310 70 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 page(Pager *pPag
14311 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c er, DbPage *pPg,
14312 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 Pgno pgno, int
14313 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48 isCommit){. PgH
14314 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20 dr *pPgOld;
14315 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
14316 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72 page being over
14317 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 written. */. Pg
14318 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 no needSyncPgno
14319 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c = 0; /* Ol
1431a 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e d value of pPg->
1431b 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73 pgno, if sync is
1431c 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 required */. i
1431d 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
1431e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1431f 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
14320 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 20 Pgno origPgno;
14321 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14322 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 The original pag
14323 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 61 e number */.. a
14324 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 ssert( pPg->nRef
14325 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 >0 );.. /* If t
14326 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f he page being mo
14327 76 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64 ved is dirty and
14328 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61 has not been sa
14329 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73 ved by the lates
1432a 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 t. ** savepoint
1432b 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20 , then save the
1432c 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 current contents
1432d 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74 of the page int
1432e 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d o the . ** sub-
1432f 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69 journal now. Thi
14330 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f s is required to
14331 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c handle the foll
14332 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a owing scenario:.
14333 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 **. ** BEGI
14334 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 N;. ** <jou
14335 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65 rnal page X, the
14336 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d n modify it in m
14337 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20 emory>. **
14338 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 SAVEPOINT one;.
14339 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20 ** <Move
1433a 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69 page X to locati
1433b 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52 on Y>. ** R
1433c 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a OLLBACK TO one;.
1433d 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67 **. ** If pag
1433e 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69 e X were not wri
1433f 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d tten to the sub-
14340 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74 journal here, it
14341 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20 would not. **
14342 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 be possible to r
14343 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65 estore its conte
14344 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f nts when the "RO
14345 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20 LLBACK TO one".
14346 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 ** statement we
14347 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e re is processed.
14348 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f . **. ** subjo
14349 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20 urnalPage() may
1434a 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 need to allocate
1434b 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 space to store
1434c 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20 pPg->pgno into.
1434d 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 ** one or more
1434e 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 savepoint bitvec
1434f 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72 s. This is the r
14350 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 eason this funct
14351 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74 ion. ** may ret
14352 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
14353 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 .. */. if( pPg
14354 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
14355 52 54 59 20 0a 20 20 20 26 26 20 73 75 62 6a 52 RTY . && subjR
14356 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 equiresPage(pPg)
14357 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b . && SQLITE_OK
14358 21 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e !=(rc = subjourn
14359 61 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 29 alPage(pPg)). )
1435a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
1435b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 . }.. PAGERTRA
1435c 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67 CE(("MOVE %d pag
1435d 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25 e %d (needSync=%
1435e 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e d) moves to %d\n
1435f 22 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 49 ", . PAGERI
14360 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e D(pPager), pPg->
14361 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67 pgno, (pPg->flag
14362 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
14363 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a C)?1:0, pgno));.
14364 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45 IOTRACE(("MOVE
14365 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50 %p %d %d\n", pP
14366 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c ager, pPg->pgno,
14367 20 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66 pgno)).. /* If
14368 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 the journal nee
14369 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 ds to be sync()e
1436a 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 50 d before page pP
1436b 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a g->pgno can. **
1436c 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 be written to,
1436d 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 store pPg->pgno
1436e 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c in local variabl
1436f 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a e needSyncPgno..
14370 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 **. ** If the
14371 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 isCommit flag i
14372 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 20 s set, there is
14373 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d no need to remem
14374 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 ber that. ** th
14375 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 e journal needs
14376 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 to be sync()ed b
14377 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 efore database p
14378 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20 age pPg->pgno .
14379 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 ** can be writt
1437a 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 en to. The calle
1437b 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 r has already pr
1437c 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 omised not to wr
1437d 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a ite to it.. */.
1437e 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 if( (pPg->flag
1437f 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
14380 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20 C) && !isCommit
14381 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50 ){. needSyncP
14382 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b gno = pPg->pgno;
14383 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 . assert( pag
14384 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 eInJournal(pPg)
14385 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 || pPg->pgno>pPa
14386 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
14387 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
14388 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
14389 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 73 DIRTY );. ass
1438a 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 ert( pPager->nee
1438b 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 dSync );. }..
1438c 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20 /* If the cache
1438d 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20 contains a page
1438e 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 with page-number
1438f 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74 pgno, remove it
14390 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68 . ** from its h
14391 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c ash chain. Also,
14392 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65 if the PgHdr.ne
14393 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 66 edSync was set f
14394 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67 or . ** page pg
14395 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d no before the 'm
14396 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20 ove' operation,
14397 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 it needs to be r
14398 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f etained . ** fo
14399 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64 r the page moved
1439a 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70 there.. */. p
1439b 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 Pg->flags &= ~PG
1439c 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 HDR_NEED_SYNC;.
1439d 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f pPgOld = pager_
1439e 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 lookup(pPager, p
1439f 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 gno);. assert(
143a0 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c !pPgOld || pPgOl
143a1 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 d->nRef==1 );.
143a2 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20 if( pPgOld ){.
143a3 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 pPg->flags |=
143a4 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 50 (pPgOld->flags&P
143a5 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b GHDR_NEED_SYNC);
143a6 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 . sqlite3Pcac
143a7 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a heDrop(pPgOld);.
143a8 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e 6f 20 }.. origPgno
143a9 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 73 = pPg->pgno;. s
143aa 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 qlite3PcacheMove
143ab 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 20 73 (pPg, pgno);. s
143ac 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
143ad 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50 Dirty(pPg);. pP
143ae 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
143af 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65 = 1;.. if( nee
143b0 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20 dSyncPgno ){.
143b1 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50 /* If needSyncP
143b2 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c gno is non-zero,
143b3 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 then the journa
143b4 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20 l file needs to
143b5 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28 be . ** sync(
143b6 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64 )ed before any d
143b7 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 ata is written t
143b8 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 o database file
143b9 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e page needSyncPgn
143ba 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e o.. ** Curren
143bb 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67 tly, no such pag
143bc 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 e exists in the
143bd 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74 page-cache and t
143be 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a he . ** "is j
143bf 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63 ournaled" bitvec
143c0 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73 flag has been s
143c1 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74 et. This needs t
143c2 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79 o be remedied by
143c3 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20 . ** loading
143c4 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 the page into th
143c5 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e e pager-cache an
143c6 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67 d setting the Pg
143c7 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20 Hdr.needSync .
143c8 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a ** flag.. *
143c9 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 *. ** If the
143ca 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20 attempt to load
143cb 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 the page into th
143cc 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69 e page-cache fai
143cd 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20 ls, (due. **
143ce 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 to a malloc() or
143cf 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c IO failure), cl
143d0 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74 ear the bit in t
143d1 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a he pInJournal[].
143d2 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74 ** array. Ot
143d3 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 herwise, if the
143d4 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61 page is loaded a
143d5 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e nd written again
143d6 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 in. ** this
143d7 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 transaction, it
143d8 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74 may be written t
143d9 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
143da 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a ile before. *
143db 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69 * it is synced i
143dc 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 nto the journal
143dd 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20 file. This way,
143de 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e it may end up in
143df 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 . ** the jour
143e0 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20 nal file twice,
143e1 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20 but that is not
143e2 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a a problem.. *
143e3 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c *. ** The sql
143e4 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63 ite3PagerGet() c
143e5 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68 all may cause th
143e6 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e e journal to syn
143e7 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a c. So make. *
143e8 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72 * sure the Pager
143e9 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 .needSync flag i
143ea 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a s set too.. *
143eb 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 /. PgHdr *pPg
143ec 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 Hdr;. assert(
143ed 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
143ee 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 c );. rc = sq
143ef 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 lite3PagerGet(pP
143f0 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67 ager, needSyncPg
143f1 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20 no, &pPgHdr);.
143f2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
143f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 _OK ){. if(
143f4 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 3c 3d 70 needSyncPgno<=p
143f5 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
143f6 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 e ){. ass
143f7 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 54 6d ert( pPager->pTm
143f8 70 53 70 61 63 65 21 3d 30 20 29 3b 0a 20 20 20 pSpace!=0 );.
143f9 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
143fa 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e ecClear(pPager->
143fb 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64 pInJournal, need
143fc 53 79 6e 63 50 67 6e 6f 2c 20 70 50 61 67 65 72 SyncPgno, pPager
143fd 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 ->pTmpSpace);.
143fe 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
143ff 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
14400 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
14401 63 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 c = 1;. asser
14402 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e t( pPager->noSyn
14403 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44 42 20 29 c==0 && !MEMDB )
14404 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e 66 6c ;. pPgHdr->fl
14405 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 ags |= PGHDR_NEE
14406 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71 6c 69 D_SYNC;. sqli
14407 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 te3PcacheMakeDir
14408 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 ty(pPgHdr);.
14409 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1440a 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a f(pPgHdr);. }..
1440b 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f 72 20 61 6e /*. ** For an
1440c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
1440d 61 73 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 74 ase, make sure t
1440e 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 he original page
1440f 20 63 6f 6e 74 69 6e 75 65 73 0a 20 20 2a 2a 20 continues. **
14410 74 6f 20 65 78 69 73 74 2c 20 69 6e 20 63 61 73 to exist, in cas
14411 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f e the transactio
14412 6e 20 6e 65 65 64 73 20 74 6f 20 72 6f 6c 6c 20 n needs to roll
14413 62 61 63 6b 2e 20 20 57 65 20 61 6c 6c 6f 63 61 back. We alloca
14414 74 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 te. ** the page
14415 20 6e 6f 77 2c 20 69 6e 73 74 65 61 64 20 6f 66 now, instead of
14416 20 61 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 62 65 at rollback, be
14417 63 61 75 73 65 20 77 65 20 63 61 6e 20 62 65 74 cause we can bet
14418 74 65 72 20 64 65 61 6c 0a 20 20 2a 2a 20 77 69 ter deal. ** wi
14419 74 68 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d th an out-of-mem
1441a 6f 72 79 20 65 72 72 6f 72 20 6e 6f 77 2e 20 20 ory error now.
1441b 54 69 63 6b 65 74 20 23 33 37 36 31 2e 0a 20 20 Ticket #3761..
1441c 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29 */. if( MEMDB )
1441d 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 4e {. DbPage *pN
1441e 65 77 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ew;. rc = sql
1441f 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 ite3PagerAcquire
14420 28 70 50 61 67 65 72 2c 20 6f 72 69 67 50 67 6e (pPager, origPgn
14421 6f 2c 20 26 70 4e 65 77 2c 20 31 29 3b 0a 20 20 o, &pNew, 1);.
14422 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
14423 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
14424 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 ite3PcacheMove(p
14425 50 67 2c 20 6f 72 69 67 50 67 6e 6f 29 3b 0a 20 Pg, origPgno);.
14426 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
14427 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
14428 33 50 61 67 65 72 55 6e 72 65 66 28 70 4e 65 77 3PagerUnref(pNew
14429 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e );. }.. return
1442a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
1442b 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
1442c 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
1442d 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 the data for the
1442e 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e specified page.
1442f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14430 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
14431 50 61 67 65 72 47 65 74 44 61 74 61 28 44 62 50 PagerGetData(DbP
14432 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 age *pPg){. ass
14433 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 ert( pPg->nRef>0
14434 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d || pPg->pPager-
14435 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75 >memDb );. retu
14436 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d rn pPg->pData;.}
14437 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
14438 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
14439 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74 Pager.nExtra byt
1443a 65 73 20 6f 66 20 22 65 78 74 72 61 22 20 73 70 es of "extra" sp
1443b 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 ace .** allocate
1443c 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 d along with the
1443d 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e specified page.
1443e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1443f 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
14440 50 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62 PagerGetExtra(Db
14441 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 Page *pPg){. re
14442 74 75 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61 turn pPg->pExtra
14443 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 ;.}../*.** Get/s
14444 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d et the locking-m
14445 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 ode for this pag
14446 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d er. Parameter eM
14447 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a ode must be one.
14448 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b ** of PAGER_LOCK
14449 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 INGMODE_QUERY, P
1444a 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
1444b 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 _NORMAL or .** P
1444c 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
1444d 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 _EXCLUSIVE. If t
1444e 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 he parameter is
1444f 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e not _QUERY, then
14450 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d .** the locking-
14451 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 mode is set to t
14452 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 he value specifi
14453 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 ed..**.** The re
14454 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 turned value is
14455 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 either PAGER_LOC
14456 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 KINGMODE_NORMAL
14457 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b or.** PAGER_LOCK
14458 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
14459 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 E, indicating th
1445a 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 e current (possi
1445b 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 bly updated).**
1445c 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f locking-mode..*/
1445d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1445e 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
1445f 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 LockingMode(Page
14460 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 r *pPager, int e
14461 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Mode){. assert(
14462 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f eMode==PAGER_LO
14463 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a CKINGMODE_QUERY.
14464 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
14465 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b Mode==PAGER_LOCK
14466 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 INGMODE_NORMAL.
14467 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
14468 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 ode==PAGER_LOCKI
14469 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
1446a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 );. assert( PA
1446b 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
1446c 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 QUERY<0 );. ass
1446d 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 ert( PAGER_LOCKI
1446e 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 NGMODE_NORMAL>=0
1446f 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e && PAGER_LOCKIN
14470 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e GMODE_EXCLUSIVE>
14471 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 =0 );. if( eMod
14472 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d e>=0 && !pPager-
14473 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 >tempFile ){.
14474 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
14475 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f veMode = (u8)eMo
14476 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e de;. }. return
14477 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 (int)pPager->ex
14478 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a clusiveMode;.}..
14479 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 /*.** Get/set th
1447a 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 e journal-mode f
1447b 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 or this pager. P
1447c 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d arameter eMode m
1447d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a ust be one of:.*
1447e 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f *.** PAGER_JO
1447f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a URNALMODE_QUERY.
14480 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
14481 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a NALMODE_DELETE.*
14482 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
14483 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a ALMODE_TRUNCATE.
14484 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
14485 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a NALMODE_PERSIST.
14486 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
14487 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 NALMODE_OFF.**
14488 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d PAGER_JOURNALM
14489 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a ODE_MEMORY.**.**
1448a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 If the paramete
1448b 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c r is not _QUERY,
1448c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 then the journa
1448d 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f l_mode is set to
1448e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 the.** value sp
1448f 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63 ecified if the c
14490 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 hange is allowed
14491 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 73 . The change is
14492 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66 disallowed.** f
14493 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 or the following
14494 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 reasons:.**.**
14495 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 * An in-memor
14496 79 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f y database can o
14497 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 nly have its jou
14498 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f rnal_mode set to
14499 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 _OFF.** or
1449a 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 _MEMORY..**.**
1449b 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c * The journal
1449c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65 mode may not be
1449d 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61 changed while a
1449e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1449f 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 active..**.** Th
144a0 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 e returned indic
144a1 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ate the current
144a2 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 (possibly update
144a3 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e d) journal-mode.
144a4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
144a5 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
144a6 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 gerJournalMode(P
144a7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
144a8 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 t eMode){. asse
144a9 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 rt( eMode==PAGER
144aa 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 _JOURNALMODE_QUE
144ab 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c RY. |
144ac 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
144ad 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 OURNALMODE_DELET
144ae 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c E. ||
144af 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f eMode==PAGER_JO
144b0 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
144b1 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c TE. |
144b2 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
144b3 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 OURNALMODE_PERSI
144b4 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c ST. |
144b5 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
144b6 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a OURNALMODE_OFF .
144b7 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
144b8 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
144b9 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 NALMODE_MEMORY )
144ba 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 ;. assert( PAGE
144bb 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
144bc 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65 ERY<0 );. if( e
144bd 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21 Mode>=0. && (!
144be 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d MEMDB || eMode==
144bf 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
144c0 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 E_MEMORY .
144c1 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 || eMode
144c2 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
144c3 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21 ODE_OFF). && !
144c4 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
144c5 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65 ed. && (!isOpe
144c6 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c n(pPager->jfd) |
144c7 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 | 0==pPager->jou
144c8 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20 rnalOff). ){.
144c9 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
144ca 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 ger->jfd) ){.
144cb 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
144cc 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a e(pPager->jfd);.
144cd 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 }. pPager
144ce 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 ->journalMode =
144cf 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 (u8)eMode;. }.
144d0 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 return (int)pPa
144d1 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
144d2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 ;.}../*.** Get/s
144d3 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 et the size-limi
144d4 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 69 t used for persi
144d5 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 stent journal fi
144d6 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 les..**.** Setti
144d7 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 ng the size limi
144d8 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f t to -1 means no
144d9 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63 limit is enforc
144da 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 ed..** An attemp
144db 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74 t to set a limit
144dc 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31 smaller than -1
144dd 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
144de 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
144df 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 64 sqlite3PagerJ
144e0 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 ournalSizeLimit(
144e1 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
144e2 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 64 iLimit){. if
144e3 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a ( iLimit>=-1 ){.
144e4 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
144e5 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 nalSizeLimit = i
144e6 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 Limit;. }. ret
144e7 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 urn pPager->jour
144e8 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a nalSizeLimit;.}.
144e9 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
144ea 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
144eb 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 Pager->pBackup v
144ec 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 ariable. The bac
144ed 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e kup module.** in
144ee 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 backup.c mainta
144ef 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ins the content
144f0 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 of this variable
144f1 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a . This module.**
144f2 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c uses it opaquel
144f3 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 y as an argument
144f4 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 to sqlite3Backu
144f5 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a pRestart() and.*
144f6 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 * sqlite3BackupU
144f7 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f pdate() only..*/
144f8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
144f9 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
144fa 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 *sqlite3PagerBac
144fb 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 kupPtr(Pager *pP
144fc 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
144fd 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 &pPager->pBackup
144fe 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
144ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
14500 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a O */../*********
14501 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 ***** End of pag
14502 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
14503 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14504 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14505 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
14506 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
14507 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
14508 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14509 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1450a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
1450b 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a August 27.**.**
1450c 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1450d 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1450e 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1450f 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
14510 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
14511 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
14512 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
14513 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
14514 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
14515 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
14516 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
14517 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
14518 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
14519 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1451a 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
1451b 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
1451c 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1451d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1451e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1451f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14521 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
14522 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76 $Id: btmutex.c,v
14523 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f 32 30 1.17 2009/07/20
14524 20 31 32 3a 33 33 3a 33 33 20 64 72 68 20 45 78 12:33:33 drh Ex
14525 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 p $.**.** This f
14526 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
14527 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d e used to implem
14528 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e 20 42 ent mutexes on B
14529 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a tree objects..**
1452a 20 54 68 69 73 20 63 6f 64 65 20 72 65 61 6c 6c This code reall
1452b 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74 72 y belongs in btr
1452c 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72 65 65 ee.c. But btree
1452d 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20 74 6f .c is getting to
1452e 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 20 o.** big and we
1452f 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 74 want to break it
14530 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 69 down some. Thi
14531 73 20 70 61 63 6b 61 67 65 64 20 73 65 65 6d 65 s packaged seeme
14532 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f 64 d like.** a good
14533 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f 2a breakout..*/./*
14534 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
14535 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 2e 68 clude btreeInt.h
14536 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
14537 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a f btmutex.c ****
14538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
14539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
1453a 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 49 6e gin file btreeIn
1453b 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
1453c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1453d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
1453e 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36 .** 2004 April 6
1453f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
14540 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
14541 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
14542 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
14543 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
14544 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
14545 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
14546 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
14547 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
14548 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
14549 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1454a 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1454b 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1454c 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1454d 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1454e 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1454f 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
14550 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
14551 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14552 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14553 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
14555 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49 6e 74 ** $Id: btreeInt
14556 2e 68 2c 76 20 31 2e 35 32 20 32 30 30 39 2f 30 .h,v 1.52 2009/0
14557 37 2f 31 35 20 31 37 3a 32 35 3a 34 36 20 64 72 7/15 17:25:46 dr
14558 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 h Exp $.**.** Th
14559 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e is file implemen
1455a 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 ts a external (d
1455b 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 isk-based) datab
1455c 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 ase using BTrees
1455d 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69 ..** For a detai
1455e 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f led discussion o
1455f 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20 f BTrees, refer
14560 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e to.**.** Don
14561 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48 ald E. Knuth, TH
14562 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45 E ART OF COMPUTE
14563 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56 R PROGRAMMING, V
14564 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20 olume 3:.**
14565 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61 "Sorting And Sea
14566 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34 rching", pages 4
14567 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d 73-480. Addison-
14568 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75 Wesley.** Pu
14569 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79 blishing Company
1456a 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61 , Reading, Massa
1456b 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 chusetts..**.**
1456c 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69 The basic idea i
1456d 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65 s that each page
1456e 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e of the file con
1456f 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65 tains N database
14570 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20 .** entries and
14571 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 N+1 pointers to
14572 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 subpages..**.**
14573 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
14574 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14575 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14576 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14577 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30 --.** | Ptr(0
14578 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72 ) | Key(0) | Ptr
14579 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e (1) | Key(1) | .
1457a 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20 .. | Key(N-1) |
1457b 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d Ptr(N) |.** --
1457c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1457d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1457e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1457f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
14580 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 *.** All of the
14581 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65 keys on the page
14582 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69 that Ptr(0) poi
14583 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75 nts to have valu
14584 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 es less.** than
14585 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20 Key(0). All of
14586 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65 the keys on page
14587 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20 Ptr(1) and its
14588 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a subpages have.**
14589 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 values greater
1458a 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20 than Key(0) and
1458b 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29 less than Key(1)
1458c 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 . All of the ke
1458d 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20 ys.** on Ptr(N)
1458e 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 and its subpages
1458f 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65 have values gre
14590 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d ater than Key(N-
14591 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66 1). And.** so f
14592 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 orth..**.** Find
14593 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 ing a particular
14594 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65 key requires re
14595 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20 ading O(log(M))
14596 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a pages from the .
14597 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20 ** disk where M
14598 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
14599 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
1459a 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 tree..**.** In t
1459b 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
1459c 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c on, a single fil
1459d 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f e can hold one o
1459e 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 r more separate
1459f 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63 .** BTrees. Eac
145a0 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74 h BTree is ident
145a1 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64 ified by the ind
145a2 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70 ex of its root p
145a3 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79 age. The.** key
145a4 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e and data for an
145a5 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62 y entry are comb
145a6 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 ined to form the
145a7 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a "payload". A.*
145a8 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f * fixed amount o
145a9 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65 f payload can be
145aa 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c carried directl
145ab 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 y on the databas
145ac 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74 e.** page. If t
145ad 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61 he payload is la
145ae 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 rger than the pr
145af 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e eset amount then
145b0 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65 surplus.** byte
145b1 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 s are stored on
145b2 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
145b3 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 The payload for
145b4 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64 an entry.** and
145b5 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70 the preceding p
145b6 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69 ointer are combi
145b7 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43 ned to form a "C
145b8 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20 ell". Each .**
145b9 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c page has a small
145ba 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f header which co
145bb 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e ntains the Ptr(N
145bc 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74 ) pointer and ot
145bd 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 her.** informati
145be 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73 on such as the s
145bf 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 ize of key and d
145c0 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 ata..**.** FORMA
145c1 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 T DETAILS.**.**
145c2 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69 The file is divi
145c3 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20 ded into pages.
145c4 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 The first page
145c5 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 is called page 1
145c6 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 ,.** the second
145c7 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73 is page 2, and s
145c8 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65 o forth. A page
145c9 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 number of zero
145ca 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f indicates.** "no
145cb 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68 such page". Th
145cc 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 e page size can
145cd 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 77 be anything betw
145ce 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 een 512 and 6553
145cf 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20 6..** Each page
145d0 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20 can be either a
145d1 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72 btree page, a fr
145d2 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 61 eelist page or a
145d3 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 n overflow.** pa
145d4 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ge..**.** The fi
145d5 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 61 rst page is alwa
145d6 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65 2e ys a btree page.
145d7 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30 20 The first 100
145d8 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 72 bytes of the fir
145d9 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 st.** page conta
145da 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 61 in a special hea
145db 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 68 der (the "file h
145dc 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65 73 eader") that des
145dd 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 2e cribes the file.
145de 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f .** The format o
145df 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 65 f the file heade
145e0 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a r is as follows:
145e1 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 .**.** OFFSET
145e2 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 SIZE DESCRI
145e3 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 PTION.** 0
145e4 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61 64 16 Head
145e5 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c 69 er string: "SQLi
145e6 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 22 te format 3\000"
145e7 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 20 .** 16
145e8 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 65 2 Page size
145e9 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20 in bytes. .**
145ea 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20 20 18 1
145eb 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 77 File format w
145ec 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 rite version.**
145ed 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20 20 19 1
145ee 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 72 File format r
145ef 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 ead version.**
145f0 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20 20 1
145f1 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 65 Bytes of unuse
145f2 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65 d space at the e
145f3 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 0a nd of each page.
145f4 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 20 ** 21
145f5 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 64 1 Max embedd
145f6 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ed payload fract
145f7 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 20 ion.** 22
145f8 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 6d 1 Min em
145f9 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 bedded payload f
145fa 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 raction.** 2
145fb 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 3 1 Mi
145fc 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66 n leaf payload f
145fd 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 raction.** 2
145fe 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46 69 4 4 Fi
145ff 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 le change counte
14600 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 20 r.** 28
14601 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65 64 4 Reserved
14602 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 0a for future use.
14603 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 20 ** 32
14604 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65 65 4 First free
14605 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 20 list page.**
14606 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20 20 36 4
14607 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69 Number of freeli
14608 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 st pages in the
14609 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 20 file.** 40
1460a 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34 2d 60 15 4-
1460b 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 73 byte meta values
1460c 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 65 passed to highe
1460d 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 20 r layers.**.**
1460e 20 20 20 34 30 20 20 20 20 20 20 20 34 20 20 20 40 4
1460f 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a Schema cookie.
14610 2a 2a 20 20 20 20 20 34 34 20 20 20 20 20 20 20 ** 44
14611 34 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 4 File forma
14612 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65 t of schema laye
14613 72 0a 2a 2a 20 20 20 20 20 34 38 20 20 20 20 20 r.** 48
14614 20 20 34 20 20 20 20 20 53 69 7a 65 20 6f 66 20 4 Size of
14615 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 20 20 page cache.**
14616 20 20 35 32 20 20 20 20 20 20 20 34 20 20 20 20 52 4
14617 20 4c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 Largest root-pa
14618 67 65 20 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61 ge (auto/incr_va
14619 63 75 75 6d 29 0a 2a 2a 20 20 20 20 20 35 36 20 cuum).** 56
1461a 20 20 20 20 20 20 34 20 20 20 20 20 31 3d 55 54 4 1=UT
1461b 46 2d 38 20 32 3d 55 54 46 31 36 6c 65 20 33 3d F-8 2=UTF16le 3=
1461c 55 54 46 31 36 62 65 0a 2a 2a 20 20 20 20 20 36 UTF16be.** 6
1461d 30 20 20 20 20 20 20 20 34 20 20 20 20 20 55 73 0 4 Us
1461e 65 72 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 er version.**
1461f 20 20 36 34 20 20 20 20 20 20 20 34 20 20 20 20 64 4
14620 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 Incremental vac
14621 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20 20 20 20 20 uum mode.**
14622 36 38 20 20 20 20 20 20 20 34 20 20 20 20 20 75 68 4 u
14623 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 32 20 nused.** 72
14624 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73 4 unus
14625 65 64 0a 2a 2a 20 20 20 20 20 37 36 20 20 20 20 ed.** 76
14626 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 64 0a 4 unused.
14627 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 **.** All of the
14628 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 integer values
14629 61 72 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 28 are big-endian (
1462a 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 most significant
1462b 20 62 79 74 65 20 66 69 72 73 74 29 2e 0a 2a 2a byte first)..**
1462c 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 63 68 61 .** The file cha
1462d 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 69 nge counter is i
1462e 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 20 ncremented when
1462f 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
14630 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68 69 73 20 changed.** This
14631 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 73 20 6f counter allows o
14632 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 ther processes t
14633 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74 68 65 20 o know when the
14634 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 file has changed
14635 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20 77 68 65 .** and thus whe
14636 6e 20 74 68 65 79 20 6e 65 65 64 20 74 6f 20 66 n they need to f
14637 6c 75 73 68 20 74 68 65 69 72 20 63 61 63 68 65 lush their cache
14638 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 20 ..**.** The max
14639 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
1463a 20 66 72 61 63 74 69 6f 6e 20 69 73 20 74 68 65 fraction is the
1463b 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 amount of the t
1463c 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a 2a 20 73 otal usable.** s
1463d 70 61 63 65 20 69 6e 20 61 20 70 61 67 65 20 74 pace in a page t
1463e 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6e 73 75 hat can be consu
1463f 6d 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 med by a single
14640 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e 64 61 72 cell for standar
14641 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e d.** B-tree (non
14642 2d 4c 45 41 46 44 41 54 41 29 20 74 61 62 6c 65 -LEAFDATA) table
14643 73 2e 20 20 41 20 76 61 6c 75 65 20 6f 66 20 32 s. A value of 2
14644 35 35 20 6d 65 61 6e 73 20 31 30 30 25 2e 20 20 55 means 100%.
14645 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 The default.** i
14646 73 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d s to limit the m
14647 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 aximum cell size
14648 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 so that at leas
14649 74 20 34 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66 t 4 cells will f
1464a 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67 it.** on one pag
1464b 65 2e 20 20 54 68 75 73 20 74 68 65 20 64 65 66 e. Thus the def
1464c 61 75 6c 74 20 6d 61 78 20 65 6d 62 65 64 64 65 ault max embedde
1464d 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
1464e 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a 2a 2a 20 on is 64..**.**
1464f 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 66 If the payload f
14650 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 6c 61 72 or a cell is lar
14651 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 ger than the max
14652 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e 20 65 payload, then e
14653 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 xtra.** payload
14654 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 6f 76 is spilled to ov
14655 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 4f erflow pages. O
14656 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 nce an overflow
14657 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 page is allocate
14658 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 20 62 79 d,.** as many by
14659 74 65 73 20 61 73 20 70 6f 73 73 69 62 6c 65 20 tes as possible
1465a 61 72 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 are moved into t
1465b 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 he overflow page
1465c 73 20 77 69 74 68 6f 75 74 20 6c 65 74 74 69 6e s without lettin
1465d 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 73 69 g.** the cell si
1465e 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77 20 74 68 ze drop below th
1465f 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 e min embedded p
14660 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 2e ayload fraction.
14661 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 20 6c .**.** The min l
14662 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 eaf payload frac
14663 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 74 68 65 tion is like the
14664 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 min embedded pa
14665 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a yload fraction.*
14666 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 * except that it
14667 20 61 70 70 6c 69 65 73 20 74 6f 20 6c 65 61 66 applies to leaf
14668 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c 45 41 46 nodes in a LEAF
14669 44 41 54 41 20 74 72 65 65 2e 20 20 54 68 65 20 DATA tree. The
1466a 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 79 6c 6f maximum.** paylo
1466b 61 64 20 66 72 61 63 74 69 6f 6e 20 66 6f 72 20 ad fraction for
1466c 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20 a LEAFDATA tree
1466d 69 73 20 61 6c 77 61 79 73 20 31 30 30 25 20 28 is always 100% (
1466e 6f 72 20 32 35 35 29 20 61 6e 64 20 69 74 0a 2a or 255) and it.*
1466f 2a 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 * not specified
14670 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a in the header..*
14671 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72 65 65 20 *.** Each btree
14672 70 61 67 65 73 20 69 73 20 64 69 76 69 64 65 64 pages is divided
14673 20 69 6e 74 6f 20 74 68 72 65 65 20 73 65 63 74 into three sect
14674 69 6f 6e 73 3a 20 20 54 68 65 20 68 65 61 64 65 ions: The heade
14675 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 20 70 r, the.** cell p
14676 6f 69 6e 74 65 72 20 61 72 72 61 79 2c 20 61 6e ointer array, an
14677 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 d the cell conte
14678 6e 74 20 61 72 65 61 2e 20 20 50 61 67 65 20 31 nt area. Page 1
14679 20 61 6c 73 6f 20 68 61 73 20 61 20 31 30 30 2d also has a 100-
1467a 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 byte.** file hea
1467b 64 65 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 der that occurs
1467c 62 65 66 6f 72 65 20 74 68 65 20 70 61 67 65 20 before the page
1467d 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 header..**.**
1467e 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |------------
1467f 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 ----|.** |
14680 66 69 6c 65 20 68 65 61 64 65 72 20 20 20 20 7c file header |
14681 20 20 20 31 30 30 20 62 79 74 65 73 2e 20 20 50 100 bytes. P
14682 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a 2a 20 20 age 1 only..**
14683 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-----------
14684 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c -----|.** |
14685 20 70 61 67 65 20 68 65 61 64 65 72 20 20 20 20 page header
14686 7c 20 20 20 38 20 62 79 74 65 73 20 66 6f 72 20 | 8 bytes for
14687 6c 65 61 76 65 73 2e 20 20 31 32 20 62 79 74 65 leaves. 12 byte
14688 73 20 66 6f 72 20 69 6e 74 65 72 69 6f 72 20 6e s for interior n
14689 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d odes.** |--
1468a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a --------------|.
1468b 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 70 ** | cell p
1468c 6f 69 6e 74 65 72 20 20 20 7c 20 20 20 7c 20 20 ointer | |
1468d 32 20 62 79 74 65 73 20 70 65 72 20 63 65 6c 6c 2 bytes per cell
1468e 2e 20 20 53 6f 72 74 65 64 20 6f 72 64 65 72 2e . Sorted order.
1468f 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 72 61 79 .** | array
14690 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 | |
14691 20 47 72 6f 77 73 20 64 6f 77 6e 77 61 72 64 0a Grows downward.
14692 2a 2a 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 ** |
14693 20 20 20 20 20 20 20 20 20 7c 20 20 20 76 0a 2a | v.*
14694 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
14695 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
14696 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20 | unallocated
14697 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 73 |.** | s
14698 70 61 63 65 20 20 20 20 20 20 20 20 20 20 7c 0a pace |.
14699 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d ** |-------
1469a 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e 20 20 ---------| ^
1469b 47 72 6f 77 73 20 75 70 77 61 72 64 73 0a 2a 2a Grows upwards.**
1469c 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 63 6f 6e | cell con
1469d 74 65 6e 74 20 20 20 7c 20 20 20 7c 20 20 41 72 tent | | Ar
1469e 62 69 74 72 61 72 79 20 6f 72 64 65 72 20 69 6e bitrary order in
1469f 74 65 72 73 70 65 72 73 65 64 20 77 69 74 68 20 terspersed with
146a0 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 freeblocks..**
146a1 20 20 20 20 7c 20 61 72 65 61 20 20 20 20 20 20 | area
146a2 20 20 20 20 20 7c 20 20 20 7c 20 20 61 6e 64 20 | | and
146a3 66 72 65 65 20 73 70 61 63 65 20 66 72 61 67 6d free space fragm
146a4 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d ents..** |-
146a5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c ---------------|
146a6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 .**.** The page
146a7 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69 headers looks li
146a8 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
146a9 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 OFFSET SIZE
146aa 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a DESCRIPTION.*
146ab 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 31 * 0 1
146ac 20 20 20 20 20 20 46 6c 61 67 73 2e 20 31 3a 20 Flags. 1:
146ad 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72 6f 64 intkey, 2: zerod
146ae 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 61 74 61 ata, 4: leafdata
146af 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 20 20 20 , 8: leaf.**
146b0 20 20 31 20 20 20 20 20 20 20 32 20 20 20 20 20 1 2
146b1 20 62 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20 byte offset to
146b2 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c the first freebl
146b3 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 ock.** 3
146b4 20 20 20 20 32 20 20 20 20 20 20 6e 75 6d 62 65 2 numbe
146b5 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 r of cells on th
146b6 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 is page.**
146b7 35 20 20 20 20 20 20 20 32 20 20 20 20 20 20 66 5 2 f
146b8 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 irst byte of the
146b9 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
146ba 65 61 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 ea.** 7
146bb 20 20 20 31 20 20 20 20 20 20 6e 75 6d 62 65 72 1 number
146bc 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 66 of fragmented f
146bd 72 65 65 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 ree bytes.**
146be 20 20 38 20 20 20 20 20 20 20 34 20 20 20 20 20 8 4
146bf 20 52 69 67 68 74 20 63 68 69 6c 64 20 28 74 68 Right child (th
146c0 65 20 50 74 72 28 4e 29 20 76 61 6c 75 65 29 2e e Ptr(N) value).
146c1 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20 6c 65 61 Omitted on lea
146c2 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ves..**.** The f
146c3 6c 61 67 73 20 64 65 66 69 6e 65 20 74 68 65 20 lags define the
146c4 66 6f 72 6d 61 74 20 6f 66 20 74 68 69 73 20 62 format of this b
146c5 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20 tree page. The
146c6 6c 65 61 66 20 66 6c 61 67 20 6d 65 61 6e 73 20 leaf flag means
146c7 74 68 61 74 0a 2a 2a 20 74 68 69 73 20 70 61 67 that.** this pag
146c8 65 20 68 61 73 20 6e 6f 20 63 68 69 6c 64 72 65 e has no childre
146c9 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64 61 74 61 n. The zerodata
146ca 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 flag means that
146cb 20 74 68 69 73 20 70 61 67 65 20 63 61 72 72 69 this page carri
146cc 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 79 73 20 es.** only keys
146cd 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20 20 54 68 and no data. Th
146ce 65 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 6d 65 e intkey flag me
146cf 61 6e 73 20 74 68 61 74 20 74 68 65 20 6b 65 79 ans that the key
146d0 20 69 73 20 61 20 69 6e 74 65 67 65 72 0a 2a 2a is a integer.**
146d1 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 65 64 which is stored
146d2 20 69 6e 20 74 68 65 20 6b 65 79 20 73 69 7a 65 in the key size
146d3 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 65 entry of the ce
146d4 6c 6c 20 68 65 61 64 65 72 20 72 61 74 68 65 72 ll header rather
146d5 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 68 65 20 than in.** the
146d6 70 61 79 6c 6f 61 64 20 61 72 65 61 2e 0a 2a 2a payload area..**
146d7 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 .** The cell poi
146d8 6e 74 65 72 20 61 72 72 61 79 20 62 65 67 69 6e nter array begin
146d9 73 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 62 s on the first b
146da 79 74 65 20 61 66 74 65 72 20 74 68 65 20 70 61 yte after the pa
146db 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68 ge header..** Th
146dc 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 e cell pointer a
146dd 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65 rray contains ze
146de 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74 ro or more 2-byt
146df 65 20 6e 75 6d 62 65 72 73 20 77 68 69 63 68 20 e numbers which
146e0 61 72 65 0a 2a 2a 20 6f 66 66 73 65 74 73 20 66 are.** offsets f
146e1 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
146e2 67 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f g of the page to
146e3 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
146e4 74 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a t in the cell.**
146e5 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 content area.
146e6 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 The cell pointer
146e7 73 20 6f 63 63 75 72 20 69 6e 20 73 6f 72 74 65 s occur in sorte
146e8 64 20 6f 72 64 65 72 2e 20 20 54 68 65 20 73 79 d order. The sy
146e9 73 74 65 6d 20 73 74 72 69 76 65 73 0a 2a 2a 20 stem strives.**
146ea 74 6f 20 6b 65 65 70 20 66 72 65 65 20 73 70 61 to keep free spa
146eb 63 65 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 ce after the las
146ec 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 73 t cell pointer s
146ed 6f 20 74 68 61 74 20 6e 65 77 20 63 65 6c 6c 73 o that new cells
146ee 20 63 61 6e 0a 2a 2a 20 62 65 20 65 61 73 69 6c can.** be easil
146ef 79 20 61 64 64 65 64 20 77 69 74 68 6f 75 74 20 y added without
146f0 68 61 76 69 6e 67 20 74 6f 20 64 65 66 72 61 67 having to defrag
146f1 6d 65 6e 74 20 74 68 65 20 70 61 67 65 2e 0a 2a ment the page..*
146f2 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e *.** Cell conten
146f3 74 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 74 t is stored at t
146f4 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 20 74 he very end of t
146f5 68 65 20 70 61 67 65 20 61 6e 64 20 67 72 6f 77 he page and grow
146f6 73 20 74 6f 77 61 72 64 20 74 68 65 0a 2a 2a 20 s toward the.**
146f7 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 beginning of the
146f8 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 6e 75 page..**.** Unu
146f9 73 65 64 20 73 70 61 63 65 20 77 69 74 68 69 6e sed space within
146fa 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
146fb 74 20 61 72 65 61 20 69 73 20 63 6f 6c 6c 65 63 t area is collec
146fc 74 65 64 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 ted into a linke
146fd 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 66 72 65 d list of.** fre
146fe 65 62 6c 6f 63 6b 73 2e 20 20 45 61 63 68 20 66 eblocks. Each f
146ff 72 65 65 62 6c 6f 63 6b 20 69 73 20 61 74 20 6c reeblock is at l
14700 65 61 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 east 4 bytes in
14701 73 69 7a 65 2e 20 20 54 68 65 20 62 79 74 65 20 size. The byte
14702 6f 66 66 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65 offset.** to the
14703 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b first freeblock
14704 20 69 73 20 67 69 76 65 6e 20 69 6e 20 74 68 65 is given in the
14705 20 68 65 61 64 65 72 2e 20 20 46 72 65 65 62 6c header. Freebl
14706 6f 63 6b 73 20 6f 63 63 75 72 20 69 6e 0a 2a 2a ocks occur in.**
14707 20 69 6e 63 72 65 61 73 69 6e 67 20 6f 72 64 65 increasing orde
14708 72 2e 20 20 42 65 63 61 75 73 65 20 61 20 66 72 r. Because a fr
14709 65 65 62 6c 6f 63 6b 20 6d 75 73 74 20 62 65 20 eeblock must be
1470a 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 at least 4 bytes
1470b 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 61 6e 79 in size,.** any
1470c 20 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 group of 3 or f
1470d 65 77 65 72 20 75 6e 75 73 65 64 20 62 79 74 65 ewer unused byte
1470e 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f s in the cell co
1470f 6e 74 65 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f ntent area canno
14710 74 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 t.** exist on th
14711 65 20 66 72 65 65 62 6c 6f 63 6b 20 63 68 61 69 e freeblock chai
14712 6e 2e 20 20 41 20 67 72 6f 75 70 20 6f 66 20 33 n. A group of 3
14713 20 6f 72 20 66 65 77 65 72 20 66 72 65 65 20 62 or fewer free b
14714 79 74 65 73 20 69 73 20 63 61 6c 6c 65 64 0a 2a ytes is called.*
14715 2a 20 61 20 66 72 61 67 6d 65 6e 74 2e 20 20 54 * a fragment. T
14716 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
14717 6f 66 20 62 79 74 65 73 20 69 6e 20 61 6c 6c 20 of bytes in all
14718 66 72 61 67 6d 65 6e 74 73 20 69 73 20 72 65 63 fragments is rec
14719 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 74 68 65 orded..** in the
1471a 20 70 61 67 65 20 68 65 61 64 65 72 20 61 74 20 page header at
1471b 6f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a 2a 2a 20 offset 7..**.**
1471c 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
1471d 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 32 IPTION.** 2
1471e 20 20 20 20 20 42 79 74 65 20 6f 66 66 73 65 74 Byte offset
1471f 20 6f 66 20 74 68 65 20 6e 65 78 74 20 66 72 65 of the next fre
14720 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 32 eblock.** 2
14721 20 20 20 20 20 42 79 74 65 73 20 69 6e 20 74 68 Bytes in th
14722 69 73 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 0a is freeblock.**.
14723 2a 2a 20 43 65 6c 6c 73 20 61 72 65 20 6f 66 20 ** Cells are of
14724 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 2e variable length.
14725 20 20 43 65 6c 6c 73 20 61 72 65 20 73 74 6f 72 Cells are stor
14726 65 64 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 ed in the cell c
14727 6f 6e 74 65 6e 74 20 61 72 65 61 20 61 74 0a 2a ontent area at.*
14728 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 * the end of the
14729 20 70 61 67 65 2e 20 20 50 6f 69 6e 74 65 72 73 page. Pointers
1472a 20 74 6f 20 74 68 65 20 63 65 6c 6c 73 20 61 72 to the cells ar
1472b 65 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f e in the cell po
1472c 69 6e 74 65 72 20 61 72 72 61 79 0a 2a 2a 20 74 inter array.** t
1472d 68 61 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 hat immediately
1472e 66 6f 6c 6c 6f 77 73 20 74 68 65 20 70 61 67 65 follows the page
1472f 20 68 65 61 64 65 72 2e 20 20 43 65 6c 6c 73 20 header. Cells
14730 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
14731 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 6f 75 73 ly.** contiguous
14732 20 6f 72 20 69 6e 20 6f 72 64 65 72 2c 20 62 75 or in order, bu
14733 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 t cell pointers
14734 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 are contiguous a
14735 6e 64 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 0a nd in order..**.
14736 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 ** Cell content
14737 6d 61 6b 65 73 20 75 73 65 20 6f 66 20 76 61 72 makes use of var
14738 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 iable length int
14739 65 67 65 72 73 2e 20 20 41 20 76 61 72 69 61 62 egers. A variab
1473a 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 69 6e 74 le.** length int
1473b 65 67 65 72 20 69 73 20 31 20 74 6f 20 39 20 62 eger is 1 to 9 b
1473c 79 74 65 73 20 77 68 65 72 65 20 74 68 65 20 6c ytes where the l
1473d 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 ower 7 bits of e
1473e 61 63 68 20 0a 2a 2a 20 62 79 74 65 20 61 72 65 ach .** byte are
1473f 20 75 73 65 64 2e 20 20 54 68 65 20 69 6e 74 65 used. The inte
14740 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 ger consists of
14741 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 all bytes that h
14742 61 76 65 20 62 69 74 20 38 20 73 65 74 20 61 6e ave bit 8 set an
14743 64 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 d.** the first b
14744 79 74 65 20 77 69 74 68 20 62 69 74 20 38 20 63 yte with bit 8 c
14745 6c 65 61 72 2e 20 20 54 68 65 20 6d 6f 73 74 20 lear. The most
14746 73 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 significant byte
14747 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a of the integer.
14748 2a 2a 20 61 70 70 65 61 72 73 20 66 69 72 73 74 ** appears first
14749 2e 20 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 . A variable-le
1474a 6e 67 74 68 20 69 6e 74 65 67 65 72 20 6d 61 79 ngth integer may
1474b 20 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 not be more tha
1474c 6e 20 39 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a n 9 bytes long..
1474d 2a 2a 20 41 73 20 61 20 73 70 65 63 69 61 6c 20 ** As a special
1474e 63 61 73 65 2c 20 61 6c 6c 20 38 20 62 79 74 65 case, all 8 byte
1474f 73 20 6f 66 20 74 68 65 20 39 74 68 20 62 79 74 s of the 9th byt
14750 65 20 61 72 65 20 75 73 65 64 20 61 73 20 64 61 e are used as da
14751 74 61 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c ta. This.** all
14752 6f 77 73 20 61 20 36 34 2d 62 69 74 20 69 6e 74 ows a 64-bit int
14753 65 67 65 72 20 74 6f 20 62 65 20 65 6e 63 6f 64 eger to be encod
14754 65 64 20 69 6e 20 39 20 62 79 74 65 73 2e 0a 2a ed in 9 bytes..*
14755 2a 0a 2a 2a 20 20 20 20 30 78 30 30 20 20 20 20 *.** 0x00
14756 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14757 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
14758 30 30 30 30 30 0a 2a 2a 20 20 20 20 30 78 37 66 00000.** 0x7f
14759 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1475a 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
1475b 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 x0000007f.**
1475c 30 78 38 31 20 30 78 30 30 20 20 20 20 20 20 20 0x81 0x00
1475d 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 become
1475e 73 20 20 30 78 30 30 30 30 30 30 38 30 0a 2a 2a s 0x00000080.**
1475f 20 20 20 20 30 78 38 32 20 30 78 30 30 20 20 20 0x82 0x00
14760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 be
14761 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 31 30 comes 0x0000010
14762 30 0a 2a 2a 20 20 20 20 30 78 38 30 20 30 78 37 0.** 0x80 0x7
14763 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f
14764 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
14765 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 61 0007f.** 0x8a
14766 20 30 78 39 31 20 30 78 64 31 20 30 78 61 63 20 0x91 0xd1 0xac
14767 30 78 37 38 20 20 62 65 63 6f 6d 65 73 20 20 30 0x78 becomes 0
14768 78 31 32 33 34 35 36 37 38 0a 2a 2a 20 20 20 20 x12345678.**
14769 30 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 0x81 0x81 0x81 0
1476a 78 38 31 20 30 78 30 31 20 20 62 65 63 6f 6d 65 x81 0x01 become
1476b 73 20 20 30 78 31 30 32 30 34 30 38 31 0a 2a 2a s 0x10204081.**
1476c 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 6c 65 6e .** Variable len
1476d 67 74 68 20 69 6e 74 65 67 65 72 73 20 61 72 65 gth integers are
1476e 20 75 73 65 64 20 66 6f 72 20 72 6f 77 69 64 73 used for rowids
1476f 20 61 6e 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 and to hold the
14770 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 number of.** by
14771 74 65 73 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 tes of key and d
14772 61 74 61 20 69 6e 20 61 20 62 74 72 65 65 20 63 ata in a btree c
14773 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ell..**.** The c
14774 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c ontent of a cell
14775 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 looks like this
14776 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 :.**.** SIZE
14777 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a DESCRIPTION.*
14778 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61 67 * 4 Pag
14779 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
1477a 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d 69 74 left child. Omit
1477b 74 65 64 20 69 66 20 6c 65 61 66 20 66 6c 61 67 ted if leaf flag
1477c 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 is set..**
1477d 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 var Number of
1477e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 20 bytes of data.
1477f 4f 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 7a Omitted if the z
14780 65 72 6f 64 61 74 61 20 66 6c 61 67 20 69 73 20 erodata flag is
14781 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 set..** var
14782 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 Number of byt
14783 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72 20 74 68 es of key. Or th
14784 65 20 6b 65 79 20 69 74 73 65 6c 66 20 69 66 20 e key itself if
14785 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73 intkey flag is s
14786 65 74 2e 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 et..** *
14787 20 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 20 20 20 Payload.**
14788 20 20 34 20 20 20 20 20 46 69 72 73 74 20 70 61 4 First pa
14789 67 65 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c ge of the overfl
1478a 6f 77 20 63 68 61 69 6e 2e 20 20 4f 6d 69 74 74 ow chain. Omitt
1478b 65 64 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f ed if no overflo
1478c 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 6c 6f 77 w.**.** Overflow
1478d 20 70 61 67 65 73 20 66 6f 72 6d 20 61 20 6c 69 pages form a li
1478e 6e 6b 65 64 20 6c 69 73 74 2e 20 20 45 61 63 68 nked list. Each
1478f 20 70 61 67 65 20 65 78 63 65 70 74 20 74 68 65 page except the
14790 20 6c 61 73 74 20 69 73 20 63 6f 6d 70 6c 65 74 last is complet
14791 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 20 77 69 ely.** filled wi
14792 74 68 20 64 61 74 61 20 28 70 61 67 65 73 69 7a th data (pagesiz
14793 65 20 2d 20 34 20 62 79 74 65 73 29 2e 20 20 54 e - 4 bytes). T
14794 68 65 20 6c 61 73 74 20 70 61 67 65 20 63 61 6e he last page can
14795 20 68 61 76 65 20 61 73 20 6c 69 74 74 6c 65 0a have as little.
14796 2a 2a 20 61 73 20 31 20 62 79 74 65 20 6f 66 20 ** as 1 byte of
14797 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 data..**.** S
14798 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
14799 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ON.** 4
1479a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
1479b 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 next overflow pa
1479c 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 ge.** *
1479d 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 72 65 65 Data.**.** Free
1479e 6c 69 73 74 20 70 61 67 65 73 20 63 6f 6d 65 20 list pages come
1479f 69 6e 20 74 77 6f 20 73 75 62 74 79 70 65 73 3a in two subtypes:
147a0 20 74 72 75 6e 6b 20 70 61 67 65 73 20 61 6e 64 trunk pages and
147a1 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 leaf pages. Th
147a2 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72 e.** file header
147a3 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 points to the f
147a4 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 irst in a linked
147a5 20 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b 20 70 list of trunk p
147a6 61 67 65 2e 20 20 45 61 63 68 20 74 72 75 6e 6b age. Each trunk
147a7 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 73 20 .** page points
147a8 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65 61 66 to multiple leaf
147a9 20 70 61 67 65 73 2e 20 20 54 68 65 20 63 6f 6e pages. The con
147aa 74 65 6e 74 20 6f 66 20 61 20 6c 65 61 66 20 70 tent of a leaf p
147ab 61 67 65 20 69 73 0a 2a 2a 20 75 6e 73 70 65 63 age is.** unspec
147ac 69 66 69 65 64 2e 20 20 41 20 74 72 75 6e 6b 20 ified. A trunk
147ad 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 page looks like
147ae 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 this:.**.** S
147af 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
147b0 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ON.** 4
147b1 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
147b2 6e 65 78 74 20 74 72 75 6e 6b 20 70 61 67 65 0a next trunk page.
147b3 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 4e 75 ** 4 Nu
147b4 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 70 6f 69 mber of leaf poi
147b5 6e 74 65 72 73 20 6f 6e 20 74 68 69 73 20 70 61 nters on this pa
147b6 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 ge.** *
147b7 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 zero or more pa
147b8 67 65 73 20 6e 75 6d 62 65 72 73 20 6f 66 20 6c ges numbers of l
147b9 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f 2a 20 54 68 eaves.*/.../* Th
147ba 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 e following valu
147bb 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d e is the maximum
147bc 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73 75 6d cell size assum
147bd 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20 70 61 ing a maximum pa
147be 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76 65 20 ge.** size give
147bf 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e above..*/.#defin
147c0 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 e MX_CELL_SIZE(p
147c1 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67 65 53 Bt) (pBt->pageS
147c2 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65 20 6d ize-8)../* The m
147c3 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
147c4 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67 cells on a sing
147c5 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 le page of the d
147c6 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 0a 2a atabase. This.*
147c7 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69 6e 69 * assumes a mini
147c8 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66 mum cell size of
147c9 20 36 20 62 79 74 65 73 20 20 28 34 20 62 79 74 6 bytes (4 byt
147ca 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c 6c 20 es for the cell
147cb 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32 itself.** plus 2
147cc 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 69 bytes for the i
147cd 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65 6c 6c ndex to the cell
147ce 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 in the page hea
147cf 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73 der). Such.** s
147d0 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20 mall cells will
147d1 62 65 20 72 61 72 65 2c 20 62 75 74 20 74 68 65 be rare, but the
147d2 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a y are possible..
147d3 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 */.#define MX_CE
147d4 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d 3e 70 LL(pBt) ((pBt->p
147d5 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f ageSize-8)/6)../
147d6 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 * Forward declar
147d7 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 ations */.typede
147d8 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 f struct MemPage
147d9 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65 64 65 MemPage;.typede
147da 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 f struct BtLock
147db 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 BtLock;../*.** T
147dc 68 69 73 20 69 73 20 61 20 6d 61 67 69 63 20 73 his is a magic s
147dd 74 72 69 6e 67 20 74 68 61 74 20 61 70 70 65 61 tring that appea
147de 72 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e rs at the beginn
147df 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 ing of every.**
147e0 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 SQLite database
147e1 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64 65 6e in order to iden
147e2 74 69 66 79 20 74 68 65 20 66 69 6c 65 20 61 73 tify the file as
147e3 20 61 20 72 65 61 6c 20 64 61 74 61 62 61 73 65 a real database
147e4 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 ..**.** You can
147e5 63 68 61 6e 67 65 20 74 68 69 73 20 76 61 6c 75 change this valu
147e6 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d e at compile-tim
147e7 65 20 62 79 20 73 70 65 63 69 66 79 69 6e 67 20 e by specifying
147e8 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49 a.** -DSQLITE_FI
147e9 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20 LE_HEADER="..."
147ea 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 on the compiler
147eb 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 command-line. T
147ec 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 he.** header mus
147ed 74 20 62 65 20 65 78 61 63 74 6c 79 20 31 36 20 t be exactly 16
147ee 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 bytes including
147ef 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 the zero-termina
147f0 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74 tor so.** the st
147f1 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68 6f 75 ring itself shou
147f2 6c 64 20 62 65 20 31 35 20 63 68 61 72 61 63 74 ld be 15 charact
147f3 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f ers long. If yo
147f4 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 u change.** the
147f5 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79 6f 75 header, then you
147f6 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 r custom library
147f7 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c will not be abl
147f8 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61 e to read .** da
147f9 74 61 62 61 73 65 73 20 67 65 6e 65 72 61 74 65 tabases generate
147fa 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 72 d by the standar
147fb 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20 d tools and the
147fc 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a standard tools.*
147fd 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 * will not be ab
147fe 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74 61 62 le to read datab
147ff 61 73 65 73 20 63 72 65 61 74 65 64 20 62 79 20 ases created by
14800 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 your custom libr
14801 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ary..*/.#ifndef
14802 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 SQLITE_FILE_HEAD
14803 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38 39 20 ER /* 123456789
14804 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64 65 66 123456 */.# def
14805 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f ine SQLITE_FILE_
14806 48 45 41 44 45 52 20 22 53 51 4c 69 74 65 20 66 HEADER "SQLite f
14807 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69 66 0a ormat 3".#endif.
14808 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79 70 65 ./*.** Page type
14809 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52 65 64 flags. An ORed
1480a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 combination of
1480b 74 68 65 73 65 20 66 6c 61 67 73 20 61 70 70 65 these flags appe
1480c 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 ar as the.** fir
1480d 73 74 20 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69 st byte of on-di
1480e 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 76 65 72 sk image of ever
1480f 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f y BTree page..*/
14810 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49 4e 54 .#define PTF_INT
14811 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64 65 66 KEY 0x01.#def
14812 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 ine PTF_ZERODATA
14813 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65 20 50 0x02.#define P
14814 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30 78 30 TF_LEAFDATA 0x0
14815 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 4.#define PTF_LE
14816 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a AF 0x08../*
14817 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61 67 65 .** As each page
14818 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 of the file is
14819 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f loaded into memo
1481a 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 ry, an instance
1481b 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
1481c 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 .** structure is
1481d 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 69 6e appended and in
1481e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 itialized to zer
1481f 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 o. This structu
14820 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66 re stores.** inf
14821 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
14822 68 65 20 70 61 67 65 20 74 68 61 74 20 69 73 20 he page that is
14823 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 decoded from the
14824 20 72 61 77 20 66 69 6c 65 20 70 61 67 65 2e 0a raw file page..
14825 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 65 6e **.** The pParen
14826 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 62 t field points b
14827 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72 65 6e ack to the paren
14828 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 61 6c t page. This al
14829 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61 lows us to.** wa
1482a 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65 65 20 lk up the BTree
1482b 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f from any leaf to
1482c 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61 72 65 the root. Care
1482d 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e 20 74 must be taken t
1482e 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74 68 65 o.** unref() the
1482f 20 70 61 72 65 6e 74 20 70 61 67 65 20 70 6f 69 parent page poi
14830 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73 20 70 nter when this p
14831 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 age is no longer
14832 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20 referenced..**
14833 54 68 65 20 70 61 67 65 44 65 73 74 72 75 63 74 The pageDestruct
14834 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68 61 6e or() routine han
14835 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72 65 2e dles that chore.
14836 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f .**.** Access to
14837 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 all fields of t
14838 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
14839 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74 controlled by t
1483a 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72 he mutex.** stor
1483b 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42 ed in MemPage.pB
1483c 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 t->mutex..*/.str
1483d 75 63 74 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20 uct MemPage {.
1483e 75 38 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 u8 isInit;
1483f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
14840 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 69 74 69 previously initi
14841 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 42 45 20 alized. MUST BE
14842 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e FIRST! */. u8 n
14843 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 Overflow;
14844 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 /* Number of ov
14845 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 erflow cell bodi
14846 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f es in aCell[] */
14847 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20 20 20 . u8 intKey;
14848 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
14849 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 if intkey flag i
1484a 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65 s set */. u8 le
1484b 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 af;
1484c 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61 66 20 /* True if leaf
1484d 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 flag is set */.
1484e 20 75 38 20 68 61 73 44 61 74 61 3b 20 20 20 20 u8 hasData;
1484f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
14850 20 74 68 69 73 20 70 61 67 65 20 73 74 6f 72 65 this page store
14851 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 68 s data */. u8 h
14852 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 drOffset;
14853 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 61 67 65 /* 100 for page
14854 20 31 2e 20 20 30 20 6f 74 68 65 72 77 69 73 65 1. 0 otherwise
14855 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c 64 50 74 */. u8 childPt
14856 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 rSize; /* 0
14857 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 if leaf==1. 4 i
14858 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 f leaf==0 */. u
14859 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 16 maxLocal;
1485a 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 /* Copy of B
1485b 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c tShared.maxLocal
1485c 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 61 78 or BtShared.max
1485d 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 6d 69 Leaf */. u16 mi
1485e 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f nLocal; /
1485f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72 * Copy of BtShar
14860 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20 42 ed.minLocal or B
14861 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61 66 20 tShared.minLeaf
14862 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 */. u16 cellOff
14863 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 set; /* Ind
14864 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66 20 66 ex in aData of f
14865 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 irst cell pointe
14866 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 65 r */. u16 nFree
14867 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
14868 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 79 umber of free by
14869 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 tes on the page
1486a 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20 */. u16 nCell;
1486b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1486c 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 ber of cells on
1486d 74 68 69 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c this page, local
1486e 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 75 and ovfl */. u
1486f 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 20 20 20 16 maskPage;
14870 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 /* Mask for
14871 70 61 67 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 page offset */.
14872 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c struct _OvflCel
14873 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74 l { /* Cells t
14874 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 hat will not fit
14875 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 on aData[] */.
14876 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 u8 *pCell;
14877 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
14878 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f rs to the body o
14879 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 f the overflow c
1487a 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 ell */. u16 i
1487b 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f dx; /
1487c 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20 63 65 * Insert this ce
1487d 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d 74 68 ll before idx-th
1487e 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65 non-overflow ce
1487f 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b ll */. } aOvfl[
14880 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 5];. BtShared *
14881 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f pBt; /* Po
14882 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61 72 65 inter to BtShare
14883 64 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65 d that this page
14884 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 is part of */.
14885 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 u8 *aData;
14886 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
14887 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65 20 6f to disk image o
14888 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 f the page data
14889 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 */. DbPage *pDb
1488a 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 61 67 Page; /* Pag
1488b 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65 20 2a er page handle *
1488c 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 /. Pgno pgno;
1488d 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
1488e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73 number for this
1488f 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a page */.};../*.
14890 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 ** The in-memory
14891 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69 73 6b image of a disk
14892 20 70 61 67 65 20 68 61 73 20 74 68 65 20 61 75 page has the au
14893 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 xiliary informat
14894 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 ion appended.**
14895 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45 58 54 to the end. EXT
14896 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e RA_SIZE is the n
14897 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
14898 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 20 74 f space needed t
14899 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74 20 65 o hold.** that e
1489a 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e xtra information
1489b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 54 ..*/.#define EXT
1489c 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66 28 4d RA_SIZE sizeof(M
1489d 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 41 emPage)../*.** A
1489e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
1489f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
148a0 72 75 63 74 75 72 65 73 20 69 73 20 73 74 6f 72 ructures is stor
148a1 65 64 20 61 74 20 42 74 53 68 61 72 65 64 2e 70 ed at BtShared.p
148a2 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61 Lock..** Locks a
148a3 72 65 20 61 64 64 65 64 20 28 6f 72 20 75 70 67 re added (or upg
148a4 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41 44 5f raded from READ_
148a5 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f LOCK to WRITE_LO
148a6 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72 73 6f CK) when a curso
148a7 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20 r .** is opened
148a8 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 on the table wit
148a9 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74 53 68 h root page BtSh
148aa 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63 ared.iTable. Loc
148ab 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a ks are removed.*
148ac 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 73 74 * from this list
148ad 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 when a transact
148ae 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
148af 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c or rolled back,
148b0 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74 or when.** a bt
148b1 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c ree handle is cl
148b2 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 osed..*/.struct
148b3 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65 BtLock {. Btree
148b4 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20 *pBtree;
148b5 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64 6c 65 /* Btree handle
148b6 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f holding this lo
148b7 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61 ck */. Pgno iTa
148b8 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ble; /*
148b9 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 Root page of ta
148ba 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63 ble */. u8 eLoc
148bb 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f k; /
148bc 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 * READ_LOCK or W
148bd 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42 RITE_LOCK */. B
148be 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20 tLock *pNext;
148bf 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 /* Next in
148c0 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c BtShared.pLock l
148c1 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 ist */.};../* Ca
148c2 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20 66 ndidate values f
148c3 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 or BtLock.eLock
148c4 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f */.#define READ_
148c5 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69 LOCK 1.#defi
148c6 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20 ne WRITE_LOCK
148c7 20 32 0a 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 2../* A Btree h
148c8 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 andle.**.** A da
148c9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
148ca 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 n contains a poi
148cb 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
148cc 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f nce of.** this o
148cd 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72 79 20 bject for every
148ce 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 68 database file th
148cf 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 at it has open.
148d0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a This structure.
148d1 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74 6f 20 ** is opaque to
148d2 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
148d3 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 nection. The da
148d4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
148d5 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 n cannot.** see
148d6 74 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 the internals of
148d7 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
148d8 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 and only deals w
148d9 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a ith pointers to.
148da 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 ** this structur
148db 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d e..**.** For som
148dc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 e database files
148dd 2c 20 74 68 65 20 73 61 6d 65 20 75 6e 64 65 72 , the same under
148de 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 lying database c
148df 61 63 68 65 20 6d 69 67 68 74 20 62 65 20 0a 2a ache might be .*
148e0 2a 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e * shared between
148e1 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 multiple connec
148e2 74 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 tions. In that
148e3 63 61 73 65 2c 20 65 61 63 68 20 63 6f 6e 74 65 case, each conte
148e4 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 ction.** has it
148e5 6f 77 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 own pointer to t
148e6 68 69 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 his object. But
148e7 20 65 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f each instance o
148e8 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a f this object.**
148e9 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 points to the s
148ea 61 6d 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a ame BtShared obj
148eb 65 63 74 2e 20 20 54 68 65 20 64 61 74 61 62 61 ect. The databa
148ec 73 65 20 63 61 63 68 65 20 61 6e 64 20 74 68 65 se cache and the
148ed 0a 2a 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63 .** schema assoc
148ee 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64 iated with the d
148ef 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 atabase file are
148f0 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 all contained w
148f1 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 ithin.** the BtS
148f2 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a hared object..**
148f3 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 .** All fields i
148f4 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
148f5 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e are accessed un
148f6 64 65 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 der sqlite3.mute
148f7 78 2e 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f x..** The pBt po
148f8 69 6e 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79 inter itself may
148f9 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 not be changed
148fa 77 68 69 6c 65 20 74 68 65 72 65 20 65 78 69 73 while there exis
148fb 74 73 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 ts cursors .** i
148fc 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 n the referenced
148fd 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70 BtShared that p
148fe 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69 oint back to thi
148ff 73 20 42 74 72 65 65 20 73 69 6e 63 65 20 74 68 s Btree since th
14900 6f 73 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 ose.** cursors h
14901 61 76 65 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 ave to do go thr
14902 6f 75 67 68 20 74 68 69 73 20 42 74 72 65 65 20 ough this Btree
14903 74 6f 20 66 69 6e 64 20 74 68 65 69 72 20 42 74 to find their Bt
14904 53 68 61 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 Shared and.** th
14905 65 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 ey often do so w
14906 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 ithout holding s
14907 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f qlite3.mutex..*/
14908 0a 73 74 72 75 63 74 20 42 74 72 65 65 20 7b 0a .struct Btree {.
14909 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
1490a 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1490b 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1490c 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 62 74 72 holding this btr
1490d 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 ee */. BtShared
1490e 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 *pBt; /* Sh
1490f 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f arable content o
14910 66 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a f this btree */.
14911 20 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 u8 inTrans;
14912 20 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f /* TRANS_NO
14913 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f NE, TRANS_READ o
14914 72 20 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f r TRANS_WRITE */
14915 0a 20 20 75 38 20 73 68 61 72 61 62 6c 65 3b 20 . u8 sharable;
14916 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
14917 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 70 42 we can share pB
14918 74 20 77 69 74 68 20 61 6e 6f 74 68 65 72 20 64 t with another d
14919 62 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 b */. u8 locked
1491a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
1491b 65 20 69 66 20 64 62 20 63 75 72 72 65 6e 74 6c e if db currentl
1491c 79 20 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 y has pBt locked
1491d 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f */. int wantTo
1491e 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 Lock; /* Numb
1491f 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c er of nested cal
14920 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 ls to sqlite3Btr
14921 65 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69 eeEnter() */. i
14922 6e 74 20 6e 42 61 63 6b 75 70 3b 20 20 20 20 20 nt nBackup;
14923 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
14924 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 73 ackup operations
14925 20 72 65 61 64 69 6e 67 20 74 68 69 73 20 62 74 reading this bt
14926 72 65 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a ree */. Btree *
14927 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c pNext; /* L
14928 69 73 74 20 6f 66 20 6f 74 68 65 72 20 73 68 61 ist of other sha
14929 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 72 6f rable Btrees fro
1492a 6d 20 74 68 65 20 73 61 6d 65 20 64 62 20 2a 2f m the same db */
1492b 0a 20 20 42 74 72 65 65 20 2a 70 50 72 65 76 3b . Btree *pPrev;
1492c 20 20 20 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f /* Back po
1492d 69 6e 74 65 72 20 6f 66 20 74 68 65 20 73 61 6d inter of the sam
1492e 65 20 6c 69 73 74 20 2a 2f 0a 23 69 66 6e 64 65 e list */.#ifnde
1492f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
14930 41 52 45 44 5f 43 41 43 48 45 0a 20 20 42 74 4c ARED_CACHE. BtL
14931 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 ock lock;
14932 2f 2a 20 4f 62 6a 65 63 74 20 75 73 65 64 20 74 /* Object used t
14933 6f 20 6c 6f 63 6b 20 70 61 67 65 20 31 20 2a 2f o lock page 1 */
14934 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a .#endif.};../*.*
14935 2a 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20 * Btree.inTrans
14936 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20 may take one of
14937 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 the following va
14938 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 lues..**.** If t
14939 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 65 he shared-data e
1493a 78 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62 xtension is enab
1493b 6c 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 led, there may b
1493c 65 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73 e multiple users
1493d 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65 .** of the Btree
1493e 20 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d structure. At m
1493f 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 ost one of these
14940 20 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74 may open a writ
14941 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a e transaction,.*
14942 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72 * but any number
14943 20 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 65 may have active
14944 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f read transactio
14945 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 ns..*/.#define T
14946 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 RANS_NONE 0.#de
14947 66 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 fine TRANS_READ
14948 20 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 1.#define TRANS
14949 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 _WRITE 2../*.**
1494a 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
1494b 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 his object repre
1494c 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64 sents a single d
1494d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
1494e 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 .** A single da
1494f 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 tabase file can
14950 62 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 65 be in use as the
14951 20 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 same time by tw
14952 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 o.** or more dat
14953 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
14954 73 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 s. When two or
14955 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 more connections
14956 20 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 are.** sharing
14957 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
14958 65 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e e file, each con
14959 6e 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f nection has it o
1495a 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74 wn.** private Bt
1495b 72 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 ree object for t
1495c 68 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68 he file and each
1495d 20 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 73 of those Btrees
1495e 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 points.** to th
1495f 69 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20 is one BtShared
14960 6f 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65 object. BtShare
14961 64 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75 d.nRef is the nu
14962 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 mber of.** conne
14963 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 ctions currently
14964 20 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 61 sharing this da
14965 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
14966 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 ** Fields in thi
14967 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
14968 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 accessed under t
14969 68 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 he BtShared.mute
1496a 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65 x.** mutex, exce
1496b 70 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20 pt for nRef and
1496c 70 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 20 pNext which are
1496d 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 accessed under t
1496e 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c he.** global SQL
1496f 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
14970 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 _MASTER mutex.
14971 54 68 65 20 70 50 61 67 65 72 20 66 69 65 6c 64 The pPager field
14972 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d .** may not be m
14973 6f 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20 odified once it
14974 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74 is initially set
14975 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 as long as nRef
14976 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65 >0..** The pSche
14977 6d 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20 ma field may be
14978 73 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42 set once under B
14979 74 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e tShared.mutex an
1497a 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20 d.** thereafter
1497b 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20 is unchanged as
1497c 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a long as nRef>0..
1497d 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 3a **.** isPending:
1497e 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 74 .**.** If a Bt
1497f 53 68 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61 Shared client fa
14980 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 ils to obtain a
14981 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20 write-lock on a
14982 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 61 database.** ta
14983 62 6c 65 20 28 62 65 63 61 75 73 65 20 74 68 65 ble (because the
14984 72 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 re exists one or
14985 20 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73 more read-locks
14986 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0a on the table),.
14987 2a 2a 20 20 20 74 68 65 20 73 68 61 72 65 64 2d ** the shared-
14988 63 61 63 68 65 20 65 6e 74 65 72 73 20 27 70 65 cache enters 'pe
14989 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 nding-lock' stat
1498a 65 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20 e and isPending
1498b 69 73 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 74 is.** set to t
1498c 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 rue..**.** The
1498d 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 shared-cache le
1498e 61 76 65 73 20 74 68 65 20 27 70 65 6e 64 69 6e aves the 'pendin
1498f 67 20 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 68 g lock' state wh
14990 65 6e 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 en either of.**
14991 20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 the following
14992 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 occur:.**.**
14993 20 31 29 20 54 68 65 20 63 75 72 72 65 6e 74 20 1) The current
14994 77 72 69 74 65 72 20 28 42 74 53 68 61 72 65 64 writer (BtShared
14995 2e 70 57 72 69 74 65 72 29 20 63 6f 6e 63 6c 75 .pWriter) conclu
14996 64 65 73 20 69 74 73 20 74 72 61 6e 73 61 63 74 des its transact
14997 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 32 ion, OR.** 2
14998 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 ) The number of
14999 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 6f 74 locks held by ot
1499a 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 her connections
1499b 64 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a drops to zero..*
1499c 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e 20 *.** while in
1499d 74 68 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 the 'pending-loc
1499e 6b 27 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f 6e k' state, no con
1499f 6e 65 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 72 nection may star
149a0 74 20 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 61 t a new.** tra
149a1 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
149a2 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20 69 This feature i
149a3 73 20 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 65 s included to he
149a4 6c 70 20 70 72 65 76 65 6e 74 20 77 72 69 74 65 lp prevent write
149a5 72 2d 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a 2f r-starvation..*/
149a6 0a 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64 .struct BtShared
149a7 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 {. Pager *pPag
149a8 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 er; /* Th
149a9 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a e page cache */.
149aa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
149ab 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
149ac 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 ase connection c
149ad 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74 urrently using t
149ae 68 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 42 his Btree */. B
149af 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 tCursor *pCursor
149b0 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f ; /* A list o
149b1 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f f all open curso
149b2 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 rs */. MemPage
149b3 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a *pPage1; /*
149b4 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 First page of t
149b5 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
149b6 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 u8 readOnly;
149b7 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
149b8 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 f the underlying
149b9 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c file is readonl
149ba 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 69 y */. u8 pageSi
149bb 7a 65 46 69 78 65 64 3b 20 20 20 20 20 2f 2a 20 zeFixed; /*
149bc 54 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 True if the page
149bd 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e size can no lon
149be 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 20 2a ger be changed *
149bf 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
149c0 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
149c1 0a 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d . u8 autoVacuum
149c2 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
149c3 20 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 if auto-vacuum
149c4 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 is enabled */.
149c5 75 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 u8 incrVacuum;
149c6 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
149c7 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 incr-vacuum is
149c8 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69 enabled */.#endi
149c9 66 0a 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 f. u16 pageSize
149ca 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 ; /* Tot
149cb 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 al number of byt
149cc 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a es on a page */.
149cd 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 u16 usableSize
149ce 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
149cf 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 r of usable byte
149d0 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a s on each page *
149d1 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c /. u16 maxLocal
149d2 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 ; /* Max
149d3 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f imum local paylo
149d4 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 ad in non-LEAFDA
149d5 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 TA tables */. u
149d6 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 16 minLocal;
149d7 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 /* Minimum
149d8 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e local payload in
149d9 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 non-LEAFDATA ta
149da 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 61 bles */. u16 ma
149db 78 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 xLeaf;
149dc 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c /* Maximum local
149dd 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 payload in a LE
149de 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a AFDATA table */.
149df 20 20 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20 u16 minLeaf;
149e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
149e1 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 um local payload
149e2 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 in a LEAFDATA t
149e3 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 able */. u8 inT
149e4 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 ransaction;
149e5 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 73 /* Transaction s
149e6 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 tate */. int nT
149e7 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 ransaction;
149e8 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 /* Number of ope
149e9 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 n transactions (
149ea 72 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f read + write) */
149eb 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 . void *pSchema
149ec 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e ; /* Poin
149ed 74 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c ter to space all
149ee 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 ocated by sqlite
149ef 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a 3BtreeSchema() *
149f0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 /. void (*xFree
149f1 53 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 Schema)(void*);
149f2 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 /* Destructor f
149f3 6f 72 20 42 74 53 68 61 72 65 64 2e 70 53 63 68 or BtShared.pSch
149f4 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ema */. sqlite3
149f5 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f _mutex *mutex; /
149f6 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 * Non-recursive
149f7 6d 75 74 65 78 20 72 65 71 75 69 72 65 64 20 74 mutex required t
149f8 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 73 74 o access this st
149f9 72 75 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 63 ruct */. Bitvec
149fa 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 20 *pHasContent;
149fb 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 /* Set of pages
149fc 6d 6f 76 65 64 20 74 6f 20 66 72 65 65 2d 6c 69 moved to free-li
149fd 73 74 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 st this transact
149fe 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ion */.#ifndef S
149ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
14a00 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52 D_CACHE. int nR
14a01 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ef;
14a02 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 /* Number of ref
14a03 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 erences to this
14a04 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 structure */. B
14a05 74 53 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20 tShared *pNext;
14a06 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 /* Next on
14a07 61 20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62 a list of sharab
14a08 6c 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 le BtShared stru
14a09 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 cts */. BtLock
14a0a 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f *pLock; /
14a0b 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 * List of locks
14a0c 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 held on this sha
14a0d 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 red-btree struct
14a0e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 72 */. Btree *pWr
14a0f 69 74 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 42 iter; /* B
14a10 74 72 65 65 20 77 69 74 68 20 63 75 72 72 65 6e tree with curren
14a11 74 6c 79 20 6f 70 65 6e 20 77 72 69 74 65 20 74 tly open write t
14a12 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 ransaction */.
14a13 75 38 20 69 73 45 78 63 6c 75 73 69 76 65 3b 20 u8 isExclusive;
14a14 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
14a15 20 70 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 pWriter has an
14a16 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
14a17 6e 20 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 38 n the db */. u8
14a18 20 69 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 isPending;
14a19 20 20 20 20 2f 2a 20 49 66 20 77 61 69 74 69 6e /* If waitin
14a1a 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73 g for read-locks
14a1b 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 6e to clear */.#en
14a1c 64 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70 dif. u8 *pTmpSp
14a1d 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 ace; /* B
14a1e 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 tShared.pageSize
14a1f 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 bytes of space
14a20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d for tmp use */.}
14a21 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 ;../*.** An inst
14a22 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
14a23 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
14a24 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 is used to hold
14a25 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 information.** a
14a26 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 bout a cell. Th
14a27 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 e parseCellPtr()
14a28 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 function fills
14a29 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
14a2a 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e e.** based on in
14a2b 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63 formation extrac
14a2c 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64 t from the raw d
14a2d 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70 isk page..*/.typ
14a2e 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c edef struct Cell
14a2f 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73 Info CellInfo;.s
14a30 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b truct CellInfo {
14a31 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 . u8 *pCell;
14a32 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
14a33 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c the start of cel
14a34 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 l content */. i
14a35 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 64 nKey; /*
14a36 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 The key for INT
14a37 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e KEY tables, or n
14a38 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
14a39 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e n key */. u32 n
14a3a 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d Data; /* Num
14a3b 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
14a3c 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50 data */. u32 nP
14a3d 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61 ayload; /* Tota
14a3e 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c l amount of payl
14a3f 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65 oad */. u16 nHe
14a40 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20 ader; /* Size
14a41 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
14a42 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 79 ent header in by
14a43 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f tes */. u16 nLo
14a44 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e cal; /* Amoun
14a45 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c t of payload hel
14a46 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75 d locally */. u
14a47 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a 16 iOverflow; /*
14a48 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66 Offset to overf
14a49 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e low page number.
14a4a 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65 Zero if no ove
14a4b 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e rflow */. u16 n
14a4c 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a Size; /* Siz
14a4d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f e of the cell co
14a4e 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69 ntent on the mai
14a4f 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f n b-tree page */
14a50 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d .};../*.** Maxim
14a51 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53 um depth of an S
14a52 51 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72 QLite B-Tree str
14a53 75 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72 ucture. Any B-Tr
14a54 65 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a ee deeper than.*
14a55 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64 * this will be d
14a56 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e eclared corrupt.
14a57 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63 This value is c
14a58 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20 alculated based
14a59 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 on a.** maximum
14a5a 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66 database size of
14a5b 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69 2^31 pages a mi
14a5c 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20 nimum fanout of
14a5d 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d 2 for a.** root-
14a5e 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61 node and 3 for a
14a5f 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61 ll other interna
14a60 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 l nodes..**.** I
14a61 66 20 61 20 74 72 65 65 20 74 68 61 74 20 61 70 f a tree that ap
14a62 70 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c pears to be tall
14a63 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 20 er than this is
14a64 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20 encountered, it
14a65 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 is.** assumed th
14a66 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 at the database
14a67 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23 is corrupt..*/.#
14a68 64 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f define BTCURSOR_
14a69 4d 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a MAX_DEPTH 20../*
14a6a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 .** A cursor is
14a6b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 a pointer to a p
14a6c 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20 articular entry
14a6d 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75 within a particu
14a6e 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69 lar.** b-tree wi
14a6f 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 thin a database
14a70 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 file..**.** The
14a71 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66 entry is identif
14a72 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61 ied by its MemPa
14a73 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 ge and the index
14a74 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 in.** MemPage.a
14a75 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e Cell[] of the en
14a76 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 try..**.** When
14a77 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 a single databas
14a78 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65 e file can share
14a79 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61 d by two more da
14a7a 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14a7b 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f ns,.** but curso
14a7c 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61 rs cannot be sha
14a7d 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f red. Each curso
14a7e 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 r is associated
14a7f 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63 with a.** partic
14a80 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f ular database co
14a81 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66 nnection identif
14a82 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74 ied BtCursor.pBt
14a83 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69 ree.db..**.** Fi
14a84 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 elds in this str
14a85 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73 ucture are acces
14a86 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74 sed under the Bt
14a87 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 Shared.mutex.**
14a88 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70 found at self->p
14a89 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73 Bt->mutex. .*/.s
14a8a 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b truct BtCursor {
14a8b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 . Btree *pBtree
14a8c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
14a8d 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69 The Btree to whi
14a8e 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62 ch this cursor b
14a8f 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68 elongs */. BtSh
14a90 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 ared *pBt;
14a91 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53 /* The BtS
14a92 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f hared this curso
14a93 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20 r points to */.
14a94 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74 BtCursor *pNext
14a95 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f , *pPrev; /* Fo
14a96 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 rms a linked lis
14a97 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 t of all cursors
14a98 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 */. struct Key
14a99 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
14a9a 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73 /* Argument pass
14a9b 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e ed to comparison
14a9c 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 function */. P
14a9d 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20 gno pgnoRoot;
14a9e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
14a9f 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69 root page of thi
14aa0 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69 s tree */. sqli
14aa1 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64 te3_int64 cached
14aa2 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72 Rowid; /* Next r
14aa3 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d owid cache. 0 m
14aa4 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a eans not valid *
14aa5 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 /. CellInfo inf
14aa6 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a o; /*
14aa7 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20 A parse of the
14aa8 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e cell we are poin
14aa9 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20 ting at */. u8
14aaa 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 wrFlag;
14aab 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
14aac 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20 f writable */.
14aad 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20 u8 atLast;
14aae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
14aaf 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 sor pointing to
14ab0 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a the last entry *
14ab1 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79 /. u8 validNKey
14ab2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
14ab3 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b True if info.nK
14ab4 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 ey is valid */.
14ab5 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20 u8 eState;
14ab6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
14ab7 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f e of the CURSOR_
14ab8 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73 XXX constants (s
14ab9 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76 ee below) */. v
14aba 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 oid *pKey;
14abb 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 61 /* Saved key tha
14abc 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c t was cursor's l
14abd 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69 ast known positi
14abe 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 on */. i64 nKey
14abf 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 ; /* Size
14ac0 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73 of pKey, or las
14ac1 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f t integer key */
14ac2 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b . int skipNext;
14ac3 20 20 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73 /* Prev() is
14ac4 20 6e 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76 noop if negativ
14ac5 65 2e 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f e. Next() is noo
14ac6 70 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f p if positive */
14ac7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14ac8 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 OMIT_INCRBLOB.
14ac9 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e u8 isIncrblobHan
14aca 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 dle; /* Tru
14acb 65 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72 e if this cursor
14acc 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20 is an incr. io
14acd 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f handle */. Pgno
14ace 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 *aOverflow;
14acf 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f /* Cache o
14ad0 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 f overflow page
14ad1 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e locations */.#en
14ad2 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b dif. i16 iPage;
14ad3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14ad4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
14ad5 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 ndex of current
14ad6 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a page in apPage *
14ad7 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 /. MemPage *apP
14ad8 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 age[BTCURSOR_MAX
14ad9 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 _DEPTH]; /* Pag
14ada 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 es from root to
14adb 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a current page */.
14adc 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 55 u16 aiIdx[BTCU
14add 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b RSOR_MAX_DEPTH];
14ade 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
14adf 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61 nt index in apPa
14ae0 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ge[i] */.};../*.
14ae1 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c ** Potential val
14ae2 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72 ues for BtCursor
14ae3 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 .eState..**.** C
14ae4 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 URSOR_VALID:.**
14ae5 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 Cursor points
14ae6 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79 to a valid entry
14ae7 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65 . getPayload() e
14ae8 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 tc. may be calle
14ae9 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f d..**.** CURSOR_
14aea 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 INVALID:.** Cu
14aeb 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f rsor does not po
14aec 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65 int to a valid e
14aed 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68 ntry. This can h
14aee 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 appen (for examp
14aef 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 le) .** becaus
14af0 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 e the table is e
14af1 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 20 mpty or because
14af2 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 74 BtreeCursorFirst
14af3 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a () has not been.
14af4 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a ** called..**.
14af5 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 ** CURSOR_REQUIR
14af6 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 ESEEK:.** The
14af7 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20 table that this
14af8 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 cursor was opene
14af9 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 d on still exist
14afa 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20 s, but has been
14afb 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73 .** modified s
14afc 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 ince the cursor
14afd 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54 was last used. T
14afe 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 he cursor positi
14aff 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 on is saved.**
14b00 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74 in variables Bt
14b01 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 Cursor.pKey and
14b02 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 BtCursor.nKey. W
14b03 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20 hen a cursor is
14b04 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 in .** this st
14b05 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73 ate, restoreCurs
14b06 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e orPosition() can
14b07 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 be called to at
14b08 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 tempt to.** se
14b09 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ek the cursor to
14b0a 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74 the saved posit
14b0b 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f ion..**.** CURSO
14b0c 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 R_FAULT:.** A
14b0d 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72 unrecoverable er
14b0e 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f ror (an I/O erro
14b0f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 r or a malloc fa
14b10 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72 ilure) has occur
14b11 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 red.** on a di
14b12 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 fferent connecti
14b13 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74 on that shares t
14b14 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63 68 he BtShared cach
14b15 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 e with this.**
14b16 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72 cursor. The er
14b17 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 65 ror has left the
14b18 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 cache in an inc
14b19 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e onsistent state.
14b1a 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 .** Do nothing
14b1b 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 20 else with this
14b1c 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 cursor. Any att
14b1d 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 empt to use the
14b1e 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 cursor.** shou
14b1f 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 ld return the er
14b20 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 ror code stored
14b21 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 in BtCursor.skip
14b22 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53 .*/.#define CURS
14b23 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 OR_INVALID
14b24 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43 0.#define C
14b25 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 URSOR_VALID
14b26 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
14b27 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 e CURSOR_REQUIRE
14b28 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65 SEEK 2.#de
14b29 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c fine CURSOR_FAUL
14b2a 54 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a T 3.
14b2b 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61 ./* .** The data
14b2c 62 61 73 65 20 70 61 67 65 20 74 68 65 20 50 45 base page the PE
14b2d 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70 NDING_BYTE occup
14b2e 69 65 73 2e 20 54 68 69 73 20 70 61 67 65 20 69 ies. This page i
14b2f 73 20 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f s never used..*/
14b30 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e .# define PENDIN
14b31 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
14b32 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 PAGER_MJ_PGNO(p
14b33 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 Bt)../*.** These
14b34 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74 macros define t
14b35 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 he location of t
14b36 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
14b37 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64 ntry for a .** d
14b38 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 68 atabase page. Th
14b39 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
14b3a 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 20 to each is the
14b3b 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 number of usable
14b3c 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63 .** bytes on eac
14b3d 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 h page of the da
14b3e 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30 tabase (often 10
14b3f 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 24). The second
14b40 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e is the.** page n
14b41 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70 umber to look up
14b42 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 in the pointer
14b43 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 map..**.** PTRMA
14b44 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73 P_PAGENO returns
14b45 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 the database pa
14b46 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
14b47 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 pointer-map.**
14b48 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 73 page that stores
14b49 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 6f the required po
14b4a 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54 inter. PTRMAP_PT
14b4b 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a ROFFSET returns.
14b4c 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 ** the offset of
14b4d 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6d the requested m
14b4e 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 ap entry..**.**
14b4f 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75 If the pgno argu
14b50 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50 ment passed to P
14b51 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20 TRMAP_PAGENO is
14b52 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 a pointer-map pa
14b53 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f ge,.** then pgno
14b54 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f is returned. So
14b55 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 (pgno==PTRMAP_P
14b56 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f AGENO(pgsz, pgno
14b57 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65 )) can be.** use
14b58 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e d to test if pgn
14b59 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d o is a pointer-m
14b5a 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f ap page. PTRMAP_
14b5b 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74 ISPAGE implement
14b5c 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a s.** this test..
14b5d 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 */.#define PTRMA
14b5e 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67 P_PAGENO(pBt, pg
14b5f 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f no) ptrmapPageno
14b60 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66 (pBt, pgno).#def
14b61 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 ine PTRMAP_PTROF
14b62 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70 FSET(pgptrmap, p
14b63 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67 gno) (5*(pgno-pg
14b64 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69 ptrmap-1)).#defi
14b65 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 ne PTRMAP_ISPAGE
14b66 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52 (pBt, pgno) (PTR
14b67 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29 MAP_PAGENO((pBt)
14b68 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29 ,(pgno))==(pgno)
14b69 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 )../*.** The poi
14b6a 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f nter map is a lo
14b6b 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20 okup table that
14b6c 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 identifies the p
14b6d 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a arent page for.*
14b6e 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67 * each child pag
14b6f 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
14b70 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72 e file. The par
14b71 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20 ent page is the
14b72 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e page that.** con
14b73 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 tains a pointer
14b74 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45 to the child. E
14b75 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 very page in the
14b76 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 database contai
14b77 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72 ns.** 0 or 1 par
14b78 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20 ent pages. (In
14b79 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61 this context 'da
14b7a 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66 tabase page' ref
14b7b 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61 ers.** to any pa
14b7c 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 ge that is not p
14b7d 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 art of the point
14b7e 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20 er map itself.)
14b7f 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61 Each pointer ma
14b80 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69 p.** entry consi
14b81 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 sts of a single
14b82 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20 byte 'type' and
14b83 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20 a 4 byte parent
14b84 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 page number..**
14b85 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69 The PTRMAP_XXX i
14b86 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77 dentifiers below
14b87 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74 are the valid t
14b88 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ypes..**.** The
14b89 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70 purpose of the p
14b8a 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f ointer map is to
14b8b 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67 facility moving
14b8c 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a pages from one.
14b8d 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 ** position in t
14b8e 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68 he file to anoth
14b8f 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75 er as part of au
14b90 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20 tovacuum. When
14b91 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76 a page.** is mov
14b92 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20 ed, the pointer
14b93 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75 in its parent mu
14b94 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f st be updated to
14b95 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a point to the.**
14b96 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 new location.
14b97 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
14b98 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 is used to locat
14b99 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 e the parent pag
14b9a 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a e quickly..**.**
14b9b 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 PTRMAP_ROOTPAGE
14b9c 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
14b9d 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 age is a root-pa
14b9e 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d ge. The page-num
14b9f 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 ber is not.**
14ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 u
14ba1 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65 sed in this case
14ba2 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46 ..**.** PTRMAP_F
14ba3 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74 REEPAGE: The dat
14ba4 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e abase page is an
14ba5 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70 unused (free) p
14ba6 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 age. The page-nu
14ba7 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 mber .**
14ba8 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74 is not
14ba9 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 used in this ca
14baa 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 se..**.** PTRMAP
14bab 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20 _OVERFLOW1: The
14bac 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
14bad 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 the first page
14bae 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a in a list of .**
14baf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14bb0 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 overflow page
14bb1 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 s. The page numb
14bb2 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 er identifies th
14bb3 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20 e page that.**
14bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14bb5 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 contains the ce
14bb6 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 ll with a pointe
14bb7 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c r to this overfl
14bb8 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 ow page..**.** P
14bb9 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a TRMAP_OVERFLOW2:
14bba 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 The database pa
14bbb 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 ge is the second
14bbc 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69 or later page i
14bbd 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 n a list of.**
14bbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14bbf 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
14bc0 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 The page-number
14bc1 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
14bc2 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20 previous.**
14bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
14bc4 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c ge in the overfl
14bc5 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a ow page list..**
14bc6 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 .** PTRMAP_BTREE
14bc7 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 : The database p
14bc8 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f age is a non-roo
14bc9 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68 t btree page. Th
14bca 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a e page number.**
14bcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
14bcc 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 dentifies the pa
14bcd 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65 rent page in the
14bce 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 btree..*/.#defi
14bcf 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 ne PTRMAP_ROOTPA
14bd0 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52 GE 1.#define PTR
14bd1 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23 MAP_FREEPAGE 2.#
14bd2 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 define PTRMAP_OV
14bd3 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e ERFLOW1 3.#defin
14bd4 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f e PTRMAP_OVERFLO
14bd5 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52 W2 4.#define PTR
14bd6 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20 MAP_BTREE 5../*
14bd7 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72 A bunch of asser
14bd8 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 t() statements t
14bd9 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e o check the tran
14bda 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61 saction state va
14bdb 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61 riables.** of ha
14bdc 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72 ndle p (type Btr
14bdd 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61 ee*) are interna
14bde 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a lly consistent..
14bdf 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65 */.#define btree
14be0 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20 Integrity(p) \.
14be1 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d assert( p->pBt-
14be2 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d >inTransaction!=
14be3 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d TRANS_NONE || p-
14be4 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 >pBt->nTransacti
14be5 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73 on==0 ); \. ass
14be6 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 ert( p->pBt->inT
14be7 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69 ransaction>=p->i
14be8 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a nTrans ); .../*.
14be9 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43 ** The ISAUTOVAC
14bea 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65 UUM macro is use
14beb 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65 d within balance
14bec 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65 _nonroot() to de
14bed 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68 termine.** if th
14bee 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f e database suppo
14bef 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 rts auto-vacuum
14bf0 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20 or not. Because
14bf1 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69 it is used.** wi
14bf2 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 thin an expressi
14bf3 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72 on that is an ar
14bf4 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65 gument to anothe
14bf5 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c r macro .** (sql
14bf6 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69 iteMallocRaw), i
14bf7 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
14bf8 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69 e to use conditi
14bf9 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e onal compilation
14bfa 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61 ..** So, this ma
14bfb 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69 cro is defined i
14bfc 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64 nstead..*/.#ifnd
14bfd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
14bfe 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e UTOVACUUM.#defin
14bff 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28 e ISAUTOVACUUM (
14c00 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 pBt->autoVacuum)
14c01 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49 .#else.#define I
14c02 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 SAUTOVACUUM 0.#e
14c03 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ndif.../*.** Thi
14c04 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 s structure is p
14c05 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72 assed around thr
14c06 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e ough all the san
14c07 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75 ity checking rou
14c08 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65 tines.** in orde
14c09 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 r to keep track
14c0a 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73 of some global s
14c0b 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tate information
14c0c 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
14c0d 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 uct IntegrityCk
14c0e 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72 IntegrityCk;.str
14c0f 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20 uct IntegrityCk
14c10 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
14c11 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65 t; /* The tre
14c12 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20 e being checked
14c13 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a out */. Pager *
14c14 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68 pPager; /* Th
14c15 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67 e associated pag
14c16 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73 er. Also access
14c17 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61 ible by pBt->pPa
14c18 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 ger */. Pgno nP
14c19 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 age; /* Nu
14c1a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
14c1b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f the database */
14c1c 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20 . int *anRef;
14c1d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14c1e 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67 f times each pag
14c1f 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20 e is referenced
14c20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20 */. int mxErr;
14c21 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61 /* Stop a
14c22 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f ccumulating erro
14c23 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61 rs when this rea
14c24 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 ches zero */. i
14c25 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 nt nErr;
14c26 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 /* Number of me
14c27 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 ssages written t
14c28 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72 o zErrMsg so far
14c29 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63 */. int malloc
14c2a 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d Failed; /* A mem
14c2b 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
14c2c 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 rror has occurre
14c2d 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20 d */. StrAccum
14c2e 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75 errMsg; /* Accu
14c2f 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72 mulate the error
14c30 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 65 message text he
14c31 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 re */.};../*.**
14c32 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 Read or write a
14c33 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79 two- and four-by
14c34 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
14c35 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f teger values..*/
14c36 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 74 .#define get2byt
14c37 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c e(x) ((x)[0]<<
14c38 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66 8 | (x)[1]).#def
14c39 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c 76 ine put2byte(p,v
14c3a 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29 ) ((p)[0] = (u8)
14c3b 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d ((v)>>8), (p)[1]
14c3c 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 66 = (u8)(v)).#def
14c3d 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c ine get4byte sql
14c3e 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65 ite3Get4byte.#de
14c3f 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71 fine put4byte sq
14c40 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f lite3Put4byte../
14c41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
14c42 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68 nd of btreeInt.h
14c43 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
14c44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14c45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
14c46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
14c47 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
14c48 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62 we left off in b
14c49 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a tmutex.c *******
14c4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 *************/.#
14c4b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14c4c 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
14c4d 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 #if SQLITE_THREA
14c4e 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 DSAFE../*.** Obt
14c4f 61 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 ain the BtShared
14c50 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 mutex associate
14c51 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 d with B-Tree ha
14c52 6e 64 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a 2a 2a ndle p. Also,.**
14c53 20 73 65 74 20 42 74 53 68 61 72 65 64 2e 64 62 set BtShared.db
14c54 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
14c55 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 handle associat
14c56 65 64 20 77 69 74 68 20 70 20 61 6e 64 20 74 68 ed with p and th
14c57 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 20 62 e.** p->locked b
14c58 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 75 65 2e 0a oolean to true..
14c59 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c */.static void l
14c5a 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74 ockBtreeMutex(Bt
14c5b 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
14c5c 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 t( p->locked==0
14c5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
14c5e 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 ite3_mutex_nothe
14c5f 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 ld(p->pBt->mutex
14c60 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
14c61 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14c62 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
14c63 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 );.. sqlite3_mu
14c64 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 tex_enter(p->pBt
14c65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70 ->mutex);. p->p
14c66 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a Bt->db = p->db;.
14c67 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b p->locked = 1;
14c68 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 .}../*.** Releas
14c69 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d e the BtShared m
14c6a 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 utex associated
14c6b 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64 with B-Tree hand
14c6c 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 63 6c 65 61 le p and.** clea
14c6d 72 20 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 64 20 r the p->locked
14c6e 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74 boolean..*/.stat
14c6f 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 ic void unlockBt
14c70 72 65 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a reeMutex(Btree *
14c71 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
14c72 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 20 >locked==1 );.
14c73 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14c74 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 mutex_held(p->pB
14c75 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
14c76 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
14c77 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
14c78 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
14c79 65 72 74 28 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 ert( p->db==p->p
14c7a 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c Bt->db );.. sql
14c7b 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
14c7c 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b (p->pBt->mutex);
14c7d 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 . p->locked = 0
14c7e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 ;.}../*.** Enter
14c7f 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 a mutex on the
14c80 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65 given BTree obje
14c81 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ct..**.** If the
14c82 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73 object is not s
14c83 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f harable, then no
14c84 20 6d 75 74 65 78 20 69 73 20 65 76 65 72 20 72 mutex is ever r
14c85 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74 equired.** and t
14c86 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
14c87 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64 no-op. The und
14c88 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73 erlying mutex is
14c89 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a non-recursive..
14c8a 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70 20 61 ** But we keep a
14c8b 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 reference count
14c8c 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f in Btree.wantTo
14c8d 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61 Lock so the beha
14c8e 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20 vior.** of this
14c8f 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63 interface is rec
14c90 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f ursive..**.** To
14c91 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73 avoid deadlocks
14c92 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65 , multiple Btree
14c93 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20 s are locked in
14c94 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a the same order.*
14c95 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73 * by all databas
14c96 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 e connections.
14c97 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20 The p->pNext is
14c98 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a a list of other.
14c99 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67 ** Btrees belong
14c9a 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ing to the same
14c9b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
14c9c 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72 ion as the p Btr
14c9d 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 ee.** which need
14c9e 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66 to be locked af
14c9f 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 63 61 ter p. If we ca
14ca0 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20 nnot get a lock
14ca1 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69 on.** p, then fi
14ca2 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f rst unlock all o
14ca3 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20 f the others on
14ca4 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77 p->pNext, then w
14ca5 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c ait.** for the l
14ca6 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76 ock to become av
14ca7 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68 ailable on p, th
14ca8 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66 en relock all of
14ca9 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65 the.** subseque
14caa 6e 74 20 42 74 72 65 65 73 20 74 68 61 74 20 64 nt Btrees that d
14cab 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f esire a lock..*/
14cac 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14cad 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14cae 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 eEnter(Btree *p)
14caf 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65 {. Btree *pLate
14cb0 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61 r;.. /* Some ba
14cb1 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b sic sanity check
14cb2 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65 ing on the Btree
14cb3 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42 . The list of B
14cb4 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65 trees. ** conne
14cb5 63 74 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e cted by pNext an
14cb6 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62 d pPrev should b
14cb7 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 e in sorted orde
14cb8 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e r by. ** Btree.
14cb9 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65 pBt value. All e
14cba 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c lements of the l
14cbb 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e ist should belon
14cbc 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 g to. ** the sa
14cbd 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f me connection. O
14cbe 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72 65 65 nly shared Btree
14cbf 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73 s are on the lis
14cc0 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 t. */. assert(
14cc1 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 p->pNext==0 || p
14cc2 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e ->pNext->pBt>p->
14cc3 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 pBt );. assert(
14cc4 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 p->pPrev==0 ||
14cc5 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d p->pPrev->pBt<p-
14cc6 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 >pBt );. assert
14cc7 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c ( p->pNext==0 ||
14cc8 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70 p->pNext->db==p
14cc9 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
14cca 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c ( p->pPrev==0 ||
14ccb 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70 p->pPrev->db==p
14ccc 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
14ccd 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c ( p->sharable ||
14cce 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 (p->pNext==0 &&
14ccf 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b p->pPrev==0) );
14cd0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 .. /* Check for
14cd1 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74 locking consist
14cd2 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 ency */. assert
14cd3 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 ( !p->locked ||
14cd4 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 p->wantToLock>0
14cd5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
14cd6 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77 sharable || p->w
14cd7 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a antToLock==0 );.
14cd8 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 . /* We should
14cd9 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c already hold a l
14cda 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
14cdb 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
14cdc 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 /. assert( sqli
14cdd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14cde 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
14cdf 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65 . /* Unless the
14ce0 20 64 61 74 61 62 61 73 65 20 69 73 20 73 68 61 database is sha
14ce1 72 61 62 6c 65 20 61 6e 64 20 75 6e 6c 6f 63 6b rable and unlock
14ce2 65 64 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 ed, then BtShare
14ce3 64 2e 64 62 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 d.db. ** should
14ce4 20 61 6c 72 65 61 64 79 20 62 65 20 73 65 74 20 already be set
14ce5 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 correctly. */.
14ce6 61 73 73 65 72 74 28 20 28 70 2d 3e 6c 6f 63 6b assert( (p->lock
14ce7 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68 61 72 ed==0 && p->shar
14ce8 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d able) || p->pBt-
14ce9 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 0a 20 >db==p->db );..
14cea 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
14ceb 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d e ) return;. p-
14cec 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 >wantToLock++;.
14ced 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 if( p->locked )
14cee 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 return;.. /* I
14cef 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65 n most cases, we
14cf0 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20 should be able
14cf1 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c to acquire the l
14cf2 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74 ock we. ** want
14cf3 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 without having
14cf4 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74 to go throught t
14cf5 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63 he ascending loc
14cf6 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65 k. ** procedure
14cf7 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20 that follows.
14cf8 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74 Just be sure not
14cf9 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a to block.. */.
14cfa 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75 if( sqlite3_mu
14cfb 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e tex_try(p->pBt->
14cfc 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f mutex)==SQLITE_O
14cfd 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d K ){. p->pBt-
14cfe 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 >db = p->db;.
14cff 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a p->locked = 1;.
14d00 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
14d01 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64 20 64 . /* To avoid d
14d02 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74 20 72 eadlock, first r
14d03 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63 6b 73 elease all locks
14d04 20 77 69 74 68 20 61 20 6c 61 72 67 65 72 0a 20 with a larger.
14d05 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61 64 64 ** BtShared add
14d06 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63 71 75 ress. Then acqu
14d07 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54 ire our lock. T
14d08 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a 20 20 hen reacquire.
14d09 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42 74 53 ** the other BtS
14d0a 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74 hared locks that
14d0b 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 we used to hold
14d0c 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a 20 20 in ascending.
14d0d 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 ** order.. */.
14d0e 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 for(pLater=p->p
14d0f 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c Next; pLater; pL
14d10 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 ater=pLater->pNe
14d11 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 xt){. assert(
14d12 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61 62 6c pLater->sharabl
14d13 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 e );. assert(
14d14 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 3d 3d pLater->pNext==
14d15 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 4e 65 0 || pLater->pNe
14d16 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72 2d 3e xt->pBt>pLater->
14d17 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
14d18 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b t( !pLater->lock
14d19 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 77 61 ed || pLater->wa
14d1a 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 ntToLock>0 );.
14d1b 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f if( pLater->lo
14d1c 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 75 6e cked ){. un
14d1d 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 lockBtreeMutex(p
14d1e 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 Later);. }.
14d1f 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 }. lockBtreeMut
14d20 65 78 28 70 29 3b 0a 20 20 66 6f 72 28 70 4c 61 ex(p);. for(pLa
14d21 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c ter=p->pNext; pL
14d22 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 ater; pLater=pLa
14d23 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
14d24 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e if( pLater->wan
14d25 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 tToLock ){.
14d26 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 lockBtreeMutex(
14d27 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 pLater);. }.
14d28 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74 }.}../*.** Exit
14d29 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d the recursive m
14d2a 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e utex on a Btree.
14d2b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14d2c 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
14d2d 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 20 treeLeave(Btree
14d2e 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68 *p){. if( p->sh
14d2f 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73 arable ){. as
14d30 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c sert( p->wantToL
14d31 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e ock>0 );. p->
14d32 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 wantToLock--;.
14d33 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c if( p->wantToL
14d34 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ock==0 ){.
14d35 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 unlockBtreeMutex
14d36 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d (p);. }. }.}
14d37 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
14d38 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
14d39 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72 ue if the BtShar
14d3a 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 ed mutex is held
14d3b 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f on the btree, o
14d3c 72 20 69 66 20 74 68 65 0a 2a 2a 20 42 2d 54 72 r if the.** B-Tr
14d3d 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65 64 ee is not marked
14d3e 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0a 2a 2a as sharable..**
14d3f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
14d40 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72 is used only fr
14d41 6f 6d 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 om within assert
14d42 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
14d43 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14d44 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
14d45 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 eHoldsMutex(Btre
14d46 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
14d47 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 p->sharable==0
14d48 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 || p->locked==0
14d49 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b || p->wantToLock
14d4a 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
14d4b 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c p->sharable==0 |
14d4c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c | p->locked==0 |
14d4d 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d | p->db==p->pBt-
14d4e 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 >db );. assert(
14d4f 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 p->sharable==0
14d50 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 || p->locked==0
14d51 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
14d52 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 _held(p->pBt->mu
14d53 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
14d54 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ( p->sharable==0
14d55 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 || p->locked==0
14d56 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
14d57 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
14d58 74 65 78 29 20 29 3b 0a 0a 20 20 72 65 74 75 72 tex) );.. retur
14d59 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d n (p->sharable==
14d5a 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 29 3b 0 || p->locked);
14d5b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e .}.#endif...#ifn
14d5c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14d5d 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45 INCRBLOB./*.** E
14d5e 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61 nter and leave a
14d5f 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 mutex on a Btre
14d60 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72 e given a cursor
14d61 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a owned by that.*
14d62 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 20 * Btree. These
14d63 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65 entry points are
14d64 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65 used by increme
14d65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e ntal I/O and can
14d66 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69 be.** omitted i
14d67 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73 f that module is
14d68 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 not used..*/.SQ
14d69 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
14d6a 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
14d6b 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 terCursor(BtCurs
14d6c 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c or *pCur){. sql
14d6d 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
14d6e 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a Cur->pBtree);.}.
14d6f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
14d70 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
14d71 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 LeaveCursor(BtCu
14d72 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 rsor *pCur){. s
14d73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
14d74 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a (pCur->pBtree);.
14d75 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
14d76 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
14d77 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 */.../*.** Ente
14d78 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65 r the mutex on e
14d79 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63 very Btree assoc
14d7a 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74 iated with a dat
14d7b 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
14d7c 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65 ion. This is ne
14d7d 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c eded (for exampl
14d7e 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73 e) prior to pars
14d7f 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 ing.** a stateme
14d80 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c nt since we will
14d81 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61 be comparing ta
14d82 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e ble and column n
14d83 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 ames.** against
14d84 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20 all schemas and
14d85 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 we do not want t
14d86 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 69 hose schemas bei
14d87 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20 ng.** reset out
14d88 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a from under us..*
14d89 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 *.** There is a
14d8a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65 corresponding le
14d8b 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72 ave-all procedur
14d8c 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20 es..**.** Enter
14d8d 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61 the mutexes in a
14d8e 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 ccending order b
14d8f 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74 y BtShared point
14d90 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f er address.** to
14d91 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69 avoid the possi
14d92 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f bility of deadlo
14d93 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65 ck when two thre
14d94 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20 ads with.** two
14d95 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69 or more btrees i
14d96 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72 n common both tr
14d97 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68 y to lock all th
14d98 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74 eir btrees.** at
14d99 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e the same instan
14d9a 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
14d9b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
14d9c 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 3BtreeEnterAll(s
14d9d 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 qlite3 *db){. i
14d9e 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 nt i;. Btree *p
14d9f 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73 , *pLater;. ass
14da0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
14da1 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
14da2 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b x) );. for(i=0;
14da3 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
14da4 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 {. p = db->aD
14da5 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73 b[i].pBt;. as
14da6 73 65 72 74 28 20 21 70 20 7c 7c 20 28 70 2d 3e sert( !p || (p->
14da7 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e locked==0 && p->
14da8 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e sharable) || p->
14da9 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 pBt->db==p->db )
14daa 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 ;. if( p && p
14dab 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
14dac 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14dad 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21 k++;. if( !
14dae 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 p->locked ){.
14daf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
14db0 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b wantToLock==1 );
14db1 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
14db2 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70 p->pPrev ) p = p
14db3 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20 ->pPrev;.
14db4 20 2f 2a 20 52 65 61 73 6f 6e 20 66 6f 72 20 41 /* Reason for A
14db5 4c 57 41 59 53 3a 20 20 54 68 65 72 65 20 6d 75 LWAYS: There mu
14db6 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f st be at least o
14db7 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 74 72 65 65 n unlocked Btree
14db8 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 in. ** t
14db9 68 65 20 63 68 61 69 6e 2e 20 20 4f 74 68 65 72 he chain. Other
14dba 77 69 73 65 20 74 68 65 20 21 70 2d 3e 6c 6f 63 wise the !p->loc
14dbb 6b 65 64 20 74 65 73 74 20 61 62 6f 76 65 20 77 ked test above w
14dbc 6f 75 6c 64 20 68 61 76 65 20 66 61 69 6c 65 64 ould have failed
14dbd 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c */. whil
14dbe 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20 e( p->locked &&
14dbf 41 4c 57 41 59 53 28 70 2d 3e 70 4e 65 78 74 29 ALWAYS(p->pNext)
14dc0 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b ) p = p->pNext;
14dc1 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61 . for(pLa
14dc2 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 ter = p->pNext;
14dc3 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 pLater; pLater=p
14dc4 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 Later->pNext){.
14dc5 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4c 61 if( pLa
14dc6 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 ter->locked ){.
14dc7 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 unloc
14dc8 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74 kBtreeMutex(pLat
14dc9 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d er);. }
14dca 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
14dcb 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 while( p ){.
14dcc 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 lockBtr
14dcd 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14dce 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 p = p->pNe
14dcf 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 xt;. }.
14dd0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
14dd1 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
14dd2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
14dd3 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 eeLeaveAll(sqlit
14dd4 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 e3 *db){. int i
14dd5 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 ;. Btree *p;.
14dd6 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14dd7 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d mutex_held(db->m
14dd8 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 utex) );. for(i
14dd9 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
14dda 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d ++){. p = db-
14ddb 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
14ddc 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 if( p && p->sha
14ddd 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 61 rable ){. a
14dde 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f ssert( p->wantTo
14ddf 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20 Lock>0 );.
14de0 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b p->wantToLock--;
14de1 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 77 61 . if( p->wa
14de2 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 ntToLock==0 ){.
14de3 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 unlockBtr
14de4 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14de5 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a }. }. }.}.
14de6 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
14de7 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 /*.** Return tru
14de8 65 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74 e if the current
14de9 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 thread holds th
14dea 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14deb 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 ction.** mutex a
14dec 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 nd all required
14ded 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 BtShared mutexes
14dee 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
14def 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 tine is used ins
14df0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
14df1 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f tements only..*/
14df2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14df3 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
14df4 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 HoldsAllMutexes(
14df5 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
14df6 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71 int i;. if( !sq
14df7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14df8 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 (db->mutex) ){.
14df9 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
14dfa 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 . for(i=0; i<db
14dfb 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 ->nDb; i++){.
14dfc 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70 Btree *p;. p
14dfd 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
14dfe 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 t;. if( p &&
14dff 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20 p->sharable &&.
14e00 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74 (p->want
14e01 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 ToLock==0 || !sq
14e02 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14e03 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29 (p->pBt->mutex))
14e04 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
14e05 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0;. }. }.
14e06 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 return 1;.}.#end
14e07 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a if /* NDEBUG */.
14e08 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 ./*.** Add a new
14e09 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74 Btree pointer t
14e0a 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 41 72 o a BtreeMutexAr
14e0b 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20 ray. .** if the
14e0c 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73 pointer can poss
14e0d 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 20 77 ibly be shared w
14e0e 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64 ith.** another d
14e0f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14e10 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f on..**.** The po
14e11 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 74 20 inters are kept
14e12 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 in sorted order
14e13 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20 by pBtree->pBt.
14e14 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65 That.** way whe
14e15 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72 n we go to enter
14e16 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73 all the mutexes
14e17 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74 , we can enter t
14e18 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 hem.** in order
14e19 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 without every ha
14e1a 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61 ving to backup a
14e1b 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 69 74 nd retry and wit
14e1c 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67 hout.** worrying
14e1d 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e about deadlock.
14e1e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 .**.** The numbe
14e1f 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 72 65 r of shared btre
14e20 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 es will always b
14e21 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79 e small (usually
14e22 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61 0 or 1).** so a
14e23 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 n insertion sort
14e24 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 65 20 is an adequate
14e25 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a algorithm here..
14e26 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14e27 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14e28 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 reeMutexArrayIns
14e29 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 ert(BtreeMutexAr
14e2a 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72 ray *pArray, Btr
14e2b 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69 ee *pBtree){. i
14e2c 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61 nt i, j;. BtSha
14e2d 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20 red *pBt;. if(
14e2e 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74 pBtree==0 || pBt
14e2f 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ree->sharable==0
14e30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64 ) return;.#ifnd
14e31 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20 ef NDEBUG. {.
14e32 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 for(i=0; i<pAr
14e33 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b ray->nMutex; i++
14e34 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
14e35 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14e36 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 i]!=pBtree );.
14e37 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
14e38 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d assert( pArray-
14e39 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20 >nMutex>=0 );.
14e3a 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e assert( pArray->
14e3b 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65 nMutex<ArraySize
14e3c 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29 (pArray->aBtree)
14e3d 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42 -1 );. pBt = pB
14e3e 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 tree->pBt;. for
14e3f 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e (i=0; i<pArray->
14e40 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 nMutex; i++){.
14e41 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 assert( pArray
14e42 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 ->aBtree[i]!=pBt
14e43 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ree );. if( p
14e44 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d Array->aBtree[i]
14e45 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20 ->pBt>pBt ){.
14e46 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d for(j=pArray-
14e47 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d >nMutex; j>i; j-
14e48 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72 -){. pArr
14e49 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20 ay->aBtree[j] =
14e4a 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a pArray->aBtree[j
14e4b 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 -1];. }.
14e4c 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 pArray->aBtre
14e4d 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20 e[i] = pBtree;.
14e4e 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75 pArray->nMu
14e4f 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 tex++;. ret
14e50 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 urn;. }. }.
14e51 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14e52 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b pArray->nMutex++
14e53 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f ] = pBtree;.}../
14e54 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d *.** Enter the m
14e55 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 utex of every bt
14e56 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79 ree in the array
14e57 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
14e58 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20 is.** called at
14e59 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
14e5a 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
14e5b 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 (). The mutexes
14e5c 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61 are.** exited a
14e5d 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
14e5e 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a same function..
14e5f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14e60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14e61 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 reeMutexArrayEnt
14e62 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 er(BtreeMutexArr
14e63 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 ay *pArray){. i
14e64 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
14e65 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 i<pArray->nMute
14e66 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 x; i++){. Btr
14e67 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e ee *p = pArray->
14e68 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f aBtree[i];. /
14e69 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e * Some basic san
14e6a 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a ity checking */.
14e6b 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 assert( i==0
14e6c 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 || pArray->aBtr
14e6d 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e ee[i-1]->pBt<p->
14e6e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
14e6f 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c t( !p->locked ||
14e70 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 p->wantToLock>0
14e71 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 );.. /* We s
14e72 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f hould already ho
14e73 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 ld a lock on the
14e74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14e75 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 tion */. asse
14e76 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
14e77 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
14e78 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 tex) );.. /*
14e79 54 68 65 20 42 74 72 65 65 20 69 73 20 73 68 61 The Btree is sha
14e7a 72 61 62 6c 65 20 62 65 63 61 75 73 65 20 6f 6e rable because on
14e7b 6c 79 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 ly sharable Btre
14e7c 65 73 20 61 72 65 20 65 6e 74 65 72 65 64 0a 20 es are entered.
14e7d 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61 ** into the a
14e7e 72 72 61 79 20 69 6e 20 74 68 65 20 66 69 72 73 rray in the firs
14e7f 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 20 20 t place. */.
14e80 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 assert( p->shara
14e81 62 6c 65 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 ble );.. p->w
14e82 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 antToLock++;.
14e83 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 if( !p->locked
14e84 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 ){. lockBtr
14e85 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14e86 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c }. }.}../*.** L
14e87 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 20 6f eave the mutex o
14e88 66 20 65 76 65 72 79 20 62 74 72 65 65 20 69 6e f every btree in
14e89 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53 the group..*/.S
14e8a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
14e8b 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
14e8c 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 utexArrayLeave(B
14e8d 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a treeMutexArray *
14e8e 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69 pArray){. int i
14e8f 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
14e90 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 Array->nMutex; i
14e91 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a ++){. Btree *
14e92 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 p = pArray->aBtr
14e93 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f ee[i];. /* So
14e94 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 me basic sanity
14e95 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 checking */.
14e96 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20 assert( i==0 ||
14e97 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 pArray->aBtree[i
14e98 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 -1]->pBt<p->pBt
14e99 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
14e9a 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20 ->locked );.
14e9b 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 assert( p->wantT
14e9c 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 oLock>0 );..
14e9d 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 /* We should alr
14e9e 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b eady hold a lock
14e9f 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
14ea0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
14ea1 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
14ea2 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
14ea3 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a >db->mutex) );..
14ea4 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14ea5 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e k--;. if( p->
14ea6 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b wantToLock==0 ){
14ea7 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 . unlockBtr
14ea8 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 eeMutex(p);.
14ea9 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53 }. }.}..#else.S
14eaa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
14eab 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
14eac 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a nter(Btree *p){.
14ead 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 p->pBt->db = p
14eae 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 ->db;.}.SQLITE_P
14eaf 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14eb0 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
14eb1 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
14eb2 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
14eb3 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
14eb4 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 +){. Btree *p
14eb5 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
14eb6 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a t;. if( p ){.
14eb7 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62 p->pBt->db
14eb8 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a = p->db;. }.
14eb9 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.}.#endif /*
14eba 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
14ebb 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f SAFE */.#endif /
14ebc 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f * ifndef SQLITE_
14ebd 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
14ebe 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a E */../*********
14ebf 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d ***** End of btm
14ec0 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utex.c *********
14ec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ec3 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
14ec4 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
14ec5 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a btree.c *******
14ec6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ec7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ec8 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
14ec9 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 April 6.**.** T
14eca 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
14ecb 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
14ecc 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
14ecd 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
14ece 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
14ecf 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
14ed0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
14ed1 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
14ed2 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
14ed3 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
14ed4 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
14ed5 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
14ed6 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
14ed7 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
14ed8 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
14ed9 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
14eda 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
14edb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14edc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14edd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14ede 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14edf 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 *******.** $Id:
14ee0 62 74 72 65 65 2e 63 2c 76 20 31 2e 37 30 35 20 btree.c,v 1.705
14ee1 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a 35 37 2009/08/10 03:57
14ee2 3a 35 38 20 73 68 61 6e 65 20 45 78 70 20 24 0a :58 shane Exp $.
14ee3 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
14ee4 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74 implements a ext
14ee5 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65 ernal (disk-base
14ee6 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e d) database usin
14ee7 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65 g BTrees..** See
14ee8 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d the header comm
14ee9 65 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74 ent on "btreeInt
14eea 2e 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e .h" for addition
14eeb 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
14eec 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64 ** Including a d
14eed 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69 escription of fi
14eee 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e le format and an
14eef 20 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65 overview of ope
14ef0 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a ration..*/../*.*
14ef1 2a 20 54 68 65 20 68 65 61 64 65 72 20 73 74 72 * The header str
14ef2 69 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 ing that appears
14ef3 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
14ef4 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 g of every.** SQ
14ef5 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a Lite database..*
14ef6 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 /.static const c
14ef7 68 61 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72 har zMagicHeader
14ef8 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45 [] = SQLITE_FILE
14ef9 5f 48 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 _HEADER;../*.**
14efa 53 65 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 Set this global
14efb 76 61 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f variable to 1 to
14efc 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 enable tracing
14efd 75 73 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a using the TRACE.
14efe 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 ** macro..*/.#if
14eff 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 0.int sqlite3Bt
14f00 72 65 65 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20 reeTrace=1; /*
14f01 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 True to enable t
14f02 72 61 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 69 racing */.# defi
14f03 6e 65 20 54 52 41 43 45 28 58 29 20 20 69 66 28 ne TRACE(X) if(
14f04 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 sqlite3BtreeTrac
14f05 65 29 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c 75 e){printf X;fflu
14f06 73 68 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 6c sh(stdout);}.#el
14f07 73 65 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 se.# define TRAC
14f08 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 E(X).#endif....#
14f09 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14f0a 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a IT_SHARED_CACHE.
14f0b 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 /*.** A list of
14f0c 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73 BtShared objects
14f0d 20 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 62 that are eligib
14f0e 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 61 le for participa
14f0f 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65 tion.** in share
14f10 64 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 76 d cache. This v
14f11 61 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65 ariable has file
14f12 20 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f scope during no
14f13 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20 rmal builds,.**
14f14 62 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72 but the test har
14f15 6e 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63 ness needs to ac
14f16 63 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61 cess it so we ma
14f17 6b 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72 ke it global for
14f18 20 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73 .** test builds
14f19 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 ..**.** Access t
14f1a 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 o this variable
14f1b 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 is protected by
14f1c 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
14f1d 54 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23 TIC_MASTER..*/.#
14f1e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
14f1f 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
14f20 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 BtShared *SQLIT
14f21 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 E_WSD sqlite3Sha
14f22 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 redCacheList = 0
14f23 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42 ;.#else.static B
14f24 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f tShared *SQLITE_
14f25 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65 WSD sqlite3Share
14f26 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a dCacheList = 0;.
14f27 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a #endif.#endif /*
14f28 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
14f29 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
14f2a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
14f2b 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
14f2c 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 *.** Enable or d
14f2d 69 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 65 isable the share
14f2e 64 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 65 d pager and sche
14f2f 6d 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a ma features..**.
14f30 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
14f31 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e has no effect on
14f32 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 existing databa
14f33 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a se connections..
14f34 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 61 ** The shared ca
14f35 63 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 65 che setting effe
14f36 63 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20 cts only future
14f37 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 calls to.** sqli
14f38 74 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69 te3_open(), sqli
14f39 74 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72 te3_open16(), or
14f3a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
14f3b 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
14f3c 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e I int sqlite3_en
14f3d 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 able_shared_cach
14f3e 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20 e(int enable){.
14f3f 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
14f40 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65 nfig.sharedCache
14f41 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 Enabled = enable
14f42 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
14f43 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a E_OK;.}.#endif..
14f44 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
14f45 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
14f46 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 E. /*. ** The
14f47 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53 functions queryS
14f48 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
14f49 6f 63 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64 ock(), setShared
14f4a 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 CacheTableLock()
14f4b 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 ,. ** and clear
14f4c 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 AllSharedCacheTa
14f4d 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20 bleLocks(). **
14f4e 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69 manipulate entri
14f4f 65 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72 es in the BtShar
14f50 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20 ed.pLock linked
14f51 6c 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f list used to sto
14f52 72 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 re. ** shared-c
14f53 61 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c ache table level
14f54 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c locks. If the l
14f55 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c ibrary is compil
14f56 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a ed with the. **
14f57 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 shared-cache fe
14f58 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20 ature disabled,
14f59 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e then there is on
14f5a 6c 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72 ly ever one user
14f5b 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74 . ** of each Bt
14f5c 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 Shared structure
14f5d 20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63 and so this loc
14f5e 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 king is not nece
14f5f 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20 ssary. . ** So
14f60 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20 define the lock
14f61 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e related function
14f62 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a s as no-ops.. *
14f63 2f 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72 /. #define quer
14f64 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
14f65 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c eLock(a,b,c) SQL
14f66 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 ITE_OK. #define
14f67 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 setSharedCacheT
14f68 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 ableLock(a,b,c)
14f69 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 SQLITE_OK. #def
14f6a 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 ine clearAllShar
14f6b 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
14f6c 73 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 64 s(a). #define d
14f6d 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 owngradeAllShare
14f6e 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 dCacheTableLocks
14f6f 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 68 61 (a). #define ha
14f70 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
14f71 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 20 31 eLock(a,b,c,d) 1
14f72 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 52 65 . #define hasRe
14f73 61 64 43 6f 6e 66 6c 69 63 74 73 28 61 2c 20 62 adConflicts(a, b
14f74 29 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ) 0.#endif..#ifn
14f75 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14f76 53 48 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69 SHARED_CACHE..#i
14f77 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
14f78 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e G./*.** This fun
14f79 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 ction is only us
14f7a 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e ed as part of an
14f7b 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
14f7c 65 6e 74 2e 20 49 74 20 63 68 65 63 6b 73 0a 2a ent. It checks.*
14f7d 2a 20 74 68 61 74 20 63 6f 6e 6e 65 63 74 69 6f * that connectio
14f7e 6e 20 70 20 68 6f 6c 64 73 20 74 68 65 20 72 65 n p holds the re
14f7f 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20 quired locks to
14f80 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 6f read or write to
14f81 20 74 68 65 20 0a 2a 2a 20 62 2d 74 72 65 65 20 the .** b-tree
14f82 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 69 with root page i
14f83 52 6f 6f 74 2e 20 49 66 20 73 6f 2c 20 74 72 75 Root. If so, tru
14f84 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f e is returned. O
14f85 74 68 65 72 77 69 73 65 2c 20 66 61 6c 73 65 2e therwise, false.
14f86 20 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 .** For example
14f87 2c 20 77 68 65 6e 20 77 72 69 74 69 6e 67 20 74 , when writing t
14f88 6f 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 o a table b-tree
14f89 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 with root-page
14f8a 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 42 74 iRoot via .** Bt
14f8b 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ree connection p
14f8c 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 Btree:.**.**
14f8d 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65 assert( hasShare
14f8e 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
14f8f 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30 pBtree, iRoot, 0
14f90 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b , WRITE_LOCK) );
14f91 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74 .**.** When writ
14f92 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 ing to an index
14f93 62 2d 74 72 65 65 20 74 68 61 74 20 72 65 73 69 b-tree that resi
14f94 64 65 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c des in a sharabl
14f95 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 e database, the
14f96 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c .** caller shoul
14f97 64 20 68 61 76 65 20 66 69 72 73 74 20 6f 62 74 d have first obt
14f98 61 69 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65 ained a lock spe
14f99 63 69 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74 cifying the root
14f9a 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 page of.** the
14f9b 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 corresponding ta
14f9c 62 6c 65 20 62 2d 74 72 65 65 2e 20 54 68 69 73 ble b-tree. This
14f9d 20 6d 61 6b 65 73 20 74 68 69 6e 67 73 20 61 20 makes things a
14f9e 62 69 74 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 bit more complic
14f9f 61 74 65 64 2c 0a 2a 2a 20 61 73 20 74 68 69 73 ated,.** as this
14fa0 20 6d 6f 64 75 6c 65 20 74 72 65 61 74 73 20 65 module treats e
14fa1 61 63 68 20 62 2d 74 72 65 65 20 61 73 20 61 20 ach b-tree as a
14fa2 73 65 70 61 72 61 74 65 20 73 74 72 75 63 74 75 separate structu
14fa3 72 65 2e 20 54 6f 20 64 65 74 65 72 6d 69 6e 65 re. To determine
14fa4 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 2d .** the table b-
14fa5 74 72 65 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 tree correspondi
14fa6 6e 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 ng to the index
14fa7 62 2d 74 72 65 65 20 62 65 69 6e 67 20 77 72 69 b-tree being wri
14fa8 74 74 65 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 75 tten, this.** fu
14fa9 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 65 nction has to se
14faa 61 72 63 68 20 74 68 72 6f 75 67 68 20 74 68 65 arch through the
14fab 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
14fac 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 ..**.** Instead
14fad 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 of a lock on the
14fae 20 62 2d 74 72 65 65 20 72 6f 6f 74 65 64 20 61 b-tree rooted a
14faf 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 68 t page iRoot, th
14fb0 65 20 63 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20 e caller may.**
14fb1 68 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f 63 hold a write-loc
14fb2 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20 k on the schema
14fb3 74 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 65 table (root page
14fb4 20 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c 73 1). This is als
14fb5 6f 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2e o.** acceptable.
14fb6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 .*/.static int h
14fb7 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 asSharedCacheTab
14fb8 6c 65 4c 6f 63 6b 28 0a 20 20 42 74 72 65 65 20 leLock(. Btree
14fb9 2a 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 *pBtree,
14fba 20 2f 2a 20 48 61 6e 64 6c 65 20 74 68 61 74 20 /* Handle that
14fbb 6d 75 73 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a must hold lock *
14fbc 2f 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 /. Pgno iRoot,
14fbd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
14fbe 6f 74 20 70 61 67 65 20 6f 66 20 62 2d 74 72 65 ot page of b-tre
14fbf 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64 e */. int isInd
14fc0 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ex, /*
14fc1 20 54 72 75 65 20 69 66 20 69 52 6f 6f 74 20 69 True if iRoot i
14fc2 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e s the root of an
14fc3 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 2a 2f index b-tree */
14fc4 0a 20 20 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65 . int eLockType
14fc5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71 /* Req
14fc6 75 69 72 65 64 20 6c 6f 63 6b 20 74 79 70 65 20 uired lock type
14fc7 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 (READ_LOCK or WR
14fc8 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a ITE_LOCK) */.){.
14fc9 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
14fca 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 42 a = (Schema *)pB
14fcb 74 72 65 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65 tree->pBt->pSche
14fcc 6d 61 3b 0a 20 20 50 67 6e 6f 20 69 54 61 62 20 ma;. Pgno iTab
14fcd 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 = 0;. BtLock *p
14fce 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 Lock;.. /* If t
14fcf 68 69 73 20 62 2d 74 72 65 65 20 64 61 74 61 62 his b-tree datab
14fd0 61 73 65 20 69 73 20 6e 6f 74 20 73 68 61 72 65 ase is not share
14fd1 61 62 6c 65 2c 20 6f 72 20 69 66 20 74 68 65 20 able, or if the
14fd2 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e client is readin
14fd3 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61 73 20 74 g. ** and has t
14fd4 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 he read-uncommit
14fd5 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68 ted flag set, th
14fd6 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 65 en no lock is re
14fd7 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a 20 49 6e quired. . ** In
14fd8 20 74 68 65 73 65 20 63 61 73 65 73 20 72 65 74 these cases ret
14fd9 75 72 6e 20 74 72 75 65 20 69 6d 6d 65 64 69 61 urn true immedia
14fda 74 65 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6c tely. If the cl
14fdb 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 ient is reading
14fdc 0a 20 20 2a 2a 20 6f 72 20 77 72 69 74 69 6e 67 . ** or writing
14fdd 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 an index b-tree
14fde 2c 20 62 75 74 20 74 68 65 20 73 63 68 65 6d 61 , but the schema
14fdf 20 69 73 20 6e 6f 74 20 6c 6f 61 64 65 64 2c 20 is not loaded,
14fe0 74 68 65 6e 20 72 65 74 75 72 6e 0a 20 20 2a 2a then return. **
14fe1 20 74 72 75 65 20 61 6c 73 6f 2e 20 49 6e 20 74 true also. In t
14fe2 68 69 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63 his case the loc
14fe3 6b 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 62 k is required, b
14fe4 75 74 20 69 74 20 69 73 20 74 6f 6f 20 64 69 66 ut it is too dif
14fe5 66 69 63 75 6c 74 0a 20 20 2a 2a 20 74 6f 20 63 ficult. ** to c
14fe6 68 65 63 6b 20 69 66 20 74 68 65 20 63 6c 69 65 heck if the clie
14fe7 6e 74 20 61 63 74 75 61 6c 6c 79 20 68 6f 6c 64 nt actually hold
14fe8 73 20 69 74 2e 20 54 68 69 73 20 64 6f 65 73 6e s it. This doesn
14fe9 27 74 20 68 61 70 70 65 6e 20 76 65 72 79 0a 20 't happen very.
14fea 20 2a 2a 20 6f 66 74 65 6e 2e 20 20 2a 2f 0a 20 ** often. */.
14feb 20 69 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68 if( (pBtree->sh
14fec 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c arable==0). ||
14fed 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41 (eLockType==REA
14fee 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65 D_LOCK && (pBtre
14fef 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 e->db->flags & S
14ff0 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d QLITE_ReadUncomm
14ff1 69 74 74 65 64 29 29 0a 20 20 20 7c 7c 20 28 69 itted)). || (i
14ff2 73 49 6e 64 65 78 20 26 26 20 28 21 70 53 63 68 sIndex && (!pSch
14ff3 65 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d ema || (pSchema-
14ff4 3e 66 6c 61 67 73 26 44 42 5f 53 63 68 65 6d 61 >flags&DB_Schema
14ff5 4c 6f 61 64 65 64 29 3d 3d 30 20 29 29 0a 20 20 Loaded)==0 )).
14ff6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
14ff7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 . }.. /* Figur
14ff8 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74 2d 70 e out the root-p
14ff9 61 67 65 20 74 68 61 74 20 74 68 65 20 6c 6f 63 age that the loc
14ffa 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 k should be held
14ffb 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20 on. For table.
14ffc 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74 68 69 ** b-trees, thi
14ffd 73 20 69 73 20 6a 75 73 74 20 74 68 65 20 72 6f s is just the ro
14ffe 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 ot page of the b
14fff 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65 61 64 -tree being read
15000 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e or. ** written
15001 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d 74 72 . For index b-tr
15002 65 65 73 2c 20 69 74 20 69 73 20 74 68 65 20 72 ees, it is the r
15003 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 oot page of the
15004 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20 associated. **
15005 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 table. */. if(
15006 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 isIndex ){.
15007 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 HashElem *p;.
15008 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 for(p=sqliteHas
15009 68 46 69 72 73 74 28 26 70 53 63 68 65 6d 61 2d hFirst(&pSchema-
1500a 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20 70 3d >idxHash); p; p=
1500b 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 sqliteHashNext(p
1500c 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 )){. Index
1500d 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78 20 2a *pIdx = (Index *
1500e 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 )sqliteHashData(
1500f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 p);. if( pI
15010 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69 dx->tnum==(int)i
15011 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Root ){.
15012 69 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 iTab = pIdx->pTa
15013 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 ble->tnum;.
15014 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
15015 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 69 52 6f {. iTab = iRo
15016 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 ot;. }.. /* Se
15017 61 72 63 68 20 66 6f 72 20 74 68 65 20 72 65 71 arch for the req
15018 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68 uired lock. Eith
15019 65 72 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 er a write-lock
1501a 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 on root-page iTa
1501b 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 b, a . ** write
1501c 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 -lock on the sch
1501d 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 20 28 69 ema table, or (i
1501e 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20 f the client is
1501f 72 65 61 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20 reading) a. **
15020 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61 read-lock on iTa
15021 62 20 77 69 6c 6c 20 73 75 66 66 69 63 65 2e 20 b will suffice.
15022 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20 Return 1 if any
15023 6f 66 20 74 68 65 73 65 20 61 72 65 20 66 6f 75 of these are fou
15024 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c nd. */. for(pL
15025 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d ock=pBtree->pBt-
15026 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70 >pLock; pLock; p
15027 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78 Lock=pLock->pNex
15028 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 t){. if( pLoc
15029 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 74 72 65 k->pBtree==pBtre
1502a 65 20 0a 20 20 20 20 20 26 26 20 28 70 4c 6f 63 e . && (pLoc
1502b 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 k->iTable==iTab
1502c 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b || (pLock->eLock
1502d 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20 ==WRITE_LOCK &&
1502e 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31 pLock->iTable==1
1502f 29 29 0a 20 20 20 20 20 26 26 20 70 4c 6f 63 6b )). && pLock
15030 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79 ->eLock>=eLockTy
15031 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 pe . ){.
15032 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
15033 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65 . }.. /* Faile
15034 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 72 65 d to find the re
15035 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a quired lock. */.
15036 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
15037 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
15038 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20 on is also used
15039 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 72 as part of asser
1503a 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f t() statements o
1503b 6e 6c 79 2e 20 49 74 20 0a 2a 2a 20 72 65 74 75 nly. It .** retu
1503c 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 72 rns true if ther
1503d 65 20 65 78 69 73 74 20 6f 6e 65 20 6f 72 20 6d e exist one or m
1503e 6f 72 65 20 63 75 72 73 6f 72 73 20 6f 70 65 6e ore cursors open
1503f 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a on the table .*
15040 2a 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 * with root page
15041 20 69 52 6f 6f 74 20 74 68 61 74 20 64 6f 20 6e iRoot that do n
15042 6f 74 20 62 65 6c 6f 6e 67 20 74 6f 20 65 69 74 ot belong to eit
15043 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 her connection p
15044 42 74 72 65 65 20 0a 2a 2a 20 6f 72 20 73 6f 6d Btree .** or som
15045 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 e other connecti
15046 6f 6e 20 74 68 61 74 20 68 61 73 20 74 68 65 20 on that has the
15047 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 read-uncommitted
15048 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a flag set..**.**
15049 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 65 For example, be
1504a 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 fore writing to
1504b 70 61 67 65 20 69 52 6f 6f 74 3a 0a 2a 2a 0a 2a page iRoot:.**.*
1504c 2a 20 20 20 20 61 73 73 65 72 74 28 20 21 68 61 * assert( !ha
1504d 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 sReadConflicts(p
1504e 42 74 72 65 65 2c 20 69 52 6f 6f 74 29 20 29 3b Btree, iRoot) );
1504f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 .*/.static int h
15050 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 asReadConflicts(
15051 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 50 Btree *pBtree, P
15052 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42 74 gno iRoot){. Bt
15053 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 Cursor *p;. for
15054 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e (p=pBtree->pBt->
15055 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d pCursor; p; p=p-
15056 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
15057 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 p->pgnoRoot==iR
15058 6f 6f 74 20 0a 20 20 20 20 20 26 26 20 70 2d 3e oot . && p->
15059 70 42 74 72 65 65 21 3d 70 42 74 72 65 65 0a 20 pBtree!=pBtree.
1505a 20 20 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70 42 && 0==(p->pB
1505b 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 tree->db->flags
1505c 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 & SQLITE_ReadUnc
1505d 6f 6d 6d 69 74 74 65 64 29 0a 20 20 20 20 29 7b ommitted). ){
1505e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
1505f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
15060 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 urn 0;.}.#endif
15061 20 20 20 2f 2a 20 23 69 66 64 65 66 20 53 51 4c /* #ifdef SQL
15062 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a ITE_DEBUG */../*
15063 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 .** Query to see
15064 20 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65 if btree handle
15065 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 p may obtain a
15066 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f lock of type eLo
15067 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 ck .** (READ_LOC
15068 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 K or WRITE_LOCK)
15069 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 on the table wi
1506a 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 th root-page iTa
1506b 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c b. Return.** SQL
1506c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f ITE_OK if the lo
1506d 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e ck may be obtain
1506e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a ed (by calling.*
1506f 2a 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 * setSharedCache
15070 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 TableLock()), or
15071 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 SQLITE_LOCKED i
15072 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 f not..*/.static
15073 20 69 6e 74 20 71 75 65 72 79 53 68 61 72 65 64 int queryShared
15074 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 CacheTableLock(B
15075 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 tree *p, Pgno iT
15076 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 ab, u8 eLock){.
15077 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15078 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 p->pBt;. BtLoc
15079 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 k *pIter;.. ass
1507a 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1507b 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 eHoldsMutex(p) )
1507c 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 ;. assert( eLoc
1507d 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
1507e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
1507f 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 K );. assert( p
15080 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 ->db!=0 );. ass
15081 65 72 74 28 20 21 28 70 2d 3e 64 62 2d 3e 66 6c ert( !(p->db->fl
15082 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 ags&SQLITE_ReadU
15083 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c 65 4c 6f ncommitted)||eLo
15084 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c ck==WRITE_LOCK||
15085 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20 0a 20 20 iTab==1 );. .
15086 2f 2a 20 49 66 20 72 65 71 75 65 73 74 69 6e 67 /* If requesting
15087 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 a write-lock, t
15088 68 65 6e 20 74 68 65 20 42 74 72 65 65 20 6d 75 hen the Btree mu
15089 73 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 st have an open
1508a 77 72 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73 write. ** trans
1508b 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66 action on this f
1508c 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75 ile. And, obviou
1508d 73 6c 79 2c 20 66 6f 72 20 74 68 69 73 20 74 6f sly, for this to
1508e 20 62 65 20 73 6f 20 74 68 65 72 65 20 0a 20 20 be so there .
1508f 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 ** must be an op
15090 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 en write transac
15091 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65 tion on the file
15092 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 itself.. */.
15093 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 assert( eLock==R
15094 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d EAD_LOCK || (p==
15095 70 42 74 2d 3e 70 57 72 69 74 65 72 20 26 26 20 pBt->pWriter &&
15096 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
15097 53 5f 57 52 49 54 45 29 20 29 3b 0a 20 20 61 73 S_WRITE) );. as
15098 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 sert( eLock==REA
15099 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 D_LOCK || pBt->i
1509a 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
1509b 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a ANS_WRITE );. .
1509c 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 6e /* This is a n
1509d 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72 o-op if the shar
1509e 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20 ed-cache is not
1509f 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28 enabled */. if(
150a0 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b !p->sharable ){
150a1 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
150a2 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a TE_OK;. }.. /*
150a3 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 If some other c
150a4 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c onnection is hol
150a5 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 ding an exclusiv
150a6 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a e lock, the. **
150a7 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 requested lock
150a8 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69 may not be obtai
150a9 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ned.. */. if(
150aa 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20 pBt->pWriter!=p
150ab 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 && pBt->isExclus
150ac 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ive ){. sqlit
150ad 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
150ae 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e ked(p->db, pBt->
150af 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20 pWriter->db);.
150b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
150b1 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 LOCKED_SHAREDCAC
150b2 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 HE;. }.. for(p
150b3 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b Iter=pBt->pLock;
150b4 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 pIter; pIter=pI
150b5 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ter->pNext){.
150b6 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f /* The conditio
150b7 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 n (pIter->eLock!
150b8 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 20 66 =eLock) in the f
150b9 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29 ollowing if(...)
150ba 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 . ** stateme
150bb 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69 nt is a simplifi
150bc 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a cation of:. *
150bd 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63 *. ** (eLoc
150be 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c k==WRITE_LOCK ||
150bf 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57 pIter->eLock==W
150c0 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a RITE_LOCK). *
150c1 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77 *. ** since w
150c2 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 20 65 e know that if e
150c3 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
150c4 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 , then no other
150c5 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a connection. *
150c6 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 52 49 * may hold a WRI
150c7 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74 TE_LOCK on any t
150c8 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66 69 6c able in this fil
150c9 65 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 63 e (since there c
150ca 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62 an. ** only b
150cb 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69 74 65 e a single write
150cc 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 r).. */. a
150cd 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c ssert( pIter->eL
150ce 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
150cf 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d | pIter->eLock==
150d0 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 WRITE_LOCK );.
150d1 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d assert( eLock=
150d2 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49 =READ_LOCK || pI
150d3 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c ter->pBtree==p |
150d4 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d | pIter->eLock==
150d5 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 READ_LOCK);.
150d6 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 if( pIter->pBtre
150d7 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69 e!=p && pIter->i
150d8 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 Table==iTab && p
150d9 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f Iter->eLock!=eLo
150da 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ck ){. sqli
150db 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f te3ConnectionBlo
150dc 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65 cked(p->db, pIte
150dd 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a r->pBtree->db);.
150de 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d if( eLock=
150df 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20 =WRITE_LOCK ){.
150e0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
150e1 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 ==pBt->pWriter )
150e2 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69 ;. pBt->i
150e3 73 50 65 6e 64 69 6e 67 20 3d 20 31 3b 0a 20 20 sPending = 1;.
150e4 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
150e5 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 rn SQLITE_LOCKED
150e6 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 _SHAREDCACHE;.
150e7 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
150e8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
150e9 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f ndif /* !SQLITE_
150ea 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
150eb 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
150ec 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
150ed 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64 _CACHE./*.** Add
150ee 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 a lock on the t
150ef 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 able with root-p
150f0 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 68 age iTable to th
150f1 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 75 e shared-btree u
150f2 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 20 sed.** by Btree
150f3 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d 65 handle p. Parame
150f4 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 62 ter eLock must b
150f5 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c 4f e either READ_LO
150f6 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 5f CK or .** WRITE_
150f7 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 LOCK..**.** This
150f8 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 function assume
150f9 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a s the following:
150fa 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54 68 65 .**.** (a) The
150fb 20 73 70 65 63 69 66 69 65 64 20 62 2d 74 72 65 specified b-tre
150fc 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e e connection han
150fd 64 6c 65 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 dle is connected
150fe 20 74 6f 20 61 20 73 68 61 72 61 62 6c 65 0a 2a to a sharable.*
150ff 2a 20 20 20 20 20 20 20 62 2d 74 72 65 65 20 64 * b-tree d
15100 61 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69 74 atabase (one wit
15101 68 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 73 h the BtShared.s
15102 68 61 72 61 62 6c 65 29 20 66 6c 61 67 20 73 65 harable) flag se
15103 74 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 28 t, and.**.** (
15104 62 29 20 4e 6f 20 6f 74 68 65 72 20 62 2d 74 72 b) No other b-tr
15105 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 ee connection ha
15106 6e 64 6c 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63 ndle holds a loc
15107 6b 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 k that conflicts
15108 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 68 20 74 .** with t
15109 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 he requested loc
1510a 6b 20 28 69 2e 65 2e 20 71 75 65 72 79 53 68 61 k (i.e. querySha
1510b 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
1510c 6b 28 29 20 68 61 73 0a 2a 2a 20 20 20 20 20 20 k() has.**
1510d 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 already been ca
1510e 6c 6c 65 64 20 61 6e 64 20 72 65 74 75 72 6e 65 lled and returne
1510f 64 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a d SQLITE_OK)..**
15110 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
15111 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 returned if the
15112 20 6c 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73 lock is added s
15113 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c uccessfully. SQL
15114 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a 2a 20 69 73 ITE_NOMEM .** is
15115 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d returned if a m
15116 61 6c 6c 6f 63 20 61 74 74 65 6d 70 74 20 66 61 alloc attempt fa
15117 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ils..*/.static i
15118 6e 74 20 73 65 74 53 68 61 72 65 64 43 61 63 68 nt setSharedCach
15119 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65 eTableLock(Btree
1511a 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65 *p, Pgno iTable
1511b 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 , u8 eLock){. B
1511c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
1511d 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 ->pBt;. BtLock
1511e 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 *pLock = 0;. Bt
1511f 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 Lock *pIter;..
15120 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
15121 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
15122 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 ) );. assert( e
15123 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 Lock==READ_LOCK
15124 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f || eLock==WRITE_
15125 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
15126 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 0a 20 ( p->db!=0 );..
15127 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e /* A connection
15128 20 77 69 74 68 20 74 68 65 20 72 65 61 64 2d 75 with the read-u
15129 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 ncommitted flag
1512a 73 65 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 set will never t
1512b 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69 ry to. ** obtai
1512c 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 75 73 n a read-lock us
1512d 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f ing this functio
1512e 6e 2e 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 64 n. The only read
1512f 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a 20 -lock obtained.
15130 20 2a 2a 20 62 79 20 61 20 63 6f 6e 6e 65 63 74 ** by a connect
15131 69 6f 6e 20 69 6e 20 72 65 61 64 2d 75 6e 63 6f ion in read-unco
15132 6d 6d 69 74 74 65 64 20 6d 6f 64 65 20 69 73 20 mmitted mode is
15133 6f 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 on the sqlite_ma
15134 73 74 65 72 20 0a 20 20 2a 2a 20 74 61 62 6c 65 ster . ** table
15135 2c 20 61 6e 64 20 74 68 61 74 20 6c 6f 63 6b 20 , and that lock
15136 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 42 is obtained in B
15137 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 29 treeBeginTrans()
15138 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
15139 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 0==(p->db->flags
1513a 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f &SQLITE_ReadUnco
1513b 6d 6d 69 74 74 65 64 29 20 7c 7c 20 65 4c 6f 63 mmitted) || eLoc
1513c 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b k==WRITE_LOCK );
1513d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 .. /* This func
1513e 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 tion should only
1513f 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 be called on a
15140 73 68 61 72 61 62 6c 65 20 62 2d 74 72 65 65 20 sharable b-tree
15141 61 66 74 65 72 20 69 74 20 0a 20 20 2a 2a 20 68 after it . ** h
15142 61 73 20 62 65 65 6e 20 64 65 74 65 72 6d 69 6e as been determin
15143 65 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 ed that no other
15144 20 62 2d 74 72 65 65 20 68 6f 6c 64 73 20 61 20 b-tree holds a
15145 63 6f 6e 66 6c 69 63 74 69 6e 67 20 6c 6f 63 6b conflicting lock
15146 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
15147 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 p->sharable );.
15148 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f assert( SQLITE_
15149 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72 65 64 43 OK==querySharedC
1514a 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c acheTableLock(p,
1514b 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63 6b 29 20 iTable, eLock)
1514c 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 );.. /* First s
1514d 65 61 72 63 68 20 74 68 65 20 6c 69 73 74 20 66 earch the list f
1514e 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c or an existing l
1514f 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c ock on this tabl
15150 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65 e. */. for(pIte
15151 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 r=pBt->pLock; pI
15152 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 ter; pIter=pIter
15153 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 ->pNext){. if
15154 28 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d ( pIter->iTable=
15155 3d 69 54 61 62 6c 65 20 26 26 20 70 49 74 65 72 =iTable && pIter
15156 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 ->pBtree==p ){.
15157 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74 pLock = pIt
15158 65 72 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b er;. break;
15159 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1515a 20 49 66 20 74 68 65 20 61 62 6f 76 65 20 73 65 If the above se
1515b 61 72 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e arch did not fin
1515c 64 20 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63 d a BtLock struc
1515d 74 20 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74 t associating Bt
1515e 72 65 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20 ree p. ** with
1515f 74 61 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c table iTable, al
15160 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c locate one and l
15161 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 ink it into the
15162 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 list.. */. if(
15163 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 !pLock ){. p
15164 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a Lock = (BtLock *
15165 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 )sqlite3MallocZe
15166 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b ro(sizeof(BtLock
15167 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f ));. if( !pLo
15168 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ck ){. retu
15169 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1516a 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b . }. pLock
1516b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c ->iTable = iTabl
1516c 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 e;. pLock->pB
1516d 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c tree = p;. pL
1516e 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 ock->pNext = pBt
1516f 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 ->pLock;. pBt
15170 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b ->pLock = pLock;
15171 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 . }.. /* Set t
15172 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 he BtLock.eLock
15173 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 variable to the
15174 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63 maximum of the c
15175 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a urrent lock. **
15176 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74 and the request
15177 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65 ed lock. This me
15178 61 6e 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c ans if a write-l
15179 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20 ock was already
1517a 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20 held. ** and a
1517b 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73 read-lock reques
1517c 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e ted, we don't in
1517d 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72 correctly downgr
1517e 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 ade the lock..
1517f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 52 49 */. assert( WRI
15180 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 TE_LOCK>READ_LOC
15181 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b K );. if( eLock
15182 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b >pLock->eLock ){
15183 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 . pLock->eLoc
15184 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a k = eLock;. }..
15185 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15186 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 OK;.}.#endif /*
15187 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 !SQLITE_OMIT_SHA
15188 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 RED_CACHE */..#i
15189 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1518a 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f T_SHARED_CACHE./
1518b 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c *.** Release all
1518c 20 74 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73 the table locks
1518d 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 (locks obtained
1518e 20 76 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a via calls to.**
1518f 20 74 68 65 20 73 65 74 53 68 61 72 65 64 43 61 the setSharedCa
15190 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70 cheTableLock() p
15191 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 rocedure) held b
15192 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 y Btree handle p
15193 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
15194 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ction assumes th
15195 61 74 20 68 61 6e 64 6c 65 20 70 20 68 61 73 20 at handle p has
15196 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20 an open read or
15197 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61 write .** transa
15198 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65 ction. If it doe
15199 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 s not, then the
1519a 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e 64 69 BtShared.isPendi
1519b 6e 67 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d ng variable.** m
1519c 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c ay be incorrectl
1519d 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74 y cleared..*/.st
1519e 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41 atic void clearA
1519f 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
151a0 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 leLocks(Btree *p
151a1 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
151a2 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
151a3 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d tLock **ppIter =
151a4 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 &pBt->pLock;..
151a5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
151a6 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
151a7 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
151a8 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30 p->sharable || 0
151a9 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61 ==*ppIter );. a
151aa 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e ssert( p->inTran
151ab 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 s>0 );.. while(
151ac 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20 *ppIter ){.
151ad 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 BtLock *pLock =
151ae 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73 *ppIter;. ass
151af 65 72 74 28 20 70 42 74 2d 3e 69 73 45 78 63 6c ert( pBt->isExcl
151b0 75 73 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d usive==0 || pBt-
151b1 3e 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d >pWriter==pLock-
151b2 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61 >pBtree );. a
151b3 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42 ssert( pLock->pB
151b4 74 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70 tree->inTrans>=p
151b5 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20 Lock->eLock );.
151b6 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 if( pLock->pB
151b7 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 tree==p ){.
151b8 20 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b *ppIter = pLock
151b9 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 61 ->pNext;. a
151ba 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54 ssert( pLock->iT
151bb 61 62 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b able!=1 || pLock
151bc 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20 ==&p->lock );.
151bd 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69 if( pLock->i
151be 54 61 62 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20 Table!=1 ){.
151bf 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
151c0 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d (pLock);. }
151c1 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
151c2 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 ppIter = &pLoc
151c3 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a k->pNext;. }.
151c4 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 }.. assert( p
151c5 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 3d 3d 30 Bt->isPending==0
151c6 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 || pBt->pWriter
151c7 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 );. if( pBt->p
151c8 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 Writer==p ){.
151c9 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 pBt->pWriter =
151ca 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78 0;. pBt->isEx
151cb 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20 clusive = 0;.
151cc 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 pBt->isPending
151cd 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 = 0;. }else if(
151ce 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 pBt->nTransacti
151cf 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20 on==2 ){. /*
151d0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
151d1 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 63 6f 6e called when con
151d2 6e 65 63 74 69 6f 6e 20 70 20 69 73 20 63 6f 6e nection p is con
151d3 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 20 cluding its .
151d4 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ** transaction.
151d5 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65 6e If there curren
151d6 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 69 tly exists a wri
151d7 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f ter, and p is no
151d8 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72 t. ** that wr
151d9 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 6e iter, then the n
151da 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 umber of locks h
151db 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f eld by connectio
151dc 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 ns other. **
151dd 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 20 than the writer
151de 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 6f must be about to
151df 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49 drop to zero. I
151e0 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20 n this case.
151e1 2a 2a 20 73 65 74 20 74 68 65 20 69 73 50 65 6e ** set the isPen
151e2 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e 0a ding flag to 0..
151e3 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 **. ** If
151e4 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 75 there is not cu
151e5 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 72 rrently a writer
151e6 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 2e , then BtShared.
151e7 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a 20 isPending must.
151e8 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61 6c ** be zero al
151e9 72 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20 6e ready. So this n
151ea 65 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72 6d ext line is harm
151eb 6c 65 73 73 20 69 6e 20 74 68 61 74 20 63 61 73 less in that cas
151ec 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 e.. */. pB
151ed 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 t->isPending = 0
151ee 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
151ef 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 his function cha
151f0 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c nges all write-l
151f1 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e ocks held by con
151f2 6e 65 63 74 69 6f 6e 20 70 20 74 6f 20 72 65 61 nection p to rea
151f3 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 d-locks..*/.stat
151f4 69 63 20 76 6f 69 64 20 64 6f 77 6e 67 72 61 64 ic void downgrad
151f5 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 eAllSharedCacheT
151f6 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 ableLocks(Btree
151f7 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 *p){. BtShared
151f8 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
151f9 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 if( pBt->pWrite
151fa 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f r==p ){. BtLo
151fb 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 ck *pLock;. p
151fc 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b Bt->pWriter = 0;
151fd 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78 63 6c . pBt->isExcl
151fe 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20 20 70 usive = 0;. p
151ff 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 Bt->isPending =
15200 30 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 0;. for(pLock
15201 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f =pBt->pLock; pLo
15202 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d ck; pLock=pLock-
15203 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 >pNext){. a
15204 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c ssert( pLock->eL
15205 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
15206 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d | pLock->pBtree=
15207 3d 70 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 =p );. pLoc
15208 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f k->eLock = READ_
15209 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a LOCK;. }. }.
1520a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
1520b 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
1520c 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 CACHE */..static
1520d 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 void releasePag
1520e 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
1520f 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 ); /* Forward r
15210 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a eference */../*.
15211 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 ** Verify that t
15212 68 65 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20 he cursor holds
15213 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 a mutex on the B
15214 74 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 6e 64 tShared.*/.#ifnd
15215 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 ef NDEBUG.static
15216 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 int cursorHolds
15217 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a Mutex(BtCursor *
15218 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c p){. return sql
15219 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1521a 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a p->pBt->mutex);.
1521b 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 }.#endif...#ifnd
1521c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
1521d 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e NCRBLOB./*.** In
1521e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 validate the ove
1521f 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
15220 63 61 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 cache for cursor
15221 20 70 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a pCur, if any..*
15222 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
15223 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
15224 43 61 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a Cache(BtCursor *
15225 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 pCur){. assert(
15226 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
15227 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c x(pCur) );. sql
15228 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e ite3_free(pCur->
15229 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 aOverflow);. pC
1522a 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 ur->aOverflow =
1522b 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 0;.}../*.** Inva
1522c 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 lidate the overf
1522d 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
1522e 63 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 che for all curs
1522f 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e ors opened.** on
15230 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 the shared btre
15231 65 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e e structure pBt.
15232 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
15233 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 invalidateAllOve
15234 72 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 rflowCache(BtSha
15235 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 red *pBt){. BtC
15236 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 ursor *p;. asse
15237 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15238 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
15239 78 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 x) );. for(p=pB
1523a 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 t->pCursor; p; p
1523b 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 =p->pNext){.
1523c 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c invalidateOverfl
1523d 6f 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a owCache(p);. }.
1523e 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
1523f 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
15240 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e before modifyin
15241 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f g the contents o
15242 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 62 2d 74 f a table.** b-t
15243 72 65 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 ree to invalidat
15244 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 e any incrblob c
15245 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 ursors that are
15246 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 open on the.** r
15247 6f 77 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 ow or one of the
15248 20 72 6f 77 73 20 62 65 69 6e 67 20 6d 6f 64 69 rows being modi
15249 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 fied..**.** If a
1524a 72 67 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 rgument isClearT
1524b 61 62 6c 65 20 69 73 20 74 72 75 65 2c 20 74 68 able is true, th
1524c 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f en the entire co
1524d 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a ntents of the.**
1524e 20 74 61 62 6c 65 20 69 73 20 61 62 6f 75 74 20 table is about
1524f 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 49 to be deleted. I
15250 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76 61 n this case inva
15251 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 lidate all incrb
15252 6c 6f 62 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f lob.** cursors o
15253 70 65 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77 pen on any row w
15254 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ithin the table
15255 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70 with root-page p
15256 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f gnoRoot..**.** O
15257 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 72 67 therwise, if arg
15258 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62 ument isClearTab
15259 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 le is false, the
1525a 6e 20 74 68 65 20 72 6f 77 20 77 69 74 68 0a 2a n the row with.*
1525b 2a 20 72 6f 77 69 64 20 69 52 6f 77 20 69 73 20 * rowid iRow is
1525c 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20 6f being replaced o
1525d 72 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 r deleted. In th
1525e 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 is case invalida
1525f 74 65 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65 te.** only those
15260 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 incrblob cursor
15261 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 73 s open on this s
15262 70 65 63 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a pecific row..*/.
15263 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 static void inva
15264 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 lidateIncrblobCu
15265 72 73 6f 72 73 28 0a 20 20 42 74 72 65 65 20 2a rsors(. Btree *
15266 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 pBtree,
15267 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
15268 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a file to check *
15269 2f 0a 20 20 69 36 34 20 69 52 6f 77 2c 20 20 20 /. i64 iRow,
1526a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1526b 68 65 20 72 6f 77 69 64 20 74 68 61 74 20 6d 69 he rowid that mi
1526c 67 68 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20 ght be changing
1526d 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6c 65 61 72 */. int isClear
1526e 54 61 62 6c 65 20 20 20 20 20 20 20 20 2f 2a 20 Table /*
1526f 54 72 75 65 20 69 66 20 61 6c 6c 20 72 6f 77 73 True if all rows
15270 20 61 72 65 20 62 65 69 6e 67 20 64 65 6c 65 74 are being delet
15271 65 64 20 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72 ed */.){. BtCur
15272 73 6f 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72 sor *p;. BtShar
15273 65 64 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65 ed *pBt = pBtree
15274 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
15275 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
15276 64 73 4d 75 74 65 78 28 70 42 74 72 65 65 29 20 dsMutex(pBtree)
15277 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e );. for(p=pBt->
15278 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d pCursor; p; p=p-
15279 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
1527a 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 p->isIncrblobHa
1527b 6e 64 6c 65 20 26 26 20 28 69 73 43 6c 65 61 72 ndle && (isClear
1527c 54 61 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f Table || p->info
1527d 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 20 29 7b 0a .nKey==iRow) ){.
1527e 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20 p->eState
1527f 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
15280 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 ;. }. }.}..#
15281 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 69 else. #define i
15282 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f nvalidateOverflo
15283 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 wCache(x). #def
15284 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c ine invalidateAl
15285 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 lOverflowCache(x
15286 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 ). #define inva
15287 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 lidateIncrblobCu
15288 72 73 6f 72 73 28 78 2c 79 2c 7a 29 0a 23 65 6e rsors(x,y,z).#en
15289 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62 dif../*.** Set b
1528a 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42 it pgno of the B
1528b 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 tShared.pHasCont
1528c 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73 ent bitvec. This
1528d 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77 is called .** w
1528e 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 hen a page that
1528f 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 previously conta
15290 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65 ined data become
15291 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 s a free-list le
15292 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a af .** page..**.
15293 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e ** The BtShared.
15294 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
15295 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72 ec exists to wor
15296 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63 k around an obsc
15297 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65 ure.** bug cause
15298 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63 d by the interac
15299 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66 tion of two usef
1529a 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69 ul IO optimizati
1529b 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a ons surrounding.
1529c 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 ** free-list lea
1529d 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 f pages:.**.**
1529e 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74 1) When all dat
1529f 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f a is deleted fro
152a0 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65 m a page and the
152a1 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a page becomes.**
152a2 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73 a free-lis
152a3 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65 t leaf page, the
152a4 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69 page is not wri
152a5 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
152a6 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73 base.** (as
152a7 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 free-list leaf
152a8 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f pages contain no
152a9 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61 meaningful data
152aa 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 ). Sometimes.**
152ab 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65 such a page
152ac 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75 is not even jou
152ad 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77 rnalled (as it w
152ae 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 ill not be modif
152af 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79 ied,.** why
152b0 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c bother journall
152b1 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20 ing it?)..**.**
152b2 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65 2) When a free
152b3 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 -list leaf page
152b4 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63 is reused, its c
152b5 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65 ontent is not re
152b6 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 ad.** from
152b7 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 the database or
152b8 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
152b9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79 ournal file (why
152ba 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20 should it.**
152bb 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20 be, if it is
152bc 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69 not at all meani
152bd 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42 ngful?)..**.** B
152be 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68 y themselves, th
152bf 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ese optimization
152c0 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20 s work fine and
152c1 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a provide a handy.
152c2 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 ** performance b
152c3 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c oost to bulk del
152c4 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70 ete or insert op
152c5 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65 erations. Howeve
152c6 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20 r, if.** a page
152c7 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 is moved to the
152c8 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68 free-list and th
152c9 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e en reused within
152ca 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61 the same.** tra
152cb 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62 nsaction, a prob
152cc 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66 lem comes up. If
152cd 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
152ce 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e journalled when
152cf 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20 .** it is moved
152d0 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 to the free-list
152d1 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20 and it is also
152d2 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 not journalled w
152d3 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74 hen it.** is ext
152d4 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 racted from the
152d5 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65 free-list and re
152d6 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f used, then the o
152d7 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 riginal data.**
152d8 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20 may be lost. In
152d9 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72 the event of a r
152da 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20 ollback, it may
152db 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a not be possible.
152dc 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 ** to restore th
152dd 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 e database to it
152de 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69 s original confi
152df 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 guration..**.**
152e0 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 The solution is
152e1 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 the BtShared.pHa
152e2 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e sContent bitvec.
152e3 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65 Whenever a page
152e4 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f is .** moved to
152e5 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c become a free-l
152e6 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 ist leaf page, t
152e7 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
152e8 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69 bit is.** set i
152e9 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68 n the bitvec. Wh
152ea 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61 enever a leaf pa
152eb 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20 ge is extracted
152ec 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 from the free-li
152ed 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 st,.** optimizat
152ee 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f ion 2 above is o
152ef 6d 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63 mmitted if the c
152f0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 orresponding bit
152f1 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73 is already.** s
152f2 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 et in BtShared.p
152f3 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20 HasContent. The
152f4 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
152f5 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72 bitvec are clear
152f6 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 ed.** at the end
152f7 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61 of every transa
152f8 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
152f9 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73 int btreeSetHas
152fa 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 Content(BtShared
152fb 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f *pBt, Pgno pgno
152fc 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
152fd 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21 LITE_OK;. if( !
152fe 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 pBt->pHasContent
152ff 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 ){. int nPag
15300 65 20 3d 20 31 30 30 3b 0a 20 20 20 20 73 71 6c e = 100;. sql
15301 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
15302 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 nt(pBt->pPager,
15303 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 2f 2a 20 &nPage);. /*
15304 49 66 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 If sqlite3PagerP
15305 61 67 65 63 6f 75 6e 74 28 29 20 66 61 69 6c 73 agecount() fails
15306 20 74 68 65 72 65 20 69 73 20 6e 6f 20 68 61 72 there is no har
15307 6d 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20 m because the.
15308 20 20 2a 2a 20 6e 50 61 67 65 20 76 61 72 69 61 ** nPage varia
15309 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ble is unchanged
1530a 20 66 72 6f 6d 20 69 74 73 20 64 65 66 61 75 6c from its defaul
1530b 74 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20 2a t value of 100 *
1530c 2f 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73 43 /. pBt->pHasC
1530d 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 33 ontent = sqlite3
1530e 42 69 74 76 65 63 43 72 65 61 74 65 28 28 75 33 BitvecCreate((u3
1530f 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 2)nPage);. if
15310 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 ( !pBt->pHasCont
15311 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ent ){. rc
15312 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
15313 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
15314 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
15315 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69 pgno<=sqlite3Bi
15316 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 48 tvecSize(pBt->pH
15317 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20 asContent) ){.
15318 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 rc = sqlite3Bi
15319 74 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 61 tvecSet(pBt->pHa
1531a 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b sContent, pgno);
1531b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1531c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 ;.}../*.** Query
1531d 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 the BtShared.pH
1531e 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72 asContent vector
1531f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
15320 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
15321 77 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74 when a free-list
15322 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 65 leaf page is re
15323 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a moved from the.*
15324 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 20 * free-list for
15325 72 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 6e reuse. It return
15326 73 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73 s false if it is
15327 20 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 76 safe to retriev
15328 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 72 e the.** page fr
15329 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 om the pager lay
1532a 65 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f 2d er with the 'no-
1532b 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 65 content' flag se
1532c 74 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 73 t. True otherwis
1532d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1532e 20 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74 btreeGetHasCont
1532f 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 ent(BtShared *pB
15330 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 t, Pgno pgno){.
15331 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 74 Bitvec *p = pBt
15332 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20 ->pHasContent;.
15333 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 return (p && (p
15334 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 65 gno>sqlite3Bitve
15335 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69 cSize(p) || sqli
15336 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2c te3BitvecTest(p,
15337 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a pgno)));.}../*.
15338 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 6f ** Clear (destro
15339 79 29 20 74 68 65 20 42 74 53 68 61 72 65 64 2e y) the BtShared.
1533a 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 pHasContent bitv
1533b 65 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20 ec. This should
1533c 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74 be.** invoked at
1533d 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 the conclusion
1533e 6f 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 72 of each write-tr
1533f 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 ansaction..*/.st
15340 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 43 atic void btreeC
15341 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 42 learHasContent(B
15342 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
15343 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 sqlite3BitvecDe
15344 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 43 stroy(pBt->pHasC
15345 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e ontent);. pBt->
15346 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b pHasContent = 0;
15347 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 .}../*.** Save t
15348 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f he current curso
15349 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 r position in th
1534a 65 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 e variables BtCu
1534b 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e rsor.nKey .** an
1534c 64 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e d BtCursor.pKey.
1534d 20 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 74 The cursor's st
1534e 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 55 ate is set to CU
1534f 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
15350 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
15351 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 er must ensure t
15352 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 hat the cursor i
15353 73 20 76 61 6c 69 64 20 28 68 61 73 20 65 53 74 s valid (has eSt
15354 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
15355 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63 D).** prior to c
15356 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 alling this rout
15357 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 ine. .*/.static
15358 20 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 int saveCursorP
15359 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 osition(BtCursor
1535a 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 *pCur){. int r
1535b 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 c;.. assert( CU
1535c 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 RSOR_VALID==pCur
1535d 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 ->eState );. as
1535e 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 sert( 0==pCur->p
1535f 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Key );. assert(
15360 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
15361 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 x(pCur) );.. rc
15362 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
15363 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 eySize(pCur, &pC
15364 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 61 73 73 ur->nKey);. ass
15365 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
15366 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 53 69 7a OK ); /* KeySiz
15367 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 e() cannot fail
15368 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 */.. /* If this
15369 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 is an intKey ta
1536a 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62 ble, then the ab
1536b 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 ove call to Btre
1536c 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20 eKeySize(). **
1536d 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67 stores the integ
1536e 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e er key in pCur->
1536f 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61 nKey. In this ca
15370 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 se this value is
15371 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69 . ** all that i
15372 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65 s required. Othe
15373 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69 rwise, if pCur i
15374 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e s not open on an
15375 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62 intKey. ** tab
15376 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20 le, then malloc
15377 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74 space for and st
15378 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b ore the pCur->nK
15379 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 ey bytes of key
1537a 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f . ** data.. */
1537b 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 72 2d 3e . if( 0==pCur->
1537c 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 apPage[0]->intKe
1537d 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 y ){. void *p
1537e 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Key = sqlite3Mal
1537f 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e loc( (int)pCur->
15380 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 nKey );. if(
15381 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 pKey ){. rc
15382 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
15383 65 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74 ey(pCur, 0, (int
15384 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65 )pCur->nKey, pKe
15385 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 y);. if( rc
15386 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
15387 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 pCur->pKe
15388 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 y = pKey;.
15389 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
1538a 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79 qlite3_free(pKey
1538b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1538c 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
1538d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1538e 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
1538f 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65 t( !pCur->apPage
15390 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21 [0]->intKey || !
15391 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20 pCur->pKey );..
15392 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
15393 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b OK ){. int i;
15394 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
15395 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b =pCur->iPage; i+
15396 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 +){. releas
15397 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 ePage(pCur->apPa
15398 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43 ge[i]);. pC
15399 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 ur->apPage[i] =
1539a 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 0;. }. pCu
1539b 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 r->iPage = -1;.
1539c 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
1539d 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 = CURSOR_REQUIRE
1539e 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76 SEEK;. }.. inv
1539f 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 alidateOverflowC
153a0 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65 ache(pCur);. re
153a1 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
153a2 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 * Save the posit
153a3 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73 ions of all curs
153a4 6f 72 73 20 65 78 63 65 70 74 20 70 45 78 63 65 ors except pExce
153a5 70 74 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 pt open on the t
153a6 61 62 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f able .** with ro
153a7 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 ot-page iRoot. U
153a8 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 sually, this is
153a9 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f called just befo
153aa 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 re cursor.** pEx
153ab 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 cept is used to
153ac 6d 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 modify the table
153ad 20 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 (BtreeDelete()
153ae 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 or BtreeInsert()
153af 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
153b0 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 saveAllCursors(
153b1 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
153b2 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 gno iRoot, BtCur
153b3 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 sor *pExcept){.
153b4 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 BtCursor *p;.
153b5 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
153b6 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
153b7 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
153b8 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c rt( pExcept==0 |
153b9 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d | pExcept->pBt==
153ba 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 pBt );. for(p=p
153bb 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
153bc 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
153bd 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 if( p!=pExcept
153be 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 && (0==iRoot ||
153bf 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f p->pgnoRoot==iRo
153c0 6f 74 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 ot) && .
153c1 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f p->eState==CURSO
153c2 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 R_VALID ){.
153c3 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 int rc = saveCu
153c4 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b rsorPosition(p);
153c5 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 . if( SQLIT
153c6 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 E_OK!=rc ){.
153c7 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
153c8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
153c9 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
153ca 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c _OK;.}../*.** Cl
153cb 65 61 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 ear the current
153cc 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e cursor position.
153cd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
153ce 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
153cf 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
153d0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
153d1 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
153d2 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
153d3 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 ) );. sqlite3_f
153d4 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
153d5 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 . pCur->pKey =
153d6 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 0;. pCur->eStat
153d7 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c e = CURSOR_INVAL
153d8 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 ID;.}../*.** In
153d9 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 this version of
153da 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 BtreeMoveto, pKe
153db 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e y is a packed in
153dc 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75 dex record.** su
153dd 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 74 ch as is generat
153de 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b ed by the OP_Mak
153df 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 eRecord opcode.
153e0 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72 Unpack the.** r
153e1 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 ecord and then c
153e2 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 all BtreeMovetoU
153e3 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 npacked() to do
153e4 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 the work..*/.sta
153e5 74 69 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76 tic int btreeMov
153e6 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 eto(. BtCursor
153e7 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 *pCur, /* Cu
153e8 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 rsor open on the
153e9 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 btree to be sea
153ea 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 rched */. const
153eb 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f void *pKey, /
153ec 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 * Packed key if
153ed 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 the btree is an
153ee 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e index */. i64 n
153ef 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f Key, /
153f0 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f * Integer key fo
153f1 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 r tables. Size
153f2 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 of pKey for indi
153f3 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 ces */. int bia
153f4 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
153f5 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 Bias search to t
153f6 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 he high end */.
153f7 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 int *pRes
153f8 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 /* Write se
153f9 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 arch results her
153fa 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
153fb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
153fc 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 /* Status c
153fd 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 ode */. Unpacke
153fe 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 dRecord *pIdxKey
153ff 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 ; /* Unpacked
15400 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 index key */. c
15401 68 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b har aSpace[150];
15402 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d /* Tem
15403 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 p space for pIdx
15404 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 Key - to avoid a
15405 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66 malloc */.. if
15406 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 ( pKey ){. as
15407 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34 sert( nKey==(i64
15408 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20 )(int)nKey );.
15409 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 pIdxKey = sqli
1540a 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 te3VdbeRecordUnp
1540b 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e ack(pCur->pKeyIn
1540c 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 fo, (int)nKey, p
1540d 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Key,.
1540e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1540f 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63 aSpac
15410 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 e, sizeof(aSpace
15411 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 ));. if( pIdx
15412 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 Key==0 ) return
15413 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15414 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b }else{. pIdxK
15415 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 ey = 0;. }. rc
15416 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d = sqlite3BtreeM
15417 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 ovetoUnpacked(pC
15418 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 ur, pIdxKey, nKe
15419 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a y, bias, pRes);.
1541a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 if( pKey ){.
1541b 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c sqlite3VdbeDel
1541c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
1541d 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a d(pIdxKey);. }.
1541e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1541f 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 /*.** Restore th
15420 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
15421 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 position it was
15422 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 in (or as close
15423 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a to as possible).
15424 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 ** when saveCurs
15425 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 orPosition() was
15426 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 called. Note th
15427 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c at this call del
15428 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 etes the .** sav
15429 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f ed position info
1542a 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 stored by saveC
1542b 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c ursorPosition(),
1542c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 so there can be
1542d 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 .** at most one
1542e 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 effective restor
1542f 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
15430 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 ) call after eac
15431 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 h .** saveCursor
15432 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 Position()..*/.s
15433 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52 tatic int btreeR
15434 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
15435 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 tion(BtCursor *p
15436 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Cur){. int rc;.
15437 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
15438 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
15439 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
1543a 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 ur->eState>=CURS
1543b 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
1543c 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
1543d 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 tate==CURSOR_FAU
1543e 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e LT ){. return
1543f 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b pCur->skipNext;
15440 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 . }. pCur->eSt
15441 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
15442 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 ALID;. rc = btr
15443 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 eeMoveto(pCur, p
15444 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d Cur->pKey, pCur-
15445 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d >nKey, 0, &pCur-
15446 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 >skipNext);. if
15447 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15448 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
15449 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b ree(pCur->pKey);
1544a 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 . pCur->pKey
1544b 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = 0;. assert(
1544c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
1544d 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 URSOR_VALID || p
1544e 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
1544f 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 SOR_INVALID );.
15450 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
15451 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f }..#define resto
15452 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
15453 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 (p) \. (p->eSta
15454 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 te>=CURSOR_REQUI
15455 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 RESEEK ? \.
15456 20 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 btreeRestore
15457 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
15458 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 ) : \. S
15459 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a QLITE_OK)../*.**
1545a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 Determine wheth
1545b 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 er or not a curs
1545c 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f or has moved fro
1545d 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 m the position i
1545e 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c t.** was last pl
1545f 61 63 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72 aced at. Cursor
15460 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20 s can move when
15461 74 68 65 20 72 6f 77 20 74 68 65 79 20 61 72 65 the row they are
15462 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 pointing.** at
15463 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 is deleted out f
15464 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a rom under them..
15465 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
15466 6e 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 ne returns an er
15467 72 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65 ror code if some
15468 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
15469 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 . The.** intege
1546a 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20 r *pHasMoved is
1546b 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68 set to one if th
1546c 65 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 e cursor has mov
1546d 65 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e ed and 0 if not.
1546e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1546f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
15470 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 reeCursorHasMove
15471 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 d(BtCursor *pCur
15472 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64 , int *pHasMoved
15473 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
15474 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 rc = restoreCurs
15475 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 orPosition(pCur)
15476 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
15477 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 *pHasMoved = 1
15478 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
15479 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d . }. if( pCur-
1547a 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
1547b 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73 VALID || pCur->s
1547c 6b 69 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20 kipNext!=0 ){.
1547d 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 *pHasMoved = 1
1547e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a ;. }else{. *
1547f 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 pHasMoved = 0;.
15480 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
15481 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 TE_OK;.}..#ifnde
15482 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
15483 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 TOVACUUM./*.** G
15484 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 iven a page numb
15485 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 er of a regular
15486 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 database page, r
15487 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a eturn the page.*
15488 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 * number for the
15489 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
1548a 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
1548b 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 the entry for th
1548c 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 e.** input page
1548d 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
1548e 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 c Pgno ptrmapPag
1548f 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 eno(BtShared *pB
15490 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 t, Pgno pgno){.
15491 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 int nPagesPerMa
15492 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50 pPage;. Pgno iP
15493 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73 trMap, ret;. as
15494 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15495 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
15496 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73 tex) );. nPages
15497 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42 PerMapPage = (pB
15498 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 t->usableSize/5)
15499 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 +1;. iPtrMap =
1549a 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 (pgno-2)/nPagesP
1549b 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74 erMapPage;. ret
1549c 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 = (iPtrMap*nPag
1549d 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20 esPerMapPage) +
1549e 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 2; . if( ret==P
1549f 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
154a0 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 (pBt) ){. ret
154a1 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ++;. }. return
154a2 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 ret;.}../*.** W
154a3 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e rite an entry in
154a4 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d to the pointer m
154a5 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ap..**.** This r
154a6 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74 outine updates t
154a7 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 he pointer map e
154a8 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75 ntry for page nu
154a9 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f mber 'key'.** so
154aa 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f that it maps to
154ab 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e type 'eType' an
154ac 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 d parent page nu
154ad 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a mber 'pgno'..**.
154ae 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e ** If *pRC is in
154af 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f itially non-zero
154b0 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 (non-SQLITE_OK)
154b1 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
154b2 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 ne is.** a no-op
154b3 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f . If an error o
154b4 63 63 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f ccurs, the appro
154b5 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 priate error cod
154b6 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 e is written.**
154b7 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 into *pRC..*/.st
154b8 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 atic void ptrmap
154b9 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 Put(BtShared *pB
154ba 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 t, Pgno key, u8
154bb 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 eType, Pgno pare
154bc 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 nt, int *pRC){.
154bd 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 DbPage *pDbPage
154be 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 ; /* The pointe
154bf 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 r map page */.
154c0 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 u8 *pPtrmap;
154c1 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
154c2 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 map data */. P
154c3 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20 gno iPtrmap;
154c4 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 /* The pointer
154c5 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20 map page number
154c6 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b */. int offset;
154c7 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
154c8 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 in pointer map
154c9 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 page */. int rc
154ca 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 ; /* R
154cb 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
154cc 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a subfunctions */.
154cd 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 . if( *pRC ) re
154ce 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 turn;.. assert(
154cf 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
154d0 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
154d1 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 );. /* The mast
154d2 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 er-journal page
154d3 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65 number must neve
154d4 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70 r be used as a p
154d5 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 ointer map page
154d6 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d */. assert( 0==
154d7 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 PTRMAP_ISPAGE(pB
154d8 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t, PENDING_BYTE_
154d9 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 PAGE(pBt)) );..
154da 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 assert( pBt->au
154db 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 toVacuum );. if
154dc 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 ( key==0 ){.
154dd 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f *pRC = SQLITE_CO
154de 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
154df 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 return;. }. iP
154e0 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 trmap = PTRMAP_P
154e1 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b AGENO(pBt, key);
154e2 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
154e3 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 agerGet(pBt->pPa
154e4 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 ger, iPtrmap, &p
154e5 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 DbPage);. if( r
154e6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
154e7 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 *pRC = rc;.
154e8 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
154e9 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 offset = PTRMAP
154ea 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d _PTROFFSET(iPtrm
154eb 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 ap, key);. if(
154ec 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 offset<0 ){.
154ed 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f *pRC = SQLITE_CO
154ee 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
154ef 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74 goto ptrmap_exit
154f0 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 ;. }. pPtrmap
154f1 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 = (u8 *)sqlite3P
154f2 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 agerGetData(pDbP
154f3 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 age);.. if( eTy
154f4 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 pe!=pPtrmap[offs
154f5 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 et] || get4byte(
154f6 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b &pPtrmap[offset+
154f7 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 1])!=parent ){.
154f8 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 TRACE(("PTRMA
154f9 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 P_UPDATE: %d->(%
154fa 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 d,%d)\n", key, e
154fb 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a Type, parent));.
154fc 20 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 *pRC= rc = s
154fd 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
154fe 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 (pDbPage);. i
154ff 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
15500 20 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 ){. pPtrma
15501 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 p[offset] = eTyp
15502 65 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 e;. put4byt
15503 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 e(&pPtrmap[offse
15504 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 t+1], parent);.
15505 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 }. }..ptrmap
15506 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 _exit:. sqlite3
15507 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 PagerUnref(pDbPa
15508 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ge);.}../*.** Re
15509 61 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d ad an entry from
1550a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
1550b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1550c 74 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 tine retrieves t
1550d 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 he pointer map e
1550e 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b ntry for page 'k
1550f 65 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 ey', writing.**
15510 74 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 the type and par
15511 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ent page number
15512 74 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a to *pEType and *
15513 70 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 pPgno respective
15514 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 ly..** An error
15515 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
15516 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f if something go
15517 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 es wrong, otherw
15518 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a ise SQLITE_OK..*
15519 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 /.static int ptr
1551a 6d 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20 mapGet(BtShared
1551b 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 *pBt, Pgno key,
1551c 75 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f u8 *pEType, Pgno
1551d 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 *pPgno){. DbPa
1551e 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f ge *pDbPage; /
1551f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
15520 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 p page */. int
15521 69 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f iPtrmap; /
15522 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 * Pointer map pa
15523 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 ge index */. u8
15524 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 *pPtrmap;
15525 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 /* Pointer map
15526 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 page data */. i
15527 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 nt offset;
15528 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 /* Offset of e
15529 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 ntry in pointer
1552a 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b map */. int rc;
1552b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1552c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1552d 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 Bt->mutex) );..
1552e 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 iPtrmap = PTRMA
1552f 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 P_PAGENO(pBt, ke
15530 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 y);. rc = sqlit
15531 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e e3PagerGet(pBt->
15532 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c pPager, iPtrmap,
15533 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 &pDbPage);. if
15534 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 ( rc!=0 ){. r
15535 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
15536 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 pPtrmap = (u8 *)
15537 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
15538 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 ata(pDbPage);..
15539 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 offset = PTRMAP
1553a 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d _PTROFFSET(iPtrm
1553b 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65 ap, key);. asse
1553c 72 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b rt( pEType!=0 );
1553d 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 . *pEType = pPt
1553e 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 rmap[offset];.
1553f 69 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 if( pPgno ) *pPg
15540 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
15541 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d Ptrmap[offset+1]
15542 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 );.. sqlite3Pag
15543 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 erUnref(pDbPage)
15544 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c ;. if( *pEType<
15545 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 1 || *pEType>5 )
15546 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15547 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72 ORRUPT_BKPT;. r
15548 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15549 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 .}..#else /* if
1554a 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f defined SQLITE_O
1554b 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a MIT_AUTOVACUUM *
1554c 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d /. #define ptrm
1554d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 apPut(w,x,y,z,rc
1554e 29 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d ). #define ptrm
1554f 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 apGet(w,x,y,z) S
15550 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 QLITE_OK. #defi
15551 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c ne ptrmapPutOvfl
15552 50 74 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65 Ptr(x, y, rc).#e
15553 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 ndif../*.** Give
15554 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 61 n a btree page a
15555 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 nd a cell index
15556 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 (0 means the fir
15557 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 st cell on.** th
15558 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 e page, 1 means
15559 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c the second cell,
1555a 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 and so forth) r
1555b 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a eturn a pointer.
1555c 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 ** to the cell c
1555d 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ontent..**.** Th
1555e 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 is routine works
1555f 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 only for pages
15560 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 that do not cont
15561 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ain overflow cel
15562 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 ls..*/.#define f
15563 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 indCell(P,I) \.
15564 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 ((P)->aData + (
15565 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 (P)->maskPage &
15566 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 get2byte(&(P)->a
15567 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 Data[(P)->cellOf
15568 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a fset+2*(I)])))..
15569 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 /*.** This a mor
1556a 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f e complex versio
1556b 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 n of findCell()
1556c 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a that works for.*
1556d 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 * pages that do
1556e 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 contain overflow
1556f 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 cells..*/.stati
15570 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c c u8 *findOverfl
15571 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a owCell(MemPage *
15572 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c pPage, int iCell
15573 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 ){. int i;. as
15574 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15575 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
15576 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
15577 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f for(i=pPage->nO
15578 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b verflow-1; i>=0;
15579 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b i--){. int k
1557a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76 ;. struct _Ov
1557b 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 flCell *pOvfl;.
1557c 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67 pOvfl = &pPag
1557d 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 e->aOvfl[i];.
1557e 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b k = pOvfl->idx;
1557f 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c . if( k<=iCel
15580 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b l ){. if( k
15581 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 ==iCell ){.
15582 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d return pOvfl-
15583 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a >pCell;. }.
15584 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 iCell--;.
15585 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
15586 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 n findCell(pPage
15587 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a , iCell);.}../*.
15588 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 ** Parse a cell
15589 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e content block an
1558a 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 d fill in the Ce
1558b 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 llInfo structure
1558c 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 . There.** are
1558d 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 two versions of
1558e 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 this function.
1558f 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 btreeParseCell()
15590 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c takes a .** cel
15591 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 l index as the s
15592 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 econd argument a
15593 6e 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c nd btreeParseCel
15594 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 lPtr() .** takes
15595 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
15596 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 e body of the ce
15597 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 ll as its second
15598 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
15599 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c Within this fil
1559a 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c e, the parseCell
1559b 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 () macro can be
1559c 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f called instead o
1559d 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43 f.** btreeParseC
1559e 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20 ellPtr(). Using
1559f 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 some compilers,
155a0 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73 this will be fas
155a1 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ter..*/.static v
155a2 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65 oid btreeParseCe
155a3 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 llPtr(. MemPage
155a4 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *pPage,
155a5 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e /* Page contain
155a6 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a ing the cell */.
155a7 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 u8 *pCell,
155a8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
155a9 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c nter to the cell
155aa 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c text. */. Cell
155ab 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 Info *pInfo
155ac 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 /* Fill in t
155ad 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
155ae 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20 .){. u16 n;
155af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
155b0 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e Number bytes in
155b1 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 cell content he
155b2 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 ader */. u32 nP
155b3 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 ayload;
155b4 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
155b5 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 ytes of cell pay
155b6 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 load */.. asser
155b7 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
155b8 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
155b9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 ->mutex) );.. p
155ba 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 Info->pCell = pC
155bb 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ell;. assert( p
155bc 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c Page->leaf==0 ||
155bd 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 pPage->leaf==1
155be 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e );. n = pPage->
155bf 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 childPtrSize;.
155c0 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 assert( n==4-4*p
155c1 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
155c2 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
155c3 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 y ){. if( pPa
155c4 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 ge->hasData ){.
155c5 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 n += getVar
155c6 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c int32(&pCell[n],
155c7 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 nPayload);.
155c8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 }else{. nPa
155c9 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d yload = 0;. }
155ca 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 . n += getVar
155cb 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 int(&pCell[n], (
155cc 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 u64*)&pInfo->nKe
155cd 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e y);. pInfo->n
155ce 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b Data = nPayload;
155cf 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
155d0 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a nfo->nData = 0;.
155d1 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 n += getVari
155d2 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 nt32(&pCell[n],
155d3 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 nPayload);. p
155d4 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 Info->nKey = nPa
155d5 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e yload;. }. pIn
155d6 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e fo->nPayload = n
155d7 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f Payload;. pInfo
155d8 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 ->nHeader = n;.
155d9 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c testcase( nPayl
155da 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c oad==pPage->maxL
155db 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 ocal );. testca
155dc 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 se( nPayload==pP
155dd 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 age->maxLocal+1
155de 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 );. if( likely(
155df 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d nPayload<=pPage-
155e0 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 >maxLocal) ){.
155e1 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 /* This is the
155e2 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 (easy) common c
155e3 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e ase where the en
155e4 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 tire payload fit
155e5 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 s. ** on the
155e6 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 local page. No
155e7 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 overflow is requ
155e8 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ired.. */.
155e9 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20 int nSize;
155ea 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 /* Total si
155eb 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 ze of cell conte
155ec 6e 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 nt in bytes */.
155ed 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c nSize = nPayl
155ee 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e oad + n;. pIn
155ef 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
155f0 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 6)nPayload;.
155f1 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 pInfo->iOverflow
155f2 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e = 0;. if( (n
155f3 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b Size & ~3)==0 ){
155f4 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 . nSize = 4
155f5 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 ; /* Mini
155f6 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 mum cell size is
155f7 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 4 */. }.
155f8 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 pInfo->nSize = (
155f9 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c u16)nSize;. }el
155fa 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 se{. /* If th
155fb 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e e payload will n
155fc 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c ot fit completel
155fd 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 y on the local p
155fe 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 age, we have.
155ff 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f ** to decide ho
15600 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 w much to store
15601 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 locally and how
15602 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e much to spill on
15603 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c to. ** overfl
15604 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 ow pages. The s
15605 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 trategy is to mi
15606 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e nimize the amoun
15607 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 t of unused.
15608 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 ** space on over
15609 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 flow pages while
1560a 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f keeping the amo
1560b 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f unt of local sto
1560c 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 rage. ** in b
1560d 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 etween minLocal
1560e 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 and maxLocal..
1560f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e **. ** Warn
15610 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 ing: changing t
15611 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 he way overflow
15612 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 payload is distr
15613 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 ibuted in any.
15614 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 ** way will re
15615 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d sult in an incom
15616 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 patible file for
15617 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 mat.. */.
15618 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f int minLocal; /
15619 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Minimum amount
1561a 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
1561b 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 locally */.
1561c 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f int maxLocal; /
1561d 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 * Maximum amount
1561e 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 of payload held
1561f 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 locally */.
15620 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f int surplus; /
15621 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f * Overflow paylo
15622 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 ad available for
15623 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a local storage *
15624 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 /.. minLocal
15625 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 = pPage->minLoca
15626 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 l;. maxLocal
15627 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 = pPage->maxLoca
15628 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d l;. surplus =
15629 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 minLocal + (nPa
1562a 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c yload - minLocal
1562b 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 )%(pPage->pBt->u
1562c 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a sableSize - 4);.
1562d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75 testcase( su
1562e 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 rplus==maxLocal
1562f 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
15630 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 surplus==maxLoc
15631 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
15632 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f surplus <= maxLo
15633 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e cal ){. pIn
15634 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 fo->nLocal = (u1
15635 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 6)surplus;. }
15636 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 else{. pInf
15637 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 o->nLocal = (u16
15638 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d )minLocal;. }
15639 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 . pInfo->iOve
1563a 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49 rflow = (u16)(pI
1563b 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 nfo->nLocal + n)
1563c 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 ;. pInfo->nSi
1563d 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 ze = pInfo->iOve
1563e 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d rflow + 4;. }.}
1563f 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65 .#define parseCe
15640 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c ll(pPage, iCell,
15641 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65 pInfo) \. btre
15642 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 eParseCellPtr((p
15643 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 Page), findCell(
15644 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 (pPage), (iCell)
15645 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74 ), (pInfo)).stat
15646 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 ic void btreePar
15647 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 seCell(. MemPag
15648 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 e *pPage,
15649 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 /* Page contai
1564a 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f ning the cell */
1564b 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 . int iCell,
1564c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1564d 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 e cell index. F
1564e 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a irst cell is 0 *
1564f 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 /. CellInfo *pI
15650 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 nfo /* F
15651 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 ill in this stru
15652 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 cture */.){. pa
15653 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 rseCell(pPage, i
15654 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a Cell, pInfo);.}.
15655 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
15656 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 he total number
15657 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 of bytes that a
15658 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 Cell needs in th
15659 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 e cell.** data a
1565a 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 rea of the btree
1565b 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 -page. The retu
1565c 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 rn number includ
1565d 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 es the cell.** d
1565e 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 ata header and t
1565f 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 he local payload
15660 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 , but not any ov
15661 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a erflow page or.*
15662 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 * the space used
15663 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 by the cell poi
15664 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 nter..*/.static
15665 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 u16 cellSizePtr(
15666 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
15667 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 u8 *pCell){. u8
15668 20 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c *pIter = &pCell
15669 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 [pPage->childPtr
1566a 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 Size];. u32 nSi
1566b 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ze;..#ifdef SQLI
1566c 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 TE_DEBUG. /* Th
1566d 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
1566e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
1566f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 n should always
15670 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20 be the same as.
15671 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 ** the (CellInf
15672 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66 o.nSize) value f
15673 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20 ound by doing a
15674 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68 full parse of th
15675 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 e. ** cell. If
15676 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 SQLITE_DEBUG is
15677 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 defined, an asse
15678 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74 rt() at the bott
15679 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 om of. ** this
1567a 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65 function verifie
1567b 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61 s that this inva
1567c 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f riant is not vio
1567d 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c lated. */. Cell
1567e 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a Info debuginfo;.
1567f 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
15680 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
15681 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23 , &debuginfo);.#
15682 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61 endif.. if( pPa
15683 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 ge->intKey ){.
15684 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20 u8 *pEnd;.
15685 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 if( pPage->hasDa
15686 74 61 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65 ta ){. pIte
15687 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 r += getVarint32
15688 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a (pIter, nSize);.
15689 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1568a 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 nSize = 0;.
1568b 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20 }.. /* pIter
1568c 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68 now points at th
1568d 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 e 64-bit integer
1568e 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61 key value, a va
1568f 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20 riable length .
15690 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54 ** integer. T
15691 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
15692 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74 ck moves pIter t
15693 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66 o point at the f
15694 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a irst byte. **
15695 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
15696 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 the key value.
15697 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70 */. pEnd = &p
15698 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69 Iter[9];. whi
15699 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30 le( (*pIter++)&0
1569a 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e x80 && pIter<pEn
1569b 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 d );. }else{.
1569c 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 pIter += getVa
1569d 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 rint32(pIter, nS
1569e 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73 ize);. }.. tes
1569f 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 tcase( nSize==pP
156a0 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b age->maxLocal );
156a1 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 . testcase( nSi
156a2 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f ze==pPage->maxLo
156a3 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e cal+1 );. if( n
156a4 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c Size>pPage->maxL
156a5 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 ocal ){. int
156a6 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 minLocal = pPage
156a7 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 ->minLocal;.
156a8 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c nSize = minLocal
156a9 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c + (nSize - minL
156aa 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e ocal) % (pPage->
156ab 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
156ac 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 - 4);. testca
156ad 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 se( nSize==pPage
156ae 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 ->maxLocal );.
156af 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a testcase( nSiz
156b0 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 e==pPage->maxLoc
156b1 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 al+1 );. if(
156b2 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 nSize>pPage->max
156b3 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e Local ){. n
156b4 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b Size = minLocal;
156b5 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65 . }. nSize
156b6 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69 += 4;. }. nSi
156b7 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65 ze += (u32)(pIte
156b8 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f r - pCell);.. /
156b9 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 * The minimum si
156ba 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69 ze of any cell i
156bb 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 s 4 bytes. */.
156bc 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 if( nSize<4 ){.
156bd 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 nSize = 4;.
156be 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 }.. assert( nSi
156bf 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 ze==debuginfo.nS
156c0 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ize );. return
156c1 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 23 69 (u16)nSize;.}.#i
156c2 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 fndef NDEBUG.sta
156c3 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 tic u16 cellSize
156c4 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
156c5 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 72 int iCell){. r
156c6 65 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 74 eturn cellSizePt
156c7 72 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65 6c r(pPage, findCel
156c8 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 29 l(pPage, iCell))
156c9 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ;.}.#endif..#ifn
156ca 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
156cb 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a AUTOVACUUM./*.**
156cc 20 49 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65 If the cell pCe
156cd 6c 6c 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65 ll, part of page
156ce 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 pPage contains
156cf 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 a pointer.** to
156d0 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
156d1 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72 , insert an entr
156d2 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 y into the point
156d3 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68 er-map.** for th
156d4 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e e overflow page.
156d5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
156d6 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 ptrmapPutOvflPtr
156d7 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
156d8 20 75 38 20 2a 70 43 65 6c 6c 2c 20 69 6e 74 20 u8 *pCell, int
156d9 2a 70 52 43 29 7b 0a 20 20 43 65 6c 6c 49 6e 66 *pRC){. CellInf
156da 6f 20 69 6e 66 6f 3b 0a 20 20 69 66 28 20 2a 70 o info;. if( *p
156db 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 RC ) return;. a
156dc 73 73 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20 ssert( pCell!=0
156dd 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 );. btreeParseC
156de 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 ellPtr(pPage, pC
156df 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61 ell, &info);. a
156e0 73 73 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61 ssert( (info.nDa
156e1 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 ta+(pPage->intKe
156e2 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d y?0:info.nKey))=
156e3 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 =info.nPayload )
156e4 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 ;. if( info.iOv
156e5 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 50 67 erflow ){. Pg
156e6 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79 no ovfl = get4by
156e7 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 te(&pCell[info.i
156e8 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 Overflow]);.
156e9 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d ptrmapPut(pPage-
156ea 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d >pBt, ovfl, PTRM
156eb 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50 AP_OVERFLOW1, pP
156ec 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 52 43 29 3b age->pgno, pRC);
156ed 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a . }.}.#endif...
156ee 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74 /*.** Defragment
156ef 20 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e the page given.
156f0 20 20 41 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20 All Cells are
156f1 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 moved to the.**
156f2 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 end of the page
156f3 61 6e 64 20 61 6c 6c 20 66 72 65 65 20 73 70 61 and all free spa
156f4 63 65 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 ce is collected
156f5 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20 into one.** big
156f6 46 72 65 65 42 6c 6b 20 74 68 61 74 20 6f 63 63 FreeBlk that occ
156f7 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 20 74 urs in between t
156f8 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 63 65 he header and ce
156f9 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72 ll.** pointer ar
156fa 72 61 79 20 61 6e 64 20 74 68 65 20 63 65 6c 6c ray and the cell
156fb 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a content area..*
156fc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 66 /.static int def
156fd 72 61 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50 ragmentPage(MemP
156fe 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 age *pPage){. i
156ff 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
15700 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
15701 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 p counter */. i
15702 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 nt pc;
15703 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 /* Add
15704 72 65 73 73 20 6f 66 20 61 20 69 2d 74 68 20 63 ress of a i-th c
15705 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 ell */. int hdr
15706 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
15707 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f /* Offset to
15708 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
15709 20 2a 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20 */. int size;
1570a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1570b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 /* Size of a ce
1570c 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 ll */. int usab
1570d 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 leSize;
1570e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1570f 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20 usable bytes on
15710 61 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 a page */. int
15711 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 cellOffset;
15712 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
15713 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 to the cell poi
15714 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 nter array */.
15715 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20 int cbrk;
15716 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
15717 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c fset to the cell
15718 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f content area */
15719 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 . int nCell;
1571a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1571b 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 Number of cells
1571c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a on the page */.
1571d 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
1571e 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 *data; /*
1571f 54 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f The page data */
15720 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
15721 20 2a 74 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a *temp; /*
15722 20 54 65 6d 70 20 61 72 65 61 20 66 6f 72 20 63 Temp area for c
15723 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 ell content */.
15724 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b int iCellFirst;
15725 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
15726 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63 irst allowable c
15727 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 ell index */. i
15728 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 nt iCellLast;
15729 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
1572a 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20 t possible cell
1572b 69 6e 64 65 78 20 2a 2f 0a 0a 0a 20 20 61 73 73 index */... ass
1572c 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
1572d 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
1572e 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
1572f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15730 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 >pBt!=0 );. ass
15731 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d ert( pPage->pBt-
15732 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53 >usableSize <= S
15733 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 QLITE_MAX_PAGE_S
15734 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 IZE );. assert(
15735 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
15736 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 w==0 );. assert
15737 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
15738 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
15739 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d >mutex) );. tem
1573a 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 p = sqlite3Pager
1573b 54 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d TempSpace(pPage-
1573c 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 >pBt->pPager);.
1573d 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
1573e 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 Data;. hdr = pP
1573f 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a age->hdrOffset;.
15740 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70 cellOffset = p
15741 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 Page->cellOffset
15742 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 ;. nCell = pPag
15743 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65 e->nCell;. asse
15744 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62 rt( nCell==get2b
15745 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
15746 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a ) );. usableSiz
15747 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e e = pPage->pBt->
15748 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62 usableSize;. cb
15749 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 rk = get2byte(&d
1574a 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d ata[hdr+5]);. m
1574b 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b emcpy(&temp[cbrk
1574c 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 ], &data[cbrk],
1574d 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72 usableSize - cbr
1574e 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61 k);. cbrk = usa
1574f 62 6c 65 53 69 7a 65 3b 0a 20 20 69 43 65 6c 6c bleSize;. iCell
15750 46 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 First = cellOffs
15751 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20 et + 2*nCell;.
15752 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 iCellLast = usab
15753 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f leSize - 4;. fo
15754 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
15755 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41 i++){. u8 *pA
15756 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 ddr; /* The
15757 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65 i-th cell pointe
15758 72 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d r */. pAddr =
15759 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 &data[cellOffse
1575a 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63 t + i*2];. pc
1575b 20 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64 = get2byte(pAdd
1575c 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 r);. testcase
1575d 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 ( pc==iCellFirst
1575e 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
1575f 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 ( pc==iCellLast
15760 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 );.#if !defined(
15761 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 SQLITE_ENABLE_OV
15762 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 ERSIZE_CELL_CHEC
15763 4b 29 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20 K). /* These
15764 63 6f 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20 conditions have
15765 61 6c 72 65 61 64 79 20 62 65 65 6e 20 76 65 72 already been ver
15766 69 66 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e ified in btreeIn
15767 69 74 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20 itPage(). **
15768 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 if SQLITE_ENABLE
15769 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 _OVERSIZE_CELL_C
1576a 48 45 43 4b 20 69 73 20 64 65 66 69 6e 65 64 20 HECK is defined
1576b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1576c 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c pc<iCellFirst ||
1576d 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b pc>iCellLast ){
1576e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1576f 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
15770 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a T;. }.#endif.
15771 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d assert( pc>=
15772 69 43 65 6c 6c 46 69 72 73 74 20 26 26 20 70 63 iCellFirst && pc
15773 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 <=iCellLast );.
15774 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 size = cellSi
15775 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 74 65 zePtr(pPage, &te
15776 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 72 mp[pc]);. cbr
15777 6b 20 2d 3d 20 73 69 7a 65 3b 0a 23 69 66 20 64 k -= size;.#if d
15778 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
15779 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 ABLE_OVERSIZE_CE
1577a 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 69 66 LL_CHECK). if
1577b 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 ( cbrk<iCellFirs
1577c 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 t ){. retur
1577d 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1577e 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c _BKPT;. }.#el
1577f 73 65 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c se. if( cbrk<
15780 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 iCellFirst || pc
15781 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 +size>usableSize
15782 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
15783 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15784 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 BKPT;. }.#end
15785 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 if. assert( c
15786 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 brk+size<=usable
15787 53 69 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43 Size && cbrk>=iC
15788 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 ellFirst );.
15789 74 65 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73 testcase( cbrk+s
1578a 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 ize==usableSize
1578b 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
1578c 20 70 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 pc+size==usable
1578d 53 69 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 63 Size );. memc
1578e 70 79 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 py(&data[cbrk],
1578f 26 74 65 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29 &temp[pc], size)
15790 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70 ;. put2byte(p
15791 41 64 64 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d Addr, cbrk);. }
15792 0a 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e . assert( cbrk>
15793 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 =iCellFirst );.
15794 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
15795 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20 hdr+5], cbrk);.
15796 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30 data[hdr+1] = 0
15797 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20 ;. data[hdr+2]
15798 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b = 0;. data[hdr+
15799 37 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 7] = 0;. memset
1579a 28 26 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 73 (&data[iCellFirs
1579b 74 5d 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 6c t], 0, cbrk-iCel
1579c 6c 46 69 72 73 74 29 3b 0a 20 20 61 73 73 65 72 lFirst);. asser
1579d 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
1579e 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
1579f 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
157a0 69 66 28 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69 if( cbrk-iCellFi
157a1 72 73 74 21 3d 70 50 61 67 65 2d 3e 6e 46 72 65 rst!=pPage->nFre
157a2 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
157a3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
157a4 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 KPT;. }. retur
157a5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
157a6 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e /*.** Allocate n
157a7 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 Byte bytes of sp
157a8 61 63 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 ace from within
157a9 74 68 65 20 42 2d 54 72 65 65 20 70 61 67 65 20 the B-Tree page
157aa 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 passed.** as the
157ab 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
157ac 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 49 64 Write into *pId
157ad 78 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f x the index into
157ae 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 0a pPage->aData[].
157af 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 ** of the first
157b0 62 79 74 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 byte of allocate
157b1 64 20 73 70 61 63 65 2e 20 52 65 74 75 72 6e 20 d space. Return
157b2 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b either SQLITE_OK
157b3 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 or.** an error
157b4 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 20 53 51 code (usually SQ
157b5 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a LITE_CORRUPT)..*
157b6 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 *.** The caller
157b7 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 guarantees that
157b8 74 68 65 72 65 20 69 73 20 73 75 66 66 69 63 69 there is suffici
157b9 65 6e 74 20 73 70 61 63 65 20 74 6f 20 6d 61 6b ent space to mak
157ba 65 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 e the.** allocat
157bb 69 6f 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ion. This routi
157bc 6e 65 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f ne might need to
157bd 20 64 65 66 72 61 67 6d 65 6e 74 20 69 6e 20 6f defragment in o
157be 72 64 65 72 20 74 6f 20 62 72 69 6e 67 0a 2a 2a rder to bring.**
157bf 20 61 6c 6c 20 74 68 65 20 73 70 61 63 65 20 74 all the space t
157c0 6f 67 65 74 68 65 72 2c 20 68 6f 77 65 76 65 72 ogether, however
157c1 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
157c2 77 69 6c 6c 20 61 76 6f 69 64 20 75 73 69 6e 67 will avoid using
157c3 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77 .** the first tw
157c4 6f 20 62 79 74 65 73 20 70 61 73 74 20 74 68 65 o bytes past the
157c5 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 cell pointer ar
157c6 65 61 20 73 69 6e 63 65 20 70 72 65 73 75 6d 61 ea since presuma
157c7 62 6c 79 20 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f bly this.** allo
157c8 63 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 cation is being
157c9 6d 61 64 65 20 69 6e 20 6f 72 64 65 72 20 74 6f made in order to
157ca 20 69 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 insert a new ce
157cb 6c 6c 2c 20 73 6f 20 77 65 20 77 69 6c 6c 0a 2a ll, so we will.*
157cc 2a 20 61 6c 73 6f 20 65 6e 64 20 75 70 20 6e 65 * also end up ne
157cd 65 64 69 6e 67 20 61 20 6e 65 77 20 63 65 6c 6c eding a new cell
157ce 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 pointer..*/.sta
157cf 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 tic int allocate
157d0 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 Space(MemPage *p
157d1 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 2c Page, int nByte,
157d2 20 69 6e 74 20 2a 70 49 64 78 29 7b 0a 20 20 63 int *pIdx){. c
157d3 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 onst int hdr = p
157d4 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
157d5 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 /* Local cac
157d6 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 68 64 72 he of pPage->hdr
157d7 4f 66 66 73 65 74 20 2a 2f 0a 20 20 75 38 20 2a Offset */. u8 *
157d8 20 63 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50 const data = pP
157d9 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 age->aData;
157da 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 /* Local cache
157db 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 of pPage->aData
157dc 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b 20 */. int nFrag;
157dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
157df 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 ber of fragmente
157e0 64 20 62 79 74 65 73 20 6f 6e 20 70 50 61 67 65 d bytes on pPage
157e1 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20 */. int top;
157e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
157e3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
157e4 72 73 74 20 62 79 74 65 20 6f 66 20 63 65 6c 6c rst byte of cell
157e5 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f content area */
157e6 0a 20 20 69 6e 74 20 67 61 70 3b 20 20 20 20 20 . int gap;
157e7 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
157e8 20 6f 66 20 67 61 70 20 62 65 74 77 65 65 6e 20 of gap between
157e9 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 6e cell pointers an
157ea 64 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a d cell content *
157eb 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
157ec 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72 /* Integer r
157ed 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
157ee 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
157ef 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
157f0 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
157f1 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
157f2 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 pPage->pBt );.
157f3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
157f4 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
157f5 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
157f6 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 . assert( nByte
157f7 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d >=0 ); /* Minim
157f8 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 um cell size is
157f9 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4 */. assert( p
157fa 50 61 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 Page->nFree>=nBy
157fb 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 te );. assert(
157fc 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
157fd 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
157fe 20 6e 42 79 74 65 3c 70 50 61 67 65 2d 3e 70 42 nByte<pPage->pB
157ff 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 38 20 t->usableSize-8
15800 29 3b 0a 0a 20 20 6e 46 72 61 67 20 3d 20 64 61 );.. nFrag = da
15801 74 61 5b 68 64 72 2b 37 5d 3b 0a 20 20 61 73 73 ta[hdr+7];. ass
15802 65 72 74 28 20 70 50 61 67 65 2d 3e 63 65 6c 6c ert( pPage->cell
15803 4f 66 66 73 65 74 20 3d 3d 20 68 64 72 20 2b 20 Offset == hdr +
15804 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 12 - 4*pPage->le
15805 61 66 20 29 3b 0a 20 20 67 61 70 20 3d 20 70 50 af );. gap = pP
15806 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
15807 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c + 2*pPage->nCell
15808 3b 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 ;. top = get2by
15809 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 te(&data[hdr+5])
1580a 3b 0a 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20 ;. if( gap>top
1580b 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
1580c 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1580d 74 65 73 74 63 61 73 65 28 20 67 61 70 2b 32 3d testcase( gap+2=
1580e 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 =top );. testca
1580f 73 65 28 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29 se( gap+1==top )
15810 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 ;. testcase( ga
15811 70 3d 3d 74 6f 70 20 29 3b 0a 0a 20 20 69 66 28 p==top );.. if(
15812 20 6e 46 72 61 67 3e 3d 36 30 20 29 7b 0a 20 20 nFrag>=60 ){.
15813 20 20 2f 2a 20 41 6c 77 61 79 73 20 64 65 66 72 /* Always defr
15814 61 67 6d 65 6e 74 20 68 69 67 68 6c 79 20 66 72 agment highly fr
15815 61 67 6d 65 6e 74 65 64 20 70 61 67 65 73 20 2a agmented pages *
15816 2f 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 /. rc = defra
15817 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 gmentPage(pPage)
15818 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
15819 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f eturn rc;. to
1581a 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 p = get2byte(&da
1581b 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 65 ta[hdr+5]);. }e
1581c 6c 73 65 20 69 66 28 20 67 61 70 2b 32 3c 3d 74 lse if( gap+2<=t
1581d 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61 op ){. /* Sea
1581e 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 73 74 rch the freelist
1581f 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66 looking for a f
15820 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f ree slot big eno
15821 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a ugh to satisfy .
15822 20 20 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65 ** the reque
15823 73 74 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 st. The allocati
15824 6f 6e 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20 on is made from
15825 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 73 the first free s
15826 6c 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74 lot in . ** t
15827 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 73 20 he list that is
15828 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 large enough to
15829 61 63 63 6f 6d 61 64 61 74 65 20 69 74 2e 0a 20 accomadate it..
1582a 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63 */. int pc
1582b 2c 20 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28 , addr;. for(
1582c 61 64 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20 addr=hdr+1; (pc
1582d 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
1582e 5b 61 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72 [addr]))>0; addr
1582f 3d 70 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 =pc){. int
15830 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 size = get2byte(
15831 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 20 20 20 &data[pc+2]);
15832 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66 72 65 /* Size of fre
15833 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 20 20 e slot */.
15834 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20 if( size>=nByte
15835 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 ){. int x
15836 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b = size - nByte;
15837 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
15838 65 28 20 78 3d 3d 34 20 29 3b 0a 20 20 20 20 20 e( x==4 );.
15839 20 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d testcase( x==
1583a 33 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 3 );. if(
1583b 20 78 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 x<4 ){.
1583c 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 /* Remove the
1583d 73 6c 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72 slot from the fr
1583e 65 65 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20 ee-list. Update
1583f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 the number of.
15840 20 20 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d ** fragm
15841 65 6e 74 65 64 20 62 79 74 65 73 20 77 69 74 68 ented bytes with
15842 69 6e 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a in the page. */.
15843 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
15844 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64 (&data[addr], &d
15845 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20 ata[pc], 2);.
15846 20 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b data[hdr+
15847 37 5d 20 3d 20 28 75 38 29 28 6e 46 72 61 67 20 7] = (u8)(nFrag
15848 2b 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 + x);. }e
15849 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
1584a 2a 20 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69 * The slot remai
1584b 6e 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c ns on the free-l
1584c 69 73 74 2e 20 52 65 64 75 63 65 20 69 74 73 20 ist. Reduce its
1584d 73 69 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a size to account.
1584e 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 ** for
1584f 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65 the portion use
15850 64 20 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c d by the new all
15851 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 ocation. */.
15852 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 put2byte(&
15853 64 61 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a data[pc+2], x);.
15854 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15855 20 20 2a 70 49 64 78 20 3d 20 70 63 20 2b 20 78 *pIdx = pc + x
15856 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
15857 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
15858 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 }. }. }..
15859 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b /* Check to mak
1585a 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20 e sure there is
1585b 65 6e 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20 enough space in
1585c 74 68 65 20 67 61 70 20 74 6f 20 73 61 74 69 73 the gap to satis
1585d 66 79 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 6f fy. ** the allo
1585e 63 61 74 69 6f 6e 2e 20 20 49 66 20 6e 6f 74 2c cation. If not,
1585f 20 64 65 66 72 61 67 6d 65 6e 74 2e 0a 20 20 2a defragment.. *
15860 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 /. testcase( ga
15861 70 2b 32 2b 6e 42 79 74 65 3d 3d 74 6f 70 20 29 p+2+nByte==top )
15862 3b 0a 20 20 69 66 28 20 67 61 70 2b 32 2b 6e 42 ;. if( gap+2+nB
15863 79 74 65 3e 74 6f 70 20 29 7b 0a 20 20 20 20 72 yte>top ){. r
15864 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 c = defragmentPa
15865 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 ge(pPage);. i
15866 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
15867 63 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 c;. top = get
15868 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
15869 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 5]);. assert(
1586a 20 67 61 70 2b 6e 42 79 74 65 3c 3d 74 6f 70 20 gap+nByte<=top
1586b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 6c );. }... /* Al
1586c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72 locate memory fr
1586d 6f 6d 20 74 68 65 20 67 61 70 20 69 6e 20 62 65 om the gap in be
1586e 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70 tween the cell p
1586f 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a ointer array. *
15870 2a 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 * and the cell c
15871 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68 ontent area. Th
15872 65 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 e btreeInitPage(
15873 29 20 63 61 6c 6c 20 68 61 73 20 61 6c 72 65 61 ) call has alrea
15874 64 79 0a 20 20 2a 2a 20 76 61 6c 69 64 61 74 65 dy. ** validate
15875 64 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 d the freelist.
15876 20 47 69 76 65 6e 20 74 68 61 74 20 74 68 65 20 Given that the
15877 66 72 65 65 6c 69 73 74 20 69 73 20 76 61 6c 69 freelist is vali
15878 64 2c 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 d, there. ** is
15879 20 6e 6f 20 77 61 79 20 74 68 61 74 20 74 68 65 no way that the
1587a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 20 allocation can
1587b 65 78 74 65 6e 64 20 6f 66 66 20 74 68 65 20 65 extend off the e
1587c 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a nd of the page..
1587d 20 20 2a 2a 20 54 68 65 20 61 73 73 65 72 74 28 ** The assert(
1587e 29 20 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73 ) below verifies
1587f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 the previous se
15880 6e 74 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 74 ntence.. */. t
15881 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 70 op -= nByte;. p
15882 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
15883 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 73 r+5], top);. as
15884 73 65 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 20 sert( top+nByte
15885 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 <= pPage->pBt->u
15886 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 2a sableSize );. *
15887 70 49 64 78 20 3d 20 74 6f 70 3b 0a 20 20 72 65 pIdx = top;. re
15888 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
15889 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1588a 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 a section of the
1588b 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f pPage->aData to
1588c 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a the freelist..*
1588d 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65 * The first byte
1588e 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65 of the new free
1588f 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d block is pPage-
15890 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a >aDisk[start].**
15891 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 and the size of
15892 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73 the block is "s
15893 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a ize" bytes..**.*
15894 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66 * Most of the ef
15895 66 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76 fort here is inv
15896 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69 olved in coalesi
15897 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66 ng adjacent.** f
15898 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20 ree blocks into
15899 61 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65 a single big fre
1589a 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 e block..*/.stat
1589b 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65 ic int freeSpace
1589c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
1589d 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20 int start, int
1589e 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 size){. int add
1589f 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a r, pbegin, hdr;.
158a0 20 20 69 6e 74 20 69 4c 61 73 74 3b 20 20 20 20 int iLast;
158a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
158a2 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 /* Largest p
158a3 6f 73 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63 ossible freebloc
158a4 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 6e k offset */. un
158a5 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 signed char *dat
158a6 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
158a7 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ;.. assert( pPa
158a8 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 ge->pBt!=0 );.
158a9 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
158aa 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
158ab 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
158ac 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 );. assert( sta
158ad 72 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66 rt>=pPage->hdrOf
158ae 66 73 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68 fset+6+pPage->ch
158af 69 6c 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20 ildPtrSize );.
158b0 61 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b assert( (start +
158b1 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70 size)<=pPage->p
158b2 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
158b3 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
158b4 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
158b5 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
158b6 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
158b7 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d ize>=0 ); /* M
158b8 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 inimum cell size
158b9 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66 is 4 */..#ifdef
158ba 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
158bb 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77 ELETE. /* Overw
158bc 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 rite deleted inf
158bd 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 ormation with ze
158be 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53 45 43 ros when the SEC
158bf 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a URE_DELETE . **
158c0 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c option is enabl
158c1 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 ed at compile-ti
158c2 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 me */. memset(&
158c3 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20 data[start], 0,
158c4 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 size);.#endif..
158c5 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61 63 /* Add the spac
158c6 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 e back into the
158c7 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66 linked list of f
158c8 72 65 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74 65 reeblocks. Note
158c9 20 74 68 61 74 0a 20 20 2a 2a 20 65 76 65 6e 20 that. ** even
158ca 74 68 6f 75 67 68 20 74 68 65 20 66 72 65 65 62 though the freeb
158cb 6c 6f 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68 lock list was ch
158cc 65 63 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e ecked by btreeIn
158cd 69 74 50 61 67 65 28 29 2c 0a 20 20 2a 2a 20 62 itPage(),. ** b
158ce 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 64 treeInitPage() d
158cf 69 64 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76 id not detect ov
158d0 65 72 6c 61 70 70 69 6e 67 20 63 65 6c 6c 73 20 erlapping cells
158d1 6f 72 0a 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63 or. ** freebloc
158d2 6b 73 20 74 68 61 74 20 6f 76 65 72 6c 61 70 70 ks that overlapp
158d3 65 64 20 63 65 6c 6c 73 2e 20 20 20 4e 6f 72 20 ed cells. Nor
158d4 64 6f 65 73 20 69 74 20 64 65 74 65 63 74 20 77 does it detect w
158d5 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c hen the. ** cel
158d6 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 65 l content area e
158d7 78 63 65 65 64 73 20 74 68 65 20 76 61 6c 75 65 xceeds the value
158d8 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 in the page hea
158d9 64 65 72 2e 20 20 49 66 20 74 68 65 73 65 0a 20 der. If these.
158da 20 2a 2a 20 73 69 74 75 61 74 69 6f 6e 73 20 61 ** situations a
158db 72 69 73 65 2c 20 74 68 65 6e 20 73 75 62 73 65 rise, then subse
158dc 71 75 65 6e 74 20 69 6e 73 65 72 74 20 6f 70 65 quent insert ope
158dd 72 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 63 6f rations might co
158de 72 72 75 70 74 0a 20 20 2a 2a 20 74 68 65 20 66 rrupt. ** the f
158df 72 65 65 6c 69 73 74 2e 20 20 53 6f 20 77 65 20 reelist. So we
158e0 64 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b do need to check
158e1 20 66 6f 72 20 63 6f 72 72 75 70 74 69 6f 6e 20 for corruption
158e2 77 68 69 6c 65 20 73 63 61 6e 6e 69 6e 67 0a 20 while scanning.
158e3 20 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 ** the freelist
158e4 2e 0a 20 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70 .. */. hdr = p
158e5 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b Page->hdrOffset;
158e6 0a 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 . addr = hdr +
158e7 31 3b 0a 20 20 69 4c 61 73 74 20 3d 20 70 50 61 1;. iLast = pPa
158e8 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
158e9 69 7a 65 20 2d 20 34 3b 0a 20 20 61 73 73 65 72 ize - 4;. asser
158ea 74 28 20 73 74 61 72 74 3c 3d 69 4c 61 73 74 20 t( start<=iLast
158eb 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 );. while( (pbe
158ec 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 gin = get2byte(&
158ed 64 61 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 data[addr]))<sta
158ee 72 74 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 rt && pbegin>0 )
158ef 7b 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e {. if( pbegin
158f0 3c 61 64 64 72 2b 34 20 29 7b 0a 20 20 20 20 20 <addr+4 ){.
158f1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
158f2 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
158f3 20 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 }. addr = pb
158f4 65 67 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 egin;. }. if(
158f5 70 62 65 67 69 6e 3e 69 4c 61 73 74 20 29 7b 0a pbegin>iLast ){.
158f6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
158f7 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
158f8 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62 }. assert( pb
158f9 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65 egin>addr || pbe
158fa 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32 gin==0 );. put2
158fb 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d byte(&data[addr]
158fc 2c 20 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32 , start);. put2
158fd 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74 byte(&data[start
158fe 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75 ], pbegin);. pu
158ff 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 t2byte(&data[sta
15900 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 rt+2], size);.
15901 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70 pPage->nFree = p
15902 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 20 28 75 Page->nFree + (u
15903 31 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43 16)size;.. /* C
15904 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74 oalesce adjacent
15905 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a free blocks */.
15906 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 addr = hdr + 1
15907 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 ;. while( (pbeg
15908 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 in = get2byte(&d
15909 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b ata[addr]))>0 ){
1590a 0a 20 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 . int pnext,
1590b 70 73 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73 psize, x;. as
1590c 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 sert( pbegin>add
1590d 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 r );. assert(
1590e 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e pbegin<=pPage->
1590f 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d pBt->usableSize-
15910 34 20 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d 4 );. pnext =
15911 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15912 70 62 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73 pbegin]);. ps
15913 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 ize = get2byte(&
15914 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b data[pbegin+2]);
15915 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 20 . if( pbegin
15916 2b 20 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70 + psize + 3 >= p
15917 6e 65 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20 next && pnext>0
15918 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61 ){. int fra
15919 67 20 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65 g = pnext - (pbe
1591a 67 69 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20 gin+psize);.
1591b 20 20 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c if( (frag<0) |
1591c 7c 20 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74 | (frag>(int)dat
1591d 61 5b 68 64 72 2b 37 5d 29 20 29 7b 0a 20 20 20 a[hdr+7]) ){.
1591e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1591f 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
15920 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
15921 61 74 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 28 75 ata[hdr+7] -= (u
15922 38 29 66 72 61 67 3b 0a 20 20 20 20 20 20 78 20 8)frag;. x
15923 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
15924 5b 70 6e 65 78 74 5d 29 3b 0a 20 20 20 20 20 20 [pnext]);.
15925 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
15926 62 65 67 69 6e 5d 2c 20 78 29 3b 0a 20 20 20 20 begin], x);.
15927 20 20 78 20 3d 20 70 6e 65 78 74 20 2b 20 67 65 x = pnext + ge
15928 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65 t2byte(&data[pne
15929 78 74 2b 32 5d 29 20 2d 20 70 62 65 67 69 6e 3b xt+2]) - pbegin;
1592a 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 . put2byte(
1592b 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 2c &data[pbegin+2],
1592c 20 78 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a x);. }else{.
1592d 20 20 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 addr = pbe
1592e 67 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a gin;. }. }..
1592f 20 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c /* If the cell
15930 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 62 65 content area be
15931 67 69 6e 73 20 77 69 74 68 20 61 20 66 72 65 65 gins with a free
15932 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74 block, remove it
15933 2e 20 2a 2f 0a 20 20 69 66 28 20 64 61 74 61 5b . */. if( data[
15934 68 64 72 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72 hdr+1]==data[hdr
15935 2b 35 5d 20 26 26 20 64 61 74 61 5b 68 64 72 2b +5] && data[hdr+
15936 32 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20 2]==data[hdr+6]
15937 29 7b 0a 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a ){. int top;.
15938 20 20 20 20 70 62 65 67 69 6e 20 3d 20 67 65 74 pbegin = get
15939 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
1593a 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 1]);. memcpy(
1593b 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 &data[hdr+1], &d
1593c 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b ata[pbegin], 2);
1593d 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 . top = get2b
1593e 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
1593f 29 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 ) + get2byte(&da
15940 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 ta[pbegin+2]);.
15941 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 put2byte(&dat
15942 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a a[hdr+5], top);.
15943 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 }. assert( sq
15944 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
15945 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
15946 50 61 67 65 29 20 29 3b 0a 20 20 72 65 74 75 72 Page) );. retur
15947 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
15948 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65 /*.** Decode the
15949 20 66 6c 61 67 73 20 62 79 74 65 20 28 74 68 65 flags byte (the
1594a 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 first byte of t
1594b 68 65 20 68 65 61 64 65 72 29 20 66 6f 72 20 61 he header) for a
1594c 20 70 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69 page.** and ini
1594d 74 69 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f tialize fields o
1594e 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74 f the MemPage st
1594f 72 75 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e ructure accordin
15950 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 gly..**.** Only
15951 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f the following co
15952 6d 62 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73 mbinations are s
15953 75 70 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68 upported. Anyth
15954 69 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a ing different.**
15955 20 69 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72 indicates a cor
15956 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
15957 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 les:.**.**
15958 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a PTF_ZERODATA.
15959 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a ** PTF_Z
1595a 45 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45 ERODATA | PTF_LE
1595b 41 46 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 AF.** PT
1595c 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 F_LEAFDATA | PTF
1595d 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20 _INTKEY.**
1595e 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 PTF_LEAFDATA
1595f 7c 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50 | PTF_INTKEY | P
15960 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69 TF_LEAF.*/.stati
15961 63 20 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67 c int decodeFlag
15962 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 s(MemPage *pPage
15963 2c 20 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b , int flagByte){
15964 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
15965 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 ; /* A copy
15966 6f 66 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f of pPage->pBt */
15967 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
15968 65 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70 e->hdrOffset==(p
15969 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 Page->pgno==1 ?
1596a 31 30 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73 100 : 0) );. as
1596b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1596c 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
1596d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1596e 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28 pPage->leaf = (
1596f 75 38 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29 u8)(flagByte>>3)
15970 3b 20 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c ; assert( PTF_L
15971 45 41 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20 EAF == 1<<3 );.
15972 20 66 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54 flagByte &= ~PT
15973 46 5f 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d F_LEAF;. pPage-
15974 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20 >childPtrSize =
15975 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 4-4*pPage->leaf;
15976 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e . pBt = pPage->
15977 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42 pBt;. if( flagB
15978 79 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41 yte==(PTF_LEAFDA
15979 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29 TA | PTF_INTKEY)
1597a 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 ){. pPage->i
1597b 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 70 ntKey = 1;. p
1597c 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 Page->hasData =
1597d 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 pPage->leaf;.
1597e 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c pPage->maxLocal
1597f 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b = pBt->maxLeaf;
15980 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c . pPage->minL
15981 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c ocal = pBt->minL
15982 65 61 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 eaf;. }else if(
15983 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f 5a flagByte==PTF_Z
15984 45 52 4f 44 41 54 41 20 29 7b 0a 20 20 20 20 70 ERODATA ){. p
15985 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 30 Page->intKey = 0
15986 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 ;. pPage->has
15987 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 50 Data = 0;. pP
15988 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 age->maxLocal =
15989 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 pBt->maxLocal;.
1598a 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 pPage->minLoc
1598b 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 al = pBt->minLoc
1598c 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 al;. }else{.
1598d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1598e 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d ORRUPT_BKPT;. }
1598f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15990 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e _OK;.}../*.** In
15991 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78 itialize the aux
15992 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 iliary informati
15993 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c on for a disk bl
15994 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ock..**.** Retur
15995 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 n SQLITE_OK on s
15996 75 63 63 65 73 73 2e 20 20 49 66 20 77 65 20 73 uccess. If we s
15997 65 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 ee that the page
15998 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e does.** not con
15999 74 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d tain a well-form
1599a 65 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ed database page
1599b 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a , then return .*
1599c 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 * SQLITE_CORRUPT
1599d 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72 . Note that a r
1599e 65 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f eturn of SQLITE_
1599f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67 OK does not.** g
159a0 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 uarantee that th
159a1 65 20 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66 e page is well-f
159a2 6f 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20 ormed. It only
159a3 73 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65 shows that.** we
159a4 20 66 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63 failed to detec
159a5 74 20 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e t any corruption
159a6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
159a7 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 btreeInitPage(Me
159a8 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 0a mPage *pPage){..
159a9 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
159aa 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 >pBt!=0 );. ass
159ab 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
159ac 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
159ad 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
159ae 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
159af 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 gno==sqlite3Page
159b0 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 rPagenumber(pPag
159b1 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
159b2 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 3d assert( pPage =
159b3 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
159b4 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 tExtra(pPage->pD
159b5 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
159b6 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61 rt( pPage->aData
159b7 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 == sqlite3Pager
159b8 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 GetData(pPage->p
159b9 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 69 66 DbPage) );.. if
159ba 28 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 ( !pPage->isInit
159bb 20 29 7b 0a 20 20 20 20 75 31 36 20 70 63 3b 20 ){. u16 pc;
159bc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 /* Ad
159bd 64 72 65 73 73 20 6f 66 20 61 20 66 72 65 65 62 dress of a freeb
159be 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67 lock within pPag
159bf 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 e->aData[] */.
159c0 20 20 75 38 20 68 64 72 3b 20 20 20 20 20 20 20 u8 hdr;
159c1 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 /* Offset t
159c2 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70 o beginning of p
159c3 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 age header */.
159c4 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 u8 *data;
159c5 20 20 20 20 20 2f 2a 20 45 71 75 61 6c 20 74 6f /* Equal to
159c6 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f pPage->aData */
159c7 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 . BtShared *p
159c8 42 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 Bt; /* Th
159c9 65 20 6d 61 69 6e 20 62 74 72 65 65 20 73 74 72 e main btree str
159ca 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 75 31 ucture */. u1
159cb 36 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 6 usableSize;
159cc 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73 /* Amount of us
159cd 61 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61 able space on ea
159ce 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 ch page */. u
159cf 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 16 cellOffset;
159d0 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d /* Offset from
159d1 20 73 74 61 72 74 20 6f 66 20 70 61 67 65 20 74 start of page t
159d2 6f 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 o first cell poi
159d3 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 nter */. u16
159d4 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 2f nFree; /
159d5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73 * Number of unus
159d6 65 64 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 ed bytes on the
159d7 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 31 36 20 page */. u16
159d8 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f top; /
159d9 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
159da 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 the cell content
159db 20 61 72 65 61 20 2a 2f 0a 20 20 20 20 69 6e 74 area */. int
159dc 20 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 iCellFirst;
159dd 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 /* First allowab
159de 6c 65 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 le cell or freeb
159df 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 lock offset */.
159e0 20 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74 int iCellLast
159e1 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f ; /* Last po
159e2 73 73 69 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66 ssible cell or f
159e3 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 reeblock offset
159e4 2a 2f 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50 */.. pBt = pP
159e5 61 67 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68 age->pBt;.. h
159e6 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
159e7 66 66 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20 ffset;. data
159e8 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a = pPage->aData;.
159e9 20 20 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c if( decodeFl
159ea 61 67 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b ags(pPage, data[
159eb 68 64 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53 hdr]) ) return S
159ec 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
159ed 50 54 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 PT;. assert(
159ee 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 pBt->pageSize>=5
159ef 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 12 && pBt->pageS
159f0 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 ize<=32768 );.
159f1 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 pPage->maskPag
159f2 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a e = pBt->pageSiz
159f3 65 20 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65 e - 1;. pPage
159f4 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b ->nOverflow = 0;
159f5 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 . usableSize
159f6 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
159f7 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65 e;. pPage->ce
159f8 6c 6c 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f llOffset = cellO
159f9 66 66 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32 ffset = hdr + 12
159fa 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 - 4*pPage->leaf
159fb 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 ;. top = get2
159fc 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
159fd 5d 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e ]);. pPage->n
159fe 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28 Cell = get2byte(
159ff 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 &data[hdr+3]);.
15a00 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 if( pPage->nC
15a01 65 6c 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29 ell>MX_CELL(pBt)
15a02 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20 ){. /* To
15a03 6d 61 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61 many cells for a
15a04 20 73 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54 single page. T
15a05 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 he page must be
15a06 63 6f 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20 corrupt */.
15a07 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15a08 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
15a09 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 }. testcase(
15a0a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d pPage->nCell==M
15a0b 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 3b 0a 0a X_CELL(pBt) );..
15a0c 20 20 20 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d /* A malform
15a0d 65 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ed database page
15a0e 20 6d 69 67 68 74 20 63 61 75 73 65 20 75 73 20 might cause us
15a0f 74 6f 20 72 65 61 64 20 70 61 73 74 20 74 68 65 to read past the
15a10 20 65 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 70 end. ** of p
15a11 61 67 65 20 77 68 65 6e 20 70 61 72 73 69 6e 67 age when parsing
15a12 20 61 20 63 65 6c 6c 2e 20 20 0a 20 20 20 20 2a a cell. . *
15a13 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c *. ** The fol
15a14 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 lowing block of
15a15 63 6f 64 65 20 63 68 65 63 6b 73 20 65 61 72 6c code checks earl
15a16 79 20 74 6f 20 73 65 65 20 69 66 20 61 20 63 65 y to see if a ce
15a17 6c 6c 20 65 78 74 65 6e 64 73 0a 20 20 20 20 2a ll extends. *
15a18 2a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f * past the end o
15a19 66 20 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72 f a page boundar
15a1a 79 20 61 6e 64 20 63 61 75 73 65 73 20 53 51 4c y and causes SQL
15a1b 49 54 45 5f 43 4f 52 52 55 50 54 20 74 6f 20 62 ITE_CORRUPT to b
15a1c 65 20 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e e . ** return
15a1d 65 64 20 69 66 20 69 74 20 64 6f 65 73 2e 0a 20 ed if it does..
15a1e 20 20 20 2a 2f 0a 20 20 20 20 69 43 65 6c 6c 46 */. iCellF
15a1f 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 irst = cellOffse
15a20 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 t + 2*pPage->nCe
15a21 6c 6c 3b 0a 20 20 20 20 69 43 65 6c 6c 4c 61 73 ll;. iCellLas
15a22 74 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d t = usableSize -
15a23 20 34 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 4;.#if defined(
15a24 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 SQLITE_ENABLE_OV
15a25 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 ERSIZE_CELL_CHEC
15a26 4b 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 K). {. i
15a27 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
15a28 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 74 /* Index into t
15a29 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 he cell pointer
15a2a 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 69 array */. i
15a2b 6e 74 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20 nt sz;
15a2c 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65 /* Size of a ce
15a2d 6c 6c 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 ll */.. if(
15a2e 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 !pPage->leaf )
15a2f 69 43 65 6c 6c 4c 61 73 74 2d 2d 3b 0a 20 20 20 iCellLast--;.
15a30 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 for(i=0; i<pP
15a31 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 age->nCell; i++)
15a32 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 67 {. pc = g
15a33 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 et2byte(&data[ce
15a34 6c 6c 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b 0a llOffset+i*2]);.
15a35 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
15a36 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74 ( pc==iCellFirst
15a37 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 );. test
15a38 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c case( pc==iCellL
15a39 61 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69 ast );. i
15a3a 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 f( pc<iCellFirst
15a3b 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 || pc>iCellLast
15a3c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
15a3d 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15a3e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
15a3f 20 20 7d 0a 20 20 20 20 20 20 20 20 73 7a 20 3d }. sz =
15a40 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
15a41 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a ge, &data[pc]);.
15a42 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
15a43 28 20 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53 ( pc+sz==usableS
15a44 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 ize );. i
15a45 66 28 20 70 63 2b 73 7a 3e 75 73 61 62 6c 65 53 f( pc+sz>usableS
15a46 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ize ){.
15a47 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15a48 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
15a49 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
15a4a 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d if( !pPage-
15a4b 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 >leaf ) iCellLas
15a4c 74 2b 2b 3b 0a 20 20 20 20 7d 20 20 0a 23 65 6e t++;. } .#en
15a4d 64 69 66 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 dif.. /* Comp
15a4e 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72 ute the total fr
15a4f 65 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 ee space on the
15a50 70 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d page */. pc =
15a51 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15a52 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72 hdr+1]);. nFr
15a53 65 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d ee = data[hdr+7]
15a54 20 2b 20 74 6f 70 3b 0a 20 20 20 20 77 68 69 6c + top;. whil
15a55 65 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20 e( pc>0 ){.
15a56 20 75 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b u16 next, size;
15a57 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 . if( pc<iC
15a58 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 ellFirst || pc>i
15a59 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 CellLast ){.
15a5a 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 /* Start of
15a5b 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f 66 free block is of
15a5c 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 f the page */.
15a5d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15a5e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15a5f 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ; . }.
15a60 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 next = get2byte
15a61 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 (&data[pc]);.
15a62 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 size = get2by
15a63 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b te(&data[pc+2]);
15a64 0a 20 20 20 20 20 20 69 66 28 20 28 6e 65 78 74 . if( (next
15a65 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73 >0 && next<=pc+s
15a66 69 7a 65 2b 33 29 20 7c 7c 20 70 63 2b 73 69 7a ize+3) || pc+siz
15a67 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a e>usableSize ){.
15a68 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 /* Free
15a69 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69 blocks must be i
15a6a 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
15a6b 72 2e 20 41 6e 64 20 74 68 65 20 6c 61 73 74 20 r. And the last
15a6c 62 79 74 65 20 6f 66 0a 09 2a 2a 20 74 68 65 20 byte of..** the
15a6d 66 72 65 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 20 free-block must
15a6e 6c 69 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62 lie on the datab
15a6f 61 73 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 ase page. */.
15a70 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
15a71 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
15a72 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 ; . }.
15a73 20 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 20 2b nFree = nFree +
15a74 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20 size;. pc
15a75 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 20 = next;. }..
15a76 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f /* At this po
15a77 69 6e 74 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61 int, nFree conta
15a78 69 6e 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 ins the sum of t
15a79 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 he offset to the
15a7a 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 6f 66 start. ** of
15a7b 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e the cell-conten
15a7c 74 20 61 72 65 61 20 70 6c 75 73 20 74 68 65 20 t area plus the
15a7d 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 number of free b
15a7e 79 74 65 73 20 77 69 74 68 69 6e 0a 20 20 20 20 ytes within.
15a7f 2a 2a 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 ** the cell-cont
15a80 65 6e 74 20 61 72 65 61 2e 20 49 66 20 74 68 69 ent area. If thi
15a81 73 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 s is greater tha
15a82 6e 20 74 68 65 20 75 73 61 62 6c 65 2d 73 69 7a n the usable-siz
15a83 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 e. ** of the
15a84 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 70 page, then the p
15a85 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72 age must be corr
15a86 75 70 74 65 64 2e 20 54 68 69 73 20 63 68 65 63 upted. This chec
15a87 6b 20 61 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 65 k also. ** se
15a88 72 76 65 73 20 74 6f 20 76 65 72 69 66 79 20 74 rves to verify t
15a89 68 61 74 20 74 68 65 20 6f 66 66 73 65 74 20 74 hat the offset t
15a8a 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 o the start of t
15a8b 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a he cell-content.
15a8c 20 20 20 20 2a 2a 20 61 72 65 61 2c 20 61 63 63 ** area, acc
15a8d 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61 ording to the pa
15a8e 67 65 20 68 65 61 64 65 72 2c 20 6c 69 65 73 20 ge header, lies
15a8f 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e within the page.
15a90 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
15a91 6e 46 72 65 65 3e 75 73 61 62 6c 65 53 69 7a 65 nFree>usableSize
15a92 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
15a93 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15a94 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20 BKPT; . }.
15a95 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 pPage->nFree =
15a96 28 75 31 36 29 28 6e 46 72 65 65 20 2d 20 69 43 (u16)(nFree - iC
15a97 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 20 20 70 ellFirst);. p
15a98 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 Page->isInit = 1
15a99 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
15a9a 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
15a9b 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 20 ** Set up a raw
15a9c 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 20 page so that it
15a9d 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 74 looks like a dat
15a9e 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 69 abase page holdi
15a9f 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73 ng.** no entries
15aa0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
15aa1 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 67 zeroPage(MemPag
15aa2 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c e *pPage, int fl
15aa3 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 ags){. unsigned
15aa4 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 char *data = pP
15aa5 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74 age->aData;. Bt
15aa6 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 Shared *pBt = pP
15aa7 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68 age->pBt;. u8 h
15aa8 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
15aa9 66 66 73 65 74 3b 0a 20 20 75 31 36 20 66 69 72 ffset;. u16 fir
15aaa 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 st;.. assert( s
15aab 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e qlite3PagerPagen
15aac 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 umber(pPage->pDb
15aad 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67 Page)==pPage->pg
15aae 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 no );. assert(
15aaf 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
15ab0 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 xtra(pPage->pDbP
15ab1 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 age) == (void*)p
15ab2 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 Page );. assert
15ab3 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 ( sqlite3PagerGe
15ab4 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 tData(pPage->pDb
15ab5 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29 3b Page) == data );
15ab6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15ab7 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
15ab8 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
15ab9 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
15aba 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
15abb 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
15abc 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 64 61 ;. /*memset(&da
15abd 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d ta[hdr], 0, pBt-
15abe 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 >usableSize - hd
15abf 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 64 72 r);*/. data[hdr
15ac0 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 67 73 3b ] = (char)flags;
15ac1 0a 20 20 66 69 72 73 74 20 3d 20 68 64 72 20 2b . first = hdr +
15ac2 20 38 20 2b 20 34 2a 28 28 66 6c 61 67 73 26 50 8 + 4*((flags&P
15ac3 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f 31 3a 30 TF_LEAF)==0 ?1:0
15ac4 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 );. memset(&dat
15ac5 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b a[hdr+1], 0, 4);
15ac6 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d . data[hdr+7] =
15ac7 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 0;. put2byte(&
15ac8 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74 data[hdr+5], pBt
15ac9 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 ->usableSize);.
15aca 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 pPage->nFree =
15acb 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
15acc 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64 - first;. decod
15acd 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c eFlags(pPage, fl
15ace 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68 ags);. pPage->h
15acf 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a drOffset = hdr;.
15ad0 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 pPage->cellOff
15ad1 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70 set = first;. p
15ad2 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
15ad3 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 = 0;. assert( p
15ad4 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31 Bt->pageSize>=51
15ad5 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69 2 && pBt->pageSi
15ad6 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 70 ze<=32768 );. p
15ad7 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d Page->maskPage =
15ad8 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d pBt->pageSize -
15ad9 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 1;. pPage->nCe
15ada 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d ll = 0;. pPage-
15adb 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a >isInit = 1;.}..
15adc 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
15add 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64 DbPage obtained
15ade 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 from the pager
15adf 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75 into a MemPage u
15ae0 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74 sed by.** the bt
15ae1 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74 ree layer..*/.st
15ae2 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 atic MemPage *bt
15ae3 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
15ae4 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 e(DbPage *pDbPag
15ae5 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74 e, Pgno pgno, Bt
15ae6 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
15ae7 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
15ae8 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74 (MemPage*)sqlit
15ae9 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
15aea 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 pDbPage);. pPag
15aeb 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74 e->aData = sqlit
15aec 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
15aed 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65 DbPage);. pPage
15aee 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 44 62 50 ->pDbPage = pDbP
15aef 61 67 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42 age;. pPage->pB
15af0 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65 t = pBt;. pPage
15af1 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 ->pgno = pgno;.
15af2 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
15af3 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d t = pPage->pgno=
15af4 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20 =1 ? 100 : 0;.
15af5 72 65 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d return pPage; .}
15af6 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61 ../*.** Get a pa
15af7 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ge from the page
15af8 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 r. Initialize t
15af9 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 he MemPage.pBt a
15afa 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 nd.** MemPage.aD
15afb 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 ata elements if
15afc 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 needed..**.** If
15afd 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66 the noContent f
15afe 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d lag is set, it m
15aff 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 eans that we do
15b00 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a not care about.*
15b01 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * the content of
15b02 20 74 68 65 20 70 61 67 65 20 61 74 20 74 68 69 the page at thi
15b03 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e s time. So do n
15b04 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73 ot go to the dis
15b05 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68 k.** to fetch th
15b06 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74 e content. Just
15b07 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e fill in the con
15b08 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20 tent with zeros
15b09 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69 for now..** If i
15b0a 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 65 20 n the future we
15b0b 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 call sqlite3Page
15b0c 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73 rWrite() on this
15b0d 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d page, that.** m
15b0e 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 74 61 eans we have sta
15b0f 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65 rted to be conce
15b10 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65 rned about conte
15b11 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a nt and the disk.
15b12 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f ** read should o
15b13 63 63 75 72 20 61 74 20 74 68 61 74 20 70 6f 69 ccur at that poi
15b14 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e nt..*/.static in
15b15 74 20 62 74 72 65 65 47 65 74 50 61 67 65 28 0a t btreeGetPage(.
15b16 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c BtShared *pBt,
15b17 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 /* The bt
15b18 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ree */. Pgno pg
15b19 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a no, /*
15b1a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 Number of the p
15b1b 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a age to fetch */.
15b1c 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 MemPage **ppPa
15b1d 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e ge, /* Return
15b1e 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 the page in thi
15b1f 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 s parameter */.
15b20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 int noContent
15b21 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 /* Do not
15b22 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e load page conten
15b23 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a t if true */.){.
15b24 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 int rc;. DbPa
15b25 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 ge *pDbPage;..
15b26 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15b27 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
15b28 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d mutex) );. rc =
15b29 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 sqlite3PagerAcq
15b2a 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 uire(pBt->pPager
15b2b 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a , pgno, (DbPage*
15b2c 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f *)&pDbPage, noCo
15b2d 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 ntent);. if( rc
15b2e 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
15b2f 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 *ppPage = btreeP
15b30 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 ageFromDbPage(pD
15b31 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 bPage, pgno, pBt
15b32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
15b33 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
15b34 52 65 74 72 69 65 76 65 20 61 20 70 61 67 65 20 Retrieve a page
15b35 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63 from the pager c
15b36 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 65 71 ache. If the req
15b37 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e uested page is n
15b38 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e ot.** already in
15b39 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 the pager cache
15b3a 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e return NULL. In
15b3b 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d itialize the Mem
15b3c 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 Page.pBt and.**
15b3d 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c MemPage.aData el
15b3e 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64 ements if needed
15b3f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 ..*/.static MemP
15b40 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f age *btreePageLo
15b41 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 2a 70 okup(BtShared *p
15b42 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
15b43 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 DbPage *pDbPag
15b44 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c e;. assert( sql
15b45 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15b46 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
15b47 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74 pDbPage = sqlit
15b48 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42 e3PagerLookup(pB
15b49 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 t->pPager, pgno)
15b4a 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20 ;. if( pDbPage
15b4b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74 ){. return bt
15b4c 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
15b4d 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
15b4e 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 pBt);. }. ret
15b4f 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 0;.}../*.**
15b50 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 Return the size
15b51 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
15b52 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 file in pages. I
15b53 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b f there is any k
15b54 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c ind of.** error,
15b55 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e return ((unsign
15b56 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73 ed int)-1)..*/.s
15b57 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72 tatic Pgno pager
15b58 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72 Pagecount(BtShar
15b59 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 ed *pBt){. int
15b5a 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e nPage = -1;. in
15b5b 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
15b5c 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 pBt->pPage1 );.
15b5d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
15b5e 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
15b5f 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 >pPager, &nPage)
15b60 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d ;. assert( rc==
15b61 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 SQLITE_OK || nPa
15b62 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75 ge==-1 );. retu
15b63 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a rn (Pgno)nPage;.
15b64 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 }../*.** Get a p
15b65 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
15b66 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a er and initializ
15b67 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 e it. This rout
15b68 69 6e 65 20 69 73 20 6a 75 73 74 20 61 0a 2a 2a ine is just a.**
15b69 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 convenience wra
15b6a 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61 pper around sepa
15b6b 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 20 62 74 rate calls to bt
15b6c 72 65 65 47 65 74 50 61 67 65 28 29 20 61 6e 64 reeGetPage() and
15b6d 20 0a 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 .** btreeInitPa
15b6e 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 ge()..**.** If a
15b6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
15b70 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 2a then the value *
15b71 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f ppPage is set to
15b72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 49 is undefined. I
15b73 74 0a 2a 2a 20 6d 61 79 20 72 65 6d 61 69 6e 20 t.** may remain
15b74 75 6e 63 68 61 6e 67 65 64 2c 20 6f 72 20 69 74 unchanged, or it
15b75 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 61 may be set to a
15b76 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 2e n invalid value.
15b77 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
15b78 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 0a 20 etAndInitPage(.
15b79 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
15b7a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
15b7b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f database file */
15b7c 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 . Pgno pgno,
15b7d 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
15b7e 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f r of the page to
15b7f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 get */. MemPag
15b80 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 2f e **ppPage /
15b81 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65 * Write the page
15b82 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 2a 2f pointer here */
15b83 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
15b84 54 45 53 54 4f 4e 4c 59 28 20 50 67 6e 6f 20 69 TESTONLY( Pgno i
15b85 4c 61 73 74 50 67 20 3d 20 70 61 67 65 72 50 61 LastPg = pagerPa
15b86 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 20 29 0a gecount(pBt); ).
15b87 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15b88 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
15b89 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72 ->mutex) );.. r
15b8a 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
15b8b 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61 (pBt, pgno, ppPa
15b8c 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 ge, 0);. if( rc
15b8d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
15b8e 20 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 rc = btreeIni
15b8f 74 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a tPage(*ppPage);.
15b90 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
15b91 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
15b92 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 eleasePage(*ppPa
15b93 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ge);. }. }..
15b94 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75 /* If the requ
15b95 65 73 74 65 64 20 70 61 67 65 20 6e 75 6d 62 65 ested page numbe
15b96 72 20 77 61 73 20 65 69 74 68 65 72 20 30 20 6f r was either 0 o
15b97 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 r greater than t
15b98 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d he page. ** num
15b99 62 65 72 20 6f 66 20 74 68 65 20 6c 61 73 74 20 ber of the last
15b9a 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 page in the data
15b9b 62 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74 base, this funct
15b9c 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 74 75 72 ion should retur
15b9d 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 43 4f n. ** SQLITE_CO
15b9e 52 52 55 50 54 20 6f 72 20 73 6f 6d 65 20 6f 74 RRUPT or some ot
15b9f 68 65 72 20 65 72 72 6f 72 20 28 69 2e 65 2e 20 her error (i.e.
15ba0 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 20 43 68 SQLITE_FULL). Ch
15ba1 65 63 6b 20 74 68 61 74 20 74 68 69 73 0a 20 20 eck that this.
15ba2 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2e 20 ** is the case.
15ba3 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 */. assert( (p
15ba4 67 6e 6f 3e 30 20 26 26 20 70 67 6e 6f 3c 3d 69 gno>0 && pgno<=i
15ba5 4c 61 73 74 50 67 29 20 7c 7c 20 72 63 21 3d 53 LastPg) || rc!=S
15ba6 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 74 65 QLITE_OK );. te
15ba7 73 74 63 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20 stcase( pgno==0
15ba8 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 );. testcase( p
15ba9 67 6e 6f 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a gno==iLastPg );.
15baa 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
15bab 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
15bac 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 MemPage. This
15bad 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 should be called
15bae 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 once for each p
15baf 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 rior.** call to
15bb0 62 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f btreeGetPage..*/
15bb1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c .static void rel
15bb2 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65 easePage(MemPage
15bb3 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20 *pPage){. if(
15bb4 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73 pPage ){. ass
15bb5 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ert( pPage->nOve
15bb6 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c 69 rflow==0 || sqli
15bb7 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 te3PagerPageRefc
15bb8 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 50 ount(pPage->pDbP
15bb9 61 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 73 age)>1 );. as
15bba 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 sert( pPage->aDa
15bbb 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 ta );. assert
15bbc 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a ( pPage->pBt );.
15bbd 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
15bbe 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 te3PagerGetExtra
15bbf 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
15bc0 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 == (void*)pPage
15bc1 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15bc2 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 sqlite3PagerGetD
15bc3 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ata(pPage->pDbPa
15bc4 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74 ge)==pPage->aDat
15bc5 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 a );. assert(
15bc6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15bc7 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
15bc8 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 mutex) );. sq
15bc9 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
15bca 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b pPage->pDbPage);
15bcb 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75 . }.}../*.** Du
15bcc 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c ring a rollback,
15bcd 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 when the pager
15bce 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74 reloads informat
15bcf 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63 ion into the cac
15bd0 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 he.** so that th
15bd1 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74 6f e cache is resto
15bd2 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 red to its origi
15bd3 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 65 nal state at the
15bd4 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 start of.** the
15bd5 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f transaction, fo
15bd6 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73 74 r each page rest
15bd7 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e ored this routin
15bd8 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a e is called..**.
15bd9 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
15bda 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 74 needs to reset t
15bdb 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73 65 he extra data se
15bdc 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64 ction at the end
15bdd 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 of the.** page
15bde 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 68 to agree with th
15bdf 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e e restored data.
15be0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
15be1 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 67 pageReinit(DbPag
15be2 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d e *pData){. Mem
15be3 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 Page *pPage;. p
15be4 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 20 Page = (MemPage
15be5 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
15be6 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a 20 tExtra(pData);.
15be7 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15be8 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e PagerPageRefcoun
15be9 74 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 20 t(pData)>0 );.
15bea 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 if( pPage->isIni
15beb 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 t ){. assert(
15bec 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15bed 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
15bee 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 50 mutex) );. pP
15bef 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b age->isInit = 0;
15bf0 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
15bf1 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e PagerPageRefcoun
15bf2 74 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 20 t(pData)>1 ){.
15bf3 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 67 /* pPage mig
15bf4 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72 65 ht not be a btre
15bf5 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67 68 e page; it migh
15bf6 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 t be an overflow
15bf7 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f page. ** o
15bf8 72 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f 72 r ptrmap page or
15bf9 20 61 20 66 72 65 65 20 70 61 67 65 2e 20 20 49 a free page. I
15bfa 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 74 n those cases, t
15bfb 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 he following.
15bfc 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74 ** call to bt
15bfd 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 77 69 reeInitPage() wi
15bfe 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 74 75 72 6e ll likely return
15bff 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e SQLITE_CORRUPT.
15c00 0a 20 20 20 20 20 20 2a 2a 20 42 75 74 20 6e 6f . ** But no
15c01 20 68 61 72 6d 20 69 73 20 64 6f 6e 65 20 62 79 harm is done by
15c02 20 74 68 69 73 2e 20 20 41 6e 64 20 69 74 20 69 this. And it i
15c03 73 20 76 65 72 79 20 69 6d 70 6f 72 74 61 6e 74 s very important
15c04 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 62 that. ** b
15c05 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62 treeInitPage() b
15c06 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72 e called on ever
15c07 79 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20 y btree page so
15c08 77 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a we make. **
15c09 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76 the call for ev
15c0a 65 72 79 20 70 61 67 65 20 74 68 61 74 20 63 6f ery page that co
15c0b 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e mes in for re-in
15c0c 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20 iting. */.
15c0d 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 btreeInitPage(pP
15c0e 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a age);. }. }.
15c0f 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 }../*.** Invoke
15c10 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 the busy handler
15c11 20 66 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f for a btree..*/
15c12 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
15c13 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c eInvokeBusyHandl
15c14 65 72 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a er(void *pArg){.
15c15 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
15c16 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41 72 = (BtShared*)pAr
15c17 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 g;. assert( pBt
15c18 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 ->db );. assert
15c19 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
15c1a 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75 held(pBt->db->mu
15c1b 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e tex) );. return
15c1c 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 sqlite3InvokeBu
15c1d 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e syHandler(&pBt->
15c1e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29 db->busyHandler)
15c1f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 ;.}../*.** Open
15c20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e a database file.
15c21 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d .** .** zFilenam
15c22 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 e is the name of
15c23 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
15c24 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d le. If zFilenam
15c25 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 6e e is NULL.** a n
15c26 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74 68 ew database with
15c27 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 69 a random name i
15c28 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 s created. This
15c29 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 0a randomly named.
15c2a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
15c2b 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 will be deleted
15c2c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72 when sqlite3Btr
15c2d 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c eeClose() is cal
15c2e 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 led..** If zFile
15c2f 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 name is ":memory
15c30 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 :" then an in-me
15c31 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73 mory database is
15c32 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74 created.** that
15c33 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
15c34 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e y destroyed when
15c35 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a it is closed..*
15c36 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 *.** If the data
15c37 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 base is already
15c38 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 20 73 61 opened in the sa
15c39 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
15c3a 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65 ection.** and we
15c3b 20 61 72 65 20 69 6e 20 73 68 61 72 65 64 20 63 are in shared c
15c3c 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 20 ache mode, then
15c3d 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66 61 the open will fa
15c3e 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 il with an.** SQ
15c3f 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 LITE_CONSTRAINT
15c40 65 72 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e 6f error. We canno
15c41 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 6d t allow two or m
15c42 6f 72 65 20 42 74 53 68 61 72 65 64 0a 2a 2a 20 ore BtShared.**
15c43 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73 objects in the s
15c44 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
15c45 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 6f nection since do
15c46 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 64 ing so will lead
15c47 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 20 .** to problems
15c48 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f with locking..*/
15c49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
15c4a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
15c4b 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 Open(. const ch
15c4c 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 ar *zFilename,
15c4d 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 /* Name of the f
15c4e 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ile containing t
15c4f 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61 73 he BTree databas
15c50 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a e */. sqlite3 *
15c51 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f db, /
15c52 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 * Associated dat
15c53 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
15c54 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 Btree **ppBtre
15c55 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 e, /* Poi
15c56 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65 nter to new Btre
15c57 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e e object written
15c58 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 here */. int f
15c59 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
15c5a 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f /* Options */
15c5b 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 . int vfsFlags
15c5c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
15c5d 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 ags passed throu
15c5e 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 gh to sqlite3_vf
15c5f 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a s.xOpen() */.){.
15c60 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
15c61 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 Vfs;
15c62 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75 /* The VFS to u
15c63 73 65 20 66 6f 72 20 74 68 69 73 20 62 74 72 65 se for this btre
15c64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 e */. BtShared
15c65 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 20 20 20 *pBt = 0;
15c66 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20 /* Shared
15c67 70 61 72 74 20 6f 66 20 62 74 72 65 65 20 73 74 part of btree st
15c68 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 72 ructure */. Btr
15c69 65 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 ee *p;
15c6a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
15c6b 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 72 6e 20 andle to return
15c6c 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
15c6d 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e 20 3d 20 ex *mutexOpen =
15c6e 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e 74 73 20 0; /* Prevents
15c6f 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e a race condition
15c70 2e 20 54 69 63 6b 65 74 20 23 33 35 33 37 20 2a . Ticket #3537 *
15c71 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
15c72 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
15c73 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 /* Result cod
15c74 65 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 e from this func
15c75 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 6e 52 65 tion */. u8 nRe
15c76 73 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 serve;
15c77 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
15c78 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 of unused space
15c79 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f on each page */
15c7a 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 . unsigned char
15c7b 20 7a 44 62 48 65 61 64 65 72 5b 31 30 30 5d 3b zDbHeader[100];
15c7c 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 65 /* Database he
15c7d 61 64 65 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a ader content */.
15c7e 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 76 61 . /* Set the va
15c7f 72 69 61 62 6c 65 20 69 73 4d 65 6d 64 62 20 74 riable isMemdb t
15c80 6f 20 74 72 75 65 20 66 6f 72 20 61 6e 20 69 6e o true for an in
15c81 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
15c82 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 61 6c 73 65 , or . ** false
15c83 20 66 6f 72 20 61 20 66 69 6c 65 2d 62 61 73 65 for a file-base
15c84 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 d database. This
15c85 20 73 79 6d 62 6f 6c 20 69 73 20 6f 6e 6c 79 20 symbol is only
15c86 72 65 71 75 69 72 65 64 20 69 66 0a 20 20 2a 2a required if. **
15c87 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 73 either of the s
15c88 68 61 72 65 64 2d 64 61 74 61 20 6f 72 20 61 75 hared-data or au
15c89 74 6f 76 61 63 75 75 6d 20 66 65 61 74 75 72 65 tovacuum feature
15c8a 73 20 61 72 65 20 63 6f 6d 70 69 6c 65 64 20 0a s are compiled .
15c8b 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69 ** into the li
15c8c 62 72 61 72 79 2e 0a 20 20 2a 2f 0a 23 69 66 20 brary.. */.#if
15c8d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
15c8e 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
15c8f 45 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 E) || !defined(S
15c90 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
15c91 41 43 55 55 4d 29 0a 20 20 23 69 66 64 65 66 20 ACUUM). #ifdef
15c92 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f SQLITE_OMIT_MEMO
15c93 52 59 44 42 0a 20 20 20 20 63 6f 6e 73 74 20 69 RYDB. const i
15c94 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a nt isMemdb = 0;.
15c95 20 20 23 65 6c 73 65 0a 20 20 20 20 63 6f 6e 73 #else. cons
15c96 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 t int isMemdb =
15c97 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 73 74 zFilename && !st
15c98 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 rcmp(zFilename,
15c99 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b 0a 20 20 23 ":memory:");. #
15c9a 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20 endif.#endif..
15c9b 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b assert( db!=0 );
15c9c 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
15c9d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
15c9e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 ->mutex) );.. p
15c9f 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a Vfs = db->pVfs;.
15ca0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
15ca1 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 locZero(sizeof(B
15ca2 74 72 65 65 29 29 3b 0a 20 20 69 66 28 20 21 70 tree));. if( !p
15ca3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
15ca4 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
15ca5 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 . p->inTrans =
15ca6 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 2d TRANS_NONE;. p-
15ca7 3e 64 62 20 3d 20 64 62 3b 0a 23 69 66 6e 64 65 >db = db;.#ifnde
15ca8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
15ca9 41 52 45 44 5f 43 41 43 48 45 0a 20 20 70 2d 3e ARED_CACHE. p->
15caa 6c 6f 63 6b 2e 70 42 74 72 65 65 20 3d 20 70 3b lock.pBtree = p;
15cab 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c . p->lock.iTabl
15cac 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 23 e = 1;.#endif..#
15cad 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
15cae 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
15caf 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 ACHE) && !define
15cb0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 d(SQLITE_OMIT_DI
15cb1 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 SKIO). /*. **
15cb2 49 66 20 74 68 69 73 20 42 74 72 65 65 20 69 73 If this Btree is
15cb3 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 a candidate for
15cb4 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74 shared cache, t
15cb5 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20 ry to find an.
15cb6 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 68 ** existing BtSh
15cb7 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74 ared object that
15cb8 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 69 we can share wi
15cb9 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 th. */. if( is
15cba 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a 46 69 6c Memdb==0 && zFil
15cbb 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 ename && zFilena
15cbc 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 28 me[0] ){. if(
15cbd 20 76 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49 vfsFlags & SQLI
15cbe 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 TE_OPEN_SHAREDCA
15cbf 43 48 45 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 CHE ){. int
15cc0 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d nFullPathname =
15cc1 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
15cc2 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20 e+1;. char
15cc3 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d *zFullPathname =
15cc4 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e sqlite3Malloc(n
15cc5 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 FullPathname);.
15cc6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 sqlite3_mut
15cc7 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b ex *mutexShared;
15cc8 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 . p->sharab
15cc9 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 le = 1;. if
15cca 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 ( !zFullPathname
15ccb 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
15ccc 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 te3_free(p);.
15ccd 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
15cce 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
15ccf 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f }. sqlite3O
15cd0 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 sFullPathname(pV
15cd1 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e fs, zFilename, n
15cd2 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46 FullPathname, zF
15cd3 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 ullPathname);.
15cd4 20 20 20 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20 mutexOpen =
15cd5 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
15cd6 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
15cd7 54 41 54 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20 TATIC_OPEN);.
15cd8 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15cd9 5f 65 6e 74 65 72 28 6d 75 74 65 78 4f 70 65 6e _enter(mutexOpen
15cda 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 );. mutexSh
15cdb 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 ared = sqlite3Mu
15cdc 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
15cdd 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
15cde 54 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 TER);. sqli
15cdf 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
15ce0 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 mutexShared);.
15ce1 20 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 for(pBt=GLOB
15ce2 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c AL(BtShared*,sql
15ce3 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
15ce4 69 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 ist); pBt; pBt=p
15ce5 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 Bt->pNext){.
15ce6 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d assert( pBt-
15ce7 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 >nRef>0 );.
15ce8 20 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 if( 0==strcmp
15ce9 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 (zFullPathname,
15cea 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 sqlite3PagerFile
15ceb 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 name(pBt->pPager
15cec 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 )).
15ced 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 && sqlite3Pa
15cee 67 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67 gerVfs(pBt->pPag
15cef 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 er)==pVfs ){.
15cf0 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a int iDb;.
15cf1 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 44 for(iD
15cf2 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62 b=db->nDb-1; iDb
15cf3 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20 >=0; iDb--){.
15cf4 20 20 20 20 20 20 20 20 20 42 74 72 65 65 20 2a Btree *
15cf5 70 45 78 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e pExisting = db->
15cf6 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20 aDb[iDb].pBt;.
15cf7 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45 if( pE
15cf8 78 69 73 74 69 6e 67 20 26 26 20 70 45 78 69 73 xisting && pExis
15cf9 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29 ting->pBt==pBt )
15cfa 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
15cfb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
15cfc 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 ave(mutexShared)
15cfd 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
15cfe 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
15cff 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a ave(mutexOpen);.
15d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
15d01 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c lite3_free(zFull
15d02 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Pathname);.
15d03 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
15d04 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 _free(p);.
15d05 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
15d06 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
15d07 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
15d08 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
15d09 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 p->pBt = p
15d0a 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 Bt;. pB
15d0b 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 t->nRef++;.
15d0c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
15d0d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
15d0e 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
15d0f 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 x_leave(mutexSha
15d10 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 red);. sqli
15d11 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61 te3_free(zFullPa
15d12 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 thname);. }.#
15d13 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
15d14 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 UG. else{.
15d15 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d /* In debug m
15d16 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c ode, we mark all
15d17 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 61 persistent data
15d18 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62 6c bases as sharabl
15d19 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20 e. ** even
15d1a 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f when they are no
15d1b 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 73 t. This exercis
15d1c 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 es the locking c
15d1d 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a ode and. **
15d1e 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f gives more oppo
15d1f 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65 rtunity for asse
15d20 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 rts(sqlite3_mute
15d21 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20 x_held()).
15d22 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f ** statements to
15d23 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72 find locking pr
15d24 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f oblems.. */
15d25 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62 . p->sharab
15d26 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65 le = 1;. }.#e
15d27 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a ndif. }.#endif.
15d28 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a if( pBt==0 ){.
15d29 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 /*. ** Th
15d2a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
15d2b 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68 rts make sure th
15d2c 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75 73 at structures us
15d2d 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20 ed by the btree
15d2e 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 are. ** the r
15d2f 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73 ight size. This
15d30 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 61 is to guard aga
15d31 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65 inst size change
15d32 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20 s that result.
15d33 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c ** when compil
15d34 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65 ing on a differe
15d35 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65 2e nt architecture.
15d36 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
15d37 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d rt( sizeof(i64)=
15d38 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34 =8 || sizeof(i64
15d39 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 )==4 );. asse
15d3a 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d rt( sizeof(u64)=
15d3b 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34 =8 || sizeof(u64
15d3c 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 )==4 );. asse
15d3d 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d rt( sizeof(u32)=
15d3e 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =4 );. assert
15d3f 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32 ( sizeof(u16)==2
15d40 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15d41 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20 sizeof(Pgno)==4
15d42 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20 );. . pBt =
15d43 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 sqlite3MallocZer
15d44 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20 o( sizeof(*pBt)
15d45 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d );. if( pBt==
15d46 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
15d47 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15d48 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f goto btree_o
15d49 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
15d4a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
15d4b 61 67 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26 agerOpen(pVfs, &
15d4c 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69 pBt->pPager, zFi
15d4d 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 lename,.
15d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d4f 20 20 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c EXTRA_SIZE, fl
15d50 61 67 73 2c 20 76 66 73 46 6c 61 67 73 2c 20 70 ags, vfsFlags, p
15d51 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 20 ageReinit);.
15d52 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
15d53 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
15d54 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 sqlite3PagerRead
15d55 46 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e Fileheader(pBt->
15d56 70 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44 pPager,sizeof(zD
15d57 62 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64 bHeader),zDbHead
15d58 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 er);. }. i
15d59 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
15d5a 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 ){. goto b
15d5b 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 tree_open_out;.
15d5c 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 62 }. pBt->db
15d5d 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 = db;. sqlit
15d5e 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 e3PagerSetBusyha
15d5f 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65 ndler(pBt->pPage
15d60 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 r, btreeInvokeBu
15d61 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b syHandler, pBt);
15d62 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42 . p->pBt = pB
15d63 74 3b 0a 20 20 0a 20 20 20 20 70 42 74 2d 3e 70 t;. . pBt->p
15d64 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 Cursor = 0;.
15d65 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b pBt->pPage1 = 0;
15d66 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e . pBt->readOn
15d67 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ly = sqlite3Page
15d68 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d rIsreadonly(pBt-
15d69 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 >pPager);. pB
15d6a 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 t->pageSize = ge
15d6b 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 t2byte(&zDbHeade
15d6c 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 r[16]);. if(
15d6d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31 pBt->pageSize<51
15d6e 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69 2 || pBt->pageSi
15d6f 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 ze>SQLITE_MAX_PA
15d70 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20 GE_SIZE.
15d71 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53 || ((pBt->pageS
15d72 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65 ize-1)&pBt->page
15d73 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Size)!=0 ){.
15d74 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
15d75 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c = 0;.#ifndef SQL
15d76 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
15d77 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 UUM. /* If
15d78 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 the magic name "
15d79 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63 :memory:" will c
15d7a 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f reate an in-memo
15d7b 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 ry database, the
15d7c 6e 0a 20 20 20 20 20 20 2a 2a 20 6c 65 61 76 65 n. ** leave
15d7d 20 74 68 65 20 61 75 74 6f 56 61 63 75 75 6d 20 the autoVacuum
15d7e 6d 6f 64 65 20 61 74 20 30 20 28 64 6f 20 6e 6f mode at 0 (do no
15d7f 74 20 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c 20 t auto-vacuum),
15d80 65 76 65 6e 20 69 66 0a 20 20 20 20 20 20 2a 2a even if. **
15d81 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
15d82 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 74 72 AUTOVACUUM is tr
15d83 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 ue. On the other
15d84 20 68 61 6e 64 2c 20 69 66 0a 20 20 20 20 20 20 hand, if.
15d85 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d ** SQLITE_OMIT_M
15d86 45 4d 4f 52 59 44 42 20 68 61 73 20 62 65 65 6e EMORYDB has been
15d87 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 22 defined, then "
15d88 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20 6a 75 73 :memory:" is jus
15d89 74 20 61 0a 20 20 20 20 20 20 2a 2a 20 72 65 67 t a. ** reg
15d8a 75 6c 61 72 20 66 69 6c 65 2d 6e 61 6d 65 2e 20 ular file-name.
15d8b 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
15d8c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 70 70 auto-vacuum app
15d8d 6c 69 65 73 20 61 73 20 70 65 72 20 6e 6f 72 6d lies as per norm
15d8e 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 al.. */.
15d8f 20 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 if( zFilename
15d90 20 26 26 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a && !isMemdb ){.
15d91 20 20 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 pBt->aut
15d92 6f 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 oVacuum = (SQLIT
15d93 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 E_DEFAULT_AUTOVA
15d94 43 55 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20 CUUM ? 1 : 0);.
15d95 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 pBt->incr
15d96 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45 Vacuum = (SQLITE
15d97 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
15d98 55 55 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b UUM==2 ? 1 : 0);
15d99 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
15d9a 20 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d nReserve =
15d9b 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
15d9c 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 nReserve =
15d9d 7a 44 62 48 65 61 64 65 72 5b 32 30 5d 3b 0a 20 zDbHeader[20];.
15d9e 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 pBt->pageSi
15d9f 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 69 66 zeFixed = 1;.#if
15da0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15da1 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
15da2 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 pBt->autoVacuu
15da3 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a m = (get4byte(&z
15da4 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 34 2a DbHeader[36 + 4*
15da5 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 20 20 4])?1:0);.
15da6 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
15da7 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62 = (get4byte(&zDb
15da8 48 65 61 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d Header[36 + 7*4]
15da9 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 )?1:0);.#endif.
15daa 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
15dab 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 lite3PagerSetPag
15dac 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 esize(pBt->pPage
15dad 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a r, &pBt->pageSiz
15dae 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 e, nReserve);.
15daf 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
15db0 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a btree_open_out;.
15db1 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 pBt->usableS
15db2 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 ize = pBt->pageS
15db3 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a ize - nReserve;.
15db4 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42 74 assert( (pBt
15db5 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d ->pageSize & 7)=
15db6 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65 =0 ); /* 8-byte
15db7 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61 alignment of pa
15db8 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69 geSize */. .#i
15db9 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
15dba 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
15dbb 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 CHE) && !defined
15dbc 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 (SQLITE_OMIT_DIS
15dbd 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 64 64 20 KIO). /* Add
15dbe 74 68 65 20 6e 65 77 20 42 74 53 68 61 72 65 64 the new BtShared
15dbf 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 6c object to the l
15dc0 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 61 72 61 inked list shara
15dc1 62 6c 65 20 42 74 53 68 61 72 65 64 73 2e 0a 20 ble BtShareds..
15dc2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d */. if( p-
15dc3 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
15dc4 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15dc5 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20 *mutexShared;.
15dc6 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d pBt->nRef =
15dc7 20 31 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 1;. mutexS
15dc8 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d hared = sqlite3M
15dc9 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
15dca 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
15dcb 53 54 45 52 29 3b 0a 20 20 20 20 20 20 69 66 28 STER);. if(
15dcc 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 SQLITE_THREADSA
15dcd 46 45 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f FE && sqlite3Glo
15dce 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d balConfig.bCoreM
15dcf 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 utex ){.
15dd0 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c pBt->mutex = sql
15dd1 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
15dd2 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 QLITE_MUTEX_FAST
15dd3 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
15dd4 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a Bt->mutex==0 ){.
15dd5 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 rc = S
15dd6 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
15dd7 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f db->mallo
15dd8 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 cFailed = 0;.
15dd9 20 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65 goto btre
15dda 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 e_open_out;.
15ddb 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
15ddc 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
15ddd 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 x_enter(mutexSha
15dde 72 65 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d red);. pBt-
15ddf 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28 >pNext = GLOBAL(
15de0 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 BtShared*,sqlite
15de1 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
15de2 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 );. GLOBAL(
15de3 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 BtShared*,sqlite
15de4 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
15de5 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73 ) = pBt;. s
15de6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
15de7 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b ve(mutexShared);
15de8 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
15de9 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 }..#if !defined(
15dea 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15deb 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 ED_CACHE) && !de
15dec 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
15ded 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49 T_DISKIO). /* I
15dee 66 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20 f the new Btree
15def 75 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20 uses a sharable
15df0 70 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20 pBtShared, then
15df1 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a link the new. *
15df2 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65 * Btree into the
15df3 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61 list of all sha
15df4 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72 rable Btrees for
15df5 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 the same connec
15df6 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c tion.. ** The l
15df7 69 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61 ist is kept in a
15df8 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 scending order b
15df9 79 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20 y pBt address..
15dfa 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 */. if( p->sha
15dfb 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 rable ){. int
15dfc 20 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 i;. Btree *p
15dfd 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 Sib;. for(i=0
15dfe 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
15dff 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 ){. if( (pS
15e00 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e ib = db->aDb[i].
15e01 70 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d pBt)!=0 && pSib-
15e02 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
15e03 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62 while( pSib
15e04 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20 ->pPrev ){ pSib
15e05 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d = pSib->pPrev; }
15e06 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e . if( p->
15e07 70 42 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b pBt<pSib->pBt ){
15e08 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e . p->pN
15e09 65 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 ext = pSib;.
15e0a 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d p->pPrev =
15e0b 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 0;. pS
15e0c 69 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 ib->pPrev = p;.
15e0d 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
15e0e 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 while( p
15e0f 53 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53 Sib->pNext && pS
15e10 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70 ib->pNext->pBt<p
15e11 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ->pBt ){.
15e12 20 20 20 20 20 70 53 69 62 20 3d 20 70 53 69 62 pSib = pSib
15e13 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 ->pNext;.
15e14 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 }. p
15e15 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e ->pNext = pSib->
15e16 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 pNext;.
15e17 20 70 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62 p->pPrev = pSib
15e18 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
15e19 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 p->pNext ){.
15e1a 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 p->pNext
15e1b 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20 ->pPrev = p;.
15e1c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15e1d 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d pSib->pNext =
15e1e 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 p;. }.
15e1f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
15e20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 }. }. }.#
15e21 65 6e 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65 endif. *ppBtree
15e22 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65 = p;..btree_ope
15e23 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21 n_out:. if( rc!
15e24 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
15e25 20 20 69 66 28 20 70 42 74 20 26 26 20 70 42 74 if( pBt && pBt
15e26 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 ->pPager ){.
15e27 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c sqlite3PagerCl
15e28 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 ose(pBt->pPager)
15e29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
15e2a 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 te3_free(pBt);.
15e2b 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
15e2c 70 29 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65 p);. *ppBtree
15e2d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 0;. }. if(
15e2e 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20 mutexOpen ){.
15e2f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15e30 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 65 _mutex_held(mute
15e31 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 73 71 xOpen) );. sq
15e32 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
15e33 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 e(mutexOpen);.
15e34 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
15e35 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e ../*.** Decremen
15e36 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e t the BtShared.n
15e37 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20 57 68 Ref counter. Wh
15e38 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 7a 65 en it reaches ze
15e39 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 ro,.** remove th
15e3a 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 e BtShared struc
15e3b 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 73 68 ture from the sh
15e3c 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52 65 74 aring list. Ret
15e3d 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 urn.** true if t
15e3e 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 he BtShared.nRef
15e3f 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68 65 73 counter reaches
15e40 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e zero and return
15e41 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 .** false if it
15e42 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74 69 76 is still positiv
15e43 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
15e44 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 removeFromShari
15e45 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65 64 20 ngList(BtShared
15e46 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 *pBt){.#ifndef S
15e47 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
15e48 44 5f 43 41 43 48 45 0a 20 20 73 71 6c 69 74 65 D_CACHE. sqlite
15e49 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3_mutex *pMaster
15e4a 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4c ;. BtShared *pL
15e4b 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76 ist;. int remov
15e4c 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 ed = 0;.. asser
15e4d 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15e4e 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75 _notheld(pBt->mu
15e4f 74 65 78 29 20 29 3b 0a 20 20 70 4d 61 73 74 65 tex) );. pMaste
15e50 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 r = sqlite3Mutex
15e51 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
15e52 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
15e53 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 );. sqlite3_mut
15e54 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 ex_enter(pMaster
15e55 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d );. pBt->nRef--
15e56 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65 ;. if( pBt->nRe
15e57 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 f<=0 ){. if(
15e58 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
15e59 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
15e5a 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b cheList)==pBt ){
15e5b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74 . GLOBAL(Bt
15e5c 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
15e5d 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20 haredCacheList)
15e5e 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 = pBt->pNext;.
15e5f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
15e60 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74 List = GLOBAL(Bt
15e61 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
15e62 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b haredCacheList);
15e63 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c . while( AL
15e64 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 70 WAYS(pList) && p
15e65 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74 List->pNext!=pBt
15e66 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 ){. pLis
15e67 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a t=pList->pNext;.
15e68 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
15e69 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 ( ALWAYS(pList)
15e6a 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 ){. pList
15e6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 ->pNext = pBt->p
15e6c 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
15e6d 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49 }. if( SQLI
15e6e 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b TE_THREADSAFE ){
15e6f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15e70 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d utex_free(pBt->m
15e71 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 utex);. }.
15e72 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 removed = 1;.
15e73 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
15e74 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29 x_leave(pMaster)
15e75 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76 ;. return remov
15e76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 ed;.#else. retu
15e77 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a rn 1;.#endif.}..
15e78 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 /*.** Make sure
15e79 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70 pBt->pTmpSpace p
15e7a 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f oints to an allo
15e7b 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58 cation of .** MX
15e7c 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 _CELL_SIZE(pBt)
15e7d 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 bytes..*/.static
15e7e 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65 void allocateTe
15e7f 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 mpSpace(BtShared
15e80 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70 *pBt){. if( !p
15e81 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b Bt->pTmpSpace ){
15e82 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 . pBt->pTmpSp
15e83 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ace = sqlite3Pag
15e84 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 eMalloc( pBt->pa
15e85 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a geSize );. }.}.
15e86 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 ./*.** Free the
15e87 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61 pBt->pTmpSpace a
15e88 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 llocation.*/.sta
15e89 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d tic void freeTem
15e8a 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20 pSpace(BtShared
15e8b 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 *pBt){. sqlite3
15e8c 50 61 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70 PageFree( pBt->p
15e8d 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74 TmpSpace);. pBt
15e8e 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b ->pTmpSpace = 0;
15e8f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
15e90 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 an open database
15e91 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20 and invalidate
15e92 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a all cursors..*/.
15e93 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
15e94 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
15e95 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a lose(Btree *p){.
15e96 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
15e97 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 = p->pBt;. BtCu
15e98 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f rsor *pCur;.. /
15e99 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 * Close all curs
15e9a 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74 ors opened via t
15e9b 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a his handle. */.
15e9c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15e9d 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
15e9e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
15e9f 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
15ea0 72 28 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70 r(p);. pCur = p
15ea1 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 Bt->pCursor;. w
15ea2 68 69 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 hile( pCur ){.
15ea3 20 20 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 BtCursor *pTmp
15ea4 20 3d 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 = pCur;. pCu
15ea5 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b r = pCur->pNext;
15ea6 0a 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 . if( pTmp->p
15ea7 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 Btree==p ){.
15ea8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
15ea9 6f 73 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b oseCursor(pTmp);
15eaa 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
15eab 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 Rollback any ac
15eac 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e tive transaction
15ead 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 and free the ha
15eae 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a ndle structure..
15eaf 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f ** The call to
15eb0 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c sqlite3BtreeRol
15eb1 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e lback() drops an
15eb2 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 y table-locks he
15eb3 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 ld by. ** this
15eb4 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 handle.. */. s
15eb5 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 qlite3BtreeRollb
15eb6 61 63 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 ack(p);. sqlite
15eb7 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
15eb8 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 . /* If there a
15eb9 72 65 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f re still other o
15eba 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
15ebb 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 ences to the sha
15ebc 72 65 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 red-btree. ** s
15ebd 74 72 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e tructure, return
15ebe 20 6e 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e now. The remain
15ebf 64 65 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 der of this proc
15ec0 65 64 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 edure cleans .
15ec1 2a 2a 20 75 70 20 74 68 65 20 73 68 61 72 65 64 ** up the shared
15ec2 2d 62 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 -btree.. */. a
15ec3 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f ssert( p->wantTo
15ec4 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f Lock==0 && p->lo
15ec5 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 cked==0 );. if(
15ec6 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c !p->sharable ||
15ec7 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 removeFromShari
15ec8 6e 67 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20 ngList(pBt) ){.
15ec9 20 20 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73 /* The pBt is
15eca 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 no longer on th
15ecb 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 e sharing list,
15ecc 73 6f 20 77 65 20 63 61 6e 20 61 63 63 65 73 73 so we can access
15ecd 0a 20 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f . ** it witho
15ece 75 74 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c ut having to hol
15ecf 64 20 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 d the mutex..
15ed0 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e **. ** Clean
15ed1 20 6f 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20 out and delete
15ed2 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
15ed3 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ect.. */.
15ed4 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43 assert( !pBt->pC
15ed5 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c ursor );. sql
15ed6 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 ite3PagerClose(p
15ed7 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
15ed8 20 69 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 if( pBt->xFreeS
15ed9 63 68 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 chema && pBt->pS
15eda 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 chema ){. p
15edb 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 Bt->xFreeSchema(
15edc 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 pBt->pSchema);.
15edd 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
15ede 5f 66 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65 _free(pBt->pSche
15edf 6d 61 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d ma);. freeTem
15ee0 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 pSpace(pBt);.
15ee1 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 sqlite3_free(pB
15ee2 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 t);. }..#ifndef
15ee3 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
15ee4 52 45 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 RED_CACHE. asse
15ee5 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 rt( p->wantToLoc
15ee6 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 k==0 );. assert
15ee7 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 ( p->locked==0 )
15ee8 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 ;. if( p->pPrev
15ee9 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 ) p->pPrev->pNe
15eea 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 xt = p->pNext;.
15eeb 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 if( p->pNext )
15eec 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 p->pNext->pPrev
15eed 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 = p->pPrev;.#end
15eee 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 if.. sqlite3_fr
15eef 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ee(p);. return
15ef0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
15ef1 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c .** Change the l
15ef2 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 imit on the numb
15ef3 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f er of pages allo
15ef4 77 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 wed in the cache
15ef5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 ..**.** The maxi
15ef6 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 mum number of ca
15ef7 63 68 65 20 70 61 67 65 73 20 69 73 20 73 65 74 che pages is set
15ef8 20 74 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 to the absolute
15ef9 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 .** value of mxP
15efa 61 67 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 age. If mxPage
15efb 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 is negative, the
15efc 20 70 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f pager will.** o
15efd 70 65 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e perate asynchron
15efe 6f 75 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 ously - it will
15eff 6e 6f 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 not stop to do f
15f00 73 79 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e sync()s.** to in
15f01 73 75 72 65 20 64 61 74 61 20 69 73 20 77 72 69 sure data is wri
15f02 74 74 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b tten to the disk
15f03 20 73 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a surface before.
15f04 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 ** continuing.
15f05 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 Transactions sti
15f06 6c 6c 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 ll work if synch
15f07 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a ronous is off,.*
15f08 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 * and the databa
15f09 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 se cannot be cor
15f0a 72 75 70 74 65 64 20 69 66 20 74 68 69 73 20 70 rupted if this p
15f0b 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 rogram.** crashe
15f0c 73 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6f s. But if the o
15f0d 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
15f0e 63 72 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 crashes or there
15f0f 20 69 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 is.** an abrupt
15f10 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 power failure w
15f11 68 65 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 hen synchronous
15f12 69 73 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 is off, the data
15f13 62 61 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 base.** could be
15f14 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f left in an inco
15f15 6e 73 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 nsistent and unr
15f16 65 63 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 ecoverable state
15f17 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 ..** Synchronous
15f18 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c is on by defaul
15f19 74 20 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f t so database co
15f1a 72 72 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a rruption is not.
15f1b 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f ** normally a wo
15f1c 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rry..*/.SQLITE_P
15f1d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15f1e 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 e3BtreeSetCacheS
15f1f 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ize(Btree *p, in
15f20 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 t mxPage){. BtS
15f21 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
15f22 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 pBt;. assert( s
15f23 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15f24 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
15f25 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
15f26 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c eEnter(p);. sql
15f27 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68 ite3PagerSetCach
15f28 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 esize(pBt->pPage
15f29 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 r, mxPage);. sq
15f2a 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15f2b 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c p);. return SQL
15f2c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
15f2d 20 43 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 Change the way
15f2e 64 61 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 data is synced t
15f2f 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 o disk in order
15f30 74 6f 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 to increase or d
15f31 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 ecrease.** how w
15f32 65 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 ell the database
15f33 20 72 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 resists damage
15f34 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 due to OS crashe
15f35 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 s and power.** f
15f36 61 69 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 ailures. Level
15f37 31 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 1 is the same as
15f38 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e asynchronous (n
15f39 6f 20 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 o syncs() occur
15f3a 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 and.** there is
15f3b 61 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 a high probabili
15f3c 74 79 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c ty of damage) L
15f3d 65 76 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 evel 2 is the de
15f3e 66 61 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a fault. There.**
15f3f 20 69 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 is a very low b
15f40 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 ut non-zero prob
15f41 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 ability of damag
15f42 65 2e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 e. Level 3 redu
15f43 63 65 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 ces the.** proba
15f44 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 bility of damage
15f45 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 to near zero bu
15f46 74 20 77 69 74 68 20 61 20 77 72 69 74 65 20 70 t with a write p
15f47 65 72 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 erformance reduc
15f48 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 tion..*/.#ifndef
15f49 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 SQLITE_OMIT_PAG
15f4a 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 ER_PRAGMAS.SQLIT
15f4b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
15f4c 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 lite3BtreeSetSaf
15f4d 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a etyLevel(Btree *
15f4e 70 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e p, int level, in
15f4f 74 20 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 t fullSync){. B
15f50 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
15f51 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
15f52 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15f53 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
15f54 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ) );. sqlite3Bt
15f55 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 reeEnter(p);. s
15f56 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 qlite3PagerSetSa
15f57 66 65 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 fetyLevel(pBt->p
15f58 50 61 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 Pager, level, fu
15f59 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 llSync);. sqlit
15f5a 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
15f5b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15f5c 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f _OK;.}.#endif../
15f5d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 *.** Return TRUE
15f5e 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 62 74 if the given bt
15f5f 72 65 65 20 69 73 20 73 65 74 20 74 6f 20 73 61 ree is set to sa
15f60 66 65 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 fety level 1. I
15f61 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 n other.** words
15f62 2c 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 , return TRUE if
15f63 20 6e 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72 no sync() occur
15f64 73 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 s on the disk fi
15f65 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 les..*/.SQLITE_P
15f66 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15f67 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 e3BtreeSyncDisab
15f68 6c 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 led(Btree *p){.
15f69 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15f6a 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 p->pBt;. int r
15f6b 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c c;. assert( sql
15f6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15f6d 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
15f6e 20 20 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
15f6f 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 eEnter(p);. ass
15f70 65 72 74 28 20 70 42 74 20 26 26 20 70 42 74 2d ert( pBt && pBt-
15f71 3e 70 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20 >pPager );. rc
15f72 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f = sqlite3PagerNo
15f73 73 79 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72 sync(pBt->pPager
15f74 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
15f75 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
15f76 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 urn rc;.}..#if !
15f77 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
15f78 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 MIT_PAGER_PRAGMA
15f79 53 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 S) || !defined(S
15f7a 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
15f7b 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 M)./*.** Change
15f7c 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 the default page
15f7d 73 20 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e s size and the n
15f7e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 umber of reserve
15f7f 64 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65 d bytes per page
15f80 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 ..** Or, if the
15f81 70 61 67 65 20 73 69 7a 65 20 68 61 73 20 61 6c page size has al
15f82 72 65 61 64 79 20 62 65 65 6e 20 66 69 78 65 64 ready been fixed
15f83 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f , return SQLITE_
15f84 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74 READONLY .** wit
15f85 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 61 6e hout changing an
15f86 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 ything..**.** Th
15f87 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74 e page size must
15f88 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 be a power of 2
15f89 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 between 512 and
15f8a 20 36 35 35 33 36 2e 20 20 49 66 20 74 68 65 20 65536. If the
15f8b 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70 page.** size sup
15f8c 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d plied does not m
15f8d 65 65 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61 eet this constra
15f8e 69 6e 74 20 74 68 65 6e 20 74 68 65 20 70 61 67 int then the pag
15f8f 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a e size is not.**
15f90 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 changed..**.**
15f91 50 61 67 65 20 73 69 7a 65 73 20 61 72 65 20 63 Page sizes are c
15f92 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65 onstrained to be
15f93 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 a power of two
15f94 73 6f 20 74 68 61 74 20 74 68 65 20 72 65 67 69 so that the regi
15f95 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 on.** of the dat
15f96 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 abase file used
15f97 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67 for locking (beg
15f98 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e inning at PENDIN
15f99 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66 G_BYTE,.** the f
15f9a 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 irst byte past t
15f9b 68 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c he 1GB boundary,
15f9c 20 30 78 34 30 30 30 30 30 30 30 29 20 6e 65 65 0x40000000) nee
15f9d 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61 ds to occur.** a
15f9e 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
15f9f 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a of a page..**.**
15fa0 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52 If parameter nR
15fa1 65 73 65 72 76 65 20 69 73 20 6c 65 73 73 20 74 eserve is less t
15fa2 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 han zero, then t
15fa3 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 he number of res
15fa4 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70 erved.** bytes p
15fa5 65 72 20 70 61 67 65 20 69 73 20 6c 65 66 74 20 er page is left
15fa6 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a unchanged..**.**
15fa7 20 49 66 20 74 68 65 20 69 46 69 78 21 3d 30 20 If the iFix!=0
15fa8 74 68 65 6e 20 74 68 65 20 70 61 67 65 53 69 7a then the pageSiz
15fa9 65 46 69 78 65 64 20 66 6c 61 67 20 69 73 20 73 eFixed flag is s
15faa 65 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 et so that the p
15fab 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20 age size.** and
15fac 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20 autovacuum mode
15fad 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 can no longer be
15fae 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c changed..*/.SQL
15faf 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15fb0 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 sqlite3BtreeSetP
15fb1 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 ageSize(Btree *p
15fb2 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20 , int pageSize,
15fb3 69 6e 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e int nReserve, in
15fb4 74 20 69 46 69 78 29 7b 0a 20 20 69 6e 74 20 72 t iFix){. int r
15fb5 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
15fb6 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15fb7 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 p->pBt;. asser
15fb8 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20 t( nReserve>=-1
15fb9 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 && nReserve<=255
15fba 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 );. sqlite3Btr
15fbb 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 eeEnter(p);. if
15fbc 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 ( pBt->pageSizeF
15fbd 69 78 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 ixed ){. sqli
15fbe 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
15fbf 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
15fc0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
15fc1 7d 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 }. if( nReserve
15fc2 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 72 <0 ){. nReser
15fc3 76 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 ve = pBt->pageSi
15fc4 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 ze - pBt->usable
15fc5 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 Size;. }. asse
15fc6 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 rt( nReserve>=0
15fc7 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35 && nReserve<=255
15fc8 20 29 3b 0a 20 20 69 66 28 20 70 61 67 65 53 69 );. if( pageSi
15fc9 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 ze>=512 && pageS
15fca 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f ize<=SQLITE_MAX_
15fcb 50 41 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 20 PAGE_SIZE &&.
15fcc 20 20 20 20 20 28 28 70 61 67 65 53 69 7a 65 2d ((pageSize-
15fcd 31 29 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 20 1)&pageSize)==0
15fce 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 ){. assert( (
15fcf 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30 pageSize & 7)==0
15fd0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15fd1 21 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20 !pBt->pPage1 &&
15fd2 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b !pBt->pCursor );
15fd3 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 . pBt->pageSi
15fd4 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 ze = (u16)pageSi
15fd5 7a 65 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 ze;. freeTemp
15fd6 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 7d 0a Space(pBt);. }.
15fd7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
15fd8 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 gerSetPagesize(p
15fd9 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 Bt->pPager, &pBt
15fda 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73 ->pageSize, nRes
15fdb 65 72 76 65 29 3b 0a 20 20 70 42 74 2d 3e 75 73 erve);. pBt->us
15fdc 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e ableSize = pBt->
15fdd 70 61 67 65 53 69 7a 65 20 2d 20 28 75 31 36 29 pageSize - (u16)
15fde 6e 52 65 73 65 72 76 65 3b 0a 20 20 69 66 28 20 nReserve;. if(
15fdf 69 46 69 78 20 29 20 70 42 74 2d 3e 70 61 67 65 iFix ) pBt->page
15fe0 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 20 SizeFixed = 1;.
15fe1 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
15fe2 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ve(p);. return
15fe3 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
15fe4 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c urn the currentl
15fe5 79 20 64 65 66 69 6e 65 64 20 70 61 67 65 20 73 y defined page s
15fe6 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ize.*/.SQLITE_PR
15fe7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15fe8 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 3BtreeGetPageSiz
15fe9 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 e(Btree *p){. r
15fea 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61 eturn p->pBt->pa
15feb 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a geSize;.}../*.**
15fec 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
15fed 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
15fee 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 pace at the end
15fef 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 74 68 of every page th
15ff0 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 74 at.** are intent
15ff1 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e 75 73 65 ually left unuse
15ff2 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 d. This is the
15ff3 22 72 65 73 65 72 76 65 64 22 20 73 70 61 63 65 "reserved" space
15ff4 20 74 68 61 74 20 69 73 0a 2a 2a 20 73 6f 6d 65 that is.** some
15ff5 74 69 6d 65 73 20 75 73 65 64 20 62 79 20 65 78 times used by ex
15ff6 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c tensions..*/.SQL
15ff7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
15ff8 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52 sqlite3BtreeGetR
15ff9 65 73 65 72 76 65 28 42 74 72 65 65 20 2a 70 29 eserve(Btree *p)
15ffa 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c {. int n;. sql
15ffb 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
15ffc 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d );. n = p->pBt-
15ffd 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70 >pageSize - p->p
15ffe 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
15fff 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
16000 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
16001 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 n;.}../*.** Set
16002 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 the maximum pag
16003 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61 e count for a da
16004 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 67 65 tabase if mxPage
16005 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a is positive..**
16006 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 No changes are
16007 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 20 69 made if mxPage i
16008 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e s 0 or negative.
16009 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f .** Regardless o
1600a 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d f the value of m
1600b 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 xPage, return th
1600c 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 e maximum page c
1600d 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ount..*/.SQLITE_
1600e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1600f 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43 te3BtreeMaxPageC
16010 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 ount(Btree *p, i
16011 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e nt mxPage){. in
16012 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 t n;. sqlite3Bt
16013 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e reeEnter(p);. n
16014 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d = sqlite3PagerM
16015 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70 axPageCount(p->p
16016 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 Bt->pPager, mxPa
16017 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ge);. sqlite3Bt
16018 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
16019 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69 eturn n;.}.#endi
1601a 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 f /* !defined(SQ
1601b 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f LITE_OMIT_PAGER_
1601c 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 PRAGMAS) || !def
1601d 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1601e 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a _VACUUM) */../*.
1601f 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61 ** Change the 'a
16020 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 uto-vacuum' prop
16021 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61 erty of the data
16022 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75 base. If the 'au
16023 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72 toVacuum'.** par
16024 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 ameter is non-ze
16025 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61 ro, then auto-va
16026 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61 cuum mode is ena
16027 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69 bled. If zero, i
16028 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 t.** is disabled
16029 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 . The default va
1602a 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f lue for the auto
1602b 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79 -vacuum property
1602c 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e is .** determin
1602d 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 ed by the SQLITE
1602e 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 _DEFAULT_AUTOVAC
1602f 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51 UUM macro..*/.SQ
16030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16031 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
16032 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 AutoVacuum(Btree
16033 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63 *p, int autoVac
16034 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c uum){.#ifdef SQL
16035 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
16036 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c UUM. return SQL
16037 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65 ITE_READONLY;.#e
16038 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a lse. BtShared *
16039 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1603a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1603b 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75 OK;. u8 av = (u
1603c 38 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20 8)autoVacuum;..
1603d 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1603e 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74 er(p);. if( pBt
1603f 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 ->pageSizeFixed
16040 26 26 20 28 61 76 20 3f 31 3a 30 29 21 3d 70 42 && (av ?1:0)!=pB
16041 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b t->autoVacuum ){
16042 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
16043 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c _READONLY;. }el
16044 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 se{. pBt->aut
16045 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 3f 31 3a oVacuum = av ?1:
16046 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 0;. pBt->incr
16047 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d 32 20 3f Vacuum = av==2 ?
16048 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 1:0;. }. sqlit
16049 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
1604a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 . return rc;.#e
1604b 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ndif.}../*.** Re
1604c 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f turn the value o
1604d 66 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 f the 'auto-vacu
1604e 75 6d 27 20 70 72 6f 70 65 72 74 79 2e 20 49 66 um' property. If
1604f 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 auto-vacuum is
16050 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 20 69 73 .** enabled 1 is
16051 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
16052 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 wise 0..*/.SQLIT
16053 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
16054 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74 lite3BtreeGetAut
16055 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 oVacuum(Btree *p
16056 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
16057 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
16058 0a 20 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f . return BTREE_
16059 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b AUTOVACUUM_NONE;
1605a 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b .#else. int rc;
1605b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1605c 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
1605d 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e (. (!p->pBt->
1605e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 54 52 45 autoVacuum)?BTRE
1605f 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e E_AUTOVACUUM_NON
16060 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d E:. (!p->pBt-
16061 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f 42 54 52 >incrVacuum)?BTR
16062 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 EE_AUTOVACUUM_FU
16063 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 5f 41 55 LL:. BTREE_AU
16064 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20 TOVACUUM_INCR.
16065 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
16066 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
16067 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d urn rc;.#endif.}
16068 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72 .../*.** Get a r
16069 65 66 65 72 65 6e 63 65 20 74 6f 20 70 50 61 67 eference to pPag
1606a 65 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 e1 of the databa
1606b 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 77 se file. This w
1606c 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75 ill.** also acqu
1606d 69 72 65 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f ire a readlock o
1606e 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a n that file..**.
1606f 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
16070 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 returned on succ
16071 65 73 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c ess. If the fil
16072 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65 e is not a.** we
16073 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 ll-formed databa
16074 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 se file, then SQ
16075 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20 LITE_CORRUPT is
16076 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c returned..** SQL
16077 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 ITE_BUSY is retu
16078 72 6e 65 64 20 69 66 20 74 68 65 20 64 61 74 61 rned if the data
16079 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 base is locked.
1607a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a SQLITE_NOMEM.**
1607b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
1607c 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 we run out of me
1607d 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 mory. .*/.static
1607e 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42 int lockBtree(B
1607f 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
16080 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 int rc;. MemPa
16081 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e ge *pPage1;. in
16082 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 t nPage;.. asse
16083 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
16084 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
16085 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
16086 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29 pBt->pPage1==0 )
16087 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
16088 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 PagerSharedLock(
16089 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
1608a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1608b 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
1608c 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 rc = btreeGetPa
1608d 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 ge(pBt, 1, &pPag
1608e 65 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 e1, 0);. if( rc
1608f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
16090 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 turn rc;.. /* D
16091 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20 o some checking
16092 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74 to help insure t
16093 68 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65 he file we opene
16094 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a d really is. **
16095 20 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73 a valid databas
16096 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20 e file. . */.
16097 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
16098 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e rPagecount(pBt->
16099 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b pPager, &nPage);
1609a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1609b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f E_OK ){. goto
1609c 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
1609d 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 ed;. }else if(
1609e 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 69 nPage>0 ){. i
1609f 6e 74 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 nt pageSize;.
160a0 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b int usableSize;
160a1 0a 20 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d . u8 *page1 =
160a2 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a pPage1->aData;.
160a3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
160a4 4e 4f 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20 NOTADB;. if(
160a5 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d memcmp(page1, zM
160a6 61 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 21 agicHeader, 16)!
160a7 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f =0 ){. goto
160a8 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c page1_init_fail
160a9 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ed;. }. if
160aa 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b ( page1[18]>1 ){
160ab 0a 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64 . pBt->read
160ac 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a Only = 1;. }.
160ad 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39 if( page1[19
160ae 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 ]>1 ){. got
160af 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 o page1_init_fai
160b0 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 led;. }..
160b1 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 65 /* The maximum e
160b2 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e mbedded fraction
160b3 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 must be exactly
160b4 20 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20 6d 25%. And the m
160b5 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d inimum. ** em
160b6 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20 bedded fraction
160b7 6d 75 73 74 20 62 65 20 31 32 2e 35 25 20 66 6f must be 12.5% fo
160b8 72 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 61 r both leaf-data
160b9 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 and non-leaf-da
160ba 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f ta.. ** The o
160bb 72 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 61 riginal design a
160bc 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d 6f llowed these amo
160bd 75 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62 75 unts to vary, bu
160be 74 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76 t as of. ** v
160bf 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65 ersion 3.6.0, we
160c0 20 72 65 71 75 69 72 65 20 74 68 65 6d 20 74 6f require them to
160c1 20 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20 2a be fixed.. *
160c2 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 /. if( memcmp
160c3 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31 (&page1[21], "\1
160c4 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d 00\040\040",3)!=
160c5 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
160c6 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
160c7 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67 d;. }. pag
160c8 65 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 eSize = get2byte
160c9 28 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20 (&page1[16]);.
160ca 20 20 69 66 28 20 28 28 70 61 67 65 53 69 7a 65 if( ((pageSize
160cb 2d 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30 -1)&pageSize)!=0
160cc 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 32 || pageSize<512
160cd 20 7c 7c 0a 20 20 20 20 20 20 20 20 28 53 51 4c ||. (SQL
160ce 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a ITE_MAX_PAGE_SIZ
160cf 45 3c 33 32 37 36 38 20 26 26 20 70 61 67 65 53 E<32768 && pageS
160d0 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 ize>SQLITE_MAX_P
160d1 41 47 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b AGE_SIZE). ){
160d2 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 . goto page
160d3 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 1_init_failed;.
160d4 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
160d5 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d (pageSize & 7)=
160d6 3d 30 20 29 3b 0a 20 20 20 20 75 73 61 62 6c 65 =0 );. usable
160d7 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20 Size = pageSize
160d8 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20 - page1[20];.
160d9 20 69 66 28 20 70 61 67 65 53 69 7a 65 21 3d 70 if( pageSize!=p
160da 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a Bt->pageSize ){.
160db 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72 /* After r
160dc 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74 eading the first
160dd 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 page of the dat
160de 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61 abase assuming a
160df 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20 page size.
160e0 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e ** of BtShared.
160e1 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76 pageSize, we hav
160e2 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 e discovered tha
160e3 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 t the page-size
160e4 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75 is. ** actu
160e5 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55 ally pageSize. U
160e6 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
160e7 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 se, leave pBt->p
160e8 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a Page1 at. *
160e9 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 * zero and retur
160ea 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65 n SQLITE_OK. The
160eb 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c caller will cal
160ec 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a l this function.
160ed 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77 ** again w
160ee 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20 ith the correct
160ef 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20 page-size..
160f0 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 */. releas
160f1 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 ePage(pPage1);.
160f2 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 pBt->usable
160f3 53 69 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 Size = (u16)usab
160f4 6c 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42 leSize;. pB
160f5 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 t->pageSize = (u
160f6 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 16)pageSize;.
160f7 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 freeTempSpace
160f8 28 70 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20 (pBt);. rc
160f9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
160fa 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 tPagesize(pBt->p
160fb 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 Pager, &pBt->pag
160fc 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 eSize,.
160fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
160fe 20 20 20 20 20 20 20 20 20 20 70 61 67 65 53 69 pageSi
160ff 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a ze-usableSize);.
16100 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
16101 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 . }. if( u
16102 73 61 62 6c 65 53 69 7a 65 3c 34 38 30 20 29 7b sableSize<480 ){
16103 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 . goto page
16104 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 1_init_failed;.
16105 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61 }. pBt->pa
16106 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 geSize = (u16)pa
16107 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d geSize;. pBt-
16108 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 >usableSize = (u
16109 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 16)usableSize;.#
1610a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1610b 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
1610c 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 pBt->autoVacuu
1610d 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 m = (get4byte(&p
1610e 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f age1[36 + 4*4])?
1610f 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 1:0);. pBt->i
16110 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 ncrVacuum = (get
16111 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 4byte(&page1[36
16112 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 + 7*4])?1:0);.#e
16113 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d ndif. }.. /* m
16114 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d axLocal is the m
16115 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 aximum amount of
16116 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 payload to stor
16117 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 e locally for.
16118 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 ** a cell. Make
16119 20 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c sure it is smal
1611a 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 l enough so that
1611b 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e at least minFan
1611c 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 out. ** cells c
1611d 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f an will fit on o
1611e 6e 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 ne page. We ass
1611f 75 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 ume a 10-byte pa
16120 67 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 ge header.. **
16121 42 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c Besides the payl
16122 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 oad, the cell mu
16123 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 st store:. **
16124 20 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 2-byte pointe
16125 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 r to the cell.
16126 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 ** 4-byte ch
16127 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a ild pointer. **
16128 20 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 9-byte nKey
16129 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 value. **
1612a 34 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 4-byte nData val
1612b 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 ue. ** 4-by
1612c 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 te overflow page
1612d 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f pointer. ** So
1612e 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 a cell consists
1612f 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 of a 2-byte poi
16130 6e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 ner, a header wh
16131 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 ich is as much a
16132 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 s. ** 17 bytes
16133 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 long, 0 to N byt
16134 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 es of payload, a
16135 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 nd an optional 4
16136 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 byte overflow.
16137 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 ** page pointer
16138 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 .. */. pBt->ma
16139 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 xLocal = (pBt->u
1613a 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34 sableSize-12)*64
1613b 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 /255 - 23;. pBt
1613c 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42 ->minLocal = (pB
1613d 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 t->usableSize-12
1613e 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 )*32/255 - 23;.
1613f 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 pBt->maxLeaf =
16140 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
16141 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e - 35;. pBt->min
16142 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61 Leaf = (pBt->usa
16143 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 bleSize-12)*32/2
16144 35 35 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72 55 - 23;. asser
16145 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 t( pBt->maxLeaf
16146 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f + 23 <= MX_CELL_
16147 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70 SIZE(pBt) );. p
16148 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 Bt->pPage1 = pPa
16149 67 65 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ge1;. return SQ
1614a 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f LITE_OK;..page1_
1614b 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 init_failed:. r
1614c 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
1614d 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 1);. pBt->pPage
1614e 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 1 = 0;. return
1614f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 rc;.}../*.** If
16150 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 there are no out
16151 73 74 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 standing cursors
16152 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 and we are not
16153 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a in the middle.**
16154 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f of a transactio
16155 6e 20 62 75 74 20 74 68 65 72 65 20 69 73 20 61 n but there is a
16156 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 read lock on th
16157 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e e database, then
16158 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
16159 20 75 6e 72 65 66 73 20 74 68 65 20 66 69 72 73 unrefs the firs
1615a 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 t page of the da
1615b 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 63 tabase file whic
1615c 68 20 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66 h .** has the ef
1615d 66 65 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e fect of releasin
1615e 67 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e g the read lock.
1615f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
16160 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e is a transaction
16161 20 69 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68 in progress, th
16162 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
16163 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
16164 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65 void unlockBtre
16165 65 49 66 55 6e 75 73 65 64 28 42 74 53 68 61 72 eIfUnused(BtShar
16166 65 64 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65 ed *pBt){. asse
16167 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
16168 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
16169 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
1616a 70 42 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 pBt->pCursor==0
1616b 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 || pBt->inTransa
1616c 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 ction>TRANS_NONE
1616d 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 );. if( pBt->i
1616e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
1616f 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d ANS_NONE && pBt-
16170 3e 70 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 >pPage1!=0 ){.
16171 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 assert( pBt->p
16172 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a Page1->aData );.
16173 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
16174 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
16175 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31 (pBt->pPager)==1
16176 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
16177 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 pBt->pPage1->aDa
16178 74 61 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 ta );. releas
16179 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 ePage(pBt->pPage
1617a 31 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 1);. pBt->pPa
1617b 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a ge1 = 0;. }.}..
1617c 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69 /*.** If pBt poi
1617d 6e 74 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20 nts to an empty
1617e 66 69 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72 file then conver
1617f 74 20 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c t that empty fil
16180 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20 e.** into a new
16181 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 20 62 empty database b
16182 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 y initializing t
16183 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 he first page of
16184 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
16185 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
16186 6e 65 77 44 61 74 61 62 61 73 65 28 42 74 53 68 newDatabase(BtSh
16187 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65 ared *pBt){. Me
16188 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e mPage *pP1;. un
16189 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 signed char *dat
1618a 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 a;. int rc;. i
1618b 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 nt nPage;.. ass
1618c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1618d 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
1618e 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 ex) );. /* The
1618f 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 68 61 database size ha
16190 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d s already been m
16191 65 61 73 75 72 65 64 20 61 6e 64 20 63 61 63 68 easured and cach
16192 65 64 2c 20 73 6f 20 66 61 69 6c 75 72 65 0a 20 ed, so failure.
16193 20 2a 2a 20 69 73 20 69 6d 70 6f 73 73 69 62 6c ** is impossibl
16194 65 20 68 65 72 65 2e 20 20 49 66 20 74 68 65 20 e here. If the
16195 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 20 6d 65 original size me
16196 61 73 75 72 65 6d 65 6e 74 20 66 61 69 6c 65 64 asurement failed
16197 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 70 72 6f 63 , then. ** proc
16198 65 73 73 69 6e 67 20 61 62 6f 72 74 73 20 62 65 essing aborts be
16199 66 6f 72 65 20 65 6e 74 65 72 69 6e 67 20 74 68 fore entering th
1619a 69 73 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 is routine. */.
1619b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1619c 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d erPagecount(pBt-
1619d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 >pPager, &nPage)
1619e 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 ;. if( NEVER(rc
1619f 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20 !=SQLITE_OK) ||
161a0 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 72 nPage>0 ){. r
161a1 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
161a2 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 pP1 = pBt->pPage
161a3 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 31 1;. assert( pP1
161a4 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 !=0 );. data =
161a5 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 pP1->aData;. rc
161a6 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
161a7 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61 67 rite(pP1->pDbPag
161a8 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 e);. if( rc ) r
161a9 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 eturn rc;. memc
161aa 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 py(data, zMagicH
161ab 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d eader, sizeof(zM
161ac 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20 20 agicHeader));.
161ad 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 7a assert( sizeof(z
161ae 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31 36 MagicHeader)==16
161af 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 );. put2byte(&
161b0 64 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70 data[16], pBt->p
161b1 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 ageSize);. data
161b2 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 [18] = 1;. data
161b3 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65 [19] = 1;. asse
161b4 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 rt( pBt->usableS
161b5 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 ize<=pBt->pageSi
161b6 7a 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c ze && pBt->usabl
161b7 65 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e eSize+255>=pBt->
161b8 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 pageSize);. dat
161b9 61 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74 a[20] = (u8)(pBt
161ba 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 ->pageSize - pBt
161bb 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20 ->usableSize);.
161bc 20 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a data[21] = 64;.
161bd 20 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b data[22] = 32;
161be 0a 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32 . data[23] = 32
161bf 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 ;. memset(&data
161c0 5b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29 [24], 0, 100-24)
161c1 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31 ;. zeroPage(pP1
161c2 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 , PTF_INTKEY|PTF
161c3 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41 _LEAF|PTF_LEAFDA
161c4 54 41 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67 TA );. pBt->pag
161c5 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a eSizeFixed = 1;.
161c6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
161c7 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
161c8 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 assert( pBt->au
161c9 74 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 toVacuum==1 || p
161ca 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d Bt->autoVacuum==
161cb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
161cc 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d Bt->incrVacuum==
161cd 31 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61 1 || pBt->incrVa
161ce 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74 cuum==0 );. put
161cf 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 4byte(&data[36 +
161d0 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f 4*4], pBt->auto
161d1 56 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62 Vacuum);. put4b
161d2 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37 yte(&data[36 + 7
161d3 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61 *4], pBt->incrVa
161d4 63 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 cuum);.#endif.
161d5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
161d6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d ;.}../*.** Attem
161d7 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65 pt to start a ne
161d8 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 w transaction. A
161d9 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
161da 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64 on.** is started
161db 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 if the second a
161dc 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65 rgument is nonze
161dd 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20 ro, otherwise a
161de 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63 read-.** transac
161df 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65 tion. If the se
161e0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
161e1 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65 2 or more and e
161e2 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e xclusive.** tran
161e3 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 saction is start
161e4 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 ed, meaning that
161e5 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 no other proces
161e6 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 s is allowed.**
161e7 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61 to access the da
161e8 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 65 78 tabase. A preex
161e9 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69 isting transacti
161ea 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a on may not be.**
161eb 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 78 63 upgraded to exc
161ec 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e lusive by callin
161ed 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 g this routine a
161ee 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74 second time - t
161ef 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74 he.** exclusivit
161f0 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b y flag only work
161f1 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e s for a new tran
161f2 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 saction..**.** A
161f3 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
161f4 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 on must be start
161f5 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 ed before attemp
161f6 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61 ting any .** cha
161f7 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 nges to the data
161f8 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 base. None of t
161f9 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
161fa 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77 tines .** will w
161fb 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61 ork unless a tra
161fc 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 nsaction is star
161fd 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a ted first:.**.**
161fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
161ff 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a eeCreateTable().
16200 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
16201 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 78 28 treeCreateIndex(
16202 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 ).** sqlite
16203 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 3BtreeClearTable
16204 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
16205 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 e3BtreeDropTable
16206 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 ().** sqlit
16207 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a e3BtreeInsert().
16208 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ** sqlite3B
16209 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20 treeDelete().**
1620a 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1620b 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a eUpdateMeta().**
1620c 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61 .** If an initia
1620d 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71 l attempt to acq
1620e 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61 uire the lock fa
1620f 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c ils because of l
16210 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a ock contention.*
16211 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 * and the databa
16212 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c se was previousl
16213 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e y unlocked, then
16214 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 invoke the busy
16215 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74 handler.** if t
16216 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75 here is one. Bu
16217 74 20 69 66 20 74 68 65 72 65 20 77 61 73 20 70 t if there was p
16218 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64 reviously a read
16219 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a -lock, do not.**
1621a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 invoke the busy
1621b 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20 handler - just
1621c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
1621d 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59 SY. SQLITE_BUSY
1621e 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 is .** returned
1621f 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 when there is a
16220 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f lready a read-lo
16221 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 ck in order to a
16222 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e void a deadlock.
16223 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 .**.** Suppose t
16224 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f here are two pro
16225 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20 cesses A and B.
16226 20 41 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f A has a read lo
16227 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20 ck and B has.**
16228 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e a reserved lock.
16229 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f B tries to pro
1622a 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76 mote to exclusiv
1622b 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 e but is blocked
1622c 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41 because.** of A
1622d 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41 's read lock. A
1622e 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74 tries to promot
1622f 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 62 75 e to reserved bu
16230 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 t is blocked by
16231 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65 B..** One or the
16232 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74 77 other of the tw
16233 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74 o processes must
16234 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 68 65 give way or the
16235 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20 re can be.** no
16236 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65 progress. By re
16237 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42 turning SQLITE_B
16238 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f USY and not invo
16239 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 63 61 king the busy ca
1623a 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41 llback.** when A
1623b 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 already has a r
1623c 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63 ead lock, we enc
1623d 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 65 ourage A to give
1623e 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a up and let B.**
1623f 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c proceed..*/.SQL
16240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
16241 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
16242 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c nTrans(Btree *p,
16243 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20 int wrflag){.
16244 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20 sqlite3 *pBlock
16245 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20 = 0;. BtShared
16246 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 *pBt = p->pBt;.
16247 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
16248 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 _OK;.. sqlite3B
16249 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
1624a 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
1624b 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
1624c 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79 btree is already
1624d 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e in a write-tran
1624e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20 saction, or it.
1624f 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69 ** is already i
16250 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 n a read-transac
16251 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d tion and a read-
16252 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a transaction. **
16253 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74 is requested, t
16254 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a his is a no-op..
16255 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e */. if( p->in
16256 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 Trans==TRANS_WRI
16257 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e TE || (p->inTran
16258 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26 s==TRANS_READ &&
16259 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20 !wrflag) ){.
1625a 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 goto trans_begu
1625b 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 n;. }.. /* Wri
1625c 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 te transactions
1625d 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 are not possible
1625e 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 on a read-only
1625f 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66 database */. if
16260 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 ( pBt->readOnly
16261 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 && wrflag ){.
16262 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 rc = SQLITE_REA
16263 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20 DONLY;. goto
16264 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d trans_begun;. }
16265 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
16266 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
16267 48 45 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68 HE. /* If anoth
16268 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 er database hand
16269 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f le has already o
1626a 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72 pened a write tr
1626b 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20 ansaction . **
1626c 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62 on this shared-b
1626d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61 tree structure a
1626e 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74 nd a second writ
1626f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e transaction is
16270 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c . ** requested,
16271 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c return SQLITE_L
16272 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 OCKED.. */. if
16273 28 20 28 77 72 66 6c 61 67 20 26 26 20 70 42 74 ( (wrflag && pBt
16274 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
16275 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c =TRANS_WRITE) ||
16276 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 pBt->isPending
16277 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20 ){. pBlock =
16278 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 pBt->pWriter->db
16279 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72 ;. }else if( wr
1627a 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74 flag>1 ){. Bt
1627b 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20 Lock *pIter;.
1627c 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e for(pIter=pBt->
1627d 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 pLock; pIter; pI
1627e 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 ter=pIter->pNext
1627f 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 ){. if( pIt
16280 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b er->pBtree!=p ){
16281 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20 . pBlock
16282 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d = pIter->pBtree-
16283 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 >db;. bre
16284 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
16285 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f }. }. if( pBlo
16286 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ck ){. sqlite
16287 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 3ConnectionBlock
16288 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b ed(p->db, pBlock
16289 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
1628a 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
1628b 43 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20 CACHE;. goto
1628c 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d trans_begun;. }
1628d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e .#endif.. /* An
1628e 79 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 y read-only or r
1628f 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 ead-write transa
16290 63 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20 ction implies a
16291 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20 read-lock on .
16292 2a 2a 20 70 61 67 65 20 31 2e 20 53 6f 20 69 66 ** page 1. So if
16293 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 72 some other shar
16294 65 64 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 20 ed-cache client
16295 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 77 72 already has a wr
16296 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f ite-lock . ** o
16297 6e 20 70 61 67 65 20 31 2c 20 74 68 65 20 74 72 n page 1, the tr
16298 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 ansaction cannot
16299 20 62 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 be opened. */.
1629a 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 rc = queryShare
1629b 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
1629c 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 p, MASTER_ROOT,
1629d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66 READ_LOCK);. if
1629e 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 ( SQLITE_OK!=rc
1629f 29 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 ) goto trans_beg
162a0 75 6e 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 un;.. do {.
162a1 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b 42 74 72 65 /* Call lockBtre
162a2 65 28 29 20 75 6e 74 69 6c 20 65 69 74 68 65 72 e() until either
162a3 20 70 42 74 2d 3e 70 50 61 67 65 31 20 69 73 20 pBt->pPage1 is
162a4 70 6f 70 75 6c 61 74 65 64 20 6f 72 0a 20 20 20 populated or.
162a5 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 ** lockBtree()
162a6 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e returns somethin
162a7 67 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c g other than SQL
162a8 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 74 72 65 ITE_OK. lockBtre
162a9 65 28 29 0a 20 20 20 20 2a 2a 20 6d 61 79 20 72 e(). ** may r
162aa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
162ab 62 75 74 20 6c 65 61 76 65 20 70 42 74 2d 3e 70 but leave pBt->p
162ac 50 61 67 65 31 20 73 65 74 20 74 6f 20 30 20 69 Page1 set to 0 i
162ad 66 20 61 66 74 65 72 0a 20 20 20 20 2a 2a 20 72 f after. ** r
162ae 65 61 64 69 6e 67 20 70 61 67 65 20 31 20 69 74 eading page 1 it
162af 20 64 69 73 63 6f 76 65 72 73 20 74 68 61 74 20 discovers that
162b0 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 the page-size of
162b1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 the database .
162b2 20 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 6e 6f ** file is no
162b3 74 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2e t pBt->pageSize.
162b4 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6c 6f In this case lo
162b5 63 6b 42 74 72 65 65 28 29 20 77 69 6c 6c 20 75 ckBtree() will u
162b6 70 64 61 74 65 0a 20 20 20 20 2a 2a 20 70 42 74 pdate. ** pBt
162b7 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f 20 74 68 ->pageSize to th
162b8 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74 e page-size of t
162b9 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e he file on disk.
162ba 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c . */. whil
162bb 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d e( pBt->pPage1==
162bc 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 0 && SQLITE_OK==
162bd 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 28 (rc = lockBtree(
162be 70 42 74 29 29 20 29 3b 0a 0a 20 20 20 20 69 66 pBt)) );.. if
162bf 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
162c0 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 && wrflag ){.
162c1 20 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 if( pBt->read
162c2 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20 Only ){.
162c3 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 rc = SQLITE_READ
162c4 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ONLY;. }els
162c5 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
162c6 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 sqlite3PagerBegi
162c7 6e 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 77 72 n(pBt->pPager,wr
162c8 66 6c 61 67 3e 31 2c 73 71 6c 69 74 65 33 54 65 flag>1,sqlite3Te
162c9 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62 mpInMemory(p->db
162ca 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ));. if(
162cb 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
162cc 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
162cd 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29 newDatabase(pBt)
162ce 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
162cf 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
162d0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
162d1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f OK ){. unlo
162d2 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 ckBtreeIfUnused(
162d3 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 pBt);. }. }w
162d4 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
162d5 5f 42 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e _BUSY && pBt->in
162d6 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
162d7 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20 NS_NONE &&.
162d8 20 20 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65 btreeInvoke
162d9 42 75 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29 BusyHandler(pBt)
162da 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 );.. if( rc==S
162db 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
162dc 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d if( p->inTrans==
162dd 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 TRANS_NONE ){.
162de 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 pBt->nTransa
162df 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 ction++;.#ifndef
162e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
162e1 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20 RED_CACHE.
162e2 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
162e3 29 7b 0a 09 61 73 73 65 72 74 28 20 70 2d 3e 6c ){..assert( p->l
162e4 6f 63 6b 2e 70 42 74 72 65 65 3d 3d 70 20 26 26 ock.pBtree==p &&
162e5 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 3d p->lock.iTable=
162e6 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d =1 );. p-
162e7 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20 3d 20 52 45 >lock.eLock = RE
162e8 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 AD_LOCK;.
162e9 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65 78 74 20 3d p->lock.pNext =
162ea 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 pBt->pLock;.
162eb 20 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 pBt->pLock
162ec 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a 20 20 20 20 = &p->lock;.
162ed 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d }.#endif. }
162ee 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 . p->inTrans
162ef 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f = (wrflag?TRANS_
162f0 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44 WRITE:TRANS_READ
162f1 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e );. if( p->in
162f2 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61 Trans>pBt->inTra
162f3 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 nsaction ){.
162f4 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 pBt->inTransac
162f5 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e tion = p->inTran
162f6 73 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 s;. }.#ifndef
162f7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
162f8 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 69 66 RED_CACHE. if
162f9 28 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 ( wrflag ){.
162fa 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e assert( !pBt->
162fb 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20 pWriter );.
162fc 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 pBt->pWriter =
162fd 70 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 73 p;. pBt->is
162fe 45 78 63 6c 75 73 69 76 65 20 3d 20 28 75 38 29 Exclusive = (u8)
162ff 28 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20 (wrflag>1);.
16300 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a 74 }.#endif. }...t
16301 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66 rans_begun:. if
16302 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16303 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 && wrflag ){.
16304 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61 /* This call ma
16305 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 74 68 kes sure that th
16306 65 20 70 61 67 65 72 20 68 61 73 20 74 68 65 20 e pager has the
16307 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f correct number o
16308 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61 f. ** open sa
16309 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65 vepoints. If the
1630a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
1630b 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 r is greater tha
1630c 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 n 0 and. ** t
1630d 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 he sub-journal i
1630e 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 s not already op
1630f 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c en, then it will
16310 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e be opened here.
16311 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
16312 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 sqlite3PagerOpe
16313 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e nSavepoint(pBt->
16314 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e pPager, p->db->n
16315 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a Savepoint);. }.
16316 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 . btreeIntegrit
16317 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 y(p);. sqlite3B
16318 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
16319 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 return rc;.}..#i
1631a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1631b 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a T_AUTOVACUUM../*
1631c 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e .** Set the poin
1631d 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 ter-map entries
1631e 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e for all children
1631f 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e 20 of page pPage.
16320 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67 Also, if.** pPag
16321 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73 e contains cells
16322 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f that point to o
16323 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73 verflow pages, s
16324 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a et the pointer.*
16325 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f * map entries fo
16326 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 r the overflow p
16327 61 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f ages as well..*/
16328 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 43 .static int setC
16329 68 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50 hildPtrmaps(MemP
1632a 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 age *pPage){. i
1632b 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
1632c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1632d 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72 /* Counter var
1632e 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e iable */. int n
1632f 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Cell;
16330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16331 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 Number of cells
16332 20 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a in page pPage *
16333 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
16334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16335 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
16336 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61 code */. BtSha
16337 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
16338 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e ->pBt;. u8 isIn
16339 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e itOrig = pPage->
1633a 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70 isInit;. Pgno p
1633b 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e gno = pPage->pgn
1633c 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 o;.. assert( sq
1633d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1633e 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
1633f 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74 ex) );. rc = bt
16340 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 reeInitPage(pPag
16341 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 e);. if( rc!=SQ
16342 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 LITE_OK ){. g
16343 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74 oto set_child_pt
16344 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20 rmaps_out;. }.
16345 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e nCell = pPage->
16346 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d nCell;.. for(i=
16347 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 0; i<nCell; i++)
16348 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 {. u8 *pCell
16349 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
1634a 2c 20 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61 , i);.. ptrma
1634b 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 pPutOvflPtr(pPag
1634c 65 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a e, pCell, &rc);.
1634d 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
1634e 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50 >leaf ){. P
1634f 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20 gno childPgno =
16350 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b get4byte(pCell);
16351 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 . ptrmapPut
16352 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c (pBt, childPgno,
16353 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 PTRMAP_BTREE, p
16354 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d gno, &rc);. }
16355 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 . }.. if( !pPa
16356 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ge->leaf ){.
16357 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d Pgno childPgno =
16358 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 get4byte(&pPage
16359 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
1635a 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 drOffset+8]);.
1635b 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c ptrmapPut(pBt,
1635c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d childPgno, PTRM
1635d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 AP_BTREE, pgno,
1635e 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63 &rc);. }..set_c
1635f 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 hild_ptrmaps_out
16360 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 :. pPage->isIni
16361 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a t = isInitOrig;.
16362 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
16363 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20 /*.** Somewhere
16364 6f 6e 20 70 50 61 67 65 20 69 73 20 61 20 70 6f on pPage is a po
16365 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 46 inter to page iF
16366 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69 rom. Modify thi
16367 73 20 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a 20 s pointer so.**
16368 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 that it points t
16369 6f 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72 o iTo. Parameter
1636a 20 65 54 79 70 65 20 64 65 73 63 72 69 62 65 73 eType describes
1636b 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69 the type of poi
1636c 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f nter to.** be mo
1636d 64 69 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c dified, as foll
1636e 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 ows:.**.** PTRMA
1636f 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50 61 P_BTREE: pPa
16370 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61 ge is a btree-pa
16371 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 ge. The pointer
16372 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c points at a chil
16373 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 d .**
16374 20 20 20 20 20 20 20 20 70 61 67 65 20 6f 66 20 page of
16375 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 pPage..**.** PTR
16376 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70 MAP_OVERFLOW1: p
16377 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d Page is a btree-
16378 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 page. The pointe
16379 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f r points at an o
1637a 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 verflow.**
1637b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 pag
1637c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 e pointed to by
1637d 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 one of the cells
1637e 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a on pPage..**.**
1637f 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
16380 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f 2: pPage is an o
16381 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68 verflow-page. Th
16382 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 e pointer points
16383 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 at the next.**
16384 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16385 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 overflow page
16386 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a in the list..*/.
16387 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66 static int modif
16388 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d yPagePointer(Mem
16389 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e Page *pPage, Pgn
1638a 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54 o iFrom, Pgno iT
1638b 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20 o, u8 eType){.
1638c 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1638d 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
1638e 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
1638f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16390 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
16391 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
16392 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 e) );. if( eTyp
16393 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c e==PTRMAP_OVERFL
16394 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 OW2 ){. /* Th
16395 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 e pointer is alw
16396 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34 20 ays the first 4
16397 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 bytes of the pag
16398 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 e in this case.
16399 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 */. if( get4
1639a 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 byte(pPage->aDat
1639b 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 a)!=iFrom ){.
1639c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1639d 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1639e 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 }. put4byt
1639f 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 e(pPage->aData,
163a0 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 iTo);. }else{.
163a1 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 u8 isInitOrig
163a2 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 = pPage->isInit
163a3 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 ;. int i;.
163a4 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 int nCell;..
163a5 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 btreeInitPage(p
163a6 50 61 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c 6c Page);. nCell
163a7 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b = pPage->nCell;
163a8 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 .. for(i=0; i
163a9 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 <nCell; i++){.
163aa 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 u8 *pCell =
163ab 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
163ac 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 i);. if( eT
163ad 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 ype==PTRMAP_OVER
163ae 46 4c 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 20 FLOW1 ){.
163af 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
163b0 20 20 20 20 20 20 20 20 62 74 72 65 65 50 61 72 btreePar
163b1 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
163b2 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
163b3 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f if( info
163b4 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 .iOverflow ){.
163b5 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f if( iFro
163b6 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65 m==get4byte(&pCe
163b7 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
163b8 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 w]) ){.
163b9 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65 put4byte(&pCe
163ba 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f ll[info.iOverflo
163bb 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 w], iTo);.
163bc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
163bd 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
163be 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
163bf 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 34 if( get4
163c0 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 byte(pCell)==iFr
163c1 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 om ){.
163c2 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 put4byte(pCell,
163c3 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 iTo);.
163c4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d break;. }
163c5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
163c6 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 . if( i==nCe
163c7 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
163c8 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 eType!=PTRMAP_BT
163c9 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 REE || .
163ca 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 get4byte(&pPag
163cb 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e e->aData[pPage->
163cc 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 hdrOffset+8])!=i
163cd 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 From ){.
163ce 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
163cf 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 RRUPT_BKPT;.
163d0 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79 }. put4by
163d1 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
163d2 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
163d3 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 t+8], iTo);.
163d4 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 }.. pPage->is
163d5 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 Init = isInitOri
163d6 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 g;. }. return
163d7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
163d8 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 *.** Move the op
163d9 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 en database page
163da 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 pDbPage to loca
163db 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69 tion iFreePage i
163dc 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 n the .** databa
163dd 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20 se. The pDbPage
163de 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e reference remain
163df 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 s valid..**.** T
163e0 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 he isCommit flag
163e1 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
163e2 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
163e3 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61 to remember tha
163e4 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c t.** the journal
163e5 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e needs to be syn
163e6 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74 c()ed before dat
163e7 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 abase page pDbPa
163e8 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 61 6e ge->pgno .** can
163e9 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 be written to.
163ea 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61 The caller has a
163eb 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20 lready promised
163ec 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 not to write to
163ed 74 68 61 74 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2f that.** page..*/
163ee 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f .static int relo
163ef 63 61 74 65 50 61 67 65 28 0a 20 20 42 74 53 68 catePage(. BtSh
163f0 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 ared *pBt,
163f1 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f /* Btree */
163f2 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50 . MemPage *pDbP
163f3 61 67 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f age, /* O
163f4 70 65 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 pen page to move
163f5 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 */. u8 eType,
163f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
163f7 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74 * Pointer map 't
163f8 79 70 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70 ype' entry for p
163f9 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f DbPage */. Pgno
163fa 20 69 50 74 72 50 61 67 65 2c 20 20 20 20 20 20 iPtrPage,
163fb 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
163fc 6d 61 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e map 'page-no' en
163fd 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 try for pDbPage
163fe 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 */. Pgno iFreeP
163ff 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a age, /*
16400 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f The location to
16401 20 6d 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f move pDbPage to
16402 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d */. int isComm
16403 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f it /
16404 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 * isCommit flag
16405 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
16406 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 20 2a 3PagerMovepage *
16407 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a /.){. MemPage *
16408 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54 pPtrPage; /* T
16409 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e he page that con
1640a 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 tains a pointer
1640b 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 to pDbPage */.
1640c 50 67 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70 Pgno iDbPage = p
1640d 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20 DbPage->pgno;.
1640e 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
1640f 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 pBt->pPager;. i
16410 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
16411 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
16412 4f 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79 OVERFLOW2 || eTy
16413 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 pe==PTRMAP_OVERF
16414 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65 LOW1 || . e
16415 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 Type==PTRMAP_BTR
16416 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 EE || eType==PTR
16417 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a MAP_ROOTPAGE );.
16418 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16419 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
1641a 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
1641b 73 65 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70 sert( pDbPage->p
1641c 42 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a Bt==pBt );.. /*
1641d 20 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61 Move page iDbPa
1641e 67 65 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72 ge from its curr
1641f 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 ent location to
16420 70 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65 page number iFre
16421 65 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45 ePage */. TRACE
16422 28 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d (("AUTOVACUUM: M
16423 6f 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65 oving %d to free
16424 20 70 61 67 65 20 25 64 20 28 70 74 72 20 70 61 page %d (ptr pa
16425 67 65 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e ge %d type %d)\n
16426 22 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61 67 ", . iDbPag
16427 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 50 e, iFreePage, iP
16428 74 72 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b trPage, eType));
16429 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
1642a 61 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61 agerMovepage(pPa
1642b 67 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44 ger, pDbPage->pD
1642c 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 bPage, iFreePage
1642d 2c 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69 , isCommit);. i
1642e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1642f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
16430 63 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65 c;. }. pDbPage
16431 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61 ->pgno = iFreePa
16432 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62 ge;.. /* If pDb
16433 50 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65 Page was a btree
16434 2d 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d -page, then it m
16435 61 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61 ay have child pa
16436 67 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73 ges and/or cells
16437 0a 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 . ** that point
16438 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 to overflow pag
16439 65 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 es. The pointer
1643a 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 map entries for
1643b 61 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70 all these. ** p
1643c 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 ages need to be
1643d 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20 changed.. **.
1643e 2a 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73 ** If pDbPage is
1643f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 an overflow pag
16440 65 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 e, then the firs
16441 74 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74 t 4 bytes may st
16442 6f 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74 ore a. ** point
16443 65 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65 er to a subseque
16444 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 nt overflow page
16445 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 . If this is the
16446 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a case, then. **
16447 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 the pointer map
16448 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64 needs to be upd
16449 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62 ated for the sub
1644a 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 sequent overflow
1644b 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 page.. */. if
1644c 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
1644d 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d BTREE || eType==
1644e 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 PTRMAP_ROOTPAGE
1644f 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43 ){. rc = setC
16450 68 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50 hildPtrmaps(pDbP
16451 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
16452 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
16453 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
16454 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
16455 20 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c Pgno nextOvfl
16456 20 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50 = get4byte(pDbP
16457 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 age->aData);.
16458 20 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30 if( nextOvfl!=0
16459 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 ){. ptrmap
1645a 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 Put(pBt, nextOvf
1645b 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c l, PTRMAP_OVERFL
1645c 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 2c 20 OW2, iFreePage,
1645d 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 &rc);. if(
1645e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1645f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
16460 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
16461 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20 }. }.. /* Fix
16462 74 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69 the database poi
16463 6e 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74 nter on page iPt
16464 72 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74 rPage that point
16465 65 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f ed at iDbPage so
16466 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f . ** that it po
16467 69 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67 ints at iFreePag
16468 65 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20 e. Also fix the
16469 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 pointer map entr
1646a 79 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50 y for. ** iPtrP
1646b 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 age.. */. if(
1646c 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f eType!=PTRMAP_RO
1646d 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 OTPAGE ){. rc
1646e 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
1646f 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26 pBt, iPtrPage, &
16470 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20 pPtrPage, 0);.
16471 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16472 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
16473 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
16474 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
16475 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67 gerWrite(pPtrPag
16476 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
16477 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16478 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 OK ){. rele
16479 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65 asePage(pPtrPage
1647a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1647b 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 rc;. }. rc
1647c 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 = modifyPagePoi
1647d 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69 nter(pPtrPage, i
1647e 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 DbPage, iFreePag
1647f 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72 e, eType);. r
16480 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50 eleasePage(pPtrP
16481 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
16482 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
16483 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
16484 42 74 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65 Bt, iFreePage, e
16485 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 Type, iPtrPage,
16486 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a &rc);. }. }.
16487 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
16488 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 /* Forward decla
16489 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20 ration required
1648a 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 by incrVacuumSte
1648b 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 p(). */.static i
1648c 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 nt allocateBtree
1648d 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c Page(BtShared *,
1648e 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e MemPage **, Pgn
1648f 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a o *, Pgno, u8);.
16490 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 ./*.** Perform a
16491 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20 single step of
16492 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 an incremental-v
16493 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73 acuum. If succes
16494 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 sful,.** return
16495 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 SQLITE_OK. If th
16496 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74 ere is no work t
16497 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66 o do (and theref
16498 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20 ore no.** point
16499 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 in calling this
1649a 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c function again),
1649b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
1649c 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20 ONE..**.** More
1649d 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73 specificly, this
1649e 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 function attemp
1649f 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a ts to re-organiz
164a0 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 e the .** databa
164a1 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c se so that the l
164a2 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 ast page of the
164a3 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 file currently i
164a4 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c n use.** is no l
164a5 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a onger in use..**
164a6 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20 .** If the nFin
164a7 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e parameter is non
164a8 2d 7a 65 72 6f 2c 20 74 68 69 73 20 66 75 6e 63 -zero, this func
164a9 74 69 6f 6e 20 61 73 73 75 6d 65 73 0a 2a 2a 20 tion assumes.**
164aa 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 that the caller
164ab 77 69 6c 6c 20 6b 65 65 70 20 63 61 6c 6c 69 6e will keep callin
164ac 67 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 g incrVacuumStep
164ad 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 69 74 20 72 () until.** it r
164ae 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f eturns SQLITE_DO
164af 4e 45 20 6f 72 20 61 6e 20 65 72 72 6f 72 2c 20 NE or an error,
164b0 61 6e 64 20 74 68 61 74 20 6e 46 69 6e 20 69 73 and that nFin is
164b1 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
164b2 66 20 70 61 67 65 73 20 74 68 65 20 64 61 74 61 f pages the data
164b3 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 63 base file will c
164b4 6f 6e 74 61 69 6e 20 61 66 74 65 72 20 74 68 69 ontain after thi
164b5 73 20 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73 s .** process is
164b6 20 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 6e complete. If n
164b7 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 69 74 20 Fin is zero, it
164b8 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a is assumed that.
164b9 2a 2a 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 ** incrVacuumSte
164ba 70 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c p() will be call
164bb 65 64 20 61 20 66 69 6e 69 74 65 20 61 6d 6f 75 ed a finite amou
164bc 6e 74 20 6f 66 20 74 69 6d 65 73 0a 2a 2a 20 77 nt of times.** w
164bd 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 79 20 hich may or may
164be 6e 6f 74 20 65 6d 70 74 79 20 74 68 65 20 66 72 not empty the fr
164bf 65 65 6c 69 73 74 2e 20 20 41 20 66 75 6c 6c 20 eelist. A full
164c0 61 75 74 6f 76 61 63 75 75 6d 0a 2a 2a 20 68 61 autovacuum.** ha
164c1 73 20 6e 46 69 6e 3e 30 2e 20 20 41 20 22 50 52 s nFin>0. A "PR
164c2 41 47 4d 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c AGMA incremental
164c3 5f 76 61 63 75 75 6d 22 20 68 61 73 20 6e 46 69 _vacuum" has nFi
164c4 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 n==0..*/.static
164c5 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74 int incrVacuumSt
164c6 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ep(BtShared *pBt
164c7 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e , Pgno nFin, Pgn
164c8 6f 20 69 4c 61 73 74 50 67 29 7b 0a 20 20 50 67 o iLastPg){. Pg
164c9 6e 6f 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20 no nFreeList;
164ca 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
164cb 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c r of pages still
164cc 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 on the free-lis
164cd 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
164ce 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
164cf 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
164d0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 61 73 ;. assert( iLas
164d1 74 50 67 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69 tPg>nFin );.. i
164d2 66 28 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47 f( !PTRMAP_ISPAG
164d3 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 E(pBt, iLastPg)
164d4 26 26 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44 && iLastPg!=PEND
164d5 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
164d6 74 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 t) ){. int rc
164d7 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a ;. u8 eType;.
164d8 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 Pgno iPtrPag
164d9 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73 e;.. nFreeLis
164da 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42 t = get4byte(&pB
164db 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
164dc 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e [36]);. if( n
164dd 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 FreeList==0 ){.
164de 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
164df 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a TE_DONE;. }..
164e0 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 rc = ptrmapG
164e1 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c et(pBt, iLastPg,
164e2 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61 &eType, &iPtrPa
164e3 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
164e4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
164e5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
164e6 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79 }. if( eTy
164e7 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe==PTRMAP_ROOTP
164e8 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 AGE ){. ret
164e9 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
164ea 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a PT_BKPT;. }..
164eb 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
164ec 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 TRMAP_FREEPAGE )
164ed 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e {. if( nFin
164ee 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ==0 ){. /
164ef 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 * Remove the pag
164f0 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73 e from the files
164f1 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73 free-list. This
164f2 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 is not required
164f3 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e . ** if n
164f4 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e Fin is non-zero.
164f5 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74 In that case, t
164f6 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c he free-list wil
164f7 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 l be. **
164f8 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 truncated to zer
164f9 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e o after this fun
164fa 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73 ction returns, s
164fb 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20 o it doesn't .
164fc 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20 ** matter
164fd 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74 if it still cont
164fe 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67 ains some garbag
164ff 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 e entries..
16500 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67 */. Pg
16501 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20 no iFreePg;.
16502 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 MemPage *pFr
16503 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 eePg;. rc
16504 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 = allocateBtree
16505 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 Page(pBt, &pFree
16506 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c Pg, &iFreePg, iL
16507 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20 astPg, 1);.
16508 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
16509 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1650a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
1650b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
1650c 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d ssert( iFreePg==
1650d 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 iLastPg );.
1650e 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1650f 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d FreePg);. }
16510 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 . } else {.
16511 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 Pgno iFreePg
16512 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
16513 20 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70 Index of free p
16514 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73 age to move pLas
16515 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20 tPg to */.
16516 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67 MemPage *pLastPg
16517 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 ;.. rc = bt
16518 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
16519 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50 iLastPg, &pLastP
1651a 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 g, 0);. if(
1651b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1651c 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
1651d 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 rc;. }..
1651e 20 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69 /* If nFin i
1651f 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f s zero, this loo
16520 70 20 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f p runs exactly o
16521 6e 63 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61 nce and page pLa
16522 73 74 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73 stPg. ** is
16523 20 73 77 61 70 70 65 64 20 77 69 74 68 20 74 68 swapped with th
16524 65 20 66 69 72 73 74 20 66 72 65 65 20 70 61 67 e first free pag
16525 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65 e pulled off the
16526 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20 free list..
16527 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e **. ** On
16528 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c the other hand,
16529 20 69 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61 if nFin is grea
1652a 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 ter than zero, t
1652b 68 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a hen keep. *
1652c 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20 * looping until
1652d 61 20 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61 a free-page loca
1652e 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 66 ted within the f
1652f 69 72 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a irst nFin pages.
16530 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 ** of the
16531 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20 file is found..
16532 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f */. do
16533 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 {. MemPa
16534 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 ge *pFreePg;.
16535 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 rc = alloca
16536 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
16537 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 &pFreePg, &iFre
16538 65 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 ePg, 0, 0);.
16539 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1653a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1653b 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1653c 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20 LastPg);.
1653d 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1653e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1653f 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65 releasePage(pFre
16540 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 ePg);. }whi
16541 6c 65 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69 le( nFin!=0 && i
16542 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20 FreePg>nFin );.
16543 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72 assert( iFr
16544 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a eePg<iLastPg );.
16545 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 20 . rc
16546 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
16547 69 74 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62 ite(pLastPg->pDb
16548 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Page);. if(
16549 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1654a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 {. rc = r
1654b 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c elocatePage(pBt,
1654c 20 70 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c pLastPg, eType,
1654d 20 69 50 74 72 50 61 67 65 2c 20 69 46 72 65 65 iPtrPage, iFree
1654e 50 67 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20 Pg, nFin!=0);.
1654f 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 }. rele
16550 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 asePage(pLastPg)
16551 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
16552 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16553 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
16554 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
16555 7d 0a 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30 }.. if( nFin==0
16556 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d ){. iLastPg-
16557 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c -;. while( iL
16558 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42 astPg==PENDING_B
16559 59 54 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50 YTE_PAGE(pBt)||P
1655a 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
1655b 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 , iLastPg) ){.
1655c 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 if( PTRMAP_I
1655d 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 SPAGE(pBt, iLast
1655e 50 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 4d Pg) ){. M
1655f 65 6d 50 61 67 65 20 2a 70 50 67 3b 0a 20 20 20 emPage *pPg;.
16560 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 74 int rc = bt
16561 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
16562 69 4c 61 73 74 50 67 2c 20 26 70 50 67 2c 20 30 iLastPg, &pPg, 0
16563 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
16564 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
16565 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
16566 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
16567 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
16568 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
16569 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 g->pDbPage);.
1656a 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
1656b 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69 (pPg);. i
1656c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1656d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
1656e 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
1656f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
16570 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 iLastPg--;.
16571 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 }. sqlite3Pag
16572 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 erTruncateImage(
16573 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c 61 pBt->pPager, iLa
16574 73 74 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 stPg);. }. ret
16575 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
16576 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d ../*.** A write-
16577 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 transaction must
16578 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 be opened befor
16579 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 e calling this f
1657a 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 70 unction..** It p
1657b 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c 65 erforms a single
1657c 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 6f unit of work to
1657d 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 6d 65 wards an increme
1657e 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a 0a ntal vacuum..**.
1657f 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 65 6d ** If the increm
16580 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 73 20 ental vacuum is
16581 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 20 74 finished after t
16582 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 his function has
16583 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f run,.** SQLITE_
16584 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 DONE is returned
16585 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 66 . If it is not f
16586 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e 6f 20 inished, but no
16587 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 0a error occurred,.
16588 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
16589 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 returned. Otherw
1658a 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72 ise an SQLite er
1658b 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51 ror code. .*/.SQ
1658c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1658d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 sqlite3BtreeInc
1658e 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70 rVacuum(Btree *p
1658f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
16590 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
16591 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 ->pBt;.. sqlite
16592 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
16593 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
16594 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
16595 41 4e 53 5f 57 52 49 54 45 20 26 26 20 70 2d 3e ANS_WRITE && p->
16596 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
16597 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 21 70 RITE );. if( !p
16598 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 Bt->autoVacuum )
16599 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1659a 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b E_DONE;. }else{
1659b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 . invalidateA
1659c 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 llOverflowCache(
1659d 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 69 pBt);. rc = i
1659e 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42 ncrVacuumStep(pB
1659f 74 2c 20 30 2c 20 70 61 67 65 72 50 61 67 65 63 t, 0, pagerPagec
165a0 6f 75 6e 74 28 70 42 74 29 29 3b 0a 20 20 7d 0a ount(pBt));. }.
165a1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
165a2 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
165a3 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
165a4 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
165a5 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71 lled prior to sq
165a6 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
165a7 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74 when a transact
165a8 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 ion.** is commit
165a9 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76 ed for an auto-v
165aa 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a acuum database..
165ab 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f **.** If SQLITE_
165ac 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 OK is returned,
165ad 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73 then *pnTrunc is
165ae 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 set to the numb
165af 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74 er of pages.** t
165b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
165b1 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63 should be trunc
165b2 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74 ated to during t
165b3 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 he commit proces
165b4 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 s. .** i.e. the
165b5 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65 database has bee
165b6 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f n reorganized so
165b7 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66 that only the f
165b8 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a irst *pnTrunc.**
165b9 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73 pages are in us
165ba 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
165bb 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 autoVacuumCommi
165bc 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 t(BtShared *pBt)
165bd 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
165be 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 ITE_OK;. Pager
165bf 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 *pPager = pBt->p
165c0 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c Pager;. VVA_ONL
165c1 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71 Y( int nRef = sq
165c2 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
165c3 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20 nt(pPager) );..
165c4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
165c5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
165c6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76 >mutex) );. inv
165c7 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c alidateAllOverfl
165c8 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 owCache(pBt);.
165c9 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f assert(pBt->auto
165ca 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21 Vacuum);. if( !
165cb 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 pBt->incrVacuum
165cc 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e ){. Pgno nFin
165cd 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
165ce 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
165cf 64 61 74 61 62 61 73 65 20 61 66 74 65 72 20 61 database after a
165d0 75 74 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a utovacuuming */.
165d1 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20 Pgno nFree;
165d2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
165d3 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 of pages on the
165d4 20 66 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61 freelist initia
165d5 6c 6c 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 lly */. Pgno
165d6 6e 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a nPtrmap; /*
165d7 20 4e 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61 Number of PtrMa
165d8 70 20 70 61 67 65 73 20 74 6f 20 62 65 20 66 72 p pages to be fr
165d9 65 65 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 eed */. Pgno
165da 69 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a iFree; /*
165db 20 54 68 65 20 6e 65 78 74 20 70 61 67 65 20 74 The next page t
165dc 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 o be freed */.
165dd 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 int nEntry;
165de 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
165df 66 20 65 6e 74 72 69 65 73 20 6f 6e 20 6f 6e 65 f entries on one
165e0 20 70 74 72 6d 61 70 20 70 61 67 65 20 2a 2f 0a ptrmap page */.
165e1 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 Pgno nOrig;
165e2 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
165e3 73 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66 se size before f
165e4 72 65 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e reeing */.. n
165e5 4f 72 69 67 20 3d 20 70 61 67 65 72 50 61 67 65 Orig = pagerPage
165e6 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 count(pBt);.
165e7 69 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 if( PTRMAP_ISPAG
165e8 45 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c E(pBt, nOrig) ||
165e9 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f nOrig==PENDING_
165ea 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
165eb 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 {. /* It is
165ec 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f not possible to
165ed 20 63 72 65 61 74 65 20 61 20 64 61 74 61 62 61 create a databa
165ee 73 65 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 se for which the
165ef 20 66 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20 final page.
165f0 20 20 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 ** is either a
165f1 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 pointer-map pag
165f2 65 20 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67 e or the pending
165f3 2d 62 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f -byte page. If o
165f4 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 ne. ** is e
165f5 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73 ncountered, this
165f6 20 69 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75 indicates corru
165f7 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a ption.. */.
165f8 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
165f9 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
165fa 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72 ;. }.. nFr
165fb 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 ee = get4byte(&p
165fc 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
165fd 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e 45 6e 74 a[36]);. nEnt
165fe 72 79 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 ry = pBt->usable
165ff 53 69 7a 65 2f 35 3b 0a 20 20 20 20 6e 50 74 72 Size/5;. nPtr
16600 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72 map = (nFree-nOr
16601 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f ig+PTRMAP_PAGENO
16602 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e (pBt, nOrig)+nEn
16603 74 72 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 20 try)/nEntry;.
16604 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 nFin = nOrig -
16605 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b nFree - nPtrmap;
16606 0a 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50 . if( nOrig>P
16607 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
16608 28 70 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45 (pBt) && nFin<PE
16609 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
1660a 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 pBt) ){. nF
1660b 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 in--;. }.
1660c 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 while( PTRMAP_IS
1660d 50 41 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20 PAGE(pBt, nFin)
1660e 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47 || nFin==PENDING
1660f 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
16610 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b ){. nFin--;
16611 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
16612 46 69 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75 Fin>nOrig ) retu
16613 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
16614 54 5f 42 4b 50 54 3b 0a 0a 20 20 20 20 66 6f 72 T_BKPT;.. for
16615 28 69 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46 (iFree=nOrig; iF
16616 72 65 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d ree>nFin && rc==
16617 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65 SQLITE_OK; iFree
16618 2d 2d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 --){. rc =
16619 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 incrVacuumStep(p
1661a 42 74 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29 Bt, nFin, iFree)
1661b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1661c 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 (rc==SQLITE_DONE
1661d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f || rc==SQLITE_O
1661e 4b 29 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b K) && nFree>0 ){
1661f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
16620 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20 TE_OK;. rc
16621 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
16622 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d ite(pBt->pPage1-
16623 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
16624 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e put4byte(&pBt->
16625 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
16626 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74 ], 0);. put
16627 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 4byte(&pBt->pPag
16628 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30 e1->aData[36], 0
16629 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1662a 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 PagerTruncateIma
1662b 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ge(pBt->pPager,
1662c 6e 46 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 nFin);. }.
1662d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1662e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
1662f 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
16630 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a (pPager);. }.
16631 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e }.. assert( n
16632 52 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 Ref==sqlite3Page
16633 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 rRefcount(pPager
16634 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ) );. return rc
16635 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 ;.}..#else /* if
16636 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16637 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23 _AUTOVACUUM */.#
16638 20 64 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64 define setChild
16639 50 74 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54 Ptrmaps(x) SQLIT
1663a 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a E_OK.#endif../*.
1663b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1663c 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 does the first p
1663d 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68 hase of a two-ph
1663e 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69 ase commit. Thi
1663f 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75 s routine.** cau
16640 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a ses a rollback j
16641 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65 ournal to be cre
16642 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73 ated (if it does
16643 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 not already exi
16644 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c st).** and popul
16645 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68 ated with enough
16646 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20 information so
16647 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20 that if a power
16648 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74 loss occurs.** t
16649 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 he database can
1664a 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 be restored to i
1664b 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 ts original stat
1664c 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63 e by playing bac
1664d 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c k.** the journal
1664e 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74 . Then the cont
1664f 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 ents of the jour
16650 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20 nal are flushed
16651 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69 out to.** the di
16652 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a sk. After the j
16653 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79 ournal is safely
16654 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63 on oxide, the c
16655 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a hanges to the.**
16656 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72 database are wr
16657 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64 itten into the d
16658 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
16659 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64 flushed to oxid
1665a 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 e..** At the end
1665b 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74 of this call, t
1665c 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 he rollback jour
1665d 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73 nal still exists
1665e 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20 on the.** disk
1665f 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c and we are still
16660 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63 holding all loc
16661 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73 ks, so the trans
16662 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a action has not.*
16663 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65 * committed. Se
16664 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f e sqlite3BtreeCo
16665 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 66 mmitPhaseTwo() f
16666 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 or the second ph
16667 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f ase of the.** co
16668 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a mmit process..**
16669 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 .** This call is
1666a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77 a no-op if no w
1666b 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
1666c 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63 is currently ac
1666d 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a tive on pBt..**.
1666e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79 ** Otherwise, sy
1666f 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 nc the database
16670 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72 file for the btr
16671 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20 ee pBt. zMaster
16672 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 points to.** the
16673 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 name of a maste
16674 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 r journal file t
16675 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72 hat should be wr
16676 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a itten into the.*
16677 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 * individual jou
16678 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73 rnal file, or is
16679 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e NULL, indicatin
1667a 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 g no master jour
1667b 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69 nal file .** (si
1667c 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72 ngle database tr
1667d 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a ansaction)..**.*
1667e 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63 * When this is c
1667f 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65 alled, the maste
16680 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64 r journal should
16681 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65 already have be
16682 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70 en.** created, p
16683 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 opulated with th
16684 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 is journal point
16685 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f er and synced to
16686 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 disk..**.** Onc
16687 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e e this is routin
16688 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20 e has returned,
16689 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72 the only thing r
1668a 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69 equired to commi
1668b 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74 t.** the write-t
1668c 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74 ransaction for t
1668d 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c his database fil
1668e 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74 e is to delete t
1668f 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53 he journal..*/.S
16690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16691 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
16692 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72 mmitPhaseOne(Btr
16693 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 ee *p, const cha
16694 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69 r *zMaster){. i
16695 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
16696 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 K;. if( p->inTr
16697 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
16698 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 ){. BtShared
16699 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
1669a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1669b 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65 Enter(p);.#ifnde
1669c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1669d 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
1669e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
1669f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
166a0 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28 utoVacuumCommit(
166a1 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 pBt);. if(
166a2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
166a3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
166a4 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
166a5 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
166a6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
166a7 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 #endif. rc =
166a8 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d sqlite3PagerComm
166a9 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e itPhaseOne(pBt->
166aa 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c pPager, zMaster,
166ab 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0);. sqlite3
166ac 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
166ad 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
166ae 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
166af 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
166b0 20 66 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65 from both Btree
166b1 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 CommitPhaseTwo()
166b2 20 61 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61 and BtreeRollba
166b3 63 6b 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63 ck().** at the c
166b4 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74 onclusion of a t
166b5 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ransaction..*/.s
166b6 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 tatic void btree
166b7 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42 EndTransaction(B
166b8 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 tree *p){. BtSh
166b9 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
166ba 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a Bt;. BtCursor *
166bb 70 43 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20 pCsr;. assert(
166bc 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
166bd 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a 20 20 sMutex(p) );..
166be 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20 /* Search for a
166bf 63 75 72 73 6f 72 20 68 65 6c 64 20 6f 70 65 6e cursor held open
166c0 20 62 79 20 74 68 69 73 20 62 2d 74 72 65 65 20 by this b-tree
166c1 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 6f connection. If o
166c2 6e 65 20 65 78 69 73 74 73 2c 0a 20 20 2a 2a 20 ne exists,. **
166c3 74 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 then the transac
166c4 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 77 tion will be dow
166c5 6e 67 72 61 64 65 64 20 74 6f 20 61 20 72 65 61 ngraded to a rea
166c6 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 d-only transacti
166c7 6f 6e 0a 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 on. ** instead
166c8 6f 66 20 61 63 74 75 61 6c 6c 79 20 63 6f 6e 63 of actually conc
166c9 6c 75 64 65 64 2e 20 41 20 73 75 62 73 65 71 75 luded. A subsequ
166ca 65 6e 74 20 63 61 6c 6c 20 74 6f 20 43 6f 6d 6d ent call to Comm
166cb 69 74 50 68 61 73 65 54 77 6f 28 29 20 0a 20 20 itPhaseTwo() .
166cc 2a 2a 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 28 29 ** or Rollback()
166cd 20 77 69 6c 6c 20 66 69 6e 69 73 68 20 74 68 65 will finish the
166ce 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 transaction and
166cf 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 unlock the data
166d0 62 61 73 65 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 base. */. for(
166d1 70 43 73 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f pCsr=pBt->pCurso
166d2 72 3b 20 70 43 73 72 20 26 26 20 70 43 73 72 2d r; pCsr && pCsr-
166d3 3e 70 42 74 72 65 65 21 3d 70 3b 20 70 43 73 72 >pBtree!=p; pCsr
166d4 3d 70 43 73 72 2d 3e 70 4e 65 78 74 29 3b 0a 20 =pCsr->pNext);.
166d5 20 61 73 73 65 72 74 28 20 70 43 73 72 3d 3d 30 assert( pCsr==0
166d6 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 || p->inTrans>T
166d7 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 0a 20 20 RANS_NONE );..
166d8 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e btreeClearHasCon
166d9 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 69 66 28 tent(pBt);. if(
166da 20 70 43 73 72 20 29 7b 0a 20 20 20 20 64 6f 77 pCsr ){. dow
166db 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 ngradeAllSharedC
166dc 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 acheTableLocks(p
166dd 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e );. p->inTran
166de 73 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a s = TRANS_READ;.
166df 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
166e0 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61 If the handle ha
166e1 64 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72 d any kind of tr
166e2 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 ansaction open,
166e3 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 decrement the .
166e4 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f ** transactio
166e5 6e 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73 n count of the s
166e6 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20 hared btree. If
166e7 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
166e8 63 6f 75 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65 count . ** re
166e9 61 63 68 65 73 20 30 2c 20 73 65 74 20 74 68 65 aches 0, set the
166ea 20 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f shared state to
166eb 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 TRANS_NONE. The
166ec 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e unlockBtreeIfUn
166ed 75 73 65 64 28 29 0a 20 20 20 20 2a 2a 20 63 61 used(). ** ca
166ee 6c 6c 20 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e ll below will un
166ef 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 20 lock the pager.
166f0 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 */. if( p->i
166f1 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f nTrans!=TRANS_NO
166f2 4e 45 20 29 7b 0a 20 20 20 20 20 20 63 6c 65 61 NE ){. clea
166f3 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 rAllSharedCacheT
166f4 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 ableLocks(p);.
166f5 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 pBt->nTransa
166f6 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69 ction--;. i
166f7 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e f( 0==pBt->nTran
166f8 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 saction ){.
166f9 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 pBt->inTransa
166fa 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f ction = TRANS_NO
166fb 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 NE;. }.
166fc 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 }.. /* Set th
166fd 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
166fe 63 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 ction state to T
166ff 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e RANS_NONE and un
16700 6c 6f 63 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a lock the . **
16701 20 70 61 67 65 72 20 69 66 20 74 68 69 73 20 63 pager if this c
16702 61 6c 6c 20 63 6c 6f 73 65 64 20 74 68 65 20 6f all closed the o
16703 6e 6c 79 20 72 65 61 64 20 6f 72 20 77 72 69 74 nly read or writ
16704 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 e transaction.
16705 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e */. p->inTran
16706 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a s = TRANS_NONE;.
16707 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 unlockBtreeI
16708 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 fUnused(pBt);.
16709 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 }.. btreeIntegr
1670a 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ity(p);.}../*.**
1670b 20 43 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e Commit the tran
1670c 73 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c saction currentl
1670d 79 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a y in progress..*
1670e 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1670f 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 e implements the
16710 20 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 second phase of
16711 20 61 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 a 2-phase commi
16712 74 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 t. The.** sqlit
16713 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
16714 73 65 4f 6e 65 28 29 20 72 6f 75 74 69 6e 65 20 seOne() routine
16715 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70 does the first p
16716 68 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a hase and should.
16717 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 ** be invoked pr
16718 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 ior to calling t
16719 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 his routine. Th
1671a 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f e sqlite3BtreeCo
1671b 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a mmitPhaseOne().*
1671c 2a 20 72 6f 75 74 69 6e 65 20 64 69 64 20 61 6c * routine did al
1671d 6c 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 77 72 l the work of wr
1671e 69 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f iting informatio
1671f 6e 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e n out to disk an
16720 64 20 66 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a d flushing the.*
16721 2a 20 63 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68 * contents so th
16722 61 74 20 74 68 65 79 20 61 72 65 20 77 72 69 74 at they are writ
16723 74 65 6e 20 6f 6e 74 6f 20 74 68 65 20 64 69 73 ten onto the dis
16724 6b 20 70 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20 k platter. All
16725 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 this.** routine
16726 68 61 73 20 74 6f 20 64 6f 20 69 73 20 64 65 6c has to do is del
16727 65 74 65 20 6f 72 20 74 72 75 6e 63 61 74 65 20 ete or truncate
16728 6f 72 20 7a 65 72 6f 20 74 68 65 20 68 65 61 64 or zero the head
16729 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65 er in the.** the
1672a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
1672b 6c 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 l (which causes
1672c 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
1672d 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a to commit) and.*
1672e 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a * drop locks..**
1672f 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 .** This will re
16730 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 lease the write
16731 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
16732 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 base file. If t
16733 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 here.** are no a
16734 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 ctive cursors, i
16735 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 t also releases
16736 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a the read lock..*
16737 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16738 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16739 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 eCommitPhaseTwo(
1673a 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 Btree *p){. BtS
1673b 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
1673c 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 pBt;.. sqlite3B
1673d 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
1673e 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 btreeIntegrity(p
1673f 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
16740 68 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69 handle has a wri
16741 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te-transaction o
16742 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 pen, commit the
16743 73 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20 shared-btrees .
16744 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
16745 61 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72 and set the shar
16746 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e ed state to TRAN
16747 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 S_READ.. */. i
16748 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 f( p->inTrans==T
16749 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 RANS_WRITE ){.
1674a 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 int rc;. as
1674b 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 sert( pBt->inTra
1674c 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
1674d 57 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 WRITE );. ass
1674e 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 ert( pBt->nTrans
1674f 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 action>0 );.
16750 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
16751 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 rCommitPhaseTwo(
16752 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
16753 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16754 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
16755 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
16756 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
16757 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 rc;. }. pB
16758 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
16759 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 = TRANS_READ;.
1675a 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 54 72 }.. btreeEndTr
1675b 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20 ansaction(p);.
1675c 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
1675d 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 e(p);. return S
1675e 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1675f 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65 ** Do both phase
16760 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a s of a commit..*
16761 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16762 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16763 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 eCommit(Btree *p
16764 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
16765 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
16766 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 (p);. rc = sqli
16767 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
16768 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 aseOne(p, 0);.
16769 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1676a 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 K ){. rc = sq
1676b 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 lite3BtreeCommit
1676c 50 68 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d PhaseTwo(p);. }
1676d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
1676e 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
1676f 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 n rc;.}..#ifndef
16770 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 NDEBUG./*.** Re
16771 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
16772 6f 66 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73 of write-cursors
16773 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 open on this ha
16774 6e 64 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f ndle. This is fo
16775 72 20 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 r use.** in asse
16776 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73 rt() expressions
16777 2c 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 , so it is only
16778 63 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42 compiled if NDEB
16779 55 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 UG is not.** def
1677a 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 ined..**.** For
1677b 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
1677c 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 this routine, a
1677d 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 write-cursor is
1677e 61 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a any cursor that.
1677f 2a 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 ** is capable of
16780 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 writing to the
16781 64 61 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d databse. That m
16782 65 61 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20 eans the cursor
16783 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c was.** originall
16784 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 y opened for wri
16785 74 69 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72 ting and the cur
16786 73 6f 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64 sor has not be d
16787 69 73 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 isabled.** by ha
16788 76 69 6e 67 20 69 74 73 20 73 74 61 74 65 20 63 ving its state c
16789 68 61 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52 hanged to CURSOR
1678a 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 _FAULT..*/.stati
1678b 63 20 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 c int countWrite
1678c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 Cursors(BtShared
1678d 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 *pBt){. BtCurs
1678e 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 or *pCur;. int
1678f 72 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 r = 0;. for(pCu
16790 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 r=pBt->pCursor;
16791 70 43 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d pCur; pCur=pCur-
16792 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
16793 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 pCur->wrFlag &&
16794 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 pCur->eState!=C
16795 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b URSOR_FAULT ) r+
16796 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e +; . }. return
16797 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a r;.}.#endif../*
16798 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
16799 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 sets the state
1679a 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 to CURSOR_FAULT
1679b 61 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a and the error.**
1679c 20 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65 code to errCode
1679d 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f for every curso
1679e 72 20 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68 r on BtShared th
1679f 61 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 at pBtree.** ref
167a0 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 erences..**.** E
167a1 76 65 72 79 20 63 75 72 73 6f 72 20 69 73 20 74 very cursor is t
167a2 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e ripped, includin
167a3 67 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 62 g cursors that b
167a4 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 elong.** to othe
167a5 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
167a6 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 ctions that happ
167a7 65 6e 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67 en to be sharing
167a8 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 77 69 .** the cache wi
167a9 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a th pBtree..**.**
167aa 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 This routine ge
167ab 74 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 ts called when a
167ac 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 rollback occurs
167ad 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 ..** All cursors
167ae 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 using the same
167af 63 61 63 68 65 20 6d 75 73 74 20 62 65 20 74 72 cache must be tr
167b0 69 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 ipped.** to prev
167b1 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 ent them from tr
167b2 79 69 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20 ying to use the
167b3 62 74 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 btree after.** t
167b4 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 he rollback. Th
167b5 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 e rollback may h
167b6 61 76 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c ave deleted tabl
167b7 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 es.** or moved r
167b8 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74 oot pages, so it
167b9 20 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 is not sufficie
167ba 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 nt to.** save th
167bb 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 e state of the c
167bc 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 ursor. The curs
167bd 6f 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e or must be.** in
167be 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 validated..*/.SQ
167bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
167c0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 d sqlite3BtreeTr
167c1 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 ipAllCursors(Btr
167c2 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 ee *pBtree, int
167c3 65 72 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 errCode){. BtCu
167c4 72 73 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 rsor *p;. sqlit
167c5 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 e3BtreeEnter(pBt
167c6 72 65 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 ree);. for(p=pB
167c7 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 tree->pBt->pCurs
167c8 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 or; p; p=p->pNex
167c9 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 t){. int i;.
167ca 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
167cb 6c 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 learCursor(p);.
167cc 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 p->eState = C
167cd 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 URSOR_FAULT;.
167ce 20 70 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65 p->skipNext = e
167cf 72 72 43 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28 rrCode;. for(
167d0 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65 i=0; i<=p->iPage
167d1 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 ; i++){. re
167d2 6c 65 61 73 65 50 61 67 65 28 70 2d 3e 61 70 50 leasePage(p->apP
167d3 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 age[i]);. p
167d4 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b ->apPage[i] = 0;
167d5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
167d6 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
167d7 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a Btree);.}../*.**
167d8 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 Rollback the tr
167d9 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f ansaction in pro
167da 67 72 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73 gress. All curs
167db 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69 ors will be.** i
167dc 6e 76 61 6c 69 64 65 64 20 62 79 20 74 68 69 73 nvalided by this
167dd 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 operation. Any
167de 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
167df 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74 a cursor.** that
167e0 20 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65 was open at the
167e1 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
167e2 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c is operation wil
167e3 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 l result.** in a
167e4 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 n error..**.** T
167e5 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 his will release
167e6 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 the write lock
167e7 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
167e8 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a file. If there.
167e9 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 ** are no active
167ea 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 cursors, it als
167eb 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72 o releases the r
167ec 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c ead lock..*/.SQL
167ed 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
167ee 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c sqlite3BtreeRoll
167ef 62 61 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a back(Btree *p){.
167f0 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 int rc;. BtSh
167f1 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
167f2 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 Bt;. MemPage *p
167f3 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65 Page1;.. sqlite
167f4 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
167f5 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 rc = saveAllCu
167f6 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 29 rsors(pBt, 0, 0)
167f7 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
167f8 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
167f9 48 45 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c HE. if( rc!=SQL
167fa 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a ITE_OK ){. /*
167fb 20 54 68 69 73 20 69 73 20 61 20 68 6f 72 72 69 This is a horri
167fc 62 6c 65 20 73 69 74 75 61 74 69 6f 6e 2e 20 41 ble situation. A
167fd 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 n IO or malloc()
167fe 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 error occurred
167ff 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72 whilst. ** tr
16800 79 69 6e 67 20 74 6f 20 73 61 76 65 20 63 75 72 ying to save cur
16801 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49 sor positions. I
16802 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 f this is an aut
16803 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 omatic rollback
16804 28 61 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 (as. ** the r
16805 65 73 75 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74 esult of a const
16806 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 raint, malloc()
16807 66 61 69 6c 75 72 65 20 6f 72 20 49 4f 20 65 72 failure or IO er
16808 72 6f 72 29 20 74 68 65 6e 20 0a 20 20 20 20 2a ror) then . *
16809 2a 20 74 68 65 20 63 61 63 68 65 20 6d 61 79 20 * the cache may
1680a 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e be internally in
1680b 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e 6f 74 20 consistent (not
1680c 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64 20 74 72 contain valid tr
1680d 65 65 73 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77 ees) so. ** w
1680e 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 e cannot simply
1680f 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
16810 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 to the caller.
16811 49 6e 73 74 65 61 64 2c 20 61 62 6f 72 74 20 0a Instead, abort .
16812 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69 ** all queri
16813 65 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 es that may be u
16814 73 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20 sing any of the
16815 63 75 72 73 6f 72 73 20 74 68 61 74 20 66 61 69 cursors that fai
16816 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a 20 20 20 led to save..
16817 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 */. sqlite3B
16818 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f treeTripAllCurso
16819 72 73 28 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 rs(p, rc);. }.#
1681a 65 6e 64 69 66 0a 20 20 62 74 72 65 65 49 6e 74 endif. btreeInt
1681b 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 69 66 egrity(p);.. if
1681c 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
1681d 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 ANS_WRITE ){.
1681e 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 20 20 61 int rc2;.. a
1681f 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57 52 49 ssert( TRANS_WRI
16820 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 TE==pBt->inTrans
16821 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 action );. rc
16822 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 2 = sqlite3Pager
16823 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 Rollback(pBt->pP
16824 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 ager);. if( r
16825 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b c2!=SQLITE_OK ){
16826 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b . rc = rc2;
16827 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 . }.. /* T
16828 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 he rollback may
16829 68 61 76 65 20 64 65 73 74 72 6f 79 65 64 20 74 have destroyed t
1682a 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 he pPage1->aData
1682b 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 value. So.
1682c 2a 2a 20 63 61 6c 6c 20 62 74 72 65 65 47 65 74 ** call btreeGet
1682d 50 61 67 65 28 29 20 6f 6e 20 70 61 67 65 20 31 Page() on page 1
1682e 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20 again to make.
1682f 20 20 20 2a 2a 20 73 75 72 65 20 70 50 61 67 65 ** sure pPage
16830 31 2d 3e 61 44 61 74 61 20 69 73 20 73 65 74 20 1->aData is set
16831 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20 correctly. */.
16832 20 20 69 66 28 20 62 74 72 65 65 47 65 74 50 61 if( btreeGetPa
16833 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67 ge(pBt, 1, &pPag
16834 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f e1, 0)==SQLITE_O
16835 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 K ){. relea
16836 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a sePage(pPage1);.
16837 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
16838 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73 ( countWriteCurs
16839 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20 ors(pBt)==0 );.
1683a 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 pBt->inTransa
1683b 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 ction = TRANS_RE
1683c 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 AD;. }.. btree
1683d 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 EndTransaction(p
1683e 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
1683f 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
16840 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
16841 20 53 74 61 72 74 20 61 20 73 74 61 74 65 6d 65 Start a stateme
16842 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f nt subtransactio
16843 6e 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 n. The subtransa
16844 63 74 69 6f 6e 20 63 61 6e 20 63 61 6e 20 62 65 ction can can be
16845 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 rolled.** back
16846 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66 independently of
16847 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 the main transa
16848 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20 ction. You must
16849 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 start a transact
1684a 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72 65 20 73 ion .** before s
1684b 74 61 72 74 69 6e 67 20 61 20 73 75 62 74 72 61 tarting a subtra
1684c 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 nsaction. The su
1684d 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 btransaction is
1684e 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 69 63 61 ended automatica
1684f 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68 65 20 6d lly .** if the m
16850 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ain transaction
16851 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73 commits or rolls
16852 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 back..**.** Sta
16853 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 tement subtransa
16854 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 ctions are used
16855 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 64 75 61 around individua
16856 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 l SQL statements
16857 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 63 6f 6e .** that are con
16858 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 61 20 tained within a
16859 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 BEGIN...COMMIT b
1685a 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73 lock. If a cons
1685b 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 traint.** error
1685c 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 occurs within th
1685d 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 e statement, the
1685e 20 65 66 66 65 63 74 20 6f 66 20 74 68 61 74 20 effect of that
1685f 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a one statement.**
16860 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 can be rolled b
16861 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 ack without havi
16862 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 ng to rollback t
16863 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 he entire transa
16864 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 ction..**.** A s
16865 74 61 74 65 6d 65 6e 74 20 73 75 62 2d 74 72 61 tatement sub-tra
16866 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 6d 70 6c nsaction is impl
16867 65 6d 65 6e 74 65 64 20 61 73 20 61 6e 20 61 6e emented as an an
16868 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e onymous savepoin
16869 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 t. The.** value
1686a 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
1686b 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 cond parameter i
1686c 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 s the total numb
1686d 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 er of savepoints
1686e 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 ,.** including t
1686f 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d 6f 75 73 he new anonymous
16870 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f 70 65 6e savepoint, open
16871 20 6f 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 on the B-Tree.
16872 69 2e 65 2e 20 69 66 20 74 68 65 72 65 0a 2a 2a i.e. if there.**
16873 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 73 are no active s
16874 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 20 6e 6f avepoints and no
16875 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 other statement
16876 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70 -transactions op
16877 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e en,.** iStatemen
16878 74 20 69 73 20 31 2e 20 54 68 69 73 20 61 6e 6f t is 1. This ano
16879 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 nymous savepoint
1687a 20 63 61 6e 20 62 65 20 72 65 6c 65 61 73 65 64 can be released
1687b 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0a or rolled back.
1687c 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c ** using the sql
1687d 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
1687e 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a nt() function..*
1687f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16880 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16881 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65 65 eBeginStmt(Btree
16882 20 2a 70 2c 20 69 6e 74 20 69 53 74 61 74 65 6d *p, int iStatem
16883 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ent){. int rc;.
16884 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
16885 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 = p->pBt;. sqli
16886 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
16887 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
16888 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 nTrans==TRANS_WR
16889 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ITE );. assert(
1688a 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d pBt->readOnly==
1688b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 0 );. assert( i
1688c 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20 Statement>0 );.
1688d 20 61 73 73 65 72 74 28 20 69 53 74 61 74 65 6d assert( iStatem
1688e 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 ent>p->db->nSave
1688f 70 6f 69 6e 74 20 29 3b 0a 20 20 69 66 28 20 4e point );. if( N
16890 45 56 45 52 28 70 2d 3e 69 6e 54 72 61 6e 73 21 EVER(p->inTrans!
16891 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 =TRANS_WRITE ||
16892 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 pBt->readOnly) )
16893 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
16894 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a 20 20 7d 65 E_INTERNAL;. }e
16895 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
16896 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
16897 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
16898 20 29 3b 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 );. /* At th
16899 65 20 70 61 67 65 72 20 6c 65 76 65 6c 2c 20 61 e pager level, a
1689a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1689b 61 63 74 69 6f 6e 20 69 73 20 61 20 73 61 76 65 action is a save
1689c 70 6f 69 6e 74 20 77 69 74 68 0a 20 20 20 20 2a point with. *
1689d 2a 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 * an index great
1689e 65 72 20 74 68 61 6e 20 61 6c 6c 20 73 61 76 65 er than all save
1689f 70 6f 69 6e 74 73 20 63 72 65 61 74 65 64 20 65 points created e
168a0 78 70 6c 69 63 69 74 6c 79 20 75 73 69 6e 67 0a xplicitly using.
168a1 20 20 20 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 ** SQL state
168a2 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 69 6c 6c ments. It is ill
168a3 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c 20 72 65 egal to open, re
168a4 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 lease or rollbac
168a5 6b 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63 k any. ** suc
168a6 68 20 73 61 76 65 70 6f 69 6e 74 73 20 77 68 69 h savepoints whi
168a7 6c 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 le the statement
168a8 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 transaction sav
168a9 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65 epoint is active
168aa 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
168ab 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 = sqlite3PagerOp
168ac 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d enSavepoint(pBt-
168ad 3e 70 50 61 67 65 72 2c 20 69 53 74 61 74 65 6d >pPager, iStatem
168ae 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 ent);. }. sqli
168af 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
168b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
168b1 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ../*.** The seco
168b2 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 nd argument to t
168b3 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70 his function, op
168b4 2c 20 69 73 20 61 6c 77 61 79 73 20 53 41 56 45 , is always SAVE
168b5 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a POINT_ROLLBACK.*
168b6 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 * or SAVEPOINT_R
168b7 45 4c 45 41 53 45 2e 20 54 68 69 73 20 66 75 6e ELEASE. This fun
168b8 63 74 69 6f 6e 20 65 69 74 68 65 72 20 72 65 6c ction either rel
168b9 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c 73 20 62 eases or rolls b
168ba 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 76 65 70 ack the.** savep
168bb 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 65 64 20 oint identified
168bc 62 79 20 70 61 72 61 6d 65 74 65 72 20 69 53 61 by parameter iSa
168bd 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 6e 64 69 vepoint, dependi
168be 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 ng on the value
168bf 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a .** of op..**.**
168c0 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65 Normally, iSave
168c1 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 point is greater
168c2 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
168c3 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c o zero. However,
168c4 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 53 41 56 if op is.** SAV
168c5 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c EPOINT_ROLLBACK,
168c6 20 74 68 65 6e 20 69 53 61 76 65 70 6f 69 6e 74 then iSavepoint
168c7 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e may also be -1.
168c8 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
168c9 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f e .** contents o
168ca 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61 f the entire tra
168cb 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c nsaction are rol
168cc 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 led back. This i
168cd 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 s different.** f
168ce 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61 rom a normal tra
168cf 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63 nsaction rollbac
168d0 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61 k, as no locks a
168d1 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64 20 re released and
168d2 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 the.** transacti
168d3 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e on remains open.
168d4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
168d5 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
168d6 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72 reeSavepoint(Btr
168d7 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 ee *p, int op, i
168d8 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a nt iSavepoint){.
168d9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
168da 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 26 26 E_OK;. if( p &&
168db 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
168dc 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 NS_WRITE ){.
168dd 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
168de 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 p->pBt;. asse
168df 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e rt( op==SAVEPOIN
168e0 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d T_RELEASE || op=
168e1 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
168e2 41 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ACK );. asser
168e3 74 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 t( iSavepoint>=0
168e4 20 7c 7c 20 28 69 53 61 76 65 70 6f 69 6e 74 3d || (iSavepoint=
168e5 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 =-1 && op==SAVEP
168e6 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29 OINT_ROLLBACK) )
168e7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
168e8 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 eeEnter(p);.
168e9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
168ea 72 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e rSavepoint(pBt->
168eb 70 50 61 67 65 72 2c 20 6f 70 2c 20 69 53 61 76 pPager, op, iSav
168ec 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 69 66 28 epoint);. if(
168ed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
168ee 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 {. rc = new
168ef 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20 Database(pBt);.
168f0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
168f1 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
168f2 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
168f3 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
168f4 61 20 6e 65 77 20 63 75 72 73 6f 72 20 66 6f 72 a new cursor for
168f5 20 74 68 65 20 42 54 72 65 65 20 77 68 6f 73 65 the BTree whose
168f6 20 72 6f 6f 74 20 69 73 20 6f 6e 20 74 68 65 20 root is on the
168f7 70 61 67 65 0a 2a 2a 20 69 54 61 62 6c 65 2e 20 page.** iTable.
168f8 49 66 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 If a read-only c
168f9 75 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 74 ursor is request
168fa 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 ed, it is assume
168fb 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 d that.** the ca
168fc 6c 6c 65 72 20 61 6c 72 65 61 64 79 20 68 61 73 ller already has
168fd 20 61 74 20 6c 65 61 73 74 20 61 20 72 65 61 64 at least a read
168fe 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f -only transactio
168ff 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e 20 74 68 65 n open.** on the
16900 20 64 61 74 61 62 61 73 65 20 61 6c 72 65 61 64 database alread
16901 79 2e 20 49 66 20 61 20 77 72 69 74 65 2d 63 75 y. If a write-cu
16902 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 74 65 rsor is requeste
16903 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 63 d, then.** the c
16904 61 6c 6c 65 72 20 69 73 20 61 73 73 75 6d 65 64 aller is assumed
16905 20 74 6f 20 68 61 76 65 20 61 6e 20 6f 70 65 6e to have an open
16906 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
16907 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 on..**.** If wrF
16908 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 lag==0, then the
16909 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 cursor can only
1690a 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 be used for rea
1690b 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c ding..** If wrFl
1690c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20 ag==1, then the
1690d 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73 cursor can be us
1690e 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f ed for reading o
1690f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 r for.** writing
16910 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74 if other condit
16911 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67 ions for writing
16912 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20 are also met.
16913 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 These.** are the
16914 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 conditions that
16915 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20 must be met in
16916 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e order for writin
16917 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 g to.** be allow
16918 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 ed:.**.** 1: Th
16919 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 e cursor must ha
1691a 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77 ve been opened w
1691b 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a ith wrFlag==1.**
1691c 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61 .** 2: Other da
1691d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1691e 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74 68 ns that share th
1691f 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61 63 e same pager cac
16920 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68 he.** but wh
16921 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 ich are not in t
16922 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 he READ_UNCOMMIT
16923 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f TED state may no
16924 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75 t have.** cu
16925 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20 rsors open with
16926 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65 wrFlag==0 on the
16927 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74 same table. Ot
16928 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74 herwise.** t
16929 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 he changes made
1692a 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63 75 by this write cu
1692b 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69 rsor would be vi
1692c 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20 sible to.**
1692d 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72 73 the read cursors
1692e 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61 in the other da
1692f 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
16930 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 n..**.** 3: The
16931 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 database must b
16932 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20 e writable (not
16933 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 on read-only med
16934 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 ia).**.** 4: Th
16935 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61 ere must be an a
16936 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f ctive transactio
16937 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 n..**.** No chec
16938 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20 king is done to
16939 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 70 make sure that p
1693a 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c age iTable reall
1693b 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 y is the.** root
1693c 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65 page of a b-tre
1693d 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 e. If it is not
1693e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f , then the curso
1693f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69 r acquired.** wi
16940 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 ll not work corr
16941 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 ectly..**.** It
16942 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 is assumed that
16943 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 the sqlite3Btree
16944 43 75 72 73 6f 72 53 69 7a 65 28 29 20 62 79 74 CursorSize() byt
16945 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a es of memory .**
16946 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
16947 43 75 72 20 68 61 76 65 20 62 65 65 6e 20 7a 65 Cur have been ze
16948 72 6f 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c roed by the call
16949 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e er..*/.static in
1694a 74 20 62 74 72 65 65 43 75 72 73 6f 72 28 0a 20 t btreeCursor(.
1694b 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 Btree *p,
1694c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1694d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 /* The b
1694e 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 tree */. int iT
1694f 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 able,
16950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16951 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 /* Root page of
16952 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a table to open *
16953 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 /. int wrFlag,
16954 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16955 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 /* 1
16956 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 to write. 0 read
16957 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 -only */. struc
16958 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 t KeyInfo *pKeyI
16959 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 nfo,
1695a 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 /* First arg t
1695b 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e o comparison fun
1695c 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 ction */. BtCur
1695d 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 sor *pCur
1695e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1695f 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6e /* Space for n
16960 65 77 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a ew cursor */.){.
16961 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
16962 3d 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 = p->pBt;
16963 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72 /* Shar
16964 65 64 20 62 2d 74 72 65 65 20 68 61 6e 64 6c 65 ed b-tree handle
16965 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
16966 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
16967 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 Mutex(p) );. as
16968 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 sert( wrFlag==0
16969 7c 7c 20 77 72 46 6c 61 67 3d 3d 31 20 29 3b 0a || wrFlag==1 );.
1696a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
1696b 69 6e 67 20 61 73 73 65 72 74 20 73 74 61 74 65 ing assert state
1696c 6d 65 6e 74 73 20 76 65 72 69 66 79 20 74 68 61 ments verify tha
1696d 74 20 69 66 20 74 68 69 73 20 69 73 20 61 20 73 t if this is a s
1696e 68 61 72 61 62 6c 65 20 0a 20 20 2a 2a 20 62 2d harable . ** b-
1696f 74 72 65 65 20 64 61 74 61 62 61 73 65 2c 20 74 tree database, t
16970 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 he connection is
16971 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71 holding the req
16972 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b uired table lock
16973 73 2c 20 0a 20 20 2a 2a 20 61 6e 64 20 74 68 61 s, . ** and tha
16974 74 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 t no other conne
16975 63 74 69 6f 6e 20 68 61 73 20 61 6e 79 20 6f 70 ction has any op
16976 65 6e 20 63 75 72 73 6f 72 20 74 68 61 74 20 63 en cursor that c
16977 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 0a 20 onflicts with .
16978 20 2a 2a 20 74 68 69 73 20 6c 6f 63 6b 2e 20 20 ** this lock.
16979 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 68 61 73 */. assert( has
1697a 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
1697b 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 Lock(p, iTable,
1697c 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 77 72 46 pKeyInfo!=0, wrF
1697d 6c 61 67 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 lag+1) );. asse
1697e 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c rt( wrFlag==0 ||
1697f 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 !hasReadConflic
16980 74 73 28 70 2c 20 69 54 61 62 6c 65 29 20 29 3b ts(p, iTable) );
16981 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 .. /* Assert th
16982 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 at the caller ha
16983 73 20 6f 70 65 6e 65 64 20 74 68 65 20 72 65 71 s opened the req
16984 75 69 72 65 64 20 74 72 61 6e 73 61 63 74 69 6f uired transactio
16985 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 n. */. assert(
16986 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 p->inTrans>TRANS
16987 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 _NONE );. asser
16988 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 t( wrFlag==0 ||
16989 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
1698a 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 S_WRITE );. ass
1698b 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 ert( pBt->pPage1
1698c 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 2d && pBt->pPage1-
1698d 3e 61 44 61 74 61 20 29 3b 0a 0a 20 20 69 66 28 >aData );.. if(
1698e 20 4e 45 56 45 52 28 77 72 46 6c 61 67 20 26 26 NEVER(wrFlag &&
1698f 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 pBt->readOnly)
16990 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
16991 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 LITE_READONLY;.
16992 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3d }. if( iTable=
16993 3d 31 20 26 26 20 70 61 67 65 72 50 61 67 65 63 =1 && pagerPagec
16994 6f 75 6e 74 28 70 42 74 29 3d 3d 30 20 29 7b 0a ount(pBt)==0 ){.
16995 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16996 45 5f 45 4d 50 54 59 3b 0a 20 20 7d 0a 0a 20 20 E_EMPTY;. }..
16997 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f /* Now that no o
16998 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20 ther errors can
16999 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69 occur, finish fi
1699a 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43 lling in the BtC
1699b 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61 ursor. ** varia
1699c 62 6c 65 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68 bles and link th
1699d 65 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 e cursor into th
1699e 65 20 42 74 53 68 61 72 65 64 20 6c 69 73 74 2e e BtShared list.
1699f 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 67 6e */. pCur->pgn
169a0 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54 oRoot = (Pgno)iT
169a1 61 62 6c 65 3b 0a 20 20 70 43 75 72 2d 3e 69 50 able;. pCur->iP
169a2 61 67 65 20 3d 20 2d 31 3b 0a 20 20 70 43 75 72 age = -1;. pCur
169a3 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 ->pKeyInfo = pKe
169a4 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 yInfo;. pCur->p
169a5 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43 75 Btree = p;. pCu
169a6 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 r->pBt = pBt;.
169a7 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20 28 pCur->wrFlag = (
169a8 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 70 43 75 u8)wrFlag;. pCu
169a9 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e r->pNext = pBt->
169aa 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 pCursor;. if( p
169ab 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 Cur->pNext ){.
169ac 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 pCur->pNext->p
169ad 50 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d Prev = pCur;. }
169ae 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 . pBt->pCursor
169af 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e = pCur;. pCur->
169b0 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
169b1 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 75 72 2d INVALID;. pCur-
169b2 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20 30 >cachedRowid = 0
169b3 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
169b4 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 E_OK;.}.SQLITE_P
169b5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
169b6 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 e3BtreeCursor(.
169b7 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20 Btree *p,
169b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
169b9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
169ba 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 The btree */. i
169bb 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 nt iTable,
169bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
169bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
169be 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 ot page of table
169bf 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e to open */. in
169c0 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 t wrFlag,
169c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
169c2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74 /* 1 t
169c3 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d o write. 0 read-
169c4 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 only */. struct
169c5 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
169c6 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 fo,
169c7 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
169c8 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 65 28 29 rg to xCompare()
169c9 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a */. BtCursor *
169ca 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20 pCur
169cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
169cc 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20 63 /* Write new c
169cd 75 72 73 6f 72 20 68 65 72 65 20 2a 2f 0a 29 7b ursor here */.){
169ce 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c . int rc;. sql
169cf 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
169d0 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 );. rc = btreeC
169d1 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c ursor(p, iTable,
169d2 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66 wrFlag, pKeyInf
169d3 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69 o, pCur);. sqli
169d4 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
169d5 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
169d6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
169d7 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74 43 he size of a BtC
169d8 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20 ursor object in
169d9 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 bytes..**.** Thi
169da 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20 s interfaces is
169db 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 75 needed so that u
169dc 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 20 sers of cursors
169dd 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a can preallocate.
169de 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 74 ** sufficient st
169df 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 20 orage to hold a
169e0 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 43 cursor. The BtC
169e1 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 20 ursor object is
169e2 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65 opaque.** to use
169e3 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f rs so they canno
169e4 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66 28 t do the sizeof(
169e5 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20 74 ) themselves - t
169e6 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a hey must call.**
169e7 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
169e8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
169e9 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
169ea 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 eCursorSize(void
169eb 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65 ){. return size
169ec 6f 66 28 42 74 43 75 72 73 6f 72 29 3b 0a 7d 0a of(BtCursor);.}.
169ed 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 ./*.** Set the c
169ee 61 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75 ached rowid valu
169ef 65 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f e of every curso
169f0 72 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 r in the same da
169f1 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 tabase file.** a
169f2 73 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e s pCur and havin
169f3 67 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 g the same root
169f4 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70 page number as p
169f5 43 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 Cur. The value
169f6 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f is.** set to iRo
169f7 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 wid..**.** Only
169f8 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 76 positive rowid v
169f9 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 alues are consid
169fa 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74 ered valid for t
169fb 68 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 his cache..** Th
169fc 65 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 69 e cache is initi
169fd 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 alized to zero,
169fe 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e indicating an in
169ff 76 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 valid cache..**
16a00 41 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 A btree will wor
16a01 6b 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f k fine with zero
16a02 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 or negative row
16a03 69 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61 ids. We just ca
16a04 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 nnot.** cache ze
16a05 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 ro or negative r
16a06 6f 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61 owids, which mea
16a07 6e 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 75 ns tables that u
16a08 73 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 se zero or.** ne
16a09 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69 gative rowids mi
16a0a 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 ght run a little
16a0b 20 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e slower. But in
16a0c 20 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a practice, zero.
16a0d 2a 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 ** or negative r
16a0e 6f 77 69 64 73 20 61 72 65 20 76 65 72 79 20 75 owids are very u
16a0f 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 ncommon so this
16a10 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 should not be a
16a11 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 problem..*/.SQLI
16a12 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
16a13 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 sqlite3BtreeSetC
16a14 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 achedRowid(BtCur
16a15 73 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 sor *pCur, sqlit
16a16 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 e3_int64 iRowid)
16a17 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b {. BtCursor *p;
16a18 0a 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 . for(p=pCur->p
16a19 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
16a1a 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
16a1b 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 if( p->pgnoRoot
16a1c 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 ==pCur->pgnoRoot
16a1d 20 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 ) p->cachedRowi
16a1e 64 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a d = iRowid;. }.
16a1f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16a20 63 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f cachedRowid==iRo
16a21 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 wid );.}../*.**
16a22 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 65 Return the cache
16a23 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 d rowid for the
16a24 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 given cursor. A
16a25 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 negative or zer
16a26 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 o.** return valu
16a27 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 e indicates that
16a28 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 the rowid cache
16a29 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 is invalid and
16a2a 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e should be.** ign
16a2b 6f 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f ored. If the ro
16a2c 77 69 64 20 63 61 63 68 65 20 68 61 73 20 6e 65 wid cache has ne
16a2d 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 ver before been
16a2e 73 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a set, then a.** z
16a2f 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e ero is returned.
16a30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
16a31 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 TE sqlite3_int64
16a32 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
16a33 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 CachedRowid(BtCu
16a34 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 rsor *pCur){. r
16a35 65 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 eturn pCur->cach
16a36 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a edRowid;.}../*.*
16a37 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 * Close a cursor
16a38 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b . The read lock
16a39 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
16a3a 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 file is release
16a3b 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 d.** when the la
16a3c 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f st cursor is clo
16a3d 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 sed..*/.SQLITE_P
16a3e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16a3f 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 e3BtreeCloseCurs
16a40 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 or(BtCursor *pCu
16a41 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 r){. Btree *pBt
16a42 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 ree = pCur->pBtr
16a43 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 ee;. if( pBtree
16a44 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
16a45 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 BtShared *pBt
16a46 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 = pCur->pBt;.
16a47 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
16a48 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 ter(pBtree);.
16a49 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
16a4a 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a arCursor(pCur);.
16a4b 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50 if( pCur->pP
16a4c 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 43 75 rev ){. pCu
16a4d 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 r->pPrev->pNext
16a4e 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 = pCur->pNext;.
16a4f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
16a50 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 pBt->pCursor = p
16a51 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Cur->pNext;.
16a52 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e }. if( pCur->
16a53 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 pNext ){. p
16a54 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 Cur->pNext->pPre
16a55 76 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 3b v = pCur->pPrev;
16a56 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
16a57 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 =0; i<=pCur->iPa
16a58 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ge; i++){.
16a59 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 releasePage(pCur
16a5a 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 ->apPage[i]);.
16a5b 20 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 }. unlockBt
16a5c 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
16a5d 3b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 ;. invalidate
16a5e 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 OverflowCache(pC
16a5f 75 72 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 ur);. /* sqli
16a60 74 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20 te3_free(pCur);
16a61 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 */. sqlite3Bt
16a62 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 reeLeave(pBtree)
16a63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 ;. }. return S
16a64 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
16a65 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 ** Make sure the
16a66 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e BtCursor* given
16a67 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 in the argument
16a68 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 has a valid.**
16a69 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 BtCursor.info st
16a6a 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 20 ructure. If it
16a6b 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 is not already v
16a6c 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 alid, call.** bt
16a6d 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 reeParseCell() t
16a6e 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a o fill it in..**
16a6f 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 .** BtCursor.inf
16a70 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 o is a cache of
16a71 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 the information
16a72 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 in the current c
16a73 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 ell..** Using th
16a74 69 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73 is cache reduces
16a75 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
16a76 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72 alls to btreePar
16a77 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 seCell()..**.**
16a78 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 2007-06-25: The
16a79 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73 re is a bug in s
16a7a 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ome versions of
16a7b 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20 MSVC that cause
16a7c 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 the.** compiler
16a7d 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65 to crash when ge
16a7e 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 tCellInfo() is i
16a7f 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 mplemented as a
16a80 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 macro..** But th
16a81 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65 ere is a measure
16a82 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e able speed advan
16a83 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68 tage to using th
16a84 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a e macro on gcc.*
16a85 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d * (when less com
16a86 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 piler optimizati
16a87 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 ons like -Os or
16a88 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64 -O0 are used and
16a89 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 the.** compiler
16a8a 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 is not doing ag
16a8b 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 ressive inlining
16a8c 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20 .) So we use a
16a8d 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a real function.**
16a8e 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20 for MSVC and a
16a8f 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74 macro for everyt
16a90 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b hing else. Tick
16a91 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 et #2457..*/.#if
16a92 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74 ndef NDEBUG. st
16a93 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 atic void assert
16a94 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f CellInfo(BtCurso
16a95 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65 r *pCur){. Ce
16a96 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 llInfo info;.
16a97 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 int iPage = pCu
16a98 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 r->iPage;. me
16a99 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 mset(&info, 0, s
16a9a 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 izeof(info));.
16a9b 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
16a9c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 (pCur->apPage[iP
16a9d 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 age], pCur->aiId
16a9e 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 x[iPage], &info)
16a9f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 ;. assert( me
16aa0 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 mcmp(&info, &pCu
16aa1 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 r->info, sizeof(
16aa2 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d info))==0 );. }
16aa3 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
16aa4 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 assertCellInfo(
16aa5 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 x).#endif.#ifdef
16aa6 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 _MSC_VER. /* U
16aa7 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 se a real functi
16aa8 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f on in MSVC to wo
16aa9 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 rk around bugs i
16aaa 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e n that compiler.
16aab 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 */. static voi
16aac 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 d getCellInfo(Bt
16aad 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
16aae 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 if( pCur->inf
16aaf 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 o.nSize==0 ){.
16ab0 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 int iPage =
16ab1 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 pCur->iPage;.
16ab2 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c btreeParseCel
16ab3 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 l(pCur->apPage[i
16ab4 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 Page],pCur->aiId
16ab5 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e x[iPage],&pCur->
16ab6 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 info);. pCu
16ab7 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 r->validNKey = 1
16ab8 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
16ab9 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 assertCellInf
16aba 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 o(pCur);. }.
16abb 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e }.#else /* if n
16abc 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 ot _MSC_VER */.
16abd 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 /* Use a macro
16abe 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d in all other com
16abf 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 pilers so that t
16ac0 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 he function is i
16ac1 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e nlined */.#defin
16ac2 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 e getCellInfo(pC
16ac3 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20 ur)
16ac4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ac5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ac6 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 \. if(
16ac7 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
16ac8 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 e==0 ){
16ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16aca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16acb 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
16acc 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 int iPage = pCur
16acd 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 ->iPage;
16ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16acf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ad0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
16ad1 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 btreeParseCell(
16ad2 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 pCur->apPage[iPa
16ad3 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b ge],pCur->aiIdx[
16ad4 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e iPage],&pCur->in
16ad5 66 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d fo); \. pCur-
16ad6 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 >validNKey = 1;
16ad7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ad8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ada 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b \. }else{
16adb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16adc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16add 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ade 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16adf 20 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73 \. ass
16ae0 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 ertCellInfo(pCur
16ae1 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
16ae2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ae3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ae4 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 \. }.#e
16ae5 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 ndif /* _MSC_VER
16ae6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 */..#ifndef NDE
16ae7 42 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 BUG /* The next
16ae8 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f 6e routine used on
16ae9 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 ly within assert
16aea 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f () statements */
16aeb 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
16aec 75 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 ue if the given
16aed 42 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c 69 BtCursor is vali
16aee 64 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72 73 d. A valid curs
16aef 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61 or is one.** tha
16af0 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 t is currently p
16af1 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 ointing to a row
16af2 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 in a (non-empty
16af3 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73 ) table..** This
16af4 20 69 73 20 61 20 76 65 72 69 66 69 63 61 74 69 is a verificati
16af5 6f 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 on routine is us
16af6 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 ed only within a
16af7 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
16af8 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ts..*/.SQLITE_PR
16af9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
16afa 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 3BtreeCursorIsVa
16afb 6c 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 lid(BtCursor *pC
16afc 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 ur){. return pC
16afd 75 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 ur && pCur->eSta
16afe 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
16aff 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 ;.}.#endif /* ND
16b00 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 EBUG */../*.** S
16b01 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 et *pSize to the
16b02 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 size of the buf
16b03 66 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f fer needed to ho
16b04 6c 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a ld the value of.
16b05 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74 ** the key for t
16b06 68 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 he current entry
16b07 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 . If the cursor
16b08 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 is not pointing
16b09 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65 .** to a valid e
16b0a 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 ntry, *pSize is
16b0b 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a set to 0. .**.**
16b0c 20 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74 For a table wit
16b0d 68 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 h the INTKEY fla
16b0e 67 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74 g set, this rout
16b0f 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
16b10 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e key.** itself, n
16b11 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ot the number of
16b12 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65 bytes in the ke
16b13 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c y..**.** The cal
16b14 6c 65 72 20 6d 75 73 74 20 70 6f 73 69 74 69 6f ler must positio
16b15 6e 20 74 68 65 20 63 75 72 73 6f 72 20 70 72 69 n the cursor pri
16b16 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 or to invoking t
16b17 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 his routine..**
16b18 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
16b19 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 cannot fail. I
16b1a 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 t always returns
16b1b 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f SQLITE_OK. .*/
16b1c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16b1d 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16b1e 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 KeySize(BtCursor
16b1f 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69 *pCur, i64 *pSi
16b20 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 ze){. assert( c
16b21 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16b22 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
16b23 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16b24 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
16b25 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d || pCur->eState=
16b26 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16b27 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
16b28 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
16b29 44 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 D ){. *pSize
16b2a 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
16b2b 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 getCellInfo(pC
16b2c 75 72 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20 ur);. *pSize
16b2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 = pCur->info.nKe
16b2e 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 y;. }. return
16b2f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
16b30 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 .** Set *pSize t
16b31 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
16b32 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e bytes of data in
16b33 20 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a the entry the.*
16b34 2a 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 * cursor current
16b35 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a ly points to..**
16b36 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d .** The caller m
16b37 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 ust guarantee th
16b38 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 at the cursor is
16b39 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e pointing to a n
16b3a 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64 on-NULL.** valid
16b3b 20 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 65 entry. In othe
16b3c 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c r words, the cal
16b3d 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 6d ling procedure m
16b3e 75 73 74 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a ust guarantee.**
16b3f 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
16b40 20 68 61 73 20 43 75 72 73 6f 72 2e 65 53 74 61 has Cursor.eSta
16b41 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
16b42 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20 ..**.** Failure
16b43 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e is not possible.
16b44 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 This function
16b45 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 always returns S
16b46 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20 QLITE_OK..** It
16b47 6d 69 67 68 74 20 6a 75 73 74 20 61 73 20 77 65 might just as we
16b48 6c 6c 20 62 65 20 61 20 70 72 6f 63 65 64 75 72 ll be a procedur
16b49 65 20 28 72 65 74 75 72 6e 69 6e 67 20 76 6f 69 e (returning voi
16b4a 64 29 20 62 75 74 20 77 65 20 63 6f 6e 74 69 6e d) but we contin
16b4b 75 65 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 ue.** to return
16b4c 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c an integer resul
16b4d 74 20 63 6f 64 65 20 66 6f 72 20 68 69 73 74 6f t code for histo
16b4e 72 69 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a rical reasons..*
16b4f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16b50 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16b51 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 eDataSize(BtCurs
16b52 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 or *pCur, u32 *p
16b53 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Size){. assert(
16b54 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
16b55 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
16b56 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
16b57 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
16b58 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f );. getCellInfo
16b59 28 70 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65 (pCur);. *pSize
16b5a 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 = pCur->info.nD
16b5b 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ata;. return SQ
16b5c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
16b5d 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 65 * Given the page
16b5e 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 number of an ov
16b5f 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 erflow page in t
16b60 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61 72 he database (par
16b61 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c ameter.** ovfl),
16b62 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 this function f
16b63 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e 75 inds the page nu
16b64 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 mber of the next
16b65 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a page in the .**
16b66 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 linked list of
16b67 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 overflow pages.
16b68 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 If possible, it
16b69 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61 uses the auto-va
16b6a 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d cuum.** pointer-
16b6b 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 64 map data instead
16b6c 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 20 of reading the
16b6d 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 content of page
16b6e 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a ovfl to do so. .
16b6f 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
16b70 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69 r occurs an SQLi
16b71 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 te error code is
16b72 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
16b73 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 wise:.**.** The
16b74 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
16b75 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 he next overflow
16b76 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e page in the lin
16b77 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 ked list is .**
16b78 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e written to *pPgn
16b79 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f oNext. If page o
16b7a 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20 vfl is the last
16b7b 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b page in its link
16b7c 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 ed .** list, *pP
16b7d 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74 gnoNext is set t
16b7e 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 o zero. .**.** I
16b7f 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20 f ppPage is not
16b80 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 NULL, and a refe
16b81 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d rence to the Mem
16b82 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 Page object corr
16b83 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 esponding.** to
16b84 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 page number pOvf
16b85 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 l was obtained,
16b86 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20 then *ppPage is
16b87 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 set to point to
16b88 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 that.** referenc
16b89 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73 e. It is the res
16b8a 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 ponsibility of t
16b8b 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c he caller to cal
16b8c 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a l releasePage().
16b8d 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f ** on *ppPage to
16b8e 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72 65 free the refere
16b8f 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 nce. In no refer
16b90 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 ence was obtaine
16b91 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 d (because.** th
16b92 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 e pointer-map wa
16b93 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e s used to obtain
16b94 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a the value for *
16b95 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e pPgnoNext), then
16b96 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73 .** *ppPage is s
16b97 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 et to zero..*/.s
16b98 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65 tatic int getOve
16b99 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53 rflowPage(. BtS
16b9a 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 hared *pBt,
16b9b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
16b9c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
16b9d 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 /. Pgno ovfl,
16b9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b9f 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72 /* Current over
16ba0 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 flow page number
16ba1 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
16ba2 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 ppPage,
16ba3 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 /* OUT: MemPa
16ba4 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 ge handle (may b
16ba5 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e e NULL) */. Pgn
16ba6 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 o *pPgnoNext
16ba7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
16ba8 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 : Next overflow
16ba9 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 page number */.)
16baa 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 {. Pgno next =
16bab 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 0;. MemPage *pP
16bac 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 age = 0;. int r
16bad 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
16bae 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16baf 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
16bb0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
16bb1 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b sert(pPgnoNext);
16bb2 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
16bb3 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
16bb4 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e . /* Try to fin
16bb5 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 d the next page
16bb6 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 in the overflow
16bb7 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 list using the.
16bb8 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 ** autovacuum p
16bb9 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 ointer-map pages
16bba 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65 . Guess that the
16bbb 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 next page in .
16bbc 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 ** the overflow
16bbd 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 list is page nu
16bbe 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 mber (ovfl+1). I
16bbf 66 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72 f that guess tur
16bc0 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 ns . ** out to
16bc1 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 be wrong, fall b
16bc2 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 ack to loading t
16bc3 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 he data of page
16bc4 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 . ** number ovf
16bc5 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 l to determine t
16bc6 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d he next page num
16bc7 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ber.. */. if(
16bc8 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
16bc9 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f ){. Pgno pgno
16bca 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 ;. Pgno iGues
16bcb 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 s = ovfl+1;.
16bcc 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 u8 eType;.. w
16bcd 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 hile( PTRMAP_ISP
16bce 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 AGE(pBt, iGuess)
16bcf 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 || iGuess==PEND
16bd0 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
16bd1 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 t) ){. iGue
16bd2 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 ss++;. }..
16bd3 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 if( iGuess<=pag
16bd4 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
16bd5 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 ){. rc = p
16bd6 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 trmapGet(pBt, iG
16bd7 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 uess, &eType, &p
16bd8 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 gno);. if(
16bd9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
16bda 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f eType==PTRMAP_O
16bdb 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f VERFLOW2 && pgno
16bdc 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 ==ovfl ){.
16bdd 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b next = iGuess;
16bde 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
16bdf 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 LITE_DONE;.
16be0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e }. }. }.#en
16be1 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e dif.. assert( n
16be2 65 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 ext==0 || rc==SQ
16be3 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 LITE_DONE );. i
16be4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
16be5 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 ){. rc = btr
16be6 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f eeGetPage(pBt, o
16be7 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b vfl, &pPage, 0);
16be8 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d . assert( rc=
16be9 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 =SQLITE_OK || pP
16bea 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 age==0 );. if
16beb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
16bec 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 ){. next =
16bed 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e get4byte(pPage->
16bee 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 aData);. }.
16bef 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 }.. *pPgnoNext
16bf0 3d 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 70 = next;. if( pp
16bf1 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 50 Page ){. *ppP
16bf2 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d age = pPage;. }
16bf3 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 else{. releas
16bf4 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
16bf5 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d }. return (rc==
16bf6 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 SQLITE_DONE ? SQ
16bf7 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d LITE_OK : rc);.}
16bf8 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 ../*.** Copy dat
16bf9 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 a from a buffer
16bfa 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72 to a page, or fr
16bfb 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 62 om a page to a b
16bfc 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 uffer..**.** pPa
16bfd 79 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74 yload is a point
16bfe 65 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65 er to data store
16bff 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61 d on database pa
16c00 67 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 ge pDbPage..** I
16c01 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 f argument eOp i
16c02 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 s false, then nB
16c03 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 yte bytes of dat
16c04 61 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 a are copied.**
16c05 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f from pPayload to
16c06 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e the buffer poin
16c07 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e 20 ted at by pBuf.
16c08 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a If eOp is true,.
16c09 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50 ** then sqlite3P
16c0a 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63 agerWrite() is c
16c0b 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65 alled on pDbPage
16c0c 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73 and nByte bytes
16c0d 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20 .** of data are
16c0e 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 copied from the
16c0f 62 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 70 buffer pBuf to p
16c10 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 Payload..**.** S
16c11 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 QLITE_OK is retu
16c12 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c rned on success,
16c13 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 otherwise an er
16c14 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 ror code..*/.sta
16c15 74 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c tic int copyPayl
16c16 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 oad(. void *pPa
16c17 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20 yload,
16c18 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 /* Pointer to p
16c19 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f age data */. vo
16c1a 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 id *pBuf,
16c1b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
16c1c 65 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a er to buffer */.
16c1d 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 int nByte,
16c1e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
16c1f 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
16c20 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 o copy */. int
16c21 65 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 eOp,
16c22 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f /* 0 -> co
16c23 70 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 py from page, 1
16c24 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20 -> copy to page
16c25 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 */. DbPage *pDb
16c26 50 61 67 65 20 20 20 20 20 20 20 20 20 20 20 2f Page /
16c27 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e * Page containin
16c28 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b g pPayload */.){
16c29 0a 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 . if( eOp ){.
16c2a 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 /* Copy data f
16c2b 72 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61 rom buffer to pa
16c2c 67 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 72 ge (a write oper
16c2d 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e ation) */. in
16c2e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 t rc = sqlite3Pa
16c2f 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65 gerWrite(pDbPage
16c30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
16c31 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16c32 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
16c33 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 }. memcpy(pP
16c34 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 ayload, pBuf, nB
16c35 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 yte);. }else{.
16c36 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 /* Copy data
16c37 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66 from page to buf
16c38 66 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 72 fer (a read oper
16c39 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 ation) */. me
16c3a 6d 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c mcpy(pBuf, pPayl
16c3b 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d oad, nByte);. }
16c3c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
16c3d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 _OK;.}../*.** Th
16c3e 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
16c3f 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f sed to read or o
16c40 76 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64 verwrite payload
16c41 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
16c42 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 for the entry th
16c43 61 74 20 74 68 65 20 70 43 75 72 20 63 75 72 73 at the pCur curs
16c44 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
16c45 6f 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a o. If the eOp.**
16c46 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c parameter is 0,
16c47 20 74 68 69 73 20 69 73 20 61 20 72 65 61 64 20 this is a read
16c48 6f 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20 operation (data
16c49 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 copied into.** b
16c4a 75 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 20 uffer pBuf). If
16c4b 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 it is non-zero,
16c4c 61 20 77 72 69 74 65 20 28 64 61 74 61 20 63 6f a write (data co
16c4d 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 pied from.** buf
16c4e 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a fer pBuf)..**.**
16c4f 20 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 A total of "amt
16c50 22 20 62 79 74 65 73 20 61 72 65 20 72 65 61 64 " bytes are read
16c51 20 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67 69 or written begi
16c52 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74 nning at "offset
16c53 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65 "..** Data is re
16c54 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 ad to or from th
16c55 65 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a e buffer pBuf..*
16c56 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 *.** The content
16c57 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77 being read or w
16c58 72 69 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70 ritten might app
16c59 65 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 ear on the main
16c5a 70 61 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 page.** or be sc
16c5b 61 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d attered out on m
16c5c 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 ultiple overflow
16c5d 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 pages..**.** If
16c5e 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73 the BtCursor.is
16c5f 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 IncrblobHandle f
16c60 6c 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20 lag is set, and
16c61 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 the current.** c
16c62 75 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73 ursor entry uses
16c63 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 one or more ove
16c64 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69 rflow pages, thi
16c65 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c s function.** al
16c66 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f locates space fo
16c67 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 r and lazily pop
16c68 6c 75 61 74 65 73 20 74 68 65 20 6f 76 65 72 66 luates the overf
16c69 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a low page-list .*
16c6a 2a 20 63 61 63 68 65 20 61 72 72 61 79 20 28 42 * cache array (B
16c6b 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f tCursor.aOverflo
16c6c 77 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63 w). Subsequent c
16c6d 61 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a alls use this.**
16c6e 20 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 cache to make s
16c6f 65 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75 eeking to the su
16c70 70 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f pplied offset mo
16c71 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a re efficient..**
16c72 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 .** Once an over
16c73 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 flow page-list c
16c74 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c ache has been al
16c75 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20 located, it may
16c76 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 be.** invalidate
16c77 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 d if some other
16c78 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f cursor writes to
16c79 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c the same table,
16c7a 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 or if.** the cu
16c7b 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f rsor is moved to
16c7c 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77 a different row
16c7d 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 . Additionally,
16c7e 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a in auto-vacuum.*
16c7f 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c * mode, the foll
16c80 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 owing events may
16c81 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f invalidate an o
16c82 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
16c83 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 t cache..**.**
16c84 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 * An incrementa
16c85 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a l vacuum,.** *
16c86 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 A commit in aut
16c87 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 o_vacuum="full"
16c88 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 mode,.** * Cre
16c89 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d ating a table (m
16c8a 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e ay require movin
16c8b 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 g an overflow pa
16c8c 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ge)..*/.static i
16c8d 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 nt accessPayload
16c8e 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 (. BtCursor *pC
16c8f 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 ur, /* Curs
16c90 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 or pointing to e
16c91 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f ntry to read fro
16c92 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 m */. u32 offse
16c93 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 t, /* B
16c94 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69 egin reading thi
16c95 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f s far into paylo
16c96 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c ad */. u32 amt,
16c97 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16c98 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62 Read this many b
16c99 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e ytes */. unsign
16c9a 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f ed char *pBuf, /
16c9b 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 74 65 * Write the byte
16c9c 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 s into this buff
16c9d 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 er */ . int eOp
16c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16c9f 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e zero to read. n
16ca0 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 on-zero to write
16ca1 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e . */.){. unsign
16ca2 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 ed char *aPayloa
16ca3 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 d;. int rc = SQ
16ca4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e LITE_OK;. u32 n
16ca5 4b 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 Key;. int iIdx
16ca6 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a = 0;. MemPage *
16ca7 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
16ca8 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
16ca9 5d 3b 20 2f 2a 20 42 74 72 65 65 20 70 61 67 65 ]; /* Btree page
16caa 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72 of current entr
16cab 79 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 y */. BtShared
16cac 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 *pBt = pCur->pBt
16cad 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
16cae 20 20 20 2f 2a 20 42 74 72 65 65 20 74 68 69 73 /* Btree this
16caf 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 cursor belongs
16cb0 74 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 to */.. assert(
16cb1 20 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 pPage );. asse
16cb2 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
16cb3 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
16cb4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16cb5 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
16cb6 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c age]<pPage->nCel
16cb7 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 l );. assert( c
16cb8 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16cb9 70 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 pCur) );.. getC
16cba 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 ellInfo(pCur);.
16cbb 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 aPayload = pCur
16cbc 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 ->info.pCell + p
16cbd 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 Cur->info.nHeade
16cbe 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 r;. nKey = (pPa
16cbf 67 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a ge->intKey ? 0 :
16cc0 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f (int)pCur->info
16cc1 2e 6e 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e .nKey);.. if( N
16cc2 45 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 EVER(offset+amt
16cc3 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 > nKey+pCur->inf
16cc4 6f 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c 20 o.nData) . ||
16cc5 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e &aPayload[pCur->
16cc6 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 info.nLocal] > &
16cc7 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 pPage->aData[pBt
16cc8 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 ->usableSize].
16cc9 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 ){. /* Trying
16cca 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 to read or writ
16ccb 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
16ccc 66 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e f the data is an
16ccd 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 error */. re
16cce 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
16ccf 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 UPT_BKPT;. }..
16cd0 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 /* Check if dat
16cd1 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 a must be read/w
16cd2 72 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 ritten to/from t
16cd3 68 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74 he btree page it
16cd4 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f self. */. if( o
16cd5 66 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f ffset<pCur->info
16cd6 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 .nLocal ){. i
16cd7 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 nt a = amt;.
16cd8 69 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 if( a+offset>pCu
16cd9 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 r->info.nLocal )
16cda 7b 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 {. a = pCur
16cdb 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 ->info.nLocal -
16cdc 6f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 offset;. }.
16cdd 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f rc = copyPaylo
16cde 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 ad(&aPayload[off
16cdf 73 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 set], pBuf, a, e
16ce0 4f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 Op, pPage->pDbPa
16ce1 67 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 ge);. offset
16ce2 3d 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d = 0;. pBuf +=
16ce3 20 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 a;. amt -= a
16ce4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f ;. }else{. o
16ce5 66 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 ffset -= pCur->i
16ce6 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a nfo.nLocal;. }.
16ce7 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
16ce8 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b E_OK && amt>0 ){
16ce9 0a 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f . const u32 o
16cea 76 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 vflSize = pBt->u
16ceb 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 sableSize - 4;
16cec 2f 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 /* Bytes content
16ced 20 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a per ovfl page *
16cee 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 /. Pgno nextP
16cef 61 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 age;.. nextPa
16cf0 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 ge = get4byte(&a
16cf1 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e Payload[pCur->in
16cf2 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 fo.nLocal]);..#i
16cf3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16cf4 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f T_INCRBLOB. /
16cf5 2a 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 * If the isIncrb
16cf6 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 lobHandle flag i
16cf7 73 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 s set and the Bt
16cf8 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 Cursor.aOverflow
16cf9 5b 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f []. ** has no
16cfa 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 t been allocated
16cfb 2c 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f , allocate it no
16cfc 77 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20 w. The array is
16cfd 73 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 sized at. **
16cfe 6f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 one entry for ea
16cff 63 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ch overflow page
16d00 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 in the overflow
16d01 20 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 chain. The.
16d02 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f ** page number o
16d03 66 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 f the first over
16d04 66 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f flow page is sto
16d05 72 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 red in aOverflow
16d06 5b 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e [0],. ** etc.
16d07 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e A value of 0 in
16d08 20 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d the aOverflow[]
16d09 20 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f array means "no
16d0a 74 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 t yet known".
16d0b 20 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 ** (the cache i
16d0c 73 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 s lazily populat
16d0d 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ed).. */.
16d0e 69 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 if( pCur->isIncr
16d0f 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 blobHandle && !p
16d10 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 Cur->aOverflow )
16d11 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 {. int nOvf
16d12 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e l = (pCur->info.
16d13 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 nPayload-pCur->i
16d14 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 nfo.nLocal+ovflS
16d15 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b ize-1)/ovflSize;
16d16 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 . pCur->aOv
16d17 65 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a erflow = (Pgno *
16d18 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 )sqlite3MallocZe
16d19 72 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a ro(sizeof(Pgno)*
16d1a 6e 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a nOvfl);. /*
16d1b 20 6e 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 73 nOvfl is always
16d1c 20 70 6f 73 69 74 69 76 65 2e 20 20 49 66 20 69 positive. If i
16d1d 74 20 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 74 t were zero, fet
16d1e 63 68 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20 chPayload would
16d1f 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 have. ** be
16d20 65 6e 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 en used instead
16d21 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e of this routine.
16d22 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c */. if( AL
16d23 57 41 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 21 WAYS(nOvfl) && !
16d24 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
16d25 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
16d26 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
16d27 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
16d28 20 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66 /* If the overf
16d29 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
16d2a 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c che has been all
16d2b 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20 ocated and the.
16d2c 20 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 ** entry for
16d2d 74 68 65 20 66 69 72 73 74 20 72 65 71 75 69 72 the first requir
16d2e 65 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 ed overflow page
16d2f 20 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a is valid, skip.
16d30 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 ** directly
16d31 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 to it.. */.
16d32 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 if( pCur->aOve
16d33 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 rflow && pCur->a
16d34 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f Overflow[offset/
16d35 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20 ovflSize] ){.
16d36 20 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65 iIdx = (offse
16d37 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 t/ovflSize);.
16d38 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 nextPage = pC
16d39 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 ur->aOverflow[iI
16d3a 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 dx];. offse
16d3b 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c t = (offset%ovfl
16d3c 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e Size);. }.#en
16d3d 64 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20 dif.. for( ;
16d3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
16d3f 20 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 61 amt>0 && nextPa
16d40 67 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69 ge; iIdx++){..#i
16d41 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16d42 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 T_INCRBLOB.
16d43 20 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c /* If required,
16d44 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 populate the ov
16d45 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 erflow page-list
16d46 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 cache. */.
16d47 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 if( pCur->aOver
16d48 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 flow ){.
16d49 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f assert(!pCur->aO
16d4a 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c verflow[iIdx] ||
16d4b 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 pCur->aOverflow
16d4c 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 [iIdx]==nextPage
16d4d 29 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d );. pCur-
16d4e 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d >aOverflow[iIdx]
16d4f 20 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 = nextPage;.
16d50 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
16d51 20 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f if( offset>=o
16d52 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 vflSize ){.
16d53 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 /* The only r
16d54 65 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 eason to read th
16d55 69 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f 62 is page is to ob
16d56 74 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20 20 tain the page.
16d57 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 ** number
16d58 66 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61 67 for the next pag
16d59 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f e in the overflo
16d5a 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67 w chain. The pag
16d5b 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 e. ** dat
16d5c 61 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 a is not require
16d5d 64 2e 20 53 6f 20 66 69 72 73 74 20 74 72 79 20 d. So first try
16d5e 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 to lookup the ov
16d5f 65 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a erflow. *
16d60 2a 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 * page-list cach
16d61 65 2c 20 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 e, if any, then
16d62 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 fall back to the
16d63 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 getOverflowPage
16d64 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 (). ** fu
16d65 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 nction..
16d66 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
16d67 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
16d68 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72 if( pCur
16d69 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 ->aOverflow && p
16d6a 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
16d6b 49 64 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 Idx+1] ){.
16d6c 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 nextPage = p
16d6d 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
16d6e 49 64 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 Idx+1];.
16d6f 7d 20 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 } else .#endif.
16d70 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 rc = ge
16d71 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 tOverflowPage(pB
16d72 74 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 t, nextPage, 0,
16d73 26 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 &nextPage);.
16d74 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 offset -= ov
16d75 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 flSize;. }e
16d76 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
16d77 4e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 69 Need to read thi
16d78 73 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e s page properly.
16d79 20 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d It contains som
16d7a 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 e of the.
16d7b 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 ** range of dat
16d7c 61 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 a that is being
16d7d 72 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 read (eOp==0) or
16d7e 20 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 written (eOp!=0
16d7f 29 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 ).. */.
16d80 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 DbPage *pD
16d81 62 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 69 bPage;. i
16d82 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 nt a = amt;.
16d83 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16d84 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 PagerGet(pBt->pP
16d85 61 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 ager, nextPage,
16d86 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 &pDbPage);.
16d87 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
16d88 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
16d89 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c aPayload = sql
16d8a 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
16d8b 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 (pDbPage);.
16d8c 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 nextPage =
16d8d 67 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f 61 get4byte(aPayloa
16d8e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 d);. if
16d8f 28 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f ( a + offset > o
16d90 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 vflSize ){.
16d91 20 20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 a = ovflS
16d92 69 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 ize - offset;.
16d93 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
16d94 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 rc = copyPay
16d95 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f load(&aPayload[o
16d96 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 ffset+4], pBuf,
16d97 61 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29 a, eOp, pDbPage)
16d98 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
16d99 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 te3PagerUnref(pD
16d9a 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
16d9b 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 offset = 0;.
16d9c 20 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 amt -= a
16d9d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 66 ;. pBuf
16d9e 20 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 7d += a;. }
16d9f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
16da0 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 }.. if( rc==SQ
16da1 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 LITE_OK && amt>0
16da2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16da3 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
16da4 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e PT;. }. return
16da5 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
16da6 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b ad part of the k
16da7 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ey associated wi
16da8 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 th cursor pCur.
16da9 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 Exactly.** "amt
16daa 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 " bytes will be
16dab 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 transfered into
16dac 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 pBuf[]. The tra
16dad 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 nsfer.** begins
16dae 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a at "offset"..**.
16daf 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 ** The caller mu
16db0 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 st ensure that p
16db1 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 Cur is pointing
16db2 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a to a valid row.*
16db3 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a * in the table..
16db4 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
16db5 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
16db6 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f s or an error co
16db7 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 de if anything g
16db8 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 oes.** wrong. A
16db9 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 n error is retur
16dba 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 ned if "offset+a
16dbb 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 mt" is larger th
16dbc 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 an.** the availa
16dbd 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a ble payload..*/.
16dbe 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
16dbf 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b nt sqlite3BtreeK
16dc0 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ey(BtCursor *pCu
16dc1 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 r, u32 offset, u
16dc2 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 32 amt, void *pB
16dc3 75 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 uf){. assert( c
16dc4 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
16dc5 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
16dc6 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16dc7 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16dc8 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
16dc9 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 >iPage>=0 && pCu
16dca 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16dcb 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 iPage] );. asse
16dcc 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
16dcd 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 pCur->iPage]<pCu
16dce 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16dcf 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b iPage]->nCell );
16dd0 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 . return access
16dd1 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 Payload(pCur, of
16dd2 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 fset, amt, (unsi
16dd3 67 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c gned char*)pBuf,
16dd4 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 0);.}../*.** Re
16dd5 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 ad part of the d
16dd6 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
16dd7 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e ith cursor pCur.
16dd8 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d Exactly.** "am
16dd9 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 t" bytes will be
16dda 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f transfered into
16ddb 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 pBuf[]. The tr
16ddc 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 ansfer.** begins
16ddd 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a at "offset"..**
16dde 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
16ddf 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 E_OK on success
16de0 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 or an error code
16de1 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 if anything goe
16de2 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 s.** wrong. An
16de3 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 error is returne
16de4 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 d if "offset+amt
16de5 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e " is larger than
16de6 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c .** the availabl
16de7 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 e payload..*/.SQ
16de8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
16de9 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
16dea 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 a(BtCursor *pCur
16deb 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 , u32 offset, u3
16dec 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 2 amt, void *pBu
16ded 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 f){. int rc;..#
16dee 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16def 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 IT_INCRBLOB. if
16df0 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d ( pCur->eState=
16df1 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
16df2 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
16df3 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a LITE_ABORT;. }.
16df4 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 #endif.. assert
16df5 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16df6 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 ex(pCur) );. rc
16df7 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 = restoreCursor
16df8 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a Position(pCur);.
16df9 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
16dfa 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 _OK ){. asser
16dfb 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d t( pCur->eState=
16dfc 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b =CURSOR_VALID );
16dfd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
16dfe 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 r->iPage>=0 && p
16dff 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
16e00 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 ->iPage] );.
16e01 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 assert( pCur->ai
16e02 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
16e03 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 <pCur->apPage[pC
16e04 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
16e05 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 l );. rc = ac
16e06 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 cessPayload(pCur
16e07 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 , offset, amt, p
16e08 42 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 Buf, 0);. }. r
16e09 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
16e0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
16e0b 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 ter to payload i
16e0c 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 nformation from
16e0d 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 the entry that t
16e0e 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 he .** pCur curs
16e0f 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
16e10 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 o. The pointer
16e11 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e is to the beginn
16e12 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 ing of.** the ke
16e13 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 y if skipKey==0
16e14 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f and it points to
16e15 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
16e16 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 f data if.** ski
16e17 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 pKey==1. The nu
16e18 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
16e19 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 available key/d
16e1a 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a ata is written.*
16e1b 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 * into *pAmt. I
16e1c 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e f *pAmt==0, then
16e1d 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
16e1e 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a ned will not be.
16e1f 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 ** a valid point
16e20 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 er..**.** This r
16e21 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 outine is an opt
16e22 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 imization. It i
16e23 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 s common for the
16e24 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 entire key.** a
16e25 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f nd data to fit o
16e26 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 n the local page
16e27 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 and for there t
16e28 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 o be no overflow
16e29 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e .** pages. When
16e2a 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 that is so, thi
16e2b 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 s routine can be
16e2c 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 used to access
16e2d 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 the.** key and d
16e2e 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 ata without maki
16e2f 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 ng a copy. If t
16e30 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 he key and/or da
16e31 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 ta spills.** ont
16e32 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
16e33 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 , then accessPay
16e34 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 load() must be u
16e35 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c sed to reassembl
16e36 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 e.** the key/dat
16e37 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e a and copy it in
16e38 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 to a preallocate
16e39 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 d buffer..**.**
16e3a 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 The pointer retu
16e3b 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 rned by this rou
16e3c 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 tine looks direc
16e3d 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 tly into the cac
16e3e 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 hed.** page of t
16e3f 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
16e40 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 e data might cha
16e41 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 nge or move the
16e42 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 next time.** any
16e43 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 btree routine i
16e44 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
16e45 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
16e46 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 ed char *fetchPa
16e47 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f yload(. BtCurso
16e48 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a r *pCur, /*
16e49 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 Cursor pointing
16e4a 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 to entry to rea
16e4b 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 d from */. int
16e4c 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 *pAmt,
16e4d 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 /* Write the nu
16e4e 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c mber of availabl
16e4f 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a e bytes here */.
16e50 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 int skipKey
16e51 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 /* read b
16e52 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 eginning at data
16e53 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 if this is true
16e54 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 */.){. unsigne
16e55 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 d char *aPayload
16e56 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
16e57 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a ge;. u32 nKey;.
16e58 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 u32 nLocal;..
16e59 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 assert( pCur!=0
16e5a 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e && pCur->iPage>
16e5b 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 =0 && pCur->apPa
16e5c 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
16e5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16e5e 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
16e5f 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 _VALID );. asse
16e60 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
16e61 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
16e62 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
16e63 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
16e64 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 ];. assert( pCu
16e65 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
16e66 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 Page]<pPage->nCe
16e67 6c 6c 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 ll );. if( NEVE
16e68 52 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 R(pCur->info.nSi
16e69 7a 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 ze==0) ){. bt
16e6a 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 reeParseCell(pCu
16e6b 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16e6c 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 iPage], pCur->ai
16e6d 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
16e6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
16e6f 20 20 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f &pCur->info
16e70 29 3b 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 );. }. aPayloa
16e71 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 d = pCur->info.p
16e72 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 Cell;. aPayload
16e73 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e += pCur->info.n
16e74 48 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50 Header;. if( pP
16e75 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
16e76 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d nKey = 0;. }
16e77 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d else{. nKey =
16e78 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f (int)pCur->info
16e79 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 .nKey;. }. if(
16e7a 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 skipKey ){.
16e7b 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 aPayload += nKey
16e7c 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 ;. nLocal = p
16e7d 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
16e7e 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 - nKey;. }else
16e7f 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 {. nLocal = p
16e80 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c Cur->info.nLocal
16e81 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4c ;. assert( nL
16e82 6f 63 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 ocal<=nKey );.
16e83 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 }. *pAmt = nLoc
16e84 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61 al;. return aPa
16e85 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a yload;.}.../*.**
16e86 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 For the entry t
16e87 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20 hat cursor pCur
16e88 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 is point to, ret
16e89 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 urn as.** many b
16e8a 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 ytes of the key
16e8b 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61 or data as are a
16e8c 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 vailable on the
16e8d 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 local.** b-tree
16e8e 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 page. Write the
16e8f 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c number of avail
16e90 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 able bytes into
16e91 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 *pAmt..**.** The
16e92 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 pointer returne
16e93 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 d is ephemeral.
16e94 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 The key/data ma
16e95 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 y move.** or be
16e96 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 destroyed on the
16e97 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e next call to an
16e98 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c y Btree routine,
16e99 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 .** including ca
16e9a 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 lls from other t
16e9b 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74 hreads against t
16e9c 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a he same cache..*
16e9d 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 * Hence, a mutex
16e9e 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 on the BtShared
16e9f 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 should be held
16ea0 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 prior to calling
16ea1 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
16ea2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f ..**.** These ro
16ea3 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74 utines is used t
16ea4 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65 o get quick acce
16ea5 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 ss to key and da
16ea6 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d ta.** in the com
16ea7 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e mon case where n
16ea8 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
16ea9 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 are used..*/.SQ
16eaa 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
16eab 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
16eac 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 BtreeKeyFetch(Bt
16ead 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
16eae 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 t *pAmt){. cons
16eaf 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 t void *p = 0;.
16eb0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16eb1 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
16eb2 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
16eb3 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
16eb4 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16eb5 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 ex(pCur) );. if
16eb6 28 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 ( ALWAYS(pCur->e
16eb7 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
16eb8 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 LID) ){. p =
16eb9 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 (const void*)fet
16eba 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 chPayload(pCur,
16ebb 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 pAmt, 0);. }.
16ebc 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 return p;.}.SQLI
16ebd 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
16ebe 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 void *sqlite3Bt
16ebf 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43 reeDataFetch(BtC
16ec0 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 ursor *pCur, int
16ec1 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 *pAmt){. const
16ec2 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 void *p = 0;.
16ec3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16ec4 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d mutex_held(pCur-
16ec5 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 >pBtree->db->mut
16ec6 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
16ec7 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
16ec8 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 x(pCur) );. if(
16ec9 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 ALWAYS(pCur->eS
16eca 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
16ecb 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 ID) ){. p = (
16ecc 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 const void*)fetc
16ecd 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 hPayload(pCur, p
16ece 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 Amt, 1);. }. r
16ecf 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn p;.}.../*.
16ed0 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 ** Move the curs
16ed1 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 or down to a new
16ed2 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 child page. Th
16ed3 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 e newPgno argume
16ed4 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 nt is the.** pag
16ed5 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
16ed6 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f child page to mo
16ed7 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ve to..**.** Thi
16ed8 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
16ed9 6e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 ns SQLITE_CORRUP
16eda 54 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 65 T if the page-he
16edb 61 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64 ader flags field
16edc 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 of.** the new c
16edd 68 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e hild page does n
16ede 6f 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61 ot match the fla
16edf 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 gs field of the
16ee0 70 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 parent (i.e..**
16ee1 69 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 if an intkey pag
16ee2 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 e appears to be
16ee3 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20 the parent of a
16ee4 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c non-intkey page,
16ee5 20 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 or.** vice-vers
16ee6 61 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e a)..*/.static in
16ee7 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 t moveToChild(Bt
16ee8 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 Cursor *pCur, u3
16ee9 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 2 newPgno){. in
16eea 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 t rc;. int i =
16eeb 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d pCur->iPage;. M
16eec 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65 emPage *pNewPage
16eed 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
16eee 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a t = pCur->pBt;..
16eef 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
16ef0 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
16ef1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
16ef2 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
16ef3 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 OR_VALID );. as
16ef4 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 sert( pCur->iPag
16ef5 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 e<BTCURSOR_MAX_D
16ef6 45 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70 43 EPTH );. if( pC
16ef7 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 ur->iPage>=(BTCU
16ef8 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 RSOR_MAX_DEPTH-1
16ef9 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
16efa 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
16efb 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 KPT;. }. rc =
16efc 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 getAndInitPage(p
16efd 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e Bt, newPgno, &pN
16efe 65 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 ewPage);. if( r
16eff 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
16f00 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b pCur->apPage[i+
16f01 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 1] = pNewPage;.
16f02 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 pCur->aiIdx[i+1
16f03 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 ] = 0;. pCur->i
16f04 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d Page++;.. pCur-
16f05 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b >info.nSize = 0;
16f06 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b . pCur->validNK
16f07 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e ey = 0;. if( pN
16f08 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 ewPage->nCell<1
16f09 7c 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 || pNewPage->int
16f0a 4b 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 Key!=pCur->apPag
16f0b 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a e[i]->intKey ){.
16f0c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16f0d 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
16f0e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
16f0f 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 ITE_OK;.}..#ifnd
16f10 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ef NDEBUG./*.**
16f11 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 20 Page pParent is
16f12 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e an internal (non
16f13 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 65 -leaf) tree page
16f14 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
16f15 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 74 .** asserts that
16f16 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 page number iCh
16f17 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d ild is the left-
16f18 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 64 child if the iId
16f19 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 x'th.** cell in
16f1a 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 page pParent. Or
16f1b 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 71 75 , if iIdx is equ
16f1c 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 al to the total
16f1d 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c number of.** cel
16f1e 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 ls in pParent, t
16f1f 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 hat page number
16f20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 69 iChild is the ri
16f21 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 ght-child of.**
16f22 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 the page..*/.sta
16f23 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50 tic void assertP
16f24 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 arentIndex(MemPa
16f25 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 ge *pParent, int
16f26 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 iIdx, Pgno iChi
16f27 6c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 ld){. assert( i
16f28 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 Idx<=pParent->nC
16f29 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64 ell );. if( iId
16f2a 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c x==pParent->nCel
16f2b 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 l ){. assert(
16f2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65 get4byte(&pPare
16f2d 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e nt->aData[pParen
16f2e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 t->hdrOffset+8])
16f2f 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 ==iChild );. }e
16f30 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
16f31 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 get4byte(findCe
16f32 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 ll(pParent, iIdx
16f33 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 ))==iChild );.
16f34 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 }.}.#else.# def
16f35 69 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e 74 ine assertParent
16f36 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 Index(x,y,z) .#e
16f37 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 ndif../*.** Move
16f38 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 the cursor up t
16f39 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 o the parent pag
16f3a 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 e..**.** pCur->i
16f3b 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 dx is set to the
16f3c 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 cell index that
16f3d 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f contains the po
16f3e 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 inter.** to the
16f3f 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69 page we are comi
16f40 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 ng from. If we
16f41 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 are coming from
16f42 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 the.** right-mos
16f43 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 65 t child page the
16f44 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 n pCur->idx is s
16f45 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 et to one more t
16f46 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 han.** the large
16f47 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a st cell index..*
16f48 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f /.static void mo
16f49 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 veToParent(BtCur
16f4a 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 sor *pCur){. as
16f4b 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
16f4c 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
16f4d 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16f4e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
16f4f 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 ALID );. assert
16f50 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 ( pCur->iPage>0
16f51 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
16f52 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
16f53 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 iPage] );. asse
16f54 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 rtParentIndex(.
16f55 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b pCur->apPage[
16f56 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 pCur->iPage-1],
16f57 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 . pCur->aiIdx
16f58 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c [pCur->iPage-1],
16f59 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 . pCur->apPa
16f5a 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
16f5b 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c >pgno. );. rel
16f5c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 easePage(pCur->a
16f5d 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
16f5e 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 e]);. pCur->iPa
16f5f 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e ge--;. pCur->in
16f60 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
16f61 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
16f62 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f = 0;.}../*.** Mo
16f63 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ve the cursor to
16f64 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f point to the ro
16f65 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73 20 62 ot page of its b
16f66 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e -tree structure.
16f67 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 .**.** If the ta
16f68 62 6c 65 20 68 61 73 20 61 20 76 69 72 74 75 61 ble has a virtua
16f69 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 l root page, the
16f6a 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 n the cursor is
16f6b 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a moved to point.*
16f6c 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c * to the virtual
16f6d 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65 root page inste
16f6e 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c ad of the actual
16f6f 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61 root page. A ta
16f70 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 72 ble has a.** vir
16f71 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 77 tual root page w
16f72 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20 72 hen the actual r
16f73 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69 6e oot page contain
16f74 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61 s no cells and a
16f75 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c .** single chil
16f76 64 20 70 61 67 65 2e 20 54 68 69 73 20 63 61 6e d page. This can
16f77 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 74 only happen wit
16f78 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 h the table root
16f79 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a ed at page 1..**
16f7a 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 .** If the b-tre
16f7b 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 65 e structure is e
16f7c 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73 6f 72 mpty, the cursor
16f7d 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f state is set to
16f7e 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 .** CURSOR_INVA
16f7f 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 LID. Otherwise,
16f80 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 73 65 the cursor is se
16f81 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 t to point to th
16f82 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20 e first.** cell
16f83 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72 located on the r
16f84 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61 6c 20 oot (or virtual
16f85 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20 74 root) page and t
16f86 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 0a he cursor state.
16f87 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 ** is set to CUR
16f88 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a SOR_VALID..**.**
16f89 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f If this functio
16f8a 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 n returns succes
16f8b 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 sfully, it may b
16f8c 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 e assumed that t
16f8d 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 he.** page-heade
16f8e 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65 r flags indicate
16f8f 20 74 68 61 74 20 74 68 65 20 5b 76 69 72 74 75 that the [virtu
16f90 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 al] root-page is
16f91 20 74 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a the expected .*
16f92 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 * kind of b-tree
16f93 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77 page (i.e. if w
16f94 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 hen opening the
16f95 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 cursor the calle
16f96 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 r did not.** spe
16f97 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 cify a KeyInfo s
16f98 74 72 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 tructure the fla
16f99 67 73 20 62 79 74 65 20 69 73 20 73 65 74 20 74 gs byte is set t
16f9a 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a o 0x05 or 0x0D,.
16f9b 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20 ** indicating a
16f9c 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 table b-tree, or
16f9d 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64 if the caller d
16f9e 69 64 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 id specify a Key
16f9f 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 Info .** structu
16fa0 72 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 re the flags byt
16fa1 65 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 32 e is set to 0x02
16fa2 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61 or 0x0A, indica
16fa3 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a ting an index.**
16fa4 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 b-tree)..*/.sta
16fa5 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f tic int moveToRo
16fa6 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ot(BtCursor *pCu
16fa7 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 r){. MemPage *p
16fa8 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d Root;. int rc =
16fa9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 SQLITE_OK;. Bt
16faa 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 ree *p = pCur->p
16fab 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 Btree;. BtShare
16fac 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
16fad 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
16fae 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
16faf 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
16fb0 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c CURSOR_INVALID <
16fb1 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
16fb2 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 EEK );. assert(
16fb3 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 CURSOR_VALID
16fb4 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 < CURSOR_REQUIRE
16fb5 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 SEEK );. assert
16fb6 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 ( CURSOR_FAULT
16fb7 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 > CURSOR_REQUIR
16fb8 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 ESEEK );. if( p
16fb9 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 Cur->eState>=CUR
16fba 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 SOR_REQUIRESEEK
16fbb 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d ){. if( pCur-
16fbc 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
16fbd 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 FAULT ){. a
16fbe 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 ssert( pCur->ski
16fbf 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b pNext!=SQLITE_OK
16fc0 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e );. return
16fc1 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b pCur->skipNext;
16fc2 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
16fc3 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 e3BtreeClearCurs
16fc4 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 or(pCur);. }..
16fc5 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 if( pCur->iPage
16fc6 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 >=0 ){. int i
16fc7 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 ;. for(i=1; i
16fc8 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 <=pCur->iPage; i
16fc9 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 ++){. relea
16fca 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 sePage(pCur->apP
16fcb 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 age[i]);. }.
16fcc 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d pCur->iPage =
16fcd 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
16fce 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 rc = getAndInit
16fcf 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e Page(pBt, pCur->
16fd0 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d pgnoRoot, &pCur-
16fd1 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20 >apPage[0]);.
16fd2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16fd3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 OK ){. pCur
16fd4 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f ->eState = CURSO
16fd5 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 R_INVALID;.
16fd6 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
16fd7 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 }. pCur->iPag
16fd8 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 e = 0;.. /* I
16fd9 66 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f f pCur->pKeyInfo
16fda 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 is not NULL, th
16fdb 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 en the caller th
16fdc 61 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63 at opened this c
16fdd 75 72 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70 ursor. ** exp
16fde 65 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 ected to open it
16fdf 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 on an index b-t
16fe0 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ree. Otherwise,
16fe1 69 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 if pKeyInfo is.
16fe2 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 ** NULL, the
16fe3 63 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61 caller expects a
16fe4 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 table b-tree. I
16fe5 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 f this is not th
16fe6 65 20 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72 e case,. ** r
16fe7 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f eturn an SQLITE_
16fe8 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20 20 CORRUPT error.
16fe9 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
16fea 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e Cur->apPage[0]->
16feb 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75 intKey==1 || pCu
16fec 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e r->apPage[0]->in
16fed 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69 tKey==0 );. i
16fee 66 28 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e f( (pCur->pKeyIn
16fef 66 6f 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70 fo==0)!=pCur->ap
16ff0 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 Page[0]->intKey
16ff1 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
16ff2 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
16ff3 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a KPT;. }. }..
16ff4 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
16ff5 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 the root page i
16ff6 73 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 s of the correct
16ff7 20 74 79 70 65 2e 20 54 68 69 73 20 6d 75 73 74 type. This must
16ff8 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 73 be the. ** cas
16ff9 65 20 61 73 20 74 68 65 20 63 61 6c 6c 20 74 6f e as the call to
16ffa 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 this function t
16ffb 68 61 74 20 6c 6f 61 64 65 64 20 74 68 65 20 72 hat loaded the r
16ffc 6f 6f 74 2d 70 61 67 65 20 28 65 69 74 68 65 72 oot-page (either
16ffd 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 . ** this call
16ffe 6f 72 20 61 20 70 72 65 76 69 6f 75 73 20 69 6e or a previous in
16fff 76 6f 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20 vocation) would
17000 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f have detected co
17001 72 72 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 rruption . ** i
17002 66 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e f the assumption
17003 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 were not true,
17004 61 6e 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f and it is not po
17005 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 66 ssible for the f
17006 6c 61 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 20 lags . ** byte
17007 74 6f 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 to have been mod
17008 69 66 69 65 64 20 77 68 69 6c 65 20 74 68 69 73 ified while this
17009 20 63 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 69 cursor is holdi
1700a 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 ng a reference.
1700b 20 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 2e ** to the page.
1700c 20 20 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 */. pRoot = p
1700d 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a Cur->apPage[0];.
1700e 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d assert( pRoot-
1700f 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e >pgno==pCur->pgn
17010 6f 52 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 oRoot );. asser
17011 74 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 t( pRoot->isInit
17012 20 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 && (pCur->pKeyI
17013 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e nfo==0)==pRoot->
17014 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75 intKey );.. pCu
17015 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b r->aiIdx[0] = 0;
17016 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 . pCur->info.nS
17017 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d ize = 0;. pCur-
17018 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 >atLast = 0;. p
17019 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
1701a 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 0;.. if( pRoot
1701b 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 ->nCell==0 && !p
1701c 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Root->leaf ){.
1701d 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a Pgno subpage;.
1701e 20 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70 if( pRoot->p
1701f 67 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e 20 gno!=1 ) return
17020 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
17021 4b 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67 65 KPT;. subpage
17022 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f = get4byte(&pRo
17023 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d ot->aData[pRoot-
17024 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a >hdrOffset+8]);.
17025 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
17026 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b = CURSOR_VALID;
17027 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
17028 43 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70 Child(pCur, subp
17029 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 age);. }else{.
1702a 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 pCur->eState
1702b 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c = ((pRoot->nCell
1702c 3e 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 >0)?CURSOR_VALID
1702d 3a 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 :CURSOR_INVALID)
1702e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1702f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 c;.}../*.** Move
17030 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e the cursor down
17031 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 to the left-mos
17032 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e t leaf entry ben
17033 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 eath the.** entr
17034 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 y to which it is
17035 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
17036 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c ing..**.** The l
17037 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 eft-most leaf is
17038 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 the one with th
17039 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d e smallest key -
1703a 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e the first.** in
1703b 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 ascending order
1703c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1703d 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 moveToLeftmost(B
1703e 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
1703f 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 Pgno pgno;. i
17040 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
17041 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 K;. MemPage *pP
17042 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 age;.. assert(
17043 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
17044 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
17045 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
17046 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
17047 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 ;. while( rc==S
17048 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 QLITE_OK && !(pP
17049 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 age = pCur->apPa
1704a 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 ge[pCur->iPage])
1704b 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73 ->leaf ){. as
1704c 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
1704d 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 x[pCur->iPage]<p
1704e 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
1704f 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 pgno = get4by
17050 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 te(findCell(pPag
17051 65 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 e, pCur->aiIdx[p
17052 43 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 Cur->iPage]));.
17053 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
17054 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b ild(pCur, pgno);
17055 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
17056 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 ;.}../*.** Move
17057 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 the cursor down
17058 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 to the right-mos
17059 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e t leaf entry ben
1705a 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 eath the.** page
1705b 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 to which it is
1705c 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
1705d 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20 ng. Notice the
1705e 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 difference.** be
1705f 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 tween moveToLeft
17060 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 most() and moveT
17061 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d oRightmost(). m
17062 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a oveToLeftmost().
17063 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66 ** finds the lef
17064 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e t-most entry ben
17065 65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a eath the *entry*
17066 20 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 whereas moveToR
17067 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 ightmost().** fi
17068 6e 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f nds the right-mo
17069 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 st entry beneath
1706a 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a the *page*..**.
1706b 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 ** The right-mos
1706c 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f t entry is the o
1706d 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 ne with the larg
1706e 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 est key - the la
1706f 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 st.** key in asc
17070 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f ending order..*/
17071 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 .static int move
17072 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75 ToRightmost(BtCu
17073 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 rsor *pCur){. P
17074 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 gno pgno;. int
17075 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
17076 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
17077 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 = 0;.. assert(
17078 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
17079 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
1707a 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
1707b 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
1707c 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d );. while( rc==
1707d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 SQLITE_OK && !(p
1707e 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
1707f 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
17080 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 )->leaf ){. p
17081 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 gno = get4byte(&
17082 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
17083 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
17084 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 );. pCur->aiI
17085 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
17086 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
17087 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
17088 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 hild(pCur, pgno)
17089 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d ;. }. if( rc==
1708a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1708b 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
1708c 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 r->iPage] = pPag
1708d 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 e->nCell-1;.
1708e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 pCur->info.nSize
1708f 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e = 0;. pCur->
17090 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 validNKey = 0;.
17091 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
17092 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 }../* Move the c
17093 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 ursor to the fir
17094 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
17095 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 table. Return S
17096 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 QLITE_OK.** on s
17097 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 uccess. Set *pR
17098 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 es to 0 if the c
17099 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 ursor actually p
1709a 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 oints to somethi
1709b 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 ng.** or set *pR
1709c 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 es to 1 if the t
1709d 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a able is empty..*
1709e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1709f 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
170a0 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 eFirst(BtCursor
170a1 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 *pCur, int *pRes
170a2 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 ){. int rc;..
170a3 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
170a4 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
170a5 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
170a6 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
170a7 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d Cur->pBtree->db-
170a8 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 >mutex) );. rc
170a9 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 = moveToRoot(pCu
170aa 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 r);. if( rc==SQ
170ab 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
170ac 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d f( pCur->eState=
170ad 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
170ae 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
170af 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
170b0 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
170b1 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 l==0 );. *p
170b2 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 Res = 1;. r
170b3 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
170b4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
170b5 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
170b6 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
170b7 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 ]->nCell>0 );.
170b8 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
170b9 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f rc = moveTo
170ba 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a Leftmost(pCur);.
170bb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
170bc 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 rn rc;.}../* Mov
170bd 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 e the cursor to
170be 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 the last entry i
170bf 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 n the table. Re
170c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a turn SQLITE_OK.*
170c1 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 * on success. S
170c2 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 et *pRes to 0 if
170c3 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 the cursor actu
170c4 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 ally points to s
170c5 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 omething.** or s
170c6 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 et *pRes to 1 if
170c7 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d the table is em
170c8 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 pty..*/.SQLITE_P
170c9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
170ca 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 e3BtreeLast(BtCu
170cb 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 rsor *pCur, int
170cc 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 *pRes){. int rc
170cd 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 ;. . assert( cu
170ce 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
170cf 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
170d0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
170d1 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 held(pCur->pBtre
170d2 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b e->db->mutex) );
170d3 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 .. /* If the cu
170d4 72 73 6f 72 20 61 6c 72 65 61 64 79 20 70 6f 69 rsor already poi
170d5 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 nts to the last
170d6 65 6e 74 72 79 2c 20 74 68 69 73 20 69 73 20 61 entry, this is a
170d7 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 no-op. */. if(
170d8 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 CURSOR_VALID==p
170d9 43 75 72 2d 3e 65 53 74 61 74 65 20 26 26 20 70 Cur->eState && p
170da 43 75 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23 Cur->atLast ){.#
170db 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
170dc 55 47 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 UG. /* This b
170dd 6c 6f 63 6b 20 73 65 72 76 65 73 20 74 6f 20 61 lock serves to a
170de 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 ssert() that the
170df 20 63 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 64 cursor really d
170e0 6f 65 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a oes point . *
170e1 2a 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e * to the last en
170e2 74 72 79 20 69 6e 20 74 68 65 20 62 2d 74 72 65 try in the b-tre
170e3 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 e. */. int ii
170e4 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 ;. for(ii=0;
170e5 69 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 ii<pCur->iPage;
170e6 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 ii++){. ass
170e7 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
170e8 5b 69 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 [ii]==pCur->apPa
170e9 67 65 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b ge[ii]->nCell );
170ea 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
170eb 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 t( pCur->aiIdx[p
170ec 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75 Cur->iPage]==pCu
170ed 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
170ee 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 iPage]->nCell-1
170ef 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
170f0 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
170f1 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29 ->iPage]->leaf )
170f2 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 ;.#endif. ret
170f3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
170f4 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 }.. rc = moveT
170f5 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 oRoot(pCur);. i
170f6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
170f7 20 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 ){. if( CURS
170f8 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 OR_INVALID==pCur
170f9 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 ->eState ){.
170fa 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
170fb 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
170fc 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b ge]->nCell==0 );
170fd 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 . *pRes = 1
170fe 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
170ff 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
17100 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
17101 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a VALID );. *
17102 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 pRes = 0;.
17103 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 rc = moveToRight
17104 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 most(pCur);.
17105 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d pCur->atLast =
17106 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f rc==SQLITE_OK ?
17107 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 1:0;. }. }.
17108 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
17109 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
1710a 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 r so that it poi
1710b 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 nts to an entry
1710c 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a near the key .**
1710d 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49 specified by pI
1710e 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e dxKey or intKey.
1710f 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 Return a succ
17110 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ess code..**.**
17111 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 For INTKEY table
17112 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 s, the intKey pa
17113 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e rameter is used.
17114 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 pIdxKey .** mu
17115 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 st be NULL. For
17116 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 index tables, p
17117 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 IdxKey is used a
17118 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 nd intKey.** is
17119 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 ignored..**.** I
1711a 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 f an exact match
1711b 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 is not found, t
1711c 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 hen the cursor i
1711d 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 s always.** left
1711e 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c pointing at a l
1711f 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77 eaf page which w
17120 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e ould hold the en
17121 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 try if it.** wer
17122 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 e present. The
17123 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 cursor might poi
17124 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 nt to an entry t
17125 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 hat comes.** bef
17126 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 ore or after the
17127 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 key..**.** An i
17128 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65 nteger is writte
17129 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 n into *pRes whi
1712a 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 ch is the result
1712b 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 of.** comparing
1712c 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 the key with th
1712d 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 e entry to which
1712e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a the cursor is .
1712f 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 ** pointing. Th
17130 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 e meaning of the
17131 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e integer written
17132 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 into.** *pRes i
17133 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
17134 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 .** *pRes<0
17135 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 The cursor
17136 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
17137 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 at an entry tha
17138 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
17139 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 is smaller
1713a 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 than intKey/pId
1713b 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 xKey or if the t
1713c 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a able is empty.**
1713d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1713e 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 and the cursor
1713f 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 is therefore le
17140 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 ft point to noth
17141 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a ing..**.** *
17142 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 pRes==0 The
17143 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 cursor is left p
17144 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e ointing at an en
17145 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 try that.**
17146 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61 exa
17147 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 ctly matches int
17148 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a Key/pIdxKey..**.
17149 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 ** *pRes>0
1714a 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 The cursor i
1714b 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 s left pointing
1714c 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 at an entry that
1714d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1714e 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 is larger t
1714f 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b han intKey/pIdxK
17150 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 ey..**.*/.SQLITE
17151 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
17152 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
17153 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 npacked(. BtCur
17154 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 sor *pCur,
17155 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f /* The curso
17156 72 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f r to be moved */
17157 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
17158 64 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 d *pIdxKey, /* U
17159 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 npacked index ke
1715a 79 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 y */. i64 intKe
1715b 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y,
1715c 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 /* The table key
1715d 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 */. int biasRi
1715e 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f ght, /
1715f 2a 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 * If true, bias
17160 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 the search to th
17161 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 e high end */.
17162 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 int *pRes
17163 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
17164 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 e search results
17165 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e here */.){. in
17166 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 t rc;.. assert(
17167 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
17168 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
17169 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1716a 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 ex_held(pCur->pB
1716b 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 tree->db->mutex)
1716c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 );. assert( pR
1716d 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 es );. assert(
1716e 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 (pIdxKey==0)==(p
1716f 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 Cur->pKeyInfo==0
17170 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ) );.. /* If th
17171 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 e cursor is alre
17172 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 ady positioned a
17173 74 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61 t the point we a
17174 72 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 re trying. ** t
17175 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 o move to, then
17176 6a 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68 just return with
17177 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f out doing any wo
17178 72 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 rk */. if( pCur
17179 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
1717a 5f 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e _VALID && pCur->
1717b 76 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 validNKey . &&
1717c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
1717d 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 ->intKey . ){.
1717e 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 if( pCur->inf
1717f 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 o.nKey==intKey )
17180 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 {. *pRes =
17181 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 0;. return
17182 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
17183 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 . if( pCur->a
17184 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69 tLast && pCur->i
17185 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 nfo.nKey<intKey
17186 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d ){. *pRes =
17187 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 -1;. retur
17188 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
17189 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d }. }.. rc = m
1718a 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b oveToRoot(pCur);
1718b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
1718c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
1718d 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
1718e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1718f 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge] );. assert(
17190 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
17191 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e ur->iPage]->isIn
17192 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 it );. assert(
17193 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
17194 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
17195 3e 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 >0 || pCur->eSta
17196 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c te==CURSOR_INVAL
17197 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 ID );. if( pCur
17198 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
17199 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 _INVALID ){.
1719a 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 *pRes = -1;.
1719b 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 assert( pCur->ap
1719c 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1719d 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 ]->nCell==0 );.
1719e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1719f 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 _OK;. }. asser
171a0 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
171a1 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 0]->intKey || pI
171a2 64 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b dxKey );. for(;
171a3 3b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c ;){. int lwr,
171a4 20 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 upr;. Pgno c
171a5 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 hldPg;. MemPa
171a6 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 ge *pPage = pCur
171a7 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
171a8 50 61 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 Page];. int c
171a9 3b 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 2d ;.. /* pPage-
171aa 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 67 >nCell must be g
171ab 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f reater than zero
171ac 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 . If this is the
171ad 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a root-page. *
171ae 2a 20 74 68 65 20 63 75 72 73 6f 72 20 77 6f 75 * the cursor wou
171af 6c 64 20 68 61 76 65 20 62 65 65 6e 20 49 4e 56 ld have been INV
171b0 41 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 20 74 ALID above and t
171b1 68 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 his for(;;) loop
171b2 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e . ** not run.
171b3 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 If this is not
171b4 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74 the root-page, t
171b5 68 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 68 hen the moveToCh
171b6 69 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 ild() routine.
171b7 20 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 ** would have
171b8 61 6c 72 65 61 64 79 20 64 65 74 65 63 74 65 64 already detected
171b9 20 64 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 db corruption.
171ba 53 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 Similarly, pPage
171bb 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 must. ** be
171bc 74 68 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28 the right kind (
171bd 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20 index or table)
171be 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 of b-tree page.
171bf 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a Otherwise. **
171c0 20 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 a moveToChild()
171c1 20 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 or moveToRoot()
171c2 20 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 call would have
171c3 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 detected corrup
171c4 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 tion. */. as
171c5 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 sert( pPage->nCe
171c6 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 ll>0 );. asse
171c7 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 rt( pPage->intKe
171c8 79 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 20 y==(pIdxKey==0)
171c9 29 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a );. lwr = 0;.
171ca 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d upr = pPage-
171cb 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 >nCell-1;. if
171cc 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20 ( biasRight ){.
171cd 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 pCur->aiIdx
171ce 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 [pCur->iPage] =
171cf 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 (u16)upr;. }e
171d0 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d lse{. pCur-
171d1 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
171d2 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72 ge] = (u16)((upr
171d3 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a +lwr)/2);. }.
171d4 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 for(;;){.
171d5 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 int idx = pCu
171d6 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
171d7 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 Page]; /* Index
171d8 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 of current cell
171d9 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 in pPage */.
171da 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 u8 *pCell;
171db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
171dc 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
171dd 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c to current cell
171de 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 in pPage */..
171df 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e pCur->info.n
171e0 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 Size = 0;.
171e1 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
171e2 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70 (pPage, idx) + p
171e3 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
171e4 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 ze;. if( pP
171e5 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 age->intKey ){.
171e6 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c i64 nCell
171e7 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 Key;. if(
171e8 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 pPage->hasData
171e9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 ){. u32
171ea 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 dummy;.
171eb 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 pCell += getVa
171ec 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 rint32(pCell, du
171ed 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a mmy);. }.
171ee 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e getVarin
171ef 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 t(pCell, (u64*)&
171f0 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 nCellKey);.
171f1 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d if( nCellKey=
171f2 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 =intKey ){.
171f3 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 c = 0;.
171f4 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 }else if( nC
171f5 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b ellKey<intKey ){
171f6 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d . c = -
171f7 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 1;. }else
171f8 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
171f9 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 rt( nCellKey>int
171fa 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20 Key );.
171fb 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 c = +1;.
171fc 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d }. pCur-
171fd 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a >validNKey = 1;.
171fe 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e pCur->in
171ff 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b fo.nKey = nCellK
17200 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ey;. }else{
17201 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
17202 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 maximum supporte
17203 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33 d page-size is 3
17204 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 73 2768 bytes. This
17205 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20 means that.
17206 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d ** the maxim
17207 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 um number of rec
17208 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64 ord bytes stored
17209 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 on an index B-T
1720a 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 ree. ** p
1720b 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38 age is at most 8
1720c 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 68 198 bytes, which
1720d 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 may be stored a
1720e 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20 s a 2-byte.
1720f 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 ** varint. Th
17210 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 is information i
17211 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70 s used to attemp
17212 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69 t to avoid parsi
17213 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ng . ** t
17214 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 he entire cell b
17215 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 y checking for t
17216 68 65 20 63 61 73 65 73 20 77 68 65 72 65 20 74 he cases where t
17217 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20 he record is .
17218 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 ** stored
17219 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20 entirely within
1721a 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 20 the b-tree page
1721b 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 by inspecting th
1721c 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 20 e first .
1721d 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74 ** 2 bytes of t
1721e 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 he cell..
1721f 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 */. int
17220 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d nCell = pCell[0]
17221 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 28 ;. if( !(
17222 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26 nCell & 0x80) &&
17223 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d nCell<=pPage->m
17224 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 axLocal ){.
17225 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
17226 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 nch runs if the
17227 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c record-size fiel
17228 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 d of the cell is
17229 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 a. **
1722a 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 single byte vari
1722b 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 nt and the recor
1722c 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 d fits entirely
1722d 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 on the main.
1722e 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 ** b-tree
1722f 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 page. */.
17230 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 c = sqlite3V
17231 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 dbeRecordCompare
17232 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 (nCell, (void*)&
17233 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 pCell[1], pIdxKe
17234 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 y);. }els
17235 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d e if( !(pCell[1]
17236 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20 & 0x80) .
17237 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 && (nCell =
17238 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 ((nCell&0x7f)<<7
17239 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 ) + pCell[1])<=p
1723a 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 Page->maxLocal.
1723b 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 ){.
1723c 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 /* The recor
1723d 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 d-size field is
1723e 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20 a 2 byte varint
1723f 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a and the record .
17240 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74 ** fit
17241 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 s entirely on th
17242 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 e main b-tree pa
17243 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ge. */.
17244 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 c = sqlite3Vdb
17245 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e eRecordCompare(n
17246 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 Cell, (void*)&pC
17247 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 ell[2], pIdxKey)
17248 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
17249 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 . /* Th
1724a 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f e record flows o
1724b 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 ver onto one or
1724c 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
1724d 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 ges. In.
1724e 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 ** this case t
1724f 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 he whole cell ne
17250 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 eds to be parsed
17251 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 , a buffer alloc
17252 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a ated. *
17253 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c * and accessPayl
17254 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 oad() used to re
17255 74 72 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 trieve the recor
17256 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 d into the.
17257 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 ** buffer b
17258 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 efore VdbeRecord
17259 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 Compare() can be
1725a 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 called. */.
1725b 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c void *pCel
1725c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 lKey;.
1725d 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c u8 * const pCell
1725e 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 Body = pCell - p
1725f 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 Page->childPtrSi
17260 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74 ze;. bt
17261 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
17262 70 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 pPage, pCellBody
17263 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a , &pCur->info);.
17264 20 20 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 nCell
17265 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 = (int)pCur->inf
17266 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 o.nKey;.
17267 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c pCellKey = sql
17268 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c ite3Malloc( nCel
17269 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 l );. i
1726a 66 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 f( pCellKey==0 )
1726b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
1726c 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1726d 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
1726e 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b o moveto_finish;
1726f 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
17270 20 20 20 20 20 20 20 72 63 20 3d 20 61 63 63 65 rc = acce
17271 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 ssPayload(pCur,
17272 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 0, nCell, (unsig
17273 6e 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b ned char*)pCellK
17274 65 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 ey, 0);.
17275 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
17276 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
17277 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a free(pCellKey);.
17278 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
17279 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a moveto_finish;.
1727a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1727b 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 c = sqlite
1727c 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 3VdbeRecordCompa
1727d 72 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b re(nCell, pCellK
1727e 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 ey, pIdxKey);.
1727f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
17280 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a free(pCellKey);.
17281 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17282 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 }. if( c==0
17283 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
17284 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 pPage->intKey &&
17285 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
17286 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d . lwr =
17287 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 idx;.
17288 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 upr = lwr - 1;.
17289 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
1728a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1728b 20 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d *pRes =
1728c 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 0;. rc
1728d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1728e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 goto mov
1728f 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 eto_finish;.
17290 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
17291 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 if( c<0 ){.
17292 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 lwr = idx
17293 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b +1;. }else{
17294 0a 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 . upr = i
17295 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 dx-1;. }.
17296 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20 if( lwr>upr
17297 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b ){. break
17298 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
17299 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
1729a 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 ->iPage] = (u16)
1729b 28 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 ((lwr+upr)/2);.
1729c 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1729d 20 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 lwr==upr+1 );.
1729e 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
1729f 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 ->isInit );.
172a0 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 if( pPage->leaf
172a1 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 ){. chldPg
172a2 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 = 0;. }else i
172a3 66 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e f( lwr>=pPage->n
172a4 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 Cell ){. ch
172a5 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 ldPg = get4byte(
172a6 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
172a7 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
172a8 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ]);. }else{.
172a9 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 chldPg = ge
172aa 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
172ab 70 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 pPage, lwr));.
172ac 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 }. if( chld
172ad 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 Pg==0 ){. a
172ae 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 ssert( pCur->aiI
172af 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c dx[pCur->iPage]<
172b0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
172b1 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
172b2 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 );. *pRes
172b3 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = c;. rc =
172b4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
172b5 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
172b6 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ish;. }. p
172b7 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
172b8 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c >iPage] = (u16)l
172b9 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e wr;. pCur->in
172ba 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
172bb 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
172bc 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 y = 0;. rc =
172bd 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 moveToChild(pCur
172be 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 , chldPg);. i
172bf 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 f( rc ) goto mov
172c0 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a eto_finish;. }.
172c1 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 moveto_finish:.
172c2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
172c3 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
172c4 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 E if the cursor
172c5 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 is not pointing
172c6 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 at an entry of t
172c7 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 he table..**.**
172c8 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 TRUE will be ret
172c9 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 urned after a ca
172ca 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 ll to sqlite3Btr
172cb 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a eeNext() moves.*
172cc 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 * past the last
172cd 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 entry in the tab
172ce 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 le or sqlite3Btr
172cf 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 eePrev() moves p
172d0 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 ast.** the first
172d1 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 entry. TRUE is
172d2 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 also returned i
172d3 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 f the table is e
172d4 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mpty..*/.SQLITE_
172d5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
172d6 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 te3BtreeEof(BtCu
172d7 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f rsor *pCur){. /
172d8 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 * TODO: What if
172d9 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e the cursor is in
172da 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 CURSOR_REQUIRES
172db 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c EEK but all tabl
172dc 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 e entries. ** h
172dd 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 ave been deleted
172de 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 ? This API will
172df 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 need to change t
172e0 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f o return an erro
172e1 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 r code. ** as w
172e2 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 ell as the boole
172e3 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e an result value.
172e4 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 . */. return (
172e5 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 CURSOR_VALID!=pC
172e6 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a ur->eState);.}..
172e7 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 /*.** Advance th
172e8 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
172e9 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 next entry in th
172ea 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a e database. If.
172eb 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 ** successful th
172ec 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 en set *pRes=0.
172ed 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a If the cursor.*
172ee 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f * was already po
172ef 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 inting to the la
172f0 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
172f1 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a database before.
172f2 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ** this routine
172f3 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e was called, then
172f4 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f set *pRes=1..*/
172f5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
172f6 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
172f7 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 Next(BtCursor *p
172f8 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b Cur, int *pRes){
172f9 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 . int rc;. int
172fa 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 idx;. MemPage
172fb 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 *pPage;.. asser
172fc 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
172fd 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 tex(pCur) );. r
172fe 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f c = restoreCurso
172ff 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b rPosition(pCur);
17300 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
17301 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
17302 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 rn rc;. }. ass
17303 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a ert( pRes!=0 );.
17304 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 if( CURSOR_INV
17305 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 ALID==pCur->eSta
17306 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 te ){. *pRes
17307 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 = 1;. return
17308 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
17309 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e if( pCur->skipN
1730a 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 ext>0 ){. pCu
1730b 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b r->skipNext = 0;
1730c 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a . *pRes = 0;.
1730d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1730e 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 E_OK;. }. pCur
1730f 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a ->skipNext = 0;.
17310 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
17311 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
17312 61 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b age];. idx = ++
17313 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
17314 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 ->iPage];. asse
17315 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 rt( pPage->isIni
17316 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 t );. assert( i
17317 64 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c dx<=pPage->nCell
17318 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 );.. pCur->inf
17319 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 o.nSize = 0;. p
1731a 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
1731b 20 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 0;. if( idx>=p
1731c 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 Page->nCell ){.
1731d 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
1731e 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 eaf ){. rc
1731f 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
17320 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 ur, get4byte(&pP
17321 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
17322 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 ->hdrOffset+8]))
17323 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
17324 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
17325 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 rc = moveToLef
17326 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 tmost(pCur);.
17327 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
17328 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
17329 20 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 }. do{.
1732a 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 if( pCur->iPa
1732b 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ge==0 ){.
1732c 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 *pRes = 1;.
1732d 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
1732e 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
1732f 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 D;. retur
17330 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
17331 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 }. moveT
17332 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 oParent(pCur);.
17333 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 pPage = pCu
17334 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
17335 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 iPage];. }whi
17336 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b le( pCur->aiIdx[
17337 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 pCur->iPage]>=pP
17338 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 age->nCell );.
17339 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
1733a 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b if( pPage->intK
1733b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ey ){. rc =
1733c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 sqlite3BtreeNex
1733d 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 t(pCur, pRes);.
1733e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1733f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
17340 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
17341 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 rc;. }. *pRes
17342 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67 = 0;. if( pPag
17343 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 e->leaf ){. r
17344 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
17345 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 . }. rc = move
17346 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 ToLeftmost(pCur)
17347 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
17348 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 .../*.** Step th
17349 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
1734a 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76 back to the prev
1734b 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68 ious entry in th
1734c 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a e database. If.
1734d 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 ** successful th
1734e 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 en set *pRes=0.
1734f 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a If the cursor.*
17350 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f * was already po
17351 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69 inting to the fi
17352 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 rst entry in the
17353 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 database before
17354 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
17355 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 was called, the
17356 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a n set *pRes=1..*
17357 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17358 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
17359 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 ePrevious(BtCurs
1735a 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 or *pCur, int *p
1735b 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a Res){. int rc;.
1735c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
1735d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
1735e 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
1735f 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 ur) );. rc = re
17360 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
17361 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 ion(pCur);. if(
17362 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
17363 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
17364 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c . }. pCur->atL
17365 61 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 43 ast = 0;. if( C
17366 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 URSOR_INVALID==p
17367 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 Cur->eState ){.
17368 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 *pRes = 1;.
17369 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1736a 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 OK;. }. if( pC
1736b 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 ur->skipNext<0 )
1736c 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 {. pCur->skip
1736d 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 Next = 0;. *p
1736e 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 Res = 0;. ret
1736f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
17370 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e }. pCur->skipN
17371 65 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 ext = 0;.. pPag
17372 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
17373 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
17374 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
17375 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 isInit );. if(
17376 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
17377 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 int idx = pC
17378 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
17379 69 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d iPage];. rc =
1737a 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
1737b 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 r, get4byte(find
1737c 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 Cell(pPage, idx)
1737d 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 ));. if( rc )
1737e 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
1737f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 c;. }. rc
17380 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 = moveToRightmos
17381 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 t(pCur);. }else
17382 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 {. while( pCu
17383 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
17384 50 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 Page]==0 ){.
17385 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
17386 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
17387 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
17388 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 URSOR_INVALID;.
17389 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 *pRes = 1
1738a 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
1738b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
1738c 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f }. moveTo
1738d 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 Parent(pCur);.
1738e 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e }. pCur->in
1738f 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
17390 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 pCur->validNKe
17391 79 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 72 y = 0;.. pCur
17392 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
17393 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 age]--;. pPag
17394 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 e = pCur->apPage
17395 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 [pCur->iPage];.
17396 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e if( pPage->in
17397 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e tKey && !pPage->
17398 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 leaf ){. rc
17399 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
1739a 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52 revious(pCur, pR
1739b 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a es);. }else{.
1739c 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1739d 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a E_OK;. }. }.
1739e 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 *pRes = 0;. r
1739f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
173a0 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 ** Allocate a ne
173a1 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 w page from the
173a2 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
173a3 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67 *.** The new pag
173a4 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 e is marked as d
173a5 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 irty. (In other
173a6 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50 words, sqlite3P
173a7 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 agerWrite().** h
173a8 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
173a9 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 called on the ne
173aa 77 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65 w page.) The ne
173ab 77 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a w page has also.
173ac 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63 ** been referenc
173ad 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 ed and the calli
173ae 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 ng routine is re
173af 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 sponsible for ca
173b0 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 lling.** sqlite3
173b1 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 PagerUnref() on
173b2 74 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 the new page whe
173b3 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a n it is done..**
173b4 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 .** SQLITE_OK is
173b5 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 returned on suc
173b6 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 cess. Any other
173b7 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e return value in
173b8 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 dicates.** an er
173b9 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e ror. *ppPage an
173ba 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 d *pPgno are und
173bb 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76 efined in the ev
173bc 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e ent of an error.
173bd 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b .** Do not invok
173be 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e e sqlite3PagerUn
173bf 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 ref() on *ppPage
173c0 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 if an error is
173c1 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
173c2 49 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 If the "nearby"
173c3 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 parameter is not
173c4 20 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62 0, then a (feeb
173c5 6c 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61 le) effort is ma
173c6 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 de to .** locate
173c7 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f a page close to
173c8 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
173c9 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73 "nearby". This
173ca 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 can be used in
173cb 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f an.** attempt to
173cc 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70 61 keep related pa
173cd 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 ges close to eac
173ce 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64 h other in the d
173cf 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a atabase file,.**
173d0 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63 which in turn c
173d1 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 an make database
173d2 20 61 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a access faster..
173d3 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78 **.** If the "ex
173d4 61 63 74 22 20 70 61 72 61 6d 65 74 65 72 20 69 act" parameter i
173d5 73 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 s not 0, and the
173d6 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 page-number nea
173d7 72 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 rby exists .** a
173d8 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 nywhere on the f
173d9 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 ree-list, then i
173da 74 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 t is guarenteed
173db 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 to be returned.
173dc 54 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 This.** is only
173dd 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63 used by auto-vac
173de 75 75 6d 20 64 61 74 61 62 61 73 65 73 20 77 68 uum databases wh
173df 65 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 en allocating a
173e0 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 new table..*/.st
173e1 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 atic int allocat
173e2 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 42 74 eBtreePage(. Bt
173e3 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 Shared *pBt, .
173e4 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 MemPage **ppPage
173e5 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f , . Pgno *pPgno
173e6 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 , . Pgno nearby
173e7 2c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a ,. u8 exact.){.
173e8 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
173e9 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 1;. int rc;. u
173ea 33 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 32 n; /* Num
173eb 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 ber of pages on
173ec 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a the freelist */.
173ed 20 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 u32 k; /*
173ee 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 Number of leaves
173ef 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 on the trunk of
173f0 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f the freelist */
173f1 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 . MemPage *pTru
173f2 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 nk = 0;. MemPag
173f3 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 e *pPrevTrunk =
173f4 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 0;. Pgno mxPage
173f5 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 ; /* Total s
173f6 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
173f7 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 ase file */.. a
173f8 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
173f9 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
173fa 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 utex) );. pPage
173fb 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 1 = pBt->pPage1;
173fc 0a 20 20 6d 78 50 61 67 65 20 3d 20 70 61 67 65 . mxPage = page
173fd 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b rPagecount(pBt);
173fe 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 . n = get4byte(
173ff 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 &pPage1->aData[3
17400 36 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 6]);. testcase(
17401 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a n==mxPage-1 );.
17402 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 if( n>=mxPage
17403 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
17404 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
17405 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 T;. }. if( n>0
17406 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 ){. /* There
17407 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 are pages on th
17408 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 e freelist. Reu
17409 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 se one of those
1740a 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 pages. */. Pg
1740b 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 no iTrunk;. u
1740c 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 8 searchList = 0
1740d 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 ; /* If the free
1740e 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 -list must be se
1740f 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 arched for 'near
17410 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 by' */. .
17411 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 74 /* If the 'exact
17412 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 ' parameter was
17413 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 true and a query
17414 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d of the pointer-
17415 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 map. ** shows
17416 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 27 that the page '
17417 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 nearby' is somew
17418 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 here on the free
17419 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 -list, then.
1741a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 ** the entire-li
1741b 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 st will be searc
1741c 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 hed for that pag
1741d 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 e.. */.#ifnde
1741e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1741f 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
17420 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 exact && nearby
17421 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 <=mxPage ){.
17422 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 u8 eType;.
17423 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79 assert( nearby
17424 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 >0 );. asse
17425 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 rt( pBt->autoVac
17426 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20 uum );. rc
17427 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c = ptrmapGet(pBt,
17428 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c nearby, &eType,
17429 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 0);. if( r
1742a 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
1742b 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d if( eType==
1742c 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 PTRMAP_FREEPAGE
1742d 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 ){. searc
1742e 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 hList = 1;.
1742f 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 }. *pPgno
17430 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a = nearby;. }.
17431 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 #endif.. /* D
17432 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 ecrement the fre
17433 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20 e-list count by
17434 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 1. Set iTrunk to
17435 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
17436 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66 e. ** first f
17437 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 ree-list trunk p
17438 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 age. iPrevTrunk
17439 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a is initially 1..
1743a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
1743b 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1743c 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 e(pPage1->pDbPag
1743d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 e);. if( rc )
1743e 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1743f 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 put4byte(&pPage1
17440 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 ->aData[36], n-1
17441 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 );.. /* The c
17442 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 ode within this
17443 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 loop is run only
17444 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 65 once if the 'se
17445 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 62 archList' variab
17446 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 le. ** is not
17447 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65 true. Otherwise
17448 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 , it runs once f
17449 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 or each trunk-pa
1744a 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a ge on the. **
1744b 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c free-list until
1744c 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 the page 'nearb
1744d 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 y' is located..
1744e 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 */. do {.
1744f 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20 pPrevTrunk
17450 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 = pTrunk;.
17451 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29 if( pPrevTrunk )
17452 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b {. iTrunk
17453 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 72 = get4byte(&pPr
17454 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 evTrunk->aData[0
17455 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ]);. }else{
17456 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 . iTrunk
17457 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 = get4byte(&pPag
17458 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a e1->aData[32]);.
17459 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65 }. te
1745a 73 74 63 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d stcase( iTrunk==
1745b 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 mxPage );.
1745c 69 66 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 if( iTrunk>mxPag
1745d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 e ){. rc
1745e 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
1745f 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c _BKPT;. }el
17460 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d se{. rc =
17461 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
17462 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 t, iTrunk, &pTru
17463 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a nk, 0);. }.
17464 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
17465 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d pTrunk =
17466 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 0;. goto
17467 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
17468 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 ge;. }..
17469 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 k = get4byte(
1746a 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 &pTrunk->aData[4
1746b 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d ]);. if( k=
1746c 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 =0 && !searchLis
1746d 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 t ){. /*
1746e 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f The trunk has no
1746f 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 leaves and the
17470 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e list is not bein
17471 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 g searched. .
17472 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 ** So extra
17473 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 ct the trunk pag
17474 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 e itself and use
17475 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 it as the newly
17476 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c . ** all
17477 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 ocated page */.
17478 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
17479 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a PrevTrunk==0 );.
1747a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1747b 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
1747c 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
1747d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
1747e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
1747f 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
17480 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
17481 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 *pPgno =
17482 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 iTrunk;.
17483 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e memcpy(&pPage1->
17484 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 aData[32], &pTru
17485 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 nk->aData[0], 4)
17486 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 ;. *ppPag
17487 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 e = pTrunk;.
17488 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a pTrunk = 0;.
17489 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 TRACE(("
1748a 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 ALLOCATE: %d tru
1748b 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 nk - %d free pag
1748c 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 es left\n", *pPg
1748d 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 no, n-1));.
1748e 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 33 }else if( k>(u3
1748f 32 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 2)(pBt->usableSi
17490 7a 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 20 ze/4 - 2) ){.
17491 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 /* Value of
17492 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e k is out of ran
17493 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f ge. Database co
17494 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 rruption */.
17495 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
17496 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
17497 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 goto end_a
17498 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 llocate_page;.#i
17499 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1749a 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
1749b 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61 }else if( sea
1749c 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62 rchList && nearb
1749d 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 y==iTrunk ){.
1749e 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 /* The list
1749f 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 is being search
174a0 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e ed and this trun
174a1 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61 k page is the pa
174a2 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f ge. ** to
174a3 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 allocate, regar
174a4 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 dless of whether
174a5 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a it has leaves..
174a6 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
174a7 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e assert( *pPgn
174a8 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 o==iTrunk );.
174a9 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 *ppPage = p
174aa 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 Trunk;. s
174ab 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 earchList = 0;.
174ac 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
174ad 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 te3PagerWrite(pT
174ae 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a runk->pDbPage);.
174af 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 if( rc )
174b0 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f {. goto
174b1 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
174b2 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ge;. }.
174b3 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 if( k==0 )
174b4 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
174b5 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 !pPrevTrunk ){.
174b6 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 memcp
174b7 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 y(&pPage1->aData
174b8 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 [32], &pTrunk->a
174b9 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 Data[0], 4);.
174ba 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
174bb 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
174bc 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 (&pPrevTrunk->aD
174bd 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d ata[0], &pTrunk-
174be 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 >aData[0], 4);.
174bf 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
174c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
174c1 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b /* The trunk
174c2 20 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65 page is require
174c3 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 d by the caller
174c4 62 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 but it contains
174c5 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f . ** po
174c6 69 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c inters to free-l
174c7 69 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 ist leaves. The
174c8 66 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d first leaf becom
174c9 65 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 es a trunk.
174ca 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 ** page in
174cb 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 this case..
174cc 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
174cd 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 MemPage *pNewT
174ce 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 runk;.
174cf 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d Pgno iNewTrunk =
174d0 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e get4byte(&pTrun
174d1 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 k->aData[8]);.
174d2 20 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 if( iNew
174d3 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 Trunk>mxPage ){
174d4 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
174d5 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
174d6 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 _BKPT;.
174d7 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
174d8 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
174d9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
174da 20 74 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 testcase( iNewT
174db 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a runk==mxPage );.
174dc 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 rc = b
174dd 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
174de 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 iNewTrunk, &pNe
174df 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 wTrunk, 0);.
174e0 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
174e1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
174e2 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
174e3 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
174e4 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
174e5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
174e6 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 3PagerWrite(pNew
174e7 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
174e8 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
174e9 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
174ea 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 rele
174eb 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e asePage(pNewTrun
174ec 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 k);.
174ed 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
174ee 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 e_page;.
174ef 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 }. me
174f0 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d mcpy(&pNewTrunk-
174f1 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 >aData[0], &pTru
174f2 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 nk->aData[0], 4)
174f3 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34 ;. put4
174f4 62 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d byte(&pNewTrunk-
174f5 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b >aData[4], k-1);
174f6 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
174f7 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 y(&pNewTrunk->aD
174f8 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d ata[8], &pTrunk-
174f9 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 >aData[12], (k-1
174fa 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 )*4);.
174fb 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 releasePage(pNew
174fc 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 Trunk);.
174fd 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e if( !pPrevTrun
174fe 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 k ){.
174ff 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17500 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
17501 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 (pPage1->pDbPage
17502 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ) );.
17503 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
17504 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 1->aData[32], iN
17505 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 ewTrunk);.
17506 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
17507 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
17508 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
17509 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 revTrunk->pDbPag
1750a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 e);.
1750b 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
1750c 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
1750d 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
1750e 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1750f 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 put4by
17510 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e te(&pPrevTrunk->
17511 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 aData[0], iNewTr
17512 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 unk);.
17513 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
17514 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a pTrunk = 0;.
17515 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22 TRACE(("
17516 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 ALLOCATE: %d tru
17517 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 nk - %d free pag
17518 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 es left\n", *pPg
17519 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 no, n-1));.#endi
1751a 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 f. }else if
1751b 28 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 ( k>0 ){.
1751c 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 /* Extract a le
1751d 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e af from the trun
1751e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32 k */. u32
1751f 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 closest;.
17520 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 Pgno iPage;.
17521 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 unsigned c
17522 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 har *aData = pTr
17523 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 unk->aData;.
17524 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17525 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
17526 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
17527 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 if( rc ){.
17528 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
17529 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
1752a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1752b 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 if( nearby>0
1752c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 ){. u32
1752d 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e i;. in
1752e 74 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 t dist;.
1752f 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 closest = 0;.
17530 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 dist =
17531 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b get4byte(&aData[
17532 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 8]) - nearby;.
17533 20 20 20 20 20 20 20 20 69 66 28 20 64 69 73 74 if( dist
17534 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73 <0 ) dist = -dis
17535 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 t;. for
17536 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b (i=1; i<k; i++){
17537 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 . int
17538 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 28 26 d2 = get4byte(&
17539 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 aData[8+i*4]) -
1753a 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 nearby;.
1753b 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 20 64 if( d2<0 ) d
1753c 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 2 = -d2;.
1753d 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 if( d2<dist
1753e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1753f 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 closest = i;.
17540 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73 dis
17541 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 t = d2;.
17542 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
17543 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
17544 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 . close
17545 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 st = 0;.
17546 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 }.. iPage
17547 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61 = get4byte(&aDa
17548 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 ta[8+closest*4])
17549 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
1754a 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 se( iPage==mxPag
1754b 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e );. if(
1754c 20 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b iPage>mxPage ){
1754d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
1754e 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1754f 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 KPT;. g
17550 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 oto end_allocate
17551 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d _page;. }
17552 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
17553 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 e( iPage==mxPage
17554 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 );. if(
17555 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 !searchList || i
17556 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a Page==nearby ){.
17557 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f int no
17558 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 Content;.
17559 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 *pPgno = iPag
1755a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 54 52 41 e;. TRA
1755b 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 CE(("ALLOCATE: %
1755c 64 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66 d was leaf %d of
1755d 20 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 %d on trunk %d"
1755e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1755f 20 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 ": %d more fre
17560 65 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 e pages\n",.
17561 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 50 *pP
17562 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 gno, closest+1,
17563 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c k, pTrunk->pgno,
17564 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 n-1));.
17565 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d if( closest<k-
17566 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 1 ){.
17567 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38 memcpy(&aData[8
17568 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 +closest*4], &aD
17569 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a ata[4+k*4], 4);.
1756a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1756b 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 put4byte(&
1756c 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a aData[4], k-1);.
1756d 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1756e 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
1756f 77 72 69 74 65 61 62 6c 65 28 70 54 72 75 6e 6b writeable(pTrunk
17570 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
17571 20 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e noConten
17572 74 20 3d 20 21 62 74 72 65 65 47 65 74 48 61 73 t = !btreeGetHas
17573 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 Content(pBt, *pP
17574 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 gno);.
17575 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
17576 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 e(pBt, *pPgno, p
17577 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 pPage, noContent
17578 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
17579 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1757a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 {. rc
1757b 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
1757c 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e rite((*ppPage)->
1757d 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
1757e 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1757f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17580 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 release
17581 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 Page(*ppPage);.
17582 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
17583 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
17584 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 searchList =
17585 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
17586 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 }. relea
17587 73 65 50 61 67 65 28 70 50 72 65 76 54 72 75 6e sePage(pPrevTrun
17588 6b 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 k);. pPrevT
17589 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 runk = 0;. }w
1758a 68 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 hile( searchList
1758b 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
1758c 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f /* There are no
1758d 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 pages on the fr
1758e 65 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 eelist, so creat
1758f 65 20 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 e a new page at
17590 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f the. ** end o
17591 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 f the file */.
17592 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 int nPage = pa
17593 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
17594 29 3b 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 );. *pPgno =
17595 6e 50 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 nPage + 1;..
17596 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 if( *pPgno==PEND
17597 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
17598 74 29 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 t) ){. (*pP
17599 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 gno)++;. }..#
1759a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1759b 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
1759c 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
1759d 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f acuum && PTRMAP_
1759e 49 53 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 ISPAGE(pBt, *pPg
1759f 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 no) ){. /*
175a0 49 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 If *pPgno refers
175a1 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 to a pointer-ma
175a2 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 p page, allocate
175a3 20 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 two new pages.
175a4 20 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 ** at the e
175a5 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 nd of the file i
175a6 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 nstead of one. T
175a7 68 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 he first allocat
175a8 65 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a ed page. **
175a9 20 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 becomes a new p
175aa 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c ointer-map page,
175ab 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 the second is u
175ac 73 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 sed by the calle
175ad 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 r.. */.
175ae 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d MemPage *pPg =
175af 20 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 0;. TRACE(
175b0 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 ("ALLOCATE: %d f
175b1 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 rom end of file
175b2 28 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 (pointer-map pag
175b3 65 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b e)\n", *pPgno));
175b4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a . assert( *
175b5 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 pPgno!=PENDING_B
175b6 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b YTE_PAGE(pBt) );
175b7 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 . rc = btre
175b8 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 eGetPage(pBt, *p
175b9 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a Pgno, &pPg, 0);.
175ba 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
175bb 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
175bc 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
175bd 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 agerWrite(pPg->p
175be 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
175bf 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 releasePage(pPg
175c0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
175c1 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
175c2 20 72 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 rc;. (*pPg
175c3 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 no)++;. if(
175c4 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 *pPgno==PENDING
175c5 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 _BYTE_PAGE(pBt)
175c6 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d ){ (*pPgno)++; }
175c7 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
175c8 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e assert( *pPgn
175c9 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f o!=PENDING_BYTE_
175ca 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 PAGE(pBt) );.
175cb 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 rc = btreeGetPa
175cc 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 ge(pBt, *pPgno,
175cd 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 ppPage, 0);.
175ce 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
175cf 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c rc;. rc = sql
175d0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 28 ite3PagerWrite((
175d1 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 *ppPage)->pDbPag
175d2 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
175d3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
175d4 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
175d5 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 ppPage);. }.
175d6 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 TRACE(("ALLOC
175d7 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 ATE: %d from end
175d8 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 of file\n", *pP
175d9 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 gno));. }.. as
175da 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 sert( *pPgno!=PE
175db 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
175dc 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c pBt) );..end_all
175dd 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 ocate_page:. re
175de 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b leasePage(pTrunk
175df 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 );. releasePage
175e0 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 (pPrevTrunk);.
175e1 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
175e2 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c K ){. if( sql
175e3 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
175e4 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d count((*ppPage)-
175e5 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 >pDbPage)>1 ){.
175e6 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
175e7 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 (*ppPage);.
175e8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
175e9 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
175ea 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 }. (*ppPage)
175eb 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 ->isInit = 0;.
175ec 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 }else{. *ppPa
175ed 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ge = 0;. }. re
175ee 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
175ef 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
175f0 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 is used to add p
175f1 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 age iPage to the
175f2 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
175f3 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 ree-list. .** It
175f4 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
175f5 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
175f6 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 already a part
175f7 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 of the free-list
175f8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ..**.** The valu
175f9 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
175fa 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
175fb 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
175fc 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a is optional..**
175fd 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 If the caller h
175fe 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 appens to have a
175ff 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
17600 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a MemPage object .
17601 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
17602 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 to page iPage h
17603 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 andy, it may pas
17604 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f s it as the seco
17605 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 nd value. .** Ot
17606 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 herwise, it may
17607 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a pass NULL..**.**
17608 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f If a pointer to
17609 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 a MemPage objec
1760a 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 t is passed as t
1760b 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
1760c 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 nt,.** its refer
1760d 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f ence count is no
1760e 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 t altered by thi
1760f 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 s function..*/.s
17610 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 tatic int freePa
17611 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 ge2(BtShared *pB
17612 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d t, MemPage *pMem
17613 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 Page, Pgno iPage
17614 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 ){. MemPage *pT
17615 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 runk = 0;
17616 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 /* Free
17617 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
17618 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e */. Pgno iTrun
17619 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 k = 0;
1761a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
1761b 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 e number of free
1761c 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 -list trunk page
1761d 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a */ . MemPage *
1761e 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 pPage1 = pBt->pP
1761f 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f age1; /* Lo
17620 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f cal reference to
17621 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d page 1 */. Mem
17622 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 Page *pPage;
17623 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17624 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 /* Page being f
17625 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c reed. May be NUL
17626 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 L. */. int rc;
17627 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17628 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
17629 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 eturn Code */.
1762a 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 int nFree;
1762b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1762c 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e /* Initial n
1762d 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f umber of pages o
1762e 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a n free-list */..
1762f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17630 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
17631 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
17632 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b sert( iPage>1 );
17633 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d . assert( !pMem
17634 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 Page || pMemPage
17635 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b ->pgno==iPage );
17636 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 .. if( pMemPage
17637 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 ){. pPage =
17638 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 pMemPage;. sq
17639 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50 lite3PagerRef(pP
1763a 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
1763b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 }else{. pPag
1763c 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f e = btreePageLoo
1763d 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b kup(pBt, iPage);
1763e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 . }.. /* Incre
1763f 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 ment the free pa
17640 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 ge count on pPag
17641 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c e1 */. rc = sql
17642 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
17643 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b Page1->pDbPage);
17644 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f . if( rc ) goto
17645 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 freepage_out;.
17646 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 nFree = get4byt
17647 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
17648 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 [36]);. put4byt
17649 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
1764a 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a [36], nFree+1);.
1764b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 .#ifdef SQLITE_S
1764c 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f ECURE_DELETE. /
1764d 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f * If the SQLITE_
1764e 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f SECURE_DELETE co
1764f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f mpile-time optio
17650 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 n is enabled, th
17651 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 en. ** always f
17652 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 ully overwrite d
17653 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 eleted informati
17654 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 on with zeros..
17655 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67 */. if( (!pPag
17656 65 20 26 26 20 28 72 63 20 3d 20 62 74 72 65 65 e && (rc = btree
17657 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 GetPage(pBt, iPa
17658 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 29 ge, &pPage, 0)))
17659 0a 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20 . ||
1765a 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 (rc = sqlite3P
1765b 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
1765c 3e 70 44 62 50 61 67 65 29 29 0a 20 20 29 7b 0a >pDbPage)). ){.
1765d 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 goto freepag
1765e 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d e_out;. }. mem
1765f 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 set(pPage->aData
17660 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d , 0, pPage->pBt-
17661 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 >pageSize);.#end
17662 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 if.. /* If the
17663 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
17664 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 s auto-vacuum, w
17665 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e rite an entry in
17666 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
17667 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 . ** to indicat
17668 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 e that the page
17669 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 is free.. */.
1766a 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d if( ISAUTOVACUUM
1766b 20 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 ){. ptrmapPu
1766c 74 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 t(pBt, iPage, PT
1766d 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 RMAP_FREEPAGE, 0
1766e 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 , &rc);. if(
1766f 72 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 rc ) goto freepa
17670 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f ge_out;. }.. /
17671 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 * Now manipulate
17672 20 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 the actual data
17673 62 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 base free-list s
17674 74 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 tructure. There
17675 61 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 are two. ** pos
17676 73 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74 sibilities. If t
17677 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 he free-list is
17678 63 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c currently empty,
17679 20 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74 or if the first
1767a 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 . ** trunk page
1767b 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 in the free-lis
1767c 74 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 t is full, then
1767d 74 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 this page will b
1767e 65 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 ecome a. ** new
1767f 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b free-list trunk
17680 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 page. Otherwise
17681 2c 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 , it will become
17682 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 a leaf of the.
17683 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 ** first trunk
17684 70 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 72 page in the curr
17685 65 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 ent free-list. T
17686 68 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 his block tests
17687 69 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f if it. ** is po
17688 73 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 ssible to add th
17689 65 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 20 e page as a new
1768a 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a free-list leaf..
1768b 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 */. if( nFree
1768c 21 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e !=0 ){. u32 n
1768d 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 Leaf;
1768e 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 /* Initial
1768f 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63 number of leaf c
17690 65 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 ells on trunk pa
17691 67 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e ge */.. iTrun
17692 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 k = get4byte(&pP
17693 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 age1->aData[32])
17694 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 ;. rc = btree
17695 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 GetPage(pBt, iTr
17696 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 unk, &pTrunk, 0)
17697 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
17698 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17699 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
1769a 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e ut;. }.. n
1769b 4c 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 Leaf = get4byte(
1769c 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 &pTrunk->aData[4
1769d 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ]);. assert(
1769e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e pBt->usableSize>
1769f 33 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 32 );. if( nL
176a0 65 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e eaf > (u32)pBt->
176a1 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 usableSize/4 - 2
176a2 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
176a3 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
176a4 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 PT;. goto f
176a5 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 reepage_out;.
176a6 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 }. if( nLeaf
176a7 20 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 < (u32)pBt->usa
176a8 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b bleSize/4 - 8 ){
176a9 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 . /* In thi
176aa 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 s case there is
176ab 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e room on the trun
176ac 6b 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74 k page to insert
176ad 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 the page.
176ae 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 ** being freed a
176af 73 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 s a new leaf..
176b0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
176b1 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72 Note that the tr
176b2 75 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 unk page is not
176b3 72 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 really full unti
176b4 6c 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 l it contains.
176b5 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a ** usableSiz
176b6 65 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c e/4 - 2 entries,
176b7 20 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f not usableSize/
176b8 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 4 - 8 entries as
176b9 20 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a we have. *
176ba 2a 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 * coded. But du
176bb 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 e to a coding er
176bc 72 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 ror in versions
176bd 6f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 of SQLite prior
176be 74 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e to. ** 3.6.
176bf 30 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74 0, databases wit
176c0 68 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b h freelist trunk
176c1 20 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d pages holding m
176c2 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a ore than. *
176c3 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d * usableSize/4 -
176c4 20 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 8 entries will
176c5 62 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 be reported as c
176c6 6f 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 orrupt. In orde
176c7 72 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 r. ** to ma
176c8 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 intain backwards
176c9 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 compatibility w
176ca 69 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f ith older versio
176cb 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 ns of SQLite,.
176cc 20 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 ** we will c
176cd 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 ontinue to restr
176ce 69 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ict the number o
176cf 66 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 f entries to usa
176d0 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 bleSize/4 - 8.
176d1 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 ** for now.
176d2 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 At some point i
176d3 6e 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e n the future (on
176d4 63 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 ce everyone has
176d5 75 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a upgraded. *
176d6 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 * to 3.6.0 or la
176d7 74 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 ter) we should c
176d8 6f 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 onsider fixing t
176d9 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 he conditional a
176da 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f bove. ** to
176db 20 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a read "usableSiz
176dc 65 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f e/4-2" instead o
176dd 66 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d f "usableSize/4-
176de 38 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 8".. */.
176df 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
176e0 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b agerWrite(pTrunk
176e1 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
176e2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
176e3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 _OK ){. p
176e4 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d ut4byte(&pTrunk-
176e5 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 >aData[4], nLeaf
176e6 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 +1);. put
176e7 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 4byte(&pTrunk->a
176e8 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c Data[8+nLeaf*4],
176e9 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 iPage);.#ifndef
176ea 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 SQLITE_SECURE_D
176eb 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 69 66 ELETE. if
176ec 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 ( pPage ){.
176ed 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
176ee 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65 rDontWrite(pPage
176ef 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 ->pDbPage);.
176f0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
176f1 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 53 rc = btreeS
176f2 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 etHasContent(pBt
176f3 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 , iPage);.
176f4 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 }. TRACE(("
176f5 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6c 65 FREE-PAGE: %d le
176f6 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 af on trunk page
176f7 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67 %d\n",pPage->pg
176f8 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 no,pTrunk->pgno)
176f9 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 );. goto fr
176fa 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 eepage_out;.
176fb 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 }. }.. /* If c
176fc 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 ontrol flows to
176fd 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e this point, then
176fe 20 69 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 73 it was not poss
176ff 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 0a ible to add the.
17700 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 62 65 ** the page be
17701 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6c ing freed as a l
17702 65 61 66 20 70 61 67 65 20 6f 66 20 74 68 65 20 eaf page of the
17703 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 first trunk in t
17704 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 he free-list..
17705 2a 2a 20 50 6f 73 73 69 62 6c 79 20 62 65 63 61 ** Possibly beca
17706 75 73 65 20 74 68 65 20 66 72 65 65 2d 6c 69 73 use the free-lis
17707 74 20 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 70 t is empty, or p
17708 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 ossibly because
17709 74 68 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 20 the . ** first
1770a 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 trunk in the fre
1770b 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e 20 e-list is full.
1770c 45 69 74 68 65 72 20 77 61 79 2c 20 74 68 65 20 Either way, the
1770d 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 page being freed
1770e 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d . ** will becom
1770f 65 20 74 68 65 20 6e 65 77 20 66 69 72 73 74 20 e the new first
17710 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 trunk page in th
17711 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a e free-list.. *
17712 2f 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30 /. if( pPage==0
17713 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 && SQLITE_OK!=(
17714 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
17715 65 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 e(pBt, iPage, &p
17716 50 61 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 Page, 0)) ){.
17717 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
17718 75 74 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 ut;. }. rc = s
17719 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1771a 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
1771b 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
1771c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 TE_OK ){. got
1771d 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
1771e 20 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 }. put4byte(p
1771f 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 Page->aData, iTr
17720 75 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 unk);. put4byte
17721 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 (&pPage->aData[4
17722 5d 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 ], 0);. put4byt
17723 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 e(&pPage1->aData
17724 5b 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 [32], iPage);.
17725 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 TRACE(("FREE-PAG
17726 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 E: %d new trunk
17727 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 page replacing %
17728 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e d\n", pPage->pgn
17729 6f 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 o, iTrunk));..fr
1772a 65 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 eepage_out:. if
1772b 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 ( pPage ){. p
1772c 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
1772d 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 ;. }. releaseP
1772e 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 age(pPage);. re
1772f 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b leasePage(pTrunk
17730 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
17731 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 }.static void fr
17732 65 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a eePage(MemPage *
17733 70 50 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 29 pPage, int *pRC)
17734 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d {. if( (*pRC)==
17735 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
17736 20 2a 70 52 43 20 3d 20 66 72 65 65 50 61 67 65 *pRC = freePage
17737 32 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 50 2(pPage->pBt, pP
17738 61 67 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f age, pPage->pgno
17739 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
1773a 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f Free any overflo
1773b 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 w pages associat
1773c 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65 ed with the give
1773d 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 n Cell..*/.stati
1773e 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 c int clearCell(
1773f 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
17740 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
17741 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 Cell){. BtShare
17742 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e d *pBt = pPage->
17743 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 pBt;. CellInfo
17744 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 info;. Pgno ovf
17745 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b lPgno;. int rc;
17746 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 . int nOvfl;.
17747 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 u16 ovflPageSize
17748 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
17749 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1774a 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
1774b 78 29 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72 x) );. btreePar
1774c 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c seCellPtr(pPage,
1774d 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a pCell, &info);.
1774e 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 if( info.iOver
1774f 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 flow==0 ){. r
17750 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
17751 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 /* No overflow
17752 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 77 pages. Return w
17753 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 ithout doing any
17754 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f thing */. }. o
17755 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 vflPgno = get4by
17756 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 te(&pCell[info.i
17757 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 Overflow]);. as
17758 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c sert( pBt->usabl
17759 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f eSize > 4 );. o
1775a 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42 vflPageSize = pB
1775b 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
1775c 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 4;. nOvfl = (in
1775d 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e fo.nPayload - in
1775e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c fo.nLocal + ovfl
1775f 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 PageSize - 1)/ov
17760 66 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 flPageSize;. as
17761 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d sert( ovflPgno==
17762 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 0 || nOvfl>0 );.
17763 20 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d while( nOvfl--
17764 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 ){. Pgno iNe
17765 78 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 xt = 0;. MemP
17766 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a age *pOvfl = 0;.
17767 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f if( ovflPgno
17768 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 <2 || ovflPgno>p
17769 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
1776a 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 t) ){. /* 0
1776b 20 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 is not a legal
1776c 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 page number and
1776d 70 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 page 1 cannot be
1776e 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 an . ** ov
1776f 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 erflow page. The
17770 72 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 refore if ovflPg
17771 6e 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65 no<2 or past the
17772 20 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20 end of the .
17773 20 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 ** file the d
17774 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 atabase must be
17775 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 corrupt. */.
17776 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17777 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
17778 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 }. if( nOvf
17779 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 l ){. rc =
1777a 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 getOverflowPage(
1777b 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 pBt, ovflPgno, &
1777c 70 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a pOvfl, &iNext);.
1777d 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 if( rc ) r
1777e 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1777f 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 rc = freePag
17780 65 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f e2(pBt, pOvfl, o
17781 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 vflPgno);. if
17782 28 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 ( pOvfl ){.
17783 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
17784 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 ef(pOvfl->pDbPag
17785 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 e);. }. if
17786 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
17787 3b 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d ;. ovflPgno =
17788 20 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 iNext;. }. re
17789 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1778a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
1778b 74 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 the byte sequenc
1778c 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 e used to repres
1778d 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 ent a cell on pa
1778e 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 ge pPage.** and
1778f 77 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20 write that byte
17790 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 sequence into pC
17791 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 ell[]. Overflow
17792 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c pages are.** al
17793 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c located and fill
17794 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 ed in as necessa
17795 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 ry. The calling
17796 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 procedure.** is
17797 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 responsible for
17798 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 making sure suf
17799 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 ficient space ha
1779a 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 s been allocated
1779b 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e .** for pCell[].
1779c 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
1779d 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 pCell does not
1779e 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 necessary need t
1779f 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 o point to the p
177a0 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 Page->aData.** a
177a1 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 rea. pCell migh
177a2 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 t point to some
177a3 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 temporary storag
177a4 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c e. The cell wil
177a5 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 l.** be construc
177a6 74 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 ted in this temp
177a7 6f 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 orary area then
177a8 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 copied into pPag
177a9 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 e->aData.** late
177aa 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
177ab 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d fillInCell(. M
177ac 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 emPage *pPage,
177ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
177ae 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63 The page that c
177af 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c ontains the cell
177b0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
177b1 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 har *pCell,
177b2 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 /* Complete
177b3 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c text of the cel
177b4 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 l */. const voi
177b5 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 d *pKey, i64 nKe
177b6 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 y, /* The key
177b7 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
177b8 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 *pData,int nDat
177b9 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 a, /* The data
177ba 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c */. int nZero,
177bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
177bc 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 /* Extra ze
177bd 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 ro bytes to appe
177be 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 nd to pData */.
177bf 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 int *pnSize
177c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
177c1 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 /* Write cell si
177c2 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ze here */.){.
177c3 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 int nPayload;.
177c4 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a const u8 *pSrc;.
177c5 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 int nSrc, n, r
177c6 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 c;. int spaceLe
177c7 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ft;. MemPage *p
177c8 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 Ovfl = 0;. MemP
177c9 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 age *pToRelease
177ca 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 = 0;. unsigned
177cb 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 char *pPrior;.
177cc 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
177cd 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 Payload;. BtSha
177ce 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 red *pBt = pPage
177cf 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 ->pBt;. Pgno pg
177d0 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e noOvfl = 0;. in
177d1 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c t nHeader;. Cel
177d2 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 lInfo info;.. a
177d3 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
177d4 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
177d5 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
177d6 0a 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e . /* pPage is n
177d7 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 ot necessarily w
177d8 72 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 riteable since p
177d9 43 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 Cell might be au
177da 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 xiliary. ** buf
177db 66 65 72 20 73 70 61 63 65 20 74 68 61 74 20 69 fer space that i
177dc 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 s separate from
177dd 74 68 65 20 70 50 61 67 65 20 62 75 66 66 65 72 the pPage buffer
177de 20 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 area */. asser
177df 74 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e t( pCell<pPage->
177e0 61 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d aData || pCell>=
177e1 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 &pPage->aData[pB
177e2 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 t->pageSize].
177e3 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 || sqli
177e4 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
177e5 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
177e6 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c ge) );.. /* Fil
177e7 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e l in the header.
177e8 20 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 */. nHeader =
177e9 30 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 0;. if( !pPage-
177ea 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 >leaf ){. nHe
177eb 61 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 ader += 4;. }.
177ec 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 if( pPage->hasD
177ed 61 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 ata ){. nHead
177ee 65 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 er += putVarint(
177ef 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c &pCell[nHeader],
177f0 20 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 nData+nZero);.
177f1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 }else{. nDat
177f2 61 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 a = nZero = 0;.
177f3 20 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 }. nHeader +=
177f4 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c putVarint(&pCell
177f5 5b 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 [nHeader], *(u64
177f6 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 *)&nKey);. btre
177f7 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
177f8 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
177f9 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e o);. assert( in
177fa 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 fo.nHeader==nHea
177fb 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 der );. assert(
177fc 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 info.nKey==nKey
177fd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e );. assert( in
177fe 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 fo.nData==(u32)(
177ff 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a nData+nZero) );.
17800 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 . /* Fill in
17801 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 the payload */.
17802 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 nPayload = nDat
17803 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 a + nZero;. if(
17804 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 pPage->intKey )
17805 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 {. pSrc = pDa
17806 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e ta;. nSrc = n
17807 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 Data;. nData
17808 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 = 0;. }else{ .
17809 20 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 if( NEVER(nKe
1780a 79 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20 y>0x7fffffff ||
1780b 70 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 pKey==0) ){.
1780c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1780d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1780e 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 }. nPayload
1780f 20 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 += (int)nKey;.
17810 20 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a pSrc = pKey;.
17811 20 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 nSrc = (int)
17812 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 nKey;. }. *pnS
17813 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 ize = info.nSize
17814 3b 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 ;. spaceLeft =
17815 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 info.nLocal;. p
17816 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c Payload = &pCell
17817 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 [nHeader];. pPr
17818 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 ior = &pCell[inf
17819 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 o.iOverflow];..
1781a 20 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 while( nPayload
1781b 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 >0 ){. if( sp
1781c 61 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 aceLeft==0 ){.#i
1781d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1781e 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
1781f 20 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d Pgno pgnoPtrm
17820 61 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f ap = pgnoOvfl; /
17821 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 * Overflow page
17822 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
17823 79 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 y page */.
17824 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
17825 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 uum ){. d
17826 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e o{. pgn
17827 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 oOvfl++;.
17828 20 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 } while( .
17829 20 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 PTRMAP_ISPA
1782a 47 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c GE(pBt, pgnoOvfl
1782b 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 ) || pgnoOvfl==P
1782c 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
1782d 28 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 (pBt) . )
1782e 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 ;. }.#endif
1782f 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f . rc = allo
17830 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
17831 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f t, &pOvfl, &pgno
17832 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 Ovfl, pgnoOvfl,
17833 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 0);.#ifndef SQLI
17834 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
17835 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 UM. /* If t
17836 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 he database supp
17837 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d orts auto-vacuum
17838 2c 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 , and the second
17839 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 or subsequent.
1783a 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 ** overflow
1783b 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 page is being a
1783c 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e llocated, add an
1783d 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f entry to the po
1783e 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 inter-map.
1783f 2a 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 ** for that page
17840 20 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a now. . **.
17841 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 ** If this
17842 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 is the first ov
17843 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 erflow page, the
17844 6e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61 n write a partia
17845 6c 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a l entry . *
17846 2a 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 * to the pointer
17847 2d 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 -map. If we writ
17848 65 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 e nothing to thi
17849 73 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c s pointer-map sl
1784a 6f 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 ot,. ** the
1784b 6e 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 n the optimistic
1784c 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 overflow chain
1784d 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c processing in cl
1784e 65 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 earCell().
1784f 2a 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 ** may misinterp
17850 72 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 ret the uninitia
17851 6c 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 lised values and
17852 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 delete the.
17853 20 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 ** wrong pages
17854 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
17855 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 se.. */.
17856 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f if( pBt->auto
17857 56 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 Vacuum && rc==SQ
17858 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
17859 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 u8 eType = (p
1785a 67 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 gnoPtrmap?PTRMAP
1785b 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 _OVERFLOW2:PTRMA
1785c 50 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 P_OVERFLOW1);.
1785d 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 ptrmapPut(
1785e 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 pBt, pgnoOvfl, e
1785f 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 Type, pgnoPtrmap
17860 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 , &rc);.
17861 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 if( rc ){.
17862 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
17863 70 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 pOvfl);.
17864 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
17865 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b . if( rc ){
17866 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
17867 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 Page(pToRelease)
17868 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
17869 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 rc;. }..
1786a 20 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c /* If pToRel
1786b 65 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f ease is not zero
1786c 20 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 than pPrior poi
1786d 6e 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 nts into the dat
1786e 61 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 a area. **
1786f 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 of pToRelease.
17870 4d 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c Make sure pToRel
17871 65 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 ease is still wr
17872 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 iteable. */.
17873 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c assert( pToRel
17874 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ease==0 || sqlit
17875 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
17876 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 le(pToRelease->p
17877 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 DbPage) );..
17878 20 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 /* If pPrior i
17879 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 s part of the da
1787a 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 ta area of pPage
1787b 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 , then make sure
1787c 20 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 pPage. **
1787d 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 is still writeab
1787e 6c 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 le */. asse
1787f 72 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 rt( pPrior<pPage
17880 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f ->aData || pPrio
17881 72 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 r>=&pPage->aData
17882 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a [pBt->pageSize].
17883 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 || s
17884 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
17885 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
17886 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 bPage) );..
17887 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 put4byte(pPrior
17888 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 , pgnoOvfl);.
17889 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1788a 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 ToRelease);.
1788b 20 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 pToRelease = p
1788c 4f 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 Ovfl;. pPri
1788d 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 or = pOvfl->aDat
1788e 61 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 a;. put4byt
1788f 65 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 e(pPrior, 0);.
17890 20 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 pPayload = &
17891 70 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b pOvfl->aData[4];
17892 0a 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 . spaceLeft
17893 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
17894 7a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 ze - 4;. }.
17895 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a n = nPayload;.
17896 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c if( n>spaceL
17897 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c eft ) n = spaceL
17898 65 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 eft;.. /* If
17899 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f pToRelease is no
1789a 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 t zero than pPay
1789b 6c 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f load points into
1789c 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 the data area.
1789d 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 ** of pToRele
1789e 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 ase. Make sure
1789f 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 pToRelease is st
178a0 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a ill writeable. *
178a1 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 /. assert( pT
178a2 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 oRelease==0 || s
178a3 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
178a4 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 teable(pToReleas
178a5 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
178a6 20 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f /* If pPaylo
178a7 61 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 ad is part of th
178a8 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 e data area of p
178a9 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 Page, then make
178aa 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a sure pPage. *
178ab 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 * is still write
178ac 61 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 able */. asse
178ad 72 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 rt( pPayload<pPa
178ae 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 ge->aData || pPa
178af 79 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 yload>=&pPage->a
178b0 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 Data[pBt->pageSi
178b1 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze].
178b2 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
178b3 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
178b4 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
178b5 20 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b if( nSrc>0 ){
178b6 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 . if( n>nSr
178b7 63 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 c ) n = nSrc;.
178b8 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 assert( pSrc
178b9 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 );. memcpy
178ba 28 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c (pPayload, pSrc,
178bb 20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a n);. }else{.
178bc 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 memset(pPa
178bd 79 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 yload, 0, n);.
178be 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 }. nPayload
178bf 20 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c -= n;. pPayl
178c0 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 oad += n;. pS
178c1 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 rc += n;. nSr
178c2 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 c -= n;. spac
178c3 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 eLeft -= n;.
178c4 69 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 if( nSrc==0 ){.
178c5 20 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 nSrc = nDat
178c6 61 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 a;. pSrc =
178c7 70 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d pData;. }. }
178c8 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 . releasePage(p
178c9 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 ToRelease);. re
178ca 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
178cb 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 }../*.** Remove
178cc 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 the i-th cell fr
178cd 6f 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 om pPage. This
178ce 72 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 routine effects
178cf 70 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 pPage only..** T
178d0 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
178d1 69 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 is not freed or
178d2 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 deallocated. It
178d3 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 is assumed that
178d4 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e .** the cell con
178d5 74 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f tent has been co
178d6 70 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 pied someplace e
178d7 6c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 lse. This routi
178d8 6e 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 ne just.** remov
178d9 65 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 es the reference
178da 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f to the cell fro
178db 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 m pPage..**.** "
178dc 73 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 sz" must be the
178dd 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
178de 69 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a in the cell..*/.
178df 73 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 static void drop
178e0 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 Cell(MemPage *pP
178e1 61 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e age, int idx, in
178e2 74 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b t sz, int *pRC){
178e3 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
178e4 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
178e5 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 er */. int pc;
178e6 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
178e7 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e t to cell conten
178e8 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 t of cell being
178e9 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 deleted */. u8
178ea 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 *data; /*
178eb 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a pPage->aData */.
178ec 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 u8 *ptr;
178ed 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 /* Used to mov
178ee 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77 e bytes around w
178ef 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a ithin data[] */.
178f0 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
178f1 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 /* The return
178f2 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 code */. int hd
178f3 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 r; /* Beg
178f4 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 inning of the he
178f5 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 ader. 0 most pa
178f6 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 ges. 100 page 1
178f7 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 */.. if( *pRC
178f8 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 ) return;.. ass
178f9 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 ert( idx>=0 && i
178fa 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 dx<pPage->nCell
178fb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d );. assert( sz=
178fc 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c =cellSize(pPage,
178fd 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 idx) );. asser
178fe 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
178ff 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
17900 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
17901 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
17902 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
17903 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
17904 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d . data = pPage-
17905 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 >aData;. ptr =
17906 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c &data[pPage->cel
17907 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d lOffset + 2*idx]
17908 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 ;. pc = get2byt
17909 65 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 e(ptr);. hdr =
1790a 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
1790b 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63 ;. testcase( pc
1790c 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 ==get2byte(&data
1790d 5b 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 [hdr+5]) );. te
1790e 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 stcase( pc+sz==p
1790f 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
17910 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 eSize );. if( p
17911 63 20 3c 20 67 65 74 32 62 79 74 65 28 26 64 61 c < get2byte(&da
17912 74 61 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 ta[hdr+5]) || pc
17913 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 +sz > pPage->pBt
17914 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a ->usableSize ){.
17915 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 *pRC = SQLIT
17916 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
17917 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
17918 20 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 rc = freeSpace
17919 28 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b (pPage, pc, sz);
1791a 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
1791b 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 *pRC = rc;.
1791c 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f return;. }. fo
1791d 72 28 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 r(i=idx+1; i<pPa
1791e 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 ge->nCell; i++,
1791f 70 74 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 ptr+=2){. ptr
17920 5b 30 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 [0] = ptr[2];.
17921 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 ptr[1] = ptr[3
17922 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e ];. }. pPage->
17923 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 nCell--;. put2b
17924 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
17925 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b , pPage->nCell);
17926 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 . pPage->nFree
17927 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 += 2;.}../*.** I
17928 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c nsert a new cell
17929 20 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c on pPage at cel
1792a 6c 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 l index "i". pC
1792b 65 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 ell points to th
1792c 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 e.** content of
1792d 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 the cell..**.**
1792e 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 If the cell cont
1792f 65 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 ent will fit on
17930 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 the page, then p
17931 75 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 ut it there. If
17932 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 it.** will not
17933 66 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 fit, then make a
17934 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c copy of the cel
17935 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 l content into p
17936 54 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 Temp if.** pTemp
17937 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 is not null. R
17938 65 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 egardless of pTe
17939 6d 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e mp, allocate a n
1793a 65 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 ew entry.** in p
1793b 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e Page->aOvfl[] an
1793c 64 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 d make it point
1793d 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 to the cell cont
1793e 65 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 ent (either.** i
1793f 6e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f n pTemp or the o
17940 72 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 riginal pCell) a
17941 6e 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 nd also record i
17942 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c ts index. .** Al
17943 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 locating a new e
17944 6e 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 ntry in pPage->a
17945 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 Cell[] implies t
17946 68 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e hat .** pPage->n
17947 4f 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 Overflow is incr
17948 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 emented..**.** I
17949 66 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a f nSkip is non-z
1794a 65 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 ero, then do not
1794b 20 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 copy the first
1794c 6e 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 nSkip bytes of t
1794d 68 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 he.** cell. The
1794e 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 caller will over
1794f 77 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 write them after
17950 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
17951 65 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 eturns. If.** nS
17952 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c kip is non-zero,
17953 20 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 then pCell may
17954 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 not point to an
17955 69 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c invalid memory l
17956 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 ocation .** (but
17957 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 pCell+nSkip is
17958 61 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a always valid)..*
17959 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
1795a 73 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 sertCell(. MemP
1795b 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a age *pPage, /*
1795c 20 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68 Page into which
1795d 20 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20 we are copying
1795e 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 */. int i,
1795f 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 /* New ce
17960 6c 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69 ll becomes the i
17961 2d 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 -th cell of the
17962 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 page */. u8 *pC
17963 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 ell, /* C
17964 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 ontent of the ne
17965 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 w cell */. int
17966 73 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a sz, /*
17967 20 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e Bytes of conten
17968 74 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 t in pCell */.
17969 75 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 u8 *pTemp,
1796a 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 /* Temp storag
1796b 65 20 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c e space for pCel
1796c 6c 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a l, if needed */.
1796d 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 Pgno iChild,
1796e 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 /* If non-ze
1796f 72 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72 73 ro, replace firs
17970 74 20 34 20 62 79 74 65 73 20 77 69 74 68 20 74 t 4 bytes with t
17971 68 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 his value */. i
17972 6e 74 20 2a 70 52 43 20 20 20 20 20 20 20 20 20 nt *pRC
17973 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 /* Read and wri
17974 74 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 te return code f
17975 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 rom here */.){.
17976 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 int idx;
17977 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 /* Where to w
17978 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f rite new cell co
17979 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20 ntent in data[]
1797a 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 */. int j;
1797b 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
1797c 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
1797d 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a end; /*
1797e 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74 First byte past
1797f 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 the last cell p
17980 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d ointer in data[]
17981 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 */. int ins;
17982 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
17983 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65 in data[] where
17984 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 new cell pointe
17985 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f r is inserted */
17986 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 . int cellOffse
17987 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 t; /* Address
17988 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f of first cell po
17989 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 inter in data[]
1798a 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 */. u8 *data;
1798b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
1798c 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f ntent of the who
1798d 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 le page */. u8
1798e 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f *ptr; /
1798f 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e * Used for movin
17990 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 g information ar
17991 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a ound in data[] *
17992 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d /.. int nSkip =
17993 20 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 30 (iChild ? 4 : 0
17994 29 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 );.. if( *pRC )
17995 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 return;.. asse
17996 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70 rt( i>=0 && i<=p
17997 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67 Page->nCell+pPag
17998 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a e->nOverflow );.
17999 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1799a 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 >nCell<=MX_CELL(
1799b 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d pPage->pBt) && M
1799c 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 X_CELL(pPage->pB
1799d 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 t)<=5460 );. as
1799e 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
1799f 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a erflow<=ArraySiz
179a0 65 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20 e(pPage->aOvfl)
179a1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d );. assert( sz=
179a2 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 =cellSizePtr(pPa
179a3 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20 20 ge, pCell) );.
179a4 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
179a5 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
179a6 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
179a7 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f . if( pPage->nO
179a8 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e verflow || sz+2>
179a9 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a pPage->nFree ){.
179aa 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 29 7b if( pTemp ){
179ab 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 . memcpy(pT
179ac 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c emp+nSkip, pCell
179ad 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 +nSkip, sz-nSkip
179ae 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d );. pCell =
179af 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 pTemp;. }.
179b0 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a if( iChild ){.
179b1 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 put4byte(p
179b2 43 65 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 Cell, iChild);.
179b3 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 }. j = pPa
179b4 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b ge->nOverflow++;
179b5 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28 . assert( j<(
179b6 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67 int)(sizeof(pPag
179b7 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 e->aOvfl)/sizeof
179b8 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d (pPage->aOvfl[0]
179b9 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d )) );. pPage-
179ba 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 >aOvfl[j].pCell
179bb 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 = pCell;. pPa
179bc 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 ge->aOvfl[j].idx
179bd 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c = (u16)i;. }el
179be 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d se{. int rc =
179bf 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
179c0 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 te(pPage->pDbPag
179c1 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
179c2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
179c3 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 *pRC = rc;.
179c4 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
179c5 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 }. assert( sq
179c6 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
179c7 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
179c8 50 61 67 65 29 20 29 3b 0a 20 20 20 20 64 61 74 Page) );. dat
179c9 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 a = pPage->aData
179ca 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 ;. cellOffset
179cb 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 = pPage->cellOf
179cc 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 fset;. end =
179cd 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 cellOffset + 2*p
179ce 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 Page->nCell;.
179cf 20 69 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 ins = cellOffse
179d0 74 20 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 t + 2*i;. rc
179d1 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 = allocateSpace(
179d2 70 50 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 pPage, sz, &idx)
179d3 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 20 ;. if( rc ){
179d4 2a 70 52 43 20 3d 20 72 63 3b 20 72 65 74 75 72 *pRC = rc; retur
179d5 6e 3b 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 n; }. /* The
179d6 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 29 20 allocateSpace()
179d7 72 6f 75 74 69 6e 65 20 67 75 61 72 61 6e 74 65 routine guarante
179d8 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 es the following
179d9 20 74 77 6f 20 70 72 6f 70 65 72 74 69 65 73 0a two properties.
179da 20 20 20 20 2a 2a 20 69 66 20 69 74 20 72 65 74 ** if it ret
179db 75 72 6e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a urns success */.
179dc 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 20 assert( idx
179dd 3e 3d 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 >= end+2 );.
179de 61 73 73 65 72 74 28 20 69 64 78 2b 73 7a 20 3c assert( idx+sz <
179df 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 = pPage->pBt->us
179e0 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 ableSize );.
179e1 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a pPage->nCell++;.
179e2 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 pPage->nFree
179e3 20 2d 3d 20 28 75 31 36 29 28 32 20 2b 20 73 7a -= (u16)(2 + sz
179e4 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 );. memcpy(&d
179e5 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 ata[idx+nSkip],
179e6 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d pCell+nSkip, sz-
179e7 6e 53 6b 69 70 29 3b 0a 20 20 20 20 69 66 28 20 nSkip);. if(
179e8 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 iChild ){.
179e9 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 69 put4byte(&data[i
179ea 64 78 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 dx], iChild);.
179eb 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e }. for(j=en
179ec 64 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b d, ptr=&data[j];
179ed 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74 j>ins; j-=2, pt
179ee 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 74 72 r-=2){. ptr
179ef 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20 [0] = ptr[-2];.
179f0 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 ptr[1] = pt
179f1 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 r[-1];. }.
179f2 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
179f3 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 ins], idx);.
179f4 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 put2byte(&data[p
179f5 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
179f6 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3], pPage->nCell
179f7 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
179f8 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
179f9 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d M. if( pPage-
179fa 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d >pBt->autoVacuum
179fb 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 ){. /* The
179fc 20 63 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 cell may contai
179fd 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 n a pointer to a
179fe 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e n overflow page.
179ff 20 49 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 If so, write.
17a00 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 ** the entry
17a01 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f for the overflo
17a02 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 w page into the
17a03 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 pointer map..
17a04 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d */. ptrm
17a05 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 apPutOvflPtr(pPa
17a06 67 65 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b ge, pCell, pRC);
17a07 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
17a08 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 }.}../*.** Add a
17a09 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 list of cells t
17a0a 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65 20 70 o a page. The p
17a0b 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e age should be in
17a0c 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a itially empty..*
17a0d 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72 65 20 * The cells are
17a0e 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69 guaranteed to fi
17a0f 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a t on the page..*
17a10 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 /.static void as
17a11 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 semblePage(. Me
17a12 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 mPage *pPage,
17a13 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 /* The page to b
17a14 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a e assemblied */.
17a15 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 int nCell,
17a16 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 /* The numbe
17a17 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 r of cells to ad
17a18 64 20 74 6f 20 74 68 69 73 20 70 61 67 65 20 2a d to this page *
17a19 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c /. u8 **apCell,
17a1a 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
17a1b 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 s to cell bodies
17a1c 20 2a 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 */. u16 *aSize
17a1d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 /* Sizes
17a1e 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f of the cells */
17a1f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 .){. int i;
17a20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
17a21 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 counter */. u8
17a22 2a 70 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f *pCellptr; /
17a23 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 * Address of nex
17a24 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a t cell pointer *
17a25 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 /. int cellbody
17a26 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 ; /* Address
17a27 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f of next cell bo
17a28 64 79 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e dy */. u8 * con
17a29 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d st data = pPage-
17a2a 3e 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 >aData;
17a2b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
17a2c 6f 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 o data for pPage
17a2d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 */. const int
17a2e 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
17a2f 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
17a30 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 /* Offset of h
17a31 65 61 64 65 72 20 6f 6e 20 70 50 61 67 65 20 2a eader on pPage *
17a32 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 /. const int nU
17a33 73 61 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 sable = pPage->p
17a34 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 Bt->usableSize;
17a35 2f 2a 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f /* Usable size o
17a36 66 20 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 f page */.. ass
17a37 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ert( pPage->nOve
17a38 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 rflow==0 );. as
17a39 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
17a3a 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
17a3b 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
17a3c 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d assert( nCell>=
17a3d 30 20 26 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 0 && nCell<=MX_C
17a3e 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 ELL(pPage->pBt)
17a3f 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 && MX_CELL(pPage
17a40 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a ->pBt)<=5460 );.
17a41 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17a42 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
17a43 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
17a44 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ) );.. /* Check
17a45 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 68 that the page h
17a46 61 73 20 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 as just been zer
17a47 6f 65 64 20 62 79 20 7a 65 72 6f 50 61 67 65 28 oed by zeroPage(
17a48 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 ) */. assert( p
17a49 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 Page->nCell==0 )
17a4a 3b 0a 20 20 61 73 73 65 72 74 28 20 67 65 74 32 ;. assert( get2
17a4b 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
17a4c 5d 29 3d 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a ])==nUsable );..
17a4d 20 20 70 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 pCellptr = &da
17a4e 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 ta[pPage->cellOf
17a4f 66 73 65 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b fset + nCell*2];
17a50 0a 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 . cellbody = nU
17a51 73 61 62 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e sable;. for(i=n
17a52 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d Cell-1; i>=0; i-
17a53 2d 29 7b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 -){. pCellptr
17a54 20 2d 3d 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 -= 2;. cellb
17a55 6f 64 79 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b ody -= aSize[i];
17a56 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70 43 . put2byte(pC
17a57 65 6c 6c 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 ellptr, cellbody
17a58 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 );. memcpy(&d
17a59 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 ata[cellbody], a
17a5a 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b pCell[i], aSize[
17a5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 i]);. }. put2b
17a5c 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
17a5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 , nCell);. put2
17a5e 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
17a5f 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 ], cellbody);.
17a60 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 pPage->nFree -=
17a61 28 6e 43 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 (nCell*2 + nUsab
17a62 6c 65 20 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a le - cellbody);.
17a63 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d pPage->nCell =
17a64 20 28 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a (u16)nCell;.}..
17a65 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
17a66 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 64 ing parameters d
17a67 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e etermine how man
17a68 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73 y adjacent pages
17a69 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a get involved.**
17a6a 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 in a balancing
17a6b 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 operation. NN i
17a6c 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
17a6d 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 neighbors on eit
17a6e 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 her side.** of t
17a6f 68 65 20 70 61 67 65 20 74 68 61 74 20 70 61 72 he page that par
17a70 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20 ticipate in the
17a71 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 balancing operat
17a72 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a ion. NB is the.
17a73 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 ** total number
17a74 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 70 61 of pages that pa
17a75 72 74 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 rticipate, inclu
17a76 64 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 ding the target
17a77 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e page and.** NN n
17a78 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 eighbors on eith
17a79 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 er side..**.** T
17a7a 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 he minimum value
17a7b 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 of NN is 1 (of
17a7c 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61 course). Increa
17a7d 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a sing NN above 1.
17a7e 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67 ** (to 2 or 3) g
17a7f 69 76 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d ives a modest im
17a80 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c provement in SEL
17a81 45 43 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70 ECT and DELETE p
17a82 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e erformance.** in
17a83 20 65 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20 exchange for a
17a84 6c 61 72 67 65 72 20 64 65 67 72 61 64 61 74 69 larger degradati
17a85 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 on in INSERT and
17a86 20 55 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61 UPDATE performa
17a87 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 nce..** The valu
17a88 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20 e of NN appears
17a89 74 6f 20 67 69 76 65 20 74 68 65 20 62 65 73 74 to give the best
17a8a 20 72 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c results overall
17a8b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 ..*/.#define NN
17a8c 31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 1 /*
17a8d 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 Number of neigh
17a8e 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73 bors on either s
17a8f 69 64 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a ide of pPage */.
17a90 23 64 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 #define NB (NN*2
17a91 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 +1) /* Tota
17a92 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 l pages involved
17a93 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20 in the balance
17a94 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */...#ifndef SQL
17a95 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 ITE_OMIT_QUICKBA
17a96 4c 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 LANCE./*.** This
17a97 20 76 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 version of bala
17a98 6e 63 65 28 29 20 68 61 6e 64 6c 65 73 20 74 68 nce() handles th
17a99 65 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c e common special
17a9a 20 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 case where.** a
17a9b 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 62 65 new entry is be
17a9c 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 ing inserted on
17a9d 74 68 65 20 65 78 74 72 65 6d 65 20 72 69 67 68 the extreme righ
17a9e 74 2d 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 t-end of the.**
17a9f 74 72 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 tree, in other w
17aa0 6f 72 64 73 2c 20 77 68 65 6e 20 74 68 65 20 6e ords, when the n
17aa1 65 77 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 ew entry will be
17aa2 63 6f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 74 come the largest
17aa3 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 .** entry in the
17aa4 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 tree..**.** Ins
17aa5 74 65 61 64 20 6f 66 20 74 72 79 69 6e 67 20 74 tead of trying t
17aa6 6f 20 62 61 6c 61 6e 63 65 20 74 68 65 20 33 20 o balance the 3
17aa7 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 right-most leaf
17aa8 70 61 67 65 73 2c 20 6a 75 73 74 20 61 64 64 0a pages, just add.
17aa9 2a 2a 20 61 20 6e 65 77 20 70 61 67 65 20 74 6f ** a new page to
17aaa 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 the right-hand
17aab 73 69 64 65 20 61 6e 64 20 70 75 74 20 74 68 65 side and put the
17aac 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72 79 20 69 one new entry i
17aad 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 2e 20 n.** that page.
17aae 20 54 68 69 73 20 6c 65 61 76 65 73 20 74 68 65 This leaves the
17aaf 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 20 74 right side of t
17ab0 68 65 20 74 72 65 65 20 73 6f 6d 65 77 68 61 74 he tree somewhat
17ab1 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 .** unbalanced.
17ab2 20 42 75 74 20 6f 64 64 73 20 61 72 65 20 74 68 But odds are th
17ab3 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e at we will be in
17ab4 73 65 72 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 serting new entr
17ab5 69 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e ies.** at the en
17ab6 64 20 73 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 d soon afterward
17ab7 73 20 73 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 s so the nearly
17ab8 65 6d 70 74 79 20 70 61 67 65 20 77 69 6c 6c 20 empty page will
17ab9 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 quickly.** fill
17aba 75 70 2e 20 20 4f 6e 20 61 76 65 72 61 67 65 2e up. On average.
17abb 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 .**.** pPage is
17abc 74 68 65 20 6c 65 61 66 20 70 61 67 65 20 77 68 the leaf page wh
17abd 69 63 68 20 69 73 20 74 68 65 20 72 69 67 68 74 ich is the right
17abe 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e 20 74 68 -most page in th
17abf 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 e tree..** pPare
17ac0 6e 74 20 69 73 20 69 74 73 20 70 61 72 65 6e 74 nt is its parent
17ac1 2e 20 20 70 50 61 67 65 20 6d 75 73 74 20 68 61 . pPage must ha
17ac2 76 65 20 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 ve a single over
17ac3 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 flow entry.** wh
17ac4 69 63 68 20 69 73 20 61 6c 73 6f 20 74 68 65 20 ich is also the
17ac5 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 right-most entry
17ac6 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a on the page..**
17ac7 0a 2a 2a 20 54 68 65 20 70 53 70 61 63 65 20 62 .** The pSpace b
17ac8 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74 6f uffer is used to
17ac9 20 73 74 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 store a tempora
17aca 72 79 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 ry copy of the d
17acb 69 76 69 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 ivider.** cell t
17acc 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 hat will be inse
17acd 72 74 65 64 20 69 6e 74 6f 20 70 50 61 72 65 6e rted into pParen
17ace 74 2e 20 53 75 63 68 20 61 20 63 65 6c 6c 20 63 t. Such a cell c
17acf 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 0a 2a onsists of a 4.*
17ad0 2a 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62 * byte page numb
17ad1 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 er followed by a
17ad2 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
17ad3 20 69 6e 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 integer. In oth
17ad4 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 er.** words, at
17ad5 6d 6f 73 74 20 31 33 20 62 79 74 65 73 2e 20 48 most 13 bytes. H
17ad6 65 6e 63 65 20 74 68 65 20 70 53 70 61 63 65 20 ence the pSpace
17ad7 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 61 buffer must be a
17ad8 74 0a 2a 2a 20 6c 65 61 73 74 20 31 33 20 62 79 t.** least 13 by
17ad9 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a tes in size..*/.
17ada 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e static int balan
17adb 63 65 5f 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 ce_quick(MemPage
17adc 20 2a 70 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 *pParent, MemPa
17add 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 ge *pPage, u8 *p
17ade 53 70 61 63 65 29 7b 0a 20 20 42 74 53 68 61 72 Space){. BtShar
17adf 65 64 20 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 ed *const pBt =
17ae0 70 50 61 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f pPage->pBt; /
17ae1 2a 20 42 2d 54 72 65 65 20 44 61 74 61 62 61 73 * B-Tree Databas
17ae2 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a e */. MemPage *
17ae3 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 pNew;
17ae4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17ae5 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 ewly allocated p
17ae6 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b age */. int rc;
17ae7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17ae9 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a Return Code */.
17aea 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 Pgno pgnoNew;
17aeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17aec 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
17aed 75 6d 62 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f umber of pNew */
17aee 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
17aef 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
17af0 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
17af1 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
17af2 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
17af3 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e teable(pParent->
17af4 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 pDbPage) );. as
17af5 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
17af6 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 erflow==1 );..
17af7 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c if( pPage->nCell
17af8 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c <=0 ) return SQL
17af9 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
17afa 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 ;.. /* Allocate
17afb 20 61 20 6e 65 77 20 70 61 67 65 2e 20 54 68 69 a new page. Thi
17afc 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f s page will beco
17afd 6d 65 20 74 68 65 20 72 69 67 68 74 2d 73 69 62 me the right-sib
17afe 6c 69 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 ling of . ** pP
17aff 61 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 age. Make the pa
17b00 72 65 6e 74 20 70 61 67 65 20 77 72 69 74 61 62 rent page writab
17b01 6c 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20 le, so that the
17b02 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c new divider cell
17b03 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 . ** may be ins
17b04 65 72 74 65 64 2e 20 49 66 20 62 6f 74 68 20 74 erted. If both t
17b05 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 hese operations
17b06 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 2c 20 are successful,
17b07 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 proceed.. */.
17b08 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 rc = allocateBtr
17b09 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 eePage(pBt, &pNe
17b0a 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 w, &pgnoNew, 0,
17b0b 30 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 0);.. if( rc==S
17b0c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 QLITE_OK ){..
17b0d 20 75 38 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 u8 *pOut = &pSp
17b0e 61 63 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a ace[4];. u8 *
17b0f 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 pCell = pPage->a
17b10 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 Ovfl[0].pCell;.
17b11 20 20 20 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 u16 szCell =
17b12 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 cellSizePtr(pPag
17b13 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 e, pCell);. u
17b14 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 8 *pStop;.. a
17b15 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
17b16 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
17b17 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 20 29 3b New->pDbPage) );
17b18 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
17b19 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 ge->aData[0]==(P
17b1a 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 TF_INTKEY|PTF_LE
17b1b 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 AFDATA|PTF_LEAF)
17b1c 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 );. zeroPage
17b1d 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 (pNew, PTF_INTKE
17b1e 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 Y|PTF_LEAFDATA|P
17b1f 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 TF_LEAF);. as
17b20 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c semblePage(pNew,
17b21 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 1, &pCell, &szC
17b22 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 ell);.. /* If
17b23 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f this is an auto
17b24 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 -vacuum database
17b25 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69 , update the poi
17b26 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 nter map. **
17b27 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72 with entries for
17b28 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61 the new page, a
17b29 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 nd any pointer f
17b2a 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 rom the . **
17b2b 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 cell on the page
17b2c 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 to an overflow
17b2d 70 61 67 65 2e 20 49 66 20 65 69 74 68 65 72 20 page. If either
17b2e 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 of these. **
17b2f 6f 70 65 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 operations fails
17b30 2c 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 , the return cod
17b31 65 20 69 73 20 73 65 74 2c 20 62 75 74 20 74 68 e is set, but th
17b32 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a e contents. *
17b33 2a 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 * of the parent
17b34 70 61 67 65 20 61 72 65 20 73 74 69 6c 6c 20 6d page are still m
17b35 61 6e 69 70 75 6c 61 74 65 64 20 62 79 20 74 68 anipulated by th
17b36 68 20 63 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 h code below..
17b37 20 20 2a 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c ** That is Ok,
17b38 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 at this point t
17b39 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 he parent page i
17b3a 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a s guaranteed to.
17b3b 20 20 20 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 ** be marked
17b3c 20 61 73 20 64 69 72 74 79 2e 20 52 65 74 75 72 as dirty. Retur
17b3d 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f ning an error co
17b3e 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 61 0a de will cause a.
17b3f 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c ** rollback,
17b40 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 undoing any cha
17b41 6e 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65 nges made to the
17b42 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 parent page..
17b43 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41 */. if( ISA
17b44 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
17b45 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
17b46 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 , pgnoNew, PTRMA
17b47 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 P_BTREE, pParent
17b48 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 ->pgno, &rc);.
17b49 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 if( szCell>p
17b4a 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b New->minLocal ){
17b4b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 . ptrmapP
17b4c 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 utOvflPtr(pNew,
17b4d 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 pCell, &rc);.
17b4e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
17b4f 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 64 69 /* Create a di
17b50 76 69 64 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e vider cell to in
17b51 73 65 72 74 20 69 6e 74 6f 20 70 50 61 72 65 6e sert into pParen
17b52 74 2e 20 54 68 65 20 64 69 76 69 64 65 72 20 63 t. The divider c
17b53 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 ell. ** consi
17b54 73 74 73 20 6f 66 20 61 20 34 2d 62 79 74 65 20 sts of a 4-byte
17b55 70 61 67 65 20 6e 75 6d 62 65 72 20 28 74 68 65 page number (the
17b56 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
17b57 70 50 61 67 65 29 20 61 6e 64 0a 20 20 20 20 2a pPage) and. *
17b58 2a 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e * a variable len
17b59 67 74 68 20 6b 65 79 20 76 61 6c 75 65 20 28 77 gth key value (w
17b5a 68 69 63 68 20 6d 75 73 74 20 62 65 20 74 68 65 hich must be the
17b5b 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 same value as t
17b5c 68 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 he. ** larges
17b5d 74 20 6b 65 79 20 6f 6e 20 70 50 61 67 65 29 2e t key on pPage).
17b5e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
17b5f 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 o find the large
17b60 73 74 20 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 st key value on
17b61 70 50 61 67 65 2c 20 66 69 72 73 74 20 66 69 6e pPage, first fin
17b62 64 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 d the right-most
17b63 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e . ** cell on
17b64 20 70 50 61 67 65 2e 20 54 68 65 20 66 69 72 73 pPage. The firs
17b65 74 20 74 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 t two fields of
17b66 74 68 69 73 20 63 65 6c 6c 20 61 72 65 20 74 68 this cell are th
17b67 65 20 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 e . ** record
17b68 2d 6c 65 6e 67 74 68 20 28 61 20 76 61 72 69 61 -length (a varia
17b69 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble length integ
17b6a 65 72 20 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 er at most 32-bi
17b6b 74 73 20 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 ts in size).
17b6c 2a 2a 20 61 6e 64 20 74 68 65 20 6b 65 79 20 76 ** and the key v
17b6d 61 6c 75 65 20 28 61 20 76 61 72 69 61 62 6c 65 alue (a variable
17b6e 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 2c length integer,
17b6f 20 6d 61 79 20 68 61 76 65 20 61 6e 79 20 76 61 may have any va
17b70 6c 75 65 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 lue).. ** The
17b71 20 66 69 72 73 74 20 6f 66 20 74 68 65 20 77 68 first of the wh
17b72 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 ile(...) loops b
17b73 65 6c 6f 77 20 73 6b 69 70 73 20 6f 76 65 72 20 elow skips over
17b74 74 68 65 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 the record-lengt
17b75 68 0a 20 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 h. ** field.
17b76 54 68 65 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 The second while
17b77 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 (...) loop copie
17b78 73 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 s the key value
17b79 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 from the. **
17b7a 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e cell on pPage in
17b7b 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 75 to the pSpace bu
17b7c 66 66 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ffer.. */.
17b7d 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c pCell = findCel
17b7e 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e l(pPage, pPage->
17b7f 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 nCell-1);. pS
17b80 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b top = &pCell[9];
17b81 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28 70 . while( (*(p
17b82 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 Cell++)&0x80) &&
17b83 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a pCell<pStop );.
17b84 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65 pStop = &pCe
17b85 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 ll[9];. while
17b86 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 ( ((*(pOut++) =
17b87 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 *(pCell++))&0x80
17b88 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 ) && pCell<pStop
17b89 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 );.. /* Inse
17b8a 72 74 20 74 68 65 20 6e 65 77 20 64 69 76 69 64 rt the new divid
17b8b 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 er cell into pPa
17b8c 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 rent. */. ins
17b8d 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c ertCell(pParent,
17b8e 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c pParent->nCell,
17b8f 20 70 53 70 61 63 65 2c 20 28 69 6e 74 29 28 70 pSpace, (int)(p
17b90 4f 75 74 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 Out-pSpace),.
17b91 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 70 0, p
17b92 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 Page->pgno, &rc)
17b93 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 ;.. /* Set th
17b94 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f e right-child po
17b95 69 6e 74 65 72 20 6f 66 20 70 50 61 72 65 6e 74 inter of pParent
17b96 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 to point to the
17b97 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 new page. */.
17b98 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 72 put4byte(&pPar
17b99 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 ent->aData[pPare
17b9a 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d nt->hdrOffset+8]
17b9b 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 , pgnoNew);. .
17b9c 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 /* Release th
17b9d 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 e reference to t
17b9e 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a he new page. */.
17b9f 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
17ba0 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 pNew);. }.. re
17ba1 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
17ba2 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
17ba3 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f _QUICKBALANCE */
17ba4 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 ..#if 0./*.** Th
17ba5 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 is function does
17ba6 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75 74 65 20 not contribute
17ba7 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 anything to the
17ba8 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c operation of SQL
17ba9 69 74 65 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f ite..** it is so
17baa 6d 65 74 69 6d 65 73 20 61 63 74 69 76 61 74 65 metimes activate
17bab 64 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 d temporarily wh
17bac 69 6c 65 20 64 65 62 75 67 67 69 6e 67 20 63 6f ile debugging co
17bad 64 65 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a de responsible .
17bae 2a 2a 20 66 6f 72 20 73 65 74 74 69 6e 67 20 70 ** for setting p
17baf 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 ointer-map entri
17bb0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e es..*/.static in
17bb1 74 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 t ptrmapCheckPag
17bb2 65 73 28 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 es(MemPage **apP
17bb3 61 67 65 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b age, int nPage){
17bb4 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 . int i, j;. f
17bb5 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b or(i=0; i<nPage;
17bb6 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 i++){. Pgno
17bb7 6e 3b 0a 20 20 20 20 75 38 20 65 3b 0a 20 20 20 n;. u8 e;.
17bb8 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 MemPage *pPage
17bb9 3d 20 61 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 = apPage[i];.
17bba 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
17bbb 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 pPage->pBt;.
17bbc 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
17bbd 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 isInit );.. f
17bbe 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d or(j=0; j<pPage-
17bbf 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 >nCell; j++){.
17bc0 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 CellInfo inf
17bc1 6f 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a o;. u8 *z;.
17bc2 20 20 20 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 . z =
17bc3 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
17bc4 6a 29 3b 0a 20 20 20 20 20 20 62 74 72 65 65 50 j);. btreeP
17bc5 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 arseCellPtr(pPag
17bc6 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 e, z, &info);.
17bc7 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 if( info.iOv
17bc8 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 erflow ){.
17bc9 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 Pgno ovfl = ge
17bca 74 34 62 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 t4byte(&z[info.i
17bcb 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 Overflow]);.
17bcc 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42 ptrmapGet(pB
17bcd 74 2c 20 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 t, ovfl, &e, &n)
17bce 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
17bcf 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f ( n==pPage->pgno
17bd0 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 && e==PTRMAP_OV
17bd1 45 52 46 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 ERFLOW1 );.
17bd2 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 }. if( !pP
17bd3 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
17bd4 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 Pgno child
17bd5 3d 20 67 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 = get4byte(z);.
17bd6 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 ptrmapGet
17bd7 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c (pBt, child, &e,
17bd8 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 &n);. as
17bd9 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e sert( n==pPage->
17bda 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 pgno && e==PTRMA
17bdb 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20 20 P_BTREE );.
17bdc 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
17bdd 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
17bde 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c . Pgno chil
17bdf 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 d = get4byte(&pP
17be0 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
17be1 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
17be2 0a 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 . ptrmapGet
17be3 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c (pBt, child, &e,
17be4 20 26 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 &n);. asse
17be5 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 rt( n==pPage->pg
17be6 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f no && e==PTRMAP_
17be7 42 54 52 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 BTREE );. }.
17be8 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d }. return 1;.}
17be9 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
17bea 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
17beb 75 73 65 64 20 74 6f 20 63 6f 70 79 20 74 68 65 used to copy the
17bec 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
17bed 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 73 74 6f b-tree node sto
17bee 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 red .** on page
17bef 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 pFrom to page pT
17bf0 6f 2e 20 49 66 20 70 61 67 65 20 70 46 72 6f 6d o. If page pFrom
17bf1 20 77 61 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 was not a leaf
17bf2 70 61 67 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 page, then.** th
17bf3 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e e pointer-map en
17bf4 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 63 tries for each c
17bf5 68 69 6c 64 20 70 61 67 65 20 61 72 65 20 75 70 hild page are up
17bf6 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 dated so that th
17bf7 65 0a 2a 2a 20 70 61 72 65 6e 74 20 70 61 67 65 e.** parent page
17bf8 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70 stored in the p
17bf9 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 70 61 ointer map is pa
17bfa 67 65 20 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d ge pTo. If pFrom
17bfb 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e contained.** an
17bfc 79 20 63 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 y cells with ove
17bfd 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 rflow page point
17bfe 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f ers, then the co
17bff 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e rresponding poin
17c00 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 ter.** map entri
17c01 65 73 20 61 72 65 20 61 6c 73 6f 20 75 70 64 61 es are also upda
17c02 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 ted so that the
17c03 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 70 parent page is p
17c04 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 age pTo..**.** I
17c05 66 20 70 46 72 6f 6d 20 69 73 20 63 75 72 72 65 f pFrom is curre
17c06 6e 74 6c 79 20 63 61 72 72 79 69 6e 67 20 61 6e ntly carrying an
17c07 79 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 y overflow cells
17c08 20 28 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 (entries in the
17c09 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66 .** MemPage.aOvf
17c0a 6c 5b 5d 20 61 72 72 61 79 29 2c 20 74 68 65 79 l[] array), they
17c0b 20 61 72 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 are not copied
17c0c 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 to pTo. .**.** B
17c0d 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c efore returning,
17c0e 20 70 61 67 65 20 70 54 6f 20 69 73 20 72 65 69 page pTo is rei
17c0f 6e 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 nitialized using
17c10 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 btreeInitPage()
17c11 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 ..**.** The perf
17c12 6f 72 6d 61 6e 63 65 20 6f 66 20 74 68 69 73 20 ormance of this
17c13 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 function is not
17c14 63 72 69 74 69 63 61 6c 2e 20 49 74 20 69 73 20 critical. It is
17c15 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 0a 2a 2a only used by .**
17c16 20 74 68 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 the balance_sha
17c17 6c 6c 6f 77 65 72 28 29 20 61 6e 64 20 62 61 6c llower() and bal
17c18 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 70 72 ance_deeper() pr
17c19 6f 63 65 64 75 72 65 73 2c 20 6e 65 69 74 68 65 ocedures, neithe
17c1a 72 20 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 r of.** which ar
17c1b 65 20 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 e called often u
17c1c 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 nder normal circ
17c1d 75 6d 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 umstances..*/.st
17c1e 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f atic void copyNo
17c1f 64 65 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 deContent(MemPag
17c20 65 20 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 e *pFrom, MemPag
17c21 65 20 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 e *pTo, int *pRC
17c22 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d ){. if( (*pRC)=
17c23 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17c24 20 20 42 74 53 68 61 72 65 64 20 2a 20 63 6f 6e BtShared * con
17c25 73 74 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e st pBt = pFrom->
17c26 70 42 74 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f pBt;. u8 * co
17c27 6e 73 74 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f nst aFrom = pFro
17c28 6d 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 75 38 m->aData;. u8
17c29 20 2a 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 * const aTo = p
17c2a 54 6f 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 To->aData;. i
17c2b 6e 74 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 nt const iFromHd
17c2c 72 20 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 r = pFrom->hdrOf
17c2d 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f fset;. int co
17c2e 6e 73 74 20 69 54 6f 48 64 72 20 3d 20 28 28 70 nst iToHdr = ((p
17c2f 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 To->pgno==1) ? 1
17c30 30 30 20 3a 20 30 29 3b 0a 20 20 20 20 54 45 53 00 : 0);. TES
17c31 54 4f 4e 4c 59 28 69 6e 74 20 72 63 3b 29 0a 20 TONLY(int rc;).
17c32 20 20 20 69 6e 74 20 69 44 61 74 61 3b 0a 20 20 int iData;.
17c33 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 . . assert(
17c34 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b pFrom->isInit );
17c35 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 . assert( pFr
17c36 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 64 om->nFree>=iToHd
17c37 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 r );. assert(
17c38 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d get2byte(&aFrom
17c39 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d 70 [iFromHdr+5])<=p
17c3a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 Bt->usableSize )
17c3b 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70 79 ;. . /* Copy
17c3c 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 the b-tree node
17c3d 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70 61 content from pa
17c3e 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 ge pFrom to page
17c3f 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 61 pTo. */. iDa
17c40 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 26 61 ta = get2byte(&a
17c41 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d From[iFromHdr+5]
17c42 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 );. memcpy(&a
17c43 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 72 6f To[iData], &aFro
17c44 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75 m[iData], pBt->u
17c45 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 61 29 sableSize-iData)
17c46 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54 ;. memcpy(&aT
17c47 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f o[iToHdr], &aFro
17c48 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72 m[iFromHdr], pFr
17c49 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b om->cellOffset +
17c4a 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29 2*pFrom->nCell)
17c4b 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 69 6e ;. . /* Rein
17c4c 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70 54 itialize page pT
17c4d 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f o so that the co
17c4e 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d 65 ntents of the Me
17c4f 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 0a mPage structure.
17c50 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68 65 ** match the
17c51 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20 69 new data. The i
17c52 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 nitialization of
17c53 20 70 54 6f 20 22 63 61 6e 6e 6f 74 22 20 66 61 pTo "cannot" fa
17c54 69 6c 2c 20 61 73 20 74 68 65 0a 20 20 20 20 2a il, as the. *
17c55 2a 20 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 * data copied fr
17c56 6f 6d 20 70 46 72 6f 6d 20 69 73 20 6b 6e 6f 77 om pFrom is know
17c57 6e 20 74 6f 20 62 65 20 76 61 6c 69 64 2e 20 20 n to be valid.
17c58 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e */. pTo->isIn
17c59 69 74 20 3d 20 30 3b 0a 20 20 20 20 54 45 53 54 it = 0;. TEST
17c5a 4f 4e 4c 59 28 72 63 20 3d 20 29 20 62 74 72 65 ONLY(rc = ) btre
17c5b 65 49 6e 69 74 50 61 67 65 28 70 54 6f 29 3b 0a eInitPage(pTo);.
17c5c 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
17c5d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 0a SQLITE_OK );. .
17c5e 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
17c5f 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d s an auto-vacuum
17c60 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 database, updat
17c61 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 e the pointer-ma
17c62 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a p entries. **
17c63 20 66 6f 72 20 61 6e 79 20 62 2d 74 72 65 65 20 for any b-tree
17c64 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 or overflow page
17c65 73 20 74 68 61 74 20 70 54 6f 20 6e 6f 77 20 63 s that pTo now c
17c66 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e ontains the poin
17c67 74 65 72 73 20 74 6f 2e 0a 20 20 20 20 2a 2f 0a ters to.. */.
17c68 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 if( ISAUTOVA
17c69 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 2a 70 CUUM ){. *p
17c6a 52 43 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 RC = setChildPtr
17c6b 6d 61 70 73 28 70 54 6f 29 3b 0a 20 20 20 20 7d maps(pTo);. }
17c6c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
17c6d 69 73 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73 is routine redis
17c6e 74 72 69 62 75 74 65 73 20 63 65 6c 6c 73 20 6f tributes cells o
17c6f 6e 20 74 68 65 20 69 50 61 72 65 6e 74 49 64 78 n the iParentIdx
17c70 27 74 68 20 63 68 69 6c 64 20 6f 66 20 70 50 61 'th child of pPa
17c71 72 65 6e 74 0a 2a 2a 20 28 68 65 72 65 61 66 74 rent.** (hereaft
17c72 65 72 20 22 74 68 65 20 70 61 67 65 22 29 20 61 er "the page") a
17c73 6e 64 20 75 70 20 74 6f 20 32 20 73 69 62 6c 69 nd up to 2 sibli
17c74 6e 67 73 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 ngs so that all
17c75 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 pages have about
17c76 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f the.** same amo
17c77 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 unt of free spac
17c78 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 73 69 6e e. Usually a sin
17c79 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65 gle sibling on e
17c7a 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 ither side of th
17c7b 65 0a 2a 2a 20 70 61 67 65 20 61 72 65 20 75 73 e.** page are us
17c7c 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 ed in the balanc
17c7d 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 6f 74 68 ing, though both
17c7e 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 68 74 20 siblings might
17c7f 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a come from one.**
17c80 20 73 69 64 65 20 69 66 20 74 68 65 20 70 61 67 side if the pag
17c81 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f e is the first o
17c82 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f 66 20 r last child of
17c83 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 74 its parent. If t
17c84 68 65 20 70 61 67 65 20 0a 2a 2a 20 68 61 73 20 he page .** has
17c85 66 65 77 65 72 20 74 68 61 6e 20 32 20 73 69 62 fewer than 2 sib
17c86 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68 69 6e 67 lings (something
17c87 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79 20 which can only
17c88 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 70 61 happen if the pa
17c89 67 65 0a 2a 2a 20 69 73 20 61 20 72 6f 6f 74 20 ge.** is a root
17c8a 70 61 67 65 20 6f 72 20 61 20 63 68 69 6c 64 20 page or a child
17c8b 6f 66 20 61 20 72 6f 6f 74 20 70 61 67 65 29 20 of a root page)
17c8c 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62 then all availab
17c8d 6c 65 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 70 le siblings.** p
17c8e 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 articipate in th
17c8f 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a e balancing..**.
17c90 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** The number of
17c91 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 74 68 65 siblings of the
17c92 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 69 page might be i
17c93 6e 63 72 65 61 73 65 64 20 6f 72 20 64 65 63 72 ncreased or decr
17c94 65 61 73 65 64 20 62 79 20 0a 2a 2a 20 6f 6e 65 eased by .** one
17c95 20 6f 72 20 74 77 6f 20 69 6e 20 61 6e 20 65 66 or two in an ef
17c96 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 70 61 67 fort to keep pag
17c97 65 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 62 es nearly full b
17c98 75 74 20 6e 6f 74 20 6f 76 65 72 20 66 75 6c 6c ut not over full
17c99 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 . .**.** Note th
17c9a 61 74 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 at when this rou
17c9b 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 tine is called,
17c9c 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c some of the cell
17c9d 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a s on the page.**
17c9e 20 6d 69 67 68 74 20 6e 6f 74 20 61 63 74 75 61 might not actua
17c9f 6c 6c 79 20 62 65 20 73 74 6f 72 65 64 20 69 6e lly be stored in
17ca0 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d MemPage.aData[]
17ca1 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 . This can happe
17ca2 6e 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 n.** if the page
17ca3 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 54 68 is overfull. Th
17ca4 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 is routine ensur
17ca5 65 73 20 74 68 61 74 20 61 6c 6c 20 63 65 6c 6c es that all cell
17ca6 73 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 74 s allocated.** t
17ca7 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 o the page and i
17ca8 74 73 20 73 69 62 6c 69 6e 67 73 20 66 69 74 20 ts siblings fit
17ca9 69 6e 74 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61 into MemPage.aDa
17caa 74 61 5b 5d 20 62 65 66 6f 72 65 20 72 65 74 75 ta[] before retu
17cab 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 rning..**.** In
17cac 74 68 65 20 63 6f 75 72 73 65 20 6f 66 20 62 61 the course of ba
17cad 6c 61 6e 63 69 6e 67 20 74 68 65 20 70 61 67 65 lancing the page
17cae 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 and its sibling
17caf 73 2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65 0a s, cells may be.
17cb0 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f ** inserted into
17cb1 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d or removed from
17cb2 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
17cb3 20 28 70 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e (pParent). Doin
17cb4 67 20 73 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 73 g so.** may caus
17cb5 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 e the parent pag
17cb6 65 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 e to become over
17cb7 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c full or underful
17cb8 6c 2e 20 49 66 20 74 68 69 73 0a 2a 2a 20 68 61 l. If this.** ha
17cb9 70 70 65 6e 73 2c 20 69 74 20 69 73 20 74 68 65 ppens, it is the
17cba 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 responsibility
17cbb 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f of the caller to
17cbc 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 72 invoke the corr
17cbd 65 63 74 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e 67 ect.** balancing
17cbe 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 69 78 20 routine to fix
17cbf 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 73 65 this problem (se
17cc0 65 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 20 e the balance()
17cc1 72 6f 75 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a routine). .**.**
17cc2 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 If this routine
17cc3 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 fails for any r
17cc4 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20 eason, it might
17cc5 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 61 leave the databa
17cc6 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75 se.** in a corru
17cc7 70 74 65 64 20 73 74 61 74 65 2e 20 53 6f 20 69 pted state. So i
17cc8 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 f this routine f
17cc9 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62 61 ails, the databa
17cca 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 se should.** be
17ccb 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
17ccc 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 ** The third arg
17ccd 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 ument to this fu
17cce 6e 63 74 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 61 nction, aOvflSpa
17ccf 63 65 2c 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ce, is a pointer
17cd0 20 74 6f 20 61 0a 2a 2a 20 62 75 66 66 65 72 20 to a.** buffer
17cd1 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f big enough to ho
17cd2 6c 64 20 6f 6e 65 20 70 61 67 65 2e 20 49 66 20 ld one page. If
17cd3 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 while inserting
17cd4 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 cells into the p
17cd5 61 72 65 6e 74 0a 2a 2a 20 70 61 67 65 20 28 70 arent.** page (p
17cd6 50 61 72 65 6e 74 29 20 74 68 65 20 70 61 72 65 Parent) the pare
17cd7 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20 nt page becomes
17cd8 6f 76 65 72 66 75 6c 6c 2c 20 74 68 69 73 20 62 overfull, this b
17cd9 75 66 66 65 72 20 69 73 0a 2a 2a 20 75 73 65 64 uffer is.** used
17cda 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 70 61 to store the pa
17cdb 72 65 6e 74 27 73 20 6f 76 65 72 66 6c 6f 77 20 rent's overflow
17cdc 63 65 6c 6c 73 2e 20 42 65 63 61 75 73 65 20 74 cells. Because t
17cdd 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73 his function ins
17cde 65 72 74 73 0a 2a 2a 20 61 20 6d 61 78 69 6d 75 erts.** a maximu
17cdf 6d 20 6f 66 20 66 6f 75 72 20 64 69 76 69 64 65 m of four divide
17ce0 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 r cells into the
17ce1 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 61 6e parent page, an
17ce2 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a d the maximum.**
17ce3 20 73 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 size of a cell
17ce4 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 61 6e stored within an
17ce5 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69 internal node i
17ce6 73 20 61 6c 77 61 79 73 20 6c 65 73 73 20 74 68 s always less th
17ce7 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 65 an 1/4.** of the
17ce8 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20 page-size, the
17ce9 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 62 75 66 aOvflSpace[] buf
17cea 66 65 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 fer is guarantee
17ceb 64 20 74 6f 20 62 65 20 6c 61 72 67 65 0a 2a 2a d to be large.**
17cec 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20 enough for all
17ced 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a overflow cells..
17cee 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76 66 6c 53 70 **.** If aOvflSp
17cef 61 63 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 ace is set to a
17cf0 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2c 20 74 68 null pointer, th
17cf1 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
17cf2 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e rns .** SQLITE_N
17cf3 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 OMEM..*/.static
17cf4 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 int balance_nonr
17cf5 6f 6f 74 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a oot(. MemPage *
17cf6 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 pParent,
17cf7 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 /* Parent
17cf8 20 70 61 67 65 20 6f 66 20 73 69 62 6c 69 6e 67 page of sibling
17cf9 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64 s being balanced
17cfa 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e */. int iParen
17cfb 74 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20 tIdx,
17cfc 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
17cfd 66 20 22 74 68 65 20 70 61 67 65 22 20 69 6e 20 f "the page" in
17cfe 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38 20 pParent */. u8
17cff 2a 61 4f 76 66 6c 53 70 61 63 65 2c 20 20 20 20 *aOvflSpace,
17d00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17d01 70 61 67 65 2d 73 69 7a 65 20 62 79 74 65 73 20 page-size bytes
17d02 6f 66 20 73 70 61 63 65 20 66 6f 72 20 70 61 72 of space for par
17d03 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 6e ent ovfl */. in
17d04 74 20 69 73 52 6f 6f 74 20 20 20 20 20 20 20 20 t isRoot
17d05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17d06 20 54 72 75 65 20 69 66 20 70 50 61 72 65 6e 74 True if pParent
17d07 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 is a root-page
17d08 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 */.){. BtShared
17d09 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 *pBt;
17d0a 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c /* The whol
17d0b 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 e database */.
17d0c 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 int nCell = 0;
17d0d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17d0e 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 Number of cells
17d0f 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 in apCell[] */.
17d10 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d int nMaxCells =
17d11 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
17d12 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 Allocated size
17d13 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c of apCell, szCel
17d14 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 l, aFrom. */. i
17d15 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20 nt nNew = 0;
17d16 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17d17 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
17d18 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 n apNew[] */. i
17d19 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 nt nOld;
17d1a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
17d1b 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
17d1c 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 n apOld[] */. i
17d1d 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 nt i, j, k;
17d1e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
17d1f 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a oop counters */.
17d20 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 20 20 20 int nxDiv;
17d21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17d22 2a 20 4e 65 78 74 20 64 69 76 69 64 65 72 20 73 * Next divider s
17d23 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e lot in pParent->
17d24 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 aCell[] */. int
17d25 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
17d26 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
17d27 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a return code */.
17d28 20 20 75 31 36 20 6c 65 61 66 43 6f 72 72 65 63 u16 leafCorrec
17d29 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 2f tion; /
17d2a 2a 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20 * 4 if pPage is
17d2b 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 6e 6f a leaf. 0 if no
17d2c 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44 t */. int leafD
17d2d 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 ata;
17d2e 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
17d2f 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6f Page is a leaf o
17d30 66 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 f a LEAFDATA tre
17d31 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c e */. int usabl
17d32 65 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 eSpace;
17d33 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 /* Bytes in
17d34 70 50 61 67 65 20 62 65 79 6f 6e 64 20 74 68 65 pPage beyond the
17d35 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 header */. int
17d36 20 70 61 67 65 46 6c 61 67 73 3b 20 20 20 20 20 pageFlags;
17d37 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c /* Val
17d38 75 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 ue of pPage->aDa
17d39 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 ta[0] */. int s
17d3a 75 62 74 6f 74 61 6c 3b 20 20 20 20 20 20 20 20 ubtotal;
17d3b 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 74 6f /* Subto
17d3c 74 61 6c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 tal of bytes in
17d3d 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67 cells on one pag
17d3e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63 e */. int iSpac
17d3f 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 e1 = 0;
17d40 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 /* First unu
17d41 73 65 64 20 62 79 74 65 20 6f 66 20 61 53 70 61 sed byte of aSpa
17d42 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 ce1[] */. int i
17d43 4f 76 66 6c 53 70 61 63 65 20 3d 20 30 3b 20 20 OvflSpace = 0;
17d44 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
17d45 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 unused byte of
17d46 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 2a 2f 0a aOvflSpace[] */.
17d47 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b int szScratch;
17d48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17d49 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74 63 * Size of scratc
17d4a 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74 h memory request
17d4b 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ed */. MemPage
17d4c 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20 *apOld[NB];
17d4d 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e /* pPage an
17d4e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c d up to two sibl
17d4f 69 6e 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 ings */. MemPag
17d50 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 e *apCopy[NB];
17d51 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74 /* Privat
17d52 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c e copies of apOl
17d53 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d d[] pages */. M
17d54 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42 emPage *apNew[NB
17d55 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70 +2]; /* p
17d56 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e Page and up to N
17d57 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72 B siblings after
17d58 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 balancing */.
17d59 75 38 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 u8 *pRight;
17d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17d5b 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 70 61 72 65 Location in pare
17d5c 6e 74 20 6f 66 20 72 69 67 68 74 2d 73 69 62 6c nt of right-sibl
17d5d 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ing pointer */.
17d5e 20 75 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d u8 *apDiv[NB-1]
17d5f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
17d60 20 44 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 Divider cells i
17d61 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69 n pParent */. i
17d62 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b nt cntNew[NB+2];
17d63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
17d64 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 ndex in aCell[]
17d65 6f 66 20 63 65 6c 6c 20 61 66 74 65 72 20 69 2d of cell after i-
17d66 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 th page */. int
17d67 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 szNew[NB+2];
17d68 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d /* Com
17d69 62 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 65 bined size of ce
17d6a 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74 lls place on i-t
17d6b 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a h page */. u8 *
17d6c 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 *apCell = 0;
17d6d 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 /* All
17d6e 63 65 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c 61 cells begin bala
17d6f 6e 63 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73 nced */. u16 *s
17d70 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 zCell;
17d71 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 /* Local
17d72 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c size of all cell
17d73 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f s in apCell[] */
17d74 0a 20 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20 . u8 *aSpace1;
17d75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17d76 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 6f 70 /* Space for cop
17d77 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73 20 ies of dividers
17d78 63 65 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 cells */. Pgno
17d79 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 pgno;
17d7a 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 /* Temp
17d7b 76 61 72 20 74 6f 20 73 74 6f 72 65 20 61 20 70 var to store a p
17d7c 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f age number in */
17d7d 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 72 65 6e .. pBt = pParen
17d7e 74 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 t->pBt;. assert
17d7f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
17d80 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
17d81 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
17d82 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
17d83 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 eable(pParent->p
17d84 44 62 50 61 67 65 29 20 29 3b 0a 0a 23 69 66 20 DbPage) );..#if
17d85 30 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 0. TRACE(("BALA
17d86 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 65 20 NCE: begin page
17d87 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e %d child of %d\n
17d88 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 ", pPage->pgno,
17d89 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b pParent->pgno));
17d8a 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74 .#endif.. /* At
17d8b 20 74 68 69 73 20 70 6f 69 6e 74 20 70 50 61 72 this point pPar
17d8c 65 6e 74 20 6d 61 79 20 68 61 76 65 20 61 74 20 ent may have at
17d8d 6d 6f 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f most one overflo
17d8e 77 20 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20 w cell. And if.
17d8f 20 2a 2a 20 74 68 69 73 20 6f 76 65 72 66 6c 6f ** this overflo
17d90 77 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 6e w cell is presen
17d91 74 2c 20 69 74 20 6d 75 73 74 20 62 65 20 74 68 t, it must be th
17d92 65 20 63 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a e cell with . *
17d93 2a 20 69 6e 64 65 78 20 69 50 61 72 65 6e 74 49 * index iParentI
17d94 64 78 2e 20 54 68 69 73 20 73 63 65 6e 61 72 69 dx. This scenari
17d95 6f 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 77 68 o comes about wh
17d96 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
17d97 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 . ** is called
17d98 28 69 6e 64 69 72 65 63 74 6c 79 29 20 66 72 6f (indirectly) fro
17d99 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 m sqlite3BtreeDe
17d9a 6c 65 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 lete().. */. a
17d9b 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e ssert( pParent->
17d9c 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 nOverflow==0 ||
17d9d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c pParent->nOverfl
17d9e 6f 77 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 ow==1 );. asser
17d9f 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 t( pParent->nOve
17da0 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 rflow==0 || pPar
17da1 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 ent->aOvfl[0].id
17da2 78 3d 3d 69 50 61 72 65 6e 74 49 64 78 20 29 3b x==iParentIdx );
17da3 0a 0a 20 20 69 66 28 20 21 61 4f 76 66 6c 53 70 .. if( !aOvflSp
17da4 61 63 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ace ){. retur
17da5 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
17da6 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 }.. /* Find t
17da7 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 he sibling pages
17da8 20 74 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73 to balance. Als
17da9 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 63 65 6c o locate the cel
17daa 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 0a 20 ls in pParent .
17dab 20 2a 2a 20 74 68 61 74 20 64 69 76 69 64 65 20 ** that divide
17dac 74 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e the siblings. An
17dad 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
17dae 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c to find NN sibl
17daf 69 6e 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69 ings on . ** ei
17db0 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 ther side of pPa
17db1 67 65 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67 ge. More sibling
17db2 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d s are taken from
17db3 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76 one side, howev
17db4 65 72 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 er, . ** if the
17db5 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61 re are fewer tha
17db6 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e n NN siblings on
17db7 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e the other side.
17db8 20 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a If pParent. **
17db9 20 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72 has NB or fewer
17dba 20 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61 children then a
17dbb 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 ll children of p
17dbc 50 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e Parent are taken
17dbd 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 . . **. ** Th
17dbe 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f is loop also dro
17dbf 70 73 20 74 68 65 20 64 69 76 69 64 65 72 20 63 ps the divider c
17dc0 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 70 61 ells from the pa
17dc1 72 65 6e 74 20 70 61 67 65 2e 20 54 68 69 73 0a rent page. This.
17dc2 20 20 2a 2a 20 77 61 79 2c 20 74 68 65 20 72 65 ** way, the re
17dc3 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 66 mainder of the f
17dc4 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 unction does not
17dc5 20 68 61 76 65 20 74 6f 20 64 65 61 6c 20 77 69 have to deal wi
17dc6 74 68 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72 th any. ** over
17dc7 66 6c 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74 68 flow cells in th
17dc8 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 73 e parent page, s
17dc9 69 6e 63 65 20 69 66 20 61 6e 79 20 65 78 69 73 ince if any exis
17dca 74 65 64 20 74 68 65 79 20 77 69 6c 6c 0a 20 20 ted they will.
17dcb 2a 2a 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 ** have already
17dcc 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20 been removed..
17dcd 2a 2f 0a 20 20 69 20 3d 20 70 50 61 72 65 6e 74 */. i = pParent
17dce 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50 ->nOverflow + pP
17dcf 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 arent->nCell;.
17dd0 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e if( i<2 ){. n
17dd1 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 6e 4f xDiv = 0;. nO
17dd2 6c 64 20 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c 73 ld = i+1;. }els
17dd3 65 7b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 33 3b e{. nOld = 3;
17dd4 0a 20 20 20 20 69 66 28 20 69 50 61 72 65 6e 74 . if( iParent
17dd5 49 64 78 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 Idx==0 ){
17dd6 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 .
17dd7 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 nxDiv = 0;.
17dd8 7d 65 6c 73 65 20 69 66 28 20 69 50 61 72 65 6e }else if( iParen
17dd9 74 49 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20 tIdx==i ){.
17dda 20 6e 78 44 69 76 20 3d 20 69 2d 32 3b 0a 20 20 nxDiv = i-2;.
17ddb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
17ddc 78 44 69 76 20 3d 20 69 50 61 72 65 6e 74 49 64 xDiv = iParentId
17ddd 78 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 x-1;. }. i
17dde 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = 2;. }. if(
17ddf 28 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 (i+nxDiv-pParent
17de0 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50 ->nOverflow)==pP
17de1 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a arent->nCell ){.
17de2 20 20 20 20 70 52 69 67 68 74 20 3d 20 26 70 50 pRight = &pP
17de3 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 arent->aData[pPa
17de4 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b rent->hdrOffset+
17de5 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 8];. }else{.
17de6 20 70 52 69 67 68 74 20 3d 20 66 69 6e 64 43 65 pRight = findCe
17de7 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 ll(pParent, i+nx
17de8 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 Div-pParent->nOv
17de9 65 72 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70 erflow);. }. p
17dea 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 gno = get4byte(p
17deb 52 69 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 28 Right);. while(
17dec 20 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 67 1 ){. rc = g
17ded 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 etAndInitPage(pB
17dee 74 2c 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b t, pgno, &apOld[
17def 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 i]);. if( rc
17df0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 ){. memset(
17df1 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a apOld, 0, (i+1)*
17df2 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29 sizeof(MemPage*)
17df3 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61 );. goto ba
17df4 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
17df5 20 20 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c }. nMaxCel
17df6 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d ls += 1+apOld[i]
17df7 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d ->nCell+apOld[i]
17df8 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 ->nOverflow;.
17df9 20 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20 if( (i--)==0 )
17dfa 62 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20 break;.. if(
17dfb 69 2b 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74 i+nxDiv==pParent
17dfc 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 26 ->aOvfl[0].idx &
17dfd 26 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 & pParent->nOver
17dfe 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 61 70 flow ){. ap
17dff 44 69 76 5b 69 5d 20 3d 20 70 50 61 72 65 6e 74 Div[i] = pParent
17e00 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c ->aOvfl[0].pCell
17e01 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 ;. pgno = g
17e02 65 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d et4byte(apDiv[i]
17e03 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 );. szNew[i
17e04 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 ] = cellSizePtr(
17e05 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 pParent, apDiv[i
17e06 5d 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e ]);. pParen
17e07 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 t->nOverflow = 0
17e08 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
17e09 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69 apDiv[i] = fi
17e0a 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 ndCell(pParent,
17e0b 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d i+nxDiv-pParent-
17e0c 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20 >nOverflow);.
17e0d 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 pgno = get4by
17e0e 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 te(apDiv[i]);.
17e0f 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 szNew[i] = c
17e10 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 ellSizePtr(pPare
17e11 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a nt, apDiv[i]);..
17e12 20 20 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68 /* Drop th
17e13 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 e cell from the
17e14 70 61 72 65 6e 74 20 70 61 67 65 2e 20 61 70 44 parent page. apD
17e15 69 76 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e iv[i] still poin
17e16 74 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 ts to. ** t
17e17 68 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 74 he cell within t
17e18 68 65 20 70 61 72 65 6e 74 2c 20 65 76 65 6e 20 he parent, even
17e19 74 68 6f 75 67 68 20 69 74 20 68 61 73 20 62 65 though it has be
17e1a 65 6e 20 64 72 6f 70 70 65 64 2e 0a 20 20 20 20 en dropped..
17e1b 20 20 2a 2a 20 54 68 69 73 20 69 73 20 73 61 66 ** This is saf
17e1c 65 20 62 65 63 61 75 73 65 20 64 72 6f 70 70 69 e because droppi
17e1d 6e 67 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f ng a cell only o
17e1e 76 65 72 77 72 69 74 65 73 20 74 68 65 20 66 69 verwrites the fi
17e1f 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 rst. ** fou
17e20 72 20 62 79 74 65 73 20 6f 66 20 69 74 2c 20 61 r bytes of it, a
17e21 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e nd this function
17e22 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
17e23 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a he first. *
17e24 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 * four bytes of
17e25 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c the divider cell
17e26 2e 20 53 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 . So the pointer
17e27 20 69 73 20 73 61 66 65 20 74 6f 20 75 73 65 0a is safe to use.
17e28 20 20 20 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f ** later o
17e29 6e 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 n. . **.
17e2a 20 20 20 20 2a 2a 20 55 6e 6c 65 73 73 20 53 51 ** Unless SQ
17e2b 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 Lite is compiled
17e2c 20 69 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74 in secure-delet
17e2d 65 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 e mode. In this
17e2e 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74 case,. ** t
17e2f 68 65 20 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f he dropCell() ro
17e30 75 74 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77 utine will overw
17e31 72 69 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 rite the entire
17e32 63 65 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65 73 cell with zeroes
17e33 2e 0a 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 68 .. ** In th
17e34 69 73 20 63 61 73 65 2c 20 74 65 6d 70 6f 72 61 is case, tempora
17e35 72 69 6c 79 20 63 6f 70 79 20 74 68 65 20 63 65 rily copy the ce
17e36 6c 6c 20 69 6e 74 6f 20 74 68 65 20 61 4f 76 66 ll into the aOvf
17e37 6c 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a lSpace[]. *
17e38 2a 20 62 75 66 66 65 72 2e 20 49 74 20 77 69 6c * buffer. It wil
17e39 6c 20 62 65 20 63 6f 70 69 65 64 20 6f 75 74 20 l be copied out
17e3a 61 67 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73 again as soon as
17e3b 20 74 68 65 20 61 53 70 61 63 65 5b 5d 20 62 75 the aSpace[] bu
17e3c 66 66 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73 ffer. ** is
17e3d 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a allocated. */.
17e3e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 #ifdef SQLITE_SE
17e3f 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 CURE_DELETE.
17e40 20 20 6d 65 6d 63 70 79 28 26 61 4f 76 66 6c 53 memcpy(&aOvflS
17e41 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 pace[apDiv[i]-pP
17e42 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 61 arent->aData], a
17e43 70 44 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b 69 pDiv[i], szNew[i
17e44 5d 29 3b 0a 20 20 20 20 20 20 61 70 44 69 76 5b ]);. apDiv[
17e45 69 5d 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 i] = &aOvflSpace
17e46 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e [apDiv[i]-pParen
17e47 74 2d 3e 61 44 61 74 61 5d 3b 0a 23 65 6e 64 69 t->aData];.#endi
17e48 66 0a 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c f. dropCell
17e49 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 (pParent, i+nxDi
17e4a 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 v-pParent->nOver
17e4b 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c 20 flow, szNew[i],
17e4c 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a &rc);. }. }.
17e4d 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 . /* Make nMaxC
17e4e 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 ells a multiple
17e4f 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f of 4 in order to
17e50 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 preserve 8-byte
17e51 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 . ** alignment
17e52 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d */. nMaxCells =
17e53 20 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 (nMaxCells + 3)
17e54 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 &~3;.. /*. **
17e55 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 Allocate space f
17e56 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 or memory struct
17e57 75 72 65 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20 ures. */. k =
17e58 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 pBt->pageSize +
17e59 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 ROUND8(sizeof(Me
17e5a 6d 50 61 67 65 29 29 3b 0a 20 20 73 7a 53 63 72 mPage));. szScr
17e5b 61 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d atch =. nM
17e5c 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 axCells*sizeof(u
17e5d 38 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 8*)
17e5e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 /* apC
17e5f 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d ell */. + nM
17e60 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 axCells*sizeof(u
17e61 31 36 29 20 20 20 20 20 20 20 20 20 20 20 20 20 16)
17e62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 /* szC
17e63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 ell */. + pB
17e64 74 2d 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 t->pageSize
17e65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e66 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 /* aSp
17e67 61 63 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 6b ace1 */. + k
17e68 2a 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 *nOld;
17e69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e6a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
17e6b 67 65 20 63 6f 70 69 65 73 20 28 61 70 43 6f 70 ge copies (apCop
17e6c 79 29 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d y) */. apCell =
17e6d 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d sqlite3ScratchM
17e6e 61 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 alloc( szScratch
17e6f 20 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c ); . if( apCel
17e70 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d l==0 ){. rc =
17e71 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
17e72 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f goto balance_
17e73 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 cleanup;. }. s
17e74 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 zCell = (u16*)&a
17e75 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d pCell[nMaxCells]
17e76 3b 0a 20 20 61 53 70 61 63 65 31 20 3d 20 28 75 ;. aSpace1 = (u
17e77 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43 8*)&szCell[nMaxC
17e78 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28 ells];. assert(
17e79 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
17e7a 4e 4d 45 4e 54 28 61 53 70 61 63 65 31 29 20 29 NMENT(aSpace1) )
17e7b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f 61 ;.. /*. ** Loa
17e7c 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c d pointers to al
17e7d 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 69 62 6c 69 l cells on sibli
17e7e 6e 67 20 70 61 67 65 73 20 61 6e 64 20 74 68 65 ng pages and the
17e7f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 divider cells.
17e80 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 6f 63 ** into the loc
17e81 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 61 72 72 61 al apCell[] arra
17e82 79 2e 20 20 4d 61 6b 65 20 63 6f 70 69 65 73 20 y. Make copies
17e83 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63 of the divider c
17e84 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 73 ells. ** into s
17e85 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 pace obtained fr
17e86 6f 6d 20 61 53 70 61 63 65 31 5b 5d 20 61 6e 64 om aSpace1[] and
17e87 20 72 65 6d 6f 76 65 20 74 68 65 20 74 68 65 20 remove the the
17e88 64 69 76 69 64 65 72 20 43 65 6c 6c 73 0a 20 20 divider Cells.
17e89 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 2e ** from pParent.
17e8a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 . **. ** If th
17e8b 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6f e siblings are o
17e8c 6e 20 6c 65 61 66 20 70 61 67 65 73 2c 20 74 68 n leaf pages, th
17e8d 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70 6f 69 en the child poi
17e8e 6e 74 65 72 73 20 6f 66 20 74 68 65 0a 20 20 2a nters of the. *
17e8f 2a 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 * divider cells
17e90 61 72 65 20 73 74 72 69 70 70 65 64 20 66 72 6f are stripped fro
17e91 6d 20 74 68 65 20 63 65 6c 6c 73 20 62 65 66 6f m the cells befo
17e92 72 65 20 74 68 65 79 20 61 72 65 20 63 6f 70 69 re they are copi
17e93 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 53 70 ed. ** into aSp
17e94 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74 68 69 73 ace1[]. In this
17e95 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 way, all cells
17e96 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 72 65 20 in apCell[] are
17e97 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 63 68 69 without. ** chi
17e98 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 49 66 ld pointers. If
17e99 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f siblings are no
17e9a 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 61 t leaves, then a
17e9b 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a 20 ll cell in. **
17e9c 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65 apCell[] include
17e9d 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e child pointers.
17e9e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61 6c Either way, al
17e9f 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c l cells in apCel
17ea0 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69 l[]. ** are ali
17ea1 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c 65 ke.. **. ** le
17ea2 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34 afCorrection: 4
17ea3 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c if pPage is a l
17ea4 65 61 66 2e 20 20 30 20 69 66 20 70 50 61 67 65 eaf. 0 if pPage
17ea5 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0a is not a leaf..
17ea6 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 61 66 44 ** leafD
17ea7 61 74 61 3a 20 20 31 20 69 66 20 70 50 61 67 65 ata: 1 if pPage
17ea8 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61 20 holds key+data
17ea9 61 6e 64 20 70 50 61 72 65 6e 74 20 68 6f 6c 64 and pParent hold
17eaa 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 2a s only keys.. *
17eab 2f 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69 /. leafCorrecti
17eac 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 6c on = apOld[0]->l
17ead 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 eaf*4;. leafDat
17eae 61 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 61 a = apOld[0]->ha
17eaf 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30 sData;. for(i=0
17eb0 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a ; i<nOld; i++){.
17eb1 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 3b 0a 20 int limit;.
17eb2 20 20 20 0a 20 20 20 20 2f 2a 20 42 65 66 6f 72 . /* Befor
17eb3 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 e doing anything
17eb4 20 65 6c 73 65 2c 20 74 61 6b 65 20 61 20 63 6f else, take a co
17eb5 70 79 20 6f 66 20 74 68 65 20 69 27 74 68 20 6f py of the i'th o
17eb6 72 69 67 69 6e 61 6c 20 73 69 62 6c 69 6e 67 0a riginal sibling.
17eb7 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 74 20 ** The rest
17eb8 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e of this function
17eb9 20 77 69 6c 6c 20 75 73 65 20 64 61 74 61 20 66 will use data f
17eba 72 6f 6d 20 74 68 65 20 63 6f 70 69 65 73 20 72 rom the copies r
17ebb 61 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 ather. ** tha
17ebc 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 t the original p
17ebd 61 67 65 73 20 73 69 6e 63 65 20 74 68 65 20 6f ages since the o
17ebe 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 77 69 riginal pages wi
17ebf 6c 6c 20 62 65 20 69 6e 20 74 68 65 0a 20 20 20 ll be in the.
17ec0 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 62 ** process of b
17ec1 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e eing overwritten
17ec2 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 . */. MemPag
17ec3 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 e *pOld = apCopy
17ec4 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 [i] = (MemPage*)
17ec5 26 61 53 70 61 63 65 31 5b 70 42 74 2d 3e 70 61 &aSpace1[pBt->pa
17ec6 67 65 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a 20 geSize + k*i];.
17ec7 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20 memcpy(pOld,
17ec8 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66 apOld[i], sizeof
17ec9 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 20 20 (MemPage));.
17eca 70 4f 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28 76 pOld->aData = (v
17ecb 6f 69 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a 20 oid*)&pOld[1];.
17ecc 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e memcpy(pOld->
17ecd 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d aData, apOld[i]-
17ece 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67 >aData, pBt->pag
17ecf 65 53 69 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 6d eSize);.. lim
17ed0 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c it = pOld->nCell
17ed1 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 +pOld->nOverflow
17ed2 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a ;. for(j=0; j
17ed3 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20 <limit; j++){.
17ed4 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c assert( nCel
17ed5 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 l<nMaxCells );.
17ed6 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c apCell[nCel
17ed7 6c 5d 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f l] = findOverflo
17ed8 77 43 65 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b 0a wCell(pOld, j);.
17ed9 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 szCell[nCe
17eda 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 ll] = cellSizePt
17edb 72 28 70 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b 6e r(pOld, apCell[n
17edc 43 65 6c 6c 5d 29 3b 0a 20 20 20 20 20 20 6e 43 Cell]);. nC
17edd 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 ell++;. }.
17ede 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 26 if( i<nOld-1 &&
17edf 20 21 6c 65 61 66 44 61 74 61 29 7b 0a 20 20 20 !leafData){.
17ee0 20 20 20 75 31 36 20 73 7a 20 3d 20 28 75 31 36 u16 sz = (u16
17ee1 29 73 7a 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 )szNew[i];.
17ee2 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 u8 *pTemp;.
17ee3 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c assert( nCell<
17ee4 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 nMaxCells );.
17ee5 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d szCell[nCell]
17ee6 20 3d 20 73 7a 3b 0a 20 20 20 20 20 20 70 54 65 = sz;. pTe
17ee7 6d 70 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53 mp = &aSpace1[iS
17ee8 70 61 63 65 31 5d 3b 0a 20 20 20 20 20 20 69 53 pace1];. iS
17ee9 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 pace1 += sz;.
17eea 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 assert( sz<=p
17eeb 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 Bt->pageSize/4 )
17eec 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
17eed 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70 61 iSpace1<=pBt->pa
17eee 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 geSize );.
17eef 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c 20 61 70 memcpy(pTemp, ap
17ef0 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20 Div[i], sz);.
17ef1 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d apCell[nCell]
17ef2 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72 = pTemp+leafCor
17ef3 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61 rection;. a
17ef4 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 ssert( leafCorre
17ef5 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66 ction==0 || leaf
17ef6 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b Correction==4 );
17ef7 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 . szCell[nC
17ef8 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43 ell] = szCell[nC
17ef9 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65 ell] - leafCorre
17efa 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 ction;. if(
17efb 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a !pOld->leaf ){.
17efc 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
17efd 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d leafCorrection==
17efe 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 0 );. ass
17eff 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66 ert( pOld->hdrOf
17f00 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 fset==0 );.
17f01 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 /* The right
17f02 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 pointer of the c
17f03 68 69 6c 64 20 70 61 67 65 20 70 4f 6c 64 20 62 hild page pOld b
17f04 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a ecomes the left.
17f05 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 ** point
17f06 65 72 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 er of the divide
17f07 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 r cell */.
17f08 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b memcpy(apCell[
17f09 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61 nCell], &pOld->a
17f0a 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20 Data[8], 4);.
17f0b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17f0c 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f assert( leafCo
17f0d 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 rrection==4 );.
17f0e 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c if( szCel
17f0f 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20 l[nCell]<4 ){.
17f10 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
17f11 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c t allow any cell
17f12 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34 s smaller than 4
17f13 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20 bytes. */.
17f14 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c szCell[nCel
17f15 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 l] = 4;.
17f16 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
17f17 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 nCell++;. }.
17f18 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 }.. /*. ** Fi
17f19 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d gure out the num
17f1a 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65 ber of pages nee
17f1b 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 ded to hold all
17f1c 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a nCell cells.. *
17f1d 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d * Store this num
17f1e 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73 ber in "k". Als
17f1f 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b o compute szNew[
17f20 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 74 ] which is the t
17f21 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f otal. ** size o
17f22 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74 f all cells on t
17f23 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64 he i-th page and
17f24 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20 cntNew[] which
17f25 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a is the index. *
17f26 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66 * in apCell[] of
17f27 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64 the cell that d
17f28 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66 72 ivides page i fr
17f29 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20 om page i+1. .
17f2a 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68 ** cntNew[k] sh
17f2b 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c ould equal nCell
17f2c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75 .. **. ** Valu
17f2d 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 es computed by t
17f2e 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a his block:. **.
17f2f 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b ** k
17f30 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 : The total numb
17f31 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61 er of sibling pa
17f32 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65 ges. ** szNe
17f33 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65 w[i]: Spaced use
17f34 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69 d on the i-th si
17f35 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a bling page.. **
17f36 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e cntNew[i]: In
17f37 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 dex in apCell[]
17f38 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72 and szCell[] for
17f39 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20 the first cell
17f3a 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 to. **
17f3b 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20 6f the right o
17f3c 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 f the i-th sibli
17f3d 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73 ng page.. ** us
17f3e 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65 ableSpace: Numbe
17f3f 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 r of bytes of sp
17f40 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e ace available on
17f41 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20 each sibling..
17f42 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62 ** . */. usab
17f43 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75 leSpace = pBt->u
17f44 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b sableSize - 12 +
17f45 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b leafCorrection;
17f46 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d . for(subtotal=
17f47 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 k=i=0; i<nCell;
17f48 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 i++){. assert
17f49 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ( i<nMaxCells );
17f4a 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d . subtotal +=
17f4b 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a szCell[i] + 2;.
17f4c 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c if( subtotal
17f4d 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20 29 > usableSpace )
17f4e 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d {. szNew[k]
17f4f 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a = subtotal - sz
17f50 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63 Cell[i];. c
17f51 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20 ntNew[k] = i;.
17f52 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 61 if( leafData
17f53 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20 ){ i--; }.
17f54 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20 subtotal = 0;.
17f55 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20 k++;.
17f56 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63 if( k>NB+1 ){ rc
17f57 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
17f58 54 3b 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f T; goto balance_
17f59 63 6c 65 61 6e 75 70 3b 20 7d 0a 20 20 20 20 7d cleanup; }. }
17f5a 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20 . }. szNew[k]
17f5b 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e = subtotal;. cn
17f5c 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b tNew[k] = nCell;
17f5d 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 . k++;.. /*.
17f5e 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63 ** The packing c
17f5f 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 70 omputed by the p
17f60 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73 revious block is
17f61 20 62 69 61 73 65 64 20 74 6f 77 61 72 64 20 74 biased toward t
17f62 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a he siblings. **
17f63 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64 on the left sid
17f64 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73 69 62 e. The left sib
17f65 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73 lings are always
17f66 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68 nearly full, wh
17f67 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67 ile the. ** rig
17f68 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20 ht-most sibling
17f69 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20 might be nearly
17f6a 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f empty. This blo
17f6b 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d ck of code attem
17f6c 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75 pts. ** to adju
17f6d 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f st the packing o
17f6e 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65 f siblings to ge
17f6f 74 20 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e t a better balan
17f70 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 ce.. **. ** Th
17f71 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 is adjustment is
17f72 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70 more than an op
17f73 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 timization. The
17f74 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d packing above m
17f75 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20 ight. ** be so
17f76 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61 out of balance a
17f77 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e s to be illegal.
17f78 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 For example, t
17f79 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 he right-most.
17f7a 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 ** sibling might
17f7b 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 be completely e
17f7c 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75 mpty. This adju
17f7d 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70 stment is not op
17f7e 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66 tional.. */. f
17f7f 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69 or(i=k-1; i>0; i
17f80 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52 --){. int szR
17f81 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b ight = szNew[i];
17f82 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 /* Size of sib
17f83 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68 ling on the righ
17f84 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c t */. int szL
17f85 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d eft = szNew[i-1]
17f86 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62 ; /* Size of sib
17f87 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74 ling on the left
17f88 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20 */. int r;
17f89 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
17f8a 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f ndex of right-mo
17f8b 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20 st cell in left
17f8c 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69 sibling */. i
17f8d 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20 20 20 nt d;
17f8e 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 /* Index of f
17f8f 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65 irst cell to the
17f90 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74 20 73 left of right s
17f91 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72 ibling */.. r
17f92 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d = cntNew[i-1] -
17f93 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20 1;. d = r +
17f94 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 1 - leafData;.
17f95 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78 assert( d<nMax
17f96 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73 Cells );. ass
17f97 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73 ert( r<nMaxCells
17f98 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 );. while( s
17f99 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52 zRight==0 || szR
17f9a 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32 ight+szCell[d]+2
17f9b 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c <=szLeft-(szCell
17f9c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 [r]+2) ){.
17f9d 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c szRight += szCel
17f9e 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 l[d] + 2;.
17f9f 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c szLeft -= szCell
17fa0 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63 [r] + 2;. c
17fa1 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 ntNew[i-1]--;.
17fa2 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 r = cntNew[i
17fa3 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64 -1] - 1;. d
17fa4 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 = r + 1 - leafD
17fa5 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ata;. }. s
17fa6 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68 zNew[i] = szRigh
17fa7 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31 t;. szNew[i-1
17fa8 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a ] = szLeft;. }.
17fa9 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20 . /* Either we
17faa 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 found one or mor
17fab 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b e cells (cntnew[
17fac 30 5d 29 3e 30 29 20 6f 72 20 70 50 61 67 65 20 0])>0) or pPage
17fad 69 73 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61 is. ** a virtua
17fae 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20 l root page. A
17faf 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 virtual root pag
17fb0 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72 65 e is when the re
17fb1 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67 al root. ** pag
17fb2 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64 20 e is page 1 and
17fb3 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20 we are the only
17fb4 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61 child of that pa
17fb5 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ge.. */. asser
17fb6 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c t( cntNew[0]>0 |
17fb7 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f | (pParent->pgno
17fb8 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e ==1 && pParent->
17fb9 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20 nCell==0) );..
17fba 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a TRACE(("BALANCE:
17fbb 20 6f 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20 old: %d %d %d
17fbc 22 2c 0a 20 20 20 20 61 70 4f 6c 64 5b 30 5d 2d ",. apOld[0]-
17fbd 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c 64 >pgno, . nOld
17fbe 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d 3e >=2 ? apOld[1]->
17fbf 70 67 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 6e 4f pgno : 0,. nO
17fc0 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 5d ld>=3 ? apOld[2]
17fc1 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 29 3b ->pgno : 0. ));
17fc2 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f .. /*. ** Allo
17fc3 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73 cate k new pages
17fc4 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67 . Reuse old pag
17fc5 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c es where possibl
17fc6 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 70 e.. */. if( ap
17fc7 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 Old[0]->pgno<=1
17fc8 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
17fc9 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20 TE_CORRUPT;.
17fca 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 goto balance_cle
17fcb 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 61 67 65 anup;. }. page
17fcc 46 6c 61 67 73 20 3d 20 61 70 4f 6c 64 5b 30 5d Flags = apOld[0]
17fcd 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a 20 20 66 6f ->aData[0];. fo
17fce 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29 r(i=0; i<k; i++)
17fcf 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 {. MemPage *p
17fd0 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 69 3c 6e New;. if( i<n
17fd1 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 Old ){. pNe
17fd2 77 20 3d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 w = apNew[i] = a
17fd3 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 61 pOld[i];. a
17fd4 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 pOld[i] = 0;.
17fd5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
17fd6 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 2d 3e agerWrite(pNew->
17fd7 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
17fd8 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69 66 nNew++;. if
17fd9 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 ( rc ) goto bala
17fda 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
17fdb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
17fdc 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 20 sert( i>0 );.
17fdd 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 rc = allocate
17fde 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 BtreePage(pBt, &
17fdf 70 4e 65 77 2c 20 26 70 67 6e 6f 2c 20 70 67 6e pNew, &pgno, pgn
17fe0 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 o, 0);. if(
17fe1 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e rc ) goto balan
17fe2 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 ce_cleanup;.
17fe3 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65 apNew[i] = pNe
17fe4 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b w;. nNew++;
17fe5 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 .. /* Set t
17fe6 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
17fe7 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77 ntry for the new
17fe8 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a sibling page. *
17fe9 2f 0a 20 20 20 20 20 20 69 66 28 20 49 53 41 55 /. if( ISAU
17fea 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
17feb 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 ptrmapPut(pB
17fec 74 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50 t, pNew->pgno, P
17fed 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 TRMAP_BTREE, pPa
17fee 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 rent->pgno, &rc)
17fef 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
17ff0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17ff1 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 goto ba
17ff2 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
17ff3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
17ff4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
17ff5 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61 Free any old pa
17ff6 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f ges that were no
17ff7 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 20 t reused as new
17ff8 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68 pages.. */. wh
17ff9 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 ile( i<nOld ){.
17ffa 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4f 6c freePage(apOl
17ffb 64 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 d[i], &rc);.
17ffc 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 if( rc ) goto ba
17ffd 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 lance_cleanup;.
17ffe 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 releasePage(a
17fff 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70 pOld[i]);. ap
18000 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 Old[i] = 0;.
18001 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 i++;. }.. /*.
18002 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20 ** Put the new
18003 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69 pages in accendi
18004 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20 ng order. This
18005 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65 helps to. ** ke
18006 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 ep entries in th
18007 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f e disk file in o
18008 72 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 73 rder so that a s
18009 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 can. ** of the
1800a 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61 table is a linea
1800b 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 r scan through t
1800c 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20 he file. That.
1800d 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70 ** in turn help
1800e 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 s the operating
1800f 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65 system to delive
18010 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f r pages. ** fro
18011 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20 m the disk more
18012 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20 rapidly.. **.
18013 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73 ** An O(n^2) ins
18014 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f ertion sort algo
18015 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62 rithm is used, b
18016 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20 ut since. ** n
18017 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68 is never more th
18018 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63 an NB (a small c
18019 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73 onstant), that s
1801a 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62 hould. ** not b
1801b 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a e a problem.. *
1801c 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d *. ** When NB==
1801d 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69 3, this one opti
1801e 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74 mization makes t
1801f 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a he database. **
18020 20 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 65 about 25% faste
18021 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65 r for large inse
18022 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74 rtions and delet
18023 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 ions.. */. for
18024 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b (i=0; i<k-1; i++
18025 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20 ){. int minV
18026 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f = apNew[i]->pgno
18027 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d ;. int minI =
18028 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b i;. for(j=i+
18029 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 1; j<k; j++){.
1802a 20 20 20 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d if( apNew[j]
1802b 2d 3e 70 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64 ->pgno<(unsigned
1802c 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20 )minV ){.
1802d 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20 minI = j;.
1802e 20 20 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b minV = apNew[
1802f 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20 j]->pgno;.
18030 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
18031 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 20 20 20 20 minI>i ){.
18032 69 6e 74 20 74 3b 0a 20 20 20 20 20 20 4d 65 6d int t;. Mem
18033 50 61 67 65 20 2a 70 54 3b 0a 20 20 20 20 20 20 Page *pT;.
18034 74 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 t = apNew[i]->pg
18035 6e 6f 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61 no;. pT = a
18036 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61 pNew[i];. a
18037 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b pNew[i] = apNew[
18038 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e minI];. apN
18039 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20 ew[minI] = pT;.
1803a 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45 }. }. TRACE
1803b 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29 20 25 (("new: %d(%d) %
1803c 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28 d(%d) %d(%d) %d(
1803d 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 %d) %d(%d)\n",.
1803e 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e apNew[0]->pgn
1803f 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20 o, szNew[0],.
18040 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77 nNew>=2 ? apNew
18041 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e [1]->pgno : 0, n
18042 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31 New>=2 ? szNew[1
18043 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e ] : 0,. nNew>
18044 3d 33 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70 =3 ? apNew[2]->p
18045 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 gno : 0, nNew>=3
18046 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c ? szNew[2] : 0,
18047 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61 . nNew>=4 ? a
18048 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20 pNew[3]->pgno :
18049 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e 0, nNew>=4 ? szN
1804a 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e ew[3] : 0,. n
1804b 4e 65 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34 New>=5 ? apNew[4
1804c 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 ]->pgno : 0, nNe
1804d 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20 w>=5 ? szNew[4]
1804e 3a 20 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 : 0));.. assert
1804f 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
18050 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e writeable(pParen
18051 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 t->pDbPage) );.
18052 20 70 75 74 34 62 79 74 65 28 70 52 69 67 68 74 put4byte(pRight
18053 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d , apNew[nNew-1]-
18054 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 >pgno);.. /*.
18055 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69 ** Evenly distri
18056 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 6e bute the data in
18057 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73 apCell[] across
18058 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a the new pages..
18059 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69 ** Insert divi
1805a 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70 der cells into p
1805b 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73 Parent as necess
1805c 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20 ary.. */. j =
1805d 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 0;. for(i=0; i<
1805e 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nNew; i++){.
1805f 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20 /* Assemble the
18060 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 new sibling page
18061 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 . */. MemPage
18062 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 *pNew = apNew[i
18063 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a ];. assert( j
18064 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 <nMaxCells );.
18065 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c zeroPage(pNew,
18066 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20 pageFlags);.
18067 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e assemblePage(pN
18068 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c ew, cntNew[i]-j,
18069 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a &apCell[j], &sz
1806a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73 Cell[j]);. as
1806b 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c sert( pNew->nCel
1806c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20 l>0 || (nNew==1
1806d 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29 && cntNew[0]==0)
1806e 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1806f 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d pNew->nOverflow=
18070 3d 30 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63 =0 );.. j = c
18071 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f ntNew[i];.. /
18072 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 * If the sibling
18073 20 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 page assembled
18074 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 68 above was not th
18075 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 e right-most sib
18076 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73 ling,. ** ins
18077 65 72 74 20 61 20 64 69 76 69 64 65 72 20 63 65 ert a divider ce
18078 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 ll into the pare
18079 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a nt page.. */.
1807a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4e assert( i<nN
1807b 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c ew-1 || j==nCell
1807c 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43 );. if( j<nC
1807d 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 ell ){. u8
1807e 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 *pCell;. u8
1807f 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 *pTemp;. i
18080 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 nt sz;.. as
18081 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c sert( j<nMaxCell
18082 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c s );. pCell
18083 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 = apCell[j];.
18084 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b sz = szCell[
18085 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 j] + leafCorrect
18086 69 6f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 ion;. pTemp
18087 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 69 = &aOvflSpace[i
18088 4f 76 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20 OvflSpace];.
18089 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61 if( !pNew->lea
1808a 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d f ){. mem
1808b 63 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61 cpy(&pNew->aData
1808c 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a [8], pCell, 4);.
1808d 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1808e 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20 leafData ){.
1808f 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 /* If the tr
18090 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74 ee is a leaf-dat
18091 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20 a tree, and the
18092 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61 siblings are lea
18093 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a ves, . **
18094 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e then there is n
18095 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 o divider cell i
18096 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74 n apCell[]. Inst
18097 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 72 ead, the divider
18098 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c . ** cel
18099 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 l consists of th
1809a 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f e integer key fo
1809b 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 r the right-most
1809c 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20 cell of .
1809d 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67 ** the sibling
1809e 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20 -page assembled
1809f 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20 above only..
180a0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43 */. C
180a1 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
180a2 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20 j--;.
180a3 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c btreeParseCel
180a4 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c lPtr(pNew, apCel
180a5 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 l[j], &info);.
180a6 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54 pCell = pT
180a7 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 73 7a 20 emp;. sz
180a8 3d 20 34 20 2b 20 70 75 74 56 61 72 69 6e 74 28 = 4 + putVarint(
180a9 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e &pCell[4], info.
180aa 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 70 nKey);. p
180ab 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 Temp = 0;.
180ac 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
180ad 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20 Cell -= 4;.
180ae 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61 /* Obscure ca
180af 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d se for non-leaf-
180b0 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 74 data trees: If t
180b1 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c he cell at pCell
180b2 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 was. **
180b3 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65 previously store
180b4 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65 d on a leaf node
180b5 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74 , and its report
180b6 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20 ed size was 4.
180b7 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20 ** bytes,
180b8 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75 then it may actu
180b9 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20 ally be smaller
180ba 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20 than this .
180bb 20 20 20 2a 2a 20 28 73 65 65 20 62 74 72 65 65 ** (see btree
180bc 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 ParseCellPtr(),
180bd 34 20 62 79 74 65 73 20 69 73 20 74 68 65 20 6d 4 bytes is the m
180be 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 inimum size of.
180bf 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 ** any ce
180c0 6c 6c 29 2e 20 42 75 74 20 69 74 20 69 73 20 69 ll). But it is i
180c1 6d 70 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 73 mportant to pass
180c2 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a the correct siz
180c3 65 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a e to . **
180c4 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 insertCell(), s
180c5 6f 20 72 65 70 61 72 73 65 20 74 68 65 20 63 65 o reparse the ce
180c6 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 ll now..
180c7 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f **. ** No
180c8 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6e te that this can
180c9 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e never happen in
180ca 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20 an SQLite data
180cb 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 file, as all.
180cc 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 ** cells ar
180cd 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 e at least 4 byt
180ce 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 es. It only happ
180cf 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20 75 ens in b-trees u
180d0 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 sed. ** t
180d1 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e 20 28 o evaluate "IN (
180d2 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 SELECT ...)" and
180d3 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 similar clauses
180d4 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
180d5 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b if( szCell[
180d6 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 j]==4 ){.
180d7 20 20 20 61 73 73 65 72 74 28 6c 65 61 66 43 6f assert(leafCo
180d8 72 72 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 rrection==4);.
180d9 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c sz = cel
180da 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 lSizePtr(pParent
180db 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 , pCell);.
180dc 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
180dd 20 20 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20 iOvflSpace +=
180de 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 sz;. assert
180df 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53 ( sz<=pBt->pageS
180e0 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 61 ize/4 );. a
180e1 73 73 65 72 74 28 20 69 4f 76 66 6c 53 70 61 63 ssert( iOvflSpac
180e2 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e<=pBt->pageSize
180e3 20 29 3b 0a 20 20 20 20 20 20 69 6e 73 65 72 74 );. insert
180e4 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78 Cell(pParent, nx
180e5 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c 20 Div, pCell, sz,
180e6 70 54 65 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 6e pTemp, pNew->pgn
180e7 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 69 o, &rc);. i
180e8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
180e9 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f ) goto balance_
180ea 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 cleanup;. a
180eb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
180ec 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
180ed 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 Parent->pDbPage)
180ee 20 29 3b 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a );.. j++;.
180ef 20 20 20 20 20 20 6e 78 44 69 76 2b 2b 3b 0a 20 nxDiv++;.
180f0 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
180f1 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 t( j==nCell );.
180f2 20 61 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20 assert( nOld>0
180f3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65 );. assert( nNe
180f4 77 3e 30 20 29 3b 0a 20 20 69 66 28 20 28 70 61 w>0 );. if( (pa
180f5 67 65 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45 geFlags & PTF_LE
180f6 41 46 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38 AF)==0 ){. u8
180f7 20 2a 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f *zChild = &apCo
180f8 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74 py[nOld-1]->aDat
180f9 61 5b 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 a[8];. memcpy
180fa 28 26 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d (&apNew[nNew-1]-
180fb 3e 61 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c >aData[8], zChil
180fc 64 2c 20 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 d, 4);. }.. if
180fd 28 20 69 73 52 6f 6f 74 20 26 26 20 70 50 61 72 ( isRoot && pPar
180fe 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 ent->nCell==0 &&
180ff 20 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 pParent->hdrOff
18100 73 65 74 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e 6e set<=apNew[0]->n
18101 46 72 65 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 Free ){. /* T
18102 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
18103 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 77 20 63 the b-tree now c
18104 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 ontains no cells
18105 2e 20 54 68 65 20 6f 6e 6c 79 20 73 69 62 6c 69 . The only sibli
18106 6e 67 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 69 ng. ** page i
18107 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c s the right-chil
18108 64 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 2e d of the parent.
18109 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e Copy the conten
1810a 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ts of the. **
1810b 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 74 6f child page into
1810c 20 74 68 65 20 70 61 72 65 6e 74 2c 20 64 65 63 the parent, dec
1810d 72 65 61 73 69 6e 67 20 74 68 65 20 6f 76 65 72 reasing the over
1810e 61 6c 6c 20 68 65 69 67 68 74 20 6f 66 20 74 68 all height of th
1810f 65 0a 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 e. ** b-tree
18110 73 74 72 75 63 74 75 72 65 20 62 79 20 6f 6e 65 structure by one
18111 2e 20 54 68 69 73 20 69 73 20 64 65 73 63 72 69 . This is descri
18112 62 65 64 20 61 73 20 74 68 65 20 22 62 61 6c 61 bed as the "bala
18113 6e 63 65 2d 73 68 61 6c 6c 6f 77 65 72 22 0a 20 nce-shallower".
18114 20 20 20 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 69 ** sub-algori
18115 74 68 6d 20 69 6e 20 73 6f 6d 65 20 64 6f 63 75 thm in some docu
18116 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a mentation.. *
18117 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 *. ** If this
18118 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 is an auto-vacu
18119 75 6d 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 um database, the
1811a 20 63 61 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f 64 call to copyNod
1811b 65 43 6f 6e 74 65 6e 74 28 29 20 0a 20 20 20 20 eContent() .
1811c 2a 2a 20 73 65 74 73 20 61 6c 6c 20 70 6f 69 6e ** sets all poin
1811d 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 ter-map entries
1811e 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
1811f 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 database image
18120 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 66 6f pages . ** fo
18121 72 20 77 68 69 63 68 20 74 68 65 20 70 6f 69 6e r which the poin
18122 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 77 69 ter is stored wi
18123 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 thin the content
18124 20 62 65 69 6e 67 20 63 6f 70 69 65 64 2e 0a 20 being copied..
18125 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
18126 20 73 65 63 6f 6e 64 20 61 73 73 65 72 74 20 62 second assert b
18127 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 68 elow verifies th
18128 61 74 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 at the child pag
18129 65 20 69 73 20 64 65 66 72 61 67 6d 65 6e 74 65 e is defragmente
1812a 64 0a 20 20 20 20 2a 2a 20 28 69 74 20 6d 75 73 d. ** (it mus
1812b 74 20 62 65 2c 20 61 73 20 69 74 20 77 61 73 20 t be, as it was
1812c 6a 75 73 74 20 72 65 63 6f 6e 73 74 72 75 63 74 just reconstruct
1812d 65 64 20 75 73 69 6e 67 20 61 73 73 65 6d 62 6c ed using assembl
1812e 65 50 61 67 65 28 29 29 2e 20 54 68 69 73 0a 20 ePage()). This.
1812f 20 20 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74 61 ** is importa
18130 6e 74 20 69 66 20 74 68 65 20 70 61 72 65 6e 74 nt if the parent
18131 20 70 61 67 65 20 68 61 70 70 65 6e 73 20 74 6f page happens to
18132 20 62 65 20 70 61 67 65 20 31 20 6f 66 20 74 68 be page 1 of th
18133 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a e database. *
18134 2a 20 69 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 20 * image. */.
18135 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3d 3d 31 assert( nNew==1
18136 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
18137 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 apNew[0]->nFree
18138 3d 3d 20 0a 20 20 20 20 20 20 20 20 28 67 65 74 == . (get
18139 32 62 79 74 65 28 26 61 70 4e 65 77 5b 30 5d 2d 2byte(&apNew[0]-
1813a 3e 61 44 61 74 61 5b 35 5d 29 2d 61 70 4e 65 77 >aData[5])-apNew
1813b 5b 30 5d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2d [0]->cellOffset-
1813c 61 70 4e 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a apNew[0]->nCell*
1813d 32 29 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 63 2) . );. c
1813e 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 61 opyNodeContent(a
1813f 70 4e 65 77 5b 30 5d 2c 20 70 50 61 72 65 6e 74 pNew[0], pParent
18140 2c 20 26 72 63 29 3b 0a 20 20 20 20 66 72 65 65 , &rc);. free
18141 50 61 67 65 28 61 70 4e 65 77 5b 30 5d 2c 20 26 Page(apNew[0], &
18142 72 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 rc);. }else if(
18143 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
18144 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65 20 . /* Fix the
18145 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
18146 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 20 ies for all the
18147 63 65 6c 6c 73 20 74 68 61 74 20 77 65 72 65 20 cells that were
18148 73 68 69 66 74 65 64 20 61 72 6f 75 6e 64 2e 20 shifted around.
18149 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 72 . ** There ar
1814a 65 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 e several differ
1814b 65 6e 74 20 74 79 70 65 73 20 6f 66 20 70 6f 69 ent types of poi
1814c 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
1814d 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20 that need to.
1814e 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69 ** be dealt wi
1814f 74 68 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 th by this routi
18150 6e 65 2e 20 53 6f 6d 65 20 6f 66 20 74 68 65 73 ne. Some of thes
18151 65 20 68 61 76 65 20 62 65 65 6e 20 73 65 74 20 e have been set
18152 61 6c 72 65 61 64 79 2c 20 62 75 74 0a 20 20 20 already, but.
18153 20 2a 2a 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f ** many have no
18154 74 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t. The following
18155 20 69 73 20 61 20 73 75 6d 6d 61 72 79 3a 0a 20 is a summary:.
18156 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31 **. ** 1
18157 29 20 54 68 65 20 65 6e 74 72 69 65 73 20 61 73 ) The entries as
18158 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 sociated with ne
18159 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 w sibling pages
1815a 74 68 61 74 20 77 65 72 65 20 6e 6f 74 0a 20 20 that were not.
1815b 20 20 2a 2a 20 20 20 20 20 20 73 69 62 6c 69 6e ** siblin
1815c 67 73 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e gs when this fun
1815d 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 ction was called
1815e 2e 20 54 68 65 73 65 20 68 61 76 65 20 61 6c 72 . These have alr
1815f 65 61 64 79 0a 20 20 20 20 2a 2a 20 20 20 20 20 eady. **
18160 20 62 65 65 6e 20 73 65 74 2e 20 57 65 20 64 6f been set. We do
18161 6e 27 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 n't need to worr
18162 79 20 61 62 6f 75 74 20 6f 6c 64 20 73 69 62 6c y about old sibl
18163 69 6e 67 73 20 74 68 61 74 20 77 65 72 65 0a 20 ings that were.
18164 20 20 20 2a 2a 20 20 20 20 20 20 6d 6f 76 65 64 ** moved
18165 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 to the free-lis
18166 74 20 2d 20 74 68 65 20 66 72 65 65 50 61 67 65 t - the freePage
18167 28 29 20 63 6f 64 65 20 68 61 73 20 74 61 6b 65 () code has take
18168 6e 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 20 20 n care. **
18169 20 20 20 6f 66 20 74 68 6f 73 65 2e 0a 20 20 20 of those..
1816a 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 32 29 20 **. ** 2)
1816b 54 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 The pointer-map
1816c 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 entries associat
1816d 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73 ed with the firs
1816e 74 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 2a t overflow. *
1816f 2a 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 61 * page in a
18170 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 ny overflow chai
18171 6e 73 20 75 73 65 64 20 62 79 20 6e 65 77 20 64 ns used by new d
18172 69 76 69 64 65 72 20 63 65 6c 6c 73 2e 20 54 68 ivider cells. Th
18173 65 73 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 ese . **
18174 20 68 61 76 65 20 61 6c 73 6f 20 61 6c 72 65 61 have also alrea
18175 64 79 20 62 65 65 6e 20 74 61 6b 65 6e 20 63 61 dy been taken ca
18176 72 65 20 6f 66 20 62 79 20 74 68 65 20 69 6e 73 re of by the ins
18177 65 72 74 43 65 6c 6c 28 29 20 63 6f 64 65 2e 0a ertCell() code..
18178 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
18179 33 29 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 3) If the siblin
1817a 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 g pages are not
1817b 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 74 68 65 leaves, then the
1817c 20 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66 0a child pages of.
1817d 20 20 20 20 2a 2a 20 20 20 20 20 20 63 65 6c 6c ** cell
1817e 73 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 s stored on the
1817f 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 6d 61 sibling pages ma
18180 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 y need to be upd
18181 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 ated.. **.
18182 20 2a 2a 20 20 20 34 29 20 49 66 20 74 68 65 20 ** 4) If the
18183 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 sibling pages ar
18184 65 20 6e 6f 74 20 69 6e 74 65 72 6e 61 6c 20 69 e not internal i
18185 6e 74 6b 65 79 20 6e 6f 64 65 73 2c 20 74 68 65 ntkey nodes, the
18186 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 20 20 20 n any. **
18187 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 overflow pages
18188 20 75 73 65 64 20 62 79 20 74 68 65 73 65 20 63 used by these c
18189 65 6c 6c 73 20 6d 61 79 20 6e 65 65 64 20 74 6f ells may need to
1818a 20 62 65 20 75 70 64 61 74 65 64 0a 20 20 20 20 be updated.
1818b 2a 2a 20 20 20 20 20 20 28 69 6e 74 65 72 6e 61 ** (interna
1818c 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e l intkey nodes n
1818d 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69 ever contain poi
1818e 6e 74 65 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f nters to overflo
1818f 77 20 70 61 67 65 73 29 2e 0a 20 20 20 20 2a 2a w pages).. **
18190 0a 20 20 20 20 2a 2a 20 20 20 35 29 20 49 66 20 . ** 5) If
18191 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 the sibling page
18192 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 s are not leaves
18193 2c 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74 , then the point
18194 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 20 20 er-map. **
18195 20 20 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 entries for t
18196 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 he right-child p
18197 61 67 65 73 20 6f 66 20 65 61 63 68 20 73 69 62 ages of each sib
18198 6c 69 6e 67 20 6d 61 79 20 6e 65 65 64 0a 20 20 ling may need.
18199 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 62 65 20 ** to be
1819a 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a updated.. **.
1819b 20 20 20 20 2a 2a 20 43 61 73 65 73 20 31 20 61 ** Cases 1 a
1819c 6e 64 20 32 20 61 72 65 20 64 65 61 6c 74 20 77 nd 2 are dealt w
1819d 69 74 68 20 61 62 6f 76 65 20 62 79 20 6f 74 68 ith above by oth
1819e 65 72 20 63 6f 64 65 2e 20 54 68 65 20 6e 65 78 er code. The nex
1819f 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 64 t. ** block d
181a0 65 61 6c 73 20 77 69 74 68 20 63 61 73 65 73 20 eals with cases
181a1 33 20 61 6e 64 20 34 20 61 6e 64 20 74 68 65 20 3 and 4 and the
181a2 6f 6e 65 20 61 66 74 65 72 20 74 68 61 74 2c 20 one after that,
181a3 63 61 73 65 20 35 2e 20 53 69 6e 63 65 0a 20 20 case 5. Since.
181a4 20 20 2a 2a 20 73 65 74 74 69 6e 67 20 61 20 70 ** setting a p
181a5 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 ointer map entry
181a6 20 69 73 20 61 20 72 65 6c 61 74 69 76 65 6c 79 is a relatively
181a7 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72 61 expensive opera
181a8 74 69 6f 6e 2c 20 74 68 69 73 0a 20 20 20 20 2a tion, this. *
181a9 2a 20 63 6f 64 65 20 6f 6e 6c 79 20 73 65 74 73 * code only sets
181aa 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 pointer map ent
181ab 72 69 65 73 20 66 6f 72 20 63 68 69 6c 64 20 6f ries for child o
181ac 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 r overflow pages
181ad 20 74 68 61 74 20 68 61 76 65 0a 20 20 20 20 2a that have. *
181ae 2a 20 61 63 74 75 61 6c 6c 79 20 6d 6f 76 65 64 * actually moved
181af 20 62 65 74 77 65 65 6e 20 70 61 67 65 73 2e 20 between pages.
181b0 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 */. MemPage
181b1 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 30 5d *pNew = apNew[0]
181b2 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 ;. MemPage *p
181b3 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 30 5d 3b Old = apCopy[0];
181b4 0a 20 20 20 20 69 6e 74 20 6e 4f 76 65 72 66 6c . int nOverfl
181b5 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 ow = pOld->nOver
181b6 66 6c 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 4e flow;. int iN
181b7 65 78 74 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e extOld = pOld->n
181b8 43 65 6c 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f 77 Cell + nOverflow
181b9 3b 0a 20 20 20 20 69 6e 74 20 69 4f 76 65 72 66 ;. int iOverf
181ba 6c 6f 77 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77 low = (nOverflow
181bb 20 3f 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 ? pOld->aOvfl[0
181bc 5d 2e 69 64 78 20 3a 20 2d 31 29 3b 0a 20 20 20 ].idx : -1);.
181bd 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 j = 0;
181be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
181bf 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27 /* Current '
181c0 6f 6c 64 27 20 73 69 62 6c 69 6e 67 20 70 61 67 old' sibling pag
181c1 65 20 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b 20 e */. k = 0;
181c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
181c3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
181c4 75 72 72 65 6e 74 20 27 6e 65 77 27 20 73 69 62 urrent 'new' sib
181c5 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20 ling page */.
181c6 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c for(i=0; i<nCel
181c7 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 l; i++){. i
181c8 6e 74 20 69 73 44 69 76 69 64 65 72 20 3d 20 30 nt isDivider = 0
181c9 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 ;. while( i
181ca 3d 3d 69 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 20 ==iNextOld ){.
181cb 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 /* Cell i
181cc 69 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 is the cell imme
181cd 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e diately followin
181ce 67 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 g the last cell
181cf 6f 6e 20 6f 6c 64 0a 20 20 20 20 20 20 20 20 2a on old. *
181d0 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 6a * sibling page j
181d1 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 . If the sibling
181d2 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 70 s are not leaf p
181d3 61 67 65 73 20 6f 66 20 61 6e 0a 20 20 20 20 20 ages of an.
181d4 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74 ** intkey b-t
181d5 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69 ree, then cell i
181d6 20 77 61 73 20 61 20 64 69 76 69 64 65 72 20 63 was a divider c
181d7 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ell. */.
181d8 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b pOld = apCopy[++
181d9 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 j];. iNex
181da 74 4f 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 66 tOld = i + !leaf
181db 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65 Data + pOld->nCe
181dc 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 ll + pOld->nOver
181dd 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66 flow;. if
181de 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f ( pOld->nOverflo
181df 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e w ){. n
181e0 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d Overflow = pOld-
181e1 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 >nOverflow;.
181e2 20 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 20 iOverflow
181e3 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20 = i + !leafData
181e4 2b 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d + pOld->aOvfl[0]
181e5 2e 69 64 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a .idx;. }.
181e6 20 20 20 20 20 20 20 20 69 73 44 69 76 69 64 65 isDivide
181e7 72 20 3d 20 21 6c 65 61 66 44 61 74 61 3b 20 20 r = !leafData;
181e8 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
181e9 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 assert(nOverflow
181ea 3e 30 20 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 3c >0 || iOverflow<
181eb 69 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 i );. asser
181ec 74 28 6e 4f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c t(nOverflow<2 ||
181ed 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e pOld->aOvfl[0].
181ee 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c idx==pOld->aOvfl
181ef 5b 31 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20 [1].idx-1);.
181f0 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c assert(nOverfl
181f1 6f 77 3c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f ow<3 || pOld->aO
181f2 76 66 6c 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64 vfl[1].idx==pOld
181f3 2d 3e 61 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31 ->aOvfl[2].idx-1
181f4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d );. if( i==
181f5 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 iOverflow ){.
181f6 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d isDivider =
181f7 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1;. if(
181f8 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20 (--nOverflow)>0
181f9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76 ){. iOv
181fa 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20 erflow++;.
181fb 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 }. }..
181fc 20 20 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77 if( i==cntNew
181fd 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f [k] ){. /
181fe 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20 * Cell i is the
181ff 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 cell immediately
18200 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c following the l
18201 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a ast cell on new.
18202 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 ** sibli
18203 6e 67 20 70 61 67 65 20 6b 2e 20 49 66 20 74 68 ng page k. If th
18204 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e e siblings are n
18205 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66 ot leaf pages of
18206 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 an. ** i
18207 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68 ntkey b-tree, th
18208 65 6e 20 63 65 6c 6c 20 69 20 69 73 20 61 20 64 en cell i is a d
18209 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f ivider cell. */
1820a 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 . pNew =
1820b 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 apNew[++k];.
1820c 20 20 20 20 69 66 28 20 21 6c 65 61 66 44 61 74 if( !leafDat
1820d 61 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 a ) continue;.
1820e 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
1820f 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20 rt( j<nOld );.
18210 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e assert( k<nN
18211 65 77 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ew );.. /*
18212 49 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20 If the cell was
18213 6f 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64 originally divid
18214 65 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20 er cell (and is
18215 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20 not now) or.
18216 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77 ** an overflow
18217 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65 cell, or if the
18218 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65 cell was locate
18219 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74 d on a different
1821a 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a sibling. *
1821b 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74 68 * page before th
1821c 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 e balancing, the
1821d 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 n the pointer ma
1821e 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 p entries associ
1821f 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 69 ated. ** wi
18220 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 20 th any child or
18221 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 6e overflow pages n
18222 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 eed to be update
18223 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 d. */. if(
18224 20 69 73 44 69 76 69 64 65 72 20 7c 7c 20 70 4f isDivider || pO
18225 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e ld->pgno!=pNew->
18226 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 pgno ){.
18227 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74 if( !leafCorrect
18228 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ion ){.
18229 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
1822a 67 65 74 34 62 79 74 65 28 61 70 43 65 6c 6c 5b get4byte(apCell[
1822b 69 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 i]), PTRMAP_BTRE
1822c 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 E, pNew->pgno, &
1822d 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc);. }.
1822e 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c if( szCel
1822f 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f l[i]>pNew->minLo
18230 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 cal ){.
18231 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 ptrmapPutOvflPt
18232 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69 r(pNew, apCell[i
18233 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 ], &rc);.
18234 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
18235 0a 0a 20 20 20 20 69 66 28 20 21 6c 65 61 66 43 .. if( !leafC
18236 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 orrection ){.
18237 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e for(i=0; i<nN
18238 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ew; i++){.
18239 20 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34 u32 key = get4
1823a 62 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e byte(&apNew[i]->
1823b 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20 aData[8]);.
1823c 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 ptrmapPut(pBt
1823d 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54 , key, PTRMAP_BT
1823e 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 REE, apNew[i]->p
1823f 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 gno, &rc);.
18240 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a }. }..#if 0.
18241 20 20 20 20 2f 2a 20 54 68 65 20 70 74 72 6d 61 /* The ptrma
18242 70 43 68 65 63 6b 50 61 67 65 73 28 29 20 63 6f pCheckPages() co
18243 6e 74 61 69 6e 73 20 61 73 73 65 72 74 28 29 20 ntains assert()
18244 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 statements that
18245 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20 20 verify that.
18246 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d ** all pointer m
18247 61 70 20 70 61 67 65 73 20 61 72 65 20 73 65 74 ap pages are set
18248 20 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 73 correctly. This
18249 20 69 73 20 68 65 6c 70 66 75 6c 20 77 68 69 6c is helpful whil
1824a 65 20 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 67 e . ** debugg
1824b 69 6e 67 2e 20 54 68 69 73 20 69 73 20 75 73 75 ing. This is usu
1824c 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 62 65 ally disabled be
1824d 63 61 75 73 65 20 61 20 63 6f 72 72 75 70 74 20 cause a corrupt
1824e 64 61 74 61 62 61 73 65 20 6d 61 79 0a 20 20 20 database may.
1824f 20 2a 2a 20 63 61 75 73 65 20 61 6e 20 61 73 73 ** cause an ass
18250 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20 ert() statement
18251 74 6f 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20 to fail. */.
18252 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 ptrmapCheckPage
18253 73 28 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a s(apNew, nNew);.
18254 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50 ptrmapCheckP
18255 61 67 65 73 28 26 70 50 61 72 65 6e 74 2c 20 31 ages(&pParent, 1
18256 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 );.#endif. }..
18257 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 assert( pParent
18258 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 52 ->isInit );. TR
18259 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66 ACE(("BALANCE: f
1825a 69 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20 inished: old=%d
1825b 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c new=%d cells=%d\
1825c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 4f n",. nO
1825d 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29 ld, nNew, nCell)
1825e 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c );.. /*. ** Cl
1825f 65 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74 eanup before ret
18260 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c urning.. */.bal
18261 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 ance_cleanup:.
18262 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 sqlite3ScratchFr
18263 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f ee(apCell);. fo
18264 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 r(i=0; i<nOld; i
18265 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 ++){. release
18266 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a Page(apOld[i]);.
18267 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
18268 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nNew; i++){.
18269 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e releasePage(apN
1826a 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 ew[i]);. }.. r
1826b 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
1826c 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1826d 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e n is called when
1826e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
1826f 66 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 f a b-tree struc
18270 74 75 72 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66 ture is.** overf
18271 75 6c 6c 20 28 68 61 73 20 6f 6e 65 20 6f 72 20 ull (has one or
18272 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 more overflow pa
18273 67 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 ges)..**.** A ne
18274 77 20 63 68 69 6c 64 20 70 61 67 65 20 69 73 20 w child page is
18275 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 allocated and th
18276 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
18277 65 20 63 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a e current root.*
18278 2a 20 70 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e * page, includin
18279 67 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 g overflow cells
1827a 2c 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 , are copied int
1827b 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 54 68 65 o the child. The
1827c 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 root.** page is
1827d 20 74 68 65 6e 20 6f 76 65 72 77 72 69 74 74 65 then overwritte
1827e 6e 20 74 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20 n to make it an
1827f 65 6d 70 74 79 20 70 61 67 65 20 77 69 74 68 20 empty page with
18280 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 the right-child
18281 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e .** pointer poin
18282 74 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 20 ting to the new
18283 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f page..**.** Befo
18284 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c re returning, al
18285 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e l pointer-map en
18286 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 tries correspond
18287 69 6e 67 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a ing to pages .**
18288 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 68 that the new ch
18289 69 6c 64 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e ild-page now con
1828a 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74 tains pointers t
1828b 6f 20 61 72 65 20 75 70 64 61 74 65 64 2e 20 54 o are updated. T
1828c 68 65 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72 he.** entry corr
1828d 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 esponding to the
1828e 20 6e 65 77 20 72 69 67 68 74 2d 63 68 69 6c 64 new right-child
1828f 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 pointer of the
18290 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 root.** page is
18291 61 6c 73 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a also updated..**
18292 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 .** If successfu
18293 6c 2c 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73 l, *ppChild is s
18294 65 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 et to contain a
18295 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 reference to the
18296 20 63 68 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20 child .** page
18297 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 and SQLITE_OK is
18298 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 returned. In th
18299 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c is case the call
1829a 65 72 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a er is required.*
1829b 2a 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73 * to call releas
1829c 65 50 61 67 65 28 29 20 6f 6e 20 2a 70 70 43 68 ePage() on *ppCh
1829d 69 6c 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 ild exactly once
1829e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
1829f 63 75 72 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f curs,.** an erro
182a0 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
182a1 65 64 20 61 6e 64 20 2a 70 70 43 68 69 6c 64 20 ed and *ppChild
182a2 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a is set to 0..*/.
182a3 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e static int balan
182a4 63 65 5f 64 65 65 70 65 72 28 4d 65 6d 50 61 67 ce_deeper(MemPag
182a5 65 20 2a 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67 e *pRoot, MemPag
182a6 65 20 2a 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20 e **ppChild){.
182a7 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
182a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
182a9 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 66 * Return value f
182aa 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65 rom subprocedure
182ab 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a s */. MemPage *
182ac 70 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 pChild = 0;
182ad 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
182ae 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 to a new child
182af 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 page */. Pgno p
182b0 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 gnoChild = 0;
182b1 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
182b2 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e number of the n
182b3 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f ew child page */
182b4 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
182b5 20 3d 20 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20 = pRoot->pBt;
182b6 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 2a /* The BTree *
182b7 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f /.. assert( pRo
182b8 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 ot->nOverflow>0
182b9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
182ba 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
182bb 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a pBt->mutex) );..
182bc 20 20 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c /* Make pRoot,
182bd 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f the root page o
182be 66 20 74 68 65 20 62 2d 74 72 65 65 2c 20 77 72 f the b-tree, wr
182bf 69 74 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65 itable. Allocate
182c0 20 61 20 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67 a new . ** pag
182c1 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f e that will beco
182c2 6d 65 20 74 68 65 20 6e 65 77 20 72 69 67 68 74 me the new right
182c3 2d 63 68 69 6c 64 20 6f 66 20 70 50 61 67 65 2e -child of pPage.
182c4 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e Copy the conten
182c5 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e ts. ** of the n
182c6 6f 64 65 20 73 74 6f 72 65 64 20 6f 6e 20 70 52 ode stored on pR
182c7 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 oot into the new
182c8 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a child page.. *
182c9 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 /. rc = sqlite3
182ca 50 61 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 PagerWrite(pRoot
182cb 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 ->pDbPage);. if
182cc 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
182cd 29 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f ){. rc = allo
182ce 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
182cf 74 2c 26 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43 t,&pChild,&pgnoC
182d0 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f hild,pRoot->pgno
182d1 2c 30 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 ,0);. copyNod
182d2 65 43 6f 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20 eContent(pRoot,
182d3 70 43 68 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20 pChild, &rc);.
182d4 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 if( ISAUTOVACU
182d5 55 4d 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d UM ){. ptrm
182d6 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 43 apPut(pBt, pgnoC
182d7 68 69 6c 64 2c 20 50 54 52 4d 41 50 5f 42 54 52 hild, PTRMAP_BTR
182d8 45 45 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c EE, pRoot->pgno,
182d9 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d &rc);. }. }
182da 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 . if( rc ){.
182db 20 2a 70 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20 *ppChild = 0;.
182dc 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
182dd 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 Child);. retu
182de 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 rn rc;. }. ass
182df 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
182e0 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 68 rIswriteable(pCh
182e1 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b ild->pDbPage) );
182e2 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
182e3 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
182e4 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 le(pRoot->pDbPag
182e5 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
182e6 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 pChild->nCell==p
182e7 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a Root->nCell );..
182e8 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 TRACE(("BALANC
182e9 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25 64 20 E: copy root %d
182ea 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f into %d\n", pRoo
182eb 74 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d t->pgno, pChild-
182ec 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43 >pgno));.. /* C
182ed 6f 70 79 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 opy the overflow
182ee 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f cells from pRoo
182ef 74 20 74 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20 t to pChild */.
182f0 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e memcpy(pChild->
182f1 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 4f aOvfl, pRoot->aO
182f2 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 vfl, pRoot->nOve
182f3 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 52 6f rflow*sizeof(pRo
182f4 6f 74 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a ot->aOvfl[0]));.
182f5 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66 pChild->nOverf
182f6 6c 6f 77 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f 76 low = pRoot->nOv
182f7 65 72 66 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a 65 erflow;.. /* Ze
182f8 72 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 ro the contents
182f9 6f 66 20 70 52 6f 6f 74 2e 20 54 68 65 6e 20 69 of pRoot. Then i
182fa 6e 73 74 61 6c 6c 20 70 43 68 69 6c 64 20 61 73 nstall pChild as
182fb 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 the right-child
182fc 2e 20 2a 2f 0a 20 20 7a 65 72 6f 50 61 67 65 28 . */. zeroPage(
182fd 70 52 6f 6f 74 2c 20 70 43 68 69 6c 64 2d 3e 61 pRoot, pChild->a
182fe 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c Data[0] & ~PTF_L
182ff 45 41 46 29 3b 0a 20 20 70 75 74 34 62 79 74 65 EAF);. put4byte
18300 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 (&pRoot->aData[p
18301 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b Root->hdrOffset+
18302 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a 8], pgnoChild);.
18303 0a 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 70 43 . *ppChild = pC
18304 68 69 6c 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 hild;. return S
18305 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
18306 2a 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 ** The page that
18307 20 70 43 75 72 20 63 75 72 72 65 6e 74 6c 79 20 pCur currently
18308 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 6a 75 points to has ju
18309 73 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 st been modified
1830a 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e in.** some way.
1830b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 This function f
1830c 69 67 75 72 65 73 20 6f 75 74 20 69 66 20 74 68 igures out if th
1830d 69 73 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 is modification
1830e 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65 means the.** tre
1830f 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61 e needs to be ba
18310 6c 61 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 73 lanced, and if s
18311 6f 20 63 61 6c 6c 73 20 74 68 65 20 61 70 70 72 o calls the appr
18312 6f 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e opriate balancin
18313 67 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 42 g .** routine. B
18314 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65 alancing routine
18315 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 62 s are:.**.** b
18316 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 0a 2a alance_quick().*
18317 2a 20 20 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 * balance_deep
18318 65 72 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 er().** balanc
18319 65 5f 6e 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a 73 e_nonroot().*/.s
1831a 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 tatic int balanc
1831b 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 e(BtCursor *pCur
1831c 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1831d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 LITE_OK;. const
1831e 20 69 6e 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72 int nMin = pCur
1831f 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
18320 65 20 2a 20 32 20 2f 20 33 3b 0a 20 20 75 38 20 e * 2 / 3;. u8
18321 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 aBalanceQuickSpa
18322 63 65 5b 31 33 5d 3b 0a 20 20 75 38 20 2a 70 46 ce[13];. u8 *pF
18323 72 65 65 20 3d 20 30 3b 0a 0a 20 20 54 45 53 54 ree = 0;.. TEST
18324 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 ONLY( int balanc
18325 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20 3d e_quick_called =
18326 20 30 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 0 );. TESTONLY
18327 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 ( int balance_de
18328 65 70 65 72 5f 63 61 6c 6c 65 64 20 3d 20 30 20 eper_called = 0
18329 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 );.. do {. i
1832a 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d nt iPage = pCur-
1832b 3e 69 50 61 67 65 3b 0a 20 20 20 20 4d 65 6d 50 >iPage;. MemP
1832c 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 age *pPage = pCu
1832d 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d r->apPage[iPage]
1832e 3b 0a 0a 20 20 20 20 69 66 28 20 69 50 61 67 65 ;.. if( iPage
1832f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
18330 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
18331 77 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 w ){. /*
18332 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 The root page of
18333 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 20 6f the b-tree is o
18334 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 verfull. In this
18335 20 63 61 73 65 20 63 61 6c 6c 20 74 68 65 0a 20 case call the.
18336 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 ** balanc
18337 65 5f 64 65 65 70 65 72 28 29 20 66 75 6e 63 74 e_deeper() funct
18338 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61 20 ion to create a
18339 6e 65 77 20 63 68 69 6c 64 20 66 6f 72 20 74 68 new child for th
1833a 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 e root-page.
1833b 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 70 79 20 ** and copy
1833c 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 the current cont
1833d 65 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74 ents of the root
1833e 2d 70 61 67 65 20 74 6f 20 69 74 2e 20 54 68 65 -page to it. The
1833f 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 78 74 . ** next
18340 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 iteration of th
18341 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 e do-loop will b
18342 61 6c 61 6e 63 65 20 74 68 65 20 63 68 69 6c 64 alance the child
18343 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a page.. *
18344 2f 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 / . asser
18345 74 28 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 t( (balance_deep
18346 65 72 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 er_called++)==0
18347 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
18348 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 70 balance_deeper(p
18349 50 61 67 65 2c 20 26 70 43 75 72 2d 3e 61 70 50 Page, &pCur->apP
1834a 61 67 65 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 age[1]);.
1834b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1834c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1834d 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 31 3b pCur->iPage = 1;
1834e 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d . pCur-
1834f 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20 >aiIdx[0] = 0;.
18350 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61 pCur->a
18351 69 49 64 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 20 iIdx[1] = 0;.
18352 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
18353 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 5d 2d 3e Cur->apPage[1]->
18354 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 20 nOverflow );.
18355 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
18356 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 se{. brea
18357 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
18358 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e else if( pPage->
18359 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 nOverflow==0 &&
1835a 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e 4d pPage->nFree<=nM
1835b 69 6e 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 in ){. brea
1835c 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 k;. }else{.
1835d 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f MemPage * co
1835e 6e 73 74 20 70 50 61 72 65 6e 74 20 3d 20 70 43 nst pParent = pC
1835f 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 ur->apPage[iPage
18360 2d 31 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 -1];. int c
18361 6f 6e 73 74 20 69 49 64 78 20 3d 20 70 43 75 72 onst iIdx = pCur
18362 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 2d 31 5d ->aiIdx[iPage-1]
18363 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ;.. rc = sq
18364 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
18365 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 pParent->pDbPage
18366 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
18367 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 =SQLITE_OK ){.#i
18368 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18369 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 20 T_QUICKBALANCE.
1836a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
1836b 2d 3e 68 61 73 44 61 74 61 0a 20 20 20 20 20 20 ->hasData.
1836c 20 20 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 && pPage->nOv
1836d 65 72 66 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 20 erflow==1.
1836e 20 20 20 26 26 20 70 50 61 67 65 2d 3e 61 4f 76 && pPage->aOv
1836f 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65 fl[0].idx==pPage
18370 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 20 ->nCell.
18371 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e && pParent->pgn
18372 6f 21 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26 o!=1. &&
18373 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d pParent->nCell=
18374 3d 69 49 64 78 0a 20 20 20 20 20 20 20 20 29 7b =iIdx. ){
18375 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 . /* Ca
18376 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b ll balance_quick
18377 28 29 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e () to create a n
18378 65 77 20 73 69 62 6c 69 6e 67 20 6f 66 20 70 50 ew sibling of pP
18379 61 67 65 20 6f 6e 20 77 68 69 63 68 0a 20 20 20 age on which.
1837a 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f ** to sto
1837b 72 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 re the overflow
1837c 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 75 cell. balance_qu
1837d 69 63 6b 28 29 20 69 6e 73 65 72 74 73 20 61 20 ick() inserts a
1837e 6e 65 77 20 63 65 6c 6c 0a 20 20 20 20 20 20 20 new cell.
1837f 20 20 20 2a 2a 20 69 6e 74 6f 20 70 50 61 72 65 ** into pPare
18380 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61 nt, which may ca
18381 75 73 65 20 70 50 61 72 65 6e 74 20 6f 76 65 72 use pParent over
18382 66 6c 6f 77 2e 20 49 66 20 74 68 69 73 0a 20 20 flow. If this.
18383 20 20 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65 ** happe
18384 6e 73 2c 20 74 68 65 20 6e 65 78 74 20 69 6e 74 ns, the next int
18385 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 eration of the d
18386 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 o-loop will bala
18387 6e 63 65 20 70 50 61 72 65 6e 74 20 0a 20 20 20 nce pParent .
18388 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 65 69 ** use ei
18389 74 68 65 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e ther balance_non
1838a 72 6f 6f 74 28 29 20 6f 72 20 62 61 6c 61 6e 63 root() or balanc
1838b 65 5f 64 65 65 70 65 72 28 29 2e 20 55 6e 74 69 e_deeper(). Unti
1838c 6c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 l this.
1838d 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65 ** happens, the
1838e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69 overflow cell i
1838f 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
18390 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 aBalanceQuickSpa
18391 63 65 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a ce[]. *
18392 2a 20 62 75 66 66 65 72 2e 20 0a 20 20 20 20 20 * buffer. .
18393 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 **.
18394 20 20 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65 ** The purpose
18395 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
18396 67 20 61 73 73 65 72 74 28 29 20 69 73 20 74 6f g assert() is to
18397 20 63 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c 79 check that only
18398 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 a. **
18399 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 20 62 single call to b
1839a 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 alance_quick() i
1839b 73 20 6d 61 64 65 20 66 6f 72 20 65 61 63 68 20 s made for each
1839c 63 61 6c 6c 20 74 6f 20 74 68 69 73 0a 20 20 20 call to this.
1839d 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 ** functi
1839e 6f 6e 2e 20 49 66 20 74 68 69 73 20 77 65 72 65 on. If this were
1839f 20 6e 6f 74 20 76 65 72 69 66 69 65 64 2c 20 61 not verified, a
183a0 20 73 75 62 74 6c 65 20 62 75 67 20 69 6e 76 6f subtle bug invo
183a1 6c 76 69 6e 67 20 72 65 75 73 65 0a 20 20 20 20 lving reuse.
183a2 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 ** of the
183a3 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 aBalanceQuickSpa
183a4 63 65 5b 5d 20 6d 69 67 68 74 20 73 6e 65 61 6b ce[] might sneak
183a5 20 69 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a in.. *
183a6 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 /. asse
183a7 72 74 28 20 28 62 61 6c 61 6e 63 65 5f 71 75 69 rt( (balance_qui
183a8 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20 ck_called++)==0
183a9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 );. rc
183aa 3d 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 = balance_quick(
183ab 70 50 61 72 65 6e 74 2c 20 70 50 61 67 65 2c 20 pParent, pPage,
183ac 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61 aBalanceQuickSpa
183ad 63 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ce);. }el
183ae 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 se.#endif.
183af 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a {. /*
183b0 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 63 In this case, c
183b1 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 all balance_nonr
183b2 6f 6f 74 28 29 20 74 6f 20 72 65 64 69 73 74 72 oot() to redistr
183b3 69 62 75 74 65 20 63 65 6c 6c 73 0a 20 20 20 20 ibute cells.
183b4 20 20 20 20 20 20 2a 2a 20 62 65 74 77 65 65 6e ** between
183b5 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f pPage and up to
183b6 20 32 20 6f 66 20 69 74 73 20 73 69 62 6c 69 6e 2 of its siblin
183b7 67 20 70 61 67 65 73 2e 20 54 68 69 73 20 69 6e g pages. This in
183b8 76 6f 6c 76 65 73 0a 20 20 20 20 20 20 20 20 20 volves.
183b9 20 2a 2a 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 ** modifying th
183ba 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 e contents of pP
183bb 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 arent, which may
183bc 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20 74 cause pParent t
183bd 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 o. ** b
183be 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f ecome overfull o
183bf 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65 r underfull. The
183c0 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 next iteration
183c1 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a 20 of the do-loop.
183c2 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c ** will
183c3 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72 balance the par
183c4 65 6e 74 20 70 61 67 65 20 74 6f 20 63 6f 72 72 ent page to corr
183c5 65 63 74 20 74 68 69 73 2e 0a 20 20 20 20 20 20 ect this..
183c6 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 20 20 ** .
183c7 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65 ** If the pare
183c8 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20 nt page becomes
183c9 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f 76 overfull, the ov
183ca 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 63 erflow cell or c
183cb 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 20 2a ells. *
183cc 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 * are stored in
183cd 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 the pSpace buffe
183ce 72 20 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65 r allocated imme
183cf 64 69 61 74 65 6c 79 20 62 65 6c 6f 77 2e 20 0a diately below. .
183d0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73 ** A s
183d1 75 62 73 65 71 75 65 6e 74 20 69 74 65 72 61 74 ubsequent iterat
183d2 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f ion of the do-lo
183d3 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20 77 69 74 op will deal wit
183d4 68 20 74 68 69 73 20 62 79 0a 20 20 20 20 20 20 h this by.
183d5 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 62 ** calling b
183d6 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
183d7 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 (balance_deeper
183d8 28 29 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 () may be called
183d9 20 66 69 72 73 74 2c 0a 20 20 20 20 20 20 20 20 first,.
183da 20 20 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 ** but it does
183db 6e 27 74 20 64 65 61 6c 20 77 69 74 68 20 6f 76 n't deal with ov
183dc 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 6a erflow cells - j
183dd 75 73 74 20 6d 6f 76 65 73 20 74 68 65 6d 20 74 ust moves them t
183de 6f 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a o a. **
183df 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65 29 different page)
183e0 2e 20 4f 6e 63 65 20 74 68 69 73 20 73 75 62 73 . Once this subs
183e1 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 62 equent call to b
183e2 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 alance_nonroot()
183e3 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 . ** h
183e4 61 73 20 63 6f 6d 70 6c 65 74 65 64 2c 20 69 74 as completed, it
183e5 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 6c 65 is safe to rele
183e6 61 73 65 20 74 68 65 20 70 53 70 61 63 65 20 62 ase the pSpace b
183e7 75 66 66 65 72 20 75 73 65 64 20 62 79 0a 20 20 uffer used by.
183e8 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 ** the p
183e9 72 65 76 69 6f 75 73 20 63 61 6c 6c 2c 20 61 73 revious call, as
183ea 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 the overflow ce
183eb 6c 6c 20 64 61 74 61 20 77 69 6c 6c 20 68 61 76 ll data will hav
183ec 65 20 62 65 65 6e 20 0a 20 20 20 20 20 20 20 20 e been .
183ed 20 20 2a 2a 20 63 6f 70 69 65 64 20 65 69 74 68 ** copied eith
183ee 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79 er into the body
183ef 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70 of a database p
183f0 61 67 65 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 age or into the
183f1 6e 65 77 0a 20 20 20 20 20 20 20 20 20 20 2a 2a new. **
183f2 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 70 pSpace buffer p
183f3 61 73 73 65 64 20 74 6f 20 74 68 65 20 6c 61 74 assed to the lat
183f4 74 65 72 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 ter call to bala
183f5 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a 20 nce_nonroot()..
183f6 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
183f7 20 20 20 20 20 20 75 38 20 2a 70 53 70 61 63 65 u8 *pSpace
183f8 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 = sqlite3PageMa
183f9 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 42 74 2d 3e lloc(pCur->pBt->
183fa 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 pageSize);.
183fb 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 rc = balanc
183fc 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e e_nonroot(pParen
183fd 74 2c 20 69 49 64 78 2c 20 70 53 70 61 63 65 2c t, iIdx, pSpace,
183fe 20 69 50 61 67 65 3d 3d 31 29 3b 0a 20 20 20 20 iPage==1);.
183ff 20 20 20 20 20 20 69 66 28 20 70 46 72 65 65 20 if( pFree
18400 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f ){. /
18401 2a 20 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f * If pFree is no
18402 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 t NULL, it point
18403 73 20 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 s to the pSpace
18404 62 75 66 66 65 72 20 75 73 65 64 20 0a 20 20 20 buffer used .
18405 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61 ** by a
18406 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 previous call t
18407 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f o balance_nonroo
18408 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 t(). Its content
18409 73 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 20 s are.
1840a 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20 ** now stored
1840b 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 64 either on real d
1840c 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f 72 atabase pages or
1840d 20 77 69 74 68 69 6e 20 74 68 65 20 0a 20 20 20 within the .
1840e 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 20 ** new
1840f 70 53 70 61 63 65 20 62 75 66 66 65 72 2c 20 73 pSpace buffer, s
18410 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61 66 65 o it may be safe
18411 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e 20 2a ly freed here. *
18412 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 /. sq
18413 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 46 lite3PageFree(pF
18414 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ree);.
18415 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 }.. /*
18416 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 The pSpace buffe
18417 72 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 r will be freed
18418 61 66 74 65 72 20 74 68 65 20 6e 65 78 74 20 63 after the next c
18419 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20 all to.
1841a 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 ** balance_nonr
1841b 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 62 oot(), or just b
1841c 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 efore this funct
1841d 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 69 ion returns, whi
1841e 63 68 65 76 65 72 0a 20 20 20 20 20 20 20 20 20 chever.
1841f 20 2a 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e ** comes first.
18420 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 46 */. pF
18421 72 65 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 ree = pSpace;.
18422 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
18423 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f . pPage->nO
18424 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20 verflow = 0;..
18425 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 /* The next
18426 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 iteration of the
18427 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65 do-loop balance
18428 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 s the parent pag
18429 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 e. */. rele
1842a 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
1842b 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 pCur->iPag
1842c 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68 e--;. }. }wh
1842d 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
1842e 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 72 OK );.. if( pFr
1842f 65 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ee ){. sqlite
18430 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65 29 3PageFree(pFree)
18431 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
18432 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 c;.}.../*.** Ins
18433 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64 ert a new record
18434 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e into the BTree.
18435 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76 The key is giv
18436 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79 en by (pKey,nKey
18437 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74 ).** and the dat
18438 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70 a is given by (p
18439 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68 Data,nData). Th
1843a 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 e cursor is used
1843b 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69 only to.** defi
1843c 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68 ne what table th
1843d 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20 e record should
1843e 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f be inserted into
1843f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a . The cursor.**
18440 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
18441 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f g at a random lo
18442 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f cation..**.** Fo
18443 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c r an INTKEY tabl
18444 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79 e, only the nKey
18445 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 value of the ke
18446 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79 y is used. pKey
18447 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 is.** ignored.
18448 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20 For a ZERODATA
18449 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61 table, the pData
1844a 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62 and nData are b
1844b 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a oth ignored..**.
1844c 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 ** If the seekRe
1844d 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 sult parameter i
1844e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
1844f 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 a successful ca
18450 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55 ll to.** MovetoU
18451 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 73 65 65 npacked() to see
18452 6b 20 63 75 72 73 6f 72 20 70 43 75 72 20 74 6f k cursor pCur to
18453 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61 (pKey, nKey) ha
18454 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 s already.** bee
18455 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 65 n performed. see
18456 6b 52 65 73 75 6c 74 20 69 73 20 74 68 65 20 73 kResult is the s
18457 65 61 72 63 68 20 72 65 73 75 6c 74 20 72 65 74 earch result ret
18458 75 72 6e 65 64 20 28 61 20 6e 65 67 61 74 69 76 urned (a negativ
18459 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 70 e.** number if p
1845a 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e Cur points at an
1845b 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 73 entry that is s
1845c 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b 65 maller than (pKe
1845d 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20 y, nKey), or.**
1845e 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 a positive value
1845f 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 if pCur points
18460 61 74 20 61 6e 20 65 74 72 79 20 74 68 61 74 20 at an etry that
18461 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a is larger than .
18462 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29 ** (pKey, nKey))
18463 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 . .**.** If the
18464 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d seekResult param
18465 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f eter is non-zero
18466 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 , then the calle
18467 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 r guarantees tha
18468 74 0a 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 72 t.** cursor pCur
18469 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 is pointing at
1846a 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f 70 the existing cop
1846b 79 20 6f 66 20 61 20 72 6f 77 20 74 68 61 74 20 y of a row that
1846c 69 73 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 is to be.** over
1846d 77 72 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65 written. If the
1846e 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 seekResult para
1846f 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e meter is 0, then
18470 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79 cursor pCur may
18471 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79 .** point to any
18472 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20 entry or to no
18473 65 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64 entry at all and
18474 20 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f so this functio
18475 6e 20 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a n has to seek.**
18476 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f the cursor befo
18477 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63 re the new key c
18478 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a an be inserted..
18479 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1847a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
1847b 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75 eeInsert(. BtCu
1847c 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 rsor *pCur,
1847d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1847e 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74 sert data into t
1847f 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 he table of this
18480 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e cursor */. con
18481 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 st void *pKey, i
18482 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 64 nKey, /* T
18483 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65 he key of the ne
18484 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f w record */. co
18485 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c nst void *pData,
18486 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20 int nData, /*
18487 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20 The data of the
18488 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 new record */.
18489 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 int nZero,
1848a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1848b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 * Number of extr
1848c 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70 70 a 0 bytes to app
1848d 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20 end to data */.
1848e 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c int appendBias,
1848f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18490 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
18491 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70 is likely an app
18492 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 end */. int see
18493 6b 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20 kResult
18494 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
18495 74 20 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 74 t of prior Movet
18496 6f 55 6e 70 61 63 6b 65 64 28 29 20 63 61 6c 6c oUnpacked() call
18497 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
18498 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65 . int loc = see
18499 6b 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 kResult;
1849a 20 20 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20 /* -1: before
1849b 64 65 73 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e desired location
1849c 20 20 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20 +1: after */.
1849d 20 69 6e 74 20 73 7a 4e 65 77 3b 0a 20 20 69 6e int szNew;. in
1849e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 t idx;. MemPage
1849f 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65 65 *pPage;. Btree
184a0 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 *p = pCur->pBtr
184a1 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a ee;. BtShared *
184a2 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
184a3 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6f unsigned char *o
184a4 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e ldCell;. unsign
184a5 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c 6c ed char *newCell
184a6 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 43 75 = 0;.. if( pCu
184a7 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
184a8 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 61 R_FAULT ){. a
184a9 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 ssert( pCur->ski
184aa 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b pNext!=SQLITE_OK
184ab 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 );. return p
184ac 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 Cur->skipNext;.
184ad 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 }.. assert( cu
184ae 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
184af 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
184b0 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 ( pCur->wrFlag &
184b1 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 & pBt->inTransac
184b2 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 tion==TRANS_WRIT
184b3 45 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 4f E && !pBt->readO
184b4 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nly );. assert(
184b5 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 hasSharedCacheT
184b6 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 ableLock(p, pCur
184b7 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 ->pgnoRoot, pCur
184b8 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 ->pKeyInfo!=0, 2
184b9 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 ) );.. /* Asser
184ba 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 t that the calle
184bb 72 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 69 r has been consi
184bc 73 74 65 6e 74 2e 20 49 66 20 74 68 69 73 20 63 stent. If this c
184bd 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 ursor was opened
184be 0a 20 20 2a 2a 20 65 78 70 65 63 74 69 6e 67 20 . ** expecting
184bf 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2c an index b-tree,
184c0 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 then the caller
184c1 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 should be inser
184c2 74 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a 20 6b ting blob. ** k
184c3 65 79 73 20 77 69 74 68 20 6e 6f 20 61 73 73 6f eys with no asso
184c4 63 69 61 74 65 64 20 64 61 74 61 2e 20 49 66 20 ciated data. If
184c5 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f the cursor was o
184c6 70 65 6e 65 64 20 65 78 70 65 63 74 69 6e 67 20 pened expecting
184c7 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 20 74 an. ** intkey t
184c8 61 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c 65 72 able, the caller
184c9 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 should be inser
184ca 74 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 ting integer key
184cb 73 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 62 6c s with a. ** bl
184cc 6f 62 20 6f 66 20 61 73 73 6f 63 69 61 74 65 64 ob of associated
184cd 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 73 73 data. */. ass
184ce 65 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 3d 3d ert( (pKey==0)==
184cf 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d (pCur->pKeyInfo=
184d0 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 =0) );.. /* If
184d1 74 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 72 this is an inser
184d2 74 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 62 t into a table b
184d3 2d 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 74 -tree, invalidat
184d4 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 0a e any incrblob .
184d5 20 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 ** cursors ope
184d6 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 n on the row bei
184d7 6e 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 73 ng replaced (ass
184d8 75 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 20 uming this is a
184d9 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 65 replace. ** ope
184da 72 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 69 ration - if it i
184db 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f s not, the follo
184dc 77 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29 wing is a no-op)
184dd 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 . */. if( pCur
184de 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b ->pKeyInfo==0 ){
184df 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 . invalidateI
184e0 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 ncrblobCursors(p
184e1 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a , nKey, 0);. }.
184e2 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 . /* Save the p
184e3 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 ositions of any
184e4 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 other cursors op
184e5 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 en on this table
184e6 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73 .. **. ** In s
184e7 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 63 ome cases, the c
184e8 61 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f 76 65 all to btreeMove
184e9 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 20 to() below is a
184ea 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 no-op. For. **
184eb 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e example, when in
184ec 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e 74 serting data int
184ed 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 61 o a table with a
184ee 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 6e uto-generated in
184ef 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c teger. ** keys,
184f0 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20 the VDBE layer
184f1 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 42 invokes sqlite3B
184f2 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 69 treeLast() to fi
184f3 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 20 gure out the .
184f4 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 74 ** integer key t
184f5 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 63 o use. It then c
184f6 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 alls this functi
184f7 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 69 on to actually i
184f8 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a 20 nsert the . **
184f9 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 6e data into the in
184fa 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e 20 tkey B-Tree. In
184fb 74 68 69 73 20 63 61 73 65 20 62 74 72 65 65 4d this case btreeM
184fc 6f 76 65 74 6f 28 29 20 72 65 63 6f 67 6e 69 7a oveto() recogniz
184fd 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 es. ** that the
184fe 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 cursor is alrea
184ff 64 79 20 77 68 65 72 65 20 69 74 20 6e 65 65 64 dy where it need
18500 73 20 74 6f 20 62 65 20 61 6e 64 20 72 65 74 75 s to be and retu
18501 72 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a rns without. **
18502 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 2e doing any work.
18503 20 54 6f 20 61 76 6f 69 64 20 74 68 77 61 72 74 To avoid thwart
18504 69 6e 67 20 74 68 65 73 65 20 6f 70 74 69 6d 69 ing these optimi
18505 7a 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 20 69 zations, it is i
18506 6d 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f mportant. ** no
18507 74 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 63 t to clear the c
18508 75 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 2a 2f ursor here.. */
18509 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 . rc = saveAllC
1850a 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 ursors(pBt, pCur
1850b 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 ->pgnoRoot, pCur
1850c 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
1850d 74 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 21 turn rc;. if( !
1850e 6c 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 loc ){. rc =
1850f 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 btreeMoveto(pCur
18510 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70 , pKey, nKey, ap
18511 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 3b pendBias, &loc);
18512 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
18513 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 turn rc;. }. a
18514 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
18515 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
18516 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 74 61 D || (pCur->eSta
18517 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c te==CURSOR_INVAL
18518 49 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a 0a 20 ID && loc) );..
18519 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
1851a 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
1851b 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 e];. assert( pP
1851c 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e age->intKey || n
1851d 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 Key>=0 );. asse
1851e 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 rt( pPage->leaf
1851f 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 || !pPage->intKe
18520 79 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22 y );.. TRACE(("
18521 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25 64 INSERT: table=%d
18522 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74 61 nkey=%lld ndata
18523 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c 6e =%d page=%d %s\n
18524 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 ",. pCu
18525 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 r->pgnoRoot, nKe
18526 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65 2d y, nData, pPage-
18527 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 >pgno,.
18528 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72 77 loc==0 ? "overw
18529 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e 74 rite" : "new ent
1852a 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74 28 ry"));. assert(
1852b 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 pPage->isInit )
1852c 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 ;. allocateTemp
1852d 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e 65 Space(pBt);. ne
1852e 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 6d wCell = pBt->pTm
1852f 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e 65 pSpace;. if( ne
18530 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 wCell==0 ) retur
18531 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
18532 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c rc = fillInCel
18533 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c l(pPage, newCell
18534 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 44 , pKey, nKey, pD
18535 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 72 ata, nData, nZer
18536 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69 66 o, &szNew);. if
18537 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f ( rc ) goto end_
18538 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72 74 insert;. assert
18539 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69 7a ( szNew==cellSiz
1853a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77 43 ePtr(pPage, newC
1853b 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ell) );. assert
1853c 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c 4c ( szNew<=MX_CELL
1853d 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 _SIZE(pBt) );.
1853e 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 idx = pCur->aiId
1853f 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a x[pCur->iPage];.
18540 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 7b 0a if( loc==0 ){.
18541 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 u16 szOld;.
18542 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 assert( idx<p
18543 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
18544 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
18545 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d agerWrite(pPage-
18546 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 >pDbPage);. i
18547 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 67 f( rc ){. g
18548 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a oto end_insert;.
18549 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 6c }. oldCel
1854a 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 l = findCell(pPa
1854b 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 66 ge, idx);. if
1854c 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
1854d 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e {. memcpy(n
1854e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c ewCell, oldCell,
1854f 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 4);. }. s
18550 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50 zOld = cellSizeP
18551 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c tr(pPage, oldCel
18552 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 l);. rc = cle
18553 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c arCell(pPage, ol
18554 64 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 6f 70 dCell);. drop
18555 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c Cell(pPage, idx,
18556 20 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a 20 20 szOld, &rc);.
18557 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
18558 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 7d 65 end_insert;. }e
18559 6c 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26 lse if( loc<0 &&
1855a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 pPage->nCell>0
1855b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
1855c 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 Page->leaf );.
1855d 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e idx = ++pCur->
1855e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
1855f 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 e];. }else{.
18560 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
18561 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 69 6e leaf );. }. in
18562 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 sertCell(pPage,
18563 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a idx, newCell, sz
18564 4e 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 29 3b New, 0, 0, &rc);
18565 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 . assert( rc!=S
18566 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 QLITE_OK || pPag
18567 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 50 e->nCell>0 || pP
18568 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 age->nOverflow>0
18569 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 );.. /* If no
1856a 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 error has occure
1856b 64 20 61 6e 64 20 70 50 61 67 65 20 68 61 73 20 d and pPage has
1856c 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c an overflow cell
1856d 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28 29 , call balance()
1856e 20 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73 74 . ** to redist
1856f 72 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c 73 ribute the cells
18570 20 77 69 74 68 69 6e 20 74 68 65 20 74 72 65 65 within the tree
18571 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65 28 . Since balance(
18572 29 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a 2a 20 ) may move. **
18573 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72 6f the cursor, zero
18574 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 6e the BtCursor.in
18575 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74 43 fo.nSize and BtC
18576 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79 0a ursor.validNKey.
18577 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 2e 0a ** variables..
18578 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 69 6f **. ** Previo
18579 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 us versions of S
1857a 51 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d 6f 76 QLite called mov
1857b 65 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d 6f 76 eToRoot() to mov
1857c 65 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20 2a e the cursor. *
1857d 2a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 72 6f * back to the ro
1857e 6f 74 20 70 61 67 65 20 61 73 20 62 61 6c 61 6e ot page as balan
1857f 63 65 28 29 20 75 73 65 64 20 74 6f 20 69 6e 76 ce() used to inv
18580 61 6c 69 64 61 74 65 20 74 68 65 20 63 6f 6e 74 alidate the cont
18581 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 74 43 ents. ** of BtC
18582 75 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d 20 61 ursor.apPage[] a
18583 6e 64 20 42 74 43 75 72 73 6f 72 2e 61 69 49 64 nd BtCursor.aiId
18584 78 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 x[]. Instead of
18585 64 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 2a 2a doing that,. **
18586 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20 set the cursor
18587 73 74 61 74 65 20 74 6f 20 22 69 6e 76 61 6c 69 state to "invali
18588 64 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 63 d". This makes c
18589 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f 70 65 ommon insert ope
1858a 72 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 6c 69 rations. ** sli
1858b 67 68 74 6c 79 20 66 61 73 74 65 72 2e 0a 20 20 ghtly faster..
1858c 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 69 73 **. ** There is
1858d 20 61 20 73 75 62 74 6c 65 20 62 75 74 20 69 6d a subtle but im
1858e 70 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 7a 61 portant optimiza
1858f 74 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e 20 57 tion here too. W
18590 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a 20 20 hen inserting.
18591 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 63 6f ** multiple reco
18592 72 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e 74 6b rds into an intk
18593 65 79 20 62 2d 74 72 65 65 20 75 73 69 6e 67 20 ey b-tree using
18594 61 20 73 69 6e 67 6c 65 20 63 75 72 73 6f 72 20 a single cursor
18595 28 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 61 70 (as can. ** hap
18596 70 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 65 73 pen while proces
18597 73 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 54 20 sing an "INSERT
18598 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 22 INTO ... SELECT"
18599 20 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 74 0a statement), it.
1859a 20 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 61 67 ** is advantag
1859b 65 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 74 68 eous to leave th
1859c 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e e cursor pointin
1859d 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e g to the last en
1859e 74 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 try in. ** the
1859f 62 2d 74 72 65 65 20 69 66 20 70 6f 73 73 69 62 b-tree if possib
185a0 6c 65 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f le. If the curso
185a1 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 r is left pointi
185a2 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 0a 20 ng to the last.
185a3 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ** entry in the
185a4 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 20 table, and the
185a5 6e 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74 65 next row inserte
185a6 64 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 d has an integer
185a7 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 65 72 key. ** larger
185a8 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73 than the larges
185a9 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c 20 t existing key,
185aa 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
185ab 6f 20 69 6e 73 65 72 74 20 74 68 65 0a 20 20 2a o insert the. *
185ac 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73 65 * row without se
185ad 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 eking the cursor
185ae 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 61 20 . This can be a
185af 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 big performance
185b0 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 boost.. */. pC
185b1 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d ur->info.nSize =
185b2 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 0;. pCur->vali
185b3 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 dNKey = 0;. if(
185b4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
185b5 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c & pPage->nOverfl
185b6 6f 77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 ow ){. rc = b
185b7 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 0a 20 alance(pCur);..
185b8 20 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b 65 20 /* Must make
185b9 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 20 69 sure nOverflow i
185ba 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 s reset to zero
185bb 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 6c 61 even if the bala
185bc 6e 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 61 69 nce(). ** fai
185bd 6c 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 61 74 ls. Internal dat
185be 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 a structure corr
185bf 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 uption will resu
185c0 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 0a 20 lt otherwise. .
185c1 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 74 20 ** Also, set
185c2 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 the cursor state
185c3 20 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 68 69 to invalid. Thi
185c4 73 20 73 74 6f 70 73 20 73 61 76 65 43 75 72 73 s stops saveCurs
185c5 6f 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 20 20 orPosition().
185c6 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 ** from trying
185c7 74 6f 20 73 61 76 65 20 74 68 65 20 63 75 72 72 to save the curr
185c8 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20 ent position of
185c9 74 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f 0a the cursor. */.
185ca 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 pCur->apPage
185cb 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
185cc 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 Overflow = 0;.
185cd 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d pCur->eState =
185ce 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b CURSOR_INVALID;
185cf 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 . }. assert( p
185d0 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
185d1 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 ->iPage]->nOverf
185d2 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 5f 69 low==0 );..end_i
185d3 6e 73 65 72 74 3a 0a 20 20 72 65 74 75 72 6e 20 nsert:. return
185d4 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c rc;.}../*.** Del
185d5 65 74 65 20 74 68 65 20 65 6e 74 72 79 20 74 68 ete the entry th
185d6 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 at the cursor is
185d7 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 pointing to. T
185d8 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 he cursor.** is
185d9 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
185da 20 61 20 61 72 62 69 74 72 61 72 79 20 6c 6f 63 a arbitrary loc
185db 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ation..*/.SQLITE
185dc 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
185dd 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 ite3BtreeDelete(
185de 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
185df 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 . Btree *p = pC
185e0 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 ur->pBtree;. Bt
185e1 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
185e2 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 >pBt;
185e3 20 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 . int rc;
185e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185e5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
185e6 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 4d turn code */. M
185e7 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 emPage *pPage;
185e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185e9 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 64 /* Page to d
185ea 65 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f 6d 20 elete cell from
185eb 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
185ec 61 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 ar *pCell;
185ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
185ee 6e 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f 20 nter to cell to
185ef 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 delete */. int
185f0 69 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20 20 iCellIdx;
185f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185f2 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65 6c /* Index of cel
185f3 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 l to delete */.
185f4 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68 3b int iCellDepth;
185f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185f6 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f /* Depth o
185f7 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e f node containin
185f8 67 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 20 61 g pCell */ .. a
185f9 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
185fa 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
185fb 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
185fc 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
185fd 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
185fe 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 assert( !pBt->re
185ff 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 adOnly );. asse
18600 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 rt( pCur->wrFlag
18601 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 );. assert( ha
18602 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
18603 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70 eLock(p, pCur->p
18604 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 gnoRoot, pCur->p
18605 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29 KeyInfo!=0, 2) )
18606 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73 ;. assert( !has
18607 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c ReadConflicts(p,
18608 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29 pCur->pgnoRoot)
18609 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 );.. if( NEVER
1860a 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 (pCur->aiIdx[pCu
1860b 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d r->iPage]>=pCur-
1860c 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
1860d 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20 age]->nCell) .
1860e 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e || NEVER(pCur->
1860f 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 eState!=CURSOR_V
18610 41 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 20 72 ALID). ){. r
18611 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
18612 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e OR; /* Somethin
18613 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79 2e g has gone awry.
18614 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 */. }.. /* If
18615 20 74 68 69 73 20 69 73 20 61 20 64 65 6c 65 74 this is a delet
18616 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 72 e operation to r
18617 65 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 6f 6d emove a row from
18618 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c a table b-tree,
18619 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 . ** invalidate
1861a 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 any incrblob cu
1861b 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 rsors open on th
1861c 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 e row being dele
1861d 74 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 ted. */. if( p
1861e 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 Cur->pKeyInfo==0
1861f 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 ){. invalida
18620 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 teIncrblobCursor
18621 73 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e s(p, pCur->info.
18622 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 nKey, 0);. }..
18623 20 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 70 43 iCellDepth = pC
18624 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 43 65 ur->iPage;. iCe
18625 6c 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 llIdx = pCur->ai
18626 49 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b Idx[iCellDepth];
18627 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d . pPage = pCur-
18628 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 >apPage[iCellDep
18629 74 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 th];. pCell = f
1862a 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 indCell(pPage, i
1862b 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f 2a 20 CellIdx);.. /*
1862c 49 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 If the page cont
1862d 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79 aining the entry
1862e 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e 6f to delete is no
1862f 74 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 6d t a leaf page, m
18630 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 ove. ** the cur
18631 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67 65 sor to the large
18632 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
18633 74 72 65 65 20 74 68 61 74 20 69 73 20 73 6d 61 tree that is sma
18634 6c 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20 74 ller than. ** t
18635 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 64 he entry being d
18636 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65 6c eleted. This cel
18637 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20 74 l will replace t
18638 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 he cell being de
18639 6c 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 leted. ** from
1863a 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 the internal nod
1863b 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75 73 e. The 'previous
1863c 27 20 65 6e 74 72 79 20 69 73 20 75 73 65 64 20 ' entry is used
1863d 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61 64 for this instead
1863e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 6e 65 . ** of the 'ne
1863f 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20 74 68 xt' entry, as th
18640 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 e previous entry
18641 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 61 72 is always a par
18642 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 75 t of the. ** su
18643 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 b-tree headed by
18644 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 the child page
18645 6f 66 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e of the cell bein
18646 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20 g deleted. This
18647 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c 61 6e makes. ** balan
18648 63 69 6e 67 20 74 68 65 20 74 72 65 65 20 66 6f cing the tree fo
18649 6c 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 6c 65 llowing the dele
1864a 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 61 73 te operation eas
1864b 69 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 ier. */. if( !
1864c 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
1864d 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a int notUsed;.
1864e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1864f 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 BtreePrevious(pC
18650 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 ur, ¬Used);.
18651 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
18652 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
18653 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 Save the positi
18654 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 ons of any other
18655 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e cursors open on
18656 20 74 68 69 73 20 74 61 62 6c 65 20 62 65 66 6f this table befo
18657 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 20 61 re. ** making a
18658 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 ny modifications
18659 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 67 65 20 . Make the page
1865a 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 containing the e
1865b 6e 74 72 79 20 74 6f 20 62 65 20 0a 20 20 2a 2a ntry to be . **
1865c 20 64 65 6c 65 74 65 64 20 77 72 69 74 61 62 6c deleted writabl
1865d 65 2e 20 54 68 65 6e 20 66 72 65 65 20 61 6e 79 e. Then free any
1865e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 overflow pages
1865f 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
18660 74 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 79 20 the . ** entry
18661 61 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d 6f and finally remo
18662 76 65 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 ve the cell itse
18663 6c 66 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 lf from within t
18664 68 65 20 70 61 67 65 2e 20 20 0a 20 20 2a 2f 0a he page. . */.
18665 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 rc = saveAllCu
18666 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d rsors(pBt, pCur-
18667 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 >pgnoRoot, pCur)
18668 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
18669 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 urn rc;. rc = s
1866a 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
1866b 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
1866c 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 ;. if( rc ) ret
1866d 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 63 urn rc;. rc = c
1866e 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 learCell(pPage,
1866f 70 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 65 pCell);. dropCe
18670 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 49 ll(pPage, iCellI
18671 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 dx, cellSizePtr(
18672 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 26 pPage, pCell), &
18673 72 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 rc);. if( rc )
18674 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a return rc;.. /*
18675 20 49 66 20 74 68 65 20 63 65 6c 6c 20 64 65 6c If the cell del
18676 65 74 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f 63 eted was not loc
18677 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 ated on a leaf p
18678 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 age, then the cu
18679 72 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 72 rsor. ** is cur
1867a 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 rently pointing
1867b 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 to the largest e
1867c 6e 74 72 79 20 69 6e 20 74 68 65 20 73 75 62 2d ntry in the sub-
1867d 74 72 65 65 20 68 65 61 64 65 64 0a 20 20 2a 2a tree headed. **
1867e 20 62 79 20 74 68 65 20 63 68 69 6c 64 2d 70 61 by the child-pa
1867f 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 ge of the cell t
18680 68 61 74 20 77 61 73 20 6a 75 73 74 20 64 65 6c hat was just del
18681 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 74 eted from an int
18682 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e ernal. ** node.
18683 20 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 The cell from t
18684 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 65 he leaf node nee
18685 64 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 74 ds to be moved t
18686 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20 o the internal.
18687 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 6c ** node to repl
18688 61 63 65 20 74 68 65 20 64 65 6c 65 74 65 64 20 ace the deleted
18689 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 20 cell. */. if(
1868a 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
1868b 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65 MemPage *pLe
1868c 61 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 af = pCur->apPag
1868d 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
1868e 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 int nCell;.
1868f 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72 Pgno n = pCur
18690 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 ->apPage[iCellDe
18691 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 pth+1]->pgno;.
18692 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
18693 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 6c *pTmp;.. pCel
18694 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c 65 l = findCell(pLe
18695 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c af, pLeaf->nCell
18696 2d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d -1);. nCell =
18697 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65 cellSizePtr(pLe
18698 61 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 af, pCell);.
18699 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c 5f assert( MX_CELL_
1869a 53 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 6c 6c SIZE(pBt)>=nCell
1869b 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 74 );.. allocat
1869c 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
1869d 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 2d . pTmp = pBt-
1869e 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 20 >pTmpSpace;..
1869f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
186a0 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e 70 erWrite(pLeaf->p
186a1 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 73 DbPage);. ins
186a2 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 ertCell(pPage, i
186a3 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34 CellIdx, pCell-4
186a4 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c , nCell+4, pTmp,
186a5 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 72 n, &rc);. dr
186a6 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c opCell(pLeaf, pL
186a7 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43 eaf->nCell-1, nC
186a8 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 ell, &rc);. i
186a9 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
186aa 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c c;. }.. /* Bal
186ab 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 49 ance the tree. I
186ac 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65 f the entry dele
186ad 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 20 ted was located
186ae 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c 0a on a leaf page,.
186af 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 75 ** then the cu
186b0 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e 74 rsor still point
186b1 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20 s to that page.
186b2 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
186b3 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c 6c first. ** call
186b4 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 65 to balance() re
186b5 70 61 69 72 73 20 74 68 65 20 74 72 65 65 2c 20 pairs the tree,
186b6 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 20 and the if(...)
186b7 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a condition is. *
186b8 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 20 * never true..
186b9 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 **. ** Otherwis
186ba 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 20 e, if the entry
186bb 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 61 deleted was on a
186bc 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 n internal node
186bd 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 page, then. **
186be 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 pCur is pointing
186bf 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 67 to the leaf pag
186c0 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 63 e from which a c
186c1 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 20 ell was removed
186c2 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 20 to. ** replace
186c3 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64 the cell deleted
186c4 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e from the intern
186c5 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 73 al node. This is
186c6 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 74 slightly. ** t
186c7 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 61 ricky as the lea
186c8 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 6e f node may be un
186c9 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 65 derfull, and the
186ca 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 6d internal node m
186cb 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 65 ay. ** be eithe
186cc 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 66 r under or overf
186cd 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ull. In this cas
186ce 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e 63 e run the balanc
186cf 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 ing algorithm.
186d0 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e ** on the leaf n
186d1 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 68 ode first. If th
186d2 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 65 e balance procee
186d3 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 70 ds far enough up
186d4 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 74 the. ** tree t
186d5 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 75 hat we can be su
186d6 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f 62 re that any prob
186d7 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 72 lem in the inter
186d8 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 2a nal node has. *
186d9 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64 * been corrected
186da 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65 , so be it. Othe
186db 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c rwise, after bal
186dc 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20 ancing the leaf
186dd 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20 node,. ** walk
186de 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68 the cursor up th
186df 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e e tree to the in
186e0 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20 ternal node and
186e1 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a 20 balance it as .
186e2 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20 ** well. */.
186e3 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 rc = balance(pCu
186e4 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 r);. if( rc==SQ
186e5 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 2d LITE_OK && pCur-
186e6 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 74 >iPage>iCellDept
186e7 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 h ){. while(
186e8 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c pCur->iPage>iCel
186e9 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 lDepth ){.
186ea 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 releasePage(pCur
186eb 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
186ec 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a Page--]);. }.
186ed 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 rc = balance
186ee 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 (pCur);. }.. i
186ef 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
186f0 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f ){. moveToRo
186f1 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 ot(pCur);. }.
186f2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
186f3 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
186f4 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 BTree table. W
186f5 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 rite into *piTab
186f6 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e le the page.** n
186f7 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f umber for the ro
186f8 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e ot page of the n
186f9 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ew table..**.**
186fa 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 The type of type
186fb 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 is determined b
186fc 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 y the flags para
186fd 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 meter. Only the
186fe 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 .** following va
186ff 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 lues of flags ar
18700 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 e currently in u
18701 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 se. Other value
18702 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d s for.** flags m
18703 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a ight not work:.*
18704 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 *.** BTREE_I
18705 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 NTKEY|BTREE_LEAF
18706 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f DATA Used fo
18707 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 r SQL tables wit
18708 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 h rowid keys.**
18709 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 BTREE_ZERODA
1870a 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TA
1870b 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c Used for SQL
1870c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 indices.*/.stat
1870d 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 ic int btreeCrea
1870e 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 teTable(Btree *p
1870f 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 , int *piTable,
18710 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 int flags){. Bt
18711 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
18712 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 >pBt;. MemPage
18713 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 *pRoot;. Pgno p
18714 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 gnoRoot;. int r
18715 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 c;.. assert( sq
18716 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
18717 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 utex(p) );. ass
18718 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
18719 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
1871a 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 RITE );. assert
1871b 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 ( !pBt->readOnly
1871c 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 );..#ifdef SQLI
1871d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
1871e 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 UM. rc = alloca
1871f 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
18720 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f &pRoot, &pgnoRo
18721 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 ot, 1, 0);. if(
18722 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 rc ){. retur
18723 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a n rc;. }.#else.
18724 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
18725 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e acuum ){. Pgn
18726 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 o pgnoMove;
18727 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 /* Move a page
18728 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f here to make roo
18729 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 m for the root-p
1872a 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 age */. MemPa
1872b 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f ge *pPageMove; /
1872c 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f * The page to mo
1872d 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f ve to. */.. /
1872e 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 * Creating a new
1872f 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 table may proba
18730 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 bly require movi
18731 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 ng an existing d
18732 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 atabase. ** t
18733 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 o make room for
18734 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 the new tables r
18735 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 oot page. In cas
18736 65 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e e this page turn
18737 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 s. ** out to
18738 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 be an overflow p
18739 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 age, delete all
1873a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 overflow page-ma
1873b 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 p caches. **
1873c 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 held by open cur
1873d 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 sors.. */.
1873e 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 invalidateAllOv
1873f 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 erflowCache(pBt)
18740 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 ;.. /* Read t
18741 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 he value of meta
18742 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 [3] from the dat
18743 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 abase to determi
18744 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 ne where the.
18745 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 ** root page of
18746 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 the new table s
18747 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 hould go. meta[3
18748 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 ] is the largest
18749 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a root-page. *
1874a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 * created so far
1874b 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f , so the new roo
1874c 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b t-page is (meta[
1874d 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 3]+1).. */.
1874e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 sqlite3BtreeGe
1874f 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c tMeta(p, BTREE_L
18750 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 ARGEST_ROOT_PAGE
18751 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 , &pgnoRoot);.
18752 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 pgnoRoot++;..
18753 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f /* The new ro
18754 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 ot-page may not
18755 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 be allocated on
18756 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 a pointer-map pa
18757 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a ge, or the. *
18758 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 * PENDING_BYTE p
18759 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 age.. */.
1875a 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d while( pgnoRoot=
1875b 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 =PTRMAP_PAGENO(p
1875c 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c Bt, pgnoRoot) ||
1875d 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f . pgnoRoo
1875e 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t==PENDING_BYTE_
1875f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
18760 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 pgnoRoot++;.
18761 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
18762 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a pgnoRoot>=3 );.
18763 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 . /* Allocate
18764 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 a page. The pag
18765 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 e that currently
18766 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f resides at pgno
18767 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a Root will. **
18768 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 be moved to the
18769 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 allocated page
1876a 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f (unless the allo
1876b 63 61 74 65 64 20 70 61 67 65 20 68 61 70 70 65 cated page happe
1876c 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 ns. ** to res
1876d 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 ide at pgnoRoot)
1876e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
1876f 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 = allocateBtreeP
18770 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d age(pBt, &pPageM
18771 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 ove, &pgnoMove,
18772 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 pgnoRoot, 1);.
18773 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18774 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
18775 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 urn rc;. }..
18776 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 if( pgnoMove!
18777 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 =pgnoRoot ){.
18778 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 /* pgnoRoot i
18779 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 s the page that
1877a 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 will be used for
1877b 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f the root-page o
1877c 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e f. ** the n
1877d 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 ew table (assumi
1877e 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 ng an error did
1877f 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 not occur). But
18780 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a we were. **
18781 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d allocated pgnoM
18782 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 ove. If required
18783 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 (i.e. if it was
18784 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 not allocated.
18785 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e ** by exten
18786 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 ding the file),
18787 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 the current page
18788 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e at position pgn
18789 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 oMove. ** i
1878a 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 s already journa
1878b 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 led.. */.
1878c 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 30 u8 eType = 0
1878d 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74 ;. Pgno iPt
1878e 72 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 rPage = 0;..
1878f 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
18790 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 20 20 ageMove);..
18791 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 /* Move the pag
18792 65 20 63 75 72 72 65 6e 74 6c 79 20 61 74 20 70 e currently at p
18793 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d gnoRoot to pgnoM
18794 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 ove. */. rc
18795 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
18796 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 pBt, pgnoRoot, &
18797 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 pRoot, 0);.
18798 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18799 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
1879a 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
1879b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d . rc = ptrm
1879c 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 apGet(pBt, pgnoR
1879d 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50 oot, &eType, &iP
1879e 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 trPage);. i
1879f 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 f( eType==PTRMAP
187a0 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 _ROOTPAGE || eTy
187a1 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 pe==PTRMAP_FREEP
187a2 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 AGE ){. r
187a3 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
187a4 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d PT_BKPT;. }
187a5 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 . if( rc!=S
187a6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
187a7 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
187a8 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 pRoot);.
187a9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
187aa 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
187ab 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 eType!=PTRMAP_R
187ac 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20 20 OOTPAGE );.
187ad 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d assert( eType!=
187ae 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 PTRMAP_FREEPAGE
187af 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 );. rc = re
187b0 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 locatePage(pBt,
187b1 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 50 pRoot, eType, iP
187b2 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 trPage, pgnoMove
187b3 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 , 0);. rele
187b4 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a asePage(pRoot);.
187b5 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e . /* Obtain
187b6 20 74 68 65 20 70 61 67 65 20 61 74 20 70 67 6e the page at pgn
187b7 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 oRoot */. i
187b8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
187b9 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
187ba 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
187bb 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 rc = btreeG
187bc 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f etPage(pBt, pgno
187bd 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 Root, &pRoot, 0)
187be 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
187bf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
187c0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
187c1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
187c2 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
187c3 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 rite(pRoot->pDbP
187c4 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 age);. if(
187c5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
187c6 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
187c7 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 Page(pRoot);.
187c8 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
187c9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
187ca 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d e{. pRoot =
187cb 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20 pPageMove;.
187cc 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 } .. /* Updat
187cd 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 e the pointer-ma
187ce 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20 p and meta-data
187cf 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f with the new roo
187d0 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a t-page number. *
187d1 2f 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 /. ptrmapPut(
187d2 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 50 pBt, pgnoRoot, P
187d3 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 TRMAP_ROOTPAGE,
187d4 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 0, &rc);. if(
187d5 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c rc ){. rel
187d6 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
187d7 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
187d8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
187d9 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 sqlite3BtreeUpd
187da 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67 ateMeta(p, 4, pg
187db 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 noRoot);. if(
187dc 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c rc ){. rel
187dd 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
187de 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
187df 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 ;. }.. }else
187e0 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 {. rc = alloc
187e1 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 ateBtreePage(pBt
187e2 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 , &pRoot, &pgnoR
187e3 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 oot, 1, 0);.
187e4 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 if( rc ) return
187e5 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 rc;. }.#endif.
187e6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
187e7 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
187e8 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 (pRoot->pDbPage)
187e9 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 );. zeroPage(p
187ea 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54 Root, flags | PT
187eb 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74 F_LEAF);. sqlit
187ec 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f e3PagerUnref(pRo
187ed 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ot->pDbPage);.
187ee 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 *piTable = (int)
187ef 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 pgnoRoot;. retu
187f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
187f1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
187f2 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
187f3 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 reateTable(Btree
187f4 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c *p, int *piTabl
187f5 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 e, int flags){.
187f6 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 int rc;. sqlit
187f7 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
187f8 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 65 . rc = btreeCre
187f9 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 61 ateTable(p, piTa
187fa 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 73 ble, flags);. s
187fb 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
187fc 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
187fd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 ;.}../*.** Erase
187fe 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 the given datab
187ff 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c ase page and all
18800 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 20 its children.
18801 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 61 Return.** the pa
18802 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 ge to the freeli
18803 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e st..*/.static in
18804 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 t clearDatabaseP
18805 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 age(. BtShared
18806 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 *pBt,
18807 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 61 /* The BTree tha
18808 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 t contains the t
18809 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 able */. Pgno p
1880a 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 gno,
1880b 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 /* Page number t
1880c 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 6e 74 o clear */. int
1880d 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c 20 20 freePageFlag,
1880e 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65 /* Deallocate
1880f 20 70 61 67 65 20 69 66 20 74 72 75 65 20 2a 2f page if true */
18810 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65 . int *pnChange
18811 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 .){. MemPage *p
18812 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a Page;. int rc;.
18813 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
18814 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b *pCell;. int i;
18815 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
18816 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
18817 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
18818 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50 61 if( pgno>pagerPa
18819 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a gecount(pBt) ){.
1881a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1881b 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
1881c 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 41 }.. rc = getA
1881d 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 ndInitPage(pBt,
1881e 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0a 20 pgno, &pPage);.
1881f 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
18820 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 rc;. for(i=0;
18821 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 i<pPage->nCell;
18822 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c 20 i++){. pCell
18823 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 = findCell(pPage
18824 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21 70 , i);. if( !p
18825 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
18826 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61 rc = clearDa
18827 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20 tabasePage(pBt,
18828 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c get4byte(pCell),
18829 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 1, pnChange);.
1882a 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f if( rc ) go
1882b 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 to cleardatabase
1882c 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a page_out;. }.
1882d 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65 rc = clearCe
1882e 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 ll(pPage, pCell)
1882f 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
18830 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 oto cleardatabas
18831 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 epage_out;. }.
18832 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
18833 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c f ){. rc = cl
18834 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 earDatabasePage(
18835 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 70 pBt, get4byte(&p
18836 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d 29 2c Page->aData[8]),
18837 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 1, pnChange);.
18838 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
18839 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 cleardatabasepa
1883a 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20 ge_out;. }else
1883b 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 7b 0a if( pnChange ){.
1883c 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
1883d 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 e->intKey );.
1883e 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70 50 *pnChange += pP
1883f 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 7d 0a age->nCell;. }.
18840 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46 6c if( freePageFl
18841 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 50 61 ag ){. freePa
18842 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a ge(pPage, &rc);.
18843 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 }else if( (rc
18844 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
18845 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
18846 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 7a ge))==0 ){. z
18847 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 70 eroPage(pPage, p
18848 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 7c Page->aData[0] |
18849 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 7d 0a PTF_LEAF);. }.
1884a 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 .cleardatabasepa
1884b 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61 73 ge_out:. releas
1884c 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
1884d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1884e 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69 .** Delete all i
1884f 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 nformation from
18850 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 a single table i
18851 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
18852 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 iTable is.** th
18853 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
18854 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
18855 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 20 74 table. After t
18856 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
18857 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 rns,.** the root
18858 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c 20 page is empty,
18859 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74 73 but still exists
1885a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1885b 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77 tine will fail w
1885c 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 ith SQLITE_LOCKE
1885d 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 D if there are a
1885e 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 20 ny open.** read
1885f 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 cursors on the t
18860 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 69 74 able. Open writ
18861 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 6d 6f e cursors are mo
18862 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 72 6f ved to the.** ro
18863 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e ot of the table.
18864 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 61 6e .**.** If pnChan
18865 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 ge is not NULL,
18866 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 62 6c then table iTabl
18867 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 6e 74 e must be an int
18868 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 0a 2a key table. The.*
18869 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 * integer value
1886a 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 6e pointed to by pn
1886b 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 65 6d Change is increm
1886c 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d ented by the num
1886d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 65 ber of.** entrie
1886e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a s in the table..
1886f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18870 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
18871 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72 eeClearTable(Btr
18872 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c ee *p, int iTabl
18873 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65 e, int *pnChange
18874 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 ){. int rc;. B
18875 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
18876 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 ->pBt;. sqlite3
18877 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
18878 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
18879 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
1887a 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 );.. /* Invali
1887b 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f date all incrblo
1887c 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f b cursors open o
1887d 6e 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 28 n table iTable (
1887e 61 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65 0a assuming iTable.
1887f 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f 74 ** is the root
18880 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74 72 of a table b-tr
18881 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f ee - if it is no
18882 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t, the following
18883 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a 20 61 20 call is. ** a
18884 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 6e no-op). */. in
18885 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 validateIncrblob
18886 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 29 Cursors(p, 0, 1)
18887 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c ;.. rc = saveAl
18888 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 28 50 lCursors(pBt, (P
18889 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 29 3b 0a gno)iTable, 0);.
1888a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
1888b 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 =rc ){. rc =
1888c 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67 clearDatabasePag
1888d 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 e(pBt, (Pgno)iTa
1888e 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65 ble, 0, pnChange
1888f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
18890 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
18891 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
18892 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69 *.** Erase all i
18893 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 nformation in a
18894 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68 table and add th
18895 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 e root of the ta
18896 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72 ble to.** the fr
18897 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c eelist. Except,
18898 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 the root of the
18899 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65 principle table
1889a 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 (the one on.**
1889b 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72 page 1) is never
1889c 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72 added to the fr
1889d 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 eelist..**.** Th
1889e 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
1889f 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 fail with SQLITE
188a0 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 _LOCKED if there
188a1 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a are any open.**
188a2 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 cursors on the
188a3 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 table..**.** If
188a4 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e AUTOVACUUM is en
188a5 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61 abled and the pa
188a6 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 20 ge at iTable is
188a7 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 not the last.**
188a8 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65 root page in the
188a9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
188aa 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f then the last ro
188ab 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74 ot page .** in t
188ac 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
188ad 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 is moved into t
188ae 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 he slot formerly
188af 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20 occupied by.**
188b0 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20 iTable and that
188b1 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72 last slot former
188b2 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 74 ly occupied by t
188b3 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 he last root pag
188b4 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f e.** is added to
188b5 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e the freelist in
188b6 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e stead of iTable.
188b7 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61 In this say, a
188b8 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 ll.** root pages
188b9 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 65 are kept at the
188ba 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
188bb 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c e database file,
188bc 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63 which.** is nec
188bd 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56 essary for AUTOV
188be 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69 ACUUM to work ri
188bf 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69 ght. *piMoved i
188c0 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a s set to the .**
188c1 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61 page number tha
188c2 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 t used to be the
188c3 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 last root page
188c4 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f in the file befo
188c5 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20 re.** the move.
188c6 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73 If no page gets
188c7 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64 moved, *piMoved
188c8 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a is set to 0..**
188c9 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 The last root p
188ca 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 20 age is recorded
188cb 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74 in meta[3] and t
188cc 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d he value of.** m
188cd 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65 eta[3] is update
188ce 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 64 d by this proced
188cf 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ure..*/.static i
188d0 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c nt btreeDropTabl
188d1 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f e(Btree *p, Pgno
188d2 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 iTable, int *pi
188d3 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 Moved){. int rc
188d4 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 ;. MemPage *pPa
188d5 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 ge = 0;. BtShar
188d6 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
188d7 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
188d8 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
188d9 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
188da 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d rt( p->inTrans==
188db 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a TRANS_WRITE );..
188dc 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 /* It is illeg
188dd 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62 al to drop a tab
188de 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72 le if any cursor
188df 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 s are open on th
188e0 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e e. ** database.
188e1 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 This is because
188e2 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 in auto-vacuum
188e3 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64 mode the backend
188e4 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 may. ** need t
188e5 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72 o move another r
188e6 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c oot-page to fill
188e7 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74 a gap left by t
188e8 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 he deleted. **
188e9 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e root page. If an
188ea 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73 open cursor was
188eb 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65 using this page
188ec 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 a problem would
188ed 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 . ** occur..
188ee 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 65 72 72 **. ** This err
188ef 6f 72 20 69 73 20 63 61 75 67 68 74 20 6c 6f 6e or is caught lon
188f0 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f 6c g before control
188f1 20 72 65 61 63 68 65 73 20 74 68 69 73 20 70 6f reaches this po
188f2 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 int.. */. if(
188f3 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72 73 NEVER(pBt->pCurs
188f4 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 or) ){. sqlit
188f5 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 e3ConnectionBloc
188f6 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e ked(p->db, pBt->
188f7 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d pCursor->pBtree-
188f8 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e >db);. return
188f9 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 SQLITE_LOCKED_S
188fa 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a HAREDCACHE;. }.
188fb 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 . rc = btreeGet
188fc 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 Page(pBt, (Pgno)
188fd 69 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c 20 iTable, &pPage,
188fe 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 0);. if( rc ) r
188ff 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d eturn rc;. rc =
18900 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
18901 61 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c arTable(p, iTabl
18902 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20 e, 0);. if( rc
18903 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 ){. releasePa
18904 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72 ge(pPage);. r
18905 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 eturn rc;. }..
18906 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a *piMoved = 0;..
18907 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29 if( iTable>1 )
18908 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
18909 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
1890a 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 freePage(pPa
1890b 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 72 65 ge, &rc);. re
1890c 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
1890d 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 ;.#else. if(
1890e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
1890f 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61 ){. Pgno ma
18910 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 xRootPgno;.
18911 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
18912 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c 41 Meta(p, BTREE_LA
18913 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c RGEST_ROOT_PAGE,
18914 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a &maxRootPgno);.
18915 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 6c . if( iTabl
18916 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29 e==maxRootPgno )
18917 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 {. /* If
18918 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 the table being
18919 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 74 dropped is the t
1891a 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61 able with the la
1891b 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a rgest root-page.
1891c 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 ** numbe
1891d 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 r in the databas
1891e 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20 e, put the root
1891f 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65 page on the free
18920 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20 list. .
18921 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65 50 */. freeP
18922 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b age(pPage, &rc);
18923 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
18924 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
18925 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
18926 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18927 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18928 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
18929 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
1892a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 The table being
1892b 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f dropped does no
1892c 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 65 t have the large
1892d 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 st root-page.
1892e 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 ** number i
1892f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
18930 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65 So move the page
18931 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20 that does into
18932 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
18933 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 gap left by the
18934 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67 deleted root-pag
18935 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
18936 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
18937 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65 Move;. re
18938 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
18939 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 ;. rc = b
1893a 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
1893b 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 maxRootPgno, &p
1893c 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Move, 0);.
1893d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1893e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1893f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
18940 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 }. rc
18941 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 = relocatePage(
18942 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d pBt, pMove, PTRM
18943 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 AP_ROOTPAGE, 0,
18944 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 20 iTable, 0);.
18945 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
18946 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20 pMove);.
18947 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
18948 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
18949 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
1894a 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 6f 76 }. pMov
1894b 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72 e = 0;. r
1894c 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
1894d 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e (pBt, maxRootPgn
1894e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 o, &pMove, 0);.
1894f 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28 freePage(
18950 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a 20 20 20 pMove, &rc);.
18951 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
18952 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 (pMove);.
18953 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
18954 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
18955 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
18956 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 69 }. *pi
18957 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 50 Moved = maxRootP
18958 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 gno;. }..
18959 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e /* Set the n
1895a 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 67 ew 'max-root-pag
1895b 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 e' value in the
1895c 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e database header.
1895d 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 This. ** i
1895e 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 s the old value
1895f 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 6f less one, less o
18960 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 20 ne more if that
18961 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 20 happens to.
18962 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 61 ** be a root-pa
18963 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20 ge number, less
18964 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 61 one again if tha
18965 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a t is the. *
18966 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 * PENDING_BYTE_P
18967 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 AGE.. */.
18968 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d maxRootPgno-
18969 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 -;. while(
1896a 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e maxRootPgno==PEN
1896b 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
1896c 42 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 Bt).
1896d 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41 47 || PTRMAP_ISPAG
1896e 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 E(pBt, maxRootPg
1896f 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d no) ){. m
18970 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 axRootPgno--;.
18971 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 }. asse
18972 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21 rt( maxRootPgno!
18973 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
18974 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 GE(pBt) );..
18975 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
18976 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c reeUpdateMeta(p,
18977 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 4, maxRootPgno)
18978 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
18979 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 67 freePage(pPag
1897a 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 72 e, &rc);. r
1897b 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
1897c 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
1897d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1897e 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 If sqlite3BtreeD
1897f 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61 6c ropTable was cal
18980 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 0a 20 led on page 1..
18981 20 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c 6c ** This reall
18982 79 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20 68 y never should h
18983 61 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e 20 appen except in
18984 61 20 63 6f 72 72 75 70 74 0a 20 20 20 20 2a 2a a corrupt. **
18985 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 20 20 database. .
18986 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 */. zeroPage(
18987 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b 45 pPage, PTF_INTKE
18988 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 20 Y|PTF_LEAF );.
18989 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
1898a 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 age);. }. retu
1898b 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 rn rc; .}.SQLIT
1898c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1898d 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 lite3BtreeDropTa
1898e 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ble(Btree *p, in
1898f 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 t iTable, int *p
18990 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 iMoved){. int r
18991 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 c;. sqlite3Btre
18992 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 eEnter(p);. rc
18993 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65 = btreeDropTable
18994 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f (p, iTable, piMo
18995 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 ved);. sqlite3B
18996 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
18997 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
18998 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
18999 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 on may only be c
1899a 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d 74 alled if the b-t
1899b 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 ree connection a
1899c 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 20 61 20 lready.** has a
1899d 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 read or write tr
1899e 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f ansaction open o
1899f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a n the database..
189a0 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d **.** Read the m
189a1 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 eta-information
189a2 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 out of a databas
189a3 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d e file. Meta[0]
189a4 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 .** is the numbe
189a5 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20 r of free pages
189a6 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 currently in the
189a7 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 database. Meta
189a8 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d [1].** through m
189a9 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69 eta[15] are avai
189aa 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79 lable for use by
189ab 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20 higher layers.
189ac 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 Meta[0].** is r
189ad 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 ead-only, the ot
189ae 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 72 hers are read/wr
189af 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 ite..** .** The
189b0 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d schema layer num
189b1 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73 bers meta values
189b2 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41 differently. A
189b3 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20 t the schema.**
189b4 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 53 layer (and the S
189b5 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61 etCookie and Rea
189b6 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29 dCookie opcodes)
189b7 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
189b8 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73 20 * free pages is
189b9 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f not visible. So
189ba 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68 Cookie[0] is th
189bb 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31 e same as Meta[1
189bc 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 ]..*/.SQLITE_PRI
189bd 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
189be 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74 3BtreeGetMeta(Bt
189bf 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c ree *p, int idx,
189c0 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20 u32 *pMeta){.
189c1 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
189c2 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 p->pBt;.. sqlit
189c3 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
189c4 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
189c5 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 Trans>TRANS_NONE
189c6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 );. assert( SQ
189c7 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 LITE_OK==querySh
189c8 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
189c9 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f ck(p, MASTER_ROO
189ca 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20 29 3b T, READ_LOCK) );
189cb 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
189cc 70 50 61 67 65 31 20 29 3b 0a 20 20 61 73 73 65 pPage1 );. asse
189cd 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 rt( idx>=0 && id
189ce 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a 70 4d 65 x<=15 );.. *pMe
189cf 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 ta = get4byte(&p
189d0 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
189d1 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a a[36 + idx*4]);.
189d2 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 2d 76 61 . /* If auto-va
189d3 63 75 75 6d 20 69 73 20 64 69 73 61 62 6c 65 64 cuum is disabled
189d4 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20 61 in this build a
189d5 6e 64 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 nd this is an au
189d6 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a 2a 20 64 to-vacuum. ** d
189d7 61 74 61 62 61 73 65 2c 20 6d 61 72 6b 20 74 68 atabase, mark th
189d8 65 20 64 61 74 61 62 61 73 65 20 61 73 20 72 65 e database as re
189d9 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a 23 69 66 ad-only. */.#if
189da 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
189db 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 AUTOVACUUM. if(
189dc 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c 41 52 47 idx==BTREE_LARG
189dd 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 26 26 EST_ROOT_PAGE &&
189de 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 2d *pMeta>0 ) pBt-
189df 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 >readOnly = 1;.#
189e0 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
189e1 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 7d BtreeLeave(p);.}
189e2 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65 ../*.** Write me
189e3 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 ta-information b
189e4 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ack into the dat
189e5 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20 abase. Meta[0]
189e6 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 is.** read-only
189e7 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77 and may not be w
189e8 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ritten..*/.SQLIT
189e9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
189ea 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 lite3BtreeUpdate
189eb 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69 Meta(Btree *p, i
189ec 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74 nt idx, u32 iMet
189ed 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a a){. BtShared *
189ee 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
189ef 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
189f0 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 P1;. int rc;.
189f1 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26 assert( idx>=1 &
189f2 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73 & idx<=15 );. s
189f3 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
189f4 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
189f5 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
189f6 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 _WRITE );. asse
189f7 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 21 rt( pBt->pPage1!
189f8 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 42 =0 );. pP1 = pB
189f9 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
189fa 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
189fb 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
189fc 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
189fd 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
189fe 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 74 TE_OK ){. put
189ff 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20 4byte(&pP1[36 +
18a00 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a idx*4], iMeta);.
18a01 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
18a02 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
18a03 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45 if( idx==BTRE
18a04 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 7b E_INCR_VACUUM ){
18a05 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
18a06 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c Bt->autoVacuum |
18a07 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20 | iMeta==0 );.
18a08 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74 assert( iMet
18a09 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 a==0 || iMeta==1
18a0a 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 );. pBt->i
18a0b 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 29 ncrVacuum = (u8)
18a0c 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 6e iMeta;. }.#en
18a0d 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 dif. }. sqlite
18a0e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
18a0f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
18a10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
18a11 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f MIT_BTREECOUNT./
18a12 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
18a13 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69 rgument, pCur, i
18a14 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 s a cursor opene
18a15 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65 d on some b-tree
18a16 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e . Count the.** n
18a17 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
18a18 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61 in the b-tree a
18a19 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73 nd write the res
18a1a 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e ult to *pnEntry.
18a1b 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
18a1c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
18a1d 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 the operation is
18a1e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78 successfully ex
18a1f 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 ecuted. .** Othe
18a20 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 rwise, if an err
18a21 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 or is encountere
18a22 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72 d (i.e. an IO er
18a23 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a ror or database.
18a24 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61 ** corruption) a
18a25 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 n SQLite error c
18a26 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
18a27 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18a28 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
18a29 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f reeCount(BtCurso
18a2a 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e r *pCur, i64 *pn
18a2b 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45 Entry){. i64 nE
18a2c 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 ntry = 0;
18a2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18a2e 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 * Value to retur
18a2f 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f n in *pnEntry */
18a30 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
18a31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18a32 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
18a33 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 3d n code */. rc =
18a34 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 moveToRoot(pCur
18a35 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 );.. /* Unless
18a36 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
18a37 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c the following l
18a38 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65 oop runs one ite
18a39 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a ration for each.
18a3a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 ** page in the
18a3b 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 B-Tree structur
18a3c 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 e (not including
18a3d 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29 overflow pages)
18a3e 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 . . */. while(
18a3f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
18a40 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 78 3b 20 {. int iIdx;
18a41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18a42 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
18a43 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20 x of child node
18a44 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 20 in parent */.
18a45 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
18a46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18a47 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70 /* Current p
18a48 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 age of the b-tre
18a49 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 e */.. /* If
18a4a 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20 70 this is a leaf p
18a4b 61 67 65 20 6f 72 20 74 68 65 20 74 72 65 65 20 age or the tree
18a4c 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b 65 is not an int-ke
18a4d 79 20 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 20 y tree, then .
18a4e 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65 20 63 ** this page c
18a4f 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 6c ontains countabl
18a50 65 20 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 65 e entries. Incre
18a51 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 79 20 63 ment the entry c
18a52 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 63 ounter. ** ac
18a53 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 2a cordingly.. *
18a54 2f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 /. pPage = pC
18a55 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
18a56 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 >iPage];. if(
18a57 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 pPage->leaf ||
18a58 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 !pPage->intKey )
18a59 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 79 20 2b {. nEntry +
18a5a 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
18a5b 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 50 }.. /* pP
18a5c 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f age is a leaf no
18a5d 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61 de. This loop na
18a5e 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72 73 vigates the curs
18a5f 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0a 20 or so that it .
18a60 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 ** points to
18a61 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72 69 the first interi
18a62 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74 20 or cell that it
18a63 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 61 points to the pa
18a64 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74 rent of. ** t
18a65 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 he next page in
18a66 74 68 65 20 74 72 65 65 20 74 68 61 74 20 68 61 the tree that ha
18a67 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 76 s not yet been v
18a68 69 73 69 74 65 64 2e 20 54 68 65 0a 20 20 20 20 isited. The.
18a69 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 ** pCur->aiIdx[p
18a6a 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c 75 Cur->iPage] valu
18a6b 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 e is set to the
18a6c 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72 index of the par
18a6d 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 ent cell. **
18a6e 6f 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 of the page, or
18a6f 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
18a70 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61 cells in the pa
18a71 67 65 20 69 66 20 74 68 65 20 6e 65 78 74 20 70 ge if the next p
18a72 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 69 age. ** to vi
18a73 73 69 74 20 69 73 20 74 68 65 20 72 69 67 68 74 sit is the right
18a74 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 -child of its pa
18a75 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 rent.. **.
18a76 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 73 ** If all pages
18a77 20 69 6e 20 74 68 65 20 74 72 65 65 20 68 61 76 in the tree hav
18a78 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 2c 20 e been visited,
18a79 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
18a7a 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 to the. ** c
18a7b 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 aller.. */.
18a7c 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 if( pPage->lea
18a7d 66 20 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b 0a f ){. do {.
18a7e 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72 if( pCur
18a7f 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 ->iPage==0 ){.
18a80 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70 /* All p
18a81 61 67 65 73 20 6f 66 20 74 68 65 20 62 2d 74 72 ages of the b-tr
18a82 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 ee have been vis
18a83 69 74 65 64 2e 20 52 65 74 75 72 6e 20 73 75 63 ited. Return suc
18a84 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20 cessfully. */.
18a85 20 20 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79 *pnEntry
18a86 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 20 = nEntry;.
18a87 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
18a88 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d TE_OK;. }
18a89 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 . moveToP
18a8a 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 arent(pCur);.
18a8b 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 72 }while ( pCur
18a8c 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
18a8d 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 61 age]>=pCur->apPa
18a8e 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d ge[pCur->iPage]-
18a8f 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 20 >nCell );..
18a90 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
18a91 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 20 r->iPage]++;.
18a92 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d pPage = pCur-
18a93 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
18a94 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 age];. }..
18a95 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 74 /* Descend to t
18a96 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f 66 he child node of
18a97 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 74 the cell that t
18a98 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e he cursor curren
18a99 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e tly . ** poin
18a9a 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 74 ts at. This is t
18a9b 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69 he right-child i
18a9c 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e f (iIdx==pPage->
18a9d 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20 nCell).. */.
18a9e 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e iIdx = pCur->
18a9f 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
18aa0 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 78 e];. if( iIdx
18aa1 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 ==pPage->nCell )
18aa2 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 {. rc = mov
18aa3 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 eToChild(pCur, g
18aa4 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
18aa5 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
18aa6 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 Offset+8]));.
18aa7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
18aa8 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 = moveToChild(p
18aa9 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 Cur, get4byte(fi
18aaa 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49 ndCell(pPage, iI
18aab 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d dx)));. }. }
18aac 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 .. /* An error
18aad 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 65 has occurred. Re
18aae 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f turn an error co
18aaf 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 de. */. return
18ab0 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a rc;.}.#endif../*
18ab1 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 .** Return the p
18ab2 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 ager associated
18ab3 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 54 with a BTree. T
18ab4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
18ab5 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 sed for.** testi
18ab6 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ng and debugging
18ab7 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 only..*/.SQLITE
18ab8 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20 2a _PRIVATE Pager *
18ab9 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
18aba 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72 r(Btree *p){. r
18abb 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 eturn p->pBt->pP
18abc 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 ager;.}..#ifndef
18abd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
18abe 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a EGRITY_CHECK./*.
18abf 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73 ** Append a mess
18ac0 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72 age to the error
18ac1 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e message string.
18ac2 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
18ac3 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a checkAppendMsg(.
18ac4 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 IntegrityCk *p
18ac5 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a Check,. char *z
18ac6 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 Msg1,. const ch
18ac7 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e ar *zFormat,. .
18ac8 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
18ac9 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63 ap;. if( !pChec
18aca 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72 k->mxErr ) retur
18acb 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45 n;. pCheck->mxE
18acc 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e rr--;. pCheck->
18acd 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 nErr++;. va_sta
18ace 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b rt(ap, zFormat);
18acf 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 . if( pCheck->e
18ad0 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20 rrMsg.nChar ){.
18ad1 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
18ad2 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b umAppend(&pCheck
18ad3 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 ->errMsg, "\n",
18ad4 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d 1);. }. if( zM
18ad5 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 sg1 ){. sqlit
18ad6 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
18ad7 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 (&pCheck->errMsg
18ad8 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20 , zMsg1, -1);.
18ad9 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 }. sqlite3VXPri
18ada 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 ntf(&pCheck->err
18adb 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c Msg, 1, zFormat,
18adc 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
18add 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b p);. if( pCheck
18ade 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 ->errMsg.mallocF
18adf 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68 ailed ){. pCh
18ae0 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 eck->mallocFaile
18ae1 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e d = 1;. }.}.#en
18ae2 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
18ae3 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
18ae4 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 CK */..#ifndef S
18ae5 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
18ae6 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a RITY_CHECK./*.**
18ae7 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65 Add 1 to the re
18ae8 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f ference count fo
18ae9 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49 r page iPage. I
18aea 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65 f this is the se
18aeb 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 cond.** referenc
18aec 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61 e to the page, a
18aed 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 dd an error mess
18aee 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a age to pCheck->z
18aef 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72 ErrMsg..** Retur
18af0 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65 n 1 if there are
18af1 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65 2 ore more refe
18af2 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 rences to the pa
18af3 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69 ge and 0 if.** i
18af4 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 f this is the fi
18af5 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f rst reference to
18af6 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
18af7 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74 Also check that
18af8 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
18af9 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a is in bounds..*
18afa 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 /.static int che
18afb 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 43 ckRef(IntegrityC
18afc 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20 k *pCheck, Pgno
18afd 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f iPage, char *zCo
18afe 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50 ntext){. if( iP
18aff 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 age==0 ) return
18b00 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70 1;. if( iPage>p
18b01 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a Check->nPage ){.
18b02 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18b03 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
18b04 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61 ext, "invalid pa
18b05 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69 ge number %d", i
18b06 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Page);. retur
18b07 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 n 1;. }. if( p
18b08 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 Check->anRef[iPa
18b09 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68 ge]==1 ){. ch
18b0a 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18b0b 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 eck, zContext, "
18b0c 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 6f 2nd reference to
18b0d 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 page %d", iPage
18b0e 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b );. return 1;
18b0f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 28 . }. return (
18b10 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 pCheck->anRef[iP
18b11 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 age]++)>1;.}..#i
18b12 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18b13 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a T_AUTOVACUUM./*.
18b14 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 ** Check that th
18b15 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 e entry in the p
18b16 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 70 ointer-map for p
18b17 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 20 age iChild maps
18b18 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 72 to .** page iPar
18b19 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 70 ent, pointer typ
18b1a 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e 6f e ptrType. If no
18b1b 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 72 t, append an err
18b1c 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f or message.** to
18b1d 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 pCheck..*/.stat
18b1e 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 72 ic void checkPtr
18b1f 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 79 map(. Integrity
18b20 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a Ck *pCheck, /*
18b21 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b Integrity check
18b22 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67 context */. Pg
18b23 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20 no iChild,
18b24 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 61 /* Child pa
18b25 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 ge number */. u
18b26 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 8 eType,
18b27 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 /* Expecte
18b28 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 79 d pointer map ty
18b29 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61 pe */. Pgno iPa
18b2a 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 2f rent, /
18b2b 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74 * Expected point
18b2c 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 61 er map parent pa
18b2d 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 ge number */. c
18b2e 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20 har *zContext
18b2f 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 /* Context
18b30 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73 description (us
18b31 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67 ed for error msg
18b32 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 ) */.){. int rc
18b33 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 79 ;. u8 ePtrmapTy
18b34 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d pe;. Pgno iPtrm
18b35 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20 apParent;.. rc
18b36 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 65 = ptrmapGet(pChe
18b37 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c ck->pBt, iChild,
18b38 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 26 &ePtrmapType, &
18b39 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a iPtrmapParent);.
18b3a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18b3b 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 _OK ){. if( r
18b3c 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
18b3d 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f || rc==SQLITE_IO
18b3e 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65 ERR_NOMEM ) pChe
18b3f 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 ck->mallocFailed
18b40 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41 = 1;. checkA
18b41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18b42 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c zContext, "Fail
18b43 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61 ed to read ptrma
18b44 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c p key=%d", iChil
18b45 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a d);. return;.
18b46 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d }.. if( ePtrm
18b47 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c apType!=eType ||
18b48 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d iPtrmapParent!=
18b49 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63 iParent ){. c
18b4a 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18b4b 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
18b4c 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20 . "Bad ptr
18b4d 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64 map entry key=%d
18b4e 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64 expected=(%d,%d
18b4f 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20 ) got=(%d,%d)",
18b50 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65 . iChild, e
18b51 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65 Type, iParent, e
18b52 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72 PtrmapType, iPtr
18b53 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a mapParent);. }.
18b54 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
18b55 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 Check the integr
18b56 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c ity of the freel
18b57 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65 ist or of an ove
18b58 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e rflow page list.
18b59 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 .** Verify that
18b5a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
18b5b 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 ges on the list
18b5c 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 is N..*/.static
18b5d 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a void checkList(.
18b5e 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 IntegrityCk *p
18b5f 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 Check, /* Integ
18b60 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f rity checking co
18b61 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 ntext */. int i
18b62 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20 sFreeList,
18b63 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66 /* True for a f
18b64 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20 reelist. False
18b65 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 for overflow pag
18b66 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 e list */. int
18b67 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 iPage,
18b68 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
18b69 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20 for first page
18b6a 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 in the list */.
18b6b 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 int N,
18b6c 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 /* Expect
18b6d 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 ed number of pag
18b6e 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a es in the list *
18b6f 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 /. char *zConte
18b70 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e xt /* Con
18b71 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d text for error m
18b72 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 essages */.){.
18b73 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70 int i;. int exp
18b74 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 ected = N;. int
18b75 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b iFirst = iPage;
18b76 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 . while( N-- >
18b77 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 0 && pCheck->mxE
18b78 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 rr ){. DbPage
18b79 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20 *pOvflPage;.
18b7a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
18b7b 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69 pOvflData;. i
18b7c 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20 f( iPage<1 ){.
18b7d 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18b7e 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
18b7f 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25 ext,. "%
18b80 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69 d of %d pages mi
18b81 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66 ssing from overf
18b82 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e low list startin
18b83 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20 g at %d",.
18b84 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65 N+1, expecte
18b85 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20 d, iFirst);.
18b86 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
18b87 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 if( checkRef(
18b88 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a pCheck, iPage, z
18b89 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b Context) ) break
18b8a 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
18b8b 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b 3PagerGet(pCheck
18b8c 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29 ->pPager, (Pgno)
18b8d 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67 iPage, &pOvflPag
18b8e 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 e) ){. chec
18b8f 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
18b90 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61 k, zContext, "fa
18b91 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65 iled to get page
18b92 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 %d", iPage);.
18b93 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
18b94 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d . pOvflData =
18b95 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
18b96 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
18b97 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29 tData(pOvflPage)
18b98 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65 ;. if( isFree
18b99 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e List ){. in
18b9a 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 t n = get4byte(&
18b9b 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 pOvflData[4]);.#
18b9c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18b9d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18b9e 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e if( pCheck->
18b9f 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18ba0 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
18ba1 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 Ptrmap(pCheck, i
18ba2 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
18ba3 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 EPAGE, 0, zConte
18ba4 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18ba5 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e dif. if( n>
18ba6 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 pCheck->pBt->usa
18ba7 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 bleSize/4-2 ){.
18ba8 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 checkAppe
18ba9 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 ndMsg(pCheck, zC
18baa 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 ontext,.
18bab 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61 "freelist lea
18bac 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20 f count too big
18bad 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 on page %d", iPa
18bae 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d ge);. N--
18baf 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
18bb0 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 for(i=0;
18bb1 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 i<n; i++){.
18bb2 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 Pgno iFreeP
18bb3 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 age = get4byte(&
18bb4 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d pOvflData[8+i*4]
18bb5 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
18bb6 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18bb7 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 M. if(
18bb8 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 pCheck->pBt->aut
18bb9 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
18bba 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
18bbb 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65 ap(pCheck, iFree
18bbc 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45 Page, PTRMAP_FRE
18bbd 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 EPAGE, 0, zConte
18bbe 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d xt);. }
18bbf 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 .#endif.
18bc0 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 checkRef(pChec
18bc1 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43 k, iFreePage, zC
18bc2 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 ontext);.
18bc3 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20 }. N -=
18bc4 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d n;. }. }
18bc5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
18bc6 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
18bc7 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20 else{.
18bc8 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 /* If this datab
18bc9 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 ase supports aut
18bca 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61 o-vacuum and iPa
18bcb 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 ge is not the la
18bcc 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 st. ** page
18bcd 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f in this overflo
18bce 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68 w list, check th
18bcf 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d at the pointer-m
18bd0 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20 ap entry for.
18bd1 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 ** the follow
18bd2 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73 ing page matches
18bd3 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f iPage.. */
18bd4 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63 . if( pChec
18bd5 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 k->pBt->autoVacu
18bd6 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20 um && N>0 ){.
18bd7 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74 i = get4byt
18bd8 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 e(pOvflData);.
18bd9 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 checkPtrma
18bda 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52 p(pCheck, i, PTR
18bdb 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 MAP_OVERFLOW2, i
18bdc 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b Page, zContext);
18bdd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
18bde 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20 endif. iPage
18bdf 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c = get4byte(pOvfl
18be0 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 Data);. sqlit
18be1 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 e3PagerUnref(pOv
18be2 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 flPage);. }.}.#
18be3 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
18be4 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 OMIT_INTEGRITY_C
18be5 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 HECK */..#ifndef
18be6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 SQLITE_OMIT_INT
18be7 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a EGRITY_CHECK./*.
18be8 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61 ** Do various sa
18be9 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61 nity checks on a
18bea 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 single page of
18beb 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a a tree. Return.
18bec 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74 ** the tree dept
18bed 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72 h. Root pages r
18bee 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74 eturn 0. Parent
18bef 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a s of root pages.
18bf0 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64 ** return 1, and
18bf1 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a so forth..** .*
18bf2 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61 * These checks a
18bf3 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 re done:.**.**
18bf4 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72 1. Make sur
18bf5 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64 e that cells and
18bf6 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e freeblocks do n
18bf7 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20 ot overlap.**
18bf8 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69 but combi
18bf9 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 ne to completely
18bfa 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e cover the page.
18bfb 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b .** NO 2. Mak
18bfc 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73 e sure cell keys
18bfd 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a are in order..*
18bfe 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 * NO 3. Make
18bff 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c sure no key is l
18c00 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
18c01 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 l to zLowerBound
18c02 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61 ..** NO 4. Ma
18c03 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 ke sure no key i
18c04 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f s greater than o
18c05 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65 r equal to zUppe
18c06 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 rBound..**
18c07 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e 5. Check the in
18c08 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66 tegrity of overf
18c09 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20 low pages..**
18c0a 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65 6. Recursive
18c0b 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65 ly call checkTre
18c0c 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69 ePage on all chi
18c0d 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 ldren..** 7
18c0e 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74 . Verify that t
18c0f 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20 he depth of all
18c10 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20 children is the
18c11 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e same..** 8.
18c12 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 Make sure this
18c13 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73 page is at leas
18c14 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c t 33% full or el
18c15 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 se it is.**
18c16 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66 the root of
18c17 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 the tree..*/.st
18c18 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72 atic int checkTr
18c19 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72 eePage(. Integr
18c1a 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 ityCk *pCheck,
18c1b 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74 /* Context for t
18c1c 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20 he sanity check
18c1d 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20 */. int iPage,
18c1e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
18c1f 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
18c20 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a page to check *
18c21 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e /. char *zParen
18c22 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72 tContext /* Par
18c23 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 ent context */.)
18c24 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
18c25 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c ge;. int i, rc,
18c26 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f depth, d2, pgno
18c27 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72 , cnt;. int hdr
18c28 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69 , cellStart;. i
18c29 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a nt nCell;. u8 *
18c2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 data;. BtShared
18c2b 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61 *pBt;. int usa
18c2c 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20 bleSize;. char
18c2d 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20 zContext[100];.
18c2e 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a char *hit = 0;.
18c2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
18c30 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 ntf(sizeof(zCont
18c31 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 ext), zContext,
18c32 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61 "Page %d: ", iPa
18c33 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ge);.. /* Check
18c34 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65 that the page e
18c35 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74 xists. */. pBt
18c36 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a = pCheck->pBt;.
18c37 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 usableSize = p
18c38 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
18c39 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29 if( iPage==0 )
18c3a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
18c3b 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b checkRef(pCheck
18c3c 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74 , iPage, zParent
18c3d 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72 Context) ) retur
18c3e 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d n 0;. if( (rc =
18c3f 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
18c40 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20 t, (Pgno)iPage,
18c41 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20 29 &pPage, 0))!=0 )
18c42 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e {. checkAppen
18c43 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f dMsg(pCheck, zCo
18c44 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22 75 ntext,. "u
18c45 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 nable to get the
18c46 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f 64 page. error cod
18c47 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20 e=%d", rc);.
18c48 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
18c49 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d 50 61 67 /* Clear MemPag
18c4a 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d 61 6b 65 e.isInit to make
18c4b 20 73 75 72 65 20 74 68 65 20 63 6f 72 72 75 70 sure the corrup
18c4c 74 69 6f 6e 20 64 65 74 65 63 74 69 6f 6e 20 63 tion detection c
18c4d 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 74 72 65 ode in. ** btre
18c4e 65 49 6e 69 74 50 61 67 65 28 29 20 69 73 20 65 eInitPage() is e
18c4f 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a 20 20 70 xecuted. */. p
18c50 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 Page->isInit = 0
18c51 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 74 ;. if( (rc = bt
18c52 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 reeInitPage(pPag
18c53 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 e))!=0 ){. as
18c54 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
18c55 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a 20 _CORRUPT ); /*
18c56 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c The only possibl
18c57 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e 69 e error from Ini
18c58 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 65 tPage */. che
18c59 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 ckAppendMsg(pChe
18c5a 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 ck, zContext, .
18c5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18c5c 20 20 22 62 74 72 65 65 49 6e 69 74 50 61 67 65 "btreeInitPage
18c5d 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72 () returns error
18c5e 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a code %d", rc);.
18c5f 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
18c60 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 pPage);. retu
18c61 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 0;. }.. /*
18c62 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68 Check out all th
18c63 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 e cells.. */.
18c64 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72 depth = 0;. for
18c65 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e (i=0; i<pPage->n
18c66 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e Cell && pCheck->
18c67 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 mxErr; i++){.
18c68 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 u8 *pCell;.
18c69 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c u32 sz;. Cell
18c6a 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 Info info;..
18c6b 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64 /* Check payload
18c6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a overflow pages.
18c6d 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
18c6e 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 e3_snprintf(size
18c6f 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 of(zContext), zC
18c70 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 ontext,.
18c71 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61 "On tree pa
18c72 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22 ge %d cell %d: "
18c73 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 , iPage, i);.
18c74 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c pCell = findCel
18c75 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20 l(pPage,i);.
18c76 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
18c77 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
18c78 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d &info);. sz =
18c79 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 info.nData;.
18c7a 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 if( !pPage->int
18c7b 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e 74 Key ) sz += (int
18c7c 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 )info.nKey;.
18c7d 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f assert( sz==info
18c7e 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20 .nPayload );.
18c7f 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c if( (sz>info.nL
18c80 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 26 20 28 ocal) . && (
18c81 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
18c82 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e rflow]<=&pPage->
18c83 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c aData[pBt->usabl
18c84 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20 eSize]). ){.
18c85 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d int nPage =
18c86 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 (sz - info.nLoc
18c87 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20 al + usableSize
18c88 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65 - 5)/(usableSize
18c89 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e - 4);. Pgn
18c8a 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 o pgnoOvfl = get
18c8b 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 4byte(&pCell[inf
18c8c 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 o.iOverflow]);.#
18c8d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18c8e 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18c8f 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
18c90 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
18c91 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 checkPtrmap(p
18c92 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c Check, pgnoOvfl,
18c93 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
18c94 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 1, iPage, zConte
18c95 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18c96 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c dif. checkL
18c97 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70 ist(pCheck, 0, p
18c98 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20 gnoOvfl, nPage,
18c99 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d zContext);. }
18c9a 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73 .. /* Check s
18c9b 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68 anity of left ch
18c9c 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f ild page.. */
18c9d 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
18c9e 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70 >leaf ){. p
18c9f 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 gno = get4byte(p
18ca0 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 Cell);.#ifndef S
18ca1 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
18ca2 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20 ACUUM. if(
18ca3 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18ca4 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
18ca5 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 Ptrmap(pCheck, p
18ca6 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
18ca7 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 E, iPage, zConte
18ca8 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e xt);. }.#en
18ca9 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63 dif. d2 = c
18caa 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 heckTreePage(pCh
18cab 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 eck, pgno, zCont
18cac 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ext);. if(
18cad 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68 i>0 && d2!=depth
18cae 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
18caf 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
18cb0 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68 k, zContext, "Ch
18cb1 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64 ild page depth d
18cb2 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20 iffers");.
18cb3 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20 }. depth =
18cb4 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 d2;. }. }.
18cb5 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
18cb6 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 ){. pgno = g
18cb7 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e et4byte(&pPage->
18cb8 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 aData[pPage->hdr
18cb9 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 Offset+8]);.
18cba 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
18cbb 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 (sizeof(zContext
18cbc 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 ), zContext, .
18cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18cbe 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 61 "On page %d a
18cbf 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 22 t right child: "
18cc0 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 , iPage);.#ifnde
18cc1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
18cc2 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
18cc3 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
18cc4 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 ){. checkP
18cc5 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 trmap(pCheck, pg
18cc6 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 no, PTRMAP_BTREE
18cc7 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 , iPage, 0);.
18cc8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68 }.#endif. ch
18cc9 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65 eckTreePage(pChe
18cca 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 ck, pgno, zConte
18ccb 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 xt);. }. . /*
18ccc 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 Check for comple
18ccd 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20 74 te coverage of t
18cce 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64 he page. */. d
18ccf 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ata = pPage->aDa
18cd0 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67 ta;. hdr = pPag
18cd1 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
18cd2 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 67 hit = sqlite3Pag
18cd3 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61 eMalloc( pBt->pa
18cd4 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 geSize );. if(
18cd5 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43 hit==0 ){. pC
18cd6 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c heck->mallocFail
18cd7 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b ed = 1;. }else{
18cd8 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e 74 . u16 content
18cd9 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79 74 Offset = get2byt
18cda 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b e(&data[hdr+5]);
18cdb 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6e . assert( con
18cdc 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61 62 tentOffset<=usab
18cdd 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e leSize ); /* En
18cde 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 forced by btreeI
18cdf 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 nitPage() */.
18ce0 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74 memset(hit+cont
18ce1 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73 entOffset, 0, us
18ce2 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 ableSize-content
18ce3 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d Offset);. mem
18ce4 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74 set(hit, 1, cont
18ce5 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 entOffset);.
18ce6 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 nCell = get2byte
18ce7 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a (&data[hdr+3]);.
18ce8 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20 cellStart =
18ce9 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 hdr + 12 - 4*pPa
18cea 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f ge->leaf;. fo
18ceb 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
18cec 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 i++){. int
18ced 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 pc = get2byte(&d
18cee 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a ata[cellStart+i*
18cef 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73 2]);. u16 s
18cf0 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20 ize = 1024;.
18cf1 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 int j;. i
18cf2 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a f( pc<=usableSiz
18cf3 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 73 e-4 ){. s
18cf4 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 ize = cellSizePt
18cf5 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 r(pPage, &data[p
18cf6 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 c]);. }.
18cf7 20 20 20 69 66 28 20 28 70 63 2b 73 69 7a 65 2d if( (pc+size-
18cf8 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 1)>=usableSize )
18cf9 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 {. checkA
18cfa 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18cfb 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 0, .
18cfc 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 "Corruption det
18cfd 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 ected in cell %d
18cfe 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 on page %d",i,i
18cff 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d Page,0);. }
18d00 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f else{. fo
18d01 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a r(j=pc+size-1; j
18d02 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a >=pc; j--) hit[j
18d03 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ]++;. }.
18d04 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 74 32 62 }. i = get2b
18d05 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d yte(&data[hdr+1]
18d06 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 3e );. while( i>
18d07 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 0 ){. int s
18d08 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 20 61 73 ize, j;. as
18d09 73 65 72 74 28 20 69 3c 3d 75 73 61 62 6c 65 53 sert( i<=usableS
18d0a 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 2f 2a 20 ize-4 ); /*
18d0b 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 Enforced by btre
18d0c 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 eInitPage() */.
18d0d 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 size = get2
18d0e 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 5d 29 byte(&data[i+2])
18d0f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
18d10 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 i+size<=usableSi
18d11 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 ze ); /* Enforc
18d12 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 ed by btreeInitP
18d13 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 66 age() */. f
18d14 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a or(j=i+size-1; j
18d15 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d >=i; j--) hit[j]
18d16 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d 20 67 65 ++;. j = ge
18d17 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 5d 29 t2byte(&data[i])
18d18 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
18d19 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65 j==0 || j>i+size
18d1a 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 ); /* Enforced
18d1b 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67 by btreeInitPag
18d1c 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 e() */. ass
18d1d 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53 69 ert( j<=usableSi
18d1e 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e 66 ze-4 ); /* Enf
18d1f 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e orced by btreeIn
18d20 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 itPage() */.
18d21 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 7d 0a 20 i = j;. }.
18d22 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 for(i=cnt=0;
18d23 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b i<usableSize; i+
18d24 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69 +){. if( hi
18d25 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 t[i]==0 ){.
18d26 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 cnt++;.
18d27 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d }else if( hit[i]
18d28 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 >1 ){. ch
18d29 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18d2a 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 eck, 0,.
18d2b 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73 "Multiple uses
18d2c 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20 for byte %d of
18d2d 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61 page %d", i, iPa
18d2e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 ge);. bre
18d2f 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
18d30 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64 }. if( cnt!=d
18d31 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20 ata[hdr+7] ){.
18d32 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18d33 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 sg(pCheck, 0, .
18d34 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65 "Fragme
18d35 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 20 62 79 ntation of %d by
18d36 74 65 73 20 72 65 70 6f 72 74 65 64 20 61 73 20 tes reported as
18d37 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a %d on page %d",.
18d38 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 64 cnt, d
18d39 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67 ata[hdr+7], iPag
18d3a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
18d3b 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
18d3c 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 hit);. releaseP
18d3d 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 age(pPage);. re
18d3e 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a turn depth+1;.}.
18d3f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
18d40 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f _OMIT_INTEGRITY_
18d41 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 CHECK */..#ifnde
18d42 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e f SQLITE_OMIT_IN
18d43 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a TEGRITY_CHECK./*
18d44 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
18d45 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 65 does a complete
18d46 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67 69 check of the gi
18d47 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e 20 ven BTree file.
18d48 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 aRoot[] is.** a
18d49 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65 73 n array of pages
18d4a 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65 61 numbers were ea
18d4b 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 ch page number i
18d4c 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 s the root page
18d4d 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20 of.** a table.
18d4e 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 6d nRoot is the num
18d4f 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
18d50 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 41 n aRoot..**.** A
18d51 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 read-only or re
18d52 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63 ad-write transac
18d53 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 tion must be ope
18d54 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 ned before calli
18d55 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 ng.** this funct
18d56 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 ion..**.** Write
18d57 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
18d58 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e rror seen in *pn
18d59 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f 72 Err. Except for
18d5a 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 some memory.**
18d5b 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
18d5c 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 s, an error mes
18d5d 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d sage held in mem
18d5e 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
18d5f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 72 m.** malloc is r
18d60 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45 72 eturned if *pnEr
18d61 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 r is non-zero.
18d62 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68 65 If *pnErr==0 the
18d63 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 74 n NULL is.** ret
18d64 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d 65 6d urned. If a mem
18d65 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
18d66 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c rror occurs, NUL
18d67 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a L is returned..*
18d68 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18d69 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 char *sqlite3Bt
18d6a 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 reeIntegrityChec
18d6b 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 k(. Btree *p,
18d6c 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 /* The btree
18d6d 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f to be checked */
18d6e 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 . int *aRoot,
18d6f 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20 /* An array of
18d70 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65 root pages numbe
18d71 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61 rs for individua
18d72 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74 l trees */. int
18d73 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 nRoot, /* Nu
18d74 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
18d75 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 in aRoot[] */.
18d76 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a int mxErr, /*
18d77 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20 Stop reporting
18d78 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69 errors after thi
18d79 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 s many */. int
18d7a 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69 *pnErr /* Wri
18d7b 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 te number of err
18d7c 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73 ors seen to this
18d7d 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a variable */.){.
18d7e 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 Pgno i;. int
18d7f 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74 nRef;. Integrit
18d80 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74 yCk sCheck;. Bt
18d81 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
18d82 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 72 >pBt;. char zEr
18d83 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 r[100];.. sqlit
18d84 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
18d85 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
18d86 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 Trans>TRANS_NONE
18d87 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 && pBt->inTrans
18d88 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e action>TRANS_NON
18d89 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d 20 73 71 E );. nRef = sq
18d8a 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 lite3PagerRefcou
18d8b 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b nt(pBt->pPager);
18d8c 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20 . sCheck.pBt =
18d8d 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50 pBt;. sCheck.pP
18d8e 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 ager = pBt->pPag
18d8f 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61 er;. sCheck.nPa
18d90 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f ge = pagerPageco
18d91 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 3b unt(sCheck.pBt);
18d92 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20 . sCheck.mxErr
18d93 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63 = mxErr;. sChec
18d94 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 43 k.nErr = 0;. sC
18d95 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 heck.mallocFaile
18d96 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20 d = 0;. *pnErr
18d97 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65 63 = 0;. if( sChec
18d98 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 k.nPage==0 ){.
18d99 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
18d9a 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 ave(p);. retu
18d9b 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65 rn 0;. }. sChe
18d9c 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74 ck.anRef = sqlit
18d9d 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63 e3Malloc( (sChec
18d9e 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f k.nPage+1)*sizeo
18d9f 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30 f(sCheck.anRef[0
18da0 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68 ]) );. if( !sCh
18da1 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20 eck.anRef ){.
18da2 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 *pnErr = 1;.
18da3 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
18da4 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 ve(p);. retur
18da5 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 n 0;. }. for(i
18da6 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 =0; i<=sCheck.nP
18da7 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63 age; i++){ sChec
18da8 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20 k.anRef[i] = 0;
18da9 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f }. i = PENDING_
18daa 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a BYTE_PAGE(pBt);.
18dab 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e if( i<=sCheck.
18dac 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68 nPage ){. sCh
18dad 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31 eck.anRef[i] = 1
18dae 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 ;. }. sqlite3S
18daf 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 43 68 trAccumInit(&sCh
18db0 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72 eck.errMsg, zErr
18db1 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 , sizeof(zErr),
18db2 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 20000);.. /* Ch
18db3 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 eck the integrit
18db4 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 y of the freelis
18db5 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 t. */. checkLi
18db6 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67 st(&sCheck, 1, g
18db7 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 et4byte(&pBt->pP
18db8 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 age1->aData[32])
18db9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 65 ,. ge
18dba 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 t4byte(&pBt->pPa
18dbb 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c ge1->aData[36]),
18dbc 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a "Main freelist:
18dbd 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ");.. /* Check
18dbe 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 2e all the tables.
18dbf 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b . */. for(i=0;
18dc0 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26 (int)i<nRoot &&
18dc1 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 sCheck.mxErr; i
18dc2 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f ++){. if( aRo
18dc3 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 ot[i]==0 ) conti
18dc4 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c nue;.#ifndef SQL
18dc5 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
18dc6 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d UUM. if( pBt-
18dc7 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 >autoVacuum && a
18dc8 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 Root[i]>1 ){.
18dc9 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 checkPtrmap(&
18dca 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d sCheck, aRoot[i]
18dcb 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 , PTRMAP_ROOTPAG
18dcc 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a E, 0, 0);. }.
18dcd 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b #endif. check
18dce 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b TreePage(&sCheck
18dcf 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 , aRoot[i], "Lis
18dd0 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a t of tree roots:
18dd1 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d ");. }.. /* M
18dd2 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20 70 ake sure every p
18dd3 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 age in the file
18dd4 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20 is referenced.
18dd5 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c */. for(i=1; i<
18dd6 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 =sCheck.nPage &&
18dd7 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 sCheck.mxErr; i
18dd8 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 ++){.#ifdef SQLI
18dd9 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18dda 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 UM. if( sChec
18ddb 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b k.anRef[i]==0 ){
18ddc 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 . checkAppe
18ddd 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 ndMsg(&sCheck, 0
18dde 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 , "Page %d is ne
18ddf 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 ver used", i);.
18de0 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f }.#else. /
18de1 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
18de2 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d e supports auto-
18de3 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72 vacuum, make sur
18de4 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74 e no tables cont
18de5 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 ain. ** refer
18de6 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72 ences to pointer
18de7 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20 -map pages..
18de8 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 */. if( sChec
18de9 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26 k.anRef[i]==0 &&
18dea 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 . (PTRMAP
18deb 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21 _PAGENO(pBt, i)!
18dec 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f =i || !pBt->auto
18ded 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 Vacuum) ){.
18dee 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18def 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67 &sCheck, 0, "Pag
18df0 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 73 e %d is never us
18df1 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 ed", i);. }.
18df2 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e if( sCheck.an
18df3 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20 Ref[i]!=0 && .
18df4 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47 (PTRMAP_PAG
18df5 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26 ENO(pBt, i)==i &
18df6 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 & pBt->autoVacuu
18df7 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 m) ){. chec
18df8 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 kAppendMsg(&sChe
18df9 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20 ck, 0, "Pointer
18dfa 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20 72 map page %d is r
18dfb 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a eferenced", i);.
18dfc 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
18dfd 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
18dfe 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64 this analysis d
18dff 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79 id not leave any
18e00 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e 0a unref() pages..
18e01 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 ** This is an
18e02 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74 internal consist
18e03 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20 69 ency check; an i
18e04 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0a 20 ntegrity check.
18e05 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65 67 ** of the integ
18e06 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 2a 2f rity check.. */
18e07 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65 . if( NEVER(nRe
18e08 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65 f != sqlite3Page
18e09 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 rRefcount(pBt->p
18e0a 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63 Pager)) ){. c
18e0b 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 heckAppendMsg(&s
18e0c 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 Check, 0, .
18e0d 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 "Outstanding pa
18e0e 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72 ge count goes fr
18e0f 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 69 om %d to %d duri
18e10 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 ng this analysis
18e11 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73 ",. nRef, s
18e12 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f qlite3PagerRefco
18e13 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 unt(pBt->pPager)
18e14 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f . );. }.. /
18e15 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20 * Clean up and
18e16 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20 report errors..
18e17 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 */. sqlite3Btr
18e18 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71 eeLeave(p);. sq
18e19 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 63 lite3_free(sChec
18e1a 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20 k.anRef);. if(
18e1b 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 sCheck.mallocFai
18e1c 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 led ){. sqlit
18e1d 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 e3StrAccumReset(
18e1e 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b &sCheck.errMsg);
18e1f 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 . *pnErr = sC
18e20 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 heck.nErr+1;.
18e21 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
18e22 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b *pnErr = sCheck
18e23 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68 .nErr;. if( sCh
18e24 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71 eck.nErr==0 ) sq
18e25 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 lite3StrAccumRes
18e26 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 et(&sCheck.errMs
18e27 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c g);. return sql
18e28 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 ite3StrAccumFini
18e29 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 sh(&sCheck.errMs
18e2a 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 g);.}.#endif /*
18e2b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
18e2c 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a GRITY_CHECK */..
18e2d 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
18e2e 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f full pathname o
18e2f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 f the underlying
18e30 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
18e31 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 **.** The pager
18e32 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 filename is inva
18e33 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 riant as long as
18e34 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a the pager is.**
18e35 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73 open so it is s
18e36 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69 afe to access wi
18e37 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72 thout the BtShar
18e38 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c ed mutex..*/.SQL
18e39 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
18e3a 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 t char *sqlite3B
18e3b 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 treeGetFilename(
18e3c 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
18e3d 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 ert( p->pBt->pPa
18e3e 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 ger!=0 );. retu
18e3f 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 rn sqlite3PagerF
18e40 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e ilename(p->pBt->
18e41 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pPager);.}../*.*
18e42 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 74 * Return the pat
18e43 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 hname of the jou
18e44 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 68 rnal file for th
18e45 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 is database. The
18e46 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 return.** value
18e47 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
18e48 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 is the same reg
18e49 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 ardless of wheth
18e4a 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 er the journal f
18e4b 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 ile.** has been
18e4c 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a created or not..
18e4d 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 **.** The pager
18e4e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 journal filename
18e4f 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73 is invariant as
18e50 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67 long as the pag
18e51 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f er is.** open so
18e52 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 it is safe to a
18e53 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68 ccess without th
18e54 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 e BtShared mutex
18e55 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
18e56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ATE const char *
18e57 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a sqlite3BtreeGetJ
18e58 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 ournalname(Btree
18e59 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
18e5a 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d p->pBt->pPager!=
18e5b 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 0 );. return sq
18e5c 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
18e5d 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 lname(p->pBt->pP
18e5e 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ager);.}../*.**
18e5f 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 Return non-zero
18e60 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e if a transaction
18e61 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 is active..*/.S
18e62 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18e63 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 t sqlite3BtreeIs
18e64 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 InTrans(Btree *p
18e65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d ){. assert( p==
18e66 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
18e67 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
18e68 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 utex) );. retur
18e69 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72 n (p && (p->inTr
18e6a 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
18e6b 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ));.}../*.** Ret
18e6c 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 urn non-zero if
18e6d 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65 a read (or write
18e6e 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 ) transaction is
18e6f 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 active..*/.SQLI
18e70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
18e71 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 qlite3BtreeIsInR
18e72 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a eadTrans(Btree *
18e73 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 p){. assert( p
18e74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
18e75 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
18e76 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
18e77 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54 . return p->inT
18e78 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 rans!=TRANS_NONE
18e79 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 ;.}..SQLITE_PRIV
18e7a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
18e7b 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42 treeIsInBackup(B
18e7c 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 tree *p){. asse
18e7d 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 rt( p );. asser
18e7e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
18e7f 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
18e80 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ex) );. return
18e81 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d p->nBackup!=0;.}
18e82 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
18e83 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 ction returns a
18e84 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f pointer to a blo
18e85 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f b of memory asso
18e86 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61 ciated with.** a
18e87 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62 single shared-b
18e88 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79 tree. The memory
18e89 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65 is used by clie
18e8a 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20 nt code for its
18e8b 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 own.** purposes
18e8c 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f (for example, to
18e8d 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65 store a high-le
18e8e 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63 vel schema assoc
18e8f 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 iated with .** t
18e90 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29 he shared-btree)
18e91 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65 . The btree laye
18e92 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65 r manages refere
18e93 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73 nce counting iss
18e94 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ues..**.** The f
18e95 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 irst time this i
18e96 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68 s called on a sh
18e97 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74 ared-btree, nByt
18e98 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f es bytes of memo
18e99 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61 ry.** are alloca
18e9a 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64 ted, zeroed, and
18e9b 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 returned to the
18e9c 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63 caller. For eac
18e9d 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a h subsequent .**
18e9e 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73 call the nBytes
18e9f 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67 parameter is ig
18ea0 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e nored and a poin
18ea1 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20 ter to the same
18ea2 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 blob.** of memor
18ea3 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a y returned. .**.
18ea4 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73 ** If the nBytes
18ea5 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20 parameter is 0
18ea6 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 and the blob of
18ea7 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79 memory has not y
18ea8 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 et been.** alloc
18ea9 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 ated, a null poi
18eaa 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
18eab 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61 . If the blob ha
18eac 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a s already been.*
18ead 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 * allocated, it
18eae 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e is returned as n
18eaf 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 ormal..**.** Jus
18eb0 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61 t before the sha
18eb1 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f red-btree is clo
18eb2 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f sed, the functio
18eb3 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 n passed as the
18eb4 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65 .** xFree argume
18eb5 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f nt when the memo
18eb6 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 ry allocation wa
18eb7 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65 s made is invoke
18eb8 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f d on the .** blo
18eb9 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d b of allocated m
18eba 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63 emory. This func
18ebb 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 tion should not
18ebc 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65 call sqlite3_fre
18ebd 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 e().** on the me
18ebe 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20 mory, the btree
18ebf 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e layer does that.
18ec0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18ec1 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
18ec2 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 BtreeSchema(Btre
18ec3 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73 e *p, int nBytes
18ec4 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76 , void(*xFree)(v
18ec5 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61 oid *)){. BtSha
18ec6 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
18ec7 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 t;. sqlite3Btre
18ec8 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 eEnter(p);. if(
18ec9 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 26 !pBt->pSchema &
18eca 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20 & nBytes ){.
18ecb 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 pBt->pSchema = s
18ecc 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
18ecd 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 42 (nBytes);. pB
18ece 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 3d t->xFreeSchema =
18ecf 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71 xFree;. }. sq
18ed0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
18ed1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 74 p);. return pBt
18ed2 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a ->pSchema;.}../*
18ed3 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 .** Return SQLIT
18ed4 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
18ed5 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 20 ACHE if another
18ed6 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 user of the same
18ed7 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 65 shared .** btre
18ed8 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e e as the argumen
18ed9 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 t handle holds a
18eda 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
18edb 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 on the .** sqli
18edc 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e te_master table.
18edd 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54 Otherwise SQLIT
18ede 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
18edf 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18ee0 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f te3BtreeSchemaLo
18ee1 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a cked(Btree *p){.
18ee2 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
18ee3 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
18ee4 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
18ee5 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 tex) );. sqlite
18ee6 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
18ee7 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 rc = queryShar
18ee8 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
18ee9 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c (p, MASTER_ROOT,
18eea 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61 READ_LOCK);. a
18eeb 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
18eec 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 E_OK || rc==SQLI
18eed 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 TE_LOCKED_SHARED
18eee 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 74 CACHE );. sqlit
18eef 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
18ef0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18ef1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
18ef2 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
18ef3 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 HE./*.** Obtain
18ef4 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 a lock on the ta
18ef5 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 ble whose root p
18ef6 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68 age is iTab. Th
18ef7 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77 e.** lock is a w
18ef8 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57 rite lock if isW
18ef9 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65 ritelock is true
18efa 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a or a read lock.
18efb 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73 ** if it is fals
18efc 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
18efd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
18efe 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 BtreeLockTable(B
18eff 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 tree *p, int iTa
18f00 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 b, u8 isWriteLoc
18f01 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 k){. int rc = S
18f02 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 QLITE_OK;. asse
18f03 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d rt( p->inTrans!=
18f04 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 TRANS_NONE );.
18f05 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 if( p->sharable
18f06 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 79 ){. u8 lockTy
18f07 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b pe = READ_LOCK +
18f08 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 isWriteLock;.
18f09 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f 4c assert( READ_L
18f0a 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 OCK+1==WRITE_LOC
18f0b 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 K );. assert(
18f0c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 isWriteLock==0
18f0d 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d || isWriteLock==
18f0e 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 1 );.. sqlite
18f0f 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
18f10 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 rc = querySh
18f11 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
18f12 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b ck(p, iTab, lock
18f13 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Type);. if( r
18f14 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
18f15 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 53 68 rc = setSh
18f16 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
18f17 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b ck(p, iTab, lock
18f18 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 Type);. }.
18f19 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
18f1a 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ve(p);. }. ret
18f1b 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
18f1c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
18f1d 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f _OMIT_INCRBLOB./
18f1e 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 43 *.** Argument pC
18f1f 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 75 72 sr must be a cur
18f20 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 sor opened for w
18f21 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a riting on an .**
18f22 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 63 75 INTKEY table cu
18f23 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
18f24 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 62 6c at a valid tabl
18f25 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69 e entry. .** Thi
18f26 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 s function modif
18f27 69 65 73 20 74 68 65 20 64 61 74 61 20 73 74 6f ies the data sto
18f28 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 red as part of t
18f29 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a hat entry..**.**
18f2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 63 Only the data c
18f2b 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 20 ontent may only
18f2c 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 20 be modified, it
18f2d 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
18f2e 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 to .** change th
18f2f 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
18f30 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49 66 20 data stored. If
18f31 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
18f32 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 2a 2a 20 called with.**
18f33 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20 parameters that
18f34 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 attempt to write
18f35 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
18f36 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61 the existing da
18f37 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 69 66 69 ta,.** no modifi
18f38 63 61 74 69 6f 6e 73 20 61 72 65 20 6d 61 64 65 cations are made
18f39 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 4f 52 52 and SQLITE_CORR
18f3a 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e UPT is returned.
18f3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18f3c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
18f3d 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72 reePutData(BtCur
18f3e 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 20 6f sor *pCsr, u32 o
18f3f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 ffset, u32 amt,
18f40 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 void *z){. int
18f41 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 rc;. assert( cu
18f42 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
18f43 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Csr) );. assert
18f44 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
18f45 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 65 held(pCsr->pBtre
18f46 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b e->db->mutex) );
18f47 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d . assert( pCsr-
18f48 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c >isIncrblobHandl
18f49 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 e );.. rc = res
18f4a 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
18f4b 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 66 28 20 on(pCsr);. if(
18f4c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
18f4d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
18f4e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
18f4f 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 sr->eState!=CURS
18f50 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 OR_REQUIRESEEK )
18f51 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53 ;. if( pCsr->eS
18f52 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c tate!=CURSOR_VAL
18f53 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ID ){. return
18f54 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 SQLITE_ABORT;.
18f55 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73 }.. /* Check s
18f56 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f 6e 73 3a ome assumptions:
18f57 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65 . ** (a) the
18f58 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20 cursor is open
18f59 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a for writing,. *
18f5a 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69 73 * (b) there is
18f5b 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 74 72 a read/write tr
18f5c 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 0a ansaction open,.
18f5d 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 63 ** (c) the c
18f5e 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 73 20 onnection holds
18f5f 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 a write-lock on
18f60 74 68 65 20 74 61 62 6c 65 20 28 69 66 20 72 65 the table (if re
18f61 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a 20 20 20 quired),. **
18f62 28 64 29 20 74 68 65 72 65 20 61 72 65 20 6e 6f (d) there are no
18f63 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 61 conflicting rea
18f64 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a 20 20 2a d-locks, and. *
18f65 2a 20 20 20 28 65 29 20 74 68 65 20 63 75 72 73 * (e) the curs
18f66 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76 or points at a v
18f67 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69 alid row of an i
18f68 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a ntKey table.. *
18f69 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77 /. if( !pCsr->w
18f6a 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74 rFlag ){. ret
18f6b 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
18f6c 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 NLY;. }. asser
18f6d 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72 t( !pCsr->pBt->r
18f6e 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 73 72 2d eadOnly && pCsr-
18f6f 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 >pBt->inTransact
18f70 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
18f71 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 );. assert( ha
18f72 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c sSharedCacheTabl
18f73 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 42 74 72 eLock(pCsr->pBtr
18f74 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f ee, pCsr->pgnoRo
18f75 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a 20 20 61 ot, 0, 2) );. a
18f76 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43 ssert( !hasReadC
18f77 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d 3e 70 onflicts(pCsr->p
18f78 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e Btree, pCsr->pgn
18f79 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 73 73 65 oRoot) );. asse
18f7a 72 74 28 20 70 43 73 72 2d 3e 61 70 50 61 67 65 rt( pCsr->apPage
18f7b 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 [pCsr->iPage]->i
18f7c 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 65 74 75 ntKey );.. retu
18f7d 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 rn accessPayload
18f7e 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20 61 (pCsr, offset, a
18f7f 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 mt, (unsigned ch
18f80 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f ar *)z, 1);.}../
18f81 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 * .** Set a flag
18f82 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 on this cursor
18f83 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63 to cache the loc
18f84 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20 ations of pages
18f85 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65 from the .** ove
18f86 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74 rflow list for t
18f87 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20 he current row.
18f88 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
18f89 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a cursors opened.*
18f8a 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61 * for incrementa
18f8b 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a l blob IO only..
18f8c 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
18f8d 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20 ion sets a flag
18f8e 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c only. The actual
18f8f 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63 page location c
18f90 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20 ache.** (stored
18f91 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 in BtCursor.aOve
18f92 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f rflow[]) is allo
18f93 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62 cated and used b
18f94 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63 y function.** ac
18f95 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74 cessPayload() (t
18f96 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69 he worker functi
18f97 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74 on for sqlite3Bt
18f98 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a reeData() and.**
18f99 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
18f9a 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 Data())..*/.SQLI
18f9b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
18f9c 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 sqlite3BtreeCach
18f9d 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 eOverflow(BtCurs
18f9e 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
18f9f 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
18fa0 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
18fa1 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
18fa2 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
18fa3 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
18fa4 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
18fa5 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c (!pCur->isIncrbl
18fa6 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73 obHandle);. ass
18fa7 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72 ert(!pCur->aOver
18fa8 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69 flow);. pCur->i
18fa9 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 sIncrblobHandle
18faa 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f = 1;.}.#endif../
18fab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
18fac 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a nd of btree.c **
18fad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18fae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18faf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
18fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
18fb1 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70 egin file backup
18fb2 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
18fb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18fb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
18fb5 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72 *.** 2009 Januar
18fb6 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 28.**.** The a
18fb7 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
18fb8 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
18fb9 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
18fba 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
18fbb 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
18fbc 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
18fbd 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
18fbe 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
18fbf 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
18fc0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
18fc1 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
18fc2 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
18fc3 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
18fc4 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
18fc5 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
18fc6 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
18fc7 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
18fc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18fc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18fca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18fcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
18fcc 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
18fcd 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d contains the im
18fce 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
18fcf 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
18fd0 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49 up_XXX() .** API
18fd1 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74 functions and t
18fd2 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75 he related featu
18fd3 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 res..**.** $Id:
18fd4 62 61 63 6b 75 70 2e 63 2c 76 20 31 2e 31 39 20 backup.c,v 1.19
18fd5 32 30 30 39 2f 30 37 2f 30 36 20 31 39 3a 30 33 2009/07/06 19:03
18fd6 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :13 drh Exp $.*/
18fd7 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69 ../* Macro to fi
18fd8 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f nd the minimum o
18fd9 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61 f two numeric va
18fda 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 lues..*/.#ifndef
18fdb 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49 MIN.# define MI
18fdc 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f N(x,y) ((x)<(y)?
18fdd 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a (x):(y)).#endif.
18fde 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 ./*.** Structure
18fdf 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 allocated for e
18fe0 61 63 68 20 62 61 63 6b 75 70 20 6f 70 65 72 61 ach backup opera
18fe1 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 tion..*/.struct
18fe2 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 7b sqlite3_backup {
18fe3 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73 . sqlite3* pDes
18fe4 74 44 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 tDb; /* D
18fe5 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
18fe6 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
18fe7 42 74 72 65 65 20 2a 70 44 65 73 74 3b 20 20 20 Btree *pDest;
18fe8 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 /* Dest
18fe9 69 6e 61 74 69 6f 6e 20 62 2d 74 72 65 65 20 66 ination b-tree f
18fea 69 6c 65 20 2a 2f 0a 20 20 75 33 32 20 69 44 65 ile */. u32 iDe
18feb 73 74 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 stSchema;
18fec 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73 63 /* Original sc
18fed 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20 64 hema cookie in d
18fee 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 estination */.
18fef 69 6e 74 20 62 44 65 73 74 4c 6f 63 6b 65 64 3b int bDestLocked;
18ff0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
18ff1 20 6f 6e 63 65 20 61 20 77 72 69 74 65 2d 74 72 once a write-tr
18ff2 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 ansaction is ope
18ff3 6e 20 6f 6e 20 70 44 65 73 74 20 2a 2f 0a 0a 20 n on pDest */..
18ff4 20 50 67 6e 6f 20 69 4e 65 78 74 3b 20 20 20 20 Pgno iNext;
18ff5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
18ff6 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
18ff7 6e 65 78 74 20 73 6f 75 72 63 65 20 70 61 67 65 next source page
18ff8 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 73 71 to copy */. sq
18ff9 6c 69 74 65 33 2a 20 70 53 72 63 44 62 3b 20 20 lite3* pSrcDb;
18ffa 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 /* Source
18ffb 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
18ffc 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 53 72 */. Btree *pSr
18ffd 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f c; /
18ffe 2a 20 53 6f 75 72 63 65 20 62 2d 74 72 65 65 20 * Source b-tree
18fff 66 69 6c 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 file */.. int r
19000 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
19001 20 20 20 20 2f 2a 20 42 61 63 6b 75 70 20 70 72 /* Backup pr
19002 6f 63 65 73 73 20 65 72 72 6f 72 20 63 6f 64 65 ocess error code
19003 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20 */.. /* These
19004 74 77 6f 20 76 61 72 69 61 62 6c 65 73 20 61 72 two variables ar
19005 65 20 73 65 74 20 62 79 20 65 76 65 72 79 20 63 e set by every c
19006 61 6c 6c 20 74 6f 20 62 61 63 6b 75 70 5f 73 74 all to backup_st
19007 65 70 28 29 2e 20 54 68 65 79 20 61 72 65 0a 20 ep(). They are.
19008 20 2a 2a 20 72 65 61 64 20 62 79 20 63 61 6c 6c ** read by call
19009 73 20 74 6f 20 62 61 63 6b 75 70 5f 72 65 6d 61 s to backup_rema
1900a 69 6e 69 6e 67 28 29 20 61 6e 64 20 62 61 63 6b ining() and back
1900b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a up_pagecount()..
1900c 20 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 52 65 6d */. Pgno nRem
1900d 61 69 6e 69 6e 67 3b 20 20 20 20 20 20 20 20 20 aining;
1900e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
1900f 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 es left to copy
19010 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 63 */. Pgno nPagec
19011 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a ount; /*
19012 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
19013 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 2a pages to copy *
19014 2f 0a 0a 20 20 69 6e 74 20 69 73 41 74 74 61 63 /.. int isAttac
19015 68 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a hed; /*
19016 20 54 72 75 65 20 6f 6e 63 65 20 62 61 63 6b 75 True once backu
19017 70 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73 p has been regis
19018 74 65 72 65 64 20 77 69 74 68 20 70 61 67 65 72 tered with pager
19019 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 */. sqlite3_ba
1901a 63 6b 75 70 20 2a 70 4e 65 78 74 3b 20 20 20 2f ckup *pNext; /
1901b 2a 20 4e 65 78 74 20 62 61 63 6b 75 70 20 61 73 * Next backup as
1901c 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 6f sociated with so
1901d 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 7d 3b urce pager */.};
1901e 0a 0a 2f 2a 0a 2a 2a 20 54 48 52 45 41 44 20 53 ../*.** THREAD S
1901f 41 46 45 54 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a AFETY NOTES:.**.
19020 2a 2a 20 20 20 4f 6e 63 65 20 69 74 20 68 61 73 ** Once it has
19021 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 75 73 been created us
19022 69 6e 67 20 62 61 63 6b 75 70 5f 69 6e 69 74 28 ing backup_init(
19023 29 2c 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69 ), a single sqli
19024 74 65 33 5f 62 61 63 6b 75 70 0a 2a 2a 20 20 20 te3_backup.**
19025 73 74 72 75 63 74 75 72 65 20 6d 61 79 20 62 65 structure may be
19026 20 61 63 63 65 73 73 65 64 20 76 69 61 20 74 77 accessed via tw
19027 6f 20 67 72 6f 75 70 73 20 6f 66 20 74 68 72 65 o groups of thre
19028 61 64 2d 73 61 66 65 20 65 6e 74 72 79 20 70 6f ad-safe entry po
19029 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ints:.**.**
1902a 2a 20 56 69 61 20 74 68 65 20 73 71 6c 69 74 65 * Via the sqlite
1902b 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 41 3_backup_XXX() A
1902c 50 49 20 66 75 6e 63 74 69 6f 6e 20 62 61 63 6b PI function back
1902d 75 70 5f 73 74 65 70 28 29 20 61 6e 64 20 0a 2a up_step() and .*
1902e 2a 20 20 20 20 20 20 20 62 61 63 6b 75 70 5f 66 * backup_f
1902f 69 6e 69 73 68 28 29 2e 20 42 6f 74 68 20 74 68 inish(). Both th
19030 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 6f 62 ese functions ob
19031 74 61 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 tain the source
19032 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 database.**
19033 20 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 20 61 handle mutex a
19034 6e 64 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 nd the mutex ass
19035 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
19036 20 73 6f 75 72 63 65 20 42 74 53 68 61 72 65 64 source BtShared
19037 20 0a 2a 2a 20 20 20 20 20 20 20 73 74 72 75 63 .** struc
19038 74 75 72 65 2c 20 69 6e 20 74 68 61 74 20 6f 72 ture, in that or
19039 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a der..**.** *
1903a 20 56 69 61 20 74 68 65 20 42 61 63 6b 75 70 55 Via the BackupU
1903b 70 64 61 74 65 28 29 20 61 6e 64 20 42 61 63 6b pdate() and Back
1903c 75 70 52 65 73 74 61 72 74 28 29 20 66 75 6e 63 upRestart() func
1903d 74 69 6f 6e 73 2c 20 77 68 69 63 68 20 61 72 65 tions, which are
1903e 0a 2a 2a 20 20 20 20 20 20 20 69 6e 76 6f 6b 65 .** invoke
1903f 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 6c d by the pager l
19040 61 79 65 72 20 74 6f 20 72 65 70 6f 72 74 20 76 ayer to report v
19041 61 72 69 6f 75 73 20 73 74 61 74 65 20 63 68 61 arious state cha
19042 6e 67 65 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 nges in.**
19043 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 the page cache
19044 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
19045 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
19046 61 73 65 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a ase. The mutex.*
19047 2a 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 74 * associat
19048 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 ed with the sour
19049 63 65 20 64 61 74 61 62 61 73 65 20 42 74 53 68 ce database BtSh
1904a 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 77 ared structure w
1904b 69 6c 6c 20 61 6c 77 61 79 73 20 0a 2a 2a 20 20 ill always .**
1904c 20 20 20 20 20 62 65 20 68 65 6c 64 20 77 68 65 be held whe
1904d 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 n either of thes
1904e 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 e functions are
1904f 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 invoked..**.**
19050 20 54 68 65 20 6f 74 68 65 72 20 73 71 6c 69 74 The other sqlit
19051 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 e3_backup_XXX()
19052 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62 API functions, b
19053 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 ackup_remaining(
19054 29 20 61 6e 64 0a 2a 2a 20 20 20 62 61 63 6b 75 ) and.** backu
19055 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 20 61 72 p_pagecount() ar
19056 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66 e not thread-saf
19057 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20 e functions. If
19058 74 68 65 79 20 61 72 65 20 63 61 6c 6c 65 64 0a they are called.
19059 2a 2a 20 20 20 77 68 69 6c 65 20 73 6f 6d 65 20 ** while some
1905a 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 other thread is
1905b 63 61 6c 6c 69 6e 67 20 62 61 63 6b 75 70 5f 73 calling backup_s
1905c 74 65 70 28 29 20 6f 72 20 62 61 63 6b 75 70 5f tep() or backup_
1905d 66 69 6e 69 73 68 28 29 2c 0a 2a 2a 20 20 20 74 finish(),.** t
1905e 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e he values return
1905f 65 64 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 ed may be invali
19060 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77 d. There is no w
19061 61 79 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f ay for a call to
19062 0a 2a 2a 20 20 20 42 61 63 6b 75 70 55 70 64 61 .** BackupUpda
19063 74 65 28 29 20 6f 72 20 42 61 63 6b 75 70 52 65 te() or BackupRe
19064 73 74 61 72 74 28 29 20 74 6f 20 69 6e 74 65 72 start() to inter
19065 66 65 72 65 20 77 69 74 68 20 62 61 63 6b 75 70 fere with backup
19066 5f 72 65 6d 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 _remaining().**
19067 20 20 6f 72 20 62 61 63 6b 75 70 5f 70 61 67 65 or backup_page
19068 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 count()..**.**
19069 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 Depending on th
1906a 65 20 53 51 4c 69 74 65 20 63 6f 6e 66 69 67 75 e SQLite configu
1906b 72 61 74 69 6f 6e 2c 20 74 68 65 20 64 61 74 61 ration, the data
1906c 62 61 73 65 20 68 61 6e 64 6c 65 73 20 61 6e 64 base handles and
1906d 2f 6f 72 0a 2a 2a 20 20 20 74 68 65 20 42 74 72 /or.** the Btr
1906e 65 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 20 68 ee objects may h
1906f 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 6d 75 ave their own mu
19070 74 65 78 65 73 20 74 68 61 74 20 72 65 71 75 69 texes that requi
19071 72 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 re locking..**
19072 20 4e 6f 6e 2d 73 68 61 72 61 62 6c 65 20 42 74 Non-sharable Bt
19073 72 65 65 73 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 rees (in-memory
19074 64 61 74 61 62 61 73 65 73 20 66 6f 72 20 65 78 databases for ex
19075 61 6d 70 6c 65 29 2c 20 64 6f 20 6e 6f 74 20 68 ample), do not h
19076 61 76 65 0a 2a 2a 20 20 20 61 73 73 6f 63 69 61 ave.** associa
19077 74 65 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a ted mutexes..*/.
19078 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
19079 70 6f 69 6e 74 65 72 20 63 6f 72 72 65 73 70 6f pointer correspo
1907a 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73 nding to databas
1907b 65 20 7a 44 62 20 28 69 2e 65 2e 20 22 6d 61 69 e zDb (i.e. "mai
1907c 6e 22 2c 20 22 74 65 6d 70 22 29 0a 2a 2a 20 69 n", "temp").** i
1907d 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e n connection han
1907e 64 6c 65 20 70 44 62 2e 20 49 66 20 73 75 63 68 dle pDb. If such
1907f 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e a database cann
19080 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65 74 ot be found, ret
19081 75 72 6e 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f urn.** a NULL po
19082 69 6e 74 65 72 20 61 6e 64 20 77 72 69 74 65 20 inter and write
19083 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
19084 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2a to pErrorDb..**
19085 0a 2a 2a 20 49 66 20 74 68 65 20 22 74 65 6d 70 .** If the "temp
19086 22 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 " database is re
19087 71 75 65 73 74 65 64 2c 20 69 74 20 6d 61 79 20 quested, it may
19088 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 need to be opene
19089 64 20 62 79 20 74 68 69 73 20 0a 2a 2a 20 66 75 d by this .** fu
1908a 6e 63 74 69 6f 6e 2e 20 49 66 20 61 6e 20 65 72 nction. If an er
1908b 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
1908c 20 64 6f 69 6e 67 20 73 6f 2c 20 72 65 74 75 72 doing so, retur
1908d 6e 20 30 20 61 6e 64 20 77 72 69 74 65 20 61 6e n 0 and write an
1908e 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 .** error messa
1908f 67 65 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a ge to pErrorDb..
19090 2a 2f 0a 73 74 61 74 69 63 20 42 74 72 65 65 20 */.static Btree
19091 2a 66 69 6e 64 42 74 72 65 65 28 73 71 6c 69 74 *findBtree(sqlit
19092 65 33 20 2a 70 45 72 72 6f 72 44 62 2c 20 73 71 e3 *pErrorDb, sq
19093 6c 69 74 65 33 20 2a 70 44 62 2c 20 63 6f 6e 73 lite3 *pDb, cons
19094 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 t char *zDb){.
19095 69 6e 74 20 69 20 3d 20 73 71 6c 69 74 65 33 46 int i = sqlite3F
19096 69 6e 64 44 62 4e 61 6d 65 28 70 44 62 2c 20 7a indDbName(pDb, z
19097 44 62 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 31 Db);.. if( i==1
19098 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 ){. Parse *p
19099 50 61 72 73 65 3b 0a 20 20 20 20 69 6e 74 20 72 Parse;. int r
1909a 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 c = 0;. pPars
1909b 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b e = sqlite3Stack
1909c 41 6c 6c 6f 63 5a 65 72 6f 28 70 45 72 72 6f 72 AllocZero(pError
1909d 44 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 Db, sizeof(*pPar
1909e 73 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70 50 se));. if( pP
1909f 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 arse==0 ){.
190a0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 sqlite3Error(pE
190a1 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f 4e rrorDb, SQLITE_N
190a2 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f 66 20 6d 65 OMEM, "out of me
190a3 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 72 63 mory");. rc
190a4 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
190a5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
190a6 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20 70 pParse->db = p
190a7 44 62 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 Db;. if( sq
190a8 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 lite3OpenTempDat
190a9 61 62 61 73 65 28 70 50 61 72 73 65 29 20 29 7b abase(pParse) ){
190aa 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
190ab 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 ErrorClear(pPars
190ac 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 e);. sqli
190ad 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44 te3Error(pErrorD
190ae 62 2c 20 70 50 61 72 73 65 2d 3e 72 63 2c 20 22 b, pParse->rc, "
190af 25 73 22 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 %s", pParse->zEr
190b0 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 72 rMsg);. r
190b1 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
190b2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
190b3 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 sqlite3StackFree
190b4 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72 73 (pErrorDb, pPars
190b5 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 e);. }. if
190b6 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 ( rc ){. re
190b7 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
190b8 7d 0a 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a }.. if( i<0 ){.
190b9 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
190ba 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 (pErrorDb, SQLIT
190bb 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77 E_ERROR, "unknow
190bc 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 n database %s",
190bd 7a 44 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e zDb);. return
190be 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 0;. }.. retur
190bf 6e 20 70 44 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 n pDb->aDb[i].pB
190c0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 t;.}../*.** Crea
190c1 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 te an sqlite3_ba
190c2 63 6b 75 70 20 70 72 6f 63 65 73 73 20 74 6f 20 ckup process to
190c3 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 copy the content
190c4 73 20 6f 66 20 7a 53 72 63 44 62 20 66 72 6f 6d s of zSrcDb from
190c5 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 .** connection h
190c6 61 6e 64 6c 65 20 70 53 72 63 44 62 20 74 6f 20 andle pSrcDb to
190c7 7a 44 65 73 74 44 62 20 69 6e 20 70 44 65 73 74 zDestDb in pDest
190c8 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 Db. If successfu
190c9 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 l, return.** a p
190ca 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
190cb 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 w sqlite3_backup
190cc 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 object..**.** I
190cd 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
190ce 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 s, NULL is retur
190cf 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 ned and an error
190d0 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20 code and error
190d1 6d 65 73 73 61 67 65 0a 2a 2a 20 73 74 6f 72 65 message.** store
190d2 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 68 61 d in database ha
190d3 6e 64 6c 65 20 70 44 65 73 74 44 62 2e 0a 2a 2f ndle pDestDb..*/
190d4 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
190d5 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 te3_backup *sqli
190d6 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
190d7 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73 . sqlite3* pDes
190d8 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 tDb,
190d9 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
190da 62 61 73 65 20 74 6f 20 77 72 69 74 65 20 74 6f base to write to
190db 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
190dc 20 2a 7a 44 65 73 74 44 62 2c 20 20 20 20 20 20 *zDestDb,
190dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
190de 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 ame of database
190df 77 69 74 68 69 6e 20 70 44 65 73 74 44 62 20 2a within pDestDb *
190e0 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72 /. sqlite3* pSr
190e1 63 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 cDb,
190e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
190e3 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
190e4 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f to read from */
190e5 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
190e6 53 72 63 44 62 20 20 20 20 20 20 20 20 20 20 20 SrcDb
190e7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
190e8 20 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74 of database wit
190e9 68 69 6e 20 70 53 72 63 44 62 20 2a 2f 0a 29 7b hin pSrcDb */.){
190ea 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
190eb 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 p *p;
190ec 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
190ed 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a e to return */..
190ee 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 73 6f /* Lock the so
190ef 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 urce database ha
190f0 6e 64 6c 65 2e 20 54 68 65 20 64 65 73 74 69 6e ndle. The destin
190f1 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 20 ation database.
190f2 20 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f ** handle is no
190f3 74 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 69 73 t locked in this
190f4 20 72 6f 75 74 69 6e 65 2c 20 62 75 74 20 69 74 routine, but it
190f5 20 69 73 20 6c 6f 63 6b 65 64 20 69 6e 0a 20 20 is locked in.
190f6 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
190f7 70 5f 73 74 65 70 28 29 2e 20 54 68 65 20 75 73 p_step(). The us
190f8 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20 74 er is required t
190f9 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 6e 6f o ensure that no
190fa 0a 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 72 65 . ** other thre
190fb 61 64 20 61 63 63 65 73 73 65 73 20 74 68 65 20 ad accesses the
190fc 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 6e 64 destination hand
190fd 6c 65 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 le for the durat
190fe 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 ion. ** of the
190ff 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
19100 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 . Any attempt t
19101 6f 20 75 73 65 20 74 68 65 20 64 65 73 74 69 6e o use the destin
19102 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 61 74 61 62 ation. ** datab
19103 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 ase connection w
19104 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73 hile a backup is
19105 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 61 79 in progress may
19106 20 63 61 75 73 65 0a 20 20 2a 2a 20 61 20 6d 61 cause. ** a ma
19107 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20 64 lfunction or a d
19108 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 eadlock.. */.
19109 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1910a 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 ter(pSrcDb->mute
1910b 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 x);. sqlite3_mu
1910c 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73 74 44 tex_enter(pDestD
1910d 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69 66 b->mutex);.. if
1910e 28 20 70 53 72 63 44 62 3d 3d 70 44 65 73 74 44 ( pSrcDb==pDestD
1910f 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
19110 45 72 72 6f 72 28 0a 20 20 20 20 20 20 20 20 70 Error(. p
19111 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 45 DestDb, SQLITE_E
19112 52 52 4f 52 2c 20 22 73 6f 75 72 63 65 20 61 6e RROR, "source an
19113 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 75 d destination mu
19114 73 74 20 62 65 20 64 69 73 74 69 6e 63 74 22 0a st be distinct".
19115 20 20 20 20 29 3b 0a 20 20 20 20 70 20 3d 20 30 );. p = 0
19116 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20 ;. }else {.
19117 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 /* Allocate spac
19118 65 20 66 6f 72 20 61 20 6e 65 77 20 73 71 6c 69 e for a new sqli
19119 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 te3_backup objec
1911a 74 20 2a 2f 0a 20 20 20 20 70 20 3d 20 28 73 71 t */. p = (sq
1911b 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 73 lite3_backup *)s
1911c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
1911d 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 zeof(sqlite3_bac
1911e 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 21 kup));. if( !
1911f 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 p ){. sqlit
19120 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 2c e3Error(pDestDb,
19121 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 SQLITE_NOMEM, 0
19122 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 );. }. }..
19123 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 /* If the alloca
19124 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c 20 tion succeeded,
19125 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77 populate the new
19126 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 66 object. */. if
19127 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 ( p ){. memse
19128 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 t(p, 0, sizeof(s
19129 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 3b qlite3_backup));
1912a 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 66 . p->pSrc = f
1912b 69 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62 indBtree(pDestDb
1912c 2c 20 70 53 72 63 44 62 2c 20 7a 53 72 63 44 62 , pSrcDb, zSrcDb
1912d 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 20 );. p->pDest
1912e 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65 73 = findBtree(pDes
1912f 74 44 62 2c 20 70 44 65 73 74 44 62 2c 20 7a 44 tDb, pDestDb, zD
19130 65 73 74 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70 estDb);. p->p
19131 44 65 73 74 44 62 20 3d 20 70 44 65 73 74 44 62 DestDb = pDestDb
19132 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 44 62 20 ;. p->pSrcDb
19133 3d 20 70 53 72 63 44 62 3b 0a 20 20 20 20 70 2d = pSrcDb;. p-
19134 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 20 20 >iNext = 1;.
19135 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 p->isAttached =
19136 30 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 0;.. if( 0==p
19137 2d 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e ->pSrc || 0==p->
19138 70 44 65 73 74 20 29 7b 0a 20 20 20 20 20 20 2f pDest ){. /
19139 2a 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29 20 * One (or both)
1913a 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 of the named dat
1913b 61 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20 65 abases did not e
1913c 78 69 73 74 2e 20 41 6e 20 65 72 72 6f 72 20 68 xist. An error h
1913d 61 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 as. ** alre
1913e 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e ady been written
1913f 20 69 6e 74 6f 20 74 68 65 20 70 44 65 73 74 44 into the pDestD
19140 62 20 68 61 6e 64 6c 65 2e 20 41 6c 6c 20 74 68 b handle. All th
19141 61 74 20 69 73 20 6c 65 66 74 0a 20 20 20 20 20 at is left.
19142 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69 ** to do here i
19143 73 20 66 72 65 65 20 74 68 65 20 73 71 6c 69 74 s free the sqlit
19144 65 33 5f 62 61 63 6b 75 70 20 73 74 72 75 63 74 e3_backup struct
19145 75 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ure.. */.
19146 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
19147 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 30 (p);. p = 0
19148 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
19149 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 ( p ){. p->pS
1914a 72 63 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20 rc->nBackup++;.
1914b 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 }.. sqlite3_mu
1914c 74 65 78 5f 6c 65 61 76 65 28 70 44 65 73 74 44 tex_leave(pDestD
1914d 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c b->mutex);. sql
1914e 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1914f 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b (pSrcDb->mutex);
19150 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
19151 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 72 /*.** Argument r
19152 63 20 69 73 20 61 6e 20 53 51 4c 69 74 65 20 65 c is an SQLite e
19153 72 72 6f 72 20 63 6f 64 65 2e 20 52 65 74 75 72 rror code. Retur
19154 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 20 65 n true if this e
19155 72 72 6f 72 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 rror is .** cons
19156 69 64 65 72 65 64 20 66 61 74 61 6c 20 69 66 20 idered fatal if
19157 65 6e 63 6f 75 6e 74 65 72 65 64 20 64 75 72 69 encountered duri
19158 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72 ng a backup oper
19159 61 74 69 6f 6e 2e 20 41 6c 6c 20 65 72 72 6f 72 ation. All error
1915a 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 s.** are conside
1915b 72 65 64 20 66 61 74 61 6c 20 65 78 63 65 70 74 red fatal except
1915c 20 66 6f 72 20 53 51 4c 49 54 45 5f 42 55 53 59 for SQLITE_BUSY
1915d 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b and SQLITE_LOCK
1915e 45 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ED..*/.static in
1915f 74 20 69 73 46 61 74 61 6c 45 72 72 6f 72 28 69 t isFatalError(i
19160 6e 74 20 72 63 29 7b 0a 20 20 72 65 74 75 72 6e nt rc){. return
19161 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 (rc!=SQLITE_OK
19162 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 && rc!=SQLITE_BU
19163 53 59 20 26 26 20 41 4c 57 41 59 53 28 72 63 21 SY && ALWAYS(rc!
19164 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 29 =SQLITE_LOCKED))
19165 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d ;.}../*.** Param
19166 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70 6f eter zSrcData po
19167 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 ints to a buffer
19168 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
19169 64 61 74 61 20 66 6f 72 20 0a 2a 2a 20 70 61 67 data for .** pag
1916a 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20 74 68 e iSrcPg from th
1916b 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
1916c 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64 61 74 e. Copy this dat
1916d 61 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 64 a into the .** d
1916e 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
1916f 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ase..*/.static i
19170 6e 74 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 nt backupOnePage
19171 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
19172 2a 70 2c 20 50 67 6e 6f 20 69 53 72 63 50 67 2c *p, Pgno iSrcPg,
19173 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 72 63 44 const u8 *zSrcD
19174 61 74 61 29 7b 0a 20 20 50 61 67 65 72 20 2a 20 ata){. Pager *
19175 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72 const pDestPager
19176 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 = sqlite3BtreeP
19177 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 0a ager(p->pDest);.
19178 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63 const int nSrc
19179 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 Pgsz = sqlite3Bt
1917a 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 reeGetPageSize(p
1917b 2d 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e ->pSrc);. int n
1917c 44 65 73 74 50 67 73 7a 20 3d 20 73 71 6c 69 74 DestPgsz = sqlit
1917d 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 e3BtreeGetPageSi
1917e 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 ze(p->pDest);.
1917f 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f 70 79 20 const int nCopy
19180 3d 20 4d 49 4e 28 6e 53 72 63 50 67 73 7a 2c 20 = MIN(nSrcPgsz,
19181 6e 44 65 73 74 50 67 73 7a 29 3b 0a 20 20 63 6f nDestPgsz);. co
19182 6e 73 74 20 69 36 34 20 69 45 6e 64 20 3d 20 28 nst i64 iEnd = (
19183 69 36 34 29 69 53 72 63 50 67 2a 28 69 36 34 29 i64)iSrcPg*(i64)
19184 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 nSrcPgsz;.. int
19185 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
19186 0a 20 20 69 36 34 20 69 4f 66 66 3b 0a 0a 20 20 . i64 iOff;..
19187 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 73 74 assert( p->bDest
19188 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 73 65 Locked );. asse
19189 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f rt( !isFatalErro
1918a 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 61 73 r(p->rc) );. as
1918b 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50 45 sert( iSrcPg!=PE
1918c 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
1918d 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 3b p->pSrc->pBt) );
1918e 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63 44 . assert( zSrcD
1918f 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 ata );.. /* Cat
19190 63 68 20 74 68 65 20 63 61 73 65 20 77 68 65 72 ch the case wher
19191 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f e the destinatio
19192 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 n is an in-memor
19193 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 y database and t
19194 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 69 7a he. ** page siz
19195 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 es of the source
19196 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e and destination
19197 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 differ. . */.
19198 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 3d 6e if( nSrcPgsz!=n
19199 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 6c 69 DestPgsz && sqli
1919a 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 te3PagerIsMemdb(
1919b 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 sqlite3BtreePage
1919c 72 28 70 2d 3e 70 44 65 73 74 29 29 20 29 7b 0a r(p->pDest)) ){.
1919d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1919e 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 READONLY;. }..
1919f 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72 75 /* This loop ru
191a0 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 ns once for each
191a1 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 destination pag
191a2 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68 65 e spanned by the
191a3 20 73 6f 75 72 63 65 20 0a 20 20 2a 2a 20 70 61 source . ** pa
191a4 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69 74 65 ge. For each ite
191a5 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62 6c 65 ration, variable
191a6 20 69 4f 66 66 20 69 73 20 73 65 74 20 74 6f 20 iOff is set to
191a7 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a the byte offset.
191a8 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 73 74 ** of the dest
191a9 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 ination page..
191aa 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 66 3d 69 45 */. for(iOff=iE
191ab 6e 64 2d 28 69 36 34 29 6e 53 72 63 50 67 73 7a nd-(i64)nSrcPgsz
191ac 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ; rc==SQLITE_OK
191ad 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 69 4f && iOff<iEnd; iO
191ae 66 66 2b 3d 6e 44 65 73 74 50 67 73 7a 29 7b 0a ff+=nDestPgsz){.
191af 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 65 73 DbPage *pDes
191b0 74 50 67 20 3d 20 30 3b 0a 20 20 20 20 50 67 6e tPg = 0;. Pgn
191b1 6f 20 69 44 65 73 74 20 3d 20 28 50 67 6e 6f 29 o iDest = (Pgno)
191b2 28 69 4f 66 66 2f 6e 44 65 73 74 50 67 73 7a 29 (iOff/nDestPgsz)
191b3 2b 31 3b 0a 20 20 20 20 69 66 28 20 69 44 65 73 +1;. if( iDes
191b4 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t==PENDING_BYTE_
191b5 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 PAGE(p->pDest->p
191b6 42 74 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a Bt) ) continue;.
191b7 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
191b8 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K==(rc = sqlite3
191b9 50 61 67 65 72 47 65 74 28 70 44 65 73 74 50 61 PagerGet(pDestPa
191ba 67 65 72 2c 20 69 44 65 73 74 2c 20 26 70 44 65 ger, iDest, &pDe
191bb 73 74 50 67 29 29 0a 20 20 20 20 20 26 26 20 53 stPg)). && S
191bc 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
191bd 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
191be 65 28 70 44 65 73 74 50 67 29 29 0a 20 20 20 20 e(pDestPg)).
191bf 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 ){. const u
191c0 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 72 63 44 61 8 *zIn = &zSrcDa
191c1 74 61 5b 69 4f 66 66 25 6e 53 72 63 50 67 73 7a ta[iOff%nSrcPgsz
191c2 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 44 65 ];. u8 *zDe
191c3 73 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 stData = sqlite3
191c4 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 65 PagerGetData(pDe
191c5 73 74 50 67 29 3b 0a 20 20 20 20 20 20 75 38 20 stPg);. u8
191c6 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 73 74 44 61 *zOut = &zDestDa
191c7 74 61 5b 69 4f 66 66 25 6e 44 65 73 74 50 67 73 ta[iOff%nDestPgs
191c8 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f z];.. /* Co
191c9 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d py the data from
191ca 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 the source page
191cb 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e into the destin
191cc 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 20 20 ation page..
191cd 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72 20 ** Then clear
191ce 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 20 the Btree layer
191cf 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 66 MemPage.isInit f
191d0 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20 6d lag. Both this m
191d1 6f 64 75 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 odule. ** a
191d2 6e 64 20 74 68 65 20 70 61 67 65 72 20 63 6f 64 nd the pager cod
191d3 65 20 75 73 65 20 74 68 69 73 20 74 72 69 63 6b e use this trick
191d4 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65 20 66 (clearing the f
191d5 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 20 20 irst byte.
191d6 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 27 ** of the page '
191d7 65 78 74 72 61 27 20 73 70 61 63 65 20 74 6f 20 extra' space to
191d8 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 42 invalidate the B
191d9 74 72 65 65 20 6c 61 79 65 72 73 0a 20 20 20 20 tree layers.
191da 20 20 2a 2a 20 63 61 63 68 65 64 20 70 61 72 73 ** cached pars
191db 65 20 6f 66 20 74 68 65 20 70 61 67 65 29 2e 20 e of the page).
191dc 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 69 MemPage.isInit i
191dd 73 20 6d 61 72 6b 65 64 20 0a 20 20 20 20 20 20 s marked .
191de 2a 2a 20 22 4d 55 53 54 20 42 45 20 46 49 52 53 ** "MUST BE FIRS
191df 54 22 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 T" for this purp
191e0 6f 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ose.. */.
191e1 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c memcpy(zOut,
191e2 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 zIn, nCopy);.
191e3 20 20 20 20 28 28 75 38 20 2a 29 73 71 6c 69 74 ((u8 *)sqlit
191e4 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
191e5 70 44 65 73 74 50 67 29 29 5b 30 5d 20 3d 20 30 pDestPg))[0] = 0
191e6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
191e7 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 te3PagerUnref(pD
191e8 65 73 74 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 estPg);. }.. r
191e9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
191ea 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20 63 ** If pFile is c
191eb 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 urrently larger
191ec 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 73 than iSize bytes
191ed 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 , then truncate
191ee 69 74 20 74 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 it to.** exactly
191ef 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20 49 66 iSize bytes. If
191f0 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20 6c 61 pFile is not la
191f1 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20 rger than iSize
191f2 62 79 74 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 bytes, then.** t
191f3 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
191f4 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 a no-op..**.** R
191f5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
191f6 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 if everything is
191f7 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 successful, or
191f8 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 an SQLite error
191f9 0a 2a 2a 20 63 6f 64 65 20 69 66 20 61 6e 20 65 .** code if an e
191fa 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a rror occurs..*/.
191fb 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75 static int backu
191fc 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 73 71 pTruncateFile(sq
191fd 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c lite3_file *pFil
191fe 65 2c 20 69 36 34 20 69 53 69 7a 65 29 7b 0a 20 e, i64 iSize){.
191ff 20 69 36 34 20 69 43 75 72 72 65 6e 74 3b 0a 20 i64 iCurrent;.
19200 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
19201 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 3OsFileSize(pFil
19202 65 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20 e, &iCurrent);.
19203 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
19204 4f 4b 20 26 26 20 69 43 75 72 72 65 6e 74 3e 69 OK && iCurrent>i
19205 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 20 3d Size ){. rc =
19206 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
19207 74 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 te(pFile, iSize)
19208 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
19209 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 c;.}../*.** Regi
1920a 73 74 65 72 20 74 68 69 73 20 62 61 63 6b 75 70 ster this backup
1920b 20 6f 62 6a 65 63 74 20 77 69 74 68 20 74 68 65 object with the
1920c 20 61 73 73 6f 63 69 61 74 65 64 20 73 6f 75 72 associated sour
1920d 63 65 20 70 61 67 65 72 20 66 6f 72 0a 2a 2a 20 ce pager for.**
1920e 63 61 6c 6c 62 61 63 6b 73 20 77 68 65 6e 20 70 callbacks when p
1920f 61 67 65 73 20 61 72 65 20 63 68 61 6e 67 65 64 ages are changed
19210 20 6f 72 20 74 68 65 20 63 61 63 68 65 20 69 6e or the cache in
19211 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 validated..*/.st
19212 61 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 68 atic void attach
19213 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 73 71 6c BackupObject(sql
19214 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b ite3_backup *p){
19215 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 . sqlite3_backu
19216 70 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 p **pp;. assert
19217 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
19218 6c 64 73 4d 75 74 65 78 28 70 2d 3e 70 53 72 63 ldsMutex(p->pSrc
19219 29 20 29 3b 0a 20 20 70 70 20 3d 20 73 71 6c 69 ) );. pp = sqli
1921a 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 te3PagerBackupPt
1921b 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 r(sqlite3BtreePa
1921c 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 ger(p->pSrc));.
1921d 20 70 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 70 3b p->pNext = *pp;
1921e 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 70 2d . *pp = p;. p-
1921f 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 31 3b >isAttached = 1;
19220 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e .}../*.** Copy n
19221 50 61 67 65 20 70 61 67 65 73 20 66 72 6f 6d 20 Page pages from
19222 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74 72 65 the source b-tre
19223 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 e to the destina
19224 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
19225 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
19226 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 backup_step(sqli
19227 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 te3_backup *p, i
19228 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 nt nPage){. int
19229 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f rc;.. sqlite3_
1922a 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 mutex_enter(p->p
1922b 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 SrcDb->mutex);.
1922c 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1922d 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 er(p->pSrc);. i
1922e 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b f( p->pDestDb ){
1922f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
19230 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 ex_enter(p->pDes
19231 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d tDb->mutex);. }
19232 0a 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a .. rc = p->rc;.
19233 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 72 if( !isFatalEr
19234 72 6f 72 28 72 63 29 20 29 7b 0a 20 20 20 20 50 ror(rc) ){. P
19235 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 53 72 ager * const pSr
19236 63 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 cPager = sqlite3
19237 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 BtreePager(p->pS
19238 72 63 29 3b 20 20 20 20 20 2f 2a 20 53 6f 75 72 rc); /* Sour
19239 63 65 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 ce pager */.
1923a 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 Pager * const pD
1923b 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74 estPager = sqlit
1923c 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e e3BtreePager(p->
1923d 70 44 65 73 74 29 3b 20 20 20 2f 2a 20 44 65 73 pDest); /* Des
1923e 74 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 69 t pager */. i
1923f 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 nt ii;
19240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19241 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 /* Iterator va
19242 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e riable */. in
19243 74 20 6e 53 72 63 50 61 67 65 20 3d 20 2d 31 3b t nSrcPage = -1;
19244 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19245 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 6f 75 72 /* Size of sour
19246 63 65 20 64 62 20 69 6e 20 70 61 67 65 73 20 2a ce db in pages *
19247 2f 0a 20 20 20 20 69 6e 74 20 62 43 6c 6f 73 65 /. int bClose
19248 54 72 61 6e 73 20 3d 20 30 3b 20 20 20 20 20 20 Trans = 0;
19249 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
1924a 20 69 66 20 73 72 63 20 64 62 20 72 65 71 75 69 if src db requi
1924b 72 65 73 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f res unlocking */
1924c 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 .. /* If the
1924d 73 6f 75 72 63 65 20 70 61 67 65 72 20 69 73 20 source pager is
1924e 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 77 currently in a w
1924f 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
19250 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 , return. **
19251 53 51 4c 49 54 45 5f 42 55 53 59 20 69 6d 6d 65 SQLITE_BUSY imme
19252 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a diately.. */.
19253 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 if( p->pDest
19254 44 62 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e 70 Db && p->pSrc->p
19255 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
19256 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n==TRANS_WRITE )
19257 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
19258 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 ITE_BUSY;. }e
19259 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1925a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d SQLITE_OK;. }
1925b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 .. /* Lock th
1925c 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
1925d 74 61 62 61 73 65 2c 20 69 66 20 69 74 20 69 73 tabase, if it is
1925e 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c 72 65 not locked alre
1925f 61 64 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 ady. */. if(
19260 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 SQLITE_OK==rc &&
19261 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 3d p->bDestLocked=
19262 3d 30 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 =0. && SQLIT
19263 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
19264 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 te3BtreeBeginTra
19265 6e 73 28 70 2d 3e 70 44 65 73 74 2c 20 32 29 29 ns(p->pDest, 2))
19266 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 . ){. p
19267 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 3d 20 ->bDestLocked =
19268 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 1;. sqlite3
19269 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2d 3e BtreeGetMeta(p->
1926a 70 44 65 73 74 2c 20 42 54 52 45 45 5f 53 43 48 pDest, BTREE_SCH
1926b 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 26 70 2d EMA_VERSION, &p-
1926c 3e 69 44 65 73 74 53 63 68 65 6d 61 29 3b 0a 20 >iDestSchema);.
1926d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
1926e 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e there is no open
1926f 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f read-transactio
19270 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 n on the source
19271 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 0a 20 database, open.
19272 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49 ** one now. I
19273 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 f a transaction
19274 69 73 20 6f 70 65 6e 65 64 20 68 65 72 65 2c 20 is opened here,
19275 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 20 then it will be
19276 63 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 62 65 closed. ** be
19277 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 fore this functi
19278 6f 6e 20 65 78 69 74 73 2e 0a 20 20 20 20 2a 2f on exits.. */
19279 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1927a 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c ITE_OK && 0==sql
1927b 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 ite3BtreeIsInRea
1927c 64 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 29 20 dTrans(p->pSrc)
1927d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1927e 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
1927f 72 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30 29 rans(p->pSrc, 0)
19280 3b 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54 72 ;. bCloseTr
19281 61 6e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 ans = 1;. }.
19282 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 74 68 61 . /* Now tha
19283 74 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61 t there is a rea
19284 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 6f d-lock on the so
19285 75 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 71 urce database, q
19286 75 65 72 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 uery the. **
19287 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f 72 source pager for
19288 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
19289 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 ages in the data
1928a 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 base.. */.
1928b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1928c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
1928d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
1928e 65 63 6f 75 6e 74 28 70 53 72 63 50 61 67 65 72 ecount(pSrcPager
1928f 2c 20 26 6e 53 72 63 50 61 67 65 29 3b 0a 20 20 , &nSrcPage);.
19290 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 }. for(ii=0
19291 3b 20 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 69 ; (nPage<0 || ii
19292 3c 6e 50 61 67 65 29 20 26 26 20 70 2d 3e 69 4e <nPage) && p->iN
19293 65 78 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 50 ext<=(Pgno)nSrcP
19294 61 67 65 20 26 26 20 21 72 63 3b 20 69 69 2b 2b age && !rc; ii++
19295 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 ){. const P
19296 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 70 2d 3e gno iSrcPg = p->
19297 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 iNext;
19298 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 /* Source
19299 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
1929a 20 20 20 20 20 20 69 66 28 20 69 53 72 63 50 67 if( iSrcPg
1929b 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
1929c 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 AGE(p->pSrc->pBt
1929d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 44 62 50 ) ){. DbP
1929e 61 67 65 20 2a 70 53 72 63 50 67 3b 20 20 20 20 age *pSrcPg;
1929f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
192a0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 /* Sour
192a1 63 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 2a ce page object *
192a2 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 /. rc = s
192a3 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
192a4 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 SrcPager, iSrcPg
192a5 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 , &pSrcPg);.
192a6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
192a7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
192a8 20 20 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e rc = backupOn
192a9 65 50 61 67 65 28 70 2c 20 69 53 72 63 50 67 2c ePage(p, iSrcPg,
192aa 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
192ab 44 61 74 61 28 70 53 72 63 50 67 29 29 3b 0a 20 Data(pSrcPg));.
192ac 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
192ad 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 50 PagerUnref(pSrcP
192ae 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 g);. }.
192af 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 69 }. p->i
192b0 4e 65 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 Next++;. }.
192b1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
192b2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e _OK ){. p->
192b3 6e 50 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 72 nPagecount = nSr
192b4 63 50 61 67 65 3b 0a 20 20 20 20 20 20 70 2d 3e cPage;. p->
192b5 6e 52 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 72 nRemaining = nSr
192b6 63 50 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 cPage+1-p->iNext
192b7 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 ;. if( p->i
192b8 4e 65 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 50 Next>(Pgno)nSrcP
192b9 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 age ){. r
192ba 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
192bb 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
192bc 20 21 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 !p->isAttached
192bd 29 7b 0a 20 20 20 20 20 20 20 20 61 74 74 61 63 ){. attac
192be 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 29 hBackupObject(p)
192bf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
192c0 20 20 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 . /* Update
192c1 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 the schema vers
192c2 69 6f 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 65 ion field in the
192c3 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
192c4 61 62 61 73 65 2e 20 54 68 69 73 0a 20 20 20 20 abase. This.
192c5 2a 2a 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 75 ** is to make su
192c6 72 65 20 74 68 61 74 20 74 68 65 20 73 63 68 65 re that the sche
192c7 6d 61 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c ma-version reall
192c8 79 20 64 6f 65 73 20 63 68 61 6e 67 65 20 69 6e y does change in
192c9 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65 . ** the case
192ca 20 77 68 65 72 65 20 74 68 65 20 73 6f 75 72 63 where the sourc
192cb 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f e and destinatio
192cc 6e 20 64 61 74 61 62 61 73 65 73 20 68 61 76 65 n databases have
192cd 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d 65 the. ** same
192ce 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e schema version.
192cf 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
192d0 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
192d1 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d 20 73 . && (rc = s
192d2 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 qlite3BtreeUpdat
192d3 65 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 31 eMeta(p->pDest,1
192d4 2c 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 2b ,p->iDestSchema+
192d5 31 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 1))==SQLITE_OK.
192d6 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 ){. cons
192d7 74 20 69 6e 74 20 6e 53 72 63 50 61 67 65 73 69 t int nSrcPagesi
192d8 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 ze = sqlite3Btre
192d9 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e eGetPageSize(p->
192da 70 53 72 63 29 3b 0a 20 20 20 20 20 20 63 6f 6e pSrc);. con
192db 73 74 20 69 6e 74 20 6e 44 65 73 74 50 61 67 65 st int nDestPage
192dc 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 size = sqlite3Bt
192dd 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 reeGetPageSize(p
192de 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20 ->pDest);.
192df 69 6e 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74 int nDestTruncat
192e0 65 3b 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20 e;. . if(
192e1 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 p->pDestDb ){.
192e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 sqlite3Res
192e3 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 etInternalSchema
192e4 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 30 29 3b (p->pDestDb, 0);
192e5 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
192e6 2f 2a 20 53 65 74 20 6e 44 65 73 74 54 72 75 6e /* Set nDestTrun
192e7 63 61 74 65 20 74 6f 20 74 68 65 20 66 69 6e 61 cate to the fina
192e8 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
192e9 73 20 69 6e 20 74 68 65 20 64 65 73 74 69 6e 61 s in the destina
192ea 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 64 61 tion. ** da
192eb 74 61 62 61 73 65 2e 20 54 68 65 20 63 6f 6d 70 tabase. The comp
192ec 6c 69 63 61 74 69 6f 6e 20 68 65 72 65 20 69 73 lication here is
192ed 20 74 68 61 74 20 74 68 65 20 64 65 73 74 69 6e that the destin
192ee 61 74 69 6f 6e 20 70 61 67 65 0a 20 20 20 20 20 ation page.
192ef 20 2a 2a 20 73 69 7a 65 20 6d 61 79 20 62 65 20 ** size may be
192f0 64 69 66 66 65 72 65 6e 74 20 74 6f 20 74 68 65 different to the
192f1 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a source page siz
192f2 65 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 e. . **.
192f3 20 20 20 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 ** If the sou
192f4 72 63 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 rce page size is
192f5 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 smaller than th
192f6 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 e destination pa
192f7 67 65 20 73 69 7a 65 2c 20 0a 20 20 20 20 20 20 ge size, .
192f8 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 49 6e 20 ** round up. In
192f9 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 this case the ca
192fa 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 54 ll to sqlite3OsT
192fb 72 75 6e 63 61 74 65 28 29 20 62 65 6c 6f 77 20 runcate() below
192fc 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69 will. ** fi
192fd 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 x the size of th
192fe 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 65 72 20 e file. However
192ff 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 it is important
19300 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 20 2a 2a to call. **
19301 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 sqlite3PagerTru
19302 6e 63 61 74 65 49 6d 61 67 65 28 29 20 68 65 72 ncateImage() her
19303 65 20 73 6f 20 74 68 61 74 20 61 6e 79 20 70 61 e so that any pa
19304 67 65 73 20 69 6e 20 74 68 65 20 0a 20 20 20 20 ges in the .
19305 20 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e ** destination
19306 20 66 69 6c 65 20 74 68 61 74 20 6c 69 65 20 62 file that lie b
19307 65 79 6f 6e 64 20 74 68 65 20 6e 44 65 73 74 54 eyond the nDestT
19308 72 75 6e 63 61 74 65 20 70 61 67 65 20 6d 61 72 runcate page mar
19309 6b 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 6a k are. ** j
1930a 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20 50 61 67 ournalled by Pag
1930b 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 erCommitPhaseOne
1930c 28 29 20 62 65 66 6f 72 65 20 74 68 65 79 20 61 () before they a
1930d 72 65 20 64 65 73 74 72 6f 79 65 64 0a 20 20 20 re destroyed.
1930e 20 20 20 2a 2a 20 62 79 20 74 68 65 20 66 69 6c ** by the fil
1930f 65 20 74 72 75 6e 63 61 74 69 6f 6e 2e 0a 20 20 e truncation..
19310 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
19311 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 nSrcPagesize<nD
19312 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 estPagesize ){.
19313 20 20 20 20 20 20 20 69 6e 74 20 72 61 74 69 6f int ratio
19314 20 3d 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 = nDestPagesize
19315 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 3b 0a 20 /nSrcPagesize;.
19316 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e nDestTrun
19317 63 61 74 65 20 3d 20 28 6e 53 72 63 50 61 67 65 cate = (nSrcPage
19318 2b 72 61 74 69 6f 2d 31 29 2f 72 61 74 69 6f 3b +ratio-1)/ratio;
19319 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 44 65 . if( nDe
1931a 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 stTruncate==(int
1931b 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 )PENDING_BYTE_PA
1931c 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 GE(p->pDest->pBt
1931d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e ) ){. n
1931e 44 65 73 74 54 72 75 6e 63 61 74 65 2d 2d 3b 0a DestTruncate--;.
1931f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
19320 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e }else{. n
19321 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 6e DestTruncate = n
19322 53 72 63 50 61 67 65 20 2a 20 28 6e 53 72 63 50 SrcPage * (nSrcP
19323 61 67 65 73 69 7a 65 2f 6e 44 65 73 74 50 61 67 agesize/nDestPag
19324 65 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a esize);. }.
19325 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
19326 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 erTruncateImage(
19327 70 44 65 73 74 50 61 67 65 72 2c 20 6e 44 65 73 pDestPager, nDes
19328 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a 20 20 20 tTruncate);..
19329 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65 73 if( nSrcPages
1932a 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69 7a ize<nDestPagesiz
1932b 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 e ){. /*
1932c 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 If the source pa
1932d 67 65 2d 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c ge-size is small
1932e 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 73 74 er than the dest
1932f 69 6e 61 74 69 6f 6e 20 70 61 67 65 2d 73 69 7a ination page-siz
19330 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 77 e,. ** tw
19331 6f 20 65 78 74 72 61 20 74 68 69 6e 67 73 20 6d o extra things m
19332 61 79 20 6e 65 65 64 20 74 6f 20 68 61 70 70 65 ay need to happe
19333 6e 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 n:. **.
19334 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 54 68 65 ** * The
19335 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 61 79 destination may
19336 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75 6e need to be trun
19337 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 20 20 20 cated, and.
19338 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
19339 20 20 20 2a 20 44 61 74 61 20 73 74 6f 72 65 64 * Data stored
1933a 20 6f 6e 20 74 68 65 20 70 61 67 65 73 20 69 6d on the pages im
1933b 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 mediately follow
1933c 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20 ing the .
1933d 20 2a 2a 20 20 20 20 20 70 65 6e 64 69 6e 67 2d ** pending-
1933e 62 79 74 65 20 70 61 67 65 20 69 6e 20 74 68 65 byte page in the
1933f 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
19340 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 0a may need to be.
19341 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 63 ** c
19342 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64 opied into the d
19343 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 estination datab
19344 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ase.. */.
19345 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 36 const i6
19346 34 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29 6e 4 iSize = (i64)n
19347 53 72 63 50 61 67 65 73 69 7a 65 20 2a 20 28 69 SrcPagesize * (i
19348 36 34 29 6e 53 72 63 50 61 67 65 3b 0a 20 20 20 64)nSrcPage;.
19349 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c sqlite3_fil
1934a 65 20 2a 20 63 6f 6e 73 74 20 70 46 69 6c 65 20 e * const pFile
1934b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 = sqlite3PagerFi
1934c 6c 65 28 70 44 65 73 74 50 61 67 65 72 29 3b 0a le(pDestPager);.
1934d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1934e 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 pFile );.
1934f 20 20 61 73 73 65 72 74 28 20 28 69 36 34 29 6e assert( (i64)n
19350 44 65 73 74 54 72 75 6e 63 61 74 65 2a 28 69 36 DestTruncate*(i6
19351 34 29 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 4)nDestPagesize
19352 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 28 0a 20 20 >= iSize || (.
19353 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 nDes
19354 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 tTruncate==(int)
19355 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 (PENDING_BYTE_PA
19356 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 GE(p->pDest->pBt
19357 29 2d 31 29 0a 20 20 20 20 20 20 20 20 20 20 20 )-1).
19358 26 26 20 69 53 69 7a 65 3e 3d 50 45 4e 44 49 4e && iSize>=PENDIN
19359 47 5f 42 59 54 45 20 26 26 20 69 53 69 7a 65 3c G_BYTE && iSize<
1935a 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 44 =PENDING_BYTE+nD
1935b 65 73 74 50 61 67 65 73 69 7a 65 0a 20 20 20 20 estPagesize.
1935c 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20 20 ));.
1935d 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 if( SQLITE_OK==(
1935e 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
1935f 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
19360 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 31 pDestPager, 0, 1
19361 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53 )). && S
19362 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
19363 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 69 backupTruncateFi
19364 6c 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 le(pFile, iSize)
19365 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53 51 ). && SQ
19366 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 LITE_OK==(rc = s
19367 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 qlite3PagerSync(
19368 70 44 65 73 74 50 61 67 65 72 29 29 0a 20 20 20 pDestPager)).
19369 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
1936a 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20 20 20 i64 iOff;.
1936b 20 20 20 20 20 20 69 36 34 20 69 45 6e 64 20 3d i64 iEnd =
1936c 20 4d 49 4e 28 50 45 4e 44 49 4e 47 5f 42 59 54 MIN(PENDING_BYT
1936d 45 20 2b 20 6e 44 65 73 74 50 61 67 65 73 69 7a E + nDestPagesiz
1936e 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 e, iSize);.
1936f 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20 for(.
19370 20 20 20 20 20 20 69 4f 66 66 3d 50 45 4e 44 49 iOff=PENDI
19371 4e 47 5f 42 59 54 45 2b 6e 53 72 63 50 61 67 65 NG_BYTE+nSrcPage
19372 73 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 20 size; .
19373 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b rc==SQLITE_OK
19374 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 0a && iOff<iEnd; .
19375 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 iOff
19376 2b 3d 6e 53 72 63 50 61 67 65 73 69 7a 65 0a 20 +=nSrcPagesize.
19377 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 ){.
19378 20 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 PgHdr *p
19379 53 72 63 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 SrcPg = 0;.
1937a 20 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e const Pgn
1937b 6f 20 69 53 72 63 50 67 20 3d 20 28 50 67 6e 6f o iSrcPg = (Pgno
1937c 29 28 28 69 4f 66 66 2f 6e 53 72 63 50 61 67 65 )((iOff/nSrcPage
1937d 73 69 7a 65 29 2b 31 29 3b 0a 20 20 20 20 20 20 size)+1);.
1937e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1937f 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50 e3PagerGet(pSrcP
19380 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 ager, iSrcPg, &p
19381 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 SrcPg);.
19382 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
19383 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
19384 20 20 20 20 20 20 20 75 38 20 2a 7a 44 61 74 61 u8 *zData
19385 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
19386 65 74 44 61 74 61 28 70 53 72 63 50 67 29 3b 0a etData(pSrcPg);.
19387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63 rc
19388 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 = sqlite3OsWrit
19389 65 28 70 46 69 6c 65 2c 20 7a 44 61 74 61 2c 20 e(pFile, zData,
1938a 6e 53 72 63 50 61 67 65 73 69 7a 65 2c 20 69 4f nSrcPagesize, iO
1938b 66 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ff);.
1938c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 }. s
1938d 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
1938e 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 (pSrcPg);.
1938f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
19390 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
19391 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
19392 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
19393 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c eOne(pDestPager,
19394 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0, 0);. }.
19395 20 20 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 69 . /* Fini
19396 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68 sh committing th
19397 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f e transaction to
19398 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
19399 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 20 20 database. */.
1939a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
1939b 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 26 26 20 K==rc. &&
1939c 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
1939d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1939e 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 mitPhaseTwo(p->p
1939f 44 65 73 74 29 29 0a 20 20 20 20 20 20 29 7b 0a Dest)). ){.
193a0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
193a1 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 ITE_DONE;.
193a2 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
193a3 2a 20 49 66 20 62 43 6c 6f 73 65 54 72 61 6e 73 * If bCloseTrans
193a4 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
193a5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 70 65 his function ope
193a6 6e 65 64 20 61 20 72 65 61 64 20 74 72 61 6e 73 ned a read trans
193a7 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e action. ** on
193a8 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
193a9 62 61 73 65 2e 20 43 6c 6f 73 65 20 74 68 65 20 base. Close the
193aa 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e read transaction
193ab 20 68 65 72 65 2e 20 54 68 65 72 65 20 69 73 0a here. There is.
193ac 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 ** no need t
193ad 6f 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 o check the retu
193ae 72 6e 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65 rn values of the
193af 20 62 74 72 65 65 20 6d 65 74 68 6f 64 73 20 68 btree methods h
193b0 65 72 65 2c 20 61 73 0a 20 20 20 20 2a 2a 20 22 ere, as. ** "
193b1 63 6f 6d 6d 69 74 74 69 6e 67 22 20 61 20 72 65 committing" a re
193b2 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 ad-only transact
193b3 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e ion cannot fail.
193b4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
193b5 62 43 6c 6f 73 65 54 72 61 6e 73 20 29 7b 0a 20 bCloseTrans ){.
193b6 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 TESTONLY( i
193b7 6e 74 20 72 63 32 20 29 3b 0a 20 20 20 20 20 20 nt rc2 );.
193b8 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 20 3d TESTONLY( rc2 =
193b9 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 ) sqlite3BtreeC
193ba 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 2d ommitPhaseOne(p-
193bb 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 >pSrc, 0);.
193bc 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 7c TESTONLY( rc2 |
193bd 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65 = ) sqlite3Btree
193be 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
193bf 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 61 ->pSrc);. a
193c0 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49 ssert( rc2==SQLI
193c1 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 TE_OK );. }.
193c2 20 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 . p->rc = rc
193c3 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 ;. }. if( p->p
193c4 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 DestDb ){. sq
193c5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
193c6 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 e(p->pDestDb->mu
193c7 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 tex);. }. sqli
193c8 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 2d te3BtreeLeave(p-
193c9 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 >pSrc);. sqlite
193ca 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
193cb 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b >pSrcDb->mutex);
193cc 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
193cd 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 ./*.** Release a
193ce 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 ll resources ass
193cf 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6e 20 ociated with an
193d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a 20 sqlite3_backup*
193d1 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 handle..*/.SQLIT
193d2 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
193d3 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
193d4 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
193d5 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 p){. sqlite3_ba
193d6 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20 20 ckup **pp;
193d7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 74 /* Pt
193d8 72 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61 67 r to head of pag
193d9 65 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74 20 ers backup list
193da 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
193db 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 ex *mutex;
193dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 /* Mut
193dd 65 78 20 74 6f 20 70 72 6f 74 65 63 74 20 73 6f ex to protect so
193de 75 72 63 65 20 64 61 74 61 62 61 73 65 20 2a 2f urce database */
193df 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
193e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193e1 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
193e2 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 to return */..
193e3 20 2f 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 /* Enter the mu
193e4 74 65 78 65 73 20 2a 2f 0a 20 20 69 66 28 20 70 texes */. if( p
193e5 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==0 ) return SQL
193e6 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 ITE_OK;. sqlite
193e7 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3_mutex_enter(p-
193e8 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b >pSrcDb->mutex);
193e9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
193ea 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 nter(p->pSrc);.
193eb 20 6d 75 74 65 78 20 3d 20 70 2d 3e 70 53 72 63 mutex = p->pSrc
193ec 44 62 2d 3e 6d 75 74 65 78 3b 0a 20 20 69 66 28 Db->mutex;. if(
193ed 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 p->pDestDb ){.
193ee 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
193ef 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 _enter(p->pDestD
193f0 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a b->mutex);. }..
193f1 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68 69 73 /* Detach this
193f2 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74 68 65 backup from the
193f3 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20 2a source pager. *
193f4 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 /. if( p->pDest
193f5 44 62 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 72 Db ){. p->pSr
193f6 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b 0a 20 20 c->nBackup--;.
193f7 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 41 74 74 }. if( p->isAtt
193f8 61 63 68 65 64 20 29 7b 0a 20 20 20 20 70 70 20 ached ){. pp
193f9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 = sqlite3PagerBa
193fa 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 42 ckupPtr(sqlite3B
193fb 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 treePager(p->pSr
193fc 63 29 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 c));. while(
193fd 2a 70 70 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 *pp!=p ){.
193fe 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 pp = &(*pp)->pNe
193ff 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 xt;. }. *p
19400 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 p = p->pNext;.
19401 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61 }.. /* If a tra
19402 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 69 6c nsaction is stil
19403 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 74 l open on the Bt
19404 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 62 61 63 ree, roll it bac
19405 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 k. */. sqlite3B
19406 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e treeRollback(p->
19407 70 44 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 53 65 pDest);.. /* Se
19408 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 t the error code
19409 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 of the destinat
1940a 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e ion database han
1940b 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 28 dle. */. rc = (
1940c 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f p->rc==SQLITE_DO
1940d 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 NE) ? SQLITE_OK
1940e 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 74 : p->rc;. sqlit
1940f 65 33 45 72 72 6f 72 28 70 2d 3e 70 44 65 73 74 e3Error(p->pDest
19410 44 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f Db, rc, 0);.. /
19411 2a 20 45 78 69 74 20 74 68 65 20 6d 75 74 65 78 * Exit the mutex
19412 65 73 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 es and free the
19413 62 61 63 6b 75 70 20 63 6f 6e 74 65 78 74 20 73 backup context s
19414 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 69 tructure. */. i
19415 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b f( p->pDestDb ){
19416 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
19417 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73 ex_leave(p->pDes
19418 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d tDb->mutex);. }
19419 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
1941a 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 eave(p->pSrc);.
1941b 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 if( p->pDestDb
1941c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
1941d 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 ree(p);. }. sq
1941e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1941f 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 e(mutex);. retu
19420 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
19421 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
19422 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c r of pages still
19423 20 74 6f 20 62 65 20 62 61 63 6b 65 64 20 75 70 to be backed up
19424 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 as of the most
19425 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74 recent.** call t
19426 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 o sqlite3_backup
19427 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 _step()..*/.SQLI
19428 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
19429 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e e3_backup_remain
1942a 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ing(sqlite3_back
1942b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e up *p){. return
1942c 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a p->nRemaining;.
1942d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1942e 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
1942f 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
19430 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
19431 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 as of the most
19432 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 .** recent call
19433 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
19434 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c p_step()..*/.SQL
19435 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
19436 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 te3_backup_pagec
19437 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63 ount(sqlite3_bac
19438 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 kup *p){. retur
19439 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 3b n p->nPagecount;
1943a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
1943b 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
1943c 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e 74 d after the cont
1943d 65 6e 74 73 20 6f 66 20 70 61 67 65 20 69 50 61 ents of page iPa
1943e 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 6f 75 ge of the.** sou
1943f 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 76 rce database hav
19440 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e e been modified.
19441 20 49 66 20 70 61 67 65 20 69 50 61 67 65 20 68 If page iPage h
19442 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
19443 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 .** copied into
19444 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
19445 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 database, then t
19446 68 65 20 64 61 74 61 20 77 72 69 74 74 65 6e 20 he data written
19447 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e to the.** destin
19448 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 69 6e 76 ation is now inv
19449 61 6c 69 64 61 74 65 64 2e 20 54 68 65 20 64 65 alidated. The de
1944a 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 79 20 6f stination copy o
1944b 66 20 69 50 61 67 65 20 6e 65 65 64 73 0a 2a 2a f iPage needs.**
1944c 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 77 to be updated w
1944d 69 74 68 20 74 68 65 20 6e 65 77 20 64 61 74 61 ith the new data
1944e 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 63 6b before the back
1944f 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a up operation is.
19450 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a ** complete..**.
19451 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 ** It is assumed
19452 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 that the mutex
19453 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
19454 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
19455 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e ect.** correspon
19456 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 ding to the sour
19457 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68 ce database is h
19458 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 eld when this fu
19459 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c nction is.** cal
1945a 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 led..*/.SQLITE_P
1945b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1945c 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 te3BackupUpdate(
1945d 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
1945e 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69 50 pBackup, Pgno iP
1945f 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 age, const u8 *a
19460 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65 33 Data){. sqlite3
19461 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 _backup *p;
19462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19463 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
19464 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42 le */. for(p=pB
19465 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 ackup; p; p=p->p
19466 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 Next){. asser
19467 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
19468 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70 _held(p->pSrc->p
19469 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1946a 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 72 if( !isFatalEr
1946b 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 20 69 50 ror(p->rc) && iP
1946c 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 29 7b 0a age<p->iNext ){.
1946d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 63 /* The bac
1946e 6b 75 70 20 70 72 6f 63 65 73 73 20 70 20 68 61 kup process p ha
1946f 73 20 61 6c 72 65 61 64 79 20 63 6f 70 69 65 64 s already copied
19470 20 70 61 67 65 20 69 50 61 67 65 2e 20 42 75 74 page iPage. But
19471 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 20 2a 2a now it. **
19472 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 has been modifi
19473 65 64 20 62 79 20 61 20 74 72 61 6e 73 61 63 74 ed by a transact
19474 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 ion on the sourc
19475 65 20 70 61 67 65 72 2e 20 43 6f 70 79 0a 20 20 e pager. Copy.
19476 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 64 ** the new d
19477 61 74 61 20 69 6e 74 6f 20 74 68 65 20 62 61 63 ata into the bac
19478 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 kup.. */.
19479 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 61 63 int rc = bac
1947a 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 50 kupOnePage(p, iP
1947b 61 67 65 2c 20 61 44 61 74 61 29 3b 0a 20 20 20 age, aData);.
1947c 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
1947d 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72 63 QLITE_BUSY && rc
1947e 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 !=SQLITE_LOCKED
1947f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
19480 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
19481 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 p->rc = rc
19482 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
19483 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 }.}../*.** Res
19484 74 61 72 74 20 74 68 65 20 62 61 63 6b 75 70 20 tart the backup
19485 70 72 6f 63 65 73 73 2e 20 54 68 69 73 20 69 73 process. This is
19486 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 called when the
19487 20 70 61 67 65 72 20 6c 61 79 65 72 0a 2a 2a 20 pager layer.**
19488 64 65 74 65 63 74 73 20 74 68 61 74 20 74 68 65 detects that the
19489 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 database has be
1948a 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 en modified by a
1948b 6e 20 65 78 74 65 72 6e 61 6c 20 64 61 74 61 62 n external datab
1948c 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f ase.** connectio
1948d 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 n. In this case
1948e 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 there is no way
1948f 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68 69 63 68 of knowing which
19490 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 73 of the.** pages
19491 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 20 that have been
19492 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 copied into the
19493 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
19494 62 61 73 65 20 61 72 65 20 73 74 69 6c 6c 20 0a base are still .
19495 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20 77 68 69 ** valid and whi
19496 63 68 20 61 72 65 20 6e 6f 74 2c 20 73 6f 20 74 ch are not, so t
19497 68 65 20 65 6e 74 69 72 65 20 70 72 6f 63 65 73 he entire proces
19498 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 s needs to be re
19499 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 started..**.** I
1949a 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
1949b 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f t the mutex asso
1949c 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
1949d 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a BtShared object.
1949e 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
1949f 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 to the source d
194a0 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 20 atabase is held
194a1 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
194a2 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e on is.** called.
194a3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
194a4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
194a5 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c ackupRestart(sql
194a6 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 ite3_backup *pBa
194a7 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 ckup){. sqlite3
194a8 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 _backup *p;
194a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
194aa 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
194ab 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42 le */. for(p=pB
194ac 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 ackup; p; p=p->p
194ad 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 Next){. asser
194ae 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
194af 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70 _held(p->pSrc->p
194b0 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
194b1 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a p->iNext = 1;.
194b2 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 }.}..#ifndef S
194b3 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
194b4 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65 M./*.** Copy the
194b5 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e complete conten
194b6 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e 74 t of pBtFrom int
194b7 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 72 61 6e o pBtTo. A tran
194b8 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 saction.** must
194b9 62 65 20 61 63 74 69 76 65 20 66 6f 72 20 62 6f be active for bo
194ba 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 th files..**.**
194bb 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c 65 The size of file
194bc 20 70 54 6f 20 6d 61 79 20 62 65 20 72 65 64 75 pTo may be redu
194bd 63 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 ced by this oper
194be 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 74 68 69 ation. If anythi
194bf 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77 72 6f 6e ng .** goes wron
194c0 67 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 g, the transacti
194c1 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 72 6f 6c on on pTo is rol
194c2 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 73 75 63 led back. If suc
194c3 63 65 73 73 66 75 6c 2c 20 74 68 65 20 0a 2a 2a cessful, the .**
194c4 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
194c5 63 6f 6d 6d 69 74 74 65 64 20 62 65 66 6f 72 65 committed before
194c6 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 returning..*/.S
194c7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
194c8 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f t sqlite3BtreeCo
194c9 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a 70 54 pyFile(Btree *pT
194ca 6f 2c 20 42 74 72 65 65 20 2a 70 46 72 6f 6d 29 o, Btree *pFrom)
194cb 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 {. int rc;. sq
194cc 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 62 3b 0a lite3_backup b;.
194cd 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
194ce 74 65 72 28 70 54 6f 29 3b 0a 20 20 73 71 6c 69 ter(pTo);. sqli
194cf 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 46 te3BtreeEnter(pF
194d0 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 rom);.. /* Set
194d1 75 70 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 up an sqlite3_ba
194d2 63 6b 75 70 20 6f 62 6a 65 63 74 2e 20 73 71 6c ckup object. sql
194d3 69 74 65 33 5f 62 61 63 6b 75 70 2e 70 44 65 73 ite3_backup.pDes
194d4 74 44 62 20 6d 75 73 74 20 62 65 20 73 65 74 0a tDb must be set.
194d5 20 20 2a 2a 20 74 6f 20 30 2e 20 54 68 69 73 20 ** to 0. This
194d6 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 69 is used by the i
194d7 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
194d8 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 f sqlite3_backup
194d9 5f 73 74 65 70 28 29 0a 20 20 2a 2a 20 61 6e 64 _step(). ** and
194da 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
194db 66 69 6e 69 73 68 28 29 20 74 6f 20 64 65 74 65 finish() to dete
194dc 63 74 20 74 68 61 74 20 74 68 65 79 20 61 72 65 ct that they are
194dd 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 0a 20 20 being called.
194de 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e ** from this fun
194df 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 69 72 65 63 ction, not direc
194e0 74 6c 79 20 62 79 20 74 68 65 20 75 73 65 72 2e tly by the user.
194e1 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 . */. memset(&
194e2 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 62 29 29 b, 0, sizeof(b))
194e3 3b 0a 20 20 62 2e 70 53 72 63 44 62 20 3d 20 70 ;. b.pSrcDb = p
194e4 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 62 2e 70 53 From->db;. b.pS
194e5 72 63 20 3d 20 70 46 72 6f 6d 3b 0a 20 20 62 2e rc = pFrom;. b.
194e6 70 44 65 73 74 20 3d 20 70 54 6f 3b 0a 20 20 62 pDest = pTo;. b
194e7 2e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a 20 20 2f .iNext = 1;.. /
194e8 2a 20 30 78 37 46 46 46 46 46 46 46 20 69 73 20 * 0x7FFFFFFF is
194e9 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20 66 the hard limit f
194ea 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 or the number of
194eb 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61 pages in a data
194ec 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 base. ** file.
194ed 42 79 20 70 61 73 73 69 6e 67 20 74 68 69 73 20 By passing this
194ee 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 as the number of
194ef 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 74 pages to copy t
194f0 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 o. ** sqlite3_b
194f1 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 77 65 ackup_step(), we
194f2 20 63 61 6e 20 67 75 61 72 61 6e 74 65 65 20 74 can guarantee t
194f3 68 61 74 20 74 68 65 20 63 6f 70 79 20 66 69 6e hat the copy fin
194f4 69 73 68 65 73 20 0a 20 20 2a 2a 20 77 69 74 68 ishes . ** with
194f5 69 6e 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c in a single call
194f6 20 28 75 6e 6c 65 73 73 20 61 6e 20 65 72 72 6f (unless an erro
194f7 72 20 6f 63 63 75 72 73 29 2e 20 54 68 65 20 61 r occurs). The a
194f8 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
194f9 74 0a 20 20 2a 2a 20 63 68 65 63 6b 73 20 74 68 t. ** checks th
194fa 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 20 2d 20 is assumption -
194fb 28 70 2d 3e 72 63 29 20 73 68 6f 75 6c 64 20 62 (p->rc) should b
194fc 65 20 73 65 74 20 74 6f 20 65 69 74 68 65 72 20 e set to either
194fd 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 2a SQLITE_DONE . *
194fe 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f * or an error co
194ff 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 de.. */. sqlit
19500 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 26 e3_backup_step(&
19501 62 2c 20 30 78 37 46 46 46 46 46 46 46 29 3b 0a b, 0x7FFFFFFF);.
19502 20 20 61 73 73 65 72 74 28 20 62 2e 72 63 21 3d assert( b.rc!=
19503 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 SQLITE_OK );. r
19504 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b c = sqlite3_back
19505 75 70 5f 66 69 6e 69 73 68 28 26 62 29 3b 0a 20 up_finish(&b);.
19506 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
19507 4f 4b 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 70 OK ){. pTo->p
19508 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 Bt->pageSizeFixe
19509 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 d = 0;. }.. sq
1950a 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
1950b 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 65 pFrom);. sqlite
1950c 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f 29 3BtreeLeave(pTo)
1950d 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
1950e 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1950f 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f E_OMIT_VACUUM */
19510 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
19511 2a 20 45 6e 64 20 6f 66 20 62 61 63 6b 75 70 2e * End of backup.
19512 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
19513 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19514 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19515 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
19516 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
19517 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a emem.c *********
19518 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19519 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1951a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
1951b 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 26.**.** The au
1951c 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
1951d 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
1951e 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
1951f 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
19520 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
19521 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
19522 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
19523 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
19524 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
19525 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
19526 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
19527 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
19528 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
19529 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
1952a 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
1952b 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
1952c 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
1952d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1952e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1952f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19531 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
19532 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 le contains code
19533 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 use to manipula
19534 74 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75 te "Mem" structu
19535 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 re. A "Mem".**
19536 73 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20 stores a single
19537 76 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42 value in the VDB
19538 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 E. Mem is an op
19539 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76 aque structure v
1953a 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 isible.** only w
1953b 69 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20 ithin the VDBE.
1953c 20 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 Interface routi
1953d 6e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d nes refer to a M
1953e 65 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 em using the.**
1953f 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 name sqlite_valu
19540 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 e.**.** $Id: vdb
19541 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 35 32 20 32 emem.c,v 1.152 2
19542 30 30 39 2f 30 37 2f 32 32 20 31 38 3a 30 37 3a 009/07/22 18:07:
19543 34 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 41 drh Exp $.*/.
19544 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 ./*.** Call sqli
19545 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
19546 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 Blob() on the su
19547 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 pplied value (ty
19548 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 pe Mem*).** P if
19549 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 required..*/.#d
1954a 65 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 efine expandBlob
1954b 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 (P) (((P)->flags
1954c 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 &MEM_Zero)?sqlit
1954d 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 e3VdbeMemExpandB
1954e 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a lob(P):0)../*.**
1954f 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f If pMem is an o
19550 62 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 6c bject with a val
19551 69 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 id string repres
19552 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 entation, this r
19553 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 outine.** ensure
19554 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 s the internal e
19555 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 ncoding for the
19556 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 string represent
19557 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 ation is.** 'des
19558 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 iredEnc', one of
19559 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
1955a 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 LITE_UTF16LE or
1955b 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a SQLITE_UTF16BE..
1955c 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 **.** If pMem is
1955d 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62 not a string ob
1955e 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 ject, or the enc
1955f 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72 oding of the str
19560 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 ing.** represent
19561 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 ation is already
19562 20 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68 stored using th
19563 65 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f e requested enco
19564 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a ding, then this.
19565 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 ** routine is a
19566 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c no-op..**.** SQL
19567 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
19568 65 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72 ed if the conver
19569 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 sion is successf
1956a 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 ul (or not requi
1956b 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f red)..** SQLITE_
1956c 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74 NOMEM may be ret
1956d 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f urned if a mallo
1956e 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 c() fails during
1956f 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 conversion.** b
19570 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a etween formats..
19571 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19572 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19573 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
19574 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 Mem *pMem, int d
19575 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e esiredEnc){. in
19576 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 t rc;. assert(
19577 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
19578 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
19579 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64 assert( desired
1957a 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 Enc==SQLITE_UTF8
1957b 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d 3d || desiredEnc==
1957c 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a 20 SQLITE_UTF16LE.
1957d 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 73 || des
1957e 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
1957f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28 UTF16BE );. if(
19580 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d !(pMem->flags&M
19581 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d EM_Str) || pMem-
19582 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 >enc==desiredEnc
19583 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
19584 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
19585 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
19586 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
19587 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
19588 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 db->mutex) );.#i
19589 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
1958a 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e 20 _UTF16. return
1958b 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65 SQLITE_ERROR;.#e
1958c 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 lse.. /* MemTra
1958d 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 74 nslate() may ret
1958e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 urn SQLITE_OK or
1958f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 SQLITE_NOMEM. I
19590 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 f NOMEM is retur
19591 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 ned,. ** then t
19592 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 he encoding of t
19593 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74 he value may not
19594 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 20 have changed..
19595 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 */. rc = sqlit
19596 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 e3VdbeMemTransla
19597 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 73 te(pMem, (u8)des
19598 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 65 iredEnc);. asse
19599 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b rt(rc==SQLITE_OK
1959a 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 || rc==SQLIT
1959b 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 65 E_NOMEM);. asse
1959c 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b rt(rc==SQLITE_OK
1959d 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 || pMem->enc
1959e 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 !=desiredEnc);.
1959f 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 assert(rc==SQLI
195a0 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d TE_NOMEM || pMem
195a1 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e ->enc==desiredEn
195a2 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
195a3 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
195a4 20 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d 2d Make sure pMem-
195a5 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 77 >z points to a w
195a6 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 69 ritable allocati
195a7 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 0a on of at least .
195a8 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a ** n bytes..**.*
195a9 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 * If the memory
195aa 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 63 cell currently c
195ab 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20 6f ontains string o
195ac 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 61 r blob data.** a
195ad 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 67 nd the third arg
195ae 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 ument passed to
195af 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
195b0 20 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20 63 true, the .** c
195b1 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f urrent content o
195b2 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70 72 f the cell is pr
195b3 65 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77 69 eserved. Otherwi
195b4 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62 65 se, it may.** be
195b5 20 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a 2a discarded. .**
195b6 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
195b7 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f 44 n sets the MEM_D
195b8 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65 61 yn flag and clea
195b9 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c 6c rs any xDel call
195ba 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f back..** It also
195bb 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68 65 clears MEM_Ephe
195bc 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69 63 m and MEM_Static
195bd 2e 20 49 66 20 74 68 65 20 70 72 65 73 65 72 76 . If the preserv
195be 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e 6f e flag is .** no
195bf 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 t set, Mem.n is
195c0 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 zeroed..*/.SQLIT
195c1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
195c2 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
195c3 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 (Mem *pMem, int
195c4 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 n, int preserve)
195c5 7b 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e 3d {. assert( 1 >=
195c6 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 . ((pMem->zMa
195c7 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d lloc && pMem->zM
195c8 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 alloc==pMem->z)
195c9 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20 28 ? 1 : 0) +. (
195ca 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ((pMem->flags&ME
195cb 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 M_Dyn)&&pMem->xD
195cc 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a el) ? 1 : 0) + .
195cd 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 ((pMem->flag
195ce 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20 31 s&MEM_Ephem) ? 1
195cf 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 70 : 0) + . ((p
195d0 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 Mem->flags&MEM_S
195d1 74 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29 0a tatic) ? 1 : 0).
195d2 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
195d3 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
195d4 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 RowSet)==0 );..
195d5 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20 if( n<32 ) n =
195d6 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 32;. if( sqlite
195d7 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 3DbMallocSize(pM
195d8 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d em->db, pMem->zM
195d9 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20 alloc)<n ){.
195da 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 20 if( preserve &&
195db 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a pMem->z==pMem->z
195dc 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 Malloc ){.
195dd 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e pMem->z = pMem->
195de 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 zMalloc = sqlite
195df 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
195e0 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d (pMem->db, pMem-
195e1 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 72 >z, n);. pr
195e2 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 eserve = 0;.
195e3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
195e4 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d 2d ite3DbFree(pMem-
195e5 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c >db, pMem->zMall
195e6 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d oc);. pMem-
195e7 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 >zMalloc = sqlit
195e8 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d e3DbMallocRaw(pM
195e9 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 20 em->db, n);.
195ea 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d 65 }. }.. if( pMe
195eb 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 65 m->z && preserve
195ec 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f && pMem->zMallo
195ed 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d c && pMem->z!=pM
195ee 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 em->zMalloc ){.
195ef 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e memcpy(pMem->
195f0 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a zMalloc, pMem->z
195f1 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a , pMem->n);. }.
195f2 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 if( pMem->flag
195f3 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d 65 s&MEM_Dyn && pMe
195f4 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 70 m->xDel ){. p
195f5 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 Mem->xDel((void
195f6 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 *)(pMem->z));.
195f7 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 }.. pMem->z = p
195f8 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 Mem->zMalloc;.
195f9 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 if( pMem->z==0 )
195fa 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 {. pMem->flag
195fb 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
195fc 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d }else{. pMem-
195fd 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
195fe 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 Ephem|MEM_Static
195ff 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 );. }. pMem->x
19600 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 Del = 0;. retur
19601 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c n (pMem->z ? SQL
19602 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f ITE_OK : SQLITE_
19603 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a NOMEM);.}../*.**
19604 20 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e 20 Make the given
19605 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 Mem object MEM_D
19606 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f yn. In other wo
19607 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f 0a rds, make it so.
19608 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58 54 ** that any TEXT
19609 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 or BLOB content
1960a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 is stored in me
1960b 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
1960c 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 om.** malloc().
1960d 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65 In this way, we
1960e 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6d know that the m
1960f 65 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74 6f emory is safe to
19610 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 be.** overwritt
19611 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a 2a en or altered..*
19612 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
19613 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
19614 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d or SQLITE_NOMEM
19615 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 if malloc fails
19616 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19617 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
19618 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 dbeMemMakeWritea
19619 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ble(Mem *pMem){.
1961a 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 72 int f;. asser
1961b 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
1961c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
1961d 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
1961e 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
1961f 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
19620 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
19621 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 ;. expandBlob(p
19622 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65 6d Mem);. f = pMem
19623 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 ->flags;. if( (
19624 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 f&(MEM_Str|MEM_B
19625 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e 7a lob)) && pMem->z
19626 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 !=pMem->zMalloc
19627 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
19628 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
19629 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c em, pMem->n + 2,
1962a 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 1) ){. ret
1962b 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1962c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d ;. }. pMem
1962d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 ->z[pMem->n] = 0
1962e 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d ;. pMem->z[pM
1962f 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 em->n+1] = 0;.
19630 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d pMem->flags |=
19631 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a MEM_Term;. }..
19632 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19633 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 OK;.}../*.** If
19634 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68 the given Mem* h
19635 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 as a zero-filled
19636 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 69 tail, turn it i
19637 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a nto an ordinary.
19638 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69 ** blob stored i
19639 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c n dynamically al
1963a 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a 2a located space..*
1963b 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1963c 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 _OMIT_INCRBLOB.S
1963d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1963e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
1963f 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a ExpandBlob(Mem *
19640 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 pMem){. if( pMe
19641 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a m->flags & MEM_Z
19642 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ero ){. int n
19643 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 74 Byte;. assert
19644 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ( pMem->flags&ME
19645 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73 M_Blob );. as
19646 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
19647 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d gs&MEM_RowSet)==
19648 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
19649 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
1964a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1964b 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
1964c 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 ex) );.. /* S
1964d 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 20 et nByte to the
1964e 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1964f 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 required to stor
19650 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 62 e the expanded b
19651 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74 lob. */. nByt
19652 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d e = pMem->n + pM
19653 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 em->u.nZero;.
19654 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b if( nByte<=0 ){
19655 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 31 . nByte = 1
19656 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
19657 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
19658 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 ow(pMem, nByte,
19659 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 1) ){. retu
1965a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1965b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 . }.. mems
1965c 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d et(&pMem->z[pMem
1965d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 ->n], 0, pMem->u
1965e 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65 .nZero);. pMe
1965f 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e m->n += pMem->u.
19660 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d nZero;. pMem-
19661 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f >flags &= ~(MEM_
19662 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a Zero|MEM_Term);.
19663 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
19664 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 ITE_OK;.}.#endif
19665 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 .../*.** Make su
19666 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d re the given Mem
19667 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69 is \u0000 termi
19668 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nated..*/.SQLITE
19669 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1966a 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
1966b 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65 rminate(Mem *pMe
1966c 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
1966d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
1966e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1966f 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
19670 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d );. if( (pMem-
19671 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 >flags & MEM_Ter
19672 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e m)!=0 || (pMem->
19673 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 flags & MEM_Str)
19674 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
19675 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f n SQLITE_OK; /
19676 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 * Nothing to do
19677 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c */. }. if( sql
19678 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
19679 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c pMem, pMem->n+2,
1967a 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 1) ){. retur
1967b 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
1967c 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d }. pMem->z[pM
1967d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d em->n] = 0;. pM
1967e 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d em->z[pMem->n+1]
1967f 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c = 0;. pMem->fl
19680 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
19681 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
19682 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 _OK;.}../*.** Ad
19683 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65 d MEM_Str to the
19684 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e set of represen
19685 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 tations for the
19686 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 given Mem. Numb
19687 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 ers.** are conve
19688 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 rted using sqlit
19689 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 e3_snprintf().
1968a 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f Converting a BLO
1968b 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a B to a string.**
1968c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
1968d 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72 ** Existing repr
1968e 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f esentations MEM_
1968f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c Int and MEM_Real
19690 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c are *not* inval
19691 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 idated..**.** A
19692 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77 MEM_Null value w
19693 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73 ill never be pas
19694 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 sed to this func
19695 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74 tion. This funct
19696 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66 ion is.** used f
19697 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61 or converting va
19698 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72 lues to text for
19699 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68 returning to th
1969a 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61 e user (i.e. via
1969b 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 .** sqlite3_valu
1969c 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f e_text()), or fo
1969d 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 r ensuring that
1969e 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65 values to be use
1969f 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65 d as btree.** ke
196a0 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20 ys are strings.
196a1 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61 In the former ca
196a2 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 se a NULL pointe
196a3 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68 r is returned th
196a4 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68 e.** user and th
196a5 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e e later is an in
196a6 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69 ternal programmi
196a7 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c ng error..*/.SQL
196a8 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
196a9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
196aa 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65 ringify(Mem *pMe
196ab 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69 m, int enc){. i
196ac 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
196ad 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d K;. int fg = pM
196ae 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e em->flags;. con
196af 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33 st int nByte = 3
196b0 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 2;.. assert( pM
196b1 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
196b2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
196b3 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
196b4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 );. assert( !(
196b5 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a fg&MEM_Zero) );.
196b6 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28 assert( !(fg&(
196b7 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
196b8 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 )) );. assert(
196b9 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f fg&(MEM_Int|MEM_
196ba 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 Real) );. asser
196bb 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
196bc 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
196bd 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48 ;. assert( EIGH
196be 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 T_BYTE_ALIGNMENT
196bf 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 (pMem) );... if
196c0 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
196c1 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 Grow(pMem, nByte
196c2 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , 0) ){. retu
196c3 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
196c4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 . }.. /* For a
196c5 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72 Real or Integer
196c6 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70 , use sqlite3_mp
196c7 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75 rintf() to produ
196c8 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a ce the UTF-8. *
196c9 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 * string represe
196ca 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 ntation of the v
196cb 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74 alue. Then, if t
196cc 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f he required enco
196cd 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46 ding. ** is UTF
196ce 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62 -16le or UTF-16b
196cf 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69 e do a translati
196d0 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 on.. ** . ** F
196d1 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20 IX ME: It would
196d2 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c be better if sql
196d3 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 ite3_snprintf()
196d4 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e could do UTF-16.
196d5 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26 . */. if( fg &
196d6 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
196d7 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
196d8 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c (nByte, pMem->z,
196d9 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 "%lld", pMem->u
196da 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 .i);. }else{.
196db 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d assert( fg & M
196dc 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73 EM_Real );. s
196dd 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
196de 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 nByte, pMem->z,
196df 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e "%!.15g", pMem->
196e0 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e r);. }. pMem->
196e1 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
196e2 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 n30(pMem->z);.
196e3 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
196e4 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d TE_UTF8;. pMem-
196e5 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 >flags |= MEM_St
196e6 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 r|MEM_Term;. sq
196e7 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
196e8 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e ncoding(pMem, en
196e9 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
196ea 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 .}../*.** Memory
196eb 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 cell pMem conta
196ec 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20 ins the context
196ed 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 of an aggregate
196ee 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 function..** Thi
196ef 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 s routine calls
196f0 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74 the finalize met
196f1 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e hod for that fun
196f2 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 ction. The.** r
196f3 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67 esult of the agg
196f4 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64 regate is stored
196f5 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e back into pMem.
196f6 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
196f7 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68 LITE_ERROR if th
196f8 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f e finalizer repo
196f9 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53 rts an error. S
196fa 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 QLITE_OK.** othe
196fb 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 rwise..*/.SQLITE
196fc 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
196fd 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c ite3VdbeMemFinal
196fe 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 ize(Mem *pMem, F
196ff 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a uncDef *pFunc){.
19700 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
19701 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41 E_OK;. if( ALWA
19702 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 6e YS(pFunc && pFun
19703 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 7b c->xFinalize) ){
19704 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e . sqlite3_con
19705 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 73 text ctx;. as
19706 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 sert( (pMem->fla
19707 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d gs & MEM_Null)!=
19708 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 0 || pFunc==pMem
19709 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 20 ->u.pDef );.
1970a 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
1970b 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
1970c 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
1970d 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1970e 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 30 memset(&ctx, 0
1970f 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a , sizeof(ctx));.
19710 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20 ctx.s.flags
19711 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
19712 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d ctx.s.db = pMem-
19713 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65 >db;. ctx.pMe
19714 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74 m = pMem;. ct
19715 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b x.pFunc = pFunc;
19716 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e . pFunc->xFin
19717 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 20 alize(&ctx);.
19718 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d 65 assert( 0==(pMe
19719 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e m->flags&MEM_Dyn
1971a 29 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65 6c ) && !pMem->xDel
1971b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 );. sqlite3D
1971c 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 bFree(pMem->db,
1971d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a pMem->zMalloc);.
1971e 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2c memcpy(pMem,
1971f 20 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66 28 &ctx.s, sizeof(
19720 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 ctx.s));. rc
19721 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 = ctx.isError;.
19722 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
19723 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 }../*.** If the
19724 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e 74 memory cell cont
19725 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 61 ains a string va
19726 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 lue that must be
19727 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e 76 freed by.** inv
19728 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e 61 oking an externa
19729 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 65 l callback, free
1972a 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 it now. Calling
1972b 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a this function.*
1972c 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 20 * does not free
1972d 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 any Mem.zMalloc
1972e 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 buffer..*/.SQLIT
1972f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
19730 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
19731 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d easeExternal(Mem
19732 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
19733 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 p->db==0 || sqli
19734 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
19735 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
19736 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 testcase( p->f
19737 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20 29 lags & MEM_Agg )
19738 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d ;. testcase( p-
19739 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e >flags & MEM_Dyn
1973a 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
1973b 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 p->flags & MEM_R
1973c 6f 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74 63 owSet );. testc
1973d 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 ase( p->flags &
1973e 4d 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20 69 MEM_Frame );. i
1973f 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d f( p->flags&(MEM
19740 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d _Agg|MEM_Dyn|MEM
19741 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61 6d _RowSet|MEM_Fram
19742 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d e) ){. if( p-
19743 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29 >flags&MEM_Agg )
19744 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
19745 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 dbeMemFinalize(p
19746 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20 , p->u.pDef);.
19747 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e assert( (p->
19748 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 flags & MEM_Agg)
19749 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==0 );. sql
1974a 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1974b 73 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 se(p);. }else
1974c 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 if( p->flags&ME
1974d 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c M_Dyn && p->xDel
1974e 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
1974f 28 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f ( (p->flags&MEM_
19750 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
19751 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 p->xDel((voi
19752 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 d *)p->z);.
19753 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 p->xDel = 0;.
19754 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 }else if( p->f
19755 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20 lags&MEM_RowSet
19756 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
19757 52 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 75 RowSetClear(p->u
19758 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d .pRowSet);. }
19759 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 else if( p->flag
1975a 73 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 s&MEM_Frame ){.
1975b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1975c 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a 20 MemSetNull(p);.
1975d 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1975e 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d 65 * Release any me
1975f 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 mory held by the
19760 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c Mem. This may l
19761 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e 20 eave the Mem in
19762 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 an.** inconsiste
19763 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 78 nt state, for ex
19764 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e ample with (Mem.
19765 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 z==0) and.** (Me
19766 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 m.type==SQLITE_T
19767 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f EXT)..*/.SQLITE_
19768 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19769 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1976a 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 71 se(Mem *p){. sq
1976b 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1976c 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b 0a aseExternal(p);.
1976d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1976e 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f p->db, p->zMallo
1976f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a c);. p->z = 0;.
19770 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 p->zMalloc = 0
19771 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b ;. p->xDel = 0;
19772 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
19773 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45 20 t a 64-bit IEEE
19774 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36 34 double into a 64
19775 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
19776 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 ger..** If the d
19777 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72 ouble is too lar
19778 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30 30 ge, return 0x800
19779 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a 2a 0000000000000..*
1977a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d *.** Most system
1977b 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20 74 s appear to do t
1977c 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61 73 his simply by as
1977d 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61 signing.** varia
1977e 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75 74 bles and without
1977f 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67 65 the extra range
19780 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20 tests. But.**
19781 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72 74 there are report
19782 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 74 s that windows t
19783 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74 69 hrows an expecti
19784 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f on.** if the flo
19785 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 ating point valu
19786 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 e is out of rang
19787 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20 23 e. (See ticket #
19788 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73 2880.).** Becaus
19789 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 e we do not comp
1978a 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61 6e letely understan
1978b 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77 d the problem, w
1978c 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 e will.** take t
1978d 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20 he conservative
1978e 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c 77 approach and alw
1978f 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65 73 ays do range tes
19790 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74 74 ts.** before att
19791 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e 76 empting the conv
19792 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 ersion..*/.stati
19793 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e c i64 doubleToIn
19794 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a 20 t64(double r){.
19795 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f /*. ** Many co
19796 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f 75 mpilers we encou
19797 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66 69 nter do not defi
19798 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 ne constants for
19799 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 the. ** minimu
1979a 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34 m and maximum 64
1979b 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20 6f -bit integers, o
1979c 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74 68 r they define th
1979d 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 em. ** inconsis
1979e 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e tently. And man
1979f 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74 y do not underst
197a0 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74 and the "LL" not
197a1 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 ation.. ** So w
197a2 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e e define our own
197a3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e 74 static constant
197a4 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f 74 s here using not
197a5 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72 hing. ** larger
197a6 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20 69 than a 32-bit i
197a7 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e nteger constant.
197a8 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 . */. static c
197a9 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74 20 onst i64 maxInt
197aa 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b = LARGEST_INT64;
197ab 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
197ac 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 i64 minInt = SMA
197ad 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 LLEST_INT64;..
197ae 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69 if( r<(double)mi
197af 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 nInt ){. retu
197b0 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c rn minInt;. }el
197b1 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c 65 se if( r>(double
197b2 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20 2f )maxInt ){. /
197b3 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 72 * minInt is corr
197b4 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d ect here - not m
197b5 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e 73 axInt. It turns
197b6 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 6e out that assign
197b7 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65 72 ing. ** a ver
197b8 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 65 y large positive
197b9 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e number to an in
197ba 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 6e teger results in
197bb 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20 20 a very large.
197bc 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69 6e ** negative in
197bd 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61 6b teger. This mak
197be 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75 74 es no sense, but
197bf 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36 20 it is what x86
197c0 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a 20 hardware. **
197c1 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d 70 does so for comp
197c2 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 69 6c atibility we wil
197c3 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69 6e l do the same in
197c4 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20 20 software. */.
197c5 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b return minInt;
197c6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
197c7 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d turn (i64)r;. }
197c8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
197c9 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 6e some kind of in
197ca 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 69 63 teger value whic
197cb 68 20 69 73 20 74 68 65 20 62 65 73 74 20 77 65 h is the best we
197cc 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 65 can do.** at re
197cd 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 76 presenting the v
197ce 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d 20 alue that *pMem
197cf 64 65 73 63 72 69 62 65 73 20 61 73 20 61 6e 20 describes as an
197d0 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 70 integer..** If p
197d1 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 Mem is an intege
197d2 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 r, then the valu
197d3 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 66 20 e is exact. If
197d4 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c 6f pMem is.** a flo
197d5 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 6e ating-point then
197d6 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
197d7 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 65 67 ned is the integ
197d8 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 70 er part..** If p
197d9 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67 20 Mem is a string
197da 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 or blob, then we
197db 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 74 make an attempt
197dc 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 to convert.** i
197dd 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 72 t into a integer
197de 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74 and return that
197df 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65 . If pMem repre
197e0 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20 53 sents an.** an S
197e1 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 72 QL-NULL value, r
197e2 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 eturn 0..**.** I
197e3 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74 f pMem represent
197e4 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 s a string value
197e5 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 6d , its encoding m
197e6 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 2e ight be changed.
197e7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
197e8 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 TE i64 sqlite3Vd
197e9 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 2a beIntValue(Mem *
197ea 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 pMem){. int fla
197eb 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d gs;. assert( pM
197ec 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
197ed 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
197ee 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
197ef 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 );. assert( EI
197f0 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
197f1 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66 6c NT(pMem) );. fl
197f2 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 ags = pMem->flag
197f3 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 s;. if( flags &
197f4 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
197f5 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 return pMem->u.i
197f6 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c ;. }else if( fl
197f7 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 ags & MEM_Real )
197f8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f 75 {. return dou
197f9 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d bleToInt64(pMem-
197fa 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 >r);. }else if(
197fb 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 flags & (MEM_St
197fc 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 r|MEM_Blob) ){.
197fd 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20 i64 value;.
197fe 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d pMem->flags |=
197ff 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 MEM_Str;. if
19800 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 ( sqlite3VdbeCha
19801 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d ngeEncoding(pMem
19802 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 , SQLITE_UTF8).
19803 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
19804 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
19805 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 ate(pMem) ){.
19806 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
19807 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
19808 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71 Mem->z );. sq
19809 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 6d lite3Atoi64(pMem
1980a 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 ->z, &value);.
1980b 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a return value;.
1980c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 }else{. ret
1980d 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a urn 0;. }.}../*
1980e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 .** Return the b
1980f 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 est representati
19810 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20 on of pMem that
19811 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20 we can get into
19812 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66 a.** double. If
19813 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79 pMem is already
19814 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20 a double or an
19815 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 integer, return
19816 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49 its.** value. I
19817 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e 67 f it is a string
19818 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f or blob, try to
19819 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
1981a 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 double..** If i
1981b 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74 t is a NULL, ret
1981c 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 urn 0.0..*/.SQLI
1981d 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c TE_PRIVATE doubl
1981e 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 e sqlite3VdbeRea
1981f 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d lValue(Mem *pMem
19820 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 ){. assert( pMe
19821 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
19822 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
19823 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
19824 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 );. assert( EIG
19825 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
19826 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 28 T(pMem) );. if(
19827 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
19828 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 EM_Real ){. r
19829 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20 eturn pMem->r;.
1982a 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d }else if( pMem-
1982b 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1982c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 ){. return (
1982d 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 double)pMem->u.i
1982e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d ;. }else if( pM
1982f 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d em->flags & (MEM
19830 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 _Str|MEM_Blob) )
19831 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 {. /* (double
19832 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 )0 In case of SQ
19833 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
19834 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 NG_POINT... */.
19835 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 double val =
19836 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 70 (double)0;. p
19837 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
19838 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 M_Str;. if( s
19839 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1983a 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 Encoding(pMem, S
1983b 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 QLITE_UTF8).
1983c 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 || sqlite3Vdb
1983d 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 eMemNulTerminate
1983e 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 (pMem) ){.
1983f 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 /* (double)0 In
19840 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f case of SQLITE_O
19841 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
19842 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 72 NT... */. r
19843 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b eturn (double)0;
19844 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
19845 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 t( pMem->z );.
19846 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d sqlite3AtoF(pM
19847 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 em->z, &val);.
19848 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 return val;.
19849 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 64 }else{. /* (d
1984a 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 ouble)0 In case
1984b 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 of SQLITE_OMIT_F
1984c 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e LOATING_POINT...
1984d 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 28 */. return (
1984e 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a double)0;. }.}.
1984f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73 ./*.** The MEM s
19850 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65 tructure is alre
19851 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 ady a MEM_Real.
19852 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b Try to also mak
19853 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e e it a.** MEM_In
19854 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a t if we can..*/.
19855 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19856 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 oid sqlite3VdbeI
19857 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d ntegerAffinity(M
19858 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 em *pMem){. ass
19859 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 ert( pMem->flags
1985a 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 & MEM_Real );.
1985b 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e assert( (pMem->
1985c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
1985d 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 et)==0 );. asse
1985e 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
1985f 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
19860 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
19861 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
19862 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
19863 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 LIGNMENT(pMem) )
19864 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d ;.. pMem->u.i =
19865 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 doubleToInt64(p
19866 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f Mem->r);.. /* O
19867 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61 6c nly mark the val
19868 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 ue as an integer
19869 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 if. **. **
1986a 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d 74 (1) the round-t
1986b 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 rip conversion r
1986c 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20 69 eal->int->real i
1986d 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20 s a no-op, and.
1986e 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20 69 ** (2) The i
1986f 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 68 65 nteger is neithe
19870 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e 6f r the largest no
19871 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a 20 r the smallest.
19872 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73 69 ** possi
19873 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69 63 ble integer (tic
19874 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a 0a ket #3922). **.
19875 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ** The second
19876 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73 20 and third terms
19877 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 in the following
19878 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e 66 conditional enf
19879 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20 73 orces. ** the s
1987a 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e 20 econd condition
1987b 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d 70 under the assump
1987c 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74 69 tion that additi
1987d 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75 73 on overflow caus
1987e 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20 74 es. ** values t
1987f 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20 20 o wrap around.
19880 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 65 2c On x86 hardware,
19881 20 74 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 the third term
19882 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 74 is always. ** t
19883 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62 65 rue and could be
19884 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 77 omitted. But w
19885 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62 65 e leave it in be
19886 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a 2a cause other. **
19887 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 6d architectures m
19888 69 67 68 74 20 62 65 68 61 76 65 20 64 69 66 66 ight behave diff
19889 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 erently.. */.
1988a 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f if( pMem->r==(do
1988b 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 uble)pMem->u.i &
1988c 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c & pMem->u.i>SMAL
1988d 4c 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20 20 LEST_INT64.
1988e 20 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d && ALWAYS(pMem-
1988f 3e 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54 >u.i<LARGEST_INT
19890 36 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 64) ){. pMem-
19891 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e >flags |= MEM_In
19892 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 t;. }.}../*.**
19893 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20 Convert pMem to
19894 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49 type integer. I
19895 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 nvalidate any pr
19896 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ior representati
19897 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ons..*/.SQLITE_P
19898 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19899 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1989a 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a ify(Mem *pMem){.
1989b 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1989c 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 db==0 || sqlite3
1989d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d _mutex_held(pMem
1989e 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a ->db->mutex) );.
1989f 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
198a0 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
198a1 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Set)==0 );. ass
198a2 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
198a3 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 ALIGNMENT(pMem)
198a4 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 );.. pMem->u.i
198a5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
198a6 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d Value(pMem);. M
198a7 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d emSetTypeFlag(pM
198a8 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 em, MEM_Int);.
198a9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
198aa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
198ab 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 rt pMem so that
198ac 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45 it is of type ME
198ad 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c M_Real..** Inval
198ae 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
198af 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
198b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
198b1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
198b2 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d beMemRealify(Mem
198b3 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 *pMem){. asser
198b4 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
198b5 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
198b6 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
198b7 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
198b8 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c t( EIGHT_BYTE_AL
198b9 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b IGNMENT(pMem) );
198ba 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 .. pMem->r = sq
198bb 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
198bc 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 ue(pMem);. MemS
198bd 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c etTypeFlag(pMem,
198be 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65 MEM_Real);. re
198bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
198c0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
198c1 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 pMem so that it
198c2 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52 has types MEM_R
198c3 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f eal or MEM_Int o
198c4 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c r both..** Inval
198c5 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 idate any prior
198c6 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e representations.
198c7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
198c8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
198c9 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 beMemNumerify(Me
198ca 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 m *pMem){. doub
198cb 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34 le r1, r2;. i64
198cc 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 i;. assert( (p
198cd 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Mem->flags & (ME
198ce 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d M_Int|MEM_Real|M
198cf 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a EM_Null))==0 );.
198d0 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
198d1 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c >flags & (MEM_Bl
198d2 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20 ob|MEM_Str))!=0
198d3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
198d4 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 m->db==0 || sqli
198d5 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
198d6 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 Mem->db->mutex)
198d7 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 );. r1 = sqlite
198d8 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 3VdbeRealValue(p
198d9 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62 Mem);. i = doub
198da 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20 leToInt64(r1);.
198db 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b r2 = (double)i;
198dc 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b . if( r1==r2 ){
198dd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
198de 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d MemIntegerify(pM
198df 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 em);. }else{.
198e0 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a pMem->r = r1;.
198e1 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
198e2 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 ag(pMem, MEM_Rea
198e3 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e l);. }. return
198e4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
198e5 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 *.** Delete any
198e6 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 previous value a
198e7 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 nd set the value
198e8 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d stored in *pMem
198e9 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c to NULL..*/.SQL
198ea 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
198eb 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
198ec 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d etNull(Mem *pMem
198ed 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 ){. if( pMem->f
198ee 6c 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d 65 lags & MEM_Frame
198ef 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
198f0 64 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28 70 dbeFrameDelete(p
198f1 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b 0a Mem->u.pFrame);.
198f2 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e }. if( pMem->
198f3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
198f4 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 et ){. sqlite
198f5 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65 3RowSetClear(pMe
198f6 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 m->u.pRowSet);.
198f7 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 }. MemSetTypeF
198f8 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 lag(pMem, MEM_Nu
198f9 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 ll);. pMem->typ
198fa 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b e = SQLITE_NULL;
198fb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
198fc 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 any previous va
198fd 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 lue and set the
198fe 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c value to be a BL
198ff 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 OB of length.**
19900 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c n containing all
19901 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 zeros..*/.SQLIT
19902 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
19903 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
19904 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d ZeroBlob(Mem *pM
19905 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71 em, int n){. sq
19906 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19907 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 ase(pMem);. pMe
19908 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 m->flags = MEM_B
19909 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 lob|MEM_Zero;.
1990a 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1990b 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d ITE_BLOB;. pMem
1990c 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e ->n = 0;. if( n
1990d 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d <0 ) n = 0;. pM
1990e 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b em->u.nZero = n;
1990f 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 . pMem->enc = S
19910 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69 66 QLITE_UTF8;..#if
19911 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
19912 49 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69 74 INCRBLOB. sqlit
19913 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d e3VdbeMemGrow(pM
19914 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66 28 em, n, 0);. if(
19915 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 pMem->z ){.
19916 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 pMem->n = n;.
19917 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c memset(pMem->z,
19918 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 0, n);. }.#end
19919 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 if.}../*.** Dele
1991a 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 te any previous
1991b 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 value and set th
1991c 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 e value stored i
1991d 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a n *pMem to val,.
1991e 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 ** manifest type
1991f 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c INTEGER..*/.SQL
19920 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19921 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19922 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d 65 etInt64(Mem *pMe
19923 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 73 m, i64 val){. s
19924 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
19925 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d ease(pMem);. pM
19926 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 em->u.i = val;.
19927 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
19928 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e EM_Int;. pMem->
19929 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e type = SQLITE_IN
1992a 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TEGER;.}../*.**
1992b 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 Delete any previ
1992c 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 ous value and se
1992d 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 t the value stor
1992e 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 ed in *pMem to v
1992f 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 al,.** manifest
19930 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 type REAL..*/.SQ
19931 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19932 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
19933 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 SetDouble(Mem *p
19934 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c 29 Mem, double val)
19935 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 {. if( sqlite3I
19936 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20 20 sNaN(val) ){.
19937 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19938 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 etNull(pMem);.
19939 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
1993a 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1993b 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d (pMem);. pMem
1993c 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 70 ->r = val;. p
1993d 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1993e 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d _Real;. pMem-
1993f 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46 >type = SQLITE_F
19940 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a LOAT;. }.}../*.
19941 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 ** Delete any pr
19942 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 evious value and
19943 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f set the value o
19944 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0a f pMem to be an.
19945 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e ** empty boolean
19946 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 index..*/.SQLIT
19947 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
19948 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
19949 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 6d RowSet(Mem *pMem
1994a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1994b 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 = pMem->db;. a
1994c 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
1994d 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
1994e 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
1994f 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c Set)==0 );. sql
19950 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19951 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d se(pMem);. pMem
19952 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 ->zMalloc = sqli
19953 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
19954 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 62 b, 64);. if( db
19955 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
19956 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 {. pMem->flag
19957 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
19958 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
19959 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 t( pMem->zMalloc
1995a 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e );. pMem->u.
1995b 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 65 pRowSet = sqlite
1995c 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c 20 3RowSetInit(db,
1995d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a pMem->zMalloc, .
1995e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1995f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19960 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
19961 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 MallocSize(db, p
19962 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a Mem->zMalloc));.
19963 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
19964 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 29 ->u.pRowSet!=0 )
19965 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
19966 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a s = MEM_RowSet;.
19967 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
19968 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 urn true if the
19969 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 Mem object conta
1996a 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 4c ins a TEXT or BL
1996b 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f OB that is.** to
1996c 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 20 o large - whose
1996d 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 4c size exceeds SQL
1996e 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a ITE_MAX_LENGTH..
1996f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19970 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19971 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a eMemTooBig(Mem *
19972 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
19973 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 >db!=0 );. if(
19974 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f p->flags & (MEM_
19975 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b Str|MEM_Blob) ){
19976 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e . int n = p->
19977 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c n;. if( p->fl
19978 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
19979 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e {. n += p->
1997a 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 u.nZero;. }.
1997b 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 return n>p->d
1997c 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1997d 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a _LIMIT_LENGTH];.
1997e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20 }. return 0;
1997f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f .}../*.** Size o
19980 66 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f 74 f struct Mem not
19981 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 4d including the M
19982 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 em.zMalloc membe
19983 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 r..*/.#define ME
19984 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 5f MCELLSIZE (size_
19985 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 2d t)(&(((Mem *)0)-
19986 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a >zMalloc))../*.*
19987 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c 6f * Make an shallo
19988 77 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20 w copy of pFrom
19989 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72 into pTo. Prior
1998a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 contents of.**
1998b 70 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20 20 pTo are freed.
1998c 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69 65 The pFrom->z fie
1998d 6c 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69 63 ld is not duplic
1998e 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46 72 ated. If.** pFr
1998f 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20 74 om->z is used, t
19990 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74 hen pTo->z point
19991 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 68 s to the same th
19992 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a ing as pFrom->z.
19993 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65 74 ** and flags get
19994 73 20 73 72 63 54 79 70 65 20 28 65 69 74 68 65 s srcType (eithe
19995 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20 4d r MEM_Ephem or M
19996 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a 53 EM_Static)..*/.S
19997 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
19998 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
19999 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d mShallowCopy(Mem
1999a 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d *pTo, const Mem
1999b 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72 63 *pFrom, int src
1999c 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Type){. assert(
1999d 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 (pFrom->flags &
1999e 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
1999f 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
199a0 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
199a1 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 al(pTo);. memcp
199a2 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 y(pTo, pFrom, ME
199a3 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54 MCELLSIZE);. pT
199a4 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 o->xDel = 0;. i
199a5 66 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 f( (pFrom->flags
199a6 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 &MEM_Dyn)!=0 ||
199a7 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d pFrom->z==pFrom-
199a8 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 >zMalloc ){.
199a9 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 pTo->flags &= ~(
199aa 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 MEM_Dyn|MEM_Stat
199ab 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 ic|MEM_Ephem);.
199ac 20 20 20 61 73 73 65 72 74 28 20 73 72 63 54 79 assert( srcTy
199ad 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c pe==MEM_Ephem ||
199ae 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 srcType==MEM_St
199af 61 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d atic );. pTo-
199b0 3e 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79 70 >flags |= srcTyp
199b1 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 e;. }.}../*.**
199b2 4d 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70 79 Make a full copy
199b3 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 of pFrom into p
199b4 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 To. Prior conte
199b5 6e 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a nts of pTo are.*
199b6 2a 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 74 * freed before t
199b7 68 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e he copy is made.
199b8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
199b9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
199ba 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 beMemCopy(Mem *p
199bb 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 To, const Mem *p
199bc 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 From){. int rc
199bd 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 = SQLITE_OK;..
199be 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e assert( (pFrom->
199bf 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
199c0 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 et)==0 );. sqli
199c1 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
199c2 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a eExternal(pTo);.
199c3 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 memcpy(pTo, pF
199c4 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 rom, MEMCELLSIZE
199c5 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 );. pTo->flags
199c6 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 &= ~MEM_Dyn;..
199c7 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28 if( pTo->flags&(
199c8 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
199c9 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d ) ){. if( 0==
199ca 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 (pFrom->flags&ME
199cb 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20 M_Static) ){.
199cc 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d pTo->flags |=
199cd 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 MEM_Ephem;.
199ce 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
199cf 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
199d0 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 le(pTo);. }.
199d1 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }.. return rc;
199d2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 .}../*.** Transf
199d3 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 er the contents
199d4 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e of pFrom to pTo.
199d5 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61 Any existing va
199d6 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a lue in pTo is.**
199d7 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d freed. If pFrom
199d8 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65 contains epheme
199d9 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79 ral data, a copy
199da 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 is made..**.**
199db 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 pFrom contains a
199dc 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 n SQL NULL when
199dd 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
199de 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f urns..*/.SQLITE_
199df 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
199e0 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
199e1 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 Mem *pTo, Mem *p
199e2 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 From){. assert(
199e3 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c pFrom->db==0 ||
199e4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
199e5 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d eld(pFrom->db->m
199e6 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
199e7 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c t( pTo->db==0 ||
199e8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
199e9 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 eld(pTo->db->mut
199ea 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
199eb 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c pFrom->db==0 ||
199ec 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 pTo->db==0 || p
199ed 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 From->db==pTo->d
199ee 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56 b );.. sqlite3V
199ef 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54 dbeMemRelease(pT
199f0 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f o);. memcpy(pTo
199f1 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 , pFrom, sizeof(
199f2 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e Mem));. pFrom->
199f3 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
199f4 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 ;. pFrom->xDel
199f5 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d = 0;. pFrom->zM
199f6 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a alloc = 0;.}../*
199f7 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 .** Change the v
199f8 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f alue of a Mem to
199f9 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20 be a string or
199fa 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 a BLOB..**.** Th
199fb 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d e memory managem
199fc 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 70 ent strategy dep
199fd 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 ends on the valu
199fe 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a e of the xDel.**
199ff 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74 parameter. If t
19a00 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 he value passed
19a01 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 is SQLITE_TRANSI
19a02 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a ENT, then the .*
19a03 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69 * string is copi
19a04 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69 ed into a (possi
19a05 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 75 bly existing) bu
19a06 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20 ffer managed by
19a07 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 the .** Mem stru
19a08 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 cture. Otherwise
19a09 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62 , any existing b
19a0a 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 61 uffer is freed a
19a0b 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 nd the.** pointe
19a0c 72 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 r copied..**.**
19a0d 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 If the string is
19a0e 20 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69 too large (if i
19a0f 74 20 65 78 63 65 65 64 73 20 74 68 65 20 53 51 t exceeds the SQ
19a10 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
19a11 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29 H.** size limit)
19a12 20 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 then no memory
19a13 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 allocation occur
19a14 73 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 6e s. If the strin
19a15 67 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 g can be.** stor
19a16 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63 ed without alloc
19a17 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68 ating memory, th
19a18 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 20 en it is. If a
19a19 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
19a1a 6e 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 n.** is required
19a1b 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 74 to store the st
19a1c 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 65 ring, then value
19a1d 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 68 of pMem is unch
19a1e 61 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 anged. In.** ei
19a1f 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49 54 ther case, SQLIT
19a20 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74 75 E_TOOBIG is retu
19a21 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
19a22 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19a23 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
19a24 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 (. Mem *pMem,
19a25 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
19a26 79 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f y cell to set to
19a27 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f string value */
19a28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
19a29 2c 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 , /* String
19a2a 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e pointer */. in
19a2b 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 t n,
19a2c 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 /* Bytes in st
19a2d 72 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 ring, or negativ
19a2e 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 e */. u8 enc,
19a2f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e /* En
19a30 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 coding of z. 0
19a31 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 for BLOBs */. v
19a32 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
19a33 2a 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 *) /* Destructor
19a34 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a function */.){.
19a35 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b int nByte = n;
19a36 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c /* New val
19a37 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a ue for pMem->n *
19a38 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20 /. int iLimit;
19a39 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d /* Maxim
19a3a 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e um allowed strin
19a3b 67 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a g or blob size *
19a3c 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20 /. u16 flags =
19a3d 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 0; /* New v
19a3e 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 alue for pMem->f
19a3f 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 lags */.. asser
19a40 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c t( pMem->db==0 |
19a41 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
19a42 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d held(pMem->db->m
19a43 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
19a44 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 t( (pMem->flags
19a45 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
19a46 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69 );.. /* If z i
19a47 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
19a48 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f , set pMem to co
19a49 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c ntain an SQL NUL
19a4a 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29 L. */. if( !z )
19a4b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
19a4c 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d eMemSetNull(pMem
19a4d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
19a4e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
19a4f 69 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a if( pMem->db ){.
19a50 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65 iLimit = pMe
19a51 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 m->db->aLimit[SQ
19a52 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
19a53 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 H];. }else{.
19a54 20 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 iLimit = SQLITE
19a55 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d _MAX_LENGTH;. }
19a56 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d . flags = (enc=
19a57 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f =0?MEM_Blob:MEM_
19a58 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 Str);. if( nByt
19a59 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 e<0 ){. asser
19a5a 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20 t( enc!=0 );.
19a5b 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 if( enc==SQLITE
19a5c 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66 _UTF8 ){. f
19a5d 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 or(nByte=0; nByt
19a5e 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e e<=iLimit && z[n
19a5f 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b Byte]; nByte++){
19a60 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
19a61 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 for(nByte=0;
19a62 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 nByte<=iLimit &&
19a63 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e (z[nByte] | z[n
19a64 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b Byte+1]); nByte+
19a65 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 =2){}. }.
19a66 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 flags |= MEM_Ter
19a67 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 m;. }.. /* The
19a68 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
19a69 20 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 61 sets the new va
19a6a 6c 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e lues of Mem.z an
19a6b 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 d Mem.xDel. It.
19a6c 20 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20 ** also sets a
19a6d 66 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61 flag in local va
19a6e 72 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74 riable "flags" t
19a6f 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d o indicate the m
19a70 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 emory. ** manag
19a71 65 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45 ement (one of ME
19a72 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 M_Dyn or MEM_Sta
19a73 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 tic).. */. if(
19a74 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 xDel==SQLITE_TR
19a75 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69 ANSIENT ){. i
19a76 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 nt nAlloc = nByt
19a77 65 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 e;. if( flags
19a78 26 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20 &MEM_Term ){.
19a79 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e nAlloc += (en
19a7a 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 c==SQLITE_UTF8?1
19a7b 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 :2);. }. i
19a7c 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 f( nByte>iLimit
19a7d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
19a7e 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 SQLITE_TOOBIG;.
19a7f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c }. if( sql
19a80 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
19a81 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 pMem, nAlloc, 0)
19a82 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19a83 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
19a84 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 }. memcpy(
19a85 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c pMem->z, z, nAll
19a86 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 oc);. }else if(
19a87 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 xDel==SQLITE_DY
19a88 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c NAMIC ){. sql
19a89 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19a8a 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d se(pMem);. pM
19a8b 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d em->zMalloc = pM
19a8c 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 em->z = (char *)
19a8d 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65 z;. pMem->xDe
19a8e 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a l = 0;. }else{.
19a8f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
19a90 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
19a91 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 . pMem->z = (
19a92 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d char *)z;. pM
19a93 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b em->xDel = xDel;
19a94 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28 . flags |= ((
19a95 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41 xDel==SQLITE_STA
19a96 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a TIC)?MEM_Static:
19a97 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 MEM_Dyn);. }..
19a98 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 pMem->n = nByte
19a99 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 ;. pMem->flags
19a9a 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d = flags;. pMem-
19a9b 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f >enc = (enc==0 ?
19a9c 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 SQLITE_UTF8 : e
19a9d 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 nc);. pMem->typ
19a9e 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 e = (enc==0 ? SQ
19a9f 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 LITE_BLOB : SQLI
19aa0 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 TE_TEXT);..#ifnd
19aa1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
19aa2 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d TF16. if( pMem-
19aa3 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 >enc!=SQLITE_UTF
19aa4 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 8 && sqlite3Vdbe
19aa5 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 MemHandleBom(pMe
19aa6 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e m) ){. return
19aa7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
19aa8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 }.#endif.. if(
19aa9 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b nByte>iLimit ){
19aaa 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
19aab 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a TE_TOOBIG;. }..
19aac 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19aad 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d OK;.}../*.** Com
19aae 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 pare the values
19aaf 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65 contained by the
19ab0 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c two memory cell
19ab1 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 s, returning.**
19ab2 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f negative, zero o
19ab3 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d r positive if pM
19ab4 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e em1 is less than
19ab5 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 , equal to, or g
19ab6 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70 reater.** than p
19ab7 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72 Mem2. Sorting or
19ab8 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69 der is NULL's fi
19ab9 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 rst, followed by
19aba 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65 numbers (intege
19abb 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29 rs.** and reals)
19abc 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61 sorted numerica
19abd 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 lly, followed by
19abe 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 79 text ordered by
19abf 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a the collating.*
19ac0 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c * sequence pColl
19ac1 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f and finally blo
19ac2 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d b's ordered by m
19ac3 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 emcmp()..**.** T
19ac4 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 wo NULL values a
19ac5 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 re considered eq
19ac6 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63 ual by this func
19ac7 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
19ac8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19ac9 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f te3MemCompare(co
19aca 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 nst Mem *pMem1,
19acb 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 const Mem *pMem2
19acc 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20 , const CollSeq
19acd 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 *pColl){. int r
19ace 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b c;. int f1, f2;
19acf 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f . int combined_
19ad0 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 flags;.. /* Int
19ad1 65 72 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 61 erchange pMem1 a
19ad2 6e 64 20 70 4d 65 6d 32 20 69 66 20 74 68 65 20 nd pMem2 if the
19ad3 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
19ad4 63 65 20 73 70 65 63 69 66 69 65 73 0a 20 20 2a ce specifies. *
19ad5 2a 20 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 20 * DESC order..
19ad6 2a 2f 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 2d */. f1 = pMem1-
19ad7 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 70 >flags;. f2 = p
19ad8 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 Mem2->flags;. c
19ad9 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d 20 ombined_flags =
19ada 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 72 74 28 f1|f2;. assert(
19adb 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 (combined_flags
19adc 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
19add 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 0 );. . /* If o
19ade 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c ne value is NULL
19adf 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68 61 , it is less tha
19ae0 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66 20 n the other. If
19ae1 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a 2a both values. **
19ae2 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 72 are NULL, retur
19ae3 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 n 0.. */. if(
19ae4 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d combined_flags&M
19ae5 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 EM_Null ){. r
19ae6 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 eturn (f2&MEM_Nu
19ae7 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 ll) - (f1&MEM_Nu
19ae8 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 ll);. }.. /* I
19ae9 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 f one value is a
19aea 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20 number and the
19aeb 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 68 other is not, th
19aec 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 e number is less
19aed 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 .. ** If both a
19aee 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d 70 re numbers, comp
19aef 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66 20 are as reals if
19af0 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20 6f one is a real, o
19af1 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20 20 r as integers.
19af2 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75 65 ** if both value
19af3 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e 0a s are integers..
19af4 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 */. if( combi
19af5 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f 49 ned_flags&(MEM_I
19af6 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b 0a nt|MEM_Real) ){.
19af7 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d 45 if( !(f1&(ME
19af8 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 M_Int|MEM_Real))
19af9 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19afa 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 1;. }. if
19afb 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 7c ( !(f2&(MEM_Int|
19afc 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20 MEM_Real)) ){.
19afd 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 return -1;.
19afe 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 31 }. if( (f1
19aff 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 29 & f2 & MEM_Int)
19b00 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 ==0 ){. dou
19b01 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 20 ble r1, r2;.
19b02 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 65 if( (f1&MEM_Re
19b03 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 al)==0 ){.
19b04 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65 29 70 r1 = (double)p
19b05 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 Mem1->u.i;.
19b06 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
19b07 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20 r1 = pMem1->r;.
19b08 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
19b09 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d (f2&MEM_Real)==
19b0a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 32 20 0 ){. r2
19b0b 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32 2d = (double)pMem2-
19b0c 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 >u.i;. }els
19b0d 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 e{. r2 =
19b0e 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 20 pMem2->r;.
19b0f 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c 72 }. if( r1<r
19b10 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 2 ) return -1;.
19b11 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20 29 if( r1>r2 )
19b12 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
19b13 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
19b14 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
19b15 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 29 rt( f1&MEM_Int )
19b16 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
19b17 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 f2&MEM_Int );.
19b18 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 if( pMem1->u
19b19 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 .i < pMem2->u.i
19b1a 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 ) return -1;.
19b1b 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e if( pMem1->u.
19b1c 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 i > pMem2->u.i )
19b1d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
19b1e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
19b1f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e . }.. /* If on
19b20 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 e value is a str
19b21 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 65 ing and the othe
19b22 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65 r is a blob, the
19b23 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 2e string is less.
19b24 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 . ** If both ar
19b25 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 61 e strings, compa
19b26 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6c re using the col
19b27 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 lating functions
19b28 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d .. */. if( com
19b29 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f bined_flags&MEM_
19b2a 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28 Str ){. if( (
19b2b 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 f1 & MEM_Str)==0
19b2c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19b2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 1;. }. if
19b2e 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 29 ( (f2 & MEM_Str)
19b2f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
19b30 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 urn -1;. }..
19b31 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31 assert( pMem1
19b32 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e ->enc==pMem2->en
19b33 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 c );. assert(
19b34 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c pMem1->enc==SQL
19b35 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20 ITE_UTF8 || .
19b36 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d 3e pMem1->
19b37 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 enc==SQLITE_UTF1
19b38 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 6LE || pMem1->en
19b39 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 c==SQLITE_UTF16B
19b3a 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 E );.. /* The
19b3b 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
19b3c 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66 69 nce must be defi
19b3d 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e ned at this poin
19b3e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 2a t, even if. *
19b3f 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65 74 * the user delet
19b40 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e es the collation
19b41 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72 20 sequence after
19b42 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d the vdbe program
19b43 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 is. ** compi
19b44 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e 6f led (this was no
19b45 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73 t always the cas
19b46 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 e).. */. a
19b47 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c 7c ssert( !pColl ||
19b48 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a pColl->xCmp );.
19b49 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 . if( pColl )
19b4a 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d {. if( pMem
19b4b 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 1->enc==pColl->e
19b4c 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a nc ){. /*
19b4d 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72 65 The strings are
19b4e 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
19b4f 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e 67 correct encoding
19b50 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 20 . Call the.
19b51 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f ** compariso
19b52 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 63 n function direc
19b53 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 tly */. r
19b54 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d eturn pColl->xCm
19b55 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 p(pColl->pUser,p
19b56 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a Mem1->n,pMem1->z
19b57 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d ,pMem2->n,pMem2-
19b58 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 >z);. }else
19b59 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 {. const
19b5a 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 void *v1, *v2;.
19b5b 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 6e int n1, n
19b5c 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63 2;. Mem c
19b5d 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63 1;. Mem c
19b5e 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 2;. memse
19b5f 74 28 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f 66 t(&c1, 0, sizeof
19b60 28 63 31 29 29 3b 0a 20 20 20 20 20 20 20 20 6d (c1));. m
19b61 65 6d 73 65 74 28 26 63 32 2c 20 30 2c 20 73 69 emset(&c2, 0, si
19b62 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 20 20 20 zeof(c2));.
19b63 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19b64 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 31 mShallowCopy(&c1
19b65 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70 68 , pMem1, MEM_Eph
19b66 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c em);. sql
19b67 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c ite3VdbeMemShall
19b68 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 4d 65 6d owCopy(&c2, pMem
19b69 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 2, MEM_Ephem);.
19b6a 20 20 20 20 20 20 20 76 31 20 3d 20 73 71 6c 69 v1 = sqli
19b6b 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 te3ValueText((sq
19b6c 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 31 lite3_value*)&c1
19b6d 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 , pColl->enc);.
19b6e 20 20 20 20 20 20 20 6e 31 20 3d 20 76 31 3d 3d n1 = v1==
19b6f 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20 20 0 ? 0 : c1.n;.
19b70 20 20 20 20 20 20 76 32 20 3d 20 73 71 6c 69 74 v2 = sqlit
19b71 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c e3ValueText((sql
19b72 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 32 2c ite3_value*)&c2,
19b73 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 pColl->enc);.
19b74 20 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30 n2 = v2==0
19b75 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20 20 ? 0 : c2.n;.
19b76 20 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d rc = pColl-
19b77 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 >xCmp(pColl->pUs
19b78 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 er, n1, v1, n2,
19b79 76 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c v2);. sql
19b7a 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19b7b 73 65 28 26 63 31 29 3b 0a 20 20 20 20 20 20 20 se(&c1);.
19b7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19b7d 65 6c 65 61 73 65 28 26 63 32 29 3b 0a 20 20 20 elease(&c2);.
19b7e 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
19b7f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
19b80 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20 70 /* If a NULL p
19b81 6f 69 6e 74 65 72 20 77 61 73 20 70 61 73 73 65 ointer was passe
19b82 64 20 61 73 20 74 68 65 20 63 6f 6c 6c 61 74 65 d as the collate
19b83 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 function, fall
19b84 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 through. ** t
19b85 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 73 65 20 o the blob case
19b86 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d 70 28 29 and use memcmp()
19b87 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a . */. }. . /*
19b88 20 42 6f 74 68 20 76 61 6c 75 65 73 20 6d 75 73 Both values mus
19b89 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f 6d t be blobs. Com
19b8a 70 61 72 65 20 75 73 69 6e 67 20 6d 65 6d 63 6d pare using memcm
19b8b 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d 20 p(). */. rc =
19b8c 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c memcmp(pMem1->z,
19b8d 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d pMem2->z, (pMem
19b8e 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 1->n>pMem2->n)?p
19b8f 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e Mem2->n:pMem1->n
19b90 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 );. if( rc==0 )
19b91 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 65 6d 31 {. rc = pMem1
19b92 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a ->n - pMem2->n;.
19b93 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
19b94 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 .}../*.** Move d
19b95 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74 72 ata out of a btr
19b96 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 ee key or data f
19b97 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61 20 ield and into a
19b98 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0a 2a Mem structure..*
19b99 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20 6b 65 * The data or ke
19b9a 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 y is taken from
19b9b 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 70 the entry that p
19b9c 43 75 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 Cur is currently
19b9d 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e pointing.** to.
19b9e 20 20 6f 66 66 73 65 74 20 61 6e 64 20 61 6d 74 offset and amt
19b9f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 determine what
19ba0 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 portion of the d
19ba1 61 74 61 20 6f 72 20 6b 65 79 20 74 6f 20 72 65 ata or key to re
19ba2 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 trieve..** key i
19ba3 73 20 74 72 75 65 20 74 6f 20 67 65 74 20 74 68 s true to get th
19ba4 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 65 20 74 e key or false t
19ba5 6f 20 67 65 74 20 64 61 74 61 2e 20 20 54 68 65 o get data. The
19ba6 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 result is writt
19ba7 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 70 en.** into the p
19ba8 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a Mem element..**.
19ba9 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 74 72 75 ** The pMem stru
19baa 63 74 75 72 65 20 69 73 20 61 73 73 75 6d 65 64 cture is assumed
19bab 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 6c to be uninitial
19bac 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 69 6f 72 ized. Any prior
19bad 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 6f content.** is o
19bae 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 6f verwritten witho
19baf 75 74 20 62 65 69 6e 67 20 66 72 65 65 64 2e 0a ut being freed..
19bb0 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f **.** If this ro
19bb1 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 utine fails for
19bb2 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c 6c any reason (mall
19bb3 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 oc returns NULL
19bb4 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 or unable.** to
19bb5 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 69 read from the di
19bb6 73 6b 29 20 74 68 65 6e 20 74 68 65 20 70 4d 65 sk) then the pMe
19bb7 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 61 6e 20 m is left in an
19bb8 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 inconsistent sta
19bb9 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 te..*/.SQLITE_PR
19bba 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19bbb 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 3VdbeMemFromBtre
19bbc 65 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 e(. BtCursor *p
19bbd 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f 72 Cur, /* Cursor
19bbe 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 63 pointing at rec
19bbf 6f 72 64 20 74 6f 20 72 65 74 72 69 65 76 65 2e ord to retrieve.
19bc0 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 */. int offset
19bc1 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 , /* Offse
19bc2 74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72 74 t from the start
19bc3 20 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74 75 of data to retu
19bc4 72 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 2a rn bytes from. *
19bc5 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 /. int amt,
19bc6 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
19bc7 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74 75 of bytes to retu
19bc8 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 rn. */. int key
19bc9 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 , /* If
19bca 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65 20 true, retrieve
19bcb 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6b from the btree k
19bcc 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a 2f ey, not data. */
19bcd 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 . Mem *pMem
19bce 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 /* OUT: Ret
19bcf 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68 69 73 urn data in this
19bd0 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20 Mem structure.
19bd1 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 */.){. char *zD
19bd2 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 ata; /* D
19bd3 61 74 61 20 66 72 6f 6d 20 74 68 65 20 62 74 72 ata from the btr
19bd4 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 20 69 6e ee layer */. in
19bd5 74 20 61 76 61 69 6c 61 62 6c 65 20 3d 20 30 3b t available = 0;
19bd6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
19bd7 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 6f ytes available o
19bd8 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 74 72 65 n the local btre
19bd9 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 e page */. int
19bda 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
19bdb 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
19bdc 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c /.. assert( sql
19bdd 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
19bde 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a sValid(pCur) );.
19bdf 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 65 20 . /* Note: the
19be0 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 65 4b 65 calls to BtreeKe
19be1 79 46 65 74 63 68 28 29 20 61 6e 64 20 44 61 74 yFetch() and Dat
19be2 61 46 65 74 63 68 28 29 20 62 65 6c 6f 77 20 61 aFetch() below a
19be3 73 73 65 72 74 28 29 20 0a 20 20 2a 2a 20 74 68 ssert() . ** th
19be4 61 74 20 62 6f 74 68 20 74 68 65 20 42 74 53 68 at both the BtSh
19be5 61 72 65 64 20 61 6e 64 20 64 61 74 61 62 61 73 ared and databas
19be6 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 65 73 e handle mutexes
19be7 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f 0a 20 20 are held. */.
19be8 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 assert( (pMem->f
19be9 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
19bea 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 6b t)==0 );. if( k
19beb 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 20 ey ){. zData
19bec 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
19bed 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 70 3BtreeKeyFetch(p
19bee 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 Cur, &available)
19bef 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a ;. }else{. z
19bf0 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 Data = (char *)s
19bf1 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 qlite3BtreeDataF
19bf2 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 etch(pCur, &avai
19bf3 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73 lable);. }. as
19bf4 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30 20 29 sert( zData!=0 )
19bf5 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 65 74 2b ;.. if( offset+
19bf6 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 26 amt<=available &
19bf7 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d & (pMem->flags&M
19bf8 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b 0a 20 20 EM_Dyn)==0 ){.
19bf9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19bfa 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
19bfb 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 pMem->z = &zD
19bfc 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 ata[offset];.
19bfd 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
19bfe 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 EM_Blob|MEM_Ephe
19bff 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 53 m;. }else if( S
19c00 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
19c01 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
19c02 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 ow(pMem, amt+2,
19c03 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 0)) ){. pMem-
19c04 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f >flags = MEM_Blo
19c05 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 b|MEM_Dyn|MEM_Te
19c06 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e rm;. pMem->en
19c07 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d c = 0;. pMem-
19c08 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 >type = SQLITE_B
19c09 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 6b 65 79 LOB;. if( key
19c0a 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
19c0b 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 70 qlite3BtreeKey(p
19c0c 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 Cur, offset, amt
19c0d 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 , pMem->z);.
19c0e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
19c0f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 = sqlite3BtreeDa
19c10 74 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c ta(pCur, offset,
19c11 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a amt, pMem->z);.
19c12 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e }. pMem->
19c13 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20 z[amt] = 0;.
19c14 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d pMem->z[amt+1] =
19c15 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 0;. if( rc!=
19c16 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
19c17 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19c18 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
19c19 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d }. }. pMem
19c1a 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 ->n = amt;.. re
19c1b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 turn rc;.}../* T
19c1c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
19c1d 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
19c1e 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 73 nternally, it is
19c1f 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 not part of the
19c20 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 49 .** external API
19c21 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 20 . It works in a
19c22 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 73 similar way to s
19c23 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
19c24 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 74 t(),.** except t
19c25 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 he data returned
19c26 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 is in the encod
19c27 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 79 ing specified by
19c28 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 the second.** p
19c29 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 20 arameter, which
19c2a 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53 must be one of S
19c2b 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 QLITE_UTF16BE, S
19c2c 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 QLITE_UTF16LE or
19c2d 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2e .** SQLITE_UTF8.
19c2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 2d .**.** (2006-02-
19c2f 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 61 16:) The enc va
19c30 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 64 lue can be or-ed
19c31 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 46 with SQLITE_UTF
19c32 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49 16_ALIGNED..** I
19c33 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61 f that is the ca
19c34 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 se, then the res
19c35 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67 ult must be alig
19c36 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62 ned on an even b
19c37 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 2e yte.** boundary.
19c38 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19c39 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 TE const void *s
19c3a 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
19c3b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 sqlite3_value* p
19c3c 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 Val, u8 enc){.
19c3d 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 75 if( !pVal ) retu
19c3e 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 rn 0;.. assert(
19c3f 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pVal->db==0 ||
19c40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
19c41 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74 ld(pVal->db->mut
19c42 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
19c43 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 7e (enc&3)==(enc&~
19c44 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 SQLITE_UTF16_ALI
19c45 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65 72 GNED) );. asser
19c46 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 t( (pVal->flags
19c47 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
19c48 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c 2d );.. if( pVal-
19c49 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 >flags&MEM_Null
19c4a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
19c4b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 . }. assert( (
19c4c 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d 20 MEM_Blob>>3) ==
19c4d 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56 61 MEM_Str );. pVa
19c4e 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56 61 l->flags |= (pVa
19c4f 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 l->flags & MEM_B
19c50 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61 6e lob)>>3;. expan
19c51 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 69 dBlob(pVal);. i
19c52 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d f( pVal->flags&M
19c53 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73 71 EM_Str ){. sq
19c54 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 lite3VdbeChangeE
19c55 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e ncoding(pVal, en
19c56 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 c & ~SQLITE_UTF1
19c57 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20 20 6_ALIGNED);.
19c58 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49 54 if( (enc & SQLIT
19c59 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 E_UTF16_ALIGNED)
19c5a 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51 4c !=0 && 1==(1&SQL
19c5b 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 ITE_PTR_TO_INT(p
19c5c 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20 20 Val->z)) ){.
19c5d 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d assert( (pVal-
19c5e 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45 70 >flags & (MEM_Ep
19c5f 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 29 hem|MEM_Static))
19c60 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 !=0 );. if(
19c61 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
19c62 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56 61 akeWriteable(pVa
19c63 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b l)!=SQLITE_OK ){
19c64 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
19c65 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
19c66 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
19c67 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 MemNulTerminate(
19c68 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a pVal);. }else{.
19c69 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61 assert( (pVa
19c6a 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f l->flags&MEM_Blo
19c6b 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c b)==0 );. sql
19c6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e ite3VdbeMemStrin
19c6d 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29 3b gify(pVal, enc);
19c6e 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d 3d . assert( 0==
19c6f 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f (1&SQLITE_PTR_TO
19c70 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 _INT(pVal->z)) )
19c71 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 70 ;. }. assert(p
19c72 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 Val->enc==(enc &
19c73 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 ~SQLITE_UTF16_A
19c74 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c 2d LIGNED) || pVal-
19c75 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 >db==0.
19c76 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 || pVal->db
19c77 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
19c78 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65 6e ;. if( pVal->en
19c79 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 c==(enc & ~SQLIT
19c7a 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 E_UTF16_ALIGNED)
19c7b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
19c7c 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b Val->z;. }else{
19c7d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
19c7e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 }.}../*.** Crea
19c7f 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 te a new sqlite3
19c80 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a 2a _value object..*
19c81 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19c82 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
19c83 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 sqlite3ValueNew(
19c84 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
19c85 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 Mem *p = sqlite3
19c86 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c DbMallocZero(db,
19c87 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 sizeof(*p));.
19c88 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e if( p ){. p->
19c89 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
19c8a 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d 20 ;. p->type =
19c8b 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 SQLITE_NULL;.
19c8c 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 7d p->db = db;. }
19c8d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
19c8e 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
19c8f 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ew sqlite3_value
19c90 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69 6e object, contain
19c91 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ing the value of
19c92 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 pExpr..**.** Th
19c93 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f is only works fo
19c94 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65 78 r very simple ex
19c95 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 63 pressions that c
19c96 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63 6f onsist of one co
19c97 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20 nstant.** token
19c98 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31 22 (i.e. "5", "5.1"
19c99 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29 2e , "'a string'").
19c9a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 If the expressi
19c9b 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e on can.** be con
19c9c 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79 20 verted directly
19c9d 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68 into a value, th
19c9e 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 en the value is
19c9f 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a allocated and.**
19ca0 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74 74 a pointer writt
19ca1 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54 68 en to *ppVal. Th
19ca2 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 e caller is resp
19ca3 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 6c onsible for deal
19ca4 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 locating.** the
19ca5 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e 67 value by passing
19ca6 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56 61 it to sqlite3Va
19ca7 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72 20 lueFree() later
19ca8 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72 65 on. If the expre
19ca9 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 ssion.** cannot
19caa 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 be converted to
19cab 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 a value, then *p
19cac 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20 4e pVal is set to N
19cad 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ULL..*/.SQLITE_P
19cae 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19caf 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 e3ValueFromExpr(
19cb0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
19cb1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19cb2 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
19cb3 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 nection */. Exp
19cb4 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 r *pExpr,
19cb5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 /* The ex
19cb6 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61 6c pression to eval
19cb7 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 uate */. u8 enc
19cb8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
19cb9 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 /* Encoding
19cba 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20 61 to use */. u8 a
19cbb 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20 20 ffinity,
19cbc 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 /* Affinit
19cbd 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73 71 y to use */. sq
19cbe 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70 lite3_value **pp
19cbf 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 Val /* Write
19cc0 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 68 the new value h
19cc1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
19cc2 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c op;. char *zVal
19cc3 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f = 0;. sqlite3_
19cc4 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 3b value *pVal = 0;
19cc5 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 29 .. if( !pExpr )
19cc6 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20 30 {. *ppVal = 0
19cc7 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
19cc8 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f 70 ITE_OK;. }. op
19cc9 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 = pExpr->op;.
19cca 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 if( op==TK_REGIS
19ccb 54 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 TER ){. op =
19ccc 70 45 78 70 72 2d 3e 6f 70 32 3b 0a 20 20 7d 0a pExpr->op2;. }.
19ccd 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 . if( op==TK_ST
19cce 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 RING || op==TK_F
19ccf 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 LOAT || op==TK_I
19cd0 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 56 NTEGER ){. pV
19cd1 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 al = sqlite3Valu
19cd2 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66 eNew(db);. if
19cd3 28 20 70 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f ( pVal==0 ) goto
19cd4 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 28 no_mem;. if(
19cd5 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
19cd6 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 (pExpr, EP_IntVa
19cd7 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 lue) ){. sq
19cd8 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 lite3VdbeMemSetI
19cd9 6e 74 36 34 28 70 56 61 6c 2c 20 28 69 36 34 29 nt64(pVal, (i64)
19cda 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65 29 pExpr->u.iValue)
19cdb 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
19cdc 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 65 zVal = sqlite
19cdd 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 45 3DbStrDup(db, pE
19cde 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a xpr->u.zToken);.
19cdf 20 20 20 20 20 20 69 66 28 20 7a 56 61 6c 3d 3d if( zVal==
19ce0 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0 ) goto no_mem;
19ce1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 . sqlite3Va
19ce2 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20 lueSetStr(pVal,
19ce3 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45 -1, zVal, SQLITE
19ce4 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59 _UTF8, SQLITE_DY
19ce5 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 69 66 NAMIC);. if
19ce6 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 ( op==TK_FLOAT )
19ce7 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20 53 51 pVal->type = SQ
19ce8 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 LITE_FLOAT;.
19ce9 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 3d 3d 54 }. if( (op==T
19cea 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f 70 3d K_INTEGER || op=
19ceb 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26 20 61 =TK_FLOAT ) && a
19cec 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
19ced 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 AFF_NONE ){.
19cee 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 sqlite3ValueAp
19cef 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c plyAffinity(pVal
19cf0 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d , SQLITE_AFF_NUM
19cf1 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55 54 46 ERIC, SQLITE_UTF
19cf2 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 8);. }else{.
19cf3 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 sqlite3Valu
19cf4 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 eApplyAffinity(p
19cf5 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 53 Val, affinity, S
19cf6 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 QLITE_UTF8);.
19cf7 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 21 3d }. if( enc!=
19cf8 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 SQLITE_UTF8 ){.
19cf9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
19cfa 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 ChangeEncoding(p
19cfb 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d Val, enc);. }
19cfc 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d . }else if( op=
19cfd 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b 0a 20 =TK_UMINUS ) {.
19cfe 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
19cff 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 ==sqlite3ValueFr
19d00 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 70 72 2d omExpr(db,pExpr-
19d01 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 66 69 6e >pLeft,enc,affin
19d02 69 74 79 2c 26 70 56 61 6c 29 20 29 7b 0a 20 20 ity,&pVal) ){.
19d03 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 pVal->u.i =
19d04 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a -1 * pVal->u.i;.
19d05 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 /* (double
19d06 29 2d 31 20 49 6e 20 63 61 73 65 20 6f 66 20 53 )-1 In case of S
19d07 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
19d08 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a ING_POINT... */.
19d09 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 20 3d 20 pVal->r =
19d0a 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 70 56 61 (double)-1 * pVa
19d0b 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a l->r;. }. }.
19d0c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
19d0d 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c MIT_BLOB_LITERAL
19d0e 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d . else if( op==
19d0f 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69 TK_BLOB ){. i
19d10 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73 nt nVal;. ass
19d11 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 ert( pExpr->u.zT
19d12 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 oken[0]=='x' ||
19d13 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b pExpr->u.zToken[
19d14 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 61 0]=='X' );. a
19d15 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e ssert( pExpr->u.
19d16 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 zToken[1]=='\''
19d17 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71 );. pVal = sq
19d18 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 lite3ValueNew(db
19d19 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 61 6c );. if( !pVal
19d1a 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
19d1b 20 20 20 20 7a 56 61 6c 20 3d 20 26 70 45 78 70 zVal = &pExp
19d1c 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a r->u.zToken[2];.
19d1d 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74 nVal = sqlit
19d1e 65 33 53 74 72 6c 65 6e 33 30 28 7a 56 61 6c 29 e3Strlen30(zVal)
19d1f 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 -1;. assert(
19d20 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d 27 5c 27 27 zVal[nVal]=='\''
19d21 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 );. sqlite3V
19d22 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 dbeMemSetStr(pVa
19d23 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 l, sqlite3HexToB
19d24 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c 20 6e 56 lob(db, zVal, nV
19d25 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a 20 20 20 al), nVal/2,.
19d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19d27 20 20 20 20 20 20 30 2c 20 53 51 4c 49 54 45 5f 0, SQLITE_
19d28 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 DYNAMIC);. }.#e
19d29 6e 64 69 66 0a 0a 20 20 2a 70 70 56 61 6c 20 3d ndif.. *ppVal =
19d2a 20 70 56 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 pVal;. return
19d2b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d SQLITE_OK;..no_m
19d2c 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 em:. db->malloc
19d2d 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 Failed = 1;. sq
19d2e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
19d2f 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 zVal);. sqlite3
19d30 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b ValueFree(pVal);
19d31 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 . *ppVal = 0;.
19d32 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
19d33 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 OMEM;.}../*.** C
19d34 68 61 6e 67 65 20 74 68 65 20 73 74 72 69 6e 67 hange the string
19d35 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c value of an sql
19d36 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
19d37 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 t.*/.SQLITE_PRIV
19d38 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
19d39 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20 20 73 ValueSetStr(. s
19d3a 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c qlite3_value *v,
19d3b 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
19d3c 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 be set */. int
19d3d 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
19d3e 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 /* Length of
19d3f 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f string z */. co
19d40 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20 nst void *z,
19d41 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 /* Text of t
19d42 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f he new string */
19d43 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 . u8 enc,
19d44 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f /* Enco
19d45 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 ding to use */.
19d46 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
19d47 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74 72 75 id*) /* Destru
19d48 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74 72 ctor for the str
19d49 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 ing */.){. if(
19d4a 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d v ) sqlite3VdbeM
19d4b 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a 29 emSetStr((Mem *)
19d4c 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 v, z, n, enc, xD
19d4d 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 el);.}../*.** Fr
19d4e 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 ee an sqlite3_va
19d4f 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 lue object.*/.SQ
19d50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19d51 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 d sqlite3ValueFr
19d52 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ee(sqlite3_value
19d53 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 20 29 *v){. if( !v )
19d54 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 return;. sqlit
19d55 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
19d56 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71 ((Mem *)v);. sq
19d57 6c 69 74 65 33 44 62 46 72 65 65 28 28 28 4d 65 lite3DbFree(((Me
19d58 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d m*)v)->db, v);.}
19d59 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
19d5a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
19d5b 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 es in the sqlite
19d5c 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 61 3_value object a
19d5d 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20 ssuming.** that
19d5e 69 74 20 75 73 65 73 20 74 68 65 20 65 6e 63 6f it uses the enco
19d5f 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 ding "enc".*/.SQ
19d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19d61 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 sqlite3ValueByt
19d62 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 es(sqlite3_value
19d63 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b *pVal, u8 enc){
19d64 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d . Mem *p = (Mem
19d65 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 28 70 *)pVal;. if( (p
19d66 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c ->flags & MEM_Bl
19d67 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ob)!=0 || sqlite
19d68 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 3ValueText(pVal,
19d69 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 66 28 enc) ){. if(
19d6a 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f p->flags & MEM_
19d6b 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 72 65 Zero ){. re
19d6c 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e 75 turn p->n + p->u
19d6d 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 .nZero;. }els
19d6e 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
19d6f 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a p->n;. }. }.
19d70 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
19d71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
19d72 6e 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20 nd of vdbemem.c
19d73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
19d76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
19d77 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 75 egin file vdbeau
19d78 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a x.c ************
19d79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
19d7b 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d *.** 2003 Septem
19d7c 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ber 6.**.** The
19d7d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
19d7e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
19d7f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
19d80 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
19d81 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
19d82 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
19d83 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
19d84 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
19d85 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
19d86 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
19d87 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
19d88 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
19d89 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
19d8a 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
19d8b 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
19d8c 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
19d8d 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
19d8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19d92 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
19d93 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
19d94 75 73 65 64 20 66 6f 72 20 63 72 65 61 74 69 6e used for creatin
19d95 67 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c 20 61 g, destroying, a
19d96 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a nd populating.**
19d97 20 61 20 56 44 42 45 20 28 6f 72 20 61 6e 20 22 a VDBE (or an "
19d98 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 61 73 sqlite3_stmt" as
19d99 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 it is known to
19d9a 74 68 65 20 6f 75 74 73 69 64 65 20 77 6f 72 6c the outside worl
19d9b 64 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f d.) Prior.** to
19d9c 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20 version 2.8.7,
19d9d 61 6c 6c 20 74 68 69 73 20 63 6f 64 65 20 77 61 all this code wa
19d9e 73 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 s combined into
19d9f 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 the vdbe.c sourc
19da0 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74 e file..** But t
19da1 68 61 74 20 66 69 6c 65 20 77 61 73 20 67 65 74 hat file was get
19da2 74 69 6e 67 20 74 6f 6f 20 62 69 67 20 73 6f 20 ting too big so
19da3 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 73 this subroutines
19da4 20 77 65 72 65 20 73 70 6c 69 74 20 6f 75 74 2e were split out.
19da5 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
19da6 61 75 78 2e 63 2c 76 20 31 2e 34 38 30 20 32 30 aux.c,v 1.480 20
19da7 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31 3a 30 09/08/08 18:01:0
19da8 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 8 drh Exp $.*/..
19da9 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 ../*.** When deb
19daa 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 65 20 ugging the code
19dab 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 20 73 generator in a s
19dac 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 65 72 ymbolic debugger
19dad 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 , one can.** set
19dae 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 the sqlite3Vdbe
19daf 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 31 20 AddopTrace to 1
19db0 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 and all opcodes
19db1 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64 0a will be printed.
19db2 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20 61 ** as they are a
19db3 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73 74 dded to the inst
19db4 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a ruction stream..
19db5 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
19db6 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 _DEBUG.SQLITE_PR
19db7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19db8 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 3VdbeAddopTrace
19db9 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a = 0;.#endif.../*
19dba 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
19dbb 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 virtual databas
19dbc 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c e engine..*/.SQL
19dbd 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 ITE_PRIVATE Vdbe
19dbe 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 *sqlite3VdbeCre
19dbf 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ate(sqlite3 *db)
19dc0 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 {. Vdbe *p;. p
19dc1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
19dc2 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f ocZero(db, sizeo
19dc3 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 28 f(Vdbe) );. if(
19dc4 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
19dc5 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a ;. p->db = db;.
19dc6 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20 if( db->pVdbe
19dc7 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 ){. db->pVdbe
19dc8 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d ->pPrev = p;. }
19dc9 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 . p->pNext = db
19dca 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 ->pVdbe;. p->pP
19dcb 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 rev = 0;. db->p
19dcc 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d Vdbe = p;. p->m
19dcd 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
19dce 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e C_INIT;. return
19dcf 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d p;.}../*.** Rem
19dd0 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 74 ember the SQL st
19dd1 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 61 ring for a prepa
19dd2 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a red statement..*
19dd3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19dd4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
19dd5 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 2c eSetSql(Vdbe *p,
19dd6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
19dd7 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 72 65 int n, int isPre
19dd8 70 61 72 65 56 32 29 7b 0a 20 20 69 66 28 20 70 pareV2){. if( p
19dd9 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 ==0 ) return;.#i
19dda 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
19ddb 5f 54 52 41 43 45 0a 20 20 69 66 28 20 21 69 73 _TRACE. if( !is
19ddc 50 72 65 70 61 72 65 56 32 20 29 20 72 65 74 75 PrepareV2 ) retu
19ddd 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 rn;.#endif. ass
19dde 65 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 ert( p->zSql==0
19ddf 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 );. p->zSql = s
19de0 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
19de1 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20 p->db, z, n);.
19de2 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d p->isPrepareV2 =
19de3 20 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 31 isPrepareV2 ? 1
19de4 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 : 0;.}../*.** R
19de5 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61 73 eturn the SQL as
19de6 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 sociated with a
19de7 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
19de8 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 nt.*/.SQLITE_API
19de9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
19dea 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 ite3_sql(sqlite3
19deb 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 _stmt *pStmt){.
19dec 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
19ded 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 *)pStmt;. retu
19dee 72 6e 20 28 70 2d 3e 69 73 50 72 65 70 61 72 65 rn (p->isPrepare
19def 56 32 20 3f 20 70 2d 3e 7a 53 71 6c 20 3a 20 30 V2 ? p->zSql : 0
19df0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 );.}../*.** Swap
19df1 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 65 74 all content bet
19df2 77 65 65 6e 20 74 77 6f 20 56 44 42 45 20 73 74 ween two VDBE st
19df3 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c ructures..*/.SQL
19df4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19df5 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 sqlite3VdbeSwap
19df6 28 56 64 62 65 20 2a 70 41 2c 20 56 64 62 65 20 (Vdbe *pA, Vdbe
19df7 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 74 6d 70 *pB){. Vdbe tmp
19df8 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 61 72 20 , *pTmp;. char
19df9 2a 7a 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a *zTmp;. tmp = *
19dfa 70 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b pA;. *pA = *pB;
19dfb 0a 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20 . *pB = tmp;.
19dfc 70 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74 pTmp = pA->pNext
19dfd 3b 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20 ;. pA->pNext =
19dfe 70 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d pB->pNext;. pB-
19dff 3e 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20 >pNext = pTmp;.
19e00 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65 pTmp = pA->pPre
19e01 76 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d v;. pA->pPrev =
19e02 20 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42 pB->pPrev;. pB
19e03 2d 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a ->pPrev = pTmp;.
19e04 20 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 zTmp = pA->zSq
19e05 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 l;. pA->zSql =
19e06 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e pB->zSql;. pB->
19e07 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 70 zSql = zTmp;. p
19e08 42 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d B->isPrepareV2 =
19e09 20 70 41 2d 3e 69 73 50 72 65 70 61 72 65 56 32 pA->isPrepareV2
19e0a 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
19e0b 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 TE_DEBUG./*.** T
19e0c 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f urn tracing on o
19e0d 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f r off.*/.SQLITE_
19e0e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19e0f 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64 ite3VdbeTrace(Vd
19e10 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61 be *p, FILE *tra
19e11 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65 20 ce){. p->trace
19e12 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64 69 = trace;.}.#endi
19e13 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 f../*.** Resize
19e14 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72 the Vdbe.aOp arr
19e15 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 ay so that it is
19e16 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70 at least one op
19e17 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a larger than .**
19e18 20 69 74 20 77 61 73 2e 0a 2a 2a 0a 2a 2a 20 49 it was..**.** I
19e19 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f f an out-of-memo
19e1a 72 79 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 ry error occurs
19e1b 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 74 while resizing t
19e1c 68 65 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e he array, return
19e1d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d .** SQLITE_NOMEM
19e1e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 56 . In this case V
19e1f 64 62 65 2e 61 4f 70 20 61 6e 64 20 56 64 62 65 dbe.aOp and Vdbe
19e20 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65 6d 61 69 6e .nOpAlloc remain
19e21 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 28 .** unchanged (
19e22 74 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 20 this is so that
19e23 61 6e 79 20 6f 70 63 6f 64 65 73 20 61 6c 72 65 any opcodes alre
19e24 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 63 61 ady allocated ca
19e25 6e 20 62 65 20 0a 2a 2a 20 63 6f 72 72 65 63 74 n be .** correct
19e26 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 61 ly deallocated a
19e27 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65 long with the re
19e28 73 74 20 6f 66 20 74 68 65 20 56 64 62 65 29 2e st of the Vdbe).
19e29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
19e2a 72 6f 77 4f 70 41 72 72 61 79 28 56 64 62 65 20 rowOpArray(Vdbe
19e2b 2a 70 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70 *p){. VdbeOp *p
19e2c 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 4e 65 77 20 New;. int nNew
19e2d 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f = (p->nOpAlloc ?
19e2e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a p->nOpAlloc*2 :
19e2f 20 28 69 6e 74 29 28 31 30 32 34 2f 73 69 7a 65 (int)(1024/size
19e30 6f 66 28 4f 70 29 29 29 3b 0a 20 20 70 4e 65 77 of(Op)));. pNew
19e31 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
19e32 6c 6f 63 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f loc(p->db, p->aO
19e33 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 6f 66 28 4f p, nNew*sizeof(O
19e34 70 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 p));. if( pNew
19e35 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f 70 41 6c 6c ){. p->nOpAll
19e36 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 oc = sqlite3DbMa
19e37 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20 llocSize(p->db,
19e38 70 4e 65 77 29 2f 73 69 7a 65 6f 66 28 4f 70 29 pNew)/sizeof(Op)
19e39 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70 ;. p->aOp = p
19e3a 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 New;. }. retur
19e3b 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45 n (pNew ? SQLITE
19e3c 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d _OK : SQLITE_NOM
19e3d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 EM);.}../*.** Ad
19e3e 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74 d a new instruct
19e3f 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 ion to the list
19e40 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 of instructions
19e41 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a current in the.*
19e42 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20 * VDBE. Return
19e43 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 the address of t
19e44 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 he new instructi
19e45 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 on..**.** Parame
19e46 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 ters:.**.** p
19e47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 P
19e48 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44 ointer to the VD
19e49 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 BE.**.** op
19e4a 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 The
19e4b 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 opcode for this
19e4c 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a instruction.**.*
19e4d 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20 * p1, p2, p3
19e4e 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a Operands.**
19e4f 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69 .** Use the sqli
19e50 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
19e51 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 bel() function t
19e52 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73 o fix an address
19e53 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69 and.** the sqli
19e54 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 te3VdbeChangeP4(
19e55 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68 ) function to ch
19e56 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
19e57 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72 f the P4.** oper
19e58 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 and..*/.SQLITE_P
19e59 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19e5a 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 e3VdbeAddOp3(Vdb
19e5b 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e e *p, int op, in
19e5c 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e t p1, int p2, in
19e5d 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a t p3){. int i;.
19e5e 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a VdbeOp *pOp;..
19e5f 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 i = p->nOp;.
19e60 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
19e61 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
19e62 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f T );. assert( o
19e63 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29 p>0 && op<0xff )
19e64 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c ;. if( p->nOpAl
19e65 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 69 66 loc<=i ){. if
19e66 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 ( growOpArray(p)
19e67 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19e68 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 1;. }. }.
19e69 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20 p->nOp++;. pOp
19e6a 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 = &p->aOp[i];.
19e6b 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 28 75 pOp->opcode = (u
19e6c 38 29 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20 8)op;. pOp->p5
19e6d 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d = 0;. pOp->p1 =
19e6e 20 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d p1;. pOp->p2 =
19e6f 20 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d p2;. pOp->p3 =
19e70 20 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 p3;. pOp->p4.p
19e71 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74 = 0;. pOp->p4t
19e72 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 ype = P4_NOTUSED
19e73 3b 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d ;. p->expired =
19e74 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 0;.#ifdef SQLIT
19e75 45 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a E_DEBUG. pOp->z
19e76 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69 Comment = 0;. i
19e77 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 f( sqlite3VdbeAd
19e78 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74 dopTrace ) sqlit
19e79 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c e3VdbePrintOp(0,
19e7a 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b i, &p->aOp[i]);
19e7b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 .#endif.#ifdef V
19e7c 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f DBE_PROFILE. pO
19e7d 70 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 p->cycles = 0;.
19e7e 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23 pOp->cnt = 0;.#
19e7f 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69 endif. return i
19e80 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
19e81 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19e82 62 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70 beAddOp0(Vdbe *p
19e83 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74 , int op){. ret
19e84 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 urn sqlite3VdbeA
19e85 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20 ddOp3(p, op, 0,
19e86 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 0, 0);.}.SQLITE_
19e87 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19e88 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 te3VdbeAddOp1(Vd
19e89 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 be *p, int op, i
19e8a 6e 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e nt p1){. return
19e8b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
19e8c 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c p3(p, op, p1, 0,
19e8d 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 0);.}.SQLITE_PR
19e8e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19e8f 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 3VdbeAddOp2(Vdbe
19e90 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 *p, int op, int
19e91 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20 p1, int p2){.
19e92 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 return sqlite3Vd
19e93 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 beAddOp3(p, op,
19e94 70 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a p1, p2, 0);.}...
19e95 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 /*.** Add an opc
19e96 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 ode that include
19e97 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 s the p4 value a
19e98 73 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a s a pointer..*/.
19e99 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19e9a 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
19e9b 64 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c dOp4(. Vdbe *p,
19e9c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
19e9d 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f dd the opcode to
19e9e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e this VM */. in
19e9f 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 t op,
19ea0 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63 /* The new opc
19ea1 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c ode */. int p1,
19ea2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19ea3 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a The P1 operand *
19ea4 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 /. int p2,
19ea5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 /* The P
19ea6 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 2 operand */. i
19ea7 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 nt p3,
19ea8 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 /* The P3 ope
19ea9 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rand */. const
19eaa 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a char *zP4, /*
19eab 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 The P4 operand
19eac 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20 */. int p4type
19ead 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f /* P4 o
19eae 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29 perand type */.)
19eaf 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 {. int addr = s
19eb0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
19eb1 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 (p, op, p1, p2,
19eb2 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 p3);. sqlite3Vd
19eb3 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64 beChangeP4(p, ad
19eb4 64 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29 dr, zP4, p4type)
19eb5 3b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b ;. return addr;
19eb6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
19eb7 20 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20 a new symbolic
19eb8 6c 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73 label for an ins
19eb9 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61 truction that ha
19eba 73 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63 s yet to be.** c
19ebb 6f 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f oded. The symbo
19ebc 6c 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61 lic label is rea
19ebd 6c 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74 lly just a negat
19ebe 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 ive number. The
19ebf 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65 .** label can be
19ec0 20 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20 used as the P2
19ec1 76 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72 value of an oper
19ec2 61 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77 ation. Later, w
19ec3 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c hen.** the label
19ec4 20 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 is resolved to
19ec5 61 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65 a specific addre
19ec6 73 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c ss, the VDBE wil
19ec7 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67 l scan.** throug
19ec8 68 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20 h its operation
19ec9 6c 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20 list and change
19eca 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32 all values of P2
19ecb 20 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20 which match.**
19ecc 74 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74 the label into t
19ecd 68 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72 he resolved addr
19ece 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 ess..**.** The V
19ecf 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61 DBE knows that a
19ed0 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c P2 value is a l
19ed1 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62 abel because lab
19ed2 65 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79 els are.** alway
19ed3 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50 s negative and P
19ed4 32 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70 2 values are sup
19ed5 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e pose to be non-n
19ed6 65 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63 egative..** Henc
19ed7 65 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32 e, a negative P2
19ed8 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 value is a labe
19ed9 6c 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 l that has yet t
19eda 6f 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a o be resolved..*
19edb 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74 *.** Zero is ret
19edc 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f urned if a mallo
19edd 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 c() fails..*/.SQ
19ede 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19edf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
19ee0 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a Label(Vdbe *p){.
19ee1 20 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70 int i;. i = p
19ee2 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73 ->nLabel++;. as
19ee3 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d sert( p->magic==
19ee4 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 VDBE_MAGIC_INIT
19ee5 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e );. if( i>=p->n
19ee6 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20 LabelAlloc ){.
19ee7 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61 int n = p->nLa
19ee8 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0a belAlloc*2 + 5;.
19ee9 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20 p->aLabel =
19eea 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
19eeb 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d OrFree(p->db, p-
19eec 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20 >aLabel,.
19eed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19eee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19eef 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 n*sizeof(p->aLab
19ef0 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 70 2d 3e el[0]));. p->
19ef1 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71 nLabelAlloc = sq
19ef2 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
19ef3 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 e(p->db, p->aLab
19ef4 65 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c el)/sizeof(p->aL
19ef5 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 abel[0]);. }.
19ef6 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b if( p->aLabel ){
19ef7 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69 . p->aLabel[i
19ef8 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65 ] = -1;. }. re
19ef9 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a turn -1-i;.}../*
19efa 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65 .** Resolve labe
19efb 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 65 20 l "x" to be the
19efc 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e address of the n
19efd 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
19efe 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65 to.** be inserte
19eff 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 d. The paramete
19f00 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 65 20 r "x" must have
19f01 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72 been obtained fr
19f02 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61 om.** a prior ca
19f03 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 ll to sqlite3Vdb
19f04 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f eMakeLabel()..*/
19f05 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19f06 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
19f07 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62 ResolveLabel(Vdb
19f08 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20 e *p, int x){.
19f09 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20 int j = -1-x;.
19f0a 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 assert( p->magic
19f0b 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 ==VDBE_MAGIC_INI
19f0c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a T );. assert( j
19f0d 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62 >=0 && j<p->nLab
19f0e 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 el );. if( p->a
19f0f 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e Label ){. p->
19f10 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e aLabel[j] = p->n
19f11 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 Op;. }.}..#ifde
19f12 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 0a f SQLITE_DEBUG..
19f13 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
19f14 69 6e 67 20 74 79 70 65 20 61 6e 64 20 66 75 6e ing type and fun
19f15 63 74 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74 ction are used t
19f16 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 o iterate throug
19f17 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a h all opcodes.**
19f18 20 69 6e 20 61 20 56 64 62 65 20 6d 61 69 6e 20 in a Vdbe main
19f19 70 72 6f 67 72 61 6d 20 61 6e 64 20 65 61 63 68 program and each
19f1a 20 6f 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67 of the sub-prog
19f1b 72 61 6d 73 20 28 74 72 69 67 67 65 72 73 29 20 rams (triggers)
19f1c 69 74 20 6d 61 79 20 0a 2a 2a 20 69 6e 76 6f 6b it may .** invok
19f1d 65 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e e directly or in
19f1e 64 69 72 65 63 74 6c 79 2e 20 49 74 20 73 68 6f directly. It sho
19f1f 75 6c 64 20 62 65 20 75 73 65 64 20 61 73 20 66 uld be used as f
19f20 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
19f21 4f 70 20 2a 70 4f 70 3b 0a 2a 2a 20 20 20 56 64 Op *pOp;.** Vd
19f22 62 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a beOpIter sIter;.
19f23 2a 2a 0a 2a 2a 20 20 20 6d 65 6d 73 65 74 28 26 **.** memset(&
19f24 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 sIter, 0, sizeof
19f25 28 73 49 74 65 72 29 29 3b 0a 2a 2a 20 20 20 73 (sIter));.** s
19f26 49 74 65 72 2e 76 20 3d 20 76 3b 20 20 20 20 20 Iter.v = v;
19f27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19f28 20 20 20 20 20 20 20 2f 2f 20 76 20 69 73 20 6f // v is o
19f29 66 20 74 79 70 65 20 56 64 62 65 2a 20 0a 2a 2a f type Vdbe* .**
19f2a 20 20 20 77 68 69 6c 65 28 20 28 70 4f 70 20 3d while( (pOp =
19f2b 20 6f 70 49 74 65 72 4e 65 78 74 28 26 73 49 74 opIterNext(&sIt
19f2c 65 72 29 29 20 29 7b 0a 2a 2a 20 20 20 20 20 2f er)) ){.** /
19f2d 2f 20 44 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 / Do something w
19f2e 69 74 68 20 70 4f 70 0a 2a 2a 20 20 20 7d 0a 2a ith pOp.** }.*
19f2f 2a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 * sqlite3DbFre
19f30 65 28 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61 e(v->db, sIter.a
19f31 70 53 75 62 29 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79 pSub);.** .*/.ty
19f32 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
19f33 65 4f 70 49 74 65 72 20 56 64 62 65 4f 70 49 74 eOpIter VdbeOpIt
19f34 65 72 3b 0a 73 74 72 75 63 74 20 56 64 62 65 4f er;.struct VdbeO
19f35 70 49 74 65 72 20 7b 0a 20 20 56 64 62 65 20 2a pIter {. Vdbe *
19f36 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
19f37 20 20 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20 /* Vdbe to
19f38 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 iterate through
19f39 74 68 65 20 6f 70 63 6f 64 65 73 20 6f 66 20 2a the opcodes of *
19f3a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a /. SubProgram *
19f3b 2a 61 70 53 75 62 3b 20 20 20 20 20 20 20 20 2f *apSub; /
19f3c 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62 70 72 * Array of subpr
19f3d 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 ograms */. int
19f3e 6e 53 75 62 3b 20 20 20 20 20 20 20 20 20 20 20 nSub;
19f3f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
19f40 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
19f41 70 53 75 62 20 2a 2f 0a 20 20 69 6e 74 20 69 41 pSub */. int iA
19f42 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 ddr;
19f43 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
19f44 6f 66 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 of next instruct
19f45 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f ion to return */
19f46 0a 20 20 69 6e 74 20 69 53 75 62 3b 20 20 20 20 . int iSub;
19f47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19f48 20 30 20 3d 20 6d 61 69 6e 20 70 72 6f 67 72 61 0 = main progra
19f49 6d 2c 20 31 20 3d 20 66 69 72 73 74 20 73 75 62 m, 1 = first sub
19f4a 2d 70 72 6f 67 72 61 6d 20 65 74 63 2e 20 2a 2f -program etc. */
19f4b 0a 7d 3b 0a 73 74 61 74 69 63 20 4f 70 20 2a 6f .};.static Op *o
19f4c 70 49 74 65 72 4e 65 78 74 28 56 64 62 65 4f 70 pIterNext(VdbeOp
19f4d 49 74 65 72 20 2a 70 29 7b 0a 20 20 56 64 62 65 Iter *p){. Vdbe
19f4e 20 2a 76 20 3d 20 70 2d 3e 76 3b 0a 20 20 4f 70 *v = p->v;. Op
19f4f 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 4f 70 *pRet = 0;. Op
19f50 20 2a 61 4f 70 3b 0a 20 20 69 6e 74 20 6e 4f 70 *aOp;. int nOp
19f51 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 53 75 62 ;.. if( p->iSub
19f52 3c 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 0a 20 20 <=p->nSub ){..
19f53 20 20 69 66 28 20 70 2d 3e 69 53 75 62 3d 3d 30 if( p->iSub==0
19f54 20 29 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 ){. aOp =
19f55 76 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f v->aOp;. nO
19f56 70 20 3d 20 76 2d 3e 6e 4f 70 3b 0a 20 20 20 20 p = v->nOp;.
19f57 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 4f 70 }else{. aOp
19f58 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 = p->apSub[p->i
19f59 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b 0a 20 20 20 Sub-1]->aOp;.
19f5a 20 20 20 6e 4f 70 20 3d 20 70 2d 3e 61 70 53 75 nOp = p->apSu
19f5b 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 6e 4f b[p->iSub-1]->nO
19f5c 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 p;. }. ass
19f5d 65 72 74 28 20 70 2d 3e 69 41 64 64 72 3c 6e 4f ert( p->iAddr<nO
19f5e 70 20 29 3b 0a 0a 20 20 20 20 70 52 65 74 20 3d p );.. pRet =
19f5f 20 26 61 4f 70 5b 70 2d 3e 69 41 64 64 72 5d 3b &aOp[p->iAddr];
19f60 0a 20 20 20 20 70 2d 3e 69 41 64 64 72 2b 2b 3b . p->iAddr++;
19f61 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 41 64 64 . if( p->iAdd
19f62 72 3d 3d 6e 4f 70 20 29 7b 0a 20 20 20 20 20 20 r==nOp ){.
19f63 70 2d 3e 69 53 75 62 2b 2b 3b 0a 20 20 20 20 20 p->iSub++;.
19f64 20 70 2d 3e 69 41 64 64 72 20 3d 20 30 3b 0a 20 p->iAddr = 0;.
19f65 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 }. . if(
19f66 70 52 65 74 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pRet->p4type==P4
19f67 5f 53 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 _SUBPROGRAM ){.
19f68 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d int nByte =
19f69 20 28 70 2d 3e 6e 53 75 62 2b 31 29 2a 73 69 7a (p->nSub+1)*siz
19f6a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 eof(SubProgram*)
19f6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 ;. int j;.
19f6c 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c for(j=0; j<
19f6d 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 p->nSub; j++){.
19f6e 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 61 70 if( p->ap
19f6f 53 75 62 5b 6a 5d 3d 3d 70 52 65 74 2d 3e 70 34 Sub[j]==pRet->p4
19f70 2e 70 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61 .pProgram ) brea
19f71 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
19f72 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e 53 75 62 20 if( j==p->nSub
19f73 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70 ){. p->ap
19f74 53 75 62 20 3d 20 73 71 6c 69 74 65 33 44 62 52 Sub = sqlite3DbR
19f75 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 2d 3e eallocOrFree(v->
19f76 64 62 2c 20 70 2d 3e 61 70 53 75 62 2c 20 6e 42 db, p->apSub, nB
19f77 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 yte);. if
19f78 28 20 21 70 2d 3e 61 70 53 75 62 20 29 7b 0a 20 ( !p->apSub ){.
19f79 20 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 pRet =
19f7a 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0;. }else
19f7b 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 {. p->a
19f7c 70 53 75 62 5b 70 2d 3e 6e 53 75 62 2b 2b 5d 20 pSub[p->nSub++]
19f7d 3d 20 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67 = pRet->p4.pProg
19f7e 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ram;. }.
19f7f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
19f80 0a 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b .. return pRet;
19f81 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 .}../*.** Check
19f82 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 73 if the program s
19f83 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 4d 20 tored in the VM
19f84 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
19f85 70 50 61 72 73 65 20 6d 61 79 0a 2a 2a 20 74 68 pParse may.** th
19f86 72 6f 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63 row an ABORT exc
19f87 65 70 74 69 6f 6e 20 28 63 61 75 73 69 6e 67 20 eption (causing
19f88 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 the statement, b
19f89 75 74 20 6e 6f 74 20 74 72 61 6e 73 61 63 74 69 ut not transacti
19f8a 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c on.** to be roll
19f8b 65 64 20 62 61 63 6b 29 2e 20 54 68 69 73 20 63 ed back). This c
19f8c 6f 6e 64 69 74 69 6f 6e 20 69 73 20 74 72 75 65 ondition is true
19f8d 20 69 66 20 74 68 65 20 6d 61 69 6e 20 70 72 6f if the main pro
19f8e 67 72 61 6d 20 6f 72 20 61 6e 79 0a 2a 2a 20 73 gram or any.** s
19f8f 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 6f 6e 74 ub-programs cont
19f90 61 69 6e 73 20 61 6e 79 20 6f 66 20 74 68 65 20 ains any of the
19f91 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
19f92 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 20 77 69 * OP_Halt wi
19f93 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e th P1=SQLITE_CON
19f94 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f STRAINT and P2=O
19f95 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 E_Abort..** *
19f96 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 77 OP_HaltIfNull w
19f97 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f ith P1=SQLITE_CO
19f98 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d NSTRAINT and P2=
19f99 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a OE_Abort..** *
19f9a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a 2a 20 OP_Destroy.**
19f9b 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 65 0a * OP_VUpdate.
19f9c 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65 6e 61 ** * OP_VRena
19f9d 6d 65 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 63 68 me.**.** Then ch
19f9e 65 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c eck that the val
19f9f 75 65 20 6f 66 20 50 61 72 73 65 2e 6d 61 79 41 ue of Parse.mayA
19fa0 62 6f 72 74 20 69 73 20 74 72 75 65 20 69 66 20 bort is true if
19fa1 61 6e 0a 2a 2a 20 41 42 4f 52 54 20 6d 61 79 20 an.** ABORT may
19fa2 62 65 20 74 68 72 6f 77 6e 2c 20 6f 72 20 66 61 be thrown, or fa
19fa3 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e 20 52 lse otherwise. R
19fa4 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 eturn true if it
19fa5 20 64 6f 65 73 0a 2a 2a 20 6d 61 74 63 68 2c 20 does.** match,
19fa6 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 or false otherwi
19fa7 73 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f se. This functio
19fa8 6e 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f n is intended to
19fa9 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 70 be used as.** p
19faa 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 art of an assert
19fab 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 statement in th
19fac 65 20 63 6f 6d 70 69 6c 65 72 2e 20 53 69 6d 69 e compiler. Simi
19fad 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 lar to:.**.**
19fae 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 56 assert( sqlite3V
19faf 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 dbeAssertMayAbor
19fb0 74 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c t(pParse->pVdbe,
19fb1 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 pParse->mayAbor
19fb2 74 29 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f t) );.*/.SQLITE_
19fb3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19fb4 74 65 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 te3VdbeAssertMay
19fb5 41 62 6f 72 74 28 56 64 62 65 20 2a 76 2c 20 69 Abort(Vdbe *v, i
19fb6 6e 74 20 6d 61 79 41 62 6f 72 74 29 7b 0a 20 20 nt mayAbort){.
19fb7 69 6e 74 20 68 61 73 41 62 6f 72 74 20 3d 20 30 int hasAbort = 0
19fb8 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 ;. Op *pOp;. V
19fb9 64 62 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b dbeOpIter sIter;
19fba 0a 20 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72 . memset(&sIter
19fbb 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 , 0, sizeof(sIte
19fbc 72 29 29 3b 0a 20 20 73 49 74 65 72 2e 76 20 3d r));. sIter.v =
19fbd 20 76 3b 0a 0a 20 20 77 68 69 6c 65 28 20 28 70 v;.. while( (p
19fbe 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28 Op = opIterNext(
19fbf 26 73 49 74 65 72 29 29 21 3d 30 20 29 7b 0a 20 &sIter))!=0 ){.
19fc0 20 20 20 69 6e 74 20 6f 70 63 6f 64 65 20 3d 20 int opcode =
19fc1 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 pOp->opcode;.
19fc2 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f if( opcode==OP_
19fc3 44 65 73 74 72 6f 79 20 7c 7c 20 6f 70 63 6f 64 Destroy || opcod
19fc4 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 7c 7c e==OP_VUpdate ||
19fc5 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 65 6e opcode==OP_VRen
19fc6 61 6d 65 20 0a 20 20 20 20 20 7c 7c 20 28 28 6f ame . || ((o
19fc7 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 7c pcode==OP_Halt |
19fc8 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c | opcode==OP_Hal
19fc9 74 49 66 4e 75 6c 6c 29 20 0a 20 20 20 20 20 20 tIfNull) .
19fca 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c && (pOp->p1==SQL
19fcb 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26 ITE_CONSTRAINT &
19fcc 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 62 & pOp->p2==OE_Ab
19fcd 6f 72 74 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 ort)). ){.
19fce 20 20 20 68 61 73 41 62 6f 72 74 20 3d 20 31 3b hasAbort = 1;
19fcf 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
19fd0 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 }. }. sqlite
19fd1 33 44 62 46 72 65 65 28 76 2d 3e 64 62 2c 20 73 3DbFree(v->db, s
19fd2 49 74 65 72 2e 61 70 53 75 62 29 3b 0a 0a 20 20 Iter.apSub);..
19fd3 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 /* Return true i
19fd4 66 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79 41 f hasAbort==mayA
19fd5 62 6f 72 74 2e 20 4f 72 20 69 66 20 61 20 6d 61 bort. Or if a ma
19fd6 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 lloc failure occ
19fd7 75 72 65 64 2e 0a 20 20 2a 2a 20 49 66 20 6d 61 ured.. ** If ma
19fd8 6c 6c 6f 63 20 66 61 69 6c 65 64 2c 20 74 68 65 lloc failed, the
19fd9 6e 20 74 68 65 20 77 68 69 6c 65 28 29 20 6c 6f n the while() lo
19fda 6f 70 20 61 62 6f 76 65 20 6d 61 79 20 6e 6f 74 op above may not
19fdb 20 68 61 76 65 20 69 74 65 72 61 74 65 64 0a 20 have iterated.
19fdc 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 ** through all
19fdd 6f 70 63 6f 64 65 73 20 61 6e 64 20 68 61 73 41 opcodes and hasA
19fde 62 6f 72 74 20 6d 61 79 20 62 65 20 73 65 74 20 bort may be set
19fdf 69 6e 63 6f 72 72 65 63 74 6c 79 2e 20 52 65 74 incorrectly. Ret
19fe0 75 72 6e 0a 20 20 2a 2a 20 74 72 75 65 20 66 6f urn. ** true fo
19fe1 72 20 74 68 69 73 20 63 61 73 65 20 74 6f 20 70 r this case to p
19fe2 72 65 76 65 6e 74 20 74 68 65 20 61 73 73 65 72 revent the asser
19fe3 74 28 29 20 69 6e 20 74 68 65 20 63 61 6c 6c 65 t() in the calle
19fe4 72 73 20 66 72 61 6d 65 0a 20 20 2a 2a 20 66 72 rs frame. ** fr
19fe5 6f 6d 20 66 61 69 6c 69 6e 67 2e 20 20 2a 2f 0a om failing. */.
19fe6 20 20 72 65 74 75 72 6e 20 28 20 76 2d 3e 64 62 return ( v->db
19fe7 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c ->mallocFailed |
19fe8 7c 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79 41 | hasAbort==mayA
19fe9 62 6f 72 74 20 29 3b 0a 7d 0a 23 65 6e 64 69 66 bort );.}.#endif
19fea 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 68 72 ../*.** Loop thr
19feb 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72 61 6d ough the program
19fec 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 32 20 looking for P2
19fed 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 values that are
19fee 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e 20 6a negative.** on j
19fef 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ump instructions
19ff0 2e 20 20 45 61 63 68 20 73 75 63 68 20 76 61 6c . Each such val
19ff1 75 65 20 69 73 20 61 20 6c 61 62 65 6c 2e 20 20 ue is a label.
19ff2 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a 20 6c Resolve the.** l
19ff3 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e 67 20 abel by setting
19ff4 74 68 65 20 50 32 20 76 61 6c 75 65 20 74 6f 20 the P2 value to
19ff5 69 74 73 20 63 6f 72 72 65 63 74 20 6e 6f 6e 2d its correct non-
19ff6 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a zero value..**.*
19ff7 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
19ff8 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 61 66 s called once af
19ff9 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 ter all opcodes
19ffa 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 have been insert
19ffb 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 ed..**.** Variab
19ffc 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 le *pMaxFuncArgs
19ffd 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6d is set to the m
19ffe 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 aximum value of
19fff 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e 74 20 any P2 argument
1a000 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46 75 6e .** to an OP_Fun
1a001 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53 74 65 ction, OP_AggSte
1a002 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20 p or OP_VFilter
1a003 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 69 73 20 opcode. This is
1a004 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69 used by .** sqli
1a005 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
1a006 28 29 20 74 6f 20 73 69 7a 65 20 74 68 65 20 56 () to size the V
1a007 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 dbe.apArg[] arra
1a008 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 y..*/.static voi
1a009 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 d resolveP2Value
1a00a 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a s(Vdbe *p, int *
1a00b 70 4d 61 78 46 75 6e 63 41 72 67 73 29 7b 0a 20 pMaxFuncArgs){.
1a00c 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4d int i;. int nM
1a00d 61 78 41 72 67 73 20 3d 20 2a 70 4d 61 78 46 75 axArgs = *pMaxFu
1a00e 6e 63 41 72 67 73 3b 0a 20 20 4f 70 20 2a 70 4f ncArgs;. Op *pO
1a00f 70 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c p;. int *aLabel
1a010 20 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 = p->aLabel;.
1a011 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b p->readOnly = 1;
1a012 0a 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f . for(pOp=p->aO
1a013 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 p, i=p->nOp-1; i
1a014 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 >=0; i--, pOp++)
1a015 7b 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 {. u8 opcode
1a016 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a = pOp->opcode;..
1a017 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d if( opcode==
1a018 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f OP_Function || o
1a019 70 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 pcode==OP_AggSte
1a01a 70 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 p ){. if( p
1a01b 4f 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 Op->p5>nMaxArgs
1a01c 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 ) nMaxArgs = pOp
1a01d 2d 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 ->p5;.#ifndef SQ
1a01e 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1a01f 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 LTABLE. }else
1a020 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f if( opcode==OP_
1a021 56 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 VUpdate ){.
1a022 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 if( pOp->p2>nMa
1a023 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 xArgs ) nMaxArgs
1a024 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 = pOp->p2;.#end
1a025 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 if. }else if(
1a026 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e opcode==OP_Tran
1a027 73 61 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e saction && pOp->
1a028 70 32 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 p2!=0 ){. p
1a029 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a ->readOnly = 0;.
1a02a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1a02b 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1a02c 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f . }else if( o
1a02d 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 pcode==OP_VFilte
1a02e 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e r ){. int n
1a02f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1a030 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 p->nOp - i >= 3
1a031 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1a032 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d pOp[-1].opcode=
1a033 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 =OP_Integer );.
1a034 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d n = pOp[-1]
1a035 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e .p1;. if( n
1a036 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 >nMaxArgs ) nMax
1a037 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 Args = n;.#endif
1a038 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 . }.. if(
1a039 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 sqlite3VdbeOpcod
1a03a 65 48 61 73 50 72 6f 70 65 72 74 79 28 6f 70 63 eHasProperty(opc
1a03b 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 ode, OPFLG_JUMP)
1a03c 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b && pOp->p2<0 ){
1a03d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d . assert( -
1a03e 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 1-pOp->p2<p->nLa
1a03f 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 bel );. pOp
1a040 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 ->p2 = aLabel[-1
1a041 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d -pOp->p2];. }
1a042 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 . }. sqlite3Db
1a043 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 Free(p->db, p->a
1a044 4c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 Label);. p->aLa
1a045 62 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 bel = 0;.. *pMa
1a046 78 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 xFuncArgs = nMax
1a047 41 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 Args;.}../*.** R
1a048 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 eturn the addres
1a049 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e s of the next in
1a04a 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 struction to be
1a04b 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c inserted..*/.SQL
1a04c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1a04d 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
1a04e 6e 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b ntAddr(Vdbe *p){
1a04f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 . assert( p->ma
1a050 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
1a051 49 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e INIT );. return
1a052 20 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a p->nOp;.}../*.*
1a053 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1a054 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 returns a pointe
1a055 72 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 6f r to the array o
1a056 66 20 6f 70 63 6f 64 65 73 20 61 73 73 6f 63 69 f opcodes associ
1a057 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 ated with.** the
1a058 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 Vdbe passed as
1a059 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
1a05a 6e 74 2e 20 49 74 20 69 73 20 74 68 65 20 63 61 nt. It is the ca
1a05b 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 llers responsibi
1a05c 6c 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 61 6e lity.** to arran
1a05d 67 65 20 66 6f 72 20 74 68 65 20 72 65 74 75 72 ge for the retur
1a05e 6e 65 64 20 61 72 72 61 79 20 74 6f 20 62 65 20 ned array to be
1a05f 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 eventually freed
1a060 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 76 using the .** v
1a061 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 29 dbeFreeOpArray()
1a062 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
1a063 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e Before returnin
1a064 67 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 74 20 g, *pnOp is set
1a065 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
1a066 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 entries in the
1a067 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 72 61 returned.** arra
1a068 79 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 41 y. Also, *pnMaxA
1a069 72 67 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 rg is set to the
1a06a 20 6c 61 72 67 65 72 20 6f 66 20 69 74 73 20 63 larger of its c
1a06b 75 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 urrent value and
1a06c 20 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 .** the number
1a06d 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 of entries in th
1a06e 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 e Vdbe.apArg[] a
1a06f 72 72 61 79 20 72 65 71 75 69 72 65 64 20 74 6f rray required to
1a070 20 65 78 65 63 75 74 65 20 74 68 65 20 0a 2a 2a execute the .**
1a071 20 72 65 74 75 72 6e 65 64 20 70 72 6f 67 72 61 returned progra
1a072 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 m..*/.SQLITE_PRI
1a073 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c VATE VdbeOp *sql
1a074 69 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 ite3VdbeTakeOpAr
1a075 72 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ray(Vdbe *p, int
1a076 20 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d *pnOp, int *pnM
1a077 61 78 41 72 67 29 7b 0a 20 20 56 64 62 65 4f 70 axArg){. VdbeOp
1a078 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a *aOp = p->aOp;.
1a079 20 20 61 73 73 65 72 74 28 20 61 4f 70 20 26 26 assert( aOp &&
1a07a 20 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 !p->db->mallocF
1a07b 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43 ailed );.. /* C
1a07c 68 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74 65 heck that sqlite
1a07d 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 29 3VdbeUsesBtree()
1a07e 20 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 was not called
1a07f 6f 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 on this VM */.
1a080 61 73 73 65 72 74 28 20 70 2d 3e 61 4d 75 74 65 assert( p->aMute
1a081 78 2e 6e 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 0a x.nMutex==0 );..
1a082 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 resolveP2Value
1a083 73 28 70 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a s(p, pnMaxArg);.
1a084 20 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 *pnOp = p->nOp
1a085 3b 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a ;. p->aOp = 0;.
1a086 20 20 72 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a return aOp;.}.
1a087 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f ./*.** Add a who
1a088 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 le list of opera
1a089 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 tions to the ope
1a08a 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 ration stack. R
1a08b 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 eturn the.** add
1a08c 72 65 73 73 20 6f 66 20 74 68 65 20 66 69 72 73 ress of the firs
1a08d 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 64 64 65 t operation adde
1a08e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1a08f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1a090 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 VdbeAddOpList(Vd
1a091 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 be *p, int nOp,
1a092 56 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 VdbeOpList const
1a093 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 *aOp){. int ad
1a094 64 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d dr;. assert( p-
1a095 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1a096 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 IC_INIT );. if(
1a097 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 p->nOp + nOp >
1a098 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 p->nOpAlloc && g
1a099 72 6f 77 4f 70 41 72 72 61 79 28 70 29 20 29 7b rowOpArray(p) ){
1a09a 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
1a09b 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e }. addr = p->n
1a09c 4f 70 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 Op;. if( ALWAYS
1a09d 28 6e 4f 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 (nOp>0) ){. i
1a09e 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 nt i;. VdbeOp
1a09f 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 List const *pIn
1a0a0 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 = aOp;. for(i
1a0a1 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 =0; i<nOp; i++,
1a0a2 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e pIn++){. in
1a0a3 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a t p2 = pIn->p2;.
1a0a4 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f VdbeOp *pO
1a0a5 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 ut = &p->aOp[i+a
1a0a6 64 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 ddr];. pOut
1a0a7 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e ->opcode = pIn->
1a0a8 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f opcode;. pO
1a0a9 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 ut->p1 = pIn->p1
1a0aa 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 30 ;. if( p2<0
1a0ab 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4f && sqlite3VdbeO
1a0ac 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 pcodeHasProperty
1a0ad 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c 20 4f (pOut->opcode, O
1a0ae 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a 20 20 PFLG_JUMP) ){.
1a0af 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d pOut->p2 =
1a0b0 20 61 64 64 72 20 2b 20 41 44 44 52 28 70 32 29 addr + ADDR(p2)
1a0b1 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1a0b2 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 pOut->p2
1a0b3 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = p2;. }.
1a0b4 20 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d 20 70 pOut->p3 = p
1a0b5 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20 70 4f In->p3;. pO
1a0b6 75 74 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f ut->p4type = P4_
1a0b7 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 20 20 70 NOTUSED;. p
1a0b8 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 Out->p4.p = 0;.
1a0b9 20 20 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20 pOut->p5 =
1a0ba 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 0;.#ifdef SQLITE
1a0bb 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f 75 _DEBUG. pOu
1a0bc 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b t->zComment = 0;
1a0bd 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1a0be 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 e3VdbeAddopTrace
1a0bf 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1a0c0 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 te3VdbePrintOp(0
1a0c1 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f , i+addr, &p->aO
1a0c2 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20 p[i+addr]);.
1a0c3 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d }.#endif. }
1a0c4 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e . p->nOp += n
1a0c5 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e Op;. }. return
1a0c6 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 addr;.}../*.**
1a0c7 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 Change the value
1a0c8 20 6f 66 20 74 68 65 20 50 31 20 6f 70 65 72 61 of the P1 opera
1a0c9 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 nd for a specifi
1a0ca 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a c instruction..*
1a0cb 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1a0cc 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 s useful when a
1a0cd 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 large program is
1a0ce 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a loaded from a.*
1a0cf 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75 * static array u
1a0d0 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 sing sqlite3Vdbe
1a0d1 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 AddOpList but we
1a0d2 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a want to make a.
1a0d3 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 ** few minor cha
1a0d4 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 nges to the prog
1a0d5 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ram..*/.SQLITE_P
1a0d6 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a0d7 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 te3VdbeChangeP1(
1a0d8 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 Vdbe *p, int add
1a0d9 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 r, int val){. a
1a0da 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1a0db 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 assert( addr>=0
1a0dc 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 );. if( p->nOp
1a0dd 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e >addr ){. p->
1a0de 61 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 aOp[addr].p1 = v
1a0df 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a al;. }.}../*.**
1a0e0 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 Change the valu
1a0e1 65 20 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 e of the P2 oper
1a0e2 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 and for a specif
1a0e3 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ic instruction..
1a0e4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1a0e5 69 73 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 is useful for se
1a0e6 74 74 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 tting a jump des
1a0e7 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c tination..*/.SQL
1a0e8 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1a0e9 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1a0ea 67 65 50 32 28 56 64 62 65 20 2a 70 2c 20 69 6e geP2(Vdbe *p, in
1a0eb 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 t addr, int val)
1a0ec 7b 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 {. assert( p!=0
1a0ed 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64 );. assert( ad
1a0ee 64 72 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 dr>=0 );. if( p
1a0ef 2d 3e 6e 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 ->nOp>addr ){.
1a0f0 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 p->aOp[addr].p
1a0f1 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2 = val;. }.}..
1a0f2 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
1a0f3 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 value of the P3
1a0f4 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 operand for a s
1a0f5 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 pecific instruct
1a0f6 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
1a0f7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a0f8 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 te3VdbeChangeP3(
1a0f9 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 Vdbe *p, int add
1a0fa 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 r, int val){. a
1a0fb 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
1a0fc 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 assert( addr>=0
1a0fd 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 );. if( p->nOp
1a0fe 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e >addr ){. p->
1a0ff 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 76 aOp[addr].p3 = v
1a100 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a al;. }.}../*.**
1a101 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 Change the valu
1a102 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72 e of the P5 oper
1a103 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 and for the most
1a104 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 64 recently.** add
1a105 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f ed operation..*/
1a106 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a107 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1a108 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a 70 ChangeP5(Vdbe *p
1a109 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 73 , u8 val){. ass
1a10a 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 ert( p!=0 );. i
1a10b 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 f( p->aOp ){.
1a10c 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e assert( p->nOp>
1a10d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 0 );. p->aOp[
1a10e 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d 20 76 p->nOp-1].p5 = v
1a10f 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a al;. }.}../*.**
1a110 20 43 68 61 6e 67 65 20 74 68 65 20 50 32 20 6f Change the P2 o
1a111 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 74 72 75 perand of instru
1a112 63 74 69 6f 6e 20 61 64 64 72 20 73 6f 20 74 68 ction addr so th
1a113 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a at it points to.
1a114 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73 20 6f ** the address o
1a115 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 f the next instr
1a116 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 64 uction to be cod
1a117 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
1a118 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a119 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 56 e3VdbeJumpHere(V
1a11a 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 dbe *p, int addr
1a11b 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 ){. sqlite3Vdbe
1a11c 43 68 61 6e 67 65 50 32 28 70 2c 20 61 64 64 72 ChangeP2(p, addr
1a11d 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f , p->nOp);.}.../
1a11e 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 *.** If the inpu
1a11f 74 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 t FuncDef struct
1a120 75 72 65 20 69 73 20 65 70 68 65 6d 65 72 61 6c ure is ephemeral
1a121 2c 20 74 68 65 6e 20 66 72 65 65 20 69 74 2e 20 , then free it.
1a122 20 49 66 0a 2a 2a 20 74 68 65 20 46 75 6e 63 44 If.** the FuncD
1a123 65 66 20 69 73 20 6e 6f 74 20 65 70 68 65 72 6d ef is not epherm
1a124 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 68 al, then do noth
1a125 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ing..*/.static v
1a126 6f 69 64 20 66 72 65 65 45 70 68 65 6d 65 72 61 oid freeEphemera
1a127 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 lFunction(sqlite
1a128 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 66 20 2a 3 *db, FuncDef *
1a129 70 44 65 66 29 7b 0a 20 20 69 66 28 20 41 4c 57 pDef){. if( ALW
1a12a 41 59 53 28 70 44 65 66 29 20 26 26 20 28 70 44 AYS(pDef) && (pD
1a12b 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 ef->flags & SQLI
1a12c 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 29 21 3d TE_FUNC_EPHEM)!=
1a12d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1a12e 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 66 29 DbFree(db, pDef)
1a12f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
1a130 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 65 elete a P4 value
1a131 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a if necessary..*
1a132 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 /.static void fr
1a133 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64 62 eeP4(sqlite3 *db
1a134 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76 6f , int p4type, vo
1a135 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 70 id *p4){. if( p
1a136 34 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 4 ){. switch(
1a137 20 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 20 p4type ){.
1a138 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 0a 20 case P4_REAL:.
1a139 20 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 case P4_INT
1a13a 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 64:. case P
1a13b 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 20 20 20 4_MPRINTF:.
1a13c 20 63 61 73 65 20 50 34 5f 44 59 4e 41 4d 49 43 case P4_DYNAMIC
1a13d 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f :. case P4_
1a13e 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20 63 KEYINFO:. c
1a13f 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59 3a ase P4_INTARRAY:
1a140 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b . case P4_K
1a141 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 EYINFO_HANDOFF:
1a142 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1a143 33 44 62 46 72 65 65 28 64 62 2c 20 70 34 29 3b 3DbFree(db, p4);
1a144 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1a145 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
1a146 73 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 se P4_VDBEFUNC:
1a147 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65 46 75 {. VdbeFu
1a148 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 nc *pVdbeFunc =
1a149 28 56 64 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a (VdbeFunc *)p4;.
1a14a 20 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65 freeEphe
1a14b 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 meralFunction(db
1a14c 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 , pVdbeFunc->pFu
1a14d 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c nc);. sql
1a14e 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 ite3VdbeDeleteAu
1a14f 78 44 61 74 61 28 70 56 64 62 65 46 75 6e 63 2c xData(pVdbeFunc,
1a150 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 0);. sql
1a151 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1a152 56 64 62 65 46 75 6e 63 29 3b 0a 20 20 20 20 20 VdbeFunc);.
1a153 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1a154 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1a155 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 FUNCDEF: {.
1a156 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c freeEphemeral
1a157 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 46 75 Function(db, (Fu
1a158 6e 63 44 65 66 2a 29 70 34 29 3b 0a 20 20 20 20 ncDef*)p4);.
1a159 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1a15a 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 }. case P4
1a15b 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20 _MEM: {.
1a15c 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 sqlite3ValueFree
1a15d 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a ((sqlite3_value*
1a15e 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 )p4);. br
1a15f 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1a160 20 20 20 63 61 73 65 20 50 34 5f 56 54 41 42 20 case P4_VTAB
1a161 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 : {. sqli
1a162 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 28 56 te3VtabUnlock((V
1a163 54 61 62 6c 65 20 2a 29 70 34 29 3b 0a 20 20 20 Table *)p4);.
1a164 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a165 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 }. case P
1a166 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 3a 20 7b 4_SUBPROGRAM : {
1a167 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a168 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 VdbeProgramDelet
1a169 65 28 64 62 2c 20 28 53 75 62 50 72 6f 67 72 61 e(db, (SubProgra
1a16a 6d 20 2a 29 70 34 2c 20 31 29 3b 0a 20 20 20 20 m *)p4, 1);.
1a16b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1a16c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a }. }. }.}..
1a16d 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 73 /*.** Free the s
1a16e 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 pace allocated f
1a16f 6f 72 20 61 4f 70 20 61 6e 64 20 61 6e 79 20 70 or aOp and any p
1a170 34 20 76 61 6c 75 65 73 20 61 6c 6c 6f 63 61 74 4 values allocat
1a171 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 70 ed for the.** op
1a172 63 6f 64 65 73 20 63 6f 6e 74 61 69 6e 65 64 20 codes contained
1a173 77 69 74 68 69 6e 2e 20 49 66 20 61 4f 70 20 69 within. If aOp i
1a174 73 20 6e 6f 74 20 4e 55 4c 4c 20 69 74 20 69 73 s not NULL it is
1a175 20 61 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74 assumed to cont
1a176 61 69 6e 20 0a 2a 2a 20 6e 4f 70 20 65 6e 74 72 ain .** nOp entr
1a177 69 65 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 ies. .*/.static
1a178 76 6f 69 64 20 76 64 62 65 46 72 65 65 4f 70 41 void vdbeFreeOpA
1a179 72 72 61 79 28 73 71 6c 69 74 65 33 20 2a 64 62 rray(sqlite3 *db
1a17a 2c 20 4f 70 20 2a 61 4f 70 2c 20 69 6e 74 20 6e , Op *aOp, int n
1a17b 4f 70 29 7b 0a 20 20 69 66 28 20 61 4f 70 20 29 Op){. if( aOp )
1a17c 7b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 {. Op *pOp;.
1a17d 20 20 20 66 6f 72 28 70 4f 70 3d 61 4f 70 3b 20 for(pOp=aOp;
1a17e 70 4f 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70 pOp<&aOp[nOp]; p
1a17f 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 66 72 65 Op++){. fre
1a180 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 eP4(db, pOp->p4t
1a181 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b ype, pOp->p4.p);
1a182 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1a183 45 42 55 47 0a 20 20 20 20 20 20 73 71 6c 69 74 EBUG. sqlit
1a184 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 e3DbFree(db, pOp
1a185 2d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e ->zComment);.#en
1a186 64 69 66 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 dif . }.
1a187 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 }. sqlite3DbFr
1a188 65 65 28 64 62 2c 20 61 4f 70 29 3b 0a 7d 0a 0a ee(db, aOp);.}..
1a189 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 /*.** Decrement
1a18a 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f 6e the ref-count on
1a18b 20 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 20 the SubProgram
1a18c 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 structure passed
1a18d 20 61 73 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e as the.** secon
1a18e 64 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 d argument. If t
1a18f 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 he ref-count rea
1a190 63 68 65 73 20 7a 65 72 6f 2c 20 66 72 65 65 20 ches zero, free
1a191 74 68 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a the structure..*
1a192 2a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20 6f *.** The array o
1a193 66 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 73 f VDBE opcodes s
1a194 74 6f 72 65 64 20 61 73 20 53 75 62 50 72 6f 67 tored as SubProg
1a195 72 61 6d 2e 61 4f 70 20 69 73 20 66 72 65 65 64 ram.aOp is freed
1a196 20 69 66 0a 2a 2a 20 65 69 74 68 65 72 20 74 68 if.** either th
1a197 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 e ref-count reac
1a198 68 65 73 20 7a 65 72 6f 20 6f 72 20 70 61 72 61 hes zero or para
1a199 6d 65 74 65 72 20 66 72 65 65 6f 70 20 69 73 20 meter freeop is
1a19a 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 non-zero..**.**
1a19b 53 69 6e 63 65 20 74 68 65 20 61 72 72 61 79 20 Since the array
1a19c 6f 66 20 6f 70 63 6f 64 65 73 20 70 6f 69 6e 74 of opcodes point
1a19d 65 64 20 74 6f 20 62 79 20 53 75 62 50 72 6f 67 ed to by SubProg
1a19e 72 61 6d 2e 61 4f 70 20 6d 61 79 20 64 69 72 65 ram.aOp may dire
1a19f 63 74 6c 79 0a 2a 2a 20 6f 72 20 69 6e 64 69 72 ctly.** or indir
1a1a0 65 63 74 6c 79 20 63 6f 6e 74 61 69 6e 20 61 20 ectly contain a
1a1a1 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 reference to the
1a1a2 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 SubProgram stru
1a1a3 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a cture itself..**
1a1a4 20 42 79 20 70 61 73 73 69 6e 67 20 61 20 6e 6f By passing a no
1a1a5 6e 2d 7a 65 72 6f 20 66 72 65 65 6f 70 20 70 61 n-zero freeop pa
1a1a6 72 61 6d 65 74 65 72 2c 20 74 68 65 20 63 61 6c rameter, the cal
1a1a7 6c 65 72 20 6d 61 79 20 65 6e 73 75 72 65 20 74 ler may ensure t
1a1a8 68 61 74 20 61 6c 6c 0a 2a 2a 20 53 75 62 50 72 hat all.** SubPr
1a1a9 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 ogram structures
1a1aa 20 61 6e 64 20 74 68 65 69 72 20 61 4f 70 20 61 and their aOp a
1a1ab 72 72 61 79 73 20 61 72 65 20 66 72 65 65 64 2c rrays are freed,
1a1ac 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 72 65 even when there
1a1ad 0a 2a 2a 20 61 72 65 20 73 75 63 68 20 63 69 72 .** are such cir
1a1ae 63 75 6c 61 72 20 72 65 66 65 72 65 6e 63 65 73 cular references
1a1af 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a1b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a1b1 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 VdbeProgramDelet
1a1b2 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 e(sqlite3 *db, S
1a1b3 75 62 50 72 6f 67 72 61 6d 20 2a 70 2c 20 69 6e ubProgram *p, in
1a1b4 74 20 66 72 65 65 6f 70 29 7b 0a 20 20 69 66 28 t freeop){. if(
1a1b5 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 p ){. assert
1a1b6 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
1a1b7 20 20 20 69 66 28 20 66 72 65 65 6f 70 20 7c 7c if( freeop ||
1a1b8 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 p->nRef==1 ){.
1a1b9 20 20 20 20 20 4f 70 20 2a 61 4f 70 20 3d 20 70 Op *aOp = p
1a1ba 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 70 2d 3e ->aOp;. p->
1a1bb 61 4f 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 76 aOp = 0;. v
1a1bc 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 64 dbeFreeOpArray(d
1a1bd 62 2c 20 61 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b b, aOp, p->nOp);
1a1be 0a 20 20 20 20 20 20 70 2d 3e 6e 4f 70 20 3d 20 . p->nOp =
1a1bf 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 0;. }. p->
1a1c0 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 nRef--;. if(
1a1c1 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 p->nRef==0 ){.
1a1c2 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1a1c3 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a e(db, p);. }.
1a1c4 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 }.}.../*.** Ch
1a1c5 61 6e 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 ange N opcodes s
1a1c6 74 61 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 tarting at addr
1a1c7 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 to No-ops..*/.SQ
1a1c8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a1c9 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 d sqlite3VdbeCha
1a1ca 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a ngeToNoop(Vdbe *
1a1cb 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 p, int addr, int
1a1cc 20 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 61 4f N){. if( p->aO
1a1cd 70 20 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 p ){. VdbeOp
1a1ce 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 *pOp = &p->aOp[a
1a1cf 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 ddr];. sqlite
1a1d0 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 3 *db = p->db;.
1a1d1 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b while( N-- ){
1a1d2 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 . freeP4(db
1a1d3 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 , pOp->p4type, p
1a1d4 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 20 20 20 Op->p4.p);.
1a1d5 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30 2c 20 memset(pOp, 0,
1a1d6 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 29 3b sizeof(pOp[0]));
1a1d7 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f . pOp->opco
1a1d8 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 de = OP_Noop;.
1a1d9 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 20 20 7d pOp++;. }
1a1da 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 . }.}../*.** Ch
1a1db 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
1a1dc 66 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 f the P4 operand
1a1dd 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 for a specific
1a1de 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 instruction..**
1a1df 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
1a1e0 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 useful when a la
1a1e1 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c rge program is l
1a1e2 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 oaded from a.**
1a1e3 73 74 61 74 69 63 20 61 72 72 61 79 20 75 73 69 static array usi
1a1e4 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 ng sqlite3VdbeAd
1a1e5 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 dOpList but we w
1a1e6 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a ant to make a.**
1a1e7 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 few minor chang
1a1e8 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 es to the progra
1a1e9 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e 3d 30 m..**.** If n>=0
1a1ea 20 74 68 65 6e 20 74 68 65 20 50 34 20 6f 70 65 then the P4 ope
1a1eb 72 61 6e 64 20 69 73 20 64 79 6e 61 6d 69 63 2c rand is dynamic,
1a1ec 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 61 20 meaning that a
1a1ed 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 73 copy of.** the s
1a1ee 74 72 69 6e 67 20 69 73 20 6d 61 64 65 20 69 6e tring is made in
1a1ef 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
1a1f0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
1a1f1 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41 20 76 malloc()..** A v
1a1f2 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 6d 65 61 alue of n==0 mea
1a1f3 6e 73 20 63 6f 70 79 20 62 79 74 65 73 20 6f 66 ns copy bytes of
1a1f4 20 7a 50 34 20 75 70 20 74 6f 20 61 6e 64 20 69 zP4 up to and i
1a1f5 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a 2a 2a 20 ncluding the.**
1a1f6 66 69 72 73 74 20 6e 75 6c 6c 20 62 79 74 65 2e first null byte.
1a1f7 20 20 49 66 20 6e 3e 30 20 74 68 65 6e 20 63 6f If n>0 then co
1a1f8 70 79 20 6e 2b 31 20 62 79 74 65 73 20 6f 66 20 py n+1 bytes of
1a1f9 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3d zP4..**.** If n=
1a1fa 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69 74 20 6d =P4_KEYINFO it m
1a1fb 65 61 6e 73 20 74 68 61 74 20 7a 50 34 20 69 73 eans that zP4 is
1a1fc 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1a1fd 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
1a1fe 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 69 73 20 e..** A copy is
1a1ff 6d 61 64 65 20 6f 66 20 74 68 65 20 4b 65 79 49 made of the KeyI
1a200 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 6e nfo structure in
1a201 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
1a202 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 ed from.** sqlit
1a203 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20 62 65 e3_malloc, to be
1a204 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 freed when the
1a205 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 Vdbe is finalize
1a206 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49 d..** n==P4_KEYI
1a207 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 6e 64 69 NFO_HANDOFF indi
1a208 63 61 74 65 73 20 74 68 61 74 20 7a 50 34 20 70 cates that zP4 p
1a209 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e oints to a KeyIn
1a20a 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 fo structure.**
1a20b 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 stored in memory
1a20c 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 that the caller
1a20d 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 has obtained fr
1a20e 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
1a20f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 c. The .** calle
1a210 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66 72 65 r should not fre
1a211 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e e the allocation
1a212 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 , it will be fre
1a213 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 ed when the Vdbe
1a214 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 is.** finalized
1a215 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72 20 76 ..** .** Other v
1a216 61 6c 75 65 73 20 6f 66 20 6e 20 28 50 34 5f 53 alues of n (P4_S
1a217 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4c 53 45 TATIC, P4_COLLSE
1a218 51 20 65 74 63 2e 29 20 69 6e 64 69 63 61 74 65 Q etc.) indicate
1a219 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 that zP4 points
1a21a 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69 6e 67 20 .** to a string
1a21b 6f 72 20 73 74 72 75 63 74 75 72 65 20 74 68 61 or structure tha
1a21c 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 t is guaranteed
1a21d 74 6f 20 65 78 69 73 74 20 66 6f 72 20 74 68 65 to exist for the
1a21e 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 lifetime of.**
1a21f 74 68 65 20 56 64 62 65 2e 20 49 6e 20 74 68 65 the Vdbe. In the
1a220 73 65 20 63 61 73 65 73 20 77 65 20 63 61 6e 20 se cases we can
1a221 6a 75 73 74 20 63 6f 70 79 20 74 68 65 20 70 6f just copy the po
1a222 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 inter..**.** If
1a223 61 64 64 72 3c 30 20 74 68 65 6e 20 63 68 61 6e addr<0 then chan
1a224 67 65 20 50 34 20 6f 6e 20 74 68 65 20 6d 6f 73 ge P4 on the mos
1a225 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 t recently inser
1a226 74 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ted instruction.
1a227 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a228 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a229 64 62 65 43 68 61 6e 67 65 50 34 28 56 64 62 65 dbeChangeP4(Vdbe
1a22a 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 *p, int addr, c
1a22b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 onst char *zP4,
1a22c 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 2a 70 4f int n){. Op *pO
1a22d 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 p;. sqlite3 *db
1a22e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 ;. assert( p!=0
1a22f 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 );. db = p->db
1a230 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d ;. assert( p->m
1a231 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
1a232 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 _INIT );. if( p
1a233 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e ->aOp==0 || db->
1a234 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1a235 20 20 20 20 69 66 20 28 20 6e 21 3d 50 34 5f 4b if ( n!=P4_K
1a236 45 59 49 4e 46 4f 20 26 26 20 6e 21 3d 50 34 5f EYINFO && n!=P4_
1a237 56 54 41 42 20 29 20 7b 0a 20 20 20 20 20 20 66 VTAB ) {. f
1a238 72 65 65 50 34 28 64 62 2c 20 6e 2c 20 28 76 6f reeP4(db, n, (vo
1a239 69 64 2a 29 2a 28 63 68 61 72 2a 2a 29 26 7a 50 id*)*(char**)&zP
1a23a 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 4);. }. re
1a23b 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 turn;. }. asse
1a23c 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a rt( p->nOp>0 );.
1a23d 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 assert( addr<p
1a23e 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 ->nOp );. if( a
1a23f 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 ddr<0 ){. add
1a240 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a r = p->nOp - 1;.
1a241 20 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e }. pOp = &p->
1a242 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65 aOp[addr];. fre
1a243 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 eP4(db, pOp->p4t
1a244 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b ype, pOp->p4.p);
1a245 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 . pOp->p4.p = 0
1a246 3b 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e ;. if( n==P4_IN
1a247 54 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f T32 ){. /* No
1a248 74 65 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 te: this cast is
1a249 20 73 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 safe, because t
1a24a 68 65 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 he origin data p
1a24b 6f 69 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a oint was an int.
1a24c 20 20 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 ** that was
1a24d 63 61 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 cast to a (const
1a24e 20 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 char *). */.
1a24f 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c pOp->p4.i = SQL
1a250 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a ITE_PTR_TO_INT(z
1a251 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 P4);. pOp->p4
1a252 74 79 70 65 20 3d 20 50 34 5f 49 4e 54 33 32 3b type = P4_INT32;
1a253 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 50 34 . }else if( zP4
1a254 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e ==0 ){. pOp->
1a255 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 70 4f p4.p = 0;. pO
1a256 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e p->p4type = P4_N
1a257 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73 65 20 OTUSED;. }else
1a258 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 if( n==P4_KEYINF
1a259 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f O ){. KeyInfo
1a25a 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 *pKeyInfo;.
1a25b 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42 79 74 int nField, nByt
1a25c 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d e;.. nField =
1a25d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 ((KeyInfo*)zP4)
1a25e 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 6e 42 ->nField;. nB
1a25f 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 4b yte = sizeof(*pK
1a260 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69 65 6c eyInfo) + (nFiel
1a261 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b 65 79 d-1)*sizeof(pKey
1a262 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 Info->aColl[0])
1a263 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 4b + nField;. pK
1a264 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 eyInfo = sqlite3
1a265 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b Malloc( nByte );
1a266 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 . pOp->p4.pKe
1a267 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f yInfo = pKeyInfo
1a268 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e ;. if( pKeyIn
1a269 66 6f 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a fo ){. u8 *
1a26a 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 aSortOrder;.
1a26b 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 memcpy(pKeyInf
1a26c 6f 2c 20 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a o, zP4, nByte);.
1a26d 20 20 20 20 20 20 61 53 6f 72 74 4f 72 64 65 72 aSortOrder
1a26e 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f = pKeyInfo->aSo
1a26f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 69 rtOrder;. i
1a270 66 28 20 61 53 6f 72 74 4f 72 64 65 72 20 29 7b f( aSortOrder ){
1a271 0a 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66 . pKeyInf
1a272 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 o->aSortOrder =
1a273 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 (unsigned char*)
1a274 26 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c &pKeyInfo->aColl
1a275 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 [nField];.
1a276 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 memcpy(pKeyInf
1a277 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 61 o->aSortOrder, a
1a278 53 6f 72 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c SortOrder, nFiel
1a279 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 d);. }.
1a27a 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
1a27b 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20 P4_KEYINFO;.
1a27c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e }else{. p->
1a27d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1a27e 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 2d = 1;. pOp-
1a27f 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 >p4type = P4_NOT
1a280 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 USED;. }. }e
1a281 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 lse if( n==P4_KE
1a282 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b YINFO_HANDOFF ){
1a283 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d . pOp->p4.p =
1a284 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 (void*)zP4;.
1a285 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
1a286 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 4_KEYINFO;. }el
1a287 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 56 54 41 se if( n==P4_VTA
1a288 42 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 B ){. pOp->p4
1a289 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b .p = (void*)zP4;
1a28a 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 . pOp->p4type
1a28b 20 3d 20 50 34 5f 56 54 41 42 3b 0a 20 20 20 20 = P4_VTAB;.
1a28c 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 sqlite3VtabLock(
1a28d 28 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 3b 0a (VTable *)zP4);.
1a28e 20 20 20 20 61 73 73 65 72 74 28 20 28 28 56 54 assert( ((VT
1a28f 61 62 6c 65 20 2a 29 7a 50 34 29 2d 3e 64 62 3d able *)zP4)->db=
1a290 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 7d 65 6c 73 =p->db );. }els
1a291 65 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 e if( n<0 ){.
1a292 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f pOp->p4.p = (vo
1a293 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 id*)zP4;. pOp
1a294 2d 3e 70 34 74 79 70 65 20 3d 20 28 73 69 67 6e ->p4type = (sign
1a295 65 64 20 63 68 61 72 29 6e 3b 0a 20 20 7d 65 6c ed char)n;. }el
1a296 73 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 se{. if( n==0
1a297 20 29 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 ) n = sqlite3St
1a298 72 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 20 20 20 rlen30(zP4);.
1a299 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c pOp->p4.z = sql
1a29a 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d ite3DbStrNDup(p-
1a29b 3e 64 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 >db, zP4, n);.
1a29c 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
1a29d 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a P4_DYNAMIC;. }.
1a29e 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 }..#ifndef NDEBU
1a29f 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 G./*.** Change t
1a2a0 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 he comment on th
1a2a1 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e e the most recen
1a2a2 74 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 tly coded instru
1a2a3 63 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e ction. Or.** in
1a2a4 73 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 sert a No-op and
1a2a5 20 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 add the comment
1a2a6 20 74 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 to that new ins
1a2a7 74 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a truction. This.
1a2a8 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 ** makes the cod
1a2a9 65 20 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 e easier to read
1a2aa 20 64 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e during debuggin
1a2ab 67 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 g. None of this
1a2ac 20 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 happens.** in a
1a2ad 20 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c production buil
1a2ae 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1a2af 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a2b0 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 3VdbeComment(Vdb
1a2b1 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 e *p, const char
1a2b2 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b *zFormat, ...){
1a2b3 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 . va_list ap;.
1a2b4 20 69 66 28 20 21 70 20 29 20 72 65 74 75 72 6e if( !p ) return
1a2b5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
1a2b6 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d Op>0 || p->aOp==
1a2b7 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1a2b8 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 ->aOp==0 || p->a
1a2b9 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f Op[p->nOp-1].zCo
1a2ba 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 mment==0 || p->d
1a2bb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a2bc 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 );. if( p->nOp
1a2bd 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a ){. char **pz
1a2be 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f = &p->aOp[p->nO
1a2bf 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 p-1].zComment;.
1a2c0 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 va_start(ap,
1a2c1 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 zFormat);. sq
1a2c2 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 lite3DbFree(p->d
1a2c3 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a b, *pz);. *pz
1a2c4 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e = sqlite3VMPrin
1a2c5 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 tf(p->db, zForma
1a2c6 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 t, ap);. va_e
1a2c7 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 nd(ap);. }.}.SQ
1a2c8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a2c9 64 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f d sqlite3VdbeNoo
1a2ca 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 pComment(Vdbe *p
1a2cb 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 , const char *zF
1a2cc 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 ormat, ...){. v
1a2cd 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 a_list ap;. if(
1a2ce 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 !p ) return;.
1a2cf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
1a2d0 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 0(p, OP_Noop);.
1a2d1 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e assert( p->nOp>
1a2d2 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 0 || p->aOp==0 )
1a2d3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 ;. assert( p->a
1a2d4 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b Op==0 || p->aOp[
1a2d5 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 p->nOp-1].zComme
1a2d6 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e nt==0 || p->db->
1a2d7 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1a2d8 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a if( p->nOp ){.
1a2d9 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 char **pz =
1a2da 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 &p->aOp[p->nOp-1
1a2db 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 ].zComment;.
1a2dc 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f va_start(ap, zFo
1a2dd 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 rmat);. sqlit
1a2de 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 e3DbFree(p->db,
1a2df 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 *pz);. *pz =
1a2e0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
1a2e1 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 p->db, zFormat,
1a2e2 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 ap);. va_end(
1a2e3 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 ap);. }.}.#endi
1a2e4 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a f /* NDEBUG */.
1a2e5 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1a2e6 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 20 67 e opcode for a g
1a2e7 69 76 65 6e 20 61 64 64 72 65 73 73 2e 20 20 49 iven address. I
1a2e8 66 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73 f the address is
1a2e9 20 2d 31 2c 20 74 68 65 6e 0a 2a 2a 20 72 65 74 -1, then.** ret
1a2ea 75 72 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 urn the most rec
1a2eb 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f ently inserted o
1a2ec 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 pcode..**.** If
1a2ed 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
1a2ee 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 ion error has oc
1a2ef 63 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 curred prior to
1a2f0 74 68 65 20 63 61 6c 6c 69 6e 67 20 6f 66 20 74 the calling of t
1a2f1 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 his.** routine,
1a2f2 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 then a pointer t
1a2f3 6f 20 61 20 64 75 6d 6d 79 20 56 64 62 65 4f 70 o a dummy VdbeOp
1a2f4 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 will be returne
1a2f5 64 2e 20 20 54 68 61 74 20 6f 70 63 6f 64 65 0a d. That opcode.
1a2f6 2a 2a 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 ** is readable a
1a2f7 6e 64 20 77 72 69 74 61 62 6c 65 2c 20 62 75 74 nd writable, but
1a2f8 20 69 74 20 68 61 73 20 6e 6f 20 65 66 66 65 63 it has no effec
1a2f9 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 6f t. The return o
1a2fa 66 20 61 20 64 75 6d 6d 79 0a 2a 2a 20 6f 70 63 f a dummy.** opc
1a2fb 6f 64 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 ode allows the c
1a2fc 61 6c 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 all to continue
1a2fd 66 75 6e 63 74 69 6f 6e 69 6e 67 20 61 66 74 65 functioning afte
1a2fe 72 20 61 20 4f 4f 4d 20 66 61 75 6c 74 20 77 69 r a OOM fault wi
1a2ff 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 thout.** having
1a300 74 6f 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 to check to see
1a301 69 66 20 74 68 65 20 72 65 74 75 72 6e 20 66 72 if the return fr
1a302 6f 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 om this routine
1a303 69 73 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 is a valid point
1a304 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 er..**.** About
1a305 74 68 65 20 23 69 66 64 65 66 20 53 51 4c 49 54 the #ifdef SQLIT
1a306 45 5f 4f 4d 49 54 5f 54 52 41 43 45 3a 20 20 4e E_OMIT_TRACE: N
1a307 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 72 6f ormally, this ro
1a308 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 utine is never c
1a309 61 6c 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 alled.** unless
1a30a 70 2d 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 73 20 p->nOp>0. This
1a30b 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 74 68 is because in th
1a30c 65 20 61 62 73 65 6e 73 65 20 6f 66 20 53 51 4c e absense of SQL
1a30d 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 2c 0a ITE_OMIT_TRACE,.
1a30e 2a 2a 20 61 6e 20 4f 50 5f 54 72 61 63 65 20 69 ** an OP_Trace i
1a30f 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c nstruction is al
1a310 77 61 79 73 20 69 6e 73 65 72 74 65 64 20 62 79 ways inserted by
1a311 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 28 sqlite3VdbeGet(
1a312 29 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 ) as soon as.**
1a313 61 20 6e 65 77 20 56 44 42 45 20 69 73 20 63 72 a new VDBE is cr
1a314 65 61 74 65 64 2e 20 20 53 6f 20 77 65 20 61 72 eated. So we ar
1a315 65 20 66 72 65 65 20 74 6f 20 73 65 74 20 61 64 e free to set ad
1a316 64 72 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 dr to p->nOp-1 w
1a317 69 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 ithout.** having
1a318 20 74 6f 20 64 6f 75 62 6c 65 2d 63 68 65 63 6b to double-check
1a319 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
1a31a 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 at the result is
1a31b 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 42 non-negative. B
1a31c 75 74 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f ut.** if SQLITE_
1a31d 4f 4d 49 54 5f 54 52 41 43 45 20 69 73 20 64 65 OMIT_TRACE is de
1a31e 66 69 6e 65 64 2c 20 74 68 65 20 4f 50 5f 54 72 fined, the OP_Tr
1a31f 61 63 65 20 69 73 20 6f 6d 69 74 74 65 64 20 61 ace is omitted a
1a320 6e 64 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f nd we do need to
1a321 0a 2a 2a 20 63 68 65 63 6b 20 74 68 65 20 76 61 .** check the va
1a322 6c 75 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 lue of p->nOp-1
1a323 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e before continuin
1a324 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 g..*/.SQLITE_PRI
1a325 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c VATE VdbeOp *sql
1a326 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 ite3VdbeGetOp(Vd
1a327 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 be *p, int addr)
1a328 7b 0a 20 20 73 74 61 74 69 63 20 56 64 62 65 4f {. static VdbeO
1a329 70 20 64 75 6d 6d 79 3b 0a 20 20 61 73 73 65 72 p dummy;. asser
1a32a 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 t( p->magic==VDB
1a32b 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a E_MAGIC_INIT );.
1a32c 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a if( addr<0 ){.
1a32d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
1a32e 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 IT_TRACE. if(
1a32f 20 70 2d 3e 6e 4f 70 3d 3d 30 20 29 20 72 65 74 p->nOp==0 ) ret
1a330 75 72 6e 20 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 urn &dummy;.#end
1a331 69 66 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d if. addr = p-
1a332 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 >nOp - 1;. }.
1a333 61 73 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 assert( (addr>=0
1a334 20 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 && addr<p->nOp)
1a335 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f || p->db->mallo
1a336 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 cFailed );. if(
1a337 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 p->db->mallocFa
1a338 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 iled ){. retu
1a339 72 6e 20 26 64 75 6d 6d 79 3b 0a 20 20 7d 65 6c rn &dummy;. }el
1a33a 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 se{. return &
1a33b 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 p->aOp[addr];.
1a33c 7d 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 }.}..#if !define
1a33d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 d(SQLITE_OMIT_EX
1a33e 50 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e PLAIN) || !defin
1a33f 65 64 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 ed(NDEBUG) \.
1a340 20 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 || defined(VDB
1a341 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 E_PROFILE) || de
1a342 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
1a343 55 47 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 UG)./*.** Comput
1a344 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 e a string that
1a345 64 65 73 63 72 69 62 65 73 20 74 68 65 20 50 34 describes the P4
1a346 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 parameter for a
1a347 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 n opcode..** Use
1a348 20 7a 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 zTemp for any r
1a349 65 71 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 equired temporar
1a34a 79 20 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a y buffer space..
1a34b 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a */.static char *
1a34c 64 69 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f displayP4(Op *pO
1a34d 70 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 p, char *zTemp,
1a34e 69 6e 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 int nTemp){. ch
1a34f 61 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b ar *zP4 = zTemp;
1a350 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 . assert( nTemp
1a351 3e 3d 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 >=20 );. switch
1a352 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b ( pOp->p4type ){
1a353 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 . case P4_KEY
1a354 49 4e 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 INFO_STATIC:.
1a355 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f case P4_KEYINFO
1a356 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c : {. int i,
1a357 20 6a 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 j;. KeyInf
1a358 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f o *pKeyInfo = pO
1a359 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a p->p4.pKeyInfo;.
1a35a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a35b 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1a35c 65 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 emp, "keyinfo(%d
1a35d 22 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 ", pKeyInfo->nFi
1a35e 65 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 eld);. i =
1a35f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1a360 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f zTemp);. fo
1a361 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 r(j=0; j<pKeyInf
1a362 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b o->nField; j++){
1a363 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 . CollSeq
1a364 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e *pColl = pKeyIn
1a365 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 fo->aColl[j];.
1a366 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 if( pColl
1a367 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
1a368 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
1a369 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d en30(pColl->zNam
1a36a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 e);. if
1a36b 28 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b ( i+n>nTemp-6 ){
1a36c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d . mem
1a36d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c cpy(&zTemp[i],",
1a36e 2e 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 ...",4);.
1a36f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a370 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1a371 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 zTemp[i++] = '
1a372 2c 27 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ,';. if
1a373 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 ( pKeyInfo->aSor
1a374 74 4f 72 64 65 72 20 26 26 20 70 4b 65 79 49 6e tOrder && pKeyIn
1a375 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a fo->aSortOrder[j
1a376 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ] ){.
1a377 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d zTemp[i++] = '-
1a378 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 ';. }.
1a379 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
1a37a 26 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c &zTemp[i], pColl
1a37b 2d 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 ->zName,n+1);.
1a37c 20 20 20 20 20 20 20 20 69 20 2b 3d 20 6e 3b 0a i += n;.
1a37d 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
1a37e 28 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 29 7b ( i+4<nTemp-6 ){
1a37f 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 . memcp
1a380 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 y(&zTemp[i],",ni
1a381 6c 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 l",4);.
1a382 20 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 i += 4;.
1a383 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1a384 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 zTemp[i++] = ')
1a385 27 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 ';. zTemp[i
1a386 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 ] = 0;. ass
1a387 65 72 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0a ert( i<nTemp );.
1a388 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1a389 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 43 }. case P4_C
1a38a 4f 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20 20 20 OLLSEQ: {.
1a38b 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d CollSeq *pColl =
1a38c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a pOp->p4.pColl;.
1a38d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a38e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1a38f 65 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e emp, "collseq(%.
1a390 32 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 20s)", pColl->zN
1a391 61 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 ame);. brea
1a392 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1a393 65 20 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a e P4_FUNCDEF: {.
1a394 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 FuncDef *p
1a395 44 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 Def = pOp->p4.pF
1a396 75 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 unc;. sqlit
1a397 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d e3_snprintf(nTem
1a398 70 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28 25 64 p, zTemp, "%s(%d
1a399 29 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c )", pDef->zName,
1a39a 20 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 pDef->nArg);.
1a39b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1a39c 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 . case P4_INT
1a39d 36 34 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 64: {. sqli
1a39e 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 te3_snprintf(nTe
1a39f 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 mp, zTemp, "%lld
1a3a0 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 ", *pOp->p4.pI64
1a3a1 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1a3a2 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 }. case P
1a3a3 34 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20 20 20 4_INT32: {.
1a3a4 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a3a5 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a3a6 22 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 "%d", pOp->p4.i)
1a3a7 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1a3a8 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 }. case P4
1a3a9 5f 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20 20 73 _REAL: {. s
1a3aa 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a3ab 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 nTemp, zTemp, "%
1a3ac 2e 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e .16g", *pOp->p4.
1a3ad 70 52 65 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 pReal);. br
1a3ae 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
1a3af 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 ase P4_MEM: {.
1a3b0 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 Mem *pMem =
1a3b1 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 pOp->p4.pMem;.
1a3b2 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 assert( (pMe
1a3b3 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e m->flags & MEM_N
1a3b4 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ull)==0 );.
1a3b5 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
1a3b6 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 & MEM_Str ){.
1a3b7 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d zP4 = pMem
1a3b8 2d 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ->z;. }else
1a3b9 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
1a3ba 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 & MEM_Int ){.
1a3bb 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1a3bc 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 printf(nTemp, zT
1a3bd 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 emp, "%lld", pMe
1a3be 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7d m->u.i);. }
1a3bf 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 else if( pMem->f
1a3c0 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
1a3c1 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1a3c2 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d e3_snprintf(nTem
1a3c3 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 p, zTemp, "%.16g
1a3c4 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 ", pMem->r);.
1a3c5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1a3c6 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e assert( pMem->
1a3c7 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 flags & MEM_Blob
1a3c8 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 );. zP4
1a3c9 3d 20 22 28 62 6c 6f 62 29 22 3b 0a 20 20 20 20 = "(blob)";.
1a3ca 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
1a3cb 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
1a3cc 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1a3cd 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 61 73 65 ALTABLE. case
1a3ce 20 50 34 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20 P4_VTAB: {.
1a3cf 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1a3d0 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e pVtab = pOp->p4.
1a3d1 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 pVtab->pVtab;.
1a3d2 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a3d3 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
1a3d4 70 2c 20 22 76 74 61 62 3a 25 70 3a 25 70 22 2c p, "vtab:%p:%p",
1a3d5 20 70 56 74 61 62 2c 20 70 56 74 61 62 2d 3e 70 pVtab, pVtab->p
1a3d6 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 20 20 62 Module);. b
1a3d7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 reak;. }.#end
1a3d8 69 66 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 if. case P4_I
1a3d9 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20 20 20 20 NTARRAY: {.
1a3da 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a3db 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 f(nTemp, zTemp,
1a3dc 22 69 6e 74 61 72 72 61 79 22 29 3b 0a 20 20 20 "intarray");.
1a3dd 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1a3de 20 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 50 case P4_SUBP
1a3df 52 4f 47 52 41 4d 3a 20 7b 0a 20 20 20 20 20 20 ROGRAM: {.
1a3e0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1a3e1 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 (nTemp, zTemp, "
1a3e2 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 program");.
1a3e3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1a3e4 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
1a3e5 20 20 20 7a 50 34 20 3d 20 70 4f 70 2d 3e 70 34 zP4 = pOp->p4
1a3e6 2e 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 .z;. if( zP
1a3e7 34 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 4==0 ){.
1a3e8 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 zP4 = zTemp;.
1a3e9 20 20 20 20 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 zTemp[0] =
1a3ea 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0;. }. }
1a3eb 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a . }. assert( z
1a3ec 50 34 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 P4!=0 );. retur
1a3ed 6e 20 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a n zP4;.}.#endif.
1a3ee 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 ./*.** Declare t
1a3ef 6f 20 74 68 65 20 56 64 62 65 20 74 68 61 74 20 o the Vdbe that
1a3f0 74 68 65 20 42 54 72 65 65 20 6f 62 6a 65 63 74 the BTree object
1a3f1 20 61 74 20 64 62 2d 3e 61 44 62 5b 69 5d 20 69 at db->aDb[i] i
1a3f2 73 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 s used..*/.SQLIT
1a3f3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a3f4 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 qlite3VdbeUsesBt
1a3f5 72 65 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 ree(Vdbe *p, int
1a3f6 20 69 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b i){. int mask;
1a3f7 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 . assert( i>=0
1a3f8 26 26 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 && i<p->db->nDb
1a3f9 26 26 20 69 3c 73 69 7a 65 6f 66 28 75 33 32 29 && i<sizeof(u32)
1a3fa 2a 38 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 *8 );. assert(
1a3fb 69 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d i<(int)sizeof(p-
1a3fc 3e 62 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b >btreeMask)*8 );
1a3fd 0a 20 20 6d 61 73 6b 20 3d 20 28 28 75 33 32 29 . mask = ((u32)
1a3fe 31 29 3c 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 1)<<i;. if( (p-
1a3ff 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 >btreeMask & mas
1a400 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e k)==0 ){. p->
1a401 62 74 72 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 btreeMask |= mas
1a402 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 k;. sqlite3Bt
1a403 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 reeMutexArrayIns
1a404 65 72 74 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 ert(&p->aMutex,
1a405 70 2d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 p->db->aDb[i].pB
1a406 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 t);. }.}...#if
1a407 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f defined(VDBE_PRO
1a408 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 FILE) || defined
1a409 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f (SQLITE_DEBUG)./
1a40a 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e *.** Print a sin
1a40b 67 6c 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 gle opcode. Thi
1a40c 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
1a40d 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 d for debugging
1a40e 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f only..*/.SQLITE_
1a40f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a410 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 ite3VdbePrintOp(
1a411 46 49 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 FILE *pOut, int
1a412 70 63 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 pc, Op *pOp){.
1a413 63 68 61 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61 char *zP4;. cha
1a414 72 20 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 r zPtr[50];. st
1a415 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
1a416 2a 7a 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 *zFormat1 = "%4d
1a417 20 25 2d 31 33 73 20 25 34 64 20 25 34 64 20 25 %-13s %4d %4d %
1a418 34 64 20 25 2d 34 73 20 25 2e 32 58 20 25 73 5c 4d %-4s %.2X %s\
1a419 6e 22 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d n";. if( pOut==
1a41a 30 20 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 0 ) pOut = stdou
1a41b 74 3b 0a 20 20 7a 50 34 20 3d 20 64 69 73 70 6c t;. zP4 = displ
1a41c 61 79 50 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 ayP4(pOp, zPtr,
1a41d 73 69 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 sizeof(zPtr));.
1a41e 20 66 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a fprintf(pOut, z
1a41f 46 6f 72 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20 Format1, pc, .
1a420 20 20 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 sqlite3Opcod
1a421 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 eName(pOp->opcod
1a422 65 29 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 e), pOp->p1, pOp
1a423 2d 3e 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a ->p2, pOp->p3, z
1a424 50 34 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 P4, pOp->p5,.#if
1a425 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1a426 0a 20 20 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d . pOp->zCom
1a427 6d 65 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d ment ? pOp->zCom
1a428 6d 65 6e 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a ment : "".#else.
1a429 20 20 20 20 20 20 22 22 0a 23 65 6e 64 69 66 0a "".#endif.
1a42a 20 20 29 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f );. fflush(pO
1a42b 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ut);.}.#endif../
1a42c 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 20 *.** Release an
1a42d 61 72 72 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65 array of N Mem e
1a42e 6c 65 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 lements.*/.stati
1a42f 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4d 65 c void releaseMe
1a430 6d 41 72 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69 mArray(Mem *p, i
1a431 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 nt N){. if( p &
1a432 26 20 4e 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a & N ){. Mem *
1a433 70 45 6e 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 pEnd;. sqlite
1a434 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 3 *db = p->db;.
1a435 20 20 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 u8 malloc_fai
1a436 6c 65 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 led = db->malloc
1a437 46 61 69 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 Failed;. for(
1a438 70 45 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 pEnd=&p[N]; p<pE
1a439 6e 64 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 nd; p++){.
1a43a 61 73 73 65 72 74 28 20 28 26 70 5b 31 5d 29 3d assert( (&p[1])=
1a43b 3d 70 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 =pEnd || p[0].db
1a43c 3d 3d 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 ==p[1].db );..
1a43d 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 /* This bloc
1a43e 6b 20 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 k is really an i
1a43f 6e 6c 69 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f nlined version o
1a440 66 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d f sqlite3VdbeMem
1a441 52 65 6c 65 61 73 65 28 29 0a 20 20 20 20 20 20 Release().
1a442 2a 2a 20 74 68 61 74 20 74 61 6b 65 73 20 61 64 ** that takes ad
1a443 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66 vantage of the f
1a444 61 63 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d act that the mem
1a445 6f 72 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 ory cell value i
1a446 73 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e s . ** bein
1a447 67 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 g set to NULL af
1a448 74 65 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e ter releasing an
1a449 79 20 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 y dynamic resour
1a44a 63 65 73 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 ces.. **.
1a44b 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 ** The justi
1a44c 66 69 63 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 fication for dup
1a44d 6c 69 63 61 74 69 6e 67 20 63 6f 64 65 20 69 73 licating code is
1a44e 20 74 68 61 74 20 61 63 63 6f 72 64 69 6e 67 20 that according
1a44f 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c to . ** cal
1a450 6c 67 72 69 6e 64 2c 20 74 68 69 73 20 63 61 75 lgrind, this cau
1a451 73 65 73 20 61 20 63 65 72 74 61 69 6e 20 74 65 ses a certain te
1a452 73 74 20 63 61 73 65 20 74 6f 20 68 69 74 20 74 st case to hit t
1a453 68 65 20 43 50 55 20 34 2e 37 20 0a 20 20 20 20 he CPU 4.7 .
1a454 20 20 2a 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 ** percent les
1a455 73 20 28 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 s (x86 linux, gc
1a456 63 20 76 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c c version 4.1.2,
1a457 20 2d 4f 36 29 20 74 68 61 6e 20 69 66 20 0a 20 -O6) than if .
1a458 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d ** sqlite3M
1a459 65 6d 52 65 6c 65 61 73 65 28 29 20 77 65 72 65 emRelease() were
1a45a 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 called from her
1a45b 65 2e 20 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 e. With -O2, thi
1a45c 73 20 6a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a s jumps. **
1a45d 20 74 6f 20 36 2e 36 20 70 65 72 63 65 6e 74 2e to 6.6 percent.
1a45e 20 54 68 65 20 74 65 73 74 20 63 61 73 65 20 69 The test case i
1a45f 73 20 69 6e 73 65 72 74 69 6e 67 20 31 30 30 30 s inserting 1000
1a460 20 72 6f 77 73 20 69 6e 74 6f 20 61 20 74 61 62 rows into a tab
1a461 6c 65 20 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 le . ** wit
1a462 68 20 6e 6f 20 69 6e 64 65 78 65 73 20 75 73 69 h no indexes usi
1a463 6e 67 20 61 20 73 69 6e 67 6c 65 20 70 72 65 70 ng a single prep
1a464 61 72 65 64 20 49 4e 53 45 52 54 20 73 74 61 74 ared INSERT stat
1a465 65 6d 65 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 ement, bind() .
1a466 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 ** and rese
1a467 74 28 29 2e 20 49 6e 73 65 72 74 73 20 61 72 65 t(). Inserts are
1a468 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 grouped into a
1a469 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 transaction..
1a46a 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
1a46b 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 p->flags&(MEM_Ag
1a46c 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 g|MEM_Dyn|MEM_Fr
1a46d 61 6d 65 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20 ame|MEM_RowSet)
1a46e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1a46f 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1a470 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 (p);. }else
1a471 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 if( p->zMalloc
1a472 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1a473 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1a474 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 zMalloc);.
1a475 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 p->zMalloc = 0
1a476 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
1a477 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f p->flags = MEM_
1a478 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Null;. }.
1a479 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1a47a 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 = malloc_failed
1a47b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
1a47c 65 6c 65 74 65 20 61 20 56 64 62 65 46 72 61 6d elete a VdbeFram
1a47d 65 20 6f 62 6a 65 63 74 20 61 6e 64 20 69 74 73 e object and its
1a47e 20 63 6f 6e 74 65 6e 74 73 2e 20 56 64 62 65 46 contents. VdbeF
1a47f 72 61 6d 65 20 6f 62 6a 65 63 74 73 20 61 72 65 rame objects are
1a480 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 .** allocated by
1a481 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 the OP_Program
1a482 6f 70 63 6f 64 65 20 69 6e 20 73 71 6c 69 74 65 opcode in sqlite
1a483 33 56 64 62 65 45 78 65 63 28 29 2e 0a 2a 2f 0a 3VdbeExec()..*/.
1a484 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a485 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 oid sqlite3VdbeF
1a486 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 rameDelete(VdbeF
1a487 72 61 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 rame *p){. int
1a488 69 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d i;. Mem *aMem =
1a489 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 VdbeFrameMem(p)
1a48a 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a ;. VdbeCursor *
1a48b 2a 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 *apCsr = (VdbeCu
1a48c 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d rsor **)&aMem[p-
1a48d 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66 >nChildMem];. f
1a48e 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68 or(i=0; i<p->nCh
1a48f 69 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0a 20 20 ildCsr; i++){.
1a490 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 sqlite3VdbeFre
1a491 65 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20 61 70 eCursor(p->v, ap
1a492 43 73 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 Csr[i]);. }. r
1a493 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 61 eleaseMemArray(a
1a494 4d 65 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 4d 65 Mem, p->nChildMe
1a495 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 m);. sqlite3DbF
1a496 72 65 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 70 29 ree(p->v->db, p)
1a497 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c ;.}...#ifdef SQL
1a498 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ITE_ENABLE_MEMOR
1a499 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 4c Y_MANAGEMENT.SQL
1a49a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1a49b 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 sqlite3VdbeRelea
1a49c 73 65 42 75 66 66 65 72 73 28 56 64 62 65 20 2a seBuffers(Vdbe *
1a49d 70 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 p){. int ii;.
1a49e 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20 int nFree = 0;.
1a49f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1a4a0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
1a4a1 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 b->mutex) );. f
1a4a2 6f 72 28 69 69 3d 31 3b 20 69 69 3c 3d 70 2d 3e or(ii=1; ii<=p->
1a4a3 6e 4d 65 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 nMem; ii++){.
1a4a4 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d Mem *pMem = &p-
1a4a5 3e 61 4d 65 6d 5b 69 69 5d 3b 0a 20 20 20 20 69 >aMem[ii];. i
1a4a6 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 f( pMem->flags &
1a4a7 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 MEM_RowSet ){.
1a4a8 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 sqlite3RowS
1a4a9 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e etClear(pMem->u.
1a4aa 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a pRowSet);. }.
1a4ab 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 if( pMem->z
1a4ac 26 26 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d && pMem->flags&M
1a4ad 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 EM_Dyn ){.
1a4ae 61 73 73 65 72 74 28 20 21 70 4d 65 6d 2d 3e 78 assert( !pMem->x
1a4af 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 6e 46 72 Del );. nFr
1a4b0 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 44 62 4d ee += sqlite3DbM
1a4b1 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e allocSize(pMem->
1a4b2 64 62 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 db, pMem->z);.
1a4b3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1a4b4 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b emRelease(pMem);
1a4b5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1a4b6 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e urn nFree;.}.#en
1a4b7 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
1a4b8 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e ITE_OMIT_EXPLAIN
1a4b9 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69 ./*.** Give a li
1a4ba 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f sting of the pro
1a4bb 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74 gram in the virt
1a4bc 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a ual machine..**.
1a4bd 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 ** The interface
1a4be 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
1a4bf 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
1a4c0 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 ). But instead
1a4c1 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68 of.** running th
1a4c2 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b e code, it invok
1a4c3 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 es the callback
1a4c4 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e once for each in
1a4c5 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 struction..** Th
1a4c6 69 73 20 66 65 61 74 75 72 65 20 69 73 20 75 73 is feature is us
1a4c7 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 ed to implement
1a4c8 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a "EXPLAIN"..**.**
1a4c9 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e When p->explain
1a4ca 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75 ==1, each instru
1a4cb 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e ction is listed.
1a4cc 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 When.** p->exp
1a4cd 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 lain==2, only OP
1a4ce 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 63 _Explain instruc
1a4cf 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 64 tions are listed
1a4d0 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 72 and these.** ar
1a4d1 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 66 e shown in a dif
1a4d2 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 20 ferent format.
1a4d3 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 73 p->explain==2 is
1a4d4 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
1a4d5 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 55 nt.** EXPLAIN QU
1a4d6 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c ERY PLAN..*/.SQL
1a4d7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1a4d8 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 sqlite3VdbeList(
1a4d9 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 20 20 . Vdbe *p
1a4da 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a4db 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 The VDBE */.){.
1a4dc 20 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 int nRow;
1a4dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a4de 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
1a4df 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 6f umber of rows to
1a4e0 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 return */. int
1a4e1 20 6e 53 75 62 20 3d 20 30 3b 20 20 20 20 20 20 nSub = 0;
1a4e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a4e3 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
1a4e4 75 62 2d 76 64 62 65 73 20 73 65 65 6e 20 73 6f ub-vdbes seen so
1a4e5 20 66 61 72 20 2a 2f 0a 20 20 53 75 62 50 72 6f far */. SubPro
1a4e6 67 72 61 6d 20 2a 2a 61 70 53 75 62 20 3d 20 30 gram **apSub = 0
1a4e7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1a4e8 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62 2d 76 * Array of sub-v
1a4e9 64 62 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 dbes */. Mem *p
1a4ea 53 75 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 Sub = 0;. sqlit
1a4eb 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a e3 *db = p->db;.
1a4ec 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 int i;. int r
1a4ed 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1a4ee 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e Mem *pMem = p->
1a4ef 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70 2d pResultSet = &p-
1a4f0 3e 61 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 >aMem[1];.. ass
1a4f1 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 ert( p->explain
1a4f2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
1a4f3 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
1a4f4 43 5f 52 55 4e 20 29 3b 0a 20 20 61 73 73 65 72 C_RUN );. asser
1a4f5 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 t( db->magic==SQ
1a4f6 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 LITE_MAGIC_BUSY
1a4f7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
1a4f8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
1a4f9 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 p->rc==SQLITE_B
1a4fa 55 53 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 USY || p->rc==SQ
1a4fb 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 LITE_NOMEM );..
1a4fc 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 /* Even though
1a4fd 74 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 this opcode does
1a4fe 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 63 not use dynamic
1a4ff 20 73 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a strings for. *
1a500 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20 72 65 * the result, re
1a501 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 sult columns may
1a502 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 become dynamic
1a503 69 66 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c if the user call
1a504 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 s. ** sqlite3_c
1a505 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 olumn_text16(),
1a506 63 61 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c causing a transl
1a507 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 ation to UTF-16
1a508 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 encoding.. */.
1a509 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 releaseMemArray
1a50a 28 70 4d 65 6d 2c 20 38 29 3b 0a 0a 20 20 69 66 (pMem, 8);.. if
1a50b 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f ( p->rc==SQLITE_
1a50c 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 NOMEM ){. /*
1a50d 54 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 This happens if
1a50e 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 a malloc() insid
1a50f 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 e a call to sqli
1a510 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 te3_column_text(
1a511 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 ) or. ** sqli
1a512 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
1a513 36 28 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 6() failed. */.
1a514 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
1a515 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 iled = 1;. re
1a516 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1a517 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 R;. }.. /* Fig
1a518 75 72 65 20 6f 75 74 20 74 6f 74 61 6c 20 6e 75 ure out total nu
1a519 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 mber of rows tha
1a51a 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e t will be return
1a51b 65 64 20 62 79 20 74 68 69 73 20 0a 20 20 2a 2a ed by this . **
1a51c 20 45 58 50 4c 41 49 4e 20 70 72 6f 67 72 61 6d EXPLAIN program
1a51d 2e 20 20 2a 2f 0a 20 20 6e 52 6f 77 20 3d 20 70 . */. nRow = p
1a51e 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 70 2d 3e ->nOp;. if( p->
1a51f 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 explain==1 ){.
1a520 20 20 70 53 75 62 20 3d 20 26 70 2d 3e 61 4d 65 pSub = &p->aMe
1a521 6d 5b 39 5d 3b 0a 20 20 20 20 69 66 28 20 70 53 m[9];. if( pS
1a522 75 62 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c ub->flags&MEM_Bl
1a523 6f 62 20 29 7b 0a 20 20 20 20 20 20 6e 53 75 62 ob ){. nSub
1a524 20 3d 20 70 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f = pSub->n/sizeo
1a525 66 28 56 64 62 65 2a 29 3b 0a 20 20 20 20 20 20 f(Vdbe*);.
1a526 61 70 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 apSub = (SubProg
1a527 72 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a ram **)pSub->z;.
1a528 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d }. for(i=
1a529 30 3b 20 69 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0; i<nSub; i++){
1a52a 0a 20 20 20 20 20 20 6e 52 6f 77 20 2b 3d 20 61 . nRow += a
1a52b 70 53 75 62 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 pSub[i]->nOp;.
1a52c 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 }. }.. do{.
1a52d 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a i = p->pc++;.
1a52e 20 20 7d 77 68 69 6c 65 28 20 69 3c 6e 52 6f 77 }while( i<nRow
1a52f 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d && p->explain==
1a530 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 2 && p->aOp[i].o
1a531 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 pcode!=OP_Explai
1a532 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 6e 52 n );. if( i>=nR
1a533 6f 77 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 ow ){. p->rc
1a534 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
1a535 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e rc = SQLITE_DON
1a536 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 E;. }else if( d
1a537 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 b->u1.isInterrup
1a538 74 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 ted ){. p->rc
1a539 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 = SQLITE_INTERR
1a53a 55 50 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 UPT;. rc = SQ
1a53b 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
1a53c 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 sqlite3SetString
1a53d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 (&p->zErrMsg, db
1a53e 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 , "%s", sqlite3E
1a53f 72 72 53 74 72 28 70 2d 3e 72 63 29 29 3b 0a 20 rrStr(p->rc));.
1a540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 }else{. char
1a541 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 *z;. Op *pOp
1a542 3b 0a 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 6e ;. if( i<p->n
1a543 4f 70 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 20 Op ){. pOp
1a544 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 = &p->aOp[i];.
1a545 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
1a546 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 20 2d 3d nt j;. i -=
1a547 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 66 p->nOp;. f
1a548 6f 72 28 6a 3d 30 3b 20 69 3e 3d 61 70 53 75 62 or(j=0; i>=apSub
1a549 5b 6a 5d 2d 3e 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a [j]->nOp; j++){.
1a54a 20 20 20 20 20 20 20 20 69 20 2d 3d 20 61 70 53 i -= apS
1a54b 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 ub[j]->nOp;.
1a54c 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 }. pOp =
1a54d 26 61 70 53 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 &apSub[j]->aOp[i
1a54e 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 ];. }. if(
1a54f 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 p->explain==1 )
1a550 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c {. pMem->fl
1a551 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1a552 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 pMem->type
1a553 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 = SQLITE_INTEGER
1a554 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e ;. pMem->u.
1a555 69 20 3d 20 69 3b 20 20 20 20 20 20 20 20 20 20 i = i;
1a556 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a557 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d /* Program
1a558 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
1a559 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 pMem++;. .
1a55a 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a55b 20 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f MEM_Static|MEM_
1a55c 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 Str|MEM_Term;.
1a55d 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 pMem->z = (c
1a55e 68 61 72 2a 29 73 71 6c 69 74 65 33 4f 70 63 6f har*)sqlite3Opco
1a55f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f deName(pOp->opco
1a560 64 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64 65 20 de); /* Opcode
1a561 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
1a562 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 pMem->z!=0 );.
1a563 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 pMem->n = s
1a564 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 qlite3Strlen30(p
1a565 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 Mem->z);. p
1a566 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a567 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 TE_TEXT;. p
1a568 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Mem->enc = SQLIT
1a569 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d E_UTF8;. pM
1a56a 65 6d 2b 2b 3b 0a 0a 20 20 20 20 20 20 69 66 28 em++;.. if(
1a56b 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pOp->p4type==P4
1a56c 5f 53 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 _SUBPROGRAM ){.
1a56d 20 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 int nByte
1a56e 20 3d 20 28 6e 53 75 62 2b 31 29 2a 73 69 7a 65 = (nSub+1)*size
1a56f 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b of(SubProgram*);
1a570 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a . int j;.
1a571 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b for(j=0;
1a572 20 6a 3c 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 j<nSub; j++){.
1a573 20 20 20 20 20 20 20 20 20 69 66 28 20 61 70 53 if( apS
1a574 75 62 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 ub[j]==pOp->p4.p
1a575 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b Program ) break;
1a576 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1a577 20 20 20 69 66 28 20 6a 3d 3d 6e 53 75 62 20 26 if( j==nSub &
1a578 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c & SQLITE_OK==sql
1a579 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1a57a 70 53 75 62 2c 20 6e 42 79 74 65 2c 20 31 29 20 pSub, nByte, 1)
1a57b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 53 ){. apS
1a57c 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d ub = (SubProgram
1a57d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 **)pSub->z;.
1a57e 20 20 20 20 20 20 20 61 70 53 75 62 5b 6e 53 75 apSub[nSu
1a57f 62 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 b++] = pOp->p4.p
1a580 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20 Program;.
1a581 20 20 20 70 53 75 62 2d 3e 66 6c 61 67 73 20 7c pSub->flags |
1a582 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 = MEM_Blob;.
1a583 20 20 20 20 20 20 70 53 75 62 2d 3e 6e 20 3d 20 pSub->n =
1a584 6e 53 75 62 2a 73 69 7a 65 6f 66 28 53 75 62 50 nSub*sizeof(SubP
1a585 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20 rogram*);.
1a586 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1a587 7d 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 }.. pMem->fla
1a588 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1a589 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f pMem->u.i = pO
1a58a 70 2d 3e 70 31 3b 20 20 20 20 20 20 20 20 20 20 p->p1;
1a58b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a58c 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70 4d 65 /* P1 */. pMe
1a58d 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1a58e 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d _INTEGER;. pM
1a58f 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d em++;.. pMem-
1a590 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1a591 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 ;. pMem->u.i
1a592 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20 20 20 = pOp->p2;
1a593 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a594 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 /* P2 */.
1a595 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
1a596 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 LITE_INTEGER;.
1a597 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 pMem++;.. i
1a598 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 f( p->explain==1
1a599 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e ){. pMem->
1a59a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1a59b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 . pMem->u.i
1a59c 20 3d 20 70 4f 70 2d 3e 70 33 3b 20 20 20 20 20 = pOp->p3;
1a59d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a59e 20 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 /* P3 */.
1a59f 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1a5a0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b SQLITE_INTEGER;
1a5a1 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 . pMem++;.
1a5a2 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 }.. if( sq
1a5a3 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1a5a4 28 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20 29 7b (pMem, 32, 0) ){
1a5a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1a5a6 34 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 4 */. asser
1a5a7 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 t( p->db->malloc
1a5a8 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 Failed );.
1a5a9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1a5aa 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ROR;. }. p
1a5ab 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1a5ac 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d _Dyn|MEM_Str|MEM
1a5ad 5f 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 _Term;. z = d
1a5ae 69 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 70 4d isplayP4(pOp, pM
1a5af 65 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 20 20 em->z, 32);.
1a5b0 69 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 if( z!=pMem->z )
1a5b1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1a5b2 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4d 65 dbeMemSetStr(pMe
1a5b3 6d 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 m, z, -1, SQLITE
1a5b4 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 7d _UTF8, 0);. }
1a5b5 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
1a5b6 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 rt( pMem->z!=0 )
1a5b7 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 ;. pMem->n
1a5b8 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1a5b9 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 0(pMem->z);.
1a5ba 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 pMem->enc = SQ
1a5bb 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 7d LITE_UTF8;. }
1a5bc 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 . pMem->type
1a5bd 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 = SQLITE_TEXT;.
1a5be 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 pMem++;..
1a5bf 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d if( p->explain==
1a5c0 31 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 1 ){. if( s
1a5c1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
1a5c2 77 28 70 4d 65 6d 2c 20 34 2c 20 30 29 20 29 7b w(pMem, 4, 0) ){
1a5c3 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1a5c4 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 p->db->mallocFa
1a5c5 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 iled );.
1a5c6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1a5c7 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ROR;. }.
1a5c8 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a5c9 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 MEM_Dyn|MEM_Str
1a5ca 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 |MEM_Term;.
1a5cb 20 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 pMem->n = 2;.
1a5cc 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a5cd 69 6e 74 66 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c intf(3, pMem->z,
1a5ce 20 22 25 2e 32 78 22 2c 20 70 4f 70 2d 3e 70 35 "%.2x", pOp->p5
1a5cf 29 3b 20 20 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 ); /* P5 */.
1a5d0 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1a5d1 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 SQLITE_TEXT;.
1a5d2 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
1a5d3 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 SQLITE_UTF8;.
1a5d4 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 pMem++;. .#i
1a5d5 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1a5d6 47 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d G. if( pOp-
1a5d7 3e 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 >zComment ){.
1a5d8 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1a5d9 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 = MEM_Str|MEM_T
1a5da 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 erm;. pMe
1a5db 6d 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d m->z = pOp->zCom
1a5dc 6d 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 4d ment;. pM
1a5dd 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 em->n = sqlite3S
1a5de 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 trlen30(pMem->z)
1a5df 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e ;. pMem->
1a5e0 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 enc = SQLITE_UTF
1a5e1 38 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 8;. pMem-
1a5e2 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 >type = SQLITE_T
1a5e3 45 58 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 EXT;. }else
1a5e4 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a .#endif. {.
1a5e5 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c pMem->fl
1a5e6 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 ags = MEM_Null;
1a5e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5e8 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 /* Comment
1a5e9 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d */. pMem
1a5ea 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1a5eb 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 NULL;. }.
1a5ec 20 20 7d 0a 0a 20 20 20 20 70 2d 3e 6e 52 65 73 }.. p->nRes
1a5ed 43 6f 6c 75 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 Column = 8 - 5*(
1a5ee 70 2d 3e 65 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 p->explain-1);.
1a5ef 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 p->rc = SQLIT
1a5f0 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53 E_OK;. rc = S
1a5f1 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 QLITE_ROW;. }.
1a5f2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
1a5f3 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1a5f4 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a MIT_EXPLAIN */..
1a5f5 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1a5f6 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 BUG./*.** Print
1a5f7 74 68 65 20 53 51 4c 20 74 68 61 74 20 77 61 73 the SQL that was
1a5f8 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 used to generat
1a5f9 65 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d e a VDBE program
1a5fa 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a5fb 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a5fc 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 VdbePrintSql(Vdb
1a5fd 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 e *p){. int nOp
1a5fe 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 = p->nOp;. Vdb
1a5ff 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 eOp *pOp;. if(
1a600 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a nOp<1 ) return;.
1a601 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b pOp = &p->aOp[
1a602 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 0];. if( pOp->o
1a603 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 pcode==OP_Trace
1a604 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 && pOp->p4.z!=0
1a605 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 ){. const cha
1a606 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a r *z = pOp->p4.z
1a607 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c ;. while( sql
1a608 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 ite3Isspace(*z)
1a609 29 20 7a 2b 2b 3b 0a 20 20 20 20 70 72 69 6e 74 ) z++;. print
1a60a 66 28 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c f("SQL: [%s]\n",
1a60b 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 z);. }.}.#endi
1a60c 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 f..#if !defined(
1a60d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1a60e 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 E) && defined(SQ
1a60f 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 LITE_ENABLE_IOTR
1a610 41 43 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 ACE)./*.** Print
1a611 20 61 6e 20 49 4f 54 52 41 43 45 20 6d 65 73 73 an IOTRACE mess
1a612 61 67 65 20 73 68 6f 77 69 6e 67 20 53 51 4c 20 age showing SQL
1a613 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 content..*/.SQLI
1a614 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a615 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 sqlite3VdbeIOTra
1a616 63 65 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a ceSql(Vdbe *p){.
1a617 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e int nOp = p->n
1a618 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f Op;. VdbeOp *pO
1a619 70 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 p;. if( sqlite3
1a61a 49 6f 54 72 61 63 65 3d 3d 30 20 29 20 72 65 74 IoTrace==0 ) ret
1a61b 75 72 6e 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 urn;. if( nOp<1
1a61c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 ) return;. pOp
1a61d 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 = &p->aOp[0];.
1a61e 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1a61f 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f ==OP_Trace && pO
1a620 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 p->p4.z!=0 ){.
1a621 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 int i, j;.
1a622 63 68 61 72 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 char z[1000];.
1a623 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1a624 74 66 28 73 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c tf(sizeof(z), z,
1a625 20 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a "%s", pOp->p4.z
1a626 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
1a627 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a sqlite3Isspace(z
1a628 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 [i]); i++){}.
1a629 20 66 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 for(j=0; z[i];
1a62a 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 i++){. if(
1a62b 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a sqlite3Isspace(z
1a62c 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 [i]) ){.
1a62d 69 66 28 20 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 if( z[i-1]!=' '
1a62e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a ){. z[j
1a62f 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 ++] = ' ';.
1a630 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
1a631 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d {. z[j++]
1a632 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 7d = z[i];. }
1a633 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 . }. z[j]
1a634 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 = 0;. sqlite3
1a635 49 6f 54 72 61 63 65 28 22 53 51 4c 20 25 73 5c IoTrace("SQL %s\
1a636 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 n", z);. }.}.#e
1a637 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f ndif /* !SQLITE_
1a638 4f 4d 49 54 5f 54 52 41 43 45 20 26 26 20 53 51 OMIT_TRACE && SQ
1a639 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 LITE_ENABLE_IOTR
1a63a 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c ACE */../*.** Al
1a63b 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 72 6f locate space fro
1a63c 6d 20 61 20 66 69 78 65 64 20 73 69 7a 65 20 62 m a fixed size b
1a63d 75 66 66 65 72 2e 20 20 4d 61 6b 65 20 2a 70 70 uffer. Make *pp
1a63e 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a point to the.**
1a63f 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
1a640 2e 20 20 28 4e 6f 74 65 3a 20 20 70 70 20 69 73 . (Note: pp is
1a641 20 61 20 63 68 61 72 2a 20 72 61 74 68 65 72 20 a char* rather
1a642 74 68 61 6e 20 61 20 76 6f 69 64 2a 2a 20 74 6f than a void** to
1a643 0a 2a 2a 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 .** work around
1a644 74 68 65 20 70 6f 69 6e 74 65 72 20 61 6c 69 61 the pointer alia
1a645 73 69 6e 67 20 72 75 6c 65 73 20 6f 66 20 43 2e sing rules of C.
1a646 29 20 20 2a 70 70 20 73 68 6f 75 6c 64 20 69 6e ) *pp should in
1a647 69 74 69 61 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 itially.** be ze
1a648 72 6f 2e 20 20 49 66 20 2a 70 70 20 69 73 20 6e ro. If *pp is n
1a649 6f 74 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 ot zero, that me
1a64a 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 70 61 ans that the spa
1a64b 63 65 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a ce has already.*
1a64c 2a 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 * been allocated
1a64d 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e and this routin
1a64e 65 20 69 73 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a e is a noop..**.
1a64f 2a 2a 20 6e 42 79 74 65 20 69 73 20 74 68 65 20 ** nByte is the
1a650 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1a651 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 2e of space needed.
1a652 0a 2a 2a 0a 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 .**.** *ppFrom p
1a653 6f 69 6e 74 20 74 6f 20 61 76 61 69 6c 61 62 6c oint to availabl
1a654 65 20 73 70 61 63 65 20 61 6e 64 20 70 45 6e 64 e space and pEnd
1a655 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 65 points to the e
1a656 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 61 76 61 nd of the.** ava
1a657 69 6c 61 62 6c 65 20 73 70 61 63 65 2e 0a 2a 2a ilable space..**
1a658 0a 2a 2a 20 2a 70 6e 42 79 74 65 20 69 73 20 61 .** *pnByte is a
1a659 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20 counter of the
1a65a 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1a65b 6f 66 20 73 70 61 63 65 20 74 68 61 74 20 68 61 of space that ha
1a65c 76 65 20 66 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 ve failed.** to
1a65d 61 6c 6c 6f 63 61 74 65 2e 20 20 49 66 20 74 68 allocate. If th
1a65e 65 72 65 20 69 73 20 69 6e 73 75 66 66 69 63 69 ere is insuffici
1a65f 65 6e 74 20 73 70 61 63 65 20 69 6e 20 2a 70 70 ent space in *pp
1a660 46 72 6f 6d 20 74 6f 20 73 61 74 69 73 66 79 20 From to satisfy
1a661 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20 the.** request,
1a662 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a then increment *
1a663 70 6e 42 79 74 65 20 62 79 20 74 68 65 20 61 6d pnByte by the am
1a664 6f 75 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75 ount of the requ
1a665 65 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 est..*/.static v
1a666 6f 69 64 20 61 6c 6c 6f 63 53 70 61 63 65 28 0a oid allocSpace(.
1a667 20 20 63 68 61 72 20 2a 70 70 2c 20 20 20 20 20 char *pp,
1a668 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 /* IN/OUT
1a669 3a 20 53 65 74 20 2a 70 70 20 74 6f 20 70 6f 69 : Set *pp to poi
1a66a 6e 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20 nt to allocated
1a66b 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 buffer */. int
1a66c 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 nByte,
1a66d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1a66e 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 tes to allocate
1a66f 2a 2f 0a 20 20 75 38 20 2a 2a 70 70 46 72 6f 6d */. u8 **ppFrom
1a670 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f , /* IN/
1a671 4f 55 54 3a 20 41 6c 6c 6f 63 61 74 65 20 66 72 OUT: Allocate fr
1a672 6f 6d 20 2a 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 om *ppFrom */.
1a673 75 38 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 u8 *pEnd,
1a674 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
1a675 74 6f 20 31 20 62 79 74 65 20 70 61 73 74 20 74 to 1 byte past t
1a676 68 65 20 65 6e 64 20 6f 66 20 2a 70 70 46 72 6f he end of *ppFro
1a677 6d 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e m buffer */. in
1a678 74 20 2a 70 6e 42 79 74 65 20 20 20 20 20 20 20 t *pnByte
1a679 20 20 20 2f 2a 20 49 66 20 61 6c 6c 6f 63 61 74 /* If allocat
1a67a 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6d 61 ion cannot be ma
1a67b 64 65 2c 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 de, increment *p
1a67c 6e 42 79 74 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 nByte */.){. as
1a67d 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 sert( EIGHT_BYTE
1a67e 5f 41 4c 49 47 4e 4d 45 4e 54 28 2a 70 70 46 72 _ALIGNMENT(*ppFr
1a67f 6f 6d 29 20 29 3b 0a 20 20 69 66 28 20 28 2a 28 om) );. if( (*(
1a680 76 6f 69 64 2a 2a 29 70 70 29 3d 3d 30 20 29 7b void**)pp)==0 ){
1a681 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 . nByte = ROU
1a682 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 ND8(nByte);.
1a683 69 66 28 20 26 28 2a 70 70 46 72 6f 6d 29 5b 6e if( &(*ppFrom)[n
1a684 42 79 74 65 5d 20 3c 3d 20 70 45 6e 64 20 29 7b Byte] <= pEnd ){
1a685 0a 20 20 20 20 20 20 2a 28 76 6f 69 64 2a 2a 29 . *(void**)
1a686 70 70 20 3d 20 28 76 6f 69 64 20 2a 29 2a 70 70 pp = (void *)*pp
1a687 46 72 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 70 46 From;. *ppF
1a688 72 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 rom += nByte;.
1a689 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a }else{. *
1a68a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74 65 3b pnByte += nByte;
1a68b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1a68c 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 .** Prepare a vi
1a68d 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f rtual machine fo
1a68e 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 r execution. Th
1a68f 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e is involves thin
1a690 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c gs such.** as al
1a691 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 locating stack s
1a692 70 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c pace and initial
1a693 69 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 izing the progra
1a694 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 m counter..** Af
1a695 74 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 ter the VDBE has
1a696 20 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 be prepped, it
1a697 63 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 can be executed
1a698 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a by one or more.*
1a699 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 * calls to sqlit
1a69a 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a e3VdbeExec(). .
1a69b 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 **.** This is th
1a69c 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f e only way to mo
1a69d 76 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 ve a VDBE from V
1a69e 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 DBE_MAGIC_INIT t
1a69f 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f o.** VDBE_MAGIC_
1a6a0 52 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 RUN..**.** This
1a6a1 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 function may be
1a6a2 63 61 6c 6c 65 64 20 6d 6f 72 65 20 74 68 61 6e called more than
1a6a3 20 6f 6e 63 65 20 6f 6e 20 61 20 73 69 6e 67 6c once on a singl
1a6a4 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e e virtual machin
1a6a5 65 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 e..** The first
1a6a6 63 61 6c 6c 20 69 73 20 6d 61 64 65 20 77 68 69 call is made whi
1a6a7 6c 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 le compiling the
1a6a8 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 SQL statement.
1a6a9 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 Subsequent.** ca
1a6aa 6c 6c 73 20 61 72 65 20 6d 61 64 65 20 61 73 20 lls are made as
1a6ab 70 61 72 74 20 6f 66 20 74 68 65 20 70 72 6f 63 part of the proc
1a6ac 65 73 73 20 6f 66 20 72 65 73 65 74 74 69 6e 67 ess of resetting
1a6ad 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 a statement to
1a6ae 62 65 0a 2a 2a 20 72 65 2d 65 78 65 63 75 74 65 be.** re-execute
1a6af 64 20 28 66 72 6f 6d 20 61 20 63 61 6c 6c 20 74 d (from a call t
1a6b0 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 o sqlite3_reset(
1a6b1 29 29 2e 20 54 68 65 20 6e 56 61 72 2c 20 6e 4d )). The nVar, nM
1a6b2 65 6d 2c 20 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 em, nCursor .**
1a6b3 61 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 70 61 and isExplain pa
1a6b4 72 61 6d 65 74 65 72 73 20 61 72 65 20 6f 6e 6c rameters are onl
1a6b5 79 20 70 61 73 73 65 64 20 63 6f 72 72 65 63 74 y passed correct
1a6b6 20 76 61 6c 75 65 73 20 74 68 65 20 66 69 72 73 values the firs
1a6b7 74 20 74 69 6d 65 0a 2a 2a 20 74 68 65 20 66 75 t time.** the fu
1a6b8 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1a6b9 2e 20 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 . On subsequent
1a6ba 63 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 6c 69 calls, from sqli
1a6bb 74 65 33 5f 72 65 73 65 74 28 29 2c 20 6e 56 61 te3_reset(), nVa
1a6bc 72 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 2d r.** is passed -
1a6bd 31 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 1 and nMem, nCur
1a6be 73 6f 72 20 61 6e 64 20 69 73 45 78 70 6c 61 69 sor and isExplai
1a6bf 6e 20 61 72 65 20 61 6c 6c 20 70 61 73 73 65 64 n are all passed
1a6c0 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 zero..*/.SQLITE
1a6c1 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a6c2 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 lite3VdbeMakeRea
1a6c3 64 79 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 dy(. Vdbe *p,
1a6c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6c5 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 /* The VDBE
1a6c6 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 */. int nVar,
1a6c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6c8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1a6c9 66 20 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 f '?' see in the
1a6ca 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a SQL statement *
1a6cb 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 /. int nMem,
1a6cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6cd 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1a6ce 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 memory cells to
1a6cf 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e allocate */. in
1a6d0 74 20 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 t nCursor,
1a6d1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a6d2 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 Number of cursor
1a6d3 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f s to allocate */
1a6d4 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 . int nArg,
1a6d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6d6 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d /* Maximum num
1a6d7 62 65 72 20 6f 66 20 61 72 67 73 20 69 6e 20 53 ber of args in S
1a6d8 75 62 50 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 ubPrograms */.
1a6d9 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 2c 20 20 int isExplain,
1a6da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a6db 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58 * True if the EX
1a6dc 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69 PLAIN keywords i
1a6dd 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 s present */. i
1a6de 6e 74 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e nt usesStmtJourn
1a6df 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a al /*
1a6e0 20 54 72 75 65 20 74 6f 20 73 65 74 20 56 64 62 True to set Vdb
1a6e1 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 e.usesStmtJourna
1a6e2 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b l */.){. int n;
1a6e3 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1a6e4 20 70 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 p->db;.. asser
1a6e5 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 t( p!=0 );. ass
1a6e6 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 ert( p->magic==V
1a6e7 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 DBE_MAGIC_INIT )
1a6e8 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 ;.. /* There sh
1a6e9 6f 75 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 ould be at least
1a6ea 20 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a one opcode.. *
1a6eb 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e /. assert( p->n
1a6ec 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 Op>0 );.. /* Se
1a6ed 74 20 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 t the magic to V
1a6ee 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f DBE_MAGIC_RUN so
1a6ef 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e oner rather than
1a6f0 20 6c 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e later. */. p->
1a6f1 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 magic = VDBE_MAG
1a6f2 49 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f IC_RUN;.. /* Fo
1a6f3 72 20 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 r each cursor re
1a6f4 71 75 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c quired, also all
1a6f5 6f 63 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 ocate a memory c
1a6f6 65 6c 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a ell. Memory. **
1a6f7 20 63 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e cells (nMem+1-n
1a6f8 43 75 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 Cursor)..nMem, i
1a6f9 6e 63 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e nclusive, will n
1a6fa 65 76 65 72 20 62 65 20 75 73 65 64 20 62 79 0a ever be used by.
1a6fb 20 20 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 ** the vdbe pr
1a6fc 6f 67 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 ogram. Instead t
1a6fd 68 65 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 hey are used to
1a6fe 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 allocate space f
1a6ff 6f 72 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 or. ** VdbeCurs
1a700 6f 72 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 or/BtCursor stru
1a701 63 74 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 ctures. The blob
1a702 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 of memory assoc
1a703 69 61 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a iated with . **
1a704 20 63 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f cursor 0 is sto
1a705 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 red in memory ce
1a706 6c 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 ll nMem. Memory
1a707 63 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 cell (nMem-1).
1a708 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c ** stores the bl
1a709 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 ob of memory ass
1a70a 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72 ociated with cur
1a70b 73 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a sor 1, etc.. **
1a70c 0a 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 . ** See also:
1a70d 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 allocateCursor()
1a70e 2e 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d .. */. nMem +=
1a70f 20 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 nCursor;.. /*
1a710 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 Allocate space f
1a711 6f 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 or memory regist
1a712 65 72 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c ers, SQL variabl
1a713 65 73 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 es, VDBE cursors
1a714 20 61 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 and . ** an ar
1a715 72 61 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 ray to marshal S
1a716 51 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 QL function argu
1a717 6d 65 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 ments in. This i
1a718 73 20 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a s only done the.
1a719 20 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 ** first time
1a71a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
1a71b 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 called for a gi
1a71c 76 65 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 ven VDBE, not wh
1a71d 65 6e 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 en it is. ** be
1a71e 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 ing called from
1a71f 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 sqlite3_reset()
1a720 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72 to reset the vir
1a721 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 tual machine..
1a722 2a 2f 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 */. if( nVar>=0
1a723 20 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6d && ALWAYS(db->m
1a724 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 29 20 allocFailed==0)
1a725 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 73 72 20 ){. u8 *zCsr
1a726 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b = (u8 *)&p->aOp[
1a727 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 38 20 p->nOp];. u8
1a728 2a 7a 45 6e 64 20 3d 20 28 75 38 20 2a 29 26 70 *zEnd = (u8 *)&p
1a729 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f ->aOp[p->nOpAllo
1a72a 63 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 c];. int nByt
1a72b 65 3b 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 e;. resolveP2
1a72c 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 Values(p, &nArg)
1a72d 3b 0a 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d ;. p->usesStm
1a72e 74 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 tJournal = (u8)u
1a72f 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 0a sesStmtJournal;.
1a730 20 20 20 20 69 66 28 20 69 73 45 78 70 6c 61 69 if( isExplai
1a731 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a n && nMem<10 ){.
1a732 20 20 20 20 20 20 6e 4d 65 6d 20 3d 20 31 30 3b nMem = 10;
1a733 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 . }. memse
1a734 74 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64 2d t(zCsr, 0, zEnd-
1a735 7a 43 73 72 29 3b 0a 20 20 20 20 7a 43 73 72 20 zCsr);. zCsr
1a736 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29 += (zCsr - (u8*)
1a737 30 29 26 37 3b 0a 20 20 20 20 61 73 73 65 72 74 0)&7;. assert
1a738 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
1a739 47 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29 3b 0a GNMENT(zCsr) );.
1a73a 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 . do {.
1a73b 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 nByte = 0;.
1a73c 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 allocSpace((cha
1a73d 72 2a 29 26 70 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 r*)&p->aMem, nMe
1a73e 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 m*sizeof(Mem), &
1a73f 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 zCsr, zEnd, &nBy
1a740 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 te);. alloc
1a741 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d Space((char*)&p-
1a742 3e 61 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 >aVar, nVar*size
1a743 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 of(Mem), &zCsr,
1a744 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 zEnd, &nByte);.
1a745 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 allocSpace(
1a746 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 41 72 67 (char*)&p->apArg
1a747 2c 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 , nArg*sizeof(Me
1a748 6d 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 m*), &zCsr, zEnd
1a749 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 , &nByte);.
1a74a 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 allocSpace((cha
1a74b 72 2a 29 26 70 2d 3e 61 7a 56 61 72 2c 20 6e 56 r*)&p->azVar, nV
1a74c 61 72 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 ar*sizeof(char*)
1a74d 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 , &zCsr, zEnd, &
1a74e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c nByte);. al
1a74f 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 locSpace((char*)
1a750 26 70 2d 3e 61 70 43 73 72 2c 20 0a 20 20 20 20 &p->apCsr, .
1a751 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 75 nCu
1a752 72 73 6f 72 2a 73 69 7a 65 6f 66 28 56 64 62 65 rsor*sizeof(Vdbe
1a753 43 75 72 73 6f 72 2a 29 2c 20 26 7a 43 73 72 2c Cursor*), &zCsr,
1a754 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 0a 20 20 zEnd, &nByte.
1a755 20 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 );. if(
1a756 20 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 nByte ){.
1a757 20 20 70 2d 3e 70 46 72 65 65 20 3d 20 73 71 6c p->pFree = sql
1a758 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
1a759 28 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 (db, nByte);.
1a75a 20 20 20 7d 0a 20 20 20 20 20 20 7a 43 73 72 20 }. zCsr
1a75b 3d 20 70 2d 3e 70 46 72 65 65 3b 0a 20 20 20 20 = p->pFree;.
1a75c 20 20 7a 45 6e 64 20 3d 20 26 7a 43 73 72 5b 6e zEnd = &zCsr[n
1a75d 42 79 74 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c Byte];. }whil
1a75e 65 28 20 6e 42 79 74 65 20 26 26 20 21 64 62 2d e( nByte && !db-
1a75f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
1a760 0a 0a 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 .. p->nCursor
1a761 20 3d 20 28 75 31 36 29 6e 43 75 72 73 6f 72 3b = (u16)nCursor;
1a762 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72 . if( p->aVar
1a763 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 ){. p->nVa
1a764 72 20 3d 20 28 75 31 36 29 6e 56 61 72 3b 0a 20 r = (u16)nVar;.
1a765 20 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c for(n=0; n<
1a766 6e 56 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 nVar; n++){.
1a767 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 p->aVar[n].f
1a768 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1a769 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 56 61 72 . p->aVar
1a76a 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 [n].db = db;.
1a76b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1a76c 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 f( p->aMem ){.
1a76d 20 20 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 p->aMem--;
1a76e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a76f 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f /* aMem[] go
1a770 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 es from 1..nMem
1a771 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d */. p->nMem
1a772 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 = nMem;
1a773 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20 20 /*
1a774 20 20 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d not from 0..nM
1a775 65 6d 2d 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f em-1 */. fo
1a776 72 28 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 r(n=1; n<=nMem;
1a777 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d n++){. p-
1a778 3e 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d >aMem[n].flags =
1a779 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 MEM_Null;.
1a77a 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 p->aMem[n].db
1a77b 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 = db;. }.
1a77c 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 }. }.#ifdef
1a77d 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66 SQLITE_DEBUG. f
1a77e 6f 72 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 or(n=1; n<p->nMe
1a77f 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73 m; n++){. ass
1a780 65 72 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e ert( p->aMem[n].
1a781 64 62 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65 db==db );. }.#e
1a782 6e 64 69 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 ndif.. p->pc =
1a783 2d 31 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 -1;. p->rc = SQ
1a784 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 LITE_OK;. p->er
1a785 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 rorAction = OE_A
1a786 62 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 bort;. p->expla
1a787 69 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b in |= isExplain;
1a788 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 . p->magic = VD
1a789 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 BE_MAGIC_RUN;.
1a78a 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a p->nChange = 0;.
1a78b 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 p->cacheCtr =
1a78c 31 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 1;. p->minWrite
1a78d 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 FileFormat = 255
1a78e 3b 0a 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e ;. p->iStatemen
1a78f 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 t = 0;.#ifdef VD
1a790 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 BE_PROFILE. {.
1a791 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
1a792 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
1a793 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e i++){. p->
1a794 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a aOp[i].cnt = 0;.
1a795 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e p->aOp[i].
1a796 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 cycles = 0;.
1a797 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a }. }.#endif.}..
1a798 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 /*.** Close a VD
1a799 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 BE cursor and re
1a79a 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 lease all the re
1a79b 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72 sources that cur
1a79c 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 sor .** happens
1a79d 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 to hold..*/.SQLI
1a79e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a79f 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 sqlite3VdbeFreeC
1a7a0 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 ursor(Vdbe *p, V
1a7a1 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b dbeCursor *pCx){
1a7a2 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b . if( pCx==0 ){
1a7a3 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1a7a4 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 . if( pCx->pBt
1a7a5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
1a7a6 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 reeClose(pCx->pB
1a7a7 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 t);. /* The p
1a7a8 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c Cx->pCursor will
1a7a9 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 be close automa
1a7aa 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 tically, if it e
1a7ab 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a xists, by. **
1a7ac 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e the call above.
1a7ad 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 */. }else if(
1a7ae 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a pCx->pCursor ){.
1a7af 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1a7b0 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d CloseCursor(pCx-
1a7b1 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 >pCursor);. }.#
1a7b2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1a7b3 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1a7b4 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 if( pCx->pVtab
1a7b5 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 Cursor ){. sq
1a7b6 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1a7b7 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d r *pVtabCursor =
1a7b8 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f pCx->pVtabCurso
1a7b9 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c r;. const sql
1a7ba 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1a7bb 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 dule = pCx->pMod
1a7bc 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 ule;. p->inVt
1a7bd 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 abMethod = 1;.
1a7be 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1a7bf 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b afetyOff(p->db);
1a7c0 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 . pModule->xC
1a7c1 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 lose(pVtabCursor
1a7c2 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c );. (void)sql
1a7c3 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e ite3SafetyOn(p->
1a7c4 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 db);. p->inVt
1a7c5 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1a7c6 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a }.#endif.}../*.*
1a7c7 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 * Copy the value
1a7c8 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
1a7c9 56 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 VdbeFrame struct
1a7ca 75 72 65 20 74 6f 20 69 74 73 20 56 64 62 65 2e ure to its Vdbe.
1a7cb 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 This.** is used
1a7cc 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 , for example, w
1a7cd 68 65 6e 20 61 20 74 72 69 67 67 65 72 20 73 75 hen a trigger su
1a7ce 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 68 61 6c b-program is hal
1a7cf 74 65 64 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a ted to restore.*
1a7d0 2a 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 * control to the
1a7d1 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a main program..*
1a7d2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a7d3 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1a7d4 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62 FrameRestore(Vdb
1a7d5 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b eFrame *pFrame){
1a7d6 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72 . Vdbe *v = pFr
1a7d7 61 6d 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70 ame->v;. v->aOp
1a7d8 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a = pFrame->aOp;.
1a7d9 20 20 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d v->nOp = pFram
1a7da 65 2d 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 e->nOp;. v->aMe
1a7db 6d 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d m = pFrame->aMem
1a7dc 3b 0a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 ;. v->nMem = pF
1a7dd 72 61 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d rame->nMem;. v-
1a7de 3e 61 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d >apCsr = pFrame-
1a7df 3e 61 70 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 >apCsr;. v->nCu
1a7e0 72 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e rsor = pFrame->n
1a7e1 43 75 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d Cursor;. v->db-
1a7e2 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72 >lastRowid = pFr
1a7e3 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a ame->lastRowid;.
1a7e4 20 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 v->nChange = p
1a7e5 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a Frame->nChange;.
1a7e6 20 20 72 65 74 75 72 6e 20 70 46 72 61 6d 65 2d return pFrame-
1a7e7 3e 70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c >pc;.}../*.** Cl
1a7e8 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e ose all cursors.
1a7e9 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 .**.** Also rele
1a7ea 61 73 65 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 ase any dynamic
1a7eb 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 memory held by t
1a7ec 68 65 20 56 4d 20 69 6e 20 74 68 65 20 56 64 62 he VM in the Vdb
1a7ed 65 2e 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a e.aMem memory .*
1a7ee 2a 20 63 65 6c 6c 20 61 72 72 61 79 2e 20 54 68 * cell array. Th
1a7ef 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 is is necessary
1a7f0 61 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 as the memory ce
1a7f1 6c 6c 20 61 72 72 61 79 20 6d 61 79 20 63 6f 6e ll array may con
1a7f2 74 61 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 tain.** pointers
1a7f3 20 74 6f 20 56 64 62 65 46 72 61 6d 65 20 6f 62 to VdbeFrame ob
1a7f4 6a 65 63 74 73 2c 20 77 68 69 63 68 20 6d 61 79 jects, which may
1a7f5 20 69 6e 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e in turn contain
1a7f6 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 pointers to.**
1a7f7 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f open cursors..*/
1a7f8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f .static void clo
1a7f9 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 seAllCursors(Vdb
1a7fa 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e e *p){. if( p->
1a7fb 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 pFrame ){. Vd
1a7fc 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 beFrame *pFrame
1a7fd 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 = p->pFrame;.
1a7fe 20 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 for(pFrame=p->p
1a7ff 46 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 Frame; pFrame->p
1a800 50 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 Parent; pFrame=p
1a801 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b Frame->pParent);
1a802 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1a803 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 FrameRestore(pFr
1a804 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 ame);. }. p->p
1a805 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e Frame = 0;. p->
1a806 6e 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 nFrame = 0;.. i
1a807 66 28 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 f( p->apCsr ){.
1a808 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
1a809 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 r(i=0; i<p->nCur
1a80a 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 sor; i++){.
1a80b 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 VdbeCursor *pC
1a80c 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 = p->apCsr[i];.
1a80d 20 20 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20 if( pC ){.
1a80e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1a80f 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 beFreeCursor(p,
1a810 70 43 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e pC);. p->
1a811 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 apCsr[i] = 0;.
1a812 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1a813 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b if( p->aMem ){
1a814 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 . releaseMemA
1a815 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d rray(&p->aMem[1]
1a816 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a , p->nMem);. }.
1a817 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 }../*.** Clean u
1a818 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 65 p the VM after e
1a819 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 xecution..**.**
1a81a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
1a81b 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 l automatically
1a81c 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72 close any cursor
1a81d 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 s, lists, and/or
1a81e 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74 .** sorters that
1a81f 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e were left open.
1a820 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 It also delete
1a821 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a s the values of.
1a822 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 ** variables in
1a823 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79 the aVar[] array
1a824 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1a825 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70 Cleanup(Vdbe *p
1a826 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1a827 20 3d 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65 = p->db;..#ifde
1a828 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
1a829 20 2f 2a 20 45 78 65 63 75 74 65 20 61 73 73 65 /* Execute asse
1a82a 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
1a82b 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 to ensure that t
1a82c 68 65 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 he Vdbe.apCsr[]
1a82d 61 6e 64 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61 and . ** Vdbe.a
1a82e 4d 65 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76 Mem[] arrays hav
1a82f 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 e already been c
1a830 6c 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20 leaned up. */.
1a831 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
1a832 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 0; i<p->nCursor;
1a833 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d i++) assert( p-
1a834 3e 61 70 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e >apCsr==0 || p->
1a835 61 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20 apCsr[i]==0 );.
1a836 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e for(i=1; i<=p->
1a837 6e 4d 65 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72 nMem; i++) asser
1a838 74 28 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c t( p->aMem==0 ||
1a839 20 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 p->aMem[i].flag
1a83a 73 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 s==MEM_Null );.#
1a83b 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
1a83c 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1a83d 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 rrMsg);. p->zEr
1a83e 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 rMsg = 0;. p->p
1a83f 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d ResultSet = 0;.}
1a840 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
1a841 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 number of result
1a842 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 columns that wi
1a843 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 ll be returned b
1a844 79 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 y this SQL.** st
1a845 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 atement. This is
1a846 20 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 now set at comp
1a847 69 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 ile time, rather
1a848 20 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 than during.**
1a849 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 execution of the
1a84a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f vdbe program so
1a84b 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f that sqlite3_co
1a84c 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e lumn_count() can
1a84d 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e .** be called on
1a84e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
1a84f 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 t before sqlite3
1a850 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 _step()..*/.SQLI
1a851 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a852 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 sqlite3VdbeSetNu
1a853 6d 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 mCols(Vdbe *p, i
1a854 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a nt nResColumn){.
1a855 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b Mem *pColName;
1a856 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 . int n;. sqli
1a857 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
1a858 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 .. releaseMemAr
1a859 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c ray(p->aColName,
1a85a 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 p->nResColumn*C
1a85b 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c OLNAME_N);. sql
1a85c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1a85d 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e ->aColName);. n
1a85e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f = nResColumn*CO
1a85f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 LNAME_N;. p->nR
1a860 65 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29 esColumn = (u16)
1a861 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d nResColumn;. p-
1a862 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c >aColName = pCol
1a863 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c Name = (Mem*)sql
1a864 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
1a865 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 (db, sizeof(Mem)
1a866 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 *n );. if( p->a
1a867 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 ColName==0 ) ret
1a868 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d urn;. while( n-
1a869 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f - > 0 ){. pCo
1a86a 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d lName->flags = M
1a86b 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f EM_Null;. pCo
1a86c 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 lName->db = p->d
1a86d 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b b;. pColName+
1a86e 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 +;. }.}../*.**
1a86f 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 Set the name of
1a870 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d the idx'th colum
1a871 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 n to be returned
1a872 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 by the SQL stat
1a873 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 ement..** zName
1a874 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 must be a pointe
1a875 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 r to a nul termi
1a876 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a nated string..**
1a877 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 .** This call mu
1a878 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 st be made after
1a879 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
1a87a 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 e3VdbeSetNumCols
1a87b 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ()..**.** The fi
1a87c 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 nal parameter, x
1a87d 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 Del, must be one
1a87e 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d of SQLITE_DYNAM
1a87f 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 IC, SQLITE_STATI
1a880 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 C.** or SQLITE_T
1a881 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 RANSIENT. If it
1a882 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 is SQLITE_DYNAMI
1a883 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 C, then the buff
1a884 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f er pointed.** to
1a885 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 by zName will b
1a886 65 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 e freed by sqlit
1a887 65 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 e3DbFree() when
1a888 74 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74 the vdbe is dest
1a889 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 royed..*/.SQLITE
1a88a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1a88b 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 ite3VdbeSetColNa
1a88c 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 me(. Vdbe *p,
1a88d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a88e 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 /* Vdbe b
1a88f 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 eing configured
1a890 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 */. int idx,
1a891 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a892 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1a893 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 f column zName a
1a894 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 pplies to */. i
1a895 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 nt var,
1a896 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a897 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f /* One of the CO
1a898 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 LNAME_* constant
1a899 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 s */. const cha
1a89a 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 r *zName,
1a89b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
1a89c 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e er to buffer con
1a89d 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a taining name */.
1a89e 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
1a89f 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 oid*)
1a8a0 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e /* Memory man
1a8a1 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 agement strategy
1a8a2 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b for zName */.){
1a8a3 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d . int rc;. Mem
1a8a4 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 *pColName;. as
1a8a5 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 sert( idx<p->nRe
1a8a6 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 sColumn );. ass
1a8a7 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 ert( var<COLNAME
1a8a8 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 _N );. if( p->d
1a8a9 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a8aa 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
1a8ab 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 zName || xDel!=S
1a8ac 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b QLITE_DYNAMIC );
1a8ad 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1a8ae 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
1a8af 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e assert( p->aColN
1a8b0 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c ame!=0 );. pCol
1a8b1 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c Name = &(p->aCol
1a8b2 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e Name[idx+var*p->
1a8b3 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 nResColumn]);.
1a8b4 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1a8b5 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 MemSetStr(pColNa
1a8b6 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 me, zName, -1, S
1a8b7 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c QLITE_UTF8, xDel
1a8b8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 );. assert( rc!
1a8b9 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 =0 || !zName ||
1a8ba 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 (pColName->flags
1a8bb 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b &MEM_Term)!=0 );
1a8bc 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1a8bd 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 ./*.** A read or
1a8be 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1a8bf 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f on may or may no
1a8c0 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 t be active on d
1a8c1 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a atabase handle.*
1a8c2 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 * db. If a trans
1a8c3 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
1a8c4 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 , commit it. If
1a8c5 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 there is a.** wr
1a8c6 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
1a8c7 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 spanning more th
1a8c8 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 an one database
1a8c9 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 file, this routi
1a8ca 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 ne.** takes care
1a8cb 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a of the master j
1a8cc 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e ournal trickery.
1a8cd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 .*/.static int v
1a8ce 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 dbeCommit(sqlite
1a8cf 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 3 *db, Vdbe *p){
1a8d0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
1a8d1 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 nTrans = 0; /*
1a8d2 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 Number of databa
1a8d3 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 ses with an acti
1a8d4 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 ve write-transac
1a8d5 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 tion */. int rc
1a8d6 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1a8d7 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 int needXcommit
1a8d8 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c = 0;..#ifdef SQL
1a8d9 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1a8da 54 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20 TABLE. /* With
1a8db 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c this option, sql
1a8dc 69 74 65 33 56 74 61 62 53 79 6e 63 28 29 20 69 ite3VtabSync() i
1a8dd 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 s defined to be
1a8de 73 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c simply . ** SQL
1a8df 49 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e ITE_OK so p is n
1a8e0 6f 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20 ot used. . */.
1a8e1 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1a8e2 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 R(p);.#endif..
1a8e3 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 /* Before doing
1a8e4 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 anything else, c
1a8e5 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 all the xSync()
1a8e6 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 callback for any
1a8e7 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f . ** virtual mo
1a8e8 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 dule tables writ
1a8e9 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e ten in this tran
1a8ea 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 saction. This ha
1a8eb 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e s to. ** be don
1a8ec 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 e before determi
1a8ed 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d ning whether a m
1a8ee 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1a8ef 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 le is . ** requ
1a8f0 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e ired, as an xSyn
1a8f1 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 c() callback may
1a8f2 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 add an attached
1a8f3 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 database. ** t
1a8f4 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f o the transactio
1a8f5 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 n.. */. rc = s
1a8f6 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 qlite3VtabSync(d
1a8f7 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b b, &p->zErrMsg);
1a8f8 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1a8f9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
1a8fa 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
1a8fb 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 This loop deter
1a8fc 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 mines (a) if the
1a8fd 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f commit hook sho
1a8fe 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 uld be invoked a
1a8ff 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 nd. ** (b) how
1a900 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 many database fi
1a901 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 les have open wr
1a902 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ite transactions
1a903 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c , not . ** incl
1a904 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 uding the temp d
1a905 61 74 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 atabase. (b) is
1a906 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 important becaus
1a907 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a e if more than .
1a908 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 ** one databas
1a909 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 e file has an op
1a90a 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 en write transac
1a90b 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a tion, a master j
1a90c 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 ournal. ** file
1a90d 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 is required for
1a90e 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 an atomic commi
1a90f 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 t.. */ . for(i
1a910 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1a911 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 ++){ . Btree
1a912 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 *pBt = db->aDb[i
1a913 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 ].pBt;. if( s
1a914 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 qlite3BtreeIsInT
1a915 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 rans(pBt) ){.
1a916 20 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d needXcommit =
1a917 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 1;. if( i!
1a918 3d 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 =1 ) nTrans++;.
1a919 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
1a91a 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 f there are any
1a91b 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1a91c 6e 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b ns at all, invok
1a91d 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f e the commit hoo
1a91e 6b 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 k */. if( needX
1a91f 63 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 commit && db->xC
1a920 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b ommitCallback ){
1a921 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 . (void)sqlit
1a922 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b e3SafetyOff(db);
1a923 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 . rc = db->xC
1a924 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 ommitCallback(db
1a925 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 ->pCommitArg);.
1a926 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
1a927 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 SafetyOn(db);.
1a928 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
1a929 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1a92a 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 CONSTRAINT;.
1a92b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 }. }.. /* The
1a92c 73 69 6d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f simple case - no
1a92d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 more than one d
1a92e 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f atabase file (no
1a92f 74 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 t counting the.
1a930 20 2a 2a 20 54 45 4d 50 20 64 61 74 61 62 61 73 ** TEMP databas
1a931 65 29 20 68 61 73 20 61 20 74 72 61 6e 73 61 63 e) has a transac
1a932 74 69 6f 6e 20 61 63 74 69 76 65 2e 20 20 20 54 tion active. T
1a933 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
1a934 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 for the. ** mas
1a935 74 65 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a ter-journal.. *
1a936 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 *. ** If the re
1a937 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 turn value of sq
1a938 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c lite3BtreeGetFil
1a939 65 6e 61 6d 65 28 29 20 69 73 20 61 20 7a 65 72 ename() is a zer
1a93a 6f 20 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 o length. ** st
1a93b 72 69 6e 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 ring, it means t
1a93c 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1a93d 20 69 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 is :memory: or
1a93e 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e a temp file. In
1a93f 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65 . ** that case
1a940 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f we do not suppo
1a941 72 74 20 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d rt atomic multi-
1a942 66 69 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f file commits, so
1a943 20 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 use the . ** s
1a944 69 6d 70 6c 65 20 63 61 73 65 20 74 68 65 6e 20 imple case then
1a945 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 too.. */. if(
1a946 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 0==sqlite3Strlen
1a947 33 30 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 30(sqlite3BtreeG
1a948 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 etFilename(db->a
1a949 44 62 5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c Db[0].pBt)). |
1a94a 7c 20 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b | nTrans<=1. ){
1a94b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 . for(i=0; rc
1a94c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
1a94d 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
1a94e 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 Btree *pBt
1a94f 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
1a950 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 t;. if( pBt
1a951 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1a952 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1a953 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c mitPhaseOne(pBt,
1a954 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
1a955 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 }.. /* Do th
1a956 65 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 e commit only if
1a957 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 all databases s
1a958 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 uccessfully comp
1a959 6c 65 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 lete phase 1. .
1a95a 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 ** If one of
1a95b 74 68 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 the BtreeCommitP
1a95c 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 haseOne() calls
1a95d 66 61 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 fails, this indi
1a95e 63 61 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 cates an. **
1a95f 49 4f 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 IO error while d
1a960 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 eleting or trunc
1a961 61 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 ating a journal
1a962 66 69 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 file. It is unli
1a963 6b 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 kely,. ** but
1a964 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 could happen. I
1a965 6e 20 74 68 69 73 20 63 61 73 65 20 61 62 61 6e n this case aban
1a966 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 don processing a
1a967 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 nd return the er
1a968 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ror.. */.
1a969 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c for(i=0; rc==SQL
1a96a 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e ITE_OK && i<db->
1a96b 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 nDb; i++){.
1a96c 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 Btree *pBt = db
1a96d 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
1a96e 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 if( pBt ){.
1a96f 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1a970 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
1a971 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 aseTwo(pBt);.
1a972 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1a973 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1a974 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1a975 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 3VtabCommit(db);
1a976 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1a977 20 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 The complex cas
1a978 65 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d e - There is a m
1a979 75 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d ulti-file write-
1a97a 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 transaction acti
1a97b 76 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 ve.. ** This re
1a97c 71 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 quires a master
1a97d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 journal file to
1a97e 65 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 ensure the trans
1a97f 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 action is. ** c
1a980 6f 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c ommitted atomicl
1a981 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 y.. */.#ifndef
1a982 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
1a983 49 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 IO. else{. s
1a984 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1a985 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 = db->pVfs;.
1a986 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 int needSync =
1a987 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 0;. char *zMa
1a988 73 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 ster = 0; /* F
1a989 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 ile-name for the
1a98a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1a98b 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 */. char cons
1a98c 74 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 t *zMainFile = s
1a98d 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 qlite3BtreeGetFi
1a98e 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 lename(db->aDb[0
1a98f 5d 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 ].pBt);. sqli
1a990 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 te3_file *pMaste
1a991 72 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f r = 0;. i64 o
1a992 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 ffset = 0;. i
1a993 6e 74 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 nt res;.. /*
1a994 53 65 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 Select a master
1a995 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
1a996 65 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 e */. do {.
1a997 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b u32 iRandom;
1a998 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1a999 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 Free(db, zMaster
1a99a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1a99b 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 _randomness(size
1a99c 6f 66 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 of(iRandom), &iR
1a99d 61 6e 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d andom);. zM
1a99e 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d aster = sqlite3M
1a99f 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d Printf(db, "%s-m
1a9a0 6a 25 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c j%08X", zMainFil
1a9a1 65 2c 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 e, iRandom&0x7ff
1a9a2 66 66 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66 fffff);. if
1a9a3 28 20 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 ( !zMaster ){.
1a9a4 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1a9a5 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
1a9a6 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
1a9a7 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 lite3OsAccess(pV
1a9a8 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c fs, zMaster, SQL
1a9a9 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
1a9aa 53 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 S, &res);. }w
1a9ab 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
1a9ac 5f 4f 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20 _OK && res );.
1a9ad 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1a9ae 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 _OK ){. /*
1a9af 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 Open the master
1a9b0 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 journal. */.
1a9b1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1a9b2 4f 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c OpenMalloc(pVfs,
1a9b3 20 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 zMaster, &pMast
1a9b4 65 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 er, . S
1a9b5 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
1a9b6 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
1a9b7 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 _CREATE|.
1a9b8 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 SQLITE_OPEN_E
1a9b9 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f XCLUSIVE|SQLITE_
1a9ba 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
1a9bb 4e 41 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a NAL, 0. );.
1a9bc 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
1a9bd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1a9be 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1a9bf 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
1a9c0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1a9c1 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a ;. }. . /*
1a9c2 20 57 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20 Write the name
1a9c3 6f 66 20 65 61 63 68 20 64 61 74 61 62 61 73 65 of each database
1a9c4 20 66 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61 file in the tra
1a9c5 6e 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 nsaction into th
1a9c6 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 e new. ** mas
1a9c7 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1a9c8 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
1a9c9 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 curs at this poi
1a9ca 6e 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 nt close. **
1a9cb 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d and delete the m
1a9cc 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1a9cd 6c 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 le. All the indi
1a9ce 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 vidual journal f
1a9cf 69 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c iles. ** stil
1a9d0 6c 20 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 l have 'null' as
1a9d1 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1a9d2 6e 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 nal pointer, so
1a9d3 74 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 they will roll.
1a9d4 20 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 ** back indep
1a9d5 65 6e 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61 endently if a fa
1a9d6 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 ilure occurs..
1a9d7 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 */. for(i=0
1a9d8 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
1a9d9 29 7b 0a 20 20 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 20 20 69 66 28 20 .pBt;. if(
1a9dc 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b i==1 ) continue;
1a9dd 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 /* Ignore the
1a9de 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a TEMP database *
1a9df 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 /. if( sqli
1a9e0 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e te3BtreeIsInTran
1a9e1 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 s(pBt) ){.
1a9e2 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 char const *zF
1a9e3 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 ile = sqlite3Btr
1a9e4 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 eeGetJournalname
1a9e5 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69 (pBt);. i
1a9e6 66 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 f( zFile[0]==0 )
1a9e7 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 continue; /* I
1a9e8 67 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 gnore :memory: d
1a9e9 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 atabases */.
1a9ea 20 20 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e if( !needSyn
1a9eb 63 20 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 c && !sqlite3Btr
1a9ec 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70 eeSyncDisabled(p
1a9ed 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Bt) ){.
1a9ee 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 needSync = 1;.
1a9ef 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1a9f0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
1a9f1 72 69 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 rite(pMaster, zF
1a9f2 69 6c 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c ile, sqlite3Strl
1a9f3 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f en30(zFile)+1, o
1a9f4 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 ffset);.
1a9f5 6f 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 offset += sqlite
1a9f6 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 3Strlen30(zFile)
1a9f7 2b 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +1;. if(
1a9f8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1a9f9 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1a9fa 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d e3OsCloseFree(pM
1a9fb 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 aster);.
1a9fc 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 sqlite3OsDelet
1a9fd 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c e(pVfs, zMaster,
1a9fe 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 0);. s
1a9ff 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1aa00 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 zMaster);.
1aa01 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1aa02 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1aa03 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
1aa04 53 79 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20 Sync the master
1aa05 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 journal file. If
1aa06 20 74 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 the IOCAP_SEQUE
1aa07 4e 54 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20 NTIAL device.
1aa08 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20 ** flag is set
1aa09 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 this is not requ
1aa0a 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ired.. */.
1aa0b 20 69 66 28 20 6e 65 65 64 53 79 6e 63 20 0a 20 if( needSync .
1aa0c 20 20 20 20 26 26 20 30 3d 3d 28 73 71 6c 69 74 && 0==(sqlit
1aa0d 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 e3OsDeviceCharac
1aa0e 74 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 teristics(pMaste
1aa0f 72 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f r)&SQLITE_IOCAP_
1aa10 53 45 51 55 45 4e 54 49 41 4c 29 0a 20 20 20 20 SEQUENTIAL).
1aa11 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 && SQLITE_OK!=(
1aa12 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 rc = sqlite3OsSy
1aa13 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 51 4c 49 nc(pMaster, SQLI
1aa14 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 TE_SYNC_NORMAL))
1aa15 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71 . ){. sq
1aa16 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 lite3OsCloseFree
1aa17 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 (pMaster);.
1aa18 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
1aa19 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
1aa1a 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0);. sqlite
1aa1b 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 3DbFree(db, zMas
1aa1c 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ter);. retu
1aa1d 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 rn rc;. }..
1aa1e 20 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 /* Sync all th
1aa1f 65 20 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c e db files invol
1aa20 76 65 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73 ved in the trans
1aa21 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 action. The same
1aa22 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 call. ** set
1aa23 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 s the master jou
1aa24 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 rnal pointer in
1aa25 65 61 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20 each individual
1aa26 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 journal. If.
1aa27 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 ** an error occu
1aa28 72 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 rs here, do not
1aa29 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 delete the maste
1aa2a 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a r journal file..
1aa2b 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 **. ** If
1aa2c 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 the error occur
1aa2d 73 20 64 75 72 69 6e 67 20 74 68 65 20 66 69 72 s during the fir
1aa2e 73 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a st call to. *
1aa2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f * sqlite3BtreeCo
1aa30 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 mmitPhaseOne(),
1aa31 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 then there is a
1aa32 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 0a chance that the.
1aa33 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f ** master jo
1aa34 75 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 urnal file will
1aa35 62 65 20 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 be orphaned. But
1aa36 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 we cannot delet
1aa37 65 20 69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 e it,. ** in
1aa38 63 61 73 65 20 74 68 65 20 6d 61 73 74 65 72 20 case the master
1aa39 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
1aa3a 65 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e e was written in
1aa3b 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 to the journal.
1aa3c 20 20 20 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 ** file befor
1aa3d 65 20 74 68 65 20 66 61 69 6c 75 72 65 20 6f 63 e the failure oc
1aa3e 63 75 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 curred.. */.
1aa3f 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d for(i=0; rc==
1aa40 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 SQLITE_OK && i<d
1aa41 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 b->nDb; i++){ .
1aa42 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 Btree *pBt
1aa43 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
1aa44 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 ;. if( pBt
1aa45 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
1aa46 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1aa47 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 itPhaseOne(pBt,
1aa48 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 zMaster);.
1aa49 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 }. }. sqli
1aa4a 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 te3OsCloseFree(p
1aa4b 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 Master);. if(
1aa4c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1aa4d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
1aa4e 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 bFree(db, zMaste
1aa4f 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e r);. return
1aa50 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc;. }..
1aa51 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6d 61 /* Delete the ma
1aa52 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
1aa53 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74 73 20 e. This commits
1aa54 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
1aa55 20 41 66 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f After. ** do
1aa56 69 6e 67 20 74 68 69 73 20 74 68 65 20 64 69 72 ing this the dir
1aa57 65 63 74 6f 72 79 20 69 73 20 73 79 6e 63 65 64 ectory is synced
1aa58 20 61 67 61 69 6e 20 62 65 66 6f 72 65 20 61 6e again before an
1aa59 79 20 69 6e 64 69 76 69 64 75 61 6c 0a 20 20 20 y individual.
1aa5a 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
1aa5b 66 69 6c 65 73 20 61 72 65 20 64 65 6c 65 74 65 files are delete
1aa5c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 d.. */. rc
1aa5d 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 = sqlite3OsDele
1aa5e 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 te(pVfs, zMaster
1aa5f 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 1);. sqlite
1aa60 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 3DbFree(db, zMas
1aa61 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65 ter);. zMaste
1aa62 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 r = 0;. if( r
1aa63 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 c ){. retur
1aa64 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 n rc;. }..
1aa65 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e /* All files an
1aa66 64 20 64 69 72 65 63 74 6f 72 69 65 73 20 68 61 d directories ha
1aa67 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
1aa68 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66 synced, so the f
1aa69 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 ollowing. **
1aa6a 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
1aa6b 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
1aa6c 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20 63 Two() are only c
1aa6d 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 61 6e 64 losing files and
1aa6e 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 . ** deleting
1aa6f 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 6a or truncating j
1aa70 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65 ournals. If some
1aa71 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
1aa72 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 while. ** th
1aa73 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 is is happening
1aa74 77 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20 we don't really
1aa75 63 61 72 65 2e 20 54 68 65 20 69 6e 74 65 67 72 care. The integr
1aa76 69 74 79 20 6f 66 20 74 68 65 0a 20 20 20 20 2a ity of the. *
1aa77 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
1aa78 20 61 6c 72 65 61 64 79 20 67 75 61 72 61 6e 74 already guarant
1aa79 65 65 64 2c 20 62 75 74 20 73 6f 6d 65 20 73 74 eed, but some st
1aa7a 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e ray 'cold' journ
1aa7b 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62 als. ** may b
1aa7c 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20 e lying around.
1aa7d 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 Returning an err
1aa7e 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74 20 68 65 or code won't he
1aa7f 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20 20 20 20 lp matters..
1aa80 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 5f 73 */. disable_s
1aa81 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
1aa82 72 73 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 rs();. sqlite
1aa83 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
1aa84 6f 63 28 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d oc();. for(i=
1aa85 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
1aa86 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65 +){ . Btree
1aa87 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b *pBt = db->aDb[
1aa88 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 i].pBt;. if
1aa89 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ( pBt ){.
1aa8a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1aa8b 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 29 mitPhaseTwo(pBt)
1aa8c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1aa8d 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 sqlite3EndBe
1aa8e 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1aa8f 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 enable_simulat
1aa90 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a ed_io_errors();.
1aa91 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 . sqlite3Vtab
1aa92 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a Commit(db);. }.
1aa93 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
1aa94 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 rc;.}../* .** T
1aa95 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
1aa96 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 ks that the sqli
1aa97 74 65 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e te3.activeVdbeCn
1aa98 74 20 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 t count variable
1aa99 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 .** matches the
1aa9a 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73 number of vdbe's
1aa9b 20 69 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c in the list sql
1aa9c 69 74 65 33 2e 70 56 64 62 65 20 74 68 61 74 20 ite3.pVdbe that
1aa9d 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 are.** currently
1aa9e 20 61 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65 active. An asse
1aa9f 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 rtion fails if t
1aaa0 68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f he two counts do
1aaa1 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54 not match..** T
1aaa2 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e his is an intern
1aaa3 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e al self-check on
1aaa4 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61 ly - it is not a
1aaa5 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63 n essential proc
1aaa6 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a essing.** step..
1aaa7 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 **.** This is a
1aaa8 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20 no-op if NDEBUG
1aaa9 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 is defined..*/.#
1aaaa 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 ifndef NDEBUG.st
1aaab 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 atic void checkA
1aaac 63 74 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c ctiveVdbeCnt(sql
1aaad 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 ite3 *db){. Vdb
1aaae 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20 e *p;. int cnt
1aaaf 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 = 0;. int nWrit
1aab0 65 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d e = 0;. p = db-
1aab1 3e 70 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 >pVdbe;. while(
1aab2 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p ){. if( p-
1aab3 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1aab4 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e IC_RUN && p->pc>
1aab5 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b =0 ){. cnt+
1aab6 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e +;. if( p->
1aab7 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 readOnly==0 ) nW
1aab8 72 69 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 rite++;. }.
1aab9 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a p = p->pNext;.
1aaba 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 6e }. assert( cn
1aabb 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56 64 62 t==db->activeVdb
1aabc 65 43 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 eCnt );. assert
1aabd 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72 ( nWrite==db->wr
1aabe 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a iteVdbeCnt );.}.
1aabf 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 68 #else.#define ch
1aac0 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 eckActiveVdbeCnt
1aac1 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (x).#endif../*.*
1aac2 2a 20 46 6f 72 20 65 76 65 72 79 20 42 74 72 65 * For every Btre
1aac3 65 20 74 68 61 74 20 69 6e 20 64 61 74 61 62 61 e that in databa
1aac4 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 se connection db
1aac5 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 62 which .** has b
1aac6 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c 20 22 74 een modified, "t
1aac7 72 69 70 22 20 6f 72 20 69 6e 76 61 6c 69 64 61 rip" or invalida
1aac8 74 65 20 65 61 63 68 20 63 75 72 73 6f 72 20 69 te each cursor i
1aac9 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72 65 65 20 n.** that Btree
1aaca 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 might have been
1aacb 6d 6f 64 69 66 69 65 64 20 73 6f 20 74 68 61 74 modified so that
1aacc 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63 the cursor.** c
1aacd 61 6e 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 an never be used
1aace 20 61 67 61 69 6e 2e 20 20 54 68 69 73 20 68 61 again. This ha
1aacf 70 70 65 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c ppens when a rol
1aad0 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73 lback.*** occurs
1aad1 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 74 72 . We have to tr
1aad2 69 70 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 ip all the other
1aad3 20 63 75 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a cursors, even.*
1aad4 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 * cursor from ot
1aad5 68 65 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65 her VMs in diffe
1aad6 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f rent database co
1aad7 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f nnections,.** so
1aad8 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 that none of th
1aad9 65 6d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 em try to use th
1aada 65 20 64 61 74 61 20 61 74 20 77 68 69 63 68 20 e data at which
1aadb 74 68 65 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69 they.** were poi
1aadc 6e 74 69 6e 67 20 61 6e 64 20 77 68 69 63 68 20 nting and which
1aadd 6e 6f 77 20 6d 61 79 20 68 61 76 65 20 62 65 65 now may have bee
1aade 6e 20 63 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a n changed due.**
1aadf 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b to the rollback
1aae0 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 ..**.** Remember
1aae1 20 74 68 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b that a rollback
1aae2 20 63 61 6e 20 64 65 6c 65 74 65 20 74 61 62 6c can delete tabl
1aae3 65 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a es complete and.
1aae4 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f 6f 74 70 ** reorder rootp
1aae5 61 67 65 73 2e 20 20 53 6f 20 69 74 20 69 73 20 ages. So it is
1aae6 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 6a not sufficient j
1aae7 75 73 74 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74 ust to save.** t
1aae8 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 he state of the
1aae9 63 75 72 73 6f 72 2e 20 20 57 65 20 68 61 76 65 cursor. We have
1aaea 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 to invalidate t
1aaeb 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20 he cursor.** so
1aaec 74 68 61 74 20 69 74 20 69 73 20 6e 65 76 65 72 that it is never
1aaed 20 75 73 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a used again..*/.
1aaee 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 static void inva
1aaef 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d lidateCursorsOnM
1aaf0 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 73 71 odifiedBtrees(sq
1aaf1 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e lite3 *db){. in
1aaf2 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
1aaf3 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
1aaf4 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 . Btree *p =
1aaf5 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1aaf6 20 20 20 20 69 66 28 20 70 20 26 26 20 73 71 6c if( p && sql
1aaf7 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 ite3BtreeIsInTra
1aaf8 6e 73 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73 ns(p) ){. s
1aaf9 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 qlite3BtreeTripA
1aafa 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c llCursors(p, SQL
1aafb 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20 ITE_ABORT);.
1aafc 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 }. }.}../*.** I
1aafd 66 20 74 68 65 20 56 64 62 65 20 70 61 73 73 65 f the Vdbe passe
1aafe 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
1aaff 72 67 75 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61 rgument opened a
1ab00 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 statement-trans
1ab01 61 63 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 action,.** close
1ab02 20 69 74 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e it now. Argumen
1ab03 74 20 65 4f 70 20 6d 75 73 74 20 62 65 20 65 69 t eOp must be ei
1ab04 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 ther SAVEPOINT_R
1ab05 4f 4c 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41 OLLBACK or.** SA
1ab06 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e VEPOINT_RELEASE.
1ab07 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f If it is SAVEPO
1ab08 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 INT_ROLLBACK, th
1ab09 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 en the statement
1ab0a 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
1ab0b 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 is rolled back.
1ab0c 49 66 20 65 4f 70 20 69 73 20 53 41 56 45 50 4f If eOp is SAVEPO
1ab0d 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 INT_RELEASE, the
1ab0e 6e 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d n the .** statem
1ab0f 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1ab10 69 73 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a is commtted..**.
1ab11 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f ** If an IO erro
1ab12 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c r occurs, an SQL
1ab13 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 ITE_IOERR_XXX er
1ab14 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
1ab15 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 rned. .** Otherw
1ab16 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a ise SQLITE_OK..*
1ab17 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ab18 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1ab19 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 CloseStatement(V
1ab1a 64 62 65 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 dbe *p, int eOp)
1ab1b 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e {. sqlite3 *con
1ab1c 73 74 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 st db = p->db;.
1ab1d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1ab1e 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d _OK;.. /* If p-
1ab1f 3e 69 53 74 61 74 65 6d 65 6e 74 20 69 73 20 67 >iStatement is g
1ab20 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f reater than zero
1ab21 2c 20 74 68 65 6e 20 74 68 69 73 20 56 64 62 65 , then this Vdbe
1ab22 20 6f 70 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20 opened a . **
1ab23 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1ab24 63 74 69 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c ction that shoul
1ab25 64 20 62 65 20 63 6c 6f 73 65 64 20 68 65 72 65 d be closed here
1ab26 2e 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 . The only excep
1ab27 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61 tion. ** is tha
1ab28 74 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61 t an IO error ma
1ab29 79 20 68 61 76 65 20 6f 63 63 75 72 65 64 2c 20 y have occured,
1ab2a 63 61 75 73 69 6e 67 20 61 6e 20 65 6d 65 72 67 causing an emerg
1ab2b 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 ency rollback..
1ab2c 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 ** In this case
1ab2d 20 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 (db->nStatement
1ab2e 3d 3d 30 29 2c 20 61 6e 64 20 74 68 65 72 65 20 ==0), and there
1ab2f 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f is nothing to do
1ab30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d .. */. if( db-
1ab31 3e 6e 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 >nStatement && p
1ab32 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a ->iStatement ){.
1ab33 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 int i;. c
1ab34 6f 6e 73 74 20 69 6e 74 20 69 53 61 76 65 70 6f onst int iSavepo
1ab35 69 6e 74 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d int = p->iStatem
1ab36 65 6e 74 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65 ent-1;.. asse
1ab37 72 74 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 rt( eOp==SAVEPOI
1ab38 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65 NT_ROLLBACK || e
1ab39 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 Op==SAVEPOINT_RE
1ab3a 4c 45 41 53 45 29 3b 0a 20 20 20 20 61 73 73 65 LEASE);. asse
1ab3b 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 rt( db->nStateme
1ab3c 6e 74 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 nt>0 );. asse
1ab3d 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e rt( p->iStatemen
1ab3e 74 3d 3d 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 t==(db->nStateme
1ab3f 6e 74 2b 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e nt+db->nSavepoin
1ab40 74 29 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 t) );.. for(i
1ab41 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1ab42 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 ++){ . int
1ab43 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc2 = SQLITE_OK;
1ab44 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 . Btree *pB
1ab45 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 t = db->aDb[i].p
1ab46 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 Bt;. if( pB
1ab47 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 t ){. if(
1ab48 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f eOp==SAVEPOINT_
1ab49 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 ROLLBACK ){.
1ab4a 20 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 rc2 = sqli
1ab4b 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e te3BtreeSavepoin
1ab4c 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 t(pBt, SAVEPOINT
1ab4d 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65 _ROLLBACK, iSave
1ab4e 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 point);.
1ab4f 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
1ab50 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 2==SQLITE_OK ){.
1ab51 20 20 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 rc2 =
1ab52 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 sqlite3BtreeSave
1ab53 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 point(pBt, SAVEP
1ab54 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 53 OINT_RELEASE, iS
1ab55 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 avepoint);.
1ab56 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1ab57 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1ab58 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
1ab59 20 72 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a rc2;. }.
1ab5a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1ab5b 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 db->nStatement
1ab5c 2d 2d 3b 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 --;. p->iStat
1ab5d 65 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 ement = 0;. }.
1ab5e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1ab5f 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 *.** If SQLite i
1ab60 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75 s compiled to su
1ab61 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 pport shared-cac
1ab62 68 65 20 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62 he mode and to b
1ab63 65 20 74 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a e threadsafe,.**
1ab64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 62 this routine ob
1ab65 74 61 69 6e 73 20 74 68 65 20 6d 75 74 65 78 20 tains the mutex
1ab66 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1ab67 65 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74 each BtShared st
1ab68 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 ructure.** that
1ab69 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20 may be accessed
1ab6a 62 79 20 74 68 65 20 56 4d 20 70 61 73 73 65 64 by the VM passed
1ab6b 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e as an argument.
1ab6c 20 49 6e 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a In doing so it.
1ab6d 2a 2a 20 73 65 74 73 20 74 68 65 20 42 74 53 68 ** sets the BtSh
1ab6e 61 72 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f ared.db member o
1ab6f 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 42 74 f each of the Bt
1ab70 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 Shared structure
1ab71 73 2c 20 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74 s, ensuring.** t
1ab72 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 hat the correct
1ab73 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c busy-handler cal
1ab74 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
1ab75 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a if required..**
1ab76 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 .** If SQLite is
1ab77 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 not threadsafe
1ab78 62 75 74 20 64 6f 65 73 20 73 75 70 70 6f 72 74 but does support
1ab79 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f shared-cache mo
1ab7a 64 65 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69 de, then.** sqli
1ab7b 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
1ab7c 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f () is invoked to
1ab7d 20 73 65 74 20 74 68 65 20 42 74 53 68 61 72 65 set the BtShare
1ab7e 64 2e 64 62 20 76 61 72 69 61 62 6c 65 73 0a 2a d.db variables.*
1ab7f 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53 68 * of all of BtSh
1ab80 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73 20 ared structures
1ab81 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 74 accessible via t
1ab82 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
1ab83 6c 65 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 le .** associate
1ab84 64 20 77 69 74 68 20 74 68 65 20 56 4d 2e 20 4f d with the VM. O
1ab85 66 20 63 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20 f course only a
1ab86 73 75 62 73 65 74 20 6f 66 20 74 68 65 73 65 20 subset of these
1ab87 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 77 69 structures.** wi
1ab88 6c 6c 20 62 65 20 61 63 63 65 73 73 65 64 20 62 ll be accessed b
1ab89 79 20 74 68 65 20 56 4d 2c 20 61 6e 64 20 77 65 y the VM, and we
1ab8a 20 63 6f 75 6c 64 20 75 73 65 20 56 64 62 65 2e could use Vdbe.
1ab8b 62 74 72 65 65 4d 61 73 6b 20 74 6f 20 66 69 67 btreeMask to fig
1ab8c 75 72 65 0a 2a 2a 20 74 68 61 74 20 73 75 62 73 ure.** that subs
1ab8d 65 74 20 6f 75 74 2c 20 62 75 74 20 74 68 65 72 et out, but ther
1ab8e 65 20 69 73 20 6e 6f 20 61 64 76 61 6e 74 61 67 e is no advantag
1ab8f 65 20 74 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a e to doing so..*
1ab90 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 *.** If SQLite i
1ab91 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 s not threadsafe
1ab92 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 73 75 and does not su
1ab93 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 pport shared-cac
1ab94 68 65 20 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a he mode, this.**
1ab95 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
1ab96 6f 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 o-op..*/.#ifndef
1ab97 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1ab98 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 RED_CACHE.SQLITE
1ab99 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1ab9a 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 lite3VdbeMutexAr
1ab9b 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 rayEnter(Vdbe *p
1ab9c 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 ){.#if SQLITE_TH
1ab9d 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 READSAFE. sqlit
1ab9e 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
1ab9f 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 yEnter(&p->aMute
1aba0 78 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 x);.#else. sqli
1aba1 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
1aba2 28 70 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a (p->db);.#endif.
1aba3 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1aba4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
1aba5 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20 called the when
1aba6 61 20 56 44 42 45 20 74 72 69 65 73 20 74 6f 20 a VDBE tries to
1aba7 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44 halt. If the VD
1aba8 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 BE.** has made c
1aba9 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e hanges and is in
1abaa 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 autocommit mode
1abab 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 , then commit th
1abac 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 ose.** changes.
1abad 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 If a rollback i
1abae 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 s needed, then d
1abaf 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a o the rollback..
1abb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1abb1 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 ne is the only w
1abb2 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 ay to move the s
1abb3 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f tate of a VM fro
1abb4 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 m.** SQLITE_MAGI
1abb5 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f C_RUN to SQLITE_
1abb6 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 MAGIC_HALT. It
1abb7 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a is harmless to.*
1abb8 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 * call this on a
1abb9 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74 VM that is in t
1abba 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f he SQLITE_MAGIC_
1abbb 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a HALT state..**.*
1abbc 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f * Return an erro
1abbd 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20 r code. If the
1abbe 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 commit could not
1abbf 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73 complete becaus
1abc0 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e e of.** lock con
1abc1 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 tention, return
1abc2 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 SQLITE_BUSY. If
1abc3 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
1abc4 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 returned, it.**
1abc5 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 means the close
1abc6 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 did not happen a
1abc7 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 nd needs to be r
1abc8 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 epeated..*/.SQLI
1abc9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1abca 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 qlite3VdbeHalt(V
1abcb 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 dbe *p){. int r
1abcc 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
1abcd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
1abce 65 64 20 74 6f 20 73 74 6f 72 65 20 74 72 61 6e ed to store tran
1abcf 73 69 65 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 sient return cod
1abd0 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 es */. sqlite3
1abd1 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 *db = p->db;..
1abd2 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
1abd3 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f contains the lo
1abd4 67 69 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 gic that determi
1abd5 6e 65 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 nes if a stateme
1abd6 6e 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 nt or. ** trans
1abd7 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 action will be c
1abd8 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c ommitted or roll
1abd9 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 ed back as a res
1abda 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 ult of the. **
1abdb 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 execution of thi
1abdc 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e s virtual machin
1abdd 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 e. . **. ** If
1abde 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c any of the foll
1abdf 6f 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 owing errors occ
1abe0 75 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 ur:. **. **
1abe1 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 SQLITE_NOMEM.
1abe2 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 ** SQLITE_I
1abe3 4f 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 OERR. ** SQ
1abe4 4c 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 LITE_FULL. **
1abe5 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 SQLITE_INTERR
1abe6 55 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 UPT. **. ** Th
1abe7 65 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 en the internal
1abe8 63 61 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 cache might have
1abe9 20 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e been left in an
1abea 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 inconsistent.
1abeb 2a 2a 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 ** state. We ne
1abec 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 ed to rollback t
1abed 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 he statement tra
1abee 6e 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 nsaction, if the
1abef 72 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 re is. ** one,
1abf0 6f 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 or the complete
1abf1 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 transaction if t
1abf2 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 here is no state
1abf3 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1abf4 2e 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d .. */.. if( p-
1abf5 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1abf6 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d d ){. p->rc =
1abf7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1abf8 20 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 }. closeAllCur
1abf9 73 6f 72 73 28 70 29 3b 0a 20 20 69 66 28 20 70 sors(p);. if( p
1abfa 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 ->magic!=VDBE_MA
1abfb 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 GIC_RUN ){. r
1abfc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1abfd 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 . }. checkActi
1abfe 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a veVdbeCnt(db);..
1abff 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f /* No commit o
1ac00 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 r rollback neede
1ac01 64 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d d if the program
1ac02 20 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a never started *
1ac03 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 /. if( p->pc>=0
1ac04 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b ){. int mrc;
1ac05 20 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 /* Primary er
1ac06 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d ror code from p-
1ac07 3e 72 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 >rc */. int e
1ac08 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b StatementOp = 0;
1ac09 0a 20 20 20 20 69 6e 74 20 69 73 53 70 65 63 69 . int isSpeci
1ac0a 61 6c 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 alError;
1ac0b 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 /* Set to tr
1ac0c 75 65 20 69 66 20 61 20 27 73 70 65 63 69 61 6c ue if a 'special
1ac0d 27 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 ' error */..
1ac0e 2f 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 /* Lock all btre
1ac0f 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 es used by the s
1ac10 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 tatement */.
1ac11 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 sqlite3VdbeMutex
1ac12 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a ArrayEnter(p);..
1ac13 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 /* Check for
1ac14 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 one of the spec
1ac15 69 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 ial errors */.
1ac16 20 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 mrc = p->rc &
1ac17 30 78 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74 0xff;. assert
1ac18 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f ( p->rc!=SQLITE_
1ac19 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b IOERR_BLOCKED );
1ac1a 20 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20 /* This error
1ac1b 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 no longer exists
1ac1c 20 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61 */. isSpecia
1ac1d 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 lError = mrc==SQ
1ac1e 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 LITE_NOMEM || mr
1ac1f 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a c==SQLITE_IOERR.
1ac20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ac21 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c || mrc==SQL
1ac22 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c ITE_INTERRUPT ||
1ac23 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c mrc==SQLITE_FUL
1ac24 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 65 L;. if( isSpe
1ac25 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 cialError ){.
1ac26 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 /* If the que
1ac27 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 ry was read-only
1ac28 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 , we need do no
1ac29 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e rollback at all.
1ac2a 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20 Otherwise,.
1ac2b 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74 ** proceed wit
1ac2c 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 68 61 h the special ha
1ac2d 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f ndling.. */
1ac2e 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72 . if( !p->r
1ac2f 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d eadOnly || mrc!=
1ac30 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
1ac31 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1ac32 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d (mrc==SQLITE_NOM
1ac33 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 EM || mrc==SQLIT
1ac34 45 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 E_FULL) && p->us
1ac35 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b esStmtJournal ){
1ac36 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 . eStat
1ac37 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f ementOp = SAVEPO
1ac38 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 INT_ROLLBACK;.
1ac39 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1ac3a 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 /* We are
1ac3b 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 forced to roll
1ac3c 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 back the active
1ac3d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 transaction. Bef
1ac3e 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 ore doing.
1ac3f 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 ** so, abort
1ac40 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 any other state
1ac41 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c ments this handl
1ac42 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 e currently has
1ac43 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 active..
1ac44 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 */. i
1ac45 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 nvalidateCursors
1ac46 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 OnModifiedBtrees
1ac47 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (db);.
1ac48 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 sqlite3RollbackA
1ac49 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 ll(db);.
1ac4a 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 sqlite3CloseSa
1ac4b 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 vepoints(db);.
1ac4c 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f db->auto
1ac4d 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 Commit = 1;.
1ac4e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1ac4f 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
1ac50 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 the auto-commit
1ac51 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 flag is set and
1ac52 20 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c this is the onl
1ac53 79 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20 y active writer
1ac54 0a 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e . ** VM, then
1ac55 20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 we do either a
1ac56 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 commit or rollba
1ac57 63 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ck of the curren
1ac58 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a t transaction. .
1ac59 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f **. ** No
1ac5a 74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 te: This block a
1ac5b 6c 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 lso runs if one
1ac5c 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 of the special e
1ac5d 72 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 rrors handled .
1ac5e 20 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 ** above has
1ac5f 6f 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a occurred. . *
1ac60 2f 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 /. if( !sqlit
1ac61 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 e3VtabInSync(db)
1ac62 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 . && db->au
1ac63 74 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 toCommit . &
1ac64 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 & db->writeVdbeC
1ac65 6e 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 nt==(p->readOnly
1ac66 3d 3d 30 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 ==0) . ){.
1ac67 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 if( p->rc==SQ
1ac68 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 2d 3e 65 LITE_OK || (p->e
1ac69 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 rrorAction==OE_F
1ac6a 61 69 6c 20 26 26 20 21 69 73 53 70 65 63 69 61 ail && !isSpecia
1ac6b 6c 45 72 72 6f 72 29 20 29 7b 0a 20 20 20 20 20 lError) ){.
1ac6c 20 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 /* The auto-c
1ac6d 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 74 72 ommit flag is tr
1ac6e 75 65 2c 20 61 6e 64 20 74 68 65 20 76 64 62 65 ue, and the vdbe
1ac6f 20 70 72 6f 67 72 61 6d 20 77 61 73 20 0a 20 20 program was .
1ac70 20 20 20 20 20 20 2a 2a 20 73 75 63 63 65 73 73 ** success
1ac71 66 75 6c 20 6f 72 20 68 69 74 20 61 6e 20 27 4f ful or hit an 'O
1ac72 52 20 46 41 49 4c 27 20 63 6f 6e 73 74 72 61 69 R FAIL' constrai
1ac73 6e 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61 nt. This means a
1ac74 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20 commit .
1ac75 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e ** is required.
1ac76 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
1ac77 20 20 20 20 72 63 20 3d 20 76 64 62 65 43 6f 6d rc = vdbeCom
1ac78 6d 69 74 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 mit(db, p);.
1ac79 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1ac7a 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 TE_BUSY ){.
1ac7b 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1ac7c 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
1ac7d 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 (&p->aMutex);.
1ac7e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
1ac7f 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
1ac80 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 }else if( rc
1ac81 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1ac82 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d p->rc =
1ac83 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 rc;. s
1ac84 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c qlite3RollbackAl
1ac85 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 7d l(db);. }
1ac86 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1ac87 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 sqlite3CommitInt
1ac88 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29 ernalChanges(db)
1ac89 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1ac8a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1ac8b 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b sqlite3Rollback
1ac8c 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 7d All(db);. }
1ac8d 0a 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 . db->nStat
1ac8e 65 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 7d ement = 0;. }
1ac8f 65 6c 73 65 20 69 66 28 20 65 53 74 61 74 65 6d else if( eStatem
1ac90 65 6e 74 4f 70 3d 3d 30 20 29 7b 0a 20 20 20 20 entOp==0 ){.
1ac91 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c if( p->rc==SQL
1ac92 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 65 72 72 ITE_OK || p->err
1ac93 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 orAction==OE_Fai
1ac94 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 l ){. eSt
1ac95 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 atementOp = SAVE
1ac96 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 3b 0a 20 POINT_RELEASE;.
1ac97 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
1ac98 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f ->errorAction==O
1ac99 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 E_Abort ){.
1ac9a 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 eStatementOp
1ac9b 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c = SAVEPOINT_ROLL
1ac9c 42 41 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 BACK;. }els
1ac9d 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61 6c e{. inval
1ac9e 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f idateCursorsOnMo
1ac9f 64 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29 difiedBtrees(db)
1aca0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1aca1 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3RollbackAll(db)
1aca2 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1aca3 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 3CloseSavepoints
1aca4 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 64 62 (db);. db
1aca5 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 ->autoCommit = 1
1aca6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1aca7 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 65 53 74 . /* If eSt
1aca8 61 74 65 6d 65 6e 74 4f 70 20 69 73 20 6e 6f 6e atementOp is non
1aca9 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 73 74 -zero, then a st
1acaa 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1acab 69 6f 6e 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 ion needs to.
1acac 20 2a 2a 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 ** be committed
1acad 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e or rolled back.
1acae 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 Call sqlite3Vdb
1acaf 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 eCloseStatement(
1acb0 29 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f 20 73 ) to. ** do s
1acb1 6f 2e 20 49 66 20 74 68 69 73 20 6f 70 65 72 61 o. If this opera
1acb2 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 tion returns an
1acb3 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 65 20 63 error, and the c
1acb4 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 urrent statement
1acb5 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 63 6f . ** error co
1acb6 64 65 20 69 73 20 53 51 4c 49 54 45 5f 4f 4b 20 de is SQLITE_OK
1acb7 6f 72 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 or SQLITE_CONSTR
1acb8 41 49 4e 54 2c 20 74 68 65 6e 20 73 65 74 20 74 AINT, then set t
1acb9 68 65 20 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20 he error. **
1acba 63 6f 64 65 20 74 6f 20 74 68 65 20 6e 65 77 20 code to the new
1acbb 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 value.. */.
1acbc 20 20 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 if( eStatement
1acbd 4f 70 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d Op ){. rc =
1acbe 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 sqlite3VdbeClos
1acbf 65 53 74 61 74 65 6d 65 6e 74 28 70 2c 20 65 53 eStatement(p, eS
1acc0 74 61 74 65 6d 65 6e 74 4f 70 29 3b 0a 20 20 20 tatementOp);.
1acc1 20 20 20 69 66 28 20 72 63 20 26 26 20 28 70 2d if( rc && (p-
1acc2 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c >rc==SQLITE_OK |
1acc3 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f | p->rc==SQLITE_
1acc4 43 4f 4e 53 54 52 41 49 4e 54 29 20 29 7b 0a 20 CONSTRAINT) ){.
1acc5 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 p->rc = r
1acc6 63 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 c;. sqlit
1acc7 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1acc8 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 zErrMsg);.
1acc9 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 p->zErrMsg = 0
1acca 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1accb 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 . /* If thi
1accc 73 20 77 61 73 20 61 6e 20 49 4e 53 45 52 54 2c s was an INSERT,
1accd 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 UPDATE or DELET
1acce 45 20 61 6e 64 20 6e 6f 20 73 74 61 74 65 6d 65 E and no stateme
1accf 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 nt transaction.
1acd0 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 ** has been r
1acd1 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 75 70 64 61 olled back, upda
1acd2 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 te the database
1acd3 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 68 61 6e 67 connection chang
1acd4 65 2d 63 6f 75 6e 74 65 72 2e 20 0a 20 20 20 20 e-counter. .
1acd5 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 68 */. if( p->ch
1acd6 61 6e 67 65 43 6e 74 4f 6e 20 29 7b 0a 20 20 20 angeCntOn ){.
1acd7 20 20 20 69 66 28 20 65 53 74 61 74 65 6d 65 6e if( eStatemen
1acd8 74 4f 70 21 3d 53 41 56 45 50 4f 49 4e 54 5f 52 tOp!=SAVEPOINT_R
1acd9 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 OLLBACK ){.
1acda 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 sqlite3VdbeSe
1acdb 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e tChanges(db, p->
1acdc 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 nChange);.
1acdd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
1acde 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 qlite3VdbeSetCha
1acdf 6e 67 65 73 28 64 62 2c 20 30 29 3b 0a 20 20 20 nges(db, 0);.
1ace0 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 6e 43 }. p->nC
1ace1 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d hange = 0;. }
1ace2 0a 20 20 0a 20 20 20 20 2f 2a 20 52 6f 6c 6c 62 . . /* Rollb
1ace3 61 63 6b 20 6f 72 20 63 6f 6d 6d 69 74 20 61 6e ack or commit an
1ace4 79 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 y schema changes
1ace5 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 2e 20 that occurred.
1ace6 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 */. if( p->rc
1ace7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 !=SQLITE_OK && d
1ace8 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f b->flags&SQLITE_
1ace9 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 20 29 7b InternChanges ){
1acea 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 . sqlite3Re
1aceb 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d setInternalSchem
1acec 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 a(db, 0);.
1aced 64 62 2d 3e 66 6c 61 67 73 20 3d 20 28 64 62 2d db->flags = (db-
1acee 3e 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f >flags | SQLITE_
1acef 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 3b 0a InternChanges);.
1acf0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 }.. /* Re
1acf1 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 73 20 lease the locks
1acf2 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 */. sqlite3Bt
1acf3 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 reeMutexArrayLea
1acf4 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a ve(&p->aMutex);.
1acf5 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 }.. /* We hav
1acf6 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 68 e successfully h
1acf7 61 6c 74 65 64 20 61 6e 64 20 63 6c 6f 73 65 64 alted and closed
1acf8 20 74 68 65 20 56 4d 2e 20 20 52 65 63 6f 72 64 the VM. Record
1acf9 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 this fact. */.
1acfa 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b if( p->pc>=0 ){
1acfb 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56 . db->activeV
1acfc 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 dbeCnt--;. if
1acfd 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 ( !p->readOnly )
1acfe 7b 0a 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74 {. db->writ
1acff 65 56 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 eVdbeCnt--;.
1ad00 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 }. assert( db
1ad01 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
1ad02 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e =db->writeVdbeCn
1ad03 74 20 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d 61 t );. }. p->ma
1ad04 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 gic = VDBE_MAGIC
1ad05 5f 48 41 4c 54 3b 0a 20 20 63 68 65 63 6b 41 63 _HALT;. checkAc
1ad06 74 69 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b tiveVdbeCnt(db);
1ad07 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 . if( p->db->ma
1ad08 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1ad09 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 p->rc = SQLITE
1ad0a 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f _NOMEM;. }.. /
1ad0b 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f * If the auto-co
1ad0c 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 mmit flag is set
1ad0d 20 74 6f 20 74 72 75 65 2c 20 74 68 65 6e 20 61 to true, then a
1ad0e 6e 79 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 ny locks that we
1ad0f 72 65 20 68 65 6c 64 0a 20 20 2a 2a 20 62 79 20 re held. ** by
1ad10 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 connection db ha
1ad11 76 65 20 6e 6f 77 20 62 65 65 6e 20 72 65 6c 65 ve now been rele
1ad12 61 73 65 64 2e 20 43 61 6c 6c 20 73 71 6c 69 74 ased. Call sqlit
1ad13 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f e3ConnectionUnlo
1ad14 63 6b 65 64 28 29 20 0a 20 20 2a 2a 20 74 6f 20 cked() . ** to
1ad15 69 6e 76 6f 6b 65 20 61 6e 79 20 72 65 71 75 69 invoke any requi
1ad16 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 red unlock-notif
1ad17 79 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a y callbacks.. *
1ad18 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f /. if( db->auto
1ad19 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 Commit ){. sq
1ad1a 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 lite3ConnectionU
1ad1b 6e 6c 6f 63 6b 65 64 28 64 62 29 3b 0a 20 20 7d nlocked(db);. }
1ad1c 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e .. assert( db->
1ad1d 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20 activeVdbeCnt>0
1ad1e 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 || db->autoCommi
1ad1f 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6e 53 74 61 t==0 || db->nSta
1ad20 74 65 6d 65 6e 74 3d 3d 30 20 29 3b 0a 20 20 72 tement==0 );. r
1ad21 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1ad22 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 .}.../*.** Each
1ad23 56 44 42 45 20 68 6f 6c 64 73 20 74 68 65 20 72 VDBE holds the r
1ad24 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6d 6f 73 esult of the mos
1ad25 74 20 72 65 63 65 6e 74 20 73 71 6c 69 74 65 33 t recent sqlite3
1ad26 5f 73 74 65 70 28 29 20 63 61 6c 6c 0a 2a 2a 20 _step() call.**
1ad27 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68 69 73 20 in p->rc. This
1ad28 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 61 routine sets tha
1ad29 74 20 72 65 73 75 6c 74 20 62 61 63 6b 20 74 6f t result back to
1ad2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 SQLITE_OK..*/.S
1ad2b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1ad2c 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 id sqlite3VdbeRe
1ad2d 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 64 setStepResult(Vd
1ad2e 62 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 72 63 20 be *p){. p->rc
1ad2f 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a = SQLITE_OK;.}..
1ad30 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 /*.** Clean up a
1ad31 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63 VDBE after exec
1ad32 75 74 69 6f 6e 20 62 75 74 20 64 6f 20 6e 6f 74 ution but do not
1ad33 20 64 65 6c 65 74 65 20 74 68 65 20 56 44 42 45 delete the VDBE
1ad34 20 6a 75 73 74 20 79 65 74 2e 0a 2a 2a 20 57 72 just yet..** Wr
1ad35 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 ite any error me
1ad36 73 73 61 67 65 73 20 69 6e 74 6f 20 2a 70 7a 45 ssages into *pzE
1ad37 72 72 4d 73 67 2e 20 20 52 65 74 75 72 6e 20 74 rrMsg. Return t
1ad38 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a he result code..
1ad39 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73 **.** After this
1ad3a 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e 2c routine is run,
1ad3b 20 74 68 65 20 56 44 42 45 20 73 68 6f 75 6c 64 the VDBE should
1ad3c 20 62 65 20 72 65 61 64 79 20 74 6f 20 62 65 20 be ready to be
1ad3d 65 78 65 63 75 74 65 64 0a 2a 2a 20 61 67 61 69 executed.** agai
1ad3e 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b n..**.** To look
1ad3f 20 61 74 20 69 74 20 61 6e 6f 74 68 65 72 20 77 at it another w
1ad40 61 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ay, this routine
1ad41 20 72 65 73 65 74 73 20 74 68 65 20 73 74 61 74 resets the stat
1ad42 65 20 6f 66 20 74 68 65 0a 2a 2a 20 76 69 72 74 e of the.** virt
1ad43 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 72 6f 6d ual machine from
1ad44 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 VDBE_MAGIC_RUN
1ad45 6f 72 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 or VDBE_MAGIC_HA
1ad46 4c 54 20 62 61 63 6b 20 74 6f 0a 2a 2a 20 56 44 LT back to.** VD
1ad47 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 2e 0a 2a BE_MAGIC_INIT..*
1ad48 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ad49 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1ad4a 52 65 73 65 74 28 56 64 62 65 20 2a 70 29 7b 0a Reset(Vdbe *p){.
1ad4b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
1ad4c 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 db = p->db;..
1ad4d 2f 2a 20 49 66 20 74 68 65 20 56 4d 20 64 69 64 /* If the VM did
1ad4e 20 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f 6d 70 not run to comp
1ad4f 6c 65 74 69 6f 6e 20 6f 72 20 69 66 20 69 74 20 letion or if it
1ad50 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 6e 0a 20 encountered an.
1ad51 20 2a 2a 20 65 72 72 6f 72 2c 20 74 68 65 6e 20 ** error, then
1ad52 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 it might not hav
1ad53 65 20 62 65 65 6e 20 68 61 6c 74 65 64 20 70 72 e been halted pr
1ad54 6f 70 65 72 6c 79 2e 20 20 53 6f 20 68 61 6c 74 operly. So halt
1ad55 0a 20 20 2a 2a 20 69 74 20 6e 6f 77 2e 0a 20 20 . ** it now..
1ad56 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 */. (void)sqlit
1ad57 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a e3SafetyOn(db);.
1ad58 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c sqlite3VdbeHal
1ad59 74 28 70 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 t(p);. (void)sq
1ad5a 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1ad5b 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 b);.. /* If the
1ad5c 20 56 44 42 45 20 68 61 73 20 62 65 20 72 75 6e VDBE has be run
1ad5d 20 65 76 65 6e 20 70 61 72 74 69 61 6c 6c 79 2c even partially,
1ad5e 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 74 then transfer t
1ad5f 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0a 20 20 he error code.
1ad60 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 ** and error mes
1ad61 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 56 44 sage from the VD
1ad62 42 45 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e BE into the main
1ad63 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 database struct
1ad64 75 72 65 2e 20 20 42 75 74 0a 20 20 2a 2a 20 69 ure. But. ** i
1ad65 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20 6a f the VDBE has j
1ad66 75 73 74 20 62 65 65 6e 20 73 65 74 20 74 6f 20 ust been set to
1ad67 72 75 6e 20 62 75 74 20 68 61 73 20 6e 6f 74 20 run but has not
1ad68 61 63 74 75 61 6c 6c 79 20 65 78 65 63 75 74 65 actually execute
1ad69 64 20 61 6e 79 0a 20 20 2a 2a 20 69 6e 73 74 72 d any. ** instr
1ad6a 75 63 74 69 6f 6e 73 20 79 65 74 2c 20 6c 65 61 uctions yet, lea
1ad6b 76 65 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 ve the main data
1ad6c 62 61 73 65 20 65 72 72 6f 72 20 69 6e 66 6f 72 base error infor
1ad6d 6d 61 74 69 6f 6e 20 75 6e 63 68 61 6e 67 65 64 mation unchanged
1ad6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
1ad6f 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 pc>=0 ){. if(
1ad70 20 70 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 p->zErrMsg ){.
1ad71 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 sqlite3Begi
1ad72 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
1ad73 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 . sqlite3Va
1ad74 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 lueSetStr(db->pE
1ad75 72 72 2c 2d 31 2c 70 2d 3e 7a 45 72 72 4d 73 67 rr,-1,p->zErrMsg
1ad76 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c ,SQLITE_UTF8,SQL
1ad77 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
1ad78 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 sqlite3End
1ad79 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
1ad7a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 db->errCod
1ad7b 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20 20 e = p->rc;.
1ad7c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1ad7d 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, p->zErrMsg);.
1ad7e 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 p->zErrMsg
1ad7f 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
1ad80 69 66 28 20 70 2d 3e 72 63 20 29 7b 0a 20 20 20 if( p->rc ){.
1ad81 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 sqlite3Error(
1ad82 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 db, p->rc, 0);.
1ad83 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1ad84 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c sqlite3Error(db,
1ad85 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a SQLITE_OK, 0);.
1ad86 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1ad87 28 20 70 2d 3e 72 63 20 26 26 20 70 2d 3e 65 78 ( p->rc && p->ex
1ad88 70 69 72 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 pired ){. /*
1ad89 54 68 65 20 65 78 70 69 72 65 64 20 66 6c 61 67 The expired flag
1ad8a 20 77 61 73 20 73 65 74 20 6f 6e 20 74 68 65 20 was set on the
1ad8b 56 44 42 45 20 62 65 66 6f 72 65 20 74 68 65 20 VDBE before the
1ad8c 66 69 72 73 74 20 63 61 6c 6c 0a 20 20 20 20 2a first call. *
1ad8d 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 74 65 * to sqlite3_ste
1ad8e 70 28 29 2e 20 46 6f 72 20 63 6f 6e 73 69 73 74 p(). For consist
1ad8f 65 6e 63 79 20 28 73 69 6e 63 65 20 73 71 6c 69 ency (since sqli
1ad90 74 65 33 5f 73 74 65 70 28 29 20 77 61 73 0a 20 te3_step() was.
1ad91 20 20 20 2a 2a 20 63 61 6c 6c 65 64 29 2c 20 73 ** called), s
1ad92 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 et the database
1ad93 65 72 72 6f 72 20 69 6e 20 74 68 69 73 20 63 61 error in this ca
1ad94 73 65 20 61 73 20 77 65 6c 6c 2e 0a 20 20 20 20 se as well..
1ad95 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 */. sqlite3Er
1ad96 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c 20 30 ror(db, p->rc, 0
1ad97 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 );. sqlite3Va
1ad98 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 lueSetStr(db->pE
1ad99 72 72 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 4d rr, -1, p->zErrM
1ad9a 73 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c sg, SQLITE_UTF8,
1ad9b 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
1ad9c 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 T);. sqlite3D
1ad9d 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
1ad9e 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 rMsg);. p->zE
1ad9f 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a rrMsg = 0;. }..
1ada0 20 20 2f 2a 20 52 65 63 6c 61 69 6d 20 61 6c 6c /* Reclaim all
1ada1 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 memory used by
1ada2 74 68 65 20 56 44 42 45 0a 20 20 2a 2f 0a 20 20 the VDBE. */.
1ada3 43 6c 65 61 6e 75 70 28 70 29 3b 0a 0a 20 20 2f Cleanup(p);.. /
1ada4 2a 20 53 61 76 65 20 70 72 6f 66 69 6c 69 6e 67 * Save profiling
1ada5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
1ada6 6d 20 74 68 69 73 20 56 44 42 45 20 72 75 6e 2e m this VDBE run.
1ada7 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 42 . */.#ifdef VDB
1ada8 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 E_PROFILE. {.
1ada9 20 20 46 49 4c 45 20 2a 6f 75 74 20 3d 20 66 6f FILE *out = fo
1adaa 70 65 6e 28 22 76 64 62 65 5f 70 72 6f 66 69 6c pen("vdbe_profil
1adab 65 2e 6f 75 74 22 2c 20 22 61 22 29 3b 0a 20 20 e.out", "a");.
1adac 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 if( out ){.
1adad 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
1adae 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2d 2d fprintf(out, "--
1adaf 2d 2d 20 22 29 3b 0a 20 20 20 20 20 20 66 6f 72 -- ");. for
1adb0 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 (i=0; i<p->nOp;
1adb1 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 70 i++){. fp
1adb2 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 30 32 78 rintf(out, "%02x
1adb3 22 2c 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 ", p->aOp[i].opc
1adb4 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ode);. }.
1adb5 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
1adb6 20 22 5c 6e 22 29 3b 0a 20 20 20 20 20 20 66 6f "\n");. fo
1adb7 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
1adb8 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 i++){. f
1adb9 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 36 64 printf(out, "%6d
1adba 20 25 31 30 6c 6c 64 20 25 38 6c 6c 64 20 22 2c %10lld %8lld ",
1adbb 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 . p->a
1adbc 4f 70 5b 69 5d 2e 63 6e 74 2c 0a 20 20 20 20 20 Op[i].cnt,.
1adbd 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e p->aOp[i].
1adbe 63 79 63 6c 65 73 2c 0a 20 20 20 20 20 20 20 20 cycles,.
1adbf 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 p->aOp[i].cnt
1adc0 3e 30 20 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 >0 ? p->aOp[i].c
1adc1 79 63 6c 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d 2e ycles/p->aOp[i].
1adc2 63 6e 74 20 3a 20 30 0a 20 20 20 20 20 20 20 20 cnt : 0.
1adc3 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1adc4 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 6f 75 e3VdbePrintOp(ou
1adc5 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d t, i, &p->aOp[i]
1adc6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1adc7 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 fclose(out);.
1adc8 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
1adc9 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 p->magic = VDBE
1adca 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 _MAGIC_INIT;. r
1adcb 65 74 75 72 6e 20 70 2d 3e 72 63 20 26 20 64 62 eturn p->rc & db
1adcc 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f ->errMask;.}. ./
1adcd 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e *.** Clean up an
1adce 64 20 64 65 6c 65 74 65 20 61 20 56 44 42 45 20 d delete a VDBE
1adcf 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 2e after execution.
1add0 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 Return an inte
1add1 67 65 72 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 ger which is.**
1add2 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e the result code.
1add3 20 20 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f Write any erro
1add4 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 69 r message text i
1add5 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a nto *pzErrMsg..*
1add6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1add7 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1add8 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 20 2a 70 Finalize(Vdbe *p
1add9 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1adda 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 LITE_OK;. if( p
1addb 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
1addc 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d 61 GIC_RUN || p->ma
1addd 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
1adde 48 41 4c 54 20 29 7b 0a 20 20 20 20 72 63 20 3d HALT ){. rc =
1addf 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 sqlite3VdbeRese
1ade0 74 28 70 29 3b 0a 20 20 20 20 61 73 73 65 72 74 t(p);. assert
1ade1 28 20 28 72 63 20 26 20 70 2d 3e 64 62 2d 3e 65 ( (rc & p->db->e
1ade2 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 rrMask)==rc );.
1ade3 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 }. sqlite3Vdbe
1ade4 44 65 6c 65 74 65 28 70 29 3b 0a 20 20 72 65 74 Delete(p);. ret
1ade5 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1ade6 20 43 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 Call the destru
1ade7 63 74 6f 72 20 66 6f 72 20 65 61 63 68 20 61 75 ctor for each au
1ade8 78 64 61 74 61 20 65 6e 74 72 79 20 69 6e 20 70 xdata entry in p
1ade9 56 64 62 65 46 75 6e 63 20 66 6f 72 20 77 68 69 VdbeFunc for whi
1adea 63 68 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 ch.** the corres
1adeb 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 6e 20 6d ponding bit in m
1adec 61 73 6b 20 69 73 20 63 6c 65 61 72 2e 20 20 41 ask is clear. A
1aded 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 20 62 uxdata entries b
1adee 65 79 6f 6e 64 20 33 31 0a 2a 2a 20 61 72 65 20 eyond 31.** are
1adef 61 6c 77 61 79 73 20 64 65 73 74 72 6f 79 65 64 always destroyed
1adf0 2e 20 20 54 6f 20 64 65 73 74 72 6f 79 20 61 6c . To destroy al
1adf1 6c 20 61 75 78 64 61 74 61 20 65 6e 74 72 69 65 l auxdata entrie
1adf2 73 2c 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 s, call this.**
1adf3 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6d 61 73 routine with mas
1adf4 6b 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f k==0..*/.SQLITE_
1adf5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1adf6 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 ite3VdbeDeleteAu
1adf7 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 20 2a xData(VdbeFunc *
1adf8 70 56 64 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d pVdbeFunc, int m
1adf9 61 73 6b 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 ask){. int i;.
1adfa 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 56 64 62 for(i=0; i<pVdb
1adfb 65 46 75 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b eFunc->nAux; i++
1adfc 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 41 75 ){. struct Au
1adfd 78 44 61 74 61 20 2a 70 41 75 78 20 3d 20 26 70 xData *pAux = &p
1adfe 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b VdbeFunc->apAux[
1adff 69 5d 3b 0a 20 20 20 20 69 66 28 20 28 69 3e 33 i];. if( (i>3
1ae00 31 20 7c 7c 20 21 28 6d 61 73 6b 26 28 28 28 75 1 || !(mask&(((u
1ae01 33 32 29 31 29 3c 3c 69 29 29 29 20 26 26 20 70 32)1)<<i))) && p
1ae02 41 75 78 2d 3e 70 41 75 78 20 29 7b 0a 20 20 20 Aux->pAux ){.
1ae03 20 20 20 69 66 28 20 70 41 75 78 2d 3e 78 44 65 if( pAux->xDe
1ae04 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 lete ){.
1ae05 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 28 70 41 pAux->xDelete(pA
1ae06 75 78 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 20 ux->pAux);.
1ae07 20 7d 0a 20 20 20 20 20 20 70 41 75 78 2d 3e 70 }. pAux->p
1ae08 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 Aux = 0;. }.
1ae09 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 }.}../*.** Dele
1ae0a 74 65 20 61 6e 20 65 6e 74 69 72 65 20 56 44 42 te an entire VDB
1ae0b 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 E..*/.SQLITE_PRI
1ae0c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1ae0d 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 3VdbeDelete(Vdbe
1ae0e 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 *p){. sqlite3
1ae0f 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 *db;.. if( NEVE
1ae10 52 28 70 3d 3d 30 29 20 29 20 72 65 74 75 72 6e R(p==0) ) return
1ae11 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a ;. db = p->db;.
1ae12 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29 if( p->pPrev )
1ae13 7b 0a 20 20 20 20 70 2d 3e 70 50 72 65 76 2d 3e {. p->pPrev->
1ae14 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 pNext = p->pNext
1ae15 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
1ae16 73 73 65 72 74 28 20 64 62 2d 3e 70 56 64 62 65 ssert( db->pVdbe
1ae17 3d 3d 70 20 29 3b 0a 20 20 20 20 64 62 2d 3e 70 ==p );. db->p
1ae18 56 64 62 65 20 3d 20 70 2d 3e 70 4e 65 78 74 3b Vdbe = p->pNext;
1ae19 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4e . }. if( p->pN
1ae1a 65 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4e ext ){. p->pN
1ae1b 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e ext->pPrev = p->
1ae1c 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 72 65 6c pPrev;. }. rel
1ae1d 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e easeMemArray(p->
1ae1e 61 56 61 72 2c 20 70 2d 3e 6e 56 61 72 29 3b 0a aVar, p->nVar);.
1ae1f 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 releaseMemArra
1ae20 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 y(p->aColName, p
1ae21 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c ->nResColumn*COL
1ae22 4e 41 4d 45 5f 4e 29 3b 0a 20 20 76 64 62 65 46 NAME_N);. vdbeF
1ae23 72 65 65 4f 70 41 72 72 61 79 28 64 62 2c 20 70 reeOpArray(db, p
1ae24 2d 3e 61 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a ->aOp, p->nOp);.
1ae25 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ae26 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a db, p->aLabel);.
1ae27 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1ae28 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 db, p->aColName)
1ae29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1ae2a 65 28 64 62 2c 20 70 2d 3e 7a 53 71 6c 29 3b 0a e(db, p->zSql);.
1ae2b 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
1ae2c 45 5f 4d 41 47 49 43 5f 44 45 41 44 3b 0a 20 20 E_MAGIC_DEAD;.
1ae2d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1ae2e 2c 20 70 2d 3e 70 46 72 65 65 29 3b 0a 20 20 73 , p->pFree);. s
1ae2f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ae30 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 p);.}../*.** Ma
1ae31 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72 73 ke sure the curs
1ae32 6f 72 20 70 20 69 73 20 72 65 61 64 79 20 74 6f or p is ready to
1ae33 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 read or write t
1ae34 68 65 20 72 6f 77 20 74 6f 20 77 68 69 63 68 20 he row to which
1ae35 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 it.** was last p
1ae36 6f 73 69 74 69 6f 6e 65 64 2e 20 20 52 65 74 75 ositioned. Retu
1ae37 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 rn an error code
1ae38 20 69 66 20 61 6e 20 4f 4f 4d 20 66 61 75 6c 74 if an OOM fault
1ae39 20 6f 72 20 49 2f 4f 20 65 72 72 6f 72 0a 2a 2a or I/O error.**
1ae3a 20 70 72 65 76 65 6e 74 73 20 75 73 20 66 72 6f prevents us fro
1ae3b 6d 20 70 6f 73 69 74 69 6f 6e 69 6e 67 20 74 68 m positioning th
1ae3c 65 20 63 75 72 73 6f 72 20 74 6f 20 69 74 73 20 e cursor to its
1ae3d 63 6f 72 72 65 63 74 20 70 6f 73 69 74 69 6f 6e correct position
1ae3e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 ..**.** If a Mov
1ae3f 65 54 6f 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 eTo operation is
1ae40 20 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 pending on the
1ae41 67 69 76 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 given cursor, th
1ae42 65 6e 20 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f en do that.** Mo
1ae43 76 65 54 6f 20 6e 6f 77 2e 20 20 49 66 20 6e 6f veTo now. If no
1ae44 20 6d 6f 76 65 20 69 73 20 70 65 6e 64 69 6e 67 move is pending
1ae45 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 , check to see i
1ae46 66 20 74 68 65 20 72 6f 77 20 68 61 73 20 62 65 f the row has be
1ae47 65 6e 0a 2a 2a 20 64 65 6c 65 74 65 64 20 6f 75 en.** deleted ou
1ae48 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 t from under the
1ae49 20 63 75 72 73 6f 72 20 61 6e 64 20 69 66 20 69 cursor and if i
1ae4a 74 20 68 61 73 2c 20 6d 61 72 6b 20 74 68 65 20 t has, mark the
1ae4b 72 6f 77 20 61 73 0a 2a 2a 20 61 20 4e 55 4c 4c row as.** a NULL
1ae4c 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 row..**.** If t
1ae4d 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 he cursor is alr
1ae4e 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f eady pointing to
1ae4f 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 6f 77 the correct row
1ae50 20 61 6e 64 20 74 68 61 74 20 72 6f 77 20 68 61 and that row ha
1ae51 73 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20 64 65 s.** not been de
1ae52 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 leted out from u
1ae53 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 2c nder the cursor,
1ae54 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
1ae55 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
1ae56 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ae57 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1ae58 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 CursorMoveto(Vdb
1ae59 65 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 eCursor *p){. i
1ae5a 66 28 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f f( p->deferredMo
1ae5b 76 65 74 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 veto ){. int
1ae5c 72 65 73 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 res, rc;.#ifdef
1ae5d 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 SQLITE_TEST.
1ae5e 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 extern int sqlit
1ae5f 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b e3_search_count;
1ae60 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 .#endif. asse
1ae61 72 74 28 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 rt( p->isTable )
1ae62 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1ae63 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1ae64 61 63 6b 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 acked(p->pCursor
1ae65 2c 20 30 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 , 0, p->movetoTa
1ae66 72 67 65 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a rget, 0, &res);.
1ae67 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
1ae68 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c urn rc;. p->l
1ae69 61 73 74 52 6f 77 69 64 20 3d 20 70 2d 3e 6d 6f astRowid = p->mo
1ae6a 76 65 74 6f 54 61 72 67 65 74 3b 0a 20 20 20 20 vetoTarget;.
1ae6b 70 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 p->rowidIsValid
1ae6c 3d 20 41 4c 57 41 59 53 28 72 65 73 3d 3d 30 29 = ALWAYS(res==0)
1ae6d 20 3f 31 3a 30 3b 0a 20 20 20 20 69 66 28 20 4e ?1:0;. if( N
1ae6e 45 56 45 52 28 72 65 73 3c 30 29 20 29 7b 0a 20 EVER(res<0) ){.
1ae6f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1ae70 33 42 74 72 65 65 4e 65 78 74 28 70 2d 3e 70 43 3BtreeNext(p->pC
1ae71 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 ursor, &res);.
1ae72 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
1ae73 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 23 69 urn rc;. }.#i
1ae74 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1ae75 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61 . sqlite3_sea
1ae76 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e rch_count++;.#en
1ae77 64 69 66 0a 20 20 20 20 70 2d 3e 64 65 66 65 72 dif. p->defer
1ae78 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 redMoveto = 0;.
1ae79 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 p->cacheStatu
1ae7a 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1ae7b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 . }else if( ALW
1ae7c 41 59 53 28 70 2d 3e 70 43 75 72 73 6f 72 29 20 AYS(p->pCursor)
1ae7d 29 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 4d 6f ){. int hasMo
1ae7e 76 65 64 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 ved;. int rc
1ae7f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 = sqlite3BtreeCu
1ae80 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 70 2d 3e rsorHasMoved(p->
1ae81 70 43 75 72 73 6f 72 2c 20 26 68 61 73 4d 6f 76 pCursor, &hasMov
1ae82 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ed);. if( rc
1ae83 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
1ae84 20 69 66 28 20 68 61 73 4d 6f 76 65 64 20 29 7b if( hasMoved ){
1ae85 0a 20 20 20 20 20 20 70 2d 3e 63 61 63 68 65 53 . p->cacheS
1ae86 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1ae87 41 4c 45 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 75 ALE;. p->nu
1ae88 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d llRow = 1;. }
1ae89 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
1ae8a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1ae8b 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1ae8c 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a functions:.**.**
1ae8d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1ae8e 61 6c 54 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69 alType().** sqli
1ae8f 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1ae90 65 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 eLen().** sqlite
1ae91 33 56 64 62 65 53 65 72 69 61 6c 4c 65 6e 28 29 3VdbeSerialLen()
1ae92 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 .** sqlite3VdbeS
1ae93 65 72 69 61 6c 50 75 74 28 29 0a 2a 2a 20 73 71 erialPut().** sq
1ae94 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 lite3VdbeSerialG
1ae95 65 74 28 29 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70 et().**.** encap
1ae96 73 75 6c 61 74 65 20 74 68 65 20 63 6f 64 65 20 sulate the code
1ae97 74 68 61 74 20 73 65 72 69 61 6c 69 7a 65 73 20 that serializes
1ae98 76 61 6c 75 65 73 20 66 6f 72 20 73 74 6f 72 61 values for stora
1ae99 67 65 20 69 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 ge in SQLite.**
1ae9a 64 61 74 61 20 61 6e 64 20 69 6e 64 65 78 20 72 data and index r
1ae9b 65 63 6f 72 64 73 2e 20 45 61 63 68 20 73 65 72 ecords. Each ser
1ae9c 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 20 63 6f ialized value co
1ae9d 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 27 nsists of a.** '
1ae9e 73 65 72 69 61 6c 2d 74 79 70 65 27 20 61 6e 64 serial-type' and
1ae9f 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e a blob of data.
1aea0 20 54 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 The serial type
1aea1 20 69 73 20 61 6e 20 38 2d 62 79 74 65 20 75 6e is an 8-byte un
1aea2 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 signed.** intege
1aea3 72 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 76 r, stored as a v
1aea4 61 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 arint..**.** In
1aea5 61 6e 20 53 51 4c 69 74 65 20 69 6e 64 65 78 20 an SQLite index
1aea6 72 65 63 6f 72 64 2c 20 74 68 65 20 73 65 72 69 record, the seri
1aea7 61 6c 20 74 79 70 65 20 69 73 20 73 74 6f 72 65 al type is store
1aea8 64 20 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72 d directly befor
1aea9 65 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66 e.** the blob of
1aeaa 20 64 61 74 61 20 74 68 61 74 20 69 74 20 63 6f data that it co
1aeab 72 72 65 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e rresponds to. In
1aeac 20 61 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2c a table record,
1aead 20 61 6c 6c 20 73 65 72 69 61 6c 0a 2a 2a 20 74 all serial.** t
1aeae 79 70 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 ypes are stored
1aeaf 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
1aeb0 74 68 65 20 72 65 63 6f 72 64 2c 20 61 6e 64 20 the record, and
1aeb1 74 68 65 20 62 6c 6f 62 73 20 6f 66 20 64 61 74 the blobs of dat
1aeb2 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 2e a at.** the end.
1aeb3 20 48 65 6e 63 65 20 74 68 65 73 65 20 66 75 6e Hence these fun
1aeb4 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65 ctions allow the
1aeb5 20 63 61 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c caller to handl
1aeb6 65 20 74 68 65 0a 2a 2a 20 73 65 72 69 61 6c 2d e the.** serial-
1aeb7 74 79 70 65 20 61 6e 64 20 64 61 74 61 20 62 6c type and data bl
1aeb8 6f 62 20 73 65 70 65 72 61 74 65 6c 79 2e 0a 2a ob seperately..*
1aeb9 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1aeba 6e 67 20 74 61 62 6c 65 20 64 65 73 63 72 69 62 ng table describ
1aebb 65 73 20 74 68 65 20 76 61 72 69 6f 75 73 20 73 es the various s
1aebc 74 6f 72 61 67 65 20 63 6c 61 73 73 65 73 20 66 torage classes f
1aebd 6f 72 20 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 or data:.**.**
1aebe 20 73 65 72 69 61 6c 20 74 79 70 65 20 20 20 20 serial type
1aebf 20 20 20 20 62 79 74 65 73 20 6f 66 20 64 61 74 bytes of dat
1aec0 61 20 20 20 20 20 20 74 79 70 65 0a 2a 2a 20 20 a type.**
1aec1 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 --------------
1aec2 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ------------
1aec3 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d --- ---------
1aec4 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 30 ------.** 0
1aec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aec6 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 0
1aec7 20 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 31 NULL.** 1
1aec8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aec9 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 1
1aeca 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 signed integer
1aecb 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 20 .** 2
1aecc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
1aecd 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e sign
1aece 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 ed integer.**
1aecf 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 3
1aed0 20 20 20 20 20 20 20 20 20 33 20 20 20 20 20 20 3
1aed1 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 signed int
1aed2 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 34 20 20 eger.** 4
1aed3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aed4 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20 4
1aed5 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a signed integer.*
1aed6 2a 20 20 20 20 20 20 35 20 20 20 20 20 20 20 20 * 5
1aed7 20 20 20 20 20 20 20 20 20 20 20 20 20 36 20 20 6
1aed8 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 signed
1aed9 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
1aeda 20 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6
1aedb 20 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 8
1aedc 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 signed integ
1aedd 65 72 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20 er.** 7
1aede 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aedf 20 38 20 20 20 20 20 20 20 20 20 20 20 20 49 45 8 IE
1aee0 45 45 20 66 6c 6f 61 74 0a 2a 2a 20 20 20 20 20 EE float.**
1aee1 20 38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 8
1aee2 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 0
1aee3 20 20 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 Integer cons
1aee4 74 61 6e 74 20 30 0a 2a 2a 20 20 20 20 20 20 39 tant 0.** 9
1aee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aee6 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 0
1aee7 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 Integer consta
1aee8 6e 74 20 31 0a 2a 2a 20 20 20 20 20 31 30 2c 31 nt 1.** 10,1
1aee9 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1
1aeea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aeeb 72 65 73 65 72 76 65 64 20 66 6f 72 20 65 78 70 reserved for exp
1aeec 61 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d ansion.** N>=
1aeed 31 32 20 61 6e 64 20 65 76 65 6e 20 20 20 20 20 12 and even
1aeee 20 20 28 4e 2d 31 32 29 2f 32 20 20 20 20 20 20 (N-12)/2
1aeef 20 20 42 4c 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d BLOB.** N>=
1aef0 31 33 20 61 6e 64 20 6f 64 64 20 20 20 20 20 20 13 and odd
1aef1 20 20 28 4e 2d 31 33 29 2f 32 20 20 20 20 20 20 (N-13)/2
1aef2 20 20 74 65 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65 text.**.** The
1aef3 20 38 20 61 6e 64 20 39 20 74 79 70 65 73 20 77 8 and 9 types w
1aef4 65 72 65 20 61 64 64 65 64 20 69 6e 20 33 2e 33 ere added in 3.3
1aef5 2e 30 2c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 .0, file format
1aef6 34 2e 20 20 50 72 69 6f 72 20 76 65 72 73 69 6f 4. Prior versio
1aef7 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 ns.** of SQLite
1aef8 77 69 6c 6c 20 6e 6f 74 20 75 6e 64 65 72 73 74 will not underst
1aef9 61 6e 64 20 74 68 6f 73 65 20 73 65 72 69 61 6c and those serial
1aefa 20 74 79 70 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a types..*/../*.*
1aefb 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 72 * Return the ser
1aefc 69 61 6c 2d 74 79 70 65 20 66 6f 72 20 74 68 65 ial-type for the
1aefd 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
1aefe 20 70 4d 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pMem..*/.SQLITE
1aeff 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c _PRIVATE u32 sql
1af00 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
1af01 70 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e pe(Mem *pMem, in
1af02 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a t file_format){.
1af03 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d int flags = pM
1af04 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 6e 74 em->flags;. int
1af05 20 6e 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 n;.. if( flags
1af06 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 &MEM_Null ){.
1af07 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
1af08 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 49 if( flags&MEM_I
1af09 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 nt ){. /* Fig
1af0a 75 72 65 20 6f 75 74 20 77 68 65 74 68 65 72 20 ure out whether
1af0b 74 6f 20 75 73 65 20 31 2c 20 32 2c 20 34 2c 20 to use 1, 2, 4,
1af0c 36 20 6f 72 20 38 20 62 79 74 65 73 2e 20 2a 2f 6 or 8 bytes. */
1af0d 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f .# define MAX_
1af0e 36 42 59 54 45 20 28 28 28 28 69 36 34 29 30 78 6BYTE ((((i64)0x
1af0f 30 30 30 30 38 30 30 30 29 3c 3c 33 32 29 2d 31 00008000)<<32)-1
1af10 29 0a 20 20 20 20 69 36 34 20 69 20 3d 20 70 4d ). i64 i = pM
1af11 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 36 34 em->u.i;. u64
1af12 20 75 3b 0a 20 20 20 20 69 66 28 20 66 69 6c 65 u;. if( file
1af13 5f 66 6f 72 6d 61 74 3e 3d 34 20 26 26 20 28 69 _format>=4 && (i
1af14 26 31 29 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 &1)==i ){.
1af15 72 65 74 75 72 6e 20 38 2b 28 75 33 32 29 69 3b return 8+(u32)i;
1af16 0a 20 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69 . }. u = i
1af17 3c 30 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20 <0 ? -i : i;.
1af18 20 69 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65 if( u<=127 ) re
1af19 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 turn 1;. if(
1af1a 75 3c 3d 33 32 37 36 37 20 29 20 72 65 74 75 72 u<=32767 ) retur
1af1b 6e 20 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d n 2;. if( u<=
1af1c 38 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e 8388607 ) return
1af1d 20 33 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32 3;. if( u<=2
1af1e 31 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75 147483647 ) retu
1af1f 72 6e 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c rn 4;. if( u<
1af20 3d 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65 74 =MAX_6BYTE ) ret
1af21 75 72 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72 urn 5;. retur
1af22 6e 20 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 n 6;. }. if( f
1af23 6c 61 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b lags&MEM_Real ){
1af24 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 . return 7;.
1af25 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 }. assert( pMe
1af26 6d 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 m->db->mallocFai
1af27 6c 65 64 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45 led || flags&(ME
1af28 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
1af29 29 3b 0a 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e );. n = pMem->n
1af2a 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 ;. if( flags &
1af2b 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 MEM_Zero ){.
1af2c 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 n += pMem->u.nZe
1af2d 72 6f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 ro;. }. assert
1af2e 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 ( n>=0 );. retu
1af2f 72 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b rn ((n*2) + 12 +
1af30 20 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 ((flags&MEM_Str
1af31 29 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a )!=0));.}../*.**
1af32 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 Return the leng
1af33 74 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 63 th of the data c
1af34 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
1af35 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 65 72 the supplied ser
1af36 69 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c ial-type..*/.SQL
1af37 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
1af38 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1af39 6c 54 79 70 65 4c 65 6e 28 75 33 32 20 73 65 72 lTypeLen(u32 ser
1af3a 69 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69 66 28 ial_type){. if(
1af3b 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 serial_type>=12
1af3c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 ){. return (
1af3d 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f serial_type-12)/
1af3e 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2;. }else{.
1af3f 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 static const u8
1af40 61 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31 aSize[] = { 0, 1
1af41 2c 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c , 2, 3, 4, 6, 8,
1af42 20 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 8, 0, 0, 0, 0 }
1af43 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 53 69 ;. return aSi
1af44 7a 65 5b 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b ze[serial_type];
1af45 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
1af46 20 77 65 20 61 72 65 20 6f 6e 20 61 6e 20 61 72 we are on an ar
1af47 63 68 69 74 65 63 74 75 72 65 20 77 69 74 68 20 chitecture with
1af48 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f mixed-endian flo
1af49 61 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73 ating .** points
1af4a 20 28 65 78 3a 20 41 52 4d 37 29 20 74 68 65 6e (ex: ARM7) then
1af4b 20 73 77 61 70 20 74 68 65 20 6c 6f 77 65 72 20 swap the lower
1af4c 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68 65 4 bytes with the
1af4d 20 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 79 74 .** upper 4 byt
1af4e 65 73 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 es. Return the
1af4f 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f result..**.** Fo
1af50 72 20 6d 6f 73 74 20 61 72 63 68 69 74 65 63 74 r most architect
1af51 75 72 65 73 2c 20 74 68 69 73 20 69 73 20 61 20 ures, this is a
1af52 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 no-op..**.** (la
1af53 74 65 72 29 3a 20 20 49 74 20 69 73 20 72 65 70 ter): It is rep
1af54 6f 72 74 65 64 20 74 6f 20 6d 65 20 74 68 61 74 orted to me that
1af55 20 74 68 65 20 6d 69 78 65 64 2d 65 6e 64 69 61 the mixed-endia
1af56 6e 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 n problem.** on
1af57 41 52 4d 37 20 69 73 20 61 6e 20 69 73 73 75 65 ARM7 is an issue
1af58 20 77 69 74 68 20 47 43 43 2c 20 6e 6f 74 20 77 with GCC, not w
1af59 69 74 68 20 74 68 65 20 41 52 4d 37 20 63 68 69 ith the ARM7 chi
1af5a 70 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20 p. It seems.**
1af5b 74 68 61 74 20 65 61 72 6c 79 20 76 65 72 73 69 that early versi
1af5c 6f 6e 73 20 6f 66 20 47 43 43 20 73 74 6f 72 65 ons of GCC store
1af5d 64 20 74 68 65 20 74 77 6f 20 77 6f 72 64 73 20 d the two words
1af5e 6f 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66 of a 64-bit.** f
1af5f 6c 6f 61 74 20 69 6e 20 74 68 65 20 77 72 6f 6e loat in the wron
1af60 67 20 6f 72 64 65 72 2e 20 20 41 6e 64 20 74 68 g order. And th
1af61 61 74 20 65 72 72 6f 72 20 68 61 73 20 62 65 65 at error has bee
1af62 6e 20 70 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20 n propagated.**
1af63 65 76 65 72 20 73 69 6e 63 65 2e 20 20 54 68 65 ever since. The
1af64 20 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65 blame is not ne
1af65 63 65 73 73 61 72 69 6c 79 20 77 69 74 68 20 47 cessarily with G
1af66 43 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47 CC, though..** G
1af67 43 43 20 6d 69 67 68 74 20 68 61 76 65 20 6a 75 CC might have ju
1af68 73 74 20 63 6f 70 79 69 6e 67 20 74 68 65 20 70 st copying the p
1af69 72 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72 roblem from a pr
1af6a 69 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a ior compiler..**
1af6b 20 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20 I am also told
1af6c 74 68 61 74 20 6e 65 77 65 72 20 76 65 72 73 69 that newer versi
1af6d 6f 6e 73 20 6f 66 20 47 43 43 20 74 68 61 74 20 ons of GCC that
1af6e 66 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65 72 65 follow a differe
1af6f 6e 74 0a 2a 2a 20 41 42 49 20 67 65 74 20 74 68 nt.** ABI get th
1af70 65 20 62 79 74 65 20 6f 72 64 65 72 20 72 69 67 e byte order rig
1af71 68 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f ht..**.** Develo
1af72 70 65 72 73 20 75 73 69 6e 67 20 53 51 4c 69 74 pers using SQLit
1af73 65 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f e on an ARM7 sho
1af74 75 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 uld compile and
1af75 72 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61 70 70 run their.** app
1af76 6c 69 63 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d lication using -
1af77 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 20 DSQLITE_DEBUG=1
1af78 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 at least once.
1af79 57 69 74 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e With DEBUG.** en
1af7a 61 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73 73 65 abled, some asse
1af7b 72 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65 rts below will e
1af7c 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 62 nsure that the b
1af7d 79 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 yte order of.**
1af7e 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
1af7f 61 6c 75 65 73 20 69 73 20 63 6f 72 72 65 63 74 alues is correct
1af80 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38 ..**.** (2007-08
1af81 2d 33 30 29 20 20 46 72 61 6e 6b 20 76 61 6e 20 -30) Frank van
1af82 56 75 67 74 20 68 61 73 20 73 74 75 64 69 65 64 Vugt has studied
1af83 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c this problem cl
1af84 6f 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73 osely.** and has
1af85 20 73 65 6e 64 20 68 69 73 20 66 69 6e 64 69 6e send his findin
1af86 67 73 20 74 6f 20 74 68 65 20 53 51 4c 69 74 65 gs to the SQLite
1af87 20 64 65 76 65 6c 6f 70 65 72 73 2e 20 20 46 72 developers. Fr
1af88 61 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20 74 68 ank.** writes th
1af89 61 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65 at some Linux ke
1af8a 72 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61 rnels offer floa
1af8b 74 69 6e 67 20 70 6f 69 6e 74 20 68 61 72 64 77 ting point hardw
1af8c 61 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e are.** emulation
1af8d 20 74 68 61 74 20 75 73 65 73 20 6f 6e 6c 79 20 that uses only
1af8e 33 32 2d 62 69 74 20 6d 61 6e 74 69 73 73 61 73 32-bit mantissas
1af8f 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 75 instead of a fu
1af90 6c 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61 ll .** 48-bits a
1af91 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 s required by th
1af92 65 20 49 45 45 45 20 73 74 61 6e 64 61 72 64 2e e IEEE standard.
1af93 20 20 28 54 68 69 73 20 69 73 20 74 68 65 0a 2a (This is the.*
1af94 2a 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53 * CONFIG_FPE_FAS
1af95 54 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f TFPE option.) O
1af96 6e 20 73 75 63 68 20 73 79 73 74 65 6d 73 2c 20 n such systems,
1af97 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a floating point.*
1af98 2a 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20 * byte swapping
1af99 62 65 63 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d becomes very com
1af9a 70 6c 69 63 61 74 65 64 2e 20 20 54 6f 20 61 76 plicated. To av
1af9b 6f 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a oid problems,.**
1af9c 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 62 the necessary b
1af9d 79 74 65 20 73 77 61 70 70 69 6e 67 20 69 73 20 yte swapping is
1af9e 63 61 72 72 69 65 64 20 6f 75 74 20 75 73 69 6e carried out usin
1af9f 67 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 g a 64-bit integ
1afa0 65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74 68 61 er.** rather tha
1afa1 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 n a 64-bit float
1afa2 2e 20 20 46 72 61 6e 6b 20 61 73 73 75 72 65 73 . Frank assures
1afa3 20 75 73 20 74 68 61 74 20 74 68 65 20 63 6f 64 us that the cod
1afa4 65 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20 e here.** works
1afa5 66 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20 74 68 for him. We, th
1afa6 65 20 64 65 76 65 6c 6f 70 65 72 73 2c 20 68 61 e developers, ha
1afa7 76 65 20 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64 ve no way to ind
1afa8 65 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65 ependently.** ve
1afa9 72 69 66 79 20 74 68 69 73 2c 20 62 75 74 20 46 rify this, but F
1afaa 72 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e rank seems to kn
1afab 6f 77 20 77 68 61 74 20 68 65 20 69 73 20 74 61 ow what he is ta
1afac 6c 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73 lking about.** s
1afad 6f 20 77 65 20 74 72 75 73 74 20 68 69 6d 2e 0a o we trust him..
1afae 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1afaf 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 _MIXED_ENDIAN_64
1afb0 42 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63 BIT_FLOAT.static
1afb1 20 75 36 34 20 66 6c 6f 61 74 53 77 61 70 28 75 u64 floatSwap(u
1afb2 36 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 64 in){. union
1afb3 7b 0a 20 20 20 20 75 36 34 20 72 3b 0a 20 20 20 {. u64 r;.
1afb4 20 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75 u32 i[2];. } u
1afb5 3b 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e ;. u32 t;.. u.
1afb6 72 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e r = in;. t = u.
1afb7 69 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d i[0];. u.i[0] =
1afb8 20 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31 u.i[1];. u.i[1
1afb9 5d 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 6e 20 ] = t;. return
1afba 75 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20 u.r;.}.# define
1afbb 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 swapMixedEndianF
1afbc 6c 6f 61 74 28 58 29 20 20 58 20 3d 20 66 6c 6f loat(X) X = flo
1afbd 61 74 53 77 61 70 28 58 29 0a 23 65 6c 73 65 0a atSwap(X).#else.
1afbe 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78 # define swapMix
1afbf 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 edEndianFloat(X)
1afc0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 .#endif../*.** W
1afc1 72 69 74 65 20 74 68 65 20 73 65 72 69 61 6c 69 rite the seriali
1afc2 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20 66 6f zed data blob fo
1afc3 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 r the value stor
1afc4 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20 ed in pMem into
1afc5 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 20 61 .** buf. It is a
1afc6 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 ssumed that the
1afc7 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63 caller has alloc
1afc8 61 74 65 64 20 73 75 66 66 69 63 69 65 6e 74 20 ated sufficient
1afc9 73 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e space..** Return
1afca 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
1afcb 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a ytes written..**
1afcc 0a 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 65 20 .** nBuf is the
1afcd 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20 amount of space
1afce 6c 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20 left in buf[].
1afcf 6e 42 75 66 20 6d 75 73 74 20 61 6c 77 61 79 73 nBuf must always
1afd0 20 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f be.** large eno
1afd1 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ugh to hold the
1afd2 65 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 20 45 entire field. E
1afd3 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 66 69 xcept, if the fi
1afd4 65 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62 eld is.** a blob
1afd5 20 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c with a zero-fil
1afd6 6c 65 64 20 74 61 69 6c 2c 20 74 68 65 6e 20 62 led tail, then b
1afd7 75 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 6a 75 uf[] might be ju
1afd8 73 74 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 st the right.**
1afd9 73 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 76 65 size to hold eve
1afda 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 66 rything except f
1afdb 6f 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c or the zero-fill
1afdc 65 64 20 74 61 69 6c 2e 20 20 49 66 20 62 75 66 ed tail. If buf
1afdd 5b 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69 [].** is only bi
1afde 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 g enough to hold
1afdf 20 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 the non-zero pr
1afe0 65 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 efix, then only
1afe1 77 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 70 72 write that.** pr
1afe2 65 66 69 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e efix into buf[].
1afe3 20 20 42 75 74 20 69 66 20 62 75 66 5b 5d 20 69 But if buf[] i
1afe4 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 s large enough t
1afe5 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a o hold both the.
1afe6 2a 2a 20 70 72 65 66 69 78 20 61 6e 64 20 74 68 ** prefix and th
1afe7 65 20 74 61 69 6c 20 74 68 65 6e 20 77 72 69 74 e tail then writ
1afe8 65 20 74 68 65 20 70 72 65 66 69 78 20 61 6e 64 e the prefix and
1afe9 20 73 65 74 20 74 68 65 20 74 61 69 6c 20 74 6f set the tail to
1afea 20 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a all.** zeros..*
1afeb 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1afec 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1afed 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e actually written
1afee 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68 into buf[]. Th
1afef 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 e number.** of b
1aff0 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72 6f ytes in the zero
1aff1 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73 20 -filled tail is
1aff2 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 included in the
1aff3 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c return value onl
1aff4 79 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62 79 y.** if those by
1aff5 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64 20 tes were zeroed
1aff6 69 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 in buf[]..*/ .SQ
1aff7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 LITE_PRIVATE u32
1aff8 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1aff9 61 6c 50 75 74 28 75 38 20 2a 62 75 66 2c 20 69 alPut(u8 *buf, i
1affa 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d nt nBuf, Mem *pM
1affb 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 em, int file_for
1affc 6d 61 74 29 7b 0a 20 20 75 33 32 20 73 65 72 69 mat){. u32 seri
1affd 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 al_type = sqlite
1affe 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 3VdbeSerialType(
1afff 70 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 pMem, file_forma
1b000 74 29 3b 0a 20 20 75 33 32 20 6c 65 6e 3b 0a 0a t);. u32 len;..
1b001 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 6e 64 /* Integer and
1b002 20 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73 Real */. if( s
1b003 65 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26 erial_type<=7 &&
1b004 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 20 29 serial_type>0 )
1b005 7b 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 20 20 {. u64 v;.
1b006 20 75 33 32 20 69 3b 0a 20 20 20 20 69 66 28 20 u32 i;. if(
1b007 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29 serial_type==7 )
1b008 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1b009 73 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f sizeof(v)==sizeo
1b00a 66 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20 f(pMem->r) );.
1b00b 20 20 20 20 6d 65 6d 63 70 79 28 26 76 2c 20 26 memcpy(&v, &
1b00c 70 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 pMem->r, sizeof(
1b00d 76 29 29 3b 0a 20 20 20 20 20 20 73 77 61 70 4d v));. swapM
1b00e 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 ixedEndianFloat(
1b00f 76 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 v);. }else{.
1b010 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 v = pMem->u
1b011 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 .i;. }. le
1b012 6e 20 3d 20 69 20 3d 20 73 71 6c 69 74 65 33 56 n = i = sqlite3V
1b013 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e dbeSerialTypeLen
1b014 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 (serial_type);.
1b015 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d assert( len<=
1b016 28 75 33 32 29 6e 42 75 66 20 29 3b 0a 20 20 20 (u32)nBuf );.
1b017 20 77 68 69 6c 65 28 20 69 2d 2d 20 29 7b 0a 20 while( i-- ){.
1b018 20 20 20 20 20 62 75 66 5b 69 5d 20 3d 20 28 75 buf[i] = (u
1b019 38 29 28 76 26 30 78 46 46 29 3b 0a 20 20 20 20 8)(v&0xFF);.
1b01a 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 7d v >>= 8;. }
1b01b 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b . return len;
1b01c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 72 69 6e . }.. /* Strin
1b01d 67 20 6f 72 20 62 6c 6f 62 20 2a 2f 0a 20 20 69 g or blob */. i
1b01e 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d f( serial_type>=
1b01f 31 32 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 12 ){. assert
1b020 28 20 70 4d 65 6d 2d 3e 6e 20 2b 20 28 28 70 4d ( pMem->n + ((pM
1b021 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1b022 5a 65 72 6f 29 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a Zero)?pMem->u.nZ
1b023 65 72 6f 3a 30 29 0a 20 20 20 20 20 20 20 20 20 ero:0).
1b024 20 20 20 20 3d 3d 20 28 69 6e 74 29 73 71 6c 69 == (int)sqli
1b025 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1b026 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 eLen(serial_type
1b027 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ) );. assert(
1b028 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 pMem->n<=nBuf )
1b029 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d ;. len = pMem
1b02a 2d 3e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 ->n;. memcpy(
1b02b 62 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 buf, pMem->z, le
1b02c 6e 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d n);. if( pMem
1b02d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 ->flags & MEM_Ze
1b02e 72 6f 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20 ro ){. len
1b02f 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f += pMem->u.nZero
1b030 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1b031 6e 42 75 66 3e 3d 30 20 29 3b 0a 20 20 20 20 20 nBuf>=0 );.
1b032 20 69 66 28 20 6c 65 6e 20 3e 20 28 75 33 32 29 if( len > (u32)
1b033 6e 42 75 66 20 29 7b 0a 20 20 20 20 20 20 20 20 nBuf ){.
1b034 6c 65 6e 20 3d 20 28 75 33 32 29 6e 42 75 66 3b len = (u32)nBuf;
1b035 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
1b036 65 6d 73 65 74 28 26 62 75 66 5b 70 4d 65 6d 2d emset(&buf[pMem-
1b037 3e 6e 5d 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65 6d >n], 0, len-pMem
1b038 2d 3e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ->n);. }.
1b039 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a return len;. }.
1b03a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 6f . /* NULL or co
1b03b 6e 73 74 61 6e 74 73 20 30 20 6f 72 20 31 20 2a nstants 0 or 1 *
1b03c 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a /. return 0;.}.
1b03d 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 ./*.** Deseriali
1b03e 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c 6f 62 ze the data blob
1b03f 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 62 pointed to by b
1b040 75 66 20 61 73 20 73 65 72 69 61 6c 20 74 79 70 uf as serial typ
1b041 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0a 2a 2a e serial_type.**
1b042 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 and store the r
1b043 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d 2e 20 20 esult in pMem.
1b044 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1b045 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e r of bytes read.
1b046 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/ .SQLITE_PRIV
1b047 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 ATE u32 sqlite3V
1b048 64 62 65 53 65 72 69 61 6c 47 65 74 28 0a 20 20 dbeSerialGet(.
1b049 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1b04a 68 61 72 20 2a 62 75 66 2c 20 20 20 20 20 2f 2a har *buf, /*
1b04b 20 42 75 66 66 65 72 20 74 6f 20 64 65 73 65 72 Buffer to deser
1b04c 69 61 6c 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a 20 ialize from */.
1b04d 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 u32 serial_type
1b04e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1b04f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 74 6f * Serial type to
1b050 20 64 65 73 65 72 69 61 6c 69 7a 65 20 2a 2f 0a deserialize */.
1b051 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 Mem *pMem
1b052 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b053 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 /* Memory cell t
1b054 6f 20 77 72 69 74 65 20 76 61 6c 75 65 20 69 6e o write value in
1b055 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 to */.){. switc
1b056 68 28 20 73 65 72 69 61 6c 5f 74 79 70 65 20 29 h( serial_type )
1b057 7b 0a 20 20 20 20 63 61 73 65 20 31 30 3a 20 20 {. case 10:
1b058 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66 6f 72 /* Reserved for
1b059 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 future use */.
1b05a 20 20 20 63 61 73 65 20 31 31 3a 20 20 20 2f 2a case 11: /*
1b05b 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 Reserved for fu
1b05c 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 ture use */.
1b05d 63 61 73 65 20 30 3a 20 7b 20 20 2f 2a 20 4e 55 case 0: { /* NU
1b05e 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d LL */. pMem
1b05f 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 ->flags = MEM_Nu
1b060 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ll;. break;
1b061 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1b062 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20 73 1: { /* 1-byte s
1b063 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
1b064 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 . pMem->u.i
1b065 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 29 = (signed char)
1b066 62 75 66 5b 30 5d 3b 0a 20 20 20 20 20 20 70 4d buf[0];. pM
1b067 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1b068 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 Int;. retur
1b069 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 n 1;. }. c
1b06a 61 73 65 20 32 3a 20 7b 20 2f 2a 20 32 2d 62 79 ase 2: { /* 2-by
1b06b 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
1b06c 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d r */. pMem-
1b06d 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 >u.i = (((signed
1b06e 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 char)buf[0])<<8
1b06f 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 ) | buf[1];.
1b070 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1b071 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 MEM_Int;. r
1b072 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a 20 eturn 2;. }.
1b073 20 20 20 63 61 73 65 20 33 3a 20 7b 20 2f 2a 20 case 3: { /*
1b074 33 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 3-byte signed in
1b075 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 teger */. p
1b076 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69 Mem->u.i = (((si
1b077 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d gned char)buf[0]
1b078 29 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 31 5d )<<16) | (buf[1]
1b079 3c 3c 38 29 20 7c 20 62 75 66 5b 32 5d 3b 0a 20 <<8) | buf[2];.
1b07a 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1b07b 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1b07c 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 return 3;.
1b07d 7d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b 20 }. case 4: {
1b07e 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 4-byte signed
1b07f 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 integer */.
1b080 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 62 pMem->u.i = (b
1b081 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 uf[0]<<24) | (bu
1b082 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 f[1]<<16) | (buf
1b083 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d [2]<<8) | buf[3]
1b084 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
1b085 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1b086 20 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 return 4;.
1b087 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 35 3a }. case 5:
1b088 20 7b 20 2f 2a 20 36 2d 62 79 74 65 20 73 69 67 { /* 6-byte sig
1b089 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 ned integer */.
1b08a 20 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28 u64 x = (((
1b08b 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b signed char)buf[
1b08c 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d 0])<<8) | buf[1]
1b08d 3b 0a 20 20 20 20 20 20 75 33 32 20 79 20 3d 20 ;. u32 y =
1b08e 28 62 75 66 5b 32 5d 3c 3c 32 34 29 20 7c 20 28 (buf[2]<<24) | (
1b08f 62 75 66 5b 33 5d 3c 3c 31 36 29 20 7c 20 28 62 buf[3]<<16) | (b
1b090 75 66 5b 34 5d 3c 3c 38 29 20 7c 20 62 75 66 5b uf[4]<<8) | buf[
1b091 35 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 78 5];. x = (x
1b092 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 20 <<32) | y;.
1b093 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 pMem->u.i = *(i
1b094 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 70 4d 64*)&x;. pM
1b095 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1b096 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 Int;. retur
1b097 6e 20 36 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 n 6;. }. c
1b098 61 73 65 20 36 3a 20 20 20 2f 2a 20 38 2d 62 79 ase 6: /* 8-by
1b099 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 te signed intege
1b09a 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 37 3a r */. case 7:
1b09b 20 7b 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61 74 { /* IEEE float
1b09c 69 6e 67 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 ing point */.
1b09d 20 20 20 75 36 34 20 78 3b 0a 20 20 20 20 20 20 u64 x;.
1b09e 75 33 32 20 79 3b 0a 23 69 66 20 21 64 65 66 69 u32 y;.#if !defi
1b09f 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 ned(NDEBUG) && !
1b0a0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1b0a1 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
1b0a2 4e 54 29 0a 20 20 20 20 20 20 2f 2a 20 56 65 72 NT). /* Ver
1b0a3 69 66 79 20 74 68 61 74 20 69 6e 74 65 67 65 72 ify that integer
1b0a4 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67 20 70 s and floating p
1b0a5 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73 65 20 oint values use
1b0a6 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 20 2a the same. *
1b0a7 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 4f * byte order. O
1b0a8 72 2c 20 74 68 61 74 20 69 66 20 53 51 4c 49 54 r, that if SQLIT
1b0a9 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 E_MIXED_ENDIAN_6
1b0aa 34 42 49 54 5f 46 4c 4f 41 54 20 69 73 0a 20 20 4BIT_FLOAT is.
1b0ab 20 20 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 74 ** defined t
1b0ac 68 61 74 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 hat 64-bit float
1b0ad 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 ing point values
1b0ae 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 69 78 65 really are mixe
1b0af 64 0a 20 20 20 20 20 20 2a 2a 20 65 6e 64 69 61 d. ** endia
1b0b0 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 n.. */.
1b0b1 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
1b0b2 36 34 20 74 31 20 3d 20 28 28 75 36 34 29 30 78 64 t1 = ((u64)0x
1b0b3 33 66 66 30 30 30 30 30 29 3c 3c 33 32 3b 0a 20 3ff00000)<<32;.
1b0b4 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 static cons
1b0b5 74 20 64 6f 75 62 6c 65 20 72 31 20 3d 20 31 2e t double r1 = 1.
1b0b6 30 3b 0a 20 20 20 20 20 20 75 36 34 20 74 32 20 0;. u64 t2
1b0b7 3d 20 74 31 3b 0a 20 20 20 20 20 20 73 77 61 70 = t1;. swap
1b0b8 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 MixedEndianFloat
1b0b9 28 74 32 29 3b 0a 20 20 20 20 20 20 61 73 73 65 (t2);. asse
1b0ba 72 74 28 20 73 69 7a 65 6f 66 28 72 31 29 3d 3d rt( sizeof(r1)==
1b0bb 73 69 7a 65 6f 66 28 74 32 29 20 26 26 20 6d 65 sizeof(t2) && me
1b0bc 6d 63 6d 70 28 26 72 31 2c 20 26 74 32 2c 20 73 mcmp(&r1, &t2, s
1b0bd 69 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29 3b izeof(r1))==0 );
1b0be 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 78 .#endif.. x
1b0bf 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20 = (buf[0]<<24)
1b0c0 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c | (buf[1]<<16) |
1b0c1 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 (buf[2]<<8) | b
1b0c2 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20 3d uf[3];. y =
1b0c3 20 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c 20 (buf[4]<<24) |
1b0c4 28 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 28 (buf[5]<<16) | (
1b0c5 62 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 66 buf[6]<<8) | buf
1b0c6 5b 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 [7];. x = (
1b0c7 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 x<<32) | y;.
1b0c8 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 if( serial_typ
1b0c9 65 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20 20 e==6 ){.
1b0ca 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 pMem->u.i = *(i6
1b0cb 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20 70 4*)&x;. p
1b0cc 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1b0cd 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 _Int;. }els
1b0ce 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 e{. asser
1b0cf 74 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38 20 t( sizeof(x)==8
1b0d0 26 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e && sizeof(pMem->
1b0d1 72 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20 20 r)==8 );.
1b0d2 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e swapMixedEndian
1b0d3 46 6c 6f 61 74 28 78 29 3b 0a 20 20 20 20 20 20 Float(x);.
1b0d4 20 20 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d 3e memcpy(&pMem->
1b0d5 72 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 r, &x, sizeof(x)
1b0d6 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d );. pMem-
1b0d7 3e 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 >flags = sqlite3
1b0d8 49 73 4e 61 4e 28 70 4d 65 6d 2d 3e 72 29 20 3f IsNaN(pMem->r) ?
1b0d9 20 4d 45 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d 5f MEM_Null : MEM_
1b0da 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Real;. }.
1b0db 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 return 8;.
1b0dc 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38 3a 20 }. case 8:
1b0dd 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 30 20 /* Integer 0
1b0de 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a 20 7b */. case 9: {
1b0df 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31 20 2a /* Integer 1 *
1b0e0 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e /. pMem->u.
1b0e1 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70 65 2d i = serial_type-
1b0e2 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 8;. pMem->f
1b0e3 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1b0e4 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
1b0e5 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
1b0e6 74 3a 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6c t: {. u32 l
1b0e7 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74 79 70 en = (serial_typ
1b0e8 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20 20 70 e-12)/2;. p
1b0e9 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a Mem->z = (char *
1b0ea 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d 65 6d )buf;. pMem
1b0eb 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 ->n = len;.
1b0ec 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b pMem->xDel = 0;
1b0ed 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69 61 . if( seria
1b0ee 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b 0a 20 l_type&0x01 ){.
1b0ef 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 pMem->fla
1b0f0 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 4d gs = MEM_Str | M
1b0f1 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 EM_Ephem;.
1b0f2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
1b0f3 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1b0f4 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70 68 65 _Blob | MEM_Ephe
1b0f5 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 m;. }.
1b0f6 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 20 return len;.
1b0f7 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
1b0f8 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 0;.}.../*.** Giv
1b0f9 65 6e 20 74 68 65 20 6e 4b 65 79 2d 62 79 74 65 en the nKey-byte
1b0fa 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 encoding of a r
1b0fb 65 63 6f 72 64 20 69 6e 20 70 4b 65 79 5b 5d 2c ecord in pKey[],
1b0fc 20 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 72 65 parse the.** re
1b0fd 63 6f 72 64 20 69 6e 74 6f 20 61 20 55 6e 70 61 cord into a Unpa
1b0fe 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72 75 63 ckedRecord struc
1b0ff 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 61 20 ture. Return a
1b100 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 pointer to.** th
1b101 61 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a at structure..**
1b102 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 .** The calling
1b103 66 75 6e 63 74 69 6f 6e 20 6d 69 67 68 74 20 70 function might p
1b104 72 6f 76 69 64 65 20 73 7a 53 70 61 63 65 20 62 rovide szSpace b
1b105 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a ytes of memory.*
1b106 2a 20 73 70 61 63 65 20 61 74 20 70 53 70 61 63 * space at pSpac
1b107 65 2e 20 20 54 68 69 73 20 73 70 61 63 65 20 63 e. This space c
1b108 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 68 6f an be used to ho
1b109 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 0a ld the returned.
1b10a 2a 2a 20 56 44 62 65 50 61 72 73 65 64 52 65 63 ** VDbeParsedRec
1b10b 6f 72 64 20 73 74 72 75 63 74 75 72 65 20 69 66 ord structure if
1b10c 20 69 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f it is large eno
1b10d 75 67 68 2e 20 20 49 66 20 69 74 20 69 73 0a 2a ugh. If it is.*
1b10e 2a 20 6e 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 * not big enough
1b10f 2c 20 73 70 61 63 65 20 69 73 20 6f 62 74 61 69 , space is obtai
1b110 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 ned from sqlite3
1b111 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a _malloc()..**.**
1b112 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 The returned st
1b113 72 75 63 74 75 72 65 20 73 68 6f 75 6c 64 20 62 ructure should b
1b114 65 20 63 6c 6f 73 65 64 20 62 79 20 61 20 63 61 e closed by a ca
1b115 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ll to.** sqlite3
1b116 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b VdbeDeleteUnpack
1b117 65 64 52 65 63 6f 72 64 28 29 2e 0a 2a 2f 20 0a edRecord()..*/ .
1b118 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55 SQLITE_PRIVATE U
1b119 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 npackedRecord *s
1b11a 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 qlite3VdbeRecord
1b11b 55 6e 70 61 63 6b 28 0a 20 20 4b 65 79 49 6e 66 Unpack(. KeyInf
1b11c 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 o *pKeyInfo,
1b11d 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
1b11e 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f 72 64 about the record
1b11f 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 69 6e 74 format */. int
1b120 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 nKey,
1b121 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
1b122 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72 64 he binary record
1b123 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
1b124 20 2a 70 4b 65 79 2c 20 20 20 20 20 20 2f 2a 20 *pKey, /*
1b125 54 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72 The binary recor
1b126 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 53 70 d */. char *pSp
1b127 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a ace, /*
1b128 20 55 6e 61 6c 69 67 6e 65 64 20 73 70 61 63 65 Unaligned space
1b129 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f available to ho
1b12a 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f ld the object */
1b12b 0a 20 20 69 6e 74 20 73 7a 53 70 61 63 65 20 20 . int szSpace
1b12c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
1b12d 65 20 6f 66 20 70 53 70 61 63 65 5b 5d 20 69 6e e of pSpace[] in
1b12e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 bytes */.){. c
1b12f 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1b130 61 72 20 2a 61 4b 65 79 20 3d 20 28 63 6f 6e 73 ar *aKey = (cons
1b131 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1b132 2a 29 70 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b *)pKey;. Unpack
1b133 65 64 52 65 63 6f 72 64 20 2a 70 3b 20 20 2f 2a edRecord *p; /*
1b134 20 54 68 65 20 75 6e 70 61 63 6b 65 64 20 72 65 The unpacked re
1b135 63 6f 72 64 20 74 68 61 74 20 77 65 20 77 69 6c cord that we wil
1b136 6c 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e l return */. in
1b137 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 t nByte;
1b138 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 73 70 61 63 /* Memory spac
1b139 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 e needed to hold
1b13a 20 70 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a p, in bytes */.
1b13b 20 20 69 6e 74 20 64 3b 0a 20 20 75 33 32 20 69 int d;. u32 i
1b13c 64 78 3b 0a 20 20 75 31 36 20 75 3b 20 20 20 20 dx;. u16 u;
1b13d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 /* Uns
1b13e 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 igned loop count
1b13f 65 72 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 er */. u32 szHd
1b140 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a r;. Mem *pMem;.
1b141 20 20 69 6e 74 20 6e 4f 66 66 3b 20 20 20 20 20 int nOff;
1b142 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 61 73 /* Increas
1b143 65 20 70 53 70 61 63 65 20 62 79 20 74 68 69 73 e pSpace by this
1b144 20 6d 75 63 68 20 74 6f 20 38 2d 62 79 74 65 20 much to 8-byte
1b145 61 6c 69 67 6e 20 69 74 20 2a 2f 0a 20 20 0a 20 align it */. .
1b146 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 77 61 6e 74 /*. ** We want
1b147 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 70 6f to shift the po
1b148 69 6e 74 65 72 20 70 53 70 61 63 65 20 75 70 20 inter pSpace up
1b149 73 75 63 68 20 74 68 61 74 20 69 74 20 69 73 20 such that it is
1b14a 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 2e 0a 8-byte aligned..
1b14b 20 20 2a 2a 20 54 68 75 73 2c 20 77 65 20 6e 65 ** Thus, we ne
1b14c 65 64 20 74 6f 20 63 61 6c 63 75 6c 61 74 65 20 ed to calculate
1b14d 61 20 76 61 6c 75 65 2c 20 6e 4f 66 66 2c 20 62 a value, nOff, b
1b14e 65 74 77 65 65 6e 20 30 20 61 6e 64 20 37 2c 20 etween 0 and 7,
1b14f 74 6f 20 73 68 69 66 74 20 0a 20 20 2a 2a 20 69 to shift . ** i
1b150 74 20 62 79 2e 20 20 49 66 20 70 53 70 61 63 65 t by. If pSpace
1b151 20 69 73 20 61 6c 72 65 61 64 79 20 38 2d 62 79 is already 8-by
1b152 74 65 20 61 6c 69 67 6e 65 64 2c 20 6e 4f 66 66 te aligned, nOff
1b153 20 73 68 6f 75 6c 64 20 62 65 20 7a 65 72 6f 2e should be zero.
1b154 0a 20 20 2a 2f 0a 20 20 6e 4f 66 66 20 3d 20 28 . */. nOff = (
1b155 38 20 2d 20 28 53 51 4c 49 54 45 5f 50 54 52 5f 8 - (SQLITE_PTR_
1b156 54 4f 5f 49 4e 54 28 70 53 70 61 63 65 29 20 26 TO_INT(pSpace) &
1b157 20 37 29 29 20 26 20 37 3b 0a 20 20 70 53 70 61 7)) & 7;. pSpa
1b158 63 65 20 2b 3d 20 6e 4f 66 66 3b 0a 20 20 73 7a ce += nOff;. sz
1b159 53 70 61 63 65 20 2d 3d 20 6e 4f 66 66 3b 0a 20 Space -= nOff;.
1b15a 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 nByte = ROUND8(
1b15b 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 sizeof(UnpackedR
1b15c 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 ecord)) + sizeof
1b15d 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f 2d (Mem)*(pKeyInfo-
1b15e 3e 6e 46 69 65 6c 64 2b 31 29 3b 0a 20 20 69 66 >nField+1);. if
1b15f 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65 20 ( nByte>szSpace
1b160 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 ){. p = sqlit
1b161 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4b e3DbMallocRaw(pK
1b162 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 74 eyInfo->db, nByt
1b163 65 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 e);. if( p==0
1b164 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
1b165 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 p->flags = UNPA
1b166 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 7c CKED_NEED_FREE |
1b167 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 UNPACKED_NEED_D
1b168 45 53 54 52 4f 59 3b 0a 20 20 7d 65 6c 73 65 7b ESTROY;. }else{
1b169 0a 20 20 20 20 70 20 3d 20 28 55 6e 70 61 63 6b . p = (Unpack
1b16a 65 64 52 65 63 6f 72 64 2a 29 70 53 70 61 63 65 edRecord*)pSpace
1b16b 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d ;. p->flags =
1b16c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 UNPACKED_NEED_D
1b16d 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 2d ESTROY;. }. p-
1b16e 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 >pKeyInfo = pKey
1b16f 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c Info;. p->nFiel
1b170 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 d = pKeyInfo->nF
1b171 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 2d 3e 61 ield + 1;. p->a
1b172 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 28 4d 65 Mem = pMem = (Me
1b173 6d 2a 29 26 28 28 63 68 61 72 2a 29 70 29 5b 52 m*)&((char*)p)[R
1b174 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 OUND8(sizeof(Unp
1b175 61 63 6b 65 64 52 65 63 6f 72 64 29 29 5d 3b 0a ackedRecord))];.
1b176 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f assert( EIGHT_
1b177 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 BYTE_ALIGNMENT(p
1b178 4d 65 6d 29 20 29 3b 0a 20 20 69 64 78 20 3d 20 Mem) );. idx =
1b179 67 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 getVarint32(aKey
1b17a 2c 20 73 7a 48 64 72 29 3b 0a 20 20 64 20 3d 20 , szHdr);. d =
1b17b 73 7a 48 64 72 3b 0a 20 20 75 20 3d 20 30 3b 0a szHdr;. u = 0;.
1b17c 20 20 77 68 69 6c 65 28 20 69 64 78 3c 73 7a 48 while( idx<szH
1b17d 64 72 20 26 26 20 75 3c 70 2d 3e 6e 46 69 65 6c dr && u<p->nFiel
1b17e 64 20 26 26 20 64 3c 3d 6e 4b 65 79 20 29 7b 0a d && d<=nKey ){.
1b17f 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 u32 serial_t
1b180 79 70 65 3b 0a 0a 20 20 20 20 69 64 78 20 2b 3d ype;.. idx +=
1b181 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 61 4b getVarint32(&aK
1b182 65 79 5b 69 64 78 5d 2c 20 73 65 72 69 61 6c 5f ey[idx], serial_
1b183 74 79 70 65 29 3b 0a 20 20 20 20 70 4d 65 6d 2d type);. pMem-
1b184 3e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d >enc = pKeyInfo-
1b185 3e 65 6e 63 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e >enc;. pMem->
1b186 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 db = pKeyInfo->d
1b187 62 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 b;. pMem->fla
1b188 67 73 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d gs = 0;. pMem
1b189 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 ->zMalloc = 0;.
1b18a 20 20 20 64 20 2b 3d 20 73 71 6c 69 74 65 33 56 d += sqlite3V
1b18b 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b dbeSerialGet(&aK
1b18c 65 79 5b 64 5d 2c 20 73 65 72 69 61 6c 5f 74 79 ey[d], serial_ty
1b18d 70 65 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 70 pe, pMem);. p
1b18e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b 0a Mem++;. u++;.
1b18f 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75 3c }. assert( u<
1b190 3d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c =pKeyInfo->nFiel
1b191 64 20 2b 20 31 20 29 3b 0a 20 20 70 2d 3e 6e 46 d + 1 );. p->nF
1b192 69 65 6c 64 20 3d 20 75 3b 0a 20 20 72 65 74 75 ield = u;. retu
1b193 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a rn (void*)p;.}..
1b194 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
1b195 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 55 6e ne destroys a Un
1b196 70 61 63 6b 65 64 52 65 63 6f 72 64 20 6f 62 6a packedRecord obj
1b197 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ect..*/.SQLITE_P
1b198 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1b199 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 te3VdbeDeleteUnp
1b19a 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70 61 ackedRecord(Unpa
1b19b 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 29 7b 0a ckedRecord *p){.
1b19c 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a int i;. Mem *
1b19d 70 4d 65 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 pMem;.. assert(
1b19e 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 p!=0 );. asser
1b19f 74 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e t( p->flags & UN
1b1a0 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 PACKED_NEED_DEST
1b1a1 52 4f 59 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 ROY );. for(i=0
1b1a2 2c 20 70 4d 65 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 , pMem=p->aMem;
1b1a3 69 3c 70 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b 2b i<p->nField; i++
1b1a4 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 2f , pMem++){. /
1b1a5 2a 20 54 68 65 20 75 6e 70 61 63 6b 65 64 20 72 * The unpacked r
1b1a6 65 63 6f 72 64 20 69 73 20 61 6c 77 61 79 73 20 ecord is always
1b1a7 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 74 constructed by t
1b1a8 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 he. ** sqlite
1b1a9 33 56 64 62 65 55 6e 70 61 63 6b 52 65 63 6f 72 3VdbeUnpackRecor
1b1aa 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 62 6f d() function abo
1b1ab 76 65 2c 20 77 68 69 63 68 20 6d 61 6b 65 73 20 ve, which makes
1b1ac 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 74 72 69 6e all. ** strin
1b1ad 67 73 20 61 6e 64 20 62 6c 6f 62 73 20 73 74 61 gs and blobs sta
1b1ae 74 69 63 2e 20 20 41 6e 64 20 6e 6f 6e 65 20 6f tic. And none o
1b1af 66 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 61 f the elements a
1b1b0 72 65 0a 20 20 20 20 2a 2a 20 65 76 65 72 20 74 re. ** ever t
1b1b1 72 61 6e 73 66 6f 72 6d 65 64 2c 20 73 6f 20 74 ransformed, so t
1b1b2 68 65 72 65 20 69 73 20 6e 65 76 65 72 20 61 6e here is never an
1b1b3 79 74 68 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 ything to delete
1b1b4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1b1b5 20 4e 45 56 45 52 28 70 4d 65 6d 2d 3e 7a 4d 61 NEVER(pMem->zMa
1b1b6 6c 6c 6f 63 29 20 29 20 73 71 6c 69 74 65 33 56 lloc) ) sqlite3V
1b1b7 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d dbeMemRelease(pM
1b1b8 65 6d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 em);. }. if( p
1b1b9 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b ->flags & UNPACK
1b1ba 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 29 7b 0a ED_NEED_FREE ){.
1b1bb 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1b1bc 65 28 70 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 e(p->pKeyInfo->d
1b1bd 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a b, p);. }.}../*
1b1be 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1b1bf 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 74 n compares the t
1b1c0 77 6f 20 74 61 62 6c 65 20 72 6f 77 73 20 6f 72 wo table rows or
1b1c1 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 0a 2a index records.*
1b1c2 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7b * specified by {
1b1c3 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 61 6e nKey1, pKey1} an
1b1c4 64 20 70 50 4b 65 79 32 2e 20 20 49 74 20 72 65 d pPKey2. It re
1b1c5 74 75 72 6e 73 20 61 20 6e 65 67 61 74 69 76 65 turns a negative
1b1c6 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73 , zero.** or pos
1b1c7 69 74 69 76 65 20 69 6e 74 65 67 65 72 20 69 66 itive integer if
1b1c8 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74 68 key1 is less th
1b1c9 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 an, equal to or
1b1ca 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e .** greater than
1b1cb 20 6b 65 79 32 2e 20 20 54 68 65 20 7b 6e 4b 65 key2. The {nKe
1b1cc 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65 79 20 6d y1, pKey1} key m
1b1cd 75 73 74 20 62 65 20 61 20 62 6c 6f 62 0a 2a 2a ust be a blob.**
1b1ce 20 63 72 65 61 74 65 64 20 62 79 20 74 68 20 4f created by th O
1b1cf 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 P_MakeRecord opc
1b1d0 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e ode of the VDBE.
1b1d1 20 20 54 68 65 20 70 50 4b 65 79 32 0a 2a 2a 20 The pPKey2.**
1b1d2 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 70 61 key must be a pa
1b1d3 72 73 65 64 20 6b 65 79 20 73 75 63 68 20 61 73 rsed key such as
1b1d4 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a obtained from.*
1b1d5 2a 20 73 71 6c 69 74 65 33 56 64 62 65 50 61 72 * sqlite3VdbePar
1b1d6 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 seRecord..**.**
1b1d7 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20 64 6f Key1 and Key2 do
1b1d8 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 63 6f 6e not have to con
1b1d9 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 6e 75 tain the same nu
1b1da 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 2e 0a mber of fields..
1b1db 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 74 68 20 ** The key with
1b1dc 66 65 77 65 72 20 66 69 65 6c 64 73 20 69 73 20 fewer fields is
1b1dd 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 72 65 73 usually compares
1b1de 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 0a less than the .
1b1df 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 2e 20 20 ** longer key.
1b1e0 48 6f 77 65 76 65 72 20 69 66 20 74 68 65 20 55 However if the U
1b1e1 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 NPACKED_INCRKEY
1b1e2 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 79 32 20 flags in pPKey2
1b1e3 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64 20 74 68 is set.** and th
1b1e4 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 e common prefixe
1b1e5 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68 65 s are equal, the
1b1e6 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74 n key1 is less t
1b1e7 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20 4f 72 20 han key2..** Or
1b1e8 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f if the UNPACKED_
1b1e9 4d 41 54 43 48 5f 50 52 45 46 49 58 20 66 6c 61 MATCH_PREFIX fla
1b1ea 67 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 65 g is set and the
1b1eb 20 70 72 65 66 69 78 65 73 20 61 72 65 0a 2a 2a prefixes are.**
1b1ec 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 74 68 65 equal, then the
1b1ed 20 6b 65 79 73 20 61 72 65 20 63 6f 6e 73 69 64 keys are consid
1b1ee 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c ered to be equal
1b1ef 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 74 and.** the part
1b1f0 73 20 62 65 79 6f 6e 64 20 74 68 65 20 63 6f 6d s beyond the com
1b1f1 6d 6f 6e 20 70 72 65 66 69 78 20 61 72 65 20 69 mon prefix are i
1b1f2 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 gnored..**.** If
1b1f3 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 the UNPACKED_IG
1b1f4 4e 4f 52 45 5f 52 4f 57 49 44 20 66 6c 61 67 20 NORE_ROWID flag
1b1f5 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 is set, then the
1b1f6 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a last byte of.**
1b1f7 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20 70 the header of p
1b1f8 4b 65 79 31 20 69 73 20 69 67 6e 6f 72 65 64 2e Key1 is ignored.
1b1f9 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 It is assumed
1b1fa 74 68 61 74 20 70 4b 65 79 31 20 69 73 0a 2a 2a that pKey1 is.**
1b1fb 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 2c 20 61 an index key, a
1b1fc 6e 64 20 74 68 75 73 20 65 6e 64 73 20 77 69 74 nd thus ends wit
1b1fd 68 20 61 20 72 6f 77 69 64 20 76 61 6c 75 65 2e h a rowid value.
1b1fe 20 20 54 68 65 20 6c 61 73 74 20 62 79 74 65 0a The last byte.
1b1ff 2a 2a 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 ** of the header
1b200 20 77 69 6c 6c 20 74 68 65 72 65 66 6f 72 65 20 will therefore
1b201 62 65 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 be the serial ty
1b202 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 3a pe of the rowid:
1b203 0a 2a 2a 20 6f 6e 65 20 6f 66 20 31 2c 20 32 2c .** one of 1, 2,
1b204 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 38 2c 20 3, 4, 5, 6, 8,
1b205 6f 72 20 39 20 2d 20 74 68 65 20 69 6e 74 65 67 or 9 - the integ
1b206 65 72 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e er serial types.
1b207 0a 2a 2a 20 54 68 65 20 73 65 72 69 61 6c 20 74 .** The serial t
1b208 79 70 65 20 6f 66 20 74 68 65 20 66 69 6e 61 6c ype of the final
1b209 20 72 6f 77 69 64 20 77 69 6c 6c 20 61 6c 77 61 rowid will alwa
1b20a 79 73 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 ys be a single b
1b20b 79 74 65 2e 0a 2a 2a 20 42 79 20 69 67 6e 6f 72 yte..** By ignor
1b20c 69 6e 67 20 74 68 69 73 20 6c 61 73 74 20 62 79 ing this last by
1b20d 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 te of the header
1b20e 2c 20 77 65 20 66 6f 72 63 65 20 74 68 65 20 63 , we force the c
1b20f 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 74 6f 20 omparison.** to
1b210 69 67 6e 6f 72 65 20 74 68 65 20 72 6f 77 69 64 ignore the rowid
1b211 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 6b at the end of k
1b212 65 79 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ey1..*/.SQLITE_P
1b213 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1b214 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 e3VdbeRecordComp
1b215 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b 65 79 31 are(. int nKey1
1b216 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b , const void *pK
1b217 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20 6b 65 79 ey1, /* Left key
1b218 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 */. UnpackedRe
1b219 63 6f 72 64 20 2a 70 50 4b 65 79 32 20 20 20 20 cord *pPKey2
1b21a 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6b 65 79 /* Right key
1b21b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 64 31 3b */.){. int d1;
1b21c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1b21d 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b ffset into aKey[
1b21e 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 61 20 65 ] of next data e
1b21f 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 lement */. u32
1b220 69 64 78 31 3b 20 20 20 20 20 20 20 20 20 20 2f idx1; /
1b221 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b * Offset into aK
1b222 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 68 65 61 ey[] of next hea
1b223 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 der element */.
1b224 20 75 33 32 20 73 7a 48 64 72 31 3b 20 20 20 20 u32 szHdr1;
1b225 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1b226 20 62 79 74 65 73 20 69 6e 20 68 65 61 64 65 72 bytes in header
1b227 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b */. int i = 0;
1b228 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 . int nField;.
1b229 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 int rc = 0;. c
1b22a 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1b22b 61 72 20 2a 61 4b 65 79 31 20 3d 20 28 63 6f 6e ar *aKey1 = (con
1b22c 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
1b22d 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b 65 79 49 *)pKey1;. KeyI
1b22e 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 nfo *pKeyInfo;.
1b22f 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20 20 70 4b Mem mem1;.. pK
1b230 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65 79 32 2d eyInfo = pPKey2-
1b231 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 6d 65 6d >pKeyInfo;. mem
1b232 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f 1.enc = pKeyInfo
1b233 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31 2e 64 62 ->enc;. mem1.db
1b234 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b = pKeyInfo->db;
1b235 0a 20 20 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20 . mem1.flags =
1b236 30 3b 0a 20 20 6d 65 6d 31 2e 75 2e 69 20 3d 20 0;. mem1.u.i =
1b237 30 3b 20 20 2f 2a 20 6e 6f 74 20 6e 65 65 64 65 0; /* not neede
1b238 64 2c 20 68 65 72 65 20 74 6f 20 73 69 6c 65 6e d, here to silen
1b239 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e ce compiler warn
1b23a 69 6e 67 20 2a 2f 0a 20 20 6d 65 6d 31 2e 7a 4d ing */. mem1.zM
1b23b 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 0a 20 20 alloc = 0;. .
1b23c 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 6e 74 idx1 = getVarint
1b23d 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 72 31 32(aKey1, szHdr1
1b23e 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 72 31 );. d1 = szHdr1
1b23f 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e ;. if( pPKey2->
1b240 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 flags & UNPACKED
1b241 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 7b _IGNORE_ROWID ){
1b242 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b 0a 20 . szHdr1--;.
1b243 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b }. nField = pK
1b244 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a eyInfo->nField;.
1b245 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c 73 7a while( idx1<sz
1b246 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 79 32 Hdr1 && i<pPKey2
1b247 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 ->nField ){.
1b248 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 31 u32 serial_type1
1b249 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 ;.. /* Read t
1b24a 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 20 he serial types
1b24b 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 6c 65 for the next ele
1b24c 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b 65 79 ment in each key
1b24d 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d . */. idx1 +=
1b24e 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 61 4b getVarint32( aK
1b24f 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61 6c ey1+idx1, serial
1b250 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69 66 _type1 );. if
1b251 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73 ( d1>=nKey1 && s
1b252 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1b253 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 TypeLen(serial_t
1b254 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b 3b ype1)>0 ) break;
1b255 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 .. /* Extract
1b256 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 62 the values to b
1b257 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20 e compared..
1b258 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71 6c */. d1 += sql
1b259 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 ite3VdbeSerialGe
1b25a 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65 t(&aKey1[d1], se
1b25b 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d rial_type1, &mem
1b25c 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 1);.. /* Do t
1b25d 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 he comparison.
1b25e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
1b25f 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 lite3MemCompare(
1b260 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e &mem1, &pPKey2->
1b261 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20 aMem[i],.
1b262 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b263 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70 i<nField ? p
1b264 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 KeyInfo->aColl[i
1b265 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20 ] : 0);. if(
1b266 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 62 rc!=0 ){. b
1b267 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1b268 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e i++;. }.. /* N
1b269 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 o memory allocat
1b26a 69 6f 6e 20 69 73 20 65 76 65 72 20 75 73 65 64 ion is ever used
1b26b 20 6f 6e 20 6d 65 6d 31 2e 20 2a 2f 0a 20 20 69 on mem1. */. i
1b26c 66 28 20 4e 45 56 45 52 28 6d 65 6d 31 2e 7a 4d f( NEVER(mem1.zM
1b26d 61 6c 6c 6f 63 29 20 29 20 73 71 6c 69 74 65 33 alloc) ) sqlite3
1b26e 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1b26f 6d 65 6d 31 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 mem1);.. /* If
1b270 74 68 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 the PREFIX_SEARC
1b271 48 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e H flag is set an
1b272 64 20 61 6c 6c 20 66 69 65 6c 64 73 20 65 78 63 d all fields exc
1b273 65 70 74 20 74 68 65 20 66 69 6e 61 6c 0a 20 20 ept the final.
1b274 2a 2a 20 72 6f 77 69 64 20 66 69 65 6c 64 20 77 ** rowid field w
1b275 65 72 65 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 ere equal, then
1b276 63 6c 65 61 72 20 74 68 65 20 50 52 45 46 49 58 clear the PREFIX
1b277 5f 53 45 41 52 43 48 20 66 6c 61 67 20 61 6e 64 _SEARCH flag and
1b278 20 73 65 74 20 0a 20 20 2a 2a 20 70 50 4b 65 79 set . ** pPKey
1b279 32 2d 3e 72 6f 77 69 64 20 74 6f 20 74 68 65 20 2->rowid to the
1b27a 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 77 value of the row
1b27b 69 64 20 66 69 65 6c 64 20 69 6e 20 28 70 4b 65 id field in (pKe
1b27c 79 31 2c 20 6e 4b 65 79 31 29 2e 0a 20 20 2a 2a y1, nKey1).. **
1b27d 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 This is used by
1b27e 20 74 68 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 the OP_IsUnique
1b27f 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 opcode.. */.
1b280 69 66 28 20 28 70 50 4b 65 79 32 2d 3e 66 6c 61 if( (pPKey2->fla
1b281 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 gs & UNPACKED_PR
1b282 45 46 49 58 5f 53 45 41 52 43 48 29 20 26 26 20 EFIX_SEARCH) &&
1b283 69 3d 3d 28 70 50 4b 65 79 32 2d 3e 6e 46 69 65 i==(pPKey2->nFie
1b284 6c 64 2d 31 29 20 29 7b 0a 20 20 20 20 61 73 73 ld-1) ){. ass
1b285 65 72 74 28 20 69 64 78 31 3d 3d 73 7a 48 64 72 ert( idx1==szHdr
1b286 31 20 26 26 20 72 63 20 29 3b 0a 20 20 20 20 61 1 && rc );. a
1b287 73 73 65 72 74 28 20 6d 65 6d 31 2e 66 6c 61 67 ssert( mem1.flag
1b288 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 s & MEM_Int );.
1b289 20 20 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 pPKey2->flags
1b28a 20 26 3d 20 7e 55 4e 50 41 43 4b 45 44 5f 50 52 &= ~UNPACKED_PR
1b28b 45 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 EFIX_SEARCH;.
1b28c 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64 20 3d pPKey2->rowid =
1b28d 20 6d 65 6d 31 2e 75 2e 69 3b 0a 20 20 7d 0a 0a mem1.u.i;. }..
1b28e 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 if( rc==0 ){.
1b28f 20 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65 72 65 /* rc==0 here
1b290 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 means that one
1b291 6f 66 20 74 68 65 20 6b 65 79 73 20 72 61 6e 20 of the keys ran
1b292 6f 75 74 20 6f 66 20 66 69 65 6c 64 73 20 61 6e out of fields an
1b293 64 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 74 68 65 d. ** all the
1b294 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 74 68 fields up to th
1b295 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20 65 71 at point were eq
1b296 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e 50 41 ual. If the UNPA
1b297 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a 20 20 20 CKED_INCRKEY.
1b298 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c ** flag is set,
1b299 20 74 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20 then break the
1b29a 74 69 65 20 62 79 20 74 72 65 61 74 69 6e 67 20 tie by treating
1b29b 6b 65 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0a key2 as larger..
1b29c 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 55 50 ** If the UP
1b29d 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 ACKED_PREFIX_MAT
1b29e 43 48 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 CH flag is set,
1b29f 74 68 65 6e 20 6b 65 79 73 20 77 69 74 68 20 63 then keys with c
1b2a0 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 73 0a 20 ommon prefixes.
1b2a1 20 20 20 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 ** are consid
1b2a2 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c ered to be equal
1b2a3 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 . Otherwise, th
1b2a4 65 20 6c 6f 6e 67 65 72 20 6b 65 79 20 69 73 20 e longer key is
1b2a5 74 68 65 20 0a 20 20 20 20 2a 2a 20 6c 61 72 67 the . ** larg
1b2a6 65 72 2e 20 20 41 73 20 69 74 20 68 61 70 70 65 er. As it happe
1b2a7 6e 73 2c 20 74 68 65 20 70 50 4b 65 79 32 20 77 ns, the pPKey2 w
1b2a8 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 74 68 ill always be th
1b2a9 65 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 e longer. **
1b2aa 69 66 20 74 68 65 72 65 20 69 73 20 61 20 64 69 if there is a di
1b2ab 66 66 65 72 65 6e 63 65 2e 0a 20 20 20 20 2a 2f fference.. */
1b2ac 0a 20 20 20 20 69 66 28 20 70 50 4b 65 79 32 2d . if( pPKey2-
1b2ad 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 >flags & UNPACKE
1b2ae 44 5f 49 4e 43 52 4b 45 59 20 29 7b 0a 20 20 20 D_INCRKEY ){.
1b2af 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20 20 20 20 rc = -1;.
1b2b0 7d 65 6c 73 65 20 69 66 28 20 70 50 4b 65 79 32 }else if( pPKey2
1b2b1 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b ->flags & UNPACK
1b2b2 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 ED_PREFIX_MATCH
1b2b3 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 ){. /* Leav
1b2b4 65 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 20 20 7d e rc==0 */. }
1b2b5 65 6c 73 65 20 69 66 28 20 69 64 78 31 3c 73 7a else if( idx1<sz
1b2b6 48 64 72 31 20 29 7b 0a 20 20 20 20 20 20 72 63 Hdr1 ){. rc
1b2b7 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 = 1;. }. }e
1b2b8 6c 73 65 20 69 66 28 20 70 4b 65 79 49 6e 66 6f lse if( pKeyInfo
1b2b9 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26 26 20 ->aSortOrder &&
1b2ba 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 i<pKeyInfo->nFie
1b2bb 6c 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ld.
1b2bc 20 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 && pKeyInfo->a
1b2bd 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a SortOrder[i] ){.
1b2be 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 rc = -rc;.
1b2bf 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
1b2c0 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20 }. ../*.** pCur
1b2c1 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 69 6e 64 points at an ind
1b2c2 65 78 20 65 6e 74 72 79 20 63 72 65 61 74 65 64 ex entry created
1b2c3 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 using the OP_Ma
1b2c4 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e keRecord opcode.
1b2c5 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 72 6f 77 .** Read the row
1b2c6 69 64 20 28 74 68 65 20 6c 61 73 74 20 66 69 65 id (the last fie
1b2c7 6c 64 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ld in the record
1b2c8 29 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 ) and store it i
1b2c9 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74 n *rowid..** Ret
1b2ca 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
1b2cb 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b everything work
1b2cc 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 s, or an error c
1b2cd 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a ode otherwise..*
1b2ce 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 67 68 74 20 *.** pCur might
1b2cf 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 be pointing to t
1b2d0 65 78 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ext obtained fro
1b2d1 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 m a corrupt data
1b2d2 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 53 6f base file..** So
1b2d3 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 61 6e the content can
1b2d4 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 not be trusted.
1b2d5 20 44 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 Do appropriate
1b2d6 63 68 65 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f checks on the co
1b2d7 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ntent..*/.SQLITE
1b2d8 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1b2d9 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 ite3VdbeIdxRowid
1b2da 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 (sqlite3 *db, Bt
1b2db 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 Cursor *pCur, i6
1b2dc 34 20 2a 72 6f 77 69 64 29 7b 0a 20 20 69 36 34 4 *rowid){. i64
1b2dd 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 nCellKey = 0;.
1b2de 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20 73 int rc;. u32 s
1b2df 7a 48 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 zHdr; /*
1b2e0 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 Size of the head
1b2e1 65 72 20 2a 2f 0a 20 20 75 33 32 20 74 79 70 65 er */. u32 type
1b2e2 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 53 65 72 Rowid; /* Ser
1b2e3 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 ial type of the
1b2e4 72 6f 77 69 64 20 2a 2f 0a 20 20 75 33 32 20 6c rowid */. u32 l
1b2e5 65 6e 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a 20 enRowid; /*
1b2e6 53 69 7a 65 20 6f 66 20 74 68 65 20 72 6f 77 69 Size of the rowi
1b2e7 64 20 2a 2f 0a 20 20 4d 65 6d 20 6d 2c 20 76 3b d */. Mem m, v;
1b2e8 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d .. UNUSED_PARAM
1b2e9 45 54 45 52 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 ETER(db);.. /*
1b2ea 47 65 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 Get the size of
1b2eb 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 2e the index entry.
1b2ec 20 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73 20 65 Only indices e
1b2ed 6e 74 72 69 65 73 20 6f 66 20 6c 65 73 73 0a 20 ntries of less.
1b2ee 20 2a 2a 20 74 68 61 6e 20 32 47 69 42 20 61 72 ** than 2GiB ar
1b2ef 65 20 73 75 70 70 6f 72 74 20 2d 20 61 6e 79 74 e support - anyt
1b2f0 68 69 6e 67 20 6c 61 72 67 65 20 6d 75 73 74 20 hing large must
1b2f1 62 65 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 be database corr
1b2f2 75 70 74 69 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 79 uption.. ** Any
1b2f3 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64 corruption is d
1b2f4 65 74 65 63 74 65 64 20 69 6e 20 73 71 6c 69 74 etected in sqlit
1b2f5 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c e3BtreeParseCell
1b2f6 50 74 72 28 29 2c 20 74 68 6f 75 67 68 2c 20 73 Ptr(), though, s
1b2f7 6f 0a 20 20 2a 2a 20 74 68 69 73 20 63 6f 64 65 o. ** this code
1b2f8 20 63 61 6e 20 73 61 66 65 6c 79 20 61 73 73 75 can safely assu
1b2f9 6d 65 20 74 68 61 74 20 6e 43 65 6c 6c 4b 65 79 me that nCellKey
1b2fa 20 69 73 20 33 32 2d 62 69 74 73 20 20 0a 20 20 is 32-bits .
1b2fb 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c */. assert( sql
1b2fc 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
1b2fd 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a sValid(pCur) );.
1b2fe 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1b2ff 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c reeKeySize(pCur,
1b300 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 &nCellKey);. a
1b301 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1b302 45 5f 4f 4b 20 29 3b 20 20 20 20 20 2f 2a 20 70 E_OK ); /* p
1b303 43 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 61 Cur is always va
1b304 6c 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 lid so KeySize c
1b305 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 annot fail */.
1b306 61 73 73 65 72 74 28 20 28 6e 43 65 6c 6c 4b 65 assert( (nCellKe
1b307 79 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 y & SQLITE_MAX_U
1b308 33 32 29 3d 3d 28 75 36 34 29 6e 43 65 6c 6c 4b 32)==(u64)nCellK
1b309 65 79 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 ey );.. /* Read
1b30a 20 69 6e 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 in the complete
1b30b 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 content of the
1b30c 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f 0a 20 index entry */.
1b30d 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 memset(&m, 0, s
1b30e 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 63 20 izeof(m));. rc
1b30f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
1b310 46 72 6f 6d 42 74 72 65 65 28 70 43 75 72 2c 20 FromBtree(pCur,
1b311 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 0, (int)nCellKey
1b312 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 , 1, &m);. if(
1b313 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e rc ){. return
1b314 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 rc;. }.. /* T
1b315 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 6d he index entry m
1b316 75 73 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 ust begin with a
1b317 20 68 65 61 64 65 72 20 73 69 7a 65 20 2a 2f 0a header size */.
1b318 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 69 6e (void)getVarin
1b319 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20 73 7a t32((u8*)m.z, sz
1b31a 48 64 72 29 3b 0a 20 20 74 65 73 74 63 61 73 65 Hdr);. testcase
1b31b 28 20 73 7a 48 64 72 3d 3d 33 20 29 3b 0a 20 20 ( szHdr==3 );.
1b31c 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d testcase( szHdr=
1b31d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 20 75 6e =m.n );. if( un
1b31e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 3c 33 20 7c likely(szHdr<3 |
1b31f 7c 20 28 69 6e 74 29 73 7a 48 64 72 3e 6d 2e 6e | (int)szHdr>m.n
1b320 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 ) ){. goto id
1b321 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 x_rowid_corrupti
1b322 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 on;. }.. /* Th
1b323 65 20 6c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 e last field of
1b324 74 68 65 20 69 6e 64 65 78 20 73 68 6f 75 6c 64 the index should
1b325 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 2d be an integer -
1b326 20 74 68 65 20 52 4f 57 49 44 2e 0a 20 20 2a 2a the ROWID.. **
1b327 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 Verify that the
1b328 20 6c 61 73 74 20 65 6e 74 72 79 20 72 65 61 6c last entry real
1b329 6c 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 ly is an integer
1b32a 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 . */. (void)get
1b32b 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29 26 6d Varint32((u8*)&m
1b32c 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70 .z[szHdr-1], typ
1b32d 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 74 63 eRowid);. testc
1b32e 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1b32f 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 1 );. testcase(
1b330 20 74 79 70 65 52 6f 77 69 64 3d 3d 32 20 29 3b typeRowid==2 );
1b331 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1b332 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0a 20 20 74 eRowid==3 );. t
1b333 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1b334 69 64 3d 3d 34 20 29 3b 0a 20 20 74 65 73 74 63 id==4 );. testc
1b335 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1b336 35 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 5 );. testcase(
1b337 20 74 79 70 65 52 6f 77 69 64 3d 3d 36 20 29 3b typeRowid==6 );
1b338 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1b339 65 52 6f 77 69 64 3d 3d 38 20 29 3b 0a 20 20 74 eRowid==8 );. t
1b33a 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1b33b 69 64 3d 3d 39 20 29 3b 0a 20 20 69 66 28 20 75 id==9 );. if( u
1b33c 6e 6c 69 6b 65 6c 79 28 74 79 70 65 52 6f 77 69 nlikely(typeRowi
1b33d 64 3c 31 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 d<1 || typeRowid
1b33e 3e 39 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 3d >9 || typeRowid=
1b33f 3d 37 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 =7) ){. goto
1b340 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 idx_rowid_corrup
1b341 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c 65 6e 52 tion;. }. lenR
1b342 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 56 64 owid = sqlite3Vd
1b343 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1b344 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 typeRowid);. te
1b345 73 74 63 61 73 65 28 20 28 75 33 32 29 6d 2e 6e stcase( (u32)m.n
1b346 3d 3d 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64 ==szHdr+lenRowid
1b347 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 );. if( unlike
1b348 6c 79 28 28 75 33 32 29 6d 2e 6e 3c 73 7a 48 64 ly((u32)m.n<szHd
1b349 72 2b 6c 65 6e 52 6f 77 69 64 29 20 29 7b 0a 20 r+lenRowid) ){.
1b34a 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 goto idx_rowi
1b34b 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 d_corruption;.
1b34c 7d 0a 0a 20 20 2f 2a 20 46 65 74 63 68 20 74 68 }.. /* Fetch th
1b34d 65 20 69 6e 74 65 67 65 72 20 6f 66 66 20 74 68 e integer off th
1b34e 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 e end of the ind
1b34f 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 ex record */. s
1b350 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1b351 47 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e Get((u8*)&m.z[m.
1b352 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 n-lenRowid], typ
1b353 65 52 6f 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a eRowid, &v);. *
1b354 72 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 rowid = v.u.i;.
1b355 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1b356 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 elease(&m);. re
1b357 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1b358 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 . /* Jump here
1b359 69 66 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 if database corr
1b35a 75 70 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 uption is detect
1b35b 65 64 20 61 66 74 65 72 20 6d 20 68 61 73 20 62 ed after m has b
1b35c 65 65 6e 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 een. ** allocat
1b35d 65 64 2e 20 20 46 72 65 65 20 74 68 65 20 6d 20 ed. Free the m
1b35e 6f 62 6a 65 63 74 20 61 6e 64 20 72 65 74 75 72 object and retur
1b35f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1b360 2e 20 2a 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 . */.idx_rowid_c
1b361 6f 72 72 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 orruption:. tes
1b362 74 63 61 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 tcase( m.zMalloc
1b363 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 !=0 );. sqlite3
1b364 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1b365 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c m);. return SQL
1b366 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1b367 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 ;.}../*.** Compa
1b368 72 65 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 re the key of th
1b369 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 e index entry th
1b36a 61 74 20 63 75 72 73 6f 72 20 70 43 20 69 73 20 at cursor pC is
1b36b 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 67 61 69 pointing to agai
1b36c 6e 73 74 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 nst.** the key s
1b36d 74 72 69 6e 67 20 69 6e 20 70 55 6e 70 61 63 6b tring in pUnpack
1b36e 65 64 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 ed. Write into
1b36f 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72 0a 2a *pRes a number.*
1b370 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 74 69 * that is negati
1b371 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 ve, zero, or pos
1b372 69 74 69 76 65 20 69 66 20 70 43 20 69 73 20 6c itive if pC is l
1b373 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 ess than, equal
1b374 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 to,.** or greate
1b375 72 20 74 68 61 6e 20 70 55 6e 70 61 63 6b 65 64 r than pUnpacked
1b376 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 . Return SQLITE
1b377 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a _OK on success..
1b378 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 64 20 **.** pUnpacked
1b379 69 73 20 65 69 74 68 65 72 20 63 72 65 61 74 65 is either create
1b37a 64 20 77 69 74 68 6f 75 74 20 61 20 72 6f 77 69 d without a rowi
1b37b 64 20 6f 72 20 69 73 20 74 72 75 6e 63 61 74 65 d or is truncate
1b37c 64 20 73 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 d so that it.**
1b37d 6f 6d 69 74 73 20 74 68 65 20 72 6f 77 69 64 20 omits the rowid
1b37e 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68 65 at the end. The
1b37f 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e rowid at the en
1b380 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 d of the index e
1b381 6e 74 72 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 ntry.** is ignor
1b382 65 64 20 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e ed as well. Hen
1b383 63 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ce, this routine
1b384 20 6f 6e 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 only compares t
1b385 68 65 20 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 he prefixes .**
1b386 6f 66 20 74 68 65 20 6b 65 79 73 20 70 72 69 6f of the keys prio
1b387 72 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 r to the final r
1b388 6f 77 69 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e owid, not the en
1b389 74 69 72 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c tire key..*/.SQL
1b38a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1b38b 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 sqlite3VdbeIdxKe
1b38c 79 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65 yCompare(. Vdbe
1b38d 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 Cursor *pC,
1b38e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
1b38f 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 ursor to compare
1b390 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e against */. Un
1b391 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 packedRecord *pU
1b392 6e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 npacked, /* Unp
1b393 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 acked version of
1b394 20 6b 65 79 20 74 6f 20 63 6f 6d 70 61 72 65 20 key to compare
1b395 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 against */. int
1b396 20 2a 72 65 73 20 20 20 20 20 20 20 20 20 20 20 *res
1b397 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
1b398 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e e the comparison
1b399 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a result here */.
1b39a 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 ){. i64 nCellKe
1b39b 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b y = 0;. int rc;
1b39c 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
1b39d 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b r = pC->pCursor;
1b39e 0a 20 20 4d 65 6d 20 6d 3b 0a 0a 20 20 61 73 73 . Mem m;.. ass
1b39f 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1b3a0 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70 eCursorIsValid(p
1b3a1 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 Cur) );. rc = s
1b3a2 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 qlite3BtreeKeySi
1b3a3 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b ze(pCur, &nCellK
1b3a4 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 ey);. assert( r
1b3a5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 c==SQLITE_OK );
1b3a6 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20 61 6c /* pCur is al
1b3a7 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 ways valid so Ke
1b3a8 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 ySize cannot fai
1b3a9 6c 20 2a 2f 0a 20 20 2f 2a 20 6e 43 65 6c 6c 4b l */. /* nCellK
1b3aa 65 79 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 ey will always b
1b3ab 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 e between 0 and
1b3ac 30 78 66 66 66 66 66 66 66 66 20 62 65 63 61 75 0xffffffff becau
1b3ad 73 65 20 6f 66 20 74 68 65 20 73 61 79 0a 20 20 se of the say.
1b3ae 2a 2a 20 74 68 61 74 20 62 74 72 65 65 50 61 72 ** that btreePar
1b3af 73 65 43 65 6c 6c 50 74 72 28 29 20 61 6e 64 20 seCellPtr() and
1b3b0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 sqlite3GetVarint
1b3b1 33 32 28 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 32() are impleme
1b3b2 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 6e 43 nted */. if( nC
1b3b3 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 ellKey<=0 || nCe
1b3b4 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66 llKey>0x7fffffff
1b3b5 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 30 ){. *res = 0
1b3b6 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
1b3b7 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 7d ITE_CORRUPT;. }
1b3b8 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c . memset(&m, 0,
1b3b9 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 sizeof(m));. r
1b3ba 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
1b3bb 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e emFromBtree(pC->
1b3bc 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 69 6e 74 pCursor, 0, (int
1b3bd 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d )nCellKey, 1, &m
1b3be 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
1b3bf 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
1b3c0 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e 70 }. assert( pUnp
1b3c1 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20 55 acked->flags & U
1b3c2 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 NPACKED_IGNORE_R
1b3c3 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 20 3d OWID );. *res =
1b3c4 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f sqlite3VdbeReco
1b3c5 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d rdCompare(m.n, m
1b3c6 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b 0a .z, pUnpacked);.
1b3c7 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b3c8 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 Release(&m);. r
1b3c9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1b3ca 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
1b3cb 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 outine sets the
1b3cc 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74 75 value to be retu
1b3cd 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65 rned by subseque
1b3ce 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 nt calls to.** s
1b3cf 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 qlite3_changes()
1b3d0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1b3d1 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a handle 'db'. .*
1b3d2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b3d3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1b3d4 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c 69 eSetChanges(sqli
1b3d5 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68 te3 *db, int nCh
1b3d6 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 ange){. assert(
1b3d7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b3d8 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
1b3d9 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 ;. db->nChange
1b3da 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d = nChange;. db-
1b3db 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d >nTotalChange +=
1b3dc 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a nChange;.}../*.
1b3dd 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 6e ** Set a flag in
1b3de 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 64 the vdbe to upd
1b3df 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 63 ate the change c
1b3e0 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 ounter when it i
1b3e1 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f s finalised.** o
1b3e2 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 r reset..*/.SQLI
1b3e3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1b3e4 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 sqlite3VdbeCount
1b3e5 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76 29 Changes(Vdbe *v)
1b3e6 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74 {. v->changeCnt
1b3e7 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a On = 1;.}../*.**
1b3e8 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65 70 Mark every prep
1b3e9 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 ared statement a
1b3ea 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
1b3eb 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1b3ec 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72 tion.** as expir
1b3ed 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 ed..**.** An exp
1b3ee 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d ired statement m
1b3ef 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d 70 eans that recomp
1b3f0 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ilation of the s
1b3f1 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 tatement is.** r
1b3f2 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65 ecommend. State
1b3f3 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68 65 ments expire whe
1b3f4 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e 20 n things happen
1b3f5 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72 0a that make their.
1b3f6 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f ** programs obso
1b3f7 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 lete. Removing
1b3f8 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e user-defined fun
1b3f9 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 ctions or collat
1b3fa 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 ing.** sequences
1b3fb 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 6e , or changing an
1b3fc 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 authorization f
1b3fd 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 unction are the
1b3fe 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e types of.** thin
1b3ff 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 65 gs that make pre
1b400 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 pared statements
1b401 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 obsolete..*/.SQ
1b402 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1b403 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 d sqlite3ExpireP
1b404 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 reparedStatement
1b405 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a s(sqlite3 *db){.
1b406 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 Vdbe *p;. for
1b407 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20 (p = db->pVdbe;
1b408 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a p; p=p->pNext){.
1b409 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d p->expired =
1b40a 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 1;. }.}../*.**
1b40b 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 Return the data
1b40c 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 20 base associated
1b40d 77 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a 2a with the Vdbe..*
1b40e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b40f 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 sqlite3 *sqlite
1b410 33 56 64 62 65 44 62 28 56 64 62 65 20 2a 76 29 3VdbeDb(Vdbe *v)
1b411 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64 62 {. return v->db
1b412 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;.}../**********
1b413 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 **** End of vdbe
1b414 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a aux.c **********
1b415 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b416 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b417 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1b418 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1b419 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a vdbeapi.c ******
1b41a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b41b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b41c 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
1b41d 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 May 26.**.** The
1b41e 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
1b41f 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
1b420 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
1b421 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
1b422 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
1b423 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
1b424 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
1b425 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
1b426 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1b427 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1b428 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1b429 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1b42a 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1b42b 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1b42c 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
1b42d 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
1b42e 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
1b42f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b431 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b432 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b433 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1b434 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
1b435 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65 ode use to imple
1b436 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74 20 61 ment APIs that a
1b437 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a re part of the.*
1b438 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 * VDBE..**.** $I
1b439 64 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 20 31 d: vdbeapi.c,v 1
1b43a 2e 31 36 37 20 32 30 30 39 2f 30 36 2f 32 35 20 .167 2009/06/25
1b43b 30 31 3a 34 37 3a 31 32 20 64 72 68 20 45 78 70 01:47:12 drh Exp
1b43c 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 $.*/..#ifndef S
1b43d 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 QLITE_OMIT_DEPRE
1b43e 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 CATED./*.** Retu
1b43f 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 rn TRUE (non-zer
1b440 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d o) of the statem
1b441 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 ent supplied as
1b442 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64 an argument need
1b443 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d s.** to be recom
1b444 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d piled. A statem
1b445 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 ent needs to be
1b446 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65 recompiled whene
1b447 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75 ver the.** execu
1b448 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 tion environment
1b449 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61 changes in a wa
1b44a 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74 y that would alt
1b44b 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a er the program.*
1b44c 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70 * that sqlite3_p
1b44d 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74 repare() generat
1b44e 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 es. For example
1b44f 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f , if new functio
1b450 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69 ns or.** collati
1b451 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 ng sequences are
1b452 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69 registered or i
1b453 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 f an authorizer
1b454 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 function is.** a
1b455 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e dded or changed.
1b456 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1b457 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 nt sqlite3_expir
1b458 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ed(sqlite3_stmt
1b459 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
1b45a 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *p = (Vdbe*)pStm
1b45b 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 t;. return p==0
1b45c 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a || p->expired;.
1b45d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1b45e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
1b45f 75 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 utine destroys a
1b460 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1b461 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64 that is created
1b462 20 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 by.** the sqlit
1b463 65 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75 e3_compile() rou
1b464 74 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65 tine. The intege
1b465 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e r returned is an
1b466 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63 SQLITE_.** succ
1b467 65 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65 ess/failure code
1b468 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
1b469 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 the result of ex
1b46a 65 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 74 ecuting the virt
1b46b 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a ual.** machine..
1b46c 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1b46d 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f ne sets the erro
1b46e 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e r code and strin
1b46f 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a g returned by.**
1b470 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 sqlite3_errcode
1b471 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d (), sqlite3_errm
1b472 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 sg() and sqlite3
1b473 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a _errmsg16()..*/.
1b474 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b475 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
1b476 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1b477 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a tmt){. int rc;.
1b478 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 if( pStmt==0 )
1b479 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1b47a 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
1b47b 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 Vdbe *v = (Vd
1b47c 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 be*)pStmt;. s
1b47d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e qlite3 *db = v->
1b47e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 db;.#if SQLITE_T
1b47f 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 HREADSAFE. sq
1b480 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
1b481 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 ex = v->db->mute
1b482 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 x;.#endif. sq
1b483 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1b484 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 r(mutex);. rc
1b485 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 = sqlite3VdbeFi
1b486 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72 nalize(v);. r
1b487 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
1b488 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 it(db, rc);.
1b489 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1b48a 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a ave(mutex);. }.
1b48b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1b48c 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 20 /*.** Terminate
1b48d 74 68 65 20 63 75 72 72 65 6e 74 20 65 78 65 63 the current exec
1b48e 75 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 ution of an SQL
1b48f 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 statement and re
1b490 73 65 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 74 set it.** back t
1b491 6f 20 69 74 73 20 73 74 61 72 74 69 6e 67 20 73 o its starting s
1b492 74 61 74 65 20 73 6f 20 74 68 61 74 20 69 74 20 tate so that it
1b493 63 61 6e 20 62 65 20 72 65 75 73 65 64 2e 20 41 can be reused. A
1b494 20 73 75 63 63 65 73 73 20 63 6f 64 65 20 66 72 success code fr
1b495 6f 6d 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 20 om.** the prior
1b496 65 78 65 63 75 74 69 6f 6e 20 69 73 20 72 65 74 execution is ret
1b497 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 urned..**.** Thi
1b498 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 s routine sets t
1b499 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e he error code an
1b49a 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 d string returne
1b49b 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f d by.** sqlite3_
1b49c 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 errcode(), sqlit
1b49d 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 e3_errmsg() and
1b49e 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 sqlite3_errmsg16
1b49f 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
1b4a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 I int sqlite3_re
1b4a1 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 set(sqlite3_stmt
1b4a2 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 *pStmt){. int
1b4a3 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d rc;. if( pStmt=
1b4a4 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 =0 ){. rc = S
1b4a5 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
1b4a6 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d e{. Vdbe *v =
1b4a7 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
1b4a8 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1b4a9 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75 _enter(v->db->mu
1b4aa 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 tex);. rc = s
1b4ab 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 qlite3VdbeReset(
1b4ac 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 v);. sqlite3V
1b4ad 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 dbeMakeReady(v,
1b4ae 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 -1, 0, 0, 0, 0,
1b4af 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 0);. assert(
1b4b0 28 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 (rc & (v->db->er
1b4b1 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20 rMask))==rc );.
1b4b2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 rc = sqlite3A
1b4b3 70 69 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63 piExit(v->db, rc
1b4b4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
1b4b5 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 utex_leave(v->db
1b4b6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
1b4b7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1b4b8 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20 .** Set all the
1b4b9 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68 parameters in th
1b4ba 65 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 e compiled SQL s
1b4bb 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c tatement to NULL
1b4bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1b4bd 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 int sqlite3_clea
1b4be 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 r_bindings(sqlit
1b4bf 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1b4c0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
1b4c1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1b4c2 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 Vdbe *p = (Vdb
1b4c3 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 e*)pStmt;.#if SQ
1b4c4 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
1b4c5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
1b4c6 2a 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a *mutex = ((Vdbe*
1b4c7 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 )pStmt)->db->mut
1b4c8 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c ex;.#endif. sql
1b4c9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1b4ca 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 (mutex);. for(i
1b4cb 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 =0; i<p->nVar; i
1b4cc 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
1b4cd 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1b4ce 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 p->aVar[i]);.
1b4cf 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 p->aVar[i].flag
1b4d0 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1b4d1 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
1b4d2 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a x_leave(mutex);.
1b4d3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1b4d4 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1b4d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 ************** s
1b4d6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a qlite3_value_ *
1b4d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b4d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1b4d9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1b4da 72 6f 75 74 69 6e 65 73 20 65 78 74 72 61 63 74 routines extract
1b4db 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f information fro
1b4dc 6d 20 61 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 m a Mem or sqlit
1b4dd 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 e3_value.** stru
1b4de 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cture..*/.SQLITE
1b4df 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 _API const void
1b4e0 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 *sqlite3_value_b
1b4e1 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 lob(sqlite3_valu
1b4e2 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 e *pVal){. Mem
1b4e3 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b *p = (Mem*)pVal;
1b4e4 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 . if( p->flags
1b4e5 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f & (MEM_Blob|MEM_
1b4e6 53 74 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 Str) ){. sqli
1b4e7 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
1b4e8 42 6c 6f 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e Blob(p);. p->
1b4e9 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 flags &= ~MEM_St
1b4ea 72 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 r;. p->flags
1b4eb 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 |= MEM_Blob;.
1b4ec 20 72 65 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 return p->z;.
1b4ed 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
1b4ee 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f n sqlite3_value_
1b4ef 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a text(pVal);. }.
1b4f0 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 }.SQLITE_API int
1b4f1 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
1b4f2 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c ytes(sqlite3_val
1b4f3 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue *pVal){. ret
1b4f4 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 urn sqlite3Value
1b4f5 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 Bytes(pVal, SQLI
1b4f6 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 TE_UTF8);.}.SQLI
1b4f7 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1b4f8 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 e3_value_bytes16
1b4f9 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
1b4fa 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
1b4fb 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 sqlite3ValueByte
1b4fc 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 s(pVal, SQLITE_U
1b4fd 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 TF16NATIVE);.}.S
1b4fe 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 QLITE_API double
1b4ff 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 sqlite3_value_d
1b500 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 ouble(sqlite3_va
1b501 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 lue *pVal){. re
1b502 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 turn sqlite3Vdbe
1b503 52 65 61 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 RealValue((Mem*)
1b504 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pVal);.}.SQLITE_
1b505 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1b506 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 value_int(sqlite
1b507 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
1b508 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 73 71 return (int)sq
1b509 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1b50a 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d e((Mem*)pVal);.}
1b50b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1b50c 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 te_int64 sqlite3
1b50d 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c _value_int64(sql
1b50e 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
1b50f 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
1b510 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 te3VdbeIntValue(
1b511 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 (Mem*)pVal);.}.S
1b512 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1b513 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 unsigned char *s
1b514 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1b515 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 t(sqlite3_value
1b516 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e *pVal){. return
1b517 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 (const unsigned
1b518 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 56 char *)sqlite3V
1b519 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 alueText(pVal, S
1b51a 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 QLITE_UTF8);.}.#
1b51b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1b51c 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1b51d 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1b51e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1b51f 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c xt16(sqlite3_val
1b520 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue* pVal){. ret
1b521 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 urn sqlite3Value
1b522 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 Text(pVal, SQLIT
1b523 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a E_UTF16NATIVE);.
1b524 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e }.SQLITE_API con
1b525 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1b526 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 _value_text16be(
1b527 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1b528 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 Val){. return s
1b529 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
1b52a 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 pVal, SQLITE_UTF
1b52b 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 16BE);.}.SQLITE_
1b52c 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1b52d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1b52e 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 xt16le(sqlite3_v
1b52f 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 alue *pVal){. r
1b530 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c eturn sqlite3Val
1b531 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c ueText(pVal, SQL
1b532 49 54 45 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a ITE_UTF16LE);.}.
1b533 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1b534 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 _OMIT_UTF16 */.S
1b535 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1b536 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
1b537 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 (sqlite3_value*
1b538 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
1b539 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f pVal->type;.}../
1b53a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b53b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c ************ sql
1b53c 69 74 65 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a ite3_result_ **
1b53d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b53e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1b53f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
1b540 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 outines are used
1b541 20 62 79 20 75 73 65 72 2d 64 65 66 69 6e 65 64 by user-defined
1b542 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70 functions to sp
1b543 65 63 69 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e ecify.** the fun
1b544 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a ction result..**
1b545 0a 2a 2a 20 54 68 65 20 73 65 74 53 74 72 4f 72 .** The setStrOr
1b546 45 72 72 6f 72 28 29 20 66 75 6e 74 69 6f 6e 20 Error() funtion
1b547 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 56 64 62 calls sqlite3Vdb
1b548 65 4d 65 6d 53 65 74 53 74 72 28 29 20 74 6f 20 eMemSetStr() to
1b549 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 store the.** res
1b54a 75 6c 74 20 61 73 20 61 20 73 74 72 69 6e 67 20 ult as a string
1b54b 6f 72 20 62 6c 6f 62 20 62 75 74 20 69 66 20 74 or blob but if t
1b54c 68 65 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f he string or blo
1b54d 62 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 b is too large,
1b54e 69 74 0a 2a 2a 20 74 68 65 6e 20 73 65 74 73 20 it.** then sets
1b54f 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 the error code t
1b550 6f 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 0a o SQLITE_TOOBIG.
1b551 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
1b552 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 etResultStrOrErr
1b553 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f or(. sqlite3_co
1b554 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 2f 2a ntext *pCtx, /*
1b555 20 46 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 Function contex
1b556 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 t */. const cha
1b557 72 20 2a 7a 2c 20 20 20 20 20 20 20 20 20 20 2f r *z, /
1b558 2a 20 53 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 * String pointer
1b559 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 */. int n,
1b55a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1b55b 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67 Bytes in string
1b55c 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f , or negative */
1b55d 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 . u8 enc,
1b55e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e /* En
1b55f 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 coding of z. 0
1b560 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 for BLOBs */. v
1b561 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1b562 2a 29 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75 *) /* Destru
1b563 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ctor function */
1b564 0a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 .){. if( sqlite
1b565 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 3VdbeMemSetStr(&
1b566 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 65 pCtx->s, z, n, e
1b567 6e 63 2c 20 78 44 65 6c 29 3d 3d 53 51 4c 49 54 nc, xDel)==SQLIT
1b568 45 5f 54 4f 4f 42 49 47 20 29 7b 0a 20 20 20 20 E_TOOBIG ){.
1b569 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
1b56a 72 72 6f 72 5f 74 6f 6f 62 69 67 28 70 43 74 78 rror_toobig(pCtx
1b56b 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f );. }.}.SQLITE_
1b56c 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b56d 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 _result_blob(.
1b56e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b56f 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 *pCtx, . const
1b570 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 void *z, . int
1b571 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 n, . void (*xDe
1b572 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 l)(void *).){.
1b573 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a assert( n>=0 );.
1b574 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b575 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1b576 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1b577 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 );. setResultSt
1b578 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a rOrError(pCtx, z
1b579 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d , n, 0, xDel);.}
1b57a 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b57b 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1b57c 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 double(sqlite3_c
1b57d 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 64 6f ontext *pCtx, do
1b57e 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 uble rVal){. as
1b57f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b580 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b581 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b582 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1b583 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e etDouble(&pCtx->
1b584 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 s, rVal);.}.SQLI
1b585 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
1b586 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
1b587 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b588 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 *pCtx, const ch
1b589 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 ar *z, int n){.
1b58a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b58b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
1b58c 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
1b58d 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f ;. pCtx->isErro
1b58e 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 r = SQLITE_ERROR
1b58f 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1b590 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e emSetStr(&pCtx->
1b591 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f s, z, n, SQLITE_
1b592 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 41 UTF8, SQLITE_TRA
1b593 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 NSIENT);.}.#ifnd
1b594 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
1b595 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 TF16.SQLITE_API
1b596 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b597 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 ult_error16(sqli
1b598 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1b599 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a x, const void *z
1b59a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 , int n){. asse
1b59b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b59c 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1b59d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
1b59e 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 Ctx->isError = S
1b59f 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 QLITE_ERROR;. s
1b5a0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1b5a1 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c Str(&pCtx->s, z,
1b5a2 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 n, SQLITE_UTF16
1b5a3 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 NATIVE, SQLITE_T
1b5a4 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e RANSIENT);.}.#en
1b5a5 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 dif.SQLITE_API v
1b5a6 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1b5a7 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 lt_int(sqlite3_c
1b5a8 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e ontext *pCtx, in
1b5a9 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 t iVal){. asser
1b5aa 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b5ab 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b5ac 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
1b5ad 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 lite3VdbeMemSetI
1b5ae 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 28 nt64(&pCtx->s, (
1b5af 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c i64)iVal);.}.SQL
1b5b0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b5b1 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 ite3_result_int6
1b5b2 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 4(sqlite3_contex
1b5b3 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 56 61 t *pCtx, i64 iVa
1b5b4 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 l){. assert( sq
1b5b5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b5b6 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b5b7 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
1b5b8 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 VdbeMemSetInt64(
1b5b9 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b &pCtx->s, iVal);
1b5ba 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
1b5bb 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b5bc 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 t_null(sqlite3_c
1b5bd 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 ontext *pCtx){.
1b5be 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b5bf 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 _mutex_held(pCtx
1b5c0 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ->s.db->mutex) )
1b5c1 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1b5c2 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d emSetNull(&pCtx-
1b5c3 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 >s);.}.SQLITE_AP
1b5c4 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1b5c5 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 73 71 esult_text(. sq
1b5c6 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b5c7 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 Ctx, . const ch
1b5c8 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c ar *z, . int n,
1b5c9 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
1b5ca 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 void *).){. ass
1b5cb 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b5cc 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e ex_held(pCtx->s.
1b5cd 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
1b5ce 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 setResultStrOrEr
1b5cf 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 ror(pCtx, z, n,
1b5d0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 SQLITE_UTF8, xDe
1b5d1 6c 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 l);.}.#ifndef SQ
1b5d2 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a LITE_OMIT_UTF16.
1b5d3 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1b5d4 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
1b5d5 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 ext16(. sqlite3
1b5d6 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b5d7 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
1b5d8 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 , . int n, . v
1b5d9 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1b5da 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 *).){. assert(
1b5db 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b5dc 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b5dd 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 mutex) );. setR
1b5de 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 esultStrOrError(
1b5df 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 pCtx, z, n, SQLI
1b5e0 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 TE_UTF16NATIVE,
1b5e1 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f xDel);.}.SQLITE_
1b5e2 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1b5e3 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 _result_text16be
1b5e4 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
1b5e5 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f ext *pCtx, . co
1b5e6 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 nst void *z, .
1b5e7 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 int n, . void (
1b5e8 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 *xDel)(void *).)
1b5e9 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1b5ea 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b5eb 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b5ec 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 ) );. setResult
1b5ed 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c StrOrError(pCtx,
1b5ee 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
1b5ef 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a F16BE, xDel);.}.
1b5f0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1b5f1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
1b5f2 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 ext16le(. sqlit
1b5f3 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1b5f4 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 , . const void
1b5f5 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 *z, . int n, .
1b5f6 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1b5f7 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 id *).){. asser
1b5f8 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b5f9 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b5fa 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 ->mutex) );. se
1b5fb 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f tResultStrOrErro
1b5fc 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 r(pCtx, z, n, SQ
1b5fd 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 78 44 LITE_UTF16LE, xD
1b5fe 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a el);.}.#endif /*
1b5ff 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1b600 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 16 */.SQLITE_API
1b601 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b602 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 sult_value(sqlit
1b603 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1b604 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
1b605 2a 70 56 61 6c 75 65 29 7b 0a 20 20 61 73 73 65 *pValue){. asse
1b606 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b607 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1b608 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
1b609 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 qlite3VdbeMemCop
1b60a 79 28 26 70 43 74 78 2d 3e 73 2c 20 70 56 61 6c y(&pCtx->s, pVal
1b60b 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 ue);.}.SQLITE_AP
1b60c 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1b60d 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 esult_zeroblob(s
1b60e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b60f 70 43 74 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 pCtx, int n){.
1b610 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1b611 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1b612 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1b613 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1b614 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 43 mSetZeroBlob(&pC
1b615 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 51 4c tx->s, n);.}.SQL
1b616 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b617 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
1b618 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 r_code(sqlite3_c
1b619 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e ontext *pCtx, in
1b61a 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 70 43 t errCode){. pC
1b61b 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 65 72 tx->isError = er
1b61c 72 43 6f 64 65 3b 0a 20 20 69 66 28 20 70 43 74 rCode;. if( pCt
1b61d 78 2d 3e 73 2e 66 6c 61 67 73 20 26 20 4d 45 4d x->s.flags & MEM
1b61e 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c _Null ){. sql
1b61f 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
1b620 72 28 26 70 43 74 78 2d 3e 73 2c 20 73 71 6c 69 r(&pCtx->s, sqli
1b621 74 65 33 45 72 72 53 74 72 28 65 72 72 43 6f 64 te3ErrStr(errCod
1b622 65 29 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 e), -1, .
1b623 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b624 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 SQLITE_UTF8, S
1b625 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
1b626 20 7d 0a 7d 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 }.}../* Force a
1b627 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 n SQLITE_TOOBIG
1b628 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 error. */.SQLITE
1b629 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1b62a 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 3_result_error_t
1b62b 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 5f 63 6f oobig(sqlite3_co
1b62c 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 ntext *pCtx){.
1b62d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1b62e 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1b62f 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1b630 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 . pCtx->isError
1b631 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 = SQLITE_TOOBIG
1b632 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1b633 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e emSetStr(&pCtx->
1b634 73 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c s, "string or bl
1b635 6f 62 20 74 6f 6f 20 62 69 67 22 2c 20 2d 31 2c ob too big", -1,
1b636 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1b637 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
1b638 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 UTF8, SQLITE_STA
1b639 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 TIC);.}../* An S
1b63a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f QLITE_NOMEM erro
1b63b 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 r. */.SQLITE_API
1b63c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b63d 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d sult_error_nomem
1b63e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b63f 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 *pCtx){. asser
1b640 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b641 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b642 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
1b643 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1b644 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 20 ull(&pCtx->s);.
1b645 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d pCtx->isError =
1b646 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1b647 20 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c pCtx->s.db->mal
1b648 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d locFailed = 1;.}
1b649 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 ../*.** Execute
1b64a 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 the statement pS
1b64b 74 6d 74 2c 20 65 69 74 68 65 72 20 75 6e 74 69 tmt, either unti
1b64c 6c 20 61 20 72 6f 77 20 6f 66 20 64 61 74 61 20 l a row of data
1b64d 69 73 20 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a is ready, the.**
1b64e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f statement is co
1b64f 6d 70 6c 65 74 65 6c 79 20 65 78 65 63 75 74 65 mpletely execute
1b650 64 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 d or an error oc
1b651 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 curs..**.** This
1b652 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 routine impleme
1b653 6e 74 73 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 nts the bulk of
1b654 74 68 65 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 the logic behind
1b655 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 65 70 the sqlite_step
1b656 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 ().** API. The
1b657 6f 6e 6c 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 only thing omitt
1b658 65 64 20 69 73 20 74 68 65 20 61 75 74 6f 6d 61 ed is the automa
1b659 74 69 63 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 tic recompile if
1b65a 20 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 a .** schema ch
1b65b 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 ange has occurre
1b65c 64 2e 20 20 54 68 61 74 20 64 65 74 61 69 6c 20 d. That detail
1b65d 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 is handled by th
1b65e 65 0a 2a 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 e.** outer sqlit
1b65f 65 33 5f 73 74 65 70 28 29 20 77 72 61 70 70 65 e3_step() wrappe
1b660 72 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a r procedure..*/.
1b661 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
1b662 65 33 53 74 65 70 28 56 64 62 65 20 2a 70 29 7b e3Step(Vdbe *p){
1b663 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1b664 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 int rc;.. ass
1b665 65 72 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d ert(p);. if( p-
1b666 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 >magic!=VDBE_MAG
1b667 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 IC_RUN ){. re
1b668 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
1b669 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 SE;. }.. /* As
1b66a 73 65 72 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63 sert that malloc
1b66b 28 29 20 68 61 73 20 6e 6f 74 20 66 61 69 6c 65 () has not faile
1b66c 64 20 2a 2f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 d */. db = p->d
1b66d 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c b;. if( db->mal
1b66e 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1b66f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1b670 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 OMEM;. }.. if(
1b671 20 70 2d 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e p->pc<=0 && p->
1b672 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20 69 expired ){. i
1b673 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 72 63 3d f( ALWAYS(p->rc=
1b674 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 =SQLITE_OK) ){.
1b675 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c p->rc = SQL
1b676 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 20 20 ITE_SCHEMA;.
1b677 7d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 }. rc = SQLIT
1b678 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 E_ERROR;. got
1b679 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 70 3b 0a 20 o end_of_step;.
1b67a 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1b67b 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a SafetyOn(db) ){.
1b67c 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 p->rc = SQLI
1b67d 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 72 TE_MISUSE;. r
1b67e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 eturn SQLITE_MIS
1b67f 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 USE;. }. if( p
1b680 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 20 20 2f 2a ->pc<0 ){. /*
1b681 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1b682 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 other statement
1b683 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e s currently runn
1b684 69 6e 67 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a ing, then. **
1b685 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 reset the inter
1b686 72 75 70 74 20 66 6c 61 67 2e 20 20 54 68 69 73 rupt flag. This
1b687 20 70 72 65 76 65 6e 74 73 20 61 20 63 61 6c 6c prevents a call
1b688 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 to sqlite3_inte
1b689 72 72 75 70 74 0a 20 20 20 20 2a 2a 20 66 72 6f rrupt. ** fro
1b68a 6d 20 69 6e 74 65 72 72 75 70 74 69 6e 67 20 61 m interrupting a
1b68b 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 statement that
1b68c 68 61 73 20 6e 6f 74 20 79 65 74 20 73 74 61 72 has not yet star
1b68d 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
1b68e 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 if( db->activeVd
1b68f 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 beCnt==0 ){.
1b690 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 db->u1.isInter
1b691 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 rupted = 0;.
1b692 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1b693 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 E_OMIT_TRACE.
1b694 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 66 69 6c if( db->xProfil
1b695 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e 62 e && !db->init.b
1b696 75 73 79 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 usy ){. dou
1b697 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 20 20 ble rNow;.
1b698 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
1b699 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 Time(db->pVfs, &
1b69a 72 4e 6f 77 29 3b 0a 20 20 20 20 20 20 70 2d 3e rNow);. p->
1b69b 73 74 61 72 74 54 69 6d 65 20 3d 20 28 75 36 34 startTime = (u64
1b69c 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 )((rNow - (int)r
1b69d 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 Now)*3600.0*24.0
1b69e 2a 31 30 30 30 30 30 30 30 30 30 2e 30 29 3b 0a *1000000000.0);.
1b69f 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
1b6a0 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 db->activeVdbe
1b6a1 43 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 Cnt++;. if( p
1b6a2 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 ->readOnly==0 )
1b6a3 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 db->writeVdbeCnt
1b6a4 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70 63 20 3d 20 ++;. p->pc =
1b6a5 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 0;. }.#ifndef S
1b6a6 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 QLITE_OMIT_EXPLA
1b6a7 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c IN. if( p->expl
1b6a8 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ain ){. rc =
1b6a9 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 sqlite3VdbeList(
1b6aa 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 p);. }else.#end
1b6ab 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1b6ac 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20 20 7b T_EXPLAIN */. {
1b6ad 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1b6ae 33 56 64 62 65 45 78 65 63 28 70 29 3b 0a 20 20 3VdbeExec(p);.
1b6af 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }.. if( sqlite3
1b6b0 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 7b SafetyOff(db) ){
1b6b1 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1b6b2 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 23 69 _MISUSE;. }..#i
1b6b3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1b6b4 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20 49 6e 76 T_TRACE. /* Inv
1b6b5 6f 6b 65 20 74 68 65 20 70 72 6f 66 69 6c 65 20 oke the profile
1b6b6 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72 callback if ther
1b6b7 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f 0a 20 20 e is one. */.
1b6b8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 if( rc!=SQLITE_R
1b6b9 4f 57 20 26 26 20 64 62 2d 3e 78 50 72 6f 66 69 OW && db->xProfi
1b6ba 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e le && !db->init.
1b6bb 62 75 73 79 20 26 26 20 70 2d 3e 7a 53 71 6c 20 busy && p->zSql
1b6bc 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 4e ){. double rN
1b6bd 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c 61 70 ow;. u64 elap
1b6be 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73 71 6c seTime;.. sql
1b6bf 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d ite3OsCurrentTim
1b6c0 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f e(db->pVfs, &rNo
1b6c1 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 w);. elapseTi
1b6c2 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f 77 me = (u64)((rNow
1b6c3 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33 36 - (int)rNow)*36
1b6c4 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30 30 00.0*24.0*100000
1b6c5 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 65 6c 61 0000.0);. ela
1b6c6 70 73 65 54 69 6d 65 20 2d 3d 20 70 2d 3e 73 74 pseTime -= p->st
1b6c7 61 72 74 54 69 6d 65 3b 0a 20 20 20 20 64 62 2d artTime;. db-
1b6c8 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e 70 50 >xProfile(db->pP
1b6c9 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e 7a 53 rofileArg, p->zS
1b6ca 71 6c 2c 20 65 6c 61 70 73 65 54 69 6d 65 29 3b ql, elapseTime);
1b6cb 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 . }.#endif.. d
1b6cc 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b b->errCode = rc;
1b6cd 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f . if( SQLITE_NO
1b6ce 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 41 70 69 45 MEM==sqlite3ApiE
1b6cf 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 xit(p->db, p->rc
1b6d0 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d ) ){. p->rc =
1b6d1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1b6d2 20 7d 0a 65 6e 64 5f 6f 66 5f 73 74 65 70 3a 0a }.end_of_step:.
1b6d3 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 /* At this poi
1b6d4 6e 74 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c nt local variabl
1b6d5 65 20 72 63 20 68 6f 6c 64 73 20 74 68 65 20 76 e rc holds the v
1b6d6 61 6c 75 65 20 74 68 61 74 20 73 68 6f 75 6c 64 alue that should
1b6d7 20 62 65 20 0a 20 20 2a 2a 20 72 65 74 75 72 6e be . ** return
1b6d8 65 64 20 69 66 20 74 68 69 73 20 73 74 61 74 65 ed if this state
1b6d9 6d 65 6e 74 20 77 61 73 20 63 6f 6d 70 69 6c 65 ment was compile
1b6da 64 20 75 73 69 6e 67 20 74 68 65 20 6c 65 67 61 d using the lega
1b6db 63 79 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 cy . ** sqlite3
1b6dc 5f 70 72 65 70 61 72 65 28 29 20 69 6e 74 65 72 _prepare() inter
1b6dd 66 61 63 65 2e 20 41 63 63 6f 72 64 69 6e 67 20 face. According
1b6de 74 6f 20 74 68 65 20 64 6f 63 73 2c 20 74 68 69 to the docs, thi
1b6df 73 20 63 61 6e 20 6f 6e 6c 79 0a 20 20 2a 2a 20 s can only. **
1b6e0 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 76 61 be one of the va
1b6e1 6c 75 65 73 20 69 6e 20 74 68 65 20 66 69 72 73 lues in the firs
1b6e2 74 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f 77 t assert() below
1b6e3 2e 20 56 61 72 69 61 62 6c 65 20 70 2d 3e 72 63 . Variable p->rc
1b6e4 20 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 . ** contains
1b6e5 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 77 the value that w
1b6e6 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 ould be returned
1b6e7 20 69 66 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 if sqlite3_fina
1b6e8 6c 69 7a 65 28 29 20 0a 20 20 2a 2a 20 77 65 72 lize() . ** wer
1b6e9 65 20 63 61 6c 6c 65 64 20 6f 6e 20 73 74 61 74 e called on stat
1b6ea 65 6d 65 6e 74 20 70 2e 0a 20 20 2a 2f 0a 20 20 ement p.. */.
1b6eb 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1b6ec 54 45 5f 52 4f 57 20 20 7c 7c 20 72 63 3d 3d 53 TE_ROW || rc==S
1b6ed 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 7c 7c 20 QLITE_DONE ||
1b6ee 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 rc==SQLITE_ERROR
1b6ef 20 0a 20 20 20 20 20 20 20 7c 7c 20 72 63 3d 3d . || rc==
1b6f0 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 SQLITE_BUSY || r
1b6f1 63 3d 3d 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 c==SQLITE_MISUSE
1b6f2 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 . );. assert(
1b6f3 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f p->rc!=SQLITE_RO
1b6f4 57 20 26 26 20 70 2d 3e 72 63 21 3d 53 51 4c 49 W && p->rc!=SQLI
1b6f5 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28 TE_DONE );. if(
1b6f6 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 p->isPrepareV2
1b6f7 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f && rc!=SQLITE_RO
1b6f8 57 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f W && rc!=SQLITE_
1b6f9 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2f 2a 20 49 DONE ){. /* I
1b6fa 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 f this statement
1b6fb 20 77 61 73 20 70 72 65 70 61 72 65 64 20 75 73 was prepared us
1b6fc 69 6e 67 20 73 71 6c 69 74 65 33 5f 70 72 65 70 ing sqlite3_prep
1b6fd 61 72 65 5f 76 32 28 29 2c 20 61 6e 64 20 61 6e are_v2(), and an
1b6fe 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 68 61 . ** error ha
1b6ff 73 20 6f 63 63 75 72 65 64 2c 20 74 68 65 6e 20 s occured, then
1b700 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 return the error
1b701 20 63 6f 64 65 20 69 6e 20 70 2d 3e 72 63 20 74 code in p->rc t
1b702 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c o the. ** cal
1b703 6c 65 72 2e 20 53 65 74 20 74 68 65 20 65 72 72 ler. Set the err
1b704 6f 72 20 63 6f 64 65 20 69 6e 20 74 68 65 20 64 or code in the d
1b705 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 atabase handle t
1b706 6f 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 o the same value
1b707 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 72 63 .. */ . rc
1b708 20 3d 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d = db->errCode =
1b709 20 70 2d 3e 72 63 3b 0a 20 20 7d 0a 20 20 72 65 p->rc;. }. re
1b70a 74 75 72 6e 20 28 72 63 26 64 62 2d 3e 65 72 72 turn (rc&db->err
1b70b 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Mask);.}../*.**
1b70c 54 68 69 73 20 69 73 20 74 68 65 20 74 6f 70 2d This is the top-
1b70d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d 65 6e 74 61 level implementa
1b70e 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f tion of sqlite3_
1b70f 73 74 65 70 28 29 2e 20 20 43 61 6c 6c 0a 2a 2a step(). Call.**
1b710 20 73 71 6c 69 74 65 33 53 74 65 70 28 29 20 74 sqlite3Step() t
1b711 6f 20 64 6f 20 6d 6f 73 74 20 6f 66 20 74 68 65 o do most of the
1b712 20 77 6f 72 6b 2e 20 20 49 66 20 61 20 73 63 68 work. If a sch
1b713 65 6d 61 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ema error occurs
1b714 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74 65 ,.** call sqlite
1b715 33 52 65 70 72 65 70 61 72 65 28 29 20 61 6e 64 3Reprepare() and
1b716 20 74 72 79 20 61 67 61 69 6e 2e 0a 2a 2f 0a 53 try again..*/.S
1b717 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1b718 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69 74 lite3_step(sqlit
1b719 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1b71a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1b71b 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 66 28 TE_MISUSE;. if(
1b71c 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 69 6e pStmt ){. in
1b71d 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 56 t cnt = 0;. V
1b71e 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 dbe *v = (Vdbe*)
1b71f 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 pStmt;. sqlit
1b720 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b 0a e3 *db = v->db;.
1b721 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1b722 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 x_enter(db->mute
1b723 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 x);. while( (
1b724 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65 70 rc = sqlite3Step
1b725 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43 48 (v))==SQLITE_SCH
1b726 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 20 26 EMA. &
1b727 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 20 20 & cnt++ < 5.
1b728 20 20 20 20 20 20 20 26 26 20 28 72 63 20 3d 20 && (rc =
1b729 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 sqlite3Reprepare
1b72a 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 (v))==SQLITE_OK
1b72b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1b72c 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 _reset(pStmt);.
1b72d 20 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20 v->expired
1b72e 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 0;. }. i
1b72f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 f( rc==SQLITE_SC
1b730 48 45 4d 41 20 26 26 20 41 4c 57 41 59 53 28 76 HEMA && ALWAYS(v
1b731 2d 3e 69 73 50 72 65 70 61 72 65 56 32 29 20 26 ->isPrepareV2) &
1b732 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45 72 & ALWAYS(db->pEr
1b733 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 r) ){. /* T
1b734 68 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 his case occurs
1b735 61 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f after failing to
1b736 20 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71 recompile an sq
1b737 6c 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20 l statement. .
1b738 20 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72 ** The error
1b739 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 message from th
1b73a 65 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68 e SQL compiler h
1b73b 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 as already been
1b73c 6c 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a loaded . **
1b73d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 into the databa
1b73e 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 se handle. This
1b73f 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65 block copies the
1b740 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a error message .
1b741 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 ** from th
1b742 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1b743 65 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65 e into the state
1b744 6d 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68 ment and sets th
1b745 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 e statement.
1b746 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75 ** program cou
1b747 6e 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73 nter to 0 to ens
1b748 75 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 ure that when th
1b749 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a e statement is .
1b74a 20 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a ** finaliz
1b74b 65 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 20 ed or reset the
1b74c 70 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73 parser error mes
1b74d 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c sage is availabl
1b74e 65 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73 e via. ** s
1b74f 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 qlite3_errmsg()
1b750 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63 and sqlite3_errc
1b751 6f 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a ode().. */.
1b752 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1b753 20 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20 *zErr = (const
1b754 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 char *)sqlite3_v
1b755 61 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 alue_text(db->pE
1b756 72 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69 rr); . sqli
1b757 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d te3DbFree(db, v-
1b758 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 >zErrMsg);.
1b759 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 if( !db->malloc
1b75a 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 Failed ){.
1b75b 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 v->zErrMsg = s
1b75c 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
1b75d 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 b, zErr);.
1b75e 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 } else {.
1b75f 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b v->zErrMsg = 0;
1b760 0a 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d . v->rc =
1b761 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1b762 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1b763 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 rc = sqlite3Api
1b764 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 Exit(db, rc);.
1b765 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1b766 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 leave(db->mutex)
1b767 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1b768 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 c;.}../*.** Extr
1b769 61 63 74 20 74 68 65 20 75 73 65 72 20 64 61 74 act the user dat
1b76a 61 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 a from a sqlite3
1b76b 5f 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 _context structu
1b76c 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a re and return a.
1b76d 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 ** pointer to it
1b76e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1b76f 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 void *sqlite3_us
1b770 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f er_data(sqlite3_
1b771 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 context *p){. a
1b772 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 ssert( p && p->p
1b773 46 75 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e Func );. return
1b774 20 70 2d 3e 70 46 75 6e 63 2d 3e 70 55 73 65 72 p->pFunc->pUser
1b775 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 Data;.}../*.** E
1b776 78 74 72 61 63 74 20 74 68 65 20 75 73 65 72 20 xtract the user
1b777 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 6c 69 data from a sqli
1b778 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 75 te3_context stru
1b779 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e cture and return
1b77a 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f a.** pointer to
1b77b 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 it..*/.SQLITE_A
1b77c 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 PI sqlite3 *sqli
1b77d 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
1b77e 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f andle(sqlite3_co
1b77f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 ntext *p){. ass
1b780 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75 ert( p && p->pFu
1b781 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 nc );. return p
1b782 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ->s.db;.}../*.**
1b783 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 The following i
1b784 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
1b785 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 66 tion of an SQL f
1b786 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 61 6c 77 unction that alw
1b787 61 79 73 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 ays.** fails wit
1b788 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 h an error messa
1b789 67 65 20 73 74 61 74 69 6e 67 20 74 68 61 74 20 ge stating that
1b78a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 the function is
1b78b 75 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 77 used in the.** w
1b78c 72 6f 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 54 rong context. T
1b78d 68 65 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c he sqlite3_overl
1b78e 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 29 20 41 oad_function() A
1b78f 50 49 20 6d 69 67 68 74 20 63 6f 6e 73 74 72 75 PI might constru
1b790 63 74 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 ct.** SQL functi
1b791 6f 6e 20 74 68 61 74 20 75 73 65 20 74 68 69 73 on that use this
1b792 20 72 6f 75 74 69 6e 65 20 73 6f 20 74 68 61 74 routine so that
1b793 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 77 the functions w
1b794 69 6c 6c 20 65 78 69 73 74 0a 2a 2a 20 66 6f 72 ill exist.** for
1b795 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e name resolution
1b796 20 62 75 74 20 61 72 65 20 61 63 74 75 61 6c 6c but are actuall
1b797 79 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 y overloaded by
1b798 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f the xFindFunctio
1b799 6e 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 76 n.** method of v
1b79a 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a irtual tables..*
1b79b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b79c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76 void sqlite3Inv
1b79d 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 alidFunction(.
1b79e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b79f 2a 63 6f 6e 74 65 78 74 2c 20 20 2f 2a 20 54 68 *context, /* Th
1b7a0 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 69 e function calli
1b7a1 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
1b7a2 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 20 20 20 int NotUsed,
1b7a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1b7a4 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
1b7a5 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f s to the functio
1b7a6 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 n */. sqlite3_v
1b7a7 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 20 alue **NotUsed2
1b7a8 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 65 61 /* Value of ea
1b7a9 63 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 29 ch argument */.)
1b7aa 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a {. const char *
1b7ab 7a 4e 61 6d 65 20 3d 20 63 6f 6e 74 65 78 74 2d zName = context-
1b7ac 3e 70 46 75 6e 63 2d 3e 7a 4e 61 6d 65 3b 0a 20 >pFunc->zName;.
1b7ad 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55 char *zErr;. U
1b7ae 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
1b7af 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
1b7b0 64 32 29 3b 0a 20 20 7a 45 72 72 20 3d 20 73 71 d2);. zErr = sq
1b7b1 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 lite3_mprintf(.
1b7b2 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 "unable to
1b7b3 75 73 65 20 66 75 6e 63 74 69 6f 6e 20 25 73 20 use function %s
1b7b4 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 in the requested
1b7b5 20 63 6f 6e 74 65 78 74 22 2c 20 7a 4e 61 6d 65 context", zName
1b7b6 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 );. sqlite3_res
1b7b7 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 ult_error(contex
1b7b8 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 t, zErr, -1);.
1b7b9 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 sqlite3_free(zEr
1b7ba 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c r);.}../*.** All
1b7bb 6f 63 61 74 65 20 6f 72 20 72 65 74 75 72 6e 20 ocate or return
1b7bc 74 68 65 20 61 67 67 72 65 67 61 74 65 20 63 6f the aggregate co
1b7bd 6e 74 65 78 74 20 66 6f 72 20 61 20 75 73 65 72 ntext for a user
1b7be 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 6e 65 function. A ne
1b7bf 77 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 w.** context is
1b7c0 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 allocated on the
1b7c1 20 66 69 72 73 74 20 63 61 6c 6c 2e 20 20 53 75 first call. Su
1b7c2 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 72 bsequent calls r
1b7c3 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 73 61 6d eturn the.** sam
1b7c4 65 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77 e context that w
1b7c5 61 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 70 as returned on p
1b7c6 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53 rior calls..*/.S
1b7c7 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
1b7c8 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 sqlite3_aggregat
1b7c9 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 e_context(sqlite
1b7ca 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 3_context *p, in
1b7cb 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20 t nByte){. Mem
1b7cc 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 *pMem;. assert(
1b7cd 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 26 p && p->pFunc &
1b7ce 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 65 & p->pFunc->xSte
1b7cf 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 p );. assert( s
1b7d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1b7d1 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 d(p->s.db->mutex
1b7d2 29 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 70 2d ) );. pMem = p-
1b7d3 3e 70 4d 65 6d 3b 0a 20 20 69 66 28 20 28 70 4d >pMem;. if( (pM
1b7d4 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1b7d5 41 67 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 Agg)==0 ){. i
1b7d6 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 f( nByte==0 ){.
1b7d7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1b7d8 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
1b7d9 61 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 al(pMem);.
1b7da 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1b7db 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4d M_Null;. pM
1b7dc 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 7d em->z = 0;. }
1b7dd 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
1b7de 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
1b7df 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 3b 0a Mem, nByte, 0);.
1b7e0 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1b7e1 73 20 3d 20 4d 45 4d 5f 41 67 67 3b 0a 20 20 20 s = MEM_Agg;.
1b7e2 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 20 pMem->u.pDef
1b7e3 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a 20 20 20 20 = p->pFunc;.
1b7e4 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b if( pMem->z ){
1b7e5 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 . memset(
1b7e6 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 pMem->z, 0, nByt
1b7e7 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
1b7e8 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 }. }. return (
1b7e9 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 7d void*)pMem->z;.}
1b7ea 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1b7eb 68 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 he auxilary data
1b7ec 20 70 6f 69 6e 74 65 72 2c 20 69 66 20 61 6e 79 pointer, if any
1b7ed 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 74 , for the iArg't
1b7ee 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a h argument to.**
1b7ef 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 the user-functi
1b7f0 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 43 on defined by pC
1b7f1 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 tx..*/.SQLITE_AP
1b7f2 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f I void *sqlite3_
1b7f3 67 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 get_auxdata(sqli
1b7f4 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1b7f5 78 2c 20 69 6e 74 20 69 41 72 67 29 7b 0a 20 20 x, int iArg){.
1b7f6 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 VdbeFunc *pVdbeF
1b7f7 75 6e 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 unc;.. assert(
1b7f8 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b7f9 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b7fa 75 74 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65 utex) );. pVdbe
1b7fb 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64 Func = pCtx->pVd
1b7fc 62 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70 beFunc;. if( !p
1b7fd 56 64 62 65 46 75 6e 63 20 7c 7c 20 69 41 72 67 VdbeFunc || iArg
1b7fe 3e 3d 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 >=pVdbeFunc->nAu
1b7ff 78 20 7c 7c 20 69 41 72 67 3c 30 20 29 7b 0a 20 x || iArg<0 ){.
1b800 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
1b801 0a 20 20 72 65 74 75 72 6e 20 70 56 64 62 65 46 . return pVdbeF
1b802 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d unc->apAux[iArg]
1b803 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 .pAux;.}../*.**
1b804 53 65 74 20 74 68 65 20 61 75 78 69 6c 61 72 79 Set the auxilary
1b805 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61 6e data pointer an
1b806 64 20 64 65 6c 65 74 65 20 66 75 6e 63 74 69 6f d delete functio
1b807 6e 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 n, for the iArg'
1b808 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 th.** argument t
1b809 6f 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 o the user-funct
1b80a 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 ion defined by p
1b80b 43 74 78 2e 20 41 6e 79 20 70 72 65 76 69 6f 75 Ctx. Any previou
1b80c 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 64 65 s value is.** de
1b80d 6c 65 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 leted by calling
1b80e 20 74 68 65 20 64 65 6c 65 74 65 20 66 75 6e 63 the delete func
1b80f 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 77 tion specified w
1b810 68 65 6e 20 69 74 20 77 61 73 20 73 65 74 2e 0a hen it was set..
1b811 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f */.SQLITE_API vo
1b812 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 id sqlite3_set_a
1b813 75 78 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65 uxdata(. sqlite
1b814 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 3_context *pCtx,
1b815 20 0a 20 20 69 6e 74 20 69 41 72 67 2c 20 0a 20 . int iArg, .
1b816 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 0a 20 20 void *pAux, .
1b817 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29 28 void (*xDelete)(
1b818 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 74 72 75 void*).){. stru
1b819 63 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 78 ct AuxData *pAux
1b81a 44 61 74 61 3b 0a 20 20 56 64 62 65 46 75 6e 63 Data;. VdbeFunc
1b81b 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 *pVdbeFunc;. i
1b81c 66 28 20 69 41 72 67 3c 30 20 29 20 67 6f 74 6f f( iArg<0 ) goto
1b81d 20 66 61 69 6c 65 64 3b 0a 0a 20 20 61 73 73 65 failed;.. asse
1b81e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b81f 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1b820 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 b->mutex) );. p
1b821 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d VdbeFunc = pCtx-
1b822 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 >pVdbeFunc;. if
1b823 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 ( !pVdbeFunc ||
1b824 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3c pVdbeFunc->nAux<
1b825 3d 69 41 72 67 20 29 7b 0a 20 20 20 20 69 6e 74 =iArg ){. int
1b826 20 6e 41 75 78 20 3d 20 28 70 56 64 62 65 46 75 nAux = (pVdbeFu
1b827 6e 63 20 3f 20 70 56 64 62 65 46 75 6e 63 2d 3e nc ? pVdbeFunc->
1b828 6e 41 75 78 20 3a 20 30 29 3b 0a 20 20 20 20 69 nAux : 0);. i
1b829 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20 73 69 7a nt nMalloc = siz
1b82a 65 6f 66 28 56 64 62 65 46 75 6e 63 29 20 2b 20 eof(VdbeFunc) +
1b82b 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75 sizeof(struct Au
1b82c 78 44 61 74 61 29 2a 69 41 72 67 3b 0a 20 20 20 xData)*iArg;.
1b82d 20 70 56 64 62 65 46 75 6e 63 20 3d 20 73 71 6c pVdbeFunc = sql
1b82e 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 43 ite3DbRealloc(pC
1b82f 74 78 2d 3e 73 2e 64 62 2c 20 70 56 64 62 65 46 tx->s.db, pVdbeF
1b830 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29 3b 0a 20 unc, nMalloc);.
1b831 20 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e if( !pVdbeFun
1b832 63 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 c ){. goto
1b833 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
1b834 20 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e pCtx->pVdbeFun
1b835 63 20 3d 20 70 56 64 62 65 46 75 6e 63 3b 0a 20 c = pVdbeFunc;.
1b836 20 20 20 6d 65 6d 73 65 74 28 26 70 56 64 62 65 memset(&pVdbe
1b837 46 75 6e 63 2d 3e 61 70 41 75 78 5b 6e 41 75 78 Func->apAux[nAux
1b838 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 74 72 ], 0, sizeof(str
1b839 75 63 74 20 41 75 78 44 61 74 61 29 2a 28 69 41 uct AuxData)*(iA
1b83a 72 67 2b 31 2d 6e 41 75 78 29 29 3b 0a 20 20 20 rg+1-nAux));.
1b83b 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 pVdbeFunc->nAux
1b83c 20 3d 20 69 41 72 67 2b 31 3b 0a 20 20 20 20 70 = iArg+1;. p
1b83d 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 20 VdbeFunc->pFunc
1b83e 3d 20 70 43 74 78 2d 3e 70 46 75 6e 63 3b 0a 20 = pCtx->pFunc;.
1b83f 20 7d 0a 0a 20 20 70 41 75 78 44 61 74 61 20 3d }.. pAuxData =
1b840 20 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 &pVdbeFunc->apA
1b841 75 78 5b 69 41 72 67 5d 3b 0a 20 20 69 66 28 20 ux[iArg];. if(
1b842 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 20 26 pAuxData->pAux &
1b843 26 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c & pAuxData->xDel
1b844 65 74 65 20 29 7b 0a 20 20 20 20 70 41 75 78 44 ete ){. pAuxD
1b845 61 74 61 2d 3e 78 44 65 6c 65 74 65 28 70 41 75 ata->xDelete(pAu
1b846 78 44 61 74 61 2d 3e 70 41 75 78 29 3b 0a 20 20 xData->pAux);.
1b847 7d 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 70 41 }. pAuxData->pA
1b848 75 78 20 3d 20 70 41 75 78 3b 0a 20 20 70 41 75 ux = pAux;. pAu
1b849 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 3d xData->xDelete =
1b84a 20 78 44 65 6c 65 74 65 3b 0a 20 20 72 65 74 75 xDelete;. retu
1b84b 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a 20 20 69 rn;..failed:. i
1b84c 66 28 20 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 f( xDelete ){.
1b84d 20 20 78 44 65 6c 65 74 65 28 70 41 75 78 29 3b xDelete(pAux);
1b84e 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 . }.}..#ifndef
1b84f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 SQLITE_OMIT_DEPR
1b850 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 ECATED./*.** Ret
1b851 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1b852 66 20 74 69 6d 65 73 20 74 68 65 20 53 74 65 70 f times the Step
1b853 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 61 20 61 function of a a
1b854 67 67 72 65 67 61 74 65 20 68 61 73 20 62 65 65 ggregate has bee
1b855 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2a n .** called..**
1b856 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
1b857 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64 2e n is deprecated.
1b858 20 20 44 6f 20 6e 6f 74 20 75 73 65 20 69 74 20 Do not use it
1b859 66 6f 72 20 6e 65 77 20 63 6f 64 65 2e 20 20 49 for new code. I
1b85a 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65 20 t is.** provide
1b85b 6f 6e 6c 79 20 74 6f 20 61 76 6f 69 64 20 62 72 only to avoid br
1b85c 65 61 6b 69 6e 67 20 6c 65 67 61 63 79 20 63 6f eaking legacy co
1b85d 64 65 2e 20 20 4e 65 77 20 61 67 67 72 65 67 61 de. New aggrega
1b85e 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 te function.** i
1b85f 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 mplementations s
1b860 68 6f 75 6c 64 20 6b 65 65 70 20 74 68 65 69 72 hould keep their
1b861 20 6f 77 6e 20 63 6f 75 6e 74 73 20 77 69 74 68 own counts with
1b862 69 6e 20 74 68 65 69 72 20 61 67 67 72 65 67 61 in their aggrega
1b863 74 65 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 0a 2a te.** context..*
1b864 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1b865 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 sqlite3_aggrega
1b866 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 te_count(sqlite3
1b867 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 _context *p){.
1b868 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e assert( p && p->
1b869 70 4d 65 6d 20 26 26 20 70 2d 3e 70 46 75 6e 63 pMem && p->pFunc
1b86a 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 && p->pFunc->xS
1b86b 74 65 70 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 tep );. return
1b86c 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a 7d 0a 23 65 p->pMem->n;.}.#e
1b86d 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
1b86e 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
1b86f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
1b870 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74 result set for t
1b871 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 he statement pSt
1b872 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 mt..*/.SQLITE_AP
1b873 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
1b874 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74 lumn_count(sqlit
1b875 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1b876 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28 . Vdbe *pVm = (
1b877 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
1b878 72 65 74 75 72 6e 20 70 56 6d 20 3f 20 70 56 6d return pVm ? pVm
1b879 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3a 20 30 ->nResColumn : 0
1b87a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1b87b 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1b87c 76 61 6c 75 65 73 20 61 76 61 69 6c 61 62 6c 65 values available
1b87d 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e from the curren
1b87e 74 20 72 6f 77 20 6f 66 20 74 68 65 0a 2a 2a 20 t row of the.**
1b87f 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 currently execut
1b880 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 53 ing statement pS
1b881 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 tmt..*/.SQLITE_A
1b882 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 PI int sqlite3_d
1b883 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 ata_count(sqlite
1b884 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 3_stmt *pStmt){.
1b885 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28 56 Vdbe *pVm = (V
1b886 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 dbe *)pStmt;. i
1b887 66 28 20 70 56 6d 3d 3d 30 20 7c 7c 20 70 56 6d f( pVm==0 || pVm
1b888 2d 3e 70 52 65 73 75 6c 74 53 65 74 3d 3d 30 20 ->pResultSet==0
1b889 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 ) return 0;. re
1b88a 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 65 73 43 6f turn pVm->nResCo
1b88b 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 lumn;.}.../*.**
1b88c 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
1b88d 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66 20 74 column iCol of t
1b88e 68 65 20 67 69 76 65 6e 20 73 74 61 74 65 6d 65 he given stateme
1b88f 6e 74 20 69 73 20 76 61 6c 69 64 2e 20 20 49 66 nt is valid. If
1b890 0a 2a 2a 20 69 74 20 69 73 2c 20 72 65 74 75 72 .** it is, retur
1b891 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
1b892 68 65 20 4d 65 6d 20 66 6f 72 20 74 68 65 20 76 he Mem for the v
1b893 61 6c 75 65 20 6f 66 20 74 68 61 74 20 63 6f 6c alue of that col
1b894 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69 43 6f 6c 20 umn..** If iCol
1b895 69 73 20 6e 6f 74 20 76 61 6c 69 64 2c 20 72 65 is not valid, re
1b896 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
1b897 6f 20 61 20 4d 65 6d 20 77 68 69 63 68 20 68 61 o a Mem which ha
1b898 73 20 61 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 s a value.** of
1b899 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 NULL..*/.static
1b89a 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d 65 6d 28 73 Mem *columnMem(s
1b89b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1b89c 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 mt, int i){. Vd
1b89d 62 65 20 2a 70 56 6d 3b 0a 20 20 69 6e 74 20 76 be *pVm;. int v
1b89e 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 als;. Mem *pOut
1b89f 3b 0a 0a 20 20 70 56 6d 20 3d 20 28 56 64 62 65 ;.. pVm = (Vdbe
1b8a0 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 *)pStmt;. if(
1b8a1 70 56 6d 20 26 26 20 70 56 6d 2d 3e 70 52 65 73 pVm && pVm->pRes
1b8a2 75 6c 74 53 65 74 21 3d 30 20 26 26 20 69 3c 70 ultSet!=0 && i<p
1b8a3 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 26 Vm->nResColumn &
1b8a4 26 20 69 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 & i>=0 ){. sq
1b8a5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1b8a6 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 r(pVm->db->mutex
1b8a7 29 3b 0a 20 20 20 20 76 61 6c 73 20 3d 20 73 71 );. vals = sq
1b8a8 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 lite3_data_count
1b8a9 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 70 4f 75 (pStmt);. pOu
1b8aa 74 20 3d 20 26 70 56 6d 2d 3e 70 52 65 73 75 6c t = &pVm->pResul
1b8ab 74 53 65 74 5b 69 5d 3b 0a 20 20 7d 65 6c 73 65 tSet[i];. }else
1b8ac 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 {. /* If the
1b8ad 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
1b8ae 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
1b8af 65 6e 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61 ent is out of ra
1b8b0 6e 67 65 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 nge, return.
1b8b1 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
1b8b2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
1b8b3 61 74 69 63 20 4d 65 6d 20 6f 62 6a 65 63 74 20 atic Mem object
1b8b4 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 74 which contains t
1b8b5 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 he. ** value
1b8b6 53 51 4c 20 4e 55 4c 4c 2e 20 45 76 65 6e 20 74 SQL NULL. Even t
1b8b7 68 6f 75 67 68 20 74 68 65 20 4d 65 6d 20 73 74 hough the Mem st
1b8b8 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
1b8b9 20 61 6e 20 65 6c 65 6d 65 6e 74 0a 20 20 20 20 an element.
1b8ba 2a 2a 20 6f 66 20 74 79 70 65 20 69 36 34 2c 20 ** of type i64,
1b8bb 6f 6e 20 63 65 72 74 61 69 6e 20 61 72 63 68 69 on certain archi
1b8bc 74 65 63 74 75 72 65 20 28 78 38 36 29 20 77 69 tecture (x86) wi
1b8bd 74 68 20 63 65 72 74 61 69 6e 20 63 6f 6d 70 69 th certain compi
1b8be 6c 65 72 0a 20 20 20 20 2a 2a 20 73 77 69 74 63 ler. ** switc
1b8bf 68 65 73 20 28 2d 4f 73 29 2c 20 67 63 63 20 6d hes (-Os), gcc m
1b8c0 61 79 20 61 6c 69 67 6e 20 74 68 69 73 20 4d 65 ay align this Me
1b8c1 6d 20 6f 62 6a 65 63 74 20 6f 6e 20 61 20 34 2d m object on a 4-
1b8c2 62 79 74 65 20 62 6f 75 6e 64 61 72 79 0a 20 20 byte boundary.
1b8c3 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 ** instead of
1b8c4 61 6e 20 38 2d 62 79 74 65 20 6f 6e 65 2e 20 54 an 8-byte one. T
1b8c5 68 69 73 20 61 6c 6c 20 77 6f 72 6b 73 20 66 69 his all works fi
1b8c6 6e 65 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 ne, except that
1b8c7 77 68 65 6e 0a 20 20 20 20 2a 2a 20 72 75 6e 6e when. ** runn
1b8c8 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54 45 5f ing with SQLITE_
1b8c9 44 45 42 55 47 20 64 65 66 69 6e 65 64 20 74 68 DEBUG defined th
1b8ca 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20 73 6f e SQLite code so
1b8cb 6d 65 74 69 6d 65 73 20 61 73 73 65 72 74 28 29 metimes assert()
1b8cc 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 61 20 s. ** that a
1b8cd 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 69 73 Mem structure is
1b8ce 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 6e 20 38 located on an 8
1b8cf 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 -byte boundary.
1b8d0 54 6f 20 70 72 65 76 65 6e 74 0a 20 20 20 20 2a To prevent. *
1b8d1 2a 20 74 68 69 73 20 61 73 73 65 72 74 28 29 20 * this assert()
1b8d2 66 72 6f 6d 20 66 61 69 6c 69 6e 67 2c 20 77 68 from failing, wh
1b8d3 65 6e 20 62 75 69 6c 64 69 6e 67 20 77 69 74 68 en building with
1b8d4 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65 SQLITE_DEBUG de
1b8d5 66 69 6e 65 64 0a 20 20 20 20 2a 2a 20 75 73 69 fined. ** usi
1b8d6 6e 67 20 67 63 63 2c 20 66 6f 72 63 65 20 6e 75 ng gcc, force nu
1b8d7 6c 6c 4d 65 6d 20 74 6f 20 62 65 20 38 2d 62 79 llMem to be 8-by
1b8d8 74 65 20 61 6c 69 67 6e 65 64 20 75 73 69 6e 67 te aligned using
1b8d9 20 74 68 65 20 6d 61 67 69 63 61 6c 0a 20 20 20 the magical.
1b8da 20 2a 2a 20 5f 5f 61 74 74 72 69 62 75 74 65 5f ** __attribute_
1b8db 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29 20 _((aligned(8)))
1b8dc 6d 61 63 72 6f 2e 20 20 2a 2f 0a 20 20 20 20 73 macro. */. s
1b8dd 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65 6d 20 tatic const Mem
1b8de 6e 75 6c 6c 4d 65 6d 20 0a 23 69 66 20 64 65 66 nullMem .#if def
1b8df 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
1b8e0 47 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f G) && defined(__
1b8e1 47 4e 55 43 5f 5f 29 0a 20 20 20 20 20 20 5f 5f GNUC__). __
1b8e2 61 74 74 72 69 62 75 74 65 5f 5f 28 28 61 6c 69 attribute__((ali
1b8e3 67 6e 65 64 28 38 29 29 29 20 0a 23 65 6e 64 69 gned(8))) .#endi
1b8e4 66 0a 20 20 20 20 20 20 3d 20 7b 7b 30 7d 2c 20 f. = {{0},
1b8e5 28 64 6f 75 62 6c 65 29 30 2c 20 30 2c 20 22 22 (double)0, 0, ""
1b8e6 2c 20 30 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53 , 0, MEM_Null, S
1b8e7 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30 QLITE_NULL, 0, 0
1b8e8 2c 20 30 20 7d 3b 0a 0a 20 20 20 20 69 66 28 20 , 0 };.. if(
1b8e9 70 56 6d 20 26 26 20 41 4c 57 41 59 53 28 70 56 pVm && ALWAYS(pV
1b8ea 6d 2d 3e 64 62 29 20 29 7b 0a 20 20 20 20 20 20 m->db) ){.
1b8eb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1b8ec 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 ter(pVm->db->mut
1b8ed 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ex);. sqlit
1b8ee 65 33 45 72 72 6f 72 28 70 56 6d 2d 3e 64 62 2c e3Error(pVm->db,
1b8ef 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 SQLITE_RANGE, 0
1b8f0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 );. }. pOu
1b8f1 74 20 3d 20 28 4d 65 6d 2a 29 26 6e 75 6c 6c 4d t = (Mem*)&nullM
1b8f2 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e em;. }. return
1b8f3 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pOut;.}../*.**
1b8f4 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
1b8f5 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69 6e called after in
1b8f6 76 6f 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74 65 voking an sqlite
1b8f7 33 5f 76 61 6c 75 65 5f 58 58 58 20 66 75 6e 63 3_value_XXX func
1b8f8 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a 2a 20 63 6f tion on a .** co
1b8f9 6c 75 6d 6e 20 76 61 6c 75 65 20 28 69 2e 65 2e lumn value (i.e.
1b8fa 20 61 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 a value returne
1b8fb 64 20 62 79 20 65 76 61 6c 75 61 74 69 6e 67 20 d by evaluating
1b8fc 61 6e 20 53 51 4c 20 65 78 70 72 65 73 73 69 6f an SQL expressio
1b8fd 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 73 65 6c 65 n in the.** sele
1b8fe 63 74 20 6c 69 73 74 20 6f 66 20 61 20 53 45 4c ct list of a SEL
1b8ff 45 43 54 20 73 74 61 74 65 6d 65 6e 74 29 20 74 ECT statement) t
1b900 68 61 74 20 6d 61 79 20 63 61 75 73 65 20 61 20 hat may cause a
1b901 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 malloc() failure
1b902 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 . If .** malloc(
1b903 29 20 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68 ) has failed, th
1b904 65 20 74 68 72 65 61 64 73 20 6d 61 6c 6c 6f 63 e threads malloc
1b905 46 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20 63 Failed flag is c
1b906 6c 65 61 72 65 64 20 61 6e 64 20 74 68 65 20 72 leared and the r
1b907 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 20 6f 66 esult.** code of
1b908 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 statement pStmt
1b909 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e set to SQLITE_N
1b90a 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 OMEM..**.** Spec
1b90b 69 66 69 63 61 6c 6c 79 2c 20 74 68 69 73 20 69 ifically, this i
1b90c 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 s called from wi
1b90d 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 thin:.**.**
1b90e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 sqlite3_column_i
1b90f 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 nt().** sqli
1b910 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 te3_column_int64
1b911 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
1b912 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 0a 3_column_text().
1b913 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1b914 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a olumn_text16().*
1b915 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
1b916 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a 2a 2a 20 20 lumn_real().**
1b917 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d sqlite3_colum
1b918 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 20 20 20 n_bytes().**
1b919 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1b91a 62 79 74 65 73 31 36 28 29 0a 2a 2a 0a 2a 2a 20 bytes16().**.**
1b91b 42 75 74 20 6e 6f 74 20 66 6f 72 20 73 71 6c 69 But not for sqli
1b91c 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
1b91d 29 2c 20 77 68 69 63 68 20 6e 65 76 65 72 20 63 ), which never c
1b91e 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a alls malloc()..*
1b91f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f /.static void co
1b920 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 lumnMallocFailur
1b921 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1b922 70 53 74 6d 74 29 0a 7b 0a 20 20 2f 2a 20 49 66 pStmt).{. /* If
1b923 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 malloc() failed
1b924 20 64 75 72 69 6e 67 20 61 6e 20 65 6e 63 6f 64 during an encod
1b925 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20 77 ing conversion w
1b926 69 74 68 69 6e 20 61 6e 0a 20 20 2a 2a 20 73 71 ithin an. ** sq
1b927 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 58 58 58 lite3_column_XXX
1b928 20 41 50 49 2c 20 74 68 65 6e 20 73 65 74 20 74 API, then set t
1b929 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f he return code o
1b92a 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
1b92b 74 6f 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4e to. ** SQLITE_N
1b92c 4f 4d 45 4d 2e 20 54 68 65 20 6e 65 78 74 20 63 OMEM. The next c
1b92d 61 6c 6c 20 74 6f 20 5f 73 74 65 70 28 29 20 28 all to _step() (
1b92e 69 66 20 61 6e 79 29 20 77 69 6c 6c 20 72 65 74 if any) will ret
1b92f 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
1b930 0a 20 20 2a 2a 20 61 6e 64 20 5f 66 69 6e 61 6c . ** and _final
1b931 69 7a 65 28 29 20 77 69 6c 6c 20 72 65 74 75 72 ize() will retur
1b932 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 n NOMEM.. */.
1b933 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 Vdbe *p = (Vdbe
1b934 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 *)pStmt;. if( p
1b935 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1b936 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 sqlite3ApiExit(p
1b937 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 20 20 ->db, p->rc);.
1b938 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1b939 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
1b93a 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a ex);. }.}../***
1b93b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b93c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 ********* sqlite
1b93d 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a 3_column_ *****
1b93e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b93f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1b940 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 e following rout
1b941 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f ines are used to
1b942 20 61 63 63 65 73 73 20 65 6c 65 6d 65 6e 74 73 access elements
1b943 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
1b944 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 row.** in the re
1b945 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c sult set..*/.SQL
1b946 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1b947 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
1b948 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f mn_blob(sqlite3_
1b949 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1b94a 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 i){. const voi
1b94b 64 20 2a 76 61 6c 3b 0a 20 20 76 61 6c 20 3d 20 d *val;. val =
1b94c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c sqlite3_value_bl
1b94d 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 ob( columnMem(pS
1b94e 74 6d 74 2c 69 29 20 29 3b 0a 20 20 2f 2a 20 45 tmt,i) );. /* E
1b94f 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 72 65 ven though there
1b950 20 69 73 20 6e 6f 20 65 6e 63 6f 64 69 6e 67 20 is no encoding
1b951 63 6f 6e 76 65 72 73 69 6f 6e 2c 20 76 61 6c 75 conversion, valu
1b952 65 5f 62 6c 6f 62 28 29 20 6d 69 67 68 74 0a 20 e_blob() might.
1b953 20 2a 2a 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c ** need to call
1b954 20 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 65 78 70 malloc() to exp
1b955 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 6f and the result o
1b956 66 20 61 20 7a 65 72 6f 62 6c 6f 62 28 29 20 0a f a zeroblob() .
1b957 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e ** expression.
1b958 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c 75 6d 6e 4d . */. columnM
1b959 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
1b95a 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 mt);. return va
1b95b 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 l;.}.SQLITE_API
1b95c 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 int sqlite3_colu
1b95d 6d 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 mn_bytes(sqlite3
1b95e 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1b95f 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 t i){. int val
1b960 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1b961 62 79 74 65 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d bytes( columnMem
1b962 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 (pStmt,i) );. c
1b963 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1b964 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
1b965 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 urn val;.}.SQLIT
1b966 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1b967 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 3_column_bytes16
1b968 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1b969 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
1b96a 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 int val = sqlite
1b96b 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 3_value_bytes16(
1b96c 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1b96d 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
1b96e 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
1b96f 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 mt);. return va
1b970 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 l;.}.SQLITE_API
1b971 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 double sqlite3_c
1b972 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c olumn_double(sql
1b973 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1b974 2c 20 69 6e 74 20 69 29 7b 0a 20 20 64 6f 75 62 , int i){. doub
1b975 6c 65 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 le val = sqlite3
1b976 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 20 63 _value_double( c
1b977 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
1b978 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c ) );. columnMal
1b979 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 locFailure(pStmt
1b97a 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b );. return val;
1b97b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
1b97c 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e t sqlite3_column
1b97d 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d _int(sqlite3_stm
1b97e 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1b97f 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 {. int val = sq
1b980 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 lite3_value_int(
1b981 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1b982 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
1b983 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
1b984 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 mt);. return va
1b985 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 l;.}.SQLITE_API
1b986 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c sqlite_int64 sql
1b987 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 ite3_column_int6
1b988 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 4(sqlite3_stmt *
1b989 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1b98a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 61 sqlite_int64 va
1b98b 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
1b98c 65 5f 69 6e 74 36 34 28 20 63 6f 6c 75 6d 6e 4d e_int64( columnM
1b98d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1b98e 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1b98f 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1b990 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
1b991 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e ITE_API const un
1b992 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c signed char *sql
1b993 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
1b994 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1b995 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
1b996 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1b997 68 61 72 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74 har *val = sqlit
1b998 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 20 63 e3_value_text( c
1b999 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
1b99a 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c ) );. columnMal
1b99b 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 locFailure(pStmt
1b99c 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b );. return val;
1b99d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 .}.SQLITE_API sq
1b99e 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c lite3_value *sql
1b99f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 ite3_column_valu
1b9a0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1b9a1 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1b9a2 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 63 6f 6c Mem *pOut = col
1b9a3 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 20 69 29 umnMem(pStmt, i)
1b9a4 3b 0a 20 20 69 66 28 20 70 4f 75 74 2d 3e 66 6c ;. if( pOut->fl
1b9a5 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 20 29 ags&MEM_Static )
1b9a6 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 {. pOut->flag
1b9a7 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 61 74 69 63 s &= ~MEM_Static
1b9a8 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 ;. pOut->flag
1b9a9 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a s |= MEM_Ephem;.
1b9aa 20 20 7d 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c }. columnMall
1b9ab 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 ocFailure(pStmt)
1b9ac 3b 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 ;. return (sqli
1b9ad 74 65 33 5f 76 61 6c 75 65 20 2a 29 70 4f 75 74 te3_value *)pOut
1b9ae 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1b9af 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1b9b0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1b9b1 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
1b9b2 75 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74 umn_text16(sqlit
1b9b3 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1b9b4 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 int i){. const
1b9b5 76 6f 69 64 20 2a 76 61 6c 20 3d 20 73 71 6c 69 void *val = sqli
1b9b6 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
1b9b7 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d ( columnMem(pStm
1b9b8 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e t,i) );. column
1b9b9 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 MallocFailure(pS
1b9ba 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 tmt);. return v
1b9bb 61 6c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 al;.}.#endif /*
1b9bc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1b9bd 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 6 */.SQLITE_API
1b9be 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 int sqlite3_colu
1b9bf 6d 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f mn_type(sqlite3_
1b9c0 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1b9c1 20 69 29 7b 0a 20 20 69 6e 74 20 69 54 79 70 65 i){. int iType
1b9c2 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
1b9c3 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d _type( columnMem
1b9c4 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 (pStmt,i) );. c
1b9c5 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1b9c6 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
1b9c7 75 72 6e 20 69 54 79 70 65 3b 0a 7d 0a 0a 2f 2a urn iType;.}../*
1b9c8 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 The following f
1b9c9 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 70 65 72 unction is exper
1b9ca 69 6d 65 6e 74 61 6c 20 61 6e 64 20 73 75 62 6a imental and subj
1b9cb 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 ect to change or
1b9cc 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a 2f .** removal */./
1b9cd 2a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c *int sqlite3_col
1b9ce 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 umn_numeric_type
1b9cf 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1b9d0 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 2a 2a Stmt, int i){.**
1b9d1 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1b9d2 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 _value_numeric_t
1b9d3 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 ype( columnMem(p
1b9d4 53 74 6d 74 2c 69 29 20 29 3b 0a 2a 2a 7d 0a 2a Stmt,i) );.**}.*
1b9d5 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 /../*.** Convert
1b9d6 20 74 68 65 20 4e 2d 74 68 20 65 6c 65 6d 65 6e the N-th elemen
1b9d7 74 20 6f 66 20 70 53 74 6d 74 2d 3e 70 43 6f 6c t of pStmt->pCol
1b9d8 4e 61 6d 65 5b 5d 20 69 6e 74 6f 20 61 20 73 74 Name[] into a st
1b9d9 72 69 6e 67 20 75 73 69 6e 67 0a 2a 2a 20 78 46 ring using.** xF
1b9da 75 6e 63 28 29 20 74 68 65 6e 20 72 65 74 75 72 unc() then retur
1b9db 6e 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20 n that string.
1b9dc 49 66 20 4e 20 69 73 20 6f 75 74 20 6f 66 20 72 If N is out of r
1b9dd 61 6e 67 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a ange, return 0..
1b9de 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
1b9df 75 70 20 74 6f 20 35 20 6e 61 6d 65 73 20 66 6f up to 5 names fo
1b9e0 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 20 20 r each column.
1b9e1 75 73 65 54 79 70 65 20 64 65 74 65 72 6d 69 6e useType determin
1b9e2 65 73 20 77 68 69 63 68 0a 2a 2a 20 6e 61 6d 65 es which.** name
1b9e3 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 48 is returned. H
1b9e4 65 72 65 20 61 72 65 20 74 68 65 20 6e 61 6d 65 ere are the name
1b9e5 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 s:.**.** 0
1b9e6 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 The column na
1b9e7 6d 65 20 61 73 20 69 74 20 73 68 6f 75 6c 64 20 me as it should
1b9e8 62 65 20 64 69 73 70 6c 61 79 65 64 20 66 6f 72 be displayed for
1b9e9 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 31 20 output.** 1
1b9ea 20 20 20 20 20 54 68 65 20 64 61 74 61 74 79 70 The datatyp
1b9eb 65 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 e name for the c
1b9ec 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 32 20 20 20 olumn.** 2
1b9ed 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 The name of t
1b9ee 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 he database that
1b9ef 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69 the column deri
1b9f0 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 33 ves from.** 3
1b9f1 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 6f The name o
1b9f2 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 f the table that
1b9f3 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69 the column deri
1b9f4 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 34 ves from.** 4
1b9f5 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 6f The name o
1b9f6 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 f the table colu
1b9f7 6d 6e 20 74 68 61 74 20 74 68 65 20 72 65 73 75 mn that the resu
1b9f8 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 lt column derive
1b9f9 73 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 20 s from.**.** If
1b9fa 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f the result is no
1b9fb 74 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d t a simple colum
1b9fc 6e 20 72 65 66 65 72 65 6e 63 65 20 28 69 66 20 n reference (if
1b9fd 69 74 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 it is an express
1b9fe 69 6f 6e 0a 2a 2a 20 6f 72 20 61 20 63 6f 6e 73 ion.** or a cons
1b9ff 74 61 6e 74 29 20 74 68 65 6e 20 75 73 65 54 79 tant) then useTy
1ba00 70 65 73 20 32 2c 20 33 2c 20 61 6e 64 20 34 20 pes 2, 3, and 4
1ba01 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a return NULL..*/.
1ba02 73 74 61 74 69 63 20 63 6f 6e 73 74 20 76 6f 69 static const voi
1ba03 64 20 2a 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 d *columnName(.
1ba04 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
1ba05 53 74 6d 74 2c 0a 20 20 69 6e 74 20 4e 2c 0a 20 Stmt,. int N,.
1ba06 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 28 2a 78 const void *(*x
1ba07 46 75 6e 63 29 28 4d 65 6d 2a 29 2c 0a 20 20 69 Func)(Mem*),. i
1ba08 6e 74 20 75 73 65 54 79 70 65 0a 29 7b 0a 20 20 nt useType.){.
1ba09 63 6f 6e 73 74 20 76 6f 69 64 20 2a 72 65 74 20 const void *ret
1ba0a 3d 20 30 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d = 0;. Vdbe *p =
1ba0b 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a (Vdbe *)pStmt;.
1ba0c 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
1ba0d 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a e3 *db = p->db;.
1ba0e 20 20 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 . assert( db!
1ba0f 3d 30 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 =0 );. n = sqli
1ba10 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 te3_column_count
1ba11 28 70 53 74 6d 74 29 3b 0a 20 20 69 66 28 20 4e (pStmt);. if( N
1ba12 3c 6e 20 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 20 <n && N>=0 ){.
1ba13 20 20 4e 20 2b 3d 20 75 73 65 54 79 70 65 2a 6e N += useType*n
1ba14 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1ba15 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 tex_enter(db->mu
1ba16 74 65 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 tex);. assert
1ba17 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1ba18 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 ed==0 );. ret
1ba19 20 3d 20 78 46 75 6e 63 28 26 70 2d 3e 61 43 6f = xFunc(&p->aCo
1ba1a 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 20 20 20 20 20 lName[N]);.
1ba1b 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 61 79 20 /* A malloc may
1ba1c 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 have failed insi
1ba1d 64 65 20 6f 66 20 74 68 65 20 78 46 75 6e 63 28 de of the xFunc(
1ba1e 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 69 73 0a ) call. If this.
1ba1f 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61 ** is the ca
1ba20 73 65 2c 20 63 6c 65 61 72 20 74 68 65 20 6d 61 se, clear the ma
1ba21 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
1ba22 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e and return NULL.
1ba23 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1ba24 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1ba25 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 ){. db->ma
1ba26 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a llocFailed = 0;.
1ba27 20 20 20 20 20 20 72 65 74 20 3d 20 30 3b 0a 20 ret = 0;.
1ba28 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1ba29 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d _mutex_leave(db-
1ba2a 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
1ba2b 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a eturn ret;.}../*
1ba2c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1ba2d 61 6d 65 20 6f 66 20 74 68 65 20 4e 74 68 20 63 ame of the Nth c
1ba2e 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 olumn of the res
1ba2f 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 ult set returned
1ba30 20 62 79 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 by SQL.** state
1ba31 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 ment pStmt..*/.S
1ba32 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1ba33 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
1ba34 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 lumn_name(sqlite
1ba35 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1ba36 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
1ba37 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
1ba38 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
1ba39 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
1ba3a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
1ba3b 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 text, COLNAME_NA
1ba3c 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 ME);.}.#ifndef S
1ba3d 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
1ba3e 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1ba3f 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
1ba40 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 column_name16(sq
1ba41 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1ba42 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1ba43 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1ba44 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1ba45 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1ba46 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1ba47 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e lue_text16, COLN
1ba48 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 65 6e AME_NAME);.}.#en
1ba49 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 dif../*.** Const
1ba4a 72 61 69 6e 74 3a 20 20 49 66 20 79 6f 75 20 68 raint: If you h
1ba4b 61 76 65 20 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d ave ENABLE_COLUM
1ba4c 4e 5f 4d 45 54 41 44 41 54 41 20 74 68 65 6e 20 N_METADATA then
1ba4d 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 6e 6f 74 20 you must.** not
1ba4e 64 65 66 69 6e 65 20 4f 4d 49 54 5f 44 45 43 4c define OMIT_DECL
1ba4f 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 TYPE..*/.#if def
1ba50 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1ba51 5f 44 45 43 4c 54 59 50 45 29 20 26 26 20 64 65 _DECLTYPE) && de
1ba52 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 fined(SQLITE_ENA
1ba53 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 BLE_COLUMN_METAD
1ba54 41 54 41 29 0a 23 20 65 72 72 6f 72 20 22 4d 75 ATA).# error "Mu
1ba55 73 74 20 6e 6f 74 20 64 65 66 69 6e 65 20 62 6f st not define bo
1ba56 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 th SQLITE_OMIT_D
1ba57 45 43 4c 54 59 50 45 20 5c 0a 20 20 20 20 20 20 ECLTYPE \.
1ba58 20 20 20 61 6e 64 20 53 51 4c 49 54 45 5f 45 4e and SQLITE_EN
1ba59 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 ABLE_COLUMN_META
1ba5a 44 41 54 41 22 0a 23 65 6e 64 69 66 0a 0a 23 69 DATA".#endif..#i
1ba5b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1ba5c 54 5f 44 45 43 4c 54 59 50 45 0a 2f 2a 0a 2a 2a T_DECLTYPE./*.**
1ba5d 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 75 Return the colu
1ba5e 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 mn declaration t
1ba5f 79 70 65 20 28 69 66 20 61 70 70 6c 69 63 61 62 ype (if applicab
1ba60 6c 65 29 20 6f 66 20 74 68 65 20 27 69 27 74 68 le) of the 'i'th
1ba61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 column.** of th
1ba62 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 e result set of
1ba63 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 53 SQL statement pS
1ba64 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 tmt..*/.SQLITE_A
1ba65 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
1ba66 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 qlite3_column_de
1ba67 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 cltype(sqlite3_s
1ba68 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1ba69 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c N){. return col
1ba6a 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 umnName(. p
1ba6b 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 Stmt, N, (const
1ba6c 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 void*(*)(Mem*))s
1ba6d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1ba6e 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 t, COLNAME_DECLT
1ba6f 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 YPE);.}.#ifndef
1ba70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1ba71 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 6.SQLITE_API con
1ba72 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1ba73 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 _column_decltype
1ba74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 16(sqlite3_stmt
1ba75 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
1ba76 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
1ba77 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
1ba78 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
1ba79 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
1ba7a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c e3_value_text16,
1ba7b 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 COLNAME_DECLTYP
1ba7c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 E);.}.#endif /*
1ba7d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 SQLITE_OMIT_UTF1
1ba7e 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 6 */.#endif /* S
1ba7f 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 QLITE_OMIT_DECLT
1ba80 59 50 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 YPE */..#ifdef S
1ba81 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
1ba82 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 2f 2a 0a UMN_METADATA./*.
1ba83 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 ** Return the na
1ba84 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1ba85 73 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 se from which a
1ba86 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 result column de
1ba87 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 rives..** NULL i
1ba88 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 s returned if th
1ba89 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 e result column
1ba8a 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e is an expression
1ba8b 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a or constant or.
1ba8c 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 ** anything else
1ba8d 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e which is not an
1ba8e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 unabiguous refe
1ba8f 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 rence to a datab
1ba90 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 ase column..*/.S
1ba91 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1ba92 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f char *sqlite3_co
1ba93 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 lumn_database_na
1ba94 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 me(sqlite3_stmt
1ba95 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a *pStmt, int N){.
1ba96 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e return columnN
1ba97 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 ame(. pStmt
1ba98 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 , N, (const void
1ba99 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 *(*)(Mem*))sqlit
1ba9a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 e3_value_text, C
1ba9b 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 OLNAME_DATABASE)
1ba9c 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1ba9d 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1ba9e 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 LITE_API const v
1ba9f 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c oid *sqlite3_col
1baa0 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d umn_database_nam
1baa1 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 e16(sqlite3_stmt
1baa2 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
1baa3 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
1baa4 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
1baa5 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
1baa6 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
1baa7 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
1baa8 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 , COLNAME_DATABA
1baa9 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a SE);.}.#endif /*
1baaa 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1baab 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 16 */../*.** Ret
1baac 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 urn the name of
1baad 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 the table from w
1baae 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f hich a result co
1baaf 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a lumn derives..**
1bab0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
1bab1 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 d if the result
1bab2 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 column is an exp
1bab3 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 ression or const
1bab4 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 ant or.** anythi
1bab5 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 ng else which is
1bab6 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f not an unabiguo
1bab7 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 us reference to
1bab8 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d a database colum
1bab9 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 n..*/.SQLITE_API
1baba 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
1babb 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c ite3_column_tabl
1babc 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 e_name(sqlite3_s
1babd 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1babe 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c N){. return col
1babf 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 umnName(. p
1bac0 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 Stmt, N, (const
1bac1 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 void*(*)(Mem*))s
1bac2 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1bac3 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 t, COLNAME_TABLE
1bac4 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c );.}.#ifndef SQL
1bac5 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 ITE_OMIT_UTF16.S
1bac6 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
1bac7 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
1bac8 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 lumn_table_name1
1bac9 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 6(sqlite3_stmt *
1baca 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 pStmt, int N){.
1bacb 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 return columnNa
1bacc 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c me(. pStmt,
1bacd 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a N, (const void*
1bace 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 (*)(Mem*))sqlite
1bacf 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 3_value_text16,
1bad0 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a COLNAME_TABLE);.
1bad1 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1bad2 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
1bad3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1bad4 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
1bad5 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d able column from
1bad6 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 which a result
1bad7 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a column derives..
1bad8 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 ** NULL is retur
1bad9 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c ned if the resul
1bada 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 t column is an e
1badb 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e xpression or con
1badc 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 stant or.** anyt
1badd 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 hing else which
1bade 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 is not an unabig
1badf 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 uous reference t
1bae0 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c o a database col
1bae1 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 umn..*/.SQLITE_A
1bae2 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
1bae3 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 qlite3_column_or
1bae4 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 igin_name(sqlite
1bae5 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1bae6 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
1bae7 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
1bae8 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
1bae9 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
1baea 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
1baeb 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f text, COLNAME_CO
1baec 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 LUMN);.}.#ifndef
1baed 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1baee 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 16.SQLITE_API co
1baef 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1baf0 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 3_column_origin_
1baf1 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 name16(sqlite3_s
1baf2 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1baf3 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c N){. return col
1baf4 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 umnName(. p
1baf5 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 Stmt, N, (const
1baf6 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 void*(*)(Mem*))s
1baf7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1baf8 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c t16, COLNAME_COL
1baf9 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f UMN);.}.#endif /
1bafa 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
1bafb 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a F16 */.#endif /*
1bafc 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 SQLITE_ENABLE_C
1bafd 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 2a OLUMN_METADATA *
1bafe 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /.../***********
1baff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bb00 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e **** sqlite3_bin
1bb01 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a d_ ************
1bb02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1bb03 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 ** .** Routines
1bb04 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 20 76 used to attach v
1bb05 61 6c 75 65 73 20 74 6f 20 77 69 6c 64 63 61 72 alues to wildcar
1bb06 64 73 20 69 6e 20 61 20 63 6f 6d 70 69 6c 65 64 ds in a compiled
1bb07 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a SQL statement..
1bb08 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20 */./*.** Unbind
1bb09 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e 64 20 the value bound
1bb0a 74 6f 20 76 61 72 69 61 62 6c 65 20 69 20 69 6e to variable i in
1bb0b 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1bb0c 20 70 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 p. This is the
1bb0d 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 .** the same as
1bb0e 62 69 6e 64 69 6e 67 20 61 20 4e 55 4c 4c 20 76 binding a NULL v
1bb0f 61 6c 75 65 20 74 6f 20 74 68 65 20 63 6f 6c 75 alue to the colu
1bb10 6d 6e 2e 20 49 66 20 74 68 65 20 22 69 22 20 70 mn. If the "i" p
1bb11 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6f arameter is.** o
1bb12 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 ut of range, the
1bb13 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 69 n SQLITE_RANGE i
1bb14 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 s returned. Othe
1bb15 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a wise SQLITE_OK..
1bb16 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 65 73 73 66 **.** A successf
1bb17 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 ul evaluation of
1bb18 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 this routine ac
1bb19 71 75 69 72 65 73 20 74 68 65 20 6d 75 74 65 78 quires the mutex
1bb1a 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 65 20 6d 75 on p..** the mu
1bb1b 74 65 78 20 69 73 20 72 65 6c 65 61 73 65 64 20 tex is released
1bb1c 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 if any kind of e
1bb1d 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a rror occurs..**.
1bb1e 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 ** The error cod
1bb1f 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 e stored in data
1bb20 62 61 73 65 20 70 2d 3e 64 62 20 69 73 20 6f 76 base p->db is ov
1bb21 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 74 erwritten with t
1bb22 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c he return.** val
1bb23 75 65 20 69 6e 20 61 6e 79 20 63 61 73 65 2e 0a ue in any case..
1bb24 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 */.static int vd
1bb25 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a 70 beUnbind(Vdbe *p
1bb26 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 , int i){. Mem
1bb27 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 70 3d 3d *pVar;. if( p==
1bb28 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
1bb29 45 5f 4d 49 53 55 53 45 3b 0a 20 20 73 71 6c 69 E_MISUSE;. sqli
1bb2a 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1bb2b 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1bb2c 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56 if( p->magic!=V
1bb2d 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c 7c DBE_MAGIC_RUN ||
1bb2e 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 p->pc>=0 ){.
1bb2f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d sqlite3Error(p-
1bb30 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53 55 >db, SQLITE_MISU
1bb31 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 SE, 0);. sqli
1bb32 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1bb33 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1bb34 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1bb35 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 _MISUSE;. }. i
1bb36 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e f( i<1 || i>p->n
1bb37 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Var ){. sqlit
1bb38 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 e3Error(p->db, S
1bb39 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b QLITE_RANGE, 0);
1bb3a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1bb3b 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e ex_leave(p->db->
1bb3c 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75 mutex);. retu
1bb3d 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 3b rn SQLITE_RANGE;
1bb3e 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a 20 20 70 56 . }. i--;. pV
1bb3f 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69 5d ar = &p->aVar[i]
1bb40 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1bb41 65 6d 52 65 6c 65 61 73 65 28 70 56 61 72 29 3b emRelease(pVar);
1bb42 0a 20 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 3d . pVar->flags =
1bb43 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 73 71 6c MEM_Null;. sql
1bb44 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c ite3Error(p->db,
1bb45 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a SQLITE_OK, 0);.
1bb46 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1bb47 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e OK;.}../*.** Bin
1bb48 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 d a text or BLOB
1bb49 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 value..*/.stati
1bb4a 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 28 0a c int bindText(.
1bb4b 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1bb4c 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 65 20 pStmt, /* The
1bb4d 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 69 6e statement to bin
1bb4e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 d against */. i
1bb4f 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 nt i,
1bb50 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1bb51 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 f the parameter
1bb52 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e to bind */. con
1bb53 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 st void *zData,
1bb54 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1bb55 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 62 65 o the data to be
1bb56 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 bound */. int
1bb57 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 20 nData,
1bb58 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1bb59 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 74 6f bytes of data to
1bb5a 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 76 be bound */. v
1bb5b 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1bb5c 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 75 63 *), /* Destruc
1bb5d 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 tor for the data
1bb5e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e */. u8 encodin
1bb5f 67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 g /*
1bb60 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 Encoding for the
1bb61 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 56 64 data */.){. Vd
1bb62 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 be *p = (Vdbe *)
1bb63 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70 56 pStmt;. Mem *pV
1bb64 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 ar;. int rc;..
1bb65 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 rc = vdbeUnbind
1bb66 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 (p, i);. if( rc
1bb67 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1bb68 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d 30 20 if( zData!=0
1bb69 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 3d 20 ){. pVar =
1bb6a 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a 20 &p->aVar[i-1];.
1bb6b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1bb6c 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
1bb6d 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 Var, zData, nDat
1bb6e 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65 a, encoding, xDe
1bb6f 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 l);. if( rc
1bb70 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 ==SQLITE_OK && e
1bb71 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20 20 ncoding!=0 ){.
1bb72 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1bb73 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
1bb74 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 28 70 ding(pVar, ENC(p
1bb75 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 7d 0a ->db));. }.
1bb76 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1bb77 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 29 or(p->db, rc, 0)
1bb78 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1bb79 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 ite3ApiExit(p->d
1bb7a 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 b, rc);. }.
1bb7b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1bb7c 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
1bb7d 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
1bb7e 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 n rc;.}.../*.**
1bb7f 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c 75 Bind a blob valu
1bb80 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 61 74 e to an SQL stat
1bb81 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a ement variable..
1bb82 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1bb83 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 t sqlite3_bind_b
1bb84 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 lob(. sqlite3_s
1bb85 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 tmt *pStmt, . i
1bb86 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 nt i, . const v
1bb87 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 oid *zData, . i
1bb88 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 nt nData, . voi
1bb89 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
1bb8a 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e .){. return bin
1bb8b 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 dText(pStmt, i,
1bb8c 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 zData, nData, xD
1bb8d 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 el, 0);.}.SQLITE
1bb8e 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bb8f 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c _bind_double(sql
1bb90 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bb91 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20 , int i, double
1bb92 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 rValue){. int r
1bb93 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 c;. Vdbe *p = (
1bb94 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
1bb95 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 rc = vdbeUnbind(
1bb96 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d p, i);. if( rc=
1bb97 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1bb98 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1bb99 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e 61 56 SetDouble(&p->aV
1bb9a 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 65 29 ar[i-1], rValue)
1bb9b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1bb9c 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
1bb9d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 >mutex);. }. r
1bb9e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 eturn rc;.}.SQLI
1bb9f 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1bba0 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69 e3_bind_int(sqli
1bba1 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e 74 te3_stmt *p, int
1bba2 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29 7b i, int iValue){
1bba3 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
1bba4 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c 20 3_bind_int64(p,
1bba5 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29 3b i, (i64)iValue);
1bba6 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
1bba7 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 t sqlite3_bind_i
1bba8 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d nt64(sqlite3_stm
1bba9 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c t *pStmt, int i,
1bbaa 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 56 sqlite_int64 iV
1bbab 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b alue){. int rc;
1bbac 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
1bbad 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 be *)pStmt;. rc
1bbae 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c = vdbeUnbind(p,
1bbaf 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 i);. if( rc==S
1bbb0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1bbb1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1bbb2 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 72 5b tInt64(&p->aVar[
1bbb3 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b 0a 20 i-1], iValue);.
1bbb4 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1bbb5 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 _leave(p->db->mu
1bbb6 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
1bbb7 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f rn rc;.}.SQLITE_
1bbb8 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bbb9 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 bind_null(sqlite
1bbba 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1bbbb 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72 63 3b nt i){. int rc;
1bbbc 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 . Vdbe *p = (Vd
1bbbd 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 be*)pStmt;. rc
1bbbe 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 = vdbeUnbind(p,
1bbbf 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 i);. if( rc==SQ
1bbc0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 LITE_OK ){. s
1bbc1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1bbc2 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
1bbc3 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
1bbc4 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 c;.}.SQLITE_API
1bbc5 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1bbc6 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74 65 _text( . sqlite
1bbc7 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 3_stmt *pStmt, .
1bbc8 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 int i, . cons
1bbc9 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20 0a t char *zData, .
1bbca 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 int nData, .
1bbcb 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
1bbcc 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 d*).){. return
1bbcd 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 bindText(pStmt,
1bbce 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c i, zData, nData,
1bbcf 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 xDel, SQLITE_UT
1bbd0 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 F8);.}.#ifndef S
1bbd1 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
1bbd2 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1bbd3 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 sqlite3_bind_tex
1bbd4 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 t16(. sqlite3_s
1bbd5 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 tmt *pStmt, . i
1bbd6 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 nt i, . const v
1bbd7 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 oid *zData, . i
1bbd8 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 nt nData, . voi
1bbd9 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
1bbda 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e .){. return bin
1bbdb 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 dText(pStmt, i,
1bbdc 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 zData, nData, xD
1bbdd 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 el, SQLITE_UTF16
1bbde 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 NATIVE);.}.#endi
1bbdf 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1bbe0 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 _UTF16 */.SQLITE
1bbe1 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bbe2 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 _bind_value(sqli
1bbe3 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bbe4 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 73 71 int i, const sq
1bbe5 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1bbe6 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a lue){. int rc;.
1bbe7 20 20 73 77 69 74 63 68 28 20 70 56 61 6c 75 65 switch( pValue
1bbe8 2d 3e 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 ->type ){. ca
1bbe9 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 se SQLITE_INTEGE
1bbea 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 R: {. rc =
1bbeb 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 sqlite3_bind_int
1bbec 36 34 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 64(pStmt, i, pVa
1bbed 6c 75 65 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 lue->u.i);.
1bbee 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1bbef 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c case SQLITE_FL
1bbf0 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 OAT: {. rc
1bbf1 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 = sqlite3_bind_d
1bbf2 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 69 2c 20 ouble(pStmt, i,
1bbf3 70 56 61 6c 75 65 2d 3e 72 29 3b 0a 20 20 20 20 pValue->r);.
1bbf4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1bbf5 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 case SQLITE_B
1bbf6 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 LOB: {. if(
1bbf7 20 70 56 61 6c 75 65 2d 3e 66 6c 61 67 73 20 26 pValue->flags &
1bbf8 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 MEM_Zero ){.
1bbf9 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1bbfa 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 3_bind_zeroblob(
1bbfb 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 pStmt, i, pValue
1bbfc 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 ->u.nZero);.
1bbfd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1bbfe 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 rc = sqlite3_bi
1bbff 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69 nd_blob(pStmt, i
1bc00 2c 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 , pValue->z, pVa
1bc01 6c 75 65 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54 52 lue->n,SQLITE_TR
1bc02 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 ANSIENT);.
1bc03 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1bc04 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
1bc05 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 LITE_TEXT: {.
1bc06 20 20 20 72 63 20 3d 20 62 69 6e 64 54 65 78 74 rc = bindText
1bc07 28 70 53 74 6d 74 2c 69 2c 20 20 70 56 61 6c 75 (pStmt,i, pValu
1bc08 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c e->z, pValue->n,
1bc09 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
1bc0a 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 T,.
1bc0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bc0c 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20 pValue->enc);.
1bc0d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1bc0e 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
1bc0f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1bc10 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 te3_bind_null(pS
1bc11 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 62 tmt, i);. b
1bc12 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1bc13 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 return rc;.}.S
1bc14 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bc15 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 lite3_bind_zerob
1bc16 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 lob(sqlite3_stmt
1bc17 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 *pStmt, int i,
1bc18 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 int n){. int rc
1bc19 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 ;. Vdbe *p = (V
1bc1a 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 dbe *)pStmt;. r
1bc1b 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 c = vdbeUnbind(p
1bc1c 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , i);. if( rc==
1bc1d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1bc1e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1bc1f 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 etZeroBlob(&p->a
1bc20 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 Var[i-1], n);.
1bc21 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1bc22 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
1bc23 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ex);. }. retur
1bc24 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
1bc25 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1bc26 20 6f 66 20 77 69 6c 64 63 61 72 64 73 20 74 68 of wildcards th
1bc27 61 74 20 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 at can be potent
1bc28 69 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a ially bound to..
1bc29 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1bc2a 69 73 20 61 64 64 65 64 20 74 6f 20 73 75 70 70 is added to supp
1bc2b 6f 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e ort DBD::SQLite.
1bc2c 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 .*/.SQLITE_API
1bc2d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1bc2e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e d_parameter_coun
1bc2f 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a t(sqlite3_stmt *
1bc30 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a pStmt){. Vdbe *
1bc31 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 p = (Vdbe*)pStmt
1bc32 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70 ;. return p ? p
1bc33 2d 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f ->nVar : 0;.}../
1bc34 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6d 61 *.** Create a ma
1bc35 70 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 pping from varia
1bc36 62 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 ble numbers to v
1bc37 61 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a ariable names.**
1bc38 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 7a 56 in the Vdbe.azV
1bc39 61 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 73 ar[] array, if s
1bc3a 75 63 68 20 61 20 6d 61 70 70 69 6e 67 20 64 6f uch a mapping do
1bc3b 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a es not already.*
1bc3c 2a 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 * exist..*/.stat
1bc3d 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 56 61 ic void createVa
1bc3e 72 4d 61 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 rMap(Vdbe *p){.
1bc3f 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 if( !p->okVar )
1bc40 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 {. int j;.
1bc41 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 73 71 Op *pOp;. sq
1bc42 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1bc43 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b r(p->db->mutex);
1bc44 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 61 63 65 . /* The race
1bc45 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 20 condition here
1bc46 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 49 66 is harmless. If
1bc47 20 74 77 6f 20 74 68 72 65 61 64 73 20 63 61 6c two threads cal
1bc48 6c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 72 6f l this. ** ro
1bc49 75 74 69 6e 65 20 6f 6e 20 74 68 65 20 73 61 6d utine on the sam
1bc4a 65 20 56 64 62 65 20 61 74 20 74 68 65 20 73 61 e Vdbe at the sa
1bc4b 6d 65 20 74 69 6d 65 2c 20 74 68 65 79 20 62 6f me time, they bo
1bc4c 74 68 20 6d 69 67 68 74 20 65 6e 64 0a 20 20 20 th might end.
1bc4d 20 2a 2a 20 75 70 20 69 6e 69 74 69 61 6c 69 7a ** up initializ
1bc4e 69 6e 67 20 74 68 65 20 56 64 62 65 2e 61 7a 56 ing the Vdbe.azV
1bc4f 61 72 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 61 ar[] array. Tha
1bc50 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 65 78 t is a little ex
1bc51 74 72 61 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 20 tra. ** work
1bc52 62 75 74 20 69 74 20 72 65 73 75 6c 74 73 20 69 but it results i
1bc53 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77 65 n the same answe
1bc54 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f r.. */. fo
1bc55 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f r(j=0, pOp=p->aO
1bc56 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b p; j<p->nOp; j++
1bc57 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 , pOp++){.
1bc58 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1bc59 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b 0a =OP_Variable ){.
1bc5a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1bc5b 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 pOp->p1>0 && pOp
1bc5c 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b ->p1<=p->nVar );
1bc5d 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 . p->azVa
1bc5e 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 r[pOp->p1-1] = p
1bc5f 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 Op->p4.z;.
1bc60 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6f }. }. p->o
1bc61 6b 56 61 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 kVar = 1;. sq
1bc62 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1bc63 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b e(p->db->mutex);
1bc64 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
1bc65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
1bc66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 a wildcard para
1bc67 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e 20 4e meter. Return N
1bc68 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 65 78 ULL if the index
1bc69 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 72 61 .** is out of ra
1bc6a 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 77 69 nge or if the wi
1bc6b 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 6d 65 ldcard is unname
1bc6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 d..**.** The res
1bc6d 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 55 54 ult is always UT
1bc6e 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 F-8..*/.SQLITE_A
1bc6f 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 PI const char *s
1bc70 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
1bc71 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 meter_name(sqlit
1bc72 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bc73 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a int i){. Vdbe *
1bc74 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 p = (Vdbe*)pStmt
1bc75 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 ;. if( p==0 ||
1bc76 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 i<1 || i>p->nVar
1bc77 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
1bc78 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61 ;. }. createVa
1bc79 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 75 72 rMap(p);. retur
1bc7a 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b n p->azVar[i-1];
1bc7b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 .}../*.** Given
1bc7c 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 6d a wildcard param
1bc7d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 75 72 eter name, retur
1bc7e 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 n the index of t
1bc7f 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 77 he variable.** w
1bc80 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20 20 ith that name.
1bc81 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 76 If there is no v
1bc82 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 68 65 ariable with the
1bc83 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 given name,.**
1bc84 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c return 0..*/.SQL
1bc85 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bc86 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 te3_bind_paramet
1bc87 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33 er_index(sqlite3
1bc88 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f _stmt *pStmt, co
1bc89 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 nst char *zName)
1bc8a 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 {. Vdbe *p = (V
1bc8b 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e dbe*)pStmt;. in
1bc8c 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 t i;. if( p==0
1bc8d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
1bc8e 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61 72 . }. createVar
1bc8f 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28 20 7a Map(p); . if( z
1bc90 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 28 Name ){. for(
1bc91 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 i=0; i<p->nVar;
1bc92 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 i++){. cons
1bc93 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d 3e 61 t char *z = p->a
1bc94 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20 20 69 zVar[i];. i
1bc95 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70 28 7a f( z && strcmp(z
1bc96 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 ,zName)==0 ){.
1bc97 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 return i+1
1bc98 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1bc99 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
1bc9a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 }../*.** Transfe
1bc9b 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20 66 r all bindings f
1bc9c 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 73 74 rom the first st
1bc9d 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f 20 atement over to
1bc9e 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f 0a 53 the second..*/.S
1bc9f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1bca0 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 t sqlite3Transfe
1bca1 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 rBindings(sqlite
1bca2 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 3_stmt *pFromStm
1bca3 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 t, sqlite3_stmt
1bca4 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 62 *pToStmt){. Vdb
1bca5 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 65 e *pFrom = (Vdbe
1bca6 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 56 *)pFromStmt;. V
1bca7 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 65 dbe *pTo = (Vdbe
1bca8 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 6e 74 *)pToStmt;. int
1bca9 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 i;. assert( pT
1bcaa 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e 64 62 o->db==pFrom->db
1bcab 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 );. assert( pT
1bcac 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d 2d 3e o->nVar==pFrom->
1bcad 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 nVar );. sqlite
1bcae 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54 3_mutex_enter(pT
1bcaf 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 o->db->mutex);.
1bcb0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 72 6f for(i=0; i<pFro
1bcb1 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 m->nVar; i++){.
1bcb2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1bcb3 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 72 mMove(&pTo->aVar
1bcb4 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 61 [i], &pFrom->aVa
1bcb5 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c r[i]);. }. sql
1bcb6 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1bcb7 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 (pTo->db->mutex)
1bcb8 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1bcb9 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 E_OK;.}..#ifndef
1bcba 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 SQLITE_OMIT_DEP
1bcbb 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 RECATED./*.** De
1bcbc 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e 61 precated externa
1bcbd 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 6e l interface. In
1bcbe 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 ternal/core SQLi
1bcbf 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c te code.** shoul
1bcc0 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 54 72 d call sqlite3Tr
1bcc1 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 2e 0a ansferBindings..
1bcc2 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69 73 75 **.** Is is misu
1bcc3 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 se to call this
1bcc4 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73 74 61 routine with sta
1bcc5 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64 69 66 tements from dif
1bcc6 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61 ferent.** databa
1bcc7 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 se connections.
1bcc8 20 42 75 74 20 61 73 20 74 68 69 73 20 69 73 20 But as this is
1bcc9 61 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74 a deprecated int
1bcca 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20 77 69 erface, we.** wi
1bccb 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f ll not bother to
1bccc 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74 20 check for that
1bccd 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a condition..**.**
1bcce 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 74 If the two stat
1bccf 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 61 ements contain a
1bcd0 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 different numbe
1bcd1 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 74 r of bindings, t
1bcd2 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 45 hen.** an SQLITE
1bcd3 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e _ERROR is return
1bcd4 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65 6c 73 ed. Nothing els
1bcd5 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 2c 20 e can go wrong,
1bcd6 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 so otherwise.**
1bcd7 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
1bcd8 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
1bcd9 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1bcda 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e _transfer_bindin
1bcdb 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 gs(sqlite3_stmt
1bcdc 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 *pFromStmt, sqli
1bcdd 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d te3_stmt *pToStm
1bcde 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f t){. Vdbe *pFro
1bcdf 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d m = (Vdbe*)pFrom
1bce0 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 Stmt;. Vdbe *pT
1bce1 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74 o = (Vdbe*)pToSt
1bce2 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d mt;. if( pFrom-
1bce3 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56 61 72 >nVar!=pTo->nVar
1bce4 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1bce5 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
1bce6 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
1bce7 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 3TransferBinding
1bce8 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f s(pFromStmt, pTo
1bce9 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a Stmt);.}.#endif.
1bcea 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1bceb 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 e sqlite3* datab
1bcec 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68 ase handle to wh
1bced 69 63 68 20 74 68 65 20 70 72 65 70 61 72 65 64 ich the prepared
1bcee 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e statement given
1bcef 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d .** in the argum
1bcf0 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 ent belongs. Th
1bcf1 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 64 is is the same d
1bcf2 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 atabase handle t
1bcf3 68 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66 hat was.** the f
1bcf4 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
1bcf5 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 the sqlite3_pre
1bcf6 70 61 72 65 28 29 20 74 68 61 74 20 77 61 73 20 pare() that was
1bcf7 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a used to create.*
1bcf8 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 * the statement
1bcf9 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 in the first pla
1bcfa 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ce..*/.SQLITE_AP
1bcfb 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 I sqlite3 *sqlit
1bcfc 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c e3_db_handle(sql
1bcfd 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bcfe 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d ){. return pStm
1bcff 74 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d t ? ((Vdbe*)pStm
1bd00 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f t)->db : 0;.}../
1bd01 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
1bd02 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 inter to the nex
1bd03 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 t prepared state
1bd04 6d 65 6e 74 20 61 66 74 65 72 20 70 53 74 6d 74 ment after pStmt
1bd05 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
1bd06 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e ith database con
1bd07 6e 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 nection pDb. If
1bd08 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 pStmt is NULL,
1bd09 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 return the first
1bd0a 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 .** prepared sta
1bd0b 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64 tement for the d
1bd0c 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1bd0d 6f 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c on. Return NULL
1bd0e 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 if there.** are
1bd0f 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c no more..*/.SQL
1bd10 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
1bd11 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 stmt *sqlite3_ne
1bd12 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 xt_stmt(sqlite3
1bd13 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 *pDb, sqlite3_st
1bd14 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71 mt *pStmt){. sq
1bd15 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78 lite3_stmt *pNex
1bd16 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 t;. sqlite3_mut
1bd17 65 78 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75 ex_enter(pDb->mu
1bd18 74 65 78 29 3b 0a 20 20 69 66 28 20 70 53 74 6d tex);. if( pStm
1bd19 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 78 t==0 ){. pNex
1bd1a 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d t = (sqlite3_stm
1bd1b 74 2a 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20 t*)pDb->pVdbe;.
1bd1c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78 }else{. pNex
1bd1d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d t = (sqlite3_stm
1bd1e 74 2a 29 28 28 56 64 62 65 2a 29 70 53 74 6d 74 t*)((Vdbe*)pStmt
1bd1f 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 )->pNext;. }.
1bd20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1bd21 61 76 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b ave(pDb->mutex);
1bd22 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 78 74 3b . return pNext;
1bd23 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1bd24 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 the value of a
1bd25 73 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66 status counter f
1bd26 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 or a prepared st
1bd27 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 atement.*/.SQLIT
1bd28 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bd29 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 3_stmt_status(sq
1bd2a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bd2b 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72 t, int op, int r
1bd2c 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 62 esetFlag){. Vdb
1bd2d 65 20 2a 70 56 64 62 65 20 3d 20 28 56 64 62 65 e *pVdbe = (Vdbe
1bd2e 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76 *)pStmt;. int v
1bd2f 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 = pVdbe->aCount
1bd30 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20 er[op-1];. if(
1bd31 72 65 73 65 74 46 6c 61 67 20 29 20 70 56 64 62 resetFlag ) pVdb
1bd32 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 e->aCounter[op-1
1bd33 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 ] = 0;. return
1bd34 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a v;.}../*********
1bd35 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
1bd36 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eapi.c *********
1bd37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd39 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1bd3a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1bd3b 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.c ********
1bd3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd3e 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
1bd3f 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
1bd40 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1bd41 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1bd42 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1bd43 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1bd44 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1bd45 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1bd46 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1bd47 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1bd48 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1bd49 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1bd4a 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1bd4b 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1bd4c 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1bd4d 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1bd4e 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1bd4f 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1bd50 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1bd51 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1bd52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
1bd56 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 The code in this
1bd57 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 file implements
1bd58 20 65 78 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f execution metho
1bd59 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20 56 69 72 d of the .** Vir
1bd5a 74 75 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e tual Database En
1bd5b 67 69 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20 gine (VDBE). A
1bd5c 73 65 70 61 72 61 74 65 20 66 69 6c 65 20 28 22 separate file ("
1bd5d 76 64 62 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68 vdbeaux.c").** h
1bd5e 61 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 andles housekeep
1bd5f 69 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68 ing details such
1bd60 20 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64 as creating and
1bd61 20 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42 deleting.** VDB
1bd62 45 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68 E instances. Th
1bd63 69 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c is file is solel
1bd64 79 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20 y interested in
1bd65 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65 executing.** the
1bd66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a VDBE program..*
1bd67 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 74 65 *.** In the exte
1bd68 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 rnal interface,
1bd69 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 an "sqlite3_stmt
1bd6a 2a 22 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 *" is an opaque
1bd6b 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 pointer.** to a
1bd6c 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 VDBE..**.** The
1bd6d 53 51 4c 20 70 61 72 73 65 72 20 67 65 6e 65 72 SQL parser gener
1bd6e 61 74 65 73 20 61 20 70 72 6f 67 72 61 6d 20 77 ates a program w
1bd6f 68 69 63 68 20 69 73 20 74 68 65 6e 20 65 78 65 hich is then exe
1bd70 63 75 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 cuted by.** the
1bd71 56 44 42 45 20 74 6f 20 64 6f 20 74 68 65 20 77 VDBE to do the w
1bd72 6f 72 6b 20 6f 66 20 74 68 65 20 53 51 4c 20 73 ork of the SQL s
1bd73 74 61 74 65 6d 65 6e 74 2e 20 20 56 44 42 45 20 tatement. VDBE
1bd74 70 72 6f 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a programs are .**
1bd75 20 73 69 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d similar in form
1bd76 20 74 6f 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e to assembly lan
1bd77 67 75 61 67 65 2e 20 20 54 68 65 20 70 72 6f 67 guage. The prog
1bd78 72 61 6d 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a ram consists of.
1bd79 2a 2a 20 61 20 6c 69 6e 65 61 72 20 73 65 71 75 ** a linear sequ
1bd7a 65 6e 63 65 20 6f 66 20 6f 70 65 72 61 74 69 6f ence of operatio
1bd7b 6e 73 2e 20 20 45 61 63 68 20 6f 70 65 72 61 74 ns. Each operat
1bd7c 69 6f 6e 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 ion has an opcod
1bd7d 65 20 0a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72 e .** and 5 oper
1bd7e 61 6e 64 73 2e 20 20 4f 70 65 72 61 6e 64 73 20 ands. Operands
1bd7f 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 33 20 61 P1, P2, and P3 a
1bd80 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70 re integers. Op
1bd81 65 72 61 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20 erand P4 .** is
1bd82 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 a null-terminate
1bd83 64 20 73 74 72 69 6e 67 2e 20 20 4f 70 65 72 61 d string. Opera
1bd84 6e 64 20 50 35 20 69 73 20 61 6e 20 75 6e 73 69 nd P5 is an unsi
1bd85 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 2e 0a gned character..
1bd86 2a 2a 20 46 65 77 20 6f 70 63 6f 64 65 73 20 75 ** Few opcodes u
1bd87 73 65 20 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64 se all 5 operand
1bd88 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 s..**.** Computa
1bd89 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65 tion results are
1bd8a 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 stored on a set
1bd8b 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6e 75 of registers nu
1bd8c 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 mbered beginning
1bd8d 0a 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67 .** with 1 and g
1bd8e 6f 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e oing up to Vdbe.
1bd8f 6e 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69 nMem. Each regi
1bd90 73 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a ster can store.*
1bd91 2a 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 * either an inte
1bd92 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d ger, a null-term
1bd93 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61 inated string, a
1bd94 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a floating point.
1bd95 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68 ** number, or th
1bd96 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c e SQL "NULL" val
1bd97 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69 74 ue. An implicit
1bd98 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d conversion from
1bd99 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20 one.** type to
1bd9a 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75 72 73 the other occurs
1bd9b 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a as necessary..*
1bd9c 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 * .** Most of th
1bd9d 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 e code in this f
1bd9e 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 75 70 20 ile is taken up
1bd9f 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 by the sqlite3Vd
1bda0 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63 beExec().** func
1bda1 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20 tion which does
1bda2 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65 the work of inte
1bda3 72 70 72 65 74 69 6e 67 20 61 20 56 44 42 45 20 rpreting a VDBE
1bda4 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 program..** But
1bda5 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 73 20 61 other routines a
1bda6 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 64 re also provided
1bda7 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 75 69 6c to help in buil
1bda8 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f ding up.** a pro
1bda9 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e gram instruction
1bdaa 20 62 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e by instruction.
1bdab 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73 .**.** Various s
1bdac 63 72 69 70 74 73 20 73 63 61 6e 20 74 68 69 73 cripts scan this
1bdad 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 6e 20 source file in
1bdae 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 order to generat
1bdaf 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 e HTML.** docume
1bdb0 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 65 72 73 ntation, headers
1bdb1 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72 files, or other
1bdb2 20 64 65 72 69 76 65 64 20 66 69 6c 65 73 2e 20 derived files.
1bdb3 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a The formatting.
1bdb4 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 ** of the code i
1bdb5 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 2c 20 n this file is,
1bdb6 74 68 65 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72 therefore, impor
1bdb7 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 68 65 72 tant. See other
1bdb8 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 comments.** in
1bdb9 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65 this file for de
1bdba 74 61 69 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f tails. If in do
1bdbb 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69 ubt, do not devi
1bdbc 61 74 65 20 66 72 6f 6d 20 65 78 69 73 74 69 6e ate from existin
1bdbd 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 g.** commenting
1bdbe 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 and indentation
1bdbf 70 72 61 63 74 69 63 65 73 20 77 68 65 6e 20 63 practices when c
1bdc0 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 64 69 6e hanging or addin
1bdc1 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 g code..**.** $I
1bdc2 64 3a 20 76 64 62 65 2e 63 2c 76 20 31 2e 38 37 d: vdbe.c,v 1.87
1bdc3 34 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 4 2009/07/24 17:
1bdc4 35 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 58:53 danielk197
1bdc5 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 7 Exp $.*/../*.*
1bdc6 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1bdc7 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1bdc8 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 is incremented e
1bdc9 76 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 73 very time a curs
1bdca 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74 or.** moves, eit
1bdcb 68 65 72 20 62 79 20 74 68 65 20 4f 50 5f 53 65 her by the OP_Se
1bdcc 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f ekXX, OP_Next, o
1bdcd 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 65 r OP_Prev opcode
1bdce 73 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a 20 s. The test.**
1bdcf 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 procedures use t
1bdd0 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 his information
1bdd1 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
1bdd2 74 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a t indices are.**
1bdd3 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 working correct
1bdd4 6c 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 62 ly. This variab
1bdd5 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 le has no functi
1bdd6 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f on other than to
1bdd7 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 .** help verify
1bdd8 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 the correct oper
1bdd9 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 ation of the lib
1bdda 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 rary..*/.#ifdef
1bddb 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
1bddc 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1bddd 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 e3_search_count
1bdde 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
1bddf 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f ** When this glo
1bde0 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 bal variable is
1bde1 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74 positive, it get
1bde2 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e s decremented on
1bde3 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63 ce before.** eac
1bde4 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e h instruction in
1bde5 20 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e the VDBE. When
1bde6 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74 reaches zero, t
1bde7 68 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70 he u1.isInterrup
1bde8 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 ted.** field of
1bde9 74 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75 the sqlite3 stru
1bdea 63 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20 cture is set in
1bdeb 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74 order to simulat
1bdec 65 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e e and interrupt.
1bded 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69 .**.** This faci
1bdee 6c 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72 lity is used for
1bdef 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 testing purpose
1bdf0 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73 s only. It does
1bdf1 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a not function.**
1bdf2 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 in an ordinary
1bdf3 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 build..*/.#ifdef
1bdf4 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
1bdf5 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bdf6 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f te3_interrupt_co
1bdf7 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
1bdf8 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 ./*.** The next
1bdf9 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1bdfa 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 is incremented e
1bdfb 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f ach type the OP_
1bdfc 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 Sort opcode.** i
1bdfd 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65 s executed. The
1bdfe 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 test procedures
1bdff 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d use this inform
1be00 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 ation to make su
1be01 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69 re that.** sorti
1be02 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 20 ng is occurring
1be03 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67 or not occurring
1be04 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 at appropriate
1be05 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 61 times. This va
1be06 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f riable.** has no
1be07 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 function other
1be08 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 than to help ver
1be09 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 ify the correct
1be0a 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 operation of the
1be0b 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a .** library..*/.
1be0c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1be0d 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
1be0e 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 t sqlite3_sort_c
1be0f 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
1be10 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ../*.** The next
1be11 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
1be12 20 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 7a records the siz
1be13 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 e of the largest
1be14 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20 MEM_Blob.** or
1be15 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 73 MEM_Str that has
1be16 20 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 20 been used by a
1be17 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 68 VDBE opcode. Th
1be18 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 e test procedure
1be19 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 6e s.** use this in
1be1a 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b formation to mak
1be1b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 e sure that the
1be1c 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69 zero-blob functi
1be1d 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f onality.** is wo
1be1e 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e rking correctly.
1be1f 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 This variable
1be20 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e has no function
1be21 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a other than to.*
1be22 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 * help verify th
1be23 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 e correct operat
1be24 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 ion of the libra
1be25 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ry..*/.#ifdef SQ
1be26 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
1be27 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1be28 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 _max_blobsize =
1be29 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 0;.static void u
1be2a 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 pdateMaxBlobsize
1be2b 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 (Mem *p){. if(
1be2c 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d (p->flags & (MEM
1be2d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21 _Str|MEM_Blob))!
1be2e 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74 =0 && p->n>sqlit
1be2f 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 e3_max_blobsize
1be30 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
1be31 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d ax_blobsize = p-
1be32 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 >n;. }.}.#endif
1be33 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 72 ../*.** Test a r
1be34 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20 69 egister to see i
1be35 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68 65 f it exceeds the
1be36 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d current maximum
1be37 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 49 blob size..** I
1be38 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f 72 f it does, recor
1be39 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d 75 d the new maximu
1be3a 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f 0a m blob size..*/.
1be3b 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
1be3c 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65 66 TE_TEST) && !def
1be3d 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1be3e 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a 23 _BUILTIN_TEST).#
1be3f 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d define UPDATE_M
1be40 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 20 AX_BLOBSIZE(P)
1be41 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a updateMaxBlobsiz
1be42 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 e(P).#else.# def
1be43 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 ine UPDATE_MAX_B
1be44 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 69 LOBSIZE(P).#endi
1be45 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 f../*.** Convert
1be46 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69 73 the given regis
1be47 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e ter into a strin
1be48 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f 6e g if it isn't on
1be49 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 65 e.** already. Re
1be4a 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 turn non-zero if
1be4b 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
1be4c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 74 s..*/.#define St
1be4d 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 20 ringify(P, enc)
1be4e 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66 6c \. if(((P)->fl
1be4f 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d ags&(MEM_Str|MEM
1be50 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 71 _Blob))==0 && sq
1be51 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 lite3VdbeMemStri
1be52 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c 0a ngify(P,enc)) \.
1be53 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f 6d { goto no_m
1be54 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 em; }../*.** An
1be55 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 ephemeral string
1be56 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69 65 value (signifie
1be57 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 68 d by the MEM_Eph
1be58 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 6e em flag) contain
1be59 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 s.** a pointer t
1be5a 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 o a dynamically
1be5b 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67 allocated string
1be5c 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 65 where some othe
1be5d 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 72 r entity.** is r
1be5e 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 esponsible for d
1be5f 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 74 eallocating that
1be60 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75 73 string. Becaus
1be61 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a e the register.*
1be62 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 * does not contr
1be63 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 69 ol the string, i
1be64 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 74 t might be delet
1be65 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 72 ed without the r
1be66 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 69 egister.** knowi
1be67 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ng it..**.** Thi
1be68 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 72 s routine conver
1be69 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 ts an ephemeral
1be6a 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 79 string into a dy
1be6b 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
1be6c 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 68 ted.** string th
1be6d 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72 20 at the register
1be6e 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 2e itself controls.
1be6f 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
1be70 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 73 , it.** converts
1be71 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 74 an MEM_Ephem st
1be72 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 4d ring into an MEM
1be73 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a _Dyn string..*/.
1be74 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d 65 #define Deepheme
1be75 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 69 ralize(P) \. i
1be76 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d f( ((P)->flags&M
1be77 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a 20 EM_Ephem)!=0 \.
1be78 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
1be79 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 VdbeMemMakeWrite
1be7a 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f 20 able(P) ){ goto
1be7b 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 no_mem;}../*.**
1be7c 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 Call sqlite3Vdbe
1be7d 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 20 MemExpandBlob()
1be7e 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 on the supplied
1be7f 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d 2a value (type Mem*
1be80 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 72 ).** P if requir
1be81 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ed..*/.#define E
1be82 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 28 xpandBlob(P) (((
1be83 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 P)->flags&MEM_Ze
1be84 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 4d ro)?sqlite3VdbeM
1be85 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 3a emExpandBlob(P):
1be86 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 0)../*.** Argume
1be87 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 61 nt pMem points a
1be88 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61 t a register tha
1be89 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 t will be passed
1be8a 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 65 to a.** user-de
1be8b 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 6f fined function o
1be8c 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 r returned to th
1be8d 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72 65 e user as the re
1be8e 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79 2e sult of a query.
1be8f 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 .** The second a
1be90 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f 65 6e 63 rgument, 'db_enc
1be91 27 20 69 73 20 74 68 65 20 74 65 78 74 20 65 6e ' is the text en
1be92 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 74 coding used by t
1be93 68 65 20 76 64 62 65 20 66 6f 72 0a 2a 2a 20 72 he vdbe for.** r
1be94 65 67 69 73 74 65 72 20 76 61 72 69 61 62 6c 65 egister variable
1be95 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 s. This routine
1be96 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d 3e sets the pMem->
1be97 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d 3e 74 79 enc and pMem->ty
1be98 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 pe.** variables
1be99 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 used by the sqli
1be9a 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 72 6f te3_value_*() ro
1be9b 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 utines..*/.#defi
1be9c 6e 65 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f ne storeTypeInfo
1be9d 28 41 2c 42 29 20 5f 73 74 6f 72 65 54 79 70 65 (A,B) _storeType
1be9e 49 6e 66 6f 28 41 29 0a 73 74 61 74 69 63 20 76 Info(A).static v
1be9f 6f 69 64 20 5f 73 74 6f 72 65 54 79 70 65 49 6e oid _storeTypeIn
1bea0 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 fo(Mem *pMem){.
1bea1 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d 65 int flags = pMe
1bea2 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 m->flags;. if(
1bea3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1bea4 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 ){. pMem->ty
1bea5 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c pe = SQLITE_NULL
1bea6 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 ;. }. else if(
1bea7 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 flags & MEM_Int
1bea8 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 ){. pMem->ty
1bea9 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 pe = SQLITE_INTE
1beaa 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 GER;. }. else
1beab 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f if( flags & MEM_
1beac 52 65 61 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d Real ){. pMem
1bead 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1beae 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20 65 6c 73 FLOAT;. }. els
1beaf 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 e if( flags & ME
1beb0 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 70 4d 65 M_Str ){. pMe
1beb1 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1beb2 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a _TEXT;. }else{.
1beb3 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1beb4 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 SQLITE_BLOB;.
1beb5 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 70 65 }.}../*.** Prope
1beb6 72 74 69 65 73 20 6f 66 20 6f 70 63 6f 64 65 73 rties of opcodes
1beb7 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f 49 4e 49 . The OPFLG_INI
1beb8 54 49 41 4c 49 5a 45 52 20 6d 61 63 72 6f 20 69 TIALIZER macro i
1beb9 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 s.** created by
1beba 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 64 75 mkopcodeh.awk du
1bebb 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e ring compilation
1bebc 2e 20 20 44 61 74 61 20 69 73 20 6f 62 74 61 69 . Data is obtai
1bebd 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ned.** from the
1bebe 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 comments followi
1bebf 6e 67 20 74 68 65 20 22 63 61 73 65 20 4f 50 5f ng the "case OP_
1bec0 78 78 78 78 3a 22 20 73 74 61 74 65 6d 65 6e 74 xxxx:" statement
1bec1 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20 66 69 6c s in.** this fil
1bec2 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 63 e. .*/.static c
1bec3 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1bec4 61 72 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 ar opcodePropert
1bec5 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49 4e 49 54 y[] = OPFLG_INIT
1bec6 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 IALIZER;../*.**
1bec7 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 Return true if a
1bec8 6e 20 6f 70 63 6f 64 65 20 68 61 73 20 61 6e 79 n opcode has any
1bec9 20 6f 66 20 74 68 65 20 4f 50 46 4c 47 5f 78 78 of the OPFLG_xx
1beca 78 20 70 72 6f 70 65 72 74 69 65 73 0a 2a 2a 20 x properties.**
1becb 73 70 65 63 69 66 69 65 64 20 62 79 20 6d 61 73 specified by mas
1becc 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 k..*/.SQLITE_PRI
1becd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1bece 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f VdbeOpcodeHasPro
1becf 70 65 72 74 79 28 69 6e 74 20 6f 70 63 6f 64 65 perty(int opcode
1bed0 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 61 , int mask){. a
1bed1 73 73 65 72 74 28 20 6f 70 63 6f 64 65 3e 30 20 ssert( opcode>0
1bed2 26 26 20 6f 70 63 6f 64 65 3c 28 69 6e 74 29 73 && opcode<(int)s
1bed3 69 7a 65 6f 66 28 6f 70 63 6f 64 65 50 72 6f 70 izeof(opcodeProp
1bed4 65 72 74 79 29 20 29 3b 0a 20 20 72 65 74 75 72 erty) );. retur
1bed5 6e 20 28 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 n (opcodePropert
1bed6 79 5b 6f 70 63 6f 64 65 5d 26 6d 61 73 6b 29 21 y[opcode]&mask)!
1bed7 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c =0;.}../*.** All
1bed8 6f 63 61 74 65 20 56 64 62 65 43 75 72 73 6f 72 ocate VdbeCursor
1bed9 20 6e 75 6d 62 65 72 20 69 43 75 72 2e 20 20 52 number iCur. R
1beda 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
1bedb 74 6f 20 69 74 2e 20 20 52 65 74 75 72 6e 20 4e to it. Return N
1bedc 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 72 75 6e ULL.** if we run
1bedd 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a out of memory..
1bede 2a 2f 0a 73 74 61 74 69 63 20 56 64 62 65 43 75 */.static VdbeCu
1bedf 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 74 65 43 75 rsor *allocateCu
1bee0 72 73 6f 72 28 0a 20 20 56 64 62 65 20 2a 70 2c rsor(. Vdbe *p,
1bee1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1bee2 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 The virtual mac
1bee3 68 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 hine */. int iC
1bee4 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ur,
1bee5 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 /* Index of the
1bee6 6e 65 77 20 56 64 62 65 43 75 72 73 6f 72 20 2a new VdbeCursor *
1bee7 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 /. int nField,
1bee8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1bee9 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e ber of fields in
1beea 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e the table or in
1beeb 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 dex */. int iDb
1beec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1beed 2a 20 57 68 65 6e 20 64 61 74 61 62 61 73 65 20 * When database
1beee 74 68 65 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e the cursor belon
1beef 67 73 20 74 6f 2c 20 6f 72 20 2d 31 20 2a 2f 0a gs to, or -1 */.
1bef0 20 20 69 6e 74 20 69 73 42 74 72 65 65 43 75 72 int isBtreeCur
1bef1 73 6f 72 20 20 20 20 20 2f 2a 20 54 72 75 65 20 sor /* True
1bef2 66 6f 72 20 42 2d 54 72 65 65 2e 20 20 46 61 6c for B-Tree. Fal
1bef3 73 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 se for pseudo-ta
1bef4 62 6c 65 20 6f 72 20 76 74 61 62 20 2a 2f 0a 29 ble or vtab */.)
1bef5 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 {. /* Find the
1bef6 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 memory cell that
1bef7 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f will be used to
1bef8 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f 62 20 store the blob
1bef9 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 72 of memory. ** r
1befa 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 equired for this
1befb 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 75 VdbeCursor stru
1befc 63 74 75 72 65 2e 20 49 74 20 69 73 20 63 6f 6e cture. It is con
1befd 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65 20 61 venient to use a
1befe 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65 6d 6f . ** vdbe memo
1beff 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e 61 67 ry cell to manag
1bf00 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c e the memory all
1bf01 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 ocation required
1bf02 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64 62 65 for a. ** Vdbe
1bf03 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 Cursor structure
1bf04 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 for the followi
1bf05 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20 2a 2a ng reasons:. **
1bf06 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 74 69 . ** * Someti
1bf07 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 mes cursor numbe
1bf08 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 rs are used for
1bf09 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 a couple of diff
1bf0a 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20 20 70 erent. ** p
1bf0b 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 64 62 urposes in a vdb
1bf0c 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 20 64 e program. The d
1bf0d 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 6d 69 ifferent uses mi
1bf0e 67 68 74 20 72 65 71 75 69 72 65 0a 20 20 2a 2a ght require. **
1bf0f 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 20 73 different s
1bf10 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 ized allocations
1bf11 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 70 . Memory cells p
1bf12 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c 65 0a rovide growable.
1bf13 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63 61 74 ** allocat
1bf14 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ions.. **. **
1bf15 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67 20 45 * When using E
1bf16 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e NABLE_MEMORY_MAN
1bf17 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72 79 20 AGEMENT, memory
1bf18 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63 61 6e cell buffers can
1bf19 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66 72 65 . ** be fre
1bf1a 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20 74 68 ed lazily via th
1bf1b 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 e sqlite3_releas
1bf1c 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49 2e 20 e_memory() API.
1bf1d 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20 6d 69 This. ** mi
1bf1e 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75 6d 62 nimizes the numb
1bf1f 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63 61 6c er of malloc cal
1bf20 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 73 ls made by the s
1bf21 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a ystem.. **. **
1bf22 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f Memory cells fo
1bf23 72 20 63 75 72 73 6f 72 73 20 61 72 65 20 61 6c r cursors are al
1bf24 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 20 74 located at the t
1bf25 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72 65 73 op of the addres
1bf26 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20 4d 65 s. ** space. Me
1bf27 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d mory cell (p->nM
1bf28 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 em) corresponds
1bf29 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53 70 61 to cursor 0. Spa
1bf2a 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75 72 73 ce for. ** curs
1bf2b 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65 64 20 or 1 is managed
1bf2c 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 by memory cell (
1bf2d 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74 63 2e p->nMem-1), etc.
1bf2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 . */. Mem *pMe
1bf2f 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e m = &p->aMem[p->
1bf30 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20 20 69 nMem-iCur];.. i
1bf31 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64 62 65 nt nByte;. Vdbe
1bf32 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20 30 3b Cursor *pCx = 0;
1bf33 0a 20 20 6e 42 79 74 65 20 3d 20 0a 20 20 20 20 . nByte = .
1bf34 20 20 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 sizeof(VdbeCur
1bf35 73 6f 72 29 20 2b 20 0a 20 20 20 20 20 20 28 69 sor) + . (i
1bf36 73 42 74 72 65 65 43 75 72 73 6f 72 3f 73 71 6c sBtreeCursor?sql
1bf37 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 ite3BtreeCursorS
1bf38 69 7a 65 28 29 3a 30 29 20 2b 20 0a 20 20 20 20 ize():0) + .
1bf39 20 20 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 2*nField*sizeo
1bf3a 66 28 75 33 32 29 3b 0a 0a 20 20 61 73 73 65 72 f(u32);.. asser
1bf3b 74 28 20 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73 t( iCur<p->nCurs
1bf3c 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 or );. if( p->a
1bf3d 70 43 73 72 5b 69 43 75 72 5d 20 29 7b 0a 20 20 pCsr[iCur] ){.
1bf3e 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 sqlite3VdbeFre
1bf3f 65 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 eCursor(p, p->ap
1bf40 43 73 72 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 Csr[iCur]);.
1bf41 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d p->apCsr[iCur] =
1bf42 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 0;. }. if( SQ
1bf43 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 LITE_OK==sqlite3
1bf44 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d VdbeMemGrow(pMem
1bf45 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 , nByte, 0) ){.
1bf46 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 p->apCsr[iCur
1bf47 5d 20 3d 20 70 43 78 20 3d 20 28 56 64 62 65 43 ] = pCx = (VdbeC
1bf48 75 72 73 6f 72 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a ursor*)pMem->z;.
1bf49 20 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d memset(pMem-
1bf4a 3e 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 >z, 0, nByte);.
1bf4b 20 20 20 70 43 78 2d 3e 69 44 62 20 3d 20 69 44 pCx->iDb = iD
1bf4c 62 3b 0a 20 20 20 20 70 43 78 2d 3e 6e 46 69 65 b;. pCx->nFie
1bf4d 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a 20 20 20 ld = nField;.
1bf4e 20 69 66 28 20 6e 46 69 65 6c 64 20 29 7b 0a 20 if( nField ){.
1bf4f 20 20 20 20 20 70 43 78 2d 3e 61 54 79 70 65 20 pCx->aType
1bf50 3d 20 28 75 33 32 20 2a 29 26 70 4d 65 6d 2d 3e = (u32 *)&pMem->
1bf51 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 z[sizeof(VdbeCur
1bf52 73 6f 72 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 sor)];. }.
1bf53 20 69 66 28 20 69 73 42 74 72 65 65 43 75 72 73 if( isBtreeCurs
1bf54 6f 72 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d or ){. pCx-
1bf55 3e 70 43 75 72 73 6f 72 20 3d 20 28 42 74 43 75 >pCursor = (BtCu
1bf56 72 73 6f 72 2a 29 0a 20 20 20 20 20 20 20 20 20 rsor*).
1bf57 20 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 &pMem->z[sizeof
1bf58 28 56 64 62 65 43 75 72 73 6f 72 29 2b 32 2a 6e (VdbeCursor)+2*n
1bf59 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 Field*sizeof(u32
1bf5a 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 )];. }. }.
1bf5b 72 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a 0a 2f return pCx;.}../
1bf5c 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 *.** Try to conv
1bf5d 65 72 74 20 61 20 76 61 6c 75 65 20 69 6e 74 6f ert a value into
1bf5e 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 a numeric repre
1bf5f 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 77 65 20 sentation if we
1bf60 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77 69 74 can.** do so wit
1bf61 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 hout loss of inf
1bf62 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20 6f 74 ormation. In ot
1bf63 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 her words, if th
1bf64 65 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b e string.** look
1bf65 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c s like a number,
1bf66 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f convert it into
1bf67 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 a number. If i
1bf68 74 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6c 6f t does not.** lo
1bf69 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 ok like a number
1bf6a 2c 20 6c 65 61 76 65 20 69 74 20 61 6c 6f 6e 65 , leave it alone
1bf6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1bf6c 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 applyNumericAff
1bf6d 69 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65 63 29 inity(Mem *pRec)
1bf6e 7b 0a 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 {. if( (pRec->f
1bf6f 6c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c lags & (MEM_Real
1bf70 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 29 7b |MEM_Int))==0 ){
1bf71 0a 20 20 20 20 69 6e 74 20 72 65 61 6c 6e 75 6d . int realnum
1bf72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1bf73 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 eMemNulTerminate
1bf74 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28 20 (pRec);. if(
1bf75 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d (pRec->flags&MEM
1bf76 5f 53 74 72 29 0a 20 20 20 20 20 20 20 20 20 26 _Str). &
1bf77 26 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 & sqlite3IsNumbe
1bf78 72 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65 61 6c r(pRec->z, &real
1bf79 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63 29 20 num, pRec->enc)
1bf7a 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c ){. i64 val
1bf7b 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ue;. sqlite
1bf7c 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
1bf7d 69 6e 67 28 70 52 65 63 2c 20 53 51 4c 49 54 45 ing(pRec, SQLITE
1bf7e 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20 69 66 _UTF8);. if
1bf7f 28 20 21 72 65 61 6c 6e 75 6d 20 26 26 20 73 71 ( !realnum && sq
1bf80 6c 69 74 65 33 41 74 6f 69 36 34 28 70 52 65 63 lite3Atoi64(pRec
1bf81 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29 7b 0a ->z, &value) ){.
1bf82 20 20 20 20 20 20 20 20 70 52 65 63 2d 3e 75 2e pRec->u.
1bf83 69 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 i = value;.
1bf84 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1bf85 67 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e 74 29 g(pRec, MEM_Int)
1bf86 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1bf87 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1bf88 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 52 65 beMemRealify(pRe
1bf89 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c);. }.
1bf8a 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 }. }.}../*.** P
1bf8b 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64 65 74 rocessing is det
1bf8c 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20 61 66 ermine by the af
1bf8d 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74 65 72 finity parameter
1bf8e 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 :.**.** SQLITE_A
1bf8f 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a 20 53 FF_INTEGER:.** S
1bf90 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3a 0a QLITE_AFF_REAL:.
1bf91 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 ** SQLITE_AFF_NU
1bf92 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 72 79 MERIC:.** Try
1bf93 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 65 63 to convert pRec
1bf94 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 72 to an integer r
1bf95 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 72 epresentation or
1bf96 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 74 69 a .** floati
1bf97 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 73 65 ng-point represe
1bf98 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 69 6e ntation if an in
1bf99 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 teger representa
1bf9a 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 6e 6f tion.** is no
1bf9b 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e 6f 74 t possible. Not
1bf9c 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 67 e that the integ
1bf9d 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f er representatio
1bf9e 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 61 79 n is.** alway
1bf9f 73 20 70 72 65 66 65 72 72 65 64 2c 20 65 76 65 s preferred, eve
1bfa0 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e 69 74 n if the affinit
1bfa1 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63 61 75 y is REAL, becau
1bfa2 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e 74 65 se.** an inte
1bfa3 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ger representati
1bfa4 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 63 65 on is more space
1bfa5 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 64 69 efficient on di
1bfa6 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 sk..**.** SQLITE
1bfa7 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 20 20 _AFF_TEXT:.**
1bfa8 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f Convert pRec to
1bfa9 20 61 20 74 65 78 74 20 72 65 70 72 65 73 65 6e a text represen
1bfaa 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 tation..**.** SQ
1bfab 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 2a LITE_AFF_NONE:.*
1bfac 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 52 65 * No-op. pRe
1bfad 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a c is unchanged..
1bfae 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
1bfaf 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20 pplyAffinity(.
1bfb0 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20 20 20 Mem *pRec,
1bfb1 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 /* The value
1bfb2 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 to apply affini
1bfb3 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 ty to */. char
1bfb4 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 2f affinity, /
1bfb5 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 74 * The affinity t
1bfb6 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a o be applied */.
1bfb7 20 20 75 38 20 65 6e 63 20 20 20 20 20 20 20 20 u8 enc
1bfb8 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 69 /* Use thi
1bfb9 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 s text encoding
1bfba 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66 66 69 */.){. if( affi
1bfbb 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 nity==SQLITE_AFF
1bfbc 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20 _TEXT ){. /*
1bfbd 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74 68 65 Only attempt the
1bfbe 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 54 conversion to T
1bfbf 45 58 54 20 69 66 20 74 68 65 72 65 20 69 73 20 EXT if there is
1bfc0 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 72 65 an integer or re
1bfc1 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 al. ** repres
1bfc2 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62 20 61 entation (blob a
1bfc3 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74 20 67 nd NULL do not g
1bfc4 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20 62 75 et converted) bu
1bfc5 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20 20 20 t no string.
1bfc6 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ** representatio
1bfc7 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 n.. */. if
1bfc8 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c 61 67 ( 0==(pRec->flag
1bfc9 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20 28 70 s&MEM_Str) && (p
1bfca 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f Rec->flags&(MEM_
1bfcb 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 20 29 Real|MEM_Int)) )
1bfcc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1bfcd 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 dbeMemStringify(
1bfce 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20 20 20 pRec, enc);.
1bfcf 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c 61 67 }. pRec->flag
1bfd0 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c s &= ~(MEM_Real|
1bfd1 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 MEM_Int);. }els
1bfd2 65 20 69 66 28 20 61 66 66 69 6e 69 74 79 21 3d e if( affinity!=
1bfd3 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 SQLITE_AFF_NONE
1bfd4 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 ){. assert( a
1bfd5 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
1bfd6 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61 AFF_INTEGER || a
1bfd7 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
1bfd8 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20 20 AFF_REAL.
1bfd9 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e 69 74 || affinit
1bfda 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 y==SQLITE_AFF_NU
1bfdb 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61 70 70 MERIC );. app
1bfdc 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 lyNumericAffinit
1bfdd 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28 y(pRec);. if(
1bfde 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d pRec->flags & M
1bfdf 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 EM_Real ){.
1bfe0 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 sqlite3VdbeInte
1bfe1 67 65 72 41 66 66 69 6e 69 74 79 28 70 52 65 63 gerAffinity(pRec
1bfe2 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
1bfe3 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e /*.** Try to con
1bfe4 76 65 72 74 20 74 68 65 20 74 79 70 65 20 6f 66 vert the type of
1bfe5 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 a function argu
1bfe6 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75 6c 74 ment or a result
1bfe7 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 column.** into
1bfe8 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73 a numeric repres
1bfe9 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65 20 65 entation. Use e
1bfea 69 74 68 65 72 20 49 4e 54 45 47 45 52 20 6f 72 ither INTEGER or
1bfeb 20 52 45 41 4c 20 77 68 69 63 68 65 76 65 72 0a REAL whichever.
1bfec 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 ** is appropriat
1bfed 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64 6f 20 e. But only do
1bfee 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 the conversion i
1bfef 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 f it is possible
1bff0 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f 73 73 without.** loss
1bff1 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 of information
1bff2 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 72 and return the r
1bff3 65 76 69 73 65 64 20 74 79 70 65 20 6f 66 20 74 evised type of t
1bff4 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a he argument..**.
1bff5 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 45 58 ** This is an EX
1bff6 50 45 52 49 4d 45 4e 54 41 4c 20 61 70 69 20 61 PERIMENTAL api a
1bff7 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f nd is subject to
1bff8 20 63 68 61 6e 67 65 20 6f 72 20 72 65 6d 6f 76 change or remov
1bff9 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 al..*/.SQLITE_AP
1bffa 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
1bffb 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 lue_numeric_type
1bffc 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
1bffd 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d pVal){. Mem *pM
1bffe 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b em = (Mem*)pVal;
1bfff 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 . applyNumericA
1c000 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b 0a 20 ffinity(pMem);.
1c001 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 storeTypeInfo(p
1c002 4d 65 6d 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 Mem, 0);. retur
1c003 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 3b 0a 7d 0a n pMem->type;.}.
1c004 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72 74 65 64 20 ./*.** Exported
1c005 76 65 72 73 69 6f 6e 20 6f 66 20 61 70 70 6c 79 version of apply
1c006 41 66 66 69 6e 69 74 79 28 29 2e 20 54 68 69 73 Affinity(). This
1c007 20 6f 6e 65 20 77 6f 72 6b 73 20 6f 6e 20 73 71 one works on sq
1c008 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 0a 2a lite3_value*, .*
1c009 2a 20 6e 6f 74 20 74 68 65 20 69 6e 74 65 72 6e * not the intern
1c00a 61 6c 20 4d 65 6d 2a 20 74 79 70 65 2e 0a 2a 2f al Mem* type..*/
1c00b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c00c 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 void sqlite3Valu
1c00d 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a eApplyAffinity(.
1c00e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
1c00f 2a 70 56 61 6c 2c 20 0a 20 20 75 38 20 61 66 66 *pVal, . u8 aff
1c010 69 6e 69 74 79 2c 20 0a 20 20 75 38 20 65 6e 63 inity, . u8 enc
1c011 0a 29 7b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e .){. applyAffin
1c012 69 74 79 28 28 4d 65 6d 20 2a 29 70 56 61 6c 2c ity((Mem *)pVal,
1c013 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b affinity, enc);
1c014 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .}..#ifdef SQLIT
1c015 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 57 72 E_DEBUG./*.** Wr
1c016 69 74 65 20 61 20 6e 69 63 65 20 73 74 72 69 6e ite a nice strin
1c017 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e g representation
1c018 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 of the contents
1c019 20 6f 66 20 63 65 6c 6c 20 70 4d 65 6d 0a 2a 2a of cell pMem.**
1c01a 20 69 6e 74 6f 20 62 75 66 66 65 72 20 7a 42 75 into buffer zBu
1c01b 66 2c 20 6c 65 6e 67 74 68 20 6e 42 75 66 2e 0a f, length nBuf..
1c01c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1c01d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1c01e 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
1c01f 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 (Mem *pMem, char
1c020 20 2a 7a 42 75 66 29 7b 0a 20 20 63 68 61 72 20 *zBuf){. char
1c021 2a 7a 43 73 72 20 3d 20 7a 42 75 66 3b 0a 20 20 *zCsr = zBuf;.
1c022 69 6e 74 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c int f = pMem->fl
1c023 61 67 73 3b 0a 0a 20 20 73 74 61 74 69 63 20 63 ags;.. static c
1c024 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 onst char *const
1c025 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 22 encnames[] = {"
1c026 28 58 29 22 2c 20 22 28 38 29 22 2c 20 22 28 31 (X)", "(8)", "(1
1c027 36 4c 45 29 22 2c 20 22 28 31 36 42 45 29 22 7d 6LE)", "(16BE)"}
1c028 3b 0a 0a 20 20 69 66 28 20 66 26 4d 45 4d 5f 42 ;.. if( f&MEM_B
1c029 6c 6f 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 lob ){. int i
1c02a 3b 0a 20 20 20 20 63 68 61 72 20 63 3b 0a 20 20 ;. char c;.
1c02b 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 if( f & MEM_Dy
1c02c 6e 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 n ){. c = '
1c02d 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 z';. assert
1c02e 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 ( (f & (MEM_Stat
1c02f 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d ic|MEM_Ephem))==
1c030 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 0 );. }else i
1c031 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 f( f & MEM_Stati
1c032 63 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 c ){. c = '
1c033 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 t';. assert
1c034 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c ( (f & (MEM_Dyn|
1c035 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 MEM_Ephem))==0 )
1c036 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1c037 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b f & MEM_Ephem ){
1c038 0a 20 20 20 20 20 20 63 20 3d 20 27 65 27 3b 0a . c = 'e';.
1c039 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 assert( (f
1c03a 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d & (MEM_Static|M
1c03b 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 EM_Dyn))==0 );.
1c03c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1c03d 63 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a 0a c = 's';. }..
1c03e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1c03f 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 intf(100, zCsr,
1c040 22 25 63 22 2c 20 63 29 3b 0a 20 20 20 20 7a 43 "%c", c);. zC
1c041 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 sr += sqlite3Str
1c042 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 len30(zCsr);.
1c043 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1c044 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 64 f(100, zCsr, "%d
1c045 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 [", pMem->n);.
1c046 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 zCsr += sqlite
1c047 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 3Strlen30(zCsr);
1c048 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1c049 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 16 && i<pMem->n;
1c04a 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c i++){. sql
1c04b 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 ite3_snprintf(10
1c04c 30 2c 20 7a 43 73 72 2c 20 22 25 30 32 58 22 2c 0, zCsr, "%02X",
1c04d 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e 7a 5b 69 ((int)pMem->z[i
1c04e 5d 20 26 20 30 78 46 46 29 29 3b 0a 20 20 20 20 ] & 0xFF));.
1c04f 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 zCsr += sqlite
1c050 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 3Strlen30(zCsr);
1c051 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 . }. for(i
1c052 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 4d =0; i<16 && i<pM
1c053 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 em->n; i++){.
1c054 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d 65 6d char z = pMem
1c055 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 ->z[i];. if
1c056 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32 36 20 ( z<32 || z>126
1c057 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e 27 3b ) *zCsr++ = '.';
1c058 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a 43 73 . else *zCs
1c059 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 0a r++ = z;. }..
1c05a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1c05b 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 intf(100, zCsr,
1c05c 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65 73 5b "]%s", encnames[
1c05d 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 pMem->enc]);.
1c05e 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 zCsr += sqlite3
1c05f 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a Strlen30(zCsr);.
1c060 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f if( f & MEM_
1c061 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 73 71 Zero ){. sq
1c062 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 lite3_snprintf(1
1c063 30 30 2c 20 7a 43 73 72 2c 22 2b 25 64 7a 22 2c 00, zCsr,"+%dz",
1c064 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a pMem->u.nZero);.
1c065 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 zCsr += sq
1c066 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 lite3Strlen30(zC
1c067 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a sr);. }. *
1c068 7a 43 73 72 20 3d 20 27 5c 30 27 3b 0a 20 20 7d zCsr = '\0';. }
1c069 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d else if( f & MEM
1c06a 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 _Str ){. int
1c06b 6a 2c 20 6b 3b 0a 20 20 20 20 7a 42 75 66 5b 30 j, k;. zBuf[0
1c06c 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 69 66 28 ] = ' ';. if(
1c06d 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a f & MEM_Dyn ){.
1c06e 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 zBuf[1] =
1c06f 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 'z';. asser
1c070 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 t( (f & (MEM_Sta
1c071 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d tic|MEM_Ephem))=
1c072 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 =0 );. }else
1c073 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 if( f & MEM_Stat
1c074 69 63 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 ic ){. zBuf
1c075 5b 31 5d 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 [1] = 't';.
1c076 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
1c077 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d EM_Dyn|MEM_Ephem
1c078 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c ))==0 );. }el
1c079 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 se if( f & MEM_E
1c07a 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 7a 42 phem ){. zB
1c07b 75 66 5b 31 5d 20 3d 20 27 65 27 3b 0a 20 20 20 uf[1] = 'e';.
1c07c 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1c07d 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
1c07e 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 Dyn))==0 );.
1c07f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 42 75 }else{. zBu
1c080 66 5b 31 5d 20 3d 20 27 73 27 3b 0a 20 20 20 20 f[1] = 's';.
1c081 7d 0a 20 20 20 20 6b 20 3d 20 32 3b 0a 20 20 20 }. k = 2;.
1c082 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1c083 66 28 31 30 30 2c 20 26 7a 42 75 66 5b 6b 5d 2c f(100, &zBuf[k],
1c084 20 22 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b "%d", pMem->n);
1c085 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 . k += sqlite
1c086 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b 3Strlen30(&zBuf[
1c087 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b k]);. zBuf[k+
1c088 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20 20 20 66 6f +] = '[';. fo
1c089 72 28 6a 3d 30 3b 20 6a 3c 31 35 20 26 26 20 6a r(j=0; j<15 && j
1c08a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a <pMem->n; j++){.
1c08b 20 20 20 20 20 20 75 38 20 63 20 3d 20 70 4d 65 u8 c = pMe
1c08c 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 m->z[j];. i
1c08d 66 28 20 63 3e 3d 30 78 32 30 20 26 26 20 63 3c f( c>=0x20 && c<
1c08e 30 78 37 66 20 29 7b 0a 20 20 20 20 20 20 20 20 0x7f ){.
1c08f 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 zBuf[k++] = c;.
1c090 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1c091 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 zBuf[k++] =
1c092 27 2e 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 '.';. }.
1c093 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d }. zBuf[k++]
1c094 20 3d 20 27 5d 27 3b 0a 20 20 20 20 73 71 6c 69 = ']';. sqli
1c095 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 te3_snprintf(100
1c096 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65 6e 63 6e 61 ,&zBuf[k], encna
1c097 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b mes[pMem->enc]);
1c098 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 . k += sqlite
1c099 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b 3Strlen30(&zBuf[
1c09a 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b k]);. zBuf[k+
1c09b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 +] = 0;. }.}.#e
1c09c 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
1c09d 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 ITE_DEBUG./*.**
1c09e 50 72 69 6e 74 20 74 68 65 20 76 61 6c 75 65 20 Print the value
1c09f 6f 66 20 61 20 72 65 67 69 73 74 65 72 20 66 6f of a register fo
1c0a0 72 20 74 72 61 63 69 6e 67 20 70 75 72 70 6f 73 r tracing purpos
1c0a1 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f es:.*/.static vo
1c0a2 69 64 20 6d 65 6d 54 72 61 63 65 50 72 69 6e 74 id memTracePrint
1c0a3 28 46 49 4c 45 20 2a 6f 75 74 2c 20 4d 65 6d 20 (FILE *out, Mem
1c0a4 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 66 6c *p){. if( p->fl
1c0a5 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 ags & MEM_Null )
1c0a6 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 {. fprintf(ou
1c0a7 74 2c 20 22 20 4e 55 4c 4c 22 29 3b 0a 20 20 7d t, " NULL");. }
1c0a8 65 6c 73 65 20 69 66 28 20 28 70 2d 3e 66 6c 61 else if( (p->fla
1c0a9 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 gs & (MEM_Int|ME
1c0aa 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 4d 5f 49 6e M_Str))==(MEM_In
1c0ab 74 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20 t|MEM_Str) ){.
1c0ac 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
1c0ad 20 73 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e si:%lld", p->u.
1c0ae 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 i);. }else if(
1c0af 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 p->flags & MEM_I
1c0b0 6e 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 nt ){. fprint
1c0b1 66 28 6f 75 74 2c 20 22 20 69 3a 25 6c 6c 64 22 f(out, " i:%lld"
1c0b2 2c 20 70 2d 3e 75 2e 69 29 3b 0a 23 69 66 6e 64 , p->u.i);.#ifnd
1c0b3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ef SQLITE_OMIT_F
1c0b4 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 LOATING_POINT.
1c0b5 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 }else if( p->fla
1c0b6 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b gs & MEM_Real ){
1c0b7 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
1c0b8 2c 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72 29 , " r:%g", p->r)
1c0b9 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 ;.#endif. }else
1c0ba 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 if( p->flags &
1c0bb 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 MEM_RowSet ){.
1c0bc 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
1c0bd 20 28 72 6f 77 73 65 74 29 22 29 3b 0a 20 20 7d (rowset)");. }
1c0be 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 7a else{. char z
1c0bf 42 75 66 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71 Buf[200];. sq
1c0c0 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 lite3VdbeMemPret
1c0c1 74 79 50 72 69 6e 74 28 70 2c 20 7a 42 75 66 29 tyPrint(p, zBuf)
1c0c2 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 ;. fprintf(ou
1c0c3 74 2c 20 22 20 22 29 3b 0a 20 20 20 20 66 70 72 t, " ");. fpr
1c0c4 69 6e 74 66 28 6f 75 74 2c 20 22 25 73 22 2c 20 intf(out, "%s",
1c0c5 7a 42 75 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 zBuf);. }.}.sta
1c0c6 74 69 63 20 76 6f 69 64 20 72 65 67 69 73 74 65 tic void registe
1c0c7 72 54 72 61 63 65 28 46 49 4c 45 20 2a 6f 75 74 rTrace(FILE *out
1c0c8 2c 20 69 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20 , int iReg, Mem
1c0c9 2a 70 29 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f *p){. fprintf(o
1c0ca 75 74 2c 20 22 52 45 47 5b 25 64 5d 20 3d 20 22 ut, "REG[%d] = "
1c0cb 2c 20 69 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72 , iReg);. memTr
1c0cc 61 63 65 50 72 69 6e 74 28 6f 75 74 2c 20 70 29 acePrint(out, p)
1c0cd 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c ;. fprintf(out,
1c0ce 20 22 5c 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 "\n");.}.#endif
1c0cf 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1c0d0 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e 65 20 DEBUG.# define
1c0d1 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 52 REGISTER_TRACE(R
1c0d2 2c 4d 29 20 69 66 28 70 2d 3e 74 72 61 63 65 29 ,M) if(p->trace)
1c0d3 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d registerTrace(p-
1c0d4 3e 74 72 61 63 65 2c 52 2c 4d 29 0a 23 65 6c 73 >trace,R,M).#els
1c0d5 65 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47 49 e.# define REGI
1c0d6 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 0a STER_TRACE(R,M).
1c0d7 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65 66 20 #endif...#ifdef
1c0d8 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 0a 2f 2a VDBE_PROFILE../*
1c0d9 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f .** hwtime.h co
1c0da 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 ntains inline as
1c0db 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 sembler code for
1c0dc 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a implementing .*
1c0dd 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e * high-performan
1c0de 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e ce timing routin
1c0df 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a es..*/./********
1c0e0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 ****** Include h
1c0e1 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d wtime.h in the m
1c0e2 69 64 64 6c 65 20 6f 66 20 76 64 62 65 2e 63 20 iddle of vdbe.c
1c0e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c0e4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1c0e5 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1c0e6 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a e hwtime.h *****
1c0e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c0e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c0e9 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1c0ea 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 8 May 27.**.** T
1c0eb 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1c0ec 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1c0ed 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1c0ee 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1c0ef 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1c0f0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1c0f1 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1c0f2 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1c0f3 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1c0f4 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1c0f5 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1c0f6 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1c0f7 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1c0f8 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1c0f9 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1c0fa 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1c0fb 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1c0fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c0fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c0fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c0ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
1c101 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
1c102 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d tains inline asm
1c103 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 code for retrie
1c104 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f ving "high-perfo
1c105 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 rmance".** count
1c106 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 ers for x86 clas
1c107 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 s CPUs..**.** $I
1c108 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e d: hwtime.h,v 1.
1c109 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 3 2008/08/01 14:
1c10a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 33:15 shane Exp
1c10b 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 $.*/.#ifndef _HW
1c10c 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 TIME_H_.#define
1c10d 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a _HWTIME_H_../*.*
1c10e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1c10f 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 routine only wor
1c110 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c ks on pentium-cl
1c111 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 ass (or newer) p
1c112 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 rocessors..** It
1c113 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 uses the RDTSC
1c114 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 opcode to read t
1c115 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 he cycle count v
1c116 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a alue out of the.
1c117 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 ** processor and
1c118 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 returns that va
1c119 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 lue. This can b
1c11a 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d e used for high-
1c11b 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 res.** profiling
1c11c 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 ..*/.#if (define
1c11d 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 d(__GNUC__) || d
1c11e 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
1c11f 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 ) && \. (de
1c120 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 fined(i386) || d
1c121 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 efined(__i386__)
1c122 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 || defined(_M_I
1c123 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 X86)).. #if def
1c124 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a ined(__GNUC__)..
1c125 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
1c126 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
1c127 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
1c128 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e unsigned in
1c129 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f t lo, hi;. _
1c12a 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
1c12b 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 e__ ("rdtsc" : "
1c12c 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 =a" (lo), "=d" (
1c12d 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 hi));. retur
1c12e 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 n (sqlite_uint64
1c12f 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a )hi << 32 | lo;.
1c130 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 }.. #elif def
1c131 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a ined(_MSC_VER)..
1c132 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b __declspec(nak
1c133 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c ed) __inline sql
1c134 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 ite_uint64 __cde
1c135 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 cl sqlite3Hwtime
1c136 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 (void){. __a
1c137 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 sm {. rdt
1c138 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 sc. ret
1c139 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 ; return va
1c13a 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 lue at EDX:EAX.
1c13b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e }. }.. #en
1c13c 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 dif..#elif (defi
1c13d 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 ned(__GNUC__) &&
1c13e 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 defined(__x86_6
1c13f 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 4__)).. __inlin
1c140 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
1c141 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
1c142 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
1c143 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a igned long val;.
1c144 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
1c145 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 volatile__ ("rdt
1c146 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 sc" : "=A" (val)
1c147 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1c148 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 val;. }. .#elif
1c149 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
1c14a 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f __) && defined(_
1c14b 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e _ppc__)).. __in
1c14c 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
1c14d 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
1c14e 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 me(void){.
1c14f 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
1c150 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 ng retval;.
1c151 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a unsigned long j
1c152 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d unk;. __asm
1c153 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 __ __volatile__
1c154 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 ("\n\.
1c155 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 1: mftbu
1c156 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %1\n\.
1c157 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 mftb
1c158 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %L0\n\.
1c159 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 mftbu
1c15a 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %0\n\.
1c15b 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 cmpw
1c15c 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 %0,%1\n\.
1c15d 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 bne
1c15e 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 1b".
1c15f 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 : "=r"
1c160 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 (retval), "=r"
1c161 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 (junk));. r
1c162 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 eturn retval;.
1c163 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 }..#else.. #err
1c164 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e or Need implemen
1c165 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 tation of sqlite
1c166 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 3Hwtime() for yo
1c167 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 ur platform...
1c168 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 /*. ** To compi
1c169 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 le without imple
1c16a 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 menting sqlite3H
1c16b 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 wtime() for your
1c16c 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 platform,. **
1c16d 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 you can remove t
1c16e 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 he above #error
1c16f 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c and use the foll
1c170 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 owing. ** stub
1c171 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 function. You w
1c172 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 ill lose timing
1c173 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 support for many
1c174 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 . ** of the deb
1c175 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 ugging and testi
1c176 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 ng utilities, bu
1c177 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 t it should at.
1c178 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c ** least compil
1c179 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a e and run.. */.
1c17a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
1c17b 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
1c17c 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
1c17d 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c d){ return ((sql
1c17e 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d ite_uint64)0); }
1c17f 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 ..#endif..#endif
1c180 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 /* !defined(_HW
1c181 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a TIME_H_) */../**
1c182 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
1c183 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a of hwtime.h ***
1c184 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c185 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c186 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
1c187 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
1c188 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
1c189 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 left off in vdb
1c18a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
1c18b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 ***********/..#e
1c18c 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1c18d 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 CHECK_FOR_INTERR
1c18e 55 50 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 UPT macro define
1c18f 64 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 d here looks to
1c190 73 65 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 see if the.** sq
1c191 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 lite3_interrupt(
1c192 29 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 ) routine has be
1c193 65 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 en called. If i
1c194 74 20 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e t has been, then
1c195 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f .** processing o
1c196 66 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 f the VDBE progr
1c197 61 6d 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 am is interrupte
1c198 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 d..**.** This ma
1c199 63 72 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65 cro added to eve
1c19a 72 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 ry instruction t
1c19b 68 61 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 hat does a jump
1c19c 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 in order to.** i
1c19d 6d 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e mplement a loop.
1c19e 20 20 54 68 69 73 20 74 65 73 74 20 75 73 65 64 This test used
1c19f 20 74 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20 to be on every
1c1a0 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 single instructi
1c1a1 6f 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 on,.** but that
1c1a2 6d 65 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 meant we more te
1c1a3 73 74 69 6e 67 20 74 68 61 74 20 77 65 20 6e 65 sting that we ne
1c1a4 65 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 eded. By only t
1c1a5 65 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c esting the.** fl
1c1a6 61 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 ag on jump instr
1c1a7 75 63 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 uctions, we get
1c1a8 61 20 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 a (small) speed
1c1a9 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a improvement..*/.
1c1aa 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f #define CHECK_FO
1c1ab 52 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 R_INTERRUPT \.
1c1ac 20 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e if( db->u1.isIn
1c1ad 74 65 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f terrupted ) goto
1c1ae 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e abort_due_to_in
1c1af 74 65 72 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 terrupt;..#ifdef
1c1b0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 SQLITE_DEBUG.st
1c1b1 61 74 69 63 20 69 6e 74 20 66 69 6c 65 45 78 69 atic int fileExi
1c1b2 73 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c sts(sqlite3 *db,
1c1b3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
1c1b4 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d le){. int res =
1c1b5 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 0;. int rc = S
1c1b6 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 QLITE_OK;.#ifdef
1c1b7 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f SQLITE_TEST. /
1c1b8 2a 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72 * If we are curr
1c1b9 65 6e 74 6c 79 20 74 65 73 74 69 6e 67 20 49 4f ently testing IO
1c1ba 20 65 72 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f errors, then do
1c1bb 20 6e 6f 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 not call OsAcce
1c1bc 73 73 28 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 ss() to. ** tes
1c1bd 74 20 66 6f 72 20 74 68 65 20 70 72 65 73 65 6e t for the presen
1c1be 63 65 20 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 ce of zFile. Thi
1c1bf 73 20 69 73 20 62 65 63 61 75 73 65 20 61 6e 79 s is because any
1c1c0 20 49 4f 20 65 72 72 6f 72 20 74 68 61 74 0a 20 IO error that.
1c1c1 20 2a 2a 20 6f 63 63 75 72 73 20 68 65 72 65 20 ** occurs here
1c1c2 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f will not be repo
1c1c3 72 74 65 64 2c 20 63 61 75 73 69 6e 67 20 74 68 rted, causing th
1c1c4 65 20 74 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a e test to fail..
1c1c5 20 20 2a 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e */. extern in
1c1c6 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 t sqlite3_io_err
1c1c7 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 or_pending;. if
1c1c8 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 ( sqlite3_io_err
1c1c9 6f 72 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a or_pending<=0 ).
1c1ca 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 #endif. rc =
1c1cb 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
1c1cc 64 62 2d 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c db->pVfs, zFile,
1c1cd 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
1c1ce 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 XISTS, &res);.
1c1cf 72 65 74 75 72 6e 20 28 72 65 73 20 26 26 20 72 return (res && r
1c1d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d c==SQLITE_OK);.}
1c1d1 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
1c1d2 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 NDEBUG./*.** Th
1c1d3 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f is function is o
1c1d4 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 nly called from
1c1d5 77 69 74 68 69 6e 20 61 6e 20 61 73 73 65 72 74 within an assert
1c1d6 28 29 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 () expression. I
1c1d7 74 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68 61 74 t.** checks that
1c1d8 20 74 68 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 the sqlite3.nTr
1c1d9 61 6e 73 61 63 74 69 6f 6e 20 76 61 72 69 61 62 ansaction variab
1c1da 6c 65 20 69 73 20 63 6f 72 72 65 63 74 6c 79 20 le is correctly
1c1db 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 set to.** the nu
1c1dc 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e mber of non-tran
1c1dd 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e saction savepoin
1c1de 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 ts currently in
1c1df 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c the .** linked l
1c1e0 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 20 ist starting at
1c1e1 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f 69 sqlite3.pSavepoi
1c1e2 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 nt..** .** Usage
1c1e3 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 :.**.** asse
1c1e4 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f 69 rt( checkSavepoi
1c1e5 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a ntCount(db) );.*
1c1e6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 /.static int che
1c1e7 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 ckSavepointCount
1c1e8 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
1c1e9 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 int n = 0;. Sa
1c1ea 76 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f vepoint *p;. fo
1c1eb 72 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 r(p=db->pSavepoi
1c1ec 6e 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 nt; p; p=p->pNex
1c1ed 74 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 t) n++;. assert
1c1ee 28 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 ( n==(db->nSavep
1c1ef 6f 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 oint + db->isTra
1c1f0 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e nsactionSavepoin
1c1f1 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 t) );. return 1
1c1f2 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1c1f3 2a 20 45 78 65 63 75 74 65 20 61 73 20 6d 75 63 * Execute as muc
1c1f4 68 20 6f 66 20 61 20 56 44 42 45 20 70 72 6f 67 h of a VDBE prog
1c1f5 72 61 6d 20 61 73 20 77 65 20 63 61 6e 20 74 68 ram as we can th
1c1f6 65 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a en return..**.**
1c1f7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1c1f8 52 65 61 64 79 28 29 20 6d 75 73 74 20 62 65 20 Ready() must be
1c1f9 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68 called before th
1c1fa 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 is routine in or
1c1fb 64 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 der to.** close
1c1fc 74 68 65 20 70 72 6f 67 72 61 6d 20 77 69 74 68 the program with
1c1fd 20 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 a final OP_Halt
1c1fe 20 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20 74 and to set up t
1c1ff 68 65 20 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 he callbacks.**
1c200 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 6d 65 and the error me
1c201 73 73 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a ssage pointer..*
1c202 2a 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 61 20 *.** Whenever a
1c203 72 6f 77 20 6f 72 20 72 65 73 75 6c 74 20 64 61 row or result da
1c204 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c ta is available,
1c205 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 this routine wi
1c206 6c 6c 20 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 ll either.** inv
1c207 6f 6b 65 20 74 68 65 20 72 65 73 75 6c 74 20 63 oke the result c
1c208 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 allback (if ther
1c209 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 e is one) or ret
1c20a 75 72 6e 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 urn with.** SQLI
1c20b 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 TE_ROW..**.** If
1c20c 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d an attempt is m
1c20d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f ade to open a lo
1c20e 63 6b 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 cked database, t
1c20f 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
1c210 0a 2a 2a 20 77 69 6c 6c 20 65 69 74 68 65 72 20 .** will either
1c211 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 invoke the busy
1c212 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 callback (if the
1c213 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 re is one) or it
1c214 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 will.** return
1c215 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a SQLITE_BUSY..**.
1c216 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f ** If an error o
1c217 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 ccurs, an error
1c218 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 message is writt
1c219 65 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 en to memory obt
1c21a 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 ained.** from sq
1c21b 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 lite3_malloc() a
1c21c 6e 64 20 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 nd p->zErrMsg is
1c21d 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 made to point t
1c21e 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a o that memory..*
1c21f 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 * The error code
1c220 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 2d is stored in p-
1c221 3e 72 63 20 61 6e 64 20 74 68 69 73 20 72 6f 75 >rc and this rou
1c222 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c tine returns SQL
1c223 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a ITE_ERROR..**.**
1c224 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b If the callback
1c225 20 65 76 65 72 20 72 65 74 75 72 6e 73 20 6e 6f ever returns no
1c226 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n-zero, then the
1c227 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a program exits.*
1c228 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 * immediately.
1c229 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f There will be no
1c22a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 error message b
1c22b 75 74 20 74 68 65 20 70 2d 3e 72 63 20 66 69 65 ut the p->rc fie
1c22c 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 ld is.** set to
1c22d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 SQLITE_ABORT and
1c22e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 this routine wi
1c22f 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ll return SQLITE
1c230 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 _ERROR..**.** A
1c231 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1c232 6e 20 65 72 72 6f 72 20 63 61 75 73 65 73 20 70 n error causes p
1c233 2d 3e 72 63 20 74 6f 20 62 65 20 73 65 74 20 74 ->rc to be set t
1c234 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 o SQLITE_NOMEM a
1c235 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 nd this.** routi
1c236 6e 65 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c ne to return SQL
1c237 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a ITE_ERROR..**.**
1c238 20 4f 74 68 65 72 20 66 61 74 61 6c 20 65 72 72 Other fatal err
1c239 6f 72 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ors return SQLIT
1c23a 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 E_ERROR..**.** A
1c23b 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e fter this routin
1c23c 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 e has finished,
1c23d 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c sqlite3VdbeFinal
1c23e 69 7a 65 28 29 20 73 68 6f 75 6c 64 20 62 65 0a ize() should be.
1c23f 2a 2a 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e ** used to clean
1c240 20 75 70 20 74 68 65 20 6d 65 73 73 20 74 68 61 up the mess tha
1c241 74 20 77 61 73 20 6c 65 66 74 20 62 65 68 69 6e t was left behin
1c242 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1c243 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1c244 56 64 62 65 45 78 65 63 28 0a 20 20 56 64 62 65 VdbeExec(. Vdbe
1c245 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 *p
1c246 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 /* The VD
1c247 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 BE */.){. int p
1c248 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
1c249 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f /* The pro
1c24a 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a gram counter */.
1c24b 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 20 20 20 Op *pOp;
1c24c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c24d 43 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f Current operatio
1c24e 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 n */. int rc =
1c24f 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
1c250 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 /* Value to re
1c251 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 turn */. sqlite
1c252 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 3 *db = p->db;
1c253 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1c254 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 base */. u8 enc
1c255 6f 64 69 6e 67 20 3d 20 45 4e 43 28 64 62 29 3b oding = ENC(db);
1c256 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1c257 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f base encoding */
1c258 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 . Mem *pIn1 = 0
1c259 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c25a 20 31 73 74 20 69 6e 70 75 74 20 6f 70 65 72 61 1st input opera
1c25b 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e nd */. Mem *pIn
1c25c 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2 = 0;
1c25d 20 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 /* 2nd input
1c25e 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d operand */. Mem
1c25f 20 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 *pIn3 = 0;
1c260 20 20 20 20 20 20 20 20 2f 2a 20 33 72 64 20 69 /* 3rd i
1c261 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a nput operand */.
1c262 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b Mem *pOut = 0;
1c263 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c264 4f 75 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a Output operand *
1c265 2f 0a 20 20 75 38 20 6f 70 50 72 6f 70 65 72 74 /. u8 opPropert
1c266 79 3b 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 y;. int iCompar
1c267 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 e = 0;
1c268 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 /* Result of las
1c269 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 t OP_Compare ope
1c26a 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 ration */. int
1c26b 2a 61 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 *aPermute = 0;
1c26c 20 20 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 /* Permut
1c26d 61 74 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 ation of columns
1c26e 20 66 6f 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 for OP_Compare
1c26f 2a 2f 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 */.#ifdef VDBE_P
1c270 52 4f 46 49 4c 45 0a 20 20 75 36 34 20 73 74 61 ROFILE. u64 sta
1c271 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rt;
1c272 20 20 20 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b /* CPU clock
1c273 20 63 6f 75 6e 74 20 61 74 20 73 74 61 72 74 20 count at start
1c274 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 of opcode */. i
1c275 6e 74 20 6f 72 69 67 50 63 3b 20 20 20 20 20 20 nt origPc;
1c276 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f /* Pro
1c277 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 gram counter at
1c278 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 start of opcode
1c279 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 */.#endif.#ifnde
1c27a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 f SQLITE_OMIT_PR
1c27b 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a OGRESS_CALLBACK.
1c27c 20 20 69 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f int nProgressO
1c27d 70 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 ps = 0; /*
1c27e 4f 70 63 6f 64 65 73 20 65 78 65 63 75 74 65 64 Opcodes executed
1c27f 20 73 69 6e 63 65 20 70 72 6f 67 72 65 73 73 20 since progress
1c280 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 65 6e callback. */.#en
1c281 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a dif. /*********
1c282 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c283 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c284 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c285 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a ***********. **
1c286 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 Automatically g
1c287 65 6e 65 72 61 74 65 64 20 63 6f 64 65 0a 20 20 enerated code.
1c288 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c **. ** The foll
1c289 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 69 73 20 61 owing union is a
1c28a 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e utomatically gen
1c28b 65 72 61 74 65 64 20 62 79 20 74 68 65 0a 20 20 erated by the.
1c28c 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 72 65 73 73 ** vdbe-compress
1c28d 2e 74 63 6c 20 73 63 72 69 70 74 2e 20 20 54 68 .tcl script. Th
1c28e 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 e purpose of thi
1c28f 73 20 75 6e 69 6f 6e 20 69 73 20 74 6f 0a 20 20 s union is to.
1c290 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 61 6d ** reduce the am
1c291 6f 75 6e 74 20 6f 66 20 73 74 61 63 6b 20 73 70 ount of stack sp
1c292 61 63 65 20 72 65 71 75 69 72 65 64 20 62 79 20 ace required by
1c293 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 this function..
1c294 20 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 ** See comments
1c295 20 69 6e 20 74 68 65 20 76 64 62 65 2d 63 6f 6d in the vdbe-com
1c296 70 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70 74 press.tcl script
1c297 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20 20 for details..
1c298 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 64 62 65 45 */. union vdbeE
1c299 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 xecUnion {. s
1c29a 74 72 75 63 74 20 4f 50 5f 59 69 65 6c 64 5f 73 truct OP_Yield_s
1c29b 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c29c 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a 20 20 int pcDest;.
1c29d 20 20 7d 20 61 61 3b 0a 20 20 20 20 73 74 72 75 } aa;. stru
1c29e 63 74 20 4f 50 5f 56 61 72 69 61 62 6c 65 5f 73 ct OP_Variable_s
1c29f 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c2a0 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 int p1;
1c2a1 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 /* Variable t
1c2a2 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 o copy from */.
1c2a3 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20 int p2;
1c2a4 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
1c2a5 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a r to copy to */.
1c2a6 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 int n;
1c2a7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1c2a8 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 74 20 of values left
1c2a9 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20 to copy */.
1c2aa 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 Mem *pVar;
1c2ab 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e 67 /* Value being
1c2ac 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a transferred */.
1c2ad 20 20 20 20 7d 20 61 62 3b 0a 20 20 20 20 73 74 } ab;. st
1c2ae 72 75 63 74 20 4f 50 5f 4d 6f 76 65 5f 73 74 61 ruct OP_Move_sta
1c2af 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c2b0 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 char *zMalloc;
1c2b1 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72 69 /* Holding vari
1c2b2 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 able for allocat
1c2b3 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 ed memory */.
1c2b4 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 int n;
1c2b5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1c2b6 20 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 20 registers left
1c2b7 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20 to copy */.
1c2b8 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 int p1;
1c2b9 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f /* Register to
1c2ba 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 copy from */.
1c2bb 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 int p2;
1c2bc 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
1c2bd 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 to copy to */.
1c2be 20 20 20 7d 20 61 63 3b 0a 20 20 20 20 73 74 72 } ac;. str
1c2bf 75 63 74 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 uct OP_ResultRow
1c2c0 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c2c1 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 Mem *pMem;.
1c2c2 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 int i;.
1c2c3 7d 20 61 64 3b 0a 20 20 20 20 73 74 72 75 63 74 } ad;. struct
1c2c4 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 74 61 63 6b OP_Concat_stack
1c2c5 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 _vars {. i6
1c2c6 34 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 20 61 4 nByte;. } a
1c2c7 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 e;. struct OP
1c2c8 5f 52 65 6d 61 69 6e 64 65 72 5f 73 74 61 63 6b _Remainder_stack
1c2c9 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e _vars {. in
1c2ca 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 2f 2a t flags; /*
1c2cb 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 Combined MEM_*
1c2cc 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f 74 68 20 flags from both
1c2cd 69 6e 70 75 74 73 20 2a 2f 0a 20 20 20 20 20 20 inputs */.
1c2ce 69 36 34 20 69 41 3b 20 20 20 20 20 20 20 20 20 i64 iA;
1c2cf 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 /* Integer value
1c2d0 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 of left operand
1c2d1 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69 42 */. i64 iB
1c2d2 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 ; /* Int
1c2d3 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 72 69 eger value of ri
1c2d4 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 ght operand */.
1c2d5 20 20 20 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 double rA;
1c2d6 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c /* Real val
1c2d7 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 ue of left opera
1c2d8 6e 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 nd */. doub
1c2d9 6c 65 20 72 42 3b 20 20 20 20 20 20 2f 2a 20 52 le rB; /* R
1c2da 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 72 69 67 eal value of rig
1c2db 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 ht operand */.
1c2dc 20 20 7d 20 61 66 3b 0a 20 20 20 20 73 74 72 75 } af;. stru
1c2dd 63 74 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 5f 73 ct OP_Function_s
1c2de 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c2df 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d int i;. M
1c2e0 65 6d 20 2a 70 41 72 67 3b 0a 20 20 20 20 20 20 em *pArg;.
1c2e1 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1c2e2 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ctx;. sqlit
1c2e3 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c e3_value **apVal
1c2e4 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 ;. int n;.
1c2e5 20 20 20 7d 20 61 67 3b 0a 20 20 20 20 73 74 72 } ag;. str
1c2e6 75 63 74 20 4f 50 5f 53 68 69 66 74 52 69 67 68 uct OP_ShiftRigh
1c2e7 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c2e8 20 20 20 20 20 69 36 34 20 61 3b 0a 20 20 20 20 i64 a;.
1c2e9 20 20 69 36 34 20 62 3b 0a 20 20 20 20 7d 20 61 i64 b;. } a
1c2ea 68 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 h;. struct OP
1c2eb 5f 47 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b _Ge_stack_vars {
1c2ec 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 . int flags
1c2ed 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b ;. int res;
1c2ee 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69 . char affi
1c2ef 6e 69 74 79 3b 0a 20 20 20 20 7d 20 61 69 3b 0a nity;. } ai;.
1c2f0 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f struct OP_Co
1c2f1 6d 70 61 72 65 5f 73 74 61 63 6b 5f 76 61 72 73 mpare_stack_vars
1c2f2 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a {. int n;.
1c2f3 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
1c2f4 20 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 20 20 int p1;.
1c2f5 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 20 20 63 int p2;. c
1c2f6 6f 6e 73 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b onst KeyInfo *pK
1c2f7 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e eyInfo;. in
1c2f8 74 20 69 64 78 3b 0a 20 20 20 20 20 20 43 6f 6c t idx;. Col
1c2f9 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 lSeq *pColl;
1c2fa 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 /* Collating seq
1c2fb 75 65 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 uence to use on
1c2fc 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 20 this term */.
1c2fd 20 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20 int bRev;
1c2fe 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
1c2ff 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f 72 r DESCENDING sor
1c300 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 7d t order */. }
1c301 20 61 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 aj;. struct
1c302 4f 50 5f 4f 72 5f 73 74 61 63 6b 5f 76 61 72 73 OP_Or_stack_vars
1c303 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 31 3b {. int v1;
1c304 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 /* Left oper
1c305 61 6e 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 and: 0==FALSE,
1c306 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 1==TRUE, 2==UNKN
1c307 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 OWN or NULL */.
1c308 20 20 20 20 20 69 6e 74 20 76 32 3b 20 20 20 20 int v2;
1c309 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 /* Right operand
1c30a 3a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 : 0==FALSE, 1==T
1c30b 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 RUE, 2==UNKNOWN
1c30c 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 7d or NULL */. }
1c30d 20 61 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ak;. struct
1c30e 4f 50 5f 49 66 4e 6f 74 5f 73 74 61 63 6b 5f 76 OP_IfNot_stack_v
1c30f 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 ars {. int
1c310 63 3b 0a 20 20 20 20 7d 20 61 6c 3b 0a 20 20 20 c;. } al;.
1c311 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 6c 75 6d struct OP_Colum
1c312 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 n_stack_vars {.
1c313 20 20 20 20 20 75 33 32 20 70 61 79 6c 6f 61 64 u32 payload
1c314 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 Size; /* Numbe
1c315 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
1c316 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 e record */.
1c317 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a i64 payloadSiz
1c318 65 36 34 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f e64; /* Number o
1c319 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 f bytes in the r
1c31a 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 ecord */. i
1c31b 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 nt p1;
1c31c 20 20 2f 2a 20 50 31 20 76 61 6c 75 65 20 6f 66 /* P1 value of
1c31d 20 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 the opcode */.
1c31e 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20 int p2;
1c31f 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d /* colum
1c320 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72 n number to retr
1c321 69 65 76 65 20 2a 2f 0a 20 20 20 20 20 20 56 64 ieve */. Vd
1c322 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 beCursor *pC;
1c323 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 /* The VDBE cur
1c324 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 sor */. cha
1c325 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 20 20 20 r *zRec;
1c326 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 6f /* Pointer to co
1c327 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 2d 64 61 mplete record-da
1c328 74 61 20 2a 2f 0a 20 20 20 20 20 20 42 74 43 75 ta */. BtCu
1c329 72 73 6f 72 20 2a 70 43 72 73 72 3b 20 20 20 2f rsor *pCrsr; /
1c32a 2a 20 54 68 65 20 42 54 72 65 65 20 63 75 72 73 * The BTree curs
1c32b 6f 72 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 or */. u32
1c32c 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f *aType; /
1c32d 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73 * aType[i] holds
1c32e 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 the numeric typ
1c32f 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f e of the i-th co
1c330 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 75 33 lumn */. u3
1c331 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 2 *aOffset;
1c332 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 /* aOffset[i] i
1c333 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 61 72 s offset to star
1c334 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 69 2d t of data for i-
1c335 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 th column */.
1c336 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 int nField;
1c337 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 /* number
1c338 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 of fields in the
1c339 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 record */.
1c33a 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 int len;
1c33b 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74 /* The lengt
1c33c 68 20 6f 66 20 74 68 65 20 73 65 72 69 61 6c 69 h of the seriali
1c33d 7a 65 64 20 64 61 74 61 20 66 6f 72 20 74 68 65 zed data for the
1c33e 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 column */.
1c33f 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1c340 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1c341 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 ter */. cha
1c342 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 r *zData;
1c343 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 20 72 /* Part of the r
1c344 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f ecord being deco
1c345 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d ded */. Mem
1c346 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 20 *pDest;
1c347 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 /* Where to writ
1c348 65 20 74 68 65 20 65 78 74 72 61 63 74 65 64 20 e the extracted
1c349 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 4d value */. M
1c34a 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20 20 20 em sMem;
1c34b 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67 /* For storing
1c34c 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e the record bein
1c34d 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 20 g decoded */.
1c34e 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 u8 *zIdx;
1c34f 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
1c350 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 nto header */.
1c351 20 20 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b u8 *zEndHdr;
1c352 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1c353 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 r to first byte
1c354 61 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72 after the header
1c355 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 6f 66 */. u32 of
1c356 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 fset; /*
1c357 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 Offset into the
1c358 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 20 75 36 data */. u6
1c359 34 20 6f 66 66 73 65 74 36 34 3b 20 20 20 20 20 4 offset64;
1c35a 20 2f 2a 20 36 34 2d 62 69 74 20 6f 66 66 73 65 /* 64-bit offse
1c35b 74 2e 20 20 36 34 20 62 69 74 73 20 6e 65 65 64 t. 64 bits need
1c35c 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 65 72 ed to catch over
1c35d 66 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 69 6e flow */. in
1c35e 74 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 t szHdr;
1c35f 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
1c360 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c header size fiel
1c361 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72 65 d at start of re
1c362 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e cord */. in
1c363 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 20 t avail;
1c364 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1c365 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 tes of available
1c366 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 20 4d data */. M
1c367 65 6d 20 2a 70 52 65 67 3b 20 20 20 20 20 20 20 em *pReg;
1c368 20 20 2f 2a 20 50 73 65 75 64 6f 54 61 62 6c 65 /* PseudoTable
1c369 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 20 input register
1c36a 2a 2f 0a 20 20 20 20 7d 20 61 6d 3b 0a 20 20 20 */. } am;.
1c36b 20 73 74 72 75 63 74 20 4f 50 5f 41 66 66 69 6e struct OP_Affin
1c36c 69 74 79 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ity_stack_vars {
1c36d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 66 . char *zAf
1c36e 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 65 finity; /* The
1c36f 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 affinity to be
1c370 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 20 20 20 applied */.
1c371 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 Mem *pData0;
1c372 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 /* First reg
1c373 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 20 74 ister to which t
1c374 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 o apply affinity
1c375 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 */. Mem *p
1c376 4c 61 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 Last; /*
1c377 4c 61 73 74 20 72 65 67 69 73 74 65 72 20 74 6f Last register to
1c378 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79 20 which to apply
1c379 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 20 20 affinity */.
1c37a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 Mem *pRec;
1c37b 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
1c37c 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 register */.
1c37d 7d 20 61 6e 3b 0a 20 20 20 20 73 74 72 75 63 74 } an;. struct
1c37e 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 5f 73 OP_MakeRecord_s
1c37f 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c380 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 u8 *zNewRecord
1c381 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75 ; /* A bu
1c382 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 ffer to hold the
1c383 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65 data for the ne
1c384 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 w record */.
1c385 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 Mem *pRec;
1c386 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1c387 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 new record */.
1c388 20 20 20 20 75 36 34 20 6e 44 61 74 61 3b 20 20 u64 nData;
1c389 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1c38a 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1c38b 20 64 61 74 61 20 73 70 61 63 65 20 2a 2f 0a 20 data space */.
1c38c 20 20 20 20 20 69 6e 74 20 6e 48 64 72 3b 20 20 int nHdr;
1c38d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1c38e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1c38f 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a f header space *
1c390 2f 0a 20 20 20 20 20 20 69 36 34 20 6e 42 79 74 /. i64 nByt
1c391 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
1c392 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71 * Data space req
1c393 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 uired for this r
1c394 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 ecord */. i
1c395 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 nt nZero;
1c396 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1c397 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 of zero bytes at
1c398 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1c399 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 record */.
1c39a 69 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20 20 20 int nVarint;
1c39b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1c39c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 76 of bytes in a v
1c39d 61 72 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 arint */. u
1c39e 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 20 32 serial_type;
1c39f 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 66 69 /* Type fi
1c3a0 65 6c 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d eld */. Mem
1c3a1 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 *pData0;
1c3a2 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65 /* First fie
1c3a3 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 ld to be combine
1c3a4 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 d into the recor
1c3a5 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a d */. Mem *
1c3a6 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 pLast;
1c3a7 20 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 /* Last field
1c3a8 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f of the record */
1c3a9 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c . int nFiel
1c3aa 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a d; /*
1c3ab 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 Number of field
1c3ac 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
1c3ad 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a */. char *z
1c3ae 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 Affinity;
1c3af 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 /* The affinity
1c3b0 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 72 string for the r
1c3b1 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 ecord */. i
1c3b2 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 nt file_format;
1c3b3 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f /* File fo
1c3b4 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20 rmat to use for
1c3b5 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 encoding */.
1c3b6 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
1c3b7 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 /* Spac
1c3b8 65 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 e used in zNewRe
1c3b9 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 cord[] */.
1c3ba 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 int len;
1c3bb 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
1c3bc 20 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f 0a 20 of a field */.
1c3bd 20 20 20 7d 20 61 6f 3b 0a 20 20 20 20 73 74 72 } ao;. str
1c3be 75 63 74 20 4f 50 5f 43 6f 75 6e 74 5f 73 74 61 uct OP_Count_sta
1c3bf 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c3c0 69 36 34 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 i64 nEntry;.
1c3c1 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1c3c2 72 3b 0a 20 20 20 20 7d 20 61 70 3b 0a 20 20 20 r;. } ap;.
1c3c3 20 73 74 72 75 63 74 20 4f 50 5f 53 61 76 65 70 struct OP_Savep
1c3c4 6f 69 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 oint_stack_vars
1c3c5 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20 {. int p1;
1c3c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c3c7 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
1c3c8 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a of P1 operand *
1c3c9 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e /. char *zN
1c3ca 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
1c3cb 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
1c3cc 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a of savepoint */.
1c3cd 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b int nName;
1c3ce 0a 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 . Savepoint
1c3cf 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 53 61 *pNew;. Sa
1c3d0 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f vepoint *pSavepo
1c3d1 69 6e 74 3b 0a 20 20 20 20 20 20 53 61 76 65 70 int;. Savep
1c3d2 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 20 20 oint *pTmp;.
1c3d3 20 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 int iSavepoint
1c3d4 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a ;. int ii;.
1c3d5 20 20 20 20 7d 20 61 71 3b 0a 20 20 20 20 73 74 } aq;. st
1c3d6 72 75 63 74 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d ruct OP_AutoComm
1c3d7 69 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a it_stack_vars {.
1c3d8 20 20 20 20 20 20 69 6e 74 20 64 65 73 69 72 65 int desire
1c3d9 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 dAutoCommit;.
1c3da 20 20 20 69 6e 74 20 69 52 6f 6c 6c 62 61 63 6b int iRollback
1c3db 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 75 72 6e ;. int turn
1c3dc 4f 6e 41 43 3b 0a 20 20 20 20 7d 20 61 72 3b 0a OnAC;. } ar;.
1c3dd 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 54 72 struct OP_Tr
1c3de 61 6e 73 61 63 74 69 6f 6e 5f 73 74 61 63 6b 5f ansaction_stack_
1c3df 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72 vars {. Btr
1c3e0 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 61 ee *pBt;. } a
1c3e1 73 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 s;. struct OP
1c3e2 5f 52 65 61 64 43 6f 6f 6b 69 65 5f 73 74 61 63 _ReadCookie_stac
1c3e3 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c3e4 6e 74 20 69 4d 65 74 61 3b 0a 20 20 20 20 20 20 nt iMeta;.
1c3e5 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 69 int iDb;. i
1c3e6 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 20 20 20 20 nt iCookie;.
1c3e7 7d 20 61 74 3b 0a 20 20 20 20 73 74 72 75 63 74 } at;. struct
1c3e8 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 5f 73 74 OP_SetCookie_st
1c3e9 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c3ea 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20 Db *pDb;. }
1c3eb 61 75 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f au;. struct O
1c3ec 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 5f 73 P_VerifyCookie_s
1c3ed 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c3ee 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 20 int iMeta;.
1c3ef 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 Btree *pBt;.
1c3f0 20 20 20 7d 20 61 76 3b 0a 20 20 20 20 73 74 72 } av;. str
1c3f1 75 63 74 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 uct OP_OpenWrite
1c3f2 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c3f3 20 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a int nField;.
1c3f4 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 KeyInfo *p
1c3f5 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 KeyInfo;. i
1c3f6 6e 74 20 70 32 3b 0a 20 20 20 20 20 20 69 6e 74 nt p2;. int
1c3f7 20 69 44 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 iDb;. int
1c3f8 77 72 46 6c 61 67 3b 0a 20 20 20 20 20 20 42 74 wrFlag;. Bt
1c3f9 72 65 65 20 2a 70 58 3b 0a 20 20 20 20 20 20 56 ree *pX;. V
1c3fa 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b dbeCursor *pCur;
1c3fb 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a . Db *pDb;.
1c3fc 20 20 20 20 7d 20 61 77 3b 0a 20 20 20 20 73 74 } aw;. st
1c3fd 72 75 63 74 20 4f 50 5f 4f 70 65 6e 45 70 68 65 ruct OP_OpenEphe
1c3fe 6d 65 72 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73 meral_stack_vars
1c3ff 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c400 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 7d 20 sor *pCx;. }
1c401 61 78 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f ax;. struct O
1c402 50 5f 4f 70 65 6e 50 73 65 75 64 6f 5f 73 74 61 P_OpenPseudo_sta
1c403 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c404 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b VdbeCursor *pCx;
1c405 0a 20 20 20 20 7d 20 61 79 3b 0a 20 20 20 20 73 . } ay;. s
1c406 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 47 74 5f truct OP_SeekGt_
1c407 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c408 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 int res;.
1c409 20 20 69 6e 74 20 6f 63 3b 0a 20 20 20 20 20 20 int oc;.
1c40a 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c40b 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 UnpackedRe
1c40c 63 6f 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e cord r;. in
1c40d 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 t nField;.
1c40e 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f i64 iKey; /
1c40f 2a 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61 * The rowid we a
1c410 72 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f re to seek to */
1c411 0a 20 20 20 20 7d 20 61 7a 3b 0a 20 20 20 20 73 . } az;. s
1c412 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 5f 73 74 truct OP_Seek_st
1c413 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c414 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c415 0a 20 20 20 20 7d 20 62 61 3b 0a 20 20 20 20 73 . } ba;. s
1c416 74 72 75 63 74 20 4f 50 5f 46 6f 75 6e 64 5f 73 truct OP_Found_s
1c417 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c418 20 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69 int alreadyExi
1c419 73 74 73 3b 0a 20 20 20 20 20 20 56 64 62 65 43 sts;. VdbeC
1c41a 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 ursor *pC;.
1c41b 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 int res;.
1c41c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
1c41d 70 49 64 78 4b 65 79 3b 0a 20 20 20 20 20 20 63 pIdxKey;. c
1c41e 68 61 72 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 har aTempRec[ROU
1c41f 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 ND8(sizeof(Unpac
1c420 6b 65 64 52 65 63 6f 72 64 29 29 20 2b 20 73 69 kedRecord)) + si
1c421 7a 65 6f 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d zeof(Mem)*3 + 7]
1c422 3b 0a 20 20 20 20 7d 20 62 62 3b 0a 20 20 20 20 ;. } bb;.
1c423 73 74 72 75 63 74 20 4f 50 5f 49 73 55 6e 69 71 struct OP_IsUniq
1c424 75 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ue_stack_vars {.
1c425 20 20 20 20 20 20 75 31 36 20 69 69 3b 0a 20 20 u16 ii;.
1c426 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c427 70 43 78 3b 0a 20 20 20 20 20 20 42 74 43 75 72 pCx;. BtCur
1c428 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 sor *pCrsr;.
1c429 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 u16 nField;.
1c42a 20 20 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 0a 20 Mem *aMem;.
1c42b 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 UnpackedRec
1c42c 6f 72 64 20 72 3b 20 20 20 20 20 20 20 20 20 20 ord r;
1c42d 20 20 20 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 /* B-Tre
1c42e 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b e index search k
1c42f 65 79 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 ey */. i64
1c430 52 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 R;
1c431 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c432 2a 20 52 6f 77 69 64 20 73 74 6f 72 65 64 20 69 * Rowid stored i
1c433 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 2a 2f n register P3 */
1c434 0a 20 20 20 20 7d 20 62 63 3b 0a 20 20 20 20 73 . } bc;. s
1c435 74 72 75 63 74 20 4f 50 5f 4e 6f 74 45 78 69 73 truct OP_NotExis
1c436 74 73 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ts_stack_vars {.
1c437 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c438 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 *pC;. BtCu
1c439 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 rsor *pCrsr;.
1c43a 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 int res;.
1c43b 20 20 75 36 34 20 69 4b 65 79 3b 0a 20 20 20 20 u64 iKey;.
1c43c 7d 20 62 64 3b 0a 20 20 20 20 73 74 72 75 63 74 } bd;. struct
1c43d 20 4f 50 5f 4e 65 77 52 6f 77 69 64 5f 73 74 61 OP_NewRowid_sta
1c43e 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c43f 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20 20 i64 v;
1c440 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 /* The ne
1c441 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 20 w rowid */.
1c442 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c443 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f /* Curso
1c444 72 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 65 r of table to ge
1c445 74 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20 t the new rowid
1c446 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 */. int res
1c447 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1c448 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 /* Result of an
1c449 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 sqlite3BtreeLast
1c44a 28 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 () */. int
1c44b 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 cnt;
1c44c 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f /* Counter to
1c44d 20 6c 69 6d 69 74 20 74 68 65 20 6e 75 6d 62 65 limit the numbe
1c44e 72 20 6f 66 20 73 65 61 72 63 68 65 73 20 2a 2f r of searches */
1c44f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d . Mem *pMem
1c450 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c451 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e Register holdin
1c452 67 20 6c 61 72 67 65 73 74 20 72 6f 77 69 64 20 g largest rowid
1c453 66 6f 72 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e for AUTOINCREMEN
1c454 54 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 46 T */. VdbeF
1c455 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 rame *pFrame;
1c456 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65 20 /* Root frame
1c457 6f 66 20 56 44 42 45 20 2a 2f 0a 20 20 20 20 7d of VDBE */. }
1c458 20 62 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 be;. struct
1c459 4f 50 5f 49 6e 73 65 72 74 5f 73 74 61 63 6b 5f OP_Insert_stack_
1c45a 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d vars {. Mem
1c45b 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f *pData; /
1c45c 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 * MEM cell holdi
1c45d 6e 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 ng data for the
1c45e 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 record to be ins
1c45f 65 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d erted */. M
1c460 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 em *pKey;
1c461 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c /* MEM cell hol
1c462 64 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74 68 ding key for th
1c463 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 e record */.
1c464 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 i64 iKey;
1c465 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 /* The integ
1c466 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 er ROWID or key
1c467 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 for the record t
1c468 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f o be inserted */
1c469 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c46a 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 r *pC; /* Curs
1c46b 6f 72 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f or to table into
1c46c 20 77 68 69 63 68 20 69 6e 73 65 72 74 20 69 73 which insert is
1c46d 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20 written */.
1c46e 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 int nZero;
1c46f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1c470 20 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 zero-bytes to a
1c471 70 70 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 ppend */. i
1c472 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 nt seekResult;
1c473 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72 /* Result of pr
1c474 69 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69 66 ior seek or 0 if
1c475 20 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55 4c no USESEEKRESUL
1c476 54 20 66 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20 T flag */.
1c477 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b const char *zDb;
1c478 20 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 /* database na
1c479 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 me - used by the
1c47a 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a update hook */.
1c47b 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1c47c 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 *zTbl; /* Table
1c47d 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 name - used by
1c47e 74 68 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 the opdate hook
1c47f 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 3b */. int op;
1c480 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
1c481 63 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 code for update
1c482 68 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 hook: SQLITE_UPD
1c483 41 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e ATE or SQLITE_IN
1c484 53 45 52 54 20 2a 2f 0a 20 20 20 20 7d 20 62 66 SERT */. } bf
1c485 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c486 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 Delete_stack_var
1c487 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b s {. i64 iK
1c488 65 79 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 ey;. VdbeCu
1c489 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 rsor *pC;. }
1c48a 62 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bg;. struct O
1c48b 50 5f 52 6f 77 44 61 74 61 5f 73 74 61 63 6b 5f P_RowData_stack_
1c48c 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 vars {. Vdb
1c48d 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c48e 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 BtCursor *pCr
1c48f 73 72 3b 0a 20 20 20 20 20 20 75 33 32 20 6e 3b sr;. u32 n;
1c490 0a 20 20 20 20 20 20 69 36 34 20 6e 36 34 3b 0a . i64 n64;.
1c491 20 20 20 20 7d 20 62 68 3b 0a 20 20 20 20 73 74 } bh;. st
1c492 72 75 63 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74 ruct OP_Rowid_st
1c493 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c494 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c495 0a 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 . i64 v;.
1c496 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 sqlite3_vtab
1c497 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 *pVtab;. c
1c498 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
1c499 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 ule *pModule;.
1c49a 20 20 7d 20 62 69 3b 0a 20 20 20 20 73 74 72 75 } bi;. stru
1c49b 63 74 20 4f 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 ct OP_NullRow_st
1c49c 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c49d 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c49e 0a 20 20 20 20 7d 20 62 6a 3b 0a 20 20 20 20 73 . } bj;. s
1c49f 74 72 75 63 74 20 4f 50 5f 4c 61 73 74 5f 73 74 truct OP_Last_st
1c4a0 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c4a1 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c4a2 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 . BtCursor
1c4a3 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e *pCrsr;. in
1c4a4 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6b 3b t res;. } bk;
1c4a5 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 . struct OP_R
1c4a6 65 77 69 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 ewind_stack_vars
1c4a7 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c4a8 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 sor *pC;. B
1c4a9 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c4aa 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c4ab 20 20 20 7d 20 62 6c 3b 0a 20 20 20 20 73 74 72 } bl;. str
1c4ac 75 63 74 20 4f 50 5f 4e 65 78 74 5f 73 74 61 63 uct OP_Next_stac
1c4ad 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 k_vars {. V
1c4ae 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 dbeCursor *pC;.
1c4af 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 BtCursor *p
1c4b0 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 Crsr;. int
1c4b1 72 65 73 3b 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 res;. } bm;.
1c4b2 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 struct OP_Idx
1c4b3 49 6e 73 65 72 74 5f 73 74 61 63 6b 5f 76 61 72 Insert_stack_var
1c4b4 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 s {. VdbeCu
1c4b5 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 rsor *pC;.
1c4b6 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1c4b7 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b . int nKey;
1c4b8 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
1c4b9 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 7d 20 62 r *zKey;. } b
1c4ba 6e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 n;. struct OP
1c4bb 5f 49 64 78 44 65 6c 65 74 65 5f 73 74 61 63 6b _IdxDelete_stack
1c4bc 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c4bd 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c4be 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 BtCursor *pC
1c4bf 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 rsr;. int r
1c4c0 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b es;. Unpack
1c4c1 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 edRecord r;.
1c4c2 7d 20 62 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 } bo;. struct
1c4c3 20 4f 50 5f 49 64 78 52 6f 77 69 64 5f 73 74 61 OP_IdxRowid_sta
1c4c4 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c4c5 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1c4c6 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c4c7 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 36 34 r *pC;. i64
1c4c8 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 20 62 70 rowid;. } bp
1c4c9 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c4ca 49 64 78 47 45 5f 73 74 61 63 6b 5f 76 61 72 73 IdxGE_stack_vars
1c4cb 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c4cc 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 sor *pC;. i
1c4cd 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e nt res;. Un
1c4ce 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a packedRecord r;.
1c4cf 20 20 20 20 7d 20 62 71 3b 0a 20 20 20 20 73 74 } bq;. st
1c4d0 72 75 63 74 20 4f 50 5f 44 65 73 74 72 6f 79 5f ruct OP_Destroy_
1c4d1 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c4d2 20 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 int iMoved;.
1c4d3 20 20 20 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 int iCnt;.
1c4d4 20 20 20 20 20 56 64 62 65 20 2a 70 56 64 62 65 Vdbe *pVdbe
1c4d5 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b ;. int iDb;
1c4d6 0a 20 20 20 20 7d 20 62 72 3b 0a 20 20 20 20 73 . } br;. s
1c4d7 74 72 75 63 74 20 4f 50 5f 43 6c 65 61 72 5f 73 truct OP_Clear_s
1c4d8 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c4d9 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 20 int nChange;.
1c4da 20 20 20 7d 20 62 73 3b 0a 20 20 20 20 73 74 72 } bs;. str
1c4db 75 63 74 20 4f 50 5f 43 72 65 61 74 65 54 61 62 uct OP_CreateTab
1c4dc 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a le_stack_vars {.
1c4dd 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a int pgno;.
1c4de 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b int flags;
1c4df 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a . Db *pDb;.
1c4e0 20 20 20 20 7d 20 62 74 3b 0a 20 20 20 20 73 74 } bt;. st
1c4e1 72 75 63 74 20 4f 50 5f 50 61 72 73 65 53 63 68 ruct OP_ParseSch
1c4e2 65 6d 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ema_stack_vars {
1c4e3 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a . int iDb;.
1c4e4 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1c4e5 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 20 *zMaster;.
1c4e6 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 char *zSql;.
1c4e7 20 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 InitData init
1c4e8 44 61 74 61 3b 0a 20 20 20 20 7d 20 62 75 3b 0a Data;. } bu;.
1c4e9 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e struct OP_In
1c4ea 74 65 67 72 69 74 79 43 6b 5f 73 74 61 63 6b 5f tegrityCk_stack_
1c4eb 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c4ec 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 nRoot; /*
1c4ed 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 Number of tables
1c4ee 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d to check. (Num
1c4ef 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 ber of root page
1c4f0 73 2e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 s.) */. int
1c4f1 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 *aRoot; /*
1c4f2 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 Array of rootpag
1c4f3 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 e numbers for ta
1c4f4 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b bles to be check
1c4f5 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ed */. int
1c4f6 6a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c j; /* L
1c4f7 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
1c4f8 20 20 20 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 int nErr;
1c4f9 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c4fa 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 f errors reporte
1c4fb 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 d */. char
1c4fc 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 *z; /* Te
1c4fd 78 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 xt of the error
1c4fe 72 65 70 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20 report */.
1c4ff 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 Mem *pnErr;
1c500 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70 /* Register keep
1c501 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72 ing track of err
1c502 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f ors remaining */
1c503 0a 20 20 20 20 7d 20 62 76 3b 0a 20 20 20 20 73 . } bv;. s
1c504 74 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 41 truct OP_RowSetA
1c505 64 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a dd_stack_vars {.
1c506 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 64 78 3b Mem *pIdx;
1c507 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 56 61 6c . Mem *pVal
1c508 3b 0a 20 20 20 20 7d 20 62 77 3b 0a 20 20 20 20 ;. } bw;.
1c509 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 struct OP_RowSet
1c50a 52 65 61 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 Read_stack_vars
1c50b 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 64 {. Mem *pId
1c50c 78 3b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c x;. i64 val
1c50d 3b 0a 20 20 20 20 7d 20 62 78 3b 0a 20 20 20 20 ;. } bx;.
1c50e 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 struct OP_RowSet
1c50f 54 65 73 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 Test_stack_vars
1c510 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 53 65 74 {. int iSet
1c511 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 78 69 73 ;. int exis
1c512 74 73 3b 0a 20 20 20 20 7d 20 62 79 3b 0a 20 20 ts;. } by;.
1c513 20 20 73 74 72 75 63 74 20 4f 50 5f 50 72 6f 67 struct OP_Prog
1c514 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ram_stack_vars {
1c515 0a 20 20 20 20 20 20 69 6e 74 20 6e 4d 65 6d 3b . int nMem;
1c516 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c517 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
1c518 72 79 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 ry registers for
1c519 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a sub-program */.
1c51a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b int nByte;
1c51b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c51c 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 69 6d Bytes of runtim
1c51d 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 e space required
1c51e 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d for sub-program
1c51f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 */. Mem *p
1c520 52 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Rt;
1c521 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f /* Register to
1c522 20 61 6c 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d allocate runtim
1c523 65 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 e space */.
1c524 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 Mem *pMem;
1c525 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
1c526 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f to iterate thro
1c527 75 67 68 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 ugh memory cells
1c528 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 */. Mem *p
1c529 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 End;
1c52a 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 /* Last memory
1c52b 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 cell in new arr
1c52c 61 79 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 ay */. Vdbe
1c52d 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 Frame *pFrame;
1c52e 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 /* New vdbe
1c52f 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74 65 frame to execute
1c530 20 69 6e 20 2a 2f 0a 20 20 20 20 20 20 53 75 62 in */. Sub
1c531 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 Program *pProgra
1c532 6d 3b 20 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 m; /* Sub-prog
1c533 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65 20 2a ram to execute *
1c534 2f 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 74 3b /. void *t;
1c535 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c536 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 /* Token identif
1c537 79 69 6e 67 20 74 72 69 67 67 65 72 20 2a 2f 0a ying trigger */.
1c538 20 20 20 20 7d 20 62 7a 3b 0a 20 20 20 20 73 74 } bz;. st
1c539 72 75 63 74 20 4f 50 5f 50 61 72 61 6d 5f 73 74 ruct OP_Param_st
1c53a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c53b 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 VdbeFrame *pFra
1c53c 6d 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 me;. Mem *p
1c53d 49 6e 3b 0a 20 20 20 20 7d 20 63 61 3b 0a 20 20 In;. } ca;.
1c53e 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 65 6d 4d struct OP_MemM
1c53f 61 78 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ax_stack_vars {.
1c540 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e 31 3b Mem *pIn1;
1c541 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 6d 65 . VdbeFrame
1c542 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 20 7d 20 *pFrame;. }
1c543 63 62 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f cb;. struct O
1c544 50 5f 41 67 67 53 74 65 70 5f 73 74 61 63 6b 5f P_AggStep_stack_
1c545 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c546 20 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b n;. int i;
1c547 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d . Mem *pMem
1c548 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 ;. Mem *pRe
1c549 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
1c54a 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 _context ctx;.
1c54b 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 sqlite3_valu
1c54c 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 20 20 7d e **apVal;. }
1c54d 20 63 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20 cc;. struct
1c54e 4f 50 5f 41 67 67 46 69 6e 61 6c 5f 73 74 61 63 OP_AggFinal_stac
1c54f 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d k_vars {. M
1c550 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 7d 20 em *pMem;. }
1c551 63 64 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f cd;. struct O
1c552 50 5f 49 6e 63 72 56 61 63 75 75 6d 5f 73 74 61 P_IncrVacuum_sta
1c553 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c554 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 Btree *pBt;.
1c555 7d 20 63 65 3b 0a 20 20 20 20 73 74 72 75 63 74 } ce;. struct
1c556 20 4f 50 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b OP_VBegin_stack
1c557 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 54 _vars {. VT
1c558 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a 20 20 20 able *pVTab;.
1c559 20 7d 20 63 66 3b 0a 20 20 20 20 73 74 72 75 63 } cf;. struc
1c55a 74 20 4f 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b t OP_VOpen_stack
1c55b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c55c 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a beCursor *pCur;.
1c55d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c55e 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 ab_cursor *pVtab
1c55f 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 Cursor;. sq
1c560 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1c561 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 b;. sqlite3
1c562 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 _module *pModule
1c563 3b 0a 20 20 20 20 7d 20 63 67 3b 0a 20 20 20 20 ;. } cg;.
1c564 73 74 72 75 63 74 20 4f 50 5f 56 46 69 6c 74 65 struct OP_VFilte
1c565 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 r_stack_vars {.
1c566 20 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 int nArg;.
1c567 20 20 20 20 20 69 6e 74 20 69 51 75 65 72 79 3b int iQuery;
1c568 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c . const sql
1c569 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1c56a 64 75 6c 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 dule;. Mem
1c56b 2a 70 51 75 65 72 79 3b 0a 20 20 20 20 20 20 4d *pQuery;. M
1c56c 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 20 20 20 em *pArgc;.
1c56d 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 sqlite3_vtab_cu
1c56e 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f rsor *pVtabCurso
1c56f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 r;. sqlite3
1c570 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 _vtab *pVtab;.
1c571 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c572 70 43 75 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 pCur;. int
1c573 72 65 73 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 res;. int i
1c574 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 ;. Mem **ap
1c575 41 72 67 3b 0a 20 20 20 20 7d 20 63 68 3b 0a 20 Arg;. } ch;.
1c576 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 43 6f struct OP_VCo
1c577 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 lumn_stack_vars
1c578 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1c579 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 vtab *pVtab;.
1c57a 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 const sqlite3
1c57b 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 _module *pModule
1c57c 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 ;. Mem *pDe
1c57d 73 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 st;. sqlite
1c57e 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 3_context sConte
1c57f 78 74 3b 0a 20 20 20 20 7d 20 63 69 3b 0a 20 20 xt;. } ci;.
1c580 20 20 73 74 72 75 63 74 20 4f 50 5f 56 4e 65 78 struct OP_VNex
1c581 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c582 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 sqlite3_vta
1c583 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 b *pVtab;.
1c584 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
1c585 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c586 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 int res;.
1c587 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c588 70 43 75 72 3b 0a 20 20 20 20 7d 20 63 6a 3b 0a pCur;. } cj;.
1c589 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 52 struct OP_VR
1c58a 65 6e 61 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73 ename_stack_vars
1c58b 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
1c58c 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 _vtab *pVtab;.
1c58d 20 20 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a Mem *pName;.
1c58e 20 20 20 20 7d 20 63 6b 3b 0a 20 20 20 20 73 74 } ck;. st
1c58f 72 75 63 74 20 4f 50 5f 56 55 70 64 61 74 65 5f ruct OP_VUpdate_
1c590 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c591 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 sqlite3_vtab
1c592 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73 71 *pVtab;. sq
1c593 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1c594 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 odule;. int
1c595 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 nArg;. int
1c596 20 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 i;. sqlite
1c597 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 _int64 rowid;.
1c598 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b Mem **apArg;
1c599 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 58 3b 0a . Mem *pX;.
1c59a 20 20 20 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74 } cl;. st
1c59b 72 75 63 74 20 4f 50 5f 50 61 67 65 63 6f 75 6e ruct OP_Pagecoun
1c59c 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c59d 20 20 20 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 int p1;.
1c59e 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 int nPage;.
1c59f 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 Pager *pPage
1c5a0 72 3b 0a 20 20 20 20 7d 20 63 6d 3b 0a 20 20 20 r;. } cm;.
1c5a1 20 73 74 72 75 63 74 20 4f 50 5f 54 72 61 63 65 struct OP_Trace
1c5a2 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c5a3 20 20 20 20 63 68 61 72 20 2a 7a 54 72 61 63 65 char *zTrace
1c5a4 3b 0a 20 20 20 20 7d 20 63 6e 3b 0a 20 20 7d 20 ;. } cn;. }
1c5a5 75 3b 0a 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f u;. /* End auto
1c5a6 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 matically genera
1c5a7 74 65 64 20 63 6f 64 65 0a 20 20 2a 2a 2a 2a 2a ted code. *****
1c5a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c5a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c5aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c5ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
1c5ac 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d .. assert( p->m
1c5ad 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
1c5ae 5f 52 55 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 _RUN ); /* sqli
1c5af 74 65 33 5f 73 74 65 70 28 29 20 76 65 72 69 66 te3_step() verif
1c5b0 69 65 73 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 ies this */. as
1c5b1 73 65 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d sert( db->magic=
1c5b2 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 =SQLITE_MAGIC_BU
1c5b3 53 59 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 SY );. sqlite3V
1c5b4 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 dbeMutexArrayEnt
1c5b5 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e er(p);. if( p->
1c5b6 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
1c5b7 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
1c5b8 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c happens if a mal
1c5b9 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 loc() inside a c
1c5ba 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 all to sqlite3_c
1c5bb 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a olumn_text() or.
1c5bc 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1c5bd 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 olumn_text16() f
1c5be 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 ailed. */. g
1c5bf 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1c5c0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d assert( p->rc=
1c5c1 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d =SQLITE_OK || p-
1c5c2 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 >rc==SQLITE_BUSY
1c5c3 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 );. p->rc = SQ
1c5c4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 LITE_OK;. asser
1c5c5 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 t( p->explain==0
1c5c6 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 );. p->pResult
1c5c7 53 65 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 Set = 0;. db->b
1c5c8 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 usyHandler.nBusy
1c5c9 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f = 0;. CHECK_FO
1c5ca 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 R_INTERRUPT;. s
1c5cb 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 qlite3VdbeIOTrac
1c5cc 65 53 71 6c 28 70 29 3b 0a 23 69 66 64 65 66 20 eSql(p);.#ifdef
1c5cd 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 73 SQLITE_DEBUG. s
1c5ce 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
1c5cf 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 nMalloc();. if(
1c5d0 20 70 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 p->pc==0 . &&
1c5d1 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 ((p->db->flags
1c5d2 26 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 & SQLITE_VdbeLis
1c5d3 74 69 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 ting) || fileExi
1c5d4 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f 65 78 sts(db, "vdbe_ex
1c5d5 70 6c 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 plain")). ){.
1c5d6 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 int i;. pri
1c5d7 6e 74 66 28 22 56 44 42 45 20 50 72 6f 67 72 61 ntf("VDBE Progra
1c5d8 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a m Listing:\n");.
1c5d9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 sqlite3VdbeP
1c5da 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 rintSql(p);.
1c5db 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f for(i=0; i<p->nO
1c5dc 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 p; i++){. s
1c5dd 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f qlite3VdbePrintO
1c5de 70 28 73 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d p(stdout, i, &p-
1c5df 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a >aOp[i]);. }.
1c5e0 20 20 7d 0a 20 20 69 66 28 20 66 69 6c 65 45 78 }. if( fileEx
1c5e1 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f 74 ists(db, "vdbe_t
1c5e2 72 61 63 65 22 29 20 29 7b 0a 20 20 20 20 70 2d race") ){. p-
1c5e3 3e 74 72 61 63 65 20 3d 20 73 74 64 6f 75 74 3b >trace = stdout;
1c5e4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e . }. sqlite3En
1c5e5 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
1c5e6 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 70 63 .#endif. for(pc
1c5e7 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49 =p->pc; rc==SQLI
1c5e8 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 20 TE_OK; pc++){.
1c5e9 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 30 20 assert( pc>=0
1c5ea 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a && pc<p->nOp );.
1c5eb 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c if( db->mall
1c5ec 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 ocFailed ) goto
1c5ed 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56 no_mem;.#ifdef V
1c5ee 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 DBE_PROFILE.
1c5ef 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a 20 20 20 origPc = pc;.
1c5f0 20 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 start = sqlite3
1c5f1 48 77 74 69 6d 65 28 29 3b 0a 23 65 6e 64 69 66 Hwtime();.#endif
1c5f2 0a 20 20 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 . pOp = &p->a
1c5f3 4f 70 5b 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a 20 Op[pc];.. /*
1c5f4 4f 6e 6c 79 20 61 6c 6c 6f 77 20 74 72 61 63 69 Only allow traci
1c5f5 6e 67 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 ng if SQLITE_DEB
1c5f6 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 UG is defined..
1c5f7 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c */.#ifdef SQL
1c5f8 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 ITE_DEBUG. if
1c5f9 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 ( p->trace ){.
1c5fa 20 20 20 20 69 66 28 20 70 63 3d 3d 30 20 29 7b if( pc==0 ){
1c5fb 0a 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 28 . printf(
1c5fc 22 56 44 42 45 20 45 78 65 63 75 74 69 6f 6e 20 "VDBE Execution
1c5fd 54 72 61 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 20 Trace:\n");.
1c5fe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 sqlite3VdbeP
1c5ff 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 rintSql(p);.
1c600 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1c601 33 56 64 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e 3VdbePrintOp(p->
1c602 74 72 61 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b trace, pc, pOp);
1c603 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1c604 2d 3e 74 72 61 63 65 3d 3d 30 20 26 26 20 70 63 ->trace==0 && pc
1c605 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
1c606 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
1c607 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 69 alloc();. i
1c608 66 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 f( fileExists(db
1c609 2c 20 22 76 64 62 65 5f 73 71 6c 74 72 61 63 65 , "vdbe_sqltrace
1c60a 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ") ){. sq
1c60b 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 lite3VdbePrintSq
1c60c 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 l(p);. }.
1c60d 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 sqlite3EndBe
1c60e 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1c60f 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
1c610 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 .. /* Check
1c611 74 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 65 to see if we nee
1c612 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e d to simulate an
1c613 20 69 6e 74 65 72 72 75 70 74 2e 20 20 54 68 69 interrupt. Thi
1c614 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 0a 20 s only happens.
1c615 20 20 20 2a 2a 20 69 66 20 77 65 20 68 61 76 65 ** if we have
1c616 20 61 20 73 70 65 63 69 61 6c 20 74 65 73 74 20 a special test
1c617 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 build.. */.#i
1c618 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1c619 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1c61a 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
1c61b 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 >0 ){. sqli
1c61c 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f te3_interrupt_co
1c61d 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 unt--;. if(
1c61e 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1c61f 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 pt_count==0 ){.
1c620 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
1c621 6e 74 65 72 72 75 70 74 28 64 62 29 3b 0a 20 20 nterrupt(db);.
1c622 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 }. }.#end
1c623 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
1c624 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 TE_OMIT_PROGRESS
1c625 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20 20 2f 2a _CALLBACK. /*
1c626 20 43 61 6c 6c 20 74 68 65 20 70 72 6f 67 72 65 Call the progre
1c627 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 69 ss callback if i
1c628 74 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20 t is configured
1c629 61 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 64 and the required
1c62a 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f number. ** o
1c62b 66 20 56 44 42 45 20 6f 70 73 20 68 61 76 65 20 f VDBE ops have
1c62c 62 65 65 6e 20 65 78 65 63 75 74 65 64 20 28 65 been executed (e
1c62d 69 74 68 65 72 20 73 69 6e 63 65 20 74 68 69 73 ither since this
1c62e 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a 20 invocation of.
1c62f 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 ** sqlite3Vdb
1c630 65 45 78 65 63 28 29 20 6f 72 20 73 69 6e 63 65 eExec() or since
1c631 20 6c 61 73 74 20 74 69 6d 65 20 74 68 65 20 70 last time the p
1c632 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b rogress callback
1c633 20 77 61 73 20 63 61 6c 6c 65 64 29 2e 0a 20 20 was called)..
1c634 20 20 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 ** If the prog
1c635 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 ress callback re
1c636 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 turns non-zero,
1c637 65 78 69 74 20 74 68 65 20 76 69 72 74 75 61 6c exit the virtual
1c638 20 6d 61 63 68 69 6e 65 20 77 69 74 68 0a 20 20 machine with.
1c639 20 20 2a 2a 20 61 20 72 65 74 75 72 6e 20 63 6f ** a return co
1c63a 64 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e de SQLITE_ABORT.
1c63b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1c63c 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 29 7b db->xProgress ){
1c63d 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6e . if( db->n
1c63e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d 6e 50 72 ProgressOps==nPr
1c63f 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a 20 20 20 ogressOps ){.
1c640 20 20 20 20 20 69 6e 74 20 70 72 63 3b 0a 20 20 int prc;.
1c641 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1c642 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1c643 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1c644 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 to_misuse;.
1c645 20 20 20 70 72 63 20 3d 64 62 2d 3e 78 50 72 6f prc =db->xPro
1c646 67 72 65 73 73 28 64 62 2d 3e 70 50 72 6f 67 72 gress(db->pProgr
1c647 65 73 73 41 72 67 29 3b 0a 20 20 20 20 20 20 20 essArg);.
1c648 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1c649 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 tyOn(db) ) goto
1c64a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1c64b 75 73 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 use;. if(
1c64c 20 70 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 prc!=0 ){.
1c64d 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1c64e 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20 _INTERRUPT;.
1c64f 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f goto vdbe_
1c650 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 20 error_halt;.
1c651 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 50 }. nP
1c652 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a rogressOps = 0;.
1c653 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 50 }. nP
1c654 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20 rogressOps++;.
1c655 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
1c656 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 /* Do common set
1c657 75 70 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f up processing fo
1c658 72 20 61 6e 79 20 6f 70 63 6f 64 65 20 74 68 61 r any opcode tha
1c659 74 20 69 73 20 6d 61 72 6b 65 64 0a 20 20 20 20 t is marked.
1c65a 2a 2a 20 77 69 74 68 20 74 68 65 20 22 6f 75 74 ** with the "out
1c65b 32 2d 70 72 65 72 65 6c 65 61 73 65 22 20 74 61 2-prerelease" ta
1c65c 67 2e 20 20 53 75 63 68 20 6f 70 63 6f 64 65 73 g. Such opcodes
1c65d 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 0a 20 have a single.
1c65e 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 77 68 69 ** output whi
1c65f 63 68 20 69 73 20 73 70 65 63 69 66 69 65 64 20 ch is specified
1c660 62 79 20 74 68 65 20 50 32 20 70 61 72 61 6d 65 by the P2 parame
1c661 74 65 72 2e 20 20 54 68 65 20 50 32 20 72 65 67 ter. The P2 reg
1c662 69 73 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 ister. ** is
1c663 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 initialized to a
1c664 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 NULL.. */.
1c665 20 20 6f 70 50 72 6f 70 65 72 74 79 20 3d 20 6f opProperty = o
1c666 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 70 4f pcodeProperty[pO
1c667 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 p->opcode];.
1c668 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 if( (opProperty
1c669 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 & OPFLG_OUT2_PRE
1c66a 52 45 4c 45 41 53 45 29 21 3d 30 20 29 7b 0a 20 RELEASE)!=0 ){.
1c66b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1c66c 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 ->p2>0 );.
1c66d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c assert( pOp->p2<
1c66e 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 =p->nMem );.
1c66f 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 pOut = &p->aMe
1c670 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1c671 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1c672 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 ReleaseExternal(
1c673 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 pOut);. pOu
1c674 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e t->flags = MEM_N
1c675 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d ull;. pOut-
1c676 3e 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 >n = 0;. }els
1c677 65 0a 20 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f e. . /* Do co
1c678 6d 6d 6f 6e 20 73 65 74 75 70 20 66 6f 72 20 6f mmon setup for o
1c679 70 63 6f 64 65 73 20 6d 61 72 6b 65 64 20 77 69 pcodes marked wi
1c67a 74 68 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f th one of the fo
1c67b 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 llowing. ** c
1c67c 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 ombinations of p
1c67d 72 6f 70 65 72 74 69 65 73 2e 0a 20 20 20 20 2a roperties.. *
1c67e 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 *. **
1c67f 20 20 20 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 in1. **
1c680 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32 0a in1 in2.
1c681 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
1c682 20 69 6e 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 in1 in2 out3.
1c683 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 ** i
1c684 6e 31 20 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 n1 in3. **.
1c685 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 70 ** Variables p
1c686 49 6e 31 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 In1, pIn2, and p
1c687 49 6e 33 20 61 72 65 20 6d 61 64 65 20 74 6f 20 In3 are made to
1c688 70 6f 69 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 point to appropr
1c689 69 61 74 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 iate. ** regi
1c68a 73 74 65 72 73 20 66 6f 72 20 69 6e 70 75 74 73 sters for inputs
1c68b 2e 20 20 56 61 72 69 61 62 6c 65 20 70 4f 75 74 . Variable pOut
1c68c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6f points to the o
1c68d 75 74 70 75 74 20 72 65 67 69 73 74 65 72 2e 0a utput register..
1c68e 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 */. if( (
1c68f 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
1c690 4c 47 5f 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 LG_IN1)!=0 ){.
1c691 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1c692 3e 70 31 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 >p1>0 );. a
1c693 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d ssert( pOp->p1<=
1c694 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 p->nMem );.
1c695 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn1 = &p->aMem
1c696 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 [pOp->p1];.
1c697 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1c698 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a pOp->p1, pIn1);.
1c699 20 20 20 20 20 20 69 66 28 20 28 6f 70 50 72 6f if( (opPro
1c69a 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e perty & OPFLG_IN
1c69b 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 2)!=0 ){.
1c69c 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1c69d 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 >0 );. as
1c69e 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 sert( pOp->p2<=p
1c69f 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 ->nMem );.
1c6a0 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 pIn2 = &p->aMe
1c6a1 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1c6a2 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1c6a3 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 CE(pOp->p2, pIn2
1c6a4 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 73 );. /* As
1c6a5 20 63 75 72 72 65 6e 74 6c 79 20 69 6d 70 6c 65 currently imple
1c6a6 6d 65 6e 74 65 64 2c 20 69 6e 32 20 69 6d 70 6c mented, in2 impl
1c6a7 69 65 73 20 6f 75 74 33 2e 20 20 54 68 65 72 65 ies out3. There
1c6a8 20 69 73 20 6e 6f 20 72 65 61 73 6f 6e 0a 20 20 is no reason.
1c6a9 20 20 20 20 20 20 2a 2a 20 77 68 79 20 74 68 69 ** why thi
1c6aa 73 20 68 61 73 20 74 6f 20 62 65 2c 20 69 74 20 s has to be, it
1c6ab 6a 75 73 74 20 77 6f 72 6b 65 64 20 6f 75 74 20 just worked out
1c6ac 74 68 61 74 20 77 61 79 2e 20 2a 2f 0a 20 20 20 that way. */.
1c6ad 20 20 20 20 20 61 73 73 65 72 74 28 20 28 6f 70 assert( (op
1c6ae 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 Property & OPFLG
1c6af 5f 4f 55 54 33 29 21 3d 30 20 29 3b 0a 20 20 20 _OUT3)!=0 );.
1c6b0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1c6b1 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 ->p3>0 );.
1c6b2 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c6b3 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1c6b4 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d pOut = &p-
1c6b5 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1c6b6 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1c6b7 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 (opProperty & OP
1c6b8 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 FLG_IN3)!=0 ){.
1c6b9 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1c6ba 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 Op->p3>0 );.
1c6bb 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1c6bc 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1c6bd 20 20 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 pIn3 = &
1c6be 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1c6bf 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54 ;. REGIST
1c6c0 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 ER_TRACE(pOp->p3
1c6c1 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d , pIn3);. }
1c6c2 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 . }else if( (
1c6c3 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
1c6c4 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 LG_IN2)!=0 ){.
1c6c5 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1c6c6 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 >p2>0 );. a
1c6c7 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d ssert( pOp->p2<=
1c6c8 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 p->nMem );.
1c6c9 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d pIn2 = &p->aMem
1c6ca 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 [pOp->p2];.
1c6cb 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1c6cc 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a pOp->p2, pIn2);.
1c6cd 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f }else if( (o
1c6ce 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c pProperty & OPFL
1c6cf 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 G_IN3)!=0 ){.
1c6d0 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1c6d1 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 p3>0 );. as
1c6d2 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 sert( pOp->p3<=p
1c6d3 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 ->nMem );.
1c6d4 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pIn3 = &p->aMem[
1c6d5 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 pOp->p3];.
1c6d6 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1c6d7 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 Op->p3, pIn3);.
1c6d8 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 }.. switch
1c6d9 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b ( pOp->opcode ){
1c6da 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1c6db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6df 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73 .** What follows
1c6e0 20 69 73 20 61 20 6d 61 73 73 69 76 65 20 73 77 is a massive sw
1c6e1 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 itch statement w
1c6e2 68 65 72 65 20 65 61 63 68 20 63 61 73 65 20 69 here each case i
1c6e3 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 mplements a.** s
1c6e4 65 70 61 72 61 74 65 20 69 6e 73 74 72 75 63 74 eparate instruct
1c6e5 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75 ion in the virtu
1c6e6 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20 al machine. If
1c6e7 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73 we follow the us
1c6e8 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69 ual.** indentati
1c6e9 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 on conventions,
1c6ea 65 61 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64 each case should
1c6eb 20 62 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20 be indented by
1c6ec 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a 6 spaces. But.*
1c6ed 2a 20 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20 * that is a lot
1c6ee 6f 66 20 77 61 73 74 65 64 20 73 70 61 63 65 20 of wasted space
1c6ef 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67 on the left marg
1c6f0 69 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65 in. So the code
1c6f1 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73 within.** the s
1c6f2 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 witch statement
1c6f3 77 69 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20 will break with
1c6f4 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62 convention and b
1c6f5 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e e flush-left. An
1c6f6 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d other.** big com
1c6f7 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f ment (similar to
1c6f8 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20 this one) will
1c6f9 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69 mark the point i
1c6fa 6e 20 74 68 65 20 63 6f 64 65 20 77 68 65 72 65 n the code where
1c6fb 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f .** we transitio
1c6fc 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c n back to normal
1c6fd 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a indentation..**
1c6fe 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69 .** The formatti
1c6ff 6e 67 20 6f 66 20 65 61 63 68 20 63 61 73 65 20 ng of each case
1c700 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 is important. T
1c701 68 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20 he makefile for
1c702 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61 SQLite.** genera
1c703 74 65 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20 tes two C files
1c704 22 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20 "opcodes.h" and
1c705 22 6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73 "opcodes.c" by s
1c706 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20 canning this.**
1c707 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 file looking for
1c708 20 6c 69 6e 65 73 20 74 68 61 74 20 62 65 67 69 lines that begi
1c709 6e 20 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f n with "case OP_
1c70a 22 2e 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e ". The opcodes.
1c70b 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 h files.** will
1c70c 62 65 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23 be filled with #
1c70d 64 65 66 69 6e 65 73 20 74 68 61 74 20 67 69 76 defines that giv
1c70e 65 20 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72 e unique integer
1c70f 20 76 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a values to each.
1c710 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 ** opcode and th
1c711 65 20 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 e opcodes.c file
1c712 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 is filled with
1c713 61 6e 20 61 72 72 61 79 20 6f 66 20 73 74 72 69 an array of stri
1c714 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 ngs where.** eac
1c715 68 20 73 74 72 69 6e 67 20 69 73 20 74 68 65 20 h string is the
1c716 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f symbolic name fo
1c717 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 r the correspond
1c718 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 ing opcode. If
1c719 74 68 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 the.** case stat
1c71a 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 ement is followe
1c71b 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f d by a comment o
1c71c 66 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 f the form "/# s
1c71d 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a ame as ... #/".*
1c71e 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69 * that comment i
1c71f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
1c720 69 6e 65 20 74 68 65 20 70 61 72 74 69 63 75 6c ine the particul
1c721 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 ar value of the
1c722 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 opcode..**.** Ot
1c723 68 65 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 her keywords in
1c724 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 the comment that
1c725 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61 follows each ca
1c726 73 65 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a se are used to.*
1c727 2a 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 * construct the
1c728 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 OPFLG_INITIALIZE
1c729 52 20 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69 R value that ini
1c72a 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50 tializes opcodeP
1c72b 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 roperty[]..** Ke
1c72c 79 77 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20 ywords include:
1c72d 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f in1, in2, in3, o
1c72e 75 74 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20 ut2_prerelease,
1c72f 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65 out2, out3. See
1c730 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 .** the mkopcode
1c731 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 h.awk script for
1c732 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f additional info
1c733 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 rmation..**.** D
1c734 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f ocumentation abo
1c735 75 74 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 ut VDBE opcodes
1c736 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 is generated by
1c737 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 scanning this fi
1c738 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 le.** for lines
1c739 6f 66 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 of that contain
1c73a 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 "Opcode:". That
1c73b 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 line and all su
1c73c 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d bsequent.** comm
1c73d 65 6e 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73 ent lines are us
1c73e 65 64 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61 ed in the genera
1c73f 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f tion of the opco
1c740 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 de.html document
1c741 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a ation.** file..*
1c742 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a *.** SUMMARY:.**
1c743 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69 .** Formatti
1c744 6e 67 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 ng is important
1c745 74 6f 20 73 63 72 69 70 74 73 20 74 68 61 74 20 to scripts that
1c746 73 63 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a scan this file..
1c747 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 ** Do not de
1c748 76 69 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66 viate from the f
1c749 6f 72 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20 ormatting style
1c74a 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 currently in use
1c74b 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1c74c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c74d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c74e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c74f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c750 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 **/../* Opcode:
1c751 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a Goto * P2 * * *
1c752 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 .**.** An uncond
1c753 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 itional jump to
1c754 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54 address P2..** T
1c755 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 he next instruct
1c756 69 6f 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c ion executed wil
1c757 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 l be .** the one
1c758 20 61 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f at index P2 fro
1c759 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 m the beginning
1c75a 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 of.** the progra
1c75b 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f m..*/.case OP_Go
1c75c 74 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 to: {
1c75d 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 /* jump */. C
1c75e 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 HECK_FOR_INTERRU
1c75f 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e PT;. pc = pOp->
1c760 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b p2 - 1;. break;
1c761 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 .}../* Opcode:
1c762 47 6f 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20 Gosub P1 P2 * *
1c763 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 *.**.** Write th
1c764 65 20 63 75 72 72 65 6e 74 20 61 64 64 72 65 73 e current addres
1c765 73 20 6f 6e 74 6f 20 72 65 67 69 73 74 65 72 20 s onto register
1c766 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a P1.** and then j
1c767 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50 ump to address P
1c768 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 2..*/.case OP_Go
1c769 73 75 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 sub: {
1c76a 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 /* jump */. a
1c76b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 ssert( pOp->p1>0
1c76c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1c76d 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p1<=p->nMem )
1c76e 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 ;. pIn1 = &p->a
1c76f 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1c770 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 assert( (pIn1->f
1c771 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d lags & MEM_Dyn)=
1c772 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c =0 );. pIn1->fl
1c773 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1c774 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b pIn1->u.i = pc;
1c775 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1c776 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 E(pOp->p1, pIn1)
1c777 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 ;. pc = pOp->p2
1c778 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d - 1;. break;.}
1c779 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 ../* Opcode: Re
1c77a 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a turn P1 * * * *.
1c77b 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 **.** Jump to th
1c77c 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1c77d 6f 6e 20 61 66 74 65 72 20 74 68 65 20 61 64 64 on after the add
1c77e 72 65 73 73 20 69 6e 20 72 65 67 69 73 74 65 72 ress in register
1c77f 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P1..*/.case OP_
1c780 52 65 74 75 72 6e 3a 20 7b 20 20 20 20 20 20 20 Return: {
1c781 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 /* in1 */.
1c782 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c assert( pIn1->fl
1c783 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b ags & MEM_Int );
1c784 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70 49 6e . pc = (int)pIn
1c785 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 1->u.i;. break;
1c786 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 .}../* Opcode:
1c787 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a Yield P1 * * * *
1c788 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 .**.** Swap the
1c789 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 program counter
1c78a 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 69 with the value i
1c78b 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a n register P1..*
1c78c 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a /.case OP_Yield:
1c78d 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1c78e 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f in1 */.#if 0 /
1c78f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1c790 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1c791 61 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 65 73 a */. int pcDes
1c792 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 t;.#endif /* loc
1c793 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1c794 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a ed into u.aa */.
1c795 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d assert( (pIn1-
1c796 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e >flags & MEM_Dyn
1c797 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e )==0 );. pIn1->
1c798 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1c799 0a 20 20 75 2e 61 61 2e 70 63 44 65 73 74 20 3d . u.aa.pcDest =
1c79a 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b (int)pIn1->u.i;
1c79b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 . pIn1->u.i = p
1c79c 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 c;. REGISTER_TR
1c79d 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e ACE(pOp->p1, pIn
1c79e 31 29 3b 0a 20 20 70 63 20 3d 20 75 2e 61 61 2e 1);. pc = u.aa.
1c79f 70 63 44 65 73 74 3b 0a 20 20 62 72 65 61 6b 3b pcDest;. break;
1c7a0 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 .}../* Opcode:
1c7a1 48 61 6c 74 49 66 4e 75 6c 6c 20 20 50 31 20 50 HaltIfNull P1 P
1c7a2 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1c7a3 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 Check the value
1c7a4 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 in register P3.
1c7a5 20 49 66 20 69 73 20 69 73 20 4e 55 4c 4c 20 74 If is is NULL t
1c7a6 68 65 6e 20 48 61 6c 74 20 75 73 69 6e 67 0a 2a hen Halt using.*
1c7a7 2a 20 70 61 72 61 6d 65 74 65 72 20 50 31 2c 20 * parameter P1,
1c7a8 50 32 2c 20 61 6e 64 20 50 34 20 61 73 20 69 66 P2, and P4 as if
1c7a9 20 74 68 69 73 20 77 65 72 65 20 61 20 48 61 6c this were a Hal
1c7aa 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 t instruction.
1c7ab 49 66 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 If the.** value
1c7ac 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 69 in register P3 i
1c7ad 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
1c7ae 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1c7af 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 a no-op..*/.cas
1c7b0 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 3a e OP_HaltIfNull:
1c7b1 20 7b 20 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a { /* in3 *
1c7b2 2f 0a 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 /. if( (pIn3->f
1c7b3 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1c7b4 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 2f ==0 ) break;. /
1c7b5 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 * Fall through i
1c7b6 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 2a 2f 0a 7d nto OP_Halt */.}
1c7b7 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 ../* Opcode: Ha
1c7b8 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a lt P1 P2 * P4 *.
1c7b9 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65 64 **.** Exit immed
1c7ba 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 65 iately. All ope
1c7bb 6e 20 63 75 72 73 6f 72 73 2c 20 65 74 63 20 61 n cursors, etc a
1c7bc 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 74 re closed.** aut
1c7bd 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a omatically..**.*
1c7be 2a 20 50 31 20 69 73 20 74 68 65 20 72 65 73 75 * P1 is the resu
1c7bf 6c 74 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 lt code returned
1c7c0 20 62 79 20 73 71 6c 69 74 65 33 5f 65 78 65 63 by sqlite3_exec
1c7c1 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 65 (), sqlite3_rese
1c7c2 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 t(),.** or sqlit
1c7c3 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20 20 e3_finalize().
1c7c4 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61 6c For a normal hal
1c7c5 74 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20 62 t, this should b
1c7c6 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2e e SQLITE_OK (0).
1c7c7 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c 20 .** For errors,
1c7c8 69 74 20 63 61 6e 20 62 65 20 73 6f 6d 65 20 6f it can be some o
1c7c9 74 68 65 72 20 76 61 6c 75 65 2e 20 20 49 66 20 ther value. If
1c7ca 50 31 21 3d 30 20 74 68 65 6e 20 50 32 20 77 69 P1!=0 then P2 wi
1c7cb 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 ll determine.**
1c7cc 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
1c7cd 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 63 o rollback the c
1c7ce 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
1c7cf 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c 6c on. Do not roll
1c7d0 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d 4f back.** if P2==O
1c7d1 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68 65 20 72 E_Fail. Do the r
1c7d2 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d 4f ollback if P2==O
1c7d3 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 20 E_Rollback. If
1c7d4 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a 2a P2==OE_Abort,.**
1c7d5 20 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20 61 then back out a
1c7d6 6c 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 ll changes that
1c7d7 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 64 75 have occurred du
1c7d8 72 69 6e 67 20 74 68 69 73 20 65 78 65 63 75 74 ring this execut
1c7d9 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 ion of the.** VD
1c7da 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 72 BE, but do not r
1c7db 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e ollback the tran
1c7dc 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 saction. .**.**
1c7dd 49 66 20 50 34 20 69 73 20 6e 6f 74 20 6e 75 6c If P4 is not nul
1c7de 6c 20 74 68 65 6e 20 69 74 20 69 73 20 61 6e 20 l then it is an
1c7df 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 error message st
1c7e0 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 ring..**.** Ther
1c7e1 65 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64 20 e is an implied
1c7e2 22 48 61 6c 74 20 30 20 30 20 30 22 20 69 6e 73 "Halt 0 0 0" ins
1c7e3 74 72 75 63 74 69 6f 6e 20 69 6e 73 65 72 74 65 truction inserte
1c7e4 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e d at the very en
1c7e5 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70 72 d of.** every pr
1c7e6 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75 6d ogram. So a jum
1c7e7 70 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 p past the last
1c7e8 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 instruction of t
1c7e9 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 73 he program.** is
1c7ea 20 74 68 65 20 73 61 6d 65 20 61 73 20 65 78 65 the same as exe
1c7eb 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f 0a cuting Halt..*/.
1c7ec 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b 0a case OP_Halt: {.
1c7ed 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 if( pOp->p1==S
1c7ee 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 70 QLITE_OK && p->p
1c7ef 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 Frame ){. /*
1c7f0 48 61 6c 74 20 74 68 65 20 73 75 62 2d 70 72 6f Halt the sub-pro
1c7f1 67 72 61 6d 2e 20 52 65 74 75 72 6e 20 63 6f 6e gram. Return con
1c7f2 74 72 6f 6c 20 74 6f 20 74 68 65 20 70 61 72 65 trol to the pare
1c7f3 6e 74 20 66 72 61 6d 65 2e 20 2a 2f 0a 20 20 20 nt frame. */.
1c7f4 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 VdbeFrame *pFra
1c7f5 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a me = p->pFrame;.
1c7f6 20 20 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 p->pFrame =
1c7f7 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b pFrame->pParent;
1c7f8 0a 20 20 20 20 70 2d 3e 6e 46 72 61 6d 65 2d 2d . p->nFrame--
1c7f9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1c7fa 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 eSetChanges(db,
1c7fb 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 p->nChange);.
1c7fc 20 70 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 pc = sqlite3Vdb
1c7fd 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 eFrameRestore(pF
1c7fe 72 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 rame);. if( p
1c7ff 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 67 6e 6f 72 Op->p2==OE_Ignor
1c800 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e e ){. /* In
1c801 73 74 72 75 63 74 69 6f 6e 20 70 63 20 69 73 20 struction pc is
1c802 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 74 the OP_Program t
1c803 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 hat invoked the
1c804 73 75 62 2d 70 72 6f 67 72 61 6d 20 0a 20 20 20 sub-program .
1c805 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 ** currently
1c806 62 65 69 6e 67 20 68 61 6c 74 65 64 2e 20 49 66 being halted. If
1c807 20 74 68 65 20 70 32 20 69 6e 73 74 72 75 63 74 the p2 instruct
1c808 69 6f 6e 20 6f 66 20 74 68 69 73 20 4f 50 5f 48 ion of this OP_H
1c809 61 6c 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 alt. ** ins
1c80a 74 72 75 63 74 69 6f 6e 20 69 73 20 73 65 74 20 truction is set
1c80b 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 74 68 to OE_Ignore, th
1c80c 65 6e 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 en the sub-progr
1c80d 61 6d 20 69 73 20 74 68 72 6f 77 69 6e 67 0a 20 am is throwing.
1c80e 20 20 20 20 20 2a 2a 20 61 6e 20 49 47 4e 4f 52 ** an IGNOR
1c80f 45 20 65 78 63 65 70 74 69 6f 6e 2e 20 49 6e 20 E exception. In
1c810 74 68 69 73 20 63 61 73 65 20 6a 75 6d 70 20 74 this case jump t
1c811 6f 20 74 68 65 20 61 64 64 72 65 73 73 20 73 70 o the address sp
1c812 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 2a 2a ecified. **
1c813 20 61 73 20 74 68 65 20 70 32 20 6f 66 20 74 68 as the p2 of th
1c814 65 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f e calling OP_Pro
1c815 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20 20 20 20 gram. */.
1c816 70 63 20 3d 20 70 2d 3e 61 4f 70 5b 70 63 5d 2e pc = p->aOp[pc].
1c817 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 p2-1;. }.
1c818 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 70 2d break;. }.. p-
1c819 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 >rc = pOp->p1;.
1c81a 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 p->errorAction
1c81b 3d 20 28 75 38 29 70 4f 70 2d 3e 70 32 3b 0a 20 = (u8)pOp->p2;.
1c81c 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 69 p->pc = pc;. i
1c81d 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a f( pOp->p4.z ){.
1c81e 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1c81f 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1c820 2c 20 64 62 2c 20 22 25 73 22 2c 20 70 4f 70 2d , db, "%s", pOp-
1c821 3e 70 34 2e 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 >p4.z);. }. rc
1c822 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 = sqlite3VdbeHa
1c823 6c 74 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 lt(p);. assert(
1c824 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
1c825 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f || rc==SQLITE_O
1c826 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 K );. if( rc==S
1c827 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
1c828 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 p->rc = rc = S
1c829 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 QLITE_BUSY;. }e
1c82a 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 2d lse{. rc = p-
1c82b 3e 72 63 20 3f 20 53 51 4c 49 54 45 5f 45 52 52 >rc ? SQLITE_ERR
1c82c 4f 52 20 3a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 OR : SQLITE_DONE
1c82d 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 ;. }. goto vdb
1c82e 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 e_return;.}../*
1c82f 4f 70 63 6f 64 65 3a 20 49 6e 74 65 67 65 72 20 Opcode: Integer
1c830 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1c831 2a 20 54 68 65 20 33 32 2d 62 69 74 20 69 6e 74 * The 32-bit int
1c832 65 67 65 72 20 76 61 6c 75 65 20 50 31 20 69 73 eger value P1 is
1c833 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 72 65 written into re
1c834 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 gister P2..*/.ca
1c835 73 65 20 4f 50 5f 49 6e 74 65 67 65 72 3a 20 7b se OP_Integer: {
1c836 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 /* out2
1c837 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 -prerelease */.
1c838 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1c839 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e EM_Int;. pOut->
1c83a 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 u.i = pOp->p1;.
1c83b 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1c83c 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 32 code: Int64 * P2
1c83d 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 * P4 *.**.** P4
1c83e 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1c83f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 a 64-bit intege
1c840 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 r value..** Writ
1c841 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 e that value int
1c842 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a o register P2..*
1c843 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a /.case OP_Int64:
1c844 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1c845 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1c846 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1c847 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b 0a ->p4.pI64!=0 );.
1c848 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 pOut->flags =
1c849 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d MEM_Int;. pOut-
1c84a 3e 75 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e >u.i = *pOp->p4.
1c84b 70 49 36 34 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d pI64;. break;.}
1c84c 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 ../* Opcode: Rea
1c84d 6c 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a l * P2 * P4 *.**
1c84e 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e .** P4 is a poin
1c84f 74 65 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 ter to a 64-bit
1c850 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
1c851 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 alue..** Write t
1c852 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 hat value into r
1c853 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 egister P2..*/.c
1c854 61 73 65 20 4f 50 5f 52 65 61 6c 3a 20 7b 20 20 ase OP_Real: {
1c855 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1c856 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 2c 20 6f e as TK_FLOAT, o
1c857 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1c858 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 /. pOut->flags
1c859 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 61 73 = MEM_Real;. as
1c85a 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 sert( !sqlite3Is
1c85b 4e 61 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 NaN(*pOp->p4.pRe
1c85c 61 6c 29 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 72 al) );. pOut->r
1c85d 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 = *pOp->p4.pRea
1c85e 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f l;. break;.}../
1c85f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 * Opcode: String
1c860 38 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 8 * P2 * P4 *.**
1c861 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f .** P4 points to
1c862 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 a nul terminate
1c863 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 20 d UTF-8 string.
1c864 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 This opcode is t
1c865 72 61 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20 69 ransformed .** i
1c866 6e 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e 67 nto an OP_String
1c867 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 65 78 before it is ex
1c868 65 63 75 74 65 64 20 66 6f 72 20 74 68 65 20 66 ecuted for the f
1c869 69 72 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63 61 irst time..*/.ca
1c86a 73 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20 7b se OP_String8: {
1c86b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1c86c 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20 6f as TK_STRING, o
1c86d 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1c86e 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
1c86f 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f >p4.z!=0 );. pO
1c870 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 53 p->opcode = OP_S
1c871 74 72 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70 31 tring;. pOp->p1
1c872 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
1c873 33 30 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 0a 30(pOp->p4.z);..
1c874 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1c875 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 MIT_UTF16. if(
1c876 65 6e 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54 45 encoding!=SQLITE
1c877 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 72 63 20 _UTF8 ){. rc
1c878 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d = sqlite3VdbeMem
1c879 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70 SetStr(pOut, pOp
1c87a 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 ->p4.z, -1, SQLI
1c87b 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
1c87c 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69 66 28 STATIC);. if(
1c87d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42 rc==SQLITE_TOOB
1c87e 49 47 20 29 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 IG ) goto too_bi
1c87f 67 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 g;. if( SQLIT
1c880 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 E_OK!=sqlite3Vdb
1c881 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1c882 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67 29 20 pOut, encoding)
1c883 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 ) goto no_mem;.
1c884 20 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d assert( pOut-
1c885 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4f 75 74 2d 3e >zMalloc==pOut->
1c886 7a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 z );. assert(
1c887 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 20 4d pOut->flags & M
1c888 45 4d 5f 44 79 6e 20 29 3b 0a 20 20 20 20 70 4f EM_Dyn );. pO
1c889 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b ut->zMalloc = 0;
1c88a 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 . pOut->flags
1c88b 20 7c 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0a |= MEM_Static;.
1c88c 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 pOut->flags
1c88d 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 &= ~MEM_Dyn;.
1c88e 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 if( pOp->p4type
1c88f 3d 3d 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0a ==P4_DYNAMIC ){.
1c890 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1c891 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34 2e ree(db, pOp->p4.
1c892 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f z);. }. pO
1c893 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 p->p4type = P4_D
1c894 59 4e 41 4d 49 43 3b 0a 20 20 20 20 70 4f 70 2d YNAMIC;. pOp-
1c895 3e 70 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e 7a 3b >p4.z = pOut->z;
1c896 0a 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 . pOp->p1 = p
1c897 4f 75 74 2d 3e 6e 3b 0a 20 20 7d 0a 23 65 6e 64 Out->n;. }.#end
1c898 69 66 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 if. if( pOp->p1
1c899 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
1c89a 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
1c89b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f ){. goto too
1c89c 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 _big;. }. /* F
1c89d 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 all through to t
1c89e 68 65 20 6e 65 78 74 20 63 61 73 65 2c 20 4f 50 he next case, OP
1c89f 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20 20 0a _String */.}. .
1c8a0 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e /* Opcode: Strin
1c8a1 67 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a g P1 P2 * P4 *.*
1c8a2 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 *.** The string
1c8a3 76 61 6c 75 65 20 50 34 20 6f 66 20 6c 65 6e 67 value P4 of leng
1c8a4 74 68 20 50 31 20 28 62 79 74 65 73 29 20 69 73 th P1 (bytes) is
1c8a5 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1c8a6 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 ter P2..*/.case
1c8a7 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 20 20 20 20 OP_String: {
1c8a8 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1c8a9 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 erelease */. as
1c8aa 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 sert( pOp->p4.z!
1c8ab 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c =0 );. pOut->fl
1c8ac 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 ags = MEM_Str|ME
1c8ad 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54 65 72 M_Static|MEM_Ter
1c8ae 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d 20 70 m;. pOut->z = p
1c8af 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f 75 74 Op->p4.z;. pOut
1c8b0 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 ->n = pOp->p1;.
1c8b1 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 pOut->enc = enc
1c8b2 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f oding;. UPDATE_
1c8b3 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 MAX_BLOBSIZE(pOu
1c8b4 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1c8b5 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 20 /* Opcode: Null
1c8b6 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a * P2 * * *.**.**
1c8b7 20 57 72 69 74 65 20 61 20 4e 55 4c 4c 20 69 6e Write a NULL in
1c8b8 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1c8b9 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a */.case OP_Null:
1c8ba 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1c8bb 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1c8bc 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a */. break;.}...
1c8bd 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 20 /* Opcode: Blob
1c8be 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a P1 P2 * P4.**.**
1c8bf 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 P4 points to a
1c8c0 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50 31 20 blob of data P1
1c8c1 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74 6f bytes long. Sto
1c8c2 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62 20 re this.** blob
1c8c3 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 in register P2.
1c8c4 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1c8c5 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20 64 69 is not coded di
1c8c6 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68 65 rectly.** by the
1c8c7 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74 65 compiler. Inste
1c8c8 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 ad, the compiler
1c8c9 20 6c 61 79 65 72 20 73 70 65 63 69 66 69 65 73 layer specifies
1c8ca 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c 6f .** an OP_HexBlo
1c8cb 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68 20 74 b opcode, with t
1c8cc 68 65 20 68 65 78 20 73 74 72 69 6e 67 20 72 65 he hex string re
1c8cd 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 0a presentation of.
1c8ce 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73 20 50 ** the blob as P
1c8cf 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 4. This opcode i
1c8d0 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 74 6f s transformed to
1c8d1 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 74 an OP_Blob.** t
1c8d2 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 he first time it
1c8d3 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2f is executed..*/
1c8d4 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20 7b .case OP_Blob: {
1c8d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c8d6 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1c8d7 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 se */. assert(
1c8d8 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49 54 pOp->p1 <= SQLIT
1c8d9 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a E_MAX_LENGTH );.
1c8da 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1c8db 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70 SetStr(pOut, pOp
1c8dc 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31 2c ->p4.z, pOp->p1,
1c8dd 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d 3e 0, 0);. pOut->
1c8de 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a enc = encoding;.
1c8df 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1c8e0 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 BSIZE(pOut);. b
1c8e1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1c8e2 64 65 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20 de: Variable P1
1c8e3 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 P3 P4 *.**.**
1c8e4 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 76 61 Transfer the va
1c8e5 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64 20 70 61 lues of bound pa
1c8e6 72 61 6d 65 74 65 72 73 20 50 31 2e 2e 50 31 2b rameters P1..P1+
1c8e7 50 33 2d 31 20 69 6e 74 6f 20 72 65 67 69 73 74 P3-1 into regist
1c8e8 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 32 2b 50 33 ers.** P2..P2+P3
1c8e9 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 -1..**.** If the
1c8ea 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 61 parameter is na
1c8eb 6d 65 64 2c 20 74 68 65 6e 20 69 74 73 20 6e 61 med, then its na
1c8ec 6d 65 20 61 70 70 65 61 72 73 20 69 6e 20 50 34 me appears in P4
1c8ed 20 61 6e 64 20 50 33 3d 3d 31 2e 0a 2a 2a 20 54 and P3==1..** T
1c8ee 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20 75 he P4 value is u
1c8ef 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 sed by sqlite3_b
1c8f0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 ind_parameter_na
1c8f1 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 me()..*/.case OP
1c8f2 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a 23 69 66 _Variable: {.#if
1c8f3 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1c8f4 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1c8f5 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 69 6e 74 20 o u.ab */. int
1c8f6 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p1; /*
1c8f7 56 61 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 79 Variable to copy
1c8f8 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 from */. int p
1c8f9 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2; /* R
1c8fa 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 egister to copy
1c8fb 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 to */. int n;
1c8fc 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1c8fd 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 er of values lef
1c8fe 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 4d t to copy */. M
1c8ff 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20 em *pVar;
1c900 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e 67 20 74 /* Value being t
1c901 72 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a 23 65 ransferred */.#e
1c902 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1c903 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1c904 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a 20 20 75 2e to u.ab */.. u.
1c905 61 62 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 20 ab.p1 = pOp->p1
1c906 2d 20 31 3b 0a 20 20 75 2e 61 62 2e 70 32 20 3d - 1;. u.ab.p2 =
1c907 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 62 pOp->p2;. u.ab
1c908 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 .n = pOp->p3;.
1c909 61 73 73 65 72 74 28 20 75 2e 61 62 2e 70 31 3e assert( u.ab.p1>
1c90a 3d 30 20 26 26 20 75 2e 61 62 2e 70 31 2b 75 2e =0 && u.ab.p1+u.
1c90b 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b ab.n<=p->nVar );
1c90c 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e . assert( u.ab.
1c90d 70 32 3e 3d 31 20 26 26 20 75 2e 61 62 2e 70 32 p2>=1 && u.ab.p2
1c90e 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 2d 3e 6e 4d +u.ab.n-1<=p->nM
1c90f 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 em );. assert(
1c910 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c 7c 20 pOp->p4.z==0 ||
1c911 70 4f 70 2d 3e 70 33 3d 3d 31 20 29 3b 0a 0a 20 pOp->p3==1 );..
1c912 20 77 68 69 6c 65 28 20 75 2e 61 62 2e 6e 2d 2d while( u.ab.n--
1c913 20 3e 20 30 20 29 7b 0a 20 20 20 20 75 2e 61 62 > 0 ){. u.ab
1c914 2e 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 .pVar = &p->aVar
1c915 5b 75 2e 61 62 2e 70 31 2b 2b 5d 3b 0a 20 20 20 [u.ab.p1++];.
1c916 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
1c917 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 61 62 2e 70 MemTooBig(u.ab.p
1c918 56 61 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f Var) ){. go
1c919 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 to too_big;.
1c91a 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d }. pOut = &p-
1c91b 3e 61 4d 65 6d 5b 75 2e 61 62 2e 70 32 2b 2b 5d >aMem[u.ab.p2++]
1c91c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1c91d 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
1c91e 6e 61 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 70 nal(pOut);. p
1c91f 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Out->flags = MEM
1c920 5f 4e 75 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 _Null;. sqlit
1c921 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
1c922 43 6f 70 79 28 70 4f 75 74 2c 20 75 2e 61 62 2e Copy(pOut, u.ab.
1c923 70 56 61 72 2c 20 4d 45 4d 5f 53 74 61 74 69 63 pVar, MEM_Static
1c924 29 3b 0a 20 20 20 20 55 50 44 41 54 45 5f 4d 41 );. UPDATE_MA
1c925 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 X_BLOBSIZE(pOut)
1c926 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1c927 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 ../* Opcode: Mov
1c928 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a e P1 P2 P3 * *.*
1c929 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 76 61 *.** Move the va
1c92a 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 lues in register
1c92b 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 6f 76 65 P1..P1+P3-1 ove
1c92c 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 r into.** regist
1c92d 65 72 73 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e ers P2..P2+P3-1.
1c92e 20 20 52 65 67 69 73 74 65 72 73 20 50 31 2e 2e Registers P1..
1c92f 50 31 2b 50 31 2d 31 20 61 72 65 0a 2a 2a 20 6c P1+P1-1 are.** l
1c930 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55 eft holding a NU
1c931 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72 LL. It is an er
1c932 72 6f 72 20 66 6f 72 20 72 65 67 69 73 74 65 72 ror for register
1c933 20 72 61 6e 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 ranges.** P1..P
1c934 31 2b 50 33 2d 31 20 61 6e 64 20 50 32 2e 2e 50 1+P3-1 and P2..P
1c935 32 2b 50 33 2d 31 20 74 6f 20 6f 76 65 72 6c 61 2+P3-1 to overla
1c936 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f p..*/.case OP_Mo
1c937 76 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 ve: {.#if 0 /*
1c938 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1c939 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 moved into u.ac
1c93a 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c */. char *zMall
1c93b 6f 63 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 oc; /* Holding
1c93c 20 76 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c variable for al
1c93d 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a located memory *
1c93e 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 /. int n;
1c93f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c940 66 20 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 f registers left
1c941 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e to copy */. in
1c942 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f t p1; /
1c943 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f * Register to co
1c944 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 py from */. int
1c945 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a p2; /*
1c946 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 Register to cop
1c947 79 20 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f y to */.#endif /
1c948 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1c949 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1c94a 63 20 2a 2f 0a 0a 20 20 75 2e 61 63 2e 6e 20 3d c */.. u.ac.n =
1c94b 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 63 pOp->p3;. u.ac
1c94c 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 .p1 = pOp->p1;.
1c94d 20 75 2e 61 63 2e 70 32 20 3d 20 70 4f 70 2d 3e u.ac.p2 = pOp->
1c94e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e p2;. assert( u.
1c94f 61 63 2e 6e 3e 30 20 26 26 20 75 2e 61 63 2e 70 ac.n>0 && u.ac.p
1c950 31 3e 30 20 26 26 20 75 2e 61 63 2e 70 32 3e 30 1>0 && u.ac.p2>0
1c951 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1c952 61 63 2e 70 31 2b 75 2e 61 63 2e 6e 3c 3d 75 2e ac.p1+u.ac.n<=u.
1c953 61 63 2e 70 32 20 7c 7c 20 75 2e 61 63 2e 70 32 ac.p2 || u.ac.p2
1c954 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 31 +u.ac.n<=u.ac.p1
1c955 20 29 3b 0a 0a 20 20 70 49 6e 31 20 3d 20 26 70 );.. pIn1 = &p
1c956 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e 70 31 5d 3b ->aMem[u.ac.p1];
1c957 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1c958 65 6d 5b 75 2e 61 63 2e 70 32 5d 3b 0a 20 20 77 em[u.ac.p2];. w
1c959 68 69 6c 65 28 20 75 2e 61 63 2e 6e 2d 2d 20 29 hile( u.ac.n-- )
1c95a 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f {. assert( pO
1c95b 75 74 3c 3d 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e ut<=&p->aMem[p->
1c95c 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 61 73 73 nMem] );. ass
1c95d 65 72 74 28 20 70 49 6e 31 3c 3d 26 70 2d 3e 61 ert( pIn1<=&p->a
1c95e 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a Mem[p->nMem] );.
1c95f 20 20 20 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 u.ac.zMalloc
1c960 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 = pOut->zMalloc
1c961 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c ;. pOut->zMal
1c962 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c loc = 0;. sql
1c963 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
1c964 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 20 pOut, pIn1);.
1c965 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d pIn1->zMalloc =
1c966 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 u.ac.zMalloc;.
1c967 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 REGISTER_TRAC
1c968 45 28 75 2e 61 63 2e 70 32 2b 2b 2c 20 70 4f 75 E(u.ac.p2++, pOu
1c969 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a t);. pIn1++;.
1c96a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a pOut++;. }.
1c96b 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1c96c 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50 pcode: Copy P1 P
1c96d 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 2 * * *.**.** Ma
1c96e 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72 65 67 ke a copy of reg
1c96f 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 ister P1 into re
1c970 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a gister P2..**.**
1c971 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1c972 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70 20 63 n makes a deep c
1c973 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 opy of the value
1c974 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65 0a 2a . A duplicate.*
1c975 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61 6e 79 * is made of any
1c976 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 string or blob
1c977 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 20 61 constant. See a
1c978 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f lso OP_SCopy..*/
1c979 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b .case OP_Copy: {
1c97a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c97b 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 in1 */. assert(
1c97c 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 pOp->p2>0 );.
1c97d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c assert( pOp->p2<
1c97e 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f =p->nMem );. pO
1c97f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ut = &p->aMem[pO
1c980 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 p->p2];. assert
1c981 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a ( pOut!=pIn1 );.
1c982 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1c983 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 ShallowCopy(pOut
1c984 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 , pIn1, MEM_Ephe
1c985 6d 29 3b 0a 20 20 44 65 65 70 68 65 6d 65 72 61 m);. Deephemera
1c986 6c 69 7a 65 28 70 4f 75 74 29 3b 0a 20 20 52 45 lize(pOut);. RE
1c987 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1c988 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 ->p2, pOut);. b
1c989 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1c98a 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50 32 20 de: SCopy P1 P2
1c98b 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 * * *.**.** Make
1c98c 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 a shallow copy
1c98d 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 of register P1 i
1c98e 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e nto register P2.
1c98f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 .**.** This inst
1c990 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20 ruction makes a
1c991 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 shallow copy of
1c992 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66 20 74 the value. If t
1c993 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 61 he value.** is a
1c994 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 2c string or blob,
1c995 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79 20 69 then the copy i
1c996 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72 s only a pointer
1c997 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 to the.** origi
1c998 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20 69 66 nal and hence if
1c999 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 68 the original ch
1c99a 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20 74 68 anges so will th
1c99b 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72 73 65 e copy..** Worse
1c99c 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 , if the origina
1c99d 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74 65 64 l is deallocated
1c99e 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63 6f 6d , the copy becom
1c99f 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 20 54 es invalid..** T
1c9a0 68 75 73 20 74 68 65 20 70 72 6f 67 72 61 6d 20 hus the program
1c9a1 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 must guarantee t
1c9a2 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c hat the original
1c9a3 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e 67 65 will not change
1c9a4 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 6c .** during the l
1c9a5 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 63 ifetime of the c
1c9a6 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43 6f 70 opy. Use OP_Cop
1c9a7 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 6d 70 y to make a comp
1c9a8 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a 2a 2f lete.** copy..*/
1c9a9 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79 3a 20 .case OP_SCopy:
1c9aa 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1c9ab 69 6e 31 20 2a 2f 0a 20 20 52 45 47 49 53 54 45 in1 */. REGISTE
1c9ac 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c R_TRACE(pOp->p1,
1c9ad 20 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74 pIn1);. assert
1c9ae 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 ( pOp->p2>0 );.
1c9af 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1c9b0 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
1c9b1 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Out = &p->aMem[p
1c9b2 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 Op->p2];. asser
1c9b3 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b t( pOut!=pIn1 );
1c9b4 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1c9b5 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 mShallowCopy(pOu
1c9b6 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 t, pIn1, MEM_Eph
1c9b7 65 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f em);. REGISTER_
1c9b8 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
1c9b9 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1c9ba 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 ../* Opcode: Res
1c9bb 75 6c 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a ultRow P1 P2 * *
1c9bc 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 *.**.** The reg
1c9bd 69 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 isters P1 throug
1c9be 68 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 h P1+P2-1 contai
1c9bf 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f n a single row o
1c9c0 66 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68 f.** results. Th
1c9c1 69 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 is opcode causes
1c9c2 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 the sqlite3_ste
1c9c3 70 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d p() call to term
1c9c4 69 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e inate.** with an
1c9c5 20 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75 SQLITE_ROW retu
1c9c6 72 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73 rn code and it s
1c9c7 65 74 73 20 75 70 20 74 68 65 20 73 71 6c 69 74 ets up the sqlit
1c9c8 65 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63 e3_stmt.** struc
1c9c9 74 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20 ture to provide
1c9ca 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f access to the to
1c9cb 70 20 50 31 20 76 61 6c 75 65 73 20 61 73 20 74 p P1 values as t
1c9cc 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 he result.** row
1c9cd 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 ..*/.case OP_Res
1c9ce 75 6c 74 52 6f 77 3a 20 7b 0a 23 69 66 20 30 20 ultRow: {.#if 0
1c9cf 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1c9d0 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1c9d1 2e 61 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d .ad */. Mem *pM
1c9d2 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 23 65 6e em;. int i;.#en
1c9d3 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1c9d4 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1c9d5 6f 20 75 2e 61 64 20 2a 2f 0a 20 20 61 73 73 65 o u.ad */. asse
1c9d6 72 74 28 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d rt( p->nResColum
1c9d7 6e 3d 3d 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 n==pOp->p2 );.
1c9d8 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1c9d9 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1c9da 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d Op->p1+pOp->p2<=
1c9db 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 p->nMem+1 );..
1c9dc 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 /* If the SQLITE
1c9dd 5f 43 6f 75 6e 74 52 6f 77 73 20 66 6c 61 67 20 _CountRows flag
1c9de 69 73 20 73 65 74 20 69 6e 20 73 71 6c 69 74 65 is set in sqlite
1c9df 33 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 3.flags mask, th
1c9e0 65 6e 0a 20 20 2a 2a 20 44 4d 4c 20 73 74 61 74 en. ** DML stat
1c9e1 65 6d 65 6e 74 73 20 69 6e 76 6f 6b 65 20 74 68 ements invoke th
1c9e2 69 73 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 74 is opcode to ret
1c9e3 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1c9e4 66 20 72 6f 77 73 0a 20 20 2a 2a 20 6d 6f 64 69 f rows. ** modi
1c9e5 66 69 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 fied to the user
1c9e6 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e . This is the on
1c9e7 6c 79 20 77 61 79 20 74 68 61 74 20 61 20 56 4d ly way that a VM
1c9e8 20 74 68 61 74 0a 20 20 2a 2a 20 6f 70 65 6e 73 that. ** opens
1c9e9 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 a statement tra
1c9ea 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 69 6e 76 nsaction may inv
1c9eb 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65 2e oke this opcode.
1c9ec 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 61 . **. ** In ca
1c9ed 73 65 20 74 68 69 73 20 69 73 20 73 75 63 68 20 se this is such
1c9ee 61 20 73 74 61 74 65 6d 65 6e 74 2c 20 63 6c 6f a statement, clo
1c9ef 73 65 20 61 6e 79 20 73 74 61 74 65 6d 65 6e 74 se any statement
1c9f0 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a transaction. *
1c9f1 2a 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 * opened by this
1c9f2 20 56 4d 20 62 65 66 6f 72 65 20 72 65 74 75 72 VM before retur
1c9f3 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 ning control to
1c9f4 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 the user. This i
1c9f5 73 20 74 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 65 s to. ** ensure
1c9f6 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 2d that statement-
1c9f7 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 transactions are
1c9f8 20 61 6c 77 61 79 73 20 6e 65 73 74 65 64 2c 20 always nested,
1c9f9 6e 6f 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 2e not overlapping.
1c9fa 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 . ** If the ope
1c9fb 6e 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e n statement-tran
1c9fc 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 saction is not c
1c9fd 6c 6f 73 65 64 20 68 65 72 65 2c 20 74 68 65 6e losed here, then
1c9fe 20 74 68 65 20 75 73 65 72 0a 20 20 2a 2a 20 6d the user. ** m
1c9ff 61 79 20 73 74 65 70 20 61 6e 6f 74 68 65 72 20 ay step another
1ca00 56 4d 20 74 68 61 74 20 6f 70 65 6e 73 20 69 74 VM that opens it
1ca01 73 20 6f 77 6e 20 73 74 61 74 65 6d 65 6e 74 20 s own statement
1ca02 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 transaction. Thi
1ca03 73 0a 20 20 2a 2a 20 6d 61 79 20 6c 65 61 64 20 s. ** may lead
1ca04 74 6f 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 73 to overlapping s
1ca05 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1ca06 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a tions.. **. **
1ca07 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 The statement t
1ca08 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 65 ransaction is ne
1ca09 76 65 72 20 61 20 74 6f 70 2d 6c 65 76 65 6c 20 ver a top-level
1ca0a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 48 65 transaction. He
1ca0b 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 52 45 4c nce. ** the REL
1ca0c 45 41 53 45 20 63 61 6c 6c 20 62 65 6c 6f 77 20 EASE call below
1ca0d 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 0a can never fail..
1ca0e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
1ca0f 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 ->iStatement==0
1ca10 7c 7c 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c || db->flags&SQL
1ca11 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 3b ITE_CountRows );
1ca12 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 . rc = sqlite3V
1ca13 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e dbeCloseStatemen
1ca14 74 28 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 t(p, SAVEPOINT_R
1ca15 45 4c 45 41 53 45 29 3b 0a 20 20 69 66 28 20 4e ELEASE);. if( N
1ca16 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f EVER(rc!=SQLITE_
1ca17 4f 4b 29 20 29 7b 0a 20 20 20 20 62 72 65 61 6b OK) ){. break
1ca18 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 76 61 ;. }.. /* Inva
1ca19 6c 69 64 61 74 65 20 61 6c 6c 20 65 70 68 65 6d lidate all ephem
1ca1a 65 72 61 6c 20 63 75 72 73 6f 72 20 72 6f 77 20 eral cursor row
1ca1b 63 61 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63 caches */. p->c
1ca1c 61 63 68 65 43 74 72 20 3d 20 28 70 2d 3e 63 61 acheCtr = (p->ca
1ca1d 63 68 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a cheCtr + 2)|1;..
1ca1e 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 /* Make sure t
1ca1f 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 he results of th
1ca20 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 61 72 e current row ar
1ca21 65 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 e \000 terminate
1ca22 64 0a 20 20 2a 2a 20 61 6e 64 20 68 61 76 65 20 d. ** and have
1ca23 61 6e 20 61 73 73 69 67 6e 65 64 20 74 79 70 65 an assigned type
1ca24 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 61 . The results a
1ca25 72 65 20 64 65 2d 65 70 68 65 6d 65 72 61 6c 69 re de-ephemerali
1ca26 7a 65 64 20 61 73 0a 20 20 2a 2a 20 61 73 20 73 zed as. ** as s
1ca27 69 64 65 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f ide effect.. */
1ca28 0a 20 20 75 2e 61 64 2e 70 4d 65 6d 20 3d 20 70 . u.ad.pMem = p
1ca29 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 ->pResultSet = &
1ca2a 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d p->aMem[pOp->p1]
1ca2b 3b 0a 20 20 66 6f 72 28 75 2e 61 64 2e 69 3d 30 ;. for(u.ad.i=0
1ca2c 3b 20 75 2e 61 64 2e 69 3c 70 4f 70 2d 3e 70 32 ; u.ad.i<pOp->p2
1ca2d 3b 20 75 2e 61 64 2e 69 2b 2b 29 7b 0a 20 20 20 ; u.ad.i++){.
1ca2e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
1ca2f 75 6c 54 65 72 6d 69 6e 61 74 65 28 26 75 2e 61 ulTerminate(&u.a
1ca30 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b d.pMem[u.ad.i]);
1ca31 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e . storeTypeIn
1ca32 66 6f 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e fo(&u.ad.pMem[u.
1ca33 61 64 2e 69 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 ad.i], encoding)
1ca34 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 ;. REGISTER_T
1ca35 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 75 2e 61 RACE(pOp->p1+u.a
1ca36 64 2e 69 2c 20 26 75 2e 61 64 2e 70 4d 65 6d 5b d.i, &u.ad.pMem[
1ca37 75 2e 61 64 2e 69 5d 29 3b 0a 20 20 7d 0a 20 20 u.ad.i]);. }.
1ca38 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1ca39 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d iled ) goto no_m
1ca3a 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e em;.. /* Return
1ca3b 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 20 2a 2f SQLITE_ROW. */
1ca3c 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 20 2b 20 . p->pc = pc +
1ca3d 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 1;. rc = SQLITE
1ca3e 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 76 64 62 _ROW;. goto vdb
1ca3f 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 e_return;.}../*
1ca40 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 74 20 50 Opcode: Concat P
1ca41 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1ca42 2a 20 41 64 64 20 74 68 65 20 74 65 78 74 20 69 * Add the text i
1ca43 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 6f 6e n register P1 on
1ca44 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 to the end of th
1ca45 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 72 65 67 e text in.** reg
1ca46 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f ister P2 and sto
1ca47 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1ca48 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
1ca49 20 49 66 20 65 69 74 68 65 72 20 74 68 65 20 50 If either the P
1ca4a 31 20 6f 72 20 50 32 20 74 65 78 74 20 61 72 65 1 or P2 text are
1ca4b 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 NULL then store
1ca4c 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a 0a NULL in P3..**.
1ca4d 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 7c 7c 20 ** P3 = P2 ||
1ca4e 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 P1.**.** It is i
1ca4f 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 20 61 6e llegal for P1 an
1ca50 64 20 50 33 20 74 6f 20 62 65 20 74 68 65 20 73 d P3 to be the s
1ca51 61 6d 65 20 72 65 67 69 73 74 65 72 2e 20 53 6f ame register. So
1ca52 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 50 metimes,.** if P
1ca53 33 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 3 is the same re
1ca54 67 69 73 74 65 72 20 61 73 20 50 32 2c 20 74 68 gister as P2, th
1ca55 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
1ca56 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 is able.** to a
1ca57 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 28 29 2e void a memcpy().
1ca58 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 63 .*/.case OP_Conc
1ca59 61 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 at: {
1ca5a 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f /* same as TK_CO
1ca5b 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 NCAT, in1, in2,
1ca5c 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f out3 */.#if 0 /
1ca5d 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1ca5e 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1ca5f 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 e */. i64 nByte
1ca60 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1ca61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ca62 64 20 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a 0a d into u.ae */..
1ca63 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21 3d assert( pIn1!=
1ca64 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28 70 pOut );. if( (p
1ca65 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e In1->flags | pIn
1ca66 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 2->flags) & MEM_
1ca67 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 Null ){. sqli
1ca68 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1ca69 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 l(pOut);. bre
1ca6a 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 78 ak;. }. if( Ex
1ca6b 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 7c pandBlob(pIn1) |
1ca6c 7c 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e | ExpandBlob(pIn
1ca6d 32 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 2) ) goto no_mem
1ca6e 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 ;. Stringify(pI
1ca6f 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 n1, encoding);.
1ca70 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32 2c Stringify(pIn2,
1ca71 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 2e encoding);. u.
1ca72 61 65 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 2d ae.nByte = pIn1-
1ca73 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 >n + pIn2->n;.
1ca74 69 66 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e 64 if( u.ae.nByte>d
1ca75 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1ca76 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 _LIMIT_LENGTH] )
1ca77 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 {. goto too_b
1ca78 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 ig;. }. MemSet
1ca79 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1ca7a 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 73 EM_Str);. if( s
1ca7b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
1ca7c 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 w(pOut, (int)u.a
1ca7d 65 2e 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d e.nByte+2, pOut=
1ca7e 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f =pIn2) ){. go
1ca7f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 to no_mem;. }.
1ca80 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 20 if( pOut!=pIn2
1ca81 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f ){. memcpy(pO
1ca82 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20 ut->z, pIn2->z,
1ca83 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 pIn2->n);. }.
1ca84 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b memcpy(&pOut->z[
1ca85 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e pIn2->n], pIn1->
1ca86 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70 z, pIn1->n);. p
1ca87 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 Out->z[u.ae.nByt
1ca88 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e e] = 0;. pOut->
1ca89 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d 20 z[u.ae.nByte+1]
1ca8a 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 = 0;. pOut->fla
1ca8b 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a gs |= MEM_Term;.
1ca8c 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 pOut->n = (int
1ca8d 29 75 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 70 )u.ae.nByte;. p
1ca8e 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 Out->enc = encod
1ca8f 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 ing;. UPDATE_MA
1ca90 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 X_BLOBSIZE(pOut)
1ca91 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1ca92 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20 Opcode: Add P1
1ca93 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1ca94 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e Add the value in
1ca95 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1ca96 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1ca97 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 ister P2.** and
1ca98 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1ca99 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1ca9a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e .** If either in
1ca9b 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 put is NULL, the
1ca9c 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1ca9d 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d .*/./* Opcode: M
1ca9e 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50 33 ultiply P1 P2 P3
1ca9f 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 * *.**.**.** Mu
1caa0 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65 ltiply the value
1caa1 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1caa2 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 by the value in
1caa3 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 register P2.** a
1caa4 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1caa5 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1caa6 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1caa7 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
1caa8 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1caa9 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
1caaa 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50 32 : Subtract P1 P2
1caab 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75 P3 * *.**.** Su
1caac 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 btract the value
1caad 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1caae 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69 from the value i
1caaf 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a n register P2.**
1cab0 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 and store the r
1cab1 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 esult in registe
1cab2 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 r P3..** If eith
1cab3 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1cab4 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 , the result is
1cab5 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f NULL..*/./* Opco
1cab6 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50 32 de: Divide P1 P2
1cab7 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69 P3 * *.**.** Di
1cab8 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20 69 vide the value i
1cab9 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79 n register P1 by
1caba 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1cabb 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 gister P2.** and
1cabc 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1cabd 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1cabe 20 28 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 20 (P3=P2/P1). If
1cabf 74 68 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a 2a the value in .**
1cac0 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1cac1 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 zero, then the r
1cac2 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 49 esult is NULL. I
1cac3 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1cac4 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 s .** NULL, the
1cac5 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a result is NULL..
1cac6 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 */./* Opcode: Re
1cac7 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50 33 mainder P1 P2 P3
1cac8 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 * *.**.** Compu
1cac9 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 te the remainder
1caca 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 20 64 after integer d
1cacb 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 76 ivision of the v
1cacc 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 alue in.** regis
1cacd 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 ter P1 by the va
1cace 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1cacf 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 P2 and store the
1cad0 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a result in P3. .
1cad1 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ** If the value
1cad2 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 69 in register P2 i
1cad3 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 75 6c s zero the resul
1cad4 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 t is NULL..** If
1cad5 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 either operand
1cad6 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 is NULL, the res
1cad7 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a ult is NULL..*/.
1cad8 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 case OP_Add:
1cad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1cada 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 * same as TK_PLU
1cadb 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 S, in1, in2, out
1cadc 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 3 */.case OP_Sub
1cadd 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 20 20 tract:
1cade 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1cadf 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e K_MINUS, in1, in
1cae0 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 2, out3 */.case
1cae1 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 OP_Multiply:
1cae2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1cae3 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e e as TK_STAR, in
1cae4 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
1cae5 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 case OP_Divide:
1cae6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1cae7 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 * same as TK_SLA
1cae8 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 SH, in1, in2, ou
1cae9 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 t3 */.case OP_Re
1caea 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 20 mainder: {
1caeb 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1caec 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 TK_REM, in1, in2
1caed 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 , out3 */.#if 0
1caee 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1caef 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1caf0 2e 61 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 .af */. int fla
1caf1 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 gs; /* Comb
1caf2 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 ined MEM_* flags
1caf3 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 from both input
1caf4 73 20 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 20 s */. i64 iA;
1caf5 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
1caf6 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 r value of left
1caf7 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 34 operand */. i64
1caf8 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 iB; /*
1caf9 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 Integer value of
1cafa 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a right operand *
1cafb 2f 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 20 /. double rA;
1cafc 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 /* Real valu
1cafd 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e e of left operan
1cafe 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 42 d */. double rB
1caff 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 ; /* Real v
1cb00 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 alue of right op
1cb01 65 72 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 20 erand */.#endif
1cb02 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cb03 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cb04 61 66 20 2a 2f 0a 0a 20 20 61 70 70 6c 79 4e 75 af */.. applyNu
1cb05 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 mericAffinity(pI
1cb06 6e 31 29 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 n1);. applyNume
1cb07 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 32 ricAffinity(pIn2
1cb08 29 3b 0a 20 20 75 2e 61 66 2e 66 6c 61 67 73 20 );. u.af.flags
1cb09 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 = pIn1->flags |
1cb0a 70 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 pIn2->flags;. i
1cb0b 66 28 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 26 f( (u.af.flags &
1cb0c 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 MEM_Null)!=0 )
1cb0d 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f goto arithmetic_
1cb0e 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a result_is_null;.
1cb0f 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1cb10 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c 61 67 73 gs & pIn2->flags
1cb11 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d & MEM_Int)==MEM
1cb12 5f 49 6e 74 20 29 7b 0a 20 20 20 20 75 2e 61 66 _Int ){. u.af
1cb13 2e 69 41 20 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b .iA = pIn1->u.i;
1cb14 0a 20 20 20 20 75 2e 61 66 2e 69 42 20 3d 20 70 . u.af.iB = p
1cb15 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 73 77 In2->u.i;. sw
1cb16 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 itch( pOp->opcod
1cb17 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 e ){. case
1cb18 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 OP_Add:
1cb19 75 2e 61 66 2e 69 42 20 2b 3d 20 75 2e 61 66 2e u.af.iB += u.af.
1cb1a 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b iA; break;
1cb1b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 . case OP_S
1cb1c 75 62 74 72 61 63 74 3a 20 20 20 20 75 2e 61 66 ubtract: u.af
1cb1d 2e 69 42 20 2d 3d 20 75 2e 61 66 2e 69 41 3b 20 .iB -= u.af.iA;
1cb1e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1cb1f 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 case OP_Multi
1cb20 70 6c 79 3a 20 20 20 20 75 2e 61 66 2e 69 42 20 ply: u.af.iB
1cb21 2a 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 *= u.af.iA;
1cb22 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1cb23 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b ase OP_Divide: {
1cb24 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 . if( u.a
1cb25 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 f.iA==0 ) goto a
1cb26 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 rithmetic_result
1cb27 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 _is_null;.
1cb28 20 20 2f 2a 20 44 69 76 69 64 69 6e 67 20 74 68 /* Dividing th
1cb29 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 e largest possib
1cb2a 6c 65 20 6e 65 67 61 74 69 76 65 20 36 34 2d 62 le negative 64-b
1cb2b 69 74 20 69 6e 74 65 67 65 72 20 28 31 3c 3c 36 it integer (1<<6
1cb2c 33 29 20 62 79 0a 20 20 20 20 20 20 20 20 2a 2a 3) by. **
1cb2d 20 2d 31 20 72 65 74 75 72 6e 73 20 61 6e 20 69 -1 returns an i
1cb2e 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61 72 67 65 nteger too large
1cb2f 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 61 20 36 to store in a 6
1cb30 34 2d 62 69 74 20 64 61 74 61 2d 74 79 70 65 2e 4-bit data-type.
1cb31 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 On. ** s
1cb32 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72 65 ome architecture
1cb33 73 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 76 65 s, the value ove
1cb34 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36 33 rflows to (1<<63
1cb35 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20 20 ). On others,.
1cb36 20 20 20 20 20 20 2a 2a 20 61 20 53 49 47 46 50 ** a SIGFP
1cb37 45 20 69 73 20 69 73 73 75 65 64 2e 20 54 68 65 E is issued. The
1cb38 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 following state
1cb39 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 ment normalizes
1cb3a 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 this. **
1cb3b 62 65 68 61 76 69 6f 72 20 73 6f 20 74 68 61 74 behavior so that
1cb3c 20 61 6c 6c 20 61 72 63 68 69 74 65 63 74 75 72 all architectur
1cb3d 65 73 20 62 65 68 61 76 65 20 61 73 20 69 66 20 es behave as if
1cb3e 69 6e 74 65 67 65 72 0a 20 20 20 20 20 20 20 20 integer.
1cb3f 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6f 63 63 75 ** overflow occu
1cb40 72 72 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f rred.. */
1cb41 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 . if( u.a
1cb42 66 2e 69 41 3d 3d 2d 31 20 26 26 20 75 2e 61 66 f.iA==-1 && u.af
1cb43 2e 69 42 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e .iB==SMALLEST_IN
1cb44 54 36 34 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 T64 ) u.af.iA =
1cb45 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 1;. u.af.
1cb46 69 42 20 2f 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 iB /= u.af.iA;.
1cb47 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1cb48 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 }. defa
1cb49 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 ult: {. i
1cb4a 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 f( u.af.iA==0 )
1cb4b 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f goto arithmetic_
1cb4c 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a result_is_null;.
1cb4d 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 if( u.af
1cb4e 2e 69 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69 .iA==-1 ) u.af.i
1cb4f 41 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 A = 1;. u
1cb50 2e 61 66 2e 69 42 20 25 3d 20 75 2e 61 66 2e 69 .af.iB %= u.af.i
1cb51 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b A;. break
1cb52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1cb53 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 pOut->u.i =
1cb54 75 2e 61 66 2e 69 42 3b 0a 20 20 20 20 4d 65 6d u.af.iB;. Mem
1cb55 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1cb56 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 , MEM_Int);. }e
1cb57 6c 73 65 7b 0a 20 20 20 20 75 2e 61 66 2e 72 41 lse{. u.af.rA
1cb58 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1cb59 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 alValue(pIn1);.
1cb5a 20 20 20 75 2e 61 66 2e 72 42 20 3d 20 73 71 6c u.af.rB = sql
1cb5b 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 ite3VdbeRealValu
1cb5c 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69 e(pIn2);. swi
1cb5d 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 tch( pOp->opcode
1cb5e 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ){. case O
1cb5f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 75 P_Add: u
1cb60 2e 61 66 2e 72 42 20 2b 3d 20 75 2e 61 66 2e 72 .af.rB += u.af.r
1cb61 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a A; break;.
1cb62 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 case OP_Su
1cb63 62 74 72 61 63 74 3a 20 20 20 20 75 2e 61 66 2e btract: u.af.
1cb64 72 42 20 2d 3d 20 75 2e 61 66 2e 72 41 3b 20 20 rB -= u.af.rA;
1cb65 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1cb66 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 case OP_Multip
1cb67 6c 79 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 2a ly: u.af.rB *
1cb68 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 = u.af.rA;
1cb69 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
1cb6a 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a se OP_Divide: {.
1cb6b 20 20 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 /* (doub
1cb6c 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 le)0 In case of
1cb6d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
1cb6e 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f TING_POINT... */
1cb6f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 . if( u.a
1cb70 66 2e 72 41 3d 3d 28 64 6f 75 62 6c 65 29 30 20 f.rA==(double)0
1cb71 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 ) goto arithmeti
1cb72 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c c_result_is_null
1cb73 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72 ;. u.af.r
1cb74 42 20 2f 3d 20 75 2e 61 66 2e 72 41 3b 0a 20 20 B /= u.af.rA;.
1cb75 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1cb76 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 }. defau
1cb77 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 75 2e lt: {. u.
1cb78 61 66 2e 69 41 20 3d 20 28 69 36 34 29 75 2e 61 af.iA = (i64)u.a
1cb79 66 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 75 2e f.rA;. u.
1cb7a 61 66 2e 69 42 20 3d 20 28 69 36 34 29 75 2e 61 af.iB = (i64)u.a
1cb7b 66 2e 72 42 3b 0a 20 20 20 20 20 20 20 20 69 66 f.rB;. if
1cb7c 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 ( u.af.iA==0 ) g
1cb7d 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 oto arithmetic_r
1cb7e 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 esult_is_null;.
1cb7f 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e if( u.af.
1cb80 69 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 iA==-1 ) u.af.iA
1cb81 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e = 1;. u.
1cb82 61 66 2e 72 42 20 3d 20 28 64 6f 75 62 6c 65 29 af.rB = (double)
1cb83 28 75 2e 61 66 2e 69 42 20 25 20 75 2e 61 66 2e (u.af.iB % u.af.
1cb84 69 41 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 iA);. bre
1cb85 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1cb86 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1cb87 33 49 73 4e 61 4e 28 75 2e 61 66 2e 72 42 29 20 3IsNaN(u.af.rB)
1cb88 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 72 ){. goto ar
1cb89 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f ithmetic_result_
1cb8a 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 is_null;. }.
1cb8b 20 20 20 70 4f 75 74 2d 3e 72 20 3d 20 75 2e 61 pOut->r = u.a
1cb8c 66 2e 72 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74 f.rB;. MemSet
1cb8d 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1cb8e 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66 EM_Real);. if
1cb8f 28 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 ( (u.af.flags &
1cb90 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a MEM_Real)==0 ){.
1cb91 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1cb92 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 eIntegerAffinity
1cb93 28 70 4f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 (pOut);. }.
1cb94 7d 0a 20 20 62 72 65 61 6b 3b 0a 0a 61 72 69 74 }. break;..arit
1cb95 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 hmetic_result_is
1cb96 5f 6e 75 6c 6c 3a 0a 20 20 73 71 6c 69 74 65 33 _null:. sqlite3
1cb97 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 VdbeMemSetNull(p
1cb98 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1cb99 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c ../* Opcode: Col
1cb9a 6c 53 65 71 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a lSeq * * P4.**.*
1cb9b 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
1cb9c 72 20 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 r to a CollSeq s
1cb9d 74 72 75 63 74 2e 20 49 66 20 74 68 65 20 6e 65 truct. If the ne
1cb9e 78 74 20 63 61 6c 6c 20 74 6f 20 61 20 75 73 65 xt call to a use
1cb9f 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 r function.** or
1cba0 20 61 67 67 72 65 67 61 74 65 20 63 61 6c 6c 73 aggregate calls
1cba1 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 sqlite3GetFuncC
1cba2 6f 6c 6c 53 65 71 28 29 2c 20 74 68 69 73 20 63 ollSeq(), this c
1cba3 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
1cba4 65 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 e will.** be ret
1cba5 75 72 6e 65 64 2e 20 54 68 69 73 20 69 73 20 75 urned. This is u
1cba6 73 65 64 20 62 79 20 74 68 65 20 62 75 69 6c 74 sed by the built
1cba7 2d 69 6e 20 6d 69 6e 28 29 2c 20 6d 61 78 28 29 -in min(), max()
1cba8 20 61 6e 64 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a and nullif().**
1cba9 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a functions..**.*
1cbaa 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 * The interface
1cbab 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c used by the impl
1cbac 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
1cbad 65 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 e aforementioned
1cbae 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f functions.** to
1cbaf 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f retrieve the co
1cbb0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
1cbb1 20 73 65 74 20 62 79 20 74 68 69 73 20 6f 70 63 set by this opc
1cbb2 6f 64 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c ode is not avail
1cbb3 61 62 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 able.** publicly
1cbb4 2c 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20 66 , only to user f
1cbb5 75 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 unctions defined
1cbb6 20 69 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 in func.c..*/.c
1cbb7 61 73 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 ase OP_CollSeq:
1cbb8 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d {. assert( pOp-
1cbb9 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c >p4type==P4_COLL
1cbba 53 45 51 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a SEQ );. break;.
1cbbb 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 75 }../* Opcode: Fu
1cbbc 6e 63 74 69 6f 6e 20 50 31 20 50 32 20 50 33 20 nction P1 P2 P3
1cbbd 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f P4 P5.**.** Invo
1cbbe 6b 65 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 ke a user functi
1cbbf 6f 6e 20 28 50 34 20 69 73 20 61 20 70 6f 69 6e on (P4 is a poin
1cbc0 74 65 72 20 74 6f 20 61 20 46 75 6e 63 74 69 6f ter to a Functio
1cbc1 6e 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 n structure that
1cbc2 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20 .** defines the
1cbc3 66 75 6e 63 74 69 6f 6e 29 20 77 69 74 68 20 50 function) with P
1cbc4 35 20 61 72 67 75 6d 65 6e 74 73 20 74 61 6b 65 5 arguments take
1cbc5 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 n from register
1cbc6 50 32 20 61 6e 64 0a 2a 2a 20 73 75 63 63 65 73 P2 and.** succes
1cbc7 73 6f 72 73 2e 20 20 54 68 65 20 72 65 73 75 6c sors. The resul
1cbc8 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f t of the functio
1cbc9 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 n is stored in r
1cbca 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 52 egister P3..** R
1cbcb 65 67 69 73 74 65 72 20 50 33 20 6d 75 73 74 20 egister P3 must
1cbcc 6e 6f 74 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 not be one of th
1cbcd 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 70 75 74 e function input
1cbce 73 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 s..**.** P1 is a
1cbcf 20 33 32 2d 62 69 74 20 62 69 74 6d 61 73 6b 20 32-bit bitmask
1cbd0 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74 68 indicating wheth
1cbd1 65 72 20 6f 72 20 6e 6f 74 20 65 61 63 68 20 61 er or not each a
1cbd2 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 0a rgument to the .
1cbd3 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 ** function was
1cbd4 64 65 74 65 72 6d 69 6e 65 64 20 74 6f 20 62 65 determined to be
1cbd5 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d constant at com
1cbd6 70 69 6c 65 20 74 69 6d 65 2e 20 49 66 20 74 68 pile time. If th
1cbd7 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d e first.** argum
1cbd8 65 6e 74 20 77 61 73 20 63 6f 6e 73 74 61 6e 74 ent was constant
1cbd9 20 74 68 65 6e 20 62 69 74 20 30 20 6f 66 20 50 then bit 0 of P
1cbda 31 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 69 1 is set. This i
1cbdb 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
1cbdc 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 6d ine.** whether m
1cbdd 65 74 61 20 64 61 74 61 20 61 73 73 6f 63 69 61 eta data associa
1cbde 74 65 64 20 77 69 74 68 20 61 20 75 73 65 72 20 ted with a user
1cbdf 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
1cbe0 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 73 t using the.** s
1cbe1 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 qlite3_set_auxda
1cbe2 74 61 28 29 20 41 50 49 20 6d 61 79 20 62 65 20 ta() API may be
1cbe3 73 61 66 65 6c 79 20 72 65 74 61 69 6e 65 64 20 safely retained
1cbe4 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a until the next.*
1cbe5 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 * invocation of
1cbe6 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a this opcode..**.
1cbe7 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 41 67 67 ** See also: Agg
1cbe8 53 74 65 70 20 61 6e 64 20 41 67 67 46 69 6e 61 Step and AggFina
1cbe9 6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 75 6e l.*/.case OP_Fun
1cbea 63 74 69 6f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 ction: {.#if 0
1cbeb 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cbec 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cbed 61 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 ag */. int i;.
1cbee 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 Mem *pArg;. sq
1cbef 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 lite3_context ct
1cbf0 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c x;. sqlite3_val
1cbf1 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e ue **apVal;. in
1cbf2 74 20 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c t n;.#endif /* l
1cbf3 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1cbf4 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a oved into u.ag *
1cbf5 2f 0a 0a 20 20 75 2e 61 67 2e 6e 20 3d 20 70 4f /.. u.ag.n = pO
1cbf6 70 2d 3e 70 35 3b 0a 20 20 75 2e 61 67 2e 61 70 p->p5;. u.ag.ap
1cbf7 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a Val = p->apArg;.
1cbf8 20 20 61 73 73 65 72 74 28 20 75 2e 61 67 2e 61 assert( u.ag.a
1cbf9 70 56 61 6c 20 7c 7c 20 75 2e 61 67 2e 6e 3d 3d pVal || u.ag.n==
1cbfa 30 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 0 );.. assert(
1cbfb 75 2e 61 67 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4f u.ag.n==0 || (pO
1cbfc 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e p->p2>0 && pOp->
1cbfd 70 32 2b 75 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e 4d p2+u.ag.n<=p->nM
1cbfe 65 6d 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 em+1) );. asser
1cbff 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e t( pOp->p3<pOp->
1cc00 70 32 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 p2 || pOp->p3>=p
1cc01 4f 70 2d 3e 70 32 2b 75 2e 61 67 2e 6e 20 29 3b Op->p2+u.ag.n );
1cc02 0a 20 20 75 2e 61 67 2e 70 41 72 67 20 3d 20 26 . u.ag.pArg = &
1cc03 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d p->aMem[pOp->p2]
1cc04 3b 0a 20 20 66 6f 72 28 75 2e 61 67 2e 69 3d 30 ;. for(u.ag.i=0
1cc05 3b 20 75 2e 61 67 2e 69 3c 75 2e 61 67 2e 6e 3b ; u.ag.i<u.ag.n;
1cc06 20 75 2e 61 67 2e 69 2b 2b 2c 20 75 2e 61 67 2e u.ag.i++, u.ag.
1cc07 70 41 72 67 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 pArg++){. u.a
1cc08 67 2e 61 70 56 61 6c 5b 75 2e 61 67 2e 69 5d 20 g.apVal[u.ag.i]
1cc09 3d 20 75 2e 61 67 2e 70 41 72 67 3b 0a 20 20 20 = u.ag.pArg;.
1cc0a 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 storeTypeInfo(u
1cc0b 2e 61 67 2e 70 41 72 67 2c 20 65 6e 63 6f 64 69 .ag.pArg, encodi
1cc0c 6e 67 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 ng);. REGISTE
1cc0d 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c R_TRACE(pOp->p2,
1cc0e 20 75 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 7d u.ag.pArg);. }
1cc0f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1cc10 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 >p4type==P4_FUNC
1cc11 44 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 DEF || pOp->p4ty
1cc12 70 65 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 pe==P4_VDBEFUNC
1cc13 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 );. if( pOp->p4
1cc14 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 type==P4_FUNCDEF
1cc15 20 29 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 ){. u.ag.ctx
1cc16 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 .pFunc = pOp->p4
1cc17 2e 70 46 75 6e 63 3b 0a 20 20 20 20 75 2e 61 67 .pFunc;. u.ag
1cc18 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d .ctx.pVdbeFunc =
1cc19 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
1cc1a 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 u.ag.ctx.pVdbeF
1cc1b 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a unc = (VdbeFunc*
1cc1c 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 )pOp->p4.pVdbeFu
1cc1d 6e 63 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 nc;. u.ag.ctx
1cc1e 2e 70 46 75 6e 63 20 3d 20 75 2e 61 67 2e 63 74 .pFunc = u.ag.ct
1cc1f 78 2e 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 x.pVdbeFunc->pFu
1cc20 6e 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 nc;. }.. asser
1cc21 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 t( pOp->p3>0 &&
1cc22 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1cc23 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d );. pOut = &p-
1cc24 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1cc25 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 66 6c 61 u.ag.ctx.s.fla
1cc26 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1cc27 20 75 2e 61 67 2e 63 74 78 2e 73 2e 64 62 20 3d u.ag.ctx.s.db =
1cc28 20 64 62 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e db;. u.ag.ctx.
1cc29 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 2e s.xDel = 0;. u.
1cc2a 61 67 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 ag.ctx.s.zMalloc
1cc2b 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 = 0;.. /* The
1cc2c 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 output cell may
1cc2d 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62 already have a b
1cc2e 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e uffer allocated.
1cc2f 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 70 Move. ** the p
1cc30 6f 69 6e 74 65 72 20 74 6f 20 75 2e 61 67 2e 63 ointer to u.ag.c
1cc31 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 tx.s so in case
1cc32 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f the user-functio
1cc33 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 74 n can use. ** t
1cc34 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 he already alloc
1cc35 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 ated buffer inst
1cc36 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e ead of allocatin
1cc37 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a g a new one.. *
1cc38 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d /. sqlite3VdbeM
1cc39 65 6d 4d 6f 76 65 28 26 75 2e 61 67 2e 63 74 78 emMove(&u.ag.ctx
1cc3a 2e 73 2c 20 70 4f 75 74 29 3b 0a 20 20 4d 65 6d .s, pOut);. Mem
1cc3b 53 65 74 54 79 70 65 46 6c 61 67 28 26 75 2e 61 SetTypeFlag(&u.a
1cc3c 67 2e 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e 75 6c g.ctx.s, MEM_Nul
1cc3d 6c 29 3b 0a 0a 20 20 75 2e 61 67 2e 63 74 78 2e l);.. u.ag.ctx.
1cc3e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 69 isError = 0;. i
1cc3f 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e f( u.ag.ctx.pFun
1cc40 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 c->flags & SQLIT
1cc41 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 E_FUNC_NEEDCOLL
1cc42 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
1cc43 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 Op>p->aOp );.
1cc44 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d assert( pOp[-1]
1cc45 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c .p4type==P4_COLL
1cc46 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 SEQ );. asser
1cc47 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 t( pOp[-1].opcod
1cc48 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b e==OP_CollSeq );
1cc49 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 43 . u.ag.ctx.pC
1cc4a 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 oll = pOp[-1].p4
1cc4b 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 .pColl;. }. if
1cc4c 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1cc4d 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1cc4e 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1cc4f 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63 74 78 2e e;. (*u.ag.ctx.
1cc50 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 75 pFunc->xFunc)(&u
1cc51 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67 2e 6e 2c .ag.ctx, u.ag.n,
1cc52 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b 0a 20 20 u.ag.apVal);.
1cc53 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1cc54 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 yOn(db) ){. s
1cc55 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1cc56 65 61 73 65 28 26 75 2e 61 67 2e 63 74 78 2e 73 ease(&u.ag.ctx.s
1cc57 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 );. goto abor
1cc58 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1cc59 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d . }. if( db->m
1cc5a 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1cc5b 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 /* Even thoug
1cc5c 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 h a malloc() has
1cc5d 20 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 failed, the imp
1cc5e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
1cc5f 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 he. ** user f
1cc60 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 unction may have
1cc61 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 called an sqlit
1cc62 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 e3_result_XXX()
1cc63 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 function. **
1cc64 74 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 to return a valu
1cc65 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 e. The following
1cc66 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 call releases a
1cc67 6e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 ny resources.
1cc68 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 ** associated w
1cc69 69 74 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 ith such a value
1cc6a 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
1cc6b 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 Note: Maybe MemR
1cc6c 65 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 elease() should
1cc6d 62 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c be called if sql
1cc6e 69 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 ite3SafetyOn().
1cc6f 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f ** fails also
1cc70 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 (the if(...) st
1cc71 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 atement above).
1cc72 42 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 But if people ar
1cc73 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e e. ** misusin
1cc74 67 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 g sqlite, they h
1cc75 61 76 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c ave bigger probl
1cc76 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 ems than a leake
1cc77 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a d value.. */.
1cc78 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1cc79 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e emRelease(&u.ag.
1cc7a 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f ctx.s);. goto
1cc7b 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 no_mem;. }..
1cc7c 2f 2a 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69 /* If any auxili
1cc7d 61 72 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f ary data functio
1cc7e 6e 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c ns have been cal
1cc7f 6c 65 64 20 62 79 20 74 68 69 73 20 75 73 65 72 led by this user
1cc80 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 function,. **
1cc81 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c immediately call
1cc82 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
1cc83 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74 for any non-stat
1cc84 69 63 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a ic values.. */.
1cc85 20 20 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 if( u.ag.ctx.p
1cc86 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 VdbeFunc ){.
1cc87 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
1cc88 65 41 75 78 44 61 74 61 28 75 2e 61 67 2e 63 74 eAuxData(u.ag.ct
1cc89 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 x.pVdbeFunc, pOp
1cc8a 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e ->p1);. pOp->
1cc8b 70 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 75 p4.pVdbeFunc = u
1cc8c 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e .ag.ctx.pVdbeFun
1cc8d 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 c;. pOp->p4ty
1cc8e 70 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43 pe = P4_VDBEFUNC
1cc8f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
1cc90 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
1cc91 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 74 rned an error, t
1cc92 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f hrow an exceptio
1cc93 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e n */. if( u.ag.
1cc94 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 ctx.isError ){.
1cc95 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1cc96 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1cc97 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 db, "%s", sqlit
1cc98 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 75 e3_value_text(&u
1cc99 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20 20 .ag.ctx.s));.
1cc9a 20 72 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e 69 rc = u.ag.ctx.i
1cc9b 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f sError;. }.. /
1cc9c 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c * Copy the resul
1cc9d 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f t of the functio
1cc9e 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 n into register
1cc9f 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 P3 */. sqlite3V
1cca0 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
1cca1 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 65 g(&u.ag.ctx.s, e
1cca2 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c 69 ncoding);. sqli
1cca3 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 te3VdbeMemMove(p
1cca4 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74 78 2e 73 Out, &u.ag.ctx.s
1cca5 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
1cca6 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f VdbeMemTooBig(pO
1cca7 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 ut) ){. goto
1cca8 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52 too_big;. }. R
1cca9 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1ccaa 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 p->p3, pOut);.
1ccab 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1ccac 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 IZE(pOut);. bre
1ccad 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1ccae 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20 50 : BitAnd P1 P2 P
1ccaf 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 3 * *.**.** Take
1ccb0 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e the bit-wise AN
1ccb1 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 D of the values
1ccb2 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 in register P1 a
1ccb3 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f nd P2 and.** sto
1ccb4 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e re the result in
1ccb5 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a register P3..**
1ccb6 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 If either input
1ccb7 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
1ccb8 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1ccb9 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f ./* Opcode: BitO
1ccba 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a r P1 P2 P3 * *.*
1ccbb 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69 *.** Take the bi
1ccbc 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68 65 t-wise OR of the
1ccbd 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 values in regis
1ccbe 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e ter P1 and P2 an
1ccbf 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 d.** store the r
1ccc0 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 esult in registe
1ccc1 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 r P3..** If eith
1ccc2 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1ccc3 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 , the result is
1ccc4 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f NULL..*/./* Opco
1ccc5 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50 31 de: ShiftLeft P1
1ccc6 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1ccc7 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 67 Shift the integ
1ccc8 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 er value in regi
1ccc9 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 6c ster P2 to the l
1ccca 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 eft by the.** nu
1cccb 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70 65 mber of bits spe
1cccc 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e cified by the in
1cccd 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65 72 teger in regiser
1ccce 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 P1..** Store th
1cccf 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
1ccd0 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 ster P3..** If e
1ccd1 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e ither input is N
1ccd2 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 ULL, the result
1ccd3 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f is NULL..*/./* O
1ccd4 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67 68 pcode: ShiftRigh
1ccd5 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a t P1 P2 P3 * *.*
1ccd6 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 *.** Shift the i
1ccd7 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 nteger value in
1ccd8 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 register P2 to t
1ccd9 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65 0a he right by the.
1ccda 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 ** number of bit
1ccdb 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 s specified by t
1ccdc 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 he integer in re
1ccdd 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 gister P1..** St
1ccde 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1ccdf 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1cce0 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1cce1 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1cce2 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1cce3 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 /.case OP_BitAnd
1cce4 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 :
1cce5 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1cce6 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 BITAND, in1, in2
1cce7 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1cce8 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20 P_BitOr:
1cce9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1ccea 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 e as TK_BITOR, i
1cceb 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1ccec 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 .case OP_ShiftLe
1cced 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 ft:
1ccee 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
1ccef 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c SHIFT, in1, in2,
1ccf0 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 out3 */.case OP
1ccf1 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20 _ShiftRight: {
1ccf2 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1ccf3 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 as TK_RSHIFT, i
1ccf4 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1ccf5 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1ccf6 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ccf7 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20 20 into u.ah */.
1ccf8 69 36 34 20 61 3b 0a 20 20 69 36 34 20 62 3b 0a i64 a;. i64 b;.
1ccf9 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1ccfa 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ccfb 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20 20 into u.ah */..
1ccfc 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1ccfd 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 | pIn2->flags)
1ccfe 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 & MEM_Null ){.
1ccff 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cd00 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 SetNull(pOut);.
1cd01 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 break;. }.
1cd02 75 2e 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 33 u.ah.a = sqlite3
1cd03 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e VdbeIntValue(pIn
1cd04 32 29 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 73 2);. u.ah.b = s
1cd05 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1cd06 75 65 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 74 ue(pIn1);. swit
1cd07 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 ch( pOp->opcode
1cd08 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 ){. case OP_B
1cd09 69 74 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 68 itAnd: u.ah
1cd0a 2e 61 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 20 .a &= u.ah.b;
1cd0b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
1cd0c 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 e OP_BitOr:
1cd0d 20 20 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 68 u.ah.a |= u.ah
1cd0e 2e 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 .b; break;.
1cd0f 20 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 case OP_Shift
1cd10 4c 65 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 3c Left: u.ah.a <
1cd11 3c 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72 <= u.ah.b; br
1cd12 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 eak;. default
1cd13 3a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e : assert( pOp->
1cd14 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 opcode==OP_Shift
1cd15 52 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 Right );.
1cd16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cd17 20 20 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e 61 u.ah.a >>= u.a
1cd18 68 2e 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 h.b; break;.
1cd19 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d }. pOut->u.i =
1cd1a 20 75 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 65 u.ah.a;. MemSe
1cd1b 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1cd1c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 MEM_Int);. brea
1cd1d 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1cd1e 20 41 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a AddImm P1 P2 *
1cd1f 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 * *.** .** Add
1cd20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 the constant P2
1cd21 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 to the value in
1cd22 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 register P1..**
1cd23 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c The result is al
1cd24 77 61 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e ways an integer.
1cd25 0a 2a 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 .**.** To force
1cd26 61 6e 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 any register to
1cd27 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a be an integer, j
1cd28 75 73 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 ust add 0..*/.ca
1cd29 73 65 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 se OP_AddImm: {
1cd2a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e /* in
1cd2b 31 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 1 */. sqlite3Vd
1cd2c 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
1cd2d 70 49 6e 31 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 pIn1);. pIn1->u
1cd2e 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 .i += pOp->p2;.
1cd2f 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cd30 63 6f 64 65 3a 20 4d 75 73 74 42 65 49 6e 74 20 code: MustBeInt
1cd31 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a P1 P2 * * *.** .
1cd32 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c ** Force the val
1cd33 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1cd34 31 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 1 to be an integ
1cd35 65 72 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 er. If the valu
1cd36 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e 6f e.** in P1 is no
1cd37 74 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 t an integer and
1cd38 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 cannot be conve
1cd39 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 rted into an int
1cd3a 65 67 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74 20 eger.** without
1cd3b 64 61 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e 20 data loss, then
1cd3c 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 jump immediately
1cd3d 20 74 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 32 to P2, or if P2
1cd3e 3d 3d 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20 ==0.** raise an
1cd3f 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 SQLITE_MISMATCH
1cd40 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 exception..*/.ca
1cd41 73 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 3a se OP_MustBeInt:
1cd42 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1cd43 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 jump, in1 */.
1cd44 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 applyAffinity(pI
1cd45 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e n1, SQLITE_AFF_N
1cd46 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 UMERIC, encoding
1cd47 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e );. if( (pIn1->
1cd48 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 flags & MEM_Int)
1cd49 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ==0 ){. if( p
1cd4a 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 Op->p2==0 ){.
1cd4b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d rc = SQLITE_M
1cd4c 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67 ISMATCH;. g
1cd4d 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1cd4e 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 _error;. }els
1cd4f 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f e{. pc = pO
1cd50 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d p->p2 - 1;. }
1cd51 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 . }else{. Me
1cd52 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e mSetTypeFlag(pIn
1cd53 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 1, MEM_Int);. }
1cd54 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1cd55 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 Opcode: RealAffi
1cd56 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a nity P1 * * * *.
1cd57 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 **.** If registe
1cd58 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e r P1 holds an in
1cd59 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74 teger convert it
1cd5a 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65 to a real value
1cd5b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ..**.** This opc
1cd5c 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e ode is used when
1cd5d 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f extracting info
1cd5e 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 rmation from a c
1cd5f 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 olumn that.** ha
1cd60 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e s REAL affinity.
1cd61 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 Such column va
1cd62 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 lues may still b
1cd63 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 e stored as.** i
1cd64 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61 ntegers, for spa
1cd65 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62 ce efficiency, b
1cd66 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74 ut after extract
1cd67 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d ion we want them
1cd68 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 .** to have only
1cd69 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a a real value..*
1cd6a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 /.case OP_RealAf
1cd6b 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 finity: {
1cd6c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e /* in
1cd6d 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 1 */. if( pIn1-
1cd6e 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1cd6f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1cd70 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 dbeMemRealify(pI
1cd71 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b n1);. }. break
1cd72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
1cd73 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a ITE_OMIT_CAST./*
1cd74 20 4f 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 20 Opcode: ToText
1cd75 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1cd76 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 Force the value
1cd77 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1cd78 74 6f 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 49 to be text..** I
1cd79 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e f the value is n
1cd7a 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 umeric, convert
1cd7b 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 75 it to a string u
1cd7c 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 sing the.** equi
1cd7d 76 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 66 valent of printf
1cd7e 28 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 73 (). Blob values
1cd7f 20 61 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61 are unchanged a
1cd80 6e 64 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 77 nd.** are afterw
1cd81 61 72 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 65 ards simply inte
1cd82 72 70 72 65 74 65 64 20 61 73 20 74 65 78 74 2e rpreted as text.
1cd83 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 .**.** A NULL va
1cd84 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 lue is not chang
1cd85 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 ed by this routi
1cd86 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 ne. It remains
1cd87 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 NULL..*/.case OP
1cd88 5f 54 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 20 _ToText: {
1cd89 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1cd8a 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 ame as TK_TO_TEX
1cd8b 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 T, in1 */. if(
1cd8c 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1cd8d 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a M_Null ) break;.
1cd8e 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74 assert( MEM_St
1cd8f 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 r==(MEM_Blob>>3)
1cd90 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 );. pIn1->flag
1cd91 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 s |= (pIn1->flag
1cd92 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a s&MEM_Blob)>>3;.
1cd93 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 applyAffinity(
1cd94 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 pIn1, SQLITE_AFF
1cd95 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 _TEXT, encoding)
1cd96 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 ;. rc = ExpandB
1cd97 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73 lob(pIn1);. ass
1cd98 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 ert( pIn1->flags
1cd99 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 & MEM_Str || db
1cd9a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1cd9b 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 ;. pIn1->flags
1cd9c 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d &= ~(MEM_Int|MEM
1cd9d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d _Real|MEM_Blob|M
1cd9e 45 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 44 41 EM_Zero);. UPDA
1cd9f 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1cda0 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a pIn1);. break;.
1cda1 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f }../* Opcode: To
1cda2 42 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a Blob P1 * * * *.
1cda3 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 **.** Force the
1cda4 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1cda5 72 20 50 31 20 74 6f 20 62 65 20 61 20 42 4c 4f r P1 to be a BLO
1cda6 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c B..** If the val
1cda7 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 ue is numeric, c
1cda8 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 onvert it to a s
1cda9 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20 tring first..**
1cdaa 53 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70 Strings are simp
1cdab 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74 65 64 ly reinterpreted
1cdac 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e as blobs with n
1cdad 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74 o change.** to t
1cdae 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 he underlying da
1cdaf 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c ta..**.** A NULL
1cdb0 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 value is not ch
1cdb1 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f anged by this ro
1cdb2 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 utine. It remai
1cdb3 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 ns NULL..*/.case
1cdb4 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20 OP_ToBlob: {
1cdb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1cdb6 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
1cdb7 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 BLOB, in1 */. i
1cdb8 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
1cdb9 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 MEM_Null ) brea
1cdba 6b 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e k;. if( (pIn1->
1cdbb 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 flags & MEM_Blob
1cdbc 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 70 70 6c )==0 ){. appl
1cdbd 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 yAffinity(pIn1,
1cdbe 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c SQLITE_AFF_TEXT,
1cdbf 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 encoding);.
1cdc0 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c assert( pIn1->fl
1cdc1 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c ags & MEM_Str ||
1cdc2 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1cdc3 64 20 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 d );. MemSetT
1cdc4 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 ypeFlag(pIn1, ME
1cdc5 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 65 6c 73 65 M_Blob);. }else
1cdc6 7b 0a 20 20 20 20 70 49 6e 31 2d 3e 66 6c 61 67 {. pIn1->flag
1cdc7 73 20 26 3d 20 7e 28 4d 45 4d 5f 54 79 70 65 4d s &= ~(MEM_TypeM
1cdc8 61 73 6b 26 7e 4d 45 4d 5f 42 6c 6f 62 29 3b 0a ask&~MEM_Blob);.
1cdc9 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 }. UPDATE_MAX
1cdca 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b _BLOBSIZE(pIn1);
1cdcb 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1cdcc 4f 70 63 6f 64 65 3a 20 54 6f 4e 75 6d 65 72 69 Opcode: ToNumeri
1cdcd 63 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a c P1 * * * *.**.
1cdce 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c ** Force the val
1cdcf 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1cdd0 31 20 74 6f 20 62 65 20 6e 75 6d 65 72 69 63 20 1 to be numeric
1cdd1 28 65 69 74 68 65 72 20 61 6e 0a 2a 2a 20 69 6e (either an.** in
1cdd2 74 65 67 65 72 20 6f 72 20 61 20 66 6c 6f 61 74 teger or a float
1cdd3 69 6e 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72 ing-point number
1cdd4 2e 29 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c .).** If the val
1cdd5 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c ue is text or bl
1cdd6 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 ob, try to conve
1cdd7 72 74 20 69 74 20 74 6f 20 61 6e 20 75 73 69 6e rt it to an usin
1cdd8 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c g the.** equival
1cdd9 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 6f 72 ent of atoi() or
1cdda 20 61 74 6f 66 28 29 20 61 6e 64 20 73 74 6f 72 atof() and stor
1cddb 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 e 0 if no such c
1cddc 6f 6e 76 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 73 onversion .** is
1cddd 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a possible..**.**
1cdde 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 A NULL value is
1cddf 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 not changed by
1cde0 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 this routine. I
1cde1 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a t remains NULL..
1cde2 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 4e 75 6d */.case OP_ToNum
1cde3 65 72 69 63 3a 20 7b 20 20 20 20 20 20 20 20 20 eric: {
1cde4 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1cde5 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 as TK_TO_NUMERI
1cde6 43 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 C, in1 */. if(
1cde7 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 28 (pIn1->flags & (
1cde8 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 MEM_Null|MEM_Int
1cde9 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 29 |MEM_Real))==0 )
1cdea 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1cdeb 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 70 49 6e eMemNumerify(pIn
1cdec 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1);. }. break;
1cded 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1cdee 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f ITE_OMIT_CAST */
1cdef 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 49 ../* Opcode: ToI
1cdf0 6e 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a nt P1 * * * *.**
1cdf1 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 .** Force the va
1cdf2 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1cdf3 50 31 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 P1 be an integer
1cdf4 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 76 61 6c . If.** The val
1cdf5 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 ue is currently
1cdf6 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64 a real number, d
1cdf7 72 6f 70 20 69 74 73 20 66 72 61 63 74 69 6f 6e rop its fraction
1cdf8 61 6c 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 74 al part..** If t
1cdf9 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 he value is text
1cdfa 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f or blob, try to
1cdfb 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
1cdfc 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 n integer using
1cdfd 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e the.** equivalen
1cdfe 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 t of atoi() and
1cdff 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 75 store 0 if no su
1ce00 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 ch conversion is
1ce01 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a possible..**.**
1ce02 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 A NULL value is
1ce03 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 not changed by
1ce04 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 this routine. I
1ce05 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a t remains NULL..
1ce06 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e 74 */.case OP_ToInt
1ce07 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1ce08 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1ce09 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 2a TK_TO_INT, in1 *
1ce0a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 /. if( (pIn1->f
1ce0b 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1ce0c 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1ce0d 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1ce0e 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 ify(pIn1);. }.
1ce0f 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 break;.}..#ifnd
1ce10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
1ce11 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 AST./* Opcode: T
1ce12 6f 52 65 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a oReal P1 * * * *
1ce13 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 .**.** Force the
1ce14 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1ce15 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 66 6c er P1 to be a fl
1ce16 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d oating point num
1ce17 62 65 72 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 ber..** If The v
1ce18 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c alue is currentl
1ce19 79 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f y an integer, co
1ce1a 6e 76 65 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 nvert it..** If
1ce1b 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 the value is tex
1ce1c 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 t or blob, try t
1ce1d 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 o convert it to
1ce1e 61 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67 an integer using
1ce1f 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 the.** equivale
1ce20 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 nt of atoi() and
1ce21 20 73 74 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f store 0.0 if no
1ce22 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e such conversion
1ce23 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a is possible..**
1ce24 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 .** A NULL value
1ce25 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 is not changed
1ce26 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e by this routine.
1ce27 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c It remains NUL
1ce28 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f L..*/.case OP_To
1ce29 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 Real: {
1ce2a 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1ce2b 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 as TK_TO_REAL,
1ce2c 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 in1 */. if( (pI
1ce2d 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1ce2e 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Null)==0 ){.
1ce2f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1ce30 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d alify(pIn1);. }
1ce31 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1ce32 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1ce33 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 T_CAST */../* Op
1ce34 63 6f 64 65 3a 20 4c 74 20 50 31 20 50 32 20 50 code: Lt P1 P2 P
1ce35 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43 6f 3 P4 P5.**.** Co
1ce36 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 mpare the values
1ce37 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1ce38 61 6e 64 20 50 33 2e 20 20 49 66 20 72 65 67 28 and P3. If reg(
1ce39 50 33 29 3c 72 65 67 28 50 31 29 20 74 68 65 6e P3)<reg(P1) then
1ce3a 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 .** jump to addr
1ce3b 65 73 73 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a 20 ess P2. .**.**
1ce3c 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 If the SQLITE_JU
1ce3d 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 6f 66 20 MPIFNULL bit of
1ce3e 50 35 20 69 73 20 73 65 74 20 61 6e 64 20 65 69 P5 is set and ei
1ce3f 74 68 65 72 20 72 65 67 28 50 31 29 20 6f 72 0a ther reg(P1) or.
1ce40 2a 2a 20 72 65 67 28 50 33 29 20 69 73 20 4e 55 ** reg(P3) is NU
1ce41 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 LL then take the
1ce42 20 6a 75 6d 70 2e 20 20 49 66 20 74 68 65 20 53 jump. If the S
1ce43 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
1ce44 20 0a 2a 2a 20 62 69 74 20 69 73 20 63 6c 65 61 .** bit is clea
1ce45 72 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 r then fall thru
1ce46 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 if either opera
1ce47 6e 64 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a nd is NULL..**.*
1ce48 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46 * The SQLITE_AFF
1ce49 5f 4d 41 53 4b 20 70 6f 72 74 69 6f 6e 20 6f 66 _MASK portion of
1ce4a 20 50 35 20 6d 75 73 74 20 62 65 20 61 6e 20 61 P5 must be an a
1ce4b 66 66 69 6e 69 74 79 20 63 68 61 72 61 63 74 65 ffinity characte
1ce4c 72 20 2d 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 r -.** SQLITE_AF
1ce4d 46 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 41 F_TEXT, SQLITE_A
1ce4e 46 46 5f 49 4e 54 45 47 45 52 2c 20 61 6e 64 20 FF_INTEGER, and
1ce4f 73 6f 20 66 6f 72 74 68 2e 20 41 6e 20 61 74 74 so forth. An att
1ce50 65 6d 70 74 20 69 73 20 6d 61 64 65 20 0a 2a 2a empt is made .**
1ce51 20 74 6f 20 63 6f 65 72 63 65 20 62 6f 74 68 20 to coerce both
1ce52 69 6e 70 75 74 73 20 61 63 63 6f 72 64 69 6e 67 inputs according
1ce53 20 74 6f 20 74 68 69 73 20 61 66 66 69 6e 69 74 to this affinit
1ce54 79 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 y before the.**
1ce55 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d 61 comparison is ma
1ce56 64 65 2e 20 49 66 20 74 68 65 20 53 51 4c 49 54 de. If the SQLIT
1ce57 45 5f 41 46 46 5f 4d 41 53 4b 20 69 73 20 30 78 E_AFF_MASK is 0x
1ce58 30 30 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 00, then numeric
1ce59 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 69 73 20 .** affinity is
1ce5a 75 73 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 used. Note that
1ce5b 74 68 65 20 61 66 66 69 6e 69 74 79 20 63 6f 6e the affinity con
1ce5c 76 65 72 73 69 6f 6e 73 20 61 72 65 20 73 74 6f versions are sto
1ce5d 72 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 6f red.** back into
1ce5e 20 74 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 the input regis
1ce5f 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e 20 ters P1 and P3.
1ce60 20 53 6f 20 74 68 69 73 20 6f 70 63 6f 64 65 20 So this opcode
1ce61 63 61 6e 20 63 61 75 73 65 0a 2a 2a 20 70 65 72 can cause.** per
1ce62 73 69 73 74 65 6e 74 20 63 68 61 6e 67 65 73 20 sistent changes
1ce63 74 6f 20 72 65 67 69 73 74 65 72 73 20 50 31 20 to registers P1
1ce64 61 6e 64 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e and P3..**.** On
1ce65 63 65 20 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f ce any conversio
1ce66 6e 73 20 68 61 76 65 20 74 61 6b 65 6e 20 70 6c ns have taken pl
1ce67 61 63 65 2c 20 61 6e 64 20 6e 65 69 74 68 65 72 ace, and neither
1ce68 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 value is NULL,
1ce69 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 61 .** the values a
1ce6a 72 65 20 63 6f 6d 70 61 72 65 64 2e 20 49 66 20 re compared. If
1ce6b 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 20 both values are
1ce6c 62 6c 6f 62 73 20 74 68 65 6e 20 6d 65 6d 63 6d blobs then memcm
1ce6d 70 28 29 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 p() is.** used t
1ce6e 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
1ce6f 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 63 results of the c
1ce70 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 62 omparison. If b
1ce71 6f 74 68 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 oth values.** ar
1ce72 65 20 74 65 78 74 2c 20 74 68 65 6e 20 74 68 65 e text, then the
1ce73 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 6f 6c appropriate col
1ce74 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 lating function
1ce75 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 specified in.**
1ce76 50 34 20 69 73 20 20 75 73 65 64 20 74 6f 20 64 P4 is used to d
1ce77 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e o the comparison
1ce78 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 . If P4 is not
1ce79 73 70 65 63 69 66 69 65 64 20 74 68 65 6e 0a 2a specified then.*
1ce7a 2a 20 6d 65 6d 63 6d 70 28 29 20 69 73 20 75 73 * memcmp() is us
1ce7b 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 65 ed to compare te
1ce7c 78 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 62 xt string. If b
1ce7d 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 0a 2a oth values are.*
1ce7e 2a 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 6e 20 * numeric, then
1ce7f 61 20 6e 75 6d 65 72 69 63 20 63 6f 6d 70 61 72 a numeric compar
1ce80 69 73 6f 6e 20 69 73 20 75 73 65 64 2e 20 49 66 ison is used. If
1ce81 20 74 68 65 20 74 77 6f 20 76 61 6c 75 65 73 0a the two values.
1ce82 2a 2a 20 61 72 65 20 6f 66 20 64 69 66 66 65 72 ** are of differ
1ce83 65 6e 74 20 74 79 70 65 73 2c 20 74 68 65 6e 20 ent types, then
1ce84 6e 75 6d 62 65 72 73 20 61 72 65 20 63 6f 6e 73 numbers are cons
1ce85 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e idered less than
1ce86 0a 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20 .** strings and
1ce87 73 74 72 69 6e 67 73 20 61 72 65 20 63 6f 6e 73 strings are cons
1ce88 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e idered less than
1ce89 20 62 6c 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 blobs..**.** If
1ce8a 20 74 68 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 the SQLITE_STOR
1ce8b 45 50 32 20 62 69 74 20 6f 66 20 50 35 20 69 73 EP2 bit of P5 is
1ce8c 20 73 65 74 2c 20 74 68 65 6e 20 64 6f 20 6e 6f set, then do no
1ce8d 74 20 6a 75 6d 70 2e 20 20 49 6e 73 74 65 61 64 t jump. Instead
1ce8e 2c 0a 2a 2a 20 73 74 6f 72 65 20 61 20 62 6f 6f ,.** store a boo
1ce8f 6c 65 61 6e 20 72 65 73 75 6c 74 20 28 65 69 74 lean result (eit
1ce90 68 65 72 20 30 2c 20 6f 72 20 31 2c 20 6f 72 20 her 0, or 1, or
1ce91 4e 55 4c 4c 29 20 69 6e 20 72 65 67 69 73 74 65 NULL) in registe
1ce92 72 20 50 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f r P2..*/./* Opco
1ce93 64 65 3a 20 4e 65 20 50 31 20 50 32 20 50 33 20 de: Ne P1 P2 P3
1ce94 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 P4 P5.**.** This
1ce95 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 works just like
1ce96 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 the Lt opcode e
1ce97 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a xcept that the j
1ce98 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a ump is taken if.
1ce99 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 ** the operands
1ce9a 69 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 20 in registers P1
1ce9b 61 6e 64 20 50 33 20 61 72 65 20 6e 6f 74 20 65 and P3 are not e
1ce9c 71 75 61 6c 2e 20 20 53 65 65 20 74 68 65 20 4c qual. See the L
1ce9d 74 20 6f 70 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 t opcode for.**
1ce9e 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
1ce9f 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 mation..*/./* Op
1cea0 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32 20 50 code: Eq P1 P2 P
1cea1 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 3 P4 P5.**.** Th
1cea2 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 is works just li
1cea3 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 ke the Lt opcode
1cea4 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
1cea5 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 jump is taken i
1cea6 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 f.** the operand
1cea7 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 20 50 s in registers P
1cea8 31 20 61 6e 64 20 50 33 20 61 72 65 20 65 71 75 1 and P3 are equ
1cea9 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c al..** See the L
1ceaa 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 t opcode for add
1ceab 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
1ceac 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ion..*/./* Opcod
1cead 65 3a 20 4c 65 20 50 31 20 50 32 20 50 33 20 50 e: Le P1 P2 P3 P
1ceae 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 4 P5.**.** This
1ceaf 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 works just like
1ceb0 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 the Lt opcode ex
1ceb1 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 cept that the ju
1ceb2 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a mp is taken if.*
1ceb3 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 * the content of
1ceb4 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 register P3 is
1ceb5 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 less than or equ
1ceb6 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e al to the conten
1ceb7 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 t of.** register
1ceb8 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 P1. See the Lt
1ceb9 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 opcode for addi
1ceba 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
1cebb 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 on..*/./* Opcode
1cebc 3a 20 47 74 20 50 31 20 50 32 20 50 33 20 50 34 : Gt P1 P2 P3 P4
1cebd 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 P5.**.** This w
1cebe 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 orks just like t
1cebf 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 he Lt opcode exc
1cec0 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d ept that the jum
1cec1 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a p is taken if.**
1cec2 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
1cec3 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 67 register P3 is g
1cec4 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
1cec5 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 content of.** re
1cec6 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 gister P1. See
1cec7 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f the Lt opcode fo
1cec8 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
1cec9 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 ormation..*/./*
1ceca 4f 70 63 6f 64 65 3a 20 47 65 20 50 31 20 50 32 Opcode: Ge P1 P2
1cecb 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1cecc 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 This works just
1cecd 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f like the Lt opco
1cece 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 de except that t
1cecf 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1ced0 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 if.** the conte
1ced1 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 nt of register P
1ced2 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 3 is greater tha
1ced3 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 n or equal to th
1ced4 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 e content of.**
1ced5 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 register P1. Se
1ced6 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 e the Lt opcode
1ced7 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
1ced8 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 nformation..*/.c
1ced9 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 ase OP_Eq:
1ceda 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1cedb 20 61 73 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c as TK_EQ, jump,
1cedc 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 in1, in3 */.cas
1cedd 65 20 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 e OP_Ne:
1cede 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1cedf 73 20 54 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 s TK_NE, jump, i
1cee0 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 n1, in3 */.case
1cee1 4f 50 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20 OP_Lt:
1cee2 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cee3 54 4b 5f 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 TK_LT, jump, in1
1cee4 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 , in3 */.case OP
1cee5 5f 4c 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 _Le:
1cee6 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1cee7 5f 4c 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 _LE, jump, in1,
1cee8 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 in3 */.case OP_G
1cee9 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t:
1ceea 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 /* same as TK_G
1ceeb 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e T, jump, in1, in
1ceec 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 3 */.case OP_Ge:
1ceed 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1ceee 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c * same as TK_GE,
1ceef 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
1cef0 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1cef1 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1cef2 65 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a ed into u.ai */.
1cef3 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69 int flags;. i
1cef4 6e 74 20 72 65 73 3b 0a 20 20 63 68 61 72 20 61 nt res;. char a
1cef5 66 66 69 6e 69 74 79 3b 0a 23 65 6e 64 69 66 20 ffinity;.#endif
1cef6 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cef7 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cef8 61 69 20 2a 2f 0a 0a 20 20 75 2e 61 69 2e 66 6c ai */.. u.ai.fl
1cef9 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 ags = pIn1->flag
1cefa 73 7c 70 49 6e 33 2d 3e 66 6c 61 67 73 3b 0a 0a s|pIn3->flags;..
1cefb 20 20 69 66 28 20 75 2e 61 69 2e 66 6c 61 67 73 if( u.ai.flags
1cefc 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 &MEM_Null ){.
1cefd 20 2f 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70 /* If either op
1cefe 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 erand is NULL th
1ceff 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
1cf00 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 always NULL..
1cf01 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 73 ** The jump is
1cf02 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53 51 taken if the SQ
1cf03 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 LITE_JUMPIFNULL
1cf04 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20 20 bit is set..
1cf05 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e */. if( pOp->
1cf06 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 p5 & SQLITE_STOR
1cf07 45 50 32 20 29 7b 0a 20 20 20 20 20 20 70 4f 75 EP2 ){. pOu
1cf08 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 t = &p->aMem[pOp
1cf09 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 4d 65 6d ->p2];. Mem
1cf0a 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1cf0b 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 20 , MEM_Null);.
1cf0c 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 REGISTER_TRAC
1cf0d 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 E(pOp->p2, pOut)
1cf0e 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1cf0f 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 pOp->p5 & SQLITE
1cf10 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29 7b 0a 20 _JUMPIFNULL ){.
1cf11 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1cf12 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 2-1;. }. b
1cf13 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 75 2e 61 reak;. }.. u.a
1cf14 69 2e 61 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 i.affinity = pOp
1cf15 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 ->p5 & SQLITE_AF
1cf16 46 5f 4d 41 53 4b 3b 0a 20 20 69 66 28 20 75 2e F_MASK;. if( u.
1cf17 61 69 2e 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 ai.affinity ){.
1cf18 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 applyAffinity
1cf19 28 70 49 6e 31 2c 20 75 2e 61 69 2e 61 66 66 69 (pIn1, u.ai.affi
1cf1a 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b nity, encoding);
1cf1b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 . applyAffini
1cf1c 74 79 28 70 49 6e 33 2c 20 75 2e 61 69 2e 61 66 ty(pIn3, u.ai.af
1cf1d 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 finity, encoding
1cf1e 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
1cf1f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f allocFailed ) go
1cf20 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a to no_mem;. }..
1cf21 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1cf22 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 4type==P4_COLLSE
1cf23 51 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f Q || pOp->p4.pCo
1cf24 6c 6c 3d 3d 30 20 29 3b 0a 20 20 45 78 70 61 6e ll==0 );. Expan
1cf25 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 45 dBlob(pIn1);. E
1cf26 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b xpandBlob(pIn3);
1cf27 0a 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 73 71 . u.ai.res = sq
1cf28 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 lite3MemCompare(
1cf29 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d pIn3, pIn1, pOp-
1cf2a 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 73 77 >p4.pColl);. sw
1cf2b 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 itch( pOp->opcod
1cf2c 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 e ){. case OP
1cf2d 5f 45 71 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 _Eq: u.ai.res
1cf2e 20 3d 20 75 2e 61 69 2e 72 65 73 3d 3d 30 3b 20 = u.ai.res==0;
1cf2f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
1cf30 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 20 75 2e ase OP_Ne: u.
1cf31 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 ai.res = u.ai.re
1cf32 73 21 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b s!=0; break;
1cf33 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 74 3a . case OP_Lt:
1cf34 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 u.ai.res = u
1cf35 2e 61 69 2e 72 65 73 3c 30 3b 20 20 20 20 20 20 .ai.res<0;
1cf36 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1cf37 4f 50 5f 4c 65 3a 20 20 20 20 75 2e 61 69 2e 72 OP_Le: u.ai.r
1cf38 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c 3d 30 es = u.ai.res<=0
1cf39 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
1cf3a 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 case OP_Gt:
1cf3b 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e u.ai.res = u.ai.
1cf3c 72 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65 61 res>0; brea
1cf3d 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 k;. default:
1cf3e 20 20 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d u.ai.res =
1cf3f 20 75 2e 61 69 2e 72 65 73 3e 3d 30 3b 20 20 20 u.ai.res>=0;
1cf40 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 break;. }..
1cf41 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 if( pOp->p5 & SQ
1cf42 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a LITE_STOREP2 ){.
1cf43 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 pOut = &p->a
1cf44 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1cf45 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1cf46 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1cf47 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d . pOut->u.i =
1cf48 20 75 2e 61 69 2e 72 65 73 3b 0a 20 20 20 20 52 u.ai.res;. R
1cf49 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1cf4a 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 p->p2, pOut);.
1cf4b 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 69 2e 72 }else if( u.ai.r
1cf4c 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 es ){. pc = p
1cf4d 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 Op->p2-1;. }.
1cf4e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1cf4f 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69 6f 6e ode: Permutation
1cf50 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * * * P4 *.**.*
1cf51 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d 75 74 * Set the permut
1cf52 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 ation used by th
1cf53 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 e OP_Compare ope
1cf54 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 65 20 rator to be the
1cf55 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e 74 65 array.** of inte
1cf56 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a 0a 2a gers in P4..**.*
1cf57 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 69 6f * The permutatio
1cf58 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 n is only valid
1cf59 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 4f until the next O
1cf5a 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 4f P_Permutation, O
1cf5b 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 4f 50 P_Compare,.** OP
1cf5c 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 65 73 _Halt, or OP_Res
1cf5d 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 61 6c ultRow. Typical
1cf5e 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d 75 74 ly the OP_Permut
1cf5f 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 63 63 ation should occ
1cf60 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c ur.** immediatel
1cf61 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 4f y prior to the O
1cf62 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a 63 61 P_Compare..*/.ca
1cf63 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f se OP_Permutatio
1cf64 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 n: {. assert( p
1cf65 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 Op->p4type==P4_I
1cf66 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 73 73 NTARRAY );. ass
1cf67 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 69 20 ert( pOp->p4.ai
1cf68 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 3d 20 );. aPermute =
1cf69 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 62 72 pOp->p4.ai;. br
1cf6a 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cf6b 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 50 32 e: Compare P1 P2
1cf6c 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 P3 P4 *.**.** C
1cf6d 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 6f 72 ompare to vector
1cf6e 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 69 s of registers i
1cf6f 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 28 50 n reg(P1)..reg(P
1cf70 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 68 69 1+P3-1) (all thi
1cf71 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 61 6e s.** one "A") an
1cf72 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72 65 d in reg(P2)..re
1cf73 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22 29 g(P2+P3-1) ("B")
1cf74 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73 75 . Save the resu
1cf75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6d lt of.** the com
1cf76 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65 20 parison for use
1cf77 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f 4a by the next OP_J
1cf78 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a 2a ump instruct..**
1cf79 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79 49 .** P4 is a KeyI
1cf7a 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 nfo structure th
1cf7b 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c 61 at defines colla
1cf7c 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 ting sequences a
1cf7d 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65 72 nd sort.** order
1cf7e 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72 s for the compar
1cf7f 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d 75 ison. The permu
1cf80 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20 74 tation applies t
1cf81 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 6f o registers.** o
1cf82 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 nly. The KeyInf
1cf83 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 75 o elements are u
1cf84 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c 79 sed sequentially
1cf85 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 70 ..**.** The comp
1cf86 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72 74 arison is a sort
1cf87 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f 20 comparison, so
1cf88 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65 71 NULLs compare eq
1cf89 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61 72 ual,.** NULLs ar
1cf8a 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d 62 e less than numb
1cf8b 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72 65 ers, numbers are
1cf8c 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69 6e less than strin
1cf8d 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69 6e gs,.** and strin
1cf8e 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e gs are less than
1cf8f 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65 20 blobs..*/.case
1cf90 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 23 69 OP_Compare: {.#i
1cf91 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1cf92 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1cf93 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20 69 6e 74 to u.aj */. int
1cf94 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 n;. int i;. i
1cf95 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 70 32 3b nt p1;. int p2;
1cf96 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f . const KeyInfo
1cf97 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e *pKeyInfo;. in
1cf98 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c 53 65 71 t idx;. CollSeq
1cf99 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 *pColl; /* C
1cf9a 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1cf9b 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 e to use on this
1cf9c 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 62 term */. int b
1cf9d 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Rev; /*
1cf9e 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e True for DESCEN
1cf9f 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20 DING sort order
1cfa0 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1cfa1 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1cfa2 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a ed into u.aj */.
1cfa3 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 70 4f 70 2d . u.aj.n = pOp-
1cfa4 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e 70 4b 65 79 >p3;. u.aj.pKey
1cfa5 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Info = pOp->p4.p
1cfa6 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72 KeyInfo;. asser
1cfa7 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 29 3b 0a 20 t( u.aj.n>0 );.
1cfa8 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 4b assert( u.aj.pK
1cfa9 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 75 eyInfo!=0 );. u
1cfaa 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 .aj.p1 = pOp->p1
1cfab 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a ;. assert( u.aj
1cfac 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31 .p1>0 && u.aj.p1
1cfad 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d +u.aj.n<=p->nMem
1cfae 2b 31 20 29 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 +1 );. u.aj.p2
1cfaf 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 = pOp->p2;. ass
1cfb0 65 72 74 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 ert( u.aj.p2>0 &
1cfb1 26 20 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 6e & u.aj.p2+u.aj.n
1cfb2 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 <=p->nMem+1 );.
1cfb3 20 66 6f 72 28 75 2e 61 6a 2e 69 3d 30 3b 20 75 for(u.aj.i=0; u
1cfb4 2e 61 6a 2e 69 3c 75 2e 61 6a 2e 6e 3b 20 75 2e .aj.i<u.aj.n; u.
1cfb5 61 6a 2e 69 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 aj.i++){. u.a
1cfb6 6a 2e 69 64 78 20 3d 20 61 50 65 72 6d 75 74 65 j.idx = aPermute
1cfb7 20 3f 20 61 50 65 72 6d 75 74 65 5b 75 2e 61 6a ? aPermute[u.aj
1cfb8 2e 69 5d 20 3a 20 75 2e 61 6a 2e 69 3b 0a 20 20 .i] : u.aj.i;.
1cfb9 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1cfba 28 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 (u.aj.p1+u.aj.id
1cfbb 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a x, &p->aMem[u.aj
1cfbc 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 5d 29 3b 0a .p1+u.aj.idx]);.
1cfbd 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1cfbe 43 45 28 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e CE(u.aj.p2+u.aj.
1cfbf 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e idx, &p->aMem[u.
1cfc0 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 5d 29 aj.p2+u.aj.idx])
1cfc1 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e ;. assert( u.
1cfc2 61 6a 2e 69 3c 75 2e 61 6a 2e 70 4b 65 79 49 6e aj.i<u.aj.pKeyIn
1cfc3 66 6f 2d 3e 6e 46 69 65 6c 64 20 29 3b 0a 20 20 fo->nField );.
1cfc4 20 20 75 2e 61 6a 2e 70 43 6f 6c 6c 20 3d 20 75 u.aj.pColl = u
1cfc5 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 .aj.pKeyInfo->aC
1cfc6 6f 6c 6c 5b 75 2e 61 6a 2e 69 5d 3b 0a 20 20 20 oll[u.aj.i];.
1cfc7 20 75 2e 61 6a 2e 62 52 65 76 20 3d 20 75 2e 61 u.aj.bRev = u.a
1cfc8 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 j.pKeyInfo->aSor
1cfc9 74 4f 72 64 65 72 5b 75 2e 61 6a 2e 69 5d 3b 0a tOrder[u.aj.i];.
1cfca 20 20 20 20 69 43 6f 6d 70 61 72 65 20 3d 20 73 iCompare = s
1cfcb 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 qlite3MemCompare
1cfcc 28 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 (&p->aMem[u.aj.p
1cfcd 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20 26 70 2d 1+u.aj.idx], &p-
1cfce 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e >aMem[u.aj.p2+u.
1cfcf 61 6a 2e 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43 aj.idx], u.aj.pC
1cfd0 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 oll);. if( iC
1cfd1 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20 ompare ){.
1cfd2 69 66 28 20 75 2e 61 6a 2e 62 52 65 76 20 29 20 if( u.aj.bRev )
1cfd3 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d iCompare = -iCom
1cfd4 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61 pare;. brea
1cfd5 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 k;. }. }. a
1cfd6 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 Permute = 0;. b
1cfd7 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cfd8 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50 de: Jump P1 P2 P
1cfd9 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1cfda 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 to the instruct
1cfdb 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 ion at address P
1cfdc 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 1, P2, or P3 dep
1cfdd 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
1cfde 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 r.** in the most
1cfdf 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 recent OP_Compa
1cfe0 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 re instruction t
1cfe1 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 he P1 vector was
1cfe2 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 less than.** eq
1cfe3 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
1cfe4 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76 er than the P2 v
1cfe5 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76 ector, respectiv
1cfe6 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ely..*/.case OP_
1cfe7 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20 Jump: {
1cfe8 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 /* jump */.
1cfe9 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 if( iCompare<0
1cfea 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1cfeb 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 >p1 - 1;. }else
1cfec 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 if( iCompare==0
1cfed 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1cfee 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 ->p2 - 1;. }els
1cfef 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d e{. pc = pOp-
1cff0 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p3 - 1;. }. b
1cff1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1cff2 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33 de: And P1 P2 P3
1cff3 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 * *.**.** Take
1cff4 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 the logical AND
1cff5 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
1cff6 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1cff7 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 d P2 and.** writ
1cff8 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 e the result int
1cff9 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a o register P3..*
1cffa 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 *.** If either P
1cffb 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61 1 or P2 is 0 (fa
1cffc 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 lse) then the re
1cffd 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69 sult is 0 even i
1cffe 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 f.** the other i
1cfff 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 nput is NULL. A
1d000 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f NULL and true o
1d001 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 r two NULLs give
1d002 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 .** a NULL outpu
1d003 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a t..*/./* Opcode:
1d004 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a Or P1 P2 P3 * *
1d005 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 .**.** Take the
1d006 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 logical OR of th
1d007 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1d008 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 ster P1 and P2 a
1d009 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 nd.** store the
1d00a 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74 answer in regist
1d00b 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P3..**.** If
1d00c 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 either P1 or P2
1d00d 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 is nonzero (true
1d00e 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c ) then the resul
1d00f 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a t is 1 (true).**
1d010 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68 even if the oth
1d011 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1d012 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 . A NULL and fa
1d013 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 lse or two NULLs
1d014 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 .** give a NULL
1d015 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 output..*/.case
1d016 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 OP_And:
1d017 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d018 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 TK_AND, in1, in2
1d019 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1d01a 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20 P_Or: {
1d01b 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d01c 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 K_OR, in1, in2,
1d01d 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f out3 */.#if 0 /
1d01e 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d01f 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d020 6b 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20 k */. int v1;
1d021 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e /* Left operan
1d022 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d d: 0==FALSE, 1=
1d023 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 =TRUE, 2==UNKNOW
1d024 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 N or NULL */. i
1d025 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 nt v2; /* Rig
1d026 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 ht operand: 0==F
1d027 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1d028 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1d029 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c L */.#endif /* l
1d02a 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d02b 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a oved into u.ak *
1d02c 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 /.. if( pIn1->f
1d02d 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1d02e 29 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d ){. u.ak.v1 =
1d02f 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 2;. }else{.
1d030 20 75 2e 61 6b 2e 76 31 20 3d 20 73 71 6c 69 74 u.ak.v1 = sqlit
1d031 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 e3VdbeIntValue(p
1d032 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 In1)!=0;. }. i
1d033 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 f( pIn2->flags &
1d034 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1d035 20 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a 20 20 u.ak.v2 = 2;.
1d036 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e }else{. u.ak.
1d037 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 v2 = sqlite3Vdbe
1d038 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d IntValue(pIn2)!=
1d039 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 0;. }. if( pOp
1d03a 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 ->opcode==OP_And
1d03b 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 ){. static c
1d03c 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1d03d 61 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d ar and_logic[] =
1d03e 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 { 0, 0, 0, 0, 1
1d03f 2c 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a , 2, 0, 2, 2 };.
1d040 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 61 6e u.ak.v1 = an
1d041 64 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a d_logic[u.ak.v1*
1d042 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 65 3+u.ak.v2];. }e
1d043 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 lse{. static
1d044 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1d045 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d har or_logic[] =
1d046 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 { 0, 1, 2, 1, 1
1d047 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a , 1, 2, 1, 2 };.
1d048 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 6f 72 u.ak.v1 = or
1d049 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a 33 _logic[u.ak.v1*3
1d04a 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 0a 20 +u.ak.v2];. }.
1d04b 20 69 66 28 20 75 2e 61 6b 2e 76 31 3d 3d 32 20 if( u.ak.v1==2
1d04c 29 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 ){. MemSetTyp
1d04d 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1d04e 4e 75 6c 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Null);. }else{.
1d04f 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 pOut->u.i =
1d050 75 2e 61 6b 2e 76 31 3b 0a 20 20 20 20 4d 65 6d u.ak.v1;. Mem
1d051 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 SetTypeFlag(pOut
1d052 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a , MEM_Int);. }.
1d053 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d054 70 63 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50 32 pcode: Not P1 P2
1d055 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 * * *.**.** Int
1d056 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75 65 erpret the value
1d057 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d058 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c as a boolean val
1d059 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a ue. Store the.*
1d05a 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c 65 * boolean comple
1d05b 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65 72 ment in register
1d05c 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61 6c P2. If the val
1d05d 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1d05e 31 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 1 is .** NULL, t
1d05f 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20 73 74 hen a NULL is st
1d060 6f 72 65 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 ored in P2..*/.c
1d061 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20 ase OP_Not: {
1d062 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d063 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20 same as TK_NOT,
1d064 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d 20 in1 */. pOut =
1d065 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1d066 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 ];. if( pIn1->f
1d067 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1d068 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1d069 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 beMemSetNull(pOu
1d06a 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
1d06b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1d06c 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 21 73 etInt64(pOut, !s
1d06d 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1d06e 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 ue(pIn1));. }.
1d06f 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d070 63 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 20 code: BitNot P1
1d071 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 P2 * * *.**.** I
1d072 6e 74 65 72 70 72 65 74 20 74 68 65 20 63 6f 6e nterpret the con
1d073 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 tent of register
1d074 20 50 31 20 61 73 20 61 6e 20 69 6e 74 65 67 65 P1 as an intege
1d075 72 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a 2a r. Store the.**
1d076 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74 ones-complement
1d077 20 6f 66 20 74 68 65 20 50 31 20 76 61 6c 75 65 of the P1 value
1d078 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1d079 32 2e 20 20 49 66 20 50 31 20 68 6f 6c 64 73 0a 2. If P1 holds.
1d07a 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20 73 ** a NULL then s
1d07b 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 50 tore a NULL in P
1d07c 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 2..*/.case OP_Bi
1d07d 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 tNot: {
1d07e 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d07f 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a 2f K_BITNOT, in1 */
1d080 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1d081 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 em[pOp->p2];. i
1d082 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 f( pIn1->flags &
1d083 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1d084 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1d085 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 etNull(pOut);.
1d086 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
1d087 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 e3VdbeMemSetInt6
1d088 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 33 4(pOut, ~sqlite3
1d089 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e VdbeIntValue(pIn
1d08a 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 1));. }. break
1d08b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d08c 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a If P1 P2 P3 * *.
1d08d 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 **.** Jump to P2
1d08e 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e if the value in
1d08f 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1d090 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 true. The value
1d091 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 is.** is consid
1d092 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 ered true if it
1d093 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e is numeric and n
1d094 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 on-zero. If the
1d095 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1d096 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1d097 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 e the jump if P3
1d098 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20 is true..*/./*
1d099 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31 Opcode: IfNot P1
1d09a 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1d09b 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 Jump to P2 if t
1d09c 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1d09d 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65 ster P1 is False
1d09e 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a . The value is.
1d09f 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ** is considered
1d0a0 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20 true if it has
1d0a1 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 a numeric value
1d0a2 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65 of zero. If the
1d0a3 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 value.** in P1
1d0a4 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b is NULL then tak
1d0a5 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33 e the jump if P3
1d0a6 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73 is true..*/.cas
1d0a7 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20 e OP_If:
1d0a8 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 /* jump
1d0a9 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50 , in1 */.case OP
1d0aa 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 _IfNot: {
1d0ab 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1d0ac 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 1 */.#if 0 /* l
1d0ad 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d0ae 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a oved into u.al *
1d0af 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65 6e 64 69 /. int c;.#endi
1d0b0 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1d0b1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d0b2 75 2e 61 6c 20 2a 2f 0a 20 20 69 66 28 20 70 49 u.al */. if( pI
1d0b3 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1d0b4 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6c Null ){. u.al
1d0b5 2e 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 .c = pOp->p3;.
1d0b6 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51 }else{.#ifdef SQ
1d0b7 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
1d0b8 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 75 2e 61 NG_POINT. u.a
1d0b9 6c 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 l.c = sqlite3Vdb
1d0ba 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 eIntValue(pIn1)!
1d0bb 3d 30 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 2e =0;.#else. u.
1d0bc 61 6c 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64 al.c = sqlite3Vd
1d0bd 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31 beRealValue(pIn1
1d0be 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 )!=0.0;.#endif.
1d0bf 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f if( pOp->opco
1d0c0 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 75 de==OP_IfNot ) u
1d0c1 2e 61 6c 2e 63 20 3d 20 21 75 2e 61 6c 2e 63 3b .al.c = !u.al.c;
1d0c2 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 6c 2e . }. if( u.al.
1d0c3 63 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f c ){. pc = pO
1d0c4 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 p->p2-1;. }. b
1d0c5 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d0c6 64 65 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50 32 de: IsNull P1 P2
1d0c7 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d * * *.**.** Jum
1d0c8 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 p to P2 if the v
1d0c9 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1d0ca 20 50 31 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a P1 is NULL..*/.
1d0cb 63 61 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 case OP_IsNull:
1d0cc 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1d0cd 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c same as TK_ISNUL
1d0ce 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a L, jump, in1 */.
1d0cf 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1d0d0 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d gs & MEM_Null)!=
1d0d1 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 0 ){. pc = pO
1d0d2 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 p->p2 - 1;. }.
1d0d3 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d0d4 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31 code: NotNull P1
1d0d5 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1d0d6 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 Jump to P2 if th
1d0d7 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1d0d8 74 65 72 20 50 31 20 69 73 20 6e 6f 74 20 4e 55 ter P1 is not NU
1d0d9 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 LL. .*/.case OP
1d0da 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 _NotNull: {
1d0db 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1d0dc 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75 s TK_NOTNULL, ju
1d0dd 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 mp, in1 */. if(
1d0de 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1d0df 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a MEM_Null)==0 ){.
1d0e0 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1d0e1 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1d0e2 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d0e3 20 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 Column P1 P2 P3
1d0e4 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 P4 P5.**.** Int
1d0e5 65 72 70 72 65 74 20 74 68 65 20 64 61 74 61 20 erpret the data
1d0e6 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 that cursor P1 p
1d0e7 6f 69 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74 oints to as a st
1d0e8 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75 73 ructure built us
1d0e9 69 6e 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52 ing.** the MakeR
1d0ea 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f ecord instructio
1d0eb 6e 2e 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b n. (See the Mak
1d0ec 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66 eRecord opcode f
1d0ed 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a or additional.**
1d0ee 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
1d0ef 75 74 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 ut the format of
1d0f0 20 74 68 65 20 64 61 74 61 2e 29 20 20 45 78 74 the data.) Ext
1d0f1 72 61 63 74 20 74 68 65 20 50 32 2d 74 68 20 63 ract the P2-th c
1d0f2 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 olumn.** from th
1d0f3 69 73 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74 is record. If t
1d0f4 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 74 68 here are less th
1d0f5 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61 at (P2+1) .** va
1d0f6 6c 75 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f lues in the reco
1d0f7 72 64 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55 rd, extract a NU
1d0f8 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 LL..**.** The va
1d0f9 6c 75 65 20 65 78 74 72 61 63 74 65 64 20 69 73 lue extracted is
1d0fa 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1d0fb 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 ter P3..**.** If
1d0fc 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 the column cont
1d0fd 61 69 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20 ains fewer than
1d0fe 50 32 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 P2 fields, then
1d0ff 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20 extract a NULL.
1d100 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50 Or,.** if the P
1d101 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 4 argument is a
1d102 50 34 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76 P4_MEM use the v
1d103 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 20 61 alue of the P4 a
1d104 72 67 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68 rgument as.** th
1d105 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 e result..**.**
1d106 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 43 4c If the OPFLAG_CL
1d107 45 41 52 43 41 43 48 45 20 62 69 74 20 69 73 20 EARCACHE bit is
1d108 73 65 74 20 6f 6e 20 50 35 20 61 6e 64 20 50 31 set on P5 and P1
1d109 20 69 73 20 61 20 70 73 65 75 64 6f 2d 74 61 62 is a pseudo-tab
1d10a 6c 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 le cursor,.** th
1d10b 65 6e 20 74 68 65 20 63 61 63 68 65 20 6f 66 20 en the cache of
1d10c 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 72 65 the cursor is re
1d10d 73 65 74 20 70 72 69 6f 72 20 74 6f 20 65 78 74 set prior to ext
1d10e 72 61 63 74 69 6e 67 20 74 68 65 20 63 6f 6c 75 racting the colu
1d10f 6d 6e 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 mn..** The first
1d110 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61 67 61 69 6e OP_Column again
1d111 73 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c st a pseudo-tabl
1d112 65 20 61 66 74 65 72 20 74 68 65 20 76 61 6c 75 e after the valu
1d113 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 e of the content
1d114 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 68 61 73 .** register has
1d115 20 63 68 61 6e 67 65 64 20 73 68 6f 75 6c 64 20 changed should
1d116 68 61 76 65 20 74 68 69 73 20 62 69 74 20 73 65 have this bit se
1d117 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f t..*/.case OP_Co
1d118 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f lumn: {.#if 0 /
1d119 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d11a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d11b 6d 20 2a 2f 0a 20 20 75 33 32 20 70 61 79 6c 6f m */. u32 paylo
1d11c 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d adSize; /* Num
1d11d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1d11e 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1d11f 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 i64 payloadSize6
1d120 34 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4; /* Number of
1d121 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 bytes in the rec
1d122 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b ord */. int p1;
1d123 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1d124 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 1 value of the o
1d125 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 pcode */. int p
1d126 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 2; /*
1d127 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 column number t
1d128 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 o retrieve */.
1d129 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 VdbeCursor *pC;
1d12a 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 /* The VDBE c
1d12b 75 72 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 ursor */. char
1d12c 2a 7a 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a *zRec; /*
1d12d 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 Pointer to comp
1d12e 6c 65 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 lete record-data
1d12f 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a */. BtCursor *
1d130 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 pCrsr; /* The
1d131 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a BTree cursor */.
1d132 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 u32 *aType;
1d133 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d /* aType[i]
1d134 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 holds the numer
1d135 69 63 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 ic type of the i
1d136 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 -th column */.
1d137 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 u32 *aOffset;
1d138 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d /* aOffset[i]
1d139 20 69 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 is offset to st
1d13a 61 72 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 art of data for
1d13b 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 i-th column */.
1d13c 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 int nField;
1d13d 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 /* number of
1d13e 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 fields in the r
1d13f 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c ecord */. int l
1d140 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a en; /*
1d141 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 The length of t
1d142 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 he serialized da
1d143 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d ta for the colum
1d144 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 n */. int i;
1d145 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1d146 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 p counter */. c
1d147 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 har *zData;
1d148 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 /* Part of the
1d149 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 record being de
1d14a 63 6f 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a coded */. Mem *
1d14b 70 44 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a pDest; /*
1d14c 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 Where to write
1d14d 74 68 65 20 65 78 74 72 61 63 74 65 64 20 76 61 the extracted va
1d14e 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65 lue */. Mem sMe
1d14f 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 m; /* F
1d150 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 or storing the r
1d151 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f ecord being deco
1d152 64 65 64 20 2a 2f 0a 20 20 75 38 20 2a 7a 49 64 ded */. u8 *zId
1d153 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 x; /* I
1d154 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 ndex into header
1d155 20 2a 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 48 64 */. u8 *zEndHd
1d156 72 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e r; /* Poin
1d157 74 65 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 ter to first byt
1d158 65 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64 e after the head
1d159 65 72 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 er */. u32 offs
1d15a 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 et; /* Of
1d15b 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 fset into the da
1d15c 74 61 20 2a 2f 0a 20 20 75 36 34 20 6f 66 66 73 ta */. u64 offs
1d15d 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 et64; /* 64
1d15e 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 -bit offset. 64
1d15f 20 62 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 bits needed to
1d160 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a catch overflow *
1d161 2f 0a 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 /. int szHdr;
1d162 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1d163 66 20 74 68 65 20 68 65 61 64 65 72 20 73 69 7a f the header siz
1d164 65 20 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 e field at start
1d165 20 6f 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 of record */.
1d166 69 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 int avail;
1d167 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1d168 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 bytes of availab
1d169 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 4d 65 6d le data */. Mem
1d16a 20 2a 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 *pReg;
1d16b 2f 2a 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 /* PseudoTable i
1d16c 6e 70 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f nput register */
1d16d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1d16e 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d16f 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 0a 0a into u.am */...
1d170 20 20 75 2e 61 6d 2e 70 31 20 3d 20 70 4f 70 2d u.am.p1 = pOp-
1d171 3e 70 31 3b 0a 20 20 75 2e 61 6d 2e 70 32 20 3d >p1;. u.am.p2 =
1d172 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6d pOp->p2;. u.am
1d173 2e 70 43 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 .pC = 0;. memse
1d174 74 28 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 30 2c t(&u.am.sMem, 0,
1d175 20 73 69 7a 65 6f 66 28 75 2e 61 6d 2e 73 4d 65 sizeof(u.am.sMe
1d176 6d 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 m));. assert( u
1d177 2e 61 6d 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f .am.p1<p->nCurso
1d178 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1d179 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d Op->p3>0 && pOp-
1d17a 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1d17b 20 20 75 2e 61 6d 2e 70 44 65 73 74 20 3d 20 26 u.am.pDest = &
1d17c 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1d17d 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c ;. MemSetTypeFl
1d17e 61 67 28 75 2e 61 6d 2e 70 44 65 73 74 2c 20 4d ag(u.am.pDest, M
1d17f 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 75 2e 61 6d EM_Null);. u.am
1d180 2e 7a 52 65 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a .zRec = 0;.. /*
1d181 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74 73 This block sets
1d182 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 75 2e the variable u.
1d183 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 74 am.payloadSize t
1d184 6f 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 6e o be the total n
1d185 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 79 umber of. ** by
1d186 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 tes in the recor
1d187 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 75 2e 61 d.. **. ** u.a
1d188 6d 2e 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f m.zRec is set to
1d189 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 be the complete
1d18a 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63 text of the rec
1d18b 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61 ord if it is ava
1d18c 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 65 ilable.. ** The
1d18d 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 complete record
1d18e 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20 text is always
1d18f 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73 available for ps
1d190 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a 2a eudo-tables. **
1d191 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 If the record i
1d192 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 75 s stored in a cu
1d193 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c 65 rsor, the comple
1d194 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a 20 te record text.
1d195 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 61 ** might be ava
1d196 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 75 ilable in the u
1d197 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 63 61 63 .am.pC->aRow cac
1d198 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68 74 he. Or it might
1d199 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49 66 not be.. ** If
1d19a 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e 61 the data is una
1d19b 76 61 69 6c 61 62 6c 65 2c 20 20 75 2e 61 6d 2e vailable, u.am.
1d19c 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 4e zRec is set to N
1d19d 55 4c 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 ULL.. **. ** W
1d19e 65 20 61 6c 73 6f 20 63 6f 6d 70 75 74 65 20 74 e also compute t
1d19f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1d1a0 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 63 6f umns in the reco
1d1a1 72 64 2e 20 20 46 6f 72 20 63 75 72 73 6f 72 73 rd. For cursors
1d1a2 2c 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65 ,. ** the numbe
1d1a3 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 73 20 r of columns is
1d1a4 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 stored in the Vd
1d1a5 62 65 43 75 72 73 6f 72 2e 6e 46 69 65 6c 64 20 beCursor.nField
1d1a6 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 element.. */.
1d1a7 75 2e 61 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.am.pC = p->apC
1d1a8 73 72 5b 75 2e 61 6d 2e 70 31 5d 3b 0a 20 20 61 sr[u.am.p1];. a
1d1a9 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 21 3d ssert( u.am.pC!=
1d1aa 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 0 );.#ifndef SQL
1d1ab 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1d1ac 54 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28 20 TABLE. assert(
1d1ad 75 2e 61 6d 2e 70 43 2d 3e 70 56 74 61 62 43 75 u.am.pC->pVtabCu
1d1ae 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 rsor==0 );.#endi
1d1af 66 0a 20 20 75 2e 61 6d 2e 70 43 72 73 72 20 3d f. u.am.pCrsr =
1d1b0 20 75 2e 61 6d 2e 70 43 2d 3e 70 43 75 72 73 6f u.am.pC->pCurso
1d1b1 72 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 r;. if( u.am.pC
1d1b2 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a rsr!=0 ){. /*
1d1b3 20 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 The record is s
1d1b4 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 72 65 tored in a B-Tre
1d1b5 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 e */. rc = sq
1d1b6 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d lite3VdbeCursorM
1d1b7 6f 76 65 74 6f 28 75 2e 61 6d 2e 70 43 29 3b 0a oveto(u.am.pC);.
1d1b8 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
1d1b9 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1d1ba 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75 2e rror;. if( u.
1d1bb 61 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 am.pC->nullRow )
1d1bc 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79 {. u.am.pay
1d1bd 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 loadSize = 0;.
1d1be 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6d }else if( u.am
1d1bf 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 .pC->cacheStatus
1d1c0 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20 29 7b ==p->cacheCtr ){
1d1c1 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c . u.am.payl
1d1c2 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 oadSize = u.am.p
1d1c3 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a C->payloadSize;.
1d1c4 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 u.am.zRec
1d1c5 3d 20 28 63 68 61 72 2a 29 75 2e 61 6d 2e 70 43 = (char*)u.am.pC
1d1c6 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 ->aRow;. }els
1d1c7 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 e if( u.am.pC->i
1d1c8 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 sIndex ){.
1d1c9 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1d1ca 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 treeCursorIsVali
1d1cb 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b d(u.am.pCrsr) );
1d1cc 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1d1cd 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
1d1ce 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 u.am.pCrsr, &u.a
1d1cf 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29 m.payloadSize64)
1d1d0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1d1d1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
1d1d2 20 20 20 2f 2a 20 54 72 75 65 20 62 65 63 61 75 /* True becau
1d1d3 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 se of CursorMove
1d1d4 74 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 20 to() call above
1d1d5 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 */. /* sqli
1d1d6 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c te3BtreeParseCel
1d1d7 6c 50 74 72 28 29 20 75 73 65 73 20 67 65 74 56 lPtr() uses getV
1d1d8 61 72 69 6e 74 33 32 28 29 20 74 6f 20 65 78 74 arint32() to ext
1d1d9 72 61 63 74 20 74 68 65 0a 20 20 20 20 20 20 2a ract the. *
1d1da 2a 20 70 61 79 6c 6f 61 64 20 73 69 7a 65 2c 20 * payload size,
1d1db 73 6f 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69 so it is impossi
1d1dc 62 6c 65 20 66 6f 72 20 75 2e 61 6d 2e 70 61 79 ble for u.am.pay
1d1dd 6c 6f 61 64 53 69 7a 65 36 34 20 74 6f 20 62 65 loadSize64 to be
1d1de 0a 20 20 20 20 20 20 2a 2a 20 6c 61 72 67 65 72 . ** larger
1d1df 20 74 68 61 6e 20 33 32 20 62 69 74 73 2e 20 2a than 32 bits. *
1d1e0 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 /. assert(
1d1e1 28 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a (u.am.payloadSiz
1d1e2 65 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 e64 & SQLITE_MAX
1d1e3 5f 55 33 32 29 3d 3d 28 75 36 34 29 75 2e 61 6d _U32)==(u64)u.am
1d1e4 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 29 .payloadSize64 )
1d1e5 3b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79 ;. u.am.pay
1d1e6 6c 6f 61 64 53 69 7a 65 20 3d 20 28 75 33 32 29 loadSize = (u32)
1d1e7 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d1e8 36 34 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 64;. }else{.
1d1e9 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
1d1ea 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
1d1eb 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70 43 72 73 sValid(u.am.pCrs
1d1ec 72 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d r) );. rc =
1d1ed 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 sqlite3BtreeDat
1d1ee 61 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72 73 72 aSize(u.am.pCrsr
1d1ef 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 , &u.am.payloadS
1d1f0 69 7a 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ize);. asse
1d1f1 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
1d1f2 4b 20 29 3b 20 20 20 2f 2a 20 44 61 74 61 53 69 K ); /* DataSi
1d1f3 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c ze() cannot fail
1d1f4 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 */. }. }els
1d1f5 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 70 e if( u.am.pC->p
1d1f6 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3e 30 20 seudoTableReg>0
1d1f7 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 70 52 65 67 ){. u.am.pReg
1d1f8 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6d = &p->aMem[u.am
1d1f9 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 .pC->pseudoTable
1d1fa 52 65 67 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 Reg];. assert
1d1fb 28 20 75 2e 61 6d 2e 70 52 65 67 2d 3e 66 6c 61 ( u.am.pReg->fla
1d1fc 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b gs & MEM_Blob );
1d1fd 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 . u.am.payloa
1d1fe 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 52 65 dSize = u.am.pRe
1d1ff 67 2d 3e 6e 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a g->n;. u.am.z
1d200 52 65 63 20 3d 20 75 2e 61 6d 2e 70 52 65 67 2d Rec = u.am.pReg-
1d201 3e 7a 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d >z;. u.am.pC-
1d202 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 28 >cacheStatus = (
1d203 70 4f 70 2d 3e 70 35 26 4f 50 46 4c 41 47 5f 43 pOp->p5&OPFLAG_C
1d204 4c 45 41 52 43 41 43 48 45 29 20 3f 20 43 41 43 LEARCACHE) ? CAC
1d205 48 45 5f 53 54 41 4c 45 20 3a 20 70 2d 3e 63 61 HE_STALE : p->ca
1d206 63 68 65 43 74 72 3b 0a 20 20 20 20 61 73 73 65 cheCtr;. asse
1d207 72 74 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 rt( u.am.payload
1d208 53 69 7a 65 3d 3d 30 20 7c 7c 20 75 2e 61 6d 2e Size==0 || u.am.
1d209 7a 52 65 63 21 3d 30 20 29 3b 0a 20 20 7d 65 6c zRec!=0 );. }el
1d20a 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69 se{. /* Consi
1d20b 64 65 72 20 74 68 65 20 72 6f 77 20 74 6f 20 62 der the row to b
1d20c 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 75 2e e NULL */. u.
1d20d 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d am.payloadSize =
1d20e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 0;. }.. /* If
1d20f 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a u.am.payloadSiz
1d210 65 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 e is 0, then jus
1d211 74 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a t store a NULL *
1d212 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 /. if( u.am.pay
1d213 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 loadSize==0 ){.
1d214 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e assert( u.am.
1d215 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d pDest->flags&MEM
1d216 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 67 6f 74 _Null );. got
1d217 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b o op_column_out;
1d218 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 64 . }. assert( d
1d219 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
1d21a 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3e 3d _LIMIT_LENGTH]>=
1d21b 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 0 );. if( u.am.
1d21c 70 61 79 6c 6f 61 64 53 69 7a 65 20 3e 20 28 75 payloadSize > (u
1d21d 33 32 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 32)db->aLimit[SQ
1d21e 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1d21f 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 H] ){. goto t
1d220 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 75 oo_big;. }.. u
1d221 2e 61 6d 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 61 .am.nField = u.a
1d222 6d 2e 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 m.pC->nField;.
1d223 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 32 3c assert( u.am.p2<
1d224 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 29 3b 0a 0a u.am.nField );..
1d225 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61 /* Read and pa
1d226 72 73 65 20 74 68 65 20 74 61 62 6c 65 20 68 65 rse the table he
1d227 61 64 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65 ader. Store the
1d228 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 results of the
1d229 70 61 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 parse. ** into
1d22a 74 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65 the record heade
1d22b 72 20 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f r cache fields o
1d22c 66 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20 f the cursor..
1d22d 2a 2f 0a 20 20 75 2e 61 6d 2e 61 54 79 70 65 20 */. u.am.aType
1d22e 3d 20 75 2e 61 6d 2e 70 43 2d 3e 61 54 79 70 65 = u.am.pC->aType
1d22f 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d ;. if( u.am.pC-
1d230 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d >cacheStatus==p-
1d231 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 >cacheCtr ){.
1d232 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 20 3d 20 u.am.aOffset =
1d233 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 73 65 74 u.am.pC->aOffset
1d234 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
1d235 73 73 65 72 74 28 75 2e 61 6d 2e 61 54 79 70 65 ssert(u.am.aType
1d236 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 61 76 61 69 );. u.am.avai
1d237 6c 20 3d 20 30 3b 0a 20 20 20 20 75 2e 61 6d 2e l = 0;. u.am.
1d238 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 75 2e pC->aOffset = u.
1d239 61 6d 2e 61 4f 66 66 73 65 74 20 3d 20 26 75 2e am.aOffset = &u.
1d23a 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 6e 46 am.aType[u.am.nF
1d23b 69 65 6c 64 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e ield];. u.am.
1d23c 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20 pC->payloadSize
1d23d 3d 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 = u.am.payloadSi
1d23e 7a 65 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d ze;. u.am.pC-
1d23f 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 70 >cacheStatus = p
1d240 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 20 20 ->cacheCtr;..
1d241 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 /* Figure out h
1d242 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 61 72 ow many bytes ar
1d243 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 e in the header
1d244 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e */. if( u.am.
1d245 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 75 2e zRec ){. u.
1d246 61 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e am.zData = u.am.
1d247 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b zRec;. }else{
1d248 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e . if( u.am.
1d249 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 pC->isIndex ){.
1d24a 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 u.am.zDat
1d24b 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 a = (char*)sqlit
1d24c 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 e3BtreeKeyFetch(
1d24d 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 u.am.pCrsr, &u.a
1d24e 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 m.avail);.
1d24f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 }else{. u
1d250 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 .am.zData = (cha
1d251 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44 r*)sqlite3BtreeD
1d252 61 74 61 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 ataFetch(u.am.pC
1d253 72 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c rsr, &u.am.avail
1d254 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1d255 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28 /* If KeyFetch(
1d256 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61 )/DataFetch() ma
1d257 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65 naged to get the
1d258 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c entire payload,
1d259 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74 . ** save t
1d25a 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68 he payload in th
1d25b 65 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 e u.am.pC->aRow
1d25c 63 61 63 68 65 2e 20 20 54 68 61 74 20 77 69 6c cache. That wil
1d25d 6c 20 73 61 76 65 20 75 73 20 66 72 6f 6d 0a 20 l save us from.
1d25e 20 20 20 20 20 2a 2a 20 68 61 76 69 6e 67 20 74 ** having t
1d25f 6f 20 6d 61 6b 65 20 61 64 64 69 74 69 6f 6e 61 o make additiona
1d260 6c 20 63 61 6c 6c 73 20 74 6f 20 66 65 74 63 68 l calls to fetch
1d261 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f 72 the content por
1d262 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20 20 2a 2a tion of. **
1d263 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20 the record..
1d264 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 */. asse
1d265 72 74 28 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d rt( u.am.avail>=
1d266 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75 0 );. if( u
1d267 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 .am.payloadSize
1d268 3c 3d 20 28 75 33 32 29 75 2e 61 6d 2e 61 76 61 <= (u32)u.am.ava
1d269 69 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e il ){. u.
1d26a 61 6d 2e 7a 52 65 63 20 3d 20 75 2e 61 6d 2e 7a am.zRec = u.am.z
1d26b 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 75 2e Data;. u.
1d26c 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 28 75 am.pC->aRow = (u
1d26d 38 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 8*)u.am.zData;.
1d26e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d26f 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f u.am.pC->aRo
1d270 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 w = 0;. }.
1d271 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 }. /* The
1d272 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 following assert
1d273 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c 6c 20 is true in all
1d274 63 61 73 65 73 20 61 63 63 65 70 74 20 77 68 65 cases accept whe
1d275 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 n. ** the dat
1d276 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 abase file has b
1d277 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 65 78 een corrupted ex
1d278 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a ternally.. **
1d279 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d27a 2e 7a 52 65 63 21 3d 30 20 7c 7c 20 75 2e 61 6d .zRec!=0 || u.am
1d27b 2e 61 76 61 69 6c 3e 3d 75 2e 61 6d 2e 70 61 79 .avail>=u.am.pay
1d27c 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 75 2e 61 6d loadSize || u.am
1d27d 2e 61 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a .avail>=9 ); */.
1d27e 20 20 20 20 75 2e 61 6d 2e 73 7a 48 64 72 20 3d u.am.szHdr =
1d27f 20 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 getVarint32((u8
1d280 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e *)u.am.zData, u.
1d281 61 6d 2e 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 am.offset);..
1d282 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 /* Make sure a
1d283 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
1d284 20 68 61 73 20 6e 6f 74 20 67 69 76 65 6e 20 75 has not given u
1d285 73 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 68 65 s an oversize he
1d286 61 64 65 72 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 ader.. ** Do
1d287 74 68 69 73 20 6e 6f 77 20 74 6f 20 61 76 6f 69 this now to avoi
1d288 64 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 6d 65 d an oversize me
1d289 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e mory allocation.
1d28a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1d28b 79 70 65 20 65 6e 74 72 69 65 73 20 63 61 6e 20 ype entries can
1d28c 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 be between 1 and
1d28d 20 35 20 62 79 74 65 73 20 65 61 63 68 2e 20 20 5 bytes each.
1d28e 42 75 74 20 34 20 61 6e 64 20 35 20 62 79 74 65 But 4 and 5 byte
1d28f 0a 20 20 20 20 2a 2a 20 74 79 70 65 73 20 75 73 . ** types us
1d290 65 20 73 6f 20 6d 75 63 68 20 64 61 74 61 20 73 e so much data s
1d291 70 61 63 65 20 74 68 61 74 20 74 68 65 72 65 20 pace that there
1d292 63 61 6e 20 6f 6e 6c 79 20 62 65 20 34 30 39 36 can only be 4096
1d293 20 61 6e 64 20 33 32 20 6f 66 0a 20 20 20 20 2a and 32 of. *
1d294 2a 20 74 68 65 6d 2c 20 72 65 73 70 65 63 74 69 * them, respecti
1d295 76 65 6c 79 2e 20 20 53 6f 20 74 68 65 20 6d 61 vely. So the ma
1d296 78 69 6d 75 6d 20 68 65 61 64 65 72 20 6c 65 6e ximum header len
1d297 67 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d gth results from
1d298 20 61 0a 20 20 20 20 2a 2a 20 33 2d 62 79 74 65 a. ** 3-byte
1d299 20 74 79 70 65 20 66 6f 72 20 65 61 63 68 20 6f type for each o
1d29a 66 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 f the maximum of
1d29b 20 33 32 37 36 38 20 63 6f 6c 75 6d 6e 73 20 70 32768 columns p
1d29c 6c 75 73 20 74 68 72 65 65 0a 20 20 20 20 2a 2a lus three. **
1d29d 20 65 78 74 72 61 20 62 79 74 65 73 20 66 6f 72 extra bytes for
1d29e 20 74 68 65 20 68 65 61 64 65 72 20 6c 65 6e 67 the header leng
1d29f 74 68 20 69 74 73 65 6c 66 2e 20 20 33 32 37 36 th itself. 3276
1d2a0 38 2a 33 20 2b 20 33 20 3d 20 39 38 33 30 37 2e 8*3 + 3 = 98307.
1d2a1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1d2a2 75 2e 61 6d 2e 6f 66 66 73 65 74 20 3e 20 39 38 u.am.offset > 98
1d2a3 33 30 37 20 29 7b 0a 20 20 20 20 20 20 72 63 20 307 ){. rc
1d2a4 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 = SQLITE_CORRUPT
1d2a5 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 _BKPT;. got
1d2a6 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b o op_column_out;
1d2a7 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 . }.. /* C
1d2a8 6f 6d 70 75 74 65 20 69 6e 20 75 2e 61 6d 2e 6c ompute in u.am.l
1d2a9 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 en the number of
1d2aa 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 77 bytes of data w
1d2ab 65 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 69 e need to read i
1d2ac 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 74 n order. ** t
1d2ad 6f 20 67 65 74 20 75 2e 61 6d 2e 6e 46 69 65 6c o get u.am.nFiel
1d2ae 64 20 74 79 70 65 20 76 61 6c 75 65 73 2e 20 20 d type values.
1d2af 75 2e 61 6d 2e 6f 66 66 73 65 74 20 69 73 20 61 u.am.offset is a
1d2b0 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e n upper bound on
1d2b1 20 74 68 69 73 2e 20 20 42 75 74 0a 20 20 20 20 this. But.
1d2b2 2a 2a 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 6d ** u.am.nField m
1d2b3 69 67 68 74 20 62 65 20 73 69 67 6e 69 66 69 63 ight be signific
1d2b4 61 6e 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 antly less than
1d2b5 74 68 65 20 74 72 75 65 20 6e 75 6d 62 65 72 20 the true number
1d2b6 6f 66 20 63 6f 6c 75 6d 6e 73 0a 20 20 20 20 2a of columns. *
1d2b7 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 * in the table,
1d2b8 61 6e 64 20 69 6e 20 74 68 61 74 20 63 61 73 65 and in that case
1d2b9 2c 20 35 2a 75 2e 61 6d 2e 6e 46 69 65 6c 64 2b , 5*u.am.nField+
1d2ba 33 20 6d 69 67 68 74 20 62 65 20 73 6d 61 6c 6c 3 might be small
1d2bb 65 72 20 74 68 61 6e 20 75 2e 61 6d 2e 6f 66 66 er than u.am.off
1d2bc 73 65 74 2e 0a 20 20 20 20 2a 2a 20 57 65 20 77 set.. ** We w
1d2bd 61 6e 74 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 20 ant to minimize
1d2be 75 2e 61 6d 2e 6c 65 6e 20 69 6e 20 6f 72 64 65 u.am.len in orde
1d2bf 72 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 73 r to limit the s
1d2c0 69 7a 65 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ize of the memor
1d2c1 79 0a 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 y. ** allocat
1d2c2 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 20 ion, especially
1d2c3 69 66 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 if a corrupt dat
1d2c4 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 63 abase file has c
1d2c5 61 75 73 65 64 20 75 2e 61 6d 2e 6f 66 66 73 65 aused u.am.offse
1d2c6 74 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6f t. ** to be o
1d2c7 76 65 72 73 69 7a 65 64 2e 20 4f 66 66 73 65 74 versized. Offset
1d2c8 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 39 is limited to 9
1d2c9 38 33 30 37 20 61 62 6f 76 65 2e 20 20 42 75 74 8307 above. But
1d2ca 20 39 38 33 30 37 20 6d 69 67 68 74 0a 20 20 20 98307 might.
1d2cb 20 2a 2a 20 73 74 69 6c 6c 20 65 78 63 65 65 64 ** still exceed
1d2cc 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 79 20 61 Robson memory a
1d2cd 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d 69 74 73 llocation limits
1d2ce 20 6f 6e 20 73 6f 6d 65 20 63 6f 6e 66 69 67 75 on some configu
1d2cf 72 61 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20 rations.. **
1d2d0 4f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 On systems that
1d2d1 63 61 6e 6e 6f 74 20 74 6f 6c 65 72 61 74 65 20 cannot tolerate
1d2d2 6c 61 72 67 65 20 6d 65 6d 6f 72 79 20 61 6c 6c large memory all
1d2d3 6f 63 61 74 69 6f 6e 73 2c 20 75 2e 61 6d 2e 6e ocations, u.am.n
1d2d4 46 69 65 6c 64 2a 35 2b 33 0a 20 20 20 20 2a 2a Field*5+3. **
1d2d5 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 62 65 20 will likely be
1d2d6 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 73 69 6e much smaller sin
1d2d7 63 65 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 77 ce u.am.nField w
1d2d8 69 6c 6c 20 6c 69 6b 65 6c 79 20 62 65 20 6c 65 ill likely be le
1d2d9 73 73 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 32 ss than. ** 2
1d2da 30 20 6f 72 20 73 6f 2e 20 20 54 68 69 73 20 69 0 or so. This i
1d2db 6e 73 75 72 65 73 20 74 68 61 74 20 52 6f 62 73 nsures that Robs
1d2dc 6f 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 on memory alloca
1d2dd 74 69 6f 6e 20 6c 69 6d 69 74 73 20 61 72 65 0a tion limits are.
1d2de 20 20 20 20 2a 2a 20 6e 6f 74 20 65 78 63 65 65 ** not excee
1d2df 64 65 64 20 65 76 65 6e 20 66 6f 72 20 63 6f 72 ded even for cor
1d2e0 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
1d2e1 6c 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 les.. */.
1d2e2 75 2e 61 6d 2e 6c 65 6e 20 3d 20 75 2e 61 6d 2e u.am.len = u.am.
1d2e3 6e 46 69 65 6c 64 2a 35 20 2b 20 33 3b 0a 20 20 nField*5 + 3;.
1d2e4 20 20 69 66 28 20 75 2e 61 6d 2e 6c 65 6e 20 3e if( u.am.len >
1d2e5 20 28 69 6e 74 29 75 2e 61 6d 2e 6f 66 66 73 65 (int)u.am.offse
1d2e6 74 20 29 20 75 2e 61 6d 2e 6c 65 6e 20 3d 20 28 t ) u.am.len = (
1d2e7 69 6e 74 29 75 2e 61 6d 2e 6f 66 66 73 65 74 3b int)u.am.offset;
1d2e8 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4b 65 79 .. /* The Key
1d2e9 46 65 74 63 68 28 29 20 6f 72 20 44 61 74 61 46 Fetch() or DataF
1d2ea 65 74 63 68 28 29 20 61 62 6f 76 65 20 61 72 65 etch() above are
1d2eb 20 66 61 73 74 20 61 6e 64 20 77 69 6c 6c 20 67 fast and will g
1d2ec 65 74 20 74 68 65 20 65 6e 74 69 72 65 0a 20 20 et the entire.
1d2ed 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 61 64 ** record head
1d2ee 65 72 20 69 6e 20 6d 6f 73 74 20 63 61 73 65 73 er in most cases
1d2ef 2e 20 20 42 75 74 20 74 68 65 79 20 77 69 6c 6c . But they will
1d2f0 20 66 61 69 6c 20 74 6f 20 67 65 74 20 74 68 65 fail to get the
1d2f1 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a complete. **
1d2f2 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 record header i
1d2f3 66 20 74 68 65 20 72 65 63 6f 72 64 20 68 65 61 f the record hea
1d2f4 64 65 72 20 64 6f 65 73 20 6e 6f 74 20 66 69 74 der does not fit
1d2f5 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 on a single pag
1d2f6 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 e. ** in the
1d2f7 42 2d 54 72 65 65 2e 20 20 57 68 65 6e 20 74 68 B-Tree. When th
1d2f8 61 74 20 68 61 70 70 65 6e 73 2c 20 75 73 65 20 at happens, use
1d2f9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
1d2fa 6f 6d 42 74 72 65 65 28 29 20 74 6f 0a 20 20 20 omBtree() to.
1d2fb 20 2a 2a 20 61 63 71 75 69 72 65 20 74 68 65 20 ** acquire the
1d2fc 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 20 complete header
1d2fd 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 text.. */.
1d2fe 20 69 66 28 20 21 75 2e 61 6d 2e 7a 52 65 63 20 if( !u.am.zRec
1d2ff 26 26 20 75 2e 61 6d 2e 61 76 61 69 6c 3c 75 2e && u.am.avail<u.
1d300 61 6d 2e 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 am.len ){.
1d301 75 2e 61 6d 2e 73 4d 65 6d 2e 66 6c 61 67 73 20 u.am.sMem.flags
1d302 3d 20 30 3b 0a 20 20 20 20 20 20 75 2e 61 6d 2e = 0;. u.am.
1d303 73 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 20 sMem.db = 0;.
1d304 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1d305 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1d306 75 2e 61 6d 2e 70 43 72 73 72 2c 20 30 2c 20 75 u.am.pCrsr, 0, u
1d307 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 61 6d 2e 70 43 .am.len, u.am.pC
1d308 2d 3e 69 73 49 6e 64 65 78 2c 20 26 75 2e 61 6d ->isIndex, &u.am
1d309 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 .sMem);. if
1d30a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1d30b 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
1d30c 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 op_column_out;.
1d30d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 }. u.a
1d30e 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e 73 m.zData = u.am.s
1d30f 4d 65 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 Mem.z;. }.
1d310 20 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 20 3d 20 u.am.zEndHdr =
1d311 28 75 38 20 2a 29 26 75 2e 61 6d 2e 7a 44 61 74 (u8 *)&u.am.zDat
1d312 61 5b 75 2e 61 6d 2e 6c 65 6e 5d 3b 0a 20 20 20 a[u.am.len];.
1d313 20 75 2e 61 6d 2e 7a 49 64 78 20 3d 20 28 75 38 u.am.zIdx = (u8
1d314 20 2a 29 26 75 2e 61 6d 2e 7a 44 61 74 61 5b 75 *)&u.am.zData[u
1d315 2e 61 6d 2e 73 7a 48 64 72 5d 3b 0a 0a 20 20 20 .am.szHdr];..
1d316 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68 65 61 /* Scan the hea
1d317 64 65 72 20 61 6e 64 20 75 73 65 20 69 74 20 74 der and use it t
1d318 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 75 2e o fill in the u.
1d319 61 6d 2e 61 54 79 70 65 5b 5d 20 61 6e 64 20 75 am.aType[] and u
1d31a 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 5d 0a 20 20 .am.aOffset[].
1d31b 20 20 2a 2a 20 61 72 72 61 79 73 2e 20 20 75 2e ** arrays. u.
1d31c 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d am.aType[u.am.i]
1d31d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 will contain th
1d31e 65 20 74 79 70 65 20 69 6e 74 65 67 65 72 20 66 e type integer f
1d31f 6f 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 or the u.am.i-th
1d320 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 61 . ** column a
1d321 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b nd u.am.aOffset[
1d322 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f 6e u.am.i] will con
1d323 74 61 69 6e 20 74 68 65 20 75 2e 61 6d 2e 6f 66 tain the u.am.of
1d324 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 fset from the be
1d325 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 6f ginning. ** o
1d326 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 f the record to
1d327 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
1d328 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 75 2e data for the u.
1d329 61 6d 2e 69 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20 am.i-th column.
1d32a 20 20 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6f */. u.am.o
1d32b 66 66 73 65 74 36 34 20 3d 20 75 2e 61 6d 2e 6f ffset64 = u.am.o
1d32c 66 66 73 65 74 3b 0a 20 20 20 20 66 6f 72 28 75 ffset;. for(u
1d32d 2e 61 6d 2e 69 3d 30 3b 20 75 2e 61 6d 2e 69 3c .am.i=0; u.am.i<
1d32e 75 2e 61 6d 2e 6e 46 69 65 6c 64 3b 20 75 2e 61 u.am.nField; u.a
1d32f 6d 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 m.i++){. if
1d330 28 20 75 2e 61 6d 2e 7a 49 64 78 3c 75 2e 61 6d ( u.am.zIdx<u.am
1d331 2e 7a 45 6e 64 48 64 72 20 29 7b 0a 20 20 20 20 .zEndHdr ){.
1d332 20 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 u.am.aOffset
1d333 5b 75 2e 61 6d 2e 69 5d 20 3d 20 28 75 33 32 29 [u.am.i] = (u32)
1d334 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 3b 0a 20 u.am.offset64;.
1d335 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 49 64 78 u.am.zIdx
1d336 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 += getVarint32(
1d337 75 2e 61 6d 2e 7a 49 64 78 2c 20 75 2e 61 6d 2e u.am.zIdx, u.am.
1d338 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a aType[u.am.i]);.
1d339 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 6f 66 66 u.am.off
1d33a 73 65 74 36 34 20 2b 3d 20 73 71 6c 69 74 65 33 set64 += sqlite3
1d33b 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1d33c 6e 28 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 n(u.am.aType[u.a
1d33d 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c m.i]);. }el
1d33e 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 se{. /* I
1d33f 66 20 75 2e 61 6d 2e 69 20 69 73 20 6c 65 73 73 f u.am.i is less
1d340 20 74 68 61 74 20 75 2e 61 6d 2e 6e 46 69 65 6c that u.am.nFiel
1d341 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 d, then there ar
1d342 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69 6e e less fields in
1d343 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a this. **
1d344 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65 74 record than Set
1d345 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69 63 NumColumns indic
1d346 61 74 65 64 20 74 68 65 72 65 20 61 72 65 20 63 ated there are c
1d347 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20 olumns in the.
1d348 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 ** table.
1d349 53 65 74 20 74 68 65 20 75 2e 61 6d 2e 6f 66 66 Set the u.am.off
1d34a 73 65 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72 set for any extr
1d34b 61 20 63 6f 6c 75 6d 6e 73 20 6e 6f 74 20 70 72 a columns not pr
1d34c 65 73 65 6e 74 20 69 6e 0a 20 20 20 20 20 20 20 esent in.
1d34d 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 20 74 ** the record t
1d34e 6f 20 30 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 o 0. This tells
1d34f 63 6f 64 65 20 62 65 6c 6f 77 20 74 6f 20 73 74 code below to st
1d350 6f 72 65 20 61 20 4e 55 4c 4c 0a 20 20 20 20 20 ore a NULL.
1d351 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 ** instead of
1d352 20 64 65 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 deserializing a
1d353 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 value from the
1d354 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 20 20 record..
1d355 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e */. u.am.
1d356 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 aOffset[u.am.i]
1d357 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 = 0;. }.
1d358 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 }. sqlite3Vd
1d359 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e beMemRelease(&u.
1d35a 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 75 2e am.sMem);. u.
1d35b 61 6d 2e 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20 am.sMem.flags =
1d35c 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 20 20 2f MEM_Null;.. /
1d35d 2a 20 49 66 20 77 65 20 68 61 76 65 20 72 65 61 * If we have rea
1d35e 64 20 6d 6f 72 65 20 68 65 61 64 65 72 20 64 61 d more header da
1d35f 74 61 20 74 68 61 6e 20 77 61 73 20 63 6f 6e 74 ta than was cont
1d360 61 69 6e 65 64 20 69 6e 20 74 68 65 20 68 65 61 ained in the hea
1d361 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 der,. ** or i
1d362 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
1d363 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70 65 last field appe
1d364 61 72 73 20 74 6f 20 62 65 20 70 61 73 74 20 74 ars to be past t
1d365 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 he end of the.
1d366 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20 6f 72 20 ** record, or
1d367 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 if the end of th
1d368 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70 e last field app
1d369 65 61 72 73 20 74 6f 20 62 65 20 62 65 66 6f 72 ears to be befor
1d36a 65 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a e the end. **
1d36b 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 28 of the record (
1d36c 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c 64 73 20 when all fields
1d36d 70 72 65 73 65 6e 74 29 2c 20 74 68 65 6e 20 77 present), then w
1d36e 65 20 6d 75 73 74 20 62 65 20 64 65 61 6c 69 6e e must be dealin
1d36f 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 61 20 g. ** with a
1d370 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
1d371 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1d372 20 28 75 2e 61 6d 2e 7a 49 64 78 20 3e 20 75 2e (u.am.zIdx > u.
1d373 61 6d 2e 7a 45 6e 64 48 64 72 29 7c 7c 20 28 75 am.zEndHdr)|| (u
1d374 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 3e 20 75 .am.offset64 > u
1d375 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 .am.payloadSize)
1d376 0a 20 20 20 20 20 7c 7c 20 28 75 2e 61 6d 2e 7a . || (u.am.z
1d377 49 64 78 3d 3d 75 2e 61 6d 2e 7a 45 6e 64 48 64 Idx==u.am.zEndHd
1d378 72 20 26 26 20 75 2e 61 6d 2e 6f 66 66 73 65 74 r && u.am.offset
1d379 36 34 21 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61 64!=(u64)u.am.pa
1d37a 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a 20 20 yloadSize) ){.
1d37b 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1d37c 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1d37d 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 goto op_colu
1d37e 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 mn_out;. }.
1d37f 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 }.. /* Get the
1d380 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69 column informati
1d381 6f 6e 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 on. If u.am.aOff
1d382 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 set[u.am.p2] is
1d383 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 0a 20 non-zero, then.
1d384 20 2a 2a 20 64 65 73 65 72 69 61 6c 69 7a 65 20 ** deserialize
1d385 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 the value from t
1d386 68 65 20 72 65 63 6f 72 64 2e 20 49 66 20 75 2e he record. If u.
1d387 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e am.aOffset[u.am.
1d388 70 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a p2] is zero,. *
1d389 2a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65 * then there are
1d38a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c not enough fiel
1d38b 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 ds in the record
1d38c 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a to satisfy the.
1d38d 20 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 ** request. I
1d38e 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 n this case, set
1d38f 20 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 the value NULL
1d390 6f 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69 or to P4 if P4 i
1d391 73 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 s. ** a pointer
1d392 20 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 to a Mem object
1d393 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 .. */. if( u.a
1d394 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 m.aOffset[u.am.p
1d395 32 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 2] ){. assert
1d396 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1d397 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e );. if( u.am.
1d398 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71 zRec ){. sq
1d399 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1d39a 61 73 65 45 78 74 65 72 6e 61 6c 28 75 2e 61 6d aseExternal(u.am
1d39b 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20 73 .pDest);. s
1d39c 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1d39d 47 65 74 28 28 75 38 20 2a 29 26 75 2e 61 6d 2e Get((u8 *)&u.am.
1d39e 7a 52 65 63 5b 75 2e 61 6d 2e 61 4f 66 66 73 65 zRec[u.am.aOffse
1d39f 74 5b 75 2e 61 6d 2e 70 32 5d 5d 2c 20 75 2e 61 t[u.am.p2]], u.a
1d3a0 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d m.aType[u.am.p2]
1d3a1 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 , u.am.pDest);.
1d3a2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1d3a3 75 2e 61 6d 2e 6c 65 6e 20 3d 20 73 71 6c 69 74 u.am.len = sqlit
1d3a4 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1d3a5 4c 65 6e 28 75 2e 61 6d 2e 61 54 79 70 65 5b 75 Len(u.am.aType[u
1d3a6 2e 61 6d 2e 70 32 5d 29 3b 0a 20 20 20 20 20 20 .am.p2]);.
1d3a7 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f sqlite3VdbeMemMo
1d3a8 76 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 75 ve(&u.am.sMem, u
1d3a9 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 .am.pDest);.
1d3aa 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1d3ab 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 75 beMemFromBtree(u
1d3ac 2e 61 6d 2e 70 43 72 73 72 2c 20 75 2e 61 6d 2e .am.pCrsr, u.am.
1d3ad 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d aOffset[u.am.p2]
1d3ae 2c 20 75 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 61 6d , u.am.len, u.am
1d3af 2e 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20 26 75 .pC->isIndex, &u
1d3b0 2e 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 20 .am.sMem);.
1d3b1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1d3b2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f OK ){. go
1d3b3 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 to op_column_out
1d3b4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1d3b5 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 u.am.zData = u.a
1d3b6 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 20 20 m.sMem.z;.
1d3b7 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1d3b8 6c 47 65 74 28 28 75 38 2a 29 75 2e 61 6d 2e 7a lGet((u8*)u.am.z
1d3b9 44 61 74 61 2c 20 75 2e 61 6d 2e 61 54 79 70 65 Data, u.am.aType
1d3ba 5b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e [u.am.p2], u.am.
1d3bb 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 pDest);. }.
1d3bc 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 65 6e u.am.pDest->en
1d3bd 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 c = encoding;.
1d3be 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 }else{. if( p
1d3bf 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d Op->p4type==P4_M
1d3c0 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 EM ){. sqli
1d3c1 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f te3VdbeMemShallo
1d3c2 77 43 6f 70 79 28 75 2e 61 6d 2e 70 44 65 73 74 wCopy(u.am.pDest
1d3c3 2c 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 , pOp->p4.pMem,
1d3c4 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 MEM_Static);.
1d3c5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
1d3c6 73 65 72 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 sert( u.am.pDest
1d3c7 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c ->flags&MEM_Null
1d3c8 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 );. }. }..
1d3c9 20 2f 2a 20 49 66 20 77 65 20 64 79 6e 61 6d 69 /* If we dynami
1d3ca 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 cally allocated
1d3cb 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 space to hold th
1d3cc 65 20 64 61 74 61 20 28 69 6e 20 74 68 65 0a 20 e data (in the.
1d3cd 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d ** sqlite3VdbeM
1d3ce 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 63 61 emFromBtree() ca
1d3cf 6c 6c 20 61 62 6f 76 65 29 20 74 68 65 6e 20 74 ll above) then t
1d3d0 72 61 6e 73 66 65 72 20 63 6f 6e 74 72 6f 6c 20 ransfer control
1d3d1 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20 64 79 6e of that. ** dyn
1d3d2 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 amically allocat
1d3d3 65 64 20 73 70 61 63 65 20 6f 76 65 72 20 74 6f ed space over to
1d3d4 20 74 68 65 20 75 2e 61 6d 2e 70 44 65 73 74 20 the u.am.pDest
1d3d5 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 structure.. **
1d3d6 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 This prevents a
1d3d7 6d 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a memory copy.. *
1d3d8 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 73 4d 65 /. if( u.am.sMe
1d3d9 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 m.zMalloc ){.
1d3da 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 73 4d assert( u.am.sM
1d3db 65 6d 2e 7a 3d 3d 75 2e 61 6d 2e 73 4d 65 6d 2e em.z==u.am.sMem.
1d3dc 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 61 zMalloc );. a
1d3dd 73 73 65 72 74 28 20 21 28 75 2e 61 6d 2e 70 44 ssert( !(u.am.pD
1d3de 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d est->flags & MEM
1d3df 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73 _Dyn) );. ass
1d3e0 65 72 74 28 20 21 28 75 2e 61 6d 2e 70 44 65 73 ert( !(u.am.pDes
1d3e1 74 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f t->flags & (MEM_
1d3e2 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 20 7c Blob|MEM_Str)) |
1d3e3 7c 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 3d | u.am.pDest->z=
1d3e4 3d 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 20 29 3b 0a =u.am.sMem.z );.
1d3e5 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e u.am.pDest->
1d3e6 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 flags &= ~(MEM_E
1d3e7 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 phem|MEM_Static)
1d3e8 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 ;. u.am.pDest
1d3e9 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 ->flags |= MEM_T
1d3ea 65 72 6d 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44 erm;. u.am.pD
1d3eb 65 73 74 2d 3e 7a 20 3d 20 75 2e 61 6d 2e 73 4d est->z = u.am.sM
1d3ec 65 6d 2e 7a 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 em.z;. u.am.p
1d3ed 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 Dest->zMalloc =
1d3ee 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f u.am.sMem.zMallo
1d3ef 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 c;. }.. rc = s
1d3f0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b qlite3VdbeMemMak
1d3f1 65 57 72 69 74 65 61 62 6c 65 28 75 2e 61 6d 2e eWriteable(u.am.
1d3f2 70 44 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 pDest);..op_colu
1d3f3 6d 6e 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45 mn_out:. UPDATE
1d3f4 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 75 2e _MAX_BLOBSIZE(u.
1d3f5 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 52 45 47 am.pDest);. REG
1d3f6 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1d3f7 3e 70 33 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 >p3, u.am.pDest)
1d3f8 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1d3f9 20 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74 Opcode: Affinit
1d3fa 79 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a y P1 P2 * P4 *.*
1d3fb 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e *.** Apply affin
1d3fc 69 74 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65 ities to a range
1d3fd 20 6f 66 20 50 32 20 72 65 67 69 73 74 65 72 73 of P2 registers
1d3fe 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 50 starting with P
1d3ff 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 1..**.** P4 is a
1d400 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 string that is
1d401 50 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f P2 characters lo
1d402 6e 67 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72 ng. The nth char
1d403 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 acter of the.**
1d404 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 string indicates
1d405 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 the column affi
1d406 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 nity that should
1d407 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 be used for the
1d408 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 nth.** memory c
1d409 65 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65 ell in the range
1d40a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66 ..*/.case OP_Aff
1d40b 69 6e 69 74 79 3a 20 7b 0a 23 69 66 20 30 20 20 inity: {.#if 0
1d40c 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d40d 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d40e 61 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 41 an */. char *zA
1d40f 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 ffinity; /* Th
1d410 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 e affinity to be
1d411 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 4d 65 applied */. Me
1d412 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 m *pData0;
1d413 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74 /* First regist
1d414 65 72 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 er to which to a
1d415 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f pply affinity */
1d416 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 . Mem *pLast;
1d417 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 65 /* Last re
1d418 67 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 20 gister to which
1d419 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 to apply affinit
1d41a 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 63 y */. Mem *pRec
1d41b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 ; /* Cur
1d41c 72 65 6e 74 20 72 65 67 69 73 74 65 72 20 2a 2f rent register */
1d41d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1d41e 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d41f 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 0a 20 into u.an */..
1d420 20 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 20 u.an.zAffinity
1d421 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 75 = pOp->p4.z;. u
1d422 2e 61 6e 2e 70 44 61 74 61 30 20 3d 20 26 70 2d .an.pData0 = &p-
1d423 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1d424 20 20 75 2e 61 6e 2e 70 4c 61 73 74 20 3d 20 26 u.an.pLast = &
1d425 75 2e 61 6e 2e 70 44 61 74 61 30 5b 70 4f 70 2d u.an.pData0[pOp-
1d426 3e 70 32 2d 31 5d 3b 0a 20 20 66 6f 72 28 75 2e >p2-1];. for(u.
1d427 61 6e 2e 70 52 65 63 3d 75 2e 61 6e 2e 70 44 61 an.pRec=u.an.pDa
1d428 74 61 30 3b 20 75 2e 61 6e 2e 70 52 65 63 3c 3d ta0; u.an.pRec<=
1d429 75 2e 61 6e 2e 70 4c 61 73 74 3b 20 75 2e 61 6e u.an.pLast; u.an
1d42a 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 .pRec++){. Ex
1d42b 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6e 2e 70 52 pandBlob(u.an.pR
1d42c 65 63 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 ec);. applyAf
1d42d 66 69 6e 69 74 79 28 75 2e 61 6e 2e 70 52 65 63 finity(u.an.pRec
1d42e 2c 20 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 , u.an.zAffinity
1d42f 5b 75 2e 61 6e 2e 70 52 65 63 2d 75 2e 61 6e 2e [u.an.pRec-u.an.
1d430 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e pData0], encodin
1d431 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b g);. }. break;
1d432 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d .}../* Opcode: M
1d433 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32 20 akeRecord P1 P2
1d434 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f P3 P4 *.**.** Co
1d435 6e 76 65 72 74 20 50 32 20 72 65 67 69 73 74 65 nvert P2 registe
1d436 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 rs beginning wit
1d437 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69 6e 67 h P1 into a sing
1d438 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75 69 74 le entry.** suit
1d439 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 able for use as
1d43a 61 20 64 61 74 61 20 72 65 63 6f 72 64 20 69 6e a data record in
1d43b 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c a database tabl
1d43c 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a 2a 2a e or as a key.**
1d43d 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 54 in an index. T
1d43e 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 he details of th
1d43f 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69 72 72 e format are irr
1d440 65 6c 65 76 61 6e 74 20 61 73 20 6c 6f 6e 67 20 elevant as long
1d441 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43 6f 6c as.** the OP_Col
1d442 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20 64 umn opcode can d
1d443 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72 64 ecode the record
1d444 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66 65 72 later..** Refer
1d445 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64 65 20 to source code
1d446 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74 68 65 comments for the
1d447 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 details of the
1d448 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 record.** format
1d449 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 ..**.** P4 may b
1d44a 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 e a string that
1d44b 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 73 is P2 characters
1d44c 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20 long. The nth
1d44d 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 character of the
1d44e 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 .** string indic
1d44f 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 ates the column
1d450 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 affinity that sh
1d451 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
1d452 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 6c the nth.** fiel
1d453 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b d of the index k
1d454 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 ey..**.** The ma
1d455 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72 61 pping from chara
1d456 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74 79 cter to affinity
1d457 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 is given by the
1d458 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a 20 SQLITE_AFF_.**
1d459 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 69 macros defined i
1d45a 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a n sqliteInt.h..*
1d45b 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 4e 55 *.** If P4 is NU
1d45c 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64 65 LL then all inde
1d45d 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74 68 x fields have th
1d45e 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45 2e e affinity NONE.
1d45f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61 6b 65 .*/.case OP_Make
1d460 52 65 63 6f 72 64 3a 20 7b 0a 23 69 66 20 30 20 Record: {.#if 0
1d461 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d462 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d463 2e 61 6f 20 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 .ao */. u8 *zNe
1d464 77 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 wRecord;
1d465 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 /* A buffer to h
1d466 6f 6c 64 20 74 68 65 20 64 61 74 61 20 66 6f 72 old the data for
1d467 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 the new record
1d468 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 */. Mem *pRec;
1d469 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1d46a 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f he new record */
1d46b 0a 20 20 75 36 34 20 6e 44 61 74 61 3b 20 20 20 . u64 nData;
1d46c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1d46d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
1d46e 64 61 74 61 20 73 70 61 63 65 20 2a 2f 0a 20 20 data space */.
1d46f 69 6e 74 20 6e 48 64 72 3b 20 20 20 20 20 20 20 int nHdr;
1d470 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1d471 20 6f 66 20 62 79 74 65 73 20 6f 66 20 68 65 61 of bytes of hea
1d472 64 65 72 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 der space */. i
1d473 36 34 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 64 nByte;
1d474 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 /* Data sp
1d475 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 ace required for
1d476 20 74 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a this record */.
1d477 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 int nZero;
1d478 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1d479 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 er of zero bytes
1d47a 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
1d47b 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 he record */. i
1d47c 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20 20 20 20 nt nVarint;
1d47d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1d47e 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 76 61 of bytes in a va
1d47f 72 69 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 65 rint */. u32 se
1d480 72 69 61 6c 5f 74 79 70 65 3b 20 20 20 20 20 20 rial_type;
1d481 20 2f 2a 20 54 79 70 65 20 66 69 65 6c 64 20 2a /* Type field *
1d482 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b /. Mem *pData0;
1d483 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
1d484 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20 rst field to be
1d485 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 combined into th
1d486 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 e record */. Me
1d487 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20 m *pLast;
1d488 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 69 65 /* Last fie
1d489 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 ld of the record
1d48a 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 */. int nField
1d48b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1d48c 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 Number of fields
1d48d 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a in the record *
1d48e 2f 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e /. char *zAffin
1d48f 69 74 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 ity; /* Th
1d490 65 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e e affinity strin
1d491 67 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 g for the record
1d492 20 2a 2f 0a 20 20 69 6e 74 20 66 69 6c 65 5f 66 */. int file_f
1d493 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20 ormat; /*
1d494 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75 File format to u
1d495 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20 se for encoding
1d496 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 */. int i;
1d497 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1d498 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e 65 pace used in zNe
1d499 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 69 wRecord[] */. i
1d49a 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 nt len;
1d49b 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 /* Length
1d49c 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f 0a 23 65 of a field */.#e
1d49d 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1d49e 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d49f 74 6f 20 75 2e 61 6f 20 2a 2f 0a 0a 20 20 2f 2a to u.ao */.. /*
1d4a0 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65 Assuming the re
1d4a1 63 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20 cord contains N
1d4a2 66 69 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f fields, the reco
1d4a3 72 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a rd format looks.
1d4a4 20 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a ** like this:.
1d4a5 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d **. ** ------
1d4a6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d4a7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d4a8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d4a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d4aa 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69 --. ** | hdr-si
1d4ab 7a 65 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79 ze | type 0 | ty
1d4ac 70 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 pe 1 | ... | typ
1d4ad 65 20 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20 e N-1 | data0 |
1d4ae 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c ... | data N-1 |
1d4af 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d . ** ----------
1d4b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d4b1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d4b2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d4b3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 --------------.
1d4b4 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30 29 **. ** Data(0)
1d4b5 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 is taken from r
1d4b6 65 67 69 73 74 65 72 20 50 31 2e 20 20 44 61 74 egister P1. Dat
1d4b7 61 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d 20 a(1) comes from
1d4b8 72 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20 20 register P1+1.
1d4b9 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68 2e ** and so froth.
1d4ba 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20 . **. ** Each
1d4bb 74 79 70 65 20 66 69 65 6c 64 20 69 73 20 61 20 type field is a
1d4bc 76 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e 74 varint represent
1d4bd 69 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20 74 ing the serial t
1d4be 79 70 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 ype of the. **
1d4bf 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61 corresponding da
1d4c0 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65 65 20 ta element (see
1d4c1 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1d4c2 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a 20 20 lType()). The.
1d4c3 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69 65 6c ** hdr-size fiel
1d4c4 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61 72 69 d is also a vari
1d4c5 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65 20 nt which is the
1d4c6 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 offset from the
1d4c7 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f beginning. ** o
1d4c8 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 f the record to
1d4c9 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20 75 2e data0.. */. u.
1d4ca 61 6f 2e 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 ao.nData = 0;
1d4cb 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1d4cc 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 of bytes of data
1d4cd 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 2e 61 6f space */. u.ao
1d4ce 2e 6e 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20 .nHdr = 0;
1d4cf 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1d4d0 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 bytes of header
1d4d1 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 2e 61 6f space */. u.ao
1d4d2 2e 6e 42 79 74 65 20 3d 20 30 3b 20 20 20 20 20 .nByte = 0;
1d4d3 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 /* Data spac
1d4d4 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 e required for t
1d4d5 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 his record */.
1d4d6 75 2e 61 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 20 u.ao.nZero = 0;
1d4d7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1d4d8 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20 r of zero bytes
1d4d9 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
1d4da 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e e record */. u.
1d4db 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d ao.nField = pOp-
1d4dc 3e 70 31 3b 0a 20 20 75 2e 61 6f 2e 7a 41 66 66 >p1;. u.ao.zAff
1d4dd 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e inity = pOp->p4.
1d4de 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 z;. assert( u.a
1d4df 6f 2e 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f o.nField>0 && pO
1d4e0 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e p->p2>0 && pOp->
1d4e1 70 32 2b 75 2e 61 6f 2e 6e 46 69 65 6c 64 3c 3d p2+u.ao.nField<=
1d4e2 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 75 p->nMem+1 );. u
1d4e3 2e 61 6f 2e 70 44 61 74 61 30 20 3d 20 26 70 2d .ao.pData0 = &p-
1d4e4 3e 61 4d 65 6d 5b 75 2e 61 6f 2e 6e 46 69 65 6c >aMem[u.ao.nFiel
1d4e5 64 5d 3b 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 6c d];. u.ao.nFiel
1d4e6 64 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 d = pOp->p2;. u
1d4e7 2e 61 6f 2e 70 4c 61 73 74 20 3d 20 26 75 2e 61 .ao.pLast = &u.a
1d4e8 6f 2e 70 44 61 74 61 30 5b 75 2e 61 6f 2e 6e 46 o.pData0[u.ao.nF
1d4e9 69 65 6c 64 2d 31 5d 3b 0a 20 20 75 2e 61 6f 2e ield-1];. u.ao.
1d4ea 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 2d file_format = p-
1d4eb 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 >minWriteFileFor
1d4ec 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 mat;.. /* Loop
1d4ed 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c 65 6d through the elem
1d4ee 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c 20 6d ents that will m
1d4ef 61 6b 65 20 75 70 20 74 68 65 20 72 65 63 6f 72 ake up the recor
1d4f0 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20 2a 2a d to figure. **
1d4f1 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 out how much sp
1d4f2 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64 20 ace is required
1d4f3 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f for the new reco
1d4f4 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 75 rd.. */. for(u
1d4f5 2e 61 6f 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 .ao.pRec=u.ao.pD
1d4f6 61 74 61 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c ata0; u.ao.pRec<
1d4f7 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 =u.ao.pLast; u.a
1d4f8 6f 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 69 o.pRec++){. i
1d4f9 66 28 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 f( u.ao.zAffinit
1d4fa 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 y ){. apply
1d4fb 41 66 66 69 6e 69 74 79 28 75 2e 61 6f 2e 70 52 Affinity(u.ao.pR
1d4fc 65 63 2c 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 ec, u.ao.zAffini
1d4fd 74 79 5b 75 2e 61 6f 2e 70 52 65 63 2d 75 2e 61 ty[u.ao.pRec-u.a
1d4fe 6f 2e 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 o.pData0], encod
1d4ff 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ing);. }.
1d500 69 66 28 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 66 if( u.ao.pRec->f
1d501 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 20 26 26 lags&MEM_Zero &&
1d502 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 6e 3e 30 20 u.ao.pRec->n>0
1d503 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1d504 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f VdbeMemExpandBlo
1d505 62 28 75 2e 61 6f 2e 70 52 65 63 29 3b 0a 20 20 b(u.ao.pRec);.
1d506 20 20 7d 0a 20 20 20 20 75 2e 61 6f 2e 73 65 72 }. u.ao.ser
1d507 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 ial_type = sqlit
1d508 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1d509 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f (u.ao.pRec, u.ao
1d50a 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 .file_format);.
1d50b 20 20 20 75 2e 61 6f 2e 6c 65 6e 20 3d 20 73 71 u.ao.len = sq
1d50c 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
1d50d 79 70 65 4c 65 6e 28 75 2e 61 6f 2e 73 65 72 69 ypeLen(u.ao.seri
1d50e 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 75 2e al_type);. u.
1d50f 61 6f 2e 6e 44 61 74 61 20 2b 3d 20 75 2e 61 6f ao.nData += u.ao
1d510 2e 6c 65 6e 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e .len;. u.ao.n
1d511 48 64 72 20 2b 3d 20 73 71 6c 69 74 65 33 56 61 Hdr += sqlite3Va
1d512 72 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 73 65 72 rintLen(u.ao.ser
1d513 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69 ial_type);. i
1d514 66 28 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 66 6c f( u.ao.pRec->fl
1d515 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 ags & MEM_Zero )
1d516 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 {. /* Only
1d517 70 75 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 pure zero-filled
1d518 20 42 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 6e BLOBs can be in
1d519 70 75 74 20 74 6f 20 74 68 69 73 20 4f 70 63 6f put to this Opco
1d51a 64 65 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 20 de.. ** We
1d51b 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f do not allow blo
1d51c 62 73 20 77 69 74 68 20 61 20 70 72 65 66 69 78 bs with a prefix
1d51d 20 61 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c and a zero-fill
1d51e 65 64 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 ed tail. */.
1d51f 20 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 2b 3d 20 u.ao.nZero +=
1d520 75 2e 61 6f 2e 70 52 65 63 2d 3e 75 2e 6e 5a 65 u.ao.pRec->u.nZe
1d521 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 ro;. }else if
1d522 28 20 75 2e 61 6f 2e 6c 65 6e 20 29 7b 0a 20 20 ( u.ao.len ){.
1d523 20 20 20 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 3d u.ao.nZero =
1d524 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 0;. }. }..
1d525 20 2f 2a 20 41 64 64 20 74 68 65 20 69 6e 69 74 /* Add the init
1d526 69 61 6c 20 68 65 61 64 65 72 20 76 61 72 69 6e ial header varin
1d527 74 20 61 6e 64 20 74 6f 74 61 6c 20 74 68 65 20 t and total the
1d528 73 69 7a 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e size */. u.ao.n
1d529 48 64 72 20 2b 3d 20 75 2e 61 6f 2e 6e 56 61 72 Hdr += u.ao.nVar
1d52a 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 61 72 int = sqlite3Var
1d52b 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 6e 48 64 72 intLen(u.ao.nHdr
1d52c 29 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 56 );. if( u.ao.nV
1d52d 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72 arint<sqlite3Var
1d52e 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 6e 48 64 72 intLen(u.ao.nHdr
1d52f 29 20 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 6e 48 ) ){. u.ao.nH
1d530 64 72 2b 2b 3b 0a 20 20 7d 0a 20 20 75 2e 61 6f dr++;. }. u.ao
1d531 2e 6e 42 79 74 65 20 3d 20 75 2e 61 6f 2e 6e 48 .nByte = u.ao.nH
1d532 64 72 2b 75 2e 61 6f 2e 6e 44 61 74 61 2d 75 2e dr+u.ao.nData-u.
1d533 61 6f 2e 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 ao.nZero;. if(
1d534 75 2e 61 6f 2e 6e 42 79 74 65 3e 64 62 2d 3e 61 u.ao.nByte>db->a
1d535 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1d536 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1d537 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1d538 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 }.. /* Make s
1d539 75 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 72 ure the output r
1d53a 65 67 69 73 74 65 72 20 68 61 73 20 61 20 62 75 egister has a bu
1d53b 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 ffer large enoug
1d53c 68 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 h to store. **
1d53d 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 the new record.
1d53e 54 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 The output regis
1d53f 74 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73 ter (pOp->p3) is
1d540 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a not allowed to.
1d541 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 ** be one of t
1d542 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 he input registe
1d543 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65 20 rs (because the
1d544 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 following call t
1d545 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 o. ** sqlite3Vd
1d546 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c beMemGrow() coul
1d547 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61 d clobber the va
1d548 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 lue before it is
1d549 20 75 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61 used).. */. a
1d54a 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 ssert( pOp->p3<p
1d54b 4f 70 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 Op->p1 || pOp->p
1d54c 33 3e 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 3>=pOp->p1+pOp->
1d54d 70 32 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 p2 );. pOut = &
1d54e 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1d54f 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 ;. if( sqlite3V
1d550 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c dbeMemGrow(pOut,
1d551 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 79 74 65 (int)u.ao.nByte
1d552 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f , 0) ){. goto
1d553 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 75 no_mem;. }. u
1d554 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 20 3d .ao.zNewRecord =
1d555 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a (u8 *)pOut->z;.
1d556 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 . /* Write the
1d557 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f record */. u.ao
1d558 2e 69 20 3d 20 70 75 74 56 61 72 69 6e 74 33 32 .i = putVarint32
1d559 28 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 (u.ao.zNewRecord
1d55a 2c 20 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 , u.ao.nHdr);.
1d55b 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 2e for(u.ao.pRec=u.
1d55c 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f 2e ao.pData0; u.ao.
1d55d 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 74 pRec<=u.ao.pLast
1d55e 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b 0a ; u.ao.pRec++){.
1d55f 20 20 20 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f u.ao.serial_
1d560 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 type = sqlite3Vd
1d561 62 65 53 65 72 69 61 6c 54 79 70 65 28 75 2e 61 beSerialType(u.a
1d562 6f 2e 70 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c o.pRec, u.ao.fil
1d563 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75 e_format);. u
1d564 2e 61 6f 2e 69 20 2b 3d 20 70 75 74 56 61 72 69 .ao.i += putVari
1d565 6e 74 33 32 28 26 75 2e 61 6f 2e 7a 4e 65 77 52 nt32(&u.ao.zNewR
1d566 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d 2c 20 75 ecord[u.ao.i], u
1d567 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29 .ao.serial_type)
1d568 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69 61 6c ; /* serial
1d569 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20 20 66 type */. }. f
1d56a 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 2e 61 or(u.ao.pRec=u.a
1d56b 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f 2e 70 o.pData0; u.ao.p
1d56c 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b Rec<=u.ao.pLast;
1d56d 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b 20 20 u.ao.pRec++){
1d56e 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 61 20 2a /* serial data *
1d56f 2f 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d 20 /. u.ao.i +=
1d570 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
1d571 6c 50 75 74 28 26 75 2e 61 6f 2e 7a 4e 65 77 52 lPut(&u.ao.zNewR
1d572 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d 2c 20 28 ecord[u.ao.i], (
1d573 69 6e 74 29 28 75 2e 61 6f 2e 6e 42 79 74 65 2d int)(u.ao.nByte-
1d574 75 2e 61 6f 2e 69 29 2c 20 75 2e 61 6f 2e 70 52 u.ao.i), u.ao.pR
1d575 65 63 2c 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 ec,u.ao.file_for
1d576 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 mat);. }. asse
1d577 72 74 28 20 75 2e 61 6f 2e 69 3d 3d 75 2e 61 6f rt( u.ao.i==u.ao
1d578 2e 6e 42 79 74 65 20 29 3b 0a 0a 20 20 61 73 73 .nByte );.. ass
1d579 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 ert( pOp->p3>0 &
1d57a 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d & pOp->p3<=p->nM
1d57b 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 em );. pOut->n
1d57c 3d 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 79 74 = (int)u.ao.nByt
1d57d 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 e;. pOut->flags
1d57e 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 = MEM_Blob | ME
1d57f 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78 M_Dyn;. pOut->x
1d580 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 Del = 0;. if( u
1d581 2e 61 6f 2e 6e 5a 65 72 6f 20 29 7b 0a 20 20 20 .ao.nZero ){.
1d582 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 6f 20 3d pOut->u.nZero =
1d583 20 75 2e 61 6f 2e 6e 5a 65 72 6f 3b 0a 20 20 20 u.ao.nZero;.
1d584 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 pOut->flags |=
1d585 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20 MEM_Zero;. }.
1d586 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pOut->enc = SQLI
1d587 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 TE_UTF8; /* In
1d588 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73 case the blob is
1d589 20 65 76 65 72 20 63 6f 6e 76 65 72 74 65 64 20 ever converted
1d58a 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 52 45 47 to text */. REG
1d58b 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1d58c 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 >p3, pOut);. UP
1d58d 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1d58e 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b E(pOut);. break
1d58f 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d590 43 6f 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 Count P1 P2 * *
1d591 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 *.**.** Store th
1d592 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
1d593 69 65 73 20 28 61 6e 20 69 6e 74 65 67 65 72 20 ies (an integer
1d594 76 61 6c 75 65 29 20 69 6e 20 74 68 65 20 74 61 value) in the ta
1d595 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 0a 2a 2a ble or index .**
1d596 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f opened by curso
1d597 72 20 50 31 20 69 6e 20 72 65 67 69 73 74 65 72 r P1 in register
1d598 20 50 32 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 P2.*/.#ifndef S
1d599 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 QLITE_OMIT_BTREE
1d59a 43 4f 55 4e 54 0a 63 61 73 65 20 4f 50 5f 43 6f COUNT.case OP_Co
1d59b 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 2f unt: { /
1d59c 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1d59d 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c e */.#if 0 /* l
1d59e 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d59f 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a oved into u.ap *
1d5a0 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a /. i64 nEntry;.
1d5a1 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1d5a2 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 r;.#endif /* loc
1d5a3 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d5a4 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a 2f 0a ed into u.ap */.
1d5a5 0a 20 20 75 2e 61 70 2e 70 43 72 73 72 20 3d 20 . u.ap.pCrsr =
1d5a6 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1d5a7 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 ]->pCursor;. if
1d5a8 28 20 75 2e 61 70 2e 70 43 72 73 72 20 29 7b 0a ( u.ap.pCrsr ){.
1d5a9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d5aa 42 74 72 65 65 43 6f 75 6e 74 28 75 2e 61 70 2e BtreeCount(u.ap.
1d5ab 70 43 72 73 72 2c 20 26 75 2e 61 70 2e 6e 45 6e pCrsr, &u.ap.nEn
1d5ac 74 72 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 try);. }else{.
1d5ad 20 20 20 75 2e 61 70 2e 6e 45 6e 74 72 79 20 3d u.ap.nEntry =
1d5ae 20 30 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 0;. }. pOut->
1d5af 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1d5b0 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 . pOut->u.i = u
1d5b1 2e 61 70 2e 6e 45 6e 74 72 79 3b 0a 20 20 62 72 .ap.nEntry;. br
1d5b2 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f eak;.}.#endif../
1d5b3 2a 20 4f 70 63 6f 64 65 3a 20 53 61 76 65 70 6f * Opcode: Savepo
1d5b4 69 6e 74 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a int P1 * * P4 *.
1d5b5 2a 2a 0a 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 **.** Open, rele
1d5b6 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 ase or rollback
1d5b7 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 6e 61 the savepoint na
1d5b8 6d 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 med by parameter
1d5b9 20 50 34 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a P4, depending.*
1d5ba 2a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f * on the value o
1d5bb 66 20 50 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20 f P1. To open a
1d5bc 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 new savepoint, P
1d5bd 31 3d 3d 30 2e 20 54 6f 20 72 65 6c 65 61 73 65 1==0. To release
1d5be 20 28 63 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 (commit) an.**
1d5bf 65 78 69 73 74 69 6e 67 20 73 61 76 65 70 6f 69 existing savepoi
1d5c0 6e 74 2c 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f nt, P1==1, or to
1d5c1 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69 rollback an exi
1d5c2 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 sting savepoint
1d5c3 50 31 3d 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f P1==2..*/.case O
1d5c4 50 5f 53 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 23 P_Savepoint: {.#
1d5c5 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1d5c6 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1d5c7 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 20 20 69 6e nto u.aq */. in
1d5c8 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 t p1;
1d5c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d5ca 20 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 Value of P1 ope
1d5cb 72 61 6e 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a rand */. char *
1d5cc 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 zName;
1d5cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
1d5ce 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 2a e of savepoint *
1d5cf 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 /. int nName;.
1d5d0 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 Savepoint *pNew
1d5d1 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 ;. Savepoint *p
1d5d2 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 53 61 76 Savepoint;. Sav
1d5d3 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 epoint *pTmp;.
1d5d4 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 3b 0a int iSavepoint;.
1d5d5 20 20 69 6e 74 20 69 69 3b 0a 23 65 6e 64 69 66 int ii;.#endif
1d5d6 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d5d7 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d5d8 2e 61 71 20 2a 2f 0a 0a 20 20 75 2e 61 71 2e 70 .aq */.. u.aq.p
1d5d9 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 1 = pOp->p1;. u
1d5da 2e 61 71 2e 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d .aq.zName = pOp-
1d5db 3e 70 34 2e 7a 3b 0a 0a 20 20 2f 2a 20 41 73 73 >p4.z;.. /* Ass
1d5dc 65 72 74 20 74 68 61 74 20 74 68 65 20 75 2e 61 ert that the u.a
1d5dd 71 2e 70 31 20 70 61 72 61 6d 65 74 65 72 20 69 q.p1 parameter i
1d5de 73 20 76 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 s valid. Also th
1d5df 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 6e at if there is n
1d5e0 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e o open. ** tran
1d5e1 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 saction, then th
1d5e2 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e ere cannot be an
1d5e3 79 20 73 61 76 65 70 6f 69 6e 74 73 2e 0a 20 20 y savepoints..
1d5e4 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d */. assert( db-
1d5e5 3e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c >pSavepoint==0 |
1d5e6 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 | db->autoCommit
1d5e7 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1d5e8 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f u.aq.p1==SAVEPO
1d5e9 49 4e 54 5f 42 45 47 49 4e 7c 7c 75 2e 61 71 2e INT_BEGIN||u.aq.
1d5ea 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 p1==SAVEPOINT_RE
1d5eb 4c 45 41 53 45 7c 7c 75 2e 61 71 2e 70 31 3d 3d LEASE||u.aq.p1==
1d5ec 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
1d5ed 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
1d5ee 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c db->pSavepoint |
1d5ef 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 | db->isTransact
1d5f0 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 ionSavepoint==0
1d5f1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 65 );. assert( che
1d5f2 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 ckSavepointCount
1d5f3 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 75 (db) );.. if( u
1d5f4 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e .aq.p1==SAVEPOIN
1d5f5 54 5f 42 45 47 49 4e 20 29 7b 0a 20 20 20 20 69 T_BEGIN ){. i
1d5f6 66 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 f( db->writeVdbe
1d5f7 43 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f Cnt>0 ){. /
1d5f8 2a 20 41 20 6e 65 77 20 73 61 76 65 70 6f 69 6e * A new savepoin
1d5f9 74 20 63 61 6e 6e 6f 74 20 62 65 20 63 72 65 61 t cannot be crea
1d5fa 74 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65 ted if there are
1d5fb 20 61 63 74 69 76 65 20 77 72 69 74 65 0a 20 20 active write.
1d5fc 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
1d5fd 73 20 28 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61 s (i.e. open rea
1d5fe 64 2f 77 72 69 74 65 20 69 6e 63 72 65 6d 65 6e d/write incremen
1d5ff 74 61 6c 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73 tal blob handles
1d600 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ).. */.
1d601 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1d602 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1d603 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e db, "cannot open
1d604 20 73 61 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20 savepoint - ".
1d605 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 "SQL stat
1d606 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 ements in progre
1d607 73 73 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d ss");. rc =
1d608 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
1d609 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 }else{. u
1d60a 2e 61 71 2e 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 .aq.nName = sqli
1d60b 74 65 33 53 74 72 6c 65 6e 33 30 28 75 2e 61 71 te3Strlen30(u.aq
1d60c 2e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 .zName);..
1d60d 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 /* Create a new
1d60e 73 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 savepoint struct
1d60f 75 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e ure. */. u.
1d610 61 71 2e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 aq.pNew = sqlite
1d611 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
1d612 20 73 69 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e sizeof(Savepoin
1d613 74 29 2b 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 t)+u.aq.nName+1)
1d614 3b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 ;. if( u.aq
1d615 2e 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 .pNew ){.
1d616 20 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 6d u.aq.pNew->zNam
1d617 65 20 3d 20 28 63 68 61 72 20 2a 29 26 75 2e 61 e = (char *)&u.a
1d618 71 2e 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 q.pNew[1];.
1d619 20 20 20 6d 65 6d 63 70 79 28 75 2e 61 71 2e 70 memcpy(u.aq.p
1d61a 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 71 New->zName, u.aq
1d61b 2e 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 6e 4e 61 .zName, u.aq.nNa
1d61c 6d 65 2b 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 me+1);..
1d61d 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e /* If there is n
1d61e 6f 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 o open transacti
1d61f 6f 6e 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 on, then mark th
1d620 69 73 20 61 73 20 61 20 73 70 65 63 69 61 6c 0a is as a special.
1d621 20 20 20 20 20 20 20 20 2a 2a 20 22 74 72 61 6e ** "tran
1d622 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e saction savepoin
1d623 74 22 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 t". */. i
1d624 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 f( db->autoCommi
1d625 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 t ){. d
1d626 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 b->autoCommit =
1d627 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 0;. db-
1d628 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 >isTransactionSa
1d629 76 65 70 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20 vepoint = 1;.
1d62a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d62b 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 db->nSavep
1d62c 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 oint++;.
1d62d 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 }.. /* Li
1d62e 6e 6b 20 74 68 65 20 6e 65 77 20 73 61 76 65 70 nk the new savep
1d62f 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 64 61 oint into the da
1d630 74 61 62 61 73 65 20 68 61 6e 64 6c 65 27 73 20 tabase handle's
1d631 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 list. */.
1d632 20 75 2e 61 71 2e 70 4e 65 77 2d 3e 70 4e 65 78 u.aq.pNew->pNex
1d633 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 t = db->pSavepoi
1d634 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e nt;. db->
1d635 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 pSavepoint = u.a
1d636 71 2e 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a q.pNew;. }.
1d637 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1d638 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 u.aq.iSavepoi
1d639 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 nt = 0;.. /*
1d63a 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 73 Find the named s
1d63b 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 74 68 65 avepoint. If the
1d63c 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 73 61 re is no such sa
1d63d 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 6e vepoint, then an
1d63e 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 . ** an error
1d63f 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 is returned to
1d640 74 68 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20 20 the user. */.
1d641 20 20 66 6f 72 28 0a 20 20 20 20 20 20 75 2e 61 for(. u.a
1d642 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 64 q.pSavepoint = d
1d643 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 b->pSavepoint;.
1d644 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 u.aq.pSavep
1d645 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 65 33 53 oint && sqlite3S
1d646 74 72 49 43 6d 70 28 75 2e 61 71 2e 70 53 61 76 trICmp(u.aq.pSav
1d647 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 75 epoint->zName, u
1d648 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 .aq.zName);.
1d649 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e u.aq.pSavepoin
1d64a 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 6f t = u.aq.pSavepo
1d64b 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 int->pNext. )
1d64c 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61 {. u.aq.iSa
1d64d 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d vepoint++;. }
1d64e 0a 20 20 20 20 69 66 28 20 21 75 2e 61 71 2e 70 . if( !u.aq.p
1d64f 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 Savepoint ){.
1d650 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1d651 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1d652 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 73 61 db, "no such sa
1d653 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c 20 75 2e vepoint: %s", u.
1d654 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 aq.zName);.
1d655 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
1d656 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 OR;. }else if
1d657 28 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 77 72 (. db->wr
1d658 69 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 iteVdbeCnt>0 ||
1d659 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f (u.aq.p1==SAVEPO
1d65a 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 INT_ROLLBACK &&
1d65b 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
1d65c 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 t>1). ){.
1d65d 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 /* It is not p
1d65e 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 61 ossible to relea
1d65f 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 se (commit) a sa
1d660 76 65 70 6f 69 6e 74 20 69 66 20 74 68 65 72 65 vepoint if there
1d661 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 63 are. ** ac
1d662 74 69 76 65 20 77 72 69 74 65 20 73 74 61 74 65 tive write state
1d663 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f 74 ments. It is not
1d664 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f 6c possible to rol
1d665 6c 62 61 63 6b 20 61 20 73 61 76 65 70 6f 69 6e lback a savepoin
1d666 74 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 74 68 t. ** if th
1d667 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 ere are any acti
1d668 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61 74 ve statements at
1d669 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 all.. */.
1d66a 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 sqlite3SetS
1d66b 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1d66c 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 22 g, db,. "
1d66d 63 61 6e 6e 6f 74 20 25 73 20 73 61 76 65 70 6f cannot %s savepo
1d66e 69 6e 74 20 2d 20 53 51 4c 20 73 74 61 74 65 6d int - SQL statem
1d66f 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 ents in progress
1d670 22 2c 0a 20 20 20 20 20 20 20 20 28 75 2e 61 71 ",. (u.aq
1d671 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 .p1==SAVEPOINT_R
1d672 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62 OLLBACK ? "rollb
1d673 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29 ack": "release")
1d674 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 . );.
1d675 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
1d676 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 ;. }else{..
1d677 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 /* Determine
1d678 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1d679 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61 this is a transa
1d67a 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e ction savepoint.
1d67b 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a If so,. **
1d67c 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 52 and this is a R
1d67d 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20 ELEASE command,
1d67e 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 then the current
1d67f 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 transaction.
1d680 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74 ** is committ
1d681 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ed.. */.
1d682 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73 61 63 int isTransac
1d683 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70 53 61 76 tion = u.aq.pSav
1d684 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 epoint->pNext==0
1d685 20 26 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 && db->isTransa
1d686 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a ctionSavepoint;.
1d687 20 20 20 20 20 20 69 66 28 20 69 73 54 72 61 6e if( isTran
1d688 73 61 63 74 69 6f 6e 20 26 26 20 75 2e 61 71 2e saction && u.aq.
1d689 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 p1==SAVEPOINT_RE
1d68a 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 LEASE ){.
1d68b 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 db->autoCommit
1d68c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 = 1;. if(
1d68d 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 sqlite3VdbeHalt
1d68e 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 (p)==SQLITE_BUSY
1d68f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d ){. p-
1d690 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 >pc = pc;.
1d691 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1d692 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 it = 0;.
1d693 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 p->rc = rc = S
1d694 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
1d695 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f goto vdbe_
1d696 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 return;.
1d697 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 }. db->is
1d698 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 TransactionSavep
1d699 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 oint = 0;.
1d69a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 rc = p->rc;.
1d69b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1d69c 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 u.aq.iSavepoi
1d69d 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f nt = db->nSavepo
1d69e 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53 61 76 65 int - u.aq.iSave
1d69f 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 point - 1;.
1d6a0 20 20 20 66 6f 72 28 75 2e 61 71 2e 69 69 3d 30 for(u.aq.ii=0
1d6a1 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d 3e 6e 44 ; u.aq.ii<db->nD
1d6a2 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29 7b 0a 20 b; u.aq.ii++){.
1d6a3 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
1d6a4 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f lite3BtreeSavepo
1d6a5 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61 71 int(db->aDb[u.aq
1d6a6 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61 71 2e 70 .ii].pBt, u.aq.p
1d6a7 31 2c 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 1, u.aq.iSavepoi
1d6a8 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 nt);. i
1d6a9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1d6aa 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1d6ab 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1d6ac 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1d6ad 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1d6ae 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e if( u.aq.
1d6af 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f p1==SAVEPOINT_RO
1d6b0 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 2d 3e 66 LLBACK && (db->f
1d6b1 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 lags&SQLITE_Inte
1d6b2 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 20 29 7b rnChanges)!=0 ){
1d6b3 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1d6b4 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 e3ExpirePrepared
1d6b5 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a Statements(db);.
1d6b6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1d6b7 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 3ResetInternalSc
1d6b8 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 hema(db, 0);.
1d6b9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a }. }..
1d6ba 20 20 20 20 20 20 2f 2a 20 52 65 67 61 72 64 6c /* Regardl
1d6bb 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 ess of whether t
1d6bc 68 69 73 20 69 73 20 61 20 52 45 4c 45 41 53 45 his is a RELEASE
1d6bd 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65 or ROLLBACK, de
1d6be 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20 20 20 20 stroy all.
1d6bf 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65 ** savepoints ne
1d6c0 73 74 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 sted inside of t
1d6c1 68 65 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 he savepoint bei
1d6c2 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20 ng operated on.
1d6c3 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 */. while(
1d6c4 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d db->pSavepoint!=
1d6c5 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 u.aq.pSavepoint
1d6c6 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e ){. u.aq.
1d6c7 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 pTmp = db->pSave
1d6c8 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 point;. d
1d6c9 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 b->pSavepoint =
1d6ca 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e 65 78 74 u.aq.pTmp->pNext
1d6cb 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1d6cc 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 3DbFree(db, u.aq
1d6cd 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 .pTmp);.
1d6ce 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d db->nSavepoint--
1d6cf 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
1d6d0 20 2f 2a 20 49 66 20 69 74 20 69 73 20 61 20 52 /* If it is a R
1d6d1 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73 ELEASE, then des
1d6d2 74 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69 troy the savepoi
1d6d3 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 nt being operate
1d6d4 64 20 6f 6e 20 74 6f 6f 20 2a 2f 0a 20 20 20 20 d on too */.
1d6d5 20 20 69 66 28 20 75 2e 61 71 2e 70 31 3d 3d 53 if( u.aq.p1==S
1d6d6 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 AVEPOINT_RELEASE
1d6d7 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1d6d8 72 74 28 20 75 2e 61 71 2e 70 53 61 76 65 70 6f rt( u.aq.pSavepo
1d6d9 69 6e 74 3d 3d 64 62 2d 3e 70 53 61 76 65 70 6f int==db->pSavepo
1d6da 69 6e 74 20 29 3b 0a 20 20 20 20 20 20 20 20 64 int );. d
1d6db 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 b->pSavepoint =
1d6dc 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d u.aq.pSavepoint-
1d6dd 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
1d6de 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1d6df 2c 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e , u.aq.pSavepoin
1d6e0 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 t);. if(
1d6e1 21 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 !isTransaction )
1d6e2 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e {. db->
1d6e3 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20 nSavepoint--;.
1d6e4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1d6e5 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 65 }. }.. bre
1d6e6 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1d6e7 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 20 : AutoCommit P1
1d6e8 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 P2 * * *.**.** S
1d6e9 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 et the database
1d6ea 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 auto-commit flag
1d6eb 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 2e to P1 (1 or 0).
1d6ec 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c 20 If P2 is true,
1d6ed 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 roll.** back any
1d6ee 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 currently activ
1d6ef 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 74 e btree transact
1d6f0 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 61 ions. If there a
1d6f1 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a re any active.**
1d6f2 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f 6d VMs (apart from
1d6f3 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 6e this one), then
1d6f4 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 6c a ROLLBACK fail
1d6f5 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 69 s. A COMMIT fai
1d6f6 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 61 ls if.** there a
1d6f7 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69 6e re active writin
1d6f8 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65 20 g VMs or active
1d6f9 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68 61 VMs that use sha
1d6fa 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a red cache..**.**
1d6fb 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1d6fc 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d 20 n causes the VM
1d6fd 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 to halt..*/.case
1d6fe 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20 OP_AutoCommit:
1d6ff 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1d700 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d701 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 20 d into u.ar */.
1d702 20 69 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f int desiredAuto
1d703 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 20 69 52 Commit;. int iR
1d704 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e 74 20 74 ollback;. int t
1d705 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69 66 20 urnOnAC;.#endif
1d706 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d707 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d708 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72 2e 64 65 ar */.. u.ar.de
1d709 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20 siredAutoCommit
1d70a 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 = pOp->p1;. u.a
1d70b 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f r.iRollback = pO
1d70c 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72 2e 74 75 p->p2;. u.ar.tu
1d70d 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72 2e 64 65 rnOnAC = u.ar.de
1d70e 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20 siredAutoCommit
1d70f 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d && !db->autoComm
1d710 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e it;. assert( u.
1d711 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f ar.desiredAutoCo
1d712 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 72 2e mmit==1 || u.ar.
1d713 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 desiredAutoCommi
1d714 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 t==0 );. assert
1d715 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 ( u.ar.desiredAu
1d716 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 toCommit==1 || u
1d717 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 3d 3d 30 .ar.iRollback==0
1d718 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 );. assert( db
1d719 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
1d71a 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 0 ); /* At leas
1d71b 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 t this one VM is
1d71c 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 active */.. if
1d71d 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 ( u.ar.turnOnAC
1d71e 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 && u.ar.iRollbac
1d71f 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 k && db->activeV
1d720 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 dbeCnt>1 ){.
1d721 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 /* If this instr
1d722 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 uction implement
1d723 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 s a ROLLBACK and
1d724 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 0a 20 other VMs are.
1d725 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e ** still runn
1d726 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 61 6e 73 ing, and a trans
1d727 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
1d728 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f , return an erro
1d729 72 20 69 6e 64 69 63 61 74 69 6e 67 0a 20 20 20 r indicating.
1d72a 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 74 68 ** that the oth
1d72b 65 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 er VMs must comp
1d72c 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 20 20 20 lete first..
1d72d 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 */. sqlite3Se
1d72e 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1d72f 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 Msg, db, "cannot
1d730 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 rollback transa
1d731 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 ction - ".
1d732 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 "SQL statement
1d733 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b s in progress");
1d734 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1d735 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 _BUSY;. }else i
1d736 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 f( u.ar.turnOnAC
1d737 20 26 26 20 21 75 2e 61 72 2e 69 52 6f 6c 6c 62 && !u.ar.iRollb
1d738 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74 65 ack && db->write
1d739 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20 VdbeCnt>0 ){.
1d73a 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 /* If this inst
1d73b 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e ruction implemen
1d73c 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 20 ts a COMMIT and
1d73d 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77 72 other VMs are wr
1d73e 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 74 iting. ** ret
1d73f 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 urn an error ind
1d740 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 icating that the
1d741 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 other VMs must
1d742 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a complete first..
1d743 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
1d744 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1d745 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 zErrMsg, db, "ca
1d746 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61 6e nnot commit tran
1d747 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 saction - ".
1d748 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 "SQL stateme
1d749 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 nts in progress"
1d74a 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
1d74b 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 TE_BUSY;. }else
1d74c 20 69 66 28 20 75 2e 61 72 2e 64 65 73 69 72 65 if( u.ar.desire
1d74d 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d dAutoCommit!=db-
1d74e 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 >autoCommit ){.
1d74f 20 20 20 69 66 28 20 75 2e 61 72 2e 69 52 6f 6c if( u.ar.iRol
1d750 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 61 lback ){. a
1d751 73 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 ssert( u.ar.desi
1d752 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 redAutoCommit==1
1d753 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
1d754 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3RollbackAll(db)
1d755 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f ;. db->auto
1d756 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 Commit = 1;.
1d757 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d }else{. db-
1d758 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 >autoCommit = (u
1d759 38 29 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 8)u.ar.desiredAu
1d75a 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 toCommit;.
1d75b 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48 if( sqlite3VdbeH
1d75c 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 alt(p)==SQLITE_B
1d75d 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 70 USY ){. p
1d75e 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 ->pc = pc;.
1d75f 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 db->autoCommi
1d760 74 20 3d 20 28 75 38 29 28 31 2d 75 2e 61 72 2e t = (u8)(1-u.ar.
1d761 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 desiredAutoCommi
1d762 74 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 t);. p->r
1d763 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f c = rc = SQLITE_
1d764 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 67 6f BUSY;. go
1d765 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1d766 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1d767 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 assert( db->nS
1d768 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 3b 0a 20 tatement==0 );.
1d769 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 sqlite3CloseS
1d76a 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 avepoints(db);.
1d76b 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 if( p->rc==SQ
1d76c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1d76d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e rc = SQLITE_DON
1d76e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 E;. }else{.
1d76f 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1d770 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 ERROR;. }.
1d771 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 goto vdbe_retur
1d772 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 n;. }else{.
1d773 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 sqlite3SetString
1d774 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 (&p->zErrMsg, db
1d775 2c 0a 20 20 20 20 20 20 20 20 28 21 75 2e 61 72 ,. (!u.ar
1d776 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d .desiredAutoComm
1d777 69 74 29 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 it)?"cannot star
1d778 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 t a transaction
1d779 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 within a transac
1d77a 74 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 tion":(.
1d77b 28 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 29 (u.ar.iRollback)
1d77c 3f 22 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 ?"cannot rollbac
1d77d 6b 20 2d 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 k - no transacti
1d77e 6f 6e 20 69 73 20 61 63 74 69 76 65 22 3a 0a 20 on is active":.
1d77f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d780 20 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 "cannot commit
1d781 20 2d 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f - no transactio
1d782 6e 20 69 73 20 61 63 74 69 76 65 22 29 29 3b 0a n is active"));.
1d783 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1d784 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 62 72 _ERROR;. }. br
1d785 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d786 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 50 e: Transaction P
1d787 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1d788 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 Begin a transac
1d789 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 6e 73 tion. The trans
1d78a 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 65 6e action ends when
1d78b 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 6f 6c a Commit or Rol
1d78c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64 65 20 lback.** opcode
1d78d 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 is encountered.
1d78e 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 Depending on th
1d78f 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 73 65 e ON CONFLICT se
1d790 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20 74 72 tting, the.** tr
1d791 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 ansaction might
1d792 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 also be rolled b
1d793 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ack if an error
1d794 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a is encountered..
1d795 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 **.** P1 is the
1d796 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 index of the dat
1d797 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 77 68 abase file on wh
1d798 69 63 68 20 74 68 65 20 74 72 61 6e 73 61 63 74 ich the transact
1d799 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72 74 65 ion is.** starte
1d79a 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73 20 74 d. Index 0 is t
1d79b 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1d79c 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 file and index
1d79d 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 1 is the.** file
1d79e 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 used for tempor
1d79f 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49 6e 64 ary tables. Ind
1d7a0 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d 6f 72 ices of 2 or mor
1d7a1 65 20 61 72 65 20 75 73 65 64 20 66 6f 72 0a 2a e are used for.*
1d7a2 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 * attached datab
1d7a3 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 ases..**.** If P
1d7a4 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 2 is non-zero, t
1d7a5 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e hen a write-tran
1d7a6 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 saction is start
1d7a7 65 64 2e 20 20 41 20 52 45 53 45 52 56 45 44 20 ed. A RESERVED
1d7a8 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 lock is.** obtai
1d7a9 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ned on the datab
1d7aa 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 61 20 ase file when a
1d7ab 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1d7ac 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 4e n is started. N
1d7ad 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65 o.** other proce
1d7ae 73 73 20 63 61 6e 20 73 74 61 72 74 20 61 6e 6f ss can start ano
1d7af 74 68 65 72 20 77 72 69 74 65 20 74 72 61 6e 73 ther write trans
1d7b0 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69 action while thi
1d7b1 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 s transaction is
1d7b2 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20 20 53 .** underway. S
1d7b3 74 61 72 74 69 6e 67 20 61 20 77 72 69 74 65 20 tarting a write
1d7b4 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 73 6f transaction also
1d7b5 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c 6c 62 creates a rollb
1d7b6 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 0a 2a ack journal. A.*
1d7b7 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 * write transact
1d7b8 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 ion must be star
1d7b9 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63 ted before any c
1d7ba 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20 6d 61 hanges can be ma
1d7bb 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 de to the.** dat
1d7bc 61 62 61 73 65 2e 20 20 49 66 20 50 32 20 69 73 abase. If P2 is
1d7bd 20 32 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 2 or greater th
1d7be 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 en an EXCLUSIVE
1d7bf 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f 62 74 lock is also obt
1d7c0 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 ained.** on the
1d7c1 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 file..**.** If a
1d7c2 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
1d7c3 6f 6e 20 69 73 20 73 74 61 72 74 65 64 20 61 6e on is started an
1d7c4 64 20 74 68 65 20 56 64 62 65 2e 75 73 65 73 53 d the Vdbe.usesS
1d7c5 74 6d 74 4a 6f 75 72 6e 61 6c 20 66 6c 61 67 20 tmtJournal flag
1d7c6 69 73 0a 2a 2a 20 74 72 75 65 20 28 74 68 69 73 is.** true (this
1d7c7 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 66 20 flag is set if
1d7c8 74 68 65 20 56 64 62 65 20 6d 61 79 20 6d 6f 64 the Vdbe may mod
1d7c9 69 66 79 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e ify more than on
1d7ca 65 20 72 6f 77 20 61 6e 64 20 6d 61 79 0a 2a 2a e row and may.**
1d7cb 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54 20 throw an ABORT
1d7cc 65 78 63 65 70 74 69 6f 6e 29 2c 20 61 20 73 74 exception), a st
1d7cd 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1d7ce 69 6f 6e 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 ion may also be
1d7cf 6f 70 65 6e 65 64 2e 0a 2a 2a 20 4d 6f 72 65 20 opened..** More
1d7d0 73 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 61 20 specifically, a
1d7d1 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1d7d2 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 ction is opened
1d7d3 69 66 66 20 74 68 65 20 64 61 74 61 62 61 73 65 iff the database
1d7d4 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 .** connection i
1d7d5 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20 s currently not
1d7d6 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f in autocommit mo
1d7d7 64 65 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20 de, or if there
1d7d8 61 72 65 20 6f 74 68 65 72 0a 2a 2a 20 61 63 74 are other.** act
1d7d9 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 ive statements.
1d7da 41 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e A statement tran
1d7db 73 61 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 20 74 saction allows t
1d7dc 68 65 20 61 66 66 65 63 74 73 20 6f 66 20 74 68 he affects of th
1d7dd 69 73 0a 2a 2a 20 56 44 42 45 20 74 6f 20 62 65 is.** VDBE to be
1d7de 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 rolled back aft
1d7df 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69 74 68 er an error with
1d7e0 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f out having to ro
1d7e1 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65 ll back the.** e
1d7e2 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f ntire transactio
1d7e3 6e 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 69 n. If no error i
1d7e4 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 s encountered, t
1d7e5 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 he statement tra
1d7e6 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c nsaction.** will
1d7e7 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 automatically c
1d7e8 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 65 20 56 ommit when the V
1d7e9 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a 0a 2a 2a DBE halts..**.**
1d7ea 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f 2c 20 If P2 is zero,
1d7eb 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b then a read-lock
1d7ec 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 is obtained on
1d7ed 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1d7ee 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 e..*/.case OP_Tr
1d7ef 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 23 69 66 ansaction: {.#if
1d7f0 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1d7f1 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1d7f2 6f 20 75 2e 61 73 20 2a 2f 0a 20 20 42 74 72 65 o u.as */. Btre
1d7f3 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f e *pBt;.#endif /
1d7f4 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d7f5 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d7f6 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 s */.. assert(
1d7f7 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1d7f8 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b p->p1<db->nDb );
1d7f9 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 . assert( (p->b
1d7fa 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 treeMask & (1<<p
1d7fb 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 Op->p1))!=0 );.
1d7fc 20 75 2e 61 73 2e 70 42 74 20 3d 20 64 62 2d 3e u.as.pBt = db->
1d7fd 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 aDb[pOp->p1].pBt
1d7fe 3b 0a 0a 20 20 69 66 28 20 75 2e 61 73 2e 70 42 ;.. if( u.as.pB
1d7ff 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 t ){. rc = sq
1d800 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
1d801 72 61 6e 73 28 75 2e 61 73 2e 70 42 74 2c 20 70 rans(u.as.pBt, p
1d802 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69 66 28 Op->p2);. if(
1d803 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
1d804 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 ){. p->pc
1d805 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 = pc;. p->r
1d806 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f c = rc = SQLITE_
1d807 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f BUSY;. goto
1d808 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 vdbe_return;.
1d809 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
1d80a 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 SQLITE_OK && rc!
1d80b 3d 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 =SQLITE_READONLY
1d80c 20 2f 2a 20 26 26 20 72 63 21 3d 53 51 4c 49 54 /* && rc!=SQLIT
1d80d 45 5f 42 55 53 59 20 2a 2f 20 29 7b 0a 20 20 20 E_BUSY */ ){.
1d80e 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1d80f 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1d810 7d 0a 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e }.. if( pOp->
1d811 70 32 20 26 26 20 70 2d 3e 75 73 65 73 53 74 6d p2 && p->usesStm
1d812 74 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 26 26 tJournal. &&
1d813 20 28 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 (db->autoCommit
1d814 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 63 74 69 76 ==0 || db->activ
1d815 65 56 64 62 65 43 6e 74 3e 31 29 0a 20 20 20 20 eVdbeCnt>1).
1d816 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1d817 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
1d818 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 42 74 29 nTrans(u.as.pBt)
1d819 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d );. if( p-
1d81a 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 >iStatement==0 )
1d81b 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1d81c 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 ( db->nStatement
1d81d 3e 3d 30 20 26 26 20 64 62 2d 3e 6e 53 61 76 65 >=0 && db->nSave
1d81e 70 6f 69 6e 74 3e 3d 30 20 29 3b 0a 20 20 20 20 point>=0 );.
1d81f 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 db->nStateme
1d820 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 70 2d nt++;. p-
1d821 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 64 62 >iStatement = db
1d822 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b 20 64 ->nSavepoint + d
1d823 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3b 0a 20 b->nStatement;.
1d824 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 }. rc
1d825 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 = sqlite3BtreeBe
1d826 67 69 6e 53 74 6d 74 28 75 2e 61 73 2e 70 42 74 ginStmt(u.as.pBt
1d827 2c 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 29 , p->iStatement)
1d828 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1d829 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d82a 65 3a 20 52 65 61 64 43 6f 6f 6b 69 65 20 50 31 e: ReadCookie P1
1d82b 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1d82c 20 52 65 61 64 20 63 6f 6f 6b 69 65 20 6e 75 6d Read cookie num
1d82d 62 65 72 20 50 33 20 66 72 6f 6d 20 64 61 74 61 ber P3 from data
1d82e 62 61 73 65 20 50 31 20 61 6e 64 20 77 72 69 74 base P1 and writ
1d82f 65 20 69 74 20 69 6e 74 6f 20 72 65 67 69 73 74 e it into regist
1d830 65 72 20 50 32 2e 0a 2a 2a 20 50 33 3d 3d 31 20 er P2..** P3==1
1d831 69 73 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 is the schema ve
1d832 72 73 69 6f 6e 2e 20 20 50 33 3d 3d 32 20 69 73 rsion. P3==2 is
1d833 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f the database fo
1d834 72 6d 61 74 2e 0a 2a 2a 20 50 33 3d 3d 33 20 69 rmat..** P3==3 i
1d835 73 20 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 s the recommende
1d836 64 20 70 61 67 65 72 20 63 61 63 68 65 20 73 69 d pager cache si
1d837 7a 65 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 ze, and so forth
1d838 2e 20 20 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 . P1==0 is.** t
1d839 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1d83a 20 66 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 file and P1==1
1d83b 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 is the database
1d83c 66 69 6c 65 20 75 73 65 64 20 74 6f 20 73 74 6f file used to sto
1d83d 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 re.** temporary
1d83e 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 tables..**.** Th
1d83f 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 72 65 ere must be a re
1d840 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 ad-lock on the d
1d841 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20 atabase (either
1d842 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a a transaction.**
1d843 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 64 must be started
1d844 20 6f 72 20 74 68 65 72 65 20 6d 75 73 74 20 62 or there must b
1d845 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 e an open cursor
1d846 29 20 62 65 66 6f 72 65 0a 2a 2a 20 65 78 65 63 ) before.** exec
1d847 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72 uting this instr
1d848 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 uction..*/.case
1d849 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 3a 20 7b OP_ReadCookie: {
1d84a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d84b 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1d84c 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c e */.#if 0 /* l
1d84d 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d84e 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a oved into u.at *
1d84f 2f 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 /. int iMeta;.
1d850 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 int iDb;. int
1d851 69 43 6f 6f 6b 69 65 3b 0a 23 65 6e 64 69 66 20 iCookie;.#endif
1d852 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d853 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d854 61 74 20 2a 2f 0a 0a 20 20 75 2e 61 74 2e 69 44 at */.. u.at.iD
1d855 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 b = pOp->p1;. u
1d856 2e 61 74 2e 69 43 6f 6f 6b 69 65 20 3d 20 70 4f .at.iCookie = pO
1d857 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 p->p3;. assert(
1d858 20 70 4f 70 2d 3e 70 33 3c 53 51 4c 49 54 45 5f pOp->p3<SQLITE_
1d859 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a N_BTREE_META );.
1d85a 20 20 61 73 73 65 72 74 28 20 75 2e 61 74 2e 69 assert( u.at.i
1d85b 44 62 3e 3d 30 20 26 26 20 75 2e 61 74 2e 69 44 Db>=0 && u.at.iD
1d85c 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 b<db->nDb );. a
1d85d 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 75 ssert( db->aDb[u
1d85e 2e 61 74 2e 69 44 62 5d 2e 70 42 74 21 3d 30 20 .at.iDb].pBt!=0
1d85f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1d860 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1d861 3c 75 2e 61 74 2e 69 44 62 29 29 21 3d 30 20 29 <u.at.iDb))!=0 )
1d862 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
1d863 65 47 65 74 4d 65 74 61 28 64 62 2d 3e 61 44 62 eGetMeta(db->aDb
1d864 5b 75 2e 61 74 2e 69 44 62 5d 2e 70 42 74 2c 20 [u.at.iDb].pBt,
1d865 75 2e 61 74 2e 69 43 6f 6f 6b 69 65 2c 20 28 75 u.at.iCookie, (u
1d866 33 32 20 2a 29 26 75 2e 61 74 2e 69 4d 65 74 61 32 *)&u.at.iMeta
1d867 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d );. pOut->u.i =
1d868 20 75 2e 61 74 2e 69 4d 65 74 61 3b 0a 20 20 4d u.at.iMeta;. M
1d869 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
1d86a 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 ut, MEM_Int);.
1d86b 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1d86c 6f 64 65 3a 20 53 65 74 43 6f 6f 6b 69 65 20 50 ode: SetCookie P
1d86d 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1d86e 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74 * Write the cont
1d86f 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 ent of register
1d870 50 33 20 28 69 6e 74 65 72 70 72 65 74 65 64 20 P3 (interpreted
1d871 61 73 20 61 6e 20 69 6e 74 65 67 65 72 29 0a 2a as an integer).*
1d872 2a 20 69 6e 74 6f 20 63 6f 6f 6b 69 65 20 6e 75 * into cookie nu
1d873 6d 62 65 72 20 50 32 20 6f 66 20 64 61 74 61 62 mber P2 of datab
1d874 61 73 65 20 50 31 2e 20 20 50 32 3d 3d 31 20 69 ase P1. P2==1 i
1d875 73 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 s the schema ver
1d876 73 69 6f 6e 2e 20 20 0a 2a 2a 20 50 32 3d 3d 32 sion. .** P2==2
1d877 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 is the database
1d878 20 66 6f 72 6d 61 74 2e 20 50 32 3d 3d 33 20 69 format. P2==3 i
1d879 73 20 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65 s the recommende
1d87a 64 20 70 61 67 65 72 20 63 61 63 68 65 20 0a 2a d pager cache .*
1d87b 2a 20 73 69 7a 65 2c 20 61 6e 64 20 73 6f 20 66 * size, and so f
1d87c 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 69 73 20 orth. P1==0 is
1d87d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1d87e 65 20 66 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31 e file and P1==1
1d87f 20 69 73 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 is the .** data
1d880 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 base file used t
1d881 6f 20 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72 o store temporar
1d882 79 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 y tables..**.**
1d883 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 A transaction mu
1d884 73 74 20 62 65 20 73 74 61 72 74 65 64 20 62 65 st be started be
1d885 66 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 74 fore executing t
1d886 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 his opcode..*/.c
1d887 61 73 65 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 ase OP_SetCookie
1d888 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 33 : { /* in3
1d889 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1d88a 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1d88b 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f ved into u.au */
1d88c 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 . Db *pDb;.#end
1d88d 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1d88e 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d88f 20 75 2e 61 75 20 2a 2f 0a 20 20 61 73 73 65 72 u.au */. asser
1d890 74 28 20 70 4f 70 2d 3e 70 32 3c 53 51 4c 49 54 t( pOp->p2<SQLIT
1d891 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 E_N_BTREE_META )
1d892 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1d893 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1d894 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 1<db->nDb );. a
1d895 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 ssert( (p->btree
1d896 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e Mask & (1<<pOp->
1d897 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 61 p1))!=0 );. u.a
1d898 75 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 u.pDb = &db->aDb
1d899 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1d89a 65 72 74 28 20 75 2e 61 75 2e 70 44 62 2d 3e 70 ert( u.au.pDb->p
1d89b 42 74 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 Bt!=0 );. sqlit
1d89c 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1d89d 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a 20 ify(pIn3);. /*
1d89e 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 69 See note about i
1d89f 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f 6e ndex shifting on
1d8a0 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 2a OP_ReadCookie *
1d8a1 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 /. rc = sqlite3
1d8a2 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 BtreeUpdateMeta(
1d8a3 75 2e 61 75 2e 70 44 62 2d 3e 70 42 74 2c 20 70 u.au.pDb->pBt, p
1d8a4 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e Op->p2, (int)pIn
1d8a5 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 3->u.i);. if( p
1d8a6 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 53 43 Op->p2==BTREE_SC
1d8a7 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 29 7b 0a HEMA_VERSION ){.
1d8a8 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 /* When the
1d8a9 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 schema cookie ch
1d8aa 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74 68 anges, record th
1d8ab 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 e new cookie int
1d8ac 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 75 ernally */. u
1d8ad 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 .au.pDb->pSchema
1d8ae 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 ->schema_cookie
1d8af 3d 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 = (int)pIn3->u.i
1d8b0 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 ;. db->flags
1d8b1 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e |= SQLITE_Intern
1d8b2 43 68 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 65 Changes;. }else
1d8b3 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 54 if( pOp->p2==BT
1d8b4 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 REE_FILE_FORMAT
1d8b5 29 7b 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 ){. /* Record
1d8b6 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 changes in the
1d8b7 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 file format */.
1d8b8 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63 u.au.pDb->pSc
1d8b9 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 hema->file_forma
1d8ba 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75 2e t = (u8)pIn3->u.
1d8bb 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 i;. }. if( pOp
1d8bc 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 2f ->p1==1 ){. /
1d8bd 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c * Invalidate all
1d8be 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
1d8bf 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74 68 ents whenever th
1d8c0 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 0a e TEMP database.
1d8c1 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 73 ** schema is
1d8c2 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b 65 changed. Ticke
1d8c3 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 73 t #1644 */. s
1d8c4 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 qlite3ExpirePrep
1d8c5 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 aredStatements(d
1d8c6 62 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b b);. }. break;
1d8c7 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 .}../* Opcode: V
1d8c8 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20 50 erifyCookie P1 P
1d8c9 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 2 *.**.** Check
1d8ca 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c 6f the value of glo
1d8cb 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 72 bal database par
1d8cc 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30 20 ameter number 0
1d8cd 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 76 (the.** schema v
1d8ce 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b 65 ersion) and make
1d8cf 20 73 75 72 65 20 69 74 20 69 73 20 65 71 75 61 sure it is equa
1d8d0 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50 31 l to P2. .** P1
1d8d1 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 is the database
1d8d2 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73 number which is
1d8d3 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 0 for the main
1d8d4 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a database file.**
1d8d5 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20 66 and 1 for the f
1d8d6 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 ile holding temp
1d8d7 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 orary tables and
1d8d8 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75 6d some higher num
1d8d9 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69 6c ber.** for auxil
1d8da 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e 0a iary databases..
1d8db 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69 65 **.** The cookie
1d8dc 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 61 6c changes its val
1d8dd 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 ue whenever the
1d8de 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
1d8df 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69 73 changes..** This
1d8e0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 73 operation is us
1d8e1 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 68 65 ed to detect whe
1d8e2 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b 69 n that the cooki
1d8e3 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a e has changed.**
1d8e4 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 75 and that the cu
1d8e5 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e 65 rrent process ne
1d8e6 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 74 68 eds to reread th
1d8e7 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 e schema..**.**
1d8e8 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 Either a transac
1d8e9 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68 61 tion needs to ha
1d8ea 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 64 20 ve been started
1d8eb 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65 or an OP_Open ne
1d8ec 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78 65 eds.** to be exe
1d8ed 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 62 6c cuted (to establ
1d8ee 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b 29 ish a read lock)
1d8ef 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70 63 before this opc
1d8f0 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65 ode is.** invoke
1d8f1 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 65 d..*/.case OP_Ve
1d8f2 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 23 69 rifyCookie: {.#i
1d8f3 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1d8f4 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d8f5 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 69 6e 74 to u.av */. int
1d8f6 20 69 4d 65 74 61 3b 0a 20 20 42 74 72 65 65 20 iMeta;. Btree
1d8f7 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 *pBt;.#endif /*
1d8f8 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d8f9 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 76 20 moved into u.av
1d8fa 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 */. assert( pOp
1d8fb 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1d8fc 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 p1<db->nDb );.
1d8fd 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
1d8fe 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d eMask & (1<<pOp-
1d8ff 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e >p1))!=0 );. u.
1d900 61 76 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 av.pBt = db->aDb
1d901 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 [pOp->p1].pBt;.
1d902 20 69 66 28 20 75 2e 61 76 2e 70 42 74 20 29 7b if( u.av.pBt ){
1d903 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1d904 65 47 65 74 4d 65 74 61 28 75 2e 61 76 2e 70 42 eGetMeta(u.av.pB
1d905 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f t, BTREE_SCHEMA_
1d906 56 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a 29 VERSION, (u32 *)
1d907 26 75 2e 61 76 2e 69 4d 65 74 61 29 3b 0a 20 20 &u.av.iMeta);.
1d908 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 76 2e }else{. u.av.
1d909 69 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 iMeta = 0;. }.
1d90a 20 69 66 28 20 75 2e 61 76 2e 69 4d 65 74 61 21 if( u.av.iMeta!
1d90b 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20 =pOp->p2 ){.
1d90c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
1d90d 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 , p->zErrMsg);.
1d90e 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 p->zErrMsg =
1d90f 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
1d910 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 73 63 db, "database sc
1d911 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 hema has changed
1d912 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ");. /* If th
1d913 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 e schema-cookie
1d914 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1d915 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74 e file matches t
1d916 68 65 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a he cookie. **
1d917 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68 65 stored with the
1d918 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 in-memory repre
1d919 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 sentation of the
1d91a 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20 20 schema, do.
1d91b 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74 68 ** not reload th
1d91c 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74 68 e schema from th
1d91d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
1d91e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
1d91f 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 73 f virtual-tables
1d920 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68 69 are in use, thi
1d921 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 6e s is not just an
1d922 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 20 optimization..
1d923 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d 74 ** Often, v-t
1d924 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65 69 ables store thei
1d925 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72 20 r data in other
1d926 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 77 SQLite tables, w
1d927 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65 20 hich. ** are
1d928 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69 74 queried from wit
1d929 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 20 hin xNext() and
1d92a 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d 65 other v-table me
1d92b 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20 20 thods using.
1d92c 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65 72 ** prepared quer
1d92d 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20 71 ies. If such a q
1d92e 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d 64 uery is out-of-d
1d92f 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 77 ate, we do not w
1d930 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 ant to. ** di
1d931 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62 61 scard the databa
1d932 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74 68 se schema, as th
1d933 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70 6c e user code impl
1d934 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20 20 ementing the.
1d935 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75 6c ** v-table woul
1d936 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65 61 d have to be rea
1d937 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 dy for the sqlit
1d938 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 e3_vtab structur
1d939 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a 20 e itself. **
1d93a 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 to be invalidate
1d93b 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69 74 d whenever sqlit
1d93c 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c e3_step() is cal
1d93d 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a led from within.
1d93e 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 6c 65 ** a v-table
1d93f 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a 2f 0a method.. */.
1d940 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b if( db->aDb[
1d941 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61 pOp->p1].pSchema
1d942 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 21 ->schema_cookie!
1d943 3d 75 2e 61 76 2e 69 4d 65 74 61 20 29 7b 0a 20 =u.av.iMeta ){.
1d944 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 sqlite3Rese
1d945 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 tInternalSchema(
1d946 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 db, pOp->p1);.
1d947 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 }.. sqlite3
1d948 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 ExpirePreparedSt
1d949 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 atements(db);.
1d94a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 rc = SQLITE_SC
1d94b 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61 HEMA;. }. brea
1d94c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d94d 20 4f 70 65 6e 52 65 61 64 20 50 31 20 50 32 20 OpenRead P1 P2
1d94e 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f P3 P4 P5.**.** O
1d94f 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 pen a read-only
1d950 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64 cursor for the d
1d951 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 77 68 atabase table wh
1d952 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 ose root page is
1d953 0a 2a 2a 20 50 32 20 69 6e 20 61 20 64 61 74 61 .** P2 in a data
1d954 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 base file. The
1d955 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
1d956 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 50 determined by P
1d957 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61 3. .** P3==0 mea
1d958 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 ns the main data
1d959 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e base, P3==1 mean
1d95a 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 75 s the database u
1d95b 73 65 64 20 66 6f 72 20 0a 2a 2a 20 74 65 6d 70 sed for .** temp
1d95c 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20 61 6e orary tables, an
1d95d 64 20 50 33 3e 31 20 6d 65 61 6e 73 20 75 73 65 d P3>1 means use
1d95e 64 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 d the correspond
1d95f 69 6e 67 20 61 74 74 61 63 68 65 64 0a 2a 2a 20 ing attached.**
1d960 64 61 74 61 62 61 73 65 2e 20 20 47 69 76 65 20 database. Give
1d961 74 68 65 20 6e 65 77 20 63 75 72 73 6f 72 20 61 the new cursor a
1d962 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f 66 20 n identifier of
1d963 50 31 2e 20 20 54 68 65 20 50 31 0a 2a 2a 20 76 P1. The P1.** v
1d964 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f 74 20 62 alues need not b
1d965 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 62 75 74 e contiguous but
1d966 20 61 6c 6c 20 50 31 20 76 61 6c 75 65 73 20 73 all P1 values s
1d967 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 20 69 hould be small i
1d968 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 49 74 20 69 ntegers..** It i
1d969 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 50 s an error for P
1d96a 31 20 74 6f 20 62 65 20 6e 65 67 61 74 69 76 65 1 to be negative
1d96b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 21 3d 30 ..**.** If P5!=0
1d96c 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 63 6f then use the co
1d96d 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 ntent of registe
1d96e 72 20 50 32 20 61 73 20 74 68 65 20 72 6f 6f 74 r P2 as the root
1d96f 20 70 61 67 65 2c 20 6e 6f 74 0a 2a 2a 20 74 68 page, not.** th
1d970 65 20 76 61 6c 75 65 20 6f 66 20 50 32 20 69 74 e value of P2 it
1d971 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 self..**.** Ther
1d972 65 20 77 69 6c 6c 20 62 65 20 61 20 72 65 61 64 e will be a read
1d973 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
1d974 61 62 61 73 65 20 77 68 65 6e 65 76 65 72 20 74 abase whenever t
1d975 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70 here is an.** op
1d976 65 6e 20 63 75 72 73 6f 72 2e 20 20 49 66 20 74 en cursor. If t
1d977 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 he database was
1d978 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f 72 20 74 unlocked prior t
1d979 6f 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 o this instructi
1d97a 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20 72 65 61 on.** then a rea
1d97b 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72 d lock is acquir
1d97c 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 ed as part of th
1d97d 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 is instruction.
1d97e 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20 A read.** lock
1d97f 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f allows other pro
1d980 63 65 73 73 65 73 20 74 6f 20 72 65 61 64 20 74 cesses to read t
1d981 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 he database but
1d982 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79 prohibits.** any
1d983 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 66 other process f
1d984 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 rom modifying th
1d985 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 e database. The
1d986 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a read lock is.**
1d987 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 released when a
1d988 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 20 63 ll cursors are c
1d989 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20 losed. If this
1d98a 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 74 65 instruction atte
1d98b 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61 mpts.** to get a
1d98c 20 72 65 61 64 20 6c 6f 63 6b 20 62 75 74 20 66 read lock but f
1d98d 61 69 6c 73 2c 20 74 68 65 20 73 63 72 69 70 74 ails, the script
1d98e 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69 74 68 terminates with
1d98f 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 an.** SQLITE_BU
1d990 53 59 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a SY error code..*
1d991 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 *.** The P4 valu
1d992 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 e may be either
1d993 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49 an integer (P4_I
1d994 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74 NT32) or a point
1d995 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e er to.** a KeyIn
1d996 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 50 34 fo structure (P4
1d997 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74 _KEYINFO). If it
1d998 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1d999 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 a KeyInfo .** s
1d99a 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73 tructure, then s
1d99b 61 69 64 20 73 74 72 75 63 74 75 72 65 20 64 65 aid structure de
1d99c 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e fines the conten
1d99d 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 t and collating
1d99e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20 .** sequence of
1d99f 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 the index being
1d9a0 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 opened. Otherwis
1d9a1 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e 20 69 e, if P4 is an i
1d9a2 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65 nteger .** value
1d9a3 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74 , it is set to t
1d9a4 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1d9a5 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c umns in the tabl
1d9a6 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 e..**.** See als
1d9a7 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a o OpenWrite..*/.
1d9a8 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57 /* Opcode: OpenW
1d9a9 72 69 74 65 20 50 31 20 50 32 20 50 33 20 50 34 rite P1 P2 P3 P4
1d9aa 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 P5.**.** Open a
1d9ab 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 read/write curs
1d9ac 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f 6e 20 74 or named P1 on t
1d9ad 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 he table or inde
1d9ae 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20 x whose root.**
1d9af 70 61 67 65 20 69 73 20 50 32 2e 20 20 4f 72 20 page is P2. Or
1d9b0 69 66 20 50 35 21 3d 30 20 75 73 65 20 74 68 65 if P5!=0 use the
1d9b1 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1d9b2 73 74 65 72 20 50 32 20 74 6f 20 66 69 6e 64 20 ster P2 to find
1d9b3 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 the.** root page
1d9b4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 ..**.** The P4 v
1d9b5 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68 alue may be eith
1d9b6 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50 er an integer (P
1d9b7 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 4_INT32) or a po
1d9b8 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 inter to.** a Ke
1d9b9 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 yInfo structure
1d9ba 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 (P4_KEYINFO). If
1d9bb 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 it is a pointer
1d9bc 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a to a KeyInfo .*
1d9bd 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 * structure, the
1d9be 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72 65 n said structure
1d9bf 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f 6e defines the con
1d9c0 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 tent and collati
1d9c1 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ng .** sequence
1d9c2 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 69 of the index bei
1d9c3 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 ng opened. Other
1d9c4 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 61 wise, if P4 is a
1d9c5 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 n integer .** va
1d9c6 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 74 lue, it is set t
1d9c7 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
1d9c8 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 columns in the t
1d9c9 61 62 6c 65 2c 20 6f 72 20 74 6f 20 74 68 65 0a able, or to the.
1d9ca 2a 2a 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 ** largest index
1d9cb 20 6f 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f of any column o
1d9cc 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 f the table that
1d9cd 20 69 73 20 61 63 74 75 61 6c 6c 79 20 75 73 65 is actually use
1d9ce 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e d..**.** This in
1d9cf 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20 struction works
1d9d0 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65 just like OpenRe
1d9d1 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ad except that i
1d9d2 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73 t opens the curs
1d9d3 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72 or.** in read/wr
1d9d4 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61 ite mode. For a
1d9d5 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68 given table, th
1d9d6 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f ere can be one o
1d9d7 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79 r more read-only
1d9d8 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61 .** cursors or a
1d9d9 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69 single read/wri
1d9da 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f te cursor but no
1d9db 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65 t both..**.** Se
1d9dc 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e e also OpenRead.
1d9dd 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e .*/.case OP_Open
1d9de 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f 4f 70 Read:.case OP_Op
1d9df 65 6e 57 72 69 74 65 3a 20 7b 0a 23 69 66 20 30 enWrite: {.#if 0
1d9e0 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1d9e1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d9e2 75 2e 61 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 u.aw */. int nF
1d9e3 69 65 6c 64 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 ield;. KeyInfo
1d9e4 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e 74 *pKeyInfo;. int
1d9e5 20 70 32 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a p2;. int iDb;.
1d9e6 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 int wrFlag;.
1d9e7 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 64 62 Btree *pX;. Vdb
1d9e8 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 eCursor *pCur;.
1d9e9 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66 Db *pDb;.#endif
1d9ea 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d9eb 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d9ec 2e 61 77 20 2a 2f 0a 0a 20 20 75 2e 61 77 2e 6e .aw */.. u.aw.n
1d9ed 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 75 2e 61 Field = 0;. u.a
1d9ee 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a w.pKeyInfo = 0;.
1d9ef 20 20 75 2e 61 77 2e 70 32 20 3d 20 70 4f 70 2d u.aw.p2 = pOp-
1d9f0 3e 70 32 3b 0a 20 20 75 2e 61 77 2e 69 44 62 20 >p2;. u.aw.iDb
1d9f1 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 = pOp->p3;. ass
1d9f2 65 72 74 28 20 75 2e 61 77 2e 69 44 62 3e 3d 30 ert( u.aw.iDb>=0
1d9f3 20 26 26 20 75 2e 61 77 2e 69 44 62 3c 64 62 2d && u.aw.iDb<db-
1d9f4 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1d9f5 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1d9f6 26 20 28 31 3c 3c 75 2e 61 77 2e 69 44 62 29 29 & (1<<u.aw.iDb))
1d9f7 21 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 44 !=0 );. u.aw.pD
1d9f8 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 75 2e 61 b = &db->aDb[u.a
1d9f9 77 2e 69 44 62 5d 3b 0a 20 20 75 2e 61 77 2e 70 w.iDb];. u.aw.p
1d9fa 58 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 42 X = u.aw.pDb->pB
1d9fb 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 t;. assert( u.a
1d9fc 77 2e 70 58 21 3d 30 20 29 3b 0a 20 20 69 66 28 w.pX!=0 );. if(
1d9fd 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1d9fe 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a 20 20 _OpenWrite ){.
1d9ff 20 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20 u.aw.wrFlag =
1da00 31 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 77 2e 1;. if( u.aw.
1da01 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 pDb->pSchema->fi
1da02 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d le_format < p->m
1da03 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 inWriteFileForma
1da04 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 t ){. p->mi
1da05 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 nWriteFileFormat
1da06 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 = u.aw.pDb->pSc
1da07 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 hema->file_forma
1da08 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 t;. }. }else
1da09 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 6c 61 {. u.aw.wrFla
1da0a 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 g = 0;. }. if(
1da0b 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 pOp->p5 ){.
1da0c 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 3e assert( u.aw.p2>
1da0d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
1da0e 20 75 2e 61 77 2e 70 32 3c 3d 70 2d 3e 6e 4d 65 u.aw.p2<=p->nMe
1da0f 6d 20 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 m );. pIn2 =
1da10 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 77 2e 70 32 &p->aMem[u.aw.p2
1da11 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
1da12 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
1da13 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e 61 77 2e pIn2);. u.aw.
1da14 70 32 20 3d 20 28 69 6e 74 29 70 49 6e 32 2d 3e p2 = (int)pIn2->
1da15 75 2e 69 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 u.i;. /* The
1da16 75 2e 61 77 2e 70 32 20 76 61 6c 75 65 20 61 6c u.aw.p2 value al
1da17 77 61 79 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20 ways comes from
1da18 61 20 70 72 69 6f 72 20 4f 50 5f 43 72 65 61 74 a prior OP_Creat
1da19 65 54 61 62 6c 65 20 6f 70 63 6f 64 65 20 61 6e eTable opcode an
1da1a 64 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 6f 70 d. ** that op
1da1b 63 6f 64 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 code will always
1da1c 20 73 65 74 20 74 68 65 20 75 2e 61 77 2e 70 32 set the u.aw.p2
1da1d 20 76 61 6c 75 65 20 74 6f 20 32 20 6f 72 20 6d value to 2 or m
1da1e 6f 72 65 20 6f 72 20 65 6c 73 65 20 66 61 69 6c ore or else fail
1da1f 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 .. ** If ther
1da20 65 20 77 65 72 65 20 61 20 66 61 69 6c 75 72 65 e were a failure
1da21 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 , the prepared s
1da22 74 61 74 65 6d 65 6e 74 20 77 6f 75 6c 64 20 68 tatement would h
1da23 61 76 65 20 68 61 6c 74 65 64 0a 20 20 20 20 2a ave halted. *
1da24 2a 20 62 65 66 6f 72 65 20 72 65 61 63 68 69 6e * before reachin
1da25 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 g this instructi
1da26 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e on. */. if( N
1da27 45 56 45 52 28 75 2e 61 77 2e 70 32 3c 32 29 20 EVER(u.aw.p2<2)
1da28 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ) {. rc = S
1da29 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1da2a 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 PT;. goto a
1da2b 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1da2c 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 r;. }. }. i
1da2d 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d f( pOp->p4type==
1da2e 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 P4_KEYINFO ){.
1da2f 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 20 u.aw.pKeyInfo
1da30 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e = pOp->p4.pKeyIn
1da31 66 6f 3b 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65 fo;. u.aw.pKe
1da32 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 yInfo->enc = ENC
1da33 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 75 2e 61 (p->db);. u.a
1da34 77 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 61 77 2e w.nField = u.aw.
1da35 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1da36 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 +1;. }else if(
1da37 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1da38 49 4e 54 33 32 20 29 7b 0a 20 20 20 20 75 2e 61 INT32 ){. u.a
1da39 77 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e w.nField = pOp->
1da3a 70 34 2e 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 p4.i;. }. asse
1da3b 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 rt( pOp->p1>=0 )
1da3c 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 20 3d 20 ;. u.aw.pCur =
1da3d 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 allocateCursor(p
1da3e 2c 20 70 4f 70 2d 3e 70 31 2c 20 75 2e 61 77 2e , pOp->p1, u.aw.
1da3f 6e 46 69 65 6c 64 2c 20 75 2e 61 77 2e 69 44 62 nField, u.aw.iDb
1da40 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e 61 77 , 1);. if( u.aw
1da41 2e 70 43 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 .pCur==0 ) goto
1da42 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 77 2e 70 no_mem;. u.aw.p
1da43 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 Cur->nullRow = 1
1da44 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1da45 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e 61 77 BtreeCursor(u.aw
1da46 2e 70 58 2c 20 75 2e 61 77 2e 70 32 2c 20 75 2e .pX, u.aw.p2, u.
1da47 61 77 2e 77 72 46 6c 61 67 2c 20 75 2e 61 77 2e aw.wrFlag, u.aw.
1da48 70 4b 65 79 49 6e 66 6f 2c 20 75 2e 61 77 2e 70 pKeyInfo, u.aw.p
1da49 43 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 Cur->pCursor);.
1da4a 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 4b 65 79 u.aw.pCur->pKey
1da4b 49 6e 66 6f 20 3d 20 75 2e 61 77 2e 70 4b 65 79 Info = u.aw.pKey
1da4c 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 Info;.. /* Sinc
1da4d 65 20 69 74 20 70 65 72 66 6f 72 6d 73 20 6e 6f e it performs no
1da4e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
1da4f 6f 6e 20 6f 72 20 49 4f 2c 20 74 68 65 20 6f 6e on or IO, the on
1da50 6c 79 20 76 61 6c 75 65 73 20 74 68 61 74 0a 20 ly values that.
1da51 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 ** sqlite3Btree
1da52 43 75 72 73 6f 72 28 29 20 6d 61 79 20 72 65 74 Cursor() may ret
1da53 75 72 6e 20 61 72 65 20 53 51 4c 49 54 45 5f 45 urn are SQLITE_E
1da54 4d 50 54 59 20 61 6e 64 20 53 51 4c 49 54 45 5f MPTY and SQLITE_
1da55 4f 4b 2e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f OK.. ** SQLITE_
1da56 45 4d 50 54 59 20 69 73 20 6f 6e 6c 79 20 72 65 EMPTY is only re
1da57 74 75 72 6e 65 64 20 77 68 65 6e 20 61 74 74 65 turned when atte
1da58 6d 70 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 mpting to open t
1da59 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 72 6f he table. ** ro
1da5a 6f 74 65 64 20 61 74 20 70 61 67 65 20 31 20 6f oted at page 1 o
1da5b 66 20 61 20 7a 65 72 6f 2d 62 79 74 65 20 64 61 f a zero-byte da
1da5c 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20 61 73 tabase. */. as
1da5d 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1da5e 5f 45 4d 50 54 59 20 7c 7c 20 72 63 3d 3d 53 51 _EMPTY || rc==SQ
1da5f 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 LITE_OK );. if(
1da60 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 rc==SQLITE_EMPT
1da61 59 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 70 43 Y ){. u.aw.pC
1da62 75 72 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b ur->pCursor = 0;
1da63 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1da64 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 _OK;. }.. /* S
1da65 65 74 20 74 68 65 20 56 64 62 65 43 75 72 73 6f et the VdbeCurso
1da66 72 2e 69 73 54 61 62 6c 65 20 61 6e 64 20 69 73 r.isTable and is
1da67 49 6e 64 65 78 20 76 61 72 69 61 62 6c 65 73 2e Index variables.
1da68 20 50 72 65 76 69 6f 75 73 20 76 65 72 73 69 6f Previous versio
1da69 6e 73 20 6f 66 0a 20 20 2a 2a 20 53 51 4c 69 74 ns of. ** SQLit
1da6a 65 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 e used to check
1da6b 69 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 if the root-page
1da6c 20 66 6c 61 67 73 20 77 65 72 65 20 73 61 6e 65 flags were sane
1da6d 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 0a 20 at this point.
1da6e 20 2a 2a 20 61 6e 64 20 72 65 70 6f 72 74 20 64 ** and report d
1da6f 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
1da70 6f 6e 20 69 66 20 74 68 65 79 20 77 65 72 65 20 on if they were
1da71 6e 6f 74 2c 20 62 75 74 20 74 68 69 73 20 63 68 not, but this ch
1da72 65 63 6b 20 68 61 73 0a 20 20 2a 2a 20 73 69 6e eck has. ** sin
1da73 63 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 ce moved into th
1da74 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 20 20 e btree layer.
1da75 2a 2f 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e */. u.aw.pCur->
1da76 69 73 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 isTable = pOp->p
1da77 34 74 79 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4type!=P4_KEYINF
1da78 4f 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e O;. u.aw.pCur->
1da79 69 73 49 6e 64 65 78 20 3d 20 21 75 2e 61 77 2e isIndex = !u.aw.
1da7a 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 pCur->isTable;.
1da7b 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1da7c 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 code: OpenEpheme
1da7d 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a ral P1 P2 * P4 *
1da7e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 .**.** Open a ne
1da7f 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61 w cursor P1 to a
1da80 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 transient table
1da81 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 ..** The cursor
1da82 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 is always opened
1da83 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e read/write even
1da84 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e if .** the main
1da85 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 61 database is rea
1da86 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 d-only. The tra
1da87 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 75 61 nsient or virtua
1da88 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65 l.** table is de
1da89 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 leted automatica
1da8a 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75 72 lly when the cur
1da8b 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a sor is closed..*
1da8c 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e *.** P2 is the n
1da8d 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
1da8e 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 in the virtual
1da8f 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 table..** The cu
1da90 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 rsor points to a
1da91 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 66 20 BTree table if
1da92 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 P4==0 and to a B
1da93 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 Tree index.** if
1da94 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 P4 is not 0. I
1da95 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c f P4 is not NULL
1da96 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 , it points to a
1da97 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
1da98 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 6e re.** that defin
1da99 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 es the format of
1da9a 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 6e 64 keys in the ind
1da9b 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f ex..**.** This o
1da9c 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 63 pcode was once c
1da9d 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 alled OpenTemp.
1da9e 20 42 75 74 20 74 68 61 74 20 63 72 65 61 74 65 But that create
1da9f 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 d.** confusion b
1daa0 65 63 61 75 73 65 20 74 68 65 20 74 65 72 6d 20 ecause the term
1daa1 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 "temp table", mi
1daa2 67 68 74 20 72 65 66 65 72 20 65 69 74 68 65 72 ght refer either
1daa3 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 61 .** to a TEMP ta
1daa4 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c 20 6c ble at the SQL l
1daa5 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 evel, or to a ta
1daa6 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a ble opened by.**
1daa7 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 this opcode. T
1daa8 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 hen this opcode
1daa9 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 was call OpenVir
1daaa 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 tual. But.** th
1daab 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e 66 75 at created confu
1daac 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 77 68 sion with the wh
1daad 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c ole virtual-tabl
1daae 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 e idea..*/.case
1daaf 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
1dab0 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1dab1 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1dab2 76 65 64 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f ved into u.ax */
1dab3 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1dab4 43 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f Cx;.#endif /* lo
1dab5 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1dab6 76 65 64 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f ved into u.ax */
1dab7 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
1dab8 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 0a int openFlags =.
1dab9 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 SQLITE_OPE
1daba 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20 N_READWRITE |.
1dabb 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f SQLITE_OPEN_
1dabc 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 53 CREATE |. S
1dabd 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
1dabe 53 49 56 45 20 7c 0a 20 20 20 20 20 20 53 51 4c SIVE |. SQL
1dabf 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
1dac0 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 53 NCLOSE |. S
1dac1 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 QLITE_OPEN_TRANS
1dac2 49 45 4e 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65 IENT_DB;.. asse
1dac3 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 rt( pOp->p1>=0 )
1dac4 3b 0a 20 20 75 2e 61 78 2e 70 43 78 20 3d 20 61 ;. u.ax.pCx = a
1dac5 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c llocateCursor(p,
1dac6 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 pOp->p1, pOp->p
1dac7 32 2c 20 2d 31 2c 20 31 29 3b 0a 20 20 69 66 28 2, -1, 1);. if(
1dac8 20 75 2e 61 78 2e 70 43 78 3d 3d 30 20 29 20 67 u.ax.pCx==0 ) g
1dac9 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e oto no_mem;. u.
1daca 61 78 2e 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 ax.pCx->nullRow
1dacb 3d 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 = 1;. rc = sqli
1dacc 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 te3BtreeFactory(
1dacd 64 62 2c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 db, 0, 1, SQLITE
1dace 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 _DEFAULT_TEMP_CA
1dacf 43 48 45 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c CHE_SIZE, openFl
1dad0 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ags,.
1dad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dad2 26 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 29 3b &u.ax.pCx->pBt);
1dad3 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1dad4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
1dad5 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 sqlite3BtreeBeg
1dad6 69 6e 54 72 61 6e 73 28 75 2e 61 78 2e 70 43 78 inTrans(u.ax.pCx
1dad7 2d 3e 70 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 ->pBt, 1);. }.
1dad8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1dad9 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 OK ){. /* If
1dada 61 20 74 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 a transient inde
1dadb 78 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 63 x is required, c
1dadc 72 65 61 74 65 20 69 74 20 62 79 20 63 61 6c 6c reate it by call
1dadd 69 6e 67 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 ing. ** sqlit
1dade 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 e3BtreeCreateTab
1dadf 6c 65 28 29 20 77 69 74 68 20 74 68 65 20 42 54 le() with the BT
1dae0 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61 REE_ZERODATA fla
1dae1 67 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 g before. **
1dae2 6f 70 65 6e 69 6e 67 20 69 74 2e 20 49 66 20 61 opening it. If a
1dae3 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 transient table
1dae4 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 6a 75 is required, ju
1dae5 73 74 20 75 73 65 20 74 68 65 0a 20 20 20 20 2a st use the. *
1dae6 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
1dae7 63 72 65 61 74 65 64 20 74 61 62 6c 65 20 77 69 created table wi
1dae8 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 31 20 28 th root-page 1 (
1dae9 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 29 an INTKEY table)
1daea 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1daeb 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 pOp->p4.pKeyInf
1daec 6f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 o ){. int p
1daed 67 6e 6f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 gno;. asser
1daee 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1daef 50 34 5f 4b 45 59 49 4e 46 4f 20 29 3b 0a 20 20 P4_KEYINFO );.
1daf0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1daf1 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 BtreeCreateTable
1daf2 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 (u.ax.pCx->pBt,
1daf3 26 70 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 &pgno, BTREE_ZER
1daf4 4f 44 41 54 41 29 3b 0a 20 20 20 20 20 20 69 66 ODATA);. if
1daf5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1daf6 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
1daf7 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f t( pgno==MASTER_
1daf8 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 ROOT+1 );.
1daf9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1dafa 72 65 65 43 75 72 73 6f 72 28 75 2e 61 78 2e 70 reeCursor(u.ax.p
1dafb 43 78 2d 3e 70 42 74 2c 20 70 67 6e 6f 2c 20 31 Cx->pBt, pgno, 1
1dafc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1dafd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dafe 20 20 28 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d (KeyInfo*)pOp-
1daff 3e 70 34 2e 7a 2c 20 75 2e 61 78 2e 70 43 78 2d >p4.z, u.ax.pCx-
1db00 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 >pCursor);.
1db01 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 u.ax.pCx->pKe
1db02 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e yInfo = pOp->p4.
1db03 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 pKeyInfo;.
1db04 20 20 75 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 u.ax.pCx->pKey
1db05 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 Info->enc = ENC(
1db06 70 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a p->db);. }.
1db07 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e u.ax.pCx->
1db08 69 73 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 isTable = 0;.
1db09 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 }else{. rc
1db0a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 = sqlite3BtreeC
1db0b 75 72 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e ursor(u.ax.pCx->
1db0c 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 pBt, MASTER_ROOT
1db0d 2c 20 31 2c 20 30 2c 20 75 2e 61 78 2e 70 43 78 , 1, 0, u.ax.pCx
1db0e 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 ->pCursor);.
1db0f 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 u.ax.pCx->isTa
1db10 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 ble = 1;. }.
1db11 20 7d 0a 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 }. u.ax.pCx->i
1db12 73 49 6e 64 65 78 20 3d 20 21 75 2e 61 78 2e 70 sIndex = !u.ax.p
1db13 43 78 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 Cx->isTable;. b
1db14 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1db15 64 65 3a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 de: OpenPseudo P
1db16 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1db17 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72 * Open a new cur
1db18 73 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 sor that points
1db19 74 6f 20 61 20 66 61 6b 65 20 74 61 62 6c 65 20 to a fake table
1db1a 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 that contains a
1db1b 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 single.** row of
1db1c 20 64 61 74 61 2e 20 20 54 68 65 20 63 6f 6e 74 data. The cont
1db1d 65 6e 74 20 6f 66 20 74 68 61 74 20 6f 6e 65 20 ent of that one
1db1e 72 6f 77 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 row in the conte
1db1f 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 nt of memory.**
1db20 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 6e register P2. In
1db21 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 63 75 other words, cu
1db22 72 73 6f 72 20 50 31 20 62 65 63 6f 6d 65 73 20 rsor P1 becomes
1db23 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 74 68 65 an alias for the
1db24 20 0a 2a 2a 20 4d 45 4d 5f 42 6c 6f 62 20 63 6f .** MEM_Blob co
1db25 6e 74 65 6e 74 20 63 6f 6e 74 61 69 6e 65 64 20 ntent contained
1db26 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a in register P2..
1db27 2a 2a 0a 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74 **.** A pseudo-t
1db28 61 62 6c 65 20 63 72 65 61 74 65 64 20 62 79 20 able created by
1db29 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 this opcode is u
1db2a 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 sed to hold the
1db2b 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 a single.** row
1db2c 6f 75 74 70 75 74 20 66 72 6f 6d 20 74 68 65 20 output from the
1db2d 73 6f 72 74 65 72 20 73 6f 20 74 68 61 74 20 74 sorter so that t
1db2e 68 65 20 72 6f 77 20 63 61 6e 20 62 65 20 64 65 he row can be de
1db2f 63 6f 6d 70 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a composed into.**
1db30 20 69 6e 64 69 76 69 64 75 61 6c 20 63 6f 6c 75 individual colu
1db31 6d 6e 73 20 75 73 69 6e 67 20 74 68 65 20 4f 50 mns using the OP
1db32 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 20 _Column opcode.
1db33 20 54 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f The OP_Column o
1db34 70 63 6f 64 65 0a 2a 2a 20 69 73 20 74 68 65 20 pcode.** is the
1db35 6f 6e 6c 79 20 63 75 72 73 6f 72 20 6f 70 63 6f only cursor opco
1db36 64 65 20 74 68 61 74 20 77 6f 72 6b 73 20 77 69 de that works wi
1db37 74 68 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c th a pseudo-tabl
1db38 65 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 e..**.** P3 is t
1db39 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 he number of fie
1db3a 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 lds in the recor
1db3b 64 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ds that will be
1db3c 73 74 6f 72 65 64 20 62 79 0a 2a 2a 20 74 68 65 stored by.** the
1db3d 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a pseudo-table..*
1db3e 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73 /.case OP_OpenPs
1db3f 65 75 64 6f 3a 20 7b 0a 23 69 66 20 30 20 20 2f eudo: {.#if 0 /
1db40 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1db41 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1db42 79 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f y */. VdbeCurso
1db43 72 20 2a 70 43 78 3b 0a 23 65 6e 64 69 66 20 2f r *pCx;.#endif /
1db44 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1db45 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1db46 79 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 y */.. assert(
1db47 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 pOp->p1>=0 );.
1db48 75 2e 61 79 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 u.ay.pCx = alloc
1db49 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 ateCursor(p, pOp
1db4a 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d ->p1, pOp->p3, -
1db4b 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 75 2e 61 1, 0);. if( u.a
1db4c 79 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 y.pCx==0 ) goto
1db4d 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 79 2e 70 no_mem;. u.ay.p
1db4e 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b Cx->nullRow = 1;
1db4f 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 70 73 65 . u.ay.pCx->pse
1db50 75 64 6f 54 61 62 6c 65 52 65 67 20 3d 20 70 4f udoTableReg = pO
1db51 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 79 2e 70 43 p->p2;. u.ay.pC
1db52 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a x->isTable = 1;.
1db53 20 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 49 6e u.ay.pCx->isIn
1db54 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b dex = 0;. break
1db55 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1db56 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a 20 2a Close P1 * * * *
1db57 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 .**.** Close a c
1db58 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 ursor previously
1db59 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e 20 20 opened as P1.
1db5a 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a 2a 20 If P1 is not.**
1db5b 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 currently open,
1db5c 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1db5d 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
1db5e 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a 20 7b case OP_Close: {
1db5f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1db60 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1db61 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1db62 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 sqlite3VdbeFree
1db63 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 Cursor(p, p->apC
1db64 73 72 5b 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 20 sr[pOp->p1]);.
1db65 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1db66 5d 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a ] = 0;. break;.
1db67 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 }../* Opcode: Se
1db68 65 6b 47 65 20 50 31 20 50 32 20 50 33 20 50 34 ekGe P1 P2 P3 P4
1db69 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 *.**.** If curs
1db6a 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 or P1 refers to
1db6b 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d an SQL table (B-
1db6c 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 Tree that uses i
1db6d 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a nteger keys), .*
1db6e 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 * use the value
1db6f 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 in register P3 a
1db70 73 20 74 68 65 20 6b 65 79 2e 20 20 49 66 20 63 s the key. If c
1db71 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 ursor P1 refers
1db72 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e .** to an SQL in
1db73 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 dex, then P3 is
1db74 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 the first in an
1db75 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 array of P4 regi
1db76 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 sters .** that a
1db77 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e re used as an un
1db78 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 packed index key
1db79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 . .**.** Reposit
1db7a 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f ion cursor P1 so
1db7b 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 that it points
1db7c 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 to the smallest
1db7d 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 entry that .**
1db7e 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1db7f 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
1db80 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 key value. If th
1db81 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 ere are no recor
1db82 64 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 ds .** greater t
1db83 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
1db84 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 the key and P2 i
1db85 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e s not zero, then
1db86 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a jump to P2..**.
1db87 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 ** See also: Fou
1db88 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 nd, NotFound, Di
1db89 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 stinct, SeekLt,
1db8a 53 65 65 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a SeekGt, SeekLe.*
1db8b 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 /./* Opcode: See
1db8c 6b 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 kGt P1 P2 P3 P4
1db8d 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f *.**.** If curso
1db8e 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 r P1 refers to a
1db8f 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 n SQL table (B-T
1db90 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e ree that uses in
1db91 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a teger keys), .**
1db92 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 use the value i
1db93 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 n register P3 as
1db94 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f a key. If curso
1db95 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 r P1 refers .**
1db96 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c to an SQL index,
1db97 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 then P3 is the
1db98 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 first in an arra
1db99 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 y of P4 register
1db9a 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 s .** that are u
1db9b 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b sed as an unpack
1db9c 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a ed index key. .*
1db9d 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 *.** Reposition
1db9e 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 cursor P1 so tha
1db9f 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 t it points to
1dba0 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 the smallest ent
1dba1 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 ry that .** is g
1dba2 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
1dba3 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 key value. If th
1dba4 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 ere are no recor
1dba5 64 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 ds greater than
1dba6 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20 .** the key and
1dba7 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 P2 is not zero,
1dba8 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e then jump to P2.
1dba9 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1dbaa 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 Found, NotFound
1dbab 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b , Distinct, Seek
1dbac 4c 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b Lt, SeekGe, Seek
1dbad 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a Le.*/./* Opcode:
1dbae 20 53 65 65 6b 4c 74 20 50 31 20 50 32 20 50 33 SeekLt P1 P2 P3
1dbaf 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 P4 * .**.** If
1dbb0 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 cursor P1 refers
1dbb1 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 to an SQL table
1dbb2 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 (B-Tree that us
1dbb3 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 es integer keys)
1dbb4 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 , .** use the va
1dbb5 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1dbb6 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 P3 as a key. If
1dbb7 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 cursor P1 refers
1dbb8 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 .** to an SQL i
1dbb9 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 ndex, then P3 is
1dbba 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e the first in an
1dbbb 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 array of P4 reg
1dbbc 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 isters .** that
1dbbd 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 are used as an u
1dbbe 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 npacked index ke
1dbbf 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 y. .**.** Reposi
1dbc0 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 tion cursor P1 s
1dbc1 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 o that it point
1dbc2 73 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 s to the largest
1dbc3 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 entry that .**
1dbc4 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 is less than the
1dbc5 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 key value. If t
1dbc6 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f here are no reco
1dbc7 72 64 73 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a rds less than .*
1dbc8 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 * the key and P2
1dbc9 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 is not zero, th
1dbca 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a en jump to P2..*
1dbcb 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 *.** See also: F
1dbcc 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 ound, NotFound,
1dbcd 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 Distinct, SeekGt
1dbce 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 , SeekGe, SeekLe
1dbcf 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .*/./* Opcode: S
1dbd0 65 65 6b 4c 65 20 50 31 20 50 32 20 50 33 20 50 eekLe P1 P2 P3 P
1dbd1 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 4 *.**.** If cur
1dbd2 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f sor P1 refers to
1dbd3 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 an SQL table (B
1dbd4 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 -Tree that uses
1dbd5 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a integer keys), .
1dbd6 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 ** use the value
1dbd7 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 in register P3
1dbd8 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 as a key. If cur
1dbd9 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a sor P1 refers .*
1dbda 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 * to an SQL inde
1dbdb 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 x, then P3 is th
1dbdc 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 e first in an ar
1dbdd 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 ray of P4 regist
1dbde 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 ers .** that are
1dbdf 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 used as an unpa
1dbe0 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 cked index key.
1dbe1 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f .**.** Repositio
1dbe2 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 n cursor P1 so t
1dbe3 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f hat it points to
1dbe4 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 the largest ent
1dbe5 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c ry that .** is l
1dbe6 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ess than or equa
1dbe7 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c l to the key val
1dbe8 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 ue. If there are
1dbe9 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 no records .**
1dbea 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 less than or equ
1dbeb 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e al to the key an
1dbec 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f d P2 is not zero
1dbed 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 , then jump to P
1dbee 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 2..**.** See als
1dbef 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 o: Found, NotFou
1dbf0 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 nd, Distinct, Se
1dbf1 65 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 ekGt, SeekGe, Se
1dbf2 65 6b 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ekLt.*/.case OP_
1dbf3 53 65 65 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 SeekLt:
1dbf4 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a /* jump, in3 */.
1dbf5 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 case OP_SeekLe:
1dbf6 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1dbf7 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f in3 */.case OP_
1dbf8 53 65 65 6b 47 65 3a 20 20 20 20 20 20 20 20 20 SeekGe:
1dbf9 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a /* jump, in3 */.
1dbfa 63 61 73 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 case OP_SeekGt:
1dbfb 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c { /* jump,
1dbfc 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f in3 */.#if 0 /
1dbfd 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1dbfe 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1dbff 7a 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 0a z */. int res;.
1dc00 20 20 69 6e 74 20 6f 63 3b 0a 20 20 56 64 62 65 int oc;. Vdbe
1dc01 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 55 6e Cursor *pC;. Un
1dc02 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a packedRecord r;.
1dc03 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 int nField;.
1dc04 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f i64 iKey; /
1dc05 2a 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61 * The rowid we a
1dc06 72 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f re to seek to */
1dc07 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1dc08 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1dc09 20 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0a 0a 20 into u.az */..
1dc0a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1dc0b 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 >=0 && pOp->p1<p
1dc0c 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1dc0d 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d ssert( pOp->p2!=
1dc0e 30 20 29 3b 0a 20 20 75 2e 61 7a 2e 70 43 20 3d 0 );. u.az.pC =
1dc0f 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1dc10 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 1];. assert( u.
1dc11 61 7a 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 az.pC!=0 );. as
1dc12 73 65 72 74 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 sert( u.az.pC->p
1dc13 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 seudoTableReg==0
1dc14 20 29 3b 0a 20 20 69 66 28 20 75 2e 61 7a 2e 70 );. if( u.az.p
1dc15 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b C->pCursor!=0 ){
1dc16 0a 20 20 20 20 75 2e 61 7a 2e 6f 63 20 3d 20 70 . u.az.oc = p
1dc17 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 Op->opcode;.
1dc18 75 2e 61 7a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 u.az.pC->nullRow
1dc19 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e = 0;. if( u.
1dc1a 61 7a 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 az.pC->isTable )
1dc1b 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 {. /* The i
1dc1c 6e 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 nput value in P3
1dc1d 20 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 might be of any
1dc1e 20 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 type: integer,
1dc1f 72 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 real, string,.
1dc20 20 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 ** blob, or
1dc21 4e 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 NULL. But it ne
1dc22 65 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 eds to be an int
1dc23 65 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 eger before we c
1dc24 61 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 an do. ** t
1dc25 68 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 he seek, so cove
1dc26 72 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 rt it. */.
1dc27 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 applyNumericAffi
1dc28 6e 69 74 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 nity(pIn3);.
1dc29 20 20 75 2e 61 7a 2e 69 4b 65 79 20 3d 20 73 71 u.az.iKey = sq
1dc2a 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1dc2b 65 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 75 e(pIn3);. u
1dc2c 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .az.pC->rowidIsV
1dc2d 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20 20 alid = 0;..
1dc2e 20 2f 2a 20 49 66 20 74 68 65 20 50 33 20 76 61 /* If the P3 va
1dc2f 6c 75 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 lue could not be
1dc30 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 converted into
1dc31 61 6e 20 69 6e 74 65 67 65 72 20 77 69 74 68 6f an integer witho
1dc32 75 74 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 73 73 ut. ** loss
1dc33 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c of information,
1dc34 20 74 68 65 6e 20 73 70 65 63 69 61 6c 20 70 72 then special pr
1dc35 6f 63 65 73 73 69 6e 67 20 69 73 20 72 65 71 75 ocessing is requ
1dc36 69 72 65 64 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 ired... */.
1dc37 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 if( (pIn3->flag
1dc38 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 s & MEM_Int)==0
1dc39 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 ){. if( (
1dc3a 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn3->flags & ME
1dc3b 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 M_Real)==0 ){.
1dc3c 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
1dc3d 65 20 50 33 20 76 61 6c 75 65 20 63 61 6e 6e 6f e P3 value canno
1dc3e 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 t be converted i
1dc3f 6e 74 6f 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 nto any kind of
1dc40 61 20 6e 75 6d 62 65 72 2c 0a 20 20 20 20 20 20 a number,.
1dc41 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 ** then the
1dc42 73 65 65 6b 20 69 73 20 6e 6f 74 20 70 6f 73 73 seek is not poss
1dc43 69 62 6c 65 2c 20 73 6f 20 6a 75 6d 70 20 74 6f ible, so jump to
1dc44 20 50 32 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 P2 */.
1dc45 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1dc46 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 1;. bre
1dc47 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
1dc48 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 /* If we r
1dc49 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c each this point,
1dc4a 20 74 68 65 6e 20 74 68 65 20 50 33 20 76 61 6c then the P3 val
1dc4b 75 65 20 6d 75 73 74 20 62 65 20 61 20 66 6c 6f ue must be a flo
1dc4c 61 74 69 6e 67 0a 20 20 20 20 20 20 20 20 2a 2a ating. **
1dc4d 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 20 2a point number. *
1dc4e 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 /. assert
1dc4f 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 ( (pIn3->flags &
1dc50 20 4d 45 4d 5f 52 65 61 6c 29 21 3d 30 20 29 3b MEM_Real)!=0 );
1dc51 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e .. if( u.
1dc52 61 7a 2e 69 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 az.iKey==SMALLES
1dc53 54 5f 49 4e 54 36 34 20 26 26 20 28 70 49 6e 33 T_INT64 && (pIn3
1dc54 2d 3e 72 3c 28 64 6f 75 62 6c 65 29 75 2e 61 7a ->r<(double)u.az
1dc55 2e 69 4b 65 79 20 7c 7c 20 70 49 6e 33 2d 3e 72 .iKey || pIn3->r
1dc56 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 >0) ){.
1dc57 20 2f 2a 20 54 68 65 20 50 33 20 76 61 6c 75 65 /* The P3 value
1dc58 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 69 6e is too large in
1dc59 20 6d 61 67 6e 69 74 75 64 65 20 74 6f 20 62 65 magnitude to be
1dc5a 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 6e expressed as an
1dc5b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e . ** in
1dc5c 74 65 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 teger. */.
1dc5d 20 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d 20 31 u.az.res = 1
1dc5e 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1dc5f 70 49 6e 33 2d 3e 72 3c 30 20 29 7b 0a 20 20 20 pIn3->r<0 ){.
1dc60 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 if( u.a
1dc61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 z.oc==OP_SeekGt
1dc62 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 || u.az.oc==OP_S
1dc63 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 20 eekGe ){.
1dc64 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1dc65 74 65 33 42 74 72 65 65 46 69 72 73 74 28 75 2e te3BtreeFirst(u.
1dc66 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 az.pC->pCursor,
1dc67 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 &u.az.res);.
1dc68 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
1dc69 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
1dc6a 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1dc6b 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 error;.
1dc6c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
1dc6d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
1dc6e 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f if( u.az.oc==O
1dc6f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a P_SeekLt || u.az
1dc70 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 .oc==OP_SeekLe )
1dc71 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1dc72 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1dc73 65 4c 61 73 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 eLast(u.az.pC->p
1dc74 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 Cursor, &u.az.re
1dc75 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 s);.
1dc76 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1dc77 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 _OK ) goto abort
1dc78 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1dc79 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1dc7a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1dc7b 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 20 if( u.az.res
1dc7c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
1dc7d 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1dc7e 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1dc7f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1dc80 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1dc81 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1dc82 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f Lt || u.az.oc==O
1dc83 50 5f 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 P_SeekGe ){.
1dc84 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 /* Use the
1dc85 20 63 65 69 6c 69 6e 67 28 29 20 66 75 6e 63 74 ceiling() funct
1dc86 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 ion to convert r
1dc87 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 eal->int */.
1dc88 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e if( pIn3->
1dc89 72 20 3e 20 28 64 6f 75 62 6c 65 29 75 2e 61 7a r > (double)u.az
1dc8a 2e 69 4b 65 79 20 29 20 75 2e 61 7a 2e 69 4b 65 .iKey ) u.az.iKe
1dc8b 79 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c y++;. }el
1dc8c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a se{. /*
1dc8d 20 55 73 65 20 74 68 65 20 66 6c 6f 6f 72 28 29 Use the floor()
1dc8e 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e function to con
1dc8f 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a vert real->int *
1dc90 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 /. asse
1dc91 72 74 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f rt( u.az.oc==OP_
1dc92 53 65 65 6b 4c 65 20 7c 7c 20 75 2e 61 7a 2e 6f SeekLe || u.az.o
1dc93 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a c==OP_SeekGt );.
1dc94 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 if( pI
1dc95 6e 33 2d 3e 72 20 3c 20 28 64 6f 75 62 6c 65 29 n3->r < (double)
1dc96 75 2e 61 7a 2e 69 4b 65 79 20 29 20 75 2e 61 7a u.az.iKey ) u.az
1dc97 2e 69 4b 65 79 2d 2d 3b 0a 20 20 20 20 20 20 20 .iKey--;.
1dc98 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
1dc99 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1dc9a 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
1dc9b 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f (u.az.pC->pCurso
1dc9c 72 2c 20 30 2c 20 28 75 36 34 29 75 2e 61 7a 2e r, 0, (u64)u.az.
1dc9d 69 4b 65 79 2c 20 30 2c 20 26 75 2e 61 7a 2e 72 iKey, 0, &u.az.r
1dc9e 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 es);. if( r
1dc9f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1dca0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f goto abo
1dca1 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1dca2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1dca3 66 28 20 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 29 f( u.az.res==0 )
1dca4 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 {. u.az.p
1dca5 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1dca6 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 = 1;. u.a
1dca7 7a 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 z.pC->lastRowid
1dca8 3d 20 75 2e 61 7a 2e 69 4b 65 79 3b 0a 20 20 20 = u.az.iKey;.
1dca9 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1dcaa 20 20 20 20 20 20 75 2e 61 7a 2e 6e 46 69 65 6c u.az.nFiel
1dcab 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 d = pOp->p4.i;.
1dcac 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1dcad 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 ->p4type==P4_INT
1dcae 33 32 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 32 );. asse
1dcaf 72 74 28 20 75 2e 61 7a 2e 6e 46 69 65 6c 64 3e rt( u.az.nField>
1dcb0 30 20 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 0 );. u.az.
1dcb1 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 61 r.pKeyInfo = u.a
1dcb2 7a 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a z.pC->pKeyInfo;.
1dcb3 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 6e 46 69 u.az.r.nFi
1dcb4 65 6c 64 20 3d 20 28 75 31 36 29 75 2e 61 7a 2e eld = (u16)u.az.
1dcb5 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 69 66 nField;. if
1dcb6 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 ( u.az.oc==OP_Se
1dcb7 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d ekGt || u.az.oc=
1dcb8 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 20 =OP_SeekLe ){.
1dcb9 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 66 6c 61 u.az.r.fla
1dcba 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e gs = UNPACKED_IN
1dcbb 43 52 4b 45 59 3b 0a 20 20 20 20 20 20 7d 65 6c CRKEY;. }el
1dcbc 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a se{. u.az
1dcbd 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 .r.flags = 0;.
1dcbe 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 7a }. u.az
1dcbf 2e 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d .r.aMem = &p->aM
1dcc0 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 em[pOp->p3];.
1dcc1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1dcc2 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
1dcc3 65 64 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 ed(u.az.pC->pCur
1dcc4 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 2c 20 30 2c sor, &u.az.r, 0,
1dcc5 20 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 0, &u.az.res);.
1dcc6 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1dcc7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1dcc8 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1dcc9 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1dcca 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 }. u.az.p
1dccb 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1dccc 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 = 0;. }. u
1dccd 2e 61 7a 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 .az.pC->deferred
1dcce 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 Moveto = 0;.
1dccf 75 2e 61 7a 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.az.pC->cacheSt
1dcd0 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1dcd1 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 LE;.#ifdef SQLIT
1dcd2 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 E_TEST. sqlit
1dcd3 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b e3_search_count+
1dcd4 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 +;.#endif. if
1dcd5 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 ( u.az.oc==OP_Se
1dcd6 65 6b 47 65 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d ekGe || u.az.oc=
1dcd7 3d 4f 50 5f 53 65 65 6b 47 74 20 29 7b 0a 20 20 =OP_SeekGt ){.
1dcd8 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 if( u.az.res
1dcd9 3c 30 20 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d <0 || (u.az.res=
1dcda 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f =0 && u.az.oc==O
1dcdb 50 5f 53 65 65 6b 47 74 29 20 29 7b 0a 20 20 20 P_SeekGt) ){.
1dcdc 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1dcdd 33 42 74 72 65 65 4e 65 78 74 28 75 2e 61 7a 2e 3BtreeNext(u.az.
1dcde 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e pC->pCursor, &u.
1dcdf 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 az.res);.
1dce0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1dce1 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f OK ) goto abort_
1dce2 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1dce3 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 u.az.pC->r
1dce4 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b owidIsValid = 0;
1dce5 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1dce6 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d u.az.res =
1dce7 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
1dce8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
1dce9 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 ert( u.az.oc==OP
1dcea 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e _SeekLt || u.az.
1dceb 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b oc==OP_SeekLe );
1dcec 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e . if( u.az.
1dced 72 65 73 3e 30 20 7c 7c 20 28 75 2e 61 7a 2e 72 res>0 || (u.az.r
1dcee 65 73 3d 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63 es==0 && u.az.oc
1dcef 3d 3d 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a ==OP_SeekLt) ){.
1dcf0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1dcf1 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 ite3BtreePreviou
1dcf2 73 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 s(u.az.pC->pCurs
1dcf3 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a or, &u.az.res);.
1dcf4 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1dcf5 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
1dcf6 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1dcf7 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 ror;. u.a
1dcf8 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c z.pC->rowidIsVal
1dcf9 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 id = 0;. }e
1dcfa 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
1dcfb 75 2e 61 7a 2e 72 65 73 20 6d 69 67 68 74 20 62 u.az.res might b
1dcfc 65 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 75 e negative becau
1dcfd 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 se the table is
1dcfe 65 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f empty. Check to
1dcff 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20 . ** see
1dd00 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 if this is the c
1dd01 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a ase.. */.
1dd02 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 u.az.res
1dd03 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 = sqlite3BtreeE
1dd04 6f 66 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 of(u.az.pC->pCur
1dd05 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 sor);. }.
1dd06 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1dd07 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 pOp->p2>0 );.
1dd08 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 20 29 7b if( u.az.res ){
1dd09 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
1dd0a 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 >p2 - 1;. }.
1dd0b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
1dd0c 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e his happens when
1dd0d 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f attempting to o
1dd0e 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f pen the sqlite3_
1dd0f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 20 master table.
1dd10 20 2a 2a 20 66 6f 72 20 72 65 61 64 20 61 63 63 ** for read acc
1dd11 65 73 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ess returns SQLI
1dd12 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20 74 68 69 TE_EMPTY. In thi
1dd13 73 20 63 61 73 65 20 61 6c 77 61 79 73 0a 20 20 s case always.
1dd14 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 ** take the ju
1dd15 6d 70 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 mp (since there
1dd16 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 69 are no records i
1dd17 6e 20 74 68 65 20 74 61 62 6c 65 29 2e 0a 20 20 n the table)..
1dd18 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f */. pc = pO
1dd19 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 p->p2 - 1;. }.
1dd1a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1dd1b 63 6f 64 65 3a 20 53 65 65 6b 20 50 31 20 50 32 code: Seek P1 P2
1dd1c 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 * * *.**.** P1
1dd1d 69 73 20 61 6e 20 6f 70 65 6e 20 74 61 62 6c 65 is an open table
1dd1e 20 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69 cursor and P2 i
1dd1f 73 20 61 20 72 6f 77 69 64 20 69 6e 74 65 67 65 s a rowid intege
1dd20 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a 2a 20 66 r. Arrange.** f
1dd21 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65 20 73 6f or P1 to move so
1dd22 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1dd23 74 6f 20 74 68 65 20 72 6f 77 69 64 20 67 69 76 to the rowid giv
1dd24 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 en by P2..**.**
1dd25 54 68 69 73 20 69 73 20 61 63 74 75 61 6c 6c 79 This is actually
1dd26 20 61 20 64 65 66 65 72 72 65 64 20 73 65 65 6b a deferred seek
1dd27 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63 74 75 61 . Nothing actua
1dd28 6c 6c 79 20 68 61 70 70 65 6e 73 20 75 6e 74 69 lly happens unti
1dd29 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 l.** the cursor
1dd2a 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 is used to read
1dd2b 61 20 72 65 63 6f 72 64 2e 20 20 54 68 61 74 20 a record. That
1dd2c 77 61 79 2c 20 69 66 20 6e 6f 20 72 65 61 64 73 way, if no reads
1dd2d 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f 20 75 6e .** occur, no un
1dd2e 6e 65 63 65 73 73 61 72 79 20 49 2f 4f 20 68 61 necessary I/O ha
1dd2f 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f ppens..*/.case O
1dd30 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 2f 2a 20 P_Seek: { /*
1dd31 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a in2 */.#if 0 /*
1dd32 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1dd33 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 61 moved into u.ba
1dd34 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1dd35 20 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 *pC;.#endif /*
1dd36 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1dd37 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 61 20 moved into u.ba
1dd38 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f */.. assert( pO
1dd39 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1dd3a 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1dd3b 3b 0a 20 20 75 2e 62 61 2e 70 43 20 3d 20 70 2d ;. u.ba.pC = p-
1dd3c 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1dd3d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 61 2e . assert( u.ba.
1dd3e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 pC!=0 );. if( A
1dd3f 4c 57 41 59 53 28 75 2e 62 61 2e 70 43 2d 3e 70 LWAYS(u.ba.pC->p
1dd40 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20 Cursor!=0) ){.
1dd41 20 20 61 73 73 65 72 74 28 20 75 2e 62 61 2e 70 assert( u.ba.p
1dd42 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1dd43 20 20 75 2e 62 61 2e 70 43 2d 3e 6e 75 6c 6c 52 u.ba.pC->nullR
1dd44 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 61 ow = 0;. u.ba
1dd45 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 .pC->movetoTarge
1dd46 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 t = sqlite3VdbeI
1dd47 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 ntValue(pIn2);.
1dd48 20 20 20 75 2e 62 61 2e 70 43 2d 3e 72 6f 77 69 u.ba.pC->rowi
1dd49 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1dd4a 20 20 75 2e 62 61 2e 70 43 2d 3e 64 65 66 65 72 u.ba.pC->defer
1dd4b 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31 3b 0a 20 redMoveto = 1;.
1dd4c 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 20 20 }. break;.}.
1dd4d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75 ../* Opcode: Fou
1dd4e 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a nd P1 P2 P3 * *.
1dd4f 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 **.** Register P
1dd50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 3 holds a blob c
1dd51 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61 onstructed by Ma
1dd52 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73 keRecord. P1 is
1dd53 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 an index..** If
1dd54 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d an entry that m
1dd55 61 74 63 68 65 73 20 74 68 65 20 76 61 6c 75 65 atches the value
1dd56 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 33 20 in register p3
1dd57 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 65 exists in P1 the
1dd58 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e n.** jump to P2.
1dd59 20 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 If the P3 valu
1dd5a 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 e does not match
1dd5b 20 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31 any entry in P1
1dd5c 0a 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 .** then fall th
1dd5d 72 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73 ru. The P1 curs
1dd5e 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 or is left point
1dd5f 69 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 ing at the match
1dd60 69 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20 ing entry.** if
1dd61 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a it exists..**.**
1dd62 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1dd63 6e 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 n is used to imp
1dd64 6c 65 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70 lement the IN op
1dd65 65 72 61 74 6f 72 20 77 68 65 72 65 20 74 68 65 erator where the
1dd66 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 .** left-hand si
1dd67 64 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 de is a SELECT s
1dd68 74 61 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61 tatement. P1 ma
1dd69 79 20 62 65 20 61 20 74 72 75 65 20 69 6e 64 65 y be a true inde
1dd6a 78 2c 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20 x, or it.** may
1dd6b 62 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69 be a temporary i
1dd6c 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 ndex that holds
1dd6d 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 the results of t
1dd6e 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 he SELECT.** sta
1dd6f 74 65 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69 tement. This i
1dd70 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c nstruction is al
1dd71 73 6f 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 so used to imple
1dd72 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54 ment the.** DIST
1dd73 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20 INCT keyword in
1dd74 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
1dd75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e s..**.** This in
1dd76 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 struction checks
1dd77 20 69 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e if index P1 con
1dd78 74 61 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66 tains a record f
1dd79 6f 72 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 or which .** the
1dd7a 20 66 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69 first N seriali
1dd7b 7a 65 64 20 76 61 6c 75 65 73 20 65 78 61 63 74 zed values exact
1dd7c 6c 79 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73 ly match the N s
1dd7d 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 erialized values
1dd7e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72 .** in the recor
1dd7f 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
1dd80 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 , where N is the
1dd81 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
1dd82 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68 values in.** th
1dd83 65 20 50 33 20 72 65 63 6f 72 64 20 28 74 68 65 e P3 record (the
1dd84 20 50 33 20 72 65 63 6f 72 64 20 69 73 20 61 20 P3 record is a
1dd85 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 50 31 prefix of the P1
1dd86 20 72 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a record). .**.**
1dd87 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f See also: NotFo
1dd88 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e und, IsUnique, N
1dd89 6f 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f otExists.*/./* O
1dd8a 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 pcode: NotFound
1dd8b 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1dd8c 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 68 ** Register P3 h
1dd8d 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 olds a blob cons
1dd8e 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 tructed by MakeR
1dd8f 65 63 6f 72 64 2e 20 20 50 31 20 69 73 0a 2a 2a ecord. P1 is.**
1dd90 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 6e an index. If n
1dd91 6f 20 65 6e 74 72 79 20 65 78 69 73 74 73 20 69 o entry exists i
1dd92 6e 20 50 31 20 74 68 61 74 20 6d 61 74 63 68 65 n P1 that matche
1dd93 73 20 74 68 65 20 62 6c 6f 62 20 74 68 65 6e 20 s the blob then
1dd94 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50 32 2e 20 20 jump.** to P2.
1dd95 49 66 20 61 6e 20 65 6e 74 72 79 20 64 6f 65 73 If an entry does
1dd96 20 65 78 69 73 74 69 6e 67 2c 20 66 61 6c 6c 20 existing, fall
1dd97 74 68 72 6f 75 67 68 2e 20 20 54 68 65 20 63 75 through. The cu
1dd98 72 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20 rsor is left.**
1dd99 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
1dd9a 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 entry that match
1dd9b 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c es..**.** See al
1dd9c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 so: Found, NotEx
1dd9d 69 73 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a ists, IsUnique.*
1dd9e 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75 /.case OP_NotFou
1dd9f 6e 64 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d nd: /* jum
1dda0 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f p, in3 */.case O
1dda1 50 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20 P_Found: {
1dda2 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1dda3 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1dda4 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1dda5 64 20 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a 20 d into u.bb */.
1dda6 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73 int alreadyExis
1dda7 74 73 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 ts;. VdbeCursor
1dda8 20 2a 70 43 3b 0a 20 20 69 6e 74 20 72 65 73 3b *pC;. int res;
1dda9 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
1ddaa 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 63 68 d *pIdxKey;. ch
1ddab 61 72 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e ar aTempRec[ROUN
1ddac 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b D8(sizeof(Unpack
1ddad 65 64 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a edRecord)) + siz
1ddae 65 6f 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b eof(Mem)*3 + 7];
1ddaf 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1ddb0 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ddb1 20 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a 0a 20 into u.bb */..
1ddb2 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 u.bb.alreadyExi
1ddb3 73 74 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 sts = 0;. asser
1ddb4 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1ddb5 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1ddb6 73 6f 72 20 29 3b 0a 20 20 75 2e 62 62 2e 70 43 sor );. u.bb.pC
1ddb7 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1ddb8 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1ddb9 75 2e 62 62 2e 70 43 21 3d 30 20 29 3b 0a 20 20 u.bb.pC!=0 );.
1ddba 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 62 2e if( ALWAYS(u.bb.
1ddbb 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 pC->pCursor!=0)
1ddbc 29 7b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){.. assert(
1ddbd 75 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bb.pC->isTable
1ddbe 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
1ddbf 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 t( pIn3->flags &
1ddc0 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 MEM_Blob );.
1ddc1 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 ExpandBlob(pIn3
1ddc2 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70 49 64 78 );. u.bb.pIdx
1ddc3 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 Key = sqlite3Vdb
1ddc4 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 75 2e eRecordUnpack(u.
1ddc5 62 62 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c bb.pC->pKeyInfo,
1ddc6 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e pIn3->n, pIn3->
1ddc7 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 z,.
1ddc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ddc9 20 20 20 20 20 20 20 20 20 75 2e 62 62 2e 61 54 u.bb.aT
1ddca 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 75 empRec, sizeof(u
1ddcb 2e 62 62 2e 61 54 65 6d 70 52 65 63 29 29 3b 0a .bb.aTempRec));.
1ddcc 20 20 20 20 69 66 28 20 75 2e 62 62 2e 70 49 64 if( u.bb.pId
1ddcd 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 xKey==0 ){.
1ddce 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1ddcf 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d }. if( pOp-
1ddd0 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e >opcode==OP_Foun
1ddd1 64 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 62 2e d ){. u.bb.
1ddd2 70 49 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c pIdxKey->flags |
1ddd3 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 = UNPACKED_PREFI
1ddd4 58 5f 4d 41 54 43 48 3b 0a 20 20 20 20 7d 0a 20 X_MATCH;. }.
1ddd5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1ddd6 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
1ddd7 65 64 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72 ed(u.bb.pC->pCur
1ddd8 73 6f 72 2c 20 75 2e 62 62 2e 70 49 64 78 4b 65 sor, u.bb.pIdxKe
1ddd9 79 2c 20 30 2c 20 30 2c 20 26 75 2e 62 62 2e 72 y, 0, 0, &u.bb.r
1ddda 65 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 es);. sqlite3
1dddb 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b VdbeDeleteUnpack
1dddc 65 64 52 65 63 6f 72 64 28 75 2e 62 62 2e 70 49 edRecord(u.bb.pI
1dddd 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 dxKey);. if(
1ddde 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1dddf 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1dde0 20 20 7d 0a 20 20 20 20 75 2e 62 62 2e 61 6c 72 }. u.bb.alr
1dde1 65 61 64 79 45 78 69 73 74 73 20 3d 20 28 75 2e eadyExists = (u.
1dde2 62 62 2e 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20 bb.res==0);.
1dde3 75 2e 62 62 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bb.pC->deferre
1dde4 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 dMoveto = 0;.
1dde5 20 75 2e 62 62 2e 70 43 2d 3e 63 61 63 68 65 53 u.bb.pC->cacheS
1dde6 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1dde7 41 4c 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ALE;. }. if( p
1dde8 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 Op->opcode==OP_F
1dde9 6f 75 6e 64 20 29 7b 0a 20 20 20 20 69 66 28 20 ound ){. if(
1ddea 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 u.bb.alreadyExis
1ddeb 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 ts ) pc = pOp->p
1ddec 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 2 - 1;. }else{.
1dded 20 20 20 20 69 66 28 20 21 75 2e 62 62 2e 61 6c if( !u.bb.al
1ddee 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70 63 readyExists ) pc
1ddef 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1ddf0 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1ddf1 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 6e 69 /* Opcode: IsUni
1ddf2 71 75 65 20 50 31 20 50 32 20 50 33 20 50 34 20 que P1 P2 P3 P4
1ddf3 2a 0a 2a 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 50 *.**.** Cursor P
1ddf4 31 20 69 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 1 is open on an
1ddf5 69 6e 64 65 78 2e 20 20 53 6f 20 69 74 20 68 61 index. So it ha
1ddf6 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 69 74 s no data and it
1ddf7 73 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 20 0a s key consists .
1ddf8 2a 2a 20 6f 66 20 61 20 72 65 63 6f 72 64 20 67 ** of a record g
1ddf9 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50 5f 4d enerated by OP_M
1ddfa 61 6b 65 52 65 63 6f 72 64 20 77 68 65 72 65 20 akeRecord where
1ddfb 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69 the last field i
1ddfc 73 20 74 68 65 20 0a 2a 2a 20 72 6f 77 69 64 20 s the .** rowid
1ddfd 6f 66 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 of the entry tha
1ddfe 74 20 74 68 65 20 69 6e 64 65 78 20 72 65 66 65 t the index refe
1ddff 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rs to..**.** The
1de00 20 50 33 20 72 65 67 69 73 74 65 72 20 63 6f 6e P3 register con
1de01 74 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 tains an integer
1de02 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 record number.
1de03 43 61 6c 6c 20 74 68 69 73 20 72 65 63 6f 72 64 Call this record
1de04 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e 20 52 .** number R. R
1de05 65 67 69 73 74 65 72 20 50 34 20 69 73 20 74 68 egister P4 is th
1de06 65 20 66 69 72 73 74 20 69 6e 20 61 20 73 65 74 e first in a set
1de07 20 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f 75 73 of N contiguous
1de08 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 74 68 registers.** th
1de09 61 74 20 6d 61 6b 65 20 75 70 20 61 6e 20 75 6e at make up an un
1de0a 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 packed index key
1de0b 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 that can be use
1de0c 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 50 31 d with cursor P1
1de0d 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f ..** The value o
1de0e 66 20 4e 20 63 61 6e 20 62 65 20 69 6e 66 65 72 f N can be infer
1de0f 72 65 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72 red from the cur
1de10 73 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65 73 20 sor. N includes
1de11 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76 61 6c the rowid.** val
1de12 75 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 ue appended to t
1de13 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e he end of the in
1de14 64 65 78 20 72 65 63 6f 72 64 2e 20 54 68 69 73 dex record. This
1de15 20 72 6f 77 69 64 20 76 61 6c 75 65 20 6d 61 79 rowid value may
1de16 0a 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 .** or may not b
1de17 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 52 2e e the same as R.
1de18 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 .**.** If any of
1de19 20 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73 the N registers
1de1a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
1de1b 72 65 67 69 73 74 65 72 20 50 34 20 63 6f 6e 74 register P4 cont
1de1c 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 ains a NULL.** v
1de1d 61 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 alue, jump immed
1de1e 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a iately to P2..**
1de1f 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 .** Otherwise, t
1de20 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1de21 63 68 65 63 6b 73 20 69 66 20 63 75 72 73 6f 72 checks if cursor
1de22 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 P1 contains an
1de23 65 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65 20 74 entry.** where t
1de24 68 65 20 66 69 72 73 74 20 28 4e 2d 31 29 20 66 he first (N-1) f
1de25 69 65 6c 64 73 20 6d 61 74 63 68 20 62 75 74 20 ields match but
1de26 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20 the rowid value
1de27 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 at the end.** of
1de28 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 the index entry
1de29 20 69 73 20 6e 6f 74 20 52 2e 20 49 66 20 74 68 is not R. If th
1de2a 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 65 ere is no such e
1de2b 6e 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 ntry, control ju
1de2c 6d 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74 72 75 mps.** to instru
1de2d 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77 ction P2. Otherw
1de2e 69 73 65 2c 20 74 68 65 20 72 6f 77 69 64 20 6f ise, the rowid o
1de2f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e f the conflictin
1de30 67 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79 g index.** entry
1de31 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 72 65 is copied to re
1de32 67 69 73 74 65 72 20 50 33 20 61 6e 64 20 63 6f gister P3 and co
1de33 6e 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68 72 6f ntrol falls thro
1de34 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 0a ugh to the next.
1de35 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ** instruction..
1de36 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1de37 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 NotFound, NotExi
1de38 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 sts, Found.*/.ca
1de39 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20 se OP_IsUnique:
1de3a 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1de3b 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 , in3 */.#if 0
1de3c 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1de3d 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1de3e 62 63 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a bc */. u16 ii;.
1de3f 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1de40 78 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 x;. BtCursor *p
1de41 43 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65 Crsr;. u16 nFie
1de42 6c 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b ld;. Mem *aMem;
1de43 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 . UnpackedRecor
1de44 64 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 d r;
1de45 20 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 /* B-Tree
1de46 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 index search key
1de47 20 2a 2f 0a 20 20 69 36 34 20 52 3b 20 20 20 20 */. i64 R;
1de48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1de49 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 /* Rowi
1de4a 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 d stored in regi
1de4b 73 74 65 72 20 50 33 20 2a 2f 0a 23 65 6e 64 69 ster P3 */.#endi
1de4c 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1de4d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1de4e 75 2e 62 63 20 2a 2f 0a 0a 20 20 75 2e 62 63 2e u.bc */.. u.bc.
1de4f 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b aMem = &p->aMem[
1de50 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 20 20 2f 2a pOp->p4.i];. /*
1de51 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 Assert that the
1de52 20 76 61 6c 75 65 73 20 6f 66 20 70 61 72 61 6d values of param
1de53 65 74 65 72 73 20 50 31 20 61 6e 64 20 50 34 20 eters P1 and P4
1de54 61 72 65 20 69 6e 20 72 61 6e 67 65 2e 20 2a 2f are in range. */
1de55 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1de56 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 p4type==P4_INT32
1de57 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1de58 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 4f 70 p->p4.i>0 && pOp
1de59 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d 20 ->p4.i<=p->nMem
1de5a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1de5b 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1de5c 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1de5d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 .. /* Find the
1de5e 69 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a 2f index cursor. */
1de5f 0a 20 20 75 2e 62 63 2e 70 43 78 20 3d 20 70 2d . u.bc.pCx = p-
1de60 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1de61 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 63 2e . assert( u.bc.
1de62 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 pCx->deferredMov
1de63 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 63 eto==0 );. u.bc
1de64 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c 74 .pCx->seekResult
1de65 20 3d 20 30 3b 0a 20 20 75 2e 62 63 2e 70 43 78 = 0;. u.bc.pCx
1de66 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1de67 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 75 CACHE_STALE;. u
1de68 2e 62 63 2e 70 43 72 73 72 20 3d 20 75 2e 62 63 .bc.pCrsr = u.bc
1de69 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 0a .pCx->pCursor;..
1de6a 20 20 2f 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 /* If any of t
1de6b 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55 he values are NU
1de6c 4c 4c 2c 20 74 61 6b 65 20 74 68 65 20 6a 75 6d LL, take the jum
1de6d 70 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e 6e 46 69 p. */. u.bc.nFi
1de6e 65 6c 64 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e eld = u.bc.pCx->
1de6f 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1de70 3b 0a 20 20 66 6f 72 28 75 2e 62 63 2e 69 69 3d ;. for(u.bc.ii=
1de71 30 3b 20 75 2e 62 63 2e 69 69 3c 75 2e 62 63 2e 0; u.bc.ii<u.bc.
1de72 6e 46 69 65 6c 64 3b 20 75 2e 62 63 2e 69 69 2b nField; u.bc.ii+
1de73 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 63 +){. if( u.bc
1de74 2e 61 4d 65 6d 5b 75 2e 62 63 2e 69 69 5d 2e 66 .aMem[u.bc.ii].f
1de75 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1de76 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f ){. pc = pO
1de77 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 p->p2 - 1;.
1de78 20 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 30 3b u.bc.pCrsr = 0;
1de79 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1de7a 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
1de7b 28 20 28 75 2e 62 63 2e 61 4d 65 6d 5b 75 2e 62 ( (u.bc.aMem[u.b
1de7c 63 2e 6e 46 69 65 6c 64 5d 2e 66 6c 61 67 73 20 c.nField].flags
1de7d 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 & MEM_Null)==0 )
1de7e 3b 0a 0a 20 20 69 66 28 20 75 2e 62 63 2e 70 43 ;.. if( u.bc.pC
1de7f 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a rsr!=0 ){. /*
1de80 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e Populate the in
1de81 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20 dex search key.
1de82 2a 2f 0a 20 20 20 20 75 2e 62 63 2e 72 2e 70 4b */. u.bc.r.pK
1de83 65 79 49 6e 66 6f 20 3d 20 75 2e 62 63 2e 70 43 eyInfo = u.bc.pC
1de84 78 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 x->pKeyInfo;.
1de85 20 75 2e 62 63 2e 72 2e 6e 46 69 65 6c 64 20 3d u.bc.r.nField =
1de86 20 75 2e 62 63 2e 6e 46 69 65 6c 64 20 2b 20 31 u.bc.nField + 1
1de87 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61 ;. u.bc.r.fla
1de88 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 gs = UNPACKED_PR
1de89 45 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 EFIX_SEARCH;.
1de8a 20 75 2e 62 63 2e 72 2e 61 4d 65 6d 20 3d 20 75 u.bc.r.aMem = u
1de8b 2e 62 63 2e 61 4d 65 6d 3b 0a 0a 20 20 20 20 2f .bc.aMem;.. /
1de8c 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 * Extract the va
1de8d 6c 75 65 20 6f 66 20 75 2e 62 63 2e 52 20 66 72 lue of u.bc.R fr
1de8e 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 om register P3.
1de8f 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
1de90 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 beMemIntegerify(
1de91 70 49 6e 33 29 3b 0a 20 20 20 20 75 2e 62 63 2e pIn3);. u.bc.
1de92 52 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 0a R = pIn3->u.i;..
1de93 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 /* Search th
1de94 65 20 42 2d 54 72 65 65 20 69 6e 64 65 78 2e 20 e B-Tree index.
1de95 49 66 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e If no conflictin
1de96 67 20 72 65 63 6f 72 64 20 69 73 20 66 6f 75 6e g record is foun
1de97 64 2c 20 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 74 d, jump. ** t
1de98 6f 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c o P2. Otherwise,
1de99 20 63 6f 70 79 20 74 68 65 20 72 6f 77 69 64 20 copy the rowid
1de9a 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 of the conflicti
1de9b 6e 67 20 72 65 63 6f 72 64 20 74 6f 0a 20 20 20 ng record to.
1de9c 20 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 20 ** register P3
1de9d 61 6e 64 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 and fall through
1de9e 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 to the next ins
1de9f 74 72 75 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 truction. */.
1dea0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1dea1 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
1dea2 64 28 75 2e 62 63 2e 70 43 72 73 72 2c 20 26 75 d(u.bc.pCrsr, &u
1dea3 2e 62 63 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e .bc.r, 0, 0, &u.
1dea4 62 63 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 bc.pCx->seekResu
1dea5 6c 74 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e lt);. if( (u.
1dea6 62 63 2e 72 2e 66 6c 61 67 73 20 26 20 55 4e 50 bc.r.flags & UNP
1dea7 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1dea8 52 43 48 29 20 7c 7c 20 75 2e 62 63 2e 72 2e 72 RCH) || u.bc.r.r
1dea9 6f 77 69 64 3d 3d 75 2e 62 63 2e 52 20 29 7b 0a owid==u.bc.R ){.
1deaa 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1deab 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 p2 - 1;. }els
1deac 65 7b 0a 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 e{. pIn3->u
1dead 2e 69 20 3d 20 75 2e 62 63 2e 72 2e 72 6f 77 69 .i = u.bc.r.rowi
1deae 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 d;. }. }. b
1deaf 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1deb0 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31 de: NotExists P1
1deb1 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1deb2 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 Use the content
1deb3 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 of register P3
1deb4 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79 as a integer key
1deb5 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a . If a record .
1deb6 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79 ** with that key
1deb7 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
1deb8 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 in table of P1,
1deb9 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e then jump to P2.
1deba 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f .** If the reco
1debb 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 rd does exist, t
1debc 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 hen fall thru.
1debd 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 The cursor is le
1debe 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 ft .** pointing
1debf 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 to the record if
1dec0 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a it exists..**.*
1dec1 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 * The difference
1dec2 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70 between this op
1dec3 65 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 eration and NotF
1dec4 6f 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69 ound is that thi
1dec5 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 s.** operation a
1dec6 73 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69 ssumes the key i
1dec7 73 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 s an integer and
1dec8 20 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61 that P1 is a ta
1dec9 62 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e ble whereas.** N
1deca 6f 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 otFound assumes
1decb 6b 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f key is a blob co
1decc 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d nstructed from M
1decd 61 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a akeRecord and.**
1dece 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e P1 is an index.
1decf 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1ded0 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 Found, NotFound
1ded1 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 , IsUnique.*/.ca
1ded2 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a se OP_NotExists:
1ded3 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1ded4 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 p, in3 */.#if 0
1ded5 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ded6 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ded7 2e 62 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .bd */. VdbeCur
1ded8 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 sor *pC;. BtCur
1ded9 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e sor *pCrsr;. in
1deda 74 20 72 65 73 3b 0a 20 20 75 36 34 20 69 4b 65 t res;. u64 iKe
1dedb 79 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 y;.#endif /* loc
1dedc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1dedd 65 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a ed into u.bd */.
1dede 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d . assert( pIn3-
1dedf 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1dee0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1dee1 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1dee2 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1dee3 3b 0a 20 20 75 2e 62 64 2e 70 43 20 3d 20 70 2d ;. u.bd.pC = p-
1dee4 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1dee5 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e . assert( u.bd.
1dee6 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 pC!=0 );. asser
1dee7 74 28 20 75 2e 62 64 2e 70 43 2d 3e 69 73 54 61 t( u.bd.pC->isTa
1dee8 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ble );. assert(
1dee9 20 75 2e 62 64 2e 70 43 2d 3e 70 73 65 75 64 6f u.bd.pC->pseudo
1deea 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 TableReg==0 );.
1deeb 20 75 2e 62 64 2e 70 43 72 73 72 20 3d 20 75 2e u.bd.pCrsr = u.
1deec 62 64 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a bd.pC->pCursor;.
1deed 20 20 69 66 28 20 75 2e 62 64 2e 70 43 72 73 72 if( u.bd.pCrsr
1deee 21 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 64 2e !=0 ){. u.bd.
1deef 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 res = 0;. u.b
1def0 64 2e 69 4b 65 79 20 3d 20 70 49 6e 33 2d 3e 75 d.iKey = pIn3->u
1def1 2e 69 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c .i;. rc = sql
1def2 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 ite3BtreeMovetoU
1def3 6e 70 61 63 6b 65 64 28 75 2e 62 64 2e 70 43 72 npacked(u.bd.pCr
1def4 73 72 2c 20 30 2c 20 75 2e 62 64 2e 69 4b 65 79 sr, 0, u.bd.iKey
1def5 2c 20 30 2c 20 26 75 2e 62 64 2e 72 65 73 29 3b , 0, &u.bd.res);
1def6 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6c 61 . u.bd.pC->la
1def7 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e stRowid = pIn3->
1def8 75 2e 69 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 u.i;. u.bd.pC
1def9 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1defa 20 75 2e 62 64 2e 72 65 73 3d 3d 30 20 3f 31 3a u.bd.res==0 ?1:
1defb 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 0;. u.bd.pC->
1defc 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 nullRow = 0;.
1defd 20 75 2e 62 64 2e 70 43 2d 3e 63 61 63 68 65 53 u.bd.pC->cacheS
1defe 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1deff 41 4c 45 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 ALE;. u.bd.pC
1df00 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1df01 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e = 0;. if( u.
1df02 62 64 2e 72 65 73 21 3d 30 20 29 7b 0a 20 20 20 bd.res!=0 ){.
1df03 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1df04 2d 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 - 1;. asser
1df05 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 t( u.bd.pC->rowi
1df06 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 dIsValid==0 );.
1df07 20 20 20 7d 0a 20 20 20 20 75 2e 62 64 2e 70 43 }. u.bd.pC
1df08 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 75 ->seekResult = u
1df09 2e 62 64 2e 72 65 73 3b 0a 20 20 7d 65 6c 73 65 .bd.res;. }else
1df0a 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 {. /* This ha
1df0b 70 70 65 6e 73 20 77 68 65 6e 20 61 6e 20 61 74 ppens when an at
1df0c 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20 tempt to open a
1df0d 72 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 read cursor on t
1df0e 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 he. ** sqlite
1df0f 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 72 65 _master table re
1df10 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 turns SQLITE_EMP
1df11 54 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 TY.. */. p
1df12 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1df13 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1df14 64 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c d.pC->rowidIsVal
1df15 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 id==0 );. u.b
1df16 64 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 d.pC->seekResult
1df17 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 = 0;. }. brea
1df18 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1df19 20 53 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 Sequence P1 P2
1df1a 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 * * *.**.** Find
1df1b 20 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 the next availa
1df1c 62 6c 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d ble sequence num
1df1d 62 65 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 ber for cursor P
1df1e 31 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 1..** Write the
1df1f 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 sequence number
1df20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 into register P2
1df21 2e 0a 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 ..** The sequenc
1df22 65 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 e number on the
1df23 63 75 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d cursor is increm
1df24 65 6e 74 65 64 20 61 66 74 65 72 20 74 68 69 73 ented after this
1df25 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e .** instruction.
1df26 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 .*/.case OP_Se
1df27 71 75 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 quence: {
1df28 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1df29 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 elease */. asse
1df2a 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1df2b 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1df2c 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 rsor );. assert
1df2d 28 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e ( p->apCsr[pOp->
1df2e 70 31 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 p1]!=0 );. pOut
1df2f 2d 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 ->u.i = p->apCsr
1df30 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 73 65 71 43 6f [pOp->p1]->seqCo
1df31 75 6e 74 2b 2b 3b 0a 20 20 4d 65 6d 53 65 74 54 unt++;. MemSetT
1df32 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1df33 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b M_Int);. break;
1df34 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 .}.../* Opcode:
1df35 4e 65 77 52 6f 77 69 64 20 50 31 20 50 32 20 50 NewRowid P1 P2 P
1df36 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20 3 * *.**.** Get
1df37 61 20 6e 65 77 20 69 6e 74 65 67 65 72 20 72 65 a new integer re
1df38 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 61 2e 6b cord number (a.k
1df39 2e 61 20 22 72 6f 77 69 64 22 29 20 75 73 65 64 .a "rowid") used
1df3a 20 61 73 20 74 68 65 20 6b 65 79 20 74 6f 20 61 as the key to a
1df3b 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 72 table..** The r
1df3c 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 ecord number is
1df3d 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 75 not previously u
1df3e 73 65 64 20 61 73 20 61 20 6b 65 79 20 69 6e 20 sed as a key in
1df3f 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
1df40 74 61 62 6c 65 20 74 68 61 74 20 63 75 72 73 6f table that curso
1df41 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 2e 20 r P1 points to.
1df42 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 The new record
1df43 6e 75 6d 62 65 72 20 69 73 20 77 72 69 74 74 65 number is writte
1df44 6e 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 n.** written to
1df45 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a register P2..**.
1df46 2a 2a 20 49 66 20 50 33 3e 30 20 74 68 65 6e 20 ** If P3>0 then
1df47 50 33 20 69 73 20 61 20 72 65 67 69 73 74 65 72 P3 is a register
1df48 20 69 6e 20 74 68 65 20 72 6f 6f 74 20 66 72 61 in the root fra
1df49 6d 65 20 6f 66 20 74 68 69 73 20 56 44 42 45 20 me of this VDBE
1df4a 74 68 61 74 20 68 6f 6c 64 73 20 0a 2a 2a 20 74 that holds .** t
1df4b 68 65 20 6c 61 72 67 65 73 74 20 70 72 65 76 69 he largest previ
1df4c 6f 75 73 6c 79 20 67 65 6e 65 72 61 74 65 64 20 ously generated
1df4d 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 4e record number. N
1df4e 6f 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d o new record num
1df4f 62 65 72 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f bers are.** allo
1df50 77 65 64 20 74 6f 20 62 65 20 6c 65 73 73 20 74 wed to be less t
1df51 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2e 20 han this value.
1df52 57 68 65 6e 20 74 68 69 73 20 76 61 6c 75 65 20 When this value
1df53 72 65 61 63 68 65 73 20 69 74 73 20 6d 61 78 69 reaches its maxi
1df54 6d 75 6d 2c 20 0a 2a 2a 20 61 20 53 51 4c 49 54 mum, .** a SQLIT
1df55 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20 69 73 20 E_FULL error is
1df56 67 65 6e 65 72 61 74 65 64 2e 20 54 68 65 20 50 generated. The P
1df57 33 20 72 65 67 69 73 74 65 72 20 69 73 20 75 70 3 register is up
1df58 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 27 dated with the '
1df59 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72 65 .** generated re
1df5a 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 54 68 69 cord number. Thi
1df5b 73 20 50 33 20 6d 65 63 68 61 6e 69 73 6d 20 69 s P3 mechanism i
1df5c 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 s used to help i
1df5d 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 mplement the.**
1df5e 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65 AUTOINCREMENT fe
1df5f 61 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f ature..*/.case O
1df60 50 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 20 20 20 P_NewRowid: {
1df61 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1df62 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 prerelease */.#i
1df63 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1df64 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1df65 74 6f 20 75 2e 62 65 20 2a 2f 0a 20 20 69 36 34 to u.be */. i64
1df66 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
1df67 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 /* The new r
1df68 6f 77 69 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 owid */. VdbeCu
1df69 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 20 rsor *pC;
1df6a 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 61 /* Cursor of ta
1df6b 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 6e ble to get the n
1df6c 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e ew rowid */. in
1df6d 74 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 t res;
1df6e 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f /* Result o
1df6f 66 20 61 6e 20 73 71 6c 69 74 65 33 42 74 72 65 f an sqlite3Btre
1df70 65 4c 61 73 74 28 29 20 2a 2f 0a 20 20 69 6e 74 eLast() */. int
1df71 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 cnt;
1df72 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74 /* Counter t
1df73 6f 20 6c 69 6d 69 74 20 74 68 65 20 6e 75 6d 62 o limit the numb
1df74 65 72 20 6f 66 20 73 65 61 72 63 68 65 73 20 2a er of searches *
1df75 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 /. Mem *pMem;
1df76 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
1df77 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c gister holding l
1df78 61 72 67 65 73 74 20 72 6f 77 69 64 20 66 6f 72 argest rowid for
1df79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a AUTOINCREMENT *
1df7a 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 /. VdbeFrame *p
1df7b 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 6f Frame; /* Ro
1df7c 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 45 ot frame of VDBE
1df7d 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f */.#endif /* lo
1df7e 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1df7f 76 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 2a 2f ved into u.be */
1df80 0a 0a 20 20 75 2e 62 65 2e 76 20 3d 20 30 3b 0a .. u.be.v = 0;.
1df81 20 20 75 2e 62 65 2e 72 65 73 20 3d 20 30 3b 0a u.be.res = 0;.
1df82 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1df83 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1df84 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1df85 75 2e 62 65 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.be.pC = p->apC
1df86 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1df87 73 73 65 72 74 28 20 75 2e 62 65 2e 70 43 21 3d ssert( u.be.pC!=
1df88 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 0 );. if( NEVER
1df89 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f (u.be.pC->pCurso
1df8a 72 3d 3d 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 r==0) ){. /*
1df8b 54 68 65 20 7a 65 72 6f 20 69 6e 69 74 69 61 6c The zero initial
1df8c 69 7a 61 74 69 6f 6e 20 61 62 6f 76 65 20 69 73 ization above is
1df8d 20 61 6c 6c 20 74 68 61 74 20 69 73 20 6e 65 65 all that is nee
1df8e 64 65 64 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a ded */. }else{.
1df8f 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 /* The next
1df90 72 6f 77 69 64 20 6f 72 20 72 65 63 6f 72 64 20 rowid or record
1df91 6e 75 6d 62 65 72 20 28 64 69 66 66 65 72 65 6e number (differen
1df92 74 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20 t terms for the
1df93 73 61 6d 65 0a 20 20 20 20 2a 2a 20 74 68 69 6e same. ** thin
1df94 67 29 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 g) is obtained i
1df95 6e 20 61 20 74 77 6f 2d 73 74 65 70 20 61 6c 67 n a two-step alg
1df96 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 0a 20 orithm.. **.
1df97 20 20 20 2a 2a 20 46 69 72 73 74 20 77 65 20 61 ** First we a
1df98 74 74 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 ttempt to find t
1df99 68 65 20 6c 61 72 67 65 73 74 20 65 78 69 73 74 he largest exist
1df9a 69 6e 67 20 72 6f 77 69 64 20 61 6e 64 20 61 64 ing rowid and ad
1df9b 64 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 74 6f 20 d one. ** to
1df9c 74 68 61 74 2e 20 20 42 75 74 20 69 66 20 74 68 that. But if th
1df9d 65 20 6c 61 72 67 65 73 74 20 65 78 69 73 74 69 e largest existi
1df9e 6e 67 20 72 6f 77 69 64 20 69 73 20 61 6c 72 65 ng rowid is alre
1df9f 61 64 79 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a ady the maximum.
1dfa0 20 20 20 20 2a 2a 20 70 6f 73 69 74 69 76 65 20 ** positive
1dfa1 69 6e 74 65 67 65 72 2c 20 77 65 20 68 61 76 65 integer, we have
1dfa2 20 74 6f 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 to fall through
1dfa3 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 0a 20 to the second.
1dfa4 20 20 20 2a 2a 20 70 72 6f 62 61 62 69 6c 69 73 ** probabilis
1dfa5 74 69 63 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 tic algorithm.
1dfa6 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 **. ** The
1dfa7 73 65 63 6f 6e 64 20 61 6c 67 6f 72 69 74 68 6d second algorithm
1dfa8 20 69 73 20 74 6f 20 73 65 6c 65 63 74 20 61 20 is to select a
1dfa9 72 6f 77 69 64 20 61 74 20 72 61 6e 64 6f 6d 20 rowid at random
1dfaa 61 6e 64 20 73 65 65 20 69 66 0a 20 20 20 20 2a and see if. *
1dfab 2a 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 * it already exi
1dfac 73 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 sts in the table
1dfad 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f . If it does no
1dfae 74 20 65 78 69 73 74 2c 20 77 65 20 68 61 76 65 t exist, we have
1dfaf 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 65 64 65 . ** succeede
1dfb0 64 2e 20 20 49 66 20 74 68 65 20 72 61 6e 64 6f d. If the rando
1dfb1 6d 20 72 6f 77 69 64 20 64 6f 65 73 20 65 78 69 m rowid does exi
1dfb2 73 74 2c 20 77 65 20 73 65 6c 65 63 74 20 61 20 st, we select a
1dfb3 6e 65 77 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 new one. ** a
1dfb4 6e 64 20 74 72 79 20 61 67 61 69 6e 2c 20 75 70 nd try again, up
1dfb5 20 74 6f 20 31 30 30 20 74 69 6d 65 73 2e 0a 20 to 100 times..
1dfb6 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
1dfb7 28 20 75 2e 62 65 2e 70 43 2d 3e 69 73 54 61 62 ( u.be.pC->isTab
1dfb8 6c 65 20 29 3b 0a 20 20 20 20 75 2e 62 65 2e 63 le );. u.be.c
1dfb9 6e 74 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 nt = 0;..#ifdef
1dfba 53 51 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f 57 SQLITE_32BIT_ROW
1dfbb 49 44 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41 ID.# define MA
1dfbc 58 5f 52 4f 57 49 44 20 30 78 37 66 66 66 66 66 X_ROWID 0x7fffff
1dfbd 66 66 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 ff.#else. /*
1dfbe 53 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20 63 Some compilers c
1dfbf 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 63 6f omplain about co
1dfc0 6e 73 74 61 6e 74 73 20 6f 66 20 74 68 65 20 66 nstants of the f
1dfc1 6f 72 6d 20 30 78 37 66 66 66 66 66 66 66 66 66 orm 0x7fffffffff
1dfc2 66 66 66 66 66 66 2e 0a 20 20 20 20 2a 2a 20 4f ffffff.. ** O
1dfc3 74 68 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61 thers complain a
1dfc4 62 6f 75 74 20 30 78 37 66 66 66 66 66 66 66 66 bout 0x7ffffffff
1dfc5 66 66 66 66 66 66 66 66 4c 4c 2e 20 20 54 68 65 ffffffffLL. The
1dfc6 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
1dfc7 20 73 65 65 6d 73 0a 20 20 20 20 2a 2a 20 74 6f seems. ** to
1dfc8 20 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f 6e provide the con
1dfc9 73 74 61 6e 74 20 77 68 69 6c 65 20 6d 61 6b 69 stant while maki
1dfca 6e 67 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 73 ng all compilers
1dfcb 20 68 61 70 70 79 2e 0a 20 20 20 20 2a 2f 0a 23 happy.. */.#
1dfcc 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f define MAX_RO
1dfcd 57 49 44 20 20 28 69 36 34 29 28 20 28 28 28 75 WID (i64)( (((u
1dfce 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 64)0x7fffffff)<<
1dfcf 33 32 29 20 7c 20 28 75 36 34 29 30 78 66 66 66 32) | (u64)0xfff
1dfd0 66 66 66 66 66 20 29 0a 23 65 6e 64 69 66 0a 0a fffff ).#endif..
1dfd1 20 20 20 20 69 66 28 20 21 75 2e 62 65 2e 70 43 if( !u.be.pC
1dfd2 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 ->useRandomRowid
1dfd3 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 65 2e 76 ){. u.be.v
1dfd4 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
1dfd5 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e etCachedRowid(u.
1dfd6 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b be.pC->pCursor);
1dfd7 0a 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e . if( u.be.
1dfd8 76 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 v==0 ){.
1dfd9 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1dfda 65 4c 61 73 74 28 75 2e 62 65 2e 70 43 2d 3e 70 eLast(u.be.pC->p
1dfdb 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 72 65 Cursor, &u.be.re
1dfdc 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 s);. if(
1dfdd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1dfde 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1dfdf 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 abort_due_to_err
1dfe0 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 or;. }.
1dfe1 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 72 if( u.be.r
1dfe2 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 es ){.
1dfe3 75 2e 62 65 2e 76 20 3d 20 31 3b 0a 20 20 20 20 u.be.v = 1;.
1dfe4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1dfe5 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
1dfe6 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 ite3BtreeCursorI
1dfe7 73 56 61 6c 69 64 28 75 2e 62 65 2e 70 43 2d 3e sValid(u.be.pC->
1dfe8 70 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 pCursor) );.
1dfe9 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1dfea 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 e3BtreeKeySize(u
1dfeb 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c .be.pC->pCursor,
1dfec 20 26 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 20 &u.be.v);.
1dfed 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d assert( rc=
1dfee 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 =SQLITE_OK );
1dfef 2f 2a 20 43 61 6e 6e 6f 74 20 66 61 69 6c 20 66 /* Cannot fail f
1dff0 6f 6c 6c 6f 77 69 6e 67 20 42 74 72 65 65 4c 61 ollowing BtreeLa
1dff1 73 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 st() */.
1dff2 20 20 69 66 28 20 75 2e 62 65 2e 76 3d 3d 4d 41 if( u.be.v==MA
1dff3 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 X_ROWID ){.
1dff4 20 20 20 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e u.be.pC->
1dff5 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 3d useRandomRowid =
1dff6 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 1;. }e
1dff7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
1dff8 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 20 20 u.be.v++;.
1dff9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1dffa 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 . }..#ifnde
1dffb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
1dffc 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 TOINCREMENT.
1dffd 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b if( pOp->p3 ){
1dffe 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65 . /* Asse
1dfff 72 74 20 74 68 61 74 20 50 33 20 69 73 20 61 20 rt that P3 is a
1e000 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c valid memory cel
1e001 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 l. */. as
1e002 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 sert( pOp->p3>0
1e003 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
1e004 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 ->pFrame ){.
1e005 20 20 20 20 20 20 66 6f 72 28 75 2e 62 65 2e 70 for(u.be.p
1e006 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b Frame=p->pFrame;
1e007 20 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 70 50 u.be.pFrame->pP
1e008 61 72 65 6e 74 3b 20 75 2e 62 65 2e 70 46 72 61 arent; u.be.pFra
1e009 6d 65 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e me=u.be.pFrame->
1e00a 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 pParent);.
1e00b 20 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 /* Assert th
1e00c 61 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 64 at P3 is a valid
1e00d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f memory cell. */
1e00e 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
1e00f 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 75 2e 62 65 t( pOp->p3<=u.be
1e010 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 29 3b .pFrame->nMem );
1e011 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e . u.be.
1e012 70 4d 65 6d 20 3d 20 26 75 2e 62 65 2e 70 46 72 pMem = &u.be.pFr
1e013 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 ame->aMem[pOp->p
1e014 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 3];. }els
1e015 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e{. /*
1e016 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69 Assert that P3 i
1e017 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 s a valid memory
1e018 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 cell. */.
1e019 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1e01a 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1e01b 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 70 u.be.p
1e01c 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 Mem = &p->aMem[p
1e01d 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 Op->p3];.
1e01e 20 7d 0a 0a 20 20 20 20 20 20 20 20 52 45 47 49 }.. REGI
1e01f 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1e020 70 33 2c 20 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a p3, u.be.pMem);.
1e021 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1e022 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
1e023 28 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 (u.be.pMem);.
1e024 20 20 20 20 20 61 73 73 65 72 74 28 20 28 75 2e assert( (u.
1e025 62 65 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 be.pMem->flags &
1e026 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 MEM_Int)!=0 );
1e027 20 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 /* mem(P3) hold
1e028 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a s an integer */.
1e029 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 if( u.be
1e02a 2e 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 4d 41 58 5f .pMem->u.i==MAX_
1e02b 52 4f 57 49 44 20 7c 7c 20 75 2e 62 65 2e 70 43 ROWID || u.be.pC
1e02c 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 ->useRandomRowid
1e02d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
1e02e 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a = SQLITE_FULL;.
1e02f 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 goto a
1e030 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1e031 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 r;. }.
1e032 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 76 3c if( u.be.v<
1e033 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 2b 31 u.be.pMem->u.i+1
1e034 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e ){. u.
1e035 62 65 2e 76 20 3d 20 75 2e 62 65 2e 70 4d 65 6d be.v = u.be.pMem
1e036 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20 ->u.i + 1;.
1e037 20 20 20 7d 0a 20 20 20 20 20 20 20 20 75 2e 62 }. u.b
1e038 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 75 2e e.pMem->u.i = u.
1e039 62 65 2e 76 3b 0a 20 20 20 20 20 20 7d 0a 23 65 be.v;. }.#e
1e03a 6e 64 69 66 0a 0a 20 20 20 20 20 20 73 71 6c 69 ndif.. sqli
1e03b 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 te3BtreeSetCache
1e03c 64 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e dRowid(u.be.pC->
1e03d 70 43 75 72 73 6f 72 2c 20 75 2e 62 65 2e 76 3c pCursor, u.be.v<
1e03e 4d 41 58 5f 52 4f 57 49 44 20 3f 20 75 2e 62 65 MAX_ROWID ? u.be
1e03f 2e 76 2b 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d .v+1 : 0);. }
1e040 0a 20 20 20 20 69 66 28 20 75 2e 62 65 2e 70 43 . if( u.be.pC
1e041 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 ->useRandomRowid
1e042 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
1e043 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 20 ( pOp->p3==0 );
1e044 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 62 65 /* We cannot be
1e045 20 69 6e 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 in random rowid
1e046 20 6d 6f 64 65 20 69 66 20 74 68 69 73 20 69 73 mode if this is
1e047 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1e048 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
1e049 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e an AUTOINCREMEN
1e04a 54 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 T table. */.
1e04b 20 20 75 2e 62 65 2e 76 20 3d 20 64 62 2d 3e 6c u.be.v = db->l
1e04c 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 astRowid;.
1e04d 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 u.be.cnt = 0;.
1e04e 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 do{.
1e04f 69 66 28 20 75 2e 62 65 2e 63 6e 74 3d 3d 30 20 if( u.be.cnt==0
1e050 26 26 20 28 75 2e 62 65 2e 76 26 30 78 66 66 66 && (u.be.v&0xfff
1e051 66 66 66 29 3d 3d 75 2e 62 65 2e 76 20 29 7b 0a fff)==u.be.v ){.
1e052 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 u.be.v
1e053 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ++;. }els
1e054 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c e{. sql
1e055 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 ite3_randomness(
1e056 73 69 7a 65 6f 66 28 75 2e 62 65 2e 76 29 2c 20 sizeof(u.be.v),
1e057 26 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 20 20 &u.be.v);.
1e058 20 20 20 20 69 66 28 20 75 2e 62 65 2e 63 6e 74 if( u.be.cnt
1e059 3c 35 20 29 20 75 2e 62 65 2e 76 20 26 3d 20 30 <5 ) u.be.v &= 0
1e05a 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 xffffff;.
1e05b 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1e05c 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1e05d 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 65 2e toUnpacked(u.be.
1e05e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 pC->pCursor, 0,
1e05f 28 75 36 34 29 75 2e 62 65 2e 76 2c 20 30 2c 20 (u64)u.be.v, 0,
1e060 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20 &u.be.res);.
1e061 20 20 20 20 75 2e 62 65 2e 63 6e 74 2b 2b 3b 0a u.be.cnt++;.
1e062 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 75 2e }while( u.
1e063 62 65 2e 63 6e 74 3c 31 30 30 20 26 26 20 72 63 be.cnt<100 && rc
1e064 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 ==SQLITE_OK && u
1e065 2e 62 65 2e 72 65 73 3d 3d 30 20 29 3b 0a 20 20 .be.res==0 );.
1e066 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1e067 54 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 TE_OK && u.be.re
1e068 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 s==0 ){.
1e069 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c rc = SQLITE_FULL
1e06a 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 ;. goto a
1e06b 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1e06c 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d r;. }. }
1e06d 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 72 6f . u.be.pC->ro
1e06e 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1e06f 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 64 65 66 u.be.pC->def
1e070 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b erredMoveto = 0;
1e071 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 63 61 . u.be.pC->ca
1e072 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1e073 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d E_STALE;. }. M
1e074 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
1e075 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 ut, MEM_Int);.
1e076 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 65 pOut->u.i = u.be
1e077 2e 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a .v;. break;.}..
1e078 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 /* Opcode: Inser
1e079 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 t P1 P2 P3 P4 P5
1e07a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 .**.** Write an
1e07b 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 entry into the t
1e07c 61 62 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 able of cursor P
1e07d 31 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 1. A new entry
1e07e 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 is.** created if
1e07f 20 69 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 it doesn't alre
1e080 61 64 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 ady exist or the
1e081 20 64 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 data for an exi
1e082 73 74 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 sting.** entry i
1e083 73 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 s overwritten.
1e084 54 68 65 20 64 61 74 61 20 69 73 20 74 68 65 20 The data is the
1e085 76 61 6c 75 65 20 4d 45 4d 5f 42 6c 6f 62 20 73 value MEM_Blob s
1e086 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1e087 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20 r.** number P2.
1e088 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65 The key is store
1e089 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
1e08a 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a . The key must.*
1e08b 2a 20 62 65 20 61 20 4d 45 4d 5f 49 6e 74 2e 0a * be a MEM_Int..
1e08c 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 **.** If the OPF
1e08d 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 LAG_NCHANGE flag
1e08e 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 of P5 is set, t
1e08f 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e hen the row chan
1e090 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 ge count is.** i
1e091 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 ncremented (othe
1e092 72 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 rwise not). If
1e093 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 the OPFLAG_LASTR
1e094 4f 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20 OWID flag of P5
1e095 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 is set,.** then
1e096 72 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 rowid is stored
1e097 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 for subsequent r
1e098 65 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 eturn by the.**
1e099 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 sqlite3_last_ins
1e09a 65 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 ert_rowid() func
1e09b 74 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 tion (otherwise
1e09c 69 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 it is unmodified
1e09d 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 )..**.** If the
1e09e 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 OPFLAG_USESEEKRE
1e09f 53 55 4c 54 20 66 6c 61 67 20 6f 66 20 50 35 20 SULT flag of P5
1e0a0 69 73 20 73 65 74 20 61 6e 64 20 69 66 20 74 68 is set and if th
1e0a1 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 e result of.** t
1e0a2 68 65 20 6c 61 73 74 20 73 65 65 6b 20 6f 70 65 he last seek ope
1e0a3 72 61 74 69 6f 6e 20 28 4f 50 5f 4e 6f 74 45 78 ration (OP_NotEx
1e0a4 69 73 74 73 29 20 77 61 73 20 61 20 73 75 63 63 ists) was a succ
1e0a5 65 73 73 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a ess, then this.*
1e0a6 2a 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c * operation will
1e0a7 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 not attempt to
1e0a8 66 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 find the appropr
1e0a9 69 61 74 65 20 72 6f 77 20 62 65 66 6f 72 65 20 iate row before
1e0aa 64 6f 69 6e 67 0a 2a 2a 20 74 68 65 20 69 6e 73 doing.** the ins
1e0ab 65 72 74 20 62 75 74 20 77 69 6c 6c 20 69 6e 73 ert but will ins
1e0ac 74 65 61 64 20 6f 76 65 72 77 72 69 74 65 20 74 tead overwrite t
1e0ad 68 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20 he row that the
1e0ae 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 75 72 cursor is.** cur
1e0af 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 rently pointing
1e0b0 74 6f 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 2c to. Presumably,
1e0b1 20 74 68 65 20 70 72 69 6f 72 20 4f 50 5f 4e 6f the prior OP_No
1e0b2 74 45 78 69 73 74 73 20 6f 70 63 6f 64 65 0a 2a tExists opcode.*
1e0b3 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 6f * has already po
1e0b4 73 69 74 69 6f 6e 65 64 20 74 68 65 20 63 75 72 sitioned the cur
1e0b5 73 6f 72 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 sor correctly.
1e0b6 54 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d This is an optim
1e0b7 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 ization.** that
1e0b8 62 6f 6f 73 74 73 20 70 65 72 66 6f 72 6d 61 6e boosts performan
1e0b9 63 65 20 62 79 20 61 76 6f 69 64 69 6e 67 20 72 ce by avoiding r
1e0ba 65 64 75 6e 64 61 6e 74 20 73 65 65 6b 73 2e 0a edundant seeks..
1e0bb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 **.** If the OPF
1e0bc 4c 41 47 5f 49 53 55 50 44 41 54 45 20 66 6c 61 LAG_ISUPDATE fla
1e0bd 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 g is set, then t
1e0be 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 70 61 his opcode is pa
1e0bf 72 74 20 6f 66 20 61 6e 0a 2a 2a 20 55 50 44 41 rt of an.** UPDA
1e0c0 54 45 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4f TE operation. O
1e0c1 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 therwise (if the
1e0c2 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 29 20 flag is clear)
1e0c3 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 then this opcode
1e0c4 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 .** is part of a
1e0c5 6e 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 n INSERT operati
1e0c6 6f 6e 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 on. The differe
1e0c7 6e 63 65 20 69 73 20 6f 6e 6c 79 20 69 6d 70 6f nce is only impo
1e0c8 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 rtant to.** the
1e0c9 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 0a update hook..**.
1e0ca 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 50 34 20 ** Parameter P4
1e0cb 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 may point to a s
1e0cc 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 tring containing
1e0cd 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 2c the table-name,
1e0ce 20 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 or.** may be NU
1e0cf 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 LL. If it is not
1e0d0 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 NULL, then the
1e0d1 75 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a 20 update-hook .**
1e0d2 28 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 65 (sqlite3.xUpdate
1e0d3 43 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e 76 Callback) is inv
1e0d4 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 oked following a
1e0d5 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 65 successful inse
1e0d6 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 52 4e 49 rt..**.** (WARNI
1e0d7 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31 20 69 NG/TODO: If P1 i
1e0d8 73 20 61 20 70 73 65 75 64 6f 2d 63 75 72 73 6f s a pseudo-curso
1e0d9 72 20 61 6e 64 20 50 32 20 69 73 20 64 79 6e 61 r and P2 is dyna
1e0da 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 mically.** alloc
1e0db 61 74 65 64 2c 20 74 68 65 6e 20 6f 77 6e 65 72 ated, then owner
1e0dc 73 68 69 70 20 6f 66 20 50 32 20 69 73 20 74 72 ship of P2 is tr
1e0dd 61 6e 73 66 65 72 72 65 64 20 74 6f 20 74 68 65 ansferred to the
1e0de 20 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 0a 2a pseudo-cursor.*
1e0df 2a 20 61 6e 64 20 72 65 67 69 73 74 65 72 20 50 * and register P
1e0e0 32 20 62 65 63 6f 6d 65 73 20 65 70 68 65 6d 65 2 becomes epheme
1e0e1 72 61 6c 2e 20 20 49 66 20 74 68 65 20 63 75 72 ral. If the cur
1e0e2 73 6f 72 20 69 73 20 63 68 61 6e 67 65 64 2c 20 sor is changed,
1e0e3 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 the.** value of
1e0e4 72 65 67 69 73 74 65 72 20 50 32 20 77 69 6c 6c register P2 will
1e0e5 20 74 68 65 6e 20 63 68 61 6e 67 65 2e 20 20 4d then change. M
1e0e6 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 64 6f ake sure this do
1e0e7 65 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 73 65 20 es not.** cause
1e0e8 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 29 0a 2a any problems.).*
1e0e9 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 *.** This instru
1e0ea 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 ction only works
1e0eb 20 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68 65 on tables. The
1e0ec 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 equivalent inst
1e0ed 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 69 ruction.** for i
1e0ee 6e 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64 78 ndices is OP_Idx
1e0ef 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 65 20 Insert..*/.case
1e0f0 4f 50 5f 49 6e 73 65 72 74 3a 20 7b 0a 23 69 66 OP_Insert: {.#if
1e0f1 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e0f2 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e0f3 6f 20 75 2e 62 66 20 2a 2f 0a 20 20 4d 65 6d 20 o u.bf */. Mem
1e0f4 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a *pData; /*
1e0f5 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e MEM cell holdin
1e0f6 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 g data for the r
1e0f7 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 ecord to be inse
1e0f8 72 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 rted */. Mem *p
1e0f9 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d Key; /* M
1e0fa 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 EM cell holding
1e0fb 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72 65 63 key for the rec
1e0fc 6f 72 64 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 ord */. i64 iKe
1e0fd 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 y; /* Th
1e0fe 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44 20 e integer ROWID
1e0ff 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72 or key for the r
1e100 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 ecord to be inse
1e101 72 74 65 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 rted */. VdbeCu
1e102 72 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43 rsor *pC; /* C
1e103 75 72 73 6f 72 20 74 6f 20 74 61 62 6c 65 20 69 ursor to table i
1e104 6e 74 6f 20 77 68 69 63 68 20 69 6e 73 65 72 74 nto which insert
1e105 20 69 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 is written */.
1e106 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 int nZero;
1e107 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1e108 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 zero-bytes to ap
1e109 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 pend */. int se
1e10a 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52 ekResult; /* R
1e10b 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 73 esult of prior s
1e10c 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20 55 eek or 0 if no U
1e10d 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 SESEEKRESULT fla
1e10e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 g */. const cha
1e10f 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74 61 r *zDb; /* data
1e110 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 base name - used
1e111 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20 68 by the update h
1e112 6f 6f 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ook */. const c
1e113 68 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 har *zTbl; /* Ta
1e114 62 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 ble name - used
1e115 62 79 20 74 68 65 20 6f 70 64 61 74 65 20 68 6f by the opdate ho
1e116 6f 6b 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 ok */. int op;
1e117 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 /* Opc
1e118 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68 ode for update h
1e119 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 ook: SQLITE_UPDA
1e11a 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 TE or SQLITE_INS
1e11b 45 52 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a ERT */.#endif /*
1e11c 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e11d 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 moved into u.bf
1e11e 20 2a 2f 0a 0a 20 20 75 2e 62 66 2e 70 44 61 74 */.. u.bf.pDat
1e11f 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 a = &p->aMem[pOp
1e120 2d 3e 70 32 5d 3b 0a 20 20 75 2e 62 66 2e 70 4b ->p2];. u.bf.pK
1e121 65 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f ey = &p->aMem[pO
1e122 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 p->p3];. assert
1e123 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e124 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e125 6f 72 20 29 3b 0a 20 20 75 2e 62 66 2e 70 43 20 or );. u.bf.pC
1e126 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e127 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e128 2e 62 66 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 .bf.pC!=0 );. a
1e129 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e ssert( u.bf.pC->
1e12a 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 pCursor!=0 );.
1e12b 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d assert( u.bf.pC-
1e12c 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d >pseudoTableReg=
1e12d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e12e 75 2e 62 66 2e 70 4b 65 79 2d 3e 66 6c 61 67 73 u.bf.pKey->flags
1e12f 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 & MEM_Int );.
1e130 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d assert( u.bf.pC-
1e131 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 >isTable );. RE
1e132 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 GISTER_TRACE(pOp
1e133 2d 3e 70 32 2c 20 75 2e 62 66 2e 70 44 61 74 61 ->p2, u.bf.pData
1e134 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 );. REGISTER_TR
1e135 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 ACE(pOp->p3, u.b
1e136 66 2e 70 4b 65 79 29 3b 0a 0a 20 20 75 2e 62 66 f.pKey);.. u.bf
1e137 2e 69 4b 65 79 20 3d 20 75 2e 62 66 2e 70 4b 65 .iKey = u.bf.pKe
1e138 79 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 70 4f y->u.i;. if( pO
1e139 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4e p->p5 & OPFLAG_N
1e13a 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 CHANGE ) p->nCha
1e13b 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 20 70 4f 70 nge++;. if( pOp
1e13c 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4c 41 ->p5 & OPFLAG_LA
1e13d 53 54 52 4f 57 49 44 20 29 20 64 62 2d 3e 6c 61 STROWID ) db->la
1e13e 73 74 52 6f 77 69 64 20 3d 20 75 2e 62 66 2e 70 stRowid = u.bf.p
1e13f 4b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 Key->u.i;. if(
1e140 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 u.bf.pData->flag
1e141 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a s & MEM_Null ){.
1e142 20 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e u.bf.pData->
1e143 7a 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 66 2e z = 0;. u.bf.
1e144 70 44 61 74 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 pData->n = 0;.
1e145 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
1e146 74 28 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 t( u.bf.pData->f
1e147 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 lags & (MEM_Blob
1e148 7c 4d 45 4d 5f 53 74 72 29 20 29 3b 0a 20 20 7d |MEM_Str) );. }
1e149 0a 20 20 75 2e 62 66 2e 73 65 65 6b 52 65 73 75 . u.bf.seekResu
1e14a 6c 74 20 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 lt = ((pOp->p5 &
1e14b 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 OPFLAG_USESEEKR
1e14c 45 53 55 4c 54 29 20 3f 20 75 2e 62 66 2e 70 43 ESULT) ? u.bf.pC
1e14d 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3a 20 30 ->seekResult : 0
1e14e 29 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 44 );. if( u.bf.pD
1e14f 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ata->flags & MEM
1e150 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 75 2e 62 _Zero ){. u.b
1e151 66 2e 6e 5a 65 72 6f 20 3d 20 75 2e 62 66 2e 70 f.nZero = u.bf.p
1e152 44 61 74 61 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 Data->u.nZero;.
1e153 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 66 }else{. u.bf
1e154 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a .nZero = 0;. }.
1e155 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 sqlite3BtreeSe
1e156 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62 tCachedRowid(u.b
1e157 66 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 f.pC->pCursor, 0
1e158 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1e159 33 42 74 72 65 65 49 6e 73 65 72 74 28 75 2e 62 3BtreeInsert(u.b
1e15a 66 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 f.pC->pCursor, 0
1e15b 2c 20 75 2e 62 66 2e 69 4b 65 79 2c 0a 20 20 20 , u.bf.iKey,.
1e15c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e15d 20 20 20 20 20 20 20 75 2e 62 66 2e 70 44 61 74 u.bf.pDat
1e15e 61 2d 3e 7a 2c 20 75 2e 62 66 2e 70 44 61 74 61 a->z, u.bf.pData
1e15f 2d 3e 6e 2c 20 75 2e 62 66 2e 6e 5a 65 72 6f 2c ->n, u.bf.nZero,
1e160 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1e161 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e pOp->
1e162 70 35 20 26 20 4f 50 46 4c 41 47 5f 41 50 50 45 p5 & OPFLAG_APPE
1e163 4e 44 2c 20 75 2e 62 66 2e 73 65 65 6b 52 65 73 ND, u.bf.seekRes
1e164 75 6c 74 0a 20 20 29 3b 0a 20 20 75 2e 62 66 2e ult. );. u.bf.
1e165 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1e166 20 3d 20 30 3b 0a 20 20 75 2e 62 66 2e 70 43 2d = 0;. u.bf.pC-
1e167 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1e168 3d 20 30 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e = 0;. u.bf.pC->
1e169 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1e16a 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a CHE_STALE;.. /*
1e16b 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 61 Invoke the upda
1e16c 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 69 te-hook if requi
1e16d 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 red. */. if( rc
1e16e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 ==SQLITE_OK && d
1e16f 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 b->xUpdateCallba
1e170 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 ck && pOp->p4.z
1e171 29 7b 0a 20 20 20 20 75 2e 62 66 2e 7a 44 62 20 ){. u.bf.zDb
1e172 3d 20 64 62 2d 3e 61 44 62 5b 75 2e 62 66 2e 70 = db->aDb[u.bf.p
1e173 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 C->iDb].zName;.
1e174 20 20 20 75 2e 62 66 2e 7a 54 62 6c 20 3d 20 70 u.bf.zTbl = p
1e175 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 75 2e Op->p4.z;. u.
1e176 62 66 2e 6f 70 20 3d 20 28 28 70 4f 70 2d 3e 70 bf.op = ((pOp->p
1e177 35 20 26 20 4f 50 46 4c 41 47 5f 49 53 55 50 44 5 & OPFLAG_ISUPD
1e178 41 54 45 29 20 3f 20 53 51 4c 49 54 45 5f 55 50 ATE) ? SQLITE_UP
1e179 44 41 54 45 20 3a 20 53 51 4c 49 54 45 5f 49 4e DATE : SQLITE_IN
1e17a 53 45 52 54 29 3b 0a 20 20 20 20 61 73 73 65 72 SERT);. asser
1e17b 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73 54 61 t( u.bf.pC->isTa
1e17c 62 6c 65 20 29 3b 0a 20 20 20 20 64 62 2d 3e 78 ble );. db->x
1e17d 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 UpdateCallback(d
1e17e 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 75 b->pUpdateArg, u
1e17f 2e 62 66 2e 6f 70 2c 20 75 2e 62 66 2e 7a 44 62 .bf.op, u.bf.zDb
1e180 2c 20 75 2e 62 66 2e 7a 54 62 6c 2c 20 75 2e 62 , u.bf.zTbl, u.b
1e181 66 2e 69 4b 65 79 29 3b 0a 20 20 20 20 61 73 73 f.iKey);. ass
1e182 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 44 ert( u.bf.pC->iD
1e183 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72 b>=0 );. }. br
1e184 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e185 65 3a 20 44 65 6c 65 74 65 20 50 31 20 50 32 20 e: Delete P1 P2
1e186 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c * P4 *.**.** Del
1e187 65 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 61 ete the record a
1e188 74 20 77 68 69 63 68 20 74 68 65 20 50 31 20 63 t which the P1 c
1e189 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 ursor is current
1e18a 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a ly pointing..**.
1e18b 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 77 69 ** The cursor wi
1e18c 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f 69 6e 74 ll be left point
1e18d 69 6e 67 20 61 74 20 65 69 74 68 65 72 20 74 68 ing at either th
1e18e 65 20 6e 65 78 74 20 6f 72 20 74 68 65 20 70 72 e next or the pr
1e18f 65 76 69 6f 75 73 0a 2a 2a 20 72 65 63 6f 72 64 evious.** record
1e190 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49 in the table. I
1e191 66 20 69 74 20 69 73 20 6c 65 66 74 20 70 6f 69 f it is left poi
1e192 6e 74 69 6e 67 20 61 74 20 74 68 65 20 6e 65 78 nting at the nex
1e193 74 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 0a 2a t record, then.*
1e194 2a 20 74 68 65 20 6e 65 78 74 20 4e 65 78 74 20 * the next Next
1e195 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c instruction will
1e196 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 65 be a no-op. He
1e197 6e 63 65 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 nce it is OK to
1e198 64 65 6c 65 74 65 0a 2a 2a 20 61 20 72 65 63 6f delete.** a reco
1e199 72 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 rd from within a
1e19a 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a n Next loop..**.
1e19b 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 ** If the OPFLAG
1e19c 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 6f 66 _NCHANGE flag of
1e19d 20 50 32 20 69 73 20 73 65 74 2c 20 74 68 65 6e P2 is set, then
1e19e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 the row change
1e19f 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72 count is.** incr
1e1a0 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 77 69 emented (otherwi
1e1a1 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50 se not)..**.** P
1e1a2 31 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 70 73 1 must not be ps
1e1a3 65 75 64 6f 2d 74 61 62 6c 65 2e 20 20 49 74 20 eudo-table. It
1e1a4 68 61 73 20 74 6f 20 62 65 20 61 20 72 65 61 6c has to be a real
1e1a5 20 74 61 62 6c 65 20 77 69 74 68 0a 2a 2a 20 6d table with.** m
1e1a6 75 6c 74 69 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a ultiple rows..**
1e1a7 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 .** If P4 is not
1e1a8 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 NULL, then it i
1e1a9 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
1e1aa 65 20 74 61 62 6c 65 20 74 68 61 74 20 50 31 20 e table that P1
1e1ab 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 is.** pointing t
1e1ac 6f 2e 20 20 54 68 65 20 75 70 64 61 74 65 20 68 o. The update h
1e1ad 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f ook will be invo
1e1ae 6b 65 64 2c 20 69 66 20 69 74 20 65 78 69 73 74 ked, if it exist
1e1af 73 2e 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e s..** If P4 is n
1e1b0 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 ot NULL then the
1e1b1 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 P1 cursor must
1e1b2 68 61 76 65 20 62 65 65 6e 20 70 6f 73 69 74 69 have been positi
1e1b3 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4f 50 oned.** using OP
1e1b4 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 69 6f 72 20 _NotFound prior
1e1b5 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 to invoking this
1e1b6 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 opcode..*/.case
1e1b7 20 4f 50 5f 44 65 6c 65 74 65 3a 20 7b 0a 23 69 OP_Delete: {.#i
1e1b8 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e1b9 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e1ba 74 6f 20 75 2e 62 67 20 2a 2f 0a 20 20 69 36 34 to u.bg */. i64
1e1bb 20 69 4b 65 79 3b 0a 20 20 56 64 62 65 43 75 72 iKey;. VdbeCur
1e1bc 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 69 66 20 sor *pC;.#endif
1e1bd 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e1be 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e1bf 62 67 20 2a 2f 0a 0a 20 20 75 2e 62 67 2e 69 4b bg */.. u.bg.iK
1e1c0 65 79 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 ey = 0;. assert
1e1c1 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e1c2 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e1c3 6f 72 20 29 3b 0a 20 20 75 2e 62 67 2e 70 43 20 or );. u.bg.pC
1e1c4 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e1c5 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e1c6 2e 62 67 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 .bg.pC!=0 );. a
1e1c7 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e ssert( u.bg.pC->
1e1c8 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 20 20 2f pCursor!=0 ); /
1e1c9 2a 20 4f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 * Only valid for
1e1ca 20 72 65 61 6c 20 74 61 62 6c 65 73 2c 20 6e 6f real tables, no
1e1cb 20 70 73 65 75 64 6f 74 61 62 6c 65 73 20 2a 2f pseudotables */
1e1cc 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75 70 .. /* If the up
1e1cd 64 61 74 65 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62 date-hook will b
1e1ce 65 20 69 6e 76 6f 6b 65 64 2c 20 73 65 74 20 75 e invoked, set u
1e1cf 2e 62 67 2e 69 4b 65 79 20 74 6f 20 74 68 65 20 .bg.iKey to the
1e1d0 72 6f 77 69 64 20 6f 66 20 74 68 65 0a 20 20 2a rowid of the. *
1e1d1 2a 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 * row being dele
1e1d2 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ted.. */. if(
1e1d3 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 db->xUpdateCallb
1e1d4 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a ack && pOp->p4.z
1e1d5 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1e1d6 75 2e 62 67 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bg.pC->isTable
1e1d7 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1e1d8 75 2e 62 67 2e 70 43 2d 3e 72 6f 77 69 64 49 73 u.bg.pC->rowidIs
1e1d9 56 61 6c 69 64 20 29 3b 20 20 2f 2a 20 6c 61 73 Valid ); /* las
1e1da 74 52 6f 77 69 64 20 73 65 74 20 62 79 20 70 72 tRowid set by pr
1e1db 65 76 69 6f 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 evious OP_NotFou
1e1dc 6e 64 20 2a 2f 0a 20 20 20 20 75 2e 62 67 2e 69 nd */. u.bg.i
1e1dd 4b 65 79 20 3d 20 75 2e 62 67 2e 70 43 2d 3e 6c Key = u.bg.pC->l
1e1de 61 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a 0a 20 astRowid;. }..
1e1df 20 2f 2a 20 54 68 65 20 4f 50 5f 44 65 6c 65 74 /* The OP_Delet
1e1e0 65 20 6f 70 63 6f 64 65 20 61 6c 77 61 79 73 20 e opcode always
1e1e1 66 6f 6c 6c 6f 77 73 20 61 6e 20 4f 50 5f 4e 6f follows an OP_No
1e1e2 74 45 78 69 73 74 73 20 6f 72 20 4f 50 5f 4c 61 tExists or OP_La
1e1e3 73 74 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f 43 6f st or. ** OP_Co
1e1e4 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 lumn on the same
1e1e5 20 74 61 62 6c 65 20 77 69 74 68 6f 75 74 20 61 table without a
1e1e6 6e 79 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 6f ny intervening o
1e1e7 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 0a 20 perations that.
1e1e8 20 2a 2a 20 6d 69 67 68 74 20 6d 6f 76 65 20 6f ** might move o
1e1e9 72 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 r invalidate the
1e1ea 20 63 75 72 73 6f 72 2e 20 20 48 65 6e 63 65 20 cursor. Hence
1e1eb 63 75 72 73 6f 72 20 75 2e 62 67 2e 70 43 20 69 cursor u.bg.pC i
1e1ec 73 20 61 6c 77 61 79 73 20 70 6f 69 6e 74 69 6e s always pointin
1e1ed 67 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 72 6f g. ** to the ro
1e1ee 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 w to be deleted
1e1ef 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 56 and the sqlite3V
1e1f0 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 dbeCursorMoveto(
1e1f1 29 20 6f 70 65 72 61 74 69 6f 6e 0a 20 20 2a 2a ) operation. **
1e1f2 20 62 65 6c 6f 77 20 69 73 20 61 6c 77 61 79 73 below is always
1e1f3 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 63 61 6e a no-op and can
1e1f4 6e 6f 74 20 66 61 69 6c 2e 20 20 57 65 20 77 69 not fail. We wi
1e1f5 6c 6c 20 72 75 6e 20 69 74 20 61 6e 79 68 6f 77 ll run it anyhow
1e1f6 2c 20 74 68 6f 75 67 68 2c 0a 20 20 2a 2a 20 74 , though,. ** t
1e1f7 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 o guard against
1e1f8 66 75 74 75 72 65 20 63 68 61 6e 67 65 73 20 74 future changes t
1e1f9 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 o the code gener
1e1fa 61 74 6f 72 2e 0a 20 20 2a 2a 2f 0a 20 20 61 73 ator.. **/. as
1e1fb 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 64 sert( u.bg.pC->d
1e1fc 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 eferredMoveto==0
1e1fd 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
1e1fe 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
1e1ff 74 6f 28 75 2e 62 67 2e 70 43 29 3b 0a 20 20 69 to(u.bg.pC);. i
1e200 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c f( NEVER(rc!=SQL
1e201 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 61 ITE_OK) ) goto a
1e202 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1e203 72 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 r;.. sqlite3Btr
1e204 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 eeSetCachedRowid
1e205 28 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f (u.bg.pC->pCurso
1e206 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 r, 0);. rc = sq
1e207 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 lite3BtreeDelete
1e208 28 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f (u.bg.pC->pCurso
1e209 72 29 3b 0a 20 20 75 2e 62 67 2e 70 43 2d 3e 63 r);. u.bg.pC->c
1e20a 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 acheStatus = CAC
1e20b 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20 HE_STALE;.. /*
1e20c 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 61 74 Invoke the updat
1e20d 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 69 72 e-hook if requir
1e20e 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d ed. */. if( rc=
1e20f 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 =SQLITE_OK && db
1e210 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 ->xUpdateCallbac
1e211 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 k && pOp->p4.z )
1e212 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 {. const char
1e213 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b *zDb = db->aDb[
1e214 75 2e 62 67 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e u.bg.pC->iDb].zN
1e215 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 ame;. const c
1e216 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d har *zTbl = pOp-
1e217 3e 70 34 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e 78 >p4.z;. db->x
1e218 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64 UpdateCallback(d
1e219 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 53 b->pUpdateArg, S
1e21a 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 44 QLITE_DELETE, zD
1e21b 62 2c 20 7a 54 62 6c 2c 20 75 2e 62 67 2e 69 4b b, zTbl, u.bg.iK
1e21c 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ey);. assert(
1e21d 20 75 2e 62 67 2e 70 43 2d 3e 69 44 62 3e 3d 30 u.bg.pC->iDb>=0
1e21e 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f );. }. if( pO
1e21f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47 5f 4e p->p2 & OPFLAG_N
1e220 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 CHANGE ) p->nCha
1e221 6e 67 65 2b 2b 3b 0a 20 20 62 72 65 61 6b 3b 0a nge++;. break;.
1e222 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 }./* Opcode: Res
1e223 65 74 43 6f 75 6e 74 20 2a 20 2a 20 2a 20 2a 20 etCount * * * *
1e224 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 *.**.** The valu
1e225 65 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 20 e of the change
1e226 63 6f 75 6e 74 65 72 20 69 73 20 63 6f 70 69 65 counter is copie
1e227 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 d to the databas
1e228 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 63 68 61 6e e handle.** chan
1e229 67 65 20 63 6f 75 6e 74 65 72 20 28 72 65 74 75 ge counter (retu
1e22a 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65 rned by subseque
1e22b 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 nt calls to sqli
1e22c 74 65 33 5f 63 68 61 6e 67 65 73 28 29 29 2e 0a te3_changes())..
1e22d 2a 2a 20 54 68 65 6e 20 74 68 65 20 56 4d 73 20 ** Then the VMs
1e22e 69 6e 74 65 72 6e 61 6c 20 63 68 61 6e 67 65 20 internal change
1e22f 63 6f 75 6e 74 65 72 20 72 65 73 65 74 73 20 74 counter resets t
1e230 6f 20 30 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 o 0..** This is
1e231 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20 used by trigger
1e232 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73 programs..*/.cas
1e233 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 3a e OP_ResetCount:
1e234 20 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 {. sqlite3Vdbe
1e235 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 SetChanges(db, p
1e236 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 70 2d ->nChange);. p-
1e237 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 >nChange = 0;.
1e238 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1e239 6f 64 65 3a 20 52 6f 77 44 61 74 61 20 50 31 20 ode: RowData P1
1e23a 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 P2 * * *.**.** W
1e23b 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 rite into regist
1e23c 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65 er P2 the comple
1e23d 74 65 20 72 6f 77 20 64 61 74 61 20 66 6f 72 20 te row data for
1e23e 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 cursor P1..** Th
1e23f 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 ere is no interp
1e240 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 retation of the
1e241 64 61 74 61 2e 20 20 0a 2a 2a 20 49 74 20 69 73 data. .** It is
1e242 20 6a 75 73 74 20 63 6f 70 69 65 64 20 6f 6e 74 just copied ont
1e243 6f 20 74 68 65 20 50 32 20 72 65 67 69 73 74 65 o the P2 registe
1e244 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a r exactly as .**
1e245 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 it is found in
1e246 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1e247 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
1e248 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 P1 cursor must b
1e249 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 e pointing to a
1e24a 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 valid row (not a
1e24b 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 NULL row).** of
1e24c 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e a real table, n
1e24d 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c ot a pseudo-tabl
1e24e 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a e..*/./* Opcode:
1e24f 20 52 6f 77 4b 65 79 20 50 31 20 50 32 20 2a 20 RowKey P1 P2 *
1e250 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
1e251 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 into register P2
1e252 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f the complete ro
1e253 77 20 6b 65 79 20 66 6f 72 20 63 75 72 73 6f 72 w key for cursor
1e254 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 P1..** There is
1e255 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 no interpretati
1e256 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 on of the data.
1e257 20 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 .** The key is
1e258 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 copied onto the
1e259 50 33 20 72 65 67 69 73 74 65 72 20 65 78 61 63 P3 register exac
1e25a 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 tly as .** it is
1e25b 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 found in the da
1e25c 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1e25d 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72 ** If the P1 cur
1e25e 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e sor must be poin
1e25f 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 ting to a valid
1e260 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 row (not a NULL
1e261 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 row).** of a rea
1e262 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 l table, not a p
1e263 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a seudo-table..*/.
1e264 63 61 73 65 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a case OP_RowKey:.
1e265 63 61 73 65 20 4f 50 5f 52 6f 77 44 61 74 61 3a case OP_RowData:
1e266 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1e267 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e268 65 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a ed into u.bh */.
1e269 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e26a 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 ;. BtCursor *pC
1e26b 72 73 72 3b 0a 20 20 75 33 32 20 6e 3b 0a 20 20 rsr;. u32 n;.
1e26c 69 36 34 20 6e 36 34 3b 0a 23 65 6e 64 69 66 20 i64 n64;.#endif
1e26d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e26e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e26f 62 68 20 2a 2f 0a 0a 20 20 70 4f 75 74 20 3d 20 bh */.. pOut =
1e270 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1e271 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 20 74 68 ];.. /* Note th
1e272 61 74 20 52 6f 77 4b 65 79 20 61 6e 64 20 52 6f at RowKey and Ro
1e273 77 44 61 74 61 20 61 72 65 20 72 65 61 6c 6c 79 wData are really
1e274 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d exactly the sam
1e275 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f e instruction */
1e276 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e277 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e278 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e279 20 75 2e 62 68 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bh.pC = p->ap
1e27a 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e27b 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d assert( u.bh.pC-
1e27c 3e 69 73 54 61 62 6c 65 20 7c 7c 20 70 4f 70 2d >isTable || pOp-
1e27d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b >opcode==OP_RowK
1e27e 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ey );. assert(
1e27f 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 u.bh.pC->isIndex
1e280 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d || pOp->opcode=
1e281 3d 4f 50 5f 52 6f 77 44 61 74 61 20 29 3b 0a 20 =OP_RowData );.
1e282 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 assert( u.bh.pC
1e283 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1e284 20 75 2e 62 68 2e 70 43 2d 3e 6e 75 6c 6c 52 6f u.bh.pC->nullRo
1e285 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 w==0 );. assert
1e286 28 20 75 2e 62 68 2e 70 43 2d 3e 70 73 65 75 64 ( u.bh.pC->pseud
1e287 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a oTableReg==0 );.
1e288 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 assert( u.bh.p
1e289 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b C->pCursor!=0 );
1e28a 0a 20 20 75 2e 62 68 2e 70 43 72 73 72 20 3d 20 . u.bh.pCrsr =
1e28b 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.bh.pC->pCursor
1e28c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1e28d 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 te3BtreeCursorIs
1e28e 56 61 6c 69 64 28 75 2e 62 68 2e 70 43 72 73 72 Valid(u.bh.pCrsr
1e28f 29 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 4f ) );.. /* The O
1e290 50 5f 52 6f 77 4b 65 79 20 61 6e 64 20 4f 50 5f P_RowKey and OP_
1e291 52 6f 77 44 61 74 61 20 6f 70 63 6f 64 65 73 20 RowData opcodes
1e292 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 20 4f 50 always follow OP
1e293 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 0a 20 20 _NotExists or.
1e294 2a 2a 20 4f 50 5f 52 65 77 69 6e 64 2f 4f 70 5f ** OP_Rewind/Op_
1e295 4e 65 78 74 20 77 69 74 68 20 6e 6f 20 69 6e 74 Next with no int
1e296 65 72 76 65 6e 69 6e 67 20 69 6e 73 74 72 75 63 ervening instruc
1e297 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 68 74 tions that might
1e298 20 69 6e 76 61 6c 69 64 61 74 65 0a 20 20 2a 2a invalidate. **
1e299 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 48 65 the cursor. He
1e29a 6e 63 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e nce the followin
1e29b 67 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 g sqlite3VdbeCur
1e29c 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c sorMoveto() call
1e29d 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 is always. **
1e29e 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 63 61 6e 20 a no-op and can
1e29f 6e 65 76 65 72 20 66 61 69 6c 2e 20 20 42 75 74 never fail. But
1e2a0 20 77 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 we leave it in
1e2a1 70 6c 61 63 65 20 61 73 20 61 20 73 61 66 65 74 place as a safet
1e2a2 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 y.. */. assert
1e2a3 28 20 75 2e 62 68 2e 70 43 2d 3e 64 65 66 65 72 ( u.bh.pC->defer
1e2a4 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a redMoveto==0 );.
1e2a5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1e2a6 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 beCursorMoveto(u
1e2a7 2e 62 68 2e 70 43 29 3b 0a 20 20 69 66 28 20 4e .bh.pC);. if( N
1e2a8 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f EVER(rc!=SQLITE_
1e2a9 4f 4b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 OK) ) goto abort
1e2aa 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a _due_to_error;..
1e2ab 20 20 69 66 28 20 75 2e 62 68 2e 70 43 2d 3e 69 if( u.bh.pC->i
1e2ac 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 61 73 sIndex ){. as
1e2ad 73 65 72 74 28 20 21 75 2e 62 68 2e 70 43 2d 3e sert( !u.bh.pC->
1e2ae 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 isTable );. r
1e2af 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e2b0 4b 65 79 53 69 7a 65 28 75 2e 62 68 2e 70 43 72 KeySize(u.bh.pCr
1e2b1 73 72 2c 20 26 75 2e 62 68 2e 6e 36 34 29 3b 0a sr, &u.bh.n64);.
1e2b2 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
1e2b3 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 SQLITE_OK );
1e2b4 2f 2a 20 54 72 75 65 20 62 65 63 61 75 73 65 20 /* True because
1e2b5 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 of CursorMoveto(
1e2b6 29 20 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a ) call above */.
1e2b7 20 20 20 20 69 66 28 20 75 2e 62 68 2e 6e 36 34 if( u.bh.n64
1e2b8 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
1e2b9 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
1e2ba 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 ){. goto t
1e2bb 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 oo_big;. }.
1e2bc 20 20 75 2e 62 68 2e 6e 20 3d 20 28 75 33 32 29 u.bh.n = (u32)
1e2bd 75 2e 62 68 2e 6e 36 34 3b 0a 20 20 7d 65 6c 73 u.bh.n64;. }els
1e2be 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 e{. rc = sqli
1e2bf 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 te3BtreeDataSize
1e2c0 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 2e (u.bh.pCrsr, &u.
1e2c1 62 68 2e 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 bh.n);. asser
1e2c2 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
1e2c3 20 29 3b 20 20 20 20 2f 2a 20 44 61 74 61 53 69 ); /* DataSi
1e2c4 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c ze() cannot fail
1e2c5 20 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 62 68 */. if( u.bh
1e2c6 2e 6e 3e 28 75 33 32 29 64 62 2d 3e 61 4c 69 6d .n>(u32)db->aLim
1e2c7 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
1e2c8 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 LENGTH] ){.
1e2c9 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
1e2ca 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 73 }. }. if( s
1e2cb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
1e2cc 77 28 70 4f 75 74 2c 20 75 2e 62 68 2e 6e 2c 20 w(pOut, u.bh.n,
1e2cd 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 0) ){. goto n
1e2ce 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 4f 75 o_mem;. }. pOu
1e2cf 74 2d 3e 6e 20 3d 20 75 2e 62 68 2e 6e 3b 0a 20 t->n = u.bh.n;.
1e2d0 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1e2d1 70 4f 75 74 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b pOut, MEM_Blob);
1e2d2 0a 20 20 69 66 28 20 75 2e 62 68 2e 70 43 2d 3e . if( u.bh.pC->
1e2d3 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 72 isIndex ){. r
1e2d4 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e2d5 4b 65 79 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 Key(u.bh.pCrsr,
1e2d6 30 2c 20 75 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 0, u.bh.n, pOut-
1e2d7 3e 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 >z);. }else{.
1e2d8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e2d9 72 65 65 44 61 74 61 28 75 2e 62 68 2e 70 43 72 reeData(u.bh.pCr
1e2da 73 72 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c 20 70 sr, 0, u.bh.n, p
1e2db 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20 20 70 Out->z);. }. p
1e2dc 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 Out->enc = SQLIT
1e2dd 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63 E_UTF8; /* In c
1e2de 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73 20 ase the blob is
1e2df 65 76 65 72 20 63 61 73 74 20 74 6f 20 74 65 78 ever cast to tex
1e2e0 74 20 2a 2f 0a 20 20 55 50 44 41 54 45 5f 4d 41 t */. UPDATE_MA
1e2e1 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 X_BLOBSIZE(pOut)
1e2e2 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1e2e3 20 4f 70 63 6f 64 65 3a 20 52 6f 77 69 64 20 50 Opcode: Rowid P
1e2e4 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1e2e5 20 53 74 6f 72 65 20 69 6e 20 72 65 67 69 73 74 Store in regist
1e2e6 65 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 er P2 an integer
1e2e7 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6b 65 which is the ke
1e2e8 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 y of the table e
1e2e9 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 50 31 20 ntry that.** P1
1e2ea 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
1e2eb 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 nt to..**.** P1
1e2ec 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 6e can be either an
1e2ed 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 ordinary table
1e2ee 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 or a virtual tab
1e2ef 6c 65 2e 20 20 54 68 65 72 65 20 75 73 65 64 20 le. There used
1e2f0 74 6f 0a 2a 2a 20 62 65 20 61 20 73 65 70 61 72 to.** be a separ
1e2f1 61 74 65 20 4f 50 5f 56 52 6f 77 69 64 20 6f 70 ate OP_VRowid op
1e2f2 63 6f 64 65 20 66 6f 72 20 75 73 65 20 77 69 74 code for use wit
1e2f3 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 h virtual tables
1e2f4 2c 20 62 75 74 20 74 68 69 73 0a 2a 2a 20 6f 6e , but this.** on
1e2f5 65 20 6f 70 63 6f 64 65 20 6e 6f 77 20 77 6f 72 e opcode now wor
1e2f6 6b 73 20 66 6f 72 20 62 6f 74 68 20 74 61 62 6c ks for both tabl
1e2f7 65 20 74 79 70 65 73 2e 0a 2a 2f 0a 63 61 73 65 e types..*/.case
1e2f8 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 20 20 20 OP_Rowid: {
1e2f9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e2fa 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1e2fb 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1e2fc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e2fd 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a 2f 0a ed into u.bi */.
1e2fe 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e2ff 3b 0a 20 20 69 36 34 20 76 3b 0a 20 20 73 71 6c ;. i64 v;. sql
1e300 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
1e301 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ;. const sqlite
1e302 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
1e303 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 e;.#endif /* loc
1e304 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e305 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a 2f 0a ed into u.bi */.
1e306 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e307 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e308 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e309 20 75 2e 62 69 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bi.pC = p->ap
1e30a 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e30b 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 21 assert( u.bi.pC!
1e30c 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e30d 75 2e 62 69 2e 70 43 2d 3e 70 73 65 75 64 6f 54 u.bi.pC->pseudoT
1e30e 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 ableReg==0 );.
1e30f 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 6e 75 6c if( u.bi.pC->nul
1e310 6c 52 6f 77 20 29 7b 0a 20 20 20 20 2f 2a 20 44 lRow ){. /* D
1e311 6f 20 6e 6f 74 68 69 6e 67 20 73 6f 20 74 68 61 o nothing so tha
1e312 74 20 72 65 67 5b 50 32 5d 20 72 65 6d 61 69 6e t reg[P2] remain
1e313 73 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 62 72 s NULL */. br
1e314 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 eak;. }else if(
1e315 20 75 2e 62 69 2e 70 43 2d 3e 64 65 66 65 72 72 u.bi.pC->deferr
1e316 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 20 20 edMoveto ){.
1e317 75 2e 62 69 2e 76 20 3d 20 75 2e 62 69 2e 70 43 u.bi.v = u.bi.pC
1e318 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 0a ->movetoTarget;.
1e319 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1e31a 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1e31b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 . }else if( u.b
1e31c 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f i.pC->pVtabCurso
1e31d 72 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 70 56 r ){. u.bi.pV
1e31e 74 61 62 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 70 tab = u.bi.pC->p
1e31f 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 VtabCursor->pVta
1e320 62 3b 0a 20 20 20 20 75 2e 62 69 2e 70 4d 6f 64 b;. u.bi.pMod
1e321 75 6c 65 20 3d 20 75 2e 62 69 2e 70 56 74 61 62 ule = u.bi.pVtab
1e322 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 61 ->pModule;. a
1e323 73 73 65 72 74 28 20 75 2e 62 69 2e 70 4d 6f 64 ssert( u.bi.pMod
1e324 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b 0a 20 ule->xRowid );.
1e325 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1e326 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1e327 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1e328 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d misuse;. rc =
1e329 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 u.bi.pModule->x
1e32a 52 6f 77 69 64 28 75 2e 62 69 2e 70 43 2d 3e 70 Rowid(u.bi.pC->p
1e32b 56 74 61 62 43 75 72 73 6f 72 2c 20 26 75 2e 62 VtabCursor, &u.b
1e32c 69 2e 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 i.v);. sqlite
1e32d 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1e32e 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e ErrMsg);. p->
1e32f 7a 45 72 72 4d 73 67 20 3d 20 75 2e 62 69 2e 70 zErrMsg = u.bi.p
1e330 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 Vtab->zErrMsg;.
1e331 20 20 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 7a u.bi.pVtab->z
1e332 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 ErrMsg = 0;.
1e333 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1e334 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 yOn(db) ) goto a
1e335 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1e336 73 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 se;.#endif /* SQ
1e337 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1e338 4c 54 41 42 4c 45 20 2a 2f 0a 20 20 7d 65 6c 73 LTABLE */. }els
1e339 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 e{. assert( u
1e33a 2e 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 .bi.pC->pCursor!
1e33b 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 =0 );. rc = s
1e33c 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
1e33d 4d 6f 76 65 74 6f 28 75 2e 62 69 2e 70 43 29 3b Moveto(u.bi.pC);
1e33e 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
1e33f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1e340 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75 error;. if( u
1e341 2e 62 69 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .bi.pC->rowidIsV
1e342 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 75 2e alid ){. u.
1e343 62 69 2e 76 20 3d 20 75 2e 62 69 2e 70 43 2d 3e bi.v = u.bi.pC->
1e344 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 7d lastRowid;. }
1e345 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
1e346 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
1e347 53 69 7a 65 28 75 2e 62 69 2e 70 43 2d 3e 70 43 Size(u.bi.pC->pC
1e348 75 72 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b ursor, &u.bi.v);
1e349 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
1e34a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 c==SQLITE_OK );
1e34b 20 2f 2a 20 41 6c 77 61 79 73 20 73 6f 20 62 65 /* Always so be
1e34c 63 61 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d cause of CursorM
1e34d 6f 76 65 74 6f 28 29 20 61 62 6f 76 65 20 2a 2f oveto() above */
1e34e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 75 . }. }. pOu
1e34f 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 69 2e 76 3b t->u.i = u.bi.v;
1e350 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1e351 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1e352 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1e353 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 52 6f 77 Opcode: NullRow
1e354 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 * * * *.**.*
1e355 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
1e356 72 20 50 31 20 74 6f 20 61 20 6e 75 6c 6c 20 72 r P1 to a null r
1e357 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 6f 6c 75 ow. Any OP_Colu
1e358 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a mn operations.**
1e359 20 74 68 61 74 20 6f 63 63 75 72 20 77 68 69 6c that occur whil
1e35a 65 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 e the cursor is
1e35b 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 6f 77 20 on the null row
1e35c 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 77 will always.** w
1e35d 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a 2f 0a rite a NULL..*/.
1e35e 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 3a case OP_NullRow:
1e35f 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1e360 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e361 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a ed into u.bj */.
1e362 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e363 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e364 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e365 64 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a 0a d into u.bj */..
1e366 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e367 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e368 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e369 75 2e 62 6a 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.bj.pC = p->apC
1e36a 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1e36b 73 73 65 72 74 28 20 75 2e 62 6a 2e 70 43 21 3d ssert( u.bj.pC!=
1e36c 30 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 43 2d 3e 0 );. u.bj.pC->
1e36d 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75 nullRow = 1;. u
1e36e 2e 62 6a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .bj.pC->rowidIsV
1e36f 61 6c 69 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 alid = 0;. if(
1e370 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.bj.pC->pCursor
1e371 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
1e372 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 treeClearCursor(
1e373 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.bj.pC->pCursor
1e374 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1e375 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 61 }../* Opcode: La
1e376 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a st P1 P2 * * *.*
1e377 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 *.** The next us
1e378 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f e of the Rowid o
1e379 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 r Column or Next
1e37a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 instruction for
1e37b 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 P1 .** will ref
1e37c 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 er to the last e
1e37d 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 ntry in the data
1e37e 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e base table or in
1e37f 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 dex..** If the t
1e380 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 able or index is
1e381 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c empty and P2>0,
1e382 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 then jump immed
1e383 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a iately to P2..**
1e384 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 If P2 is 0 or i
1e385 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 f the table or i
1e386 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 ndex is not empt
1e387 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a y, fall through.
1e388 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 ** to the follow
1e389 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ing instruction.
1e38a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 73 74 .*/.case OP_Last
1e38b 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1e38c 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 mp */.#if 0 /*
1e38d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e38e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20 moved into u.bk
1e38f 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 */. VdbeCursor
1e390 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 *pC;. BtCursor
1e391 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 *pCrsr;. int re
1e392 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 s;.#endif /* loc
1e393 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e394 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a ed into u.bk */.
1e395 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e396 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e397 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e398 20 75 2e 62 6b 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bk.pC = p->ap
1e399 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e39a 61 73 73 65 72 74 28 20 75 2e 62 6b 2e 70 43 21 assert( u.bk.pC!
1e39b 3d 30 20 29 3b 0a 20 20 75 2e 62 6b 2e 70 43 72 =0 );. u.bk.pCr
1e39c 73 72 20 3d 20 75 2e 62 6b 2e 70 43 2d 3e 70 43 sr = u.bk.pC->pC
1e39d 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62 ursor;. if( u.b
1e39e 6b 2e 70 43 72 73 72 3d 3d 30 20 29 7b 0a 20 20 k.pCrsr==0 ){.
1e39f 20 20 75 2e 62 6b 2e 72 65 73 20 3d 20 31 3b 0a u.bk.res = 1;.
1e3a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 }else{. rc
1e3a1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 = sqlite3BtreeLa
1e3a2 73 74 28 75 2e 62 6b 2e 70 43 72 73 72 2c 20 26 st(u.bk.pCrsr, &
1e3a3 75 2e 62 6b 2e 72 65 73 29 3b 0a 20 20 7d 0a 20 u.bk.res);. }.
1e3a4 20 75 2e 62 6b 2e 70 43 2d 3e 6e 75 6c 6c 52 6f u.bk.pC->nullRo
1e3a5 77 20 3d 20 28 75 38 29 75 2e 62 6b 2e 72 65 73 w = (u8)u.bk.res
1e3a6 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 64 65 66 ;. u.bk.pC->def
1e3a7 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b erredMoveto = 0;
1e3a8 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 72 6f 77 69 . u.bk.pC->rowi
1e3a9 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1e3aa 75 2e 62 6b 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.bk.pC->cacheSt
1e3ab 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1e3ac 4c 45 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 LE;. if( pOp->p
1e3ad 32 3e 30 20 26 26 20 75 2e 62 6b 2e 72 65 73 20 2>0 && u.bk.res
1e3ae 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1e3af 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1e3b0 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 reak;.}.../* Opc
1e3b1 6f 64 65 3a 20 53 6f 72 74 20 50 31 20 50 32 20 ode: Sort P1 P2
1e3b2 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 * * *.**.** This
1e3b3 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 65 78 61 opcode does exa
1e3b4 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 74 68 ctly the same th
1e3b5 69 6e 67 20 61 73 20 4f 50 5f 52 65 77 69 6e 64 ing as OP_Rewind
1e3b6 20 65 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 except that.**
1e3b7 69 74 20 69 6e 63 72 65 6d 65 6e 74 73 20 61 6e it increments an
1e3b8 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 67 6c undocumented gl
1e3b9 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 75 73 obal variable us
1e3ba 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a ed for testing..
1e3bb 2a 2a 0a 2a 2a 20 53 6f 72 74 69 6e 67 20 69 73 **.** Sorting is
1e3bc 20 61 63 63 6f 6d 70 6c 69 73 68 65 64 20 62 79 accomplished by
1e3bd 20 77 72 69 74 69 6e 67 20 72 65 63 6f 72 64 73 writing records
1e3be 20 69 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 into a sorting
1e3bf 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 6e 20 72 index,.** then r
1e3c0 65 77 69 6e 64 69 6e 67 20 74 68 61 74 20 69 6e ewinding that in
1e3c1 64 65 78 20 61 6e 64 20 70 6c 61 79 69 6e 67 20 dex and playing
1e3c2 69 74 20 62 61 63 6b 20 66 72 6f 6d 20 62 65 67 it back from beg
1e3c3 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65 6e 64 inning to.** end
1e3c4 2e 20 20 57 65 20 75 73 65 20 74 68 65 20 4f 50 . We use the OP
1e3c5 5f 53 6f 72 74 20 6f 70 63 6f 64 65 20 69 6e 73 _Sort opcode ins
1e3c6 74 65 61 64 20 6f 66 20 4f 50 5f 52 65 77 69 6e tead of OP_Rewin
1e3c7 64 20 74 6f 20 64 6f 20 74 68 65 0a 2a 2a 20 72 d to do the.** r
1e3c8 65 77 69 6e 64 69 6e 67 20 73 6f 20 74 68 61 74 ewinding so that
1e3c9 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 the global vari
1e3ca 61 62 6c 65 20 77 69 6c 6c 20 62 65 20 69 6e 63 able will be inc
1e3cb 72 65 6d 65 6e 74 65 64 20 61 6e 64 0a 2a 2a 20 remented and.**
1e3cc 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73 74 73 regression tests
1e3cd 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65 20 77 can determine w
1e3ce 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
1e3cf 65 20 6f 70 74 69 6d 69 7a 65 72 20 69 73 0a 2a e optimizer is.*
1e3d0 2a 20 63 6f 72 72 65 63 74 6c 79 20 6f 70 74 69 * correctly opti
1e3d1 6d 69 7a 69 6e 67 20 6f 75 74 20 73 6f 72 74 73 mizing out sorts
1e3d2 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f 72 ..*/.case OP_Sor
1e3d3 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a t: { /* j
1e3d4 75 6d 70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 ump */.#ifdef SQ
1e3d5 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 LITE_TEST. sqli
1e3d6 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 2b 2b te3_sort_count++
1e3d7 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 ;. sqlite3_sear
1e3d8 63 68 5f 63 6f 75 6e 74 2d 2d 3b 0a 23 65 6e 64 ch_count--;.#end
1e3d9 69 66 0a 20 20 70 2d 3e 61 43 6f 75 6e 74 65 72 if. p->aCounter
1e3da 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 [SQLITE_STMTSTAT
1e3db 55 53 5f 53 4f 52 54 2d 31 5d 2b 2b 3b 0a 20 20 US_SORT-1]++;.
1e3dc 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 /* Fall through
1e3dd 69 6e 74 6f 20 4f 50 5f 52 65 77 69 6e 64 20 2a into OP_Rewind *
1e3de 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 /.}./* Opcode: R
1e3df 65 77 69 6e 64 20 50 31 20 50 32 20 2a 20 2a 20 ewind P1 P2 * *
1e3e0 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 *.**.** The next
1e3e1 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 use of the Rowi
1e3e2 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e d or Column or N
1e3e3 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1e3e4 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 for P1 .** will
1e3e5 72 65 66 65 72 20 74 6f 20 74 68 65 20 66 69 72 refer to the fir
1e3e6 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
1e3e7 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f database table o
1e3e8 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 r index..** If t
1e3e9 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 he table or inde
1e3ea 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 x is empty and P
1e3eb 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 2>0, then jump i
1e3ec 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 mmediately to P2
1e3ed 2e 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 ..** If P2 is 0
1e3ee 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 or if the table
1e3ef 6f 72 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 or index is not
1e3f0 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f empty, fall thro
1e3f1 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f ugh.** to the fo
1e3f2 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 llowing instruct
1e3f3 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ion..*/.case OP_
1e3f4 52 65 77 69 6e 64 3a 20 7b 20 20 20 20 20 20 20 Rewind: {
1e3f5 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 /* jump */.#if
1e3f6 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e3f7 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e3f8 20 75 2e 62 6c 20 2a 2f 0a 20 20 56 64 62 65 43 u.bl */. VdbeC
1e3f9 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 ursor *pC;. BtC
1e3fa 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1e3fb 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 int res;.#endif
1e3fc 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e3fd 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e3fe 62 6c 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 bl */.. assert(
1e3ff 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1e400 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f Op->p1<p->nCurso
1e401 72 20 29 3b 0a 20 20 75 2e 62 6c 2e 70 43 20 3d r );. u.bl.pC =
1e402 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 p->apCsr[pOp->p
1e403 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 1];. assert( u.
1e404 62 6c 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 bl.pC!=0 );. if
1e405 28 20 28 75 2e 62 6c 2e 70 43 72 73 72 20 3d 20 ( (u.bl.pCrsr =
1e406 75 2e 62 6c 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.bl.pC->pCursor
1e407 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d )!=0 ){. rc =
1e408 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 sqlite3BtreeFir
1e409 73 74 28 75 2e 62 6c 2e 70 43 72 73 72 2c 20 26 st(u.bl.pCrsr, &
1e40a 75 2e 62 6c 2e 72 65 73 29 3b 0a 20 20 20 20 75 u.bl.res);. u
1e40b 2e 62 6c 2e 70 43 2d 3e 61 74 46 69 72 73 74 20 .bl.pC->atFirst
1e40c 3d 20 75 2e 62 6c 2e 72 65 73 3d 3d 30 20 3f 31 = u.bl.res==0 ?1
1e40d 3a 30 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d :0;. u.bl.pC-
1e40e 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1e40f 3d 20 30 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 = 0;. u.bl.pC
1e410 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1e411 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 CACHE_STALE;.
1e412 20 75 2e 62 6c 2e 70 43 2d 3e 72 6f 77 69 64 49 u.bl.pC->rowidI
1e413 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 65 sValid = 0;. }e
1e414 6c 73 65 7b 0a 20 20 20 20 75 2e 62 6c 2e 72 65 lse{. u.bl.re
1e415 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 75 2e 62 s = 1;. }. u.b
1e416 6c 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 l.pC->nullRow =
1e417 28 75 38 29 75 2e 62 6c 2e 72 65 73 3b 0a 20 20 (u8)u.bl.res;.
1e418 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e assert( pOp->p2>
1e419 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e 0 && pOp->p2<p->
1e41a 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 nOp );. if( u.b
1e41b 6c 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 l.res ){. pc
1e41c 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1e41d 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1e41e 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20 50 * Opcode: Next P
1e41f 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1e420 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f 72 20 Advance cursor
1e421 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f P1 so that it po
1e422 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74 ints to the next
1e423 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 key/data pair i
1e424 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f n its.** table o
1e425 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 r index. If the
1e426 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6b re are no more k
1e427 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74 ey/value pairs t
1e428 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 hen fall through
1e429 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f .** to the follo
1e42a 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e wing instruction
1e42b 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 75 . But if the cu
1e42c 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77 61 73 rsor advance was
1e42d 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 successful,.**
1e42e 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 jump immediately
1e42f 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 to P2..**.** Th
1e430 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 e P1 cursor must
1e431 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74 be for a real t
1e432 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 able, not a pseu
1e433 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 do-table..**.**
1e434 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a 2a See also: Prev.*
1e435 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 65 /./* Opcode: Pre
1e436 76 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a v P1 P2 * * *.**
1e437 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75 72 73 .** Back up curs
1e438 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 or P1 so that it
1e439 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 points to the p
1e43a 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61 74 61 revious key/data
1e43b 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20 pair in its.**
1e43c 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20 table or index.
1e43d 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
1e43e 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76 61 6c previous key/val
1e43f 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 ue pairs then fa
1e440 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f ll through.** to
1e441 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
1e442 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 nstruction. But
1e443 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 62 if the cursor b
1e444 61 63 6b 75 70 20 77 61 73 20 73 75 63 63 65 73 ackup was succes
1e445 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d sful,.** jump im
1e446 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e mediately to P2.
1e447 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 .**.** The P1 cu
1e448 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 rsor must be for
1e449 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e a real table, n
1e44a 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c ot a pseudo-tabl
1e44b 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72 e..*/.case OP_Pr
1e44c 65 76 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ev: /*
1e44d 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 5f jump */.case OP_
1e44e 4e 65 78 74 3a 20 7b 20 20 20 20 20 20 20 20 2f Next: { /
1e44f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 * jump */.#if 0
1e450 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e451 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e452 2e 62 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .bm */. VdbeCur
1e453 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 sor *pC;. BtCur
1e454 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e sor *pCrsr;. in
1e455 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a t res;.#endif /*
1e456 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e457 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6d moved into u.bm
1e458 20 2a 2f 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52 */.. CHECK_FOR
1e459 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 61 73 _INTERRUPT;. as
1e45a 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e45b 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e45c 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6d Cursor );. u.bm
1e45d 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e45e 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 75 Op->p1];. if( u
1e45f 2e 62 6d 2e 70 43 3d 3d 30 20 29 7b 0a 20 20 20 .bm.pC==0 ){.
1e460 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 65 65 20 break; /* See
1e461 74 69 63 6b 65 74 20 23 32 32 37 33 20 2a 2f 0a ticket #2273 */.
1e462 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 72 73 72 }. u.bm.pCrsr
1e463 20 3d 20 75 2e 62 6d 2e 70 43 2d 3e 70 43 75 72 = u.bm.pC->pCur
1e464 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e sor;. if( u.bm.
1e465 70 43 72 73 72 3d 3d 30 20 29 7b 0a 20 20 20 20 pCrsr==0 ){.
1e466 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 u.bm.pC->nullRow
1e467 20 3d 20 31 3b 0a 20 20 20 20 62 72 65 61 6b 3b = 1;. break;
1e468 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 72 65 73 20 . }. u.bm.res
1e469 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 75 = 1;. assert( u
1e46a 2e 62 6d 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 .bm.pC->deferred
1e46b 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 Moveto==0 );. r
1e46c 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d c = pOp->opcode=
1e46d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c 69 74 =OP_Next ? sqlit
1e46e 65 33 42 74 72 65 65 4e 65 78 74 28 75 2e 62 6d e3BtreeNext(u.bm
1e46f 2e 70 43 72 73 72 2c 20 26 75 2e 62 6d 2e 72 65 .pCrsr, &u.bm.re
1e470 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20 s) :.
1e471 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e472 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 sqlite3BtreeP
1e473 72 65 76 69 6f 75 73 28 75 2e 62 6d 2e 70 43 72 revious(u.bm.pCr
1e474 73 72 2c 20 26 75 2e 62 6d 2e 72 65 73 29 3b 0a sr, &u.bm.res);.
1e475 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 u.bm.pC->nullR
1e476 6f 77 20 3d 20 28 75 38 29 75 2e 62 6d 2e 72 65 ow = (u8)u.bm.re
1e477 73 3b 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 63 61 s;. u.bm.pC->ca
1e478 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 cheStatus = CACH
1e479 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 75 E_STALE;. if( u
1e47a 2e 62 6d 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 .bm.res==0 ){.
1e47b 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1e47c 20 31 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 1;. if( pOp-
1e47d 3e 70 35 20 29 20 70 2d 3e 61 43 6f 75 6e 74 65 >p5 ) p->aCounte
1e47e 72 5b 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a r[pOp->p5-1]++;.
1e47f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1e480 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ST. sqlite3_s
1e481 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 earch_count++;.#
1e482 65 6e 64 69 66 0a 20 20 7d 0a 20 20 75 2e 62 6d endif. }. u.bm
1e483 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 .pC->rowidIsVali
1e484 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a d = 0;. break;.
1e485 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 }../* Opcode: Id
1e486 78 49 6e 73 65 72 74 20 50 31 20 50 32 20 50 33 xInsert P1 P2 P3
1e487 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65 67 69 * P5.**.** Regi
1e488 73 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61 20 ster P2 holds a
1e489 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61 SQL index key ma
1e48a 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 de using the.**
1e48b 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72 MakeRecord instr
1e48c 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f uctions. This o
1e48d 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 68 61 pcode writes tha
1e48e 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68 t key.** into th
1e48f 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74 e index P1. Dat
1e490 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 a for the entry
1e491 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33 is nil..**.** P3
1e492 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74 20 is a flag that
1e493 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20 provides a hint
1e494 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 to the b-tree la
1e495 79 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a yer that this.**
1e496 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c insert is likel
1e497 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e y to be an appen
1e498 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e d..**.** This in
1e499 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 struction only w
1e49a 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73 orks for indices
1e49b 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e . The equivalen
1e49c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a t instruction.**
1e49d 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f for tables is O
1e49e 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 P_Insert..*/.cas
1e49f 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20 e OP_IdxInsert:
1e4a0 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 { /* in2
1e4a1 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1e4a2 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e4a3 65 64 20 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a ed into u.bn */.
1e4a4 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e4a5 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 ;. BtCursor *pC
1e4a6 72 73 72 3b 0a 20 20 69 6e 74 20 6e 4b 65 79 3b rsr;. int nKey;
1e4a7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1e4a8 4b 65 79 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c Key;.#endif /* l
1e4a9 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e4aa 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6e 20 2a oved into u.bn *
1e4ab 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 /.. assert( pOp
1e4ac 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e4ad 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e4ae 0a 20 20 75 2e 62 6e 2e 70 43 20 3d 20 70 2d 3e . u.bn.pC = p->
1e4af 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1e4b0 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 assert( u.bn.p
1e4b1 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 C!=0 );. assert
1e4b2 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 ( pIn2->flags &
1e4b3 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 75 2e MEM_Blob );. u.
1e4b4 62 6e 2e 70 43 72 73 72 20 3d 20 75 2e 62 6e 2e bn.pCrsr = u.bn.
1e4b5 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 pC->pCursor;. i
1e4b6 66 28 20 41 4c 57 41 59 53 28 75 2e 62 6e 2e 70 f( ALWAYS(u.bn.p
1e4b7 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 Crsr!=0) ){.
1e4b8 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 43 2d assert( u.bn.pC-
1e4b9 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 >isTable==0 );.
1e4ba 20 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c rc = ExpandBl
1e4bb 6f 62 28 70 49 6e 32 29 3b 0a 20 20 20 20 69 66 ob(pIn2);. if
1e4bc 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1e4bd 29 7b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 6e 4b ){. u.bn.nK
1e4be 65 79 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 ey = pIn2->n;.
1e4bf 20 20 20 20 75 2e 62 6e 2e 7a 4b 65 79 20 3d 20 u.bn.zKey =
1e4c0 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20 20 20 20 72 pIn2->z;. r
1e4c1 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e4c2 49 6e 73 65 72 74 28 75 2e 62 6e 2e 70 43 72 73 Insert(u.bn.pCrs
1e4c3 72 2c 20 75 2e 62 6e 2e 7a 4b 65 79 2c 20 75 2e r, u.bn.zKey, u.
1e4c4 62 6e 2e 6e 4b 65 79 2c 20 22 22 2c 20 30 2c 20 bn.nKey, "", 0,
1e4c5 30 2c 20 70 4f 70 2d 3e 70 33 2c 0a 20 20 20 20 0, pOp->p3,.
1e4c6 20 20 20 20 20 20 28 28 70 4f 70 2d 3e 70 35 20 ((pOp->p5
1e4c7 26 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b & OPFLAG_USESEEK
1e4c8 52 45 53 55 4c 54 29 20 3f 20 75 2e 62 6e 2e 70 RESULT) ? u.bn.p
1e4c9 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3a 20 C->seekResult :
1e4ca 30 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 0). );.
1e4cb 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 assert( u.bn.p
1e4cc 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e4cd 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 75 2e o==0 );. u.
1e4ce 62 6e 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 bn.pC->cacheStat
1e4cf 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e4d0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 ;. }. }. br
1e4d1 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e4d2 65 3a 20 49 64 78 44 65 6c 65 74 65 20 50 31 20 e: IdxDelete P1
1e4d3 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1e4d4 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 50 The content of P
1e4d5 33 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 3 registers star
1e4d6 74 69 6e 67 20 61 74 20 72 65 67 69 73 74 65 72 ting at register
1e4d7 20 50 32 20 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75 P2 form.** an u
1e4d8 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 npacked index ke
1e4d9 79 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 y. This opcode r
1e4da 65 6d 6f 76 65 73 20 74 68 61 74 20 65 6e 74 72 emoves that entr
1e4db 79 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 69 y from the .** i
1e4dc 6e 64 65 78 20 6f 70 65 6e 65 64 20 62 79 20 63 ndex opened by c
1e4dd 75 72 73 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 ursor P1..*/.cas
1e4de 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 3a 20 e OP_IdxDelete:
1e4df 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1e4e0 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e4e1 64 20 69 6e 74 6f 20 75 2e 62 6f 20 2a 2f 0a 20 d into u.bo */.
1e4e2 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e4e3 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 . BtCursor *pCr
1e4e4 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 sr;. int res;.
1e4e5 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1e4e6 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 r;.#endif /* loc
1e4e7 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e4e8 65 64 20 69 6e 74 6f 20 75 2e 62 6f 20 2a 2f 0a ed into u.bo */.
1e4e9 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e4ea 70 33 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 p3>0 );. assert
1e4eb 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 ( pOp->p2>0 && p
1e4ec 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3c 3d Op->p2+pOp->p3<=
1e4ed 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 61 p->nMem+1 );. a
1e4ee 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1e4ef 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 0 && pOp->p1<p->
1e4f0 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 nCursor );. u.b
1e4f1 6f 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b o.pC = p->apCsr[
1e4f2 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1e4f3 72 74 28 20 75 2e 62 6f 2e 70 43 21 3d 30 20 29 rt( u.bo.pC!=0 )
1e4f4 3b 0a 20 20 75 2e 62 6f 2e 70 43 72 73 72 20 3d ;. u.bo.pCrsr =
1e4f5 20 75 2e 62 6f 2e 70 43 2d 3e 70 43 75 72 73 6f u.bo.pC->pCurso
1e4f6 72 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 r;. if( ALWAYS(
1e4f7 75 2e 62 6f 2e 70 43 72 73 72 21 3d 30 29 20 29 u.bo.pCrsr!=0) )
1e4f8 7b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 70 4b 65 {. u.bo.r.pKe
1e4f9 79 49 6e 66 6f 20 3d 20 75 2e 62 6f 2e 70 43 2d yInfo = u.bo.pC-
1e4fa 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 >pKeyInfo;. u
1e4fb 2e 62 6f 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 .bo.r.nField = (
1e4fc 75 31 36 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 u16)pOp->p3;.
1e4fd 20 75 2e 62 6f 2e 72 2e 66 6c 61 67 73 20 3d 20 u.bo.r.flags =
1e4fe 30 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 61 4d 0;. u.bo.r.aM
1e4ff 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f em = &p->aMem[pO
1e500 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 72 63 20 3d p->p2];. rc =
1e501 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1e502 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 6f etoUnpacked(u.bo
1e503 2e 70 43 72 73 72 2c 20 26 75 2e 62 6f 2e 72 2c .pCrsr, &u.bo.r,
1e504 20 30 2c 20 30 2c 20 26 75 2e 62 6f 2e 72 65 73 0, 0, &u.bo.res
1e505 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
1e506 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 6f QLITE_OK && u.bo
1e507 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 .res==0 ){.
1e508 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e509 65 65 44 65 6c 65 74 65 28 75 2e 62 6f 2e 70 43 eeDelete(u.bo.pC
1e50a 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rsr);. }.
1e50b 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43 2d assert( u.bo.pC-
1e50c 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d >deferredMoveto=
1e50d 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 6f 2e 70 =0 );. u.bo.p
1e50e 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1e50f 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 CACHE_STALE;.
1e510 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1e511 20 4f 70 63 6f 64 65 3a 20 49 64 78 52 6f 77 69 Opcode: IdxRowi
1e512 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a d P1 P2 * * *.**
1e513 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 .** Write into r
1e514 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e egister P2 an in
1e515 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
1e516 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e he last entry in
1e517 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a 2a the record at.*
1e518 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 * the end of the
1e519 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e 74 index key point
1e51a 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72 20 ed to by cursor
1e51b 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67 65 P1. This intege
1e51c 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 74 r should be.** t
1e51d 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 he rowid of the
1e51e 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20 77 table entry to w
1e51f 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78 20 hich this index
1e520 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a 2a entry points..**
1e521 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52 6f .** See also: Ro
1e522 77 69 64 2c 20 4d 61 6b 65 52 65 63 6f 72 64 2e wid, MakeRecord.
1e523 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 52 .*/.case OP_IdxR
1e524 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 owid: {
1e525 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1e526 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1e527 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e528 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e529 75 2e 62 70 20 2a 2f 0a 20 20 42 74 43 75 72 73 u.bp */. BtCurs
1e52a 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 62 or *pCrsr;. Vdb
1e52b 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 eCursor *pC;. i
1e52c 36 34 20 72 6f 77 69 64 3b 0a 23 65 6e 64 69 66 64 rowid;.#endif
1e52d 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e52e 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e52f 2e 62 70 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .bp */.. assert
1e530 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e531 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e532 6f 72 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 20 or );. u.bp.pC
1e533 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e534 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e535 2e 62 70 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 .bp.pC!=0 );. u
1e536 2e 62 70 2e 70 43 72 73 72 20 3d 20 75 2e 62 70 .bp.pCrsr = u.bp
1e537 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 .pC->pCursor;.
1e538 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 70 2e if( ALWAYS(u.bp.
1e539 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 pCrsr!=0) ){.
1e53a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1e53b 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e eCursorMoveto(u.
1e53c 62 70 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20 bp.pC);. if(
1e53d 4e 45 56 45 52 28 72 63 29 20 29 20 67 6f 74 6f NEVER(rc) ) goto
1e53e 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1e53f 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ror;. assert(
1e540 20 75 2e 62 70 2e 70 43 2d 3e 64 65 66 65 72 72 u.bp.pC->deferr
1e541 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 edMoveto==0 );.
1e542 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e assert( u.bp.
1e543 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 pC->isTable==0 )
1e544 3b 0a 20 20 20 20 69 66 28 20 21 75 2e 62 70 2e ;. if( !u.bp.
1e545 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 pC->nullRow ){.
1e546 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1e547 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 64 62 3VdbeIdxRowid(db
1e548 2c 20 75 2e 62 70 2e 70 43 72 73 72 2c 20 26 75 , u.bp.pCrsr, &u
1e549 2e 62 70 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20 .bp.rowid);.
1e54a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1e54b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 _OK ){. g
1e54c 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1e54d 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a _error;. }.
1e54e 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 MemSetType
1e54f 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1e550 6e 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d nt);. pOut-
1e551 3e 75 2e 69 20 3d 20 75 2e 62 70 2e 72 6f 77 69 >u.i = u.bp.rowi
1e552 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 d;. }. }. b
1e553 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1e554 64 65 3a 20 49 64 78 47 45 20 50 31 20 50 32 20 de: IdxGE P1 P2
1e555 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 P3 P4 P5.**.** T
1e556 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20 76 he P4 register v
1e557 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 alues beginning
1e558 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 with P3 form an
1e559 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a unpacked index .
1e55a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 ** key that omit
1e55b 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f s the ROWID. Co
1e55c 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 mpare this key v
1e55d 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 alue against the
1e55e 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 index .** that
1e55f 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 P1 is currently
1e560 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e pointing to, ign
1e561 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 oring the ROWID
1e562 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e on the P1 index.
1e563 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 .**.** If the P1
1e564 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 index entry is
1e565 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
1e566 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 equal to the key
1e567 20 76 61 6c 75 65 0a 2a 2a 20 74 68 65 6e 20 6a value.** then j
1e568 75 6d 70 20 74 6f 20 50 32 2e 20 20 4f 74 68 65 ump to P2. Othe
1e569 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 rwise fall throu
1e56a 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 gh to the next i
1e56b 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a nstruction..**.*
1e56c 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a * If P5 is non-z
1e56d 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 ero then the key
1e56e 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 value is increa
1e56f 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f sed by an epsilo
1e570 6e 20 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74 n .** prior to t
1e571 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 he comparison.
1e572 54 68 69 73 20 6d 61 6b 65 20 74 68 65 20 6f 70 This make the op
1e573 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 code work like I
1e574 64 78 47 54 20 65 78 63 65 70 74 0a 2a 2a 20 74 dxGT except.** t
1e575 68 61 74 20 69 66 20 74 68 65 20 6b 65 79 20 66 hat if the key f
1e576 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 20 rom register P3
1e577 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 74 is a prefix of t
1e578 68 65 20 6b 65 79 20 69 6e 20 74 68 65 20 63 75 he key in the cu
1e579 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 20 72 65 73 rsor,.** the res
1e57a 75 6c 74 20 69 73 20 66 61 6c 73 65 20 77 68 65 ult is false whe
1e57b 72 65 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 reas it would be
1e57c 20 74 72 75 65 20 77 69 74 68 20 49 64 78 47 54 true with IdxGT
1e57d 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1e57e 49 64 78 4c 54 20 50 31 20 50 32 20 50 33 20 2a IdxLT P1 P2 P3 *
1e57f 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 P5.**.** The P4
1e580 20 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 register values
1e581 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 beginning with
1e582 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 P3 form an unpac
1e583 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 ked index .** ke
1e584 79 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 y that omits the
1e585 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 ROWID. Compare
1e586 20 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 this key value
1e587 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 against the inde
1e588 78 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 x .** that P1 is
1e589 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1e58a 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 ing to, ignoring
1e58b 20 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 the ROWID on th
1e58c 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a e P1 index..**.*
1e58d 2a 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 * If the P1 inde
1e58e 78 20 65 6e 74 72 79 20 69 73 20 6c 65 73 73 20 x entry is less
1e58f 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c than the key val
1e590 75 65 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 ue then jump to
1e591 50 32 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 P2..** Otherwise
1e592 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f fall through to
1e593 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 the next instru
1e594 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ction..**.** If
1e595 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 P5 is non-zero t
1e596 68 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 hen the key valu
1e597 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 e is increased b
1e598 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20 70 72 69 y an epsilon pri
1e599 6f 72 20 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f or .** to the co
1e59a 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 mparison. This
1e59b 6d 61 6b 65 73 20 74 68 65 20 6f 70 63 6f 64 65 makes the opcode
1e59c 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78 4c 45 work like IdxLE
1e59d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 ..*/.case OP_Idx
1e59e 4c 54 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 LT: /*
1e59f 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1e5a0 65 20 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20 20 e OP_IdxGE: {
1e5a1 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1e5a2 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 3 */.#if 0 /* l
1e5a3 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e5a4 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a oved into u.bq *
1e5a5 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e5a6 70 43 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 pC;. int res;.
1e5a7 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1e5a8 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 r;.#endif /* loc
1e5a9 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e5aa 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a ed into u.bq */.
1e5ab 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e5ac 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e5ad 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e5ae 20 75 2e 62 71 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bq.pC = p->ap
1e5af 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e5b0 61 73 73 65 72 74 28 20 75 2e 62 71 2e 70 43 21 assert( u.bq.pC!
1e5b1 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 =0 );. if( ALWA
1e5b2 59 53 28 75 2e 62 71 2e 70 43 2d 3e 70 43 75 72 YS(u.bq.pC->pCur
1e5b3 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61 sor!=0) ){. a
1e5b4 73 73 65 72 74 28 20 75 2e 62 71 2e 70 43 2d 3e ssert( u.bq.pC->
1e5b5 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d deferredMoveto==
1e5b6 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
1e5b7 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70 pOp->p5==0 || p
1e5b8 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20 Op->p5==1 );.
1e5b9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1e5ba 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 type==P4_INT32 )
1e5bb 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 70 4b 65 ;. u.bq.r.pKe
1e5bc 79 49 6e 66 6f 20 3d 20 75 2e 62 71 2e 70 43 2d yInfo = u.bq.pC-
1e5bd 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 >pKeyInfo;. u
1e5be 2e 62 71 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 .bq.r.nField = (
1e5bf 75 31 36 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 u16)pOp->p4.i;.
1e5c0 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 if( pOp->p5 )
1e5c1 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 2e 66 {. u.bq.r.f
1e5c2 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f lags = UNPACKED_
1e5c3 49 4e 43 52 4b 45 59 20 7c 20 55 4e 50 41 43 4b INCRKEY | UNPACK
1e5c4 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b ED_IGNORE_ROWID;
1e5c5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1e5c6 20 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d u.bq.r.flags =
1e5c7 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 UNPACKED_IGNORE
1e5c8 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 _ROWID;. }.
1e5c9 20 20 75 2e 62 71 2e 72 2e 61 4d 65 6d 20 3d 20 u.bq.r.aMem =
1e5ca 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1e5cb 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ];. rc = sqli
1e5cc 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d te3VdbeIdxKeyCom
1e5cd 70 61 72 65 28 75 2e 62 71 2e 70 43 2c 20 26 75 pare(u.bq.pC, &u
1e5ce 2e 62 71 2e 72 2c 20 26 75 2e 62 71 2e 72 65 73 .bq.r, &u.bq.res
1e5cf 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e );. if( pOp->
1e5d0 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54 opcode==OP_IdxLT
1e5d1 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 ){. u.bq.r
1e5d2 65 73 20 3d 20 2d 75 2e 62 71 2e 72 65 73 3b 0a es = -u.bq.res;.
1e5d3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e5d4 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 assert( pOp->op
1e5d5 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47 45 20 29 code==OP_IdxGE )
1e5d6 3b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 65 73 ;. u.bq.res
1e5d7 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
1e5d8 28 20 75 2e 62 71 2e 72 65 73 3e 30 20 29 7b 0a ( u.bq.res>0 ){.
1e5d9 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1e5da 70 32 20 2d 20 31 20 3b 0a 20 20 20 20 7d 0a 20 p2 - 1 ;. }.
1e5db 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1e5dc 2a 20 4f 70 63 6f 64 65 3a 20 44 65 73 74 72 6f * Opcode: Destro
1e5dd 79 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a y P1 P2 P3 * *.*
1e5de 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 *.** Delete an e
1e5df 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 20 74 ntire database t
1e5e0 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68 able or index wh
1e5e1 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 6e ose root page in
1e5e2 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
1e5e3 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 file is given b
1e5e4 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 y P1..**.** The
1e5e5 74 61 62 6c 65 20 62 65 69 6e 67 20 64 65 73 74 table being dest
1e5e6 72 6f 79 65 64 20 69 73 20 69 6e 20 74 68 65 20 royed is in the
1e5e7 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1e5e8 6c 65 20 69 66 20 50 33 3d 3d 30 2e 20 20 49 66 le if P3==0. If
1e5e9 0a 2a 2a 20 50 33 3d 3d 31 20 74 68 65 6e 20 74 .** P3==1 then t
1e5ea 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 he table to be c
1e5eb 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 lear is in the a
1e5ec 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
1e5ed 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 e file.** that i
1e5ee 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 s used to store
1e5ef 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 tables create us
1e5f0 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f ing CREATE TEMPO
1e5f1 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a RARY TABLE..**.*
1e5f2 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 * If AUTOVACUUM
1e5f3 69 73 20 65 6e 61 62 6c 65 64 20 74 68 65 6e 20 is enabled then
1e5f4 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
1e5f5 68 61 74 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 hat another root
1e5f6 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 62 page.** might b
1e5f7 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 e moved into the
1e5f8 20 6e 65 77 6c 79 20 64 65 6c 65 74 65 64 20 72 newly deleted r
1e5f9 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f 72 64 65 oot page in orde
1e5fa 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c 0a 2a 2a r to keep all.**
1e5fb 20 72 6f 6f 74 20 70 61 67 65 73 20 63 6f 6e 74 root pages cont
1e5fc 69 67 75 6f 75 73 20 61 74 20 74 68 65 20 62 65 iguous at the be
1e5fd 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 ginning of the d
1e5fe 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 66 6f atabase. The fo
1e5ff 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 rmer.** value of
1e600 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 74 the root page t
1e601 68 61 74 20 6d 6f 76 65 64 20 2d 20 69 74 73 20 hat moved - its
1e602 76 61 6c 75 65 20 62 65 66 6f 72 65 20 74 68 65 value before the
1e603 20 6d 6f 76 65 20 6f 63 63 75 72 72 65 64 20 2d move occurred -
1e604 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 69 6e .** is stored in
1e605 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 register P2. I
1e606 66 20 6e 6f 20 70 61 67 65 20 0a 2a 2a 20 6d 6f f no page .** mo
1e607 76 65 6d 65 6e 74 20 77 61 73 20 72 65 71 75 69 vement was requi
1e608 72 65 64 20 28 62 65 63 61 75 73 65 20 74 68 65 red (because the
1e609 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f table being dro
1e60a 70 70 65 64 20 77 61 73 20 61 6c 72 65 61 64 79 pped was already
1e60b 20 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 6f 6e .** the last on
1e60c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
1e60d 65 29 20 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 e) then a zero i
1e60e 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 s stored in regi
1e60f 73 74 65 72 20 50 32 2e 0a 2a 2a 20 49 66 20 41 ster P2..** If A
1e610 55 54 4f 56 41 43 55 55 4d 20 69 73 20 64 69 73 UTOVACUUM is dis
1e611 61 62 6c 65 64 20 74 68 65 6e 20 61 20 7a 65 72 abled then a zer
1e612 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 o is stored in r
1e613 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a egister P2..**.*
1e614 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 6c 65 61 * See also: Clea
1e615 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 73 r.*/.case OP_Des
1e616 74 72 6f 79 3a 20 7b 20 20 20 20 20 2f 2a 20 6f troy: { /* o
1e617 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1e618 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1e619 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e61a 64 20 69 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a 20 d into u.br */.
1e61b 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 69 int iMoved;. i
1e61c 6e 74 20 69 43 6e 74 3b 0a 20 20 56 64 62 65 20 nt iCnt;. Vdbe
1e61d 2a 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 44 *pVdbe;. int iD
1e61e 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 b;.#endif /* loc
1e61f 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e620 65 64 20 69 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a ed into u.br */.
1e621 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1e622 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1e623 0a 20 20 75 2e 62 72 2e 69 43 6e 74 20 3d 20 30 . u.br.iCnt = 0
1e624 3b 0a 20 20 66 6f 72 28 75 2e 62 72 2e 70 56 64 ;. for(u.br.pVd
1e625 62 65 3d 64 62 2d 3e 70 56 64 62 65 3b 20 75 2e be=db->pVdbe; u.
1e626 62 72 2e 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 br.pVdbe; u.br.p
1e627 56 64 62 65 20 3d 20 75 2e 62 72 2e 70 56 64 62 Vdbe = u.br.pVdb
1e628 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 e->pNext){. i
1e629 66 28 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 6d f( u.br.pVdbe->m
1e62a 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
1e62b 5f 52 55 4e 20 26 26 20 75 2e 62 72 2e 70 56 64 _RUN && u.br.pVd
1e62c 62 65 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 be->inVtabMethod
1e62d 3c 32 20 26 26 20 75 2e 62 72 2e 70 56 64 62 65 <2 && u.br.pVdbe
1e62e 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 ->pc>=0 ){.
1e62f 20 75 2e 62 72 2e 69 43 6e 74 2b 2b 3b 0a 20 20 u.br.iCnt++;.
1e630 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 }. }.#else.
1e631 75 2e 62 72 2e 69 43 6e 74 20 3d 20 64 62 2d 3e u.br.iCnt = db->
1e632 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 0a 23 activeVdbeCnt;.#
1e633 65 6e 64 69 66 0a 20 20 69 66 28 20 75 2e 62 72 endif. if( u.br
1e634 2e 69 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 72 .iCnt>1 ){. r
1e635 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 c = SQLITE_LOCKE
1e636 44 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f 72 41 D;. p->errorA
1e637 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 ction = OE_Abort
1e638 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 ;. }else{. u
1e639 2e 62 72 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 .br.iDb = pOp->p
1e63a 33 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 3;. assert( u
1e63b 2e 62 72 2e 69 43 6e 74 3d 3d 31 20 29 3b 0a 20 .br.iCnt==1 );.
1e63c 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 assert( (p->b
1e63d 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75 treeMask & (1<<u
1e63e 2e 62 72 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a .br.iDb))!=0 );.
1e63f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e640 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64 BtreeDropTable(d
1e641 62 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d b->aDb[u.br.iDb]
1e642 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 26 .pBt, pOp->p1, &
1e643 75 2e 62 72 2e 69 4d 6f 76 65 64 29 3b 0a 20 20 u.br.iMoved);.
1e644 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1e645 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1e646 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d . pOut->u.i =
1e647 20 75 2e 62 72 2e 69 4d 6f 76 65 64 3b 0a 23 69 u.br.iMoved;.#i
1e648 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1e649 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
1e64a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1e64b 4f 4b 20 26 26 20 75 2e 62 72 2e 69 4d 6f 76 65 OK && u.br.iMove
1e64c 64 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 d!=0 ){. sq
1e64d 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 lite3RootPageMov
1e64e 65 64 28 26 64 62 2d 3e 61 44 62 5b 75 2e 62 72 ed(&db->aDb[u.br
1e64f 2e 69 44 62 5d 2c 20 75 2e 62 72 2e 69 4d 6f 76 .iDb], u.br.iMov
1e650 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 ed, pOp->p1);.
1e651 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
1e652 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1e653 63 6f 64 65 3a 20 43 6c 65 61 72 20 50 31 20 50 code: Clear P1 P
1e654 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 2 P3.**.** Delet
1e655 65 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 73 20 6f e all contents o
1e656 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 f the database t
1e657 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68 able or index wh
1e658 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a ose root page.**
1e659 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1e65a 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 file is given b
1e65b 79 20 50 31 2e 20 20 42 75 74 2c 20 75 6e 6c 69 y P1. But, unli
1e65c 6b 65 20 44 65 73 74 72 6f 79 2c 20 64 6f 20 6e ke Destroy, do n
1e65d 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 ot.** remove the
1e65e 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1e65f 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 from the databas
1e660 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 e file..**.** Th
1e661 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c e table being cl
1e662 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 6d 61 ear is in the ma
1e663 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 in database file
1e664 20 69 66 20 50 32 3d 3d 30 2e 20 20 49 66 0a 2a if P2==0. If.*
1e665 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 74 68 65 * P2==1 then the
1e666 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 table to be cle
1e667 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78 ar is in the aux
1e668 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 iliary database
1e669 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 file.** that is
1e66a 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61 used to store ta
1e66b 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 69 6e bles create usin
1e66c 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41 g CREATE TEMPORA
1e66d 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 RY TABLE..**.**
1e66e 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 If the P3 value
1e66f 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1e670 6e 20 74 68 65 20 74 61 62 6c 65 20 72 65 66 65 n the table refe
1e671 72 72 65 64 20 74 6f 20 6d 75 73 74 20 62 65 20 rred to must be
1e672 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 74 61 62 an.** intkey tab
1e673 6c 65 20 28 61 6e 20 53 51 4c 20 74 61 62 6c 65 le (an SQL table
1e674 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 65 78 29 2e , not an index).
1e675 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
1e676 65 20 72 6f 77 20 63 68 61 6e 67 65 20 0a 2a 2a e row change .**
1e677 20 63 6f 75 6e 74 20 69 73 20 69 6e 63 72 65 6d count is increm
1e678 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d ented by the num
1e679 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 ber of rows in t
1e67a 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 he table being c
1e67b 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 49 66 20 50 leared. .** If P
1e67c 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 3 is greater tha
1e67d 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 n zero, then the
1e67e 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
1e67f 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 0a register P3 is.
1e680 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 65 6d 65 6e ** also incremen
1e681 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 ted by the numbe
1e682 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 r of rows in the
1e683 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 table being cle
1e684 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ared..**.** See
1e685 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79 0a 2a 2f also: Destroy.*/
1e686 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61 72 3a 20 .case OP_Clear:
1e687 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1e688 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e689 64 20 69 6e 74 6f 20 75 2e 62 73 20 2a 2f 0a 20 d into u.bs */.
1e68a 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 23 65 int nChange;.#e
1e68b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e68c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e68d 74 6f 20 75 2e 62 73 20 2a 2f 0a 0a 20 20 75 2e to u.bs */.. u.
1e68e 62 73 2e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a bs.nChange = 0;.
1e68f 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 assert( (p->bt
1e690 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f reeMask & (1<<pO
1e691 70 2d 3e 70 32 29 29 21 3d 30 20 29 3b 0a 20 20 p->p2))!=0 );.
1e692 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e693 65 43 6c 65 61 72 54 61 62 6c 65 28 0a 20 20 20 eClearTable(.
1e694 20 20 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e db->aDb[pOp->
1e695 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 p2].pBt, pOp->p1
1e696 2c 20 28 70 4f 70 2d 3e 70 33 20 3f 20 26 75 2e , (pOp->p3 ? &u.
1e697 62 73 2e 6e 43 68 61 6e 67 65 20 3a 20 30 29 0a bs.nChange : 0).
1e698 20 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e );. if( pOp->
1e699 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 68 p3 ){. p->nCh
1e69a 61 6e 67 65 20 2b 3d 20 75 2e 62 73 2e 6e 43 68 ange += u.bs.nCh
1e69b 61 6e 67 65 3b 0a 20 20 20 20 69 66 28 20 70 4f ange;. if( pO
1e69c 70 2d 3e 70 33 3e 30 20 29 7b 0a 20 20 20 20 20 p->p3>0 ){.
1e69d 20 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 p->aMem[pOp->p3
1e69e 5d 2e 75 2e 69 20 2b 3d 20 75 2e 62 73 2e 6e 43 ].u.i += u.bs.nC
1e69f 68 61 6e 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d hange;. }. }
1e6a0 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1e6a1 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 54 61 Opcode: CreateTa
1e6a2 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ble P1 P2 * * *.
1e6a3 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 **.** Allocate a
1e6a4 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 74 68 new table in th
1e6a5 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1e6a6 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 file if P1==0 or
1e6a7 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c in the.** auxil
1e6a8 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 iary database fi
1e6a9 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 le if P1==1 or i
1e6aa 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 n an attached da
1e6ab 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e tabase if.** P1>
1e6ac 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 1. Write the ro
1e6ad 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f ot page number o
1e6ae 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 f the new table
1e6af 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 into.** register
1e6b0 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 P2.**.** The di
1e6b1 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e fference between
1e6b2 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 6e 20 a table and an
1e6b3 69 6e 64 65 78 20 69 73 20 74 68 69 73 3a 20 20 index is this:
1e6b4 41 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a 20 A table must.**
1e6b5 68 61 76 65 20 61 20 34 2d 62 79 74 65 20 69 6e have a 4-byte in
1e6b6 74 65 67 65 72 20 6b 65 79 20 61 6e 64 20 63 61 teger key and ca
1e6b7 6e 20 68 61 76 65 20 61 72 62 69 74 72 61 72 79 n have arbitrary
1e6b8 20 64 61 74 61 2e 20 20 41 6e 20 69 6e 64 65 78 data. An index
1e6b9 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 62 69 74 .** has an arbit
1e6ba 72 61 72 79 20 6b 65 79 20 62 75 74 20 6e 6f 20 rary key but no
1e6bb 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 data..**.** See
1e6bc 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 6e 64 65 also: CreateInde
1e6bd 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 x.*/./* Opcode:
1e6be 43 72 65 61 74 65 49 6e 64 65 78 20 50 31 20 50 CreateIndex P1 P
1e6bf 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 2 * * *.**.** Al
1e6c0 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 69 6e 64 locate a new ind
1e6c1 65 78 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 ex in the main d
1e6c2 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 atabase file if
1e6c3 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a P1==0 or in the.
1e6c4 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 ** auxiliary dat
1e6c5 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 abase file if P1
1e6c6 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 ==1 or in an att
1e6c7 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 ached database i
1e6c8 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 f.** P1>1. Writ
1e6c9 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 e the root page
1e6ca 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 number of the ne
1e6cb 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 w table into.**
1e6cc 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a register P2..**.
1e6cd 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74 61 ** See documenta
1e6ce 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61 74 tion on OP_Creat
1e6cf 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69 74 eTable for addit
1e6d0 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
1e6d1 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 n..*/.case OP_Cr
1e6d2 65 61 74 65 49 6e 64 65 78 3a 20 20 20 20 20 20 eateIndex:
1e6d3 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1e6d4 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63 61 73 65 erelease */.case
1e6d5 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 3a OP_CreateTable:
1e6d6 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f { /* o
1e6d7 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1e6d8 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1e6d9 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e6da 64 20 69 6e 74 6f 20 75 2e 62 74 20 2a 2f 0a 20 d into u.bt */.
1e6db 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 int pgno;. int
1e6dc 20 66 6c 61 67 73 3b 0a 20 20 44 62 20 2a 70 44 flags;. Db *pD
1e6dd 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 b;.#endif /* loc
1e6de 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e6df 65 64 20 69 6e 74 6f 20 75 2e 62 74 20 2a 2f 0a ed into u.bt */.
1e6e0 0a 20 20 75 2e 62 74 2e 70 67 6e 6f 20 3d 20 30 . u.bt.pgno = 0
1e6e1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1e6e2 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e6e3 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 1<db->nDb );. a
1e6e4 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 ssert( (p->btree
1e6e5 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e Mask & (1<<pOp->
1e6e6 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 62 p1))!=0 );. u.b
1e6e7 74 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 t.pDb = &db->aDb
1e6e8 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1e6e9 65 72 74 28 20 75 2e 62 74 2e 70 44 62 2d 3e 70 ert( u.bt.pDb->p
1e6ea 42 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 Bt!=0 );. if( p
1e6eb 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 Op->opcode==OP_C
1e6ec 72 65 61 74 65 54 61 62 6c 65 20 29 7b 0a 20 20 reateTable ){.
1e6ed 20 20 2f 2a 20 75 2e 62 74 2e 66 6c 61 67 73 20 /* u.bt.flags
1e6ee 3d 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20 = BTREE_INTKEY;
1e6ef 2a 2f 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61 67 */. u.bt.flag
1e6f0 73 20 3d 20 42 54 52 45 45 5f 4c 45 41 46 44 41 s = BTREE_LEAFDA
1e6f1 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b TA|BTREE_INTKEY;
1e6f2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e . }else{. u.
1e6f3 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 45 bt.flags = BTREE
1e6f4 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 20 7d 0a 20 _ZERODATA;. }.
1e6f5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e6f6 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e eeCreateTable(u.
1e6f7 62 74 2e 70 44 62 2d 3e 70 42 74 2c 20 26 75 2e bt.pDb->pBt, &u.
1e6f8 62 74 2e 70 67 6e 6f 2c 20 75 2e 62 74 2e 66 6c bt.pgno, u.bt.fl
1e6f9 61 67 73 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e ags);. pOut->u.
1e6fa 69 20 3d 20 75 2e 62 74 2e 70 67 6e 6f 3b 0a 20 i = u.bt.pgno;.
1e6fb 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1e6fc 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a pOut, MEM_Int);.
1e6fd 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e6fe 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63 68 65 pcode: ParseSche
1e6ff 6d 61 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a ma P1 P2 * P4 *.
1e700 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64 20 70 **.** Read and p
1e701 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69 65 73 arse all entries
1e702 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 45 from the SQLITE
1e703 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66 _MASTER table of
1e704 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a 2a 20 database P1.**
1e705 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 20 57 that match the W
1e706 48 45 52 45 20 63 6c 61 75 73 65 20 50 34 2e 20 HERE clause P4.
1e707 20 50 32 20 69 73 20 74 68 65 20 22 66 6f 72 63 P2 is the "forc
1e708 65 22 20 66 6c 61 67 2e 20 20 20 41 6c 77 61 79 e" flag. Alway
1e709 73 20 64 6f 0a 2a 2a 20 74 68 65 20 70 61 72 73 s do.** the pars
1e70a 69 6e 67 20 69 66 20 50 32 20 69 73 20 74 72 75 ing if P2 is tru
1e70b 65 2e 20 20 49 66 20 50 32 20 69 73 20 66 61 6c e. If P2 is fal
1e70c 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f se, then this ro
1e70d 75 74 69 6e 65 20 69 73 20 61 0a 2a 2a 20 6e 6f utine is a.** no
1e70e 2d 6f 70 20 69 66 20 74 68 65 20 73 63 68 65 6d -op if the schem
1e70f 61 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 a is not current
1e710 6c 79 20 6c 6f 61 64 65 64 2e 20 20 49 6e 20 6f ly loaded. In o
1e711 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 50 ther words, if P
1e712 32 0a 2a 2a 20 69 73 20 66 61 6c 73 65 2c 20 74 2.** is false, t
1e713 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 he SQLITE_MASTER
1e714 20 74 61 62 6c 65 20 69 73 20 6f 6e 6c 79 20 70 table is only p
1e715 61 72 73 65 64 20 69 66 20 74 68 65 20 72 65 73 arsed if the res
1e716 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65 t of the.** sche
1e717 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 6c 6f ma is already lo
1e718 61 64 65 64 20 69 6e 74 6f 20 74 68 65 20 73 79 aded into the sy
1e719 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a mbol table..**.*
1e71a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e * This opcode in
1e71b 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73 65 72 vokes the parser
1e71c 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 to create a new
1e71d 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1e71e 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73 20 74 ,.** then runs t
1e71f 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d he new virtual m
1e720 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73 20 74 achine. It is t
1e721 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61 6e 74 hus a re-entrant
1e722 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 opcode..*/.case
1e723 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 3a OP_ParseSchema:
1e724 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1e725 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e726 65 64 20 69 6e 74 6f 20 75 2e 62 75 20 2a 2f 0a ed into u.bu */.
1e727 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 63 6f 6e int iDb;. con
1e728 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 st char *zMaster
1e729 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a ;. char *zSql;.
1e72a 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 InitData initD
1e72b 61 74 61 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c ata;.#endif /* l
1e72c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e72d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75 20 2a oved into u.bu *
1e72e 2f 0a 0a 20 20 75 2e 62 75 2e 69 44 62 20 3d 20 /.. u.bu.iDb =
1e72f 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72 pOp->p1;. asser
1e730 74 28 20 75 2e 62 75 2e 69 44 62 3e 3d 30 20 26 t( u.bu.iDb>=0 &
1e731 26 20 75 2e 62 75 2e 69 44 62 3c 64 62 2d 3e 6e & u.bu.iDb<db->n
1e732 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 Db );.. /* If p
1e733 4f 70 2d 3e 70 32 20 69 73 20 30 2c 20 74 68 65 Op->p2 is 0, the
1e734 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 n this opcode is
1e735 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 being executed
1e736 74 6f 20 72 65 61 64 20 61 0a 20 20 2a 2a 20 73 to read a. ** s
1e737 69 6e 67 6c 65 20 72 6f 77 2c 20 66 6f 72 20 65 ingle row, for e
1e738 78 61 6d 70 6c 65 20 74 68 65 20 72 6f 77 20 63 xample the row c
1e739 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
1e73a 61 20 6e 65 77 20 69 6e 64 65 78 0a 20 20 2a 2a a new index. **
1e73b 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73 created by this
1e73c 20 56 44 42 45 2c 20 66 72 6f 6d 20 74 68 65 20 VDBE, from the
1e73d 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
1e73e 62 6c 65 2e 20 49 74 20 6f 6e 6c 79 0a 20 20 2a ble. It only. *
1e73f 2a 20 64 6f 65 73 20 74 68 69 73 20 69 66 20 74 * does this if t
1e740 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
1e741 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 63 68 65 6d in-memory schem
1e742 61 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 20 a is currently.
1e743 20 2a 2a 20 6c 6f 61 64 65 64 2e 20 4f 74 68 65 ** loaded. Othe
1e744 72 77 69 73 65 2c 20 74 68 65 20 6e 65 77 20 69 rwise, the new i
1e745 6e 64 65 78 20 64 65 66 69 6e 69 74 69 6f 6e 20 ndex definition
1e746 63 61 6e 20 62 65 20 6c 6f 61 64 65 64 20 61 6c can be loaded al
1e747 6f 6e 67 0a 20 20 2a 2a 20 77 69 74 68 20 74 68 ong. ** with th
1e748 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 73 63 e rest of the sc
1e749 68 65 6d 61 20 77 68 65 6e 20 69 74 20 69 73 20 hema when it is
1e74a 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a 20 required.. **.
1e74b 20 2a 2a 20 41 6c 74 68 6f 75 67 68 20 74 68 65 ** Although the
1e74c 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 mutex on the Bt
1e74d 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 Shared object th
1e74e 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 at corresponds t
1e74f 6f 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 o. ** database
1e750 75 2e 62 75 2e 69 44 62 20 28 74 68 65 20 64 61 u.bu.iDb (the da
1e751 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e tabase containin
1e752 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 g the sqlite_mas
1e753 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 72 ter table. ** r
1e754 65 61 64 20 62 79 20 74 68 69 73 20 69 6e 73 74 ead by this inst
1e755 72 75 63 74 69 6f 6e 29 20 69 73 20 63 75 72 72 ruction) is curr
1e756 65 6e 74 6c 79 20 68 65 6c 64 2c 20 69 74 20 69 ently held, it i
1e757 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a 20 s necessary to.
1e758 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6d ** obtain the m
1e759 75 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20 61 74 utexes on all at
1e75a 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 tached databases
1e75b 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69 6e 67 before checking
1e75c 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 if. ** the sch
1e75d 65 6d 61 20 6f 66 20 75 2e 62 75 2e 69 44 62 20 ema of u.bu.iDb
1e75e 69 73 20 6c 6f 61 64 65 64 2e 20 54 68 69 73 20 is loaded. This
1e75f 69 73 20 62 65 63 61 75 73 65 2c 20 61 74 20 74 is because, at t
1e760 68 65 20 73 74 61 72 74 20 6f 66 0a 20 20 2a 2a he start of. **
1e761 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 the sqlite3_exe
1e762 63 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 2c 20 c() call below,
1e763 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f SQLite will invo
1e764 6b 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 ke. ** sqlite3B
1e765 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 2e 20 treeEnterAll().
1e766 49 66 20 61 6c 6c 20 6d 75 74 65 78 65 73 20 61 If all mutexes a
1e767 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 re not already h
1e768 65 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20 75 2e eld, the. ** u.
1e769 62 75 2e 69 44 62 20 6d 75 74 65 78 20 6d 61 79 bu.iDb mutex may
1e76a 20 62 65 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 be temporarily
1e76b 72 65 6c 65 61 73 65 64 20 74 6f 20 61 76 6f 69 released to avoi
1e76c 64 20 64 65 61 64 6c 6f 63 6b 2e 20 49 66 0a 20 d deadlock. If.
1e76d 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73 ** this happens
1e76e 2c 20 74 68 65 6e 20 73 6f 6d 65 20 6f 74 68 65 , then some othe
1e76f 72 20 74 68 72 65 61 64 20 6d 61 79 20 64 65 6c r thread may del
1e770 65 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 ete the in-memor
1e771 79 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6f 66 y. ** schema of
1e772 20 64 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69 database u.bu.i
1e773 44 62 20 62 65 66 6f 72 65 20 74 68 65 20 53 51 Db before the SQ
1e774 4c 20 73 74 61 74 65 6d 65 6e 74 20 72 75 6e 73 L statement runs
1e775 2e 20 54 68 65 20 73 63 68 65 6d 61 0a 20 20 2a . The schema. *
1e776 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 * will not be re
1e777 6c 6f 61 64 65 64 20 62 65 63 75 61 73 65 20 74 loaded becuase t
1e778 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 he db->init.busy
1e779 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 flag is set. Th
1e77a 69 73 0a 20 20 2a 2a 20 63 61 6e 20 72 65 73 75 is. ** can resu
1e77b 6c 74 20 69 6e 20 61 20 22 6e 6f 20 73 75 63 68 lt in a "no such
1e77c 20 74 61 62 6c 65 3a 20 73 71 6c 69 74 65 5f 6d table: sqlite_m
1e77d 61 73 74 65 72 22 20 6f 72 20 22 6d 61 6c 66 6f aster" or "malfo
1e77e 72 6d 65 64 0a 20 20 2a 2a 20 64 61 74 61 62 61 rmed. ** databa
1e77f 73 65 20 73 63 68 65 6d 61 22 20 65 72 72 6f 72 se schema" error
1e780 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 20 being returned
1e781 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a to the user.. *
1e782 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 /. assert( sqli
1e783 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
1e784 65 78 28 64 62 2d 3e 61 44 62 5b 75 2e 62 75 2e ex(db->aDb[u.bu.
1e785 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 20 20 73 iDb].pBt) );. s
1e786 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1e787 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20 70 All(db);. if( p
1e788 4f 70 2d 3e 70 32 20 7c 7c 20 44 62 48 61 73 50 Op->p2 || DbHasP
1e789 72 6f 70 65 72 74 79 28 64 62 2c 20 75 2e 62 75 roperty(db, u.bu
1e78a 2e 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c .iDb, DB_SchemaL
1e78b 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 75 2e oaded) ){. u.
1e78c 62 75 2e 7a 4d 61 73 74 65 72 20 3d 20 53 43 48 bu.zMaster = SCH
1e78d 45 4d 41 5f 54 41 42 4c 45 28 75 2e 62 75 2e 69 EMA_TABLE(u.bu.i
1e78e 44 62 29 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e Db);. u.bu.in
1e78f 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a itData.db = db;.
1e790 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 u.bu.initDat
1e791 61 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b a.iDb = pOp->p1;
1e792 0a 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 . u.bu.initDa
1e793 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20 26 70 ta.pzErrMsg = &p
1e794 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 ->zErrMsg;. u
1e795 2e 62 75 2e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 .bu.zSql = sqlit
1e796 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20 e3MPrintf(db,.
1e797 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d "SELECT nam
1e798 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c e, rootpage, sql
1e799 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20 57 48 FROM '%q'.%s WH
1e79a 45 52 45 20 25 73 22 2c 0a 20 20 20 20 20 20 20 ERE %s",.
1e79b 64 62 2d 3e 61 44 62 5b 75 2e 62 75 2e 69 44 62 db->aDb[u.bu.iDb
1e79c 5d 2e 7a 4e 61 6d 65 2c 20 75 2e 62 75 2e 7a 4d ].zName, u.bu.zM
1e79d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 34 2e 7a aster, pOp->p4.z
1e79e 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 75 2e );. if( u.bu.
1e79f 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 zSql==0 ){.
1e7a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
1e7a1 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 EM;. }else{.
1e7a2 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 (void)sqlit
1e7a3 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b e3SafetyOff(db);
1e7a4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 . assert( d
1e7a5 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 b->init.busy==0
1e7a6 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 );. db->ini
1e7a7 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 t.busy = 1;.
1e7a8 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e u.bu.initData.
1e7a9 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1e7aa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 64 assert( !d
1e7ab 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1e7ac 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 );. rc = sq
1e7ad 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 75 lite3_exec(db, u
1e7ae 2e 62 75 2e 7a 53 71 6c 2c 20 73 71 6c 69 74 65 .bu.zSql, sqlite
1e7af 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26 3InitCallback, &
1e7b0 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2c 20 30 u.bu.initData, 0
1e7b1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d );. if( rc=
1e7b2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 =SQLITE_OK ) rc
1e7b3 3d 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e = u.bu.initData.
1e7b4 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 rc;. sqlite
1e7b5 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 62 75 3DbFree(db, u.bu
1e7b6 2e 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20 64 62 .zSql);. db
1e7b7 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b ->init.busy = 0;
1e7b8 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c . (void)sql
1e7b9 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1e7ba 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
1e7bb 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 lite3BtreeLeaveA
1e7bc 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 ll(db);. if( rc
1e7bd 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
1e7be 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 {. goto no_me
1e7bf 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a m;. }. break;.
1e7c0 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 }..#if !defined(
1e7c1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c SQLITE_OMIT_ANAL
1e7c2 59 5a 45 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 YZE)./* Opcode:
1e7c3 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 50 31 20 LoadAnalysis P1
1e7c4 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 * * * *.**.** Re
1e7c5 61 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 ad the sqlite_st
1e7c6 61 74 31 20 74 61 62 6c 65 20 66 6f 72 20 64 61 at1 table for da
1e7c7 74 61 62 61 73 65 20 50 31 20 61 6e 64 20 6c 6f tabase P1 and lo
1e7c8 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a ad the content.*
1e7c9 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20 * of that table
1e7ca 69 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 into the interna
1e7cb 6c 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 l index hash tab
1e7cc 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 le. This will c
1e7cd 61 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e 61 6c ause.** the anal
1e7ce 79 73 69 73 20 74 6f 20 62 65 20 75 73 65 64 20 ysis to be used
1e7cf 77 68 65 6e 20 70 72 65 70 61 72 69 6e 67 20 61 when preparing a
1e7d0 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 71 75 ll subsequent qu
1e7d1 65 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f eries..*/.case O
1e7d2 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 3a 20 P_LoadAnalysis:
1e7d3 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d {. assert( pOp-
1e7d4 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e7d5 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 72 1<db->nDb );. r
1e7d6 63 20 3d 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 c = sqlite3Analy
1e7d7 73 69 73 4c 6f 61 64 28 64 62 2c 20 70 4f 70 2d sisLoad(db, pOp-
1e7d8 3e 70 31 29 3b 0a 20 20 62 72 65 61 6b 3b 20 20 >p1);. break;
1e7d9 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 .}.#endif /* !de
1e7da 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1e7db 54 5f 41 4e 41 4c 59 5a 45 29 20 2a 2f 0a 0a 2f T_ANALYZE) */../
1e7dc 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 61 * Opcode: DropTa
1e7dd 62 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a ble P1 * * P4 *.
1e7de 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 **.** Remove the
1e7df 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 internal (in-me
1e7e0 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63 mory) data struc
1e7e1 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72 tures that descr
1e7e2 69 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 ibe.** the table
1e7e3 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 named P4 in dat
1e7e4 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 abase P1. This
1e7e5 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
1e7e6 61 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 64 72 a table.** is dr
1e7e7 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74 opped in order t
1e7e8 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72 o keep the inter
1e7e9 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 nal representati
1e7ea 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 on of the.** sch
1e7eb 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77 ema consistent w
1e7ec 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64 ith what is on d
1e7ed 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f isk..*/.case OP_
1e7ee 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 20 73 DropTable: {. s
1e7ef 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
1e7f0 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70 eleteTable(db, p
1e7f1 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e Op->p1, pOp->p4.
1e7f2 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a z);. break;.}..
1e7f3 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 49 /* Opcode: DropI
1e7f4 6e 64 65 78 20 50 31 20 2a 20 2a 20 50 34 20 2a ndex P1 * * P4 *
1e7f5 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 .**.** Remove th
1e7f6 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d e internal (in-m
1e7f7 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 emory) data stru
1e7f8 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 ctures that desc
1e7f9 72 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e 64 65 ribe.** the inde
1e7fa 78 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 x named P4 in da
1e7fb 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 tabase P1. This
1e7fc 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 is called after
1e7fd 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20 an index.** is
1e7fe 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 dropped in order
1e7ff 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 to keep the int
1e800 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
1e801 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 tion of the.** s
1e802 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 chema consistent
1e803 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e with what is on
1e804 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f disk..*/.case O
1e805 50 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b 0a 20 P_DropIndex: {.
1e806 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e sqlite3UnlinkAn
1e807 64 44 65 6c 65 74 65 49 6e 64 65 78 28 64 62 2c dDeleteIndex(db,
1e808 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 pOp->p1, pOp->p
1e809 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 4.z);. break;.}
1e80a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f ../* Opcode: Dro
1e80b 70 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a 20 pTrigger P1 * *
1e80c 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 P4 *.**.** Remov
1e80d 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 e the internal (
1e80e 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 in-memory) data
1e80f 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20 structures that
1e810 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 describe.** the
1e811 74 72 69 67 67 65 72 20 6e 61 6d 65 64 20 50 34 trigger named P4
1e812 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e in database P1.
1e813 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 This is called
1e814 20 61 66 74 65 72 20 61 20 74 72 69 67 67 65 72 after a trigger
1e815 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 .** is dropped i
1e816 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
1e817 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
1e818 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
1e819 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e he.** schema con
1e81a 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 sistent with wha
1e81b 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f t is on disk..*/
1e81c 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 72 69 .case OP_DropTri
1e81d 67 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 74 65 gger: {. sqlite
1e81e 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
1e81f 54 72 69 67 67 65 72 28 64 62 2c 20 70 4f 70 2d Trigger(db, pOp-
1e820 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b >p1, pOp->p4.z);
1e821 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 . break;.}...#i
1e822 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1e823 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
1e824 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 K./* Opcode: Int
1e825 65 67 72 69 74 79 43 6b 20 50 31 20 50 32 20 50 egrityCk P1 P2 P
1e826 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 6f 20 3 * P5.**.** Do
1e827 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74 an analysis of t
1e828 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 he currently ope
1e829 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53 74 6f n database. Sto
1e82a 72 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 re in.** registe
1e82b 72 20 50 31 20 74 68 65 20 74 65 78 74 20 6f 66 r P1 the text of
1e82c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1e82d 65 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e 79 e describing any
1e82e 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49 66 problems..** If
1e82f 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72 65 no problems are
1e830 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 61 20 found, store a
1e831 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 65 72 NULL in register
1e832 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 P1..**.** The r
1e833 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e 74 61 egister P3 conta
1e834 69 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 ins the maximum
1e835 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 77 65 number of allowe
1e836 64 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 74 20 d errors..** At
1e837 6d 6f 73 74 20 72 65 67 28 50 33 29 20 65 72 72 most reg(P3) err
1e838 6f 72 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f ors will be repo
1e839 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 rted..** In othe
1e83a 72 20 77 6f 72 64 73 2c 20 74 68 65 20 61 6e 61 r words, the ana
1e83b 6c 79 73 69 73 20 73 74 6f 70 73 20 61 73 20 73 lysis stops as s
1e83c 6f 6f 6e 20 61 73 20 72 65 67 28 50 31 29 20 65 oon as reg(P1) e
1e83d 72 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 73 65 rrors are .** se
1e83e 65 6e 2e 20 20 52 65 67 28 50 31 29 20 69 73 20 en. Reg(P1) is
1e83f 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 updated with the
1e840 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 number of error
1e841 73 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a 2a 0a s remaining..**.
1e842 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 ** The root page
1e843 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c 6c 20 numbers of all
1e844 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61 tables in the da
1e845 74 61 62 61 73 65 20 61 72 65 20 69 6e 74 65 67 tabase are integ
1e846 65 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 er.** stored in
1e847 72 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b reg(P1), reg(P1+
1e848 31 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 20 2e 1), reg(P1+2), .
1e849 2e 2e 2e 20 20 54 68 65 72 65 20 61 72 65 20 50 ... There are P
1e84a 32 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f 74 61 2 tables.** tota
1e84b 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 l..**.** If P5 i
1e84c 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20 s not zero, the
1e84d 63 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e check is done on
1e84e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 the auxiliary d
1e84f 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 2c atabase.** file,
1e850 20 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61 not the main da
1e851 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
1e852 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 ** This opcode i
1e853 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d s used to implem
1e854 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69 74 ent the integrit
1e855 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e 0a y_check pragma..
1e856 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 */.case OP_Integ
1e857 72 69 74 79 43 6b 3a 20 7b 0a 23 69 66 20 30 20 rityCk: {.#if 0
1e858 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e859 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e85a 2e 62 76 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f .bv */. int nRo
1e85b 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ot; /* Numb
1e85c 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 er of tables to
1e85d 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 20 check. (Number
1e85e 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 20 of root pages.)
1e85f 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b */. int *aRoot;
1e860 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 /* Array of
1e861 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72 rootpage number
1e862 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20 s for tables to
1e863 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 be checked */.
1e864 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 int j;
1e865 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1e866 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 */. int nErr;
1e867 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1e868 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 f errors reporte
1e869 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 d */. char *z;
1e86a 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f /* Text o
1e86b 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 70 6f f the error repo
1e86c 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 6e 45 rt */. Mem *pnE
1e86d 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73 rr; /* Regis
1e86e 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63 ter keeping trac
1e86f 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 k of errors rema
1e870 69 6e 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 20 ining */.#endif
1e871 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e872 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e873 62 76 20 2a 2f 0a 0a 20 20 75 2e 62 76 2e 6e 52 bv */.. u.bv.nR
1e874 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 oot = pOp->p2;.
1e875 20 61 73 73 65 72 74 28 20 75 2e 62 76 2e 6e 52 assert( u.bv.nR
1e876 6f 6f 74 3e 30 20 29 3b 0a 20 20 75 2e 62 76 2e oot>0 );. u.bv.
1e877 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 44 aRoot = sqlite3D
1e878 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 bMallocRaw(db, s
1e879 69 7a 65 6f 66 28 69 6e 74 29 2a 28 75 2e 62 76 izeof(int)*(u.bv
1e87a 2e 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a 20 20 69 .nRoot+1) );. i
1e87b 66 28 20 75 2e 62 76 2e 61 52 6f 6f 74 3d 3d 30 f( u.bv.aRoot==0
1e87c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1e87d 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e87e 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 3>0 && pOp->p3<=
1e87f 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 62 p->nMem );. u.b
1e880 76 2e 70 6e 45 72 72 20 3d 20 26 70 2d 3e 61 4d v.pnErr = &p->aM
1e881 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 em[pOp->p3];. a
1e882 73 73 65 72 74 28 20 28 75 2e 62 76 2e 70 6e 45 ssert( (u.bv.pnE
1e883 72 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f rr->flags & MEM_
1e884 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 Int)!=0 );. ass
1e885 65 72 74 28 20 28 75 2e 62 76 2e 70 6e 45 72 72 ert( (u.bv.pnErr
1e886 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 ->flags & (MEM_S
1e887 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 tr|MEM_Blob))==0
1e888 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d );. pIn1 = &p-
1e889 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1e88a 20 20 66 6f 72 28 75 2e 62 76 2e 6a 3d 30 3b 20 for(u.bv.j=0;
1e88b 75 2e 62 76 2e 6a 3c 75 2e 62 76 2e 6e 52 6f 6f u.bv.j<u.bv.nRoo
1e88c 74 3b 20 75 2e 62 76 2e 6a 2b 2b 29 7b 0a 20 20 t; u.bv.j++){.
1e88d 20 20 75 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 u.bv.aRoot[u.b
1e88e 76 2e 6a 5d 20 3d 20 28 69 6e 74 29 73 71 6c 69 v.j] = (int)sqli
1e88f 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 te3VdbeIntValue(
1e890 26 70 49 6e 31 5b 75 2e 62 76 2e 6a 5d 29 3b 0a &pIn1[u.bv.j]);.
1e891 20 20 7d 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 }. u.bv.aRoot
1e892 5b 75 2e 62 76 2e 6a 5d 20 3d 20 30 3b 0a 20 20 [u.bv.j] = 0;.
1e893 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c assert( pOp->p5<
1e894 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 db->nDb );. ass
1e895 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 ert( (p->btreeMa
1e896 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 sk & (1<<pOp->p5
1e897 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 62 76 2e ))!=0 );. u.bv.
1e898 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 z = sqlite3Btree
1e899 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 64 IntegrityCheck(d
1e89a 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e b->aDb[pOp->p5].
1e89b 70 42 74 2c 20 75 2e 62 76 2e 61 52 6f 6f 74 2c pBt, u.bv.aRoot,
1e89c 20 75 2e 62 76 2e 6e 52 6f 6f 74 2c 0a 20 20 20 u.bv.nRoot,.
1e89d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e89e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 (i
1e89f 6e 74 29 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 75 nt)u.bv.pnErr->u
1e8a0 2e 69 2c 20 26 75 2e 62 76 2e 6e 45 72 72 29 3b .i, &u.bv.nErr);
1e8a1 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1e8a2 28 64 62 2c 20 75 2e 62 76 2e 61 52 6f 6f 74 29 (db, u.bv.aRoot)
1e8a3 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72 2d 3e ;. u.bv.pnErr->
1e8a4 75 2e 69 20 2d 3d 20 75 2e 62 76 2e 6e 45 72 72 u.i -= u.bv.nErr
1e8a5 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1e8a6 65 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b emSetNull(pIn1);
1e8a7 0a 20 20 69 66 28 20 75 2e 62 76 2e 6e 45 72 72 . if( u.bv.nErr
1e8a8 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
1e8a9 74 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 29 3b 0a t( u.bv.z==0 );.
1e8aa 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 76 }else if( u.bv
1e8ab 2e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 .z==0 ){. got
1e8ac 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65 6c 73 o no_mem;. }els
1e8ad 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 e{. sqlite3Vd
1e8ae 62 65 4d 65 6d 53 65 74 53 74 72 28 70 49 6e 31 beMemSetStr(pIn1
1e8af 2c 20 75 2e 62 76 2e 7a 2c 20 2d 31 2c 20 53 51 , u.bv.z, -1, SQ
1e8b0 4c 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 LITE_UTF8, sqlit
1e8b1 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 20 20 e3_free);. }.
1e8b2 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1e8b3 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c IZE(pIn1);. sql
1e8b4 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
1e8b5 63 6f 64 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63 coding(pIn1, enc
1e8b6 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b oding);. break;
1e8b7 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1e8b8 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 ITE_OMIT_INTEGRI
1e8b9 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 TY_CHECK */../*
1e8ba 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 41 64 Opcode: RowSetAd
1e8bb 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a d P1 P2 * * *.**
1e8bc 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 69 .** Insert the i
1e8bd 6e 74 65 67 65 72 20 76 61 6c 75 65 20 68 65 6c nteger value hel
1e8be 64 20 62 79 20 72 65 67 69 73 74 65 72 20 50 32 d by register P2
1e8bf 20 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 61 6e 20 into a boolean
1e8c0 69 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 20 69 6e index.** held in
1e8c1 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1e8c2 0a 2a 2a 20 41 6e 20 61 73 73 65 72 74 69 6f 6e .** An assertion
1e8c3 20 66 61 69 6c 73 20 69 66 20 50 32 20 69 73 20 fails if P2 is
1e8c4 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a not an integer..
1e8c5 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 */.case OP_RowSe
1e8c6 74 41 64 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a tAdd: { /*
1e8c7 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f in2 */.#if 0 /
1e8c8 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e8c9 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e8ca 77 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 w */. Mem *pIdx
1e8cb 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 6c 3b 0a 23 ;. Mem *pVal;.#
1e8cc 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1e8cd 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e8ce 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 20 61 73 nto u.bw */. as
1e8cf 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 sert( pOp->p1>0
1e8d0 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e && pOp->p1<=p->n
1e8d1 4d 65 6d 20 29 3b 0a 20 20 75 2e 62 77 2e 70 49 Mem );. u.bw.pI
1e8d2 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f dx = &p->aMem[pO
1e8d3 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e8d4 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 ( pOp->p2>0 && p
1e8d5 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p2<=p->nMem
1e8d6 29 3b 0a 20 20 75 2e 62 77 2e 70 56 61 6c 20 3d );. u.bw.pVal =
1e8d7 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1e8d8 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2];. assert( (u
1e8d9 2e 62 77 2e 70 56 61 6c 2d 3e 66 6c 61 67 73 20 .bw.pVal->flags
1e8da 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b & MEM_Int)!=0 );
1e8db 0a 20 20 69 66 28 20 28 75 2e 62 77 2e 70 49 64 . if( (u.bw.pId
1e8dc 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 x->flags & MEM_R
1e8dd 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 owSet)==0 ){.
1e8de 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1e8df 65 74 52 6f 77 53 65 74 28 75 2e 62 77 2e 70 49 etRowSet(u.bw.pI
1e8e0 64 78 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e dx);. if( (u.
1e8e1 62 77 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 bw.pIdx->flags &
1e8e2 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
1e8e3 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 ) goto no_mem;.
1e8e4 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 6f 77 53 }. sqlite3RowS
1e8e5 65 74 49 6e 73 65 72 74 28 75 2e 62 77 2e 70 49 etInsert(u.bw.pI
1e8e6 64 78 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 75 dx->u.pRowSet, u
1e8e7 2e 62 77 2e 70 56 61 6c 2d 3e 75 2e 69 29 3b 0a .bw.pVal->u.i);.
1e8e8 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e8e9 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 52 65 61 pcode: RowSetRea
1e8ea 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a d P1 P2 P3 * *.*
1e8eb 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 *.** Extract the
1e8ec 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20 smallest value
1e8ed 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 from boolean ind
1e8ee 65 78 20 50 31 20 61 6e 64 20 70 75 74 20 74 68 ex P1 and put th
1e8ef 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a at value into.**
1e8f0 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 20 4f register P3. O
1e8f1 72 2c 20 69 66 20 62 6f 6f 6c 65 61 6e 20 69 6e r, if boolean in
1e8f2 64 65 78 20 50 31 20 69 73 20 69 6e 69 74 69 61 dex P1 is initia
1e8f3 6c 6c 79 20 65 6d 70 74 79 2c 20 6c 65 61 76 65 lly empty, leave
1e8f4 20 50 33 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 P3.** unchanged
1e8f5 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 69 6e 73 and jump to ins
1e8f6 74 72 75 63 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a truction P2..*/.
1e8f7 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 52 65 case OP_RowSetRe
1e8f8 61 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a ad: { /* j
1e8f9 75 6d 70 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 ump, out3 */.#if
1e8fa 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e8fb 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e8fc 6f 20 75 2e 62 78 20 2a 2f 0a 20 20 4d 65 6d 20 o u.bx */. Mem
1e8fd 2a 70 49 64 78 3b 0a 20 20 69 36 34 20 76 61 6c *pIdx;. i64 val
1e8fe 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e8ff 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e900 64 20 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a 20 d into u.bx */.
1e901 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1e902 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 >0 && pOp->p1<=p
1e903 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 43 48 45 43 ->nMem );. CHEC
1e904 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b K_FOR_INTERRUPT;
1e905 0a 20 20 75 2e 62 78 2e 70 49 64 78 20 3d 20 26 . u.bx.pIdx = &
1e906 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d p->aMem[pOp->p1]
1e907 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 ;. pOut = &p->a
1e908 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1e909 69 66 28 20 28 75 2e 62 78 2e 70 49 64 78 2d 3e if( (u.bx.pIdx->
1e90a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
1e90b 65 74 29 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c et)==0. || sql
1e90c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 75 ite3RowSetNext(u
1e90d 2e 62 78 2e 70 49 64 78 2d 3e 75 2e 70 52 6f 77 .bx.pIdx->u.pRow
1e90e 53 65 74 2c 20 26 75 2e 62 78 2e 76 61 6c 29 3d Set, &u.bx.val)=
1e90f 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 54 =0. ){. /* T
1e910 68 65 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 he boolean index
1e911 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 is empty */.
1e912 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1e913 65 74 4e 75 6c 6c 28 75 2e 62 78 2e 70 49 64 78 etNull(u.bx.pIdx
1e914 29 3b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d );. pc = pOp-
1e915 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 >p2 - 1;. }else
1e916 7b 0a 20 20 20 20 2f 2a 20 41 20 76 61 6c 75 65 {. /* A value
1e917 20 77 61 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d was pulled from
1e918 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 the index */.
1e919 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e91a 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 3>0 && pOp->p3<=
1e91b 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 73 p->nMem );. s
1e91c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1e91d 49 6e 74 36 34 28 70 4f 75 74 2c 20 75 2e 62 78 Int64(pOut, u.bx
1e91e 2e 76 61 6c 29 3b 0a 20 20 7d 0a 20 20 62 72 65 .val);. }. bre
1e91f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e920 3a 20 52 6f 77 53 65 74 54 65 73 74 20 50 31 20 : RowSetTest P1
1e921 50 32 20 50 33 20 50 34 0a 2a 2a 0a 2a 2a 20 52 P2 P3 P4.**.** R
1e922 65 67 69 73 74 65 72 20 50 33 20 69 73 20 61 73 egister P3 is as
1e923 73 75 6d 65 64 20 74 6f 20 68 6f 6c 64 20 61 20 sumed to hold a
1e924 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 64-bit integer v
1e925 61 6c 75 65 2e 20 49 66 20 72 65 67 69 73 74 65 alue. If registe
1e926 72 20 50 31 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 r P1.** contains
1e927 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 a RowSet object
1e928 20 61 6e 64 20 74 68 61 74 20 52 6f 77 53 65 74 and that RowSet
1e929 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 object contains
1e92a 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 68 65 .** the value he
1e92b 6c 64 20 69 6e 20 50 33 2c 20 6a 75 6d 70 20 74 ld in P3, jump t
1e92c 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 4f o register P2. O
1e92d 74 68 65 72 77 69 73 65 2c 20 69 6e 73 65 72 74 therwise, insert
1e92e 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 the.** integer
1e92f 69 6e 20 50 33 20 69 6e 74 6f 20 74 68 65 20 52 in P3 into the R
1e930 6f 77 53 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e owSet and contin
1e931 75 65 20 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 ue on to the.**
1e932 6e 65 78 74 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a next opcode..**.
1e933 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62 ** The RowSet ob
1e934 6a 65 63 74 20 69 73 20 6f 70 74 69 6d 69 7a 65 ject is optimize
1e935 64 20 66 6f 72 20 74 68 65 20 63 61 73 65 20 77 d for the case w
1e936 68 65 72 65 20 73 75 63 63 65 73 73 69 76 65 20 here successive
1e937 73 65 74 73 0a 2a 2a 20 6f 66 20 69 6e 74 65 67 sets.** of integ
1e938 65 72 73 2c 20 77 68 65 72 65 20 65 61 63 68 20 ers, where each
1e939 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 set contains no
1e93a 64 75 70 6c 69 63 61 74 65 73 2e 20 45 61 63 68 duplicates. Each
1e93b 20 73 65 74 0a 2a 2a 20 6f 66 20 76 61 6c 75 65 set.** of value
1e93c 73 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 s is identified
1e93d 62 79 20 61 20 75 6e 69 71 75 65 20 50 34 20 76 by a unique P4 v
1e93e 61 6c 75 65 2e 20 54 68 65 20 66 69 72 73 74 20 alue. The first
1e93f 73 65 74 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 set.** must have
1e940 20 50 34 3d 3d 30 2c 20 74 68 65 20 66 69 6e 61 P4==0, the fina
1e941 6c 20 73 65 74 20 50 34 3d 2d 31 2e 20 20 50 34 l set P4=-1. P4
1e942 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 must be either
1e943 2d 31 20 6f 72 0a 2a 2a 20 6e 6f 6e 2d 6e 65 67 -1 or.** non-neg
1e944 61 74 69 76 65 2e 20 20 46 6f 72 20 6e 6f 6e 2d ative. For non-
1e945 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 73 20 negative values
1e946 6f 66 20 50 34 20 6f 6e 6c 79 20 74 68 65 20 6c of P4 only the l
1e947 6f 77 65 72 20 34 0a 2a 2a 20 62 69 74 73 20 61 ower 4.** bits a
1e948 72 65 20 73 69 67 6e 69 66 69 63 61 6e 74 2e 0a re significant..
1e949 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c 6c 6f 77 **.** This allow
1e94a 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 3a s optimizations:
1e94b 20 28 61 29 20 77 68 65 6e 20 50 34 3d 3d 30 20 (a) when P4==0
1e94c 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
1e94d 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 65 20 to test.** the
1e94e 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 66 6f rowset object fo
1e94f 72 20 50 33 2c 20 61 73 20 69 74 20 69 73 20 67 r P3, as it is g
1e950 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 20 74 6f uaranteed not to
1e951 20 63 6f 6e 74 61 69 6e 20 69 74 2c 0a 2a 2a 20 contain it,.**
1e952 28 62 29 20 77 68 65 6e 20 50 34 3d 3d 2d 31 20 (b) when P4==-1
1e953 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 there is no need
1e954 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 76 to insert the v
1e955 61 6c 75 65 2c 20 61 73 20 69 74 20 77 69 6c 6c alue, as it will
1e956 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20 74 65 73 .** never be tes
1e957 74 65 64 20 66 6f 72 2c 20 61 6e 64 20 28 63 29 ted for, and (c)
1e958 20 77 68 65 6e 20 61 20 76 61 6c 75 65 20 74 68 when a value th
1e959 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 73 65 at is part of se
1e95a 74 20 58 20 69 73 0a 2a 2a 20 69 6e 73 65 72 74 t X is.** insert
1e95b 65 64 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 ed, there is no
1e95c 6e 65 65 64 20 74 6f 20 73 65 61 72 63 68 20 74 need to search t
1e95d 6f 20 73 65 65 20 69 66 20 74 68 65 20 73 61 6d o see if the sam
1e95e 65 20 76 61 6c 75 65 20 77 61 73 0a 2a 2a 20 70 e value was.** p
1e95f 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 reviously insert
1e960 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 65 ed as part of se
1e961 74 20 58 20 28 6f 6e 6c 79 20 69 66 20 69 74 20 t X (only if it
1e962 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a was previously.*
1e963 2a 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61 * inserted as pa
1e964 72 74 20 6f 66 20 73 6f 6d 65 20 6f 74 68 65 72 rt of some other
1e965 20 73 65 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f set)..*/.case O
1e966 50 5f 52 6f 77 53 65 74 54 65 73 74 3a 20 7b 20 P_RowSetTest: {
1e967 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e968 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 /* jump, in1
1e969 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 , in3 */.#if 0
1e96a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e96b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e96c 62 79 20 2a 2f 0a 20 20 69 6e 74 20 69 53 65 74 by */. int iSet
1e96d 3b 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a ;. int exists;.
1e96e 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e96f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e970 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a 0a 20 20 into u.by */..
1e971 75 2e 62 79 2e 69 53 65 74 20 3d 20 70 4f 70 2d u.by.iSet = pOp-
1e972 3e 70 34 2e 69 3b 0a 20 20 61 73 73 65 72 74 28 >p4.i;. assert(
1e973 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d 45 4d pIn3->flags&MEM
1e974 5f 49 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 _Int );.. /* If
1e975 20 74 68 65 72 65 20 69 73 20 61 6e 79 74 68 69 there is anythi
1e976 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 ng other than a
1e977 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 69 6e rowset object in
1e978 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 31 2c memory cell P1,
1e979 0a 20 20 2a 2a 20 64 65 6c 65 74 65 20 69 74 20 . ** delete it
1e97a 6e 6f 77 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 now and initiali
1e97b 7a 65 20 50 31 20 77 69 74 68 20 61 6e 20 65 6d ze P1 with an em
1e97c 70 74 79 20 72 6f 77 73 65 74 0a 20 20 2a 2f 0a pty rowset. */.
1e97d 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1e97e 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
1e97f 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
1e980 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 e3VdbeMemSetRowS
1e981 65 74 28 70 49 6e 31 29 3b 0a 20 20 20 20 69 66 et(pIn1);. if
1e982 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1e983 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 MEM_RowSet)==0
1e984 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 ) goto no_mem;.
1e985 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f }.. assert( pO
1e986 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e p->p4type==P4_IN
1e987 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 T32 );. assert(
1e988 20 75 2e 62 79 2e 69 53 65 74 3d 3d 2d 31 20 7c u.by.iSet==-1 |
1e989 7c 20 75 2e 62 79 2e 69 53 65 74 3e 3d 30 20 29 | u.by.iSet>=0 )
1e98a 3b 0a 20 20 69 66 28 20 75 2e 62 79 2e 69 53 65 ;. if( u.by.iSe
1e98b 74 20 29 7b 0a 20 20 20 20 75 2e 62 79 2e 65 78 t ){. u.by.ex
1e98c 69 73 74 73 20 3d 20 73 71 6c 69 74 65 33 52 6f ists = sqlite3Ro
1e98d 77 53 65 74 54 65 73 74 28 70 49 6e 31 2d 3e 75 wSetTest(pIn1->u
1e98e 2e 70 52 6f 77 53 65 74 2c 0a 20 20 20 20 20 20 .pRowSet,.
1e98f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e990 20 20 20 20 20 20 20 20 20 28 75 38 29 28 75 2e (u8)(u.
1e991 62 79 2e 69 53 65 74 3e 3d 30 20 3f 20 75 2e 62 by.iSet>=0 ? u.b
1e992 79 2e 69 53 65 74 20 26 20 30 78 66 20 3a 20 30 y.iSet & 0xf : 0
1e993 78 66 66 29 2c 0a 20 20 20 20 20 20 20 20 20 20 xff),.
1e994 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e995 20 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 29 3b pIn3->u.i);
1e996 0a 20 20 20 20 69 66 28 20 75 2e 62 79 2e 65 78 . if( u.by.ex
1e997 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 70 63 ists ){. pc
1e998 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1e999 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1e99a 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 62 }. }. if( u.b
1e99b 79 2e 69 53 65 74 3e 3d 30 20 29 7b 0a 20 20 20 y.iSet>=0 ){.
1e99c 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e sqlite3RowSetIn
1e99d 73 65 72 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f sert(pIn1->u.pRo
1e99e 77 53 65 74 2c 20 70 49 6e 33 2d 3e 75 2e 69 29 wSet, pIn3->u.i)
1e99f 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1e9a0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
1e9a1 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 0a E_OMIT_TRIGGER..
1e9a2 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 6f 67 72 /* Opcode: Progr
1e9a3 61 6d 20 50 31 20 50 32 20 50 33 20 50 34 20 2a am P1 P2 P3 P4 *
1e9a4 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 .**.** Execute t
1e9a5 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 he trigger progr
1e9a6 61 6d 20 70 61 73 73 65 64 20 61 73 20 50 34 20 am passed as P4
1e9a7 28 74 79 70 65 20 50 34 5f 53 55 42 50 52 4f 47 (type P4_SUBPROG
1e9a8 52 41 4d 29 2e 20 0a 2a 2a 0a 2a 2a 20 50 31 20 RAM). .**.** P1
1e9a9 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 61 64 64 contains the add
1e9aa 72 65 73 73 20 6f 66 20 74 68 65 20 6d 65 6d 6f ress of the memo
1e9ab 72 79 20 63 65 6c 6c 20 74 68 61 74 20 63 6f 6e ry cell that con
1e9ac 74 61 69 6e 73 20 74 68 65 20 66 69 72 73 74 20 tains the first
1e9ad 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c 6c 20 memory .** cell
1e9ae 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76 in an array of v
1e9af 61 6c 75 65 73 20 75 73 65 64 20 61 73 20 61 72 alues used as ar
1e9b0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 73 guments to the s
1e9b1 75 62 2d 70 72 6f 67 72 61 6d 2e 20 50 32 20 0a ub-program. P2 .
1e9b2 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 ** contains the
1e9b3 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 address to jump
1e9b4 74 6f 20 69 66 20 74 68 65 20 73 75 62 2d 70 72 to if the sub-pr
1e9b5 6f 67 72 61 6d 20 74 68 72 6f 77 73 20 61 6e 20 ogram throws an
1e9b6 49 47 4e 4f 52 45 20 0a 2a 2a 20 65 78 63 65 70 IGNORE .** excep
1e9b7 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 20 52 tion using the R
1e9b8 41 49 53 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e AISE() function.
1e9b9 20 52 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e Register P3 con
1e9ba 74 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 tains the addres
1e9bb 73 20 0a 2a 2a 20 6f 66 20 61 20 6d 65 6d 6f 72 s .** of a memor
1e9bc 79 20 63 65 6c 6c 20 69 6e 20 74 68 69 73 20 28 y cell in this (
1e9bd 74 68 65 20 70 61 72 65 6e 74 29 20 56 4d 20 74 the parent) VM t
1e9be 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 61 hat is used to a
1e9bf 6c 6c 6f 63 61 74 65 20 74 68 65 20 0a 2a 2a 20 llocate the .**
1e9c0 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 memory required
1e9c1 62 79 20 74 68 65 20 73 75 62 2d 76 64 62 65 20 by the sub-vdbe
1e9c2 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2a 0a 2a at runtime..**.*
1e9c3 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
1e9c4 72 20 74 6f 20 74 68 65 20 56 4d 20 63 6f 6e 74 r to the VM cont
1e9c5 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67 aining the trigg
1e9c6 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 er program..*/.c
1e9c7 61 73 65 20 4f 50 5f 50 72 6f 67 72 61 6d 3a 20 ase OP_Program:
1e9c8 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1e9c9 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1e9ca 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e9cb 76 65 64 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f ved into u.bz */
1e9cc 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 . int nMem;
1e9cd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1e9ce 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 72 mber of memory r
1e9cf 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 75 62 egisters for sub
1e9d0 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e -program */. in
1e9d1 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 t nByte;
1e9d2 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
1e9d3 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20 f runtime space
1e9d4 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 75 62 required for sub
1e9d5 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 4d 65 -program */. Me
1e9d6 6d 20 2a 70 52 74 3b 20 20 20 20 20 20 20 20 20 m *pRt;
1e9d7 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
1e9d8 72 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 72 75 r to allocate ru
1e9d9 6e 74 69 6d 65 20 73 70 61 63 65 20 2a 2f 0a 20 ntime space */.
1e9da 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 Mem *pMem;
1e9db 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
1e9dc 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f to iterate thro
1e9dd 75 67 68 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 ugh memory cells
1e9de 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 45 6e 64 3b */. Mem *pEnd;
1e9df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e9e0 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65 6c Last memory cel
1e9e1 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20 2a l in new array *
1e9e2 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 /. VdbeFrame *p
1e9e3 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e Frame; /* N
1e9e4 65 77 20 76 64 62 65 20 66 72 61 6d 65 20 74 6f ew vdbe frame to
1e9e5 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f 0a 20 execute in */.
1e9e6 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 SubProgram *pPr
1e9e7 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 53 75 62 2d ogram; /* Sub-
1e9e8 70 72 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 program to execu
1e9e9 74 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 3b te */. void *t;
1e9ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e9eb 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 /* Token identif
1e9ec 79 69 6e 67 20 74 72 69 67 67 65 72 20 2a 2f 0a ying trigger */.
1e9ed 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e9ee 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e9ef 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f 0a 0a 20 20 into u.bz */..
1e9f0 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 20 3d 20 u.bz.pProgram =
1e9f1 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d pOp->p4.pProgram
1e9f2 3b 0a 20 20 75 2e 62 7a 2e 70 52 74 20 3d 20 26 ;. u.bz.pRt = &
1e9f3 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1e9f4 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a ;. assert( u.bz
1e9f5 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3e 30 .pProgram->nOp>0
1e9f6 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
1e9f7 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67 SQLITE_RecTrigg
1e9f8 65 72 73 20 66 6c 61 67 20 69 73 20 63 6c 65 61 ers flag is clea
1e9f9 72 2c 20 74 68 65 6e 20 72 65 63 75 72 73 69 76 r, then recursiv
1e9fa 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a e invocation of.
1e9fb 20 20 2a 2a 20 74 72 69 67 67 65 72 73 20 69 73 ** triggers is
1e9fc 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 62 61 disabled for ba
1e9fd 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 ckwards compatib
1e9fe 69 6c 69 74 79 20 28 66 6c 61 67 20 73 65 74 2f ility (flag set/
1e9ff 63 6c 65 61 72 65 64 20 62 79 0a 20 20 2a 2a 20 cleared by. **
1ea00 74 68 65 20 22 50 52 41 47 4d 41 20 72 65 63 75 the "PRAGMA recu
1ea01 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 22 20 rsive_triggers"
1ea02 63 6f 6d 6d 61 6e 64 29 2e 0a 20 20 2a 2a 0a 20 command).. **.
1ea03 20 2a 2a 20 49 74 20 69 73 20 72 65 63 75 72 73 ** It is recurs
1ea04 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f ive invocation o
1ea05 66 20 74 72 69 67 67 65 72 73 2c 20 61 74 20 74 f triggers, at t
1ea06 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68 he SQL level, th
1ea07 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 at is. ** disab
1ea08 6c 65 64 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 led. In some cas
1ea09 65 73 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 es a single trig
1ea0a 67 65 72 20 6d 61 79 20 67 65 6e 65 72 61 74 65 ger may generate
1ea0b 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20 more than one.
1ea0c 20 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 28 ** SubProgram (
1ea0d 69 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6d if the trigger m
1ea0e 61 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77 ay be executed w
1ea0f 69 74 68 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e ith more than on
1ea10 65 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a e different. **
1ea11 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 ON CONFLICT alg
1ea12 6f 72 69 74 68 6d 29 2e 20 53 75 62 50 72 6f 67 orithm). SubProg
1ea13 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61 ram structures a
1ea14 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
1ea15 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69 . ** single tri
1ea16 67 67 65 72 20 61 6c 6c 20 68 61 76 65 20 74 68 gger all have th
1ea17 65 20 73 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 e same value for
1ea18 20 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 2e the SubProgram.
1ea19 74 6f 6b 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61 token. ** varia
1ea1a 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ble.. */. if(
1ea1b 30 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 0==(db->flags&SQ
1ea1c 4c 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 LITE_RecTriggers
1ea1d 29 20 29 7b 0a 20 20 20 20 75 2e 62 7a 2e 74 20 ) ){. u.bz.t
1ea1e 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d = u.bz.pProgram-
1ea1f 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28 >token;. for(
1ea20 75 2e 62 7a 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 u.bz.pFrame=p->p
1ea21 46 72 61 6d 65 3b 20 75 2e 62 7a 2e 70 46 72 61 Frame; u.bz.pFra
1ea22 6d 65 20 26 26 20 75 2e 62 7a 2e 70 46 72 61 6d me && u.bz.pFram
1ea23 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e 62 7a 2e 74 e->token!=u.bz.t
1ea24 3b 20 75 2e 62 7a 2e 70 46 72 61 6d 65 3d 75 2e ; u.bz.pFrame=u.
1ea25 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 bz.pFrame->pPare
1ea26 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 nt);. if( u.b
1ea27 7a 2e 70 46 72 61 6d 65 20 29 20 62 72 65 61 6b z.pFrame ) break
1ea28 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e ;. }.. if( p->
1ea29 6e 46 72 61 6d 65 3e 64 62 2d 3e 61 4c 69 6d 69 nFrame>db->aLimi
1ea2a 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 t[SQLITE_LIMIT_T
1ea2b 52 49 47 47 45 52 5f 44 45 50 54 48 5d 20 29 7b RIGGER_DEPTH] ){
1ea2c 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1ea2d 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 _ERROR;. sqli
1ea2e 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1ea2f 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 74 >zErrMsg, db, "t
1ea30 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f oo many levels o
1ea31 66 20 74 72 69 67 67 65 72 20 72 65 63 75 72 73 f trigger recurs
1ea32 69 6f 6e 22 29 3b 0a 20 20 20 20 62 72 65 61 6b ion");. break
1ea33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67 69 ;. }.. /* Regi
1ea34 73 74 65 72 20 75 2e 62 7a 2e 70 52 74 20 69 73 ster u.bz.pRt is
1ea35 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 used to store t
1ea36 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 he memory requir
1ea37 65 64 20 74 6f 20 73 61 76 65 20 74 68 65 20 73 ed to save the s
1ea38 74 61 74 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 tate. ** of the
1ea39 20 63 75 72 72 65 6e 74 20 70 72 6f 67 72 61 6d current program
1ea3a 2c 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79 , and the memory
1ea3b 20 72 65 71 75 69 72 65 64 20 61 74 20 72 75 6e required at run
1ea3c 74 69 6d 65 20 74 6f 20 65 78 65 63 75 74 65 0a time to execute.
1ea3d 20 20 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 ** the trigger
1ea3e 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 69 program. If thi
1ea3f 73 20 74 72 69 67 67 65 72 20 68 61 73 20 62 65 s trigger has be
1ea40 65 6e 20 66 69 72 65 64 20 62 65 66 6f 72 65 2c en fired before,
1ea41 20 74 68 65 6e 20 75 2e 62 7a 2e 70 52 74 0a 20 then u.bz.pRt.
1ea42 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 61 ** is already a
1ea43 6c 6c 6f 63 61 74 65 64 2e 20 4f 74 68 65 72 77 llocated. Otherw
1ea44 69 73 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20 ise, it must be
1ea45 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a 2f initialized. */
1ea46 0a 20 20 69 66 28 20 28 75 2e 62 7a 2e 70 52 74 . if( (u.bz.pRt
1ea47 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d ->flags&MEM_Fram
1ea48 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 e)==0 ){. /*
1ea49 53 75 62 50 72 6f 67 72 61 6d 2e 6e 4d 65 6d 20 SubProgram.nMem
1ea4a 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 is set to the nu
1ea4b 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 mber of memory c
1ea4c 65 6c 6c 73 20 75 73 65 64 20 62 79 20 74 68 65 ells used by the
1ea4d 0a 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 . ** program
1ea4e 73 74 6f 72 65 64 20 69 6e 20 53 75 62 50 72 6f stored in SubPro
1ea4f 67 72 61 6d 2e 61 4f 70 2e 20 41 73 20 77 65 6c gram.aOp. As wel
1ea50 6c 20 61 73 20 74 68 65 73 65 2c 20 6f 6e 65 20 l as these, one
1ea51 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 63 65 memory. ** ce
1ea52 6c 6c 20 69 73 20 72 65 71 75 69 72 65 64 20 66 ll is required f
1ea53 6f 72 20 65 61 63 68 20 63 75 72 73 6f 72 20 75 or each cursor u
1ea54 73 65 64 20 62 79 20 74 68 65 20 70 72 6f 67 72 sed by the progr
1ea55 61 6d 2e 20 53 65 74 20 6c 6f 63 61 6c 0a 20 20 am. Set local.
1ea56 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 75 2e ** variable u.
1ea57 62 7a 2e 6e 4d 65 6d 20 28 61 6e 64 20 6c 61 74 bz.nMem (and lat
1ea58 65 72 2c 20 56 64 62 65 46 72 61 6d 65 2e 6e 43 er, VdbeFrame.nC
1ea59 68 69 6c 64 4d 65 6d 29 20 74 6f 20 74 68 69 73 hildMem) to this
1ea5a 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 value.. */.
1ea5b 20 20 20 75 2e 62 7a 2e 6e 4d 65 6d 20 3d 20 75 u.bz.nMem = u
1ea5c 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d .bz.pProgram->nM
1ea5d 65 6d 20 2b 20 75 2e 62 7a 2e 70 50 72 6f 67 72 em + u.bz.pProgr
1ea5e 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 75 2e am->nCsr;. u.
1ea5f 62 7a 2e 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 bz.nByte = ROUND
1ea60 38 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61 8(sizeof(VdbeFra
1ea61 6d 65 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 me)).
1ea62 20 20 20 2b 20 75 2e 62 7a 2e 6e 4d 65 6d 20 2a + u.bz.nMem *
1ea63 20 73 69 7a 65 6f 66 28 4d 65 6d 29 0a 20 20 20 sizeof(Mem).
1ea64 20 20 20 20 20 20 20 20 20 20 20 2b 20 75 2e 62 + u.b
1ea65 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 z.pProgram->nCsr
1ea66 20 2a 20 73 69 7a 65 6f 66 28 56 64 62 65 43 75 * sizeof(VdbeCu
1ea67 72 73 6f 72 20 2a 29 3b 0a 20 20 20 20 75 2e 62 rsor *);. u.b
1ea68 7a 2e 70 46 72 61 6d 65 20 3d 20 73 71 6c 69 74 z.pFrame = sqlit
1ea69 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 e3DbMallocZero(d
1ea6a 62 2c 20 75 2e 62 7a 2e 6e 42 79 74 65 29 3b 0a b, u.bz.nByte);.
1ea6b 20 20 20 20 69 66 28 20 21 75 2e 62 7a 2e 70 46 if( !u.bz.pF
1ea6c 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 67 6f rame ){. go
1ea6d 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d to no_mem;. }
1ea6e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1ea6f 4d 65 6d 52 65 6c 65 61 73 65 28 75 2e 62 7a 2e MemRelease(u.bz.
1ea70 70 52 74 29 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 pRt);. u.bz.p
1ea71 52 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f Rt->flags = MEM_
1ea72 46 72 61 6d 65 3b 0a 20 20 20 20 75 2e 62 7a 2e Frame;. u.bz.
1ea73 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 20 3d 20 pRt->u.pFrame =
1ea74 75 2e 62 7a 2e 70 46 72 61 6d 65 3b 0a 0a 20 20 u.bz.pFrame;..
1ea75 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 76 u.bz.pFrame->v
1ea76 20 3d 20 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 = p;. u.bz.p
1ea77 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d Frame->nChildMem
1ea78 20 3d 20 75 2e 62 7a 2e 6e 4d 65 6d 3b 0a 20 20 = u.bz.nMem;.
1ea79 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e u.bz.pFrame->n
1ea7a 43 68 69 6c 64 43 73 72 20 3d 20 75 2e 62 7a 2e ChildCsr = u.bz.
1ea7b 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a pProgram->nCsr;.
1ea7c 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d u.bz.pFrame-
1ea7d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 75 2e >pc = pc;. u.
1ea7e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 20 bz.pFrame->aMem
1ea7f 3d 20 70 2d 3e 61 4d 65 6d 3b 0a 20 20 20 20 75 = p->aMem;. u
1ea80 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d .bz.pFrame->nMem
1ea81 20 3d 20 70 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 = p->nMem;.
1ea82 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 70 43 u.bz.pFrame->apC
1ea83 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 3b 0a 20 sr = p->apCsr;.
1ea84 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e u.bz.pFrame->
1ea85 6e 43 75 72 73 6f 72 20 3d 20 70 2d 3e 6e 43 75 nCursor = p->nCu
1ea86 72 73 6f 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 rsor;. u.bz.p
1ea87 46 72 61 6d 65 2d 3e 61 4f 70 20 3d 20 70 2d 3e Frame->aOp = p->
1ea88 61 4f 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 aOp;. u.bz.pF
1ea89 72 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70 2d 3e 6e rame->nOp = p->n
1ea8a 4f 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 Op;. u.bz.pFr
1ea8b 61 6d 65 2d 3e 74 6f 6b 65 6e 20 3d 20 75 2e 62 ame->token = u.b
1ea8c 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65 z.pProgram->toke
1ea8d 6e 3b 0a 0a 20 20 20 20 75 2e 62 7a 2e 70 45 6e n;.. u.bz.pEn
1ea8e 64 20 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65 d = &VdbeFrameMe
1ea8f 6d 28 75 2e 62 7a 2e 70 46 72 61 6d 65 29 5b 75 m(u.bz.pFrame)[u
1ea90 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 .bz.pFrame->nChi
1ea91 6c 64 4d 65 6d 5d 3b 0a 20 20 20 20 66 6f 72 28 ldMem];. for(
1ea92 75 2e 62 7a 2e 70 4d 65 6d 3d 56 64 62 65 46 72 u.bz.pMem=VdbeFr
1ea93 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 61 ameMem(u.bz.pFra
1ea94 6d 65 29 3b 20 75 2e 62 7a 2e 70 4d 65 6d 21 3d me); u.bz.pMem!=
1ea95 75 2e 62 7a 2e 70 45 6e 64 3b 20 75 2e 62 7a 2e u.bz.pEnd; u.bz.
1ea96 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 75 pMem++){. u
1ea97 2e 62 7a 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 .bz.pMem->flags
1ea98 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 = MEM_Null;.
1ea99 20 20 75 2e 62 7a 2e 70 4d 65 6d 2d 3e 64 62 20 u.bz.pMem->db
1ea9a 3d 20 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 = db;. }. }e
1ea9b 6c 73 65 7b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 lse{. u.bz.pF
1ea9c 72 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 52 74 2d rame = u.bz.pRt-
1ea9d 3e 75 2e 70 46 72 61 6d 65 3b 0a 20 20 20 20 61 >u.pFrame;. a
1ea9e 73 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f ssert( u.bz.pPro
1ea9f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e 62 7a 2e gram->nMem+u.bz.
1eaa0 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d pProgram->nCsr==
1eaa1 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 u.bz.pFrame->nCh
1eaa2 69 6c 64 4d 65 6d 20 29 3b 0a 20 20 20 20 61 73 ildMem );. as
1eaa3 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f 67 sert( u.bz.pProg
1eaa4 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 7a 2e ram->nCsr==u.bz.
1eaa5 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 pFrame->nChildCs
1eaa6 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 r );. assert(
1eaa7 20 70 63 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 pc==u.bz.pFrame
1eaa8 2d 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 70 ->pc );. }.. p
1eaa9 2d 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20 20 75 2e ->nFrame++;. u.
1eaaa 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 bz.pFrame->pPare
1eaab 6e 74 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a nt = p->pFrame;.
1eaac 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6c u.bz.pFrame->l
1eaad 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c astRowid = db->l
1eaae 61 73 74 52 6f 77 69 64 3b 0a 20 20 75 2e 62 7a astRowid;. u.bz
1eaaf 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 .pFrame->nChange
1eab0 20 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 = p->nChange;.
1eab1 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b p->nChange = 0;
1eab2 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 75 . p->pFrame = u
1eab3 2e 62 7a 2e 70 46 72 61 6d 65 3b 0a 20 20 70 2d .bz.pFrame;. p-
1eab4 3e 61 4d 65 6d 20 3d 20 26 56 64 62 65 46 72 61 >aMem = &VdbeFra
1eab5 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 61 6d meMem(u.bz.pFram
1eab6 65 29 5b 2d 31 5d 3b 0a 20 20 70 2d 3e 6e 4d 65 e)[-1];. p->nMe
1eab7 6d 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d m = u.bz.pFrame-
1eab8 3e 6e 43 68 69 6c 64 4d 65 6d 3b 0a 20 20 70 2d >nChildMem;. p-
1eab9 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 29 >nCursor = (u16)
1eaba 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 u.bz.pFrame->nCh
1eabb 69 6c 64 43 73 72 3b 0a 20 20 70 2d 3e 61 70 43 ildCsr;. p->apC
1eabc 73 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 sr = (VdbeCursor
1eabd 20 2a 2a 29 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e **)&p->aMem[p->
1eabe 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d 3e 61 4f nMem+1];. p->aO
1eabf 70 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 p = u.bz.pProgra
1eac0 6d 2d 3e 61 4f 70 3b 0a 20 20 70 2d 3e 6e 4f 70 m->aOp;. p->nOp
1eac1 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d = u.bz.pProgram
1eac2 2d 3e 6e 4f 70 3b 0a 20 20 70 63 20 3d 20 2d 31 ->nOp;. pc = -1
1eac3 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f ;.. break;.}../
1eac4 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 61 6d 20 * Opcode: Param
1eac5 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1eac6 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 * This opcode is
1eac7 20 6f 6e 6c 79 20 65 76 65 72 20 70 72 65 73 65 only ever prese
1eac8 6e 74 20 69 6e 20 73 75 62 2d 70 72 6f 67 72 61 nt in sub-progra
1eac9 6d 73 20 63 61 6c 6c 65 64 20 76 69 61 20 74 68 ms called via th
1eaca 65 20 0a 2a 2a 20 4f 50 5f 50 72 6f 67 72 61 6d e .** OP_Program
1eacb 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 43 6f instruction. Co
1eacc 70 79 20 61 20 76 61 6c 75 65 20 63 75 72 72 65 py a value curre
1eacd 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 61 ntly stored in a
1eace 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c 6c memory .** cell
1eacf 20 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 of the calling
1ead0 28 70 61 72 65 6e 74 29 20 66 72 61 6d 65 20 74 (parent) frame t
1ead1 6f 20 63 65 6c 6c 20 50 32 20 69 6e 20 74 68 65 o cell P2 in the
1ead2 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 73 20 current frames
1ead3 0a 2a 2a 20 61 64 64 72 65 73 73 20 73 70 61 63 .** address spac
1ead4 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 e. This is used
1ead5 62 79 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 by trigger progr
1ead6 61 6d 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 ams to access th
1ead7 65 20 6e 65 77 2e 2a 20 0a 2a 2a 20 61 6e 64 20 e new.* .** and
1ead8 6f 6c 64 2e 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a old.* values..**
1ead9 0a 2a 2a 20 54 68 65 20 61 64 64 72 65 73 73 20 .** The address
1eada 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 of the cell in t
1eadb 68 65 20 70 61 72 65 6e 74 20 66 72 61 6d 65 20 he parent frame
1eadc 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 is determined by
1eadd 20 61 64 64 69 6e 67 0a 2a 2a 20 74 68 65 20 76 adding.** the v
1eade 61 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20 61 alue of the P1 a
1eadf 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 76 rgument to the v
1eae0 61 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20 61 alue of the P1 a
1eae1 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a rgument to the.*
1eae2 2a 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f * calling OP_Pro
1eae3 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e gram instruction
1eae4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 ..*/.case OP_Par
1eae5 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 am: {
1eae6 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1eae7 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 se */.#if 0 /*
1eae8 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1eae9 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 moved into u.ca
1eaea 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a */. VdbeFrame *
1eaeb 70 46 72 61 6d 65 3b 0a 20 20 4d 65 6d 20 2a 70 pFrame;. Mem *p
1eaec 49 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f In;.#endif /* lo
1eaed 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1eaee 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a 2f ved into u.ca */
1eaef 0a 20 20 75 2e 63 61 2e 70 46 72 61 6d 65 20 3d . u.ca.pFrame =
1eaf0 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 75 2e p->pFrame;. u.
1eaf1 63 61 2e 70 49 6e 20 3d 20 26 75 2e 63 61 2e 70 ca.pIn = &u.ca.p
1eaf2 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d Frame->aMem[pOp-
1eaf3 3e 70 31 20 2b 20 75 2e 63 61 2e 70 46 72 61 6d >p1 + u.ca.pFram
1eaf4 65 2d 3e 61 4f 70 5b 75 2e 63 61 2e 70 46 72 61 e->aOp[u.ca.pFra
1eaf5 6d 65 2d 3e 70 63 5d 2e 70 31 5d 3b 0a 20 20 73 me->pc].p1];. s
1eaf6 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
1eaf7 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 75 llowCopy(pOut, u
1eaf8 2e 63 61 2e 70 49 6e 2c 20 4d 45 4d 5f 45 70 68 .ca.pIn, MEM_Eph
1eaf9 65 6d 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a em);. break;.}.
1eafa 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 .#endif /* #ifnd
1eafb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1eafc 52 49 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 RIGGER */..#ifnd
1eafd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1eafe 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20 UTOINCREMENT./*
1eaff 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50 Opcode: MemMax P
1eb00 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1eb01 20 50 31 20 69 73 20 61 20 72 65 67 69 73 74 65 P1 is a registe
1eb02 72 20 69 6e 20 74 68 65 20 72 6f 6f 74 20 66 72 r in the root fr
1eb03 61 6d 65 20 6f 66 20 74 68 69 73 20 56 4d 20 28 ame of this VM (
1eb04 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 69 the root frame i
1eb05 73 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 66 s.** different f
1eb06 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 rom the current
1eb07 66 72 61 6d 65 20 69 66 20 74 68 69 73 20 69 6e frame if this in
1eb08 73 74 72 75 63 74 69 6f 6e 20 69 73 20 62 65 69 struction is bei
1eb09 6e 67 20 65 78 65 63 75 74 65 64 0a 2a 2a 20 77 ng executed.** w
1eb0a 69 74 68 69 6e 20 61 20 73 75 62 2d 70 72 6f 67 ithin a sub-prog
1eb0b 72 61 6d 29 2e 20 53 65 74 20 74 68 65 20 76 61 ram). Set the va
1eb0c 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20 lue of register
1eb0d 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 P1 to the maximu
1eb0e 6d 20 6f 66 20 0a 2a 2a 20 69 74 73 20 63 75 72 m of .** its cur
1eb0f 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20 74 rent value and t
1eb10 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1eb11 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 ster P2..**.** T
1eb12 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1eb13 74 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 20 throws an error
1eb14 69 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 if the memory ce
1eb15 6c 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61 ll is not initia
1eb16 6c 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 lly.** an intege
1eb17 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65 r..*/.case OP_Me
1eb18 6d 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 2f mMax: { /
1eb19 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 * in2 */.#if 0
1eb1a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1eb1b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1eb1c 63 62 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e cb */. Mem *pIn
1eb1d 31 3b 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 1;. VdbeFrame *
1eb1e 70 46 72 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f pFrame;.#endif /
1eb1f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1eb20 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1eb21 62 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 46 b */. if( p->pF
1eb22 72 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 28 rame ){. for(
1eb23 75 2e 63 62 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 u.cb.pFrame=p->p
1eb24 46 72 61 6d 65 3b 20 75 2e 63 62 2e 70 46 72 61 Frame; u.cb.pFra
1eb25 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 2e 63 me->pParent; u.c
1eb26 62 2e 70 46 72 61 6d 65 3d 75 2e 63 62 2e 70 46 b.pFrame=u.cb.pF
1eb27 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a rame->pParent);.
1eb28 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 20 3d 20 u.cb.pIn1 =
1eb29 26 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 61 4d &u.cb.pFrame->aM
1eb2a 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 7d em[pOp->p1];. }
1eb2b 65 6c 73 65 7b 0a 20 20 20 20 75 2e 63 62 2e 70 else{. u.cb.p
1eb2c 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 In1 = &p->aMem[p
1eb2d 4f 70 2d 3e 70 31 5d 3b 0a 20 20 7d 0a 20 20 73 Op->p1];. }. s
1eb2e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1eb2f 65 67 65 72 69 66 79 28 75 2e 63 62 2e 70 49 6e egerify(u.cb.pIn
1eb30 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 1);. sqlite3Vdb
1eb31 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
1eb32 49 6e 32 29 3b 0a 20 20 69 66 28 20 75 2e 63 62 In2);. if( u.cb
1eb33 2e 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32 2d .pIn1->u.i<pIn2-
1eb34 3e 75 2e 69 29 7b 0a 20 20 20 20 75 2e 63 62 2e >u.i){. u.cb.
1eb35 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e 32 pIn1->u.i = pIn2
1eb36 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 65 ->u.i;. }. bre
1eb37 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1eb38 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
1eb39 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a INCREMENT */../*
1eb3a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20 50 Opcode: IfPos P
1eb3b 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1eb3c 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 If the value of
1eb3d 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1eb3e 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a 75 1 or greater, ju
1eb3f 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 mp to P2..**.**
1eb40 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f It is illegal to
1eb41 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 use this instru
1eb42 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 ction on a regis
1eb43 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a ter that does.**
1eb44 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 not contain an
1eb45 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 integer. An ass
1eb46 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c ertion fault wil
1eb47 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 l result if you
1eb48 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f try..*/.case OP_
1eb49 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20 20 IfPos: {
1eb4a 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a /* jump, in1 */.
1eb4b 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e assert( pIn1->
1eb4c 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b flags&MEM_Int );
1eb4d 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 . if( pIn1->u.i
1eb4e 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 >0 ){. pc =
1eb4f 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1eb50 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1eb51 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20 50 31 Opcode: IfNeg P1
1eb52 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1eb53 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 If the value of
1eb54 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6c register P1 is l
1eb55 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 6a ess than zero, j
1eb56 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a ump to P2. .**.*
1eb57 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 * It is illegal
1eb58 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 to use this inst
1eb59 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 ruction on a reg
1eb5a 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a ister that does.
1eb5b 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 ** not contain a
1eb5c 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 n integer. An a
1eb5d 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 ssertion fault w
1eb5e 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f ill result if yo
1eb5f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f u try..*/.case O
1eb60 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20 20 20 P_IfNeg: {
1eb61 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a /* jump, in1 *
1eb62 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 /. assert( pIn1
1eb63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 ->flags&MEM_Int
1eb64 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 );. if( pIn1->u
1eb65 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70 63 20 .i<0 ){. pc
1eb66 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1eb67 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1eb68 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65 72 6f * Opcode: IfZero
1eb69 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1eb6a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ** If the value
1eb6b 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69 of register P1 i
1eb6c 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75 6d s exactly 0, jum
1eb6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 p to P2. .**.**
1eb6e 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f It is illegal to
1eb6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 use this instru
1eb70 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 ction on a regis
1eb71 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a ter that does.**
1eb72 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 not contain an
1eb73 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 integer. An ass
1eb74 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c ertion fault wil
1eb75 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 l result if you
1eb76 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f try..*/.case OP_
1eb77 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 20 IfZero: {
1eb78 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f /* jump, in1 */
1eb79 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d . assert( pIn1-
1eb7a 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 >flags&MEM_Int )
1eb7b 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e ;. if( pIn1->u.
1eb7c 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63 20 i==0 ){. pc
1eb7d 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1eb7e 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1eb7f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74 65 * Opcode: AggSte
1eb80 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35 0a p * P2 P3 P4 P5.
1eb81 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 **.** Execute th
1eb82 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 e step function
1eb83 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 for an aggregate
1eb84 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74 69 . The.** functi
1eb85 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d 65 on has P5 argume
1eb86 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20 70 nts. P4 is a p
1eb87 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 ointer to the Fu
1eb88 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 ncDef.** structu
1eb89 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65 re that specifie
1eb8a 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 s the function.
1eb8b 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a 2a Use register.**
1eb8c 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75 6d P3 as the accum
1eb8d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ulator..**.** Th
1eb8e 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 61 e P5 arguments a
1eb8f 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 re taken from re
1eb90 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69 74 gister P2 and it
1eb91 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e s.** successors.
1eb92 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 53 .*/.case OP_AggS
1eb93 74 65 70 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a tep: {.#if 0 /*
1eb94 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1eb95 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 63 moved into u.cc
1eb96 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69 */. int n;. i
1eb97 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 nt i;. Mem *pMe
1eb98 6d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a m;. Mem *pRec;.
1eb99 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1eb9a 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 33 t ctx;. sqlite3
1eb9b 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a _value **apVal;.
1eb9c 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1eb9d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1eb9e 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 0a 20 20 into u.cc */..
1eb9f 75 2e 63 63 2e 6e 20 3d 20 70 4f 70 2d 3e 70 35 u.cc.n = pOp->p5
1eba0 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 63 ;. assert( u.cc
1eba1 2e 6e 3e 3d 30 20 29 3b 0a 20 20 75 2e 63 63 2e .n>=0 );. u.cc.
1eba2 70 52 65 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pRec = &p->aMem[
1eba3 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 75 2e 63 63 pOp->p2];. u.cc
1eba4 2e 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 .apVal = p->apAr
1eba5 67 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 g;. assert( u.c
1eba6 63 2e 61 70 56 61 6c 20 7c 7c 20 75 2e 63 63 2e c.apVal || u.cc.
1eba7 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 75 2e n==0 );. for(u.
1eba8 63 63 2e 69 3d 30 3b 20 75 2e 63 63 2e 69 3c 75 cc.i=0; u.cc.i<u
1eba9 2e 63 63 2e 6e 3b 20 75 2e 63 63 2e 69 2b 2b 2c .cc.n; u.cc.i++,
1ebaa 20 75 2e 63 63 2e 70 52 65 63 2b 2b 29 7b 0a 20 u.cc.pRec++){.
1ebab 20 20 20 75 2e 63 63 2e 61 70 56 61 6c 5b 75 2e u.cc.apVal[u.
1ebac 63 63 2e 69 5d 20 3d 20 75 2e 63 63 2e 70 52 65 cc.i] = u.cc.pRe
1ebad 63 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 c;. storeType
1ebae 49 6e 66 6f 28 75 2e 63 63 2e 70 52 65 63 2c 20 Info(u.cc.pRec,
1ebaf 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 encoding);. }.
1ebb0 20 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e 63 20 u.cc.ctx.pFunc
1ebb1 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b = pOp->p4.pFunc;
1ebb2 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1ebb3 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c p3>0 && pOp->p3<
1ebb4 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e =p->nMem );. u.
1ebb5 63 63 2e 63 74 78 2e 70 4d 65 6d 20 3d 20 75 2e cc.ctx.pMem = u.
1ebb6 63 63 2e 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d cc.pMem = &p->aM
1ebb7 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 em[pOp->p3];. u
1ebb8 2e 63 63 2e 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20 .cc.pMem->n++;.
1ebb9 20 75 2e 63 63 2e 63 74 78 2e 73 2e 66 6c 61 67 u.cc.ctx.s.flag
1ebba 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1ebbb 75 2e 63 63 2e 63 74 78 2e 73 2e 7a 20 3d 20 30 u.cc.ctx.s.z = 0
1ebbc 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 2e 7a ;. u.cc.ctx.s.z
1ebbd 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 75 2e Malloc = 0;. u.
1ebbe 63 63 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 cc.ctx.s.xDel =
1ebbf 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 2e 0;. u.cc.ctx.s.
1ebc0 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 63 63 2e db = db;. u.cc.
1ebc1 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b ctx.isError = 0;
1ebc2 0a 20 20 75 2e 63 63 2e 63 74 78 2e 70 43 6f 6c . u.cc.ctx.pCol
1ebc3 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e 63 l = 0;. if( u.c
1ebc4 63 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 c.ctx.pFunc->fla
1ebc5 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 gs & SQLITE_FUNC
1ebc6 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 _NEEDCOLL ){.
1ebc7 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e assert( pOp>p->
1ebc8 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 aOp );. asser
1ebc9 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 t( pOp[-1].p4typ
1ebca 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b e==P4_COLLSEQ );
1ebcb 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1ebcc 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f [-1].opcode==OP_
1ebcd 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 75 CollSeq );. u
1ebce 2e 63 63 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 .cc.ctx.pColl =
1ebcf 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c pOp[-1].p4.pColl
1ebd0 3b 0a 20 20 7d 0a 20 20 28 75 2e 63 63 2e 63 74 ;. }. (u.cc.ct
1ebd1 78 2e 70 46 75 6e 63 2d 3e 78 53 74 65 70 29 28 x.pFunc->xStep)(
1ebd2 26 75 2e 63 63 2e 63 74 78 2c 20 75 2e 63 63 2e &u.cc.ctx, u.cc.
1ebd3 6e 2c 20 75 2e 63 63 2e 61 70 56 61 6c 29 3b 0a n, u.cc.apVal);.
1ebd4 20 20 69 66 28 20 75 2e 63 63 2e 63 74 78 2e 69 if( u.cc.ctx.i
1ebd5 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 71 sError ){. sq
1ebd6 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1ebd7 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1ebd8 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 "%s", sqlite3_va
1ebd9 6c 75 65 5f 74 65 78 74 28 26 75 2e 63 63 2e 63 lue_text(&u.cc.c
1ebda 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d tx.s));. rc =
1ebdb 20 75 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f u.cc.ctx.isErro
1ebdc 72 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 r;. }. sqlite3
1ebdd 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1ebde 75 2e 63 63 2e 63 74 78 2e 73 29 3b 0a 20 20 62 u.cc.ctx.s);. b
1ebdf 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1ebe0 64 65 3a 20 41 67 67 46 69 6e 61 6c 20 50 31 20 de: AggFinal P1
1ebe1 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * P4 *.**.**
1ebe2 45 78 65 63 75 74 65 20 74 68 65 20 66 69 6e 61 Execute the fina
1ebe3 6c 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 lizer function f
1ebe4 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e or an aggregate.
1ebe5 20 20 50 31 20 69 73 0a 2a 2a 20 74 68 65 20 6d P1 is.** the m
1ebe6 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 emory location t
1ebe7 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75 6d hat is the accum
1ebe8 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20 61 ulator for the a
1ebe9 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 ggregate..**.**
1ebea 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 P2 is the number
1ebeb 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 of arguments th
1ebec 61 74 20 74 68 65 20 73 74 65 70 20 66 75 6e 63 at the step func
1ebed 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64 0a 2a tion takes and.*
1ebee 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
1ebef 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 r to the FuncDef
1ebf0 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 74 69 for this functi
1ebf1 6f 6e 2e 20 20 54 68 65 20 50 32 0a 2a 2a 20 61 on. The P2.** a
1ebf2 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 75 rgument is not u
1ebf3 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 6f sed by this opco
1ebf4 64 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 de. It is only
1ebf5 74 68 65 72 65 20 74 6f 20 64 69 73 61 6d 62 69 there to disambi
1ebf6 67 75 61 74 65 0a 2a 2a 20 66 75 6e 63 74 69 6f guate.** functio
1ebf7 6e 73 20 74 68 61 74 20 63 61 6e 20 74 61 6b 65 ns that can take
1ebf8 20 76 61 72 79 69 6e 67 20 6e 75 6d 62 65 72 73 varying numbers
1ebf9 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 of arguments.
1ebfa 54 68 65 0a 2a 2a 20 50 34 20 61 72 67 75 6d 65 The.** P4 argume
1ebfb 6e 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64 65 nt is only neede
1ebfc 64 20 66 6f 72 20 74 68 65 20 64 65 67 65 6e 65 d for the degene
1ebfd 72 61 74 65 20 63 61 73 65 20 77 68 65 72 65 0a rate case where.
1ebfe 2a 2a 20 74 68 65 20 73 74 65 70 20 66 75 6e 63 ** the step func
1ebff 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 70 72 65 tion was not pre
1ec00 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2e 0a viously called..
1ec01 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 46 69 */.case OP_AggFi
1ec02 6e 61 6c 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a nal: {.#if 0 /*
1ec03 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1ec04 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 64 moved into u.cd
1ec05 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b */. Mem *pMem;
1ec06 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1ec07 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ec08 20 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20 into u.cd */.
1ec09 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1ec0a 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 0 && pOp->p1<=p-
1ec0b 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 64 2e >nMem );. u.cd.
1ec0c 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pMem = &p->aMem[
1ec0d 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1ec0e 72 74 28 20 28 75 2e 63 64 2e 70 4d 65 6d 2d 3e rt( (u.cd.pMem->
1ec0f 66 6c 61 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 flags & ~(MEM_Nu
1ec10 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20 ll|MEM_Agg))==0
1ec11 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1ec12 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 3VdbeMemFinalize
1ec13 28 75 2e 63 64 2e 70 4d 65 6d 2c 20 70 4f 70 2d (u.cd.pMem, pOp-
1ec14 3e 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66 >p4.pFunc);. if
1ec15 28 20 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 ( rc ){. sqli
1ec16 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1ec17 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 >zErrMsg, db, "%
1ec18 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 s", sqlite3_valu
1ec19 65 5f 74 65 78 74 28 75 2e 63 64 2e 70 4d 65 6d e_text(u.cd.pMem
1ec1a 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ));. }. sqlite
1ec1b 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
1ec1c 69 6e 67 28 75 2e 63 64 2e 70 4d 65 6d 2c 20 65 ing(u.cd.pMem, e
1ec1d 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 55 50 44 41 ncoding);. UPDA
1ec1e 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 TE_MAX_BLOBSIZE(
1ec1f 75 2e 63 64 2e 70 4d 65 6d 29 3b 0a 20 20 69 66 u.cd.pMem);. if
1ec20 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ( sqlite3VdbeMem
1ec21 54 6f 6f 42 69 67 28 75 2e 63 64 2e 70 4d 65 6d TooBig(u.cd.pMem
1ec22 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f ) ){. goto to
1ec23 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72 65 o_big;. }. bre
1ec24 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 65 66 ak;.}...#if !def
1ec25 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1ec26 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64 65 66 _VACUUM) && !def
1ec27 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1ec28 5f 41 54 54 41 43 48 29 0a 2f 2a 20 4f 70 63 6f _ATTACH)./* Opco
1ec29 64 65 3a 20 56 61 63 75 75 6d 20 2a 20 2a 20 2a de: Vacuum * * *
1ec2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 56 61 63 75 75 * *.**.** Vacuu
1ec2b 6d 20 74 68 65 20 65 6e 74 69 72 65 20 64 61 74 m the entire dat
1ec2c 61 62 61 73 65 2e 20 20 54 68 69 73 20 6f 70 63 abase. This opc
1ec2d 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 6f ode will cause o
1ec2e 74 68 65 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 ther virtual.**
1ec2f 6d 61 63 68 69 6e 65 73 20 74 6f 20 62 65 20 63 machines to be c
1ec30 72 65 61 74 65 64 20 61 6e 64 20 72 75 6e 2e 20 reated and run.
1ec31 20 49 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 It may not be c
1ec32 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 alled from withi
1ec33 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 n.** a transacti
1ec34 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 on..*/.case OP_V
1ec35 61 63 75 75 6d 3a 20 7b 0a 20 20 69 66 28 20 73 acuum: {. if( s
1ec36 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1ec37 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1ec38 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 20 _due_to_misuse;
1ec39 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 . rc = sqlite3R
1ec3a 75 6e 56 61 63 75 75 6d 28 26 70 2d 3e 7a 45 72 unVacuum(&p->zEr
1ec3b 72 4d 73 67 2c 20 64 62 29 3b 0a 20 20 69 66 28 rMsg, db);. if(
1ec3c 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
1ec3d 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1ec3e 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1ec3f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1ec40 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 if..#if !defined
1ec41 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 (SQLITE_OMIT_AUT
1ec42 4f 56 41 43 55 55 4d 29 0a 2f 2a 20 4f 70 63 6f OVACUUM)./* Opco
1ec43 64 65 3a 20 49 6e 63 72 56 61 63 75 75 6d 20 50 de: IncrVacuum P
1ec44 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1ec45 20 50 65 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c Perform a singl
1ec46 65 20 73 74 65 70 20 6f 66 20 74 68 65 20 69 6e e step of the in
1ec47 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
1ec48 20 70 72 6f 63 65 64 75 72 65 20 6f 6e 0a 2a 2a procedure on.**
1ec49 20 74 68 65 20 50 31 20 64 61 74 61 62 61 73 65 the P1 database
1ec4a 2e 20 49 66 20 74 68 65 20 76 61 63 75 75 6d 20 . If the vacuum
1ec4b 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 6a 75 has finished, ju
1ec4c 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f mp to instructio
1ec4d 6e 0a 2a 2a 20 50 32 2e 20 4f 74 68 65 72 77 69 n.** P2. Otherwi
1ec4e 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 se, fall through
1ec4f 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 to the next ins
1ec50 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 truction..*/.cas
1ec51 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 3a e OP_IncrVacuum:
1ec52 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1ec53 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c p */.#if 0 /* l
1ec54 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1ec55 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 65 20 2a oved into u.ce *
1ec56 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a /. Btree *pBt;.
1ec57 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1ec58 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ec59 69 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 0a 20 20 into u.ce */..
1ec5a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1ec5b 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 =0 && pOp->p1<db
1ec5c 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 ->nDb );. asser
1ec5d 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1ec5e 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 & (1<<pOp->p1))
1ec5f 21 3d 30 20 29 3b 0a 20 20 75 2e 63 65 2e 70 42 !=0 );. u.ce.pB
1ec60 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d t = db->aDb[pOp-
1ec61 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 72 63 20 3d >p1].pBt;. rc =
1ec62 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 sqlite3BtreeInc
1ec63 72 56 61 63 75 75 6d 28 75 2e 63 65 2e 70 42 74 rVacuum(u.ce.pBt
1ec64 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1ec65 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 ITE_DONE ){.
1ec66 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1ec67 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1ec68 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 E_OK;. }. brea
1ec69 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 k;.}.#endif../*
1ec6a 4f 70 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50 Opcode: Expire P
1ec6b 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 1 * * * *.**.**
1ec6c 43 61 75 73 65 20 70 72 65 63 6f 6d 70 69 6c 65 Cause precompile
1ec6d 64 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 d statements to
1ec6e 62 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 become expired.
1ec6f 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 74 65 An expired state
1ec70 6d 65 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 69 ment.** fails wi
1ec71 74 68 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 th an error code
1ec72 20 6f 66 20 53 51 4c 49 54 45 5f 53 43 48 45 4d of SQLITE_SCHEM
1ec73 41 20 69 66 20 69 74 20 69 73 20 65 76 65 72 20 A if it is ever
1ec74 65 78 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 69 executed .** (vi
1ec75 61 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 a sqlite3_step()
1ec76 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 20 )..** .** If P1
1ec77 69 73 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 53 is 0, then all S
1ec78 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65 QL statements be
1ec79 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 49 66 come expired. If
1ec7a 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c P1 is non-zero,
1ec7b 0a 2a 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 .** then only th
1ec7c 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 e currently exec
1ec7d 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 uting statement
1ec7e 69 73 20 61 66 66 65 63 74 65 64 2e 20 0a 2a 2f is affected. .*/
1ec7f 0a 63 61 73 65 20 4f 50 5f 45 78 70 69 72 65 3a .case OP_Expire:
1ec80 20 7b 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e 70 {. if( !pOp->p
1ec81 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 1 ){. sqlite3
1ec82 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 ExpirePreparedSt
1ec83 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 atements(db);.
1ec84 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 78 }else{. p->ex
1ec85 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 pired = 1;. }.
1ec86 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 break;.}..#ifnd
1ec87 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
1ec88 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 4f HARED_CACHE./* O
1ec89 70 63 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 6b pcode: TableLock
1ec8a 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1ec8b 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f *.** Obtain a lo
1ec8c 63 6b 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c ck on a particul
1ec8d 61 72 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69 ar table. This i
1ec8e 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 6e nstruction is on
1ec8f 6c 79 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 ly used when.**
1ec90 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 the shared-cache
1ec91 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 feature is enab
1ec92 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 50 31 20 69 led. .**.** P1 i
1ec93 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
1ec94 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 73 he database in s
1ec95 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20 6f 66 20 qlite3.aDb[] of
1ec96 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
1ec97 6f 6e 20 77 68 69 63 68 20 74 68 65 20 6c 6f 63 on which the loc
1ec98 6b 20 69 73 20 61 63 71 75 69 72 65 64 2e 20 20 k is acquired.
1ec99 41 20 72 65 61 64 6c 6f 63 6b 20 69 73 20 6f 62 A readlock is ob
1ec9a 74 61 69 6e 65 64 20 69 66 20 50 33 3d 3d 30 20 tained if P3==0
1ec9b 6f 72 0a 2a 2a 20 61 20 77 72 69 74 65 20 6c 6f or.** a write lo
1ec9c 63 6b 20 69 66 20 50 33 3d 3d 31 2e 0a 2a 2a 0a ck if P3==1..**.
1ec9d 2a 2a 20 50 32 20 63 6f 6e 74 61 69 6e 73 20 74 ** P2 contains t
1ec9e 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 he root-page of
1ec9f 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 the table to loc
1eca0 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 63 6f 6e 74 k..**.** P4 cont
1eca1 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 ains a pointer t
1eca2 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 o the name of th
1eca3 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6c 6f e table being lo
1eca4 63 6b 65 64 2e 20 54 68 69 73 20 69 73 20 6f 6e cked. This is on
1eca5 6c 79 0a 2a 2a 20 75 73 65 64 20 74 6f 20 67 65 ly.** used to ge
1eca6 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 nerate an error
1eca7 6d 65 73 73 61 67 65 20 69 66 20 74 68 65 20 6c message if the l
1eca8 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 ock cannot be ob
1eca9 74 61 69 6e 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 tained..*/.case
1ecaa 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20 7b 0a OP_TableLock: {.
1ecab 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b u8 isWriteLock
1ecac 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b 0a = (u8)pOp->p3;.
1ecad 20 20 69 66 28 20 69 73 57 72 69 74 65 4c 6f 63 if( isWriteLoc
1ecae 6b 20 7c 7c 20 30 3d 3d 28 64 62 2d 3e 66 6c 61 k || 0==(db->fla
1ecaf 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e gs&SQLITE_ReadUn
1ecb0 63 6f 6d 6d 69 74 74 65 64 29 20 29 7b 0a 20 20 committed) ){.
1ecb1 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e int p1 = pOp->
1ecb2 70 31 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28 p1; . assert(
1ecb3 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 64 62 2d p1>=0 && p1<db-
1ecb4 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 >nDb );. asse
1ecb5 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 rt( (p->btreeMas
1ecb6 6b 20 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 20 k & (1<<p1))!=0
1ecb7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 );. assert( i
1ecb8 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c sWriteLock==0 ||
1ecb9 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 isWriteLock==1
1ecba 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1ecbb 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c te3BtreeLockTabl
1ecbc 65 28 64 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 e(db->aDb[p1].pB
1ecbd 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 t, pOp->p2, isWr
1ecbe 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 iteLock);. if
1ecbf 28 20 28 72 63 26 30 78 46 46 29 3d 3d 53 51 4c ( (rc&0xFF)==SQL
1ecc0 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 ITE_LOCKED ){.
1ecc1 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
1ecc2 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 z = pOp->p4.z;.
1ecc3 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 sqlite3SetS
1ecc4 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1ecc5 67 2c 20 64 62 2c 20 22 64 61 74 61 62 61 73 65 g, db, "database
1ecc6 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 table is locked
1ecc7 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 7d : %s", z);. }
1ecc8 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1ecc9 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1ecca 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
1eccb 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 HE */..#ifndef S
1eccc 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1eccd 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 ALTABLE./* Opcod
1ecce 65 3a 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 e: VBegin * * *
1eccf 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 P4 *.**.** P4 ma
1ecd0 79 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 y be a pointer t
1ecd1 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 o an sqlite3_vta
1ecd2 62 20 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 b structure. If
1ecd3 73 6f 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a 2a so, call the .**
1ecd4 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66 xBegin method f
1ecd5 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a or that table..*
1ecd6 2a 0a 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 68 *.** Also, wheth
1ecd7 65 72 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 20 er or not P4 is
1ecd8 73 65 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 set, check that
1ecd9 74 68 69 73 20 69 73 20 6e 6f 74 20 62 65 69 6e this is not bein
1ecda 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a 2a g called from.**
1ecdb 20 77 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 61 within a callba
1ecdc 63 6b 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 ck to a virtual
1ecdd 74 61 62 6c 65 20 78 53 79 6e 63 28 29 20 6d 65 table xSync() me
1ecde 74 68 6f 64 2e 20 49 66 20 69 74 20 69 73 2c 20 thod. If it is,
1ecdf 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 the error.** cod
1ece0 65 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f e will be set to
1ece1 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a SQLITE_LOCKED..
1ece2 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 69 */.case OP_VBegi
1ece3 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c n: {.#if 0 /* l
1ece4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1ece5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 66 20 2a oved into u.cf *
1ece6 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61 /. VTable *pVTa
1ece7 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 b;.#endif /* loc
1ece8 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1ece9 65 64 20 69 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a ed into u.cf */.
1ecea 20 20 75 2e 63 66 2e 70 56 54 61 62 20 3d 20 70 u.cf.pVTab = p
1eceb 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 Op->p4.pVtab;.
1ecec 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 rc = sqlite3Vtab
1eced 42 65 67 69 6e 28 64 62 2c 20 75 2e 63 66 2e 70 Begin(db, u.cf.p
1ecee 56 54 61 62 29 3b 0a 20 20 69 66 28 20 75 2e 63 VTab);. if( u.c
1ecef 66 2e 70 56 54 61 62 20 29 7b 0a 20 20 20 20 73 f.pVTab ){. s
1ecf0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ecf1 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 p->zErrMsg);.
1ecf2 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 p->zErrMsg = u
1ecf3 2e 63 66 2e 70 56 54 61 62 2d 3e 70 56 74 61 62 .cf.pVTab->pVtab
1ecf4 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 ->zErrMsg;. u
1ecf5 2e 63 66 2e 70 56 54 61 62 2d 3e 70 56 74 61 62 .cf.pVTab->pVtab
1ecf6 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1ecf7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1ecf8 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1ecf9 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1ecfa 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1ecfb 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1ecfc 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a TABLE./* Opcode:
1ecfd 20 56 43 72 65 61 74 65 20 50 31 20 2a 20 2a 20 VCreate P1 * *
1ecfe 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 P4 *.**.** P4 is
1ecff 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 the name of a v
1ed00 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 irtual table in
1ed01 64 61 74 61 62 61 73 65 20 50 31 2e 20 43 61 6c database P1. Cal
1ed02 6c 20 74 68 65 20 78 43 72 65 61 74 65 20 6d 65 l the xCreate me
1ed03 74 68 6f 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74 thod.** for that
1ed04 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 table..*/.case
1ed05 4f 50 5f 56 43 72 65 61 74 65 3a 20 7b 0a 20 20 OP_VCreate: {.
1ed06 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 rc = sqlite3Vtab
1ed07 43 61 6c 6c 43 72 65 61 74 65 28 64 62 2c 20 70 CallCreate(db, p
1ed08 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e Op->p1, pOp->p4.
1ed09 7a 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b z, &p->zErrMsg);
1ed0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1ed0b 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1ed0c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1ed0d 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1ed0e 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1ed0f 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1ed10 44 65 73 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 Destroy P1 * * P
1ed11 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 4 *.**.** P4 is
1ed12 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 the name of a vi
1ed13 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 rtual table in d
1ed14 61 74 61 62 61 73 65 20 50 31 2e 20 20 43 61 6c atabase P1. Cal
1ed15 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d l the xDestroy m
1ed16 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 ethod.** of that
1ed17 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 table..*/.case
1ed18 4f 50 5f 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20 OP_VDestroy: {.
1ed19 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 p->inVtabMethod
1ed1a 20 3d 20 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c = 2;. rc = sql
1ed1b 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 ite3VtabCallDest
1ed1c 72 6f 79 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c roy(db, pOp->p1,
1ed1d 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70 pOp->p4.z);. p
1ed1e 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d ->inVtabMethod =
1ed1f 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 0;. break;.}.#
1ed20 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1ed21 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1ed22 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
1ed23 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1ed24 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1ed25 3a 20 56 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50 : VOpen P1 * * P
1ed26 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 4 *.**.** P4 is
1ed27 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 a pointer to a v
1ed28 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a irtual table obj
1ed29 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f ect, an sqlite3_
1ed2a 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a vtab structure..
1ed2b 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f ** P1 is a curso
1ed2c 72 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 r number. This
1ed2d 6f 70 63 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 opcode opens a c
1ed2e 75 72 73 6f 72 20 74 6f 20 74 68 65 20 76 69 72 ursor to the vir
1ed2f 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e tual.** table an
1ed30 64 20 73 74 6f 72 65 73 20 74 68 61 74 20 63 75 d stores that cu
1ed31 72 73 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 rsor in P1..*/.c
1ed32 61 73 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a ase OP_VOpen: {.
1ed33 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1ed34 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ed35 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 20 20 56 into u.cg */. V
1ed36 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b dbeCursor *pCur;
1ed37 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f . sqlite3_vtab_
1ed38 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 cursor *pVtabCur
1ed39 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 sor;. sqlite3_v
1ed3a 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 tab *pVtab;. sq
1ed3b 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d lite3_module *pM
1ed3c 6f 64 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f 2a odule;.#endif /*
1ed3d 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1ed3e 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 67 moved into u.cg
1ed3f 20 2a 2f 0a 0a 20 20 75 2e 63 67 2e 70 43 75 72 */.. u.cg.pCur
1ed40 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 70 56 74 = 0;. u.cg.pVt
1ed41 61 62 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 abCursor = 0;.
1ed42 75 2e 63 67 2e 70 56 74 61 62 20 3d 20 70 4f 70 u.cg.pVtab = pOp
1ed43 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 ->p4.pVtab->pVta
1ed44 62 3b 0a 20 20 75 2e 63 67 2e 70 4d 6f 64 75 6c b;. u.cg.pModul
1ed45 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 e = (sqlite3_mod
1ed46 75 6c 65 20 2a 29 75 2e 63 67 2e 70 56 74 61 62 ule *)u.cg.pVtab
1ed47 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 ->pModule;. ass
1ed48 65 72 74 28 75 2e 63 67 2e 70 56 74 61 62 20 26 ert(u.cg.pVtab &
1ed49 26 20 75 2e 63 67 2e 70 4d 6f 64 75 6c 65 29 3b & u.cg.pModule);
1ed4a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1ed4b 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1ed4c 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1ed4d 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 misuse;. rc = u
1ed4e 2e 63 67 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 .cg.pModule->xOp
1ed4f 65 6e 28 75 2e 63 67 2e 70 56 74 61 62 2c 20 26 en(u.cg.pVtab, &
1ed50 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72 u.cg.pVtabCursor
1ed51 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1ed52 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1ed53 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 g);. p->zErrMsg
1ed54 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e 7a = u.cg.pVtab->z
1ed55 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 67 2e 70 ErrMsg;. u.cg.p
1ed56 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 Vtab->zErrMsg =
1ed57 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 0;. if( sqlite3
1ed58 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 SafetyOn(db) ) g
1ed59 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1ed5a 5f 6d 69 73 75 73 65 3b 0a 20 20 69 66 28 20 53 _misuse;. if( S
1ed5b 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a QLITE_OK==rc ){.
1ed5c 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a /* Initializ
1ed5d 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 e sqlite3_vtab_c
1ed5e 75 72 73 6f 72 20 62 61 73 65 20 63 6c 61 73 73 ursor base class
1ed5f 20 2a 2f 0a 20 20 20 20 75 2e 63 67 2e 70 56 74 */. u.cg.pVt
1ed60 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 20 abCursor->pVtab
1ed61 3d 20 75 2e 63 67 2e 70 56 74 61 62 3b 0a 0a 20 = u.cg.pVtab;..
1ed62 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 73 65 /* Initialise
1ed63 20 76 64 62 65 20 63 75 72 73 6f 72 20 6f 62 6a vdbe cursor obj
1ed64 65 63 74 20 2a 2f 0a 20 20 20 20 75 2e 63 67 2e ect */. u.cg.
1ed65 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 pCur = allocateC
1ed66 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 ursor(p, pOp->p1
1ed67 2c 20 30 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 , 0, -1, 0);.
1ed68 20 69 66 28 20 75 2e 63 67 2e 70 43 75 72 20 29 if( u.cg.pCur )
1ed69 7b 0a 20 20 20 20 20 20 75 2e 63 67 2e 70 43 75 {. u.cg.pCu
1ed6a 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 3d r->pVtabCursor =
1ed6b 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f u.cg.pVtabCurso
1ed6c 72 3b 0a 20 20 20 20 20 20 75 2e 63 67 2e 70 43 r;. u.cg.pC
1ed6d 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e ur->pModule = u.
1ed6e 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e cg.pVtabCursor->
1ed6f 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a pVtab->pModule;.
1ed70 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1ed71 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1ed72 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 63 d = 1;. u.c
1ed73 67 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 g.pModule->xClos
1ed74 65 28 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 e(u.cg.pVtabCurs
1ed75 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 or);. }. }.
1ed76 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1ed77 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1ed78 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a VIRTUALTABLE */.
1ed79 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1ed7a 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1ed7b 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46 69 E./* Opcode: VFi
1ed7c 6c 74 65 72 20 50 31 20 50 32 20 50 33 20 50 34 lter P1 P2 P3 P4
1ed7d 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 *.**.** P1 is a
1ed7e 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 75 cursor opened u
1ed7f 73 69 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32 20 sing VOpen. P2
1ed80 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 74 6f is an address to
1ed81 20 6a 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20 74 jump to if.** t
1ed82 68 65 20 66 69 6c 74 65 72 65 64 20 72 65 73 75 he filtered resu
1ed83 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 2e lt set is empty.
1ed84 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69 74 .**.** P4 is eit
1ed85 68 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 her NULL or a st
1ed86 72 69 6e 67 20 74 68 61 74 20 77 61 73 20 67 65 ring that was ge
1ed87 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 78 nerated by the x
1ed88 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 BestIndex.** met
1ed89 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c hod of the modul
1ed8a 65 2e 20 20 54 68 65 20 69 6e 74 65 72 70 72 65 e. The interpre
1ed8b 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 50 34 tation of the P4
1ed8c 20 73 74 72 69 6e 67 20 69 73 20 6c 65 66 74 0a string is left.
1ed8d 2a 2a 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 ** to the module
1ed8e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e implementation.
1ed8f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1ed90 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 de invokes the x
1ed91 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f 6e Filter method on
1ed92 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
1ed93 6c 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 le specified.**
1ed94 62 79 20 50 31 2e 20 20 54 68 65 20 69 6e 74 65 by P1. The inte
1ed95 67 65 72 20 71 75 65 72 79 20 70 6c 61 6e 20 70 ger query plan p
1ed96 61 72 61 6d 65 74 65 72 20 74 6f 20 78 46 69 6c arameter to xFil
1ed97 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 69 6e ter is stored in
1ed98 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 2e register.** P3.
1ed99 20 52 65 67 69 73 74 65 72 20 50 33 2b 31 20 73 Register P3+1 s
1ed9a 74 6f 72 65 73 20 74 68 65 20 61 72 67 63 20 70 tores the argc p
1ed9b 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 70 arameter to be p
1ed9c 61 73 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 assed to the.**
1ed9d 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 20 xFilter method.
1ed9e 52 65 67 69 73 74 65 72 73 20 50 33 2b 32 2e 2e Registers P3+2..
1ed9f 50 33 2b 31 2b 61 72 67 63 20 61 72 65 20 74 68 P3+1+argc are th
1eda0 65 20 61 72 67 63 0a 2a 2a 20 61 64 64 69 74 69 e argc.** additi
1eda1 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 onal parameters
1eda2 77 68 69 63 68 20 61 72 65 20 70 61 73 73 65 64 which are passed
1eda3 20 74 6f 0a 2a 2a 20 78 46 69 6c 74 65 72 20 61 to.** xFilter a
1eda4 73 20 61 72 67 76 2e 20 52 65 67 69 73 74 65 72 s argv. Register
1eda5 20 50 33 2b 32 20 62 65 63 6f 6d 65 73 20 61 72 P3+2 becomes ar
1eda6 67 76 5b 30 5d 20 77 68 65 6e 20 70 61 73 73 65 gv[0] when passe
1eda7 64 20 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a d to xFilter..**
1eda8 0a 2a 2a 20 41 20 6a 75 6d 70 20 69 73 20 6d 61 .** A jump is ma
1eda9 64 65 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 de to P2 if the
1edaa 72 65 73 75 6c 74 20 73 65 74 20 61 66 74 65 72 result set after
1edab 20 66 69 6c 74 65 72 69 6e 67 20 77 6f 75 6c 64 filtering would
1edac 20 62 65 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 be empty..*/.ca
1edad 73 65 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20 7b se OP_VFilter: {
1edae 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1edaf 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1edb0 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1edb1 74 6f 20 75 2e 63 68 20 2a 2f 0a 20 20 69 6e 74 to u.ch */. int
1edb2 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 51 75 nArg;. int iQu
1edb3 65 72 79 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c ery;. const sql
1edb4 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1edb5 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75 dule;. Mem *pQu
1edb6 65 72 79 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 ery;. Mem *pArg
1edb7 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 c;. sqlite3_vta
1edb8 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 b_cursor *pVtabC
1edb9 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 ursor;. sqlite3
1edba 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 _vtab *pVtab;.
1edbb 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 VdbeCursor *pCur
1edbc 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 ;. int res;. i
1edbd 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 nt i;. Mem **ap
1edbe 41 72 67 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c Arg;.#endif /* l
1edbf 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1edc0 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 68 20 2a oved into u.ch *
1edc1 2f 0a 0a 20 20 75 2e 63 68 2e 70 51 75 65 72 79 /.. u.ch.pQuery
1edc2 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1edc3 3e 70 33 5d 3b 0a 20 20 75 2e 63 68 2e 70 41 72 >p3];. u.ch.pAr
1edc4 67 63 20 3d 20 26 75 2e 63 68 2e 70 51 75 65 72 gc = &u.ch.pQuer
1edc5 79 5b 31 5d 3b 0a 20 20 75 2e 63 68 2e 70 43 75 y[1];. u.ch.pCu
1edc6 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 r = p->apCsr[pOp
1edc7 2d 3e 70 31 5d 3b 0a 20 20 52 45 47 49 53 54 45 ->p1];. REGISTE
1edc8 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c R_TRACE(pOp->p3,
1edc9 20 75 2e 63 68 2e 70 51 75 65 72 79 29 3b 0a 20 u.ch.pQuery);.
1edca 20 61 73 73 65 72 74 28 20 75 2e 63 68 2e 70 43 assert( u.ch.pC
1edcb 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 ur->pVtabCursor
1edcc 29 3b 0a 20 20 75 2e 63 68 2e 70 56 74 61 62 43 );. u.ch.pVtabC
1edcd 75 72 73 6f 72 20 3d 20 75 2e 63 68 2e 70 43 75 ursor = u.ch.pCu
1edce 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a r->pVtabCursor;.
1edcf 20 20 75 2e 63 68 2e 70 56 74 61 62 20 3d 20 75 u.ch.pVtab = u
1edd0 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 2d .ch.pVtabCursor-
1edd1 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 68 2e 70 >pVtab;. u.ch.p
1edd2 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 68 2e 70 56 Module = u.ch.pV
1edd3 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 tab->pModule;..
1edd4 20 2f 2a 20 47 72 61 62 20 74 68 65 20 69 6e 64 /* Grab the ind
1edd5 65 78 20 6e 75 6d 62 65 72 20 61 6e 64 20 61 72 ex number and ar
1edd6 67 63 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f gc parameters */
1edd7 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 63 68 . assert( (u.ch
1edd8 2e 70 51 75 65 72 79 2d 3e 66 6c 61 67 73 26 4d .pQuery->flags&M
1edd9 45 4d 5f 49 6e 74 29 21 3d 30 20 26 26 20 75 2e EM_Int)!=0 && u.
1edda 63 68 2e 70 41 72 67 63 2d 3e 66 6c 61 67 73 3d ch.pArgc->flags=
1eddb 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 75 2e =MEM_Int );. u.
1eddc 63 68 2e 6e 41 72 67 20 3d 20 28 69 6e 74 29 75 ch.nArg = (int)u
1eddd 2e 63 68 2e 70 41 72 67 63 2d 3e 75 2e 69 3b 0a .ch.pArgc->u.i;.
1edde 20 20 75 2e 63 68 2e 69 51 75 65 72 79 20 3d 20 u.ch.iQuery =
1eddf 28 69 6e 74 29 75 2e 63 68 2e 70 51 75 65 72 79 (int)u.ch.pQuery
1ede0 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49 6e 76 ->u.i;.. /* Inv
1ede1 6f 6b 65 20 74 68 65 20 78 46 69 6c 74 65 72 20 oke the xFilter
1ede2 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a 20 20 method */. {.
1ede3 20 20 75 2e 63 68 2e 72 65 73 20 3d 20 30 3b 0a u.ch.res = 0;.
1ede4 20 20 20 20 75 2e 63 68 2e 61 70 41 72 67 20 3d u.ch.apArg =
1ede5 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 66 p->apArg;. f
1ede6 6f 72 28 75 2e 63 68 2e 69 20 3d 20 30 3b 20 75 or(u.ch.i = 0; u
1ede7 2e 63 68 2e 69 3c 75 2e 63 68 2e 6e 41 72 67 3b .ch.i<u.ch.nArg;
1ede8 20 75 2e 63 68 2e 69 2b 2b 29 7b 0a 20 20 20 20 u.ch.i++){.
1ede9 20 20 75 2e 63 68 2e 61 70 41 72 67 5b 75 2e 63 u.ch.apArg[u.c
1edea 68 2e 69 5d 20 3d 20 26 75 2e 63 68 2e 70 41 72 h.i] = &u.ch.pAr
1edeb 67 63 5b 75 2e 63 68 2e 69 2b 31 5d 3b 0a 20 20 gc[u.ch.i+1];.
1edec 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 storeTypeInf
1eded 6f 28 75 2e 63 68 2e 61 70 41 72 67 5b 75 2e 63 o(u.ch.apArg[u.c
1edee 68 2e 69 5d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a h.i], 0);. }.
1edef 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1edf0 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 SafetyOff(db) )
1edf1 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1edf2 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 70 2d o_misuse;. p-
1edf3 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 >inVtabMethod =
1edf4 31 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 68 1;. rc = u.ch
1edf5 2e 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65 .pModule->xFilte
1edf6 72 28 75 2e 63 68 2e 70 56 74 61 62 43 75 72 73 r(u.ch.pVtabCurs
1edf7 6f 72 2c 20 75 2e 63 68 2e 69 51 75 65 72 79 2c or, u.ch.iQuery,
1edf8 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 63 68 pOp->p4.z, u.ch
1edf9 2e 6e 41 72 67 2c 20 75 2e 63 68 2e 61 70 41 72 .nArg, u.ch.apAr
1edfa 67 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 g);. p->inVta
1edfb 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20 bMethod = 0;.
1edfc 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1edfd 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a b, p->zErrMsg);.
1edfe 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d p->zErrMsg =
1edff 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a 45 72 u.ch.pVtab->zEr
1ee00 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 68 2e 70 rMsg;. u.ch.p
1ee01 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 Vtab->zErrMsg =
1ee02 30 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 0;. if( rc==S
1ee03 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1ee04 20 20 75 2e 63 68 2e 72 65 73 20 3d 20 75 2e 63 u.ch.res = u.c
1ee05 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 h.pModule->xEof(
1ee06 75 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 u.ch.pVtabCursor
1ee07 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
1ee08 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
1ee09 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1ee0a 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1ee0b 0a 0a 20 20 20 20 69 66 28 20 75 2e 63 68 2e 72 .. if( u.ch.r
1ee0c 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d es ){. pc =
1ee0d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1ee0e 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 63 68 2e 70 }. }. u.ch.p
1ee0f 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 Cur->nullRow = 0
1ee10 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 ;.. break;.}.#e
1ee11 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1ee12 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1ee13 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1ee14 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1ee15 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a TABLE./* Opcode:
1ee16 20 56 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 VColumn P1 P2 P
1ee17 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 3 * *.**.** Stor
1ee18 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 e the value of t
1ee19 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20 he P2-th column
1ee1a 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66 of.** the row of
1ee1b 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 the virtual-tab
1ee1c 6c 65 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 le that the .**
1ee1d 50 31 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69 P1 cursor is poi
1ee1e 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 65 nting to into re
1ee1f 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 gister P3..*/.ca
1ee20 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b se OP_VColumn: {
1ee21 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1ee22 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ee23 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 20 20 into u.ci */.
1ee24 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1ee25 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c tab;. const sql
1ee26 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1ee27 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44 65 dule;. Mem *pDe
1ee28 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f st;. sqlite3_co
1ee29 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a ntext sContext;.
1ee2a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1ee2b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ee2c 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 0a 20 20 into u.ci */..
1ee2d 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 VdbeCursor *pCur
1ee2e 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1ee2f 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1ee30 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f pCur->pVtabCurso
1ee31 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1ee32 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d Op->p3>0 && pOp-
1ee33 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1ee34 20 20 75 2e 63 69 2e 70 44 65 73 74 20 3d 20 26 u.ci.pDest = &
1ee35 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d p->aMem[pOp->p3]
1ee36 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 ;. if( pCur->nu
1ee37 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73 71 6c llRow ){. sql
1ee38 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1ee39 6c 6c 28 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a ll(u.ci.pDest);.
1ee3a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 break;. }.
1ee3b 20 75 2e 63 69 2e 70 56 74 61 62 20 3d 20 70 43 u.ci.pVtab = pC
1ee3c 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d ur->pVtabCursor-
1ee3d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 69 2e 70 >pVtab;. u.ci.p
1ee3e 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 69 2e 70 56 Module = u.ci.pV
1ee3f 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 tab->pModule;.
1ee40 61 73 73 65 72 74 28 20 75 2e 63 69 2e 70 4d 6f assert( u.ci.pMo
1ee41 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b dule->xColumn );
1ee42 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 63 69 2e . memset(&u.ci.
1ee43 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a sContext, 0, siz
1ee44 65 6f 66 28 75 2e 63 69 2e 73 43 6f 6e 74 65 78 eof(u.ci.sContex
1ee45 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f t));.. /* The o
1ee46 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61 utput cell may a
1ee47 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62 75 lready have a bu
1ee48 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20 ffer allocated.
1ee49 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 Move. ** the cu
1ee4a 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 74 rrent contents t
1ee4b 6f 20 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e o u.ci.sContext.
1ee4c 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 s so in case the
1ee4d 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 0a 20 user-function.
1ee4e 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 65 20 ** can use the
1ee4f 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 already allocate
1ee50 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 d buffer instead
1ee51 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 of allocating a
1ee52 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 . ** new one..
1ee53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 */. sqlite3Vdb
1ee54 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 63 69 2e 73 eMemMove(&u.ci.s
1ee55 43 6f 6e 74 65 78 74 2e 73 2c 20 75 2e 63 69 2e Context.s, u.ci.
1ee56 70 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65 74 pDest);. MemSet
1ee57 54 79 70 65 46 6c 61 67 28 26 75 2e 63 69 2e 73 TypeFlag(&u.ci.s
1ee58 43 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e Context.s, MEM_N
1ee59 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c ull);.. if( sql
1ee5a 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1ee5b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1ee5c 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1ee5d 72 63 20 3d 20 75 2e 63 69 2e 70 4d 6f 64 75 6c rc = u.ci.pModul
1ee5e 65 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d e->xColumn(pCur-
1ee5f 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 75 >pVtabCursor, &u
1ee60 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2c 20 70 4f .ci.sContext, pO
1ee61 70 2d 3e 70 32 29 3b 0a 20 20 73 71 6c 69 74 65 p->p2);. sqlite
1ee62 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1ee63 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
1ee64 72 72 4d 73 67 20 3d 20 75 2e 63 69 2e 70 56 74 rrMsg = u.ci.pVt
1ee65 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 ab->zErrMsg;. u
1ee66 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .ci.pVtab->zErrM
1ee67 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e sg = 0;. if( u.
1ee68 63 69 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72 ci.sContext.isEr
1ee69 72 6f 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ror ){. rc =
1ee6a 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e 69 73 u.ci.sContext.is
1ee6b 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a Error;. }.. /*
1ee6c 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74 Copy the result
1ee6d 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
1ee6e 20 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 to the P3 regis
1ee6f 74 65 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20 ter. We. ** do
1ee70 74 68 69 73 20 72 65 67 61 72 64 6c 65 73 73 20 this regardless
1ee71 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f of whether or no
1ee72 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 t an error occur
1ee73 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61 6e red to ensure an
1ee74 79 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 y. ** dynamic a
1ee75 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 75 2e 63 llocation in u.c
1ee76 69 2e 73 43 6f 6e 74 65 78 74 2e 73 20 28 61 20 i.sContext.s (a
1ee77 4d 65 6d 20 73 74 72 75 63 74 29 20 69 73 20 20 Mem struct) is
1ee78 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 released.. */.
1ee79 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1ee7a 67 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e 63 69 geEncoding(&u.ci
1ee7b 2e 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e 63 .sContext.s, enc
1ee7c 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53 54 oding);. REGIST
1ee7d 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 ER_TRACE(pOp->p3
1ee7e 2c 20 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a 20 , u.ci.pDest);.
1ee7f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
1ee80 6f 76 65 28 75 2e 63 69 2e 70 44 65 73 74 2c 20 ove(u.ci.pDest,
1ee81 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e 73 &u.ci.sContext.s
1ee82 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f );. UPDATE_MAX_
1ee83 42 4c 4f 42 53 49 5a 45 28 75 2e 63 69 2e 70 44 BLOBSIZE(u.ci.pD
1ee84 65 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c est);.. if( sql
1ee85 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1ee86 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f ){. goto abo
1ee87 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1ee88 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 ;. }. if( sqli
1ee89 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
1ee8a 28 75 2e 63 69 2e 70 44 65 73 74 29 20 29 7b 0a (u.ci.pDest) ){.
1ee8b 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
1ee8c 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1ee8d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1ee8e 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1ee8f 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 BLE */..#ifndef
1ee90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1ee91 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f UALTABLE./* Opco
1ee92 64 65 3a 20 56 4e 65 78 74 20 50 31 20 50 32 20 de: VNext P1 P2
1ee93 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 * * *.**.** Adva
1ee94 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c nce virtual tabl
1ee95 65 20 50 31 20 74 6f 20 74 68 65 20 6e 65 78 74 e P1 to the next
1ee96 20 72 6f 77 20 69 6e 20 69 74 73 20 72 65 73 75 row in its resu
1ee97 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a 75 lt set and.** ju
1ee98 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f mp to instructio
1ee99 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66 20 74 68 n P2. Or, if th
1ee9a 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
1ee9b 68 61 73 20 72 65 61 63 68 65 64 0a 2a 2a 20 74 has reached.** t
1ee9c 68 65 20 65 6e 64 20 6f 66 20 69 74 73 20 72 65 he end of its re
1ee9d 73 75 6c 74 20 73 65 74 2c 20 74 68 65 6e 20 66 sult set, then f
1ee9e 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 all through to t
1ee9f 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 he next instruct
1eea0 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ion..*/.case OP_
1eea1 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6a 75 VNext: { /* ju
1eea2 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 mp */.#if 0 /*
1eea3 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1eea4 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 moved into u.cj
1eea5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 */. sqlite3_vta
1eea6 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 b *pVtab;. cons
1eea7 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
1eea8 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 *pModule;. int
1eea9 20 72 65 73 3b 0a 20 20 56 64 62 65 43 75 72 73 res;. VdbeCurs
1eeaa 6f 72 20 2a 70 43 75 72 3b 0a 23 65 6e 64 69 66 or *pCur;.#endif
1eeab 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1eeac 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1eead 2e 63 6a 20 2a 2f 0a 0a 20 20 75 2e 63 6a 2e 72 .cj */.. u.cj.r
1eeae 65 73 20 3d 20 30 3b 0a 20 20 75 2e 63 6a 2e 70 es = 0;. u.cj.p
1eeaf 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 Cur = p->apCsr[p
1eeb0 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1eeb1 74 28 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 t( u.cj.pCur->pV
1eeb2 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 tabCursor );. i
1eeb3 66 28 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 6e 75 f( u.cj.pCur->nu
1eeb4 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 llRow ){. bre
1eeb5 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 6a 2e 70 ak;. }. u.cj.p
1eeb6 56 74 61 62 20 3d 20 75 2e 63 6a 2e 70 43 75 72 Vtab = u.cj.pCur
1eeb7 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 ->pVtabCursor->p
1eeb8 56 74 61 62 3b 0a 20 20 75 2e 63 6a 2e 70 4d 6f Vtab;. u.cj.pMo
1eeb9 64 75 6c 65 20 3d 20 75 2e 63 6a 2e 70 56 74 61 dule = u.cj.pVta
1eeba 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 b->pModule;. as
1eebb 73 65 72 74 28 20 75 2e 63 6a 2e 70 4d 6f 64 75 sert( u.cj.pModu
1eebc 6c 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 20 le->xNext );..
1eebd 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e /* Invoke the xN
1eebe 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 ext() method of
1eebf 74 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72 the module. Ther
1eec0 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 e is no way for
1eec1 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 the. ** underly
1eec2 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ing implementati
1eec3 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 on to return an
1eec4 65 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63 error if one occ
1eec5 75 72 73 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 urs during. **
1eec6 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61 64 xNext(). Instead
1eec7 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 , if an error oc
1eec8 63 75 72 73 2c 20 74 72 75 65 20 69 73 20 72 65 curs, true is re
1eec9 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74 69 turned (indicati
1eeca 6e 67 20 74 68 61 74 0a 20 20 2a 2a 20 64 61 74 ng that. ** dat
1eecb 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29 20 a is available)
1eecc 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f and the error co
1eecd 64 65 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e de returned when
1eece 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a 2a xColumn or. **
1eecf 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 68 some other meth
1eed0 6f 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f 6b od is next invok
1eed1 65 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20 76 ed on the save v
1eed2 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 irtual table cur
1eed3 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 sor.. */. if(
1eed4 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1eed5 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1eed6 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1eed7 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 . p->inVtabMeth
1eed8 6f 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 75 od = 1;. rc = u
1eed9 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 .cj.pModule->xNe
1eeda 78 74 28 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 xt(u.cj.pCur->pV
1eedb 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 2d tabCursor);. p-
1eedc 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 >inVtabMethod =
1eedd 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 0;. sqlite3DbFr
1eede 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1eedf 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 g);. p->zErrMsg
1eee0 20 3d 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a = u.cj.pVtab->z
1eee1 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 6a 2e 70 ErrMsg;. u.cj.p
1eee2 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 Vtab->zErrMsg =
1eee3 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 0;. if( rc==SQL
1eee4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 2e ITE_OK ){. u.
1eee5 63 6a 2e 72 65 73 20 3d 20 75 2e 63 6a 2e 70 4d cj.res = u.cj.pM
1eee6 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 6a odule->xEof(u.cj
1eee7 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 .pCur->pVtabCurs
1eee8 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 or);. }. if( s
1eee9 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1eeea 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1eeeb 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a due_to_misuse;..
1eeec 20 20 69 66 28 20 21 75 2e 63 6a 2e 72 65 73 20 if( !u.cj.res
1eeed 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ){. /* If the
1eeee 72 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d 70 re is data, jump
1eeef 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70 63 to P2 */. pc
1eef0 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1eef1 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1eef2 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1eef3 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1eef4 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 E */..#ifndef SQ
1eef5 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1eef6 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1eef7 3a 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20 2a : VRename P1 * *
1eef8 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 P4 *.**.** P4 i
1eef9 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
1eefa 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f virtual table o
1eefb 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 bject, an sqlite
1eefc 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 3_vtab structure
1eefd 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 ..** This opcode
1eefe 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 invokes the cor
1eeff 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61 responding xRena
1ef00 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 76 me method. The v
1ef01 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69 73 alue.** in regis
1ef02 74 65 72 20 50 31 20 69 73 20 70 61 73 73 65 64 ter P1 is passed
1ef03 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61 72 as the zName ar
1ef04 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78 52 gument to the xR
1ef05 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f ename method..*/
1ef06 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65 .case OP_VRename
1ef07 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1ef08 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1ef09 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f ved into u.ck */
1ef0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 . sqlite3_vtab
1ef0b 2a 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a 70 *pVtab;. Mem *p
1ef0c 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 Name;.#endif /*
1ef0d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1ef0e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b 20 moved into u.ck
1ef0f 2a 2f 0a 0a 20 20 75 2e 63 6b 2e 70 56 74 61 62 */.. u.ck.pVtab
1ef10 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 = pOp->p4.pVtab
1ef11 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 6b 2e ->pVtab;. u.ck.
1ef12 70 4e 61 6d 65 20 3d 20 26 70 2d 3e 61 4d 65 6d pName = &p->aMem
1ef13 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1ef14 65 72 74 28 20 75 2e 63 6b 2e 70 56 74 61 62 2d ert( u.ck.pVtab-
1ef15 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d >pModule->xRenam
1ef16 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f e );. REGISTER_
1ef17 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 75 TRACE(pOp->p1, u
1ef18 2e 63 6b 2e 70 4e 61 6d 65 29 3b 0a 20 20 61 73 .ck.pName);. as
1ef19 73 65 72 74 28 20 75 2e 63 6b 2e 70 4e 61 6d 65 sert( u.ck.pName
1ef1a 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 ->flags & MEM_St
1ef1b 72 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 r );. if( sqlit
1ef1c 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1ef1d 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1ef1e 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63 _to_misuse;. rc
1ef1f 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 = u.ck.pVtab->p
1ef20 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 28 Module->xRename(
1ef21 75 2e 63 6b 2e 70 56 74 61 62 2c 20 75 2e 63 6b u.ck.pVtab, u.ck
1ef22 2e 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 .pName->z);. sq
1ef23 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1ef24 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 p->zErrMsg);. p
1ef25 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6b ->zErrMsg = u.ck
1ef26 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b .pVtab->zErrMsg;
1ef27 0a 20 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 7a . u.ck.pVtab->z
1ef28 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 ErrMsg = 0;. if
1ef29 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1ef2a 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f n(db) ) goto abo
1ef2b 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1ef2c 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 ;.. break;.}.#e
1ef2d 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
1ef2e 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1ef2f 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 LTABLE./* Opcode
1ef30 3a 20 56 55 70 64 61 74 65 20 50 31 20 50 32 20 : VUpdate P1 P2
1ef31 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 P3 P4 *.**.** P4
1ef32 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1ef33 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
1ef34 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 object, an sqli
1ef35 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 te3_vtab structu
1ef36 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f re..** This opco
1ef37 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 de invokes the c
1ef38 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 55 70 orresponding xUp
1ef39 64 61 74 65 20 6d 65 74 68 6f 64 2e 20 50 32 20 date method. P2
1ef3a 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 63 6f values.** are co
1ef3b 6e 74 69 67 75 6f 75 73 20 6d 65 6d 6f 72 79 20 ntiguous memory
1ef3c 63 65 6c 6c 73 20 73 74 61 72 74 69 6e 67 20 61 cells starting a
1ef3d 74 20 50 33 20 74 6f 20 70 61 73 73 20 74 6f 20 t P3 to pass to
1ef3e 74 68 65 20 78 55 70 64 61 74 65 20 0a 2a 2a 20 the xUpdate .**
1ef3f 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 invocation. The
1ef40 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1ef41 72 20 28 50 33 2b 50 32 2d 31 29 20 63 6f 72 72 r (P3+P2-1) corr
1ef42 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a esponds to the .
1ef43 2a 2a 20 70 32 74 68 20 65 6c 65 6d 65 6e 74 20 ** p2th element
1ef44 6f 66 20 74 68 65 20 61 72 67 76 20 61 72 72 61 of the argv arra
1ef45 79 20 70 61 73 73 65 64 20 74 6f 20 78 55 70 64 y passed to xUpd
1ef46 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ate..**.** The x
1ef47 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20 77 69 Update method wi
1ef48 6c 6c 20 64 6f 20 61 20 44 45 4c 45 54 45 20 6f ll do a DELETE o
1ef49 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20 62 r an INSERT or b
1ef4a 6f 74 68 2e 0a 2a 2a 20 54 68 65 20 61 72 67 76 oth..** The argv
1ef4b 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28 77 68 69 [0] element (whi
1ef4c 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 ch corresponds t
1ef4d 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 33 o memory cell P3
1ef4e 29 0a 2a 2a 20 69 73 20 74 68 65 20 72 6f 77 69 ).** is the rowi
1ef4f 64 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 64 65 d of a row to de
1ef50 6c 65 74 65 2e 20 20 49 66 20 61 72 67 76 5b 30 lete. If argv[0
1ef51 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 6e ] is NULL then n
1ef52 6f 20 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 6f o .** deletion o
1ef53 63 63 75 72 73 2e 20 20 54 68 65 20 61 72 67 76 ccurs. The argv
1ef54 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69 73 20 74 [1] element is t
1ef55 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 he rowid of the
1ef56 6e 65 77 20 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 new .** row. Th
1ef57 69 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 74 is can be NULL t
1ef58 6f 20 68 61 76 65 20 74 68 65 20 76 69 72 74 75 o have the virtu
1ef59 61 6c 20 74 61 62 6c 65 20 73 65 6c 65 63 74 20 al table select
1ef5a 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 the new .** rowi
1ef5b 64 20 66 6f 72 20 69 74 73 65 6c 66 2e 20 20 54 d for itself. T
1ef5c 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 65 6c he subsequent el
1ef5d 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72 ements in the ar
1ef5e 72 61 79 20 61 72 65 20 0a 2a 2a 20 74 68 65 20 ray are .** the
1ef5f 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c 75 6d 6e values of column
1ef60 73 20 69 6e 20 74 68 65 20 6e 65 77 20 72 6f 77 s in the new row
1ef61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 ..**.** If P2==1
1ef62 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65 72 74 20 then no insert
1ef63 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 61 is performed. a
1ef64 72 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72 6f rgv[0] is the ro
1ef65 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 wid of.** a row
1ef66 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a to delete..**.**
1ef67 20 50 31 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e P1 is a boolean
1ef68 20 66 6c 61 67 2e 20 49 66 20 69 74 20 69 73 20 flag. If it is
1ef69 73 65 74 20 74 6f 20 74 72 75 65 20 61 6e 64 20 set to true and
1ef6a 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c 6c the xUpdate call
1ef6b 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 .** is successfu
1ef6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 l, then the valu
1ef6d 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 e returned by sq
1ef6e 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 lite3_last_inser
1ef6f 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a 20 69 73 t_rowid() .** is
1ef70 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 set to the valu
1ef71 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66 e of the rowid f
1ef72 6f 72 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20 or the row just
1ef73 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 inserted..*/.cas
1ef74 65 20 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b 0a e OP_VUpdate: {.
1ef75 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1ef76 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ef77 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0a 20 20 73 into u.cl */. s
1ef78 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 qlite3_vtab *pVt
1ef79 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f ab;. sqlite3_mo
1ef7a 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1ef7b 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e 74 int nArg;. int
1ef7c 20 69 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 i;. sqlite_int
1ef7d 36 34 20 72 6f 77 69 64 3b 0a 20 20 4d 65 6d 20 64 rowid;. Mem
1ef7e 2a 2a 61 70 41 72 67 3b 0a 20 20 4d 65 6d 20 2a **apArg;. Mem *
1ef7f 70 58 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f pX;.#endif /* lo
1ef80 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1ef81 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f ved into u.cl */
1ef82 0a 0a 20 20 75 2e 63 6c 2e 70 56 74 61 62 20 3d .. u.cl.pVtab =
1ef83 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e pOp->p4.pVtab->
1ef84 70 56 74 61 62 3b 0a 20 20 75 2e 63 6c 2e 70 4d pVtab;. u.cl.pM
1ef85 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 odule = (sqlite3
1ef86 5f 6d 6f 64 75 6c 65 20 2a 29 75 2e 63 6c 2e 70 _module *)u.cl.p
1ef87 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 Vtab->pModule;.
1ef88 20 75 2e 63 6c 2e 6e 41 72 67 20 3d 20 70 4f 70 u.cl.nArg = pOp
1ef89 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 ->p2;. assert(
1ef8a 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1ef8b 56 54 41 42 20 29 3b 0a 20 20 69 66 28 20 41 4c VTAB );. if( AL
1ef8c 57 41 59 53 28 75 2e 63 6c 2e 70 4d 6f 64 75 6c WAYS(u.cl.pModul
1ef8d 65 2d 3e 78 55 70 64 61 74 65 29 20 29 7b 0a 20 e->xUpdate) ){.
1ef8e 20 20 20 75 2e 63 6c 2e 61 70 41 72 67 20 3d 20 u.cl.apArg =
1ef8f 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 75 2e p->apArg;. u.
1ef90 63 6c 2e 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d cl.pX = &p->aMem
1ef91 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66 [pOp->p3];. f
1ef92 6f 72 28 75 2e 63 6c 2e 69 3d 30 3b 20 75 2e 63 or(u.cl.i=0; u.c
1ef93 6c 2e 69 3c 75 2e 63 6c 2e 6e 41 72 67 3b 20 75 l.i<u.cl.nArg; u
1ef94 2e 63 6c 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 .cl.i++){.
1ef95 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 2e storeTypeInfo(u.
1ef96 63 6c 2e 70 58 2c 20 30 29 3b 0a 20 20 20 20 20 cl.pX, 0);.
1ef97 20 75 2e 63 6c 2e 61 70 41 72 67 5b 75 2e 63 6c u.cl.apArg[u.cl
1ef98 2e 69 5d 20 3d 20 75 2e 63 6c 2e 70 58 3b 0a 20 .i] = u.cl.pX;.
1ef99 20 20 20 20 20 75 2e 63 6c 2e 70 58 2b 2b 3b 0a u.cl.pX++;.
1ef9a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 }. if( sq
1ef9b 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1ef9c 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1ef9d 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1ef9e 20 20 20 72 63 20 3d 20 75 2e 63 6c 2e 70 4d 6f rc = u.cl.pMo
1ef9f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 28 75 2e dule->xUpdate(u.
1efa0 63 6c 2e 70 56 74 61 62 2c 20 75 2e 63 6c 2e 6e cl.pVtab, u.cl.n
1efa1 41 72 67 2c 20 75 2e 63 6c 2e 61 70 41 72 67 2c Arg, u.cl.apArg,
1efa2 20 26 75 2e 63 6c 2e 72 6f 77 69 64 29 3b 0a 20 &u.cl.rowid);.
1efa3 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1efa4 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1efa5 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 ;. p->zErrMsg
1efa6 20 3d 20 75 2e 63 6c 2e 70 56 74 61 62 2d 3e 7a = u.cl.pVtab->z
1efa7 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 6c ErrMsg;. u.cl
1efa8 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 .pVtab->zErrMsg
1efa9 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c = 0;. if( sql
1efaa 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1efab 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1efac 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 e_to_misuse;.
1efad 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1efae 4f 4b 20 26 26 20 70 4f 70 2d 3e 70 31 20 29 7b OK && pOp->p1 ){
1efaf 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 . assert( u
1efb0 2e 63 6c 2e 6e 41 72 67 3e 31 20 26 26 20 75 2e .cl.nArg>1 && u.
1efb1 63 6c 2e 61 70 41 72 67 5b 30 5d 20 26 26 20 28 cl.apArg[0] && (
1efb2 75 2e 63 6c 2e 61 70 41 72 67 5b 30 5d 2d 3e 66 u.cl.apArg[0]->f
1efb3 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 lags&MEM_Null) )
1efb4 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74 ;. db->last
1efb5 52 6f 77 69 64 20 3d 20 75 2e 63 6c 2e 72 6f 77 Rowid = u.cl.row
1efb6 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d id;. }. p-
1efb7 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a >nChange++;. }.
1efb8 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1efb9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1efba 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1efbb 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 4c 49 54 ..#ifndef SQLIT
1efbc 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 E_OMIT_PAGER_PRA
1efbd 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 GMAS./* Opcode:
1efbe 50 61 67 65 63 6f 75 6e 74 20 50 31 20 50 32 20 Pagecount P1 P2
1efbf 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 * * *.**.** Writ
1efc0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75 e the current nu
1efc1 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
1efc2 20 64 61 74 61 62 61 73 65 20 50 31 20 74 6f 20 database P1 to
1efc3 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32 2e 0a memory cell P2..
1efc4 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 67 65 63 */.case OP_Pagec
1efc5 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 ount: {
1efc6 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1efc7 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 lease */.#if 0
1efc8 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1efc9 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1efca 63 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 0a cm */. int p1;.
1efcb 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 50 int nPage;. P
1efcc 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 23 65 ager *pPager;.#e
1efcd 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1efce 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1efcf 74 6f 20 75 2e 63 6d 20 2a 2f 0a 0a 20 20 75 2e to u.cm */.. u.
1efd0 63 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b cm.p1 = pOp->p1;
1efd1 0a 20 20 75 2e 63 6d 2e 70 50 61 67 65 72 20 3d . u.cm.pPager =
1efd2 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 sqlite3BtreePag
1efd3 65 72 28 64 62 2d 3e 61 44 62 5b 75 2e 63 6d 2e er(db->aDb[u.cm.
1efd4 70 31 5d 2e 70 42 74 29 3b 0a 20 20 72 63 20 3d p1].pBt);. rc =
1efd5 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
1efd6 65 63 6f 75 6e 74 28 75 2e 63 6d 2e 70 50 61 67 ecount(u.cm.pPag
1efd7 65 72 2c 20 26 75 2e 63 6d 2e 6e 50 61 67 65 29 er, &u.cm.nPage)
1efd8 3b 0a 20 20 2f 2a 20 4f 50 5f 50 61 67 65 63 6f ;. /* OP_Pageco
1efd9 75 6e 74 20 69 73 20 61 6c 77 61 79 73 20 63 61 unt is always ca
1efda 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
1efdb 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 a read transact
1efdc 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a 2a 20 70 ion. The. ** p
1efdd 61 67 65 20 63 6f 75 6e 74 20 68 61 73 20 61 6c age count has al
1efde 72 65 61 64 79 20 62 65 65 6e 20 73 75 63 63 65 ready been succe
1efdf 73 73 66 75 6c 6c 79 20 72 65 61 64 20 61 6e 64 ssfully read and
1efe0 20 63 61 63 68 65 64 2e 20 20 53 6f 20 74 68 65 cached. So the
1efe1 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 . ** sqlite3Pag
1efe2 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 63 61 erPagecount() ca
1efe3 6c 6c 20 61 62 6f 76 65 20 63 61 6e 6e 6f 74 20 ll above cannot
1efe4 66 61 69 6c 2e 20 2a 2f 0a 20 20 69 66 28 20 41 fail. */. if( A
1efe5 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45 LWAYS(rc==SQLITE
1efe6 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 70 4f 75 74 _OK) ){. pOut
1efe7 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1efe8 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 t;. pOut->u.i
1efe9 20 3d 20 75 2e 63 6d 2e 6e 50 61 67 65 3b 0a 20 = u.cm.nPage;.
1efea 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1efeb 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
1efec 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a LITE_OMIT_TRACE.
1efed 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 63 65 /* Opcode: Trace
1efee 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * * * P4 *.**.*
1efef 2a 20 49 66 20 74 72 61 63 69 6e 67 20 69 73 20 * If tracing is
1eff0 65 6e 61 62 6c 65 64 20 28 62 79 20 74 68 65 20 enabled (by the
1eff1 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 29 sqlite3_trace())
1eff2 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 6e interface, then
1eff3 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 20 73 74 .** the UTF-8 st
1eff4 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20 69 ring contained i
1eff5 6e 20 50 34 20 69 73 20 65 6d 69 74 74 65 64 20 n P4 is emitted
1eff6 6f 6e 20 74 68 65 20 74 72 61 63 65 20 63 61 6c on the trace cal
1eff7 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f lback..*/.case O
1eff8 50 5f 54 72 61 63 65 3a 20 7b 0a 23 69 66 20 30 P_Trace: {.#if 0
1eff9 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1effa 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1effb 75 2e 63 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a u.cn */. char *
1effc 7a 54 72 61 63 65 3b 0a 23 65 6e 64 69 66 20 2f zTrace;.#endif /
1effd 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1effe 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1efff 6e 20 2a 2f 0a 0a 20 20 75 2e 63 6e 2e 7a 54 72 n */.. u.cn.zTr
1f000 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 34 2e 7a ace = (pOp->p4.z
1f001 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a 20 70 ? pOp->p4.z : p
1f002 2d 3e 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 75 ->zSql);. if( u
1f003 2e 63 6e 2e 7a 54 72 61 63 65 20 29 7b 0a 20 20 .cn.zTrace ){.
1f004 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61 63 65 if( db->xTrace
1f005 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 78 54 ){. db->xT
1f006 72 61 63 65 28 64 62 2d 3e 70 54 72 61 63 65 41 race(db->pTraceA
1f007 72 67 2c 20 75 2e 63 6e 2e 7a 54 72 61 63 65 29 rg, u.cn.zTrace)
1f008 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 ;. }.#ifdef S
1f009 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
1f00a 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 if( (db->flags &
1f00b 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 SQLITE_SqlTrace
1f00c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )!=0 ){. sq
1f00d 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
1f00e 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 25 73 5c ("SQL-trace: %s\
1f00f 6e 22 2c 20 75 2e 63 6e 2e 7a 54 72 61 63 65 29 n", u.cn.zTrace)
1f010 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f ;. }.#endif /
1f011 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a * SQLITE_DEBUG *
1f012 2f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d /. }. break;.}
1f013 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4f 70 63 .#endif.../* Opc
1f014 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a 20 ode: Noop * * *
1f015 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f 74 * *.**.** Do not
1f016 68 69 6e 67 2e 20 20 54 68 69 73 20 69 6e 73 74 hing. This inst
1f017 72 75 63 74 69 6f 6e 20 69 73 20 6f 66 74 65 6e ruction is often
1f018 20 75 73 65 66 75 6c 20 61 73 20 61 20 6a 75 6d useful as a jum
1f019 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e p.** destination
1f01a 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d ..*/./*.** The m
1f01b 61 67 69 63 20 45 78 70 6c 61 69 6e 20 6f 70 63 agic Explain opc
1f01c 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e 73 ode are only ins
1f01d 65 72 74 65 64 20 77 68 65 6e 20 65 78 70 6c 61 erted when expla
1f01e 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a 20 in==2 (which.**
1f01f 69 73 20 74 6f 20 73 61 79 20 77 68 65 6e 20 74 is to say when t
1f020 68 65 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 he EXPLAIN QUERY
1f021 20 50 4c 41 4e 20 73 79 6e 74 61 78 20 69 73 20 PLAN syntax is
1f022 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20 6f used.).** This o
1f023 70 63 6f 64 65 20 72 65 63 6f 72 64 73 20 69 6e pcode records in
1f024 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 formation from t
1f025 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 49 he optimizer. I
1f026 74 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 65 20 t is the.** the
1f027 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70 2e same as a no-op.
1f028 20 20 54 68 69 73 20 6f 70 63 6f 64 65 73 6e 65 This opcodesne
1f029 76 65 72 20 61 70 70 65 61 72 73 20 69 6e 20 61 ver appears in a
1f02a 20 72 65 61 6c 20 56 4d 20 70 72 6f 67 72 61 6d real VM program
1f02b 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a 20 7b 20 ..*/.default: {
1f02c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
1f02d 20 69 73 20 72 65 61 6c 6c 79 20 4f 50 5f 4e 6f is really OP_No
1f02e 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70 6c 61 69 op and OP_Explai
1f02f 6e 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a n */. break;.}.
1f030 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
1f031 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f032 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f033 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f034 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1f035 2a 2a 20 54 68 65 20 63 61 73 65 73 20 6f 66 20 ** The cases of
1f036 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 65 the switch state
1f037 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 69 73 20 ment above this
1f038 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c 20 line should all
1f039 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 62 be indented.** b
1f03a 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 y 6 spaces. But
1f03b 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 36 the left-most 6
1f03c 20 73 70 61 63 65 73 20 68 61 76 65 20 62 65 65 spaces have bee
1f03d 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d 70 n removed to imp
1f03e 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 65 61 64 rove the.** read
1f03f 61 62 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74 ability. From t
1f040 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77 his point on dow
1f041 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e n, the normal in
1f042 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73 20 dentation rules
1f043 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e are.** restored.
1f044 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1f045 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f046 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f047 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f048 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1f049 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 56 44 }..#ifdef VD
1f04a 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 7b BE_PROFILE. {
1f04b 0a 20 20 20 20 20 20 75 36 34 20 65 6c 61 70 73 . u64 elaps
1f04c 65 64 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 ed = sqlite3Hwti
1f04d 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a 20 20 me() - start;.
1f04e 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20 pOp->cycles
1f04f 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20 20 20 20 += elapsed;.
1f050 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23 69 pOp->cnt++;.#i
1f051 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72 69 f 0. fpri
1f052 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31 30 ntf(stdout, "%10
1f053 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65 64 29 3b llu ", elapsed);
1f054 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1f055 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f VdbePrintOp(stdo
1f056 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d 3e ut, origPc, &p->
1f057 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65 aOp[origPc]);.#e
1f058 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69 ndif. }.#endi
1f059 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f f.. /* The fo
1f05a 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 64 llowing code add
1f05b 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 s nothing to the
1f05c 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e actual function
1f05d 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 ality. ** of
1f05e 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 74 the program. It
1f05f 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f is only here fo
1f060 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 r testing and de
1f061 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20 bugging.. **
1f062 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e On the other han
1f063 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 6e 20 d, it does burn
1f064 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 72 79 CPU cycles every
1f065 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 20 time through.
1f066 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74 ** the evaluat
1f067 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20 or loop. So we
1f068 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74 can leave it out
1f069 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20 when NDEBUG is
1f06a 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a defined.. */.
1f06b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
1f06c 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 2d assert( pc>=-
1f06d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 1 && pc<p->nOp )
1f06e 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ;..#ifdef SQLITE
1f06f 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70 _DEBUG. if( p
1f070 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 ->trace ){.
1f071 20 69 66 28 20 72 63 21 3d 30 20 29 20 66 70 72 if( rc!=0 ) fpr
1f072 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 72 intf(p->trace,"r
1f073 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 20 c=%d\n",rc);.
1f074 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74 if( opPropert
1f075 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 y & OPFLG_OUT2_P
1f076 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 RERELEASE ){.
1f077 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 61 registerTra
1f078 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 ce(p->trace, pOp
1f079 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 ->p2, pOut);.
1f07a 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f }. if( o
1f07b 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c pProperty & OPFL
1f07c 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20 G_OUT3 ){.
1f07d 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28 registerTrace(
1f07e 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 p->trace, pOp->p
1f07f 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 3, pOut);.
1f080 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 20 }. }.#endif
1f081 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 /* SQLITE_DEBUG
1f082 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44 */.#endif /* ND
1f083 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a 20 EBUG */. } /*
1f084 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 The end of the f
1f085 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 20 or(;;) loop the
1f086 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 6f 70 loops through op
1f087 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 codes */.. /* I
1f088 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 f we reach this
1f089 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20 point, it means
1f08a 74 68 61 74 20 65 78 65 63 75 74 69 6f 6e 20 69 that execution i
1f08b 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 0a s finished with.
1f08c 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 66 ** an error of
1f08d 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a 2f some kind.. */
1f08e 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 .vdbe_error_halt
1f08f 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 20 29 :. assert( rc )
1f090 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a ;. p->rc = rc;.
1f091 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c sqlite3VdbeHal
1f092 74 28 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d t(p);. if( rc==
1f093 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d SQLITE_IOERR_NOM
1f094 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 EM ) db->mallocF
1f095 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63 20 ailed = 1;. rc
1f096 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1f097 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 . /* This is th
1f098 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20 6f e only way out o
1f099 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 f this procedure
1f09a 2e 20 20 57 65 20 68 61 76 65 20 74 6f 0a 20 20 . We have to.
1f09b 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d ** release the m
1f09c 75 74 65 78 65 73 20 6f 6e 20 62 74 72 65 65 73 utexes on btrees
1f09d 20 74 68 61 74 20 77 65 72 65 20 61 63 71 75 69 that were acqui
1f09e 72 65 64 20 61 74 20 74 68 65 0a 20 20 2a 2a 20 red at the. **
1f09f 74 6f 70 2e 20 2a 2f 0a 76 64 62 65 5f 72 65 74 top. */.vdbe_ret
1f0a0 75 72 6e 3a 0a 20 20 73 71 6c 69 74 65 33 42 74 urn:. sqlite3Bt
1f0a1 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 reeMutexArrayLea
1f0a2 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a ve(&p->aMutex);.
1f0a3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 return rc;..
1f0a4 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 /* Jump to here
1f0a5 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 if a string or b
1f0a6 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e 20 lob larger than
1f0a7 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 SQLITE_MAX_LENGT
1f0a8 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e H. ** is encoun
1f0a9 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f tered.. */.too_
1f0aa 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 65 big:. sqlite3Se
1f0ab 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1f0ac 4d 73 67 2c 20 64 62 2c 20 22 73 74 72 69 6e 67 Msg, db, "string
1f0ad 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 or blob too big
1f0ae 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 ");. rc = SQLIT
1f0af 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 6f E_TOOBIG;. goto
1f0b0 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 vdbe_error_halt
1f0b1 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 ;.. /* Jump to
1f0b2 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63 here if a malloc
1f0b3 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 6e () fails.. */.n
1f0b4 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c o_mem:. db->mal
1f0b5 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
1f0b6 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1f0b7 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1f0b8 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 b, "out of memor
1f0b9 79 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 y");. rc = SQLI
1f0ba 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f TE_NOMEM;. goto
1f0bb 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 vdbe_error_halt
1f0bc 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 ;.. /* Jump to
1f0bd 68 65 72 65 20 66 6f 72 20 61 6e 20 53 51 4c 49 here for an SQLI
1f0be 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 2e TE_MISUSE error.
1f0bf 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f . */.abort_due_
1f0c0 74 6f 5f 6d 69 73 75 73 65 3a 0a 20 20 72 63 20 to_misuse:. rc
1f0c1 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b = SQLITE_MISUSE;
1f0c2 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 . /* Fall thru
1f0c3 69 6e 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 into abort_due_t
1f0c4 6f 5f 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 2f 2a o_error */.. /*
1f0c5 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f Jump to here fo
1f0c6 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
1f0c7 20 6f 66 20 66 61 74 61 6c 20 65 72 72 6f 72 2e of fatal error.
1f0c8 20 20 54 68 65 20 22 72 63 22 20 76 61 72 69 61 The "rc" varia
1f0c9 62 6c 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 ble. ** should
1f0ca 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6e hold the error n
1f0cb 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 umber.. */.abor
1f0cc 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3a 0a t_due_to_error:.
1f0cd 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 45 72 assert( p->zEr
1f0ce 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 rMsg==0 );. if(
1f0cf 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1f0d0 64 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f d ) rc = SQLITE_
1f0d1 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63 21 NOMEM;. if( rc!
1f0d2 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f =SQLITE_IOERR_NO
1f0d3 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 MEM ){. sqlit
1f0d4 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1f0d5 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1f0d6 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 ", sqlite3ErrStr
1f0d7 28 72 63 29 29 3b 0a 20 20 7d 0a 20 20 67 6f 74 (rc));. }. got
1f0d8 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c o vdbe_error_hal
1f0d9 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f t;.. /* Jump to
1f0da 20 68 65 72 65 20 69 66 20 74 68 65 20 73 71 6c here if the sql
1f0db 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
1f0dc 20 41 50 49 20 73 65 74 73 20 74 68 65 20 69 6e API sets the in
1f0dd 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c 61 terrupt. ** fla
1f0de 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 g.. */.abort_du
1f0df 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a 0a e_to_interrupt:.
1f0e0 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 75 31 assert( db->u1
1f0e1 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 .isInterrupted )
1f0e2 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f ;. rc = SQLITE_
1f0e3 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 2d 3e INTERRUPT;. p->
1f0e4 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 rc = rc;. sqlit
1f0e5 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1f0e6 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 zErrMsg, db, "%s
1f0e7 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 ", sqlite3ErrStr
1f0e8 28 72 63 29 29 3b 0a 20 20 67 6f 74 6f 20 76 64 (rc));. goto vd
1f0e9 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 7d be_error_halt;.}
1f0ea 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1f0eb 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 63 20 * End of vdbe.c
1f0ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f0ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f0ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f0ef 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1f0f0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
1f0f1 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a eblob.c ********
1f0f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f0f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f0f4 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 /./*.** 2007 May
1f0f5 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 1.**.** The aut
1f0f6 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
1f0f7 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
1f0f8 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
1f0f9 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
1f0fa 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
1f0fb 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
1f0fc 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
1f0fd 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
1f0fe 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
1f0ff 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
1f100 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
1f101 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
1f102 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
1f103 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
1f104 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
1f105 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
1f106 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
1f107 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f108 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f109 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f10a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f10b 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
1f10c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
1f10d 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
1f10e 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c t incremental BL
1f10f 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49 OB I/O..**.** $I
1f110 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76 20 d: vdbeblob.c,v
1f111 31 2e 33 35 20 32 30 30 39 2f 30 37 2f 30 32 20 1.35 2009/07/02
1f112 30 37 3a 34 37 3a 33 33 20 64 61 6e 69 65 6c 6b 07:47:33 danielk
1f113 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 1977 Exp $.*/...
1f114 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1f115 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a MIT_INCRBLOB../*
1f116 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65 .** Valid sqlite
1f117 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20 3_blob* handles
1f118 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f point to Incrblo
1f119 62 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f b structures..*/
1f11a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1f11b 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f Incrblob Incrblo
1f11c 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c b;.struct Incrbl
1f11d 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 ob {. int flags
1f11e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1f11f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73 * Copy of "flags
1f120 22 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 " passed to sqli
1f121 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20 te3_blob_open()
1f122 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 */. int nByte;
1f123 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f124 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f Size of open blo
1f125 62 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 b, in bytes */.
1f126 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20 int iOffset;
1f127 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
1f128 20 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20 offset of blob
1f129 69 6e 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a in cursor data *
1f12a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
1f12b 73 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 sr; /* C
1f12c 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 ursor pointing a
1f12d 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20 t blob row */.
1f12e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1f12f 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65 tmt; /* State
1f130 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72 ment holding cur
1f131 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 sor open */. sq
1f132 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
1f133 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 /* The ass
1f134 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 ociated database
1f135 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70 */.};../*.** Op
1f136 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 en a blob handle
1f137 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1f138 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 int sqlite3_blob
1f139 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 _open(. sqlite3
1f13a 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 * db,
1f13b 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
1f13c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
1f13d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
1f13e 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 , /* The
1f13f 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
1f140 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
1f141 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 blob */. const
1f142 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20 char *zTable,
1f143 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
1f144 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 containing the b
1f145 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 lob */. const c
1f146 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20 har *zColumn,
1f147 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63 /* The column c
1f148 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c ontaining the bl
1f149 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 ob */. sqlite_i
1f14a 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 nt64 iRow,
1f14b 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61 /* The row conta
1f14c 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a ining the glob *
1f14d 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
1f14e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1f14f 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74 rue -> read/writ
1f150 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20 e access, false
1f151 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a -> read-only */.
1f152 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a sqlite3_blob *
1f153 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e *ppBlob /* Han
1f154 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e dle for accessin
1f155 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72 g the blob retur
1f156 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ned here */.){.
1f157 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20 int nAttempt =
1f158 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 0;. int iCol;
1f159 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f15a 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e Index of zColumn
1f15b 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a in row-record *
1f15c 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42 /.. /* This VDB
1f15d 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20 E program seeks
1f15e 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 20 74 a btree cursor t
1f15f 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 64 o the identified
1f160 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f . ** db/table/
1f161 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72 row entry. The r
1f162 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 eason for using
1f163 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 a vdbe program i
1f164 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77 nstead. ** of w
1f165 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 riting code to u
1f166 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 se the b-tree la
1f167 79 65 72 20 64 69 72 65 63 74 6c 79 20 69 73 20 yer directly is
1f168 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64 that the. ** vd
1f169 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 be program will
1f16a 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f take advantage o
1f16b 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 72 f the various tr
1f16c 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 ansaction,. **
1f16d 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f locking and erro
1f16e 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61 r handling infra
1f16f 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 structure built
1f170 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20 into the vdbe..
1f171 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73 **. ** After s
1f172 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f eeking the curso
1f173 72 2c 20 74 68 65 20 76 64 62 65 20 65 78 65 63 r, the vdbe exec
1f174 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 73 75 6c utes an OP_Resul
1f175 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65 20 tRow.. ** Code
1f176 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20 external to the
1f177 56 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f Vdbe then "borro
1f178 77 73 22 20 74 68 65 20 62 2d 74 72 65 65 20 63 ws" the b-tree c
1f179 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75 ursor and. ** u
1f17a 73 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d ses it to implem
1f17b 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61 ent the blob_rea
1f17c 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28 d(), blob_write(
1f17d 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62 ) and . ** blob
1f17e 5f 62 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f _bytes() functio
1f17f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 ns.. **. ** Th
1f180 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 e sqlite3_blob_c
1f181 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 lose() function
1f182 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64 finalizes the vd
1f183 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a be program,. **
1f184 20 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68 which closes th
1f185 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 e b-tree cursor
1f186 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63 and (possibly) c
1f187 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a ommits the . **
1f188 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 transaction..
1f189 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 */. static cons
1f18a 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65 t VdbeOpList ope
1f18b 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20 nBlob[] = {.
1f18c 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c {OP_Transaction,
1f18d 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f 0, 0, 0}, /
1f18e 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74 72 61 * 0: Start a tra
1f18f 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 nsaction */.
1f190 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 {OP_VerifyCookie
1f191 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f , 0, 0, 0}, /
1f192 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73 * 1: Check the s
1f193 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a chema cookie */.
1f194 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65 4c 6f 63 {OP_TableLoc
1f195 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 k, 0, 0, 0},
1f196 20 20 20 2f 2a 20 32 3a 20 41 63 71 75 69 72 65 /* 2: Acquire
1f197 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 a read or write
1f198 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 2f 2a lock */.. /*
1f199 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c One of the foll
1f19a 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74 72 75 owing two instru
1f19b 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c 61 63 ctions is replac
1f19c 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e 6f 6f 70 ed by an OP_Noop
1f19d 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 . */. {OP_Ope
1f19e 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30 7d 2c nRead, 0, 0, 0},
1f19f 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 4f 70 /* 3: Op
1f1a0 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20 en cursor 0 for
1f1a1 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20 20 7b reading */. {
1f1a2 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c OP_OpenWrite, 0,
1f1a3 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 0, 0}, /*
1f1a4 20 34 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 4: Open cursor
1f1a5 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 0 for read/write
1f1a6 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 61 72 */.. {OP_Var
1f1a7 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 31 7d 2c iable, 1, 1, 1},
1f1a8 20 20 20 20 20 20 20 20 2f 2a 20 35 3a 20 50 75 /* 5: Pu
1f1a9 73 68 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20 sh the rowid to
1f1aa 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20 the stack */.
1f1ab 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 {OP_NotExists,
1f1ac 30 2c 20 39 2c 20 31 7d 2c 20 20 20 20 20 20 20 0, 9, 1},
1f1ad 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68 65 20 63 /* 6: Seek the c
1f1ae 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 ursor */. {OP
1f1af 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 31 _Column, 0, 0, 1
1f1b0 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 37 }, /* 7
1f1b1 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52 65 73 */. {OP_Res
1f1b2 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20 30 7d ultRow, 1, 0, 0}
1f1b3 2c 20 20 20 20 20 20 20 2f 2a 20 38 20 20 2a 2f , /* 8 */
1f1b4 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c 20 . {OP_Close,
1f1b5 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 0, 0, 0},
1f1b6 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a 20 20 20 /* 9 */.
1f1b7 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 2c {OP_Halt, 0, 0,
1f1b8 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20 0},
1f1b9 2f 2a 20 31 30 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 /* 10 */. };..
1f1ba 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 Vdbe *v = 0;.
1f1bb 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1f1bc 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 OK;. char *zErr
1f1bd 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 = 0;. Table *p
1f1be 54 61 62 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 Tab;. Parse *pP
1f1bf 61 72 73 65 3b 0a 0a 20 20 2a 70 70 42 6c 6f 62 arse;.. *ppBlob
1f1c0 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f = 0;. sqlite3_
1f1c1 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e mutex_enter(db->
1f1c2 6d 75 74 65 78 29 3b 0a 20 20 70 50 61 72 73 65 mutex);. pParse
1f1c3 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 = sqlite3StackA
1f1c4 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 llocRaw(db, size
1f1c5 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a 20 20 of(*pParse));.
1f1c6 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 29 7b if( pParse==0 ){
1f1c7 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1f1c8 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f _NOMEM;. goto
1f1c9 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a blob_open_out;.
1f1ca 20 20 7d 0a 20 20 64 6f 20 7b 0a 20 20 20 20 6d }. do {. m
1f1cb 65 6d 73 65 74 28 70 50 61 72 73 65 2c 20 30 2c emset(pParse, 0,
1f1cc 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29 29 3b sizeof(Parse));
1f1cd 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 20 . pParse->db
1f1ce 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66 28 20 73 = db;.. if( s
1f1cf 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1f1d0 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 b) ){. sqli
1f1d1 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 te3DbFree(db, zE
1f1d2 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 rr);. sqlit
1f1d3 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20 e3StackFree(db,
1f1d4 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 pParse);. s
1f1d5 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1f1d6 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 ve(db->mutex);.
1f1d7 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1f1d8 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 7d TE_MISUSE;. }
1f1d9 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 .. sqlite3Btr
1f1da 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a eeEnterAll(db);.
1f1db 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 pTab = sqlit
1f1dc 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 e3LocateTable(pP
1f1dd 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c 65 2c arse, 0, zTable,
1f1de 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 zDb);. if( p
1f1df 54 61 62 20 26 26 20 49 73 56 69 72 74 75 61 6c Tab && IsVirtual
1f1e0 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 (pTab) ){.
1f1e1 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 pTab = 0;.
1f1e2 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1f1e3 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 pParse, "cannot
1f1e4 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 74 61 62 open virtual tab
1f1e5 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 29 le: %s", zTable)
1f1e6 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 ;. }.#ifndef
1f1e7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 SQLITE_OMIT_VIEW
1f1e8 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26 . if( pTab &&
1f1e9 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 pTab->pSelect )
1f1ea 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 {. pTab = 0
1f1eb 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
1f1ec 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1f1ed 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 65 "cannot open vie
1f1ee 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 29 3b w: %s", zTable);
1f1ef 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1f1f0 20 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20 if( !pTab ){.
1f1f1 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d if( pParse-
1f1f2 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 >zErrMsg ){.
1f1f3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f1f4 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 e(db, zErr);.
1f1f5 20 20 20 20 20 7a 45 72 72 20 3d 20 70 50 61 72 zErr = pPar
1f1f6 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 se->zErrMsg;.
1f1f7 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 pParse->zEr
1f1f8 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 rMsg = 0;.
1f1f9 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c }. rc = SQL
1f1fa 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 ITE_ERROR;.
1f1fb 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1f1fc 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 fetyOff(db);.
1f1fd 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
1f1fe 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 eaveAll(db);.
1f1ff 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 goto blob_ope
1f200 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 n_out;. }..
1f201 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63 68 20 /* Now search
1f202 70 54 61 62 20 66 6f 72 20 74 68 65 20 65 78 61 pTab for the exa
1f203 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 ct column. */.
1f204 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 for(iCol=0; iC
1f205 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b ol < pTab->nCol;
1f206 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20 20 20 iCol++) {.
1f207 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
1f208 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 Cmp(pTab->aCol[i
1f209 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c Col].zName, zCol
1f20a 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 umn)==0 ){.
1f20b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1f20c 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
1f20d 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c iCol==pTab->nCol
1f20e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1f20f 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 3DbFree(db, zErr
1f210 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d 20 );. zErr =
1f211 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1f212 62 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 b, "no such colu
1f213 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 7a 43 6f mn: \"%s\"", zCo
1f214 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 72 63 20 lumn);. rc
1f215 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1f216 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 (void)sqli
1f217 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1f218 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 ;. sqlite3B
1f219 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 treeLeaveAll(db)
1f21a 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f ;. goto blo
1f21b 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 b_open_out;.
1f21c 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
1f21d 20 76 61 6c 75 65 20 69 73 20 62 65 69 6e 67 20 value is being
1f21e 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 opened for writi
1f21f 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 ng, check that t
1f220 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e he. ** column
1f221 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 65 64 2e is not indexed.
1f222 20 49 74 20 69 73 20 61 67 61 69 6e 73 74 20 74 It is against t
1f223 68 65 20 72 75 6c 65 73 20 74 6f 20 6f 70 65 6e he rules to open
1f224 20 61 6e 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 an. ** index
1f225 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 ed column for wr
1f226 69 74 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 iting.. */.
1f227 20 20 69 66 28 20 66 6c 61 67 73 20 29 7b 0a 20 if( flags ){.
1f228 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 Index *pIdx
1f229 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 ;. for(pIdx
1f22a 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
1f22b 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
1f22c 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 pNext){.
1f22d 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 int j;. f
1f22e 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e or(j=0; j<pIdx->
1f22f 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 nColumn; j++){.
1f230 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 if( pId
1f231 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d x->aiColumn[j]==
1f232 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 iCol ){.
1f233 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1f234 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 e(db, zErr);.
1f235 20 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 zErr =
1f236 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1f237 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b,.
1f238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f239 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 69 6e 64 "cannot open ind
1f23a 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 exed column for
1f23b 77 72 69 74 69 6e 67 22 29 3b 0a 20 20 20 20 20 writing");.
1f23c 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
1f23d 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 TE_ERROR;.
1f23e 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 (void)sqli
1f23f 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1f240 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 ;. sq
1f241 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 lite3BtreeLeaveA
1f242 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 ll(db);.
1f243 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1f244 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 en_out;.
1f245 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1f246 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1f247 20 76 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 v = sqlite3Vdbe
1f248 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20 20 20 Create(db);.
1f249 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 69 if( v ){. i
1f24a 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 nt iDb = sqlite3
1f24b 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
1f24c 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 , pTab->pSchema)
1f24d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1f24e 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 dbeAddOpList(v,
1f24f 73 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 sizeof(openBlob)
1f250 2f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69 /sizeof(VdbeOpLi
1f251 73 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a st), openBlob);.
1f252 20 20 20 20 20 20 66 6c 61 67 73 20 3d 20 21 21 flags = !!
1f253 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
1f254 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 /* flags
1f255 3d 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 = (flags ? 1 : 0
1f256 29 3b 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 ); */.. /*
1f257 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 Configure the OP
1f258 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a _Transaction */.
1f259 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f25a 65 43 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 eChangeP1(v, 0,
1f25b 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 iDb);. sqli
1f25c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1f25d 76 2c 20 30 2c 20 66 6c 61 67 73 29 3b 0a 0a 20 v, 0, flags);..
1f25e 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
1f25f 65 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43 e the OP_VerifyC
1f260 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 ookie */. s
1f261 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1f262 50 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 P1(v, 1, iDb);.
1f263 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f264 43 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 ChangeP2(v, 1, p
1f265 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 Tab->pSchema->sc
1f266 68 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 hema_cookie);..
1f267 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 /* Make sur
1f268 65 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c e a mutex is hel
1f269 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 d on the table t
1f26a 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f o be accessed */
1f26b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1f26c 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 beUsesBtree(v, i
1f26d 44 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 Db); .. /*
1f26e 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 Configure the OP
1f26f 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 _TableLock instr
1f270 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 uction */.
1f271 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f272 65 50 31 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a eP1(v, 2, iDb);.
1f273 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f274 65 43 68 61 6e 67 65 50 32 28 76 2c 20 32 2c 20 eChangeP2(v, 2,
1f275 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 pTab->tnum);.
1f276 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1f277 61 6e 67 65 50 33 28 76 2c 20 32 2c 20 66 6c 61 angeP3(v, 2, fla
1f278 67 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 gs);. sqlit
1f279 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 e3VdbeChangeP4(v
1f27a 2c 20 32 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 , 2, pTab->zName
1f27b 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b , P4_TRANSIENT);
1f27c 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 .. /* Remov
1f27d 65 20 65 69 74 68 65 72 20 74 68 65 20 4f 50 5f e either the OP_
1f27e 4f 70 65 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 OpenWrite or Ope
1f27f 6e 52 65 61 64 2e 20 53 65 74 20 74 68 65 20 50 nRead. Set the P
1f280 32 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 61 2 . ** para
1f281 6d 65 74 65 72 20 6f 66 20 74 68 65 20 6f 74 68 meter of the oth
1f282 65 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d er to pTab->tnum
1f283 2e 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 . */. sqli
1f284 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e te3VdbeChangeToN
1f285 6f 6f 70 28 76 2c 20 34 20 2d 20 66 6c 61 67 73 oop(v, 4 - flags
1f286 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , 1);. sqli
1f287 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1f288 76 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 70 54 v, 3 + flags, pT
1f289 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 ab->tnum);.
1f28a 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1f28b 67 65 50 33 28 76 2c 20 33 20 2b 20 66 6c 61 67 geP3(v, 3 + flag
1f28c 73 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 s, iDb);..
1f28d 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 /* Configure the
1f28e 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1f28f 6e 73 2e 20 43 6f 6e 66 69 67 75 72 65 20 74 68 ns. Configure th
1f290 65 20 63 75 72 73 6f 72 20 74 6f 0a 20 20 20 20 e cursor to.
1f291 20 20 2a 2a 20 74 68 69 6e 6b 20 74 68 61 74 20 ** think that
1f292 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 6f 6e the table has on
1f293 65 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68 e more column th
1f294 61 6e 20 69 74 20 72 65 61 6c 6c 79 0a 20 20 20 an it really.
1f295 20 20 20 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f ** does. An O
1f296 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 P_Column to retr
1f297 69 65 76 65 20 74 68 69 73 20 69 6d 61 67 69 6e ieve this imagin
1f298 61 72 79 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a ary column will.
1f299 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 ** always
1f29a 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 return an SQL NU
1f29b 4c 4c 2e 20 54 68 69 73 20 69 73 20 75 73 65 66 LL. This is usef
1f29c 75 6c 20 62 65 63 61 75 73 65 20 69 74 20 6d 65 ul because it me
1f29d 61 6e 73 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 ans. ** we
1f29e 63 61 6e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f can invoke OP_Co
1f29f 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 lumn to fill in
1f2a0 74 68 65 20 76 64 62 65 20 63 75 72 73 6f 72 73 the vdbe cursors
1f2a1 20 74 79 70 65 20 0a 20 20 20 20 20 20 2a 2a 20 type . **
1f2a2 61 6e 64 20 6f 66 66 73 65 74 20 63 61 63 68 65 and offset cache
1f2a3 20 77 69 74 68 6f 75 74 20 63 61 75 73 69 6e 67 without causing
1f2a4 20 61 6e 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a any IO.. *
1f2a5 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 /. sqlite3V
1f2a6 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 33 dbeChangeP4(v, 3
1f2a7 2b 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 +flags, SQLITE_I
1f2a8 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e NT_TO_PTR(pTab->
1f2a9 6e 43 6f 6c 2b 31 29 2c 50 34 5f 49 4e 54 33 32 nCol+1),P4_INT32
1f2aa 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f2ab 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 VdbeChangeP2(v,
1f2ac 37 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 7, pTab->nCol);.
1f2ad 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d if( !db->m
1f2ae 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f2af 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1f2b0 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31 beMakeReady(v, 1
1f2b1 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29 , 1, 1, 0, 0, 0)
1f2b2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1f2b3 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42 . sqlite3B
1f2b4 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 treeLeaveAll(db)
1f2b5 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1f2b6 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b e3SafetyOff(db);
1f2b7 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 . if( NEVER(r
1f2b8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c c!=SQLITE_OK) ||
1f2b9 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1f2ba 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 d ){. goto
1f2bb 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 blob_open_out;.
1f2bc 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 }.. sqlite
1f2bd 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73 71 3_bind_int64((sq
1f2be 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20 lite3_stmt *)v,
1f2bf 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 1, iRow);. rc
1f2c0 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 = sqlite3_step(
1f2c1 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 (sqlite3_stmt *)
1f2c2 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d v);. if( rc!=
1f2c3 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 SQLITE_ROW ){.
1f2c4 20 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a nAttempt++;.
1f2c5 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1f2c6 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c e3_finalize((sql
1f2c7 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a ite3_stmt *)v);.
1f2c8 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1f2c9 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 ree(db, zErr);.
1f2ca 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 zErr = sqli
1f2cb 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 73 te3MPrintf(db, s
1f2cc 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 qlite3_errmsg(db
1f2cd 29 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b ));. v = 0;
1f2ce 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 . }. } while
1f2cf 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20 ( nAttempt<5 &&
1f2d0 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d rc==SQLITE_SCHEM
1f2d1 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d A );.. if( rc==
1f2d2 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 SQLITE_ROW ){.
1f2d3 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 /* The row-rec
1f2d4 6f 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65 ord has been ope
1f2d5 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ned successfully
1f2d6 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 . Check that the
1f2d7 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 . ** column i
1f2d8 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 n question conta
1f2d9 69 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c ins text or a bl
1f2da 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69 ob. If it contai
1f2db 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 ns. ** text,
1f2dc 69 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20 it is up to the
1f2dd 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68 caller to get th
1f2de 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 e encoding right
1f2df 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 .. */. Inc
1f2e0 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 rblob *pBlob;.
1f2e1 20 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e u32 type = v->
1f2e2 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b apCsr[0]->aType[
1f2e3 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 iCol];.. if(
1f2e4 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 type<12 ){.
1f2e5 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1f2e6 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 b, zErr);.
1f2e7 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 zErr = sqlite3MP
1f2e8 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f rintf(db, "canno
1f2e9 74 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 t open value of
1f2ea 74 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 type %s",.
1f2eb 20 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c type==0?"nul
1f2ec 6c 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 l": type==7?"rea
1f2ed 6c 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 l": "integer".
1f2ee 20 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 );. rc
1f2ef 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1f2f0 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f goto blob_
1f2f1 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a open_out;. }.
1f2f2 20 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 pBlob = (Inc
1f2f3 72 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 rblob *)sqlite3D
1f2f4 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1f2f5 73 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 sizeof(Incrblob)
1f2f6 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
1f2f7 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f2f8 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1f2f9 65 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 ee(db, pBlob);.
1f2fa 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1f2fb 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
1f2fc 20 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 pBlob->flags
1f2fd 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c = flags;. pBl
1f2fe 6f 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 ob->pCsr = v->a
1f2ff 70 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 pCsr[0]->pCursor
1f300 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1f301 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 eeEnterCursor(pB
1f302 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 lob->pCsr);.
1f303 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 sqlite3BtreeCach
1f304 65 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d eOverflow(pBlob-
1f305 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 >pCsr);. sqli
1f306 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 te3BtreeLeaveCur
1f307 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 sor(pBlob->pCsr)
1f308 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 ;. pBlob->pSt
1f309 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 mt = (sqlite3_st
1f30a 6d 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f mt *)v;. pBlo
1f30b 62 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e b->iOffset = v->
1f30c 61 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 apCsr[0]->aOffse
1f30d 74 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c t[iCol];. pBl
1f30e 6f 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 ob->nByte = sqli
1f30f 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1f310 65 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 eLen(type);.
1f311 70 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a pBlob->db = db;.
1f312 20 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 *ppBlob = (s
1f313 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 qlite3_blob *)pB
1f314 6c 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 lob;. rc = SQ
1f315 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
1f316 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1f317 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
1f318 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 3DbFree(db, zErr
1f319 29 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 73 71 );. zErr = sq
1f31a 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
1f31b 20 22 6e 6f 20 73 75 63 68 20 72 6f 77 69 64 3a "no such rowid:
1f31c 20 25 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 %lld", iRow);.
1f31d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1f31e 52 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f RROR;. }..blob_
1f31f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 open_out:. if(
1f320 76 20 26 26 20 28 72 63 21 3d 53 51 4c 49 54 45 v && (rc!=SQLITE
1f321 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f _OK || db->mallo
1f322 63 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 cFailed) ){.
1f323 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c sqlite3VdbeFinal
1f324 69 7a 65 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71 ize(v);. }. sq
1f325 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 lite3Error(db, r
1f326 63 2c 20 7a 45 72 72 29 3b 0a 20 20 73 71 6c 69 c, zErr);. sqli
1f327 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 te3DbFree(db, zE
1f328 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 rr);. sqlite3St
1f329 61 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 ackFree(db, pPar
1f32a 73 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 se);. rc = sqli
1f32b 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 te3ApiExit(db, r
1f32c 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 c);. sqlite3_mu
1f32d 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 tex_leave(db->mu
1f32e 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 tex);. return r
1f32f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 c;.}../*.** Clos
1f330 65 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 e a blob handle
1f331 74 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 that was previou
1f332 73 6c 79 20 63 72 65 61 74 65 64 20 75 73 69 6e sly created usin
1f333 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f g.** sqlite3_blo
1f334 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c b_open()..*/.SQL
1f335 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1f336 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 te3_blob_close(s
1f337 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c qlite3_blob *pBl
1f338 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 ob){. Incrblob
1f339 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a *p = (Incrblob *
1f33a 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 )pBlob;. int rc
1f33b 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
1f33c 0a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 .. if( p ){.
1f33d 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 db = p->db;.
1f33e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1f33f 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b nter(db->mutex);
1f340 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1f341 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 3_finalize(p->pS
1f342 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 tmt);. sqlite
1f343 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 3DbFree(db, p);.
1f344 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1f345 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 x_leave(db->mute
1f346 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 x);. }else{.
1f347 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1f348 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f349 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f ;.}../*.** Perfo
1f34a 72 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 rm a read or wri
1f34b 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 te operation on
1f34c 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 a blob.*/.static
1f34d 20 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72 69 int blobReadWri
1f34e 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c te(. sqlite3_bl
1f34f 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f ob *pBlob, . vo
1f350 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c id *z, . int n,
1f351 20 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 2c . int iOffset,
1f352 20 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 . int (*xCall)
1f353 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c (BtCursor*, u32,
1f354 20 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a u32, void*).){.
1f355 20 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 int rc;. Incr
1f356 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 blob *p = (Incrb
1f357 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 lob *)pBlob;. V
1f358 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 dbe *v;. sqlite
1f359 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3 *db;.. if( p=
1f35a 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
1f35b 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 64 62 20 TE_MISUSE;. db
1f35c 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 = p->db;. sqlit
1f35d 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1f35e 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d b->mutex);. v =
1f35f 20 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 (Vdbe*)p->pStmt
1f360 3b 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 ;.. if( n<0 ||
1f361 69 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f iOffset<0 || (iO
1f362 66 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 ffset+n)>p->nByt
1f363 65 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 e ){. /* Requ
1f364 65 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61 est is out of ra
1f365 6e 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72 nge. Return a tr
1f366 61 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a ansient error. *
1f367 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 /. rc = SQLIT
1f368 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c E_ERROR;. sql
1f369 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 ite3Error(db, SQ
1f36a 4c 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a LITE_ERROR, 0);.
1f36b 20 20 7d 20 65 6c 73 65 20 69 66 28 20 76 3d 3d } else if( v==
1f36c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 0 ){. /* If t
1f36d 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 here is no state
1f36e 6d 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 ment handle, the
1f36f 6e 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c n the blob-handl
1f370 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 e has. ** alr
1f371 65 61 64 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 eady been invali
1f372 64 61 74 65 64 2e 20 52 65 74 75 72 6e 20 53 51 dated. Return SQ
1f373 4c 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 LITE_ABORT in th
1f374 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a is case.. */.
1f375 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1f376 41 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a ABORT;. }else{.
1f377 20 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 /* Call eith
1f378 65 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f er BtreeData() o
1f379 72 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 r BtreePutData()
1f37a 2e 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 . If SQLITE_ABOR
1f37b 54 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 T is. ** retu
1f37c 72 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 rned, clean-up t
1f37d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e he statement han
1f37e 64 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 dle.. */.
1f37f 61 73 73 65 72 74 28 20 64 62 20 3d 3d 20 76 2d assert( db == v-
1f380 3e 64 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 >db );. sqlit
1f381 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 e3BtreeEnterCurs
1f382 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 or(p->pCsr);.
1f383 20 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 rc = xCall(p->p
1f384 43 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e Csr, iOffset+p->
1f385 69 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a iOffset, n, z);.
1f386 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1f387 4c 65 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 LeaveCursor(p->p
1f388 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 Csr);. if( rc
1f389 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 ==SQLITE_ABORT )
1f38a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1f38b 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a dbeFinalize(v);.
1f38c 20 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d p->pStmt =
1f38d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
1f38e 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 db->errCode
1f38f 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e = rc;. v->
1f390 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 rc = rc;. }.
1f391 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 }. rc = sqlite
1f392 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3ApiExit(db, rc)
1f393 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
1f394 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 x_leave(db->mute
1f395 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b x);. return rc;
1f396 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 .}../*.** Read d
1f397 61 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 ata from a blob
1f398 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 handle..*/.SQLIT
1f399 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1f39a 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 3_blob_read(sqli
1f39b 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c te3_blob *pBlob,
1f39c 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c void *z, int n,
1f39d 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 int iOffset){.
1f39e 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 return blobRead
1f39f 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 Write(pBlob, z,
1f3a0 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 n, iOffset, sqli
1f3a1 74 65 33 42 74 72 65 65 44 61 74 61 29 3b 0a 7d te3BtreeData);.}
1f3a2 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
1f3a3 74 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e ta to a blob han
1f3a4 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 dle..*/.SQLITE_A
1f3a5 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1f3a6 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 lob_write(sqlite
1f3a7 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 3_blob *pBlob, c
1f3a8 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e onst void *z, in
1f3a9 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 t n, int iOffset
1f3aa 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 ){. return blob
1f3ab 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c ReadWrite(pBlob,
1f3ac 20 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 (void *)z, n, i
1f3ad 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 Offset, sqlite3B
1f3ae 74 72 65 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a treePutData);.}.
1f3af 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 ./*.** Query a b
1f3b0 6c 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 lob handle for t
1f3b1 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 he size of the d
1f3b2 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 ata..**.** The I
1f3b3 6e 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 ncrblob.nByte fi
1f3b4 65 6c 64 20 69 73 20 66 69 78 65 64 20 66 6f 72 eld is fixed for
1f3b5 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 the lifetime of
1f3b6 20 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a the Incrblob.**
1f3b7 20 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 so no mutex is
1f3b8 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 63 63 required for acc
1f3b9 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ess..*/.SQLITE_A
1f3ba 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1f3bb 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 lob_bytes(sqlite
1f3bc 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 3_blob *pBlob){.
1f3bd 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 Incrblob *p =
1f3be 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f (Incrblob *)pBlo
1f3bf 62 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 b;. return p ?
1f3c0 70 2d 3e 6e 42 79 74 65 20 3a 20 30 3b 0a 7d 0a p->nByte : 0;.}.
1f3c1 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 .#endif /* #ifnd
1f3c2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
1f3c3 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a NCRBLOB */../***
1f3c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1f3c5 6f 66 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a of vdbeblob.c **
1f3c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f3c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f3c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1f3c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1f3ca 6e 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 n file journal.c
1f3cb 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1f3cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f3cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1f3ce 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 32 * 2007 August 22
1f3cf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1f3d0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1f3d1 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1f3d2 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1f3d3 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1f3d4 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1f3d5 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1f3d6 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1f3d7 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1f3d8 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1f3d9 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1f3da 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1f3db 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1f3dc 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1f3dd 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1f3de 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1f3df 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1f3e0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1f3e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f3e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f3e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f3e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1f3e5 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
1f3e6 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 39 20 journal.c,v 1.9
1f3e7 32 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36 2009/01/20 17:06
1f3e8 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :27 danielk1977
1f3e9 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 Exp $.*/..#ifdef
1f3ea 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 SQLITE_ENABLE_A
1f3eb 54 4f 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a TOMIC_WRITE../*.
1f3ec 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 ** This file imp
1f3ed 6c 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 lements a specia
1f3ee 6c 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 l kind of sqlite
1f3ef 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 3_file object us
1f3f0 65 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 ed.** by SQLite
1f3f1 74 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 to create journa
1f3f2 6c 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 l files if the a
1f3f3 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 tomic-write opti
1f3f4 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 mization.** is e
1f3f5 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 nabled..**.** Th
1f3f6 65 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 e distinctive ch
1f3f7 61 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 aracteristic of
1f3f8 74 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c this sqlite3_fil
1f3f9 65 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a e is that the.**
1f3fa 20 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 actual on disk
1f3fb 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 file is created
1f3fc 6c 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 lazily. When the
1f3fd 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 file is created
1f3fe 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 ,.** the caller
1f3ff 73 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66 specifies a buff
1f400 65 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 er size for an i
1f401 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 n-memory buffer
1f402 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f to.** be used to
1f403 20 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20 service read()
1f404 61 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75 and write() requ
1f405 65 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c ests. The actual
1f406 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b file.** on disk
1f407 20 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 is not created
1f408 6f 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 or populated unt
1f409 69 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a il either:.**.**
1f40a 20 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 1) The in-mem
1f40b 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 ory representati
1f40c 6f 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 on grows too lar
1f40d 67 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 ge for the alloc
1f40e 61 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 ated .** bu
1f40f 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 ffer, or.** 2)
1f410 20 54 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 The sqlite3Jour
1f411 6e 61 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63 nalCreate() func
1f412 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a tion is called..
1f413 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f */..../*.** A Jo
1f414 75 72 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 urnalFile object
1f415 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f is a subclass o
1f416 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 f sqlite3_file u
1f417 73 65 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 sed by.** as an
1f418 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 open file handle
1f419 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c for journal fil
1f41a 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f es..*/.struct Jo
1f41b 75 72 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 urnalFile {. sq
1f41c 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
1f41d 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a *pMethod; /*
1f41e 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 I/O methods on
1f41f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f journal files */
1f420 0a 20 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 . int nBuf;
1f421 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f422 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 /* Size of zB
1f423 75 66 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f uf[] in bytes */
1f424 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 . char *zBuf;
1f425 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f426 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 /* Space to b
1f427 75 66 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 uffer journal wr
1f428 69 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 ites */. int iS
1f429 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
1f42a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f /* Amo
1f42b 75 6e 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 unt of zBuf[] cu
1f42c 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a rrently used */.
1f42d 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
1f42e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f42f 20 20 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 /* xOpen flags
1f430 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 */. sqlite3_vf
1f431 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 s *pVfs;
1f432 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 /* The "re
1f433 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 al" underlying V
1f434 46 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f FS */. sqlite3_
1f435 66 69 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 file *pReal;
1f436 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 /* The "
1f437 72 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 real" underlying
1f438 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
1f439 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1f43a 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 *zJournal;
1f43b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1f43c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1f43d 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 e */.};.typedef
1f43e 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 struct JournalFi
1f43f 6c 65 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a le JournalFile;.
1f440 0a 2f 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 ./*.** If it doe
1f441 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 s not already ex
1f442 69 73 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 ists, create and
1f443 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e populate the on
1f444 2d 64 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 -disk file .** f
1f445 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 or JournalFile p
1f446 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1f447 63 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e createFile(Journ
1f448 61 6c 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e alFile *p){. in
1f449 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1f44a 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 ;. if( !p->pRea
1f44b 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 l ){. sqlite3
1f44c 5f 66 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 _file *pReal = (
1f44d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 sqlite3_file *)&
1f44e 70 5b 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 p[1];. rc = s
1f44f 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e qlite3OsOpen(p->
1f450 70 56 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 pVfs, p->zJourna
1f451 6c 2c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 l, pReal, p->fla
1f452 67 73 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 gs, 0);. if(
1f453 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1f454 0a 20 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 . p->pReal
1f455 3d 20 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 = pReal;. i
1f456 66 28 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b f( p->iSize>0 ){
1f457 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1f458 70 2d 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 p->iSize<=p->nBu
1f459 66 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d f);. rc =
1f45a 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
1f45b 70 2d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 p->pReal, p->zBu
1f45c 66 2c 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b f, p->iSize, 0);
1f45d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1f45e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1f45f 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 }../*.** Close t
1f460 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
1f461 69 63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 ic int jrnlClose
1f462 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1f463 4a 66 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 Jfd){. JournalF
1f464 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1f465 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1f466 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a if( p->pReal ){.
1f467 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
1f468 73 65 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 se(p->pReal);.
1f469 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 }. sqlite3_free
1f46a 28 70 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 (p->zBuf);. ret
1f46b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f46c 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 ../*.** Read dat
1f46d 61 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e a from the file.
1f46e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a .*/.static int j
1f46f 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 rnlRead(. sqlit
1f470 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1f471 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1f472 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 file from which
1f473 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f to read */. vo
1f474 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 id *zBuf,
1f475 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 /* Put the
1f476 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a results here */.
1f477 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 int iAmt,
1f478 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1f479 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 er of bytes to r
1f47a 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f ead */. sqlite_
1f47b 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 int64 iOfst
1f47c 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 /* Begin reading
1f47d 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 at this offset
1f47e 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d */.){. int rc =
1f47f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f SQLITE_OK;. Jo
1f480 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 urnalFile *p = (
1f481 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a JournalFile *)pJ
1f482 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 fd;. if( p->pRe
1f483 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 al ){. rc = s
1f484 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e qlite3OsRead(p->
1f485 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d pReal, zBuf, iAm
1f486 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c t, iOfst);. }el
1f487 73 65 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 se if( (iAmt+iOf
1f488 73 74 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a st)>p->iSize ){.
1f489 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1f48a 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
1f48b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d ;. }else{. m
1f48c 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e emcpy(zBuf, &p->
1f48d 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d zBuf[iOfst], iAm
1f48e 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
1f48f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 rc;.}../*.** Wr
1f490 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 ite data to the
1f491 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1f492 69 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 int jrnlWrite(.
1f493 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
1f494 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a Jfd, /* The j
1f495 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f ournal file into
1f496 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 which to write
1f497 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
1f498 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 *zBuf, /* T
1f499 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 ake data to be w
1f49a 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 ritten from here
1f49b 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 */. int iAmt,
1f49c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f49d 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1f49e 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 to write */. sq
1f49f 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 lite_int64 iOfst
1f4a0 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 /* Begin wr
1f4a1 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 iting at this of
1f4a2 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 fset into the fi
1f4a3 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 le */.){. int r
1f4a4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1f4a5 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 JournalFile *p
1f4a6 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a = (JournalFile *
1f4a7 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d )pJfd;. if( !p-
1f4a8 3e 70 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74 >pReal && (iOfst
1f4a9 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 +iAmt)>p->nBuf )
1f4aa 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 {. rc = creat
1f4ab 65 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 eFile(p);. }.
1f4ac 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1f4ad 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e K ){. if( p->
1f4ae 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 pReal ){. r
1f4af 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
1f4b0 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 te(p->pReal, zBu
1f4b1 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b f, iAmt, iOfst);
1f4b2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1f4b3 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 memcpy(&p->zBu
1f4b4 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 f[iOfst], zBuf,
1f4b5 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 iAmt);. if(
1f4b6 20 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 p->iSize<(iOfst
1f4b7 2b 69 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 +iAmt) ){.
1f4b8 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f p->iSize = (iO
1f4b9 66 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 fst+iAmt);.
1f4ba 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
1f4bb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1f4bc 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 ** Truncate the
1f4bd 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1f4be 69 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 int jrnlTruncate
1f4bf 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1f4c0 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 Jfd, sqlite_int6
1f4c1 34 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 4 size){. int r
1f4c2 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1f4c3 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 JournalFile *p
1f4c4 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a = (JournalFile *
1f4c5 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e )pJfd;. if( p->
1f4c6 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 pReal ){. rc
1f4c7 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
1f4c8 61 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 ate(p->pReal, si
1f4c9 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ze);. }else if(
1f4ca 20 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 size<p->iSize )
1f4cb 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d {. p->iSize =
1f4cc 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 size;. }. ret
1f4cd 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1f4ce 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a Sync the file..
1f4cf 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f4d0 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 nlSync(sqlite3_f
1f4d1 69 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 ile *pJfd, int f
1f4d2 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b lags){. int rc;
1f4d3 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a . JournalFile *
1f4d4 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 p = (JournalFile
1f4d5 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 *)pJfd;. if( p
1f4d6 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 ->pReal ){. r
1f4d7 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e c = sqlite3OsSyn
1f4d8 63 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 c(p->pReal, flag
1f4d9 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 s);. }else{.
1f4da 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1f4db 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f4dc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 ;.}../*.** Query
1f4dd 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1f4de 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a file in bytes..
1f4df 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f4e0 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 nlFileSize(sqlit
1f4e1 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 e3_file *pJfd, s
1f4e2 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 qlite_int64 *pSi
1f4e3 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ze){. int rc =
1f4e4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 SQLITE_OK;. Jou
1f4e5 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a rnalFile *p = (J
1f4e6 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 ournalFile *)pJf
1f4e7 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 d;. if( p->pRea
1f4e8 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 l ){. rc = sq
1f4e9 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
1f4ea 70 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 p->pReal, pSize)
1f4eb 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a ;. }else{. *
1f4ec 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f pSize = (sqlite_
1f4ed 69 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b int64) p->iSize;
1f4ee 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f4ef 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 ;.}../*.** Table
1f4f0 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 of methods for
1f4f1 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 JournalFile sqli
1f4f2 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e te3_file object.
1f4f3 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
1f4f4 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
1f4f5 68 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 hods JournalFile
1f4f6 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c Methods = {. 1,
1f4f7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f4f8 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 iVersion */. jr
1f4f9 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 nlClose, /*
1f4fa 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c xClose */. jrnl
1f4fb 52 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 Read, /* xR
1f4fc 65 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 ead */. jrnlWri
1f4fd 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 te, /* xWrit
1f4fe 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 e */. jrnlTrunc
1f4ff 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 ate, /* xTrunca
1f500 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 te */. jrnlSync
1f501 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 , /* xSync
1f502 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a */. jrnlFileSiz
1f503 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 e, /* xFileSize
1f504 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1f505 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f /* xLock */
1f506 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 . 0,
1f507 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a /* xUnlock */.
1f508 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
1f509 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 /* xCheckReserv
1f50a 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 edLock */. 0,
1f50b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 /* xF
1f50c 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 ileControl */.
1f50d 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1f50e 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f * xSectorSize */
1f50f 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 . 0
1f510 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 /* xDeviceChar
1f511 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d acteristics */.}
1f512 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 ;../* .** Open a
1f513 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
1f514 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f515 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 int sqlite3Jour
1f516 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 nalOpen(. sqlit
1f517 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
1f518 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 /* The VFS
1f519 20 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 to use for actu
1f51a 61 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 al file I/O */.
1f51b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
1f51c 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e me, /* N
1f51d 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ame of the journ
1f51e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c al file */. sql
1f51f 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c ite3_file *pJfd,
1f520 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c /* Preal
1f521 6c 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 located, blank f
1f522 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ile handle */.
1f523 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
1f524 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
1f525 65 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 ening flags */.
1f526 20 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 int nBuf
1f527 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
1f528 79 74 65 73 20 62 75 66 66 65 72 65 64 20 62 65 ytes buffered be
1f529 66 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 fore opening the
1f52a 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f file */.){. Jo
1f52b 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 urnalFile *p = (
1f52c 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a JournalFile *)pJ
1f52d 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 fd;. memset(p,
1f52e 30 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 0, sqlite3Journa
1f52f 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 lSize(pVfs));.
1f530 69 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 if( nBuf>0 ){.
1f531 20 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 p->zBuf = sqli
1f532 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 te3MallocZero(nB
1f533 75 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d uf);. if( !p-
1f534 3e 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 >zBuf ){. r
1f535 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1f536 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 EM;. }. }els
1f537 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 e{. return sq
1f538 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 lite3OsOpen(pVfs
1f539 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 , zName, pJfd, f
1f53a 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 lags, 0);. }.
1f53b 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f p->pMethod = &Jo
1f53c 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 urnalFileMethods
1f53d 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 ;. p->nBuf = nB
1f53e 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d uf;. p->flags =
1f53f 20 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f flags;. p->zJo
1f540 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 urnal = zName;.
1f541 20 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b p->pVfs = pVfs;
1f542 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1f543 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 _OK;.}../*.** If
1f544 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 the argument p
1f545 70 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 points to a Jour
1f546 6e 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 nalFile structur
1f547 65 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 e, and the under
1f548 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 lying.** file ha
1f549 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 s not yet been c
1f54a 72 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 reated, create i
1f54b 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 t now..*/.SQLITE
1f54c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1f54d 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 ite3JournalCreat
1f54e 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
1f54f 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 p){. if( p->pMe
1f550 74 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 thods!=&JournalF
1f551 69 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 ileMethods ){.
1f552 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1f553 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e OK;. }. return
1f554 20 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 createFile((Jou
1f555 72 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d rnalFile *)p);.}
1f556 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 ../* .** Return
1f557 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
1f558 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 tes required to
1f559 73 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 store a JournalF
1f55a 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 76 66 ile that uses vf
1f55b 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 s.** pVfs to cre
1f55c 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 ate the underlyi
1f55d 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 ng on-disk files
1f55e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f55f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a ATE int sqlite3J
1f560 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 ournalSize(sqlit
1f561 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 e3_vfs *pVfs){.
1f562 20 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 return (pVfs->s
1f563 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a zOsFile+sizeof(J
1f564 6f 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a ournalFile));.}.
1f565 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
1f566 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a ******* End of j
1f567 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a ournal.c *******
1f568 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f569 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f56a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
1f56b 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
1f56c 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 le memjournal.c
1f56d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f56e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f56f 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
1f570 30 38 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 08 October 7.**.
1f571 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1f572 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1f573 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1f574 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1f575 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1f576 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1f577 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1f578 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1f579 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1f57a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1f57b 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1f57c 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1f57d 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1f57e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1f57f 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1f580 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1f581 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1f582 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1f583 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f584 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f585 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f586 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
1f587 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
1f588 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f ains code use to
1f589 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e implement an in
1f58a 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b -memory rollback
1f58b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 journal..** The
1f58c 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 in-memory rollb
1f58d 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 ack journal is u
1f58e 73 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 sed to journal t
1f58f 72 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a ransactions for.
1f590 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 ** ":memory:" da
1f591 74 61 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e tabases and when
1f592 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 the journal_mod
1f593 65 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 e=MEMORY pragma
1f594 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 40 is used..**.** @
1f595 28 23 29 20 24 49 64 3a 20 6d 65 6d 6a 6f 75 72 (#) $Id: memjour
1f596 6e 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 nal.c,v 1.12 200
1f597 39 2f 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30 9/05/04 11:42:30
1f598 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
1f599 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 $.*/../* Forwar
1f59a 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 d references to
1f59b 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 internal structu
1f59c 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 res */.typedef s
1f59d 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c truct MemJournal
1f59e 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70 MemJournal;.typ
1f59f 65 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 edef struct File
1f5a0 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b Point FilePoint;
1f5a1 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1f5a2 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68 FileChunk FileCh
1f5a3 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74 unk;../* Space t
1f5a4 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62 o hold the rollb
1f5a5 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 ack journal is a
1f5a6 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72 llocated in incr
1f5a7 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69 ements of.** thi
1f5a8 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2a s many bytes..**
1f5a9 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 63 68 6f .** The size cho
1f5aa 73 65 6e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 sen is a little
1f5ab 6c 65 73 73 20 74 68 61 6e 20 61 20 70 6f 77 65 less than a powe
1f5ac 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20 r of two. That
1f5ad 77 61 79 2c 0a 2a 2a 20 74 68 65 20 46 69 6c 65 way,.** the File
1f5ae 43 68 75 6e 6b 20 6f 62 6a 65 63 74 20 77 69 6c Chunk object wil
1f5af 6c 20 68 61 76 65 20 61 20 73 69 7a 65 20 74 68 l have a size th
1f5b0 61 74 20 61 6c 6d 6f 73 74 20 65 78 61 63 74 6c at almost exactl
1f5b1 79 20 66 69 6c 6c 73 0a 2a 2a 20 61 20 70 6f 77 y fills.** a pow
1f5b2 65 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61 er-of-two alloca
1f5b3 74 69 6f 6e 2e 20 20 54 68 69 73 20 6d 69 6d 69 tion. This mimi
1f5b4 6d 69 7a 65 73 20 77 61 73 74 65 64 20 73 70 61 mizes wasted spa
1f5b5 63 65 20 69 6e 20 70 6f 77 65 72 2d 6f 66 2d 74 ce in power-of-t
1f5b6 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c wo.** memory all
1f5b7 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 ocators..*/.#def
1f5b8 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e ine JOURNAL_CHUN
1f5b9 4b 53 49 5a 45 20 28 28 69 6e 74 29 28 31 30 32 KSIZE ((int)(102
1f5ba 34 2d 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 4-sizeof(FileChu
1f5bb 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61 63 72 6f nk*)))../* Macro
1f5bc 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e to find the min
1f5bd 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 imum of two nume
1f5be 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 ric values..*/.#
1f5bf 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 ifndef MIN.# def
1f5c0 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 ine MIN(x,y) ((x
1f5c1 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 )<(y)?(x):(y)).#
1f5c2 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
1f5c3 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
1f5c4 6c 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 l is composed of
1f5c5 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f a linked list o
1f5c6 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
1f5c7 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 es..*/.struct Fi
1f5c8 6c 65 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 leChunk {. File
1f5c9 43 68 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20 Chunk *pNext;
1f5ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1f5cb 65 78 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65 ext chunk in the
1f5cc 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 journal */. u8
1f5cd 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f zChunk[JOURNAL_
1f5ce 43 48 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a CHUNKSIZE]; /*
1f5cf 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 Content of this
1f5d0 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a chunk */.};../*
1f5d1 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
1f5d2 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 of this object s
1f5d3 65 72 76 65 73 20 61 73 20 61 20 63 75 72 73 6f erves as a curso
1f5d4 72 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 r into the rollb
1f5d5 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 ack journal..**
1f5d6 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 The cursor can b
1f5d7 65 20 65 69 74 68 65 72 20 66 6f 72 20 72 65 61 e either for rea
1f5d8 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e ding or writing.
1f5d9 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50 .*/.struct FileP
1f5da 6f 69 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 oint {. sqlite3
1f5db 5f 69 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20 _int64 iOffset;
1f5dc 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
1f5dd 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 et from the begi
1f5de 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c nning of the fil
1f5df 65 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b e */. FileChunk
1f5e0 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 *pChunk;
1f5e1 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 /* Specif
1f5e2 69 63 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68 ic chunk into wh
1f5e3 69 63 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 ich cursor point
1f5e4 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 s */.};../*.** T
1f5e5 68 69 73 20 73 75 62 63 6c 61 73 73 20 69 73 20 his subclass is
1f5e6 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 a subclass of sq
1f5e7 6c 69 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63 lite3_file. Eac
1f5e8 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f h open memory-jo
1f5e9 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 urnal.** is an i
1f5ea 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
1f5eb 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 class..*/.struct
1f5ec 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 MemJournal {.
1f5ed 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
1f5ee 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 ds *pMethod;
1f5ef 2f 2a 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e /* Parent class.
1f5f0 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 20 2a MUST BE FIRST *
1f5f1 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 /. FileChunk *p
1f5f2 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 First;
1f5f3 20 20 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69 /* Head of i
1f5f4 6e 2d 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c n-memory chunk-l
1f5f5 69 73 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 ist */. FilePoi
1f5f6 6e 74 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20 nt endpoint;
1f5f7 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
1f5f8 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ter to the end o
1f5f9 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 f the file */.
1f5fa 46 69 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f FilePoint readpo
1f5fb 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 int;
1f5fc 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
1f5fd 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 e end of the las
1f5fe 74 20 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a t xRead() */.};.
1f5ff 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
1f600 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d from the in-mem
1f601 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ory journal file
1f602 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 69 . This is the i
1f603 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a mplementation.**
1f604 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
1f605 76 66 73 2e 78 52 65 61 64 20 6d 65 74 68 6f 64 vfs.xRead method
1f606 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1f607 6d 65 6d 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73 memjrnlRead(. s
1f608 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1f609 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 d, /* The jou
1f60a 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 rnal file from w
1f60b 68 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a hich to read */.
1f60c 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 void *zBuf,
1f60d 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 /* Put
1f60e 74 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 the results here
1f60f 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 */. int iAmt,
1f610 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f611 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1f612 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c to read */. sql
1f613 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 ite_int64 iOfst
1f614 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 /* Begin rea
1f615 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 ding at this off
1f616 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a set */.){. MemJ
1f617 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d ournal *p = (Mem
1f618 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a Journal *)pJfd;.
1f619 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 7a 42 75 u8 *zOut = zBu
1f61a 66 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64 20 3d f;. int nRead =
1f61b 20 69 41 6d 74 3b 0a 20 20 69 6e 74 20 69 43 68 iAmt;. int iCh
1f61c 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 46 69 6c unkOffset;. Fil
1f61d 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a eChunk *pChunk;.
1f61e 0a 20 20 2f 2a 20 53 51 4c 69 74 65 20 6e 65 76 . /* SQLite nev
1f61f 65 72 20 74 72 69 65 73 20 74 6f 20 72 65 61 64 er tries to read
1f620 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
1f621 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 a rollback jour
1f622 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 nal file */. as
1f623 73 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74 sert( iOfst+iAmt
1f624 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f <=p->endpoint.iO
1f625 66 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20 ffset );.. if(
1f626 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 p->readpoint.iOf
1f627 66 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69 fset!=iOfst || i
1f628 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 Ofst==0 ){. s
1f629 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 qlite3_int64 iOf
1f62a 66 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 f = 0;. for(p
1f62b 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b Chunk=p->pFirst;
1f62c 20 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59 53 . ALWAYS
1f62d 28 70 43 68 75 6e 6b 29 20 26 26 20 28 69 4f 66 (pChunk) && (iOf
1f62e 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 f+JOURNAL_CHUNKS
1f62f 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20 20 20 IZE)<=iOfst;.
1f630 20 20 20 20 20 70 43 68 75 6e 6b 3d 70 43 68 75 pChunk=pChu
1f631 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b nk->pNext. ){
1f632 0a 20 20 20 20 20 20 69 4f 66 66 20 2b 3d 20 4a . iOff += J
1f633 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1f634 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1f635 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d . pChunk = p-
1f636 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e >readpoint.pChun
1f637 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75 6e 6b k;. }.. iChunk
1f638 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 69 Offset = (int)(i
1f639 4f 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 Ofst%JOURNAL_CHU
1f63a 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a NKSIZE);. do {.
1f63b 20 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d int iSpace =
1f63c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 JOURNAL_CHUNKSI
1f63d 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 ZE - iChunkOffse
1f63e 74 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 t;. int nCopy
1f63f 20 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a = MIN(nRead, (J
1f640 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1f641 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 - iChunkOffset)
1f642 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f );. memcpy(zO
1f643 75 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68 ut, &pChunk->zCh
1f644 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 unk[iChunkOffset
1f645 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a ], nCopy);. z
1f646 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20 Out += nCopy;.
1f647 20 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 61 63 nRead -= iSpac
1f648 65 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f 66 66 e;. iChunkOff
1f649 73 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 set = 0;. } whi
1f64a 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20 26 26 20 le( nRead>=0 &&
1f64b 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e (pChunk=pChunk->
1f64c 70 4e 65 78 74 29 21 3d 30 20 26 26 20 6e 52 65 pNext)!=0 && nRe
1f64d 61 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61 ad>0 );. p->rea
1f64e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d dpoint.iOffset =
1f64f 20 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70 iOfst+iAmt;. p
1f650 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 ->readpoint.pChu
1f651 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20 nk = pChunk;..
1f652 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1f653 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
1f654 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c data to the fil
1f655 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1f656 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20 memjrnlWrite(.
1f657 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
1f658 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a Jfd, /* The j
1f659 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f ournal file into
1f65a 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 which to write
1f65b 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
1f65c 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 *zBuf, /* T
1f65d 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 ake data to be w
1f65e 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 ritten from here
1f65f 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 */. int iAmt,
1f660 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f661 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1f662 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 to write */. sq
1f663 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 lite_int64 iOfst
1f664 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 /* Begin wr
1f665 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 iting at this of
1f666 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 fset into the fi
1f667 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f le */.){. MemJo
1f668 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a urnal *p = (MemJ
1f669 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 ournal *)pJfd;.
1f66a 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41 int nWrite = iA
1f66b 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65 mt;. u8 *zWrite
1f66c 20 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a = (u8 *)zBuf;..
1f66d 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 /* An in-memor
1f66e 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 y journal file s
1f66f 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20 hould only ever
1f670 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20 be appended to.
1f671 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65 Random. ** acce
1f672 73 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f ss writes are no
1f673 74 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71 t required by sq
1f674 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 lite.. */. ass
1f675 65 72 74 28 20 69 4f 66 73 74 3d 3d 70 2d 3e 65 ert( iOfst==p->e
1f676 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1f677 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
1f678 4d 45 54 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20 METER(iOfst);..
1f679 20 77 68 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 while( nWrite>0
1f67a 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e ){. FileChun
1f67b 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 k *pChunk = p->e
1f67c 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a ndpoint.pChunk;.
1f67d 20 20 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 int iChunkOf
1f67e 66 73 65 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e fset = (int)(p->
1f67f 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 endpoint.iOffset
1f680 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 %JOURNAL_CHUNKSI
1f681 5a 45 29 3b 0a 20 20 20 20 69 6e 74 20 69 53 70 ZE);. int iSp
1f682 61 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 74 65 ace = MIN(nWrite
1f683 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 , JOURNAL_CHUNKS
1f684 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 IZE - iChunkOffs
1f685 65 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 43 et);.. if( iC
1f686 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 29 7b hunkOffset==0 ){
1f687 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 68 . /* New ch
1f688 75 6e 6b 20 69 73 20 72 65 71 75 69 72 65 64 20 unk is required
1f689 74 6f 20 65 78 74 65 6e 64 20 74 68 65 20 66 69 to extend the fi
1f68a 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 69 6c le. */. Fil
1f68b 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d 20 73 eChunk *pNew = s
1f68c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
1f68d 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b 29 29 zeof(FileChunk))
1f68e 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 ;. if( !pNe
1f68f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 w ){. ret
1f690 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
1f691 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a _NOMEM;. }.
1f692 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 pNew->pNex
1f693 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 t = 0;. if(
1f694 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 20 pChunk ){.
1f695 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46 assert( p->pF
1f696 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 irst );.
1f697 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 pChunk->pNext =
1f698 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 pNew;. }els
1f699 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 e{. asser
1f69a 74 28 20 21 70 2d 3e 70 46 69 72 73 74 20 29 3b t( !p->pFirst );
1f69b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 69 72 . p->pFir
1f69c 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 st = pNew;.
1f69d 20 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e 64 70 }. p->endp
1f69e 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 4e oint.pChunk = pN
1f69f 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d ew;. }.. m
1f6a0 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70 6f 69 emcpy(&p->endpoi
1f6a1 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e nt.pChunk->zChun
1f6a2 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c k[iChunkOffset],
1f6a3 20 7a 57 72 69 74 65 2c 20 69 53 70 61 63 65 29 zWrite, iSpace)
1f6a4 3b 0a 20 20 20 20 7a 57 72 69 74 65 20 2b 3d 20 ;. zWrite +=
1f6a5 69 53 70 61 63 65 3b 0a 20 20 20 20 6e 57 72 69 iSpace;. nWri
1f6a6 74 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 te -= iSpace;.
1f6a7 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f p->endpoint.iO
1f6a8 66 66 73 65 74 20 2b 3d 20 69 53 70 61 63 65 3b ffset += iSpace;
1f6a9 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 . }.. return S
1f6aa 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1f6ab 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 ** Truncate the
1f6ac 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1f6ad 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 int memjrnlTrunc
1f6ae 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
1f6af 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 *pJfd, sqlite_i
1f6b0 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 4d 65 nt64 size){. Me
1f6b1 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d mJournal *p = (M
1f6b2 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 emJournal *)pJfd
1f6b3 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 ;. FileChunk *p
1f6b4 43 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72 74 28 Chunk;. assert(
1f6b5 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e 55 53 size==0);. UNUS
1f6b6 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73 69 7a ED_PARAMETER(siz
1f6b7 65 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d 20 70 e);. pChunk = p
1f6b8 2d 3e 70 46 69 72 73 74 3b 0a 20 20 77 68 69 6c ->pFirst;. whil
1f6b9 65 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 e( pChunk ){.
1f6ba 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54 6d 70 FileChunk *pTmp
1f6bb 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 = pChunk;. p
1f6bc 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e Chunk = pChunk->
1f6bd 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 pNext;. sqlit
1f6be 65 33 5f 66 72 65 65 28 70 54 6d 70 29 3b 0a 20 e3_free(pTmp);.
1f6bf 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a }. sqlite3MemJ
1f6c0 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66 64 29 ournalOpen(pJfd)
1f6c1 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1f6c2 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
1f6c3 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a lose the file..*
1f6c4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d /.static int mem
1f6c5 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 jrnlClose(sqlite
1f6c6 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 3_file *pJfd){.
1f6c7 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 memjrnlTruncate
1f6c8 28 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72 65 74 (pJfd, 0);. ret
1f6c9 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f6ca 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 .../*.** Sync th
1f6cb 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 79 e file..**.** Sy
1f6cc 6e 63 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f ncing an in-memo
1f6cd 72 79 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 20 ry journal is a
1f6ce 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20 69 6e 20 no-op. And, in
1f6cf 66 61 63 74 2c 20 74 68 69 73 20 72 6f 75 74 69 fact, this routi
1f6d0 6e 65 0a 2a 2a 20 69 73 20 6e 65 76 65 72 20 63 ne.** is never c
1f6d1 61 6c 6c 65 64 20 69 6e 20 61 20 77 6f 72 6b 69 alled in a worki
1f6d2 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
1f6d3 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 n. This impleme
1f6d4 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65 78 69 73 74 ntation.** exist
1f6d5 73 20 70 75 72 65 6c 79 20 61 73 20 61 20 63 6f s purely as a co
1f6d6 6e 74 69 6e 67 65 6e 63 79 2c 20 69 6e 20 63 61 ntingency, in ca
1f6d7 73 65 20 73 6f 6d 65 20 6d 61 6c 66 75 6e 63 74 se some malfunct
1f6d8 69 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 ion in some othe
1f6d9 72 0a 2a 2a 20 70 61 72 74 20 6f 66 20 53 51 4c r.** part of SQL
1f6da 69 74 65 20 63 61 75 73 65 73 20 53 79 6e 63 20 ite causes Sync
1f6db 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 to be called by
1f6dc 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73 74 61 74 mistake..*/.stat
1f6dd 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 79 ic int memjrnlSy
1f6de 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 nc(sqlite3_file
1f6df 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f *NotUsed, int No
1f6e0 74 55 73 65 64 32 29 7b 20 20 20 2f 2a 4e 4f 5f tUsed2){ /*NO_
1f6e1 54 45 53 54 2a 2f 0a 20 20 55 4e 55 53 45 44 5f TEST*/. UNUSED_
1f6e2 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 PARAMETER2(NotUs
1f6e3 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 20 20 ed, NotUsed2);
1f6e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6e5 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
1f6e6 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b /. assert( 0 );
1f6e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6ea 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 72 /*NO_TEST*/. r
1f6eb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1f6ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f /*NO
1f6ef 5f 54 45 53 54 2a 2f 0a 7d 20 20 20 20 20 20 20 _TEST*/.}
1f6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6f3 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 /*NO_TEST
1f6f4 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 */../*.** Query
1f6f5 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
1f6f6 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a file in bytes..*
1f6f7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d /.static int mem
1f6f8 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c jrnlFileSize(sql
1f6f9 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c ite3_file *pJfd,
1f6fa 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 sqlite_int64 *p
1f6fb 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 Size){. MemJour
1f6fc 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 nal *p = (MemJou
1f6fd 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 2a rnal *)pJfd;. *
1f6fe 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f pSize = (sqlite_
1f6ff 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64 70 6f 69 int64) p->endpoi
1f700 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20 20 72 65 nt.iOffset;. re
1f701 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1f702 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f }../*.** Table o
1f703 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d 65 f methods for Me
1f704 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65 33 mJournal sqlite3
1f705 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f _file object..*/
1f706 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73 .static struct s
1f707 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
1f708 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 s MemJournalMeth
1f709 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 ods = {. 1,
1f70a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 /* i
1f70b 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6d 65 6d Version */. mem
1f70c 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f jrnlClose, /
1f70d 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6d 65 * xClose */. me
1f70e 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20 mjrnlRead,
1f70f 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6d 65 /* xRead */. me
1f710 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 20 mjrnlWrite,
1f711 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 6d /* xWrite */. m
1f712 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 emjrnlTruncate,
1f713 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f /* xTruncate */
1f714 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c 20 . memjrnlSync,
1f715 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f /* xSync */
1f716 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 . memjrnlFileSi
1f717 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a ze, /* xFileSiz
1f718 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 e */. 0,
1f719 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
1f71a 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 k */. 0,
1f71b 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c /* xUnl
1f71c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 ock */. 0,
1f71d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
1f71e 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
1f71f 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1f720 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 /* xFile
1f721 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 Control */. 0,
1f722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f723 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f * xSectorSize */
1f724 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 . 0
1f725 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43 /* xDeviceC
1f726 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 2a haracteristics *
1f727 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 /.};../* .** Ope
1f728 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 n a journal file
1f729 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f72a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f72b 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 MemJournalOpen(s
1f72c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1f72d 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c d){. MemJournal
1f72e 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 *p = (MemJourna
1f72f 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 61 73 73 65 l *)pJfd;. asse
1f730 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
1f731 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29 3b 0a 20 LIGNMENT(p) );.
1f732 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 memset(p, 0, sq
1f733 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 lite3MemJournalS
1f734 69 7a 65 28 29 29 3b 0a 20 20 70 2d 3e 70 4d 65 ize());. p->pMe
1f735 74 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e thod = &MemJourn
1f736 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a alMethods;.}../*
1f737 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
1f738 69 66 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 if the file-hand
1f739 6c 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 le passed as an
1f73a 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 argument is .**
1f73b 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
1f73c 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f rnal .*/.SQLITE_
1f73d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1f73e 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 te3IsMemJournal(
1f73f 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1f740 66 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a fd){. return pJ
1f741 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d fd->pMethods==&M
1f742 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 emJournalMethods
1f743 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 ;.}../* .** Retu
1f744 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
1f745 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 bytes required
1f746 74 6f 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f to store a MemJo
1f747 75 72 6e 61 6c 20 74 68 61 74 20 75 73 65 73 20 urnal that uses
1f748 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 vfs.** pVfs to c
1f749 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c reate the underl
1f74a 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c ying on-disk fil
1f74b 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
1f74c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1f74d 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
1f74e 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 void){. return
1f74f 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61 sizeof(MemJourna
1f750 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a l);.}../********
1f751 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
1f752 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a mjournal.c *****
1f753 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f754 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f755 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1f756 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1f757 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a e walker.c *****
1f758 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f759 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f75a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1f75b 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a 8 August 16.**.*
1f75c 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1f75d 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1f75e 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1f75f 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1f760 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1f761 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1f762 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1f763 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1f764 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1f765 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1f766 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1f767 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1f768 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1f769 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1f76a 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1f76b 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1f76c 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1f76d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1f76e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f76f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f771 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1f772 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1f773 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 routines used f
1f774 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 or walking the p
1f775 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a arser tree for.*
1f776 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 * an SQL stateme
1f777 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 nt..**.** $Id: w
1f778 61 6c 6b 65 72 2e 63 2c 76 20 31 2e 37 20 32 30 alker.c,v 1.7 20
1f779 30 39 2f 30 36 2f 31 35 20 32 33 3a 31 35 3a 35 09/06/15 23:15:5
1f77a 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 9 drh Exp $.*/..
1f77b 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 ./*.** Walk an e
1f77c 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
1f77d 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c Invoke the call
1f77e 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 back once for ea
1f77f 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 ch node.** of th
1f780 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 e expression, wh
1f781 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 ile decending.
1f782 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c (In other words,
1f783 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a the callback.**
1f784 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f is invoked befo
1f785 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c re visiting chil
1f786 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 dren.).**.** The
1f787 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 return value fr
1f788 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 om the callback
1f789 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 should be one of
1f78a 20 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f the WRC_*.** co
1f78b 6e 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69 nstants to speci
1f78c 66 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 fy how to procee
1f78d 64 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e d with the walk.
1f78e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f .**.** WRC_Co
1f78f 6e 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 ntinue Cont
1f790 69 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20 inue descending
1f791 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a down the tree..*
1f792 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e *.** WRC_Prun
1f793 65 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 e Do not
1f794 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 descend into ch
1f795 69 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 ild nodes. But
1f796 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 allow.**
1f797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
1f798 65 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e e walk to contin
1f799 75 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20 ue with sibling
1f79a 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 nodes..**.**
1f79b 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 WRC_Abort
1f79c 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c Do no more cal
1f79d 6c 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 lbacks. Unwind
1f79e 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a the stack and.**
1f79f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f7a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65 return the
1f7a1 20 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 top-level walk
1f7a2 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 call..**.** The
1f7a3 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f return value fro
1f7a4 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 m this routine i
1f7a5 73 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 s WRC_Abort to a
1f7a6 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 bandon the tree
1f7a7 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f walk.** and WRC_
1f7a8 43 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 Continue to cont
1f7a9 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f inue..*/.SQLITE_
1f7aa 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1f7ab 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b te3WalkExpr(Walk
1f7ac 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 er *pWalker, Exp
1f7ad 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 r *pExpr){. int
1f7ae 20 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72 rc;. if( pExpr
1f7af 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 ==0 ) return WRC
1f7b0 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73 _Continue;. tes
1f7b1 74 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 tcase( ExprHasPr
1f7b2 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1f7b3 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 _TokenOnly) );.
1f7b4 20 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48 testcase( ExprH
1f7b5 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
1f7b6 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b , EP_Reduced) );
1f7b7 0a 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d . rc = pWalker-
1f7b8 3e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 >xExprCallback(p
1f7b9 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a Walker, pExpr);.
1f7ba 20 20 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f if( rc==WRC_Co
1f7bb 6e 74 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20 ntinue.
1f7bc 20 20 20 20 20 26 26 20 21 45 78 70 72 48 61 73 && !ExprHas
1f7bd 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 AnyProperty(pExp
1f7be 72 2c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 r,EP_TokenOnly)
1f7bf 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
1f7c0 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b e3WalkExpr(pWalk
1f7c1 65 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 er, pExpr->pLeft
1f7c2 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1f7c3 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71 bort;. if( sq
1f7c4 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 lite3WalkExpr(pW
1f7c5 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 alker, pExpr->pR
1f7c6 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 57 ight) ) return W
1f7c7 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 RC_Abort;. if
1f7c8 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
1f7c9 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
1f7ca 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 elect) ){.
1f7cb 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 if( sqlite3WalkS
1f7cc 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 elect(pWalker, p
1f7cd 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 Expr->x.pSelect)
1f7ce 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1f7cf 6f 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ort;. }else{.
1f7d0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1f7d1 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 3WalkExprList(pW
1f7d2 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e alker, pExpr->x.
1f7d3 70 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 pList) ) return
1f7d4 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d WRC_Abort;. }
1f7d5 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f7d6 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a & WRC_Abort;.}.
1f7d7 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 ./*.** Call sqli
1f7d8 74 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f te3WalkExpr() fo
1f7d9 72 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 r every expressi
1f7da 6f 6e 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 on in list p or
1f7db 75 6e 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 until.** an abor
1f7dc 74 20 72 65 71 75 65 73 74 20 69 73 20 73 65 65 t request is see
1f7dd 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1f7de 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1f7df 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c WalkExprList(Wal
1f7e0 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 ker *pWalker, Ex
1f7e1 70 72 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e prList *p){. in
1f7e2 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 t i;. struct Ex
1f7e3 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
1f7e4 65 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 em;. if( p ){.
1f7e5 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 for(i=p->nExp
1f7e6 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 r, pItem=p->a; i
1f7e7 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b >0; i--, pItem++
1f7e8 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
1f7e9 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1f7ea 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 lker, pItem->pEx
1f7eb 70 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 pr) ) return WRC
1f7ec 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
1f7ed 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 }. return WRC_C
1f7ee 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ontinue;.}../*.*
1f7ef 2a 20 57 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 * Walk all expre
1f7f0 73 73 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 ssions associate
1f7f1 64 20 77 69 74 68 20 53 45 4c 45 43 54 20 73 74 d with SELECT st
1f7f2 61 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a atement p. Do.*
1f7f3 2a 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 * not invoke the
1f7f4 20 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b SELECT callback
1f7f5 20 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 28 6f on p, but do (o
1f7f6 66 20 63 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 f course) invoke
1f7f7 0a 2a 2a 20 61 6e 79 20 65 78 70 72 20 63 61 6c .** any expr cal
1f7f8 6c 62 61 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 lbacks and SELEC
1f7f9 54 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 T callbacks that
1f7fa 20 63 6f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 come from subqu
1f7fb 65 72 69 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e eries..** Return
1f7fc 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 WRC_Abort or WR
1f7fd 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 C_Continue..*/.S
1f7fe 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1f7ff 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c t sqlite3WalkSel
1f800 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a ectExpr(Walker *
1f801 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 pWalker, Select
1f802 2a 70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 *p){. if( sqlit
1f803 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 e3WalkExprList(p
1f804 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 Walker, p->pELis
1f805 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f t) ) return WRC_
1f806 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c Abort;. if( sql
1f807 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1f808 6c 6b 65 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 lker, p->pWhere)
1f809 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1f80a 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ort;. if( sqlit
1f80b 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 e3WalkExprList(p
1f80c 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 Walker, p->pGrou
1f80d 70 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 pBy) ) return WR
1f80e 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 C_Abort;. if( s
1f80f 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1f810 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 Walker, p->pHavi
1f811 6e 67 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 ng) ) return WRC
1f812 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 _Abort;. if( sq
1f813 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1f814 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f t(pWalker, p->pO
1f815 72 64 65 72 42 79 29 20 29 20 72 65 74 75 72 6e rderBy) ) return
1f816 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 WRC_Abort;. if
1f817 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 ( sqlite3WalkExp
1f818 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c r(pWalker, p->pL
1f819 69 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57 imit) ) return W
1f81a 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 RC_Abort;. if(
1f81b 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1f81c 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 pWalker, p->pOff
1f81d 73 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 set) ) return WR
1f81e 43 5f 41 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 C_Abort;. retur
1f81f 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a n WRC_Continue;.
1f820 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 }../*.** Walk th
1f821 65 20 70 61 72 73 65 20 74 72 65 65 73 20 61 73 e parse trees as
1f822 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c sociated with al
1f823 6c 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 l subqueries in
1f824 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 the.** FROM clau
1f825 73 65 20 6f 66 20 53 45 4c 45 43 54 20 73 74 61 se of SELECT sta
1f826 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f tement p. Do no
1f827 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c t invoke the sel
1f828 65 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 ect.** callback
1f829 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 on p, but do inv
1f82a 6f 6b 65 20 69 74 20 6f 6e 20 65 61 63 68 20 46 oke it on each F
1f82b 52 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75 ROM clause subqu
1f82c 65 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e ery.** and on an
1f82d 79 20 73 75 62 71 75 65 72 69 65 73 20 66 75 72 y subqueries fur
1f82e 74 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 ther down in the
1f82f 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 0a tree. Return .
1f830 2a 2a 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 ** WRC_Abort or
1f831 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f WRC_Continue;.*/
1f832 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f833 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 int sqlite3WalkS
1f834 65 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 electFrom(Walker
1f835 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 *pWalker, Selec
1f836 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 t *p){. SrcList
1f837 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b *pSrc;. int i;
1f838 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 . struct SrcLis
1f839 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a t_item *pItem;..
1f83a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 pSrc = p->pSrc
1f83b 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 ;. if( ALWAYS(p
1f83c 53 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 Src) ){. for(
1f83d 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 i=pSrc->nSrc, pI
1f83e 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 tem=pSrc->a; i>0
1f83f 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i--, pItem++){
1f840 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1f841 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 e3WalkSelect(pWa
1f842 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 lker, pItem->pSe
1f843 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 lect) ){.
1f844 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1f845 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d t;. }. }
1f846 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 . }. return WR
1f847 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a C_Continue;.} ..
1f848 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 /*.** Call sqlit
1f849 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 e3WalkExpr() for
1f84a 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f every expressio
1f84b 6e 20 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74 n in Select stat
1f84c 65 6d 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f ement p..** Invo
1f84d 6b 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 ke sqlite3WalkSe
1f84e 6c 65 63 74 28 29 20 66 6f 72 20 73 75 62 71 75 lect() for subqu
1f84f 65 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f eries in the FRO
1f850 4d 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 M clause and.**
1f851 6f 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 on the compound
1f852 73 65 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d select chain, p-
1f853 3e 70 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 >pPrior..**.** R
1f854 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
1f855 75 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 ue under normal
1f856 63 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 conditions. Ret
1f857 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66 urn WRC_Abort if
1f858 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20 .** there is an
1f859 61 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a abort request..*
1f85a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b *.** If the Walk
1f85b 65 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 er does not have
1f85c 20 61 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 an xSelectCallb
1f85d 61 63 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20 ack() then this
1f85e 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 routine.** is a
1f85f 6e 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 no-op returning
1f860 57 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f WRC_Continue..*/
1f861 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1f862 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 int sqlite3WalkS
1f863 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 elect(Walker *pW
1f864 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 alker, Select *p
1f865 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 ){. int rc;. i
1f866 66 28 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b f( p==0 || pWalk
1f867 65 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 er->xSelectCallb
1f868 61 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ack==0 ) return
1f869 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 WRC_Continue;.
1f86a 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 rc = WRC_Continu
1f86b 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29 e;. while( p )
1f86c 7b 0a 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b {. rc = pWalk
1f86d 65 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 er->xSelectCallb
1f86e 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b ack(pWalker, p);
1f86f 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72 . if( rc ) br
1f870 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c eak;. if( sql
1f871 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 ite3WalkSelectEx
1f872 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 pr(pWalker, p) )
1f873 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1f874 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 t;. if( sqlit
1f875 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d e3WalkSelectFrom
1f876 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 (pWalker, p) ) r
1f877 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1f878 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 . p = p->pPri
1f879 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e or;. }. return
1f87a 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b rc & WRC_Abort;
1f87b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
1f87c 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 *** End of walke
1f87d 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a r.c ************
1f87e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f87f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f880 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1f881 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 *** Begin file r
1f882 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a esolve.c *******
1f883 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f884 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f885 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 **/./*.** 2008 A
1f886 75 67 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 ugust 18.**.** T
1f887 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1f888 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1f889 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1f88a 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1f88b 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1f88c 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1f88d 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1f88e 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1f88f 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1f890 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1f891 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1f892 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1f893 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1f894 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1f895 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1f896 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1f897 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1f898 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f899 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f89a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f89b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f89c 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
1f89d 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1f89e 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 routines used f
1f89f 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 or walking the p
1f8a0 61 72 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a arser tree and.*
1f8a1 2a 20 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 * resolve all id
1f8a2 65 6e 74 69 66 69 65 72 73 20 62 79 20 61 73 73 entifiers by ass
1f8a3 6f 63 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69 ociating them wi
1f8a4 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a th a particular.
1f8a5 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c ** table and col
1f8a6 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 umn..**.** $Id:
1f8a7 72 65 73 6f 6c 76 65 2e 63 2c 76 20 31 2e 33 30 resolve.c,v 1.30
1f8a8 20 32 30 30 39 2f 30 36 2f 31 35 20 32 33 3a 31 2009/06/15 23:1
1f8a9 35 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a 2a 5:59 drh Exp $.*
1f8aa 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 /../*.** Turn th
1f8ab 65 20 70 45 78 70 72 20 65 78 70 72 65 73 73 69 e pExpr expressi
1f8ac 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 on into an alias
1f8ad 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 for the iCol-th
1f8ae 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a column of the.*
1f8af 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e 20 * result set in
1f8b0 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 pEList..**.** If
1f8b1 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1f8b2 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 column is a simp
1f8b3 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 le column refere
1f8b4 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 nce, then this r
1f8b5 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 outine.** makes
1f8b6 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 20 an exact copy.
1f8b7 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 But for any othe
1f8b8 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 73 r kind of expres
1f8b9 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f sion, this.** ro
1f8ba 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 utine make a cop
1f8bb 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 y of the result
1f8bc 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 set column as th
1f8bd 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 e argument to th
1f8be 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 e.** TK_AS opera
1f8bf 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 tor. The TK_AS
1f8c0 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 20 operator causes
1f8c1 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 the expression t
1f8c2 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 o be.** evaluate
1f8c3 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 d just once and
1f8c4 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 20 then reused for
1f8c5 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a each alias..**.*
1f8c6 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 * The reason for
1f8c7 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65 suppressing the
1f8c8 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 6e TK_AS term when
1f8c9 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1f8ca 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 is a simple.** c
1f8cb 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 olumn reference
1f8cc 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 is so that the c
1f8cd 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 olumn reference
1f8ce 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a will be recogniz
1f8cf 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 ed as.** usable
1f8d0 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69 by indices withi
1f8d1 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 n the WHERE clau
1f8d2 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f se processing lo
1f8d3 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b gic. .**.** Hack
1f8d4 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 : The TK_AS ope
1f8d5 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 74 rator is inhibit
1f8d6 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d ed if zType[0]==
1f8d7 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 'G'. This means
1f8d8 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 52 .** that in a GR
1f8d9 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 74 OUP BY clause, t
1f8da 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
1f8db 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 65 evaluated twice
1f8dc 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 . Hence:.**.**
1f8dd 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f SELECT rando
1f8de 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e m()%5 AS x, coun
1f8df 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52 t(*) FROM tab GR
1f8e0 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 OUP BY x.**.** I
1f8e1 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a s equivalent to:
1f8e2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 .**.** SELEC
1f8e3 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 T random()%5 AS
1f8e4 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d x, count(*) FROM
1f8e5 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 61 tab GROUP BY ra
1f8e6 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 ndom()%5.**.** T
1f8e7 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 6e he result of ran
1f8e8 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 47 dom()%5 in the G
1f8e9 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 ROUP BY clause i
1f8ea 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 65 s probably diffe
1f8eb 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 rent.** from the
1f8ec 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 72 result in the r
1f8ed 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d esult-set. We m
1f8ee 69 67 68 74 20 66 69 78 20 74 68 69 73 20 73 6f ight fix this so
1f8ef 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 meday. Or.** th
1f8f0 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 en again, we mig
1f8f1 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 ht not....*/.sta
1f8f2 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 tic void resolve
1f8f3 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 2a Alias(. Parse *
1f8f4 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
1f8f5 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
1f8f6 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 xt */. ExprList
1f8f7 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f *pEList, /
1f8f8 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a * A result set *
1f8f9 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 /. int iCol,
1f8fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 /* A
1f8fb 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 column in the re
1f8fc 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 sult set. 0..pE
1f8fd 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f List->nExpr-1 */
1f8fe 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 . Expr *pExpr,
1f8ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 /* Tra
1f900 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f nsform this into
1f901 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 65 an alias to the
1f902 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
1f903 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 const char *zTy
1f904 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 pe /* "GROU
1f905 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 P" or "ORDER" or
1f906 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 "" */.){. Expr
1f907 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20 *pOrig;
1f908 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 /* The iCol-t
1f909 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 h column of the
1f90a 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
1f90b 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20 Expr *pDup;
1f90c 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
1f90d 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c f pOrig */. sql
1f90e 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
1f90f 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1f910 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
1f911 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f /.. assert( iCo
1f912 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c l>=0 && iCol<pEL
1f913 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 ist->nExpr );.
1f914 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e pOrig = pEList->
1f915 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 a[iCol].pExpr;.
1f916 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d assert( pOrig!=
1f917 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1f918 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 Orig->flags & EP
1f919 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 _Resolved );. d
1f91a 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1f91b 20 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 21 if( pOrig->op!
1f91c 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 =TK_COLUMN && zT
1f91d 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20 ype[0]!='G' ){.
1f91e 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 pDup = sqlite
1f91f 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 3ExprDup(db, pOr
1f920 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 75 70 ig, 0);. pDup
1f921 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 = sqlite3PExpr(
1f922 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 pParse, TK_AS, p
1f923 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 Dup, 0, 0);.
1f924 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 if( pDup==0 ) re
1f925 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 turn;. if( pE
1f926 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 List->a[iCol].iA
1f927 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 lias==0 ){.
1f928 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d pEList->a[iCol]
1f929 2e 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 28 .iAlias = (u16)(
1f92a 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 ++pParse->nAlias
1f92b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 75 );. }. pDu
1f92c 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c 69 p->iTable = pELi
1f92d 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 st->a[iCol].iAli
1f92e 61 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 as;. }else if(
1f92f 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
1f930 70 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 61 6c pOrig, EP_IntVal
1f931 75 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e 75 2e ue) || pOrig->u.
1f932 7a 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 zToken==0 ){.
1f933 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 pDup = sqlite3E
1f934 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 xprDup(db, pOrig
1f935 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44 , 0);. if( pD
1f936 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a up==0 ) return;.
1f937 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 }else{. cha
1f938 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f 72 69 r *zToken = pOri
1f939 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 g->u.zToken;.
1f93a 20 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 6e 21 assert( zToken!
1f93b 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 67 2d =0 );. pOrig-
1f93c 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 >u.zToken = 0;.
1f93d 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 pDup = sqlite
1f93e 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 3ExprDup(db, pOr
1f93f 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f 72 69 ig, 0);. pOri
1f940 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a 54 g->u.zToken = zT
1f941 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 44 oken;. if( pD
1f942 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a up==0 ) return;.
1f943 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44 75 assert( (pDu
1f944 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f 52 p->flags & (EP_R
1f945 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f educed|EP_TokenO
1f946 6e 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 nly))==0 );.
1f947 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d 20 pDup->flags2 |=
1f948 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 EP2_MallocedToke
1f949 6e 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 2e 7a n;. pDup->u.z
1f94a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 44 Token = sqlite3D
1f94b 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54 6f 6b bStrDup(db, zTok
1f94c 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 en);. }. if( p
1f94d 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 Expr->flags & EP
1f94e 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 _ExpCollate ){.
1f94f 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d pDup->pColl =
1f950 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 pExpr->pColl;.
1f951 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c pDup->flags |
1f952 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b = EP_ExpCollate;
1f953 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 . }. sqlite3Ex
1f954 70 72 43 6c 65 61 72 28 64 62 2c 20 70 45 78 70 prClear(db, pExp
1f955 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 78 r);. memcpy(pEx
1f956 70 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66 pr, pDup, sizeof
1f957 28 2a 70 45 78 70 72 29 29 3b 0a 20 20 73 71 6c (*pExpr));. sql
1f958 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1f959 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 Dup);.}../*.** G
1f95a 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 iven the name of
1f95b 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 a column of the
1f95c 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 form X.Y.Z or Y
1f95d 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f .Z or just Z, lo
1f95e 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61 ok up.** that na
1f95f 6d 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66 me in the set of
1f960 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69 source tables i
1f961 6e 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d n pSrcList and m
1f962 61 6b 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a ake the pExpr .*
1f963 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 * expression nod
1f964 65 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20 e refer back to
1f965 74 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75 that source colu
1f966 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 mn. The followi
1f967 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 ng changes.** ar
1f968 65 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a e made to pExpr:
1f969 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d .**.** pExpr-
1f96a 3e 69 44 62 20 20 20 20 20 20 20 20 20 20 20 53 >iDb S
1f96b 65 74 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 et the index in
1f96c 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65 db->aDb[] of the
1f96d 20 64 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 20 database X.**
1f96e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f96f 20 20 20 20 20 20 20 28 65 76 65 6e 20 69 66 20 (even if
1f970 58 20 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a 2a X is implied)..*
1f971 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 * pExpr->iTab
1f972 6c 65 20 20 20 20 20 20 20 20 53 65 74 20 74 6f le Set to
1f973 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 the cursor numb
1f974 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 er for the table
1f975 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 obtained.**
1f976 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f977 20 20 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69 from pSrcLi
1f978 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d st..** pExpr-
1f979 3e 70 54 61 62 20 20 20 20 20 20 20 20 20 20 50 >pTab P
1f97a 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 oints to the Tab
1f97b 6c 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 le structure of
1f97c 58 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a 20 X.Y (even if.**
1f97d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f97e 20 20 20 20 20 20 20 20 58 20 61 6e 64 2f 6f 72 X and/or
1f97f 20 59 20 61 72 65 20 69 6d 70 6c 69 65 64 2e 29 Y are implied.)
1f980 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 43 .** pExpr->iC
1f981 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74 20 olumn Set
1f982 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 to the column nu
1f983 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68 65 20 mber within the
1f984 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78 table..** pEx
1f985 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 pr->op
1f986 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 Set to TK_COLU
1f987 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d MN..** pExpr-
1f988 3e 70 4c 65 66 74 20 20 20 20 20 20 20 20 20 41 >pLeft A
1f989 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 ny expression th
1f98a 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 is points to is
1f98b 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 45 deleted.** pE
1f98c 78 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20 20 xpr->pRight
1f98d 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f Any expressio
1f98e 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f n this points to
1f98f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a is deleted..**.
1f990 2a 2a 20 54 68 65 20 7a 44 62 20 76 61 72 69 61 ** The zDb varia
1f991 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ble is the name
1f992 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1f993 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 73 (the "X"). This
1f994 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a value may be.**
1f995 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68 NULL meaning th
1f996 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68 at name is of th
1f997 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e e form Y.Z or Z.
1f998 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20 Any available
1f999 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 database.** can
1f99a 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a 54 be used. The zT
1f99b 61 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73 able variable is
1f99c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1f99d 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 table (the "Y")
1f99e 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 . This.** value
1f99f 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 can be NULL if
1f9a0 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c zDb is also NULL
1f9a1 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73 20 . If zTable is
1f9a2 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 NULL it.** means
1f9a3 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 6f that the form o
1f9a4 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a 20 f the name is Z
1f9a5 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 and that columns
1f9a6 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 0a from any table.
1f9a7 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a ** can be used..
1f9a8 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 6d **.** If the nam
1f9a9 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 6f e cannot be reso
1f9aa 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 73 lved unambiguous
1f9ab 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 ly, leave an err
1f9ac 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e or message.** in
1f9ad 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 pParse and retu
1f9ae 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20 20 52 rn WRC_Abort. R
1f9af 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 20 eturn WRC_Prune
1f9b0 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 on success..*/.s
1f9b1 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70 tatic int lookup
1f9b2 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 Name(. Parse *p
1f9b3 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 Parse, /*
1f9b4 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 The parsing cont
1f9b5 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ext */. const c
1f9b6 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 2f 2a har *zDb, /*
1f9b7 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
1f9b8 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 abase containing
1f9b9 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20 table, or NULL
1f9ba 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1f9bb 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e 61 6d *zTab, /* Nam
1f9bc 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 61 e of table conta
1f9bd 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72 ining column, or
1f9be 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 NULL */. const
1f9bf 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 char *zCol,
1f9c0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
1f9c1 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65 olumn. */. Name
1f9c2 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 Context *pNC,
1f9c3 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e /* The name con
1f9c4 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73 text used to res
1f9c5 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f olve the name */
1f9c6 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 . Expr *pExpr
1f9c7 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 /* Make
1f9c8 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70 this EXPR node p
1f9c9 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65 oint to the sele
1f9ca 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29 cted column */.)
1f9cb 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 {. int i, j;
1f9cc 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1f9cd 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 counters */. i
1f9ce 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 20 nt cnt = 0;
1f9cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f9d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 /* Number of ma
1f9d1 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 tching column na
1f9d2 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 mes */. int cnt
1f9d3 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Tab = 0;
1f9d4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1f9d5 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 mber of matching
1f9d6 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a table names */.
1f9d7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1f9d8 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 pParse->db;
1f9d9 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1f9da 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
1f9db 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 /. struct SrcLi
1f9dc 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 st_item *pItem;
1f9dd 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f 72 /* Use for
1f9de 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53 looping over pS
1f9df 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a rcList items */.
1f9e0 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
1f9e1 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20 _item *pMatch =
1f9e2 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63 68 0; /* The match
1f9e3 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74 65 ing pSrcList ite
1f9e4 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 m */. NameConte
1f9e5 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43 xt *pTopNC = pNC
1f9e6 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 ; /* Firs
1f9e7 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e t namecontext in
1f9e8 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 53 the list */. S
1f9e9 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d chema *pSchema =
1f9ea 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1f9eb 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 74 68 /* Schema of th
1f9ec 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a e expression */.
1f9ed 20 20 69 6e 74 20 69 73 54 72 69 67 67 65 72 20 int isTrigger
1f9ee 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
1f9ef 70 4e 43 20 29 3b 20 20 20 20 20 2f 2a 20 74 68 pNC ); /* th
1f9f0 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 63 e name context c
1f9f1 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 2a annot be NULL. *
1f9f2 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c /. assert( zCol
1f9f3 20 29 3b 20 20 20 20 2f 2a 20 54 68 65 20 5a 20 ); /* The Z
1f9f4 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20 in X.Y.Z cannot
1f9f5 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 61 73 73 be NULL */. ass
1f9f6 65 72 74 28 20 7e 45 78 70 72 48 61 73 41 6e 79 ert( ~ExprHasAny
1f9f7 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
1f9f8 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f EP_TokenOnly|EP_
1f9f9 52 65 64 75 63 65 64 29 20 29 3b 0a 0a 20 20 2f Reduced) );.. /
1f9fa 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
1f9fb 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63 node to no-matc
1f9fc 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e 69 54 h */. pExpr->iT
1f9fd 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 45 78 able = -1;. pEx
1f9fe 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 pr->pTab = 0;.
1f9ff 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 ExprSetIrreducib
1fa00 6c 65 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f 2a le(pExpr);.. /*
1fa01 20 53 74 61 72 74 20 61 74 20 74 68 65 20 69 6e Start at the in
1fa02 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 74 ner-most context
1fa03 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 72 and move outwar
1fa04 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 20 d until a match
1fa05 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 68 is found */. wh
1fa06 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d ile( pNC && cnt=
1fa07 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 =0 ){. ExprLi
1fa08 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20 st *pEList;.
1fa09 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 SrcList *pSrcLis
1fa0a 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 t = pNC->pSrcLis
1fa0b 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63 t;.. if( pSrc
1fa0c 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f List ){. fo
1fa0d 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72 r(i=0, pItem=pSr
1fa0e 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63 cList->a; i<pSrc
1fa0f 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c List->nSrc; i++,
1fa10 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1fa11 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a Table *pTab;.
1fa12 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b int iDb;
1fa13 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 . Column
1fa14 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20 *pCol;. .
1fa15 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e pTab = pItem->
1fa16 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73 pTab;. as
1fa17 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26 sert( pTab!=0 &&
1fa18 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20 pTab->zName!=0
1fa19 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d );. iDb =
1fa1a 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
1fa1b 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e Index(db, pTab->
1fa1c 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 pSchema);.
1fa1d 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e assert( pTab->
1fa1e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 nCol>0 );.
1fa1f 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 if( zTab ){.
1fa20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 if( pIte
1fa21 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 m->zAlias ){.
1fa22 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a char *z
1fa23 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d TabName = pItem-
1fa24 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 >zAlias;.
1fa25 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1fa26 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 StrICmp(zTabName
1fa27 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e , zTab)!=0 ) con
1fa28 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 tinue;.
1fa29 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1fa2a 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 char *zTabNa
1fa2b 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 me = pTab->zName
1fa2c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
1fa2d 28 20 4e 45 56 45 52 28 7a 54 61 62 4e 61 6d 65 ( NEVER(zTabName
1fa2e 3d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 65 33 53 ==0) || sqlite3S
1fa2f 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c trICmp(zTabName,
1fa30 20 7a 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 20 zTab)!=0 ){.
1fa31 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 conti
1fa32 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nue;.
1fa33 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 }. i
1fa34 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 6c f( zDb!=0 && sql
1fa35 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e ite3StrICmp(db->
1fa36 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 aDb[iDb].zName,
1fa37 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 zDb)!=0 ){.
1fa38 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 continu
1fa39 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d e;. }
1fa3a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1fa3b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1fa3c 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b 29 f( 0==(cntTab++)
1fa3d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 45 ){. pE
1fa3e 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 xpr->iTable = pI
1fa3f 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 tem->iCursor;.
1fa40 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1fa41 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 Tab = pTab;.
1fa42 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 pSchema =
1fa43 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 pTab->pSchema;.
1fa44 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 pMatch
1fa45 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 = pItem;.
1fa46 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a }. for(j
1fa47 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 =0, pCol=pTab->a
1fa48 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f Col; j<pTab->nCo
1fa49 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b l; j++, pCol++){
1fa4a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 . if( s
1fa4b 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 qlite3StrICmp(pC
1fa4c 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 ol->zName, zCol)
1fa4d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1fa4e 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e IdList *pUsin
1fa4f 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 g;. c
1fa50 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 nt++;.
1fa51 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1fa52 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 = pItem->iCursor
1fa53 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 ;. pE
1fa54 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 xpr->pTab = pTab
1fa55 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d ;. pM
1fa56 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 atch = pItem;.
1fa57 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d pSchem
1fa58 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d a = pTab->pSchem
1fa59 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f a;. /
1fa5a 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68 65 * Substitute the
1fa5b 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 2d rowid (column -
1fa5c 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 47 1) for the INTEG
1fa5d 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a ER PRIMARY KEY *
1fa5e 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 /. pE
1fa5f 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a xpr->iColumn = j
1fa60 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 ==pTab->iPKey ?
1fa61 2d 31 20 3a 20 28 69 31 36 29 6a 3b 0a 20 20 20 -1 : (i16)j;.
1fa62 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3c 70 if( i<p
1fa63 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 SrcList->nSrc-1
1fa64 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1fa65 20 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f if( pItem[1].jo
1fa66 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 intype & JT_NATU
1fa67 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 RAL ){.
1fa68 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 /* If thi
1fa69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 72 65 64 s match occurred
1fa6a 20 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61 62 in the left tab
1fa6b 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 le of a natural
1fa6c 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 join,.
1fa6d 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b ** then sk
1fa6e 69 70 20 74 68 65 20 72 69 67 68 74 20 74 61 62 ip the right tab
1fa6f 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 le to avoid a du
1fa70 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 2a 2f plicate match */
1fa71 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fa72 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 pItem++;.
1fa73 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 i++;.
1fa74 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c }el
1fa75 73 65 20 69 66 28 20 28 70 55 73 69 6e 67 20 3d se if( (pUsing =
1fa76 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 pItem[1].pUsing
1fa77 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
1fa78 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
1fa79 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 73 20 is match occurs
1fa7a 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 on a column that
1fa7b 20 69 73 20 69 6e 20 74 68 65 20 55 53 49 4e 47 is in the USING
1fa7c 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 clause.
1fa7d 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 ** of a
1fa7e 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 join, skip the s
1fa7f 65 61 72 63 68 20 6f 66 20 74 68 65 20 72 69 67 earch of the rig
1fa80 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 ht table of the
1fa81 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20 join.
1fa82 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 ** to avoid
1fa83 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74 a duplicate mat
1fa84 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 ch there. */.
1fa85 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
1fa86 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 k;.
1fa87 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 for(k=0; k<p
1fa88 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 Using->nId; k++)
1fa89 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1fa8a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1fa8b 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 trICmp(pUsing->a
1fa8c 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 [k].zName, zCol)
1fa8d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1fa8e 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d pItem
1fa8f 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ++;.
1fa90 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 i++;.
1fa91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa92 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1fa93 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1fa94 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1fa95 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1fa96 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1fa97 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1fa98 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fa99 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1fa9a 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }..#ifndef SQLI
1fa9b 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
1fa9c 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 /* If we hav
1fa9d 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 65 e not already re
1fa9e 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c solved the name,
1fa9f 20 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 then maybe .
1faa0 20 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77 2e ** it is a new.
1faa1 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 * or old.* trigg
1faa2 65 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66 65 er argument refe
1faa3 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 rence. */.
1faa4 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a if( zDb==0 && z
1faa5 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 Tab!=0 && cnt==0
1faa6 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69 && pParse->pTri
1faa7 67 67 65 72 54 61 62 21 3d 30 20 29 7b 0a 20 20 ggerTab!=0 ){.
1faa8 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 50 61 int op = pPa
1faa9 72 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 3b rse->eTriggerOp;
1faaa 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 . Table *pT
1faab 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 ab = 0;. as
1faac 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 44 45 4c sert( op==TK_DEL
1faad 45 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 ETE || op==TK_UP
1faae 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 DATE || op==TK_I
1faaf 4e 53 45 52 54 20 29 3b 0a 20 20 20 20 20 20 69 NSERT );. i
1fab0 66 28 20 6f 70 21 3d 54 4b 5f 44 45 4c 45 54 45 f( op!=TK_DELETE
1fab1 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1fab2 6d 70 28 22 6e 65 77 22 2c 7a 54 61 62 29 20 3d mp("new",zTab) =
1fab3 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 = 0 ){. p
1fab4 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 31 Expr->iTable = 1
1fab5 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d ;. pTab =
1fab6 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 pParse->pTrigge
1fab7 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 65 6c 73 rTab;. }els
1fab8 65 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 53 e if( op!=TK_INS
1fab9 45 52 54 20 26 26 20 73 71 6c 69 74 65 33 53 74 ERT && sqlite3St
1faba 72 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 54 61 62 rICmp("old",zTab
1fabb 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1fabc 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
1fabd 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 0;. pTab
1fabe 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 = pParse->pTrigg
1fabf 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a 0a erTab;. }..
1fac0 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 if( pTab )
1fac1 7b 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 { . int i
1fac2 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53 63 Col;. pSc
1fac3 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 hema = pTab->pSc
1fac4 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e hema;. cn
1fac5 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 tTab++;.
1fac6 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 if( sqlite3IsRow
1fac7 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 id(zCol) ){.
1fac8 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b iCol = -1;
1fac9 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1faca 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 43 for(iC
1facb 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d ol=0; iCol<pTab-
1facc 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a >nCol; iCol++){.
1facd 20 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c 75 Colu
1face 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62 mn *pCol = &pTab
1facf 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 ->aCol[iCol];.
1fad0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
1fad1 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f lite3StrICmp(pCo
1fad2 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d l->zName, zCol)=
1fad3 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1fad4 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 if( iCol==pT
1fad5 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 ab->iPKey ){.
1fad6 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f iCo
1fad7 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 l = -1;.
1fad8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fad9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1fada 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fadb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1fadc 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f . if( iCo
1fadd 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a l<pTab->nCol ){.
1fade 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b cnt++;
1fadf 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 . if( i
1fae0 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 Col<0 ){.
1fae1 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 pExpr->affi
1fae2 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 nity = SQLITE_AF
1fae3 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 F_INTEGER;.
1fae4 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
1fae5 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 Expr->iTable==0
1fae6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 ){. t
1fae7 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 estcase( iCol==3
1fae8 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 1 );.
1fae9 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d testcase( iCol=
1faea 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 20 =32 );.
1faeb 20 20 20 70 50 61 72 73 65 2d 3e 6f 6c 64 6d 61 pParse->oldma
1faec 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d 33 32 20 sk |= (iCol>=32
1faed 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 28 ? 0xffffffff : (
1faee 28 28 75 33 32 29 31 29 3c 3c 69 43 6f 6c 29 29 ((u32)1)<<iCol))
1faef 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1faf0 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1faf1 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 69 43 Column = (i16)iC
1faf2 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45 ol;. pE
1faf3 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 xpr->pTab = pTab
1faf4 3b 0a 20 20 20 20 20 20 20 20 20 20 69 73 54 72 ;. isTr
1faf5 69 67 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 20 igger = 1;.
1faf6 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1faf7 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 }.#endif /* !de
1faf8 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1faf9 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20 T_TRIGGER) */..
1fafa 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72 /*. ** Per
1fafb 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73 haps the name is
1fafc 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
1fafd 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f the ROWID. */
1fafe 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 . if( cnt==0
1faff 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20 && cntTab==1 &&
1fb00 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a sqlite3IsRowid(z
1fb01 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e Col) ){. cn
1fb02 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78 t = 1;. pEx
1fb03 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 pr->iColumn = -1
1fb04 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 ;. pExpr->a
1fb05 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 ffinity = SQLITE
1fb06 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 _AFF_INTEGER;.
1fb07 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 }.. /*.
1fb08 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 ** If the input
1fb09 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a is of the form Z
1fb0a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59 (not Y.Z or X.Y
1fb0b 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d .Z) then the nam
1fb0c 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 e Z. ** might
1fb0d 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73 refer to an res
1fb0e 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 ult-set alias.
1fb0f 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f This happens, fo
1fb10 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a r example, when.
1fb11 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 ** we are re
1fb12 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e solving names in
1fb13 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
1fb14 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
1fb15 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20 ng command:.
1fb16 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45 **. ** SE
1fb17 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52 LECT a+b AS x FR
1fb18 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78 OM table WHERE x
1fb19 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 <10;. **.
1fb1a 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65 ** In cases like
1fb1b 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70 this, replace p
1fb1c 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 79 Expr with a copy
1fb1d 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
1fb1e 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66 on that. ** f
1fb1f 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20 orms the result
1fb20 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22 set entry ("a+b"
1fb21 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29 in the example)
1fb22 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 and return imme
1fb23 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 diately.. **
1fb24 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 78 Note that the ex
1fb25 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 pression in the
1fb26 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c result set shoul
1fb27 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 d have already b
1fb28 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c een. ** resol
1fb29 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 20 ved by the time
1fb2a 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
1fb2b 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 is resolved..
1fb2c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 */. if( cnt
1fb2d 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d ==0 && (pEList =
1fb2e 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30 pNC->pEList)!=0
1fb2f 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20 && zTab==0 ){.
1fb30 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c for(j=0; j<
1fb31 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a pEList->nExpr; j
1fb32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 ++){. cha
1fb33 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d r *zAs = pEList-
1fb34 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 >a[j].zName;.
1fb35 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 if( zAs!=0
1fb36 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
1fb37 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 p(zAs, zCol)==0
1fb38 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 ){. Exp
1fb39 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 r *pOrig;.
1fb3a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
1fb3b 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 r->pLeft==0 && p
1fb3c 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 Expr->pRight==0
1fb3d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 );. ass
1fb3e 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c ert( pExpr->x.pL
1fb3f 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ist==0 );.
1fb40 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
1fb41 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 r->x.pSelect==0
1fb42 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 );. pOr
1fb43 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a ig = pEList->a[j
1fb44 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 ].pExpr;.
1fb45 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c if( !pNC->all
1fb46 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73 owAgg && ExprHas
1fb47 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 Property(pOrig,
1fb48 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 EP_Agg) ){.
1fb49 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
1fb4a 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1fb4b 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 misuse of aliase
1fb4c 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c d aggregate %s",
1fb4d 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 zAs);.
1fb4e 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1fb4f 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ort;. }
1fb50 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c . resol
1fb51 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 veAlias(pParse,
1fb52 70 45 4c 69 73 74 2c 20 6a 2c 20 70 45 78 70 72 pEList, j, pExpr
1fb53 2c 20 22 22 29 3b 0a 20 20 20 20 20 20 20 20 20 , "");.
1fb54 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 cnt = 1;.
1fb55 20 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a pMatch = 0;.
1fb56 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1fb57 28 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 62 ( zTab==0 && zDb
1fb58 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ==0 );.
1fb59 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 goto lookupname
1fb5a 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a _end;. }.
1fb5b 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a } . }..
1fb5c 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 /* Advance t
1fb5d 6f 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 o the next name
1fb5e 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f context. The lo
1fb5f 6f 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 op will exit whe
1fb60 6e 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 n either. **
1fb61 77 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 we have a match
1fb62 28 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 (cnt>0) or when
1fb63 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 we run out of na
1fb64 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 me contexts..
1fb65 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d */. if( cnt=
1fb66 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 =0 ){. pNC
1fb67 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 = pNC->pNext;.
1fb68 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 }. }.. /*.
1fb69 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 72 ** If X and Y ar
1fb6a 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 e NULL (in other
1fb6b 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 words if only t
1fb6c 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a he column name Z
1fb6d 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 is. ** supplie
1fb6e 64 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 d) and the value
1fb6f 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 of Z is enclose
1fb70 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 d in double-quot
1fb71 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 es, then. ** Z
1fb72 69 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 is a string lite
1fb73 72 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27 ral if it doesn'
1fb74 74 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 t match any colu
1fb75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 mn names. In th
1fb76 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 at. ** case, we
1fb77 20 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 need to return
1fb78 72 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e right away and n
1fb79 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e ot make any chan
1fb7a 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 ges to. ** pExp
1fb7b 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 r.. **. ** Bec
1fb7c 61 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 ause no referenc
1fb7d 65 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 e was made to ou
1fb7e 74 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 ter contexts, th
1fb7f 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a e pNC->nRef. **
1fb80 20 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 fields are not
1fb81 63 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 changed in any c
1fb82 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 ontext.. */. i
1fb83 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 f( cnt==0 && zTa
1fb84 62 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73 50 b==0 && ExprHasP
1fb85 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 roperty(pExpr,EP
1fb86 5f 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0a 20 _DblQuoted) ){.
1fb87 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
1fb88 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 K_STRING;. pE
1fb89 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 xpr->pTab = 0;.
1fb8a 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
1fb8b 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 une;. }.. /*.
1fb8c 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 ** cnt==0 means
1fb8d 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 6d there was not m
1fb8e 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 61 atch. cnt>1 mea
1fb8f 6e 73 20 74 68 65 72 65 20 77 65 72 65 20 74 77 ns there were tw
1fb90 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d o or. ** more m
1fb91 61 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 20 atches. Either
1fb92 77 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e 20 way, we have an
1fb93 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 error.. */. if
1fb94 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 20 ( cnt!=1 ){.
1fb95 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 const char *zErr
1fb96 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e 74 ;. zErr = cnt
1fb97 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 63 ==0 ? "no such c
1fb98 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67 75 olumn" : "ambigu
1fb99 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22 ous column name"
1fb9a 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29 7b ;. if( zDb ){
1fb9b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
1fb9c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1fb9d 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20 7a %s: %s.%s.%s", z
1fb9e 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c 20 Err, zDb, zTab,
1fb9f 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 zCol);. }else
1fba0 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20 if( zTab ){.
1fba1 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1fba2 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 sg(pParse, "%s:
1fba3 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 54 %s.%s", zErr, zT
1fba4 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d ab, zCol);. }
1fba5 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
1fba6 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1fba7 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 45 se, "%s: %s", zE
1fba8 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d rr, zCol);. }
1fba9 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 . pTopNC->nEr
1fbaa 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 r++;. }.. /* I
1fbab 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 f a column from
1fbac 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 4c a table in pSrcL
1fbad 69 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 65 ist is reference
1fbae 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a 20 d, then record.
1fbaf 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 6e ** this fact in
1fbb0 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 5b the pSrcList.a[
1fbb1 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 73 ].colUsed bitmas
1fbb2 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 75 k. Column 0 cau
1fbb3 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 74 ses. ** bit 0 t
1fbb4 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 6d o be set. Colum
1fbb5 6e 20 31 20 73 65 74 73 20 62 69 74 20 31 2e 20 n 1 sets bit 1.
1fbb6 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 And so forth.
1fbb7 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 If the. ** colu
1fbb8 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 mn number is gre
1fbb9 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e 75 ater than the nu
1fbba 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 mber of bits in
1fbbb 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a the bitmask. **
1fbbc 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 69 then set the hi
1fbbd 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 20 gh-order bit of
1fbbe 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 2a the bitmask.. *
1fbbf 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 /. if( pExpr->i
1fbc0 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d 61 Column>=0 && pMa
1fbc1 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e tch!=0 ){. in
1fbc2 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f t n = pExpr->iCo
1fbc3 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 61 lumn;. testca
1fbc4 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a se( n==BMS-1 );.
1fbc5 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20 29 if( n>=BMS )
1fbc6 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 2d {. n = BMS-
1fbc7 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 1;. }. ass
1fbc8 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 ert( pMatch->iCu
1fbc9 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 rsor==pExpr->iTa
1fbca 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 ble );. pMatc
1fbcb 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 h->colUsed |= ((
1fbcc 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 Bitmask)1)<<n;.
1fbcd 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 }.. /* Clean u
1fbce 70 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a p and return. *
1fbcf 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 /. sqlite3ExprD
1fbd0 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d elete(db, pExpr-
1fbd1 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 72 >pLeft);. pExpr
1fbd2 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73 ->pLeft = 0;. s
1fbd3 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1fbd4 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 (db, pExpr->pRig
1fbd5 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 ht);. pExpr->pR
1fbd6 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 ight = 0;. pExp
1fbd7 72 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67 67 r->op = (isTrigg
1fbd8 65 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52 20 er ? TK_TRIGGER
1fbd9 3a 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c 6f : TK_COLUMN);.lo
1fbda 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 okupname_end:.
1fbdb 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 if( cnt==1 ){.
1fbdc 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 assert( pNC!=0
1fbdd 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 );. sqlite3A
1fbde 75 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 uthRead(pParse,
1fbdf 70 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 pExpr, pSchema,
1fbe0 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a pNC->pSrcList);.
1fbe1 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 /* Increment
1fbe2 20 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 the nRef value
1fbe3 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 on all name cont
1fbe4 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 exts from TopNC
1fbe5 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 up to. ** the
1fbe6 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 point where the
1fbe7 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a name matched. *
1fbe8 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 /. for(;;){.
1fbe9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f assert( pTo
1fbea 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 pNC!=0 );.
1fbeb 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a pTopNC->nRef++;.
1fbec 20 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 if( pTopNC
1fbed 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 ==pNC ) break;.
1fbee 20 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 pTopNC = pT
1fbef 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 opNC->pNext;.
1fbf0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 }. return WR
1fbf1 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c 73 C_Prune;. } els
1fbf2 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 e {. return W
1fbf3 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d 0a RC_Abort;. }.}.
1fbf4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
1fbf5 69 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 ine is callback
1fbf6 66 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 for sqlite3WalkE
1fbf7 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 xpr()..**.** Res
1fbf8 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 olve symbolic na
1fbf9 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 mes into TK_COLU
1fbfa 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 MN operators for
1fbfb 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
1fbfc 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 node in the expr
1fbfd 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 ession tree. Re
1fbfe 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e turn 0 to contin
1fbff 75 65 20 74 68 65 20 73 65 61 72 63 68 20 64 6f ue the search do
1fc00 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f wn.** the tree o
1fc01 72 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 r 2 to abort the
1fc02 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a tree walk..**.*
1fc03 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 * This routine a
1fc04 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 lso does error c
1fc05 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 hecking and name
1fc06 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a resolution for.
1fc07 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 ** function name
1fc08 73 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 s. The operator
1fc09 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 for aggregate f
1fc0a 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e unctions is chan
1fc0b 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 ged.** to TK_AGG
1fc0c 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 _FUNCTION..*/.st
1fc0d 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 atic int resolve
1fc0e 45 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 ExprStep(Walker
1fc0f 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a *pWalker, Expr *
1fc10 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f pExpr){. NameCo
1fc11 6e 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 ntext *pNC;. Pa
1fc12 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 rse *pParse;..
1fc13 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 pNC = pWalker->u
1fc14 2e 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 .pNC;. assert(
1fc15 70 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 pNC!=0 );. pPar
1fc16 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
1fc17 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 ;. assert( pPar
1fc18 73 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 se==pWalker->pPa
1fc19 72 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 rse );.. if( Ex
1fc1a 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
1fc1b 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c (pExpr, EP_Resol
1fc1c 76 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 ved) ) return WR
1fc1d 43 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 C_Prune;. ExprS
1fc1e 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 etProperty(pExpr
1fc1f 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a , EP_Resolved);.
1fc20 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
1fc21 20 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 if( pNC->pSrcLi
1fc22 73 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c st && pNC->pSrcL
1fc23 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b ist->nAlloc>0 ){
1fc24 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 . SrcList *pS
1fc25 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 rcList = pNC->pS
1fc26 72 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 rcList;. int
1fc27 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 i;. for(i=0;
1fc28 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d i<pNC->pSrcList-
1fc29 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nSrc; i++){.
1fc2a 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c assert( pSrcL
1fc2b 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f ist->a[i].iCurso
1fc2c 72 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 r>=0 && pSrcList
1fc2d 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 ->a[i].iCursor<p
1fc2e 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 Parse->nTab);.
1fc2f 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
1fc30 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e switch( pExpr->
1fc31 6f 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e op ){..#if defin
1fc32 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
1fc33 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c _UPDATE_DELETE_L
1fc34 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 IMIT) && !define
1fc35 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 d(SQLITE_OMIT_SU
1fc36 42 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 BQUERY). /* T
1fc37 68 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 he special opera
1fc38 74 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 tor TK_ROW means
1fc39 20 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 use the rowid f
1fc3a 6f 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 or the first.
1fc3b 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 ** column in th
1fc3c 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 e FROM clause.
1fc3d 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
1fc3e 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 the LIMIT and OR
1fc3f 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c DER BY. ** cl
1fc40 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 ause processing
1fc41 6f 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 on UPDATE and DE
1fc42 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e LETE statements.
1fc43 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 . */. case
1fc44 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 TK_ROW: {.
1fc45 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 SrcList *pSrcLi
1fc46 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 st = pNC->pSrcLi
1fc47 73 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 st;. struct
1fc48 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
1fc49 49 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 Item;. asse
1fc4a 72 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 rt( pSrcList &&
1fc4b 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d pSrcList->nSrc==
1fc4c 31 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 1 );. pItem
1fc4d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 = pSrcList->a;
1fc4e 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 . pExpr->op
1fc4f 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 = TK_COLUMN;.
1fc50 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 pExpr->pTab
1fc51 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 = pItem->pTab;.
1fc52 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1fc53 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 le = pItem->iCur
1fc54 73 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 sor;. pExpr
1fc55 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a ->iColumn = -1;.
1fc56 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 pExpr->aff
1fc57 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 inity = SQLITE_A
1fc58 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 FF_INTEGER;.
1fc59 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1fc5a 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 endif /* defined
1fc5b 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 (SQLITE_ENABLE_U
1fc5c 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d PDATE_DELETE_LIM
1fc5d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 IT) && !defined(
1fc5e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1fc5f 55 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a UERY) */.. /*
1fc60 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 A lone identifi
1fc61 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f er is the name o
1fc62 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 f a column..
1fc63 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 */. case TK_I
1fc64 44 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 D: {. retur
1fc65 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 n lookupName(pPa
1fc66 72 73 65 2c 20 30 2c 20 30 2c 20 70 45 78 70 72 rse, 0, 0, pExpr
1fc67 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43 2c ->u.zToken, pNC,
1fc68 20 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 pExpr);. }.
1fc69 20 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 . /* A table
1fc6a 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e name and column
1fc6b 20 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 name: ID.ID
1fc6c 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 . ** Or a dat
1fc6d 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 abase, table and
1fc6e 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e column: ID.ID.
1fc6f 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 ID. */. ca
1fc70 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 se TK_DOT: {.
1fc71 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1fc72 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63 6f Column;. co
1fc73 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
1fc74 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 ;. const ch
1fc75 61 72 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20 45 ar *zDb;. E
1fc76 78 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 xpr *pRight;..
1fc77 20 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c /* if( pSrcL
1fc78 69 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 ist==0 ) break;
1fc79 2a 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 */. pRight
1fc7a 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b = pExpr->pRight;
1fc7b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 . if( pRigh
1fc7c 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a t->op==TK_ID ){.
1fc7d 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 30 3b zDb = 0;
1fc7e 0a 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20 . zTable
1fc7f 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e = pExpr->pLeft->
1fc80 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 u.zToken;.
1fc81 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 zColumn = pRig
1fc82 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 ht->u.zToken;.
1fc83 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1fc84 20 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68 assert( pRigh
1fc85 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b t->op==TK_DOT );
1fc86 0a 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 70 . zDb = p
1fc87 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a Expr->pLeft->u.z
1fc88 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a Token;. z
1fc89 54 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d 3e Table = pRight->
1fc8a 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b pLeft->u.zToken;
1fc8b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e . zColumn
1fc8c 20 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67 68 = pRight->pRigh
1fc8d 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 t->u.zToken;.
1fc8e 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
1fc8f 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 n lookupName(pPa
1fc90 72 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c 65 rse, zDb, zTable
1fc91 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 , zColumn, pNC,
1fc92 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 pExpr);. }..
1fc93 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 /* Resolve fu
1fc94 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 nction names.
1fc95 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
1fc96 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 CONST_FUNC:.
1fc97 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e case TK_FUNCTION
1fc98 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 : {. ExprLi
1fc99 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 st *pList = pExp
1fc9a 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f r->x.pList; /
1fc9b 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c * The argument l
1fc9c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ist */. int
1fc9d 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 n = pList ? pLi
1fc9e 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20 st->nExpr : 0;
1fc9f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
1fca0 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 rguments */.
1fca1 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 int no_such_fu
1fca2 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a nc = 0; /*
1fca3 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 True if no such
1fca4 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 function exists
1fca5 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 */. int wr
1fca6 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 ong_num_args = 0
1fca7 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 ; /* True if
1fca8 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 wrong number of
1fca9 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 arguments */.
1fcaa 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d int is_agg =
1fcab 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1fcac 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e /* True if is an
1fcad 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
1fcae 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ion */. int
1fcaf 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 auth;
1fcb0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 /* Auth
1fcb1 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 orization to use
1fcb2 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f the function */
1fcb3 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 . int nId;
1fcb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcb5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1fcb6 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 characters in fu
1fcb7 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 nction name */.
1fcb8 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1fcb9 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 *zId;
1fcba 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e /* The function
1fcbb 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 name. */.
1fcbc 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 FuncDef *pDef;
1fcbd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1fcbe 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
1fcbf 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f the function */
1fcc0 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 . u8 enc =
1fcc1 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b ENC(pParse->db);
1fcc2 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
1fcc3 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a se encoding */..
1fcc4 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1fcc5 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f pExpr->op==TK_CO
1fcc6 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 NST_FUNC );.
1fcc7 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
1fcc8 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
1fcc9 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
1fcca 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 70 );. zId = p
1fccb 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a Expr->u.zToken;.
1fccc 20 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c 69 nId = sqli
1fccd 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 29 te3Strlen30(zId)
1fcce 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 ;. pDef = s
1fccf 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 qlite3FindFuncti
1fcd0 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a on(pParse->db, z
1fcd1 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c Id, nId, n, enc,
1fcd2 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 0);. if( p
1fcd3 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Def==0 ){.
1fcd4 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 pDef = sqlite3
1fcd5 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 FindFunction(pPa
1fcd6 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 rse->db, zId, nI
1fcd7 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a d, -1, enc, 0);.
1fcd8 20 20 20 20 20 20 20 20 69 66 28 20 70 44 65 66 if( pDef
1fcd9 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1fcda 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 no_such_func =
1fcdb 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 1;. }else
1fcdc 7b 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e {. wron
1fcdd 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a g_num_args = 1;.
1fcde 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1fcdf 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 }else{. i
1fce0 73 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 s_agg = pDef->xF
1fce1 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a unc==0;. }.
1fce2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1fce3 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
1fce4 4e 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 N. if( pDef
1fce5 20 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 ){. auth
1fce6 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 = sqlite3AuthCh
1fce7 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 eck(pParse, SQLI
1fce8 54 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 TE_FUNCTION, 0,
1fce9 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b pDef->zName, 0);
1fcea 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 . if( aut
1fceb 68 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a h!=SQLITE_OK ){.
1fcec 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 75 if( au
1fced 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 th==SQLITE_DENY
1fcee 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
1fcef 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1fcf0 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 Parse, "not auth
1fcf1 6f 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 orized to use fu
1fcf2 6e 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 nction: %s",.
1fcf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcf5 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pDef->zName);.
1fcf6 20 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e pNC->
1fcf7 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nErr++;.
1fcf8 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 }. pE
1fcf9 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c xpr->op = TK_NUL
1fcfa 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 L;. ret
1fcfb 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
1fcfc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1fcfd 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 .#endif. if
1fcfe 28 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43 ( is_agg && !pNC
1fcff 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20 ->allowAgg ){.
1fd00 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1fd01 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d orMsg(pParse, "m
1fd02 69 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 isuse of aggrega
1fd03 74 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 te function %.*s
1fd04 28 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 ()", nId,zId);.
1fd05 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 pNC->nErr
1fd06 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 ++;. is_a
1fd07 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 gg = 0;. }e
1fd08 6c 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f lse if( no_such_
1fd09 66 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 func ){.
1fd0a 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1fd0b 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 pParse, "no such
1fd0c 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 function: %.*s"
1fd0d 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 , nId, zId);.
1fd0e 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b pNC->nErr++
1fd0f 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
1fd10 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 ( wrong_num_args
1fd11 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1fd12 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1fd13 73 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 se,"wrong number
1fd14 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f of arguments to
1fd15 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 function %.*s()
1fd16 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
1fd17 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 nId, zId);.
1fd18 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a pNC->nErr++;.
1fd19 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1fd1a 28 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20 ( is_agg ){.
1fd1b 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 pExpr->op =
1fd1c 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b TK_AGG_FUNCTION;
1fd1d 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 . pNC->ha
1fd1e 73 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 sAgg = 1;.
1fd1f 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 }. if( is_a
1fd20 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 gg ) pNC->allowA
1fd21 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 gg = 0;. sq
1fd22 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1fd23 74 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74 t(pWalker, pList
1fd24 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f );. if( is_
1fd25 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 agg ) pNC->allow
1fd26 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f Agg = 1;. /
1fd27 2a 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 * FIX ME: Compu
1fd28 74 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 te pExpr->affini
1fd29 74 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 ty based on the
1fd2a 65 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a expected return.
1fd2b 20 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 ** type of
1fd2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 the function .
1fd2d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 */. re
1fd2e 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a turn WRC_Prune;.
1fd2f 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1fd30 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1fd31 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 RY. case TK_S
1fd32 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 ELECT:. case
1fd33 54 4b 5f 45 58 49 53 54 53 3a 20 20 74 65 73 74 TK_EXISTS: test
1fd34 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
1fd35 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 65 =TK_EXISTS );.#e
1fd36 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b ndif. case TK
1fd37 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 _IN: {. tes
1fd38 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 tcase( pExpr->op
1fd39 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20 20 ==TK_IN );.
1fd3a 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
1fd3b 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 erty(pExpr, EP_x
1fd3c 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 IsSelect) ){.
1fd3d 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 int nRef =
1fd3e 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 pNC->nRef;.#ifnd
1fd3f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
1fd40 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 28 HECK. if(
1fd41 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b pNC->isCheck ){
1fd42 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1fd43 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1fd44 65 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 72 e,"subqueries pr
1fd45 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 ohibited in CHEC
1fd46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b K constraints");
1fd47 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 . }.#endi
1fd48 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 f. sqlite
1fd49 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 3WalkSelect(pWal
1fd4a 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 ker, pExpr->x.pS
1fd4b 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 elect);.
1fd4c 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 assert( pNC->nRe
1fd4d 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 f>=nRef );.
1fd4e 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 if( nRef!=pNC
1fd4f 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 ->nRef ){.
1fd50 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 ExprSetPrope
1fd51 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 rty(pExpr, EP_Va
1fd52 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 rSelect);.
1fd53 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1fd54 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1fd55 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1fd56 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 73 IT_CHECK. cas
1fd57 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b e TK_VARIABLE: {
1fd58 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e . if( pNC->
1fd59 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 isCheck ){.
1fd5a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1fd5b 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 6d sg(pParse,"param
1fd5c 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 64 eters prohibited
1fd5d 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 in CHECK constr
1fd5e 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d aints");. }
1fd5f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1fd60 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
1fd61 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d return (pParse-
1fd62 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d >nErr || pParse-
1fd63 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1fd64 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a d) ? WRC_Abort :
1fd65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
1fd66 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 ../*.** pEList i
1fd67 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 72 s a list of expr
1fd68 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72 essions which ar
1fd69 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 73 e really the res
1fd6a 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a 2a ult set of the.*
1fd6b 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 * a SELECT state
1fd6c 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 74 ment. pE is a t
1fd6d 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 erm in an ORDER
1fd6e 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 BY or GROUP BY c
1fd6f 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 lause..** This r
1fd70 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 6f outine checks to
1fd71 20 73 65 65 20 69 66 20 70 45 20 69 73 20 61 20 see if pE is a
1fd72 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 simple identifie
1fd73 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f r which correspo
1fd74 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 nds.** to the AS
1fd75 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 -name of one of
1fd76 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
1fd77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
1fd78 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 . If it is,.**
1fd79 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
1fd7a 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62 urn an integer b
1fd7b 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 etween 1 and N w
1fd7c 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 here N is the nu
1fd7d 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 mber of.** eleme
1fd7e 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 nts in pEList, c
1fd7f 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
1fd80 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 the matching ent
1fd81 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 ry. If there is
1fd82 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 .** no match, or
1fd83 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 20 if pE is not a
1fd84 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 simple identifie
1fd85 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 r, then this rou
1fd86 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 tine.** return 0
1fd87 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 ..**.** pEList h
1fd88 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 as been resolved
1fd89 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a . pE has not..*
1fd8a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
1fd8b 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 olveAsName(. Pa
1fd8c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1fd8d 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
1fd8e 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 ext for error me
1fd8f 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 ssages */. Expr
1fd90 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f List *pEList, /
1fd91 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 73 * List of expres
1fd92 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f sions to scan */
1fd93 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20 . Expr *pE
1fd94 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 /* Express
1fd95 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 6e ion we are tryin
1fd96 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b g to match */.){
1fd97 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
1fd98 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
1fd99 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 55 4e 55 53 unter */.. UNUS
1fd9a 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61 ED_PARAMETER(pPa
1fd9b 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 45 2d rse);.. if( pE-
1fd9c 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 >op==TK_ID ){.
1fd9d 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 char *zCol = p
1fd9e 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 E->u.zToken;.
1fd9f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 for(i=0; i<pELi
1fda0 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b st->nExpr; i++){
1fda1 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 . char *zAs
1fda2 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e = pEList->a[i].
1fda3 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 zName;. if(
1fda4 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 zAs!=0 && sqlit
1fda5 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a e3StrICmp(zAs, z
1fda6 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Col)==0 ){.
1fda7 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 return i+1;.
1fda8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1fda9 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
1fdaa 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 6f /*.** pE is a po
1fdab 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 inter to an expr
1fdac 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 20 ession which is
1fdad 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e a single term in
1fdae 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 the.** ORDER BY
1fdaf 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 of a compound S
1fdb0 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 72 ELECT. The expr
1fdb1 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 ession has not b
1fdb2 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f een.** name reso
1fdb3 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 lved..**.** At t
1fdb4 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f he point this ro
1fdb5 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
1fdb6 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 we already know
1fdb7 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 that the.** ORD
1fdb8 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e 6f ER BY term is no
1fdb9 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 t an integer ind
1fdba 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 75 ex into the resu
1fdbb 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a lt set. That.**
1fdbc 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 case is handled
1fdbd 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 by the calling
1fdbe 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 routine..**.** A
1fdbf 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 ttempt to match
1fdc0 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c pE against resul
1fdc1 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e t set columns in
1fdc2 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a the left-most.*
1fdc3 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 * SELECT stateme
1fdc4 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 nt. Return the
1fdc5 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 6d index i of the m
1fdc6 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a atching column,.
1fdc7 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 74 ** as an indicat
1fdc8 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ion to the calle
1fdc9 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 r that it should
1fdca 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74 sort by the i-t
1fdcb 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 h column..** The
1fdcc 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d left-most colum
1fdcd 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 n is 1. In othe
1fdce 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 6c r words, the val
1fdcf 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 ue returned is t
1fdd0 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 he.** same integ
1fdd1 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f er value that wo
1fdd2 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 uld be used in t
1fdd3 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
1fdd4 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 to indicate.**
1fdd5 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a the column..**.*
1fdd6 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f * If there is no
1fdd7 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 match, return 0
1fdd8 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20 . Return -1 if
1fdd9 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e an error occurs.
1fdda 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
1fddb 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 esolveOrderByTer
1fddc 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50 mToExprList(. P
1fddd 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
1fdde 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
1fddf 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d text for error m
1fde0 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c essages */. Sel
1fde1 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 ect *pSelect,
1fde2 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
1fde3 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65 atement with the
1fde4 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
1fde5 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 */. Expr *pE
1fde6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1fde7 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42 specific ORDER B
1fde8 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 Y term */.){. i
1fde9 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
1fdea 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1fdeb 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 r */. ExprList
1fdec 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 *pEList; /* The
1fded 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 columns of the
1fdee 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
1fdef 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 NameContext nc;
1fdf0 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 /* Name conte
1fdf1 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 xt for resolving
1fdf2 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 pE */.. assert
1fdf3 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 ( sqlite3ExprIsI
1fdf4 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d 3d nteger(pE, &i)==
1fdf5 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 0 );. pEList =
1fdf6 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b pSelect->pEList;
1fdf7 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 .. /* Resolve a
1fdf8 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 ll names in the
1fdf9 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78 ORDER BY term ex
1fdfa 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 pression. */.
1fdfb 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 memset(&nc, 0, s
1fdfc 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 izeof(nc));. nc
1fdfd 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
1fdfe 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 ;. nc.pSrcList
1fdff 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b = pSelect->pSrc;
1fe00 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 . nc.pEList = p
1fe01 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f EList;. nc.allo
1fe02 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e wAgg = 1;. nc.n
1fe03 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 Err = 0;. if( s
1fe04 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
1fe05 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 rNames(&nc, pE)
1fe06 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1fe07 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 rorClear(pParse)
1fe08 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
1fe09 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f }.. /* Try to
1fe0a 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52 match the ORDER
1fe0b 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61 BY expression a
1fe0c 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73 gainst an expres
1fe0d 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 sion. ** in the
1fe0e 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65 result set. Re
1fe0f 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 turn an 1-based
1fe10 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74 index of the mat
1fe11 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c ching. ** resul
1fe12 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a t-set entry.. *
1fe13 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 /. for(i=0; i<p
1fe14 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b EList->nExpr; i+
1fe15 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 +){. if( sqli
1fe16 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 te3ExprCompare(p
1fe17 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 EList->a[i].pExp
1fe18 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20 r, pE) ){.
1fe19 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 return i+1;.
1fe1a 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e }. }.. /* If n
1fe1b 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 o match, return
1fe1c 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 0. */. return 0
1fe1d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 ;.}../*.** Gener
1fe1e 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 ate an ORDER BY
1fe1f 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d or GROUP BY term
1fe20 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 out-of-range er
1fe21 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ror..*/.static v
1fe22 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 oid resolveOutOf
1fe23 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 61 RangeError(. Pa
1fe24 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
1fe25 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 6f /* The erro
1fe26 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 r context into w
1fe27 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68 hich to write th
1fe28 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e e error */. con
1fe29 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 st char *zType,
1fe2a 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f /* "ORDER" o
1fe2b 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 r "GROUP" */. i
1fe2c 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 nt i,
1fe2d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 /* The ind
1fe2e 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 20 ex (1-based) of
1fe2f 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 the term out of
1fe30 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d range */. int m
1fe31 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x
1fe32 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 72 /* Largest per
1fe33 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 6f missible value o
1fe34 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 f i */.){. sqli
1fe35 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1fe36 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 20 se, . "%r %s
1fe37 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 BY term out of r
1fe38 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 ange - should be
1fe39 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e 20 ". "between
1fe3a 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 1 and %d", i, zT
1fe3b 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a ype, mx);.}../*.
1fe3c 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f ** Analyze the O
1fe3d 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 RDER BY clause i
1fe3e 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c n a compound SEL
1fe3f 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ECT statement.
1fe40 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 Modify.** each
1fe41 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 term of the ORDE
1fe42 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 R BY clause is a
1fe43 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 constant intege
1fe44 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 r between 1.** a
1fe45 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20 nd N where N is
1fe46 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f the number of co
1fe47 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d lumns in the com
1fe48 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a pound SELECT..**
1fe49 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 .** ORDER BY ter
1fe4a 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 65 ms that are alre
1fe4b 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 62 ady an integer b
1fe4c 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 etween 1 and N a
1fe4d 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 re.** unmodified
1fe4e 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d . ORDER BY term
1fe4f 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 67 s that are integ
1fe50 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 20 ers outside the
1fe51 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 range of.** 1 th
1fe52 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 65 rough N generate
1fe53 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 an error. ORDE
1fe54 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 R BY terms that
1fe55 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a are expressions.
1fe56 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 61 ** are matched a
1fe57 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65 gainst result se
1fe58 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 t expressions of
1fe59 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
1fe5a 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 .** beginning wi
1fe5b 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 th the left-most
1fe5c 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b SELECT and work
1fe5d 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 72 ing toward the r
1fe5e 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 ight..** At the
1fe5f 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 65 first match, the
1fe60 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73 ORDER BY expres
1fe61 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 sion is transfor
1fe62 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 med into.** the
1fe63 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e integer column n
1fe64 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 umber..**.** Ret
1fe65 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
1fe66 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a f errors seen..*
1fe67 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
1fe68 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 olveCompoundOrde
1fe69 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 rBy(. Parse *pP
1fe6a 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 arse, /*
1fe6b 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e Parsing context.
1fe6c 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 Leave error me
1fe6d 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 ssages here */.
1fe6e 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
1fe6f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 /* The SE
1fe70 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 LECT statement c
1fe71 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 ontaining the OR
1fe72 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 DER BY */.){. i
1fe73 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 nt i;. ExprList
1fe74 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 *pOrderBy;. Ex
1fe75 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a prList *pEList;.
1fe76 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
1fe77 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 int moreToDo =
1fe78 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 1;.. pOrderBy =
1fe79 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 pSelect->pOrder
1fe7a 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72 By;. if( pOrder
1fe7b 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 By==0 ) return 0
1fe7c 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
1fe7d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f >db;.#if SQLITE_
1fe7e 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 MAX_COLUMN. if(
1fe7f 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 pOrderBy->nExpr
1fe80 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
1fe81 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d TE_LIMIT_COLUMN]
1fe82 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
1fe83 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1fe84 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 "too many terms
1fe85 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 in ORDER BY clau
1fe86 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e se");. return
1fe87 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 1;. }.#endif.
1fe88 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 for(i=0; i<pOrd
1fe89 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b erBy->nExpr; i++
1fe8a 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d ){. pOrderBy-
1fe8b 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a >a[i].done = 0;.
1fe8c 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 }. pSelect->p
1fe8d 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c Next = 0;. whil
1fe8e 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 e( pSelect->pPri
1fe8f 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 or ){. pSelec
1fe90 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 t->pPrior->pNext
1fe91 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 = pSelect;.
1fe92 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 pSelect = pSelec
1fe93 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 t->pPrior;. }.
1fe94 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 while( pSelect
1fe95 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 && moreToDo ){.
1fe96 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
1fe97 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
1fe98 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 moreToDo = 0
1fe99 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 ;. pEList = p
1fe9a 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a Select->pEList;.
1fe9b 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 assert( pELi
1fe9c 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 st!=0 );. for
1fe9d 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 (i=0, pItem=pOrd
1fe9e 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 erBy->a; i<pOrde
1fe9f 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c rBy->nExpr; i++,
1fea0 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1fea1 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a int iCol = -1;.
1fea2 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20 Expr *pE,
1fea3 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28 *pDup;. if(
1fea4 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 pItem->done ) c
1fea5 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 ontinue;. p
1fea6 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 E = pItem->pExpr
1fea7 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
1fea8 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
1fea9 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 (pE, &iCol) ){.
1feaa 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c if( iCol<
1feab 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 =0 || iCol>pELis
1feac 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 t->nExpr ){.
1fead 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 resolveOut
1feae 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 OfRangeError(pPa
1feaf 72 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 2b rse, "ORDER", i+
1feb0 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 1, pEList->nExpr
1feb1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
1feb2 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d urn 1;. }
1feb3 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1feb4 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 iCol = res
1feb5 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 olveAsName(pPars
1feb6 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a e, pEList, pE);.
1feb7 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c if( iCol
1feb8 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1feb9 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 pDup = sqlite3E
1feba 78 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20 30 xprDup(db, pE, 0
1febb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
1febc 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c !db->mallocFail
1febd 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ed ){.
1febe 20 20 61 73 73 65 72 74 28 70 44 75 70 29 3b 0a assert(pDup);.
1febf 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c iCol
1fec0 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 = resolveOrderB
1fec1 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 yTermToExprList(
1fec2 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c pParse, pSelect,
1fec3 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 pDup);.
1fec4 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 }. sq
1fec5 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
1fec6 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 db, pDup);.
1fec7 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1fec8 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b if( iCol>0 ){
1fec9 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 . CollSeq
1feca 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 *pColl = pE->pC
1fecb 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 oll;. int
1fecc 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 flags = pE->fla
1fecd 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 gs & EP_ExpColla
1fece 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 te;. sqli
1fecf 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
1fed0 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70 , pE);. p
1fed1 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 Item->pExpr = pE
1fed2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 = sqlite3Expr(d
1fed3 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 b, TK_INTEGER, 0
1fed4 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
1fed5 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b E==0 ) return 1;
1fed6 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f . pE->pCo
1fed7 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 ll = pColl;.
1fed8 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d pE->flags |=
1fed9 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66 EP_IntValue | f
1feda 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 45 lags;. pE
1fedb 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 43 6f ->u.iValue = iCo
1fedc 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d l;. pItem
1fedd 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 ->iCol = (u16)iC
1fede 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 ol;. pIte
1fedf 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 m->done = 1;.
1fee0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fee1 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a moreToDo = 1;.
1fee2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1fee3 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c pSelect = pSel
1fee4 65 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a ect->pNext;. }.
1fee5 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 for(i=0; i<pOr
1fee6 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b derBy->nExpr; i+
1fee7 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 +){. if( pOrd
1fee8 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d erBy->a[i].done=
1fee9 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
1feea 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1feeb 73 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59 se, "%r ORDER BY
1feec 20 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d term does not m
1feed 61 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20 atch any ".
1feee 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69 "column i
1feef 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
1fef0 22 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72 ", i+1);. r
1fef1 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
1fef2 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
1fef3 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76 ../*.** Check ev
1fef4 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 ery term in the
1fef5 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 ORDER BY or GROU
1fef6 50 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64 P BY clause pOrd
1fef7 65 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53 erBy of.** the S
1fef8 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
1fef9 70 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79 pSelect. If any
1fefa 20 74 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e term is referen
1fefb 63 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c ce to a.** resul
1fefc 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t set expression
1fefd 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 (as determined
1fefe 62 79 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e by the ExprList.
1feff 61 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a a.iCol field).**
1ff00 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 then convert th
1ff01 61 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63 at term into a c
1ff02 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 opy of the corre
1ff03 73 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20 sponding result
1ff04 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a set.** column..*
1ff05 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f *.** If any erro
1ff06 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c rs are detected,
1ff07 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 add an error me
1ff08 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 20 ssage to pParse
1ff09 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f and.** return no
1ff0a 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 n-zero. Return
1ff0b 7a 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72 zero if no error
1ff0c 73 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 s are seen..*/.S
1ff0d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1ff0e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 t sqlite3Resolve
1ff0f 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 OrderGroupBy(.
1ff10 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
1ff11 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 /* Parsing
1ff12 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 context. Leave
1ff13 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
1ff14 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 here */. Select
1ff15 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 *pSelect,
1ff16 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
1ff17 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 atement containi
1ff18 6e 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f ng the clause */
1ff19 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 . ExprList *pOr
1ff1a 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20 derBy, /* The
1ff1b 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 ORDER BY or GROU
1ff1c 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62 P BY clause to b
1ff1d 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 e processed */.
1ff1e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 const char *zTy
1ff1f 70 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 pe /* "ORDER
1ff20 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a " or "GROUP" */.
1ff21 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 ){. int i;. sq
1ff22 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
1ff23 73 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69 se->db;. ExprLi
1ff24 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 st *pEList;. st
1ff25 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
1ff26 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 em *pItem;.. if
1ff27 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c ( pOrderBy==0 ||
1ff28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
1ff29 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 locFailed ) retu
1ff2a 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 rn 0;.#if SQLITE
1ff2b 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 _MAX_COLUMN. if
1ff2c 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 ( pOrderBy->nExp
1ff2d 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c r>db->aLimit[SQL
1ff2e 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e ITE_LIMIT_COLUMN
1ff2f 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ] ){. sqlite3
1ff30 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1ff31 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 "too many terms
1ff32 20 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65 in %s BY clause
1ff33 22 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72 ", zType);. r
1ff34 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e eturn 1;. }.#en
1ff35 64 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 dif. pEList = p
1ff36 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a Select->pEList;.
1ff37 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 assert( pEList
1ff38 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 !=0 ); /* sqlit
1ff39 65 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75 e3SelectNew() gu
1ff3a 61 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f arantees this */
1ff3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 . for(i=0, pIte
1ff3c 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 m=pOrderBy->a; i
1ff3d 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 <pOrderBy->nExpr
1ff3e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
1ff3f 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e . if( pItem->
1ff40 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 iCol ){. if
1ff41 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45 ( pItem->iCol>pE
1ff42 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 List->nExpr ){.
1ff43 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 resolveOu
1ff44 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 tOfRangeError(pP
1ff45 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 arse, zType, i+1
1ff46 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 , pEList->nExpr)
1ff47 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
1ff48 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
1ff49 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 resolveAlias(p
1ff4a 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 Parse, pEList, p
1ff4b 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49 Item->iCol-1, pI
1ff4c 74 65 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 70 tem->pExpr, zTyp
1ff4d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
1ff4e 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
1ff4f 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 ** pOrderBy is a
1ff50 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 n ORDER BY or GR
1ff51 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 6e OUP BY clause in
1ff52 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1ff53 74 20 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 68 t pSelect..** Th
1ff54 65 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f e Name context o
1ff55 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 f the SELECT sta
1ff56 74 65 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 20 tement is pNC.
1ff57 7a 54 79 70 65 20 69 73 20 65 69 74 68 65 72 0a zType is either.
1ff58 2a 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 ** "ORDER" or "G
1ff59 52 4f 55 50 22 20 64 65 70 65 6e 64 69 6e 67 20 ROUP" depending
1ff5a 6f 6e 20 77 68 69 63 68 20 74 79 70 65 20 6f 66 on which type of
1ff5b 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 clause pOrderBy
1ff5c 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 is..**.** This
1ff5d 72 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73 routine resolves
1ff5e 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 each term of th
1ff5f 65 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e e clause into an
1ff60 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 expression..**
1ff61 49 66 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 If the order-by
1ff62 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 term is an integ
1ff63 65 72 20 49 20 62 65 74 77 65 65 6e 20 31 20 61 er I between 1 a
1ff64 6e 64 20 4e 20 28 77 68 65 72 65 20 4e 20 69 73 nd N (where N is
1ff65 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
1ff66 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
1ff67 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 result set of t
1ff68 68 65 20 53 45 4c 45 43 54 29 20 74 68 65 6e 20 he SELECT) then
1ff69 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a the expression.*
1ff6a 2a 20 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 74 * in the resolut
1ff6b 69 6f 6e 20 69 73 20 61 20 63 6f 70 79 20 6f 66 ion is a copy of
1ff6c 20 74 68 65 20 49 2d 74 68 20 72 65 73 75 6c 74 the I-th result
1ff6d 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e -set expression.
1ff6e 20 20 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 65 If.** the orde
1ff6f 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 r-by term is an
1ff70 69 64 65 6e 74 69 66 79 20 74 68 61 74 20 63 6f identify that co
1ff71 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 rresponds to the
1ff72 20 41 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61 AS-name of.** a
1ff73 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 result-set expr
1ff74 65 73 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 ession, then the
1ff75 20 74 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 74 term resolves t
1ff76 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a o a copy of the.
1ff77 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 ** result-set ex
1ff78 70 72 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72 pression. Other
1ff79 77 69 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 wise, the expres
1ff7a 73 69 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 64 sion is resolved
1ff7b 20 69 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c in.** the usual
1ff7c 20 77 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c way - using sql
1ff7d 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
1ff7e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 ames()..**.** Th
1ff7f 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
1ff80 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
1ff81 20 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72 errors. If err
1ff82 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0a ors occur, then.
1ff83 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 ** an appropriat
1ff84 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
1ff85 6d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69 6e might be left in
1ff86 20 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 65 pParse. (OOM e
1ff87 72 72 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 65 rrors.** excepte
1ff88 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e d.).*/.static in
1ff89 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 t resolveOrderGr
1ff8a 6f 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f 6e oupBy(. NameCon
1ff8b 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 2f text *pNC, /
1ff8c 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 * The name conte
1ff8d 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 xt of the SELECT
1ff8e 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
1ff8f 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c Select *pSelect,
1ff90 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c /* The SEL
1ff91 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68 6f ECT statement ho
1ff92 6c 64 69 6e 67 20 70 4f 72 64 65 72 42 79 20 2a lding pOrderBy *
1ff93 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f /. ExprList *pO
1ff94 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20 rderBy, /* An
1ff95 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 ORDER BY or GROU
1ff96 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 72 P BY clause to r
1ff97 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73 esolve */. cons
1ff98 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 t char *zType
1ff99 20 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44 /* Either "ORD
1ff9a 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20 ER" or "GROUP",
1ff9b 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a as appropriate *
1ff9c 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 /.){. int i;
1ff9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff9e 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
1ff9f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 unter */. int i
1ffa0 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Col;
1ffa1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
1ffa2 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 umn number */.
1ffa3 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
1ffa4 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 2f item *pItem; /
1ffa5 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 * A term of the
1ffa6 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
1ffa7 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 */. Parse *pPar
1ffa8 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 se;
1ffa9 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
1ffaa 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
1ffab 6e 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 nResult;
1ffac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1ffad 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e mber of terms in
1ffae 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1ffaf 2a 2f 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 */.. if( pOrder
1ffb0 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 By==0 ) return 0
1ffb1 3b 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 53 ;. nResult = pS
1ffb2 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e elect->pEList->n
1ffb3 45 78 70 72 3b 0a 20 20 70 50 61 72 73 65 20 3d Expr;. pParse =
1ffb4 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 pNC->pParse;.
1ffb5 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 for(i=0, pItem=p
1ffb6 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f OrderBy->a; i<pO
1ffb7 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 rderBy->nExpr; i
1ffb8 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
1ffb9 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 49 74 Expr *pE = pIt
1ffba 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 em->pExpr;. i
1ffbb 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e Col = resolveAsN
1ffbc 61 6d 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c ame(pParse, pSel
1ffbd 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 29 ect->pEList, pE)
1ffbe 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 ;. if( iCol>0
1ffbf 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
1ffc0 61 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 an AS-name match
1ffc1 20 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 is found, mark
1ffc2 74 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 6f this ORDER BY co
1ffc3 6c 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 lumn as being.
1ffc4 20 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 ** a copy of
1ffc5 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 the iCol-th res
1ffc6 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 ult-set column.
1ffc7 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 The subsequent
1ffc8 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a call to. **
1ffc9 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f sqlite3ResolveO
1ffca 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 rderGroupBy() wi
1ffcb 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65 ll convert the e
1ffcc 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 xpression to a.
1ffcd 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 ** copy of
1ffce 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 the iCol-th resu
1ffcf 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f lt-set expressio
1ffd0 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 n. */. pIte
1ffd1 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 m->iCol = (u16)i
1ffd2 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 Col;. conti
1ffd3 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 nue;. }. i
1ffd4 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 f( sqlite3ExprIs
1ffd5 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f Integer(pE, &iCo
1ffd6 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 l) ){. /* T
1ffd7 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d he ORDER BY term
1ffd8 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 is an integer c
1ffd9 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c onstant. Again,
1ffda 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a set the column.
1ffdb 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 ** number
1ffdc 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 52 so that sqlite3R
1ffdd 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
1ffde 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 By() will conver
1ffdf 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f t the. ** o
1ffe0 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 rder-by term to
1ffe1 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 a copy of the re
1ffe2 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
1ffe3 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ion */. if(
1ffe4 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 iCol<1 ){.
1ffe5 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 resolveOutOfR
1ffe6 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 angeError(pParse
1ffe7 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 , zType, i+1, nR
1ffe8 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 esult);.
1ffe9 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 return 1;.
1ffea 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 }. pItem->i
1ffeb 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b Col = (u16)iCol;
1ffec 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b . continue;
1ffed 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f . }.. /* O
1ffee 74 68 65 72 77 69 73 65 2c 20 74 72 65 61 74 20 therwise, treat
1ffef 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 the ORDER BY ter
1fff0 6d 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 m as an ordinary
1fff1 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
1fff2 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d pItem->iCol =
1fff3 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 0;. if( sqli
1fff4 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
1fff5 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a mes(pNC, pE) ){.
1fff6 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
1fff7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1fff8 72 6e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 rn sqlite3Resolv
1fff9 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 eOrderGroupBy(pP
1fffa 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 arse, pSelect, p
1fffb 4f 72 64 65 72 42 79 2c 20 7a 54 79 70 65 29 3b OrderBy, zType);
1fffc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 .}../*.** Resolv
1fffd 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 53 e names in the S
1fffe 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
1ffff 70 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 p and all of its
20000 20 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f descendents..*/
20001 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f .static int reso
20002 6c 76 65 53 65 6c 65 63 74 53 74 65 70 28 57 61 lveSelectStep(Wa
20003 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 lker *pWalker, S
20004 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d elect *p){. Nam
20005 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 eContext *pOuter
20006 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 NC; /* Context
20007 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
20008 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e is SELECT */. N
20009 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 ameContext sNC;
2000a 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 /* Name c
2000b 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 53 ontext of this S
2000c 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 ELECT */. int i
2000d 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 sCompound;
2000e 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 20 /* True if p
2000f 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 is a compound se
20010 6c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 lect */. int nC
20011 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 ompound;
20012 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
20013 6f 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 ompound terms pr
20014 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 20 2a ocessed so far *
20015 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 /. Parse *pPars
20016 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 e; /* P
20017 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
20018 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
20019 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 List; /* R
2001a 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 esult set expres
2001b 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 sion list */. i
2001c 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
2001d 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
2001e 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 ounter */. Expr
2001f 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 List *pGroupBy;
20020 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 /* The GROUP
20021 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 BY clause */.
20022 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 Select *pLeftmos
20023 74 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d t; /* Left-
20024 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 20 6f most of SELECT o
20025 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a f a compound */.
20026 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
20027 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
20028 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
20029 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 */. .. assert
2002a 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 ( p!=0 );. if(
2002b 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 p->selFlags & SF
2002c 5f 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 _Resolved ){.
2002d 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
2002e 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e e;. }. pOuterN
2002f 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 C = pWalker->u.p
20030 4e 43 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 NC;. pParse = p
20031 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a Walker->pParse;.
20032 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
20033 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c b;.. /* Normall
20034 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 y sqlite3SelectE
20035 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 xpand() will be
20036 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e 64 called first and
20037 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 will have. **
20038 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65 64 already expanded
20039 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20 48 this SELECT. H
2003a 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69 73 20 owever, if this
2003b 69 73 20 61 20 73 75 62 71 75 65 72 79 20 77 69 is a subquery wi
2003c 74 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 thin. ** an exp
2003d 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 ression, sqlite3
2003e 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
2003f 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 () will be calle
20040 64 20 77 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a d without a. **
20041 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 prior call to s
20042 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 qlite3SelectExpa
20043 6e 64 28 29 2e 20 20 57 68 65 6e 20 74 68 61 74 nd(). When that
20044 20 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 happens, let.
20045 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 ** sqlite3Select
20046 50 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 Prep() do all of
20047 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 the processing
20048 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 2e for this SELECT.
20049 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c . ** sqlite3Sel
2004a 65 63 74 50 72 65 70 28 29 20 77 69 6c 6c 20 69 ectPrep() will i
2004b 6e 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 nvoke both sqlit
2004c 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 e3SelectExpand()
2004d 20 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 and. ** this r
2004e 6f 75 74 69 6e 65 20 69 6e 20 74 68 65 20 63 6f outine in the co
2004f 72 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a rrect order.. *
20050 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 /. if( (p->selF
20051 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 lags & SF_Expand
20052 65 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 ed)==0 ){. sq
20053 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 lite3SelectPrep(
20054 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 pParse, p, pOute
20055 72 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 72 6e rNC);. return
20056 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c (pParse->nErr |
20057 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
20058 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 ed) ? WRC_Abort
20059 3a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d : WRC_Prune;. }
2005a 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d .. isCompound =
2005b 20 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 p->pPrior!=0;.
2005c 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a nCompound = 0;.
2005d 20 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b pLeftmost = p;
2005e 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 . while( p ){.
2005f 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 assert( (p->s
20060 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 elFlags & SF_Exp
20061 61 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 anded)!=0 );.
20062 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c assert( (p->sel
20063 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c Flags & SF_Resol
20064 76 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 ved)==0 );. p
20065 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 ->selFlags |= SF
20066 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 _Resolved;..
20067 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65 /* Resolve the e
20068 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 xpressions in th
20069 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 e LIMIT and OFFS
2006a 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65 73 ET clauses. Thes
2006b 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 e. ** are not
2006c 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 allowed to refe
2006d 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 r to any names,
2006e 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74 79 so pass an empty
2006f 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 NameContext..
20070 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 */. memset(
20071 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 &sNC, 0, sizeof(
20072 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 sNC));. sNC.p
20073 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
20074 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 if( sqlite3R
20075 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 esolveExprNames(
20076 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 &sNC, p->pLimit)
20077 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ||. sqli
20078 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
20079 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 mes(&sNC, p->pOf
2007a 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 72 fset) ){. r
2007b 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
2007c 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
2007d 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63 61 Set up the loca
2007e 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 l name-context t
2007f 6f 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74 65 o pass to sqlite
20080 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
20081 73 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 s() to. ** re
20082 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c 74 solve the result
20083 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 -set expression
20084 6c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 list.. */.
20085 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 sNC.allowAgg =
20086 31 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 1;. sNC.pSrcL
20087 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 ist = p->pSrc;.
20088 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 sNC.pNext = p
20089 4f 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20 OuterNC;. .
2008a 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 /* Resolve names
2008b 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
2008c 65 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73 et. */. pELis
2008d 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 t = p->pEList;.
2008e 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 assert( pELis
2008f 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 t!=0 );. for(
20090 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e i=0; i<pEList->n
20091 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
20092 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 45 4c Expr *pX = pEL
20093 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b ist->a[i].pExpr;
20094 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
20095 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
20096 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a es(&sNC, pX) ){.
20097 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 return W
20098 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 RC_Abort;.
20099 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
2009a 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 72 65 * Recursively re
2009b 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 solve names in a
2009c 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 ll subqueries.
2009d 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 */. for(i=0
2009e 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 ; i<p->pSrc->nSr
2009f 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 c; i++){. s
200a0 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
200a1 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e em *pItem = &p->
200a2 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 pSrc->a[i];.
200a3 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 if( pItem->pSe
200a4 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 lect ){.
200a5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76 const char *zSav
200a6 65 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 edContext = pPar
200a7 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 se->zAuthContext
200a8 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 ;. if( pI
200a9 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61 tem->zName ) pPa
200aa 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 rse->zAuthContex
200ab 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 t = pItem->zName
200ac 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
200ad 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 3ResolveSelectNa
200ae 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 mes(pParse, pIte
200af 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75 74 m->pSelect, pOut
200b0 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 70 erNC);. p
200b1 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 Parse->zAuthCont
200b2 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74 ext = zSavedCont
200b3 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ext;. if(
200b4 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c pParse->nErr ||
200b5 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
200b6 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 d ) return WRC_A
200b7 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 bort;. }.
200b8 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
200b9 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 67 there are no ag
200ba 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
200bb 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d s in the result-
200bc 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 set, and no GROU
200bd 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78 70 P BY . ** exp
200be 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 ression, do not
200bf 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 73 allow aggregates
200c0 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f in any of the o
200c1 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 ther expressions
200c2 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
200c3 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 ert( (p->selFlag
200c4 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 s & SF_Aggregate
200c5 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72 6f )==0 );. pGro
200c6 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 upBy = p->pGroup
200c7 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72 6f By;. if( pGro
200c8 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 upBy || sNC.hasA
200c9 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73 gg ){. p->s
200ca 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 elFlags |= SF_Ag
200cb 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c gregate;. }el
200cc 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c se{. sNC.al
200cd 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 lowAgg = 0;.
200ce 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 }. . /* If a
200cf 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 69 HAVING clause i
200d0 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 s present, then
200d1 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 there must be a
200d2 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e GROUP BY clause.
200d3 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
200d4 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 70 p->pHaving && !p
200d5 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 GroupBy ){.
200d6 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
200d7 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f 55 (pParse, "a GROU
200d8 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 72 P BY clause is r
200d9 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 48 equired before H
200da 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 AVING");. r
200db 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
200dc 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
200dd 20 41 64 64 20 74 68 65 20 65 78 70 72 65 73 73 Add the express
200de 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 20 ion list to the
200df 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 name-context bef
200e0 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 65 0a ore parsing the.
200e1 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 ** other exp
200e2 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 ressions in the
200e3 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
200e4 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 . This is so tha
200e5 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 t. ** express
200e6 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 ions in the WHER
200e7 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29 20 E clause (etc.)
200e8 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78 70 can refer to exp
200e9 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 ressions by.
200ea 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74 68 ** aliases in th
200eb 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 e result set..
200ec 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f **. ** Mino
200ed 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 r point: If this
200ee 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 is the case, th
200ef 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f en the expressio
200f0 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a n will be. **
200f1 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f re-evaluated fo
200f2 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 65 r each reference
200f3 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 to it.. */.
200f4 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 sNC.pEList =
200f5 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 p->pEList;. i
200f6 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 f( sqlite3Resolv
200f7 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c eExprNames(&sNC,
200f8 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 p->pWhere) ||.
200f9 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 sqlite3Res
200fa 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 olveExprNames(&s
200fb 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a NC, p->pHaving).
200fc 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ){. ret
200fd 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
200fe 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 }.. /* The
200ff 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47 52 ORDER BY and GR
20100 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 20 6d OUP BY clauses m
20101 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 ay not refer to
20102 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 terms in. **
20103 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 0a 20 outer queries .
20104 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e */. sNC.pN
20105 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 ext = 0;. sNC
20106 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a .allowAgg = 1;..
20107 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 /* Process t
20108 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
20109 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e se for singleton
2010a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
2010b 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f ts.. ** The O
2010c 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 RDER BY clause f
2010d 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c or compounds SEL
2010e 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 ECT statements i
2010f 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a s handled. **
20110 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c below, after al
20111 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d l of the result-
20112 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 sets for all of
20113 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a the elements of.
20114 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f ** the compo
20115 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65 und have been re
20116 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 solved.. */.
20117 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f 75 if( !isCompou
20118 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72 64 nd && resolveOrd
20119 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 erGroupBy(&sNC,
2011a 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 p, p->pOrderBy,
2011b 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20 20 "ORDER") ){.
2011c 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
2011d 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 rt;. }. if
2011e 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
2011f 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ed ){. retu
20120 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
20121 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 }. . /* Re
20122 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50 20 solve the GROUP
20123 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20 74 BY clause. At t
20124 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61 he same time, ma
20125 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a 20 ke sure . **
20126 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 the GROUP BY cla
20127 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e use does not con
20128 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 66 tain aggregate f
20129 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f unctions.. */
2012a 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 . if( pGroupB
2012b 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 y ){. struc
2012c 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
2012d 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20 *pItem;. .
2012e 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 if( resolveOr
2012f 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c derGroupBy(&sNC,
20130 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 47 p, pGroupBy, "G
20131 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 ROUP") || db->ma
20132 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
20133 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
20134 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a _Abort;. }.
20135 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 for(i=0, p
20136 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 Item=pGroupBy->a
20137 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 ; i<pGroupBy->nE
20138 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b xpr; i++, pItem+
20139 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +){. if(
2013a 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
2013b 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50 pItem->pExpr, EP
2013c 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 _Agg) ){.
2013d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
2013e 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 67 72 sg(pParse, "aggr
2013f 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
20140 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 are not allowed
20141 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 in ".
20142 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 59 "the GROUP BY
20143 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 20 clause");.
20144 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
20145 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 7d Abort;. }
20146 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
20147 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 /* Advance t
20148 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20 o the next term
20149 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a of the compound.
2014a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 */. p = p
2014b 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43 ->pPrior;. nC
2014c 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a ompound++;. }..
2014d 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 /* Resolve the
2014e 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20 63 ORDER BY on a c
2014f 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 ompound SELECT a
20150 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f fter all terms o
20151 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f f. ** the compo
20152 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65 und have been re
20153 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 solved.. */. i
20154 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 f( isCompound &&
20155 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 resolveCompound
20156 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20 OrderBy(pParse,
20157 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20 pLeftmost) ){.
20158 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
20159 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 rt;. }.. retur
2015a 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a n WRC_Prune;.}..
2015b 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
2015c 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 72 ne walks an expr
2015d 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 ession tree and
2015e 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 6e resolves referen
2015f 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 ces to.** table
20160 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 columns and resu
20161 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 lt-set columns.
20162 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d At the same tim
20163 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63 e, do error.** c
20164 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 hecking on funct
20165 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73 65 ion usage and se
20166 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79 20 t a flag if any
20167 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
20168 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e ons.** are seen.
20169 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 .**.** To resolv
2016a 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 e table columns
2016b 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c 6f references we lo
2016c 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72 ok for nodes (or
2016d 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74 68 subtrees) of th
2016e 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a e .** form X.Y.Z
2016f 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 or Y.Z or just
20170 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20 Z where.**.**
20171 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d 65 X: The name
20172 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 20 of a database.
20173 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20 Ex: "main" or
20174 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20 "temp" or.**
20175 20 20 20 20 20 20 20 74 68 65 20 73 79 6d 62 6f the symbo
20176 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 lic name assigne
20177 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d 65 d to an ATTACH-e
20178 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a d database..**.*
20179 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 65 20 * Y: The
2017a 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20 name of a table
2017b 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 in a FROM clause
2017c 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67 67 . Or in a trigg
2017d 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 er.**
2017e 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 one of the speci
2017f 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f al names "old" o
20180 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20 r "new"..**.**
20181 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d Z: The nam
20182 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e e of a column in
20183 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20 table Y..**.**
20184 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65 20 The node at the
20185 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62 74 root of the subt
20186 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 ree is modified
20187 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
20188 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20 20 * Expr.op
20189 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20 54 Changed to T
2018a 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45 K_COLUMN.** E
2018b 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 6f xpr.pTab Po
2018c 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c ints to the Tabl
2018d 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 e object for X.Y
2018e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f 6c .** Expr.iCol
2018f 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e umn The column
20190 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 20 index in X.Y.
20191 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 -1 for the rowid
20192 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54 61 ..** Expr.iTa
20193 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45 20 ble The VDBE
20194 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f cursor number fo
20195 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 r X.Y.**.**.** T
20196 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c 74 o resolve result
20197 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 73 2c -set references,
20198 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65 73 look for expres
20199 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68 sion nodes of th
2019a 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74 e.** form Z (wit
2019b 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72 65 h no X and Y pre
2019c 66 69 78 29 20 77 68 65 72 65 20 74 68 65 20 5a fix) where the Z
2019d 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 69 67 matches the rig
2019e 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20 ht-hand.** size
2019f 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65 20 of an AS clause
201a0 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 in the result-se
201a1 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20 20 t of a SELECT.
201a2 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f 6e The Z expression
201a3 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64 20 .** is replaced
201a4 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 by a copy of the
201a5 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 left-hand side
201a6 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 of the result-se
201a7 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a t expression..**
201a8 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20 Table-name and
201a9 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74 function resolut
201aa 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 ion occurs on th
201ab 65 20 73 75 62 73 74 69 74 75 74 65 64 20 65 78 e substituted ex
201ac 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 pression.** tree
201ad 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
201ae 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 in:.**.** S
201af 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20 ELECT a+b AS x,
201b0 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31 c+d AS y FROM t1
201b1 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a ORDER BY x;.**.
201b2 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d 20 ** The "x" term
201b3 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 79 20 of the order by
201b4 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 22 is replaced by "
201b5 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a a+b" to render:.
201b6 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 **.** SELEC
201b7 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 T a+b AS x, c+d
201b8 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 AS y FROM t1 ORD
201b9 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a ER BY a+b;.**.**
201ba 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 Function calls
201bb 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20 6d are checked to m
201bc 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
201bd 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a e function is .*
201be 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74 68 * defined and th
201bf 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e at the correct n
201c0 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
201c1 74 73 20 61 72 65 20 73 70 65 63 69 66 69 65 64 ts are specified
201c2 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 ..** If the func
201c3 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72 65 tion is an aggre
201c4 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74 gate function, t
201c5 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61 73 hen the pNC->has
201c6 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e Agg is.** set an
201c7 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 d the opcode is
201c8 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f changed from TK_
201c9 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41 FUNCTION to TK_A
201ca 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 GG_FUNCTION..**
201cb 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e If an expression
201cc 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 contains aggreg
201cd 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 ate functions th
201ce 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a en the EP_Agg.**
201cf 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 65 property on the
201d0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 expression is s
201d1 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72 et..**.** An err
201d2 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65 or message is le
201d3 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 66 20 ft in pParse if
201d4 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 73 anything is amis
201d5 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a s. The number.*
201d6 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 20 72 * if errors is r
201d7 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 eturned..*/.SQLI
201d8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
201d9 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
201da 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 rNames( . NameC
201db 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 ontext *pNC,
201dc 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 /* Namespace
201dd 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 to resolve expre
201de 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 ssions in. */.
201df 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 Expr *pExpr
201e0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
201e1 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 xpression to be
201e2 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a analyzed. */.){.
201e3 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 67 int savedHasAg
201e4 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a g;. Walker w;..
201e5 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 if( pExpr==0 )
201e6 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 return 0;.#if S
201e7 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
201e8 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50 EPTH>0. {. P
201e9 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 arse *pParse = p
201ea 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 NC->pParse;.
201eb 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 if( sqlite3ExprC
201ec 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 heckHeight(pPars
201ed 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 e, pExpr->nHeigh
201ee 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e t+pNC->pParse->n
201ef 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 Height) ){.
201f0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
201f1 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 . pParse->nHe
201f2 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e ight += pExpr->n
201f3 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 Height;. }.#end
201f4 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 67 if. savedHasAgg
201f5 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a = pNC->hasAgg;.
201f6 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 pNC->hasAgg =
201f7 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 0;. w.xExprCall
201f8 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 back = resolveEx
201f9 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c prStep;. w.xSel
201fa 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 ectCallback = re
201fb 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b solveSelectStep;
201fc 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 4e . w.pParse = pN
201fd 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 C->pParse;. w.u
201fe 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71 .pNC = pNC;. sq
201ff 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 lite3WalkExpr(&w
20200 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53 51 , pExpr);.#if SQ
20201 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 LITE_MAX_EXPR_DE
20202 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 PTH>0. pNC->pPa
20203 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 rse->nHeight -=
20204 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a pExpr->nHeight;.
20205 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e 43 #endif. if( pNC
20206 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e 70 50 ->nErr>0 || w.pP
20207 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a arse->nErr>0 ){.
20208 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 ExprSetPrope
20209 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 rty(pExpr, EP_Er
2020a 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ror);. }. if(
2020b 70 4e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20 pNC->hasAgg ){.
2020c 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 ExprSetProper
2020d 74 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67 ty(pExpr, EP_Agg
2020e 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 );. }else if( s
2020f 61 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20 avedHasAgg ){.
20210 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 pNC->hasAgg =
20211 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
20212 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
20213 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 pExpr, EP_Error)
20214 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f ;.}.../*.** Reso
20215 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e lve all names in
20216 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 all expressions
20217 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64 of a SELECT and
20218 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e in all.** decen
20219 64 65 6e 74 73 20 6f 66 20 74 68 65 20 53 45 4c dents of the SEL
2021a 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 ECT, including c
2021b 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20 ompounds off of
2021c 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75 p->pPrior,.** su
2021d 62 71 75 65 72 69 65 73 20 69 6e 20 65 78 70 72 bqueries in expr
2021e 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62 essions, and sub
2021f 71 75 65 72 69 65 73 20 75 73 65 64 20 61 73 20 queries used as
20220 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74 FROM clause.** t
20221 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 erms..**.** See
20222 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
20223 70 72 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 20 prNames() for a
20224 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 description of t
20225 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74 he kinds of.** t
20226 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74 ransformations t
20227 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a hat occur..**.**
20228 20 41 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 74 All SELECT stat
20229 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 61 ements should ha
2022a 76 65 20 62 65 65 6e 20 65 78 70 61 6e 64 65 64 ve been expanded
2022b 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 using.** sqlite
2022c 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 3SelectExpand()
2022d 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e prior to invokin
2022e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a g this routine..
2022f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20230 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
20231 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 solveSelectNames
20232 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
20233 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e, /* Th
20234 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 e parser context
20235 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c */. Select *p,
20236 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20237 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 The SELECT state
20238 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 ment being coded
20239 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 . */. NameConte
2023a 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a xt *pOuterNC /*
2023b 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f Name context fo
2023c 72 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54 20 r parent SELECT
2023d 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a statement */.){.
2023e 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61 Walker w;.. a
2023f 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
20240 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b w.xExprCallback
20241 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 = resolveExprSt
20242 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 ep;. w.xSelectC
20243 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 allback = resolv
20244 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 eSelectStep;. w
20245 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
20246 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f ;. w.u.pNC = pO
20247 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65 uterNC;. sqlite
20248 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 3WalkSelect(&w,
20249 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a p);.}../********
2024a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 ****** End of re
2024b 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a solve.c ********
2024c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2024d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2024e 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
2024f 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
20250 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a e expr.c *******
20251 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20252 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20253 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
20254 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
20255 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
20256 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
20257 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
20258 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
20259 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
2025a 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
2025b 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
2025c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
2025d 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
2025e 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
2025f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
20260 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
20261 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
20262 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
20263 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
20264 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
20265 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
20266 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
20267 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20268 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20269 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2026a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
2026b 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
2026c 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 ins routines use
2026d 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 d for analyzing
2026e 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a expressions and.
2026f 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e ** for generatin
20270 67 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 g VDBE code that
20271 20 65 76 61 6c 75 61 74 65 73 20 65 78 70 72 65 evaluates expre
20272 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 ssions in SQLite
20273 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ..*/../*.** Retu
20274 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 79 rn the 'affinity
20275 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 ' of the express
20276 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e 79 ion pExpr if any
20277 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 ..**.** If pExpr
20278 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20 is a column, a
20279 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 63 reference to a c
2027a 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 53 olumn via an 'AS
2027b 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61 ' alias,.** or a
2027c 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 68 sub-select with
2027d 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 a column as the
2027e 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 74 return value, t
2027f 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 69 hen the .** affi
20280 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f 6c nity of that col
20281 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e umn is returned.
20282 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 30 Otherwise, 0x00
20283 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a is returned,.**
20284 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 61 indicating no a
20285 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 20 ffinity for the
20286 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a expression..**.*
20287 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 45 * i.e. the WHERE
20288 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 73 clause expresss
20289 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c ions in the foll
2028a 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 owing statements
2028b 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20 all.** have an
2028c 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20 affinity:.**.**
2028d 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 CREATE TABLE t1(
2028e 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 a);.** SELECT *
2028f 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3b FROM t1 WHERE a;
20290 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 20 .** SELECT a AS
20291 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 b FROM t1 WHERE
20292 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 b;.** SELECT * F
20293 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 65 ROM t1 WHERE (se
20294 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 3b lect a from t1);
20295 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20296 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 TE char sqlite3E
20297 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 xprAffinity(Expr
20298 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 *pExpr){. int
20299 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a op = pExpr->op;.
2029a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c if( op==TK_SEL
2029b 45 43 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ECT ){. asser
2029c 74 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 26 t( pExpr->flags&
2029d 45 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b 0a EP_xIsSelect );.
2029e 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
2029f 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 e3ExprAffinity(p
202a0 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d Expr->x.pSelect-
202a1 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 >pEList->a[0].pE
202a2 78 70 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 xpr);. }.#ifnde
202a3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 f SQLITE_OMIT_CA
202a4 53 54 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f ST. if( op==TK_
202a5 43 41 53 54 20 29 7b 0a 20 20 20 20 61 73 73 65 CAST ){. asse
202a6 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
202a7 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 erty(pExpr, EP_I
202a8 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 ntValue) );.
202a9 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 66 return sqlite3Af
202aa 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 72 finityType(pExpr
202ab 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d ->u.zToken);. }
202ac 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f .#endif. if( (o
202ad 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
202ae 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d || op==TK_COLUM
202af 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 N || op==TK_REGI
202b0 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78 STER) . && pEx
202b1 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b pr->pTab!=0. ){
202b2 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 . /* op==TK_R
202b3 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 EGISTER && pExpr
202b4 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e ->pTab!=0 happen
202b5 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 s when pExpr was
202b6 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 originally.
202b7 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 ** a TK_COLUMN b
202b8 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c ut was previousl
202b9 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 y evaluated and
202ba 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 cached in a regi
202bb 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ster */. int
202bc 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 j = pExpr->iColu
202bd 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20 mn;. if( j<0
202be 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
202bf 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 AFF_INTEGER;.
202c0 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
202c1 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d pTab && j<pExpr-
202c2 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 >pTab->nCol );.
202c3 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d return pExpr-
202c4 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61 >pTab->aCol[j].a
202c5 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72 ffinity;. }. r
202c6 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66 eturn pExpr->aff
202c7 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 inity;.}../*.**
202c8 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e Set the collatin
202c9 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 g sequence for e
202ca 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 xpression pExpr
202cb 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74 to be the collat
202cc 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ing.** sequence
202cd 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e named by pToken.
202ce 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e Return a poin
202cf 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73 ter to the revis
202d0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a ed expression..*
202d1 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 * The collating
202d2 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b sequence is mark
202d3 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22 ed as "explicit"
202d4 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78 using the EP_Ex
202d5 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67 pCollate.** flag
202d6 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63 . An explicit c
202d7 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
202d8 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20 e will override
202d9 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c implicit.** coll
202da 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e ating sequences.
202db 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
202dc 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
202dd 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 ExprSetColl(Pars
202de 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
202df 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 *pExpr, Token *p
202e0 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 CollName){. cha
202e1 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 r *zColl = 0;
202e2 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 /* Dequ
202e3 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c oted name of col
202e4 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
202e5 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 */. CollSeq *pC
202e6 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a oll;. sqlite3 *
202e7 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
202e8 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 . zColl = sqlit
202e9 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
202ea 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a db, pCollName);.
202eb 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a if( pExpr && z
202ec 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c Coll ){. pCol
202ed 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 l = sqlite3Locat
202ee 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c eCollSeq(pParse,
202ef 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 zColl);. if(
202f0 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 pColl ){.
202f1 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 pExpr->pColl = p
202f2 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78 70 Coll;. pExp
202f3 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 r->flags |= EP_E
202f4 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 7d xpCollate;. }
202f5 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 . }. sqlite3Db
202f6 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b Free(db, zColl);
202f7 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 3b . return pExpr;
202f8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
202f9 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c the default col
202fa 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
202fb 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 for the expressi
202fc 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20 on pExpr. If.**
202fd 74 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66 61 there is no defa
202fe 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 ult collation ty
202ff 70 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f pe, return 0..*/
20300 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20301 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
20302 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 ExprCollSeq(Pars
20303 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
20304 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53 *pExpr){. CollS
20305 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 eq *pColl = 0;.
20306 20 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70 72 Expr *p = pExpr
20307 3b 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 ;. while( ALWAY
20308 53 28 70 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 S(p) ){. int
20309 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 op;. pColl =
2030a 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 p->pColl;. if
2030b 28 20 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b 3b ( pColl ) break;
2030c 0a 20 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b . op = p->op;
2030d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62 . if( p->pTab
2030e 21 3d 30 20 26 26 20 28 0a 20 20 20 20 20 20 20 !=0 && (.
2030f 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 op==TK_AGG_COLU
20310 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c MN || op==TK_COL
20311 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 UMN || op==TK_RE
20312 47 49 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b GISTER || op==TK
20313 5f 54 52 49 47 47 45 52 0a 20 20 20 20 29 29 7b _TRIGGER. )){
20314 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b . /* op==TK
20315 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e _REGISTER && p->
20316 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20 pTab!=0 happens
20317 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f when pExpr was o
20318 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 riginally.
20319 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 ** a TK_COLUMN b
2031a 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c ut was previousl
2031b 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 y evaluated and
2031c 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 cached in a regi
2031d 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f ster */. co
2031e 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b nst char *zColl;
2031f 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 . int j = p
20320 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 ->iColumn;.
20321 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 if( j>=0 ){.
20322 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 sqlite3 *db
20323 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
20324 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 zColl = p
20325 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e ->pTab->aCol[j].
20326 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 zColl;. p
20327 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 Coll = sqlite3Fi
20328 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e ndCollSeq(db, EN
20329 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29 C(db), zColl, 0)
2032a 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d ;. pExpr-
2032b 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a >pColl = pColl;.
2032c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
2032d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
2032e 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 26 f( op!=TK_CAST &
2032f 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 29 & op!=TK_UPLUS )
20330 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 {. break;.
20331 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e }. p = p->
20332 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 28 pLeft;. }. if(
20333 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c sqlite3CheckCol
20334 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f lSeq(pParse, pCo
20335 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c ll) ){ . pCol
20336 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 l = 0;. }. ret
20337 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a urn pColl;.}../*
20338 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e 20 .** pExpr is an
20339 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d operand of a com
2033a 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 parison operator
2033b 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a 2a . aff2 is the.*
2033c 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 * type affinity
2033d 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 65 of the other ope
2033e 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74 rand. This rout
2033f 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a ine returns the.
20340 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 ** type affinity
20341 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
20342 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d used for the com
20343 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 parison operator
20344 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20345 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 ATE char sqlite3
20346 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
20347 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 Expr *pExpr, cha
20348 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 20 r aff2){. char
20349 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 78 aff1 = sqlite3Ex
2034a 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 prAffinity(pExpr
2034b 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 26 );. if( aff1 &&
2034c 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 aff2 ){. /*
2034d 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68 Both sides of th
2034e 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65 e comparison are
2034f 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65 columns. If one
20350 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 20 has numeric.
20351 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73 ** affinity, us
20352 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73 e that. Otherwis
20353 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74 e use no affinit
20354 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 y.. */. if
20355 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 ( sqlite3IsNumer
20356 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 29 icAffinity(aff1)
20357 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 6d || sqlite3IsNum
20358 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 ericAffinity(aff
20359 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 2) ){. retu
2035a 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 rn SQLITE_AFF_NU
2035b 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65 MERIC;. }else
2035c 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
2035d 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a QLITE_AFF_NONE;.
2035e 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
2035f 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 32 ( !aff1 && !aff2
20360 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 ){. /* Neith
20361 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 er side of the c
20362 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 63 omparison is a c
20363 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20 olumn. Compare
20364 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c the. ** resul
20365 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20 ts directly..
20366 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 */. return S
20367 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a QLITE_AFF_NONE;.
20368 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
20369 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 6f One side is a co
2036a 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 20 lumn, the other
2036b 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 20 is not. Use the
2036c 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 columns affinity
2036d 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 . */. assert(
2036e 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32 aff1==0 || aff2
2036f 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 ==0 );. retur
20370 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b n (aff1 + aff2);
20371 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 . }.}../*.** pE
20372 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69 xpr is a compari
20373 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 52 son operator. R
20374 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 61 eturn the type a
20375 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f ffinity that sho
20376 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65 uld.** be applie
20377 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e d to both operan
20378 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e ds prior to doin
20379 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e g the comparison
2037a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
2037b 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e comparisonAffin
2037c 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 ity(Expr *pExpr)
2037d 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 20 {. char aff;.
2037e 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f assert( pExpr->o
2037f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70 p==TK_EQ || pExp
20380 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 r->op==TK_IN ||
20381 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 pExpr->op==TK_LT
20382 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 ||. pE
20383 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c xpr->op==TK_GT |
20384 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f | pExpr->op==TK_
20385 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d GE || pExpr->op=
20386 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20 =TK_LE ||.
20387 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 pExpr->op==T
20388 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74 K_NE );. assert
20389 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 29 ( pExpr->pLeft )
2038a 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 ;. aff = sqlite
2038b 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 3ExprAffinity(pE
2038c 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69 xpr->pLeft);. i
2038d 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 f( pExpr->pRight
2038e 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 71 ){. aff = sq
2038f 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
20390 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 67 nity(pExpr->pRig
20391 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73 ht, aff);. }els
20392 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f e if( ExprHasPro
20393 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
20394 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
20395 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 aff = sqlite3C
20396 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 ompareAffinity(p
20397 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d Expr->x.pSelect-
20398 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 >pEList->a[0].pE
20399 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c xpr, aff);. }el
2039a 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20 se if( !aff ){.
2039b 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f aff = SQLITE_
2039c 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 AFF_NONE;. }.
2039d 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f return aff;.}../
2039e 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 *.** pExpr is a
2039f 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65 comparison expre
203a0 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20 ssion, eg. '=',
203a1 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63 '<', IN(...) etc
203a2 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74 ..** idx_affinit
203a3 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74 y is the affinit
203a4 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20 y of an indexed
203a5 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74 column. Return t
203a6 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e rue.** if the in
203a7 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74 dex with affinit
203a8 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d y idx_affinity m
203a9 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d ay be used to im
203aa 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63 plement.** the c
203ab 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78 omparison in pEx
203ac 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 pr..*/.SQLITE_PR
203ad 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
203ae 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 3IndexAffinityOk
203af 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 (Expr *pExpr, ch
203b0 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29 ar idx_affinity)
203b1 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 {. char aff = c
203b2 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 omparisonAffinit
203b3 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74 y(pExpr);. swit
203b4 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63 ch( aff ){. c
203b5 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e ase SQLITE_AFF_N
203b6 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72 ONE:. retur
203b7 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51 n 1;. case SQ
203b8 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20 LITE_AFF_TEXT:.
203b9 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f return idx_
203ba 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 affinity==SQLITE
203bb 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64 _AFF_TEXT;. d
203bc 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65 efault:. re
203bd 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75 turn sqlite3IsNu
203be 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64 mericAffinity(id
203bf 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d x_affinity);. }
203c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
203c1 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68 the P5 value th
203c2 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 at should be use
203c3 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63 d for a binary c
203c4 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63 omparison.** opc
203c5 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47 ode (OP_Eq, OP_G
203c6 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20 e etc.) used to
203c7 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61 compare pExpr1 a
203c8 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74 nd pExpr2..*/.st
203c9 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f atic u8 binaryCo
203ca 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45 mpareP5(Expr *pE
203cb 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70 xpr1, Expr *pExp
203cc 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 r2, int jumpIfNu
203cd 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20 ll){. u8 aff =
203ce 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70 (char)sqlite3Exp
203cf 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32 rAffinity(pExpr2
203d0 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73 );. aff = (u8)s
203d1 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 qlite3CompareAff
203d2 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66 inity(pExpr1, af
203d3 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e f) | (u8)jumpIfN
203d4 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66 ull;. return af
203d5 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 f;.}../*.** Retu
203d6 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
203d7 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 the collation se
203d8 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75 quence that shou
203d9 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a ld be used by.**
203da 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 a binary compar
203db 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f ison operator co
203dc 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e mparing pLeft an
203dd 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 d pRight..**.**
203de 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 If the left hand
203df 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20 expression has
203e0 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 a collating sequ
203e1 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20 ence type, then
203e2 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f it is.** used. O
203e3 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c therwise the col
203e4 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
203e5 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61 for the right ha
203e6 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a nd expression.**
203e7 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65 is used, or the
203e8 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59 default (BINARY
203e9 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70 ) if neither exp
203ea 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f ression has a co
203eb 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e llating.** type.
203ec 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 .**.** Argument
203ed 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20 pRight (but not
203ee 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20 pLeft) may be a
203ef 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e null pointer. In
203f0 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 this case,.** i
203f1 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 t is not conside
203f2 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 red..*/.SQLITE_P
203f3 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a RIVATE CollSeq *
203f4 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d sqlite3BinaryCom
203f5 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50 pareCollSeq(. P
203f6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 arse *pParse, .
203f7 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20 Expr *pLeft, .
203f8 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b Expr *pRight.){
203f9 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c . CollSeq *pCol
203fa 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 l;. assert( pLe
203fb 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 ft );. if( pLef
203fc 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 t->flags & EP_Ex
203fd 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 pCollate ){.
203fe 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70 assert( pLeft->p
203ff 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c Coll );. pCol
20400 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c l = pLeft->pColl
20401 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52 ;. }else if( pR
20402 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e ight && pRight->
20403 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f flags & EP_ExpCo
20404 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 llate ){. ass
20405 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f ert( pRight->pCo
20406 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 ll );. pColl
20407 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b = pRight->pColl;
20408 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 . }else{. pC
20409 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 oll = sqlite3Exp
2040a 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c rCollSeq(pParse,
2040b 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 pLeft);. if(
2040c 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 !pColl ){.
2040d 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 pColl = sqlite3
2040e 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 ExprCollSeq(pPar
2040f 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 se, pRight);.
20410 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
20411 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pColl;.}../*.**
20412 47 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 65 Generate the ope
20413 72 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d 70 rands for a comp
20414 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f 6e arison operation
20415 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 6e . Before.** gen
20416 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 erating the code
20417 20 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 6e for each operan
20418 64 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 6e d, set the EP_An
20419 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 yAff.** flag on
2041a 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 the expression s
2041b 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 o that it will b
2041c 65 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 61 e able to used a
2041d 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 6d .** cached colum
2041e 6e 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 73 n value that has
2041f 20 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 65 previously unde
20420 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 69 rgone an.** affi
20421 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a nity change..*/.
20422 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 static void code
20423 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28 CompareOperands(
20424 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20425 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 , /* Parsing
20426 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 and code generat
20427 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
20428 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 Expr *pLeft,
20429 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f /* The left o
2042a 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 perand */. int
2042b 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f 2a *pRegLeft, /*
2042c 20 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20 Register where
2042d 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20 left operand is
2042e 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 stored */. int
2042f 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f 2a *pFreeLeft, /*
20430 20 46 72 65 65 20 74 68 69 73 20 72 65 67 69 73 Free this regis
20431 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a 2f ter when done */
20432 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c . Expr *pRight,
20433 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 /* The righ
20434 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 t operand */. i
20435 6e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 20 nt *pRegRight,
20436 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 /* Register whe
20437 72 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 re right operand
20438 20 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 is stored */.
20439 69 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 20 int *pFreeRight
2043a 20 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 20 /* Write temp
2043b 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 67 register for rig
2043c 68 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 65 ht operand there
2043d 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 */.){. while(
2043e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 pLeft->op==TK_UP
2043f 4c 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 4c LUS ) pLeft = pL
20440 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 4c eft->pLeft;. pL
20441 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 eft->flags |= EP
20442 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 _AnyAff;. *pReg
20443 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 Left = sqlite3Ex
20444 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
20445 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 4c e, pLeft, pFreeL
20446 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 eft);. while( p
20447 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 Right->op==TK_UP
20448 4c 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 70 LUS ) pRight = p
20449 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 Right->pLeft;.
2044a 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c 3d pRight->flags |=
2044b 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 EP_AnyAff;. *p
2044c 52 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 74 RegRight = sqlit
2044d 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
2044e 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 70 Parse, pRight, p
2044f 46 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f FreeRight);.}../
20450 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
20451 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 de for a compari
20452 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f son operator..*/
20453 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 .static int code
20454 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 65 Compare(. Parse
20455 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 *pParse, /*
20456 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64 The parsing (and
20457 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 code generating
20458 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 ) context */. E
20459 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 xpr *pLeft,
2045a 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65 /* The left ope
2045b 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a rand */. Expr *
2045c 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 54 pRight, /* T
2045d 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 he right operand
2045e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65 */. int opcode
2045f 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 , /* The c
20460 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 65 omparison opcode
20461 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 69 */. int in1, i
20462 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 73 nt in2, /* Regis
20463 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 72 ter holding oper
20464 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 65 ands */. int de
20465 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a st, /* J
20466 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65 ump here if true
20467 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70 . */. int jump
20468 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20 IfNull /* If
20469 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69 true, jump if ei
2046a 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
2046b 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 NULL */.){. int
2046c 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b p5;. int addr;
2046d 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a . CollSeq *p4;.
2046e 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 42 . p4 = sqlite3B
2046f 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c inaryCompareColl
20470 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 Seq(pParse, pLef
20471 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 35 t, pRight);. p5
20472 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 = binaryCompare
20473 50 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 74 P5(pLeft, pRight
20474 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
20475 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 addr = sqlite3V
20476 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65 dbeAddOp4(pParse
20477 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c ->pVdbe, opcode,
20478 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c in2, dest, in1,
20479 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
2047a 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 (voi
2047b 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 d*)p4, P4_COLLSE
2047c 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 Q);. sqlite3Vdb
2047d 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65 eChangeP5(pParse
2047e 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29 ->pVdbe, (u8)p5)
2047f 3b 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 51 ;. if( (p5 & SQ
20480 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 3d LITE_AFF_MASK)!=
20481 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 SQLITE_AFF_NONE
20482 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
20483 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 prCacheAffinityC
20484 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e hange(pParse, in
20485 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 1, 1);. sqlit
20486 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e e3ExprCacheAffin
20487 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 ityChange(pParse
20488 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 , in2, 1);. }.
20489 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a return addr;.}.
2048a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
2048b 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a EXPR_DEPTH>0./*.
2048c 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 72 ** Check that ar
2048d 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69 gument nHeight i
2048e 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 s less than or e
2048f 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 qual to the maxi
20490 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f mum.** expressio
20491 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e n depth allowed.
20492 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c If it is not, l
20493 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 eave an error me
20494 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72 ssage in.** pPar
20495 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
20496 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
20497 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 3ExprCheckHeight
20498 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
20499 69 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20 int nHeight){.
2049a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
2049b 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67 OK;. int mxHeig
2049c 68 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d ht = pParse->db-
2049d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
2049e 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d IMIT_EXPR_DEPTH]
2049f 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e ;. if( nHeight>
204a0 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 mxHeight ){.
204a1 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
204a2 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
204a3 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 "Expression tree
204a4 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d is too large (m
204a5 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29 aximum depth %d)
204a6 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20 ", mxHeight.
204a7 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
204a8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 TE_ERROR;. }.
204a9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
204aa 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 The following t
204ab 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 hree functions,
204ac 68 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20 heightOfExpr(),
204ad 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 heightOfExprList
204ae 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74 ().** and height
204af 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20 OfSelect(), are
204b0 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
204b1 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 e the maximum he
204b2 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 ight.** of any e
204b3 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72 xpression tree r
204b4 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65 eferenced by the
204b5 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 structure passe
204b6 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 d as the.** firs
204b7 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a t argument..**.*
204b8 2a 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75 * If this maximu
204b9 6d 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61 m height is grea
204ba 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 ter than the cur
204bb 72 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74 rent value point
204bc 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65 ed.** to by pnHe
204bd 69 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64 ight, the second
204be 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e parameter, then
204bf 20 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74 set *pnHeight t
204c0 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e o that.** value.
204c1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
204c2 68 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 70 heightOfExpr(Exp
204c3 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 r *p, int *pnHei
204c4 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b ght){. if( p ){
204c5 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69 . if( p->nHei
204c6 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b ght>*pnHeight ){
204c7 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74 . *pnHeight
204c8 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 = p->nHeight;.
204c9 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 }. }.}.stati
204ca 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 c void heightOfE
204cb 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 xprList(ExprList
204cc 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 *p, int *pnHeig
204cd 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a ht){. if( p ){.
204ce 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 int i;. f
204cf 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 or(i=0; i<p->nEx
204d0 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 pr; i++){.
204d1 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e heightOfExpr(p->
204d2 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65 a[i].pExpr, pnHe
204d3 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ight);. }. }
204d4 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 .}.static void h
204d5 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65 eightOfSelect(Se
204d6 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e lect *p, int *pn
204d7 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 Height){. if( p
204d8 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 ){. heightOf
204d9 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20 Expr(p->pWhere,
204da 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 pnHeight);. h
204db 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 eightOfExpr(p->p
204dc 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74 Having, pnHeight
204dd 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 );. heightOfE
204de 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 xpr(p->pLimit, p
204df 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
204e0 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f ightOfExpr(p->pO
204e1 66 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29 ffset, pnHeight)
204e2 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 ;. heightOfEx
204e3 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 prList(p->pEList
204e4 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 , pnHeight);.
204e5 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
204e6 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70 t(p->pGroupBy, p
204e7 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 nHeight);. he
204e8 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 ightOfExprList(p
204e9 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65 ->pOrderBy, pnHe
204ea 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 ight);. heigh
204eb 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72 tOfSelect(p->pPr
204ec 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a ior, pnHeight);.
204ed 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 }.}../*.** Set
204ee 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 the Expr.nHeigh
204ef 74 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68 t variable in th
204f0 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 e structure pass
204f1 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67 ed as an .** arg
204f2 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73 ument. An expres
204f3 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69 sion with no chi
204f4 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73 ldren, Expr.pLis
204f5 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53 t or .** Expr.pS
204f6 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73 elect member has
204f7 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20 a height of 1.
204f8 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 73 Any other expres
204f9 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65 sion.** has a he
204fa 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68 ight equal to th
204fb 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 e maximum height
204fc 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a of any other .*
204fd 2a 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 70 * referenced Exp
204fe 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 r plus one..*/.s
204ff 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 53 tatic void exprS
20500 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 70 etHeight(Expr *p
20501 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 ){. int nHeight
20502 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 = 0;. heightOf
20503 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26 Expr(p->pLeft, &
20504 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67 nHeight);. heig
20505 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67 htOfExpr(p->pRig
20506 68 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 ht, &nHeight);.
20507 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
20508 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 erty(p, EP_xIsSe
20509 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65 69 lect) ){. hei
2050a 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 78 ghtOfSelect(p->x
2050b 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67 .pSelect, &nHeig
2050c 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ht);. }else{.
2050d 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 heightOfExprLi
2050e 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 26 st(p->x.pList, &
2050f 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 nHeight);. }.
20510 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65 p->nHeight = nHe
20511 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a ight + 1;.}../*.
20512 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e ** Set the Expr.
20513 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65 nHeight variable
20514 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 53 using the exprS
20515 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 74 etHeight() funct
20516 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68 ion. If.** the h
20517 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72 eight is greater
20518 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75 than the maximu
20519 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73 m allowed expres
2051a 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c sion depth,.** l
2051b 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e eave an error in
2051c 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 pParse..*/.SQLI
2051d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
2051e 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 sqlite3ExprSetHe
2051f 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 ight(Parse *pPar
20520 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 se, Expr *p){.
20521 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 29 exprSetHeight(p)
20522 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 ;. sqlite3ExprC
20523 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 heckHeight(pPars
20524 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a e, p->nHeight);.
20525 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
20526 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 the maximum heig
20527 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 ht of any expres
20528 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 sion tree refere
20529 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 nced.** by the s
2052a 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 elect statement
2052b 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 passed as an arg
2052c 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ument..*/.SQLITE
2052d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
2052e 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 ite3SelectExprHe
2052f 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b ight(Select *p){
20530 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d . int nHeight =
20531 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65 0;. heightOfSe
20532 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74 lect(p, &nHeight
20533 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69 );. return nHei
20534 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 ght;.}.#else. #
20535 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 65 define exprSetHe
20536 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f ight(y).#endif /
20537 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 * SQLITE_MAX_EXP
20538 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a R_DEPTH>0 */../*
20539 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
2053a 20 69 73 20 74 68 65 20 63 6f 72 65 20 61 6c 6c is the core all
2053b 6f 63 61 74 6f 72 20 66 6f 72 20 45 78 70 72 20 ocator for Expr
2053c 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e nodes..**.** Con
2053d 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 struct a new exp
2053e 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 ression node and
2053f 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 return a pointe
20540 72 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 r to it. Memory
20541 0a 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 .** for this nod
20542 65 20 61 6e 64 20 66 6f 72 20 74 68 65 20 70 54 e and for the pT
20543 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 oken argument is
20544 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 a single alloca
20545 74 69 6f 6e 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 tion.** obtained
20546 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 44 62 4d from sqlite3DbM
20547 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65 20 63 61 alloc(). The ca
20548 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a lling function.*
20549 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 * is responsible
2054a 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 for making sure
2054b 20 74 68 65 20 6e 6f 64 65 20 65 76 65 6e 74 75 the node eventu
2054c 61 6c 6c 79 20 67 65 74 73 20 66 72 65 65 64 2e ally gets freed.
2054d 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74 .**.** If dequot
2054e 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 e is true, then
2054f 74 68 65 20 74 6f 6b 65 6e 20 28 69 66 20 69 74 the token (if it
20550 20 65 78 69 73 74 73 29 20 69 73 20 64 65 71 75 exists) is dequ
20551 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 64 65 71 75 oted..** If dequ
20552 6f 74 65 20 69 73 20 66 61 6c 73 65 2c 20 6e 6f ote is false, no
20553 20 64 65 71 75 6f 74 69 6e 67 20 69 73 20 70 65 dequoting is pe
20554 72 66 6f 72 6d 61 6e 63 65 2e 20 20 54 68 65 20 rformance. The
20555 64 65 51 75 6f 74 65 0a 2a 2a 20 70 61 72 61 6d deQuote.** param
20556 65 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 eter is ignored
20557 69 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c if pToken is NUL
20558 4c 20 6f 72 20 69 66 20 74 68 65 20 74 6f 6b 65 L or if the toke
20559 6e 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 70 n does not.** ap
2055a 70 65 61 72 20 74 6f 20 62 65 20 71 75 6f 74 65 pear to be quote
2055b 64 2e 20 20 49 66 20 74 68 65 20 71 75 6f 74 65 d. If the quote
2055c 73 20 77 65 72 65 20 6f 66 20 74 68 65 20 66 6f s were of the fo
2055d 72 6d 20 22 2e 2e 2e 22 20 28 64 6f 75 62 6c 65 rm "..." (double
2055e 2d 71 75 6f 74 65 73 29 0a 2a 2a 20 74 68 65 6e -quotes).** then
2055f 20 74 68 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 the EP_DblQuote
20560 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f 6e d flag is set on
20561 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20562 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 node..**.** Spec
20563 69 61 6c 20 63 61 73 65 3a 20 20 49 66 20 6f 70 ial case: If op
20564 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 61 6e 64 ==TK_INTEGER and
20565 20 70 54 6f 6b 65 6e 20 70 6f 69 6e 74 73 20 74 pToken points t
20566 6f 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 0a o a string that.
20567 2a 2a 20 63 61 6e 20 62 65 20 74 72 61 6e 73 6c ** can be transl
20568 61 74 65 64 20 69 6e 74 6f 20 61 20 33 32 2d 62 ated into a 32-b
20569 69 74 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e it integer, then
2056a 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 6e 6f the token is no
2056b 74 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 75 t.** stored in u
2056c 2e 7a 54 6f 6b 65 6e 2e 20 20 49 6e 73 74 65 61 .zToken. Instea
2056d 64 2c 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 d, the integer v
2056e 61 6c 75 65 73 20 69 73 20 77 72 69 74 74 65 6e alues is written
2056f 0a 2a 2a 20 69 6e 74 6f 20 75 2e 69 56 61 6c 75 .** into u.iValu
20570 65 20 61 6e 64 20 74 68 65 20 45 50 5f 49 6e 74 e and the EP_Int
20571 56 61 6c 75 65 20 66 6c 61 67 20 69 73 20 73 65 Value flag is se
20572 74 2e 20 20 4e 6f 20 65 78 74 72 61 20 73 74 6f t. No extra sto
20573 72 61 67 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 rage.** is alloc
20574 61 74 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 ated to hold the
20575 20 69 6e 74 65 67 65 72 20 74 65 78 74 20 61 6e integer text an
20576 64 20 74 68 65 20 64 65 71 75 6f 74 65 20 66 6c d the dequote fl
20577 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a ag is ignored..*
20578 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20579 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
2057a 70 72 41 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 prAlloc(. sqlit
2057b 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
2057c 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 /* Handle for
2057d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
2057e 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e Zero() (may be n
2057f 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 ull) */. int op
20580 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
20581 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 /* Expression
20582 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 opcode */. cons
20583 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c t Token *pToken,
20584 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 /* Token arg
20585 75 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 ument. Might be
20586 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 64 NULL */. int d
20587 65 71 75 6f 74 65 20 20 20 20 20 20 20 20 20 20 equote
20588 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 65 /* True to de
20589 71 75 6f 74 65 20 2a 2f 0a 29 7b 0a 20 20 45 78 quote */.){. Ex
2058a 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 pr *pNew;. int
2058b 6e 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 69 6e nExtra = 0;. in
2058c 74 20 69 56 61 6c 75 65 20 3d 20 30 3b 0a 0a 20 t iValue = 0;..
2058d 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 if( pToken ){.
2058e 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e if( op!=TK_IN
2058f 54 45 47 45 52 20 7c 7c 20 70 54 6f 6b 65 6e 2d TEGER || pToken-
20590 3e 7a 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 >z==0.
20591 7c 7c 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 || sqlite3GetInt
20592 33 32 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 26 69 32(pToken->z, &i
20593 56 61 6c 75 65 29 3d 3d 30 20 29 7b 0a 20 20 20 Value)==0 ){.
20594 20 20 20 6e 45 78 74 72 61 20 3d 20 70 54 6f 6b nExtra = pTok
20595 65 6e 2d 3e 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 en->n+1;. }.
20596 20 7d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 }. pNew = sqli
20597 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
20598 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29 db, sizeof(Expr)
20599 2b 6e 45 78 74 72 61 29 3b 0a 20 20 69 66 28 20 +nExtra);. if(
2059a 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e 65 77 pNew ){. pNew
2059b 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 ->op = (u8)op;.
2059c 20 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 pNew->iAgg =
2059d 2d 31 3b 0a 20 20 20 20 69 66 28 20 70 54 6f 6b -1;. if( pTok
2059e 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 en ){. if(
2059f 6e 45 78 74 72 61 3d 3d 30 20 29 7b 0a 20 20 20 nExtra==0 ){.
205a0 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 pNew->flags
205a1 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b |= EP_IntValue;
205a2 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 . pNew->u
205a3 2e 69 56 61 6c 75 65 20 3d 20 69 56 61 6c 75 65 .iValue = iValue
205a4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
205a5 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 int c;.
205a6 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 pNew->u.zT
205a7 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 70 oken = (char*)&p
205a8 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 New[1];.
205a9 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 75 2e 7a memcpy(pNew->u.z
205aa 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 7a Token, pToken->z
205ab 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 , pToken->n);.
205ac 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 pNew->u.zT
205ad 6f 6b 65 6e 5b 70 54 6f 6b 65 6e 2d 3e 6e 5d 20 oken[pToken->n]
205ae 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 = 0;. if(
205af 20 64 65 71 75 6f 74 65 20 26 26 20 6e 45 78 74 dequote && nExt
205b0 72 61 3e 3d 33 20 0a 20 20 20 20 20 20 20 20 20 ra>=3 .
205b1 20 20 20 20 26 26 20 28 28 63 20 3d 20 70 54 6f && ((c = pTo
205b2 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 27 ken->z[0])=='\''
205b3 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63 3d || c=='"' || c=
205b4 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29 20 ='[' || c=='`')
205b5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
205b6 69 74 65 33 44 65 71 75 6f 74 65 28 70 4e 65 77 ite3Dequote(pNew
205b7 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 ->u.zToken);.
205b8 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 22 if( c=='"
205b9 27 20 29 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 ' ) pNew->flags
205ba 7c 3d 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 3b |= EP_DblQuoted;
205bb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
205bc 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 53 51 4c }. }.#if SQL
205bd 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 ITE_MAX_EXPR_DEP
205be 54 48 3e 30 0a 20 20 20 20 70 4e 65 77 2d 3e 6e TH>0. pNew->n
205bf 48 65 69 67 68 74 20 3d 20 31 3b 0a 23 65 6e 64 Height = 1;.#end
205c0 69 66 20 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 if . }. retur
205c1 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
205c2 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
205c3 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 expression node
205c4 66 72 6f 6d 20 61 20 7a 65 72 6f 2d 74 65 72 6d from a zero-term
205c5 69 6e 61 74 65 64 20 74 6f 6b 65 6e 20 74 68 61 inated token tha
205c6 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 t has.** already
205c7 20 62 65 65 6e 20 64 65 71 75 6f 74 65 64 2e 0a been dequoted..
205c8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
205c9 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
205ca 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a xpr(. sqlite3 *
205cb 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f db, /
205cc 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c * Handle for sql
205cd 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
205ce 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 () (may be null)
205cf 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 */. int op,
205d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
205d1 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f Expression opco
205d2 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 de */. const ch
205d3 61 72 20 2a 7a 54 6f 6b 65 6e 20 20 20 20 20 20 ar *zToken
205d4 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e /* Token argumen
205d5 74 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c t. Might be NUL
205d6 4c 20 2a 2f 0a 29 7b 0a 20 20 54 6f 6b 65 6e 20 L */.){. Token
205d7 78 3b 0a 20 20 78 2e 7a 20 3d 20 7a 54 6f 6b 65 x;. x.z = zToke
205d8 6e 3b 0a 20 20 78 2e 6e 20 3d 20 7a 54 6f 6b 65 n;. x.n = zToke
205d9 6e 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n ? sqlite3Strle
205da 6e 33 30 28 7a 54 6f 6b 65 6e 29 20 3a 20 30 3b n30(zToken) : 0;
205db 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
205dc 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 6f 3ExprAlloc(db, o
205dd 70 2c 20 26 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a p, &x, 0);.}../*
205de 0a 2a 2a 20 41 74 74 61 63 68 20 73 75 62 74 72 .** Attach subtr
205df 65 65 73 20 70 4c 65 66 74 20 61 6e 64 20 70 52 ees pLeft and pR
205e0 69 67 68 74 20 74 6f 20 74 68 65 20 45 78 70 72 ight to the Expr
205e1 20 6e 6f 64 65 20 70 52 6f 6f 74 2e 0a 2a 2a 0a node pRoot..**.
205e2 2a 2a 20 49 66 20 70 52 6f 6f 74 3d 3d 4e 55 4c ** If pRoot==NUL
205e3 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 L that means tha
205e4 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 t a memory alloc
205e5 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 ation error has
205e6 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 49 6e 20 occurred..** In
205e7 74 68 61 74 20 63 61 73 65 2c 20 64 65 6c 65 74 that case, delet
205e8 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20 70 e the subtrees p
205e9 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e Left and pRight.
205ea 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
205eb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
205ec 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65 xprAttachSubtree
205ed 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 s(. sqlite3 *db
205ee 2c 0a 20 20 45 78 70 72 20 2a 70 52 6f 6f 74 2c ,. Expr *pRoot,
205ef 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 0a . Expr *pLeft,.
205f0 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 Expr *pRight.)
205f1 7b 0a 20 20 69 66 28 20 70 52 6f 6f 74 3d 3d 30 {. if( pRoot==0
205f2 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
205f3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
205f4 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 );. sqlite3E
205f5 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c xprDelete(db, pL
205f6 65 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 eft);. sqlite
205f7 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
205f8 70 52 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 pRight);. }else
205f9 7b 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 {. if( pRight
205fa 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d ){. pRoot-
205fb 3e 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 >pRight = pRight
205fc 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 ;. if( pRig
205fd 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 ht->flags & EP_E
205fe 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 xpCollate ){.
205ff 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 pRoot->flag
20600 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 s |= EP_ExpColla
20601 74 65 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 6f te;. pRoo
20602 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68 t->pColl = pRigh
20603 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 t->pColl;.
20604 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
20605 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 70 pLeft ){. p
20606 52 6f 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70 4c Root->pLeft = pL
20607 65 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 eft;. if( p
20608 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 Left->flags & EP
20609 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 _ExpCollate ){.
2060a 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c pRoot->fl
2060b 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c ags |= EP_ExpCol
2060c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 52 late;. pR
2060d 6f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 oot->pColl = pLe
2060e 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 ft->pColl;.
2060f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 65 78 70 }. }. exp
20610 72 53 65 74 48 65 69 67 68 74 28 70 52 6f 6f 74 rSetHeight(pRoot
20611 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
20612 41 6c 6c 6f 63 61 74 65 20 61 20 45 78 70 72 20 Allocate a Expr
20613 6e 6f 64 65 20 77 68 69 63 68 20 6a 6f 69 6e 73 node which joins
20614 20 61 73 20 6d 61 6e 79 20 61 73 20 74 77 6f 20 as many as two
20615 73 75 62 74 72 65 65 73 2e 0a 2a 2a 0a 2a 2a 20 subtrees..**.**
20616 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 66 20 74 One or both of t
20617 68 65 20 73 75 62 74 72 65 65 73 20 63 61 6e 20 he subtrees can
20618 62 65 20 4e 55 4c 4c 2e 20 20 52 65 74 75 72 6e be NULL. Return
20619 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
2061a 65 20 6e 65 77 0a 2a 2a 20 45 78 70 72 20 6e 6f e new.** Expr no
2061b 64 65 2e 20 20 4f 72 2c 20 69 66 20 61 6e 20 4f de. Or, if an O
2061c 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c OM error occurs,
2061d 20 73 65 74 20 70 50 61 72 73 65 2d 3e 64 62 2d set pParse->db-
2061e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2c 0a 2a >mallocFailed,.*
2061f 2a 20 66 72 65 65 20 74 68 65 20 73 75 62 74 72 * free the subtr
20620 65 65 73 20 61 6e 64 20 72 65 74 75 72 6e 20 4e ees and return N
20621 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ULL..*/.SQLITE_P
20622 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
20623 69 74 65 33 50 45 78 70 72 28 0a 20 20 50 61 72 ite3PExpr(. Par
20624 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
20625 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
20626 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 context */. int
20627 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 op,
20628 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 /* Expressi
20629 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45 on opcode */. E
2062a 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 xpr *pLeft,
2062b 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f /* Left o
2062c 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 perand */. Expr
2062d 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20 20 *pRight,
2062e 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 /* Right ope
2062f 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 rand */. const
20630 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 Token *pToken
20631 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
20632 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 ken */.){. Expr
20633 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 *p = sqlite3Exp
20634 72 41 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e 64 rAlloc(pParse->d
20635 62 2c 20 6f 70 2c 20 70 54 6f 6b 65 6e 2c 20 31 b, op, pToken, 1
20636 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 );. sqlite3Expr
20637 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 70 AttachSubtrees(p
20638 50 61 72 73 65 2d 3e 64 62 2c 20 70 2c 20 70 4c Parse->db, p, pL
20639 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 eft, pRight);.
2063a 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
2063b 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72 ** Join two expr
2063c 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e essions using an
2063d 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20 AND operator.
2063e 49 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73 If either expres
2063f 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c sion is.** NULL,
20640 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 then just retur
20641 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 n the other expr
20642 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 ession..*/.SQLIT
20643 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
20644 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 sqlite3ExprAnd(s
20645 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 qlite3 *db, Expr
20646 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70 *pLeft, Expr *p
20647 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c Right){. if( pL
20648 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 eft==0 ){. re
20649 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d turn pRight;. }
2064a 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d else if( pRight=
2064b 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
2064c 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b pLeft;. }else{
2064d 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 20 . Expr *pNew
2064e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c = sqlite3ExprAll
2064f 6f 63 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 30 oc(db, TK_AND, 0
20650 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 0);. sqlite
20651 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72 3ExprAttachSubtr
20652 65 65 73 28 64 62 2c 20 70 4e 65 77 2c 20 70 4c ees(db, pNew, pL
20653 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 eft, pRight);.
20654 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 20 return pNew;.
20655 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 }.}../*.** Cons
20656 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 truct a new expr
20657 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 ession node for
20658 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 a function with
20659 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75 multiple.** argu
2065a 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ments..*/.SQLITE
2065b 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 _PRIVATE Expr *s
2065c 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 qlite3ExprFuncti
2065d 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 on(Parse *pParse
2065e 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 , ExprList *pLis
2065f 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e t, Token *pToken
20660 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b ){. Expr *pNew;
20661 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
20662 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 pParse->db;. a
20663 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b ssert( pToken );
20664 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
20665 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54 3ExprAlloc(db, T
20666 4b 5f 46 55 4e 43 54 49 4f 4e 2c 20 70 54 6f 6b K_FUNCTION, pTok
20667 65 6e 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 4e en, 1);. if( pN
20668 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c ew==0 ){. sql
20669 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 ite3ExprListDele
2066a 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20 2f te(db, pList); /
2066b 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20 6c * Avoid memory l
2066c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 eak when malloc
2066d 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74 fails */. ret
2066e 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 urn 0;. }. pNe
2066f 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 w->x.pList = pLi
20670 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 st;. assert( !E
20671 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20672 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 New, EP_xIsSelec
20673 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 t) );. sqlite3E
20674 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 xprSetHeight(pPa
20675 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72 65 rse, pNew);. re
20676 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a turn pNew;.}../*
20677 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 72 .** Assign a var
20678 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20 iable number to
20679 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 an expression th
2067a 61 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 6c at encodes a wil
2067b 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20 dcard.** in the
2067c 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 original SQL sta
2067d 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 tement. .**.**
2067e 57 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69 73 Wildcards consis
2067f 74 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c 65 ting of a single
20680 20 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e 65 "?" are assigne
20681 64 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 65 d the next seque
20682 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c ntial.** variabl
20683 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 e number..**.**
20684 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 Wildcards of the
20685 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 form "?nnn" are
20686 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 75 assigned the nu
20687 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20 mber "nnn". We
20688 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e make.** sure "nn
20689 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65 n" is not too be
2068a 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e 69 to avoid a deni
2068b 61 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61 74 al of service at
2068c 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65 tack when.** the
2068d 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 SQL statement c
2068e 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78 74 omes from an ext
2068f 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a ernal source..**
20690 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66 .** Wildcards of
20691 20 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22 the form ":aaa"
20692 2c 20 22 40 61 61 61 22 2c 20 6f 72 20 22 24 61 , "@aaa", or "$a
20693 61 61 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 aa" are assigned
20694 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 the same number
20695 0a 2a 2a 20 61 73 20 74 68 65 20 70 72 65 76 69 .** as the previ
20696 6f 75 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 ous instance of
20697 74 68 65 20 73 61 6d 65 20 77 69 6c 64 63 61 72 the same wildcar
20698 64 2e 20 20 4f 72 20 69 66 20 74 68 69 73 20 69 d. Or if this i
20699 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 s the first.** i
2069a 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 77 nstance of the w
2069b 69 6c 64 63 61 72 64 2c 20 74 68 65 20 6e 65 78 ildcard, the nex
2069c 74 20 73 65 71 75 65 6e 69 61 6c 20 76 61 72 69 t sequenial vari
2069d 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a 2a able number is.*
2069e 2a 20 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 * assigned..*/.S
2069f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
206a0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 id sqlite3ExprAs
206a1 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 signVarNumber(Pa
206a2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
206a3 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 73 71 6c r *pExpr){. sql
206a4 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
206a5 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 e->db;. const c
206a6 68 61 72 20 2a 7a 3b 0a 0a 20 20 69 66 28 20 70 har *z;.. if( p
206a7 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e Expr==0 ) return
206a8 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 ;. assert( !Exp
206a9 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
206aa 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c pExpr, EP_IntVal
206ab 75 65 7c 45 50 5f 52 65 64 75 63 65 64 7c 45 50 ue|EP_Reduced|EP
206ac 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 _TokenOnly) );.
206ad 20 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 z = pExpr->u.zT
206ae 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 oken;. assert(
206af 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 z!=0 );. assert
206b0 28 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 69 ( z[0]!=0 );. i
206b1 66 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 f( z[1]==0 ){.
206b2 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 /* Wildcard of
206b3 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 20 the form "?".
206b4 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 20 Assign the next
206b5 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 variable number
206b6 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a */. assert( z
206b7 5b 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20 20 20 20 [0]=='?' );.
206b8 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
206b9 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b 0a ++pParse->nVar;.
206ba 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 30 5d }else if( z[0]
206bb 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20 =='?' ){. /*
206bc 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 Wildcard of the
206bd 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f form "?nnn". Co
206be 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61 nvert "nnn" to a
206bf 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a 20 20 n integer and.
206c0 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73 20 74 ** use it as t
206c1 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 he variable numb
206c2 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b er */. int i;
206c3 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 . pExpr->iTab
206c4 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28 28 63 le = i = atoi((c
206c5 68 61 72 2a 29 26 7a 5b 31 5d 29 3b 0a 20 20 20 har*)&z[1]);.
206c6 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 30 20 testcase( i==0
206c7 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
206c8 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 73 i==1 );. tes
206c9 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c tcase( i==db->aL
206ca 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
206cb 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 T_VARIABLE_NUMBE
206cc 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 65 73 74 R]-1 );. test
206cd 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 case( i==db->aLi
206ce 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
206cf 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 _VARIABLE_NUMBER
206d0 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 31 ] );. if( i<1
206d1 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d 69 74 || i>db->aLimit
206d2 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 [SQLITE_LIMIT_VA
206d3 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 RIABLE_NUMBER] )
206d4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
206d5 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
206d6 22 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 "variable number
206d7 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e must be between
206d8 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c 0a 20 20 ?1 and ?%d",.
206d9 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d db->aLim
206da 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
206db 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d VARIABLE_NUMBER]
206dc 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
206dd 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 i>pParse->nVar
206de 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d ){. pParse-
206df 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 20 20 7d >nVar = i;. }
206e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
206e1 20 57 69 6c 64 63 61 72 64 73 20 6c 69 6b 65 20 Wildcards like
206e2 22 3a 61 61 61 22 2c 20 22 24 61 61 61 22 20 6f ":aaa", "$aaa" o
206e3 72 20 22 40 61 61 61 22 2e 20 20 52 65 75 73 65 r "@aaa". Reuse
206e4 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 the same variab
206e5 6c 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 le. ** number
206e6 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 61 70 as the prior ap
206e7 70 65 61 72 61 6e 63 65 20 6f 66 20 74 68 65 20 pearance of the
206e8 73 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69 66 same name, or if
206e9 20 74 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a 2a the name. **
206ea 20 68 61 73 20 6e 65 76 65 72 20 61 70 70 65 61 has never appea
206eb 72 65 64 20 62 65 66 6f 72 65 2c 20 72 65 75 73 red before, reus
206ec 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 e the same varia
206ed 62 6c 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a ble number. *
206ee 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 /. int i;.
206ef 20 75 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 u32 n;. n =
206f0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
206f1 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b z);. for(i=0;
206f2 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 i<pParse->nVarE
206f3 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
206f4 20 45 78 70 72 20 2a 70 45 20 3d 20 70 50 61 72 Expr *pE = pPar
206f5 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 69 5d se->apVarExpr[i]
206f6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
206f7 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 pE!=0 );. i
206f8 66 28 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 75 2e f( memcmp(pE->u.
206f9 7a 54 6f 6b 65 6e 2c 20 7a 2c 20 6e 29 3d 3d 30 zToken, z, n)==0
206fa 20 26 26 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e && pE->u.zToken
206fb 5b 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 [n]==0 ){.
206fc 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
206fd 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 = pE->iTable;.
206fe 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
206ff 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
20700 66 28 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 f( i>=pParse->nV
20701 61 72 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 arExpr ){.
20702 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
20703 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b 0a ++pParse->nVar;.
20704 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 if( pParse
20705 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 50 61 72 ->nVarExpr>=pPar
20706 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f se->nVarExprAllo
20707 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 c-1 ){. p
20708 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 Parse->nVarExprA
20709 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 65 2d 3e lloc += pParse->
2070a 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 20 nVarExprAlloc +
2070b 31 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 10;. pPar
2070c 73 65 2d 3e 61 70 56 61 72 45 78 70 72 20 3d 0a se->apVarExpr =.
2070d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
2070e 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 te3DbReallocOrFr
2070f 65 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 ee(.
20710 20 20 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 db,.
20711 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 pParse->apVa
20712 72 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 rExpr,.
20713 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 pParse->nVa
20714 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65 6f rExprAlloc*sizeo
20715 66 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 f(pParse->apVarE
20716 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 xpr[0]).
20717 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 );. }.
20718 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 if( !db->ma
20719 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
2071a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
2071b 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 21 arse->apVarExpr!
2071c 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 50 =0 );. pP
2071d 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b arse->apVarExpr[
2071e 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 pParse->nVarExpr
2071f 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 ++] = pExpr;.
20720 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 0a }. }. } .
20721 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e if( !pParse->n
20722 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e 6e Err && pParse->n
20723 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 Var>db->aLimit[S
20724 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
20725 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a ABLE_NUMBER] ){.
20726 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
20727 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f Msg(pParse, "too
20728 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69 61 62 many SQL variab
20729 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a les");. }.}../*
2072a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 65 78 70 .** Clear an exp
2072b 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72 ression structur
2072c 65 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69 e without deleti
2072d 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72 65 ng the structure
2072e 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 75 62 73 itself..** Subs
2072f 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c 65 tructure is dele
20730 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ted..*/.SQLITE_P
20731 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20732 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c te3ExprClear(sql
20733 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a ite3 *db, Expr *
20734 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 21 p){. assert( p!
20735 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 45 78 70 =0 );. if( !Exp
20736 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
20737 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 p, EP_TokenOnly)
20738 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
20739 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d xprDelete(db, p-
2073a 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c >pLeft);. sql
2073b 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
2073c 62 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 b, p->pRight);.
2073d 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50 if( !ExprHasP
2073e 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 roperty(p, EP_Re
2073f 64 75 63 65 64 29 20 26 26 20 28 70 2d 3e 66 6c duced) && (p->fl
20740 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c 6f ags2 & EP2_Mallo
20741 63 65 64 54 6f 6b 65 6e 29 21 3d 30 20 29 7b 0a cedToken)!=0 ){.
20742 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
20743 72 65 65 28 64 62 2c 20 70 2d 3e 75 2e 7a 54 6f ree(db, p->u.zTo
20744 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ken);. }.
20745 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
20746 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c rty(p, EP_xIsSel
20747 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 ect) ){. sq
20748 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 lite3SelectDelet
20749 65 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 e(db, p->x.pSele
2074a 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ct);. }else{.
2074b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
2074c 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 rListDelete(db,
2074d 70 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20 p->x.pList);.
2074e 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
2074f 52 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 Recursively dele
20750 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e te an expression
20751 20 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tree..*/.SQLITE
20752 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
20753 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
20754 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 sqlite3 *db, Exp
20755 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d r *p){. if( p==
20756 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 0 ) return;. sq
20757 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64 lite3ExprClear(d
20758 62 2c 20 70 29 3b 0a 20 20 69 66 28 20 21 45 78 b, p);. if( !Ex
20759 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
2075a 20 45 50 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 EP_Static) ){.
2075b 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
2075c 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a (db, p);. }.}..
2075d 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
2075e 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
2075f 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 allocated for t
20760 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74 he expression st
20761 72 75 63 74 75 72 65 20 0a 2a 2a 20 70 61 73 73 ructure .** pass
20762 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
20763 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 69 argument. This i
20764 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66 20 s always one of
20765 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 0a 2a EXPR_FULLSIZE,.*
20766 2a 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 * EXPR_REDUCEDSI
20767 5a 45 20 6f 72 20 45 58 50 52 5f 54 4f 4b 45 4e ZE or EXPR_TOKEN
20768 4f 4e 4c 59 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 ONLYSIZE..*/.sta
20769 74 69 63 20 69 6e 74 20 65 78 70 72 53 74 72 75 tic int exprStru
2076a 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 29 7b ctSize(Expr *p){
2076b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 . if( ExprHasPr
2076c 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b operty(p, EP_Tok
2076d 65 6e 4f 6e 6c 79 29 20 29 20 72 65 74 75 72 6e enOnly) ) return
2076e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 EXPR_TOKENONLYS
2076f 49 5a 45 3b 0a 20 20 69 66 28 20 45 78 70 72 48 IZE;. if( ExprH
20770 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 asProperty(p, EP
20771 5f 52 65 64 75 63 65 64 29 20 29 20 72 65 74 75 _Reduced) ) retu
20772 72 6e 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 rn EXPR_REDUCEDS
20773 49 5a 45 3b 0a 20 20 72 65 74 75 72 6e 20 45 58 IZE;. return EX
20774 50 52 5f 46 55 4c 4c 53 49 5a 45 3b 0a 7d 0a 0a PR_FULLSIZE;.}..
20775 2f 2a 0a 2a 2a 20 54 68 65 20 64 75 70 65 64 45 /*.** The dupedE
20776 78 70 72 2a 53 69 7a 65 28 29 20 72 6f 75 74 69 xpr*Size() routi
20777 6e 65 73 20 65 61 63 68 20 72 65 74 75 72 6e 20 nes each return
20778 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
20779 74 65 73 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 tes required.**
2077a 74 6f 20 73 74 6f 72 65 20 61 20 63 6f 70 79 20 to store a copy
2077b 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e of an expression
2077c 20 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20 74 or expression t
2077d 72 65 65 2e 20 20 54 68 65 79 20 64 69 66 66 65 ree. They diffe
2077e 72 20 69 6e 0a 2a 2a 20 68 6f 77 20 6d 75 63 68 r in.** how much
2077f 20 6f 66 20 74 68 65 20 74 72 65 65 20 69 73 20 of the tree is
20780 6d 65 61 73 75 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 measured..**.**
20781 20 20 20 20 64 75 70 65 64 45 78 70 72 53 74 72 dupedExprStr
20782 75 63 74 53 69 7a 65 28 29 20 20 20 20 20 53 69 uctSize() Si
20783 7a 65 20 6f 66 20 6f 6e 6c 79 20 74 68 65 20 45 ze of only the E
20784 78 70 72 20 73 74 72 75 63 74 75 72 65 20 0a 2a xpr structure .*
20785 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 72 4e * dupedExprN
20786 6f 64 65 53 69 7a 65 28 29 20 20 20 20 20 20 20 odeSize()
20787 53 69 7a 65 20 6f 66 20 45 78 70 72 20 2b 20 73 Size of Expr + s
20788 70 61 63 65 20 66 6f 72 20 74 6f 6b 65 6e 0a 2a pace for token.*
20789 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 72 53 * dupedExprS
2078a 69 7a 65 28 29 20 20 20 20 20 20 20 20 20 20 20 ize()
2078b 45 78 70 72 20 2b 20 74 6f 6b 65 6e 20 2b 20 73 Expr + token + s
2078c 75 62 74 72 65 65 20 63 6f 6d 70 6f 6e 65 6e 74 ubtree component
2078d 73 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.**.***********
2078e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
2078f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20791 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20792 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 75 70 65 64 .**.** The duped
20793 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 29 ExprStructSize()
20794 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
20795 73 20 74 77 6f 20 76 61 6c 75 65 73 20 4f 52 2d s two values OR-
20796 65 64 20 74 6f 67 65 74 68 65 72 3a 20 20 0a 2a ed together: .*
20797 2a 20 28 31 29 20 74 68 65 20 73 70 61 63 65 20 * (1) the space
20798 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 20 63 required for a c
20799 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 20 opy of the Expr
2079a 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 61 structure only a
2079b 6e 64 20 0a 2a 2a 20 28 32 29 20 74 68 65 20 45 nd .** (2) the E
2079c 50 5f 78 78 78 20 66 6c 61 67 73 20 74 68 61 74 P_xxx flags that
2079d 20 69 6e 64 69 63 61 74 65 20 77 68 61 74 20 74 indicate what t
2079e 68 65 20 73 74 72 75 63 74 75 72 65 20 73 69 7a he structure siz
2079f 65 20 73 68 6f 75 6c 64 20 62 65 2e 0a 2a 2a 20 e should be..**
207a0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
207a1 73 20 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 s is always one
207a2 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 45 of:.**.** E
207a3 58 50 52 5f 46 55 4c 4c 53 49 5a 45 0a 2a 2a 20 XPR_FULLSIZE.**
207a4 20 20 20 20 20 45 58 50 52 5f 52 45 44 55 43 45 EXPR_REDUCE
207a5 44 53 49 5a 45 20 20 20 7c 20 45 50 5f 52 65 64 DSIZE | EP_Red
207a6 75 63 65 64 0a 2a 2a 20 20 20 20 20 20 45 58 50 uced.** EXP
207a7 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 R_TOKENONLYSIZE
207a8 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 0a 2a | EP_TokenOnly.*
207a9 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 *.** The size of
207aa 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63 the structure c
207ab 61 6e 20 62 65 20 66 6f 75 6e 64 20 62 79 20 6d an be found by m
207ac 61 73 6b 69 6e 67 20 74 68 65 20 72 65 74 75 72 asking the retur
207ad 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 n value.** of th
207ae 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 is routine with
207af 30 78 66 66 66 2e 20 20 54 68 65 20 66 6c 61 67 0xfff. The flag
207b0 73 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 62 s can be found b
207b1 79 20 6d 61 73 6b 69 6e 67 20 74 68 65 0a 2a 2a y masking the.**
207b2 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69 return value wi
207b3 74 68 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50 th EP_Reduced|EP
207b4 5f 54 6f 6b 65 6e 4f 6e 6c 79 2e 0a 2a 2a 0a 2a _TokenOnly..**.*
207b5 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 74 68 * Note that with
207b6 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 50 5f flags==EXPRDUP_
207b7 52 45 44 55 43 45 2c 20 74 68 69 73 20 72 6f 75 REDUCE, this rou
207b8 74 69 6e 65 73 20 77 6f 72 6b 73 20 6f 6e 20 66 tines works on f
207b9 75 6c 6c 2d 73 69 7a 65 0a 2a 2a 20 28 75 6e 72 ull-size.** (unr
207ba 65 64 75 63 65 64 29 20 45 78 70 72 20 6f 62 6a educed) Expr obj
207bb 65 63 74 73 20 61 73 20 74 68 65 79 20 6f 72 20 ects as they or
207bc 6f 72 69 67 69 6e 61 6c 6c 79 20 63 6f 6e 73 74 originally const
207bd 72 75 63 74 65 64 20 62 79 20 74 68 65 20 70 61 ructed by the pa
207be 72 73 65 72 2e 0a 2a 2a 20 44 75 72 69 6e 67 20 rser..** During
207bf 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 61 6c 79 expression analy
207c0 73 69 73 2c 20 65 78 74 72 61 20 69 6e 66 6f 72 sis, extra infor
207c1 6d 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75 74 mation is comput
207c2 65 64 20 61 6e 64 20 6d 6f 76 65 64 20 69 6e 74 ed and moved int
207c3 6f 0a 2a 2a 20 6c 61 74 65 72 20 70 61 72 74 73 o.** later parts
207c4 20 6f 66 20 74 65 68 20 45 78 70 72 20 6f 62 6a of teh Expr obj
207c5 65 63 74 20 61 6e 64 20 74 68 61 74 20 65 78 74 ect and that ext
207c6 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d ra information m
207c7 69 67 68 74 20 67 65 74 20 63 68 6f 70 70 65 64 ight get chopped
207c8 0a 2a 2a 20 6f 66 66 20 69 66 20 74 68 65 20 65 .** off if the e
207c9 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 64 xpression is red
207ca 75 63 65 64 2e 20 20 4e 6f 74 65 20 61 6c 73 6f uced. Note also
207cb 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f that it does no
207cc 74 20 77 6f 72 6b 20 74 6f 0a 2a 2a 20 6d 61 6b t work to.** mak
207cd 65 20 61 20 45 58 50 52 44 55 50 5f 52 45 44 55 e a EXPRDUP_REDU
207ce 43 45 20 63 6f 70 79 20 6f 66 20 61 20 72 65 64 CE copy of a red
207cf 75 63 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e uced expression.
207d0 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 6c 65 67 It is only leg
207d1 61 6c 0a 2a 2a 20 74 6f 20 72 65 64 75 63 65 20 al.** to reduce
207d2 61 20 70 72 69 73 74 69 6e 65 20 65 78 70 72 65 a pristine expre
207d3 73 73 69 6f 6e 20 74 72 65 65 20 66 72 6f 6d 20 ssion tree from
207d4 74 68 65 20 70 61 72 73 65 72 2e 20 20 54 68 65 the parser. The
207d5 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
207d6 2a 2a 20 6f 66 20 64 75 70 65 64 45 78 70 72 53 ** of dupedExprS
207d7 74 72 75 63 74 53 69 7a 65 28 29 20 63 6f 6e 74 tructSize() cont
207d8 61 69 6e 20 6d 75 6c 74 69 70 6c 65 20 61 73 73 ain multiple ass
207d9 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
207da 20 74 68 61 74 20 61 74 74 65 6d 70 74 0a 2a 2a that attempt.**
207db 20 74 6f 20 65 6e 66 6f 72 63 65 20 74 68 69 73 to enforce this
207dc 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a constraint..*/.
207dd 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 64 static int duped
207de 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 45 ExprStructSize(E
207df 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 xpr *p, int flag
207e0 73 29 7b 0a 20 20 69 6e 74 20 6e 53 69 7a 65 3b s){. int nSize;
207e1 0a 20 20 61 73 73 65 72 74 28 20 66 6c 61 67 73 . assert( flags
207e2 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 ==EXPRDUP_REDUCE
207e3 20 7c 7c 20 66 6c 61 67 73 3d 3d 30 20 29 3b 20 || flags==0 );
207e4 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 66 6c 61 67 /* Only one flag
207e5 20 76 61 6c 75 65 20 61 6c 6c 6f 77 65 64 20 2a value allowed *
207e6 2f 0a 20 20 69 66 28 20 30 3d 3d 28 66 6c 61 67 /. if( 0==(flag
207e7 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 s&EXPRDUP_REDUCE
207e8 29 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d ) ){. nSize =
207e9 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 3b 0a EXPR_FULLSIZE;.
207ea 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
207eb 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 ert( !ExprHasAny
207ec 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 Property(p, EP_T
207ed 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 okenOnly|EP_Redu
207ee 63 65 64 29 20 29 3b 0a 20 20 20 20 61 73 73 65 ced) );. asse
207ef 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
207f0 65 72 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d 4a erty(p, EP_FromJ
207f1 6f 69 6e 29 20 29 3b 20 0a 20 20 20 20 61 73 73 oin) ); . ass
207f2 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 32 20 ert( (p->flags2
207f3 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f & EP2_MallocedTo
207f4 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 ken)==0 );. a
207f5 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 ssert( (p->flags
207f6 32 20 26 20 45 50 32 5f 49 72 72 65 64 75 63 69 2 & EP2_Irreduci
207f7 62 6c 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 ble)==0 );. i
207f8 66 28 20 70 2d 3e 70 4c 65 66 74 20 7c 7c 20 70 f( p->pLeft || p
207f9 2d 3e 70 52 69 67 68 74 20 7c 7c 20 70 2d 3e 70 ->pRight || p->p
207fa 43 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 2e 70 4c 69 Coll || p->x.pLi
207fb 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a st ){. nSiz
207fc 65 20 3d 20 45 58 50 52 5f 52 45 44 55 43 45 44 e = EXPR_REDUCED
207fd 53 49 5a 45 20 7c 20 45 50 5f 52 65 64 75 63 65 SIZE | EP_Reduce
207fe 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 d;. }else{.
207ff 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 nSize = EXPR
20800 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 7c _TOKENONLYSIZE |
20801 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 3b 0a 20 EP_TokenOnly;.
20802 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
20803 6e 20 6e 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a n nSize;.}../*.*
20804 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
20805 72 65 74 75 72 6e 73 20 74 68 65 20 73 70 61 63 returns the spac
20806 65 20 69 6e 20 62 79 74 65 73 20 72 65 71 75 69 e in bytes requi
20807 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 red to store the
20808 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74 68 65 copy .** of the
20809 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20 Expr structure
2080a 61 6e 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 and a copy of th
2080b 65 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 6e 20 e Expr.u.zToken
2080c 73 74 72 69 6e 67 20 28 69 66 20 74 68 61 74 0a string (if that.
2080d 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 64 65 66 ** string is def
2080e 69 6e 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 ined.).*/.static
2080f 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 4e 6f int dupedExprNo
20810 64 65 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 deSize(Expr *p,
20811 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e int flags){. in
20812 74 20 6e 42 79 74 65 20 3d 20 64 75 70 65 64 45 t nByte = dupedE
20813 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c xprStructSize(p,
20814 20 66 6c 61 67 73 29 20 26 20 30 78 66 66 66 3b flags) & 0xfff;
20815 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50 . if( !ExprHasP
20816 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 6e roperty(p, EP_In
20817 74 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e tValue) && p->u.
20818 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 6e 42 zToken ){. nB
20819 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 yte += sqlite3St
2081a 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b rlen30(p->u.zTok
2081b 65 6e 29 2b 31 3b 0a 20 20 7d 0a 20 20 72 65 74 en)+1;. }. ret
2081c 75 72 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 urn ROUND8(nByte
2081d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
2081e 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
2081f 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 bytes required
20820 74 6f 20 63 72 65 61 74 65 20 61 20 64 75 70 6c to create a dupl
20821 69 63 61 74 65 20 6f 66 20 74 68 65 20 0a 2a 2a icate of the .**
20822 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73 73 expression pass
20823 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
20824 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 73 65 argument. The se
20825 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
20826 20 61 0a 2a 2a 20 6d 61 73 6b 20 63 6f 6e 74 61 a.** mask conta
20827 69 6e 69 6e 67 20 45 58 50 52 44 55 50 5f 58 58 ining EXPRDUP_XX
20828 58 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 X flags..**.** T
20829 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
2082a 64 20 69 6e 63 6c 75 64 65 73 20 73 70 61 63 65 d includes space
2082b 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70 to create a cop
2082c 79 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 y of the Expr st
2082d 72 75 63 74 0a 2a 2a 20 69 74 73 65 6c 66 20 61 ruct.** itself a
2082e 6e 64 20 74 68 65 20 62 75 66 66 65 72 20 72 65 nd the buffer re
2082f 66 65 72 72 65 64 20 74 6f 20 62 79 20 45 78 70 ferred to by Exp
20830 72 2e 75 2e 7a 54 6f 6b 65 6e 2c 20 69 66 20 61 r.u.zToken, if a
20831 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ny..**.** If the
20832 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 EXPRDUP_REDUCE
20833 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
20834 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c n the return val
20835 75 65 20 69 6e 63 6c 75 64 65 73 20 0a 2a 2a 20 ue includes .**
20836 73 70 61 63 65 20 74 6f 20 64 75 70 6c 69 63 61 space to duplica
20837 74 65 20 61 6c 6c 20 45 78 70 72 20 6e 6f 64 65 te all Expr node
20838 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 66 6f s in the tree fo
20839 72 6d 65 64 20 62 79 20 45 78 70 72 2e 70 4c 65 rmed by Expr.pLe
2083a 66 74 20 0a 2a 2a 20 61 6e 64 20 45 78 70 72 2e ft .** and Expr.
2083b 70 52 69 67 68 74 20 76 61 72 69 61 62 6c 65 73 pRight variables
2083c 20 28 62 75 74 20 6e 6f 74 20 66 6f 72 20 61 6e (but not for an
2083d 79 20 73 74 72 75 63 74 75 72 65 73 20 70 6f 69 y structures poi
2083e 6e 74 65 64 20 74 6f 20 6f 72 20 0a 2a 2a 20 64 nted to or .** d
2083f 65 73 63 65 6e 64 65 64 20 66 72 6f 6d 20 74 68 escended from th
20840 65 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f e Expr.x.pList o
20841 72 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 r Expr.x.pSelect
20842 20 76 61 72 69 61 62 6c 65 73 29 2e 0a 2a 2f 0a variables)..*/.
20843 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 64 static int duped
20844 45 78 70 72 53 69 7a 65 28 45 78 70 72 20 2a 70 ExprSize(Expr *p
20845 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
20846 69 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 int nByte = 0;.
20847 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 42 if( p ){. nB
20848 79 74 65 20 3d 20 64 75 70 65 64 45 78 70 72 4e yte = dupedExprN
20849 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 odeSize(p, flags
2084a 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 );. if( flags
2084b 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 &EXPRDUP_REDUCE
2084c 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 2b ){. nByte +
2084d 3d 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 = dupedExprSize(
2084e 70 2d 3e 70 4c 65 66 74 2c 20 66 6c 61 67 73 29 p->pLeft, flags)
2084f 20 2b 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 + dupedExprSize
20850 28 70 2d 3e 70 52 69 67 68 74 2c 20 66 6c 61 67 (p->pRight, flag
20851 73 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 s);. }. }.
20852 72 65 74 75 72 6e 20 6e 42 79 74 65 3b 0a 7d 0a return nByte;.}.
20853 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
20854 74 69 6f 6e 20 69 73 20 73 69 6d 69 6c 61 72 20 tion is similar
20855 74 6f 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 to sqlite3ExprDu
20856 70 28 29 2c 20 65 78 63 65 70 74 20 74 68 61 74 p(), except that
20857 20 69 66 20 70 7a 42 75 66 66 65 72 20 0a 2a 2a if pzBuffer .**
20858 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 is not NULL the
20859 6e 20 2a 70 7a 42 75 66 66 65 72 20 69 73 20 61 n *pzBuffer is a
2085a 73 73 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 ssumed to point
2085b 74 6f 20 61 20 62 75 66 66 65 72 20 6c 61 72 67 to a buffer larg
2085c 65 20 65 6e 6f 75 67 68 20 0a 2a 2a 20 74 6f 20 e enough .** to
2085d 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20 6f store the copy o
2085e 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70 2c 20 f expression p,
2085f 74 68 65 20 63 6f 70 69 65 73 20 6f 66 20 70 2d the copies of p-
20860 3e 75 2e 7a 54 6f 6b 65 6e 0a 2a 2a 20 28 69 66 >u.zToken.** (if
20861 20 61 70 70 6c 69 63 61 62 6c 65 29 2c 20 61 6e applicable), an
20862 64 20 74 68 65 20 63 6f 70 69 65 73 20 6f 66 20 d the copies of
20863 74 68 65 20 70 2d 3e 70 4c 65 66 74 20 61 6e 64 the p->pLeft and
20864 20 70 2d 3e 70 52 69 67 68 74 20 65 78 70 72 65 p->pRight expre
20865 73 73 69 6f 6e 73 2c 0a 2a 2a 20 69 66 20 61 6e ssions,.** if an
20866 79 2e 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e y. Before return
20867 69 6e 67 2c 20 2a 70 7a 42 75 66 66 65 72 20 69 ing, *pzBuffer i
20868 73 20 73 65 74 20 74 6f 20 74 68 65 20 66 69 72 s set to the fir
20869 73 74 20 62 79 74 65 20 70 61 73 73 65 64 20 74 st byte passed t
2086a 68 65 0a 2a 2a 20 70 6f 72 74 69 6f 6e 20 6f 66 he.** portion of
2086b 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 70 69 the buffer copi
2086c 65 64 20 69 6e 74 6f 20 62 79 20 74 68 69 73 20 ed into by this
2086d 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
2086e 74 69 63 20 45 78 70 72 20 2a 65 78 70 72 44 75 tic Expr *exprDu
2086f 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 p(sqlite3 *db, E
20870 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 xpr *p, int flag
20871 73 2c 20 75 38 20 2a 2a 70 7a 42 75 66 66 65 72 s, u8 **pzBuffer
20872 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 20 ){. Expr *pNew
20873 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
20874 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c /* Val
20875 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a ue to return */.
20876 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63 if( p ){. c
20877 6f 6e 73 74 20 69 6e 74 20 69 73 52 65 64 75 63 onst int isReduc
20878 65 64 20 3d 20 28 66 6c 61 67 73 26 45 58 50 52 ed = (flags&EXPR
20879 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 DUP_REDUCE);.
2087a 20 75 38 20 2a 7a 41 6c 6c 6f 63 3b 0a 20 20 20 u8 *zAlloc;.
2087b 20 75 33 32 20 73 74 61 74 69 63 46 6c 61 67 20 u32 staticFlag
2087c 3d 20 30 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 = 0;.. assert
2087d 28 20 70 7a 42 75 66 66 65 72 3d 3d 30 20 7c 7c ( pzBuffer==0 ||
2087e 20 69 73 52 65 64 75 63 65 64 20 29 3b 0a 0a 20 isReduced );..
2087f 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 /* Figure out
20880 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 where to write
20881 74 68 65 20 6e 65 77 20 45 78 70 72 20 73 74 72 the new Expr str
20882 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 20 20 69 ucture. */. i
20883 66 28 20 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 f( pzBuffer ){.
20884 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 2a 70 zAlloc = *p
20885 7a 42 75 66 66 65 72 3b 0a 20 20 20 20 20 20 73 zBuffer;. s
20886 74 61 74 69 63 46 6c 61 67 20 3d 20 45 50 5f 53 taticFlag = EP_S
20887 74 61 74 69 63 3b 0a 20 20 20 20 7d 65 6c 73 65 tatic;. }else
20888 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d {. zAlloc =
20889 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
2088a 52 61 77 28 64 62 2c 20 64 75 70 65 64 45 78 70 Raw(db, dupedExp
2088b 72 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 29 rSize(p, flags))
2088c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 ;. }. pNew
2088d 20 3d 20 28 45 78 70 72 20 2a 29 7a 41 6c 6c 6f = (Expr *)zAllo
2088e 63 3b 0a 0a 20 20 20 20 69 66 28 20 70 4e 65 77 c;.. if( pNew
2088f 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 ){. /* Set
20890 20 6e 4e 65 77 53 69 7a 65 20 74 6f 20 74 68 65 nNewSize to the
20891 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 65 64 20 size allocated
20892 66 6f 72 20 74 68 65 20 73 74 72 75 63 74 75 72 for the structur
20893 65 20 70 6f 69 6e 74 65 64 20 74 6f 0a 20 20 20 e pointed to.
20894 20 20 20 2a 2a 20 62 79 20 70 4e 65 77 2e 20 54 ** by pNew. T
20895 68 69 73 20 69 73 20 65 69 74 68 65 72 20 45 58 his is either EX
20896 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 45 58 50 PR_FULLSIZE, EXP
20897 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 R_REDUCEDSIZE or
20898 0a 20 20 20 20 20 20 2a 2a 20 45 58 50 52 5f 54 . ** EXPR_T
20899 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 20 6e 54 OKENONLYSIZE. nT
2089a 6f 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 74 oken is set to t
2089b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
2089c 65 73 20 63 6f 6e 73 75 6d 65 64 0a 20 20 20 20 es consumed.
2089d 20 20 2a 2a 20 62 79 20 74 68 65 20 63 6f 70 79 ** by the copy
2089e 20 6f 66 20 74 68 65 20 70 2d 3e 75 2e 7a 54 6f of the p->u.zTo
2089f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66 20 61 ken string (if a
208a0 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ny).. */.
208a1 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e const unsign
208a2 65 64 20 6e 53 74 72 75 63 74 53 69 7a 65 20 3d ed nStructSize =
208a3 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 dupedExprStruct
208a4 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a Size(p, flags);.
208a5 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 const int
208a6 6e 4e 65 77 53 69 7a 65 20 3d 20 6e 53 74 72 75 nNewSize = nStru
208a7 63 74 53 69 7a 65 20 26 20 30 78 66 66 66 3b 0a ctSize & 0xfff;.
208a8 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e int nToken
208a9 3b 0a 20 20 20 20 20 20 69 66 28 20 21 45 78 70 ;. if( !Exp
208aa 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 rHasProperty(p,
208ab 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26 20 EP_IntValue) &&
208ac 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 p->u.zToken ){.
208ad 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 nToken =
208ae 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
208af 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 20 2b 20 31 p->u.zToken) + 1
208b0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
208b1 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 nToken =
208b2 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
208b3 20 69 66 28 20 69 73 52 65 64 75 63 65 64 20 29 if( isReduced )
208b4 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
208b5 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
208b6 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 y(p, EP_Reduced)
208b7 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 6d ==0 );. m
208b8 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c emcpy(zAlloc, p,
208b9 20 6e 4e 65 77 53 69 7a 65 29 3b 0a 20 20 20 20 nNewSize);.
208ba 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
208bb 20 69 6e 74 20 6e 53 69 7a 65 20 3d 20 65 78 70 int nSize = exp
208bc 72 53 74 72 75 63 74 53 69 7a 65 28 70 29 3b 0a rStructSize(p);.
208bd 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a memcpy(z
208be 41 6c 6c 6f 63 2c 20 70 2c 20 6e 53 69 7a 65 29 Alloc, p, nSize)
208bf 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 ;. memset
208c0 28 26 7a 41 6c 6c 6f 63 5b 6e 53 69 7a 65 5d 2c (&zAlloc[nSize],
208c1 20 30 2c 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 0, EXPR_FULLSIZ
208c2 45 2d 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 E-nSize);.
208c3 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 }.. /* Set
208c4 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 2c 20 the EP_Reduced,
208c5 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2c 20 61 6e EP_TokenOnly, an
208c6 64 20 45 50 5f 53 74 61 74 69 63 20 66 6c 61 67 d EP_Static flag
208c7 73 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e s appropriately.
208c8 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e */. pNew->
208c9 66 6c 61 67 73 20 26 3d 20 7e 28 45 50 5f 52 65 flags &= ~(EP_Re
208ca 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e duced|EP_TokenOn
208cb 6c 79 7c 45 50 5f 53 74 61 74 69 63 29 3b 0a 20 ly|EP_Static);.
208cc 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 pNew->flags
208cd 20 7c 3d 20 6e 53 74 72 75 63 74 53 69 7a 65 20 |= nStructSize
208ce 26 20 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50 & (EP_Reduced|EP
208cf 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 3b 0a 20 20 20 _TokenOnly);.
208d0 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c pNew->flags |
208d1 3d 20 73 74 61 74 69 63 46 6c 61 67 3b 0a 0a 20 = staticFlag;..
208d2 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 /* Copy the
208d3 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 p->u.zToken str
208d4 69 6e 67 2c 20 69 66 20 61 6e 79 2e 20 2a 2f 0a ing, if any. */.
208d5 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e if( nToken
208d6 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 ){. char
208d7 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4e 65 77 2d *zToken = pNew-
208d8 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 28 63 68 61 >u.zToken = (cha
208d9 72 2a 29 26 7a 41 6c 6c 6f 63 5b 6e 4e 65 77 53 r*)&zAlloc[nNewS
208da 69 7a 65 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 ize];. me
208db 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c 20 70 2d 3e mcpy(zToken, p->
208dc 75 2e 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e u.zToken, nToken
208dd 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
208de 20 20 69 66 28 20 30 3d 3d 28 28 70 2d 3e 66 6c if( 0==((p->fl
208df 61 67 73 7c 70 4e 65 77 2d 3e 66 6c 61 67 73 29 ags|pNew->flags)
208e0 20 26 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 & EP_TokenOnly)
208e1 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 ){. /* F
208e2 69 6c 6c 20 69 6e 20 74 68 65 20 70 4e 65 77 2d ill in the pNew-
208e3 3e 78 2e 70 53 65 6c 65 63 74 20 6f 72 20 70 4e >x.pSelect or pN
208e4 65 77 2d 3e 78 2e 70 4c 69 73 74 20 6d 65 6d 62 ew->x.pList memb
208e5 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 er. */. i
208e6 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 f( ExprHasProper
208e7 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 ty(p, EP_xIsSele
208e8 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ct) ){.
208e9 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 pNew->x.pSelect
208ea 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 = sqlite3Select
208eb 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 Dup(db, p->x.pSe
208ec 6c 65 63 74 2c 20 69 73 52 65 64 75 63 65 64 29 lect, isReduced)
208ed 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
208ee 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d . pNew-
208ef 3e 78 2e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 >x.pList = sqlit
208f0 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 e3ExprListDup(db
208f1 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 69 73 , p->x.pList, is
208f2 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 Reduced);.
208f3 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 }. }..
208f4 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 70 4e /* Fill in pN
208f5 65 77 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 4e ew->pLeft and pN
208f6 65 77 2d 3e 70 52 69 67 68 74 2e 20 2a 2f 0a 20 ew->pRight. */.
208f7 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 if( ExprHas
208f8 41 6e 79 50 72 6f 70 65 72 74 79 28 70 4e 65 77 AnyProperty(pNew
208f9 2c 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f , EP_Reduced|EP_
208fa 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 TokenOnly) ){.
208fb 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 2b 3d 20 zAlloc +=
208fc 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a dupedExprNodeSiz
208fd 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 e(p, flags);.
208fe 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 if( ExprHas
208ff 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 Property(pNew, E
20900 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0a 20 20 P_Reduced) ){.
20901 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c pNew->pL
20902 65 66 74 20 3d 20 65 78 70 72 44 75 70 28 64 62 eft = exprDup(db
20903 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45 58 50 52 , p->pLeft, EXPR
20904 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c DUP_REDUCE, &zAl
20905 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 loc);.
20906 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 65 pNew->pRight = e
20907 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52 xprDup(db, p->pR
20908 69 67 68 74 2c 20 45 58 50 52 44 55 50 5f 52 45 ight, EXPRDUP_RE
20909 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a DUCE, &zAlloc);.
2090a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2090b 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20 29 if( pzBuffer )
2090c 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a 42 {. *pzB
2090d 75 66 66 65 72 20 3d 20 7a 41 6c 6c 6f 63 3b 0a uffer = zAlloc;.
2090e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2090f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
20910 4e 65 77 2d 3e 66 6c 61 67 73 32 20 3d 20 30 3b New->flags2 = 0;
20911 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 45 78 . if( !Ex
20912 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
20913 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 (p, EP_TokenOnly
20914 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
20915 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 73 71 6c New->pLeft = sql
20916 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
20917 70 2d 3e 70 4c 65 66 74 2c 20 30 29 3b 0a 20 20 p->pLeft, 0);.
20918 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52 pNew->pR
20919 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 ight = sqlite3Ex
2091a 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52 69 prDup(db, p->pRi
2091b 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 ght, 0);.
2091c 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
2091d 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 }. }. return p
2091e 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 New;.}../*.** Th
2091f 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 72 6f 75 e following grou
20920 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 6d 61 p of routines ma
20921 6b 65 20 64 65 65 70 20 63 6f 70 69 65 73 20 6f ke deep copies o
20922 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a f expressions,.*
20923 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 * expression lis
20924 74 73 2c 20 49 44 20 6c 69 73 74 73 2c 20 61 6e ts, ID lists, an
20925 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 d select stateme
20926 6e 74 73 2e 20 20 54 68 65 20 63 6f 70 69 65 73 nts. The copies
20927 20 63 61 6e 0a 2a 2a 20 62 65 20 64 65 6c 65 74 can.** be delet
20928 65 64 20 28 62 79 20 62 65 69 6e 67 20 70 61 73 ed (by being pas
20929 73 65 64 20 74 6f 20 74 68 65 69 72 20 72 65 73 sed to their res
2092a 70 65 63 74 69 76 65 20 2e 2e 2e 44 65 6c 65 74 pective ...Delet
2092b 65 28 29 20 72 6f 75 74 69 6e 65 73 29 0a 2a 2a e() routines).**
2092c 20 77 69 74 68 6f 75 74 20 65 66 66 65 63 74 69 without effecti
2092d 6e 67 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 73 ng the originals
2092e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 ..**.** The expr
2092f 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 49 44 2c ession list, ID,
20930 20 61 6e 64 20 73 6f 75 72 63 65 20 6c 69 73 74 and source list
20931 73 20 72 65 74 75 72 6e 20 62 79 20 73 71 6c 69 s return by sqli
20932 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 29 te3ExprListDup()
20933 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 49 64 4c 69 ,.** sqlite3IdLi
20934 73 74 44 75 70 28 29 2c 20 61 6e 64 20 73 71 6c stDup(), and sql
20935 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 29 ite3SrcListDup()
20936 20 63 61 6e 20 6e 6f 74 20 62 65 20 66 75 72 74 can not be furt
20937 68 65 72 20 65 78 70 61 6e 64 65 64 20 0a 2a 2a her expanded .**
20938 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 by subsequent c
20939 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 2a 4c alls to sqlite*L
2093a 69 73 74 41 70 70 65 6e 64 28 29 20 72 6f 75 74 istAppend() rout
2093b 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 ines..**.** Any
2093c 74 61 62 6c 65 73 20 74 68 61 74 20 74 68 65 20 tables that the
2093d 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 70 6f SrcList might po
2093e 69 6e 74 20 74 6f 20 61 72 65 20 6e 6f 74 20 64 int to are not d
2093f 75 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a uplicated..**.**
20940 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d The flags param
20941 65 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 20 eter contains a
20942 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 combination of t
20943 68 65 20 45 58 50 52 44 55 50 5f 58 58 58 20 66 he EXPRDUP_XXX f
20944 6c 61 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 lags..** If the
20945 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 66 EXPRDUP_REDUCE f
20946 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e lag is set, then
20947 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 72 the structure r
20948 65 74 75 72 6e 65 64 20 69 73 20 61 0a 2a 2a 20 eturned is a.**
20949 74 72 75 6e 63 61 74 65 64 20 76 65 72 73 69 6f truncated versio
2094a 6e 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20 45 n of the usual E
2094b 78 70 72 20 73 74 72 75 63 74 75 72 65 20 74 68 xpr structure th
2094c 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 at will be store
2094d 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 d as.** part of
2094e 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 the in-memory re
2094f 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
20950 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
20951 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ema..*/.SQLITE_P
20952 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
20953 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c 69 ite3ExprDup(sqli
20954 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 te3 *db, Expr *p
20955 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 , int flags){.
20956 72 65 74 75 72 6e 20 65 78 70 72 44 75 70 28 64 return exprDup(d
20957 62 2c 20 70 2c 20 66 6c 61 67 73 2c 20 30 29 3b b, p, flags, 0);
20958 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
20959 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 E ExprList *sqli
2095a 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 73 te3ExprListDup(s
2095b 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 qlite3 *db, Expr
2095c 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 List *p, int fla
2095d 67 73 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 gs){. ExprList
2095e 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75 63 74 20 *pNew;. struct
2095f 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
20960 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74 65 6d 3b Item, *pOldItem;
20961 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 . int i;. if(
20962 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b p==0 ) return 0;
20963 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
20964 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
20965 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 sizeof(*pNew) )
20966 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
20967 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
20968 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 30 ew->iECursor = 0
20969 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72 20 ;. pNew->nExpr
2096a 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d = pNew->nAlloc =
2096b 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 4e 65 p->nExpr;. pNe
2096c 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20 73 w->a = pItem = s
2096d 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
2096e 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 70 72 2a w(db, p->nExpr*
2096f 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 sizeof(p->a[0])
20970 29 3b 0a 20 20 69 66 28 20 70 49 74 65 6d 3d 3d );. if( pItem==
20971 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
20972 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29 DbFree(db, pNew)
20973 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
20974 20 20 7d 20 0a 20 20 70 4f 6c 64 49 74 65 6d 20 } . pOldItem
20975 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d = p->a;. for(i=
20976 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 0; i<p->nExpr; i
20977 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c 20 70 4f 6c ++, pItem++, pOl
20978 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 dItem++){. Ex
20979 70 72 20 2a 70 4f 6c 64 45 78 70 72 20 3d 20 70 pr *pOldExpr = p
2097a 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a OldItem->pExpr;.
2097b 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 pItem->pExpr
2097c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
2097d 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 2c 20 p(db, pOldExpr,
2097e 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 49 74 65 flags);. pIte
2097f 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 m->zName = sqlit
20980 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 e3DbStrDup(db, p
20981 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b OldItem->zName);
20982 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53 70 61 . pItem->zSpa
20983 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 n = sqlite3DbStr
20984 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d Dup(db, pOldItem
20985 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 20 70 49 ->zSpan);. pI
20986 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 3d tem->sortOrder =
20987 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 6f 72 74 4f pOldItem->sortO
20988 72 64 65 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d rder;. pItem-
20989 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 >done = 0;. p
2098a 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 70 4f 6c Item->iCol = pOl
2098b 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b 0a 20 20 20 dItem->iCol;.
2098c 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d pItem->iAlias =
2098d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 41 6c 69 61 pOldItem->iAlia
2098e 73 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 s;. }. return
2098f 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 pNew;.}../*.** I
20990 66 20 63 75 72 73 6f 72 73 2c 20 74 72 69 67 67 f cursors, trigg
20991 65 72 73 2c 20 76 69 65 77 73 20 61 6e 64 20 73 ers, views and s
20992 75 62 71 75 65 72 69 65 73 20 61 72 65 20 61 6c ubqueries are al
20993 6c 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a l omitted from.*
20994 2a 20 74 68 65 20 62 75 69 6c 64 2c 20 74 68 65 * the build, the
20995 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f n none of the fo
20996 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 llowing routines
20997 2c 20 65 78 63 65 70 74 20 66 6f 72 20 0a 2a 2a , except for .**
20998 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 sqlite3SelectDu
20999 70 28 29 2c 20 63 61 6e 20 62 65 20 63 61 6c 6c p(), can be call
2099a 65 64 2e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 ed. sqlite3Selec
2099b 74 44 75 70 28 29 20 69 73 20 73 6f 6d 65 74 69 tDup() is someti
2099c 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69 mes.** called wi
2099d 74 68 20 61 20 4e 55 4c 4c 20 61 72 67 75 6d 65 th a NULL argume
2099e 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 nt..*/.#if !defi
2099f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
209a0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 VIEW) || !define
209a1 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 d(SQLITE_OMIT_TR
209a2 49 47 47 45 52 29 20 5c 0a 20 7c 7c 20 21 64 65 IGGER) \. || !de
209a3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
209a4 54 5f 53 55 42 51 55 45 52 59 29 0a 53 51 4c 49 T_SUBQUERY).SQLI
209a5 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 TE_PRIVATE SrcLi
209a6 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 st *sqlite3SrcLi
209a7 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 stDup(sqlite3 *d
209a8 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 69 b, SrcList *p, i
209a9 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 72 63 nt flags){. Src
209aa 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e List *pNew;. in
209ab 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 t i;. int nByte
209ac 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
209ad 65 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74 65 eturn 0;. nByte
209ae 20 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20 = sizeof(*p) +
209af 28 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a (p->nSrc>0 ? siz
209b0 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28 eof(p->a[0]) * (
209b1 70 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b p->nSrc-1) : 0);
209b2 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
209b3 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
209b4 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 nByte );. if(
209b5 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e pNew==0 ) return
209b6 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 63 0;. pNew->nSrc
209b7 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 = pNew->nAlloc
209b8 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f 72 = p->nSrc;. for
209b9 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 3b (i=0; i<p->nSrc;
209ba 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 i++){. struc
209bb 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a t SrcList_item *
209bc 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 pNewItem = &pNew
209bd 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 ->a[i];. stru
209be 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 ct SrcList_item
209bf 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e *pOldItem = &p->
209c0 61 5b 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20 a[i];. Table
209c1 2a 70 54 61 62 3b 0a 20 20 20 20 70 4e 65 77 49 *pTab;. pNewI
209c2 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d tem->zDatabase =
209c3 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
209c4 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a (db, pOldItem->z
209c5 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 70 Database);. p
209c6 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d NewItem->zName =
209c7 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
209c8 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a (db, pOldItem->z
209c9 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 Name);. pNewI
209ca 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 tem->zAlias = sq
209cb 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
209cc 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c 69 , pOldItem->zAli
209cd 61 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 as);. pNewIte
209ce 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 4f m->jointype = pO
209cf 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 ldItem->jointype
209d0 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
209d1 69 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49 74 iCursor = pOldIt
209d2 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 em->iCursor;.
209d3 20 70 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f 70 pNewItem->isPop
209d4 75 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49 74 65 ulated = pOldIte
209d5 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b 0a m->isPopulated;.
209d6 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 49 pNewItem->zI
209d7 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62 ndex = sqlite3Db
209d8 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 StrDup(db, pOldI
209d9 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 tem->zIndex);.
209da 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74 49 pNewItem->notI
209db 6e 64 65 78 65 64 20 3d 20 70 4f 6c 64 49 74 65 ndexed = pOldIte
209dc 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3b 0a 20 m->notIndexed;.
209dd 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 49 6e pNewItem->pIn
209de 64 65 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e dex = pOldItem->
209df 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61 62 pIndex;. pTab
209e0 20 3d 20 70 4e 65 77 49 74 65 6d 2d 3e 70 54 61 = pNewItem->pTa
209e1 62 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 54 b = pOldItem->pT
209e2 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 ab;. if( pTab
209e3 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e ){. pTab->
209e4 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 nRef++;. }.
209e5 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 65 6c pNewItem->pSel
209e6 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c ect = sqlite3Sel
209e7 65 63 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 ectDup(db, pOldI
209e8 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 66 6c tem->pSelect, fl
209e9 61 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 ags);. pNewIt
209ea 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74 65 em->pOn = sqlite
209eb 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 3ExprDup(db, pOl
209ec 64 49 74 65 6d 2d 3e 70 4f 6e 2c 20 66 6c 61 67 dItem->pOn, flag
209ed 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d s);. pNewItem
209ee 2d 3e 70 55 73 69 6e 67 20 3d 20 73 71 6c 69 74 ->pUsing = sqlit
209ef 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20 e3IdListDup(db,
209f0 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e 67 pOldItem->pUsing
209f1 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d );. pNewItem-
209f2 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64 49 >colUsed = pOldI
209f3 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 tem->colUsed;.
209f4 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b }. return pNew;
209f5 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
209f6 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 E IdList *sqlite
209f7 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 3IdListDup(sqlit
209f8 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a e3 *db, IdList *
209f9 70 29 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70 4e p){. IdList *pN
209fa 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 ew;. int i;. i
209fb 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
209fc 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 0;. pNew = sql
209fd 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
209fe 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 db, sizeof(*pNew
209ff 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d ) );. if( pNew=
20a00 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
20a01 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e 65 pNew->nId = pNe
20a02 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e w->nAlloc = p->n
20a03 49 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 Id;. pNew->a =
20a04 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
20a05 61 77 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 69 aw(db, p->nId*si
20a06 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b zeof(p->a[0]) );
20a07 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d 3d . if( pNew->a==
20a08 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
20a09 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29 DbFree(db, pNew)
20a0a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
20a0b 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
20a0c 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 <p->nId; i++){.
20a0d 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 struct IdList
20a0e 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 _item *pNewItem
20a0f 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 = &pNew->a[i];.
20a10 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 struct IdList
20a11 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 _item *pOldItem
20a12 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 = &p->a[i];.
20a13 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 pNewItem->zName
20a14 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
20a15 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
20a16 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 zName);. pNew
20a17 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c 64 Item->idx = pOld
20a18 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a 20 Item->idx;. }.
20a19 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
20a1a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
20a1b 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 elect *sqlite3Se
20a1c 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 20 lectDup(sqlite3
20a1d 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 *db, Select *p,
20a1e 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 65 int flags){. Se
20a1f 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69 66 lect *pNew;. if
20a20 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
20a21 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 0;. pNew = sqli
20a22 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
20a23 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b b, sizeof(*p) );
20a24 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 . if( pNew==0 )
20a25 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 return 0;. pNe
20a26 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 w->pEList = sqli
20a27 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 te3ExprListDup(d
20a28 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 66 6c b, p->pEList, fl
20a29 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 53 ags);. pNew->pS
20a2a 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c rc = sqlite3SrcL
20a2b 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 53 istDup(db, p->pS
20a2c 72 63 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e rc, flags);. pN
20a2d 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c ew->pWhere = sql
20a2e 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
20a2f 70 2d 3e 70 57 68 65 72 65 2c 20 66 6c 61 67 73 p->pWhere, flags
20a30 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75 );. pNew->pGrou
20a31 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 pBy = sqlite3Exp
20a32 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e rListDup(db, p->
20a33 70 47 72 6f 75 70 42 79 2c 20 66 6c 61 67 73 29 pGroupBy, flags)
20a34 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 6e ;. pNew->pHavin
20a35 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 g = sqlite3ExprD
20a36 75 70 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e up(db, p->pHavin
20a37 67 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 g, flags);. pNe
20a38 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 w->pOrderBy = sq
20a39 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 lite3ExprListDup
20a3a 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 (db, p->pOrderBy
20a3b 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 , flags);. pNew
20a3c 2d 3e 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 ->op = p->op;.
20a3d 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 73 pNew->pPrior = s
20a3e 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 qlite3SelectDup(
20a3f 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 66 db, p->pPrior, f
20a40 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 lags);. pNew->p
20a41 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 45 Limit = sqlite3E
20a42 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c xprDup(db, p->pL
20a43 69 6d 69 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 imit, flags);.
20a44 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 pNew->pOffset =
20a45 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 sqlite3ExprDup(d
20a46 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 66 b, p->pOffset, f
20a47 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 69 lags);. pNew->i
20a48 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 70 4e 65 Limit = 0;. pNe
20a49 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a w->iOffset = 0;.
20a4a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73 pNew->selFlags
20a4b 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 = p->selFlags &
20a4c 20 7e 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 ~SF_UsesEphemer
20a4d 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69 67 al;. pNew->pRig
20a4e 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 70 4e htmost = 0;. pN
20a4f 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d ew->addrOpenEphm
20a50 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 [0] = -1;. pNew
20a51 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 ->addrOpenEphm[1
20a52 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e ] = -1;. pNew->
20a53 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 addrOpenEphm[2]
20a54 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 = -1;. return p
20a55 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c New;.}.#else.SQL
20a56 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 ITE_PRIVATE Sele
20a57 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 ct *sqlite3Selec
20a58 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 tDup(sqlite3 *db
20a59 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 , Select *p, int
20a5a 20 66 6c 61 67 73 29 7b 0a 20 20 61 73 73 65 72 flags){. asser
20a5b 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20 72 65 74 t( p==0 );. ret
20a5c 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a urn 0;.}.#endif.
20a5d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 ../*.** Add a ne
20a5e 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 w element to the
20a5f 20 65 6e 64 20 6f 66 20 61 6e 20 65 78 70 72 65 end of an expre
20a60 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20 ssion list. If
20a61 70 4c 69 73 74 20 69 73 0a 2a 2a 20 69 6e 69 74 pList is.** init
20a62 69 61 6c 6c 79 20 4e 55 4c 4c 2c 20 74 68 65 6e ially NULL, then
20a63 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 65 78 create a new ex
20a64 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a pression list..*
20a65 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 *.** If a memory
20a66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
20a67 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 65 6e r occurs, the en
20a68 74 69 72 65 20 6c 69 73 74 20 69 73 20 66 72 65 tire list is fre
20a69 65 64 20 61 6e 64 0a 2a 2a 20 4e 55 4c 4c 20 69 ed and.** NULL i
20a6a 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 s returned. If
20a6b 6e 6f 6e 2d 4e 55 4c 4c 20 69 73 20 72 65 74 75 non-NULL is retu
20a6c 72 6e 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73 rned, then it is
20a6d 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 guaranteed.** t
20a6e 68 61 74 20 74 68 65 20 6e 65 77 20 65 6e 74 72 hat the new entr
20a6f 79 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c y was successful
20a70 6c 79 20 61 70 70 65 6e 64 65 64 2e 0a 2a 2f 0a ly appended..*/.
20a71 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
20a72 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 xprList *sqlite3
20a73 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 0a ExprListAppend(.
20a74 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
20a75 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 /* Par
20a76 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
20a77 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 ExprList *pLis
20a78 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 t, /* Lis
20a79 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70 t to which to ap
20a7a 70 65 6e 64 2e 20 4d 69 67 68 74 20 62 65 20 4e pend. Might be N
20a7b 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ULL */. Expr *p
20a7c 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20 20 Expr
20a7d 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 /* Expression t
20a7e 6f 20 62 65 20 61 70 70 65 6e 64 65 64 2e 20 4d o be appended. M
20a7f 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a ight be NULL */.
20a80 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
20a81 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
20a82 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b if( pList==0 ){
20a83 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c . pList = sql
20a84 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
20a85 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 (db, sizeof(Expr
20a86 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28 List) );. if(
20a87 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 pList==0 ){.
20a88 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a goto no_mem;.
20a89 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
20a8a 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d ( pList->nAlloc=
20a8b 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 =0 );. }. if(
20a8c 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70 pList->nAlloc<=p
20a8d 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 List->nExpr ){.
20a8e 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
20a8f 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 20 20 st_item *a;.
20a90 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e int n = pList->n
20a91 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b 0a 20 20 20 Alloc*2 + 4;.
20a92 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 a = sqlite3DbRe
20a93 61 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73 74 2d alloc(db, pList-
20a94 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28 70 4c 69 >a, n*sizeof(pLi
20a95 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 20 st->a[0]));.
20a96 69 66 28 20 61 3d 3d 30 20 29 7b 0a 20 20 20 20 if( a==0 ){.
20a97 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
20a98 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74 2d 3e }. pList->
20a99 61 20 3d 20 61 3b 0a 20 20 20 20 70 4c 69 73 74 a = a;. pList
20a9a 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 ->nAlloc = sqlit
20a9b 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 e3DbMallocSize(d
20a9c 62 2c 20 61 29 2f 73 69 7a 65 6f 66 28 61 5b 30 b, a)/sizeof(a[0
20a9d 5d 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 ]);. }. assert
20a9e 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29 3b ( pList->a!=0 );
20a9f 0a 20 20 69 66 28 20 31 20 29 7b 0a 20 20 20 20 . if( 1 ){.
20aa0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
20aa1 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 item *pItem = &p
20aa2 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e List->a[pList->n
20aa3 45 78 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d Expr++];. mem
20aa4 73 65 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 set(pItem, 0, si
20aa5 7a 65 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20 zeof(*pItem));.
20aa6 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 pItem->pExpr
20aa7 3d 20 70 45 78 70 72 3b 0a 20 20 7d 0a 20 20 72 = pExpr;. }. r
20aa8 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 0a 6e 6f eturn pList;..no
20aa9 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 20 2f 2a 20 _mem: . /*
20aaa 41 76 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65 Avoid leaking me
20aab 6d 6f 72 79 20 69 66 20 6d 61 6c 6c 6f 63 20 68 mory if malloc h
20aac 61 73 20 66 61 69 6c 65 64 2e 20 2a 2f 0a 20 20 as failed. */.
20aad 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
20aae 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 e(db, pExpr);.
20aaf 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
20ab0 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 elete(db, pList)
20ab1 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
20ab2 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 ./*.** Set the E
20ab3 78 70 72 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61 6d xprList.a[].zNam
20ab4 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 e element of the
20ab5 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 most recently a
20ab6 64 64 65 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e 20 dded item.** on
20ab7 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c the expression l
20ab8 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 ist..**.** pList
20ab9 20 6d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 66 might be NULL f
20aba 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20 ollowing an OOM
20abb 65 72 72 6f 72 2e 20 20 42 75 74 20 70 4e 61 6d error. But pNam
20abc 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 e should never b
20abd 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 61 e.** NULL. If a
20abe 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
20abf 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 20 70 50 on fails, the pP
20ac0 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
20ac1 46 61 69 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 69 Failed flag.** i
20ac2 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 s set..*/.SQLITE
20ac3 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
20ac4 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 lite3ExprListSet
20ac5 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 Name(. Parse *p
20ac6 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
20ac7 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
20ac8 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 xt */. ExprList
20ac9 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 *pList,
20aca 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68 /* List to which
20acb 20 74 6f 20 61 64 64 20 74 68 65 20 73 70 61 6e to add the span
20acc 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e . */. Token *pN
20acd 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f ame, /
20ace 2a 20 4e 61 6d 65 20 74 6f 20 62 65 20 61 64 64 * Name to be add
20acf 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 ed */. int dequ
20ad0 6f 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ote
20ad1 2f 2a 20 54 72 75 65 20 74 6f 20 63 61 75 73 65 /* True to cause
20ad2 20 74 68 65 20 6e 61 6d 65 20 74 6f 20 62 65 20 the name to be
20ad3 64 65 71 75 6f 74 65 64 20 2a 2f 0a 29 7b 0a 20 dequoted */.){.
20ad4 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d assert( pList!=
20ad5 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 0 || pParse->db-
20ad6 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30 >mallocFailed!=0
20ad7 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 );. if( pList
20ad8 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 ){. struct Ex
20ad9 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
20ada 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 em;. assert(
20adb 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29 pList->nExpr>0 )
20adc 3b 0a 20 20 20 20 70 49 74 65 6d 20 3d 20 26 70 ;. pItem = &p
20add 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e List->a[pList->n
20ade 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 61 73 73 Expr-1];. ass
20adf 65 72 74 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d ert( pItem->zNam
20ae0 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 49 74 65 e==0 );. pIte
20ae1 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 m->zName = sqlit
20ae2 65 33 44 62 53 74 72 4e 44 75 70 28 70 50 61 72 e3DbStrNDup(pPar
20ae3 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 2d 3e 7a se->db, pName->z
20ae4 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 , pName->n);.
20ae5 20 69 66 28 20 64 65 71 75 6f 74 65 20 26 26 20 if( dequote &&
20ae6 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 73 pItem->zName ) s
20ae7 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 70 49 qlite3Dequote(pI
20ae8 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d tem->zName);. }
20ae9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
20aea 65 20 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e 7a e ExprList.a[].z
20aeb 53 70 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 Span element of
20aec 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
20aed 79 20 61 64 64 65 64 20 69 74 65 6d 0a 2a 2a 20 y added item.**
20aee 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f on the expressio
20aef 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c n list..**.** pL
20af0 69 73 74 20 6d 69 67 68 74 20 62 65 20 4e 55 4c ist might be NUL
20af1 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 4f L following an O
20af2 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20 70 OM error. But p
20af3 53 70 61 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 Span should neve
20af4 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 49 r be.** NULL. I
20af5 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
20af6 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 ation fails, the
20af7 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
20af8 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 0a 2a locFailed flag.*
20af9 2a 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c * is set..*/.SQL
20afa 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
20afb 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
20afc 53 65 74 53 70 61 6e 28 0a 20 20 50 61 72 73 65 SetSpan(. Parse
20afd 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
20afe 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
20aff 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c ntext */. ExprL
20b00 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 ist *pList,
20b01 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 /* List to wh
20b02 69 63 68 20 74 6f 20 61 64 64 20 74 68 65 20 73 ich to add the s
20b03 70 61 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 53 70 pan. */. ExprSp
20b04 61 6e 20 2a 70 53 70 61 6e 20 20 20 20 20 20 20 an *pSpan
20b05 20 20 2f 2a 20 54 68 65 20 73 70 61 6e 20 74 6f /* The span to
20b06 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 29 7b 0a be added */.){.
20b07 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
20b08 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 pParse->db;. as
20b09 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 7c sert( pList!=0 |
20b0a 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
20b0b 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 ed!=0 );. if( p
20b0c 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 74 72 75 List ){. stru
20b0d 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
20b0e 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 *pItem = &pList
20b0f 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 ->a[pList->nExpr
20b10 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 -1];. assert(
20b11 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 pList->nExpr>0
20b12 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 );. assert( d
20b13 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
20b14 7c 7c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3d || pItem->pExpr=
20b15 3d 70 53 70 61 6e 2d 3e 70 45 78 70 72 20 29 3b =pSpan->pExpr );
20b16 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
20b17 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 53 ee(db, pItem->zS
20b18 70 61 6e 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d pan);. pItem-
20b19 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 65 33 >zSpan = sqlite3
20b1a 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 DbStrNDup(db, (c
20b1b 68 61 72 2a 29 70 53 70 61 6e 2d 3e 7a 53 74 61 har*)pSpan->zSta
20b1c 72 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 rt,.
20b1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20b1e 20 20 20 20 20 20 20 20 28 69 6e 74 29 28 70 53 (int)(pS
20b1f 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 70 61 pan->zEnd - pSpa
20b20 6e 2d 3e 7a 53 74 61 72 74 29 29 3b 0a 20 20 7d n->zStart));. }
20b21 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
20b22 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
20b23 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69 6e 73 pEList contains
20b24 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69 6d 69 more than iLimi
20b25 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20 6c t elements,.** l
20b26 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 eave an error me
20b27 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2e ssage in pParse.
20b28 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20b29 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
20b2a 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 xprListCheckLeng
20b2b 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 th(. Parse *pPa
20b2c 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73 74 20 rse,. ExprList
20b2d 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e 73 74 *pEList,. const
20b2e 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74 0a 29 char *zObject.)
20b2f 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70 50 61 {. int mx = pPa
20b30 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b rse->db->aLimit[
20b31 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c SQLITE_LIMIT_COL
20b32 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61 73 65 UMN];. testcase
20b33 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69 ( pEList && pELi
20b34 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20 29 3b st->nExpr==mx );
20b35 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45 4c . testcase( pEL
20b36 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e ist && pEList->n
20b37 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20 20 Expr==mx+1 );.
20b38 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 if( pEList && pE
20b39 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78 20 29 List->nExpr>mx )
20b3a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
20b3b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 orMsg(pParse, "t
20b3c 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 oo many columns
20b3d 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63 74 29 in %s", zObject)
20b3e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 ;. }.}../*.** D
20b3f 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 elete an entire
20b40 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e expression list.
20b41 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20b42 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
20b43 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 xprListDelete(sq
20b44 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 4c lite3 *db, ExprL
20b45 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 ist *pList){. i
20b46 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 nt i;. struct E
20b47 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
20b48 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 tem;. if( pList
20b49 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
20b4a 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 assert( pList->a
20b4b 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d 3e 6e !=0 || (pList->n
20b4c 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 73 74 Expr==0 && pList
20b4d 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b 0a ->nAlloc==0) );.
20b4e 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d assert( pList-
20b4f 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d 3e 6e >nExpr<=pList->n
20b50 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 28 70 Alloc );. for(p
20b51 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 Item=pList->a, i
20b52 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 =0; i<pList->nEx
20b53 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b pr; i++, pItem++
20b54 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
20b55 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 prDelete(db, pIt
20b56 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 em->pExpr);.
20b57 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
20b58 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b , pItem->zName);
20b59 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
20b5a 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 53 ee(db, pItem->zS
20b5b 70 61 6e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 pan);. }. sqli
20b5c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c te3DbFree(db, pL
20b5d 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 ist->a);. sqlit
20b5e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 e3DbFree(db, pLi
20b5f 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 st);.}../*.** Th
20b60 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
20b61 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b Walker callback
20b62 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69 20 s. Walker.u.pi
20b63 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 is a pointer.**
20b64 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 to an integer.
20b65 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
20b66 72 65 20 63 68 65 63 6b 69 6e 67 20 61 6e 20 65 re checking an e
20b67 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 65 65 xpression to see
20b68 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 20 63 .** if it is a c
20b69 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 20 2a 57 onstant. Set *W
20b6a 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30 20 alker.u.pi to 0
20b6b 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f if the expressio
20b6c 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73 n is.** not cons
20b6d 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 tant..**.** Thes
20b6e 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 e callback routi
20b6f 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 nes are used to
20b70 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f implement the fo
20b71 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
20b72 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 sqlite3ExprIs
20b73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20 20 Constant().**
20b74 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 sqlite3ExprIsC
20b75 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29 onstantNotJoin()
20b76 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 .** sqlite3E
20b77 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 xprIsConstantOrF
20b78 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a unction().**.*/.
20b79 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 4e static int exprN
20b7a 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 odeIsConstant(Wa
20b7b 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 lker *pWalker, E
20b7c 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20 20 xpr *pExpr){..
20b7d 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e 75 /* If pWalker->u
20b7e 2e 69 20 69 73 20 33 20 74 68 65 6e 20 61 6e 79 .i is 3 then any
20b7f 20 74 65 72 6d 20 6f 66 20 74 68 65 20 65 78 70 term of the exp
20b80 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6d ression that com
20b81 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 es from. ** the
20b82 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 ON or USING cla
20b83 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20 64 uses of a join d
20b84 69 73 71 75 61 6c 69 66 69 65 73 20 74 68 65 20 isqualifies the
20b85 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 expression. **
20b86 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73 69 from being consi
20b87 64 65 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e 20 dered constant.
20b88 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c 6b 65 72 */. if( pWalker
20b89 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 78 70 72 ->u.i==3 && Expr
20b8a 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 HasAnyProperty(p
20b8b 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 Expr, EP_FromJoi
20b8c 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b 65 n) ){. pWalke
20b8d 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 r->u.i = 0;.
20b8e 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
20b8f 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 ;. }.. switch(
20b90 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 pExpr->op ){.
20b91 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66 75 /* Consider fu
20b92 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f nctions to be co
20b93 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c 20 74 68 nstant if all th
20b94 65 69 72 20 61 72 67 75 6d 65 6e 74 73 20 61 72 eir arguments ar
20b95 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 2a e constant. *
20b96 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e 75 * and pWalker->u
20b97 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 73 .i==2 */. cas
20b98 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 e TK_FUNCTION:.
20b99 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65 72 if( pWalker
20b9a 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75 72 ->u.i==2 ) retur
20b9b 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61 n 0;. /* Fa
20b9c 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 ll through */.
20b9d 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20 case TK_ID:.
20b9e 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e case TK_COLUMN
20b9f 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 :. case TK_AG
20ba0 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 G_FUNCTION:.
20ba1 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 case TK_AGG_COLU
20ba2 4d 4e 3a 0a 20 20 20 20 20 20 74 65 73 74 63 61 MN:. testca
20ba3 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
20ba4 4b 5f 49 44 20 29 3b 0a 20 20 20 20 20 20 74 65 K_ID );. te
20ba5 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
20ba6 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a p==TK_COLUMN );.
20ba7 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
20ba8 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 pExpr->op==TK_AG
20ba9 47 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20 G_FUNCTION );.
20baa 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 testcase( pE
20bab 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f xpr->op==TK_AGG_
20bac 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 COLUMN );.
20bad 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 pWalker->u.i = 0
20bae 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 ;. return W
20baf 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 64 65 RC_Abort;. de
20bb0 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 74 65 73 fault:. tes
20bb1 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 tcase( pExpr->op
20bb2 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 20 2f ==TK_SELECT ); /
20bb3 2a 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f * selectNodeIsCo
20bb4 6e 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 73 61 nstant will disa
20bb5 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 74 65 llow */. te
20bb6 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
20bb7 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 20 p==TK_EXISTS );
20bb8 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 /* selectNodeIsC
20bb9 6f 6e 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 73 onstant will dis
20bba 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 72 allow */. r
20bbb 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
20bbc 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 ue;. }.}.static
20bbd 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64 65 49 int selectNodeI
20bbe 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72 sConstant(Walker
20bbf 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 *pWalker, Selec
20bc0 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 t *NotUsed){. U
20bc1 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
20bc2 4e 6f 74 55 73 65 64 29 3b 0a 20 20 70 57 61 6c NotUsed);. pWal
20bc3 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 ker->u.i = 0;.
20bc4 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
20bc5 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 65 ;.}.static int e
20bc6 78 70 72 49 73 43 6f 6e 73 74 28 45 78 70 72 20 xprIsConst(Expr
20bc7 2a 70 2c 20 69 6e 74 20 69 6e 69 74 46 6c 61 67 *p, int initFlag
20bc8 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 ){. Walker w;.
20bc9 20 77 2e 75 2e 69 20 3d 20 69 6e 69 74 46 6c 61 w.u.i = initFla
20bca 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c g;. w.xExprCall
20bcb 62 61 63 6b 20 3d 20 65 78 70 72 4e 6f 64 65 49 back = exprNodeI
20bcc 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 77 2e 78 sConstant;. w.x
20bcd 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d SelectCallback =
20bce 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e selectNodeIsCon
20bcf 73 74 61 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33 stant;. sqlite3
20bd0 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 29 3b WalkExpr(&w, p);
20bd1 0a 20 20 72 65 74 75 72 6e 20 77 2e 75 2e 69 3b . return w.u.i;
20bd2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 .}../*.** Walk a
20bd3 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 n expression tre
20bd4 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 e. Return 1 if
20bd5 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
20bd6 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e s constant.** an
20bd7 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 d 0 if it involv
20bd8 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 es variables or
20bd9 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e 0a function calls..
20bda 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 **.** For the pu
20bdb 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 rposes of this f
20bdc 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c unction, a doubl
20bdd 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 e-quoted string
20bde 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 (ex: "abc").** i
20bdf 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 s considered a v
20be0 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 ariable but a si
20be1 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 ngle-quoted stri
20be2 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 ng (ex: 'abc') i
20be3 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e s.** a constant.
20be4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20be5 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 TE int sqlite3Ex
20be6 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 prIsConstant(Exp
20be7 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 r *p){. return
20be8 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 31 exprIsConst(p, 1
20be9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b );.}../*.** Walk
20bea 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 an expression t
20beb 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 ree. Return 1 i
20bec 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
20bed 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 is constant.**
20bee 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72 69 that does no ori
20bef 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68 65 20 ginate from the
20bf0 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 ON or USING clau
20bf1 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0a 2a ses of a join..*
20bf2 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 69 74 * Return 0 if it
20bf3 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 involves variab
20bf4 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 les or function
20bf5 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20 66 calls or terms f
20bf6 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72 20 rom.** an ON or
20bf7 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2f USING clause..*/
20bf8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20bf9 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 int sqlite3ExprI
20bfa 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e sConstantNotJoin
20bfb 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 (Expr *p){. ret
20bfc 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 urn exprIsConst(
20bfd 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 p, 3);.}../*.**
20bfe 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 Walk an expressi
20bff 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e on tree. Return
20c00 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73 1 if the expres
20c01 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 sion is constant
20c02 0a 2a 2a 20 6f 72 20 61 20 66 75 6e 63 74 69 6f .** or a functio
20c03 6e 20 63 61 6c 6c 20 77 69 74 68 20 63 6f 6e 73 n call with cons
20c04 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 73 2e 20 tant arguments.
20c05 20 52 65 74 75 72 6e 20 61 6e 64 20 30 20 69 66 Return and 0 if
20c06 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 61 6e there.** are an
20c07 79 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a y variables..**.
20c08 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f ** For the purpo
20c09 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 ses of this func
20c0a 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 tion, a double-q
20c0b 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 uoted string (ex
20c0c 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 : "abc").** is c
20c0d 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 72 69 onsidered a vari
20c0e 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e 67 6c able but a singl
20c0f 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 e-quoted string
20c10 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 0a 2a (ex: 'abc') is.*
20c11 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f * a constant..*/
20c12 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20c13 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 int sqlite3ExprI
20c14 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 sConstantOrFunct
20c15 69 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 ion(Expr *p){.
20c16 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e return exprIsCon
20c17 73 74 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f 2a 0a st(p, 2);.}../*.
20c18 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 ** If the expres
20c19 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61 20 63 sion p codes a c
20c1a 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 20 onstant integer
20c1b 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20 65 6e that is small en
20c1c 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74 20 69 ough.** to fit i
20c1d 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 n a 32-bit integ
20c1e 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61 6e 64 er, return 1 and
20c1f 20 70 75 74 20 74 68 65 20 76 61 6c 75 65 20 6f put the value o
20c20 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a f the integer.**
20c21 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20 49 66 in *pValue. If
20c22 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20c23 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 is not an intege
20c24 72 20 6f 72 20 69 66 20 69 74 20 69 73 20 74 6f r or if it is to
20c25 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69 74 20 o big.** to fit
20c26 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32 2d 62 in a signed 32-b
20c27 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 it integer, retu
20c28 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65 20 2a rn 0 and leave *
20c29 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67 65 64 pValue unchanged
20c2a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20c2b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
20c2c 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78 70 xprIsInteger(Exp
20c2d 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61 6c 75 r *p, int *pValu
20c2e 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 e){. int rc = 0
20c2f 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 ;. if( p->flags
20c30 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 & EP_IntValue )
20c31 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20 3d 20 {. *pValue =
20c32 70 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20 p->u.iValue;.
20c33 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
20c34 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 switch( p->op )
20c35 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e {. case TK_IN
20c36 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 TEGER: {. r
20c37 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 49 6e c = sqlite3GetIn
20c38 74 33 32 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c t32(p->u.zToken,
20c39 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 pValue);.
20c3a 61 73 73 65 72 74 28 20 72 63 3d 3d 30 20 29 3b assert( rc==0 );
20c3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
20c3c 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
20c3d 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 72 UPLUS: {. r
20c3e 63 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 49 c = sqlite3ExprI
20c3f 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 sInteger(p->pLef
20c40 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 t, pValue);.
20c41 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
20c42 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 case TK_UMINU
20c43 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 S: {. int v
20c44 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
20c45 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
20c46 28 70 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 29 (p->pLeft, &v) )
20c47 7b 0a 20 20 20 20 20 20 20 20 2a 70 56 61 6c 75 {. *pValu
20c48 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 e = -v;.
20c49 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a rc = 1;. }.
20c4a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
20c4b 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
20c4c 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 break;. }. if(
20c4d 20 72 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 rc ){. asser
20c4e 74 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f t( ExprHasAnyPro
20c4f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 perty(p, EP_Redu
20c50 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 ced|EP_TokenOnly
20c51 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
20c52 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 || (p->flags2 &
20c53 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b EP2_MallocedTok
20c54 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d en)==0 );. p-
20c55 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52 >op = TK_INTEGER
20c56 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c ;. p->flags |
20c57 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 = EP_IntValue;.
20c58 20 20 20 70 2d 3e 75 2e 69 56 61 6c 75 65 20 3d p->u.iValue =
20c59 20 2a 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 20 *pValue;. }.
20c5a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
20c5b 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
20c5c 69 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 if the given str
20c5d 69 6e 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20 ing is a row-id
20c5e 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a column name..*/.
20c5f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
20c60 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 nt sqlite3IsRowi
20c61 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 d(const char *z)
20c62 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 {. if( sqlite3S
20c63 74 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49 trICmp(z, "_ROWI
20c64 44 5f 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e D_")==0 ) return
20c65 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 1;. if( sqlite
20c66 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57 3StrICmp(z, "ROW
20c67 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e ID")==0 ) return
20c68 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 1;. if( sqlite
20c69 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44 3StrICmp(z, "OID
20c6a 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 ")==0 ) return 1
20c6b 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
20c6c 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
20c6d 75 65 20 69 66 20 77 65 20 61 72 65 20 61 62 6c ue if we are abl
20c6e 65 20 74 6f 20 74 68 65 20 49 4e 20 6f 70 65 72 e to the IN oper
20c6f 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f ator optimizatio
20c70 6e 20 6f 6e 20 61 0a 2a 2a 20 71 75 65 72 79 20 n on a.** query
20c71 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a of the form.**.*
20c72 2a 20 20 20 20 20 20 20 78 20 49 4e 20 28 53 45 * x IN (SE
20c73 4c 45 43 54 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 LECT ...).**.**
20c74 57 68 65 72 65 20 74 68 65 20 53 45 4c 45 43 54 Where the SELECT
20c75 2e 2e 2e 20 63 6c 61 75 73 65 20 69 73 20 61 73 ... clause is as
20c76 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 specified by th
20c77 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 e parameter to t
20c78 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a his.** routine..
20c79 2a 2a 0a 2a 2a 20 54 68 65 20 53 65 6c 65 63 74 **.** The Select
20c7a 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 69 object passed i
20c7b 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 n has already be
20c7c 65 6e 20 70 72 65 70 72 6f 63 65 73 73 65 64 20 en preprocessed
20c7d 61 6e 64 20 6e 6f 0a 2a 2a 20 65 72 72 6f 72 73 and no.** errors
20c7e 20 68 61 76 65 20 62 65 65 6e 20 66 6f 75 6e 64 have been found
20c7f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
20c80 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 ITE_OMIT_SUBQUER
20c81 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 43 Y.static int isC
20c82 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 andidateForInOpt
20c83 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 (Select *p){. S
20c84 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 rcList *pSrc;.
20c85 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 ExprList *pEList
20c86 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b ;. Table *pTab;
20c87 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
20c88 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 turn 0;
20c89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 69 67 /* rig
20c8a 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 ht-hand side of
20c8b 49 4e 20 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a IN is SELECT */.
20c8c 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 if( p->pPrior
20c8d 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
20c8e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 /* Not
20c8f 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 a compound SELEC
20c90 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 T */. if( p->se
20c91 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 lFlags & (SF_Dis
20c92 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 tinct|SF_Aggrega
20c93 74 65 29 20 29 7b 0a 20 20 20 20 74 65 73 74 63 te) ){. testc
20c94 61 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 ase( (p->selFlag
20c95 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 s & (SF_Distinct
20c96 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d |SF_Aggregate))=
20c97 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 29 3b 0a =SF_Distinct );.
20c98 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70 testcase( (p
20c99 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46 ->selFlags & (SF
20c9a 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 _Distinct|SF_Agg
20c9b 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 41 67 67 regate))==SF_Agg
20c9c 72 65 67 61 74 65 20 29 3b 0a 20 20 20 20 72 65 regate );. re
20c9d 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 49 turn 0; /* No DI
20c9e 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 61 STINCT keyword a
20c9f 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 nd no aggregate
20ca0 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d functions */. }
20ca1 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 47 . assert( p->pG
20ca2 72 6f 75 70 42 79 3d 3d 30 20 29 3b 20 20 20 20 roupBy==0 );
20ca3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
20ca4 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61 no GROUP BY cla
20ca5 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e use */. if( p->
20ca6 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20 pLimit ) return
20ca7 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
20ca8 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54 20 /* Has no LIMIT
20ca9 63 6c 61 75 73 65 20 2a 2f 0a 20 20 61 73 73 65 clause */. asse
20caa 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d 3d rt( p->pOffset==
20cab 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 0 );
20cac 20 20 20 2f 2a 20 4e 6f 20 4c 49 4d 49 54 20 6d /* No LIMIT m
20cad 65 61 6e 73 20 6e 6f 20 4f 46 46 53 45 54 20 2a eans no OFFSET *
20cae 2f 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72 /. if( p->pWher
20caf 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 e ) return 0;
20cb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
20cb1 73 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 s no WHERE claus
20cb2 65 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70 2d e */. pSrc = p-
20cb3 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 >pSrc;. assert(
20cb4 20 70 53 72 63 21 3d 30 20 29 3b 0a 20 20 69 66 pSrc!=0 );. if
20cb5 28 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 ( pSrc->nSrc!=1
20cb6 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
20cb7 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 20 74 /* Single t
20cb8 65 72 6d 20 69 6e 20 46 52 4f 4d 20 63 6c 61 75 erm in FROM clau
20cb9 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 se */. if( pSrc
20cba 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20 29 ->a[0].pSelect )
20cbb 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 2f return 0; /
20cbc 2a 20 46 52 4f 4d 20 69 73 20 6e 6f 74 20 61 20 * FROM is not a
20cbd 73 75 62 71 75 65 72 79 20 6f 72 20 76 69 65 77 subquery or view
20cbe 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 70 53 72 */. pTab = pSr
20cbf 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 c->a[0].pTab;.
20cc0 69 66 28 20 4e 45 56 45 52 28 70 54 61 62 3d 3d if( NEVER(pTab==
20cc1 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 0) ) return 0;.
20cc2 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 70 assert( pTab->p
20cc3 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 20 20 Select==0 );
20cc4 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 /* FROM
20cc5 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61 20 clause is not a
20cc6 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20 49 73 view */. if( Is
20cc7 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 Virtual(pTab) )
20cc8 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 return 0;
20cc9 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 /* FROM clause
20cca 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 not a virtual ta
20ccb 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 ble */. pEList
20ccc 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 = p->pEList;. i
20ccd 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 f( pEList->nExpr
20cce 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 20 !=1 ) return 0;
20ccf 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c /* One col
20cd0 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c umn in the resul
20cd1 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28 20 70 t set */. if( p
20cd2 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 EList->a[0].pExp
20cd3 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e r->op!=TK_COLUMN
20cd4 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 ) return 0; /*
20cd5 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 Result is a colu
20cd6 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 mn */. return 1
20cd7 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
20cd8 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
20cd9 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 RY */../*.** Thi
20cda 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 s function is us
20cdb 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d ed by the implem
20cdc 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
20cdd 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61 74 6f IN (...) operato
20cde 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69 r..** It's job i
20cdf 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63 72 65 s to find or cre
20ce0 61 74 65 20 61 20 62 2d 74 72 65 65 20 73 74 72 ate a b-tree str
20ce1 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 20 ucture that may
20ce2 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74 68 65 be used.** eithe
20ce3 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 r to test for me
20ce4 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68 65 20 mbership of the
20ce5 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f 20 (...) set or to
20ce6 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 0a iterate through.
20ce7 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73 2c 20 ** its members,
20ce8 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69 63 61 skipping duplica
20ce9 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 tes..**.** The i
20cea 6e 64 65 78 20 6f 66 20 74 68 65 20 63 75 72 73 ndex of the curs
20ceb 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 or opened on the
20cec 20 62 2d 74 72 65 65 20 28 64 61 74 61 62 61 73 b-tree (databas
20ced 65 20 74 61 62 6c 65 2c 20 64 61 74 61 62 61 73 e table, databas
20cee 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 72 20 65 e index .** or e
20cef 70 68 65 72 6d 61 6c 20 74 61 62 6c 65 29 20 69 phermal table) i
20cf0 73 20 73 74 6f 72 65 64 20 69 6e 20 70 58 2d 3e s stored in pX->
20cf1 69 54 61 62 6c 65 20 62 65 66 6f 72 65 20 74 68 iTable before th
20cf2 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
20cf3 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 rns..** The retu
20cf4 72 6e 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 rned value of th
20cf5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 69 is function indi
20cf6 63 61 74 65 73 20 74 68 65 20 62 2d 74 72 65 65 cates the b-tree
20cf7 20 74 79 70 65 2c 20 61 73 20 66 6f 6c 6c 6f 77 type, as follow
20cf8 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 5f 49 4e s:.**.** IN_IN
20cf9 44 45 58 5f 52 4f 57 49 44 20 2d 20 54 68 65 20 DEX_ROWID - The
20cfa 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 cursor was opene
20cfb 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 d on a database
20cfc 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 49 4e 5f 49 table..** IN_I
20cfd 4e 44 45 58 5f 49 4e 44 45 58 20 2d 20 54 68 65 NDEX_INDEX - The
20cfe 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e cursor was open
20cff 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 ed on a database
20d00 20 69 6e 64 65 78 2e 0a 2a 2a 20 20 20 49 4e 5f index..** IN_
20d01 49 4e 44 45 58 5f 45 50 48 20 2d 20 20 20 54 68 INDEX_EPH - Th
20d02 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 e cursor was ope
20d03 6e 65 64 20 6f 6e 20 61 20 73 70 65 63 69 61 6c ned on a special
20d04 6c 79 20 63 72 65 61 74 65 64 20 61 6e 64 0a 2a ly created and.*
20d05 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
20d06 20 20 20 20 20 70 6f 70 75 6c 61 74 65 64 20 65 populated e
20d07 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e 0a pheremal table..
20d08 2a 2a 0a 2a 2a 20 41 6e 20 65 78 69 73 74 69 6e **.** An existin
20d09 67 20 62 2d 74 72 65 65 20 6d 61 79 20 6f 6e 6c g b-tree may onl
20d0a 79 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65 y be used if the
20d0b 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 68 SELECT is of th
20d0c 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 66 6f 72 6d e simple.** form
20d0d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 :.**.** SELE
20d0e 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d CT <column> FROM
20d0f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 <table>.**.** I
20d10 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e 64 f the prNotFound
20d11 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c parameter is 0,
20d12 20 74 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 then the b-tree
20d13 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f will be used to
20d14 20 69 74 65 72 61 74 65 0a 2a 2a 20 74 68 72 6f iterate.** thro
20d15 75 67 68 20 74 68 65 20 73 65 74 20 6d 65 6d 62 ugh the set memb
20d16 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 61 6e ers, skipping an
20d17 79 20 64 75 70 6c 69 63 61 74 65 73 2e 20 49 6e y duplicates. In
20d18 20 74 68 69 73 20 63 61 73 65 20 61 6e 0a 2a 2a this case an.**
20d19 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 epheremal table
20d1a 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 75 6e must be used un
20d1b 6c 65 73 73 20 74 68 65 20 73 65 6c 65 63 74 65 less the selecte
20d1c 64 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 67 75 d <column> is gu
20d1d 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 aranteed.** to b
20d1e 65 20 75 6e 69 71 75 65 20 2d 20 65 69 74 68 65 e unique - eithe
20d1f 72 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 r because it is
20d20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 an INTEGER PRIMA
20d21 52 59 20 4b 45 59 20 6f 72 20 69 74 0a 2a 2a 20 RY KEY or it.**
20d22 68 61 73 20 61 20 55 4e 49 51 55 45 20 63 6f 6e has a UNIQUE con
20d23 73 74 72 61 69 6e 74 20 6f 72 20 55 4e 49 51 55 straint or UNIQU
20d24 45 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 E index..**.** I
20d25 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e 64 f the prNotFound
20d26 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
20d27 74 20 30 2c 20 74 68 65 6e 20 74 68 65 20 62 2d t 0, then the b-
20d28 74 72 65 65 20 77 69 6c 6c 20 62 65 20 75 73 65 tree will be use
20d29 64 20 0a 2a 2a 20 66 6f 72 20 66 61 73 74 20 73 d .** for fast s
20d2a 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 et membership te
20d2b 73 74 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 sts. In this cas
20d2c 65 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 e an epheremal t
20d2d 61 62 6c 65 20 6d 75 73 74 20 0a 2a 2a 20 62 65 able must .** be
20d2e 20 75 73 65 64 20 75 6e 6c 65 73 73 20 3c 63 6f used unless <co
20d2f 6c 75 6d 6e 3e 20 69 73 20 61 6e 20 49 4e 54 45 lumn> is an INTE
20d30 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 GER PRIMARY KEY
20d31 6f 72 20 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 or an index can
20d32 0a 2a 2a 20 62 65 20 66 6f 75 6e 64 20 77 69 74 .** be found wit
20d33 68 20 3c 63 6f 6c 75 6d 6e 3e 20 61 73 20 69 74 h <column> as it
20d34 73 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 s left-most colu
20d35 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 mn..**.** When t
20d36 68 65 20 62 2d 74 72 65 65 20 69 73 20 62 65 69 he b-tree is bei
20d37 6e 67 20 75 73 65 64 20 66 6f 72 20 6d 65 6d 62 ng used for memb
20d38 65 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 68 ership tests, th
20d39 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
20d3a 6f 6e 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 6b on.** needs to k
20d3b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e now whether or n
20d3c 6f 74 20 74 68 65 20 73 74 72 75 63 74 75 72 65 ot the structure
20d3d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c contains an SQL
20d3e 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c 75 65 20 NULL .** value
20d3f 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 72 72 in order to corr
20d40 65 63 74 6c 79 20 65 76 61 6c 75 61 74 65 20 65 ectly evaluate e
20d41 78 70 72 65 73 73 69 6f 6e 73 20 6c 69 6b 65 20 xpressions like
20d42 22 58 20 49 4e 20 28 59 2c 20 5a 29 22 2e 0a 2a "X IN (Y, Z)"..*
20d43 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 * If there is a
20d44 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 chance that the
20d45 62 2d 74 72 65 65 20 6d 69 67 68 74 20 63 6f 6e b-tree might con
20d46 74 61 69 6e 20 61 20 4e 55 4c 4c 20 76 61 6c 75 tain a NULL valu
20d47 65 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2c e at.** runtime,
20d48 20 74 68 65 6e 20 61 20 72 65 67 69 73 74 65 72 then a register
20d49 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
20d4a 64 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e d the register n
20d4b 75 6d 62 65 72 20 77 72 69 74 74 65 6e 0a 2a 2a umber written.**
20d4c 20 74 6f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2e to *prNotFound.
20d4d 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
20d4e 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 chance that the
20d4f 62 2d 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 b-tree contains
20d50 61 0a 2a 2a 20 4e 55 4c 4c 20 76 61 6c 75 65 2c a.** NULL value,
20d51 20 74 68 65 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e then *prNotFoun
20d52 64 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e d is left unchan
20d53 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ged..**.** If a
20d54 72 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f register is allo
20d55 63 61 74 65 64 20 61 6e 64 20 69 74 73 20 6c 6f cated and its lo
20d56 63 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e cation stored in
20d57 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 68 *prNotFound, th
20d58 65 6e 0a 2a 2a 20 69 74 73 20 69 6e 69 74 69 61 en.** its initia
20d59 6c 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2e l value is NULL.
20d5a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20 64 If the b-tree d
20d5b 6f 65 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20 63 oes not remain c
20d5c 6f 6e 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20 74 onstant.** for t
20d5d 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 he duration of t
20d5e 68 65 20 71 75 65 72 79 20 28 69 2e 65 2e 20 74 he query (i.e. t
20d5f 68 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 67 he SELECT that g
20d60 65 6e 65 72 61 74 65 73 20 74 68 65 20 62 2d 74 enerates the b-t
20d61 72 65 65 0a 2a 2a 20 69 73 20 61 20 63 6f 72 72 ree.** is a corr
20d62 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 29 elated subquery)
20d63 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 then the value
20d64 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 of the allocated
20d65 20 72 65 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 register is.**
20d66 72 65 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 reset to NULL ea
20d67 63 68 20 74 69 6d 65 20 74 68 65 20 62 2d 74 72 ch time the b-tr
20d68 65 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74 65 ee is repopulate
20d69 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 74 d. This allows t
20d6a 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20 he.** caller to
20d6b 75 73 65 20 76 64 62 65 20 63 6f 64 65 20 65 71 use vdbe code eq
20d6c 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65 20 uivalent to the
20d6d 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
20d6e 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72 3d if( register=
20d6f 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 20 =NULL ){.**
20d70 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 74 has_null = <test
20d71 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74 75 if data structu
20d72 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c re contains null
20d73 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74 65 >.** registe
20d74 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a r = 1.** }.**.
20d75 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 ** in order to a
20d76 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 void running the
20d77 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 73 <test if data s
20d78 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e tructure contain
20d79 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 20 s null>.** test
20d7a 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 more often than
20d7b 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f is necessary..*/
20d7c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
20d7d 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 OMIT_SUBQUERY.SQ
20d7e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
20d7f 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e sqlite3FindInIn
20d80 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 dex(Parse *pPars
20d81 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e 74 e, Expr *pX, int
20d82 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20 *prNotFound){.
20d83 20 53 65 6c 65 63 74 20 2a 70 3b 20 20 20 20 20 Select *p;
20d84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d85 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 /* SELECT
20d86 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 6f 66 to the right of
20d87 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a IN operator */.
20d88 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 30 3b int eType = 0;
20d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d8a 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 /* Type
20d8b 6f 66 20 52 48 53 20 74 61 62 6c 65 2e 20 49 4e of RHS table. IN
20d8c 5f 49 4e 44 45 58 5f 2a 20 2a 2f 0a 20 20 69 6e _INDEX_* */. in
20d8d 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 65 2d t iTab = pParse-
20d8e 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 20 20 20 >nTab++;
20d8f 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 /* Cursor of
20d90 20 74 68 65 20 52 48 53 20 74 61 62 6c 65 20 2a the RHS table *
20d91 2f 0a 20 20 69 6e 74 20 6d 75 73 74 42 65 55 6e /. int mustBeUn
20d92 69 71 75 65 20 3d 20 28 70 72 4e 6f 74 46 6f 75 ique = (prNotFou
20d93 6e 64 3d 3d 30 29 3b 20 20 20 2f 2a 20 54 72 75 nd==0); /* Tru
20d94 65 20 69 66 20 52 48 53 20 6d 75 73 74 20 62 65 e if RHS must be
20d95 20 75 6e 69 71 75 65 20 2a 2f 0a 0a 20 20 2f 2a unique */.. /*
20d96 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
20d97 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 an existing tab
20d98 6c 65 20 6f 72 20 69 6e 64 65 78 20 63 61 6e 20 le or index can
20d99 62 65 20 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20 be used to. **
20d9a 73 61 74 69 73 66 79 20 74 68 65 20 71 75 65 72 satisfy the quer
20d9b 79 2e 20 20 54 68 69 73 20 69 73 20 70 72 65 66 y. This is pref
20d9c 65 72 61 62 6c 65 20 74 6f 20 67 65 6e 65 72 61 erable to genera
20d9d 74 69 6e 67 20 61 20 6e 65 77 20 0a 20 20 2a 2a ting a new . **
20d9e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 ephemeral table
20d9f 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 28 45 78 .. */. p = (Ex
20da0 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 58 prHasProperty(pX
20da1 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
20da2 3f 20 70 58 2d 3e 78 2e 70 53 65 6c 65 63 74 20 ? pX->x.pSelect
20da3 3a 20 30 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 : 0);. if( ALWA
20da4 59 53 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d YS(pParse->nErr=
20da5 3d 30 29 20 26 26 20 69 73 43 61 6e 64 69 64 61 =0) && isCandida
20da6 74 65 46 6f 72 49 6e 4f 70 74 28 70 29 20 29 7b teForInOpt(p) ){
20da7 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
20da8 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 = pParse->db;
20da9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
20daa 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
20dab 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a on */. Expr *
20dac 70 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 pExpr = p->pELis
20dad 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 20 20 t->a[0].pExpr;
20dae 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 3c /* Expression <
20daf 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20 69 column> */. i
20db0 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d nt iCol = pExpr-
20db1 3e 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 >iColumn;
20db2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
20db3 66 20 63 6f 6c 75 6d 6e 20 3c 63 6f 6c 75 6d 6e f column <column
20db4 3e 20 2a 2f 0a 20 20 20 20 56 64 62 65 20 2a 76 > */. Vdbe *v
20db5 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
20db6 65 28 70 50 61 72 73 65 29 3b 20 20 20 20 20 20 e(pParse);
20db7 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 68 69 /* Virtual machi
20db8 6e 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a ne being coded *
20db9 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 /. Table *pTa
20dba 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 b = p->pSrc->a[0
20dbb 5d 2e 70 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 ].pTab; /*
20dbc 54 61 62 6c 65 20 3c 74 61 62 6c 65 3e 2e 20 2a Table <table>. *
20dbd 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20 /. int iDb;
20dbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20dbf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20dc0 44 61 74 61 62 61 73 65 20 69 64 78 20 66 6f 72 Database idx for
20dc1 20 70 54 61 62 20 2a 2f 0a 20 20 20 0a 20 20 20 pTab */. .
20dc2 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 56 /* Code an OP_V
20dc3 65 72 69 66 79 43 6f 6f 6b 69 65 20 61 6e 64 20 erifyCookie and
20dc4 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 66 6f 72 OP_TableLock for
20dc5 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 <table>. */.
20dc6 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
20dc7 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 hemaToIndex(db,
20dc8 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
20dc9 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 sqlite3CodeV
20dca 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 erifySchema(pPar
20dcb 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 se, iDb);. sq
20dcc 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 lite3TableLock(p
20dcd 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 Parse, iDb, pTab
20dce 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d ->tnum, 0, pTab-
20dcf 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a >zName);.. /*
20dd0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
20dd1 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 s only called fr
20dd2 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 2e 20 49 om two places. I
20dd3 6e 20 62 6f 74 68 20 63 61 73 65 73 20 74 68 65 n both cases the
20dd4 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 68 61 73 vdbe. ** has
20dd5 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c already been al
20dd6 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 73 73 75 located. So assu
20dd7 6d 65 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 me sqlite3GetVdb
20dd8 65 28 29 20 69 73 20 61 6c 77 61 79 73 0a 20 20 e() is always.
20dd9 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 ** successful
20dda 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 here.. */.
20ddb 20 61 73 73 65 72 74 28 76 29 3b 0a 20 20 20 20 assert(v);.
20ddc 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 if( iCol<0 ){.
20ddd 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b int iMem = +
20dde 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
20ddf 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a int iAddr;.
20de0 0a 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 . iAddr = s
20de1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
20de2 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 (v, OP_If, iMem)
20de3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
20de4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
20de5 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d Integer, 1, iMem
20de6 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );.. sqlite
20de7 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 3OpenTable(pPars
20de8 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54 e, iTab, iDb, pT
20de9 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 ab, OP_OpenRead)
20dea 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 ;. eType =
20deb 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a IN_INDEX_ROWID;.
20dec 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
20ded 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 beJumpHere(v, iA
20dee 64 64 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ddr);. }else{
20def 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 . Index *pI
20df0 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dx;
20df1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
20df2 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 terator variable
20df3 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 */.. /* Th
20df4 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 e collation sequ
20df5 65 6e 63 65 20 75 73 65 64 20 62 79 20 74 68 65 ence used by the
20df6 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20 comparison. If
20df7 61 6e 20 69 6e 64 65 78 20 69 73 20 74 6f 0a 20 an index is to.
20df8 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65 64 20 ** be used
20df9 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 65 in place of a te
20dfa 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 6d 75 73 mp-table, it mus
20dfb 74 20 62 65 20 6f 72 64 65 72 65 64 20 61 63 63 t be ordered acc
20dfc 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 ording. **
20dfd 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f to this collatio
20dfe 6e 20 73 65 71 75 65 6e 63 65 2e 20 20 2a 2f 0a n sequence. */.
20dff 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 CollSeq *p
20e00 52 65 71 20 3d 20 73 71 6c 69 74 65 33 42 69 6e Req = sqlite3Bin
20e01 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 aryCompareCollSe
20e02 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c q(pParse, pX->pL
20e03 65 66 74 2c 20 70 45 78 70 72 29 3b 0a 0a 20 20 eft, pExpr);..
20e04 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 /* Check tha
20e05 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 t the affinity t
20e06 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 hat will be used
20e07 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20 to perform the
20e08 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 . ** compar
20e09 69 73 6f 6e 20 69 73 20 74 68 65 20 73 61 6d 65 ison is the same
20e0a 20 61 73 20 74 68 65 20 61 66 66 69 6e 69 74 79 as the affinity
20e0b 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 of the column.
20e0c 49 66 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69 If. ** it i
20e0d 73 20 6e 6f 74 2c 20 69 74 20 69 73 20 6e 6f 74 s not, it is not
20e0e 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 possible to use
20e0f 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20 20 20 20 any index..
20e10 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 */. char
20e11 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e aff = comparison
20e12 41 66 66 69 6e 69 74 79 28 70 58 29 3b 0a 20 20 Affinity(pX);.
20e13 20 20 20 20 69 6e 74 20 61 66 66 69 6e 69 74 79 int affinity
20e14 5f 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f _ok = (pTab->aCo
20e15 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 l[iCol].affinity
20e16 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49 ==aff||aff==SQLI
20e17 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 TE_AFF_NONE);..
20e18 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 for(pIdx=pT
20e19 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 ab->pIndex; pIdx
20e1a 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20 && eType==0 &&
20e1b 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64 affinity_ok; pId
20e1c 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a x=pIdx->pNext){.
20e1d 20 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64 if( (pId
20e1e 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d x->aiColumn[0]==
20e1f 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 26 iCol). &
20e20 26 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c & sqlite3FindCol
20e21 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 lSeq(db, ENC(db)
20e22 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 , pIdx->azColl[0
20e23 5d 2c 20 30 29 3d 3d 70 52 65 71 0a 20 20 20 20 ], 0)==pReq.
20e24 20 20 20 20 20 26 26 20 28 21 6d 75 73 74 42 65 && (!mustBe
20e25 55 6e 69 71 75 65 20 7c 7c 20 28 70 49 64 78 2d Unique || (pIdx-
20e26 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70 >nColumn==1 && p
20e27 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 Idx->onError!=OE
20e28 5f 4e 6f 6e 65 29 29 0a 20 20 20 20 20 20 20 20 _None)).
20e29 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
20e2a 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 iMem = ++pParse
20e2b 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 ->nMem;.
20e2c 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 int iAddr;.
20e2d 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 4b 65 char *pKe
20e2e 79 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 y;. .
20e2f 70 4b 65 79 20 3d 20 28 63 68 61 72 20 2a 29 73 pKey = (char *)s
20e30 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e qlite3IndexKeyin
20e31 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 fo(pParse, pIdx)
20e32 3b 0a 20 20 20 20 20 20 20 20 20 20 69 41 64 64 ;. iAdd
20e33 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 r = sqlite3VdbeA
20e34 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 ddOp1(v, OP_If,
20e35 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 20 iMem);.
20e36 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
20e37 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
20e38 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 20 20 0a 20 , 1, iMem);. .
20e39 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
20e3a 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 VdbeAddOp4(v, OP
20e3b 5f 4f 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c _OpenRead, iTab,
20e3c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 pIdx->tnum, iDb
20e3d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
20e3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20e3f 20 70 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f pKey,P4_KEYINFO
20e40 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 _HANDOFF);.
20e41 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 VdbeComment
20e42 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d ((v, "%s", pIdx-
20e43 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 >zName));.
20e44 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 eType = IN_I
20e45 4e 44 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 NDEX_INDEX;..
20e46 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
20e47 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 beJumpHere(v, iA
20e48 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ddr);.
20e49 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 if( prNotFound &
20e4a 26 20 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 & !pTab->aCol[iC
20e4b 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 ol].notNull ){.
20e4c 20 20 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f *prNo
20e4d 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 tFound = ++pPars
20e4e 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 e->nMem;.
20e4f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
20e50 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
20e51 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30 .. if( eType==0
20e52 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 75 6c 64 ){. /* Could
20e53 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 20 65 78 not found an ex
20e54 69 73 74 69 6e 67 20 61 62 6c 65 20 6f 72 20 69 isting able or i
20e55 6e 64 65 78 20 74 6f 20 75 73 65 20 61 73 20 74 ndex to use as t
20e56 68 65 20 52 48 53 20 62 2d 74 72 65 65 2e 0a 20 he RHS b-tree..
20e57 20 20 20 2a 2a 20 57 65 20 77 69 6c 6c 20 68 61 ** We will ha
20e58 76 65 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 ve to generate a
20e59 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c n ephemeral tabl
20e5a 65 20 74 6f 20 64 6f 20 74 68 65 20 6a 6f 62 2e e to do the job.
20e5b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 . */. int
20e5c 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 30 rMayHaveNull = 0
20e5d 3b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e ;. eType = IN
20e5e 5f 49 4e 44 45 58 5f 45 50 48 3b 0a 20 20 20 20 _INDEX_EPH;.
20e5f 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 29 if( prNotFound )
20e60 7b 0a 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f {. *prNotFo
20e61 75 6e 64 20 3d 20 72 4d 61 79 48 61 76 65 4e 75 und = rMayHaveNu
20e62 6c 6c 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e ll = ++pParse->n
20e63 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 Mem;. }else i
20e64 66 28 20 70 58 2d 3e 70 4c 65 66 74 2d 3e 69 43 f( pX->pLeft->iC
20e65 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 45 78 70 72 olumn<0 && !Expr
20e66 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 HasAnyProperty(p
20e67 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 X, EP_xIsSelect)
20e68 20 29 7b 0a 20 20 20 20 20 20 65 54 79 70 65 20 ){. eType
20e69 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 = IN_INDEX_ROWID
20e6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
20e6b 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 te3CodeSubselect
20e6c 28 70 50 61 72 73 65 2c 20 70 58 2c 20 72 4d 61 (pParse, pX, rMa
20e6d 79 48 61 76 65 4e 75 6c 6c 2c 20 65 54 79 70 65 yHaveNull, eType
20e6e 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 ==IN_INDEX_ROWID
20e6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
20e70 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 pX->iTable = iTa
20e71 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 b;. }. return
20e72 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a eType;.}.#endif.
20e73 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
20e74 63 6f 64 65 20 66 6f 72 20 73 63 61 6c 61 72 20 code for scalar
20e75 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64 20 subqueries used
20e76 61 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e as an expression
20e77 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f 70 65 72 61 .** and IN opera
20e78 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c 65 73 3a tors. Examples:
20e79 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53 45 4c 45 .**.** (SELE
20e7a 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 CT a FROM b)
20e7b 20 20 20 20 20 20 2d 2d 20 73 75 62 71 75 65 72 -- subquer
20e7c 79 0a 2a 2a 20 20 20 20 20 45 58 49 53 54 53 20 y.** EXISTS
20e7d 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 (SELECT a FROM b
20e7e 29 20 20 20 2d 2d 20 45 58 49 53 54 53 20 73 75 ) -- EXISTS su
20e7f 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 78 20 bquery.** x
20e80 49 4e 20 28 34 2c 35 2c 31 31 29 20 20 20 20 20 IN (4,5,11)
20e81 20 20 20 20 20 20 20 20 20 2d 2d 20 49 4e 20 6f -- IN o
20e82 70 65 72 61 74 6f 72 20 77 69 74 68 20 6c 69 73 perator with lis
20e83 74 20 6f 6e 20 72 69 67 68 74 2d 68 61 6e 64 20 t on right-hand
20e84 73 69 64 65 0a 2a 2a 20 20 20 20 20 78 20 49 4e side.** x IN
20e85 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 (SELECT a FROM
20e86 62 29 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65 b) -- IN ope
20e87 72 61 74 6f 72 20 77 69 74 68 20 73 75 62 71 75 rator with subqu
20e88 65 72 79 20 6f 6e 20 74 68 65 20 72 69 67 68 74 ery on the right
20e89 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 .**.** The pExpr
20e8a 20 70 61 72 61 6d 65 74 65 72 20 64 65 73 63 72 parameter descr
20e8b 69 62 65 73 20 74 68 65 20 65 78 70 72 65 73 73 ibes the express
20e8c 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ion that contain
20e8d 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f 70 65 72 s the IN.** oper
20e8e 61 74 6f 72 20 6f 72 20 73 75 62 71 75 65 72 79 ator or subquery
20e8f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d ..**.** If param
20e90 65 74 65 72 20 69 73 52 6f 77 69 64 20 69 73 20 eter isRowid is
20e91 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 65 non-zero, then e
20e92 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 xpression pExpr
20e93 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a is guaranteed.**
20e94 20 74 6f 20 62 65 20 6f 66 20 74 68 65 20 66 6f to be of the fo
20e95 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49 4e 20 28 rm "<rowid> IN (
20e96 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68 65 72 65 ?, ?, ?)", where
20e97 20 3c 72 6f 77 69 64 3e 20 69 73 20 61 20 72 65 <rowid> is a re
20e98 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f 20 73 6f ference.** to so
20e99 6d 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 63 me integer key c
20e9a 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 62 6c 65 olumn of a table
20e9b 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 69 73 B-Tree. In this
20e9c 20 63 61 73 65 2c 20 75 73 65 20 61 6e 0a 2a 2a case, use an.**
20e9d 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 20 74 intkey B-Tree t
20e9e 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 o store the set
20e9f 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61 6c 75 65 of IN(...) value
20ea0 73 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 s instead of the
20ea1 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c 6f 77 65 usual.** (slowe
20ea2 72 29 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 r) variable leng
20ea3 74 68 20 6b 65 79 73 20 42 2d 54 72 65 65 2e 0a th keys B-Tree..
20ea4 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79 48 61 76 **.** If rMayHav
20ea5 65 4e 75 6c 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72 eNull is non-zer
20ea6 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 o, that means th
20ea7 61 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e at the operation
20ea8 20 69 73 20 61 6e 20 49 4e 0a 2a 2a 20 28 6e 6f is an IN.** (no
20ea9 74 20 61 20 53 45 4c 45 43 54 20 6f 72 20 45 58 t a SELECT or EX
20eaa 49 53 54 53 29 20 61 6e 64 20 74 68 61 74 20 74 ISTS) and that t
20eab 68 65 20 52 48 53 20 6d 69 67 68 74 20 63 6f 6e he RHS might con
20eac 74 61 69 6e 73 20 4e 55 4c 4c 73 2e 0a 2a 2a 20 tains NULLs..**
20ead 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 74 68 65 Furthermore, the
20eae 20 49 4e 20 69 73 20 69 6e 20 61 20 57 48 45 52 IN is in a WHER
20eaf 45 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 61 E clause and tha
20eb0 74 20 77 65 20 72 65 61 6c 6c 79 20 77 61 6e 74 t we really want
20eb1 0a 2a 2a 20 74 6f 20 69 74 65 72 61 74 65 20 6f .** to iterate o
20eb2 76 65 72 20 74 68 65 20 52 48 53 20 6f 66 20 74 ver the RHS of t
20eb3 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69 he IN operator i
20eb4 6e 20 6f 72 64 65 72 20 74 6f 20 71 75 69 63 6b n order to quick
20eb5 6c 79 20 6c 6f 63 61 74 65 0a 2a 2a 20 61 6c 6c ly locate.** all
20eb6 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 4c corresponding L
20eb7 48 53 20 65 6c 65 6d 65 6e 74 73 2e 20 20 41 6c HS elements. Al
20eb8 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 64 l this routine d
20eb9 6f 65 73 20 69 73 20 69 6e 69 74 69 61 6c 69 7a oes is initializ
20eba 65 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 e.** the registe
20ebb 72 20 67 69 76 65 6e 20 62 79 20 72 4d 61 79 48 r given by rMayH
20ebc 61 76 65 4e 75 6c 6c 20 74 6f 20 4e 55 4c 4c 2e aveNull to NULL.
20ebd 20 20 43 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e Calling routin
20ebe 65 73 20 77 69 6c 6c 20 74 61 6b 65 0a 2a 2a 20 es will take.**
20ebf 63 61 72 65 20 6f 66 20 63 68 61 6e 67 69 6e 67 care of changing
20ec0 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 76 this register v
20ec1 61 6c 75 65 20 74 6f 20 6e 6f 6e 2d 4e 55 4c 4c alue to non-NULL
20ec2 20 69 66 20 74 68 65 20 52 48 53 20 69 73 20 4e if the RHS is N
20ec3 55 4c 4c 2d 66 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 ULL-free..**.**
20ec4 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 If rMayHaveNull
20ec5 69 73 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 is zero, that me
20ec6 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 75 62 ans that the sub
20ec7 71 75 65 72 79 20 69 73 20 62 65 69 6e 67 20 75 query is being u
20ec8 73 65 64 0a 2a 2a 20 66 6f 72 20 6d 65 6d 62 65 sed.** for membe
20ec9 72 73 68 69 70 20 74 65 73 74 69 6e 67 20 6f 6e rship testing on
20eca 6c 79 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f ly. There is no
20ecb 20 6e 65 65 64 20 74 6f 20 69 6e 69 74 69 61 6c need to initial
20ecc 69 7a 65 20 61 6e 79 0a 2a 2a 20 72 65 67 69 73 ize any.** regis
20ecd 74 65 72 73 20 74 6f 20 69 6e 64 69 63 61 74 65 ters to indicate
20ece 20 74 68 65 20 70 72 65 73 65 6e 73 65 20 6f 72 the presense or
20ecf 20 61 62 73 65 6e 63 65 20 6f 66 20 4e 55 4c 4c absence of NULL
20ed0 73 20 6f 6e 20 74 68 65 20 52 48 53 2e 0a 2a 2f s on the RHS..*/
20ed1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
20ed2 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 OMIT_SUBQUERY.SQ
20ed3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20ed4 64 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 d sqlite3CodeSub
20ed5 73 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 select(. Parse
20ed6 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
20ed7 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
20ed8 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a text */. Expr *
20ed9 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 pExpr,
20eda 20 20 2f 2a 20 54 68 65 20 49 4e 2c 20 53 45 4c /* The IN, SEL
20edb 45 43 54 2c 20 6f 72 20 45 58 49 53 54 53 20 6f ECT, or EXISTS o
20edc 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 perator */. int
20edd 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 20 rMayHaveNull,
20ede 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
20edf 20 74 68 61 74 20 72 65 63 6f 72 64 73 20 77 68 that records wh
20ee0 65 74 68 65 72 20 4e 55 4c 4c 73 20 65 78 69 73 ether NULLs exis
20ee1 74 20 69 6e 20 52 48 53 20 2a 2f 0a 20 20 69 6e t in RHS */. in
20ee2 74 20 69 73 52 6f 77 69 64 20 20 20 20 20 20 20 t isRowid
20ee3 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 /* If true
20ee4 2c 20 4c 48 53 20 6f 66 20 49 4e 20 6f 70 65 72 , LHS of IN oper
20ee5 61 74 6f 72 20 69 73 20 61 20 72 6f 77 69 64 20 ator is a rowid
20ee6 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 */.){. int test
20ee7 41 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 Addr = 0;
20ee8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20ee9 2f 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 /* One-time test
20eea 20 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 address */. Vd
20eeb 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 be *v = sqlite3G
20eec 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a etVdbe(pParse);.
20eed 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 if( NEVER(v==0
20eee 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 ) ) return;. sq
20eef 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 lite3ExprCachePu
20ef0 73 68 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 2f sh(pParse);.. /
20ef1 2a 20 54 68 69 73 20 63 6f 64 65 20 6d 75 73 74 * This code must
20ef2 20 62 65 20 72 75 6e 20 69 6e 20 69 74 73 20 65 be run in its e
20ef3 6e 74 69 72 65 74 79 20 65 76 65 72 79 20 74 69 ntirety every ti
20ef4 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74 me it is encount
20ef5 65 72 65 64 0a 20 20 2a 2a 20 69 66 20 61 6e 79 ered. ** if any
20ef6 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
20ef7 67 20 69 73 20 74 72 75 65 3a 0a 20 20 2a 2a 0a g is true:. **.
20ef8 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 ** * The r
20ef9 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 ight-hand side i
20efa 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73 s a correlated s
20efb 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 20 20 20 ubquery. **
20efc 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e * The right-han
20efd 64 20 73 69 64 65 20 69 73 20 61 6e 20 65 78 70 d side is an exp
20efe 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e ression list con
20eff 74 61 69 6e 69 6e 67 20 76 61 72 69 61 62 6c 65 taining variable
20f00 73 0a 20 20 2a 2a 20 20 20 20 2a 20 20 57 65 20 s. ** * We
20f01 61 72 65 20 69 6e 73 69 64 65 20 61 20 74 72 69 are inside a tri
20f02 67 67 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 gger. **. ** I
20f03 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f f all of the abo
20f04 76 65 20 61 72 65 20 66 61 6c 73 65 2c 20 74 68 ve are false, th
20f05 65 6e 20 77 65 20 63 61 6e 20 72 75 6e 20 74 68 en we can run th
20f06 69 73 20 63 6f 64 65 20 6a 75 73 74 20 6f 6e 63 is code just onc
20f07 65 0a 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20 e. ** save the
20f08 72 65 73 75 6c 74 73 2c 20 61 6e 64 20 72 65 75 results, and reu
20f09 73 65 20 74 68 65 20 73 61 6d 65 20 72 65 73 75 se the same resu
20f0a 6c 74 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74 lt on subsequent
20f0b 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 invocations..
20f0c 2a 2f 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 */. if( !ExprHa
20f0d 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 sAnyProperty(pEx
20f0e 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74 pr, EP_VarSelect
20f0f 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54 ) && !pParse->pT
20f10 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20 riggerTab ){.
20f11 20 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61 int mem = ++pPa
20f12 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 rse->nMem;. s
20f13 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
20f14 28 76 2c 20 4f 50 5f 49 66 2c 20 6d 65 6d 29 3b (v, OP_If, mem);
20f15 0a 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 . testAddr =
20f16 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20f17 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
20f18 20 31 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 61 73 1, mem);. as
20f19 73 65 72 74 28 20 74 65 73 74 41 64 64 72 3e 30 sert( testAddr>0
20f1a 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e || pParse->db->
20f1b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
20f1c 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70 }.. switch( p
20f1d 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 Expr->op ){.
20f1e 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 case TK_IN: {.
20f1f 20 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 char affinit
20f20 79 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f y;. KeyInfo
20f21 20 6b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 keyInfo;.
20f22 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 int addr;
20f23 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f /* Address of O
20f24 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 P_OpenEphemeral
20f25 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 instruction */.
20f26 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 Expr *pLeft
20f27 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b = pExpr->pLeft;
20f28 0a 0a 20 20 20 20 20 20 69 66 28 20 72 4d 61 79 .. if( rMay
20f29 48 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 HaveNull ){.
20f2a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
20f2b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c ddOp2(v, OP_Null
20f2c 2c 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c , 0, rMayHaveNul
20f2d 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 l);. }..
20f2e 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20 73 71 affinity = sq
20f2f 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 lite3ExprAffinit
20f30 79 28 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 y(pLeft);..
20f31 20 2f 2a 20 57 68 65 74 68 65 72 20 74 68 69 73 /* Whether this
20f32 20 69 73 20 61 6e 20 27 78 20 49 4e 28 53 45 4c is an 'x IN(SEL
20f33 45 43 54 2e 2e 2e 29 27 20 6f 72 20 61 6e 20 27 ECT...)' or an '
20f34 78 20 49 4e 28 3c 65 78 70 72 6c 69 73 74 3e 29 x IN(<exprlist>)
20f35 27 0a 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 '. ** expre
20f36 73 73 69 6f 6e 20 69 74 20 69 73 20 68 61 6e 64 ssion it is hand
20f37 6c 65 64 20 74 68 65 20 73 61 6d 65 20 77 61 79 led the same way
20f38 2e 20 41 20 76 69 72 74 75 61 6c 20 74 61 62 6c . A virtual tabl
20f39 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 e is . ** f
20f3a 69 6c 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c illed with singl
20f3b 65 2d 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 e-field index ke
20f3c 79 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 ys representing
20f3d 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 the results.
20f3e 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 ** from the SE
20f3f 4c 45 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 LECT or the <exp
20f40 72 6c 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a rlist>.. **
20f41 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 . ** If the
20f42 20 27 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 'x' expression
20f43 69 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 is a column valu
20f44 65 2c 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 e, or the SELECT
20f45 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 .... ** sta
20f46 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 tement returns a
20f47 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 column value, t
20f48 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 hen the affinity
20f49 20 6f 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a of that. *
20f4a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 * column is used
20f4b 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e to build the in
20f4c 64 65 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 dex keys. If bot
20f4d 68 20 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 h 'x' and the.
20f4e 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e ** SELECT...
20f4f 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 statement are c
20f50 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d olumns, then num
20f51 65 72 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 eric affinity is
20f52 20 75 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 used. ** i
20f53 66 20 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 f either column
20f54 68 61 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 has NUMERIC or I
20f55 4e 54 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e NTEGER affinity.
20f56 20 49 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 If neither.
20f57 20 20 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 ** 'x' nor the
20f58 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 SELECT... state
20f59 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 ment are columns
20f5a 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 , then numeric a
20f5b 66 66 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a ffinity. **
20f5c 20 69 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 is used..
20f5d 2a 2f 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e */. pExpr->
20f5e 69 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d iTable = pParse-
20f5f 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 >nTab++;. a
20f60 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
20f61 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 eAddOp2(v, OP_Op
20f62 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 enEphemeral, pEx
20f63 70 72 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 pr->iTable, !isR
20f64 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d owid);. mem
20f65 73 65 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c set(&keyInfo, 0,
20f66 20 73 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 sizeof(keyInfo)
20f67 29 3b 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f );. keyInfo
20f68 2e 6e 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 .nField = 1;..
20f69 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 if( ExprHasP
20f6a 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
20f6b 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a P_xIsSelect) ){.
20f6c 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 /* Case
20f6d 31 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 1: expr IN (
20f6e 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 SELECT ...).
20f6f 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
20f70 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
20f71 74 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73 to write the res
20f72 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 ults of the sele
20f73 63 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 ct into the temp
20f74 6f 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a orary. **
20f75 20 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 table allocated
20f76 20 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 and opened abov
20f77 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 e.. */.
20f78 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 SelectDest
20f79 20 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 dest;. E
20f7a 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b xprList *pEList;
20f7b 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 .. assert
20f7c 28 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 ( !isRowid );.
20f7d 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c sqlite3Sel
20f7e 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 ectDestInit(&des
20f7f 74 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 t, SRT_Set, pExp
20f80 72 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 r->iTable);.
20f81 20 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 dest.affinit
20f82 79 20 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 y = (u8)affinity
20f83 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
20f84 28 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 ( (pExpr->iTable
20f85 26 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 &0x0000FFFF)==pE
20f86 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 xpr->iTable );.
20f87 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
20f88 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c e3Select(pParse,
20f89 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 pExpr->x.pSelec
20f8a 74 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 t, &dest) ){.
20f8b 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
20f8c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
20f8d 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d pEList = pExpr-
20f8e 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 >x.pSelect->pELi
20f8f 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 st;. if(
20f90 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21 3d 30 ALWAYS(pEList!=0
20f91 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 && pEList->nExp
20f92 72 3e 30 29 20 29 7b 20 0a 20 20 20 20 20 20 20 r>0) ){ .
20f93 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c keyInfo.aColl
20f94 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 6e [0] = sqlite3Bin
20f95 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 aryCompareCollSe
20f96 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d q(pParse, pExpr-
20f97 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 20 20 >pLeft,.
20f98 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b pEList->a[
20f99 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 0].pExpr);.
20f9a 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
20f9b 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c if( pExpr->x.pL
20f9c 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 ist!=0 ){.
20f9d 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 20 /* Case 2:
20f9e 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c 69 expr IN (exprli
20f9f 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 st). **.
20fa0 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 ** For ea
20fa1 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 ch expression, b
20fa2 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 uild an index ke
20fa3 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 y from the evalu
20fa4 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 ation and.
20fa5 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e ** store it in
20fa6 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 the temporary t
20fa7 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 able. If <expr>
20fa8 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 is a column, the
20fa9 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a n use. **
20faa 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 that columns af
20fab 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69 6c finity when buil
20fac 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 2e ding index keys.
20fad 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f If <expr> is no
20fae 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 t. ** a c
20faf 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 olumn, use numer
20fb0 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 ic affinity..
20fb1 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
20fb2 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 45 int i;. E
20fb3 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d xprList *pList =
20fb4 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b pExpr->x.pList;
20fb5 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 . struct
20fb6 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 ExprList_item *p
20fb7 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e Item;. in
20fb8 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20 t r1, r2, r3;..
20fb9 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 69 if( !affi
20fba 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20 nity ){.
20fbb 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c affinity = SQL
20fbc 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 ITE_AFF_NONE;.
20fbd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
20fbe 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d keyInfo.aColl[0]
20fbf 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
20fc0 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 llSeq(pParse, pE
20fc1 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 xpr->pLeft);..
20fc2 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 /* Loop th
20fc3 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65 rough each expre
20fc4 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69 ssion in <exprli
20fc5 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 st>. */.
20fc6 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r1 = sqlite3GetT
20fc7 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
20fc8 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c r2 = sql
20fc9 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
20fca 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
20fcb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
20fcc 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
20fcd 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 6f r2);. fo
20fce 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 r(i=pList->nExpr
20fcf 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 , pItem=pList->a
20fd0 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 ; i>0; i--, pIte
20fd1 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 m++){.
20fd2 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65 Expr *pE2 = pIte
20fd3 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20 m->pExpr;..
20fd4 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 /* If the e
20fd5 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 xpression is not
20fd6 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77 constant then w
20fd7 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20 e will need to.
20fd8 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61 ** disa
20fd9 62 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61 ble the test tha
20fda 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 t was generated
20fdb 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73 above that makes
20fdc 20 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20 sure.
20fdd 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c ** this code onl
20fde 79 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e y executes once.
20fdf 20 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20 Because for a
20fe0 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20 non-constant.
20fe1 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 ** expres
20fe2 73 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 sion we need to
20fe3 72 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 rerun this code
20fe4 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20 each time..
20fe5 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
20fe6 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 26 if( testAddr &
20fe7 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 & !sqlite3ExprIs
20fe8 43 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b Constant(pE2) ){
20fe9 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
20fea 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f ite3VdbeChangeTo
20feb 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72 Noop(v, testAddr
20fec 2d 31 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 -1, 2);.
20fed 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 30 testAddr = 0
20fee 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 ;. }..
20fef 20 20 20 20 20 20 20 20 20 2f 2a 20 45 76 61 6c /* Eval
20ff0 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73 uate the express
20ff1 69 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74 20 69 ion and insert i
20ff2 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 20 t into the temp
20ff3 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 table */.
20ff4 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45 r3 = sqlite3E
20ff5 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 xprCodeTarget(pP
20ff6 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a arse, pE2, r1);.
20ff7 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 if( is
20ff8 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 Rowid ){.
20ff9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20ffa 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 AddOp2(v, OP_Mus
20ffb 74 42 65 49 6e 74 2c 20 72 33 2c 20 73 71 6c 69 tBeInt, r3, sqli
20ffc 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 te3VdbeCurrentAd
20ffd 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 20 dr(v)+2);.
20ffe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
20fff 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e eAddOp3(v, OP_In
21000 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 sert, pExpr->iTa
21001 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a 20 20 ble, r2, r3);.
21002 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
21003 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
21004 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
21005 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 OP_MakeRecord, r
21006 33 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69 6e 3, 1, r2, &affin
21007 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 ity, 1);.
21008 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21009 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
2100a 6e 67 65 28 70 50 61 72 73 65 2c 20 72 33 2c 20 nge(pParse, r3,
2100b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1);.
2100c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
2100d 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 2(v, OP_IdxInser
2100e 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 t, pExpr->iTable
2100f 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20 , r2);.
21010 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
21011 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
21012 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
21013 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 e, r1);.
21014 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
21015 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 mpReg(pParse, r2
21016 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
21017 20 69 66 28 20 21 69 73 52 6f 77 69 64 20 29 7b if( !isRowid ){
21018 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21019 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 VdbeChangeP4(v,
2101a 61 64 64 72 2c 20 28 76 6f 69 64 20 2a 29 26 6b addr, (void *)&k
2101b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e eyInfo, P4_KEYIN
2101c 46 4f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 FO);. }.
2101d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
2101e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 . case TK_EXI
2101f 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b STS:. case TK
21020 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20 64 65 66 _SELECT:. def
21021 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 2f 2a ault: {. /*
21022 20 49 66 20 74 68 69 73 20 68 61 73 20 74 6f 20 If this has to
21023 62 65 20 61 20 73 63 61 6c 61 72 20 53 45 4c 45 be a scalar SELE
21024 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f CT. Generate co
21025 64 65 20 74 6f 20 70 75 74 20 74 68 65 0a 20 20 de to put the.
21026 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 ** value of
21027 74 68 69 73 20 73 65 6c 65 63 74 20 69 6e 20 61 this select in a
21028 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 memory cell and
21029 20 72 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 record the numb
2102a 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 er. ** of t
2102b 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 he memory cell i
2102c 6e 20 69 43 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 n iColumn. If t
2102d 68 69 73 20 69 73 20 61 6e 20 45 58 49 53 54 53 his is an EXISTS
2102e 2c 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a , write. **
2102f 20 61 6e 20 69 6e 74 65 67 65 72 20 30 20 28 6e an integer 0 (n
21030 6f 74 20 65 78 69 73 74 73 29 20 6f 72 20 31 20 ot exists) or 1
21031 28 65 78 69 73 74 73 29 20 69 6e 74 6f 20 61 20 (exists) into a
21032 6d 65 6d 6f 72 79 20 63 65 6c 6c 0a 20 20 20 20 memory cell.
21033 20 20 2a 2a 20 61 6e 64 20 72 65 63 6f 72 64 20 ** and record
21034 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c that memory cell
21035 20 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20 in iColumn..
21036 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 */. stat
21037 69 63 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f ic const Token o
21038 6e 65 20 3d 20 7b 20 22 31 22 2c 20 31 20 7d 3b ne = { "1", 1 };
21039 20 20 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 6c /* Token for l
2103a 69 74 65 72 61 6c 20 76 61 6c 75 65 20 31 20 2a iteral value 1 *
2103b 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a /. Select *
2103c 70 53 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 pSel;
2103d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2103e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
2103f 74 20 74 6f 20 65 6e 63 6f 64 65 20 2a 2f 0a 20 t to encode */.
21040 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 SelectDest
21041 64 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 dest;
21042 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f /* Ho
21043 77 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 53 w to deal with S
21044 45 4c 45 43 74 20 72 65 73 75 6c 74 20 2a 2f 0a ELECt result */.
21045 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21046 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 pExpr->op==TK_E
21047 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 XISTS );. t
21048 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
21049 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b op==TK_SELECT );
2104a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
2104b 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 Expr->op==TK_EXI
2104c 53 54 53 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 STS || pExpr->op
2104d 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 0a ==TK_SELECT );..
2104e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 45 78 assert( Ex
2104f 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
21050 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 xpr, EP_xIsSelec
21051 74 29 20 29 3b 0a 20 20 20 20 20 20 70 53 65 6c t) );. pSel
21052 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c = pExpr->x.pSel
21053 65 63 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ect;. sqlit
21054 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 e3SelectDestInit
21055 28 26 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61 (&dest, 0, ++pPa
21056 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 rse->nMem);.
21057 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d if( pExpr->op=
21058 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 =TK_SELECT ){.
21059 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 dest.eDest
2105a 20 3d 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 = SRT_Mem;.
2105b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2105c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c ddOp2(v, OP_Null
2105d 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 , 0, dest.iParm)
2105e 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f ;. VdbeCo
2105f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 mment((v, "Init
21060 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 22 subquery result"
21061 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b ));. }else{
21062 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65 44 . dest.eD
21063 65 73 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73 est = SRT_Exists
21064 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
21065 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21066 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65 P_Integer, 0, de
21067 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 st.iParm);.
21068 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 VdbeComment((
21069 76 2c 20 22 49 6e 69 74 20 45 58 49 53 54 53 20 v, "Init EXISTS
2106a 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 result"));.
2106b 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
2106c 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 ExprDelete(pPars
2106d 65 2d 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69 e->db, pSel->pLi
2106e 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c mit);. pSel
2106f 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 ->pLimit = sqlit
21070 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 e3PExpr(pParse,
21071 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 TK_INTEGER, 0, 0
21072 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69 , &one);. i
21073 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 f( sqlite3Select
21074 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26 (pParse, pSel, &
21075 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 dest) ){.
21076 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
21077 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 . pExpr->iC
21078 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 64 65 73 olumn = (i16)des
21079 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 45 t.iParm;. E
2107a 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c xprSetIrreducibl
2107b 65 28 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 e(pExpr);.
2107c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
2107d 0a 0a 20 20 69 66 28 20 74 65 73 74 41 64 64 72 .. if( testAddr
2107e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
2107f 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 dbeJumpHere(v, t
21080 65 73 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d 0a estAddr-1);. }.
21081 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
21082 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 hePop(pParse, 1)
21083 3b 0a 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 ;.. return;.}.#
21084 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
21085 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f OMIT_SUBQUERY */
21086 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74 ../*.** Duplicat
21087 65 20 61 6e 20 38 2d 62 79 74 65 20 76 61 6c 75 e an 8-byte valu
21088 65 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 e.*/.static char
21089 20 2a 64 75 70 38 62 79 74 65 73 28 56 64 62 65 *dup8bytes(Vdbe
2108a 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *v, const char
2108b 2a 69 6e 29 7b 0a 20 20 63 68 61 72 20 2a 6f 75 *in){. char *ou
2108c 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
2108d 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 56 64 locRaw(sqlite3Vd
2108e 62 65 44 62 28 76 29 2c 20 38 29 3b 0a 20 20 69 beDb(v), 8);. i
2108f 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 6d 65 f( out ){. me
21090 6d 63 70 79 28 6f 75 74 2c 20 69 6e 2c 20 38 29 mcpy(out, in, 8)
21091 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f ;. }. return o
21092 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e ut;.}../*.** Gen
21093 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63 erate an instruc
21094 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 tion that will p
21095 75 74 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 ut the floating
21096 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 64 point.** value d
21097 65 73 63 72 69 62 65 64 20 62 79 20 7a 5b 30 2e escribed by z[0.
21098 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 .n-1] into regis
21099 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 ter iMem..**.**
2109a 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 The z[] string w
2109b 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 ill probably not
2109c 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 be zero-termina
2109d 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a ted. But the .*
2109e 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 * z[n] character
2109f 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
210a0 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 o be something t
210a1 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f hat does not loo
210a2 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f k.** like the co
210a3 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 ntinuation of th
210a4 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
210a5 74 69 63 20 76 6f 69 64 20 63 6f 64 65 52 65 61 tic void codeRea
210a6 6c 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74 l(Vdbe *v, const
210a7 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 65 char *z, int ne
210a8 67 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 4d gateFlag, int iM
210a9 65 6d 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 em){. if( ALWAY
210aa 53 28 7a 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 S(z!=0) ){. d
210ab 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20 20 20 ouble value;.
210ac 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 73 char *zV;. s
210ad 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20 26 76 qlite3AtoF(z, &v
210ae 61 6c 75 65 29 3b 0a 20 20 20 20 61 73 73 65 72 alue);. asser
210af 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e 61 4e t( !sqlite3IsNaN
210b0 28 76 61 6c 75 65 29 20 29 3b 20 2f 2a 20 54 68 (value) ); /* Th
210b1 65 20 6e 65 77 20 41 74 6f 46 20 6e 65 76 65 72 e new AtoF never
210b2 20 72 65 74 75 72 6e 73 20 4e 61 4e 20 2a 2f 0a returns NaN */.
210b3 20 20 20 20 69 66 28 20 6e 65 67 61 74 65 46 6c if( negateFl
210b4 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 ag ) value = -va
210b5 6c 75 65 3b 0a 20 20 20 20 7a 56 20 3d 20 64 75 lue;. zV = du
210b6 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 72 p8bytes(v, (char
210b7 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 73 *)&value);. s
210b8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
210b9 28 76 2c 20 4f 50 5f 52 65 61 6c 2c 20 30 2c 20 (v, OP_Real, 0,
210ba 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f iMem, 0, zV, P4_
210bb 52 45 41 4c 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f REAL);. }.}.../
210bc 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e *.** Generate an
210bd 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
210be 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 69 t will put the i
210bf 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65 20 nteger describe
210c0 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e 2e by.** text z[0..
210c1 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 74 n-1] into regist
210c2 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 er iMem..**.** T
210c3 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69 he z[] string wi
210c4 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 ll probably not
210c5 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 be zero-terminat
210c6 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a 2a ed. But the .**
210c7 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 20 z[n] character
210c8 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f is guaranteed to
210c9 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 be something th
210ca 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b at does not look
210cb 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e .** like the con
210cc 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 65 tinuation of the
210cd 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 number..*/.stat
210ce 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74 65 ic void codeInte
210cf 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78 70 ger(Vdbe *v, Exp
210d0 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e 65 r *pExpr, int ne
210d1 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29 gFlag, int iMem)
210d2 7b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 {. if( pExpr->f
210d3 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61 6c lags & EP_IntVal
210d4 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 ue ){. int i
210d5 3d 20 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75 = pExpr->u.iValu
210d6 65 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c e;. if( negFl
210d7 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20 ag ) i = -i;.
210d8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
210d9 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
210da 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 65 , i, iMem);. }e
210db 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 lse{. const c
210dc 68 61 72 20 2a 7a 20 3d 20 70 45 78 70 72 2d 3e har *z = pExpr->
210dd 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 61 73 u.zToken;. as
210de 73 65 72 74 28 20 7a 21 3d 30 20 29 3b 0a 20 20 sert( z!=0 );.
210df 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 74 if( sqlite3Fit
210e0 73 49 6e 36 34 42 69 74 73 28 7a 2c 20 6e 65 67 sIn64Bits(z, neg
210e1 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20 69 Flag) ){. i
210e2 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 64 value;.
210e3 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 20 20 char *zV;.
210e4 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c sqlite3Atoi64(z,
210e5 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 &value);.
210e6 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 76 61 if( negFlag ) va
210e7 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 lue = -value;.
210e8 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 zV = dup8byt
210e9 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 es(v, (char*)&va
210ea 6c 75 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 lue);. sqli
210eb 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
210ec 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 69 4d OP_Int64, 0, iM
210ed 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f 49 4e em, 0, zV, P4_IN
210ee 54 36 34 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b T64);. }else{
210ef 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 . codeReal(
210f0 76 2c 20 7a 2c 20 6e 65 67 46 6c 61 67 2c 20 69 v, z, negFlag, i
210f1 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Mem);. }. }.
210f2 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 }../*.** Clear a
210f3 20 63 61 63 68 65 20 65 6e 74 72 79 2e 0a 2a 2f cache entry..*/
210f4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 63 .static void cac
210f5 68 65 45 6e 74 72 79 43 6c 65 61 72 28 50 61 72 heEntryClear(Par
210f6 73 65 20 2a 70 50 61 72 73 65 2c 20 73 74 72 75 se *pParse, stru
210f7 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 29 ct yColCache *p)
210f8 7b 0a 20 20 69 66 28 20 70 2d 3e 74 65 6d 70 52 {. if( p->tempR
210f9 65 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 eg ){. if( pP
210fa 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 arse->nTempReg<A
210fb 72 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d rraySize(pParse-
210fc 3e 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20 >aTempReg) ){.
210fd 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 65 6d pParse->aTem
210fe 70 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e 54 65 pReg[pParse->nTe
210ff 6d 70 52 65 67 2b 2b 5d 20 3d 20 70 2d 3e 69 52 mpReg++] = p->iR
21100 65 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d eg;. }. p-
21101 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 >tempReg = 0;.
21102 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f }.}.../*.** Reco
21103 72 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e rd in the column
21104 20 63 61 63 68 65 20 74 68 61 74 20 61 20 70 61 cache that a pa
21105 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 20 rticular column
21106 66 72 6f 6d 20 61 0a 2a 2a 20 70 61 72 74 69 63 from a.** partic
21107 75 6c 61 72 20 74 61 62 6c 65 20 69 73 20 73 74 ular table is st
21108 6f 72 65 64 20 69 6e 20 61 20 70 61 72 74 69 63 ored in a partic
21109 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e 0a 2a ular register..*
2110a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
2110b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
2110c 72 43 61 63 68 65 53 74 6f 72 65 28 50 61 72 73 rCacheStore(Pars
2110d 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
2110e 54 61 62 2c 20 69 6e 74 20 69 43 6f 6c 2c 20 69 Tab, int iCol, i
2110f 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 nt iReg){. int
21110 69 3b 0a 20 20 69 6e 74 20 6d 69 6e 4c 72 75 3b i;. int minLru;
21111 0a 20 20 69 6e 74 20 69 64 78 4c 72 75 3b 0a 20 . int idxLru;.
21112 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 struct yColCach
21113 65 20 2a 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 e *p;.. assert(
21114 20 69 52 65 67 3e 30 20 29 3b 20 20 2f 2a 20 52 iReg>0 ); /* R
21115 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20 egister numbers
21116 61 72 65 20 61 6c 77 61 79 73 20 70 6f 73 69 74 are always posit
21117 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ive */. assert(
21118 20 69 43 6f 6c 3e 3d 2d 31 20 26 26 20 69 43 6f iCol>=-1 && iCo
21119 6c 3c 33 32 37 36 38 20 29 3b 20 20 2f 2a 20 46 l<32768 ); /* F
2111a 69 6e 69 74 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d inite column num
2111b 62 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 bers */.. /* Fi
2111c 72 73 74 20 72 65 70 6c 61 63 65 20 61 6e 79 20 rst replace any
2111d 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 20 2a existing entry *
2111e 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 /. for(i=0, p=p
2111f 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
21120 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c ; i<SQLITE_N_COL
21121 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 CACHE; i++, p++)
21122 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 {. if( p->iRe
21123 67 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d g && p->iTable==
21124 69 54 61 62 20 26 26 20 70 2d 3e 69 43 6f 6c 75 iTab && p->iColu
21125 6d 6e 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 mn==iCol ){.
21126 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 cacheEntryClea
21127 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 r(pParse, p);.
21128 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 p->iLevel =
21129 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 pParse->iCacheLe
2112a 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 vel;. p->iR
2112b 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 20 eg = iReg;.
2112c 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 p->affChange =
2112d 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 0;. p->lru
2112e 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 = pParse->iCache
2112f 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 Cnt++;. ret
21130 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a urn;. }. }..
21131 20 20 2f 2a 20 46 69 6e 64 20 61 6e 20 65 6d 70 /* Find an emp
21132 74 79 20 73 6c 6f 74 20 61 6e 64 20 72 65 70 6c ty slot and repl
21133 61 63 65 20 69 74 20 2a 2f 0a 20 20 66 6f 72 28 ace it */. for(
21134 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
21135 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
21136 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
21137 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, p++){. if
21138 28 20 70 2d 3e 69 52 65 67 3d 3d 30 20 29 7b 0a ( p->iReg==0 ){.
21139 20 20 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 p->iLevel
2113a 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 = pParse->iCache
2113b 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d 3e Level;. p->
2113c 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 iTable = iTab;.
2113d 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 p->iColumn
2113e 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70 2d = iCol;. p-
2113f 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 >iReg = iReg;.
21140 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 p->affChange
21141 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 74 = 0;. p->t
21142 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 empReg = 0;.
21143 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 p->lru = pPars
21144 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a e->iCacheCnt++;.
21145 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
21146 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 }. }.. /* Re
21147 70 6c 61 63 65 20 74 68 65 20 6c 61 73 74 20 72 place the last r
21148 65 63 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a ecently used */.
21149 20 20 6d 69 6e 4c 72 75 20 3d 20 30 78 37 66 66 minLru = 0x7ff
2114a 66 66 66 66 66 3b 0a 20 20 69 64 78 4c 72 75 20 fffff;. idxLru
2114b 3d 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30 2c = -1;. for(i=0,
2114c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 p=pParse->aColC
2114d 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e ache; i<SQLITE_N
2114e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 _COLCACHE; i++,
2114f 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p++){. if( p-
21150 3e 6c 72 75 3c 6d 69 6e 4c 72 75 20 29 7b 0a 20 >lru<minLru ){.
21151 20 20 20 20 20 69 64 78 4c 72 75 20 3d 20 69 3b idxLru = i;
21152 0a 20 20 20 20 20 20 6d 69 6e 4c 72 75 20 3d 20 . minLru =
21153 70 2d 3e 6c 72 75 3b 0a 20 20 20 20 7d 0a 20 20 p->lru;. }.
21154 7d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 69 }. if( ALWAYS(i
21155 64 78 4c 72 75 3e 3d 30 29 20 29 7b 0a 20 20 20 dxLru>=0) ){.
21156 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 43 p = &pParse->aC
21157 6f 6c 43 61 63 68 65 5b 69 64 78 4c 72 75 5d 3b olCache[idxLru];
21158 0a 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d . p->iLevel =
21159 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c pParse->iCacheL
2115a 65 76 65 6c 3b 0a 20 20 20 20 70 2d 3e 69 54 61 evel;. p->iTa
2115b 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 ble = iTab;.
2115c 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f p->iColumn = iCo
2115d 6c 3b 0a 20 20 20 20 70 2d 3e 69 52 65 67 20 3d l;. p->iReg =
2115e 20 69 52 65 67 3b 0a 20 20 20 20 70 2d 3e 61 66 iReg;. p->af
2115f 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 fChange = 0;.
21160 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b p->tempReg = 0;
21161 0a 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 . p->lru = pP
21162 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b arse->iCacheCnt+
21163 2b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 +;. return;.
21164 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 }.}../*.** Indi
21165 63 61 74 65 20 74 68 61 74 20 61 20 72 65 67 69 cate that a regi
21166 73 74 65 72 20 69 73 20 62 65 69 6e 67 20 6f 76 ster is being ov
21167 65 72 77 72 69 74 74 65 6e 2e 20 20 50 75 72 67 erwritten. Purg
21168 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a e the register.*
21169 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d * from the colum
2116a 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 n cache..*/.SQLI
2116b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
2116c 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
2116d 52 65 6d 6f 76 65 28 50 61 72 73 65 20 2a 70 50 Remove(Parse *pP
2116e 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b arse, int iReg){
2116f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 . int i;. stru
21170 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b ct yColCache *p;
21171 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 . for(i=0, p=pP
21172 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b arse->aColCache;
21173 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 i<SQLITE_N_COLC
21174 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b ACHE; i++, p++){
21175 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 . if( p->iReg
21176 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 ==iReg ){.
21177 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 cacheEntryClear(
21178 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 pParse, p);.
21179 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 p->iReg = 0;.
2117a 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
2117b 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 63 * Remember the c
2117c 75 72 72 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 61 urrent column ca
2117d 63 68 65 20 63 6f 6e 74 65 78 74 2e 20 20 41 6e che context. An
2117e 79 20 6e 65 77 20 65 6e 74 72 69 65 73 20 61 64 y new entries ad
2117f 64 65 64 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20 ded.** added to
21180 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 the column cache
21181 20 61 66 74 65 72 20 74 68 69 73 20 63 61 6c 6c after this call
21182 20 61 72 65 20 72 65 6d 6f 76 65 64 20 77 68 65 are removed whe
21183 6e 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 n the.** corresp
21184 6f 6e 64 69 6e 67 20 70 6f 70 20 6f 63 63 75 72 onding pop occur
21185 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
21186 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
21187 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 50 3ExprCachePush(P
21188 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 arse *pParse){.
21189 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c pParse->iCacheL
2118a 65 76 65 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a evel++;.}../*.**
2118b 20 52 65 6d 6f 76 65 20 66 72 6f 6d 20 74 68 65 Remove from the
2118c 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 61 6e column cache an
2118d 79 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 77 y entries that w
2118e 65 72 65 20 61 64 64 65 64 20 73 69 6e 63 65 20 ere added since
2118f 74 68 65 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 the.** the previ
21190 6f 75 73 20 4e 20 50 75 73 68 20 6f 70 65 72 61 ous N Push opera
21191 74 69 6f 6e 73 2e 20 20 49 6e 20 6f 74 68 65 72 tions. In other
21192 20 77 6f 72 64 73 2c 20 72 65 73 74 6f 72 65 20 words, restore
21193 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 74 6f 20 the cache.** to
21194 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61 73 the state it was
21195 20 69 6e 20 4e 20 50 75 73 68 65 73 20 61 67 6f in N Pushes ago
21196 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
21197 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
21198 45 78 70 72 43 61 63 68 65 50 6f 70 28 50 61 72 ExprCachePop(Par
21199 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
2119a 4e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 N){. int i;. s
2119b 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
2119c 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 3e *p;. assert( N>
2119d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
2119e 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 Parse->iCacheLev
2119f 65 6c 3e 3d 4e 20 29 3b 0a 20 20 70 50 61 72 73 el>=N );. pPars
211a0 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20 2d e->iCacheLevel -
211a1 3d 20 4e 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 = N;. for(i=0,
211a2 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
211a3 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
211a4 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
211a5 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e ++){. if( p->
211a6 69 52 65 67 20 26 26 20 70 2d 3e 69 4c 65 76 65 iReg && p->iLeve
211a7 6c 3e 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 l>pParse->iCache
211a8 4c 65 76 65 6c 20 29 7b 0a 20 20 20 20 20 20 63 Level ){. c
211a9 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 acheEntryClear(p
211aa 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 Parse, p);.
211ab 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 p->iReg = 0;.
211ac 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
211ad 20 57 68 65 6e 20 61 20 63 61 63 68 65 64 20 63 When a cached c
211ae 6f 6c 75 6d 6e 20 69 73 20 72 65 75 73 65 64 2c olumn is reused,
211af 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
211b0 69 74 73 20 72 65 67 69 73 74 65 72 20 69 73 0a its register is.
211b1 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 76 61 ** no longer ava
211b2 69 6c 61 62 6c 65 20 61 73 20 61 20 74 65 6d 70 ilable as a temp
211b3 20 72 65 67 69 73 74 65 72 2e 20 20 74 69 63 6b register. tick
211b4 65 74 20 23 33 38 37 39 3a 20 20 74 68 61 74 20 et #3879: that
211b5 73 61 6d 65 0a 2a 2a 20 72 65 67 69 73 74 65 72 same.** register
211b6 20 6d 69 67 68 74 20 62 65 20 69 6e 20 74 68 65 might be in the
211b7 20 63 61 63 68 65 20 69 6e 20 6d 75 6c 74 69 70 cache in multip
211b8 6c 65 20 70 6c 61 63 65 73 2c 20 73 6f 20 62 65 le places, so be
211b9 20 73 75 72 65 20 74 6f 0a 2a 2a 20 67 65 74 20 sure to.** get
211ba 74 68 65 6d 20 61 6c 6c 2e 0a 2a 2f 0a 73 74 61 them all..*/.sta
211bb 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
211bc 45 78 70 72 43 61 63 68 65 50 69 6e 52 65 67 69 ExprCachePinRegi
211bd 73 74 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 ster(Parse *pPar
211be 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 se, int iReg){.
211bf 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
211c0 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 yColCache *p;.
211c1 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
211c2 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
211c3 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
211c4 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
211c5 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d if( p->iReg==
211c6 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70 2d iReg ){. p-
211c7 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 >tempReg = 0;.
211c8 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
211c9 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
211ca 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61 63 74 hat will extract
211cb 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 the iColumn-th
211cc 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74 column from.** t
211cd 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20 73 74 able pTab and st
211ce 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 ore the column v
211cf 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69 73 74 alue in a regist
211d0 65 72 2e 20 20 41 6e 20 65 66 66 6f 72 74 0a 2a er. An effort.*
211d1 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 73 74 6f * is made to sto
211d2 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 re the column va
211d3 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
211d4 69 52 65 67 2c 20 62 75 74 20 74 68 69 73 20 69 iReg, but this i
211d5 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 72 61 6e 74 s.** not guarant
211d6 65 65 64 2e 20 20 54 68 65 20 6c 6f 63 61 74 69 eed. The locati
211d7 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e on of the column
211d8 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e value is return
211d9 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 ed..**.** There
211da 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 must be an open
211db 63 75 72 73 6f 72 20 74 6f 20 70 54 61 62 20 69 cursor to pTab i
211dc 6e 20 69 54 61 62 6c 65 20 77 68 65 6e 20 74 68 n iTable when th
211dd 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 is routine.** is
211de 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 43 6f called. If iCo
211df 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 63 6f 64 65 lumn<0 then code
211e0 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 68 is generated th
211e1 61 74 20 65 78 74 72 61 63 74 73 20 74 68 65 20 at extracts the
211e2 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 rowid..**.** Thi
211e3 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 s routine might
211e4 61 74 74 65 6d 70 74 20 74 6f 20 72 65 75 73 65 attempt to reuse
211e5 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
211e6 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a e column that.**
211e7 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
211e8 6e 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 61 20 n loaded into a
211e9 72 65 67 69 73 74 65 72 2e 20 20 54 68 65 20 76 register. The v
211ea 61 6c 75 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 alue will always
211eb 0a 2a 2a 20 62 65 20 75 73 65 64 20 69 66 20 69 .** be used if i
211ec 74 20 68 61 73 20 6e 6f 74 20 75 6e 64 65 72 67 t has not underg
211ed 6f 6e 65 20 61 6e 79 20 61 66 66 69 6e 69 74 79 one any affinity
211ee 20 63 68 61 6e 67 65 73 2e 20 20 42 75 74 20 69 changes. But i
211ef 66 0a 2a 2a 20 61 6e 20 61 66 66 69 6e 69 74 79 f.** an affinity
211f0 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75 change has occu
211f1 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 rred, then the c
211f2 61 63 68 65 64 20 76 61 6c 75 65 20 77 69 6c 6c ached value will
211f3 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 75 73 65 64 only be.** used
211f4 20 69 66 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 if allowAffChng
211f5 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c is true..*/.SQL
211f6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
211f7 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 sqlite3ExprCodeG
211f8 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 50 61 72 73 etColumn(. Pars
211f9 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 e *pParse, /*
211fa 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 Parsing and code
211fb 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 generating cont
211fc 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a ext */. Table *
211fd 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 44 65 73 pTab, /* Des
211fe 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 cription of the
211ff 74 61 62 6c 65 20 77 65 20 61 72 65 20 72 65 61 table we are rea
21200 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 ding from */. i
21201 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 nt iColumn,
21202 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 /* Index of the
21203 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a table column */.
21204 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 int iTable,
21205 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 /* The cursor
21206 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 pointing to the
21207 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 table */. int
21208 69 52 65 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 iReg, /*
21209 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 68 65 Store results he
2120a 72 65 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 6c 6f re */. int allo
2120b 77 41 66 66 43 68 6e 67 20 2f 2a 20 54 72 75 65 wAffChng /* True
2120c 20 69 66 20 70 72 69 6f 72 20 61 66 66 69 6e 69 if prior affini
2120d 74 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 4f ty changes are O
2120e 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a K */.){. Vdbe *
2120f 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
21210 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 e;. int i;. st
21211 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a ruct yColCache *
21212 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 p;.. for(i=0, p
21213 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 =pParse->aColCac
21214 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 he; i<SQLITE_N_C
21215 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b OLCACHE; i++, p+
21216 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 +){. if( p->i
21217 52 65 67 3e 30 20 26 26 20 70 2d 3e 69 54 61 62 Reg>0 && p->iTab
21218 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 2d le==iTable && p-
21219 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d >iColumn==iColum
2121a 6e 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 n. &&
2121b 28 21 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 7c (!p->affChange |
2121c 7c 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 29 20 | allowAffChng)
2121d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 ){. p->lru
2121e 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 = pParse->iCache
2121f 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c Cnt++;. sql
21220 69 74 65 33 45 78 70 72 43 61 63 68 65 50 69 6e ite3ExprCachePin
21221 52 65 67 69 73 74 65 72 28 70 50 61 72 73 65 2c Register(pParse,
21222 20 70 2d 3e 69 52 65 67 29 3b 0a 20 20 20 20 20 p->iReg);.
21223 20 72 65 74 75 72 6e 20 70 2d 3e 69 52 65 67 3b return p->iReg;
21224 0a 20 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20 61 . }. } . a
21225 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 ssert( v!=0 );.
21226 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 if( iColumn<0 )
21227 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
21228 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f eAddOp2(v, OP_Ro
21229 77 69 64 2c 20 69 54 61 62 6c 65 2c 20 69 52 65 wid, iTable, iRe
2122a 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 g);. }else if(
2122b 41 4c 57 41 59 53 28 70 54 61 62 21 3d 30 29 20 ALWAYS(pTab!=0)
2122c 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 ){. int op =
2122d 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 IsVirtual(pTab)
2122e 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f ? OP_VColumn : O
2122f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 P_Column;. sq
21230 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21231 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 20 69 v, op, iTable, i
21232 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 Column, iReg);.
21233 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e sqlite3Column
21234 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c Default(v, pTab,
21235 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b iColumn, iReg);
21236 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 . }. sqlite3Ex
21237 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 prCacheStore(pPa
21238 72 73 65 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f rse, iTable, iCo
21239 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 72 lumn, iReg);. r
2123a 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f eturn iReg;.}../
2123b 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c 6c 20 63 *.** Clear all c
2123c 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e 74 72 olumn cache entr
2123d 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ies..*/.SQLITE_P
2123e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
2123f 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 te3ExprCacheClea
21240 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 r(Parse *pParse)
21241 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 {. int i;. str
21242 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 uct yColCache *p
21243 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d ;.. for(i=0, p=
21244 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 pParse->aColCach
21245 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f e; i<SQLITE_N_CO
21246 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b LCACHE; i++, p++
21247 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 ){. if( p->iR
21248 65 67 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 eg ){. cach
21249 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 eEntryClear(pPar
2124a 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d se, p);. p-
2124b 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d >iReg = 0;. }
2124c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
2124d 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68 cord the fact th
2124e 61 74 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 at an affinity c
2124f 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 hange has occurr
21250 65 64 20 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a 20 ed on iCount.**
21251 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 registers starti
21252 6e 67 20 77 69 74 68 20 69 53 74 61 72 74 2e 0a ng with iStart..
21253 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21254 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
21255 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 prCacheAffinityC
21256 68 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 61 hange(Parse *pPa
21257 72 73 65 2c 20 69 6e 74 20 69 53 74 61 72 74 2c rse, int iStart,
21258 20 69 6e 74 20 69 43 6f 75 6e 74 29 7b 0a 20 20 int iCount){.
21259 69 6e 74 20 69 45 6e 64 20 3d 20 69 53 74 61 72 int iEnd = iStar
2125a 74 20 2b 20 69 43 6f 75 6e 74 20 2d 20 31 3b 0a t + iCount - 1;.
2125b 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 int i;. struc
2125c 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a t yColCache *p;.
2125d 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 for(i=0, p=pPa
2125e 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 rse->aColCache;
2125f 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 i<SQLITE_N_COLCA
21260 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a CHE; i++, p++){.
21261 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 int r = p->i
21262 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d Reg;. if( r>=
21263 69 53 74 61 72 74 20 26 26 20 72 3c 3d 69 45 6e iStart && r<=iEn
21264 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 66 d ){. p->af
21265 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 20 20 fChange = 1;.
21266 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
21267 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
21268 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66 72 move content fr
21269 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 72 om registers iFr
2126a 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d om...iFrom+nReg-
2126b 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 1.** over to iTo
2126c 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b 65 ..iTo+nReg-1. Ke
2126d 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 ep the column ca
2126e 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e 0a che up-to-date..
2126f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21270 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
21271 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65 prCodeMove(Parse
21272 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 *pParse, int iF
21273 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e rom, int iTo, in
21274 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 t nReg){. int i
21275 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
21276 61 63 68 65 20 2a 70 3b 0a 20 20 69 66 28 20 4e ache *p;. if( N
21277 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f 29 EVER(iFrom==iTo)
21278 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c ) return;. sql
21279 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 ite3VdbeAddOp3(p
2127a 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 Parse->pVdbe, OP
2127b 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 69 54 _Move, iFrom, iT
2127c 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f 72 28 o, nReg);. for(
2127d 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
2127e 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
2127f 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
21280 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e ++, p++){. in
21281 74 20 78 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 t x = p->iReg;.
21282 20 20 20 69 66 28 20 78 3e 3d 69 46 72 6f 6d 20 if( x>=iFrom
21283 26 26 20 78 3c 69 46 72 6f 6d 2b 6e 52 65 67 20 && x<iFrom+nReg
21284 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 ){. p->iReg
21285 20 2b 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20 += iTo-iFrom;.
21286 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
21287 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
21288 74 6f 20 63 6f 70 79 20 63 6f 6e 74 65 6e 74 20 to copy content
21289 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 from registers i
2128a 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 From...iFrom+nRe
2128b 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 g-1.** over to i
2128c 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a To..iTo+nReg-1..
2128d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
2128e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
2128f 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65 prCodeCopy(Parse
21290 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 *pParse, int iF
21291 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e rom, int iTo, in
21292 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 t nReg){. int i
21293 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 46 ;. if( NEVER(iF
21294 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74 75 rom==iTo) ) retu
21295 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 rn;. for(i=0; i
21296 3c 6e 52 65 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nReg; i++){.
21297 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21298 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 p2(pParse->pVdbe
21299 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 46 72 6f 6d , OP_Copy, iFrom
2129a 2b 69 2c 20 69 54 6f 2b 69 29 3b 0a 20 20 7d 0a +i, iTo+i);. }.
2129b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
2129c 74 72 75 65 20 69 66 20 61 6e 79 20 72 65 67 69 true if any regi
2129d 73 74 65 72 20 69 6e 20 74 68 65 20 72 61 6e 67 ster in the rang
2129e 65 20 69 46 72 6f 6d 2e 2e 69 54 6f 20 28 69 6e e iFrom..iTo (in
2129f 63 6c 75 73 69 76 65 29 0a 2a 2a 20 69 73 20 75 clusive).** is u
212a0 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 sed as part of t
212a1 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e he column cache.
212a2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
212a3 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 sedAsColumnCache
212a4 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
212a5 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 int iFrom, int i
212a6 54 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 To){. int i;.
212a7 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 struct yColCache
212a8 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 *p;. for(i=0,
212a9 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
212aa 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
212ab 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
212ac 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 3d ++){. int r =
212ad 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 p->iReg;. if
212ae 28 20 72 3e 3d 69 46 72 6f 6d 20 26 26 20 72 3c ( r>=iFrom && r<
212af 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 20 31 3b =iTo ) return 1;
212b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
212b1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
212b2 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f last instructio
212b3 6e 20 63 6f 64 65 64 20 69 73 20 61 6e 20 65 70 n coded is an ep
212b4 68 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66 20 hemeral copy of
212b5 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 any of.** the re
212b6 67 69 73 74 65 72 73 20 69 6e 20 74 68 65 20 6e gisters in the n
212b7 52 65 67 20 72 65 67 69 73 74 65 72 73 20 62 65 Reg registers be
212b8 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 52 65 ginning with iRe
212b9 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 g, then.** conve
212ba 72 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 rt the last inst
212bb 72 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f 50 5f ruction from OP_
212bc 53 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f 70 79 SCopy to OP_Copy
212bd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
212be 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
212bf 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72 ExprHardCopy(Par
212c0 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 se *pParse, int
212c1 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 7b iReg, int nReg){
212c2 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a . VdbeOp *pOp;.
212c3 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 61 73 Vdbe *v;.. as
212c4 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 62 sert( pParse->db
212c5 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d ->mallocFailed==
212c6 30 20 29 3b 0a 20 20 76 20 3d 20 70 50 61 72 73 0 );. v = pPars
212c7 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65 e->pVdbe;. asse
212c8 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 70 4f rt( v!=0 );. pO
212c9 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 p = sqlite3VdbeG
212ca 65 74 4f 70 28 76 2c 20 2d 31 29 3b 0a 20 20 61 etOp(v, -1);. a
212cb 73 73 65 72 74 28 20 70 4f 70 21 3d 30 20 29 3b ssert( pOp!=0 );
212cc 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f . if( pOp->opco
212cd 64 65 3d 3d 4f 50 5f 53 43 6f 70 79 20 26 26 20 de==OP_SCopy &&
212ce 70 4f 70 2d 3e 70 31 3e 3d 69 52 65 67 20 26 26 pOp->p1>=iReg &&
212cf 20 70 4f 70 2d 3e 70 31 3c 69 52 65 67 2b 6e 52 pOp->p1<iReg+nR
212d0 65 67 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 6f eg ){. pOp->o
212d1 70 63 6f 64 65 20 3d 20 4f 50 5f 43 6f 70 79 3b pcode = OP_Copy;
212d2 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 . }.}../*.** Ge
212d3 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 nerate code to s
212d4 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f tore the value o
212d5 66 20 74 68 65 20 69 41 6c 69 61 73 2d 74 68 20 f the iAlias-th
212d6 61 6c 69 61 73 20 69 6e 20 72 65 67 69 73 74 65 alias in registe
212d7 72 0a 2a 2a 20 74 61 72 67 65 74 2e 20 20 54 68 r.** target. Th
212d8 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 e first time thi
212d9 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70 45 78 s is called, pEx
212da 70 72 20 69 73 20 65 76 61 6c 75 61 74 65 64 20 pr is evaluated
212db 74 6f 20 63 6f 6d 70 75 74 65 0a 2a 2a 20 74 68 to compute.** th
212dc 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 e value of the a
212dd 6c 69 61 73 2e 20 20 54 68 65 20 76 61 6c 75 65 lias. The value
212de 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 6e is stored in an
212df 20 61 75 78 69 6c 69 61 72 79 20 72 65 67 69 73 auxiliary regis
212e0 74 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e ter.** and the n
212e1 75 6d 62 65 72 20 6f 66 20 74 68 61 74 20 72 65 umber of that re
212e2 67 69 73 74 65 72 20 69 73 20 72 65 74 75 72 6e gister is return
212e3 65 64 2e 20 20 4f 6e 20 73 75 62 73 65 71 75 65 ed. On subseque
212e4 6e 74 20 63 61 6c 6c 73 2c 0a 2a 2a 20 74 68 65 nt calls,.** the
212e5 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 register number
212e6 20 69 73 20 72 65 74 75 72 6e 65 64 20 77 69 74 is returned wit
212e7 68 6f 75 74 20 67 65 6e 65 72 61 74 69 6e 67 20 hout generating
212e8 61 6e 79 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 any code..**.**
212e9 4e 6f 74 65 20 74 68 61 74 20 69 6e 20 6f 72 64 Note that in ord
212ea 65 72 20 66 6f 72 20 74 68 69 73 20 74 6f 20 77 er for this to w
212eb 6f 72 6b 2c 20 63 6f 64 65 20 6d 75 73 74 20 62 ork, code must b
212ec 65 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20 74 e generated in t
212ed 68 65 0a 2a 2a 20 73 61 6d 65 20 6f 72 64 65 72 he.** same order
212ee 20 74 68 61 74 20 69 74 20 69 73 20 65 78 65 63 that it is exec
212ef 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 69 61 uted..**.** Alia
212f0 73 65 73 20 61 72 65 20 6e 75 6d 62 65 72 65 64 ses are numbered
212f1 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 31 starting with 1
212f2 2e 20 20 53 6f 20 69 41 6c 69 61 73 20 69 73 20 . So iAlias is
212f3 69 6e 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20 in the range.**
212f4 6f 66 20 31 20 74 6f 20 70 50 61 72 73 65 2d 3e of 1 to pParse->
212f5 6e 41 6c 69 61 73 20 69 6e 63 6c 75 73 69 76 65 nAlias inclusive
212f6 2e 20 20 0a 2a 2a 0a 2a 2a 20 70 50 61 72 73 65 . .**.** pParse
212f7 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d ->aAlias[iAlias-
212f8 31 5d 20 72 65 63 6f 72 64 73 20 74 68 65 20 72 1] records the r
212f9 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 egister number w
212fa 68 65 72 65 20 74 68 65 20 76 61 6c 75 65 0a 2a here the value.*
212fb 2a 20 6f 66 20 74 68 65 20 69 41 6c 69 61 73 2d * of the iAlias-
212fc 74 68 20 61 6c 69 61 73 20 69 73 20 73 74 6f 72 th alias is stor
212fd 65 64 2e 20 20 49 66 20 7a 65 72 6f 2c 20 74 68 ed. If zero, th
212fe 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 at means that th
212ff 65 0a 2a 2a 20 61 6c 69 61 73 20 68 61 73 20 6e e.** alias has n
21300 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f 6d 70 ot yet been comp
21301 75 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 uted..*/.static
21302 69 6e 74 20 63 6f 64 65 41 6c 69 61 73 28 50 61 int codeAlias(Pa
21303 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
21304 20 69 41 6c 69 61 73 2c 20 45 78 70 72 20 2a 70 iAlias, Expr *p
21305 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 Expr, int target
21306 29 7b 0a 23 69 66 20 30 0a 20 20 73 71 6c 69 74 ){.#if 0. sqlit
21307 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d e3 *db = pParse-
21308 3e 64 62 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b >db;. int iReg;
21309 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e . if( pParse->n
2130a 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 50 61 72 73 AliasAlloc<pPars
2130b 65 2d 3e 6e 41 6c 69 61 73 20 29 7b 0a 20 20 20 e->nAlias ){.
2130c 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 20 pParse->aAlias
2130d 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c = sqlite3DbReall
2130e 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70 50 61 ocOrFree(db, pPa
2130f 72 73 65 2d 3e 61 41 6c 69 61 73 2c 0a 20 20 20 rse->aAlias,.
21310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21311 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 si
21312 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 41 6c zeof(pParse->aAl
21313 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65 2d 3e ias[0])*pParse->
21314 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20 74 65 nAlias );. te
21315 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61 6c 6c stcase( db->mall
21316 6f 63 46 61 69 6c 65 64 20 26 26 20 70 50 61 72 ocFailed && pPar
21317 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3e se->nAliasAlloc>
21318 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 0 );. if( db-
21319 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
2131a 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 6d 65 return 0;. me
2131b 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e 61 41 mset(&pParse->aA
2131c 6c 69 61 73 5b 70 50 61 72 73 65 2d 3e 6e 41 6c lias[pParse->nAl
2131d 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 2c 0a 20 20 iasAlloc], 0,.
2131e 20 20 20 20 20 20 20 20 20 28 70 50 61 72 73 65 (pParse
2131f 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 72 73 65 2d ->nAlias-pParse-
21320 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 29 2a 73 69 >nAliasAlloc)*si
21321 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 41 6c zeof(pParse->aAl
21322 69 61 73 5b 30 5d 29 29 3b 0a 20 20 20 20 70 50 ias[0]));. pP
21323 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f arse->nAliasAllo
21324 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 c = pParse->nAli
21325 61 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 as;. }. assert
21326 28 20 69 41 6c 69 61 73 3e 30 20 26 26 20 69 41 ( iAlias>0 && iA
21327 6c 69 61 73 3c 3d 70 50 61 72 73 65 2d 3e 6e 41 lias<=pParse->nA
21328 6c 69 61 73 20 29 3b 0a 20 20 69 52 65 67 20 3d lias );. iReg =
21329 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b pParse->aAlias[
2132a 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 20 69 66 28 iAlias-1];. if(
2132b 20 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 iReg==0 ){.
2132c 69 66 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63 if( pParse->iCac
2132d 68 65 4c 65 76 65 6c 3e 30 20 29 7b 0a 20 20 20 heLevel>0 ){.
2132e 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 iReg = sqlite
2132f 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 3ExprCodeTarget(
21330 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 pParse, pExpr, t
21331 61 72 67 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 arget);. }els
21332 65 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 e{. iReg =
21333 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
21334 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
21335 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 rCode(pParse, pE
21336 78 70 72 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 xpr, iReg);.
21337 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 pParse->aAlias
21338 5b 69 41 6c 69 61 73 2d 31 5d 20 3d 20 69 52 65 [iAlias-1] = iRe
21339 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 g;. }. }. r
2133a 65 74 75 72 6e 20 69 52 65 67 3b 0a 23 65 6c 73 eturn iReg;.#els
2133b 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d e. UNUSED_PARAM
2133c 45 54 45 52 28 69 41 6c 69 61 73 29 3b 0a 20 20 ETER(iAlias);.
2133d 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 return sqlite3Ex
2133e 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 prCodeTarget(pPa
2133f 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 rse, pExpr, targ
21340 65 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f et);.#endif.}../
21341 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
21342 64 65 20 69 6e 74 6f 20 74 68 65 20 63 75 72 72 de into the curr
21343 65 6e 74 20 56 64 62 65 20 74 6f 20 65 76 61 6c ent Vdbe to eval
21344 75 61 74 65 20 74 68 65 20 67 69 76 65 6e 0a 2a uate the given.*
21345 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 41 * expression. A
21346 74 74 65 6d 70 74 20 74 6f 20 73 74 6f 72 65 20 ttempt to store
21347 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 72 the results in r
21348 65 67 69 73 74 65 72 20 22 74 61 72 67 65 74 22 egister "target"
21349 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ..** Return the
2134a 72 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 register where r
2134b 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 esults are store
2134c 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 d..**.** With th
2134d 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 72 is routine, ther
2134e 65 20 69 73 20 6e 6f 20 67 75 61 72 61 6e 74 65 e is no guarante
2134f 65 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 77 e that results w
21350 69 6c 6c 0a 2a 2a 20 62 65 20 73 74 6f 72 65 64 ill.** be stored
21351 20 69 6e 20 74 61 72 67 65 74 2e 20 20 54 68 65 in target. The
21352 20 72 65 73 75 6c 74 20 6d 69 67 68 74 20 62 65 result might be
21353 20 73 74 6f 72 65 64 20 69 6e 20 73 6f 6d 65 20 stored in some
21354 6f 74 68 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 other.** registe
21355 72 20 69 66 20 69 74 20 69 73 20 63 6f 6e 76 65 r if it is conve
21356 6e 69 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 20 nient to do so.
21357 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e The calling fun
21358 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 63 68 ction.** must ch
21359 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 eck the return c
2135a 6f 64 65 20 61 6e 64 20 6d 6f 76 65 20 74 68 65 ode and move the
2135b 20 72 65 73 75 6c 74 73 20 74 6f 20 74 68 65 20 results to the
2135c 64 65 73 69 72 65 64 0a 2a 2a 20 72 65 67 69 73 desired.** regis
2135d 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ter..*/.SQLITE_P
2135e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
2135f 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 e3ExprCodeTarget
21360 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
21361 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 Expr *pExpr, int
21362 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62 65 target){. Vdbe
21363 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 *v = pParse->pV
21364 64 62 65 3b 20 20 2f 2a 20 54 68 65 20 56 4d 20 dbe; /* The VM
21365 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 under constructi
21366 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 on */. int op;
21367 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21368 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 65 20 /* The opcode
21369 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 being coded */.
2136a 20 69 6e 74 20 69 6e 52 65 67 20 3d 20 74 61 72 int inReg = tar
2136b 67 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 get; /* Re
2136c 73 75 6c 74 73 20 73 74 6f 72 65 64 20 69 6e 20 sults stored in
2136d 72 65 67 69 73 74 65 72 20 69 6e 52 65 67 20 2a register inReg *
2136e 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 /. int regFree1
2136f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
21370 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 If non-zero fre
21371 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 e this temporary
21372 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 register */. i
21373 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b nt regFree2 = 0;
21374 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e /* If n
21375 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 on-zero free thi
21376 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 s temporary regi
21377 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 31 ster */. int r1
21378 2c 20 72 32 2c 20 72 33 2c 20 72 34 3b 20 20 20 , r2, r3, r4;
21379 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 72 /* Various r
2137a 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20 egister numbers
2137b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
2137c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f = pParse->db; /
2137d 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
2137e 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 onnection */..
2137f 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 assert( target>0
21380 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72 && target<=pPar
21381 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 66 se->nMem );. if
21382 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 ( v==0 ){. as
21383 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 62 sert( pParse->db
21384 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
21385 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
21386 20 20 7d 0a 0a 20 20 69 66 28 20 70 45 78 70 72 }.. if( pExpr
21387 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 ==0 ){. op =
21388 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 TK_NULL;. }else
21389 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70 72 {. op = pExpr
2138a 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69 74 ->op;. }. swit
2138b 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 ch( op ){. ca
2138c 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e se TK_AGG_COLUMN
2138d 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 : {. AggInf
2138e 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 45 o *pAggInfo = pE
2138f 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 xpr->pAggInfo;.
21390 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49 struct AggI
21391 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 nfo_col *pCol =
21392 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b &pAggInfo->aCol[
21393 70 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20 20 pExpr->iAgg];.
21394 20 20 20 20 69 66 28 20 21 70 41 67 67 49 6e 66 if( !pAggInf
21395 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29 7b o->directMode ){
21396 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
21397 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29 3b pCol->iMem>0 );
21398 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d . inReg =
21399 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 20 pCol->iMem;.
2139a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
2139b 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67 67 }else if( pAgg
2139c 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e 67 Info->useSorting
2139d 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Idx ){. s
2139e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
2139f 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 (v, OP_Column, p
213a0 41 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67 AggInfo->sorting
213a1 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Idx,.
213a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213a3 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 pCol->iSorter
213a4 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29 3b Column, target);
213a5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
213a6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a }. /*
213a7 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c Otherwise, fall
213a8 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 54 thru into the T
213a9 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a 2f K_COLUMN case */
213aa 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
213ab 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 TK_COLUMN: {.
213ac 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 if( pExpr->iT
213ad 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 able<0 ){.
213ae 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68 /* This only h
213af 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 69 appens when codi
213b0 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 ng check constra
213b1 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 ints */.
213b2 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
213b3 63 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20 20 ckBase>0 );.
213b4 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70 inReg = pExp
213b5 72 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50 61 r->iColumn + pPa
213b6 72 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20 20 rse->ckBase;.
213b7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
213b8 20 20 74 65 73 74 63 61 73 65 28 20 28 70 45 78 testcase( (pEx
213b9 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 pr->flags & EP_A
213ba 6e 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20 20 nyAff)!=0 );.
213bb 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c inReg = sql
213bc 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 ite3ExprCodeGetC
213bd 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 45 olumn(pParse, pE
213be 78 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20 20 xpr->pTab,.
213bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213c0 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
213c1 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78 70 r->iColumn, pExp
213c2 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65 r->iTable, targe
213c3 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
213c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
213c5 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 pExpr->flags
213c6 20 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a 20 & EP_AnyAff);.
213c7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
213c8 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
213c9 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b se TK_INTEGER: {
213ca 0a 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67 . codeInteg
213cb 65 72 28 76 2c 20 70 45 78 70 72 2c 20 30 2c 20 er(v, pExpr, 0,
213cc 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 target);. b
213cd 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
213ce 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b case TK_FLOAT: {
213cf 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 . assert( !
213d0 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
213d1 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c pExpr, EP_IntVal
213d2 75 65 29 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 ue) );. cod
213d3 65 52 65 61 6c 28 76 2c 20 70 45 78 70 72 2d 3e eReal(v, pExpr->
213d4 75 2e 7a 54 6f 6b 65 6e 2c 20 30 2c 20 74 61 72 u.zToken, 0, tar
213d5 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 get);. brea
213d6 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
213d7 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 e TK_STRING: {.
213d8 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 assert( !Ex
213d9 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
213da 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 xpr, EP_IntValue
213db 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ) );. sqlit
213dc 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
213dd 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 74 OP_String8, 0, t
213de 61 72 67 65 74 2c 20 30 2c 20 70 45 78 70 72 2d arget, 0, pExpr-
213df 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 >u.zToken, 0);.
213e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
213e1 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55 }. case TK_NU
213e2 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 LL: {. sqli
213e3 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
213e4 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 OP_Null, 0, tar
213e5 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 get);. brea
213e6 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 k;. }.#ifndef
213e7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f SQLITE_OMIT_BLO
213e8 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 B_LITERAL. ca
213e9 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20 se TK_BLOB: {.
213ea 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 int n;.
213eb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a const char *z;.
213ec 20 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f char *zBlo
213ed 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 b;. assert(
213ee 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
213ef 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 y(pExpr, EP_IntV
213f0 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61 alue) );. a
213f1 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e ssert( pExpr->u.
213f2 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c zToken[0]=='x' |
213f3 7c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 | pExpr->u.zToke
213f4 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 n[0]=='X' );.
213f5 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
213f6 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 ->u.zToken[1]=='
213f7 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 7a 20 3d \'' );. z =
213f8 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 &pExpr->u.zToke
213f9 6e 5b 32 5d 3b 0a 20 20 20 20 20 20 6e 20 3d 20 n[2];. n =
213fa 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
213fb 7a 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 61 73 z) - 1;. as
213fc 73 65 72 74 28 20 7a 5b 6e 5d 3d 3d 27 5c 27 27 sert( z[n]=='\''
213fd 20 29 3b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 20 );. zBlob
213fe 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c = sqlite3HexToBl
213ff 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 ob(sqlite3VdbeDb
21400 28 76 29 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 (v), z, n);.
21401 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21402 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 Op4(v, OP_Blob,
21403 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30 2c 20 n/2, target, 0,
21404 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41 4d 49 zBlob, P4_DYNAMI
21405 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b C);. break;
21406 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
21407 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42 case TK_VARIAB
21408 4c 45 3a 20 7b 0a 20 20 20 20 20 20 56 64 62 65 LE: {. Vdbe
21409 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 61 Op *pOp;. a
2140a 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
2140b 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
2140c 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 P_IntValue) );.
2140d 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
2140e 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 21 3d 30 20 pr->u.zToken!=0
2140f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21410 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e pExpr->u.zToken
21411 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 [0]!=0 );.
21412 69 66 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f if( pExpr->u.zTo
21413 6b 65 6e 5b 31 5d 3d 3d 30 0a 20 20 20 20 20 20 ken[1]==0.
21414 20 20 20 26 26 20 28 70 4f 70 20 3d 20 73 71 6c && (pOp = sql
21415 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c ite3VdbeGetOp(v,
21416 20 2d 31 29 29 2d 3e 6f 70 63 6f 64 65 3d 3d 4f -1))->opcode==O
21417 50 5f 56 61 72 69 61 62 6c 65 0a 20 20 20 20 20 P_Variable.
21418 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 31 2b 70 && pOp->p1+p
21419 4f 70 2d 3e 70 33 3d 3d 70 45 78 70 72 2d 3e 69 Op->p3==pExpr->i
2141a 54 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26 Table. &
2141b 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70 & pOp->p2+pOp->p
2141c 33 3d 3d 74 61 72 67 65 74 0a 20 20 20 20 20 20 3==target.
2141d 20 20 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 3d && pOp->p4.z=
2141e 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 =0. ){.
2141f 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 72 /* If the pr
21420 65 76 69 6f 75 73 20 69 6e 73 74 72 75 63 74 69 evious instructi
21421 6f 6e 20 77 61 73 20 61 20 63 6f 70 79 20 6f 66 on was a copy of
21422 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 75 6e the previous un
21423 6e 61 6d 65 64 0a 20 20 20 20 20 20 20 20 2a 2a named. **
21424 20 70 61 72 61 6d 65 74 65 72 20 69 6e 74 6f 20 parameter into
21425 74 68 65 20 70 72 65 76 69 6f 75 73 20 72 65 67 the previous reg
21426 69 73 74 65 72 2c 20 74 68 65 6e 20 73 69 6d 70 ister, then simp
21427 6c 79 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 ly increment the
21428 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 70 65 . ** repe
21429 61 74 20 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20 at count on the
2142a 70 72 69 6f 72 20 69 6e 73 74 72 75 63 74 69 6f prior instructio
2142b 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 6d 61 n rather than ma
2142c 6b 69 6e 67 20 61 20 6e 65 77 0a 20 20 20 20 20 king a new.
2142d 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f ** instructio
2142e 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 n.. */.
2142f 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 2b 2b 3b pOp->p3++;
21430 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
21431 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21432 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 56 61 eAddOp3(v, OP_Va
21433 72 69 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 69 riable, pExpr->i
21434 54 61 62 6c 65 2c 20 74 61 72 67 65 74 2c 20 31 Table, target, 1
21435 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
21436 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 Expr->u.zToken[1
21437 5d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 ]!=0 ){.
21438 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
21439 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 45 78 ngeP4(v, -1, pEx
2143a 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 pr->u.zToken, 0)
2143b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
2143c 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
2143d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
2143e 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20 TK_REGISTER: {.
2143f 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 inReg = pEx
21440 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 pr->iTable;.
21441 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
21442 20 20 20 63 61 73 65 20 54 4b 5f 41 53 3a 20 7b case TK_AS: {
21443 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 63 . inReg = c
21444 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c odeAlias(pParse,
21445 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 pExpr->iTable,
21446 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 pExpr->pLeft, ta
21447 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 rget);. bre
21448 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 ak;. }.#ifnde
21449 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 f SQLITE_OMIT_CA
2144a 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 ST. case TK_C
2144b 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 AST: {. /*
2144c 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 Expressions of t
2144d 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54 28 he form: CAST(
2144e 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29 20 pLeft AS token)
2144f 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66 */. int aff
21450 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20 69 , to_op;. i
21451 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 nReg = sqlite3Ex
21452 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 prCodeTarget(pPa
21453 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
21454 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 t, target);.
21455 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
21456 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
21457 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 , EP_IntValue) )
21458 3b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 73 71 ;. aff = sq
21459 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 lite3AffinityTyp
2145a 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 e(pExpr->u.zToke
2145b 6e 29 3b 0a 20 20 20 20 20 20 74 6f 5f 6f 70 20 n);. to_op
2145c 3d 20 61 66 66 20 2d 20 53 51 4c 49 54 45 5f 41 = aff - SQLITE_A
2145d 46 46 5f 54 45 58 54 20 2b 20 4f 50 5f 54 6f 54 FF_TEXT + OP_ToT
2145e 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ext;. asser
2145f 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 t( to_op==OP_ToT
21460 65 78 74 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 ext || aff!=S
21461 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 QLITE_AFF_TEXT
21462 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 );. asser
21463 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 t( to_op==OP_ToB
21464 6c 6f 62 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 lob || aff!=S
21465 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 QLITE_AFF_NONE
21466 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 );. asser
21467 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e t( to_op==OP_ToN
21468 75 6d 65 72 69 63 20 7c 7c 20 61 66 66 21 3d 53 umeric || aff!=S
21469 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 QLITE_AFF_NUMERI
2146a 43 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 C );. asser
2146b 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 t( to_op==OP_ToI
2146c 6e 74 20 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 nt || aff!=S
2146d 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 QLITE_AFF_INTEGE
2146e 52 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 R );. asser
2146f 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 t( to_op==OP_ToR
21470 65 61 6c 20 20 20 20 7c 7c 20 61 66 66 21 3d 53 eal || aff!=S
21471 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 QLITE_AFF_REAL
21472 20 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 );. testc
21473 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 ase( to_op==OP_T
21474 6f 54 65 78 74 20 29 3b 0a 20 20 20 20 20 20 74 oText );. t
21475 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d estcase( to_op==
21476 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b 0a 20 20 20 OP_ToBlob );.
21477 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f testcase( to_
21478 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 op==OP_ToNumeric
21479 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
2147a 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f se( to_op==OP_To
2147b 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 Int );. tes
2147c 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 tcase( to_op==OP
2147d 5f 54 6f 52 65 61 6c 20 29 3b 0a 20 20 20 20 20 _ToReal );.
2147e 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67 if( inReg!=targ
2147f 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 et ){. sq
21480 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21481 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 v, OP_SCopy, inR
21482 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 eg, target);.
21483 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 inReg = tar
21484 67 65 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 get;. }.
21485 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21486 64 4f 70 31 28 76 2c 20 74 6f 5f 6f 70 2c 20 69 dOp1(v, to_op, i
21487 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 74 65 73 nReg);. tes
21488 74 63 61 73 65 28 20 75 73 65 64 41 73 43 6f 6c tcase( usedAsCol
21489 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c umnCache(pParse,
2148a 20 69 6e 52 65 67 2c 20 69 6e 52 65 67 29 20 29 inReg, inReg) )
2148b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
2148c 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 xprCacheAffinity
2148d 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 Change(pParse, i
2148e 6e 52 65 67 2c 20 31 29 3b 0a 20 20 20 20 20 20 nReg, 1);.
2148f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
21490 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
21491 49 54 5f 43 41 53 54 20 2a 2f 0a 20 20 20 20 63 IT_CAST */. c
21492 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 ase TK_LT:. c
21493 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 ase TK_LE:. c
21494 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 ase TK_GT:. c
21495 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 ase TK_GE:. c
21496 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 ase TK_NE:. c
21497 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 ase TK_EQ: {.
21498 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 assert( TK_LT
21499 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 ==OP_Lt );.
2149a 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d assert( TK_LE==
2149b 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 OP_Le );. a
2149c 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 ssert( TK_GT==OP
2149d 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 _Gt );. ass
2149e 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 ert( TK_GE==OP_G
2149f 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 e );. asser
214a0 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 t( TK_EQ==OP_Eq
214a1 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
214a2 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b TK_NE==OP_Ne );
214a3 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
214a4 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 op==TK_LT );.
214a5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
214a6 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 ==TK_LE );.
214a7 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
214a8 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65 K_GT );. te
214a9 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 stcase( op==TK_G
214aa 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 E );. testc
214ab 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 ase( op==TK_EQ )
214ac 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
214ad 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 ( op==TK_NE );.
214ae 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 codeCompare
214af 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c Operands(pParse,
214b0 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 pExpr->pLeft, &
214b1 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 r1, ®Free1,.
214b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
214b4 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 pExpr->pRight,
214b5 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b &r2, ®Free2);
214b6 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 . codeCompa
214b7 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 re(pParse, pExpr
214b8 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e ->pLeft, pExpr->
214b9 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 pRight, op,.
214ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31 r1
214bb 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c , r2, inReg, SQL
214bc 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 ITE_STOREP2);.
214bd 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
214be 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 gFree1==0 );.
214bf 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
214c0 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 Free2==0 );.
214c1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
214c2 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a case TK_AND:.
214c3 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a case TK_OR:.
214c4 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c 55 53 case TK_PLUS
214c5 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 :. case TK_ST
214c6 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f AR:. case TK_
214c7 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73 65 20 MINUS:. case
214c8 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61 73 65 TK_REM:. case
214c9 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20 TK_BITAND:.
214ca 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20 case TK_BITOR:.
214cb 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41 53 48 case TK_SLASH
214cc 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 53 :. case TK_LS
214cd 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65 20 54 HIFT:. case T
214ce 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20 20 63 K_RSHIFT: . c
214cf 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b ase TK_CONCAT: {
214d0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
214d1 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b K_AND==OP_And );
214d2 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
214d3 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20 K_OR==OP_Or );.
214d4 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
214d5 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a PLUS==OP_Add );.
214d6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
214d7 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72 _MINUS==OP_Subtr
214d8 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 act );. ass
214d9 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f ert( TK_REM==OP_
214da 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20 20 20 Remainder );.
214db 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 assert( TK_BI
214dc 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20 TAND==OP_BitAnd
214dd 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
214de 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69 TK_BITOR==OP_Bi
214df 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 tOr );. ass
214e0 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f ert( TK_SLASH==O
214e1 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20 20 20 P_Divide );.
214e2 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 53 48 assert( TK_LSH
214e3 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66 IFT==OP_ShiftLef
214e4 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 t );. asser
214e5 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50 t( TK_RSHIFT==OP
214e6 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20 _ShiftRight );.
214e7 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
214e8 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61 CONCAT==OP_Conca
214e9 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 t );. testc
214ea 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20 ase( op==TK_AND
214eb 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
214ec 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a e( op==TK_OR );.
214ed 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
214ee 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20 op==TK_PLUS );.
214ef 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
214f0 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20 p==TK_MINUS );.
214f1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
214f2 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20 p==TK_REM );.
214f3 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
214f4 3d 54 4b 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20 =TK_BITAND );.
214f5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
214f6 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20 ==TK_BITOR );.
214f7 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
214f8 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20 ==TK_SLASH );.
214f9 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
214fa 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20 ==TK_LSHIFT );.
214fb 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
214fc 70 3d 3d 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a p==TK_RSHIFT );.
214fd 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
214fe 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b op==TK_CONCAT );
214ff 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 . r1 = sqli
21500 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
21501 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
21502 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 Left, ®Free1)
21503 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c ;. r2 = sql
21504 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
21505 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21506 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65 pRight, ®Free
21507 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2);. sqlite
21508 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 3VdbeAddOp3(v, o
21509 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65 p, r2, r1, targe
2150a 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 t);. testca
2150b 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
2150c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
2150d 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 e( regFree2==0 )
2150e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
2150f 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
21510 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 _UMINUS: {.
21511 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 Expr *pLeft = p
21512 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 Expr->pLeft;.
21513 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 assert( pLeft
21514 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c );. if( pL
21515 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 eft->op==TK_FLOA
21516 54 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 T ){. ass
21517 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
21518 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21519 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 IntValue) );.
2151a 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c codeReal(v,
2151b 20 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e pLeft->u.zToken
2151c 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 , 1, target);.
2151d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c }else if( pL
2151e 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 eft->op==TK_INTE
2151f 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 63 GER ){. c
21520 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70 4c odeInteger(v, pL
21521 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74 29 3b eft, 1, target);
21522 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
21523 20 20 20 20 20 20 72 65 67 46 72 65 65 31 20 3d regFree1 =
21524 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 r1 = sqlite3Get
21525 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b TempReg(pParse);
21526 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21527 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21528 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 31 29 _Integer, 0, r1)
21529 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 ;. r2 = s
2152a 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
2152b 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 mp(pParse, pExpr
2152c 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 ->pLeft, ®Fre
2152d 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c e2);. sql
2152e 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
2152f 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72 , OP_Subtract, r
21530 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b 0a 2, r1, target);.
21531 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
21532 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b ( regFree2==0 );
21533 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
21534 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 nReg = target;.
21535 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21536 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 }. case TK_BI
21537 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 TNOT:. case T
21538 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 61 K_NOT: {. a
21539 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e 4f 54 ssert( TK_BITNOT
2153a 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b 0a 20 ==OP_BitNot );.
2153b 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
2153c 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20 NOT==OP_Not );.
2153d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
2153e 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 3b 0a p==TK_BITNOT );.
2153f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21540 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 20 op==TK_NOT );.
21541 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 r1 = sqlite3
21542 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 ExprCodeTemp(pPa
21543 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
21544 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 t, ®Free1);.
21545 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
21546 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
21547 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 inReg = targ
21548 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 et;. sqlite
21549 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 3VdbeAddOp2(v, o
2154a 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b 0a 20 p, r1, inReg);.
2154b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
2154c 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 }. case TK_IS
2154d 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 NULL:. case T
2154e 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 K_NOTNULL: {.
2154f 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 int addr;.
21550 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49 53 assert( TK_IS
21551 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 NULL==OP_IsNull
21552 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21553 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f TK_NOTNULL==OP_
21554 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 NotNull );.
21555 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21556 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 K_ISNULL );.
21557 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21558 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 TK_NOTNULL );.
21559 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2155a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 ddOp2(v, OP_Inte
2155b 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b ger, 1, target);
2155c 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 . r1 = sqli
2155d 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
2155e 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
2155f 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 Left, ®Free1)
21560 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21561 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
21562 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 . addr = sq
21563 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
21564 76 2c 20 6f 70 2c 20 72 31 29 3b 0a 20 20 20 20 v, op, r1);.
21565 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21566 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d Op2(v, OP_AddImm
21567 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b 0a 20 , target, -1);.
21568 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21569 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 JumpHere(v, addr
2156a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
2156b 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
2156c 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 K_AGG_FUNCTION:
2156d 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 6f 20 {. AggInfo
2156e 2a 70 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e *pInfo = pExpr->
2156f 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 pAggInfo;.
21570 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a if( pInfo==0 ){.
21571 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
21572 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
21573 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 (pExpr, EP_IntVa
21574 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 lue) );.
21575 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
21576 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20 pParse, "misuse
21577 6f 66 20 61 67 67 72 65 67 61 74 65 3a 20 25 73 of aggregate: %s
21578 28 29 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 ()", pExpr->u.zT
21579 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c oken);. }el
2157a 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 se{. inRe
2157b 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 g = pInfo->aFunc
2157c 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e 69 4d [pExpr->iAgg].iM
2157d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 em;. }.
2157e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
2157f 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 case TK_CONST
21580 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65 20 _FUNC:. case
21581 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 TK_FUNCTION: {.
21582 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 ExprList *p
21583 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c Farg; /* L
21584 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 ist of function
21585 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 arguments */.
21586 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20 20 20 int nFarg;
21587 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
21588 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 ber of function
21589 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 arguments */.
2158a 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 FuncDef *pDef
2158b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
2158c 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 function defini
2158d 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 tion object */.
2158e 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 int nId;
2158f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
21590 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 75 6e ength of the fun
21591 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e 20 62 79 ction name in by
21592 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e tes */. con
21593 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 st char *zId;
21594 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 /* The funct
21595 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 ion name */.
21596 20 20 69 6e 74 20 63 6f 6e 73 74 4d 61 73 6b 20 int constMask
21597 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4d 61 73 6b = 0; /* Mask
21598 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 of function arg
21599 75 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 20 uments that are
2159a 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 20 20 20 20 constant */.
2159b 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
2159c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
2159d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 counter */.
2159e 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 u8 enc = ENC(d
2159f 62 29 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 b); /* The
215a0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 text encoding us
215a1 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 62 ed by this datab
215a2 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 6f 6c ase */. Col
215a3 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b lSeq *pColl = 0;
215a4 20 20 20 20 2f 2a 20 41 20 63 6f 6c 6c 61 74 69 /* A collati
215a5 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a ng sequence */..
215a6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 assert( !E
215a7 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
215a8 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 Expr, EP_xIsSele
215a9 63 74 29 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ct) );. tes
215aa 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f tcase( op==TK_CO
215ab 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 NST_FUNC );.
215ac 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
215ad 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 TK_FUNCTION );.
215ae 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 if( ExprHas
215af 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 AnyProperty(pExp
215b0 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 r, EP_TokenOnly)
215b1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 61 72 ){. pFar
215b2 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c g = 0;. }el
215b3 73 65 7b 0a 20 20 20 20 20 20 20 20 70 46 61 72 se{. pFar
215b4 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 g = pExpr->x.pLi
215b5 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 st;. }.
215b6 20 20 6e 46 61 72 67 20 3d 20 70 46 61 72 67 20 nFarg = pFarg
215b7 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70 72 20 3a ? pFarg->nExpr :
215b8 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 0;. assert
215b9 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
215ba 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
215bb 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
215bc 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a zId = pExpr->u.z
215bd 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 49 64 Token;. nId
215be 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
215bf 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 20 70 30(zId);. p
215c0 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e Def = sqlite3Fin
215c1 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 dFunction(db, zI
215c2 64 2c 20 6e 49 64 2c 20 6e 46 61 72 67 2c 20 65 d, nId, nFarg, e
215c3 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 nc, 0);. if
215c4 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 ( pDef==0 ){.
215c5 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
215c6 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e rMsg(pParse, "un
215c7 6b 6e 6f 77 6e 20 66 75 6e 63 74 69 6f 6e 3a 20 known function:
215c8 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 20 7a 49 %.*s()", nId, zI
215c9 64 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 d);. brea
215ca 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
215cb 20 69 66 28 20 70 46 61 72 67 20 29 7b 0a 20 20 if( pFarg ){.
215cc 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 r1 = sqlit
215cd 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 e3GetTempRange(p
215ce 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20 Parse, nFarg);.
215cf 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
215d0 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 prCachePush(pPar
215d1 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69 63 6b se); /* Tick
215d2 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65 20 et 2ea2425d34be
215d3 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
215d4 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 e3ExprCodeExprLi
215d5 73 74 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 st(pParse, pFarg
215d6 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 , r1, 1);.
215d7 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
215d8 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 hePop(pParse, 1)
215d9 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32 65 ; /* Ticket 2e
215da 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20 20 a2425d34be */.
215db 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
215dc 20 20 20 72 31 20 3d 20 30 3b 0a 20 20 20 20 20 r1 = 0;.
215dd 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
215de 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
215df 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73 BLE. /* Pos
215e0 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74 sibly overload t
215e1 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 he function if t
215e2 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
215e3 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 t is. ** a
215e4 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f virtual table co
215e5 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 lumn.. **.
215e6 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69 ** For infi
215e7 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b x functions (LIK
215e8 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c E, GLOB, REGEXP,
215e9 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65 20 and MATCH) use
215ea 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 the. ** sec
215eb 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f ond argument, no
215ec 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73 20 t the first, as
215ed 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 the argument to
215ee 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a test to. **
215ef 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61 20 see if it is a
215f0 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 column in a virt
215f1 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 ual table. This
215f2 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73 65 is done because
215f3 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 . ** the le
215f4 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e ft operand of in
215f5 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 fix functions (t
215f6 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77 61 he operand we wa
215f7 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 nt to. ** c
215f8 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 ontrol overloadi
215f9 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20 74 ng) ends up as t
215fa 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
215fb 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20 nt to the.
215fc 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 ** function. Th
215fd 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41 20 e expression "A
215fe 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69 76 glob B" is equiv
215ff 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 alent to .
21600 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20 ** "glob(B,A).
21601 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 We want to use t
21602 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62 20 he A in "A glob
21603 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20 20 B" to test.
21604 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e ** for function
21605 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42 overloading. B
21606 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42 20 ut we use the B
21607 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c term in "glob(B,
21608 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 A)".. */.
21609 20 20 20 20 69 66 28 20 6e 46 61 72 67 3e 3d 32 if( nFarg>=2
2160a 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 && (pExpr->flag
2160b 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 s & EP_InfixFunc
2160c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 ) ){. pDe
2160d 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f f = sqlite3VtabO
2160e 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 verloadFunction(
2160f 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67 2c db, pDef, nFarg,
21610 20 70 46 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 78 pFarg->a[1].pEx
21611 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 pr);. }else
21612 20 69 66 28 20 6e 46 61 72 67 3e 30 20 29 7b 0a if( nFarg>0 ){.
21613 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 pDef = s
21614 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f qlite3VtabOverlo
21615 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 adFunction(db, p
21616 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 Def, nFarg, pFar
21617 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a g->a[0].pExpr);.
21618 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
21619 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
2161a 6e 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 nFarg; i++){.
2161b 20 20 20 20 20 69 66 28 20 69 3c 33 32 20 26 26 if( i<32 &&
2161c 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f sqlite3ExprIsCo
2161d 6e 73 74 61 6e 74 28 70 46 61 72 67 2d 3e 61 5b nstant(pFarg->a[
2161e 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 i].pExpr) ){.
2161f 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b constMask
21620 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20 |= (1<<i);.
21621 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
21622 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 ( (pDef->flags &
21623 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 SQLITE_FUNC_NEE
21624 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 DCOLL)!=0 && !pC
21625 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 oll ){.
21626 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 pColl = sqlite3
21627 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 ExprCollSeq(pPar
21628 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e se, pFarg->a[i].
21629 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 pExpr);.
2162a 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
2162b 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 if( pDef->flags
2162c 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 & SQLITE_FUNC_NE
2162d 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 EDCOLL ){.
2162e 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 if( !pColl ) p
2162f 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 Coll = db->pDflt
21630 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 Coll; . s
21631 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
21632 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 (v, OP_CollSeq,
21633 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 0, 0, 0, (char *
21634 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 )pColl, P4_COLLS
21635 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 EQ);. }.
21636 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21637 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 dOp4(v, OP_Funct
21638 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 ion, constMask,
21639 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 20 r1, target,.
2163a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2163b 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 66 2c (char*)pDef,
2163c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 P4_FUNCDEF);.
2163d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
2163e 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 6e hangeP5(v, (u8)n
2163f 46 61 72 67 29 3b 0a 20 20 20 20 20 20 69 66 28 Farg);. if(
21640 20 6e 46 61 72 67 20 29 7b 0a 20 20 20 20 20 20 nFarg ){.
21641 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
21642 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 TempRange(pParse
21643 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 , r1, nFarg);.
21644 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
21645 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 te3ExprCacheAffi
21646 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 nityChange(pPars
21647 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 e, r1, nFarg);.
21648 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21649 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
2164a 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 _OMIT_SUBQUERY.
2164b 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 case TK_EXIST
2164c 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 S:. case TK_S
2164d 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 74 ELECT: {. t
2164e 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
2164f 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20 EXISTS );.
21650 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21651 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 _SELECT );.
21652 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 sqlite3CodeSubs
21653 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 45 elect(pParse, pE
21654 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 xpr, 0, 0);.
21655 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d inReg = pExpr-
21656 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 >iColumn;.
21657 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
21658 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 case TK_IN: {.
21659 20 20 20 20 20 69 6e 74 20 72 4e 6f 74 46 6f 75 int rNotFou
2165a 6e 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e nd = 0;. in
2165b 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d t rMayHaveNull =
2165c 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 32 0;. int j2
2165d 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20 , j3, j4, j5;.
2165e 20 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 char affinit
2165f 79 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 54 79 y;. int eTy
21660 70 65 3b 0a 0a 20 20 20 20 20 20 56 64 62 65 4e pe;.. VdbeN
21661 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 oopComment((v, "
21662 62 65 67 69 6e 20 49 4e 20 65 78 70 72 20 72 25 begin IN expr r%
21663 64 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20 d", target));.
21664 20 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69 eType = sqli
21665 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70 te3FindInIndex(p
21666 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 Parse, pExpr, &r
21667 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 MayHaveNull);.
21668 20 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 if( rMayHave
21669 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 Null ){.
2166a 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 rNotFound = ++pP
2166b 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
2166c 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 }.. /* Fi
2166d 67 75 72 65 20 6f 75 74 20 74 68 65 20 61 66 66 gure out the aff
2166e 69 6e 69 74 79 20 74 6f 20 75 73 65 20 74 6f 20 inity to use to
2166f 63 72 65 61 74 65 20 61 20 6b 65 79 20 66 72 6f create a key fro
21670 6d 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 m the results.
21671 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 78 ** of the ex
21672 70 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 6e 69 pression. affini
21673 74 79 53 74 72 20 73 74 6f 72 65 73 20 61 20 73 tyStr stores a s
21674 74 61 74 69 63 20 73 74 72 69 6e 67 20 73 75 69 tatic string sui
21675 74 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 20 20 table for.
21676 2a 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 ** P4 of OP_Make
21677 52 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f Record.. */
21678 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20 . affinity
21679 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 = comparisonAffi
2167a 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 0a 0a 20 nity(pExpr);...
2167b 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 /* Code the
2167c 20 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22 3c 65 <expr> from "<e
2167d 78 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22 2e 20 xpr> IN (...)".
2167e 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 The temporary ta
2167f 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 70 45 78 ble. ** pEx
21680 70 72 2d 3e 69 54 61 62 6c 65 20 63 6f 6e 74 61 pr->iTable conta
21681 69 6e 73 20 74 68 65 20 76 61 6c 75 65 73 20 74 ins the values t
21682 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 hat make up the
21683 28 2e 2e 2e 29 20 73 65 74 2e 0a 20 20 20 20 20 (...) set..
21684 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
21685 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 3ExprCachePush(p
21686 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 Parse);. sq
21687 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 lite3ExprCode(pP
21688 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
21689 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 ft, target);.
2168a 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 j2 = sqlite3V
2168b 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
2168c 49 73 4e 75 6c 6c 2c 20 74 61 72 67 65 74 29 3b IsNull, target);
2168d 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 . if( eType
2168e 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 ==IN_INDEX_ROWID
2168f 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 33 20 3d ){. j3 =
21690 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21691 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 p1(v, OP_MustBeI
21692 6e 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 nt, target);.
21693 20 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 j4 = sqlite
21694 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
21695 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 70 45 78 P_NotExists, pEx
21696 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 74 pr->iTable, 0, t
21697 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 arget);.
21698 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21699 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
2169a 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 1, target);.
2169b 20 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65 j5 = sqlite
2169c 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 3VdbeAddOp0(v, O
2169d 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20 P_Goto);.
2169e 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 sqlite3VdbeJump
2169f 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 Here(v, j3);.
216a0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
216a1 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b JumpHere(v, j4);
216a2 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
216a3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
216a4 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72 _Integer, 0, tar
216a5 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 get);. }els
216a6 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 e{. r2 =
216a7 72 65 67 46 72 65 65 32 20 3d 20 73 71 6c 69 74 regFree2 = sqlit
216a8 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
216a9 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f rse);.. /
216aa 2a 20 43 72 65 61 74 65 20 61 20 72 65 63 6f 72 * Create a recor
216ab 64 20 61 6e 64 20 74 65 73 74 20 66 6f 72 20 73 d and test for s
216ac 65 74 20 6d 65 6d 62 65 72 73 68 69 70 2e 20 49 et membership. I
216ad 66 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 69 f the set contai
216ae 6e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 ns. ** th
216af 65 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 6a 75 e value, then ju
216b0 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 mp to the end of
216b1 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 2e 20 the test code.
216b2 54 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 20 The target.
216b3 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20 73 ** register s
216b4 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 till contains th
216b5 65 20 74 72 75 65 20 28 31 29 20 76 61 6c 75 65 e true (1) value
216b6 20 77 72 69 74 74 65 6e 20 74 6f 20 69 74 20 65 written to it e
216b7 61 72 6c 69 65 72 2e 0a 20 20 20 20 20 20 20 20 arlier..
216b8 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
216b9 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
216ba 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 74 OP_MakeRecord, t
216bb 61 72 67 65 74 2c 20 31 2c 20 72 32 2c 20 26 61 arget, 1, r2, &a
216bc 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 ffinity, 1);.
216bd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
216be 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
216bf 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 eger, 1, target)
216c0 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73 ;. j5 = s
216c1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
216c2 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 (v, OP_Found, pE
216c3 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 xpr->iTable, 0,
216c4 72 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a r2);.. /*
216c5 20 49 66 20 74 68 65 20 73 65 74 20 6d 65 6d 62 If the set memb
216c6 65 72 73 68 69 70 20 74 65 73 74 20 66 61 69 6c ership test fail
216c7 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 s, then the resu
216c8 6c 74 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20 lt of the .
216c9 20 20 20 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e ** "x IN (...
216ca 29 22 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 )" expression mu
216cb 73 74 20 62 65 20 65 69 74 68 65 72 20 30 20 6f st be either 0 o
216cc 72 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73 r NULL. If the s
216cd 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f et. ** co
216ce 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 ntains no NULL v
216cf 61 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 alues, then the
216d0 72 65 73 75 6c 74 20 69 73 20 30 2e 20 49 66 20 result is 0. If
216d1 74 68 65 20 73 65 74 20 0a 20 20 20 20 20 20 20 the set .
216d2 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 ** contains one
216d3 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 or more NULL va
216d4 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 lues, then the r
216d5 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 esult of the.
216d6 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 ** expressi
216d7 6f 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e on is also NULL.
216d8 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
216d9 20 20 20 20 69 66 28 20 72 4e 6f 74 46 6f 75 6e if( rNotFoun
216da 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 d==0 ){.
216db 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68 /* This branch
216dc 20 72 75 6e 73 20 69 66 20 69 74 20 69 73 20 6b runs if it is k
216dd 6e 6f 77 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20 nown at compile
216de 74 69 6d 65 20 28 6e 6f 77 29 20 74 68 61 74 20 time (now) that
216df 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 . ** th
216e0 65 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e e set contains n
216e1 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54 o NULL values. T
216e2 68 69 73 20 68 61 70 70 65 6e 73 20 61 73 20 74 his happens as t
216e3 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 he result.
216e4 20 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 ** of a "NOT
216e5 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e NULL" constrain
216e6 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 t in the databas
216e7 65 20 73 63 68 65 6d 61 2e 20 4e 6f 20 6e 65 65 e schema. No nee
216e8 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 d. ** t
216e9 6f 20 74 65 73 74 20 74 68 65 20 64 61 74 61 20 o test the data
216ea 73 74 72 75 63 74 75 72 65 20 61 74 20 72 75 6e structure at run
216eb 74 69 6d 65 20 69 6e 20 74 68 69 73 20 63 61 73 time in this cas
216ec 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
216ed 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
216ee 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
216ef 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 P_Integer, 0, ta
216f0 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d rget);. }
216f1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
216f2 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 70 6f /* This block po
216f3 70 75 6c 61 74 65 73 20 74 68 65 20 72 4e 6f 74 pulates the rNot
216f4 46 6f 75 6e 64 20 72 65 67 69 73 74 65 72 20 77 Found register w
216f5 69 74 68 20 65 69 74 68 65 72 20 4e 55 4c 4c 0a ith either NULL.
216f6 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 ** or
216f7 30 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61 0 (an integer va
216f8 6c 75 65 29 2e 20 49 66 20 74 68 65 20 64 61 74 lue). If the dat
216f9 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 a structure cont
216fa 61 69 6e 73 20 6f 6e 65 0a 20 20 20 20 20 20 20 ains one.
216fb 20 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 20 4e 55 ** or more NU
216fc 4c 4c 73 2c 20 74 68 65 6e 20 73 65 74 20 72 4e LLs, then set rN
216fd 6f 74 46 6f 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e otFound to NULL.
216fe 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74 20 Otherwise, set
216ff 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 it. **
21700 74 6f 20 30 2e 20 49 66 20 72 65 67 69 73 74 65 to 0. If registe
21701 72 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 r rMayHaveNull i
21702 73 20 61 6c 72 65 61 64 79 20 73 65 74 20 74 6f s already set to
21703 20 73 6f 6d 65 20 76 61 6c 75 65 0a 20 20 20 20 some value.
21704 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 74 ** other t
21705 68 61 6e 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 han NULL, then t
21706 68 65 20 74 65 73 74 20 68 61 73 20 61 6c 72 65 he test has alre
21707 61 64 79 20 62 65 65 6e 20 72 75 6e 20 61 6e 64 ady been run and
21708 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 . ** r
21709 4e 6f 74 46 6f 75 6e 64 20 69 73 20 61 6c 72 65 NotFound is alre
2170a 61 64 79 20 70 6f 70 75 6c 61 74 65 64 2e 0a 20 ady populated..
2170b 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
2170c 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e static con
2170d 73 74 20 63 68 61 72 20 6e 75 6c 6c 52 65 63 6f st char nullReco
2170e 72 64 5b 5d 20 3d 20 7b 20 30 78 30 32 2c 20 30 rd[] = { 0x02, 0
2170f 78 30 30 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 x00 };.
21710 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j3 = sqlite3Vdb
21711 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f eAddOp1(v, OP_No
21712 74 4e 75 6c 6c 2c 20 72 4d 61 79 48 61 76 65 4e tNull, rMayHaveN
21713 75 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ull);.
21714 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21715 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
21716 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 rNotFound);.
21717 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21718 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42 beAddOp4(v, OP_B
21719 6c 6f 62 2c 20 32 2c 20 72 4d 61 79 48 61 76 65 lob, 2, rMayHave
2171a 4e 75 6c 6c 2c 20 30 2c 20 0a 20 20 20 20 20 20 Null, 0, .
2171b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2171c 20 20 20 20 20 20 20 6e 75 6c 6c 52 65 63 6f 72 nullRecor
2171d 64 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 d, P4_STATIC);.
2171e 20 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 71 j4 = sq
2171f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21720 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 v, OP_Found, pEx
21721 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 pr->iTable, 0, r
21722 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 MayHaveNull);.
21723 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21724 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21725 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 4e 6f 74 Integer, 0, rNot
21726 46 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 Found);.
21727 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
21728 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 pHere(v, j4);.
21729 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
2172a 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
2172b 33 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 3);.. /
2172c 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 * Copy the value
2172d 20 6f 66 20 72 65 67 69 73 74 65 72 20 72 4e 6f of register rNo
2172e 74 46 6f 75 6e 64 20 28 77 68 69 63 68 20 69 73 tFound (which is
2172f 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 either NULL or
21730 30 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 0). **
21731 69 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 into the target
21732 72 65 67 69 73 74 65 72 2e 20 54 68 69 73 20 77 register. This w
21733 69 6c 6c 20 62 65 20 74 68 65 20 72 65 73 75 6c ill be the resul
21734 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 t of the.
21735 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ** expression
21736 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 .. */.
21737 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
21738 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21739 5f 43 6f 70 79 2c 20 72 4e 6f 74 46 6f 75 6e 64 _Copy, rNotFound
2173a 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
2173b 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
2173c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
2173d 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 mpHere(v, j2);.
2173e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
2173f 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29 3b JumpHere(v, j5);
21740 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
21741 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 prCachePop(pPars
21742 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 56 64 62 e, 1);. Vdb
21743 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e eComment((v, "en
21744 64 20 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20 d IN expr r%d",
21745 74 61 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20 target));.
21746 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
21747 64 69 66 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a dif. /*. *
21748 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79 * x BETWEEN y
21749 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 AND z. **.
2174a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75 ** This is equ
2174b 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a ivalent to. *
2174c 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 *. ** x>=y
2174d 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a AND x<=z. **
2174e 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 6f . ** X is sto
2174f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c red in pExpr->pL
21750 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 eft.. ** Y is
21751 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 stored in pExpr
21752 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 ->pList->a[0].pE
21753 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73 xpr.. ** Z is
21754 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72 stored in pExpr
21755 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 ->pList->a[1].pE
21756 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 xpr.. */.
21757 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a case TK_BETWEEN:
21758 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 {. Expr *p
21759 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c Left = pExpr->pL
2175a 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 eft;. struc
2175b 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
2175c 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 2d *pLItem = pExpr-
2175d 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 >x.pList->a;.
2175e 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 Expr *pRight
2175f 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b = pLItem->pExpr;
21760 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 .. codeComp
21761 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 areOperands(pPar
21762 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 se, pLeft, &r1,
21763 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 ®Free1,.
21764 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21765 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 pRi
21766 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 ght, &r2, ®Fr
21767 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ee2);. test
21768 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d case( regFree1==
21769 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 0 );. testc
2176a 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 ase( regFree2==0
2176b 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73 );. r3 = s
2176c 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
2176d 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
2176e 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r4 = sqlite3GetT
2176f 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
21770 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 codeCompar
21771 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c e(pParse, pLeft,
21772 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a pRight, OP_Ge,.
21773 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21774 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 r1, r2, r3, SQ
21775 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 LITE_STOREP2);.
21776 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 pLItem++;.
21777 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c pRight = pL
21778 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 Item->pExpr;.
21779 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 sqlite3Releas
2177a 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
2177b 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 regFree2);.
2177c 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 r2 = sqlite3Ex
2177d 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
2177e 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 46 e, pRight, ®F
2177f 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 ree2);. tes
21780 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
21781 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 =0 );. code
21782 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 Compare(pParse,
21783 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f pLeft, pRight, O
21784 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 P_Le, r1, r2, r4
21785 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 , SQLITE_STOREP2
21786 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21787 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
21788 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 _And, r3, r4, ta
21789 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c rget);. sql
2178a 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
2178b 65 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a eg(pParse, r3);.
2178c 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c sqlite3Rel
2178d 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
2178e 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62 se, r4);. b
2178f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21790 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b case TK_UPLUS: {
21791 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 . inReg = s
21792 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
21793 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 rget(pParse, pEx
21794 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 pr->pLeft, targe
21795 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b t);. break;
21796 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 . }.. case
21797 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 7b 0a 20 TK_TRIGGER: {.
21798 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f /* If the o
21799 70 63 6f 64 65 20 69 73 20 54 4b 5f 54 52 49 47 pcode is TK_TRIG
2179a 47 45 52 2c 20 74 68 65 6e 20 74 68 65 20 65 78 GER, then the ex
2179b 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 72 65 pression is a re
2179c 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20 2a 2a ference. **
2179d 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 to a column in
2179e 74 68 65 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 the new.* or old
2179f 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 .* pseudo-tables
217a0 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 0a 20 20 available to.
217a1 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 70 ** trigger p
217a2 72 6f 67 72 61 6d 73 2e 20 49 6e 20 74 68 69 73 rograms. In this
217a3 20 63 61 73 65 20 45 78 70 72 2e 69 54 61 62 6c case Expr.iTabl
217a4 65 20 69 73 20 73 65 74 20 74 6f 20 31 20 66 6f e is set to 1 fo
217a5 72 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6e r the. ** n
217a6 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c ew.* pseudo-tabl
217a7 65 2c 20 6f 72 20 30 20 66 6f 72 20 74 68 65 20 e, or 0 for the
217a8 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 old.* pseudo-tab
217a9 6c 65 2e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e le. Expr.iColumn
217aa 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65 74 . ** is set
217ab 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f to the column o
217ac 66 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 f the pseudo-tab
217ad 6c 65 20 74 6f 20 72 65 61 64 2c 20 6f 72 20 74 le to read, or t
217ae 6f 20 2d 31 20 74 6f 0a 20 20 20 20 20 20 2a 2a o -1 to. **
217af 20 72 65 61 64 20 74 68 65 20 72 6f 77 69 64 20 read the rowid
217b0 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2a 0a field.. **.
217b1 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65 78 70 ** The exp
217b2 72 65 73 73 69 6f 6e 20 69 73 20 69 6d 70 6c 65 ression is imple
217b3 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 61 6e 20 mented using an
217b4 4f 50 5f 50 61 72 61 6d 20 6f 70 63 6f 64 65 2e OP_Param opcode.
217b5 20 54 68 65 20 70 31 0a 20 20 20 20 20 20 2a 2a The p1. **
217b6 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 73 65 parameter is se
217b7 74 20 74 6f 20 30 20 66 6f 72 20 61 6e 20 6f 6c t to 0 for an ol
217b8 64 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e 63 d.rowid referenc
217b9 65 2c 20 6f 72 20 74 6f 20 28 69 2b 31 29 0a 20 e, or to (i+1).
217ba 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 66 65 72 ** to refer
217bb 65 6e 63 65 20 61 6e 6f 74 68 65 72 20 63 6f 6c ence another col
217bc 75 6d 6e 20 6f 66 20 74 68 65 20 6f 6c 64 2e 2a umn of the old.*
217bd 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 77 pseudo-table, w
217be 68 65 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 here . ** i
217bf 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
217c0 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 46 6f 72 the column. For
217c1 20 61 20 6e 65 77 2e 72 6f 77 69 64 20 72 65 66 a new.rowid ref
217c2 65 72 65 6e 63 65 2c 20 70 31 20 69 73 0a 20 20 erence, p1 is.
217c3 20 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 28 6e ** set to (n
217c4 2b 31 29 2c 20 77 68 65 72 65 20 6e 20 69 73 20 +1), where n is
217c5 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f the number of co
217c6 6c 75 6d 6e 73 20 69 6e 20 65 61 63 68 20 70 73 lumns in each ps
217c7 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 20 20 20 20 eudo-table..
217c8 20 20 2a 2a 20 46 6f 72 20 61 20 72 65 66 65 72 ** For a refer
217c9 65 6e 63 65 20 74 6f 20 61 6e 79 20 6f 74 68 65 ence to any othe
217ca 72 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 r column in the
217cb 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 new.* pseudo-tab
217cc 6c 65 2c 20 70 31 0a 20 20 20 20 20 20 2a 2a 20 le, p1. **
217cd 69 73 20 73 65 74 20 74 6f 20 28 6e 2b 32 2b 69 is set to (n+2+i
217ce 29 2c 20 77 68 65 72 65 20 6e 20 61 6e 64 20 69 ), where n and i
217cf 20 61 72 65 20 61 73 20 64 65 66 69 6e 65 64 20 are as defined
217d0 70 72 65 76 69 6f 75 73 6c 79 2e 20 46 6f 72 0a previously. For.
217d1 20 20 20 20 20 20 2a 2a 20 65 78 61 6d 70 6c 65 ** example
217d2 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f , if the table o
217d3 6e 20 77 68 69 63 68 20 74 72 69 67 67 65 72 73 n which triggers
217d4 20 61 72 65 20 62 65 69 6e 67 20 66 69 72 65 64 are being fired
217d5 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65 63 is. ** dec
217d6 6c 61 72 65 64 20 61 73 3a 0a 20 20 20 20 20 20 lared as:.
217d7 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 43 52 **. ** CR
217d8 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c EATE TABLE t1(a,
217d9 20 62 29 3b 0a 20 20 20 20 20 20 2a 2a 0a 20 20 b);. **.
217da 20 20 20 20 2a 2a 20 54 68 65 6e 20 70 31 20 69 ** Then p1 i
217db 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 s interpreted as
217dc 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 20 20 follows:.
217dd 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31 **. ** p1
217de 3d 3d 30 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e ==0 -> old.
217df 72 6f 77 69 64 20 20 20 20 20 70 31 3d 3d 33 20 rowid p1==3
217e0 20 20 2d 3e 20 20 20 20 6e 65 77 2e 72 6f 77 69 -> new.rowi
217e1 64 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31 3d d. ** p1=
217e2 3d 31 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 61 =1 -> old.a
217e3 20 20 20 20 20 20 20 20 20 70 31 3d 3d 34 20 20 p1==4
217e4 20 2d 3e 20 20 20 20 6e 65 77 2e 61 0a 20 20 20 -> new.a.
217e5 20 20 20 2a 2a 20 20 20 70 31 3d 3d 32 20 20 20 ** p1==2
217e6 2d 3e 20 20 20 20 6f 6c 64 2e 62 20 20 20 20 20 -> old.b
217e7 20 20 20 20 70 31 3d 3d 35 20 20 20 2d 3e 20 20 p1==5 ->
217e8 20 20 6e 65 77 2e 62 20 20 20 20 20 20 20 0a 20 new.b .
217e9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54 61 */. Ta
217ea 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 45 78 70 ble *pTab = pExp
217eb 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 69 r->pTab;. i
217ec 6e 74 20 70 31 20 3d 20 70 45 78 70 72 2d 3e 69 nt p1 = pExpr->i
217ed 54 61 62 6c 65 20 2a 20 28 70 54 61 62 2d 3e 6e Table * (pTab->n
217ee 43 6f 6c 2b 31 29 20 2b 20 31 20 2b 20 70 45 78 Col+1) + 1 + pEx
217ef 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 0a 20 20 pr->iColumn;..
217f0 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
217f1 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 7c 7c 20 r->iTable==0 ||
217f2 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 31 pExpr->iTable==1
217f3 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
217f4 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e ( pExpr->iColumn
217f5 3e 3d 2d 31 20 26 26 20 70 45 78 70 72 2d 3e 69 >=-1 && pExpr->i
217f6 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f Column<pTab->nCo
217f7 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 l );. asser
217f8 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 30 t( pTab->iPKey<0
217f9 20 7c 7c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 || pExpr->iColu
217fa 6d 6e 21 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 mn!=pTab->iPKey
217fb 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
217fc 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 28 70 54 p1>=0 && p1<(pT
217fd 61 62 2d 3e 6e 43 6f 6c 2a 32 2b 32 29 20 29 3b ab->nCol*2+2) );
217fe 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 .. sqlite3V
217ff 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
21800 50 61 72 61 6d 2c 20 70 31 2c 20 74 61 72 67 65 Param, p1, targe
21801 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f t);. VdbeCo
21802 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73 mment((v, "%s.%s
21803 20 2d 3e 20 24 25 64 22 2c 0a 20 20 20 20 20 20 -> $%d",.
21804 20 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 (pExpr->iTable
21805 20 3f 20 22 6e 65 77 22 20 3a 20 22 6f 6c 64 22 ? "new" : "old"
21806 29 2c 0a 20 20 20 20 20 20 20 20 28 70 45 78 70 ),. (pExp
21807 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 3f 20 22 r->iColumn<0 ? "
21808 72 6f 77 69 64 22 20 3a 20 70 45 78 70 72 2d 3e rowid" : pExpr->
21809 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 pTab->aCol[pExpr
2180a 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65 ->iColumn].zName
2180b 29 2c 0a 20 20 20 20 20 20 20 20 74 61 72 67 65 ),. targe
2180c 74 0a 20 20 20 20 20 20 29 29 3b 0a 0a 20 20 20 t. ));..
2180d 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c /* If the col
2180e 75 6d 6e 20 68 61 73 20 52 45 41 4c 20 61 66 66 umn has REAL aff
2180f 69 6e 69 74 79 2c 20 69 74 20 6d 61 79 20 63 75 inity, it may cu
21810 72 72 65 6e 74 6c 79 20 62 65 20 73 74 6f 72 65 rrently be store
21811 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 2a 2a d as an. **
21812 20 69 6e 74 65 67 65 72 2e 20 55 73 65 20 4f 50 integer. Use OP
21813 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 74 6f _RealAffinity to
21814 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 make sure it is
21815 20 72 65 61 6c 6c 79 20 72 65 61 6c 2e 20 20 2a really real. *
21816 2f 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 /. if( pExp
21817 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 0a 20 r->iColumn>=0 .
21818 20 20 20 20 20 20 26 26 20 70 54 61 62 2d 3e 61 && pTab->a
21819 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 Col[pExpr->iColu
2181a 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 53 51 mn].affinity==SQ
2181b 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 LITE_AFF_REAL.
2181c 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ){. s
2181d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
2181e 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e (v, OP_RealAffin
2181f 69 74 79 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 ity, target);.
21820 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
21821 6b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f k;. }... /
21822 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 3a *. ** Form A:
21823 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 78 . ** CASE x
21824 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 WHEN e1 THEN r1
21825 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 WHEN e2 THEN r2
21826 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 ... WHEN eN THE
21827 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a N rN ELSE y END.
21828 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f **. ** Fo
21829 72 6d 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20 43 rm B:. ** C
2182a 41 53 45 20 57 48 45 4e 20 65 31 20 54 48 45 4e ASE WHEN e1 THEN
2182b 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e r1 WHEN e2 THEN
2182c 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 r2 ... WHEN eN
2182d 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 THEN rN ELSE y E
2182e 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a ND. **. **
2182f 20 46 6f 72 6d 20 41 20 69 73 20 63 61 6e 20 62 Form A is can b
21830 65 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e e transformed in
21831 74 6f 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e to the equivalen
21832 74 20 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c 6c t form B as foll
21833 6f 77 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 ows:. ** CA
21834 53 45 20 57 48 45 4e 20 78 3d 65 31 20 54 48 45 SE WHEN x=e1 THE
21835 4e 20 72 31 20 57 48 45 4e 20 78 3d 65 32 20 54 N r1 WHEN x=e2 T
21836 48 45 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20 2a HEN r2 .... *
21837 2a 20 20 20 20 20 20 20 20 57 48 45 4e 20 78 3d * WHEN x=
21838 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 eN THEN rN ELSE
21839 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 y END. **.
2183a 20 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78 69 ** X (if it exi
2183b 73 74 73 29 20 69 73 20 69 6e 20 70 45 78 70 72 sts) is in pExpr
2183c 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 ->pLeft.. **
2183d 59 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 Y is in pExpr->p
2183e 52 69 67 68 74 2e 20 20 54 68 65 20 59 20 69 73 Right. The Y is
2183f 20 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 also optional.
21840 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0a If there is no.
21841 20 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61 75 ** ELSE clau
21842 73 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 se and no other
21843 74 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74 68 term matches, th
21844 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 en the result of
21845 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70 72 the. ** expr
21846 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 ssion is NULL..
21847 20 20 20 2a 2a 20 45 69 20 69 73 20 69 6e 20 70 ** Ei is in p
21848 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 Expr->pList->a[i
21849 2a 32 5d 20 61 6e 64 20 52 69 20 69 73 20 70 45 *2] and Ri is pE
2184a 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a xpr->pList->a[i*
2184b 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 2+1].. **.
2184c 20 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f ** The result o
2184d 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
2184e 20 69 73 20 74 68 65 20 52 69 20 66 6f 72 20 74 is the Ri for t
2184f 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e he first matchin
21850 67 20 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 g Ei,. ** or
21851 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d if there is no m
21852 61 74 63 68 69 6e 67 20 45 69 2c 20 74 68 65 20 atching Ei, the
21853 45 4c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72 20 ELSE term Y, or
21854 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 20 20 if there is.
21855 2a 2a 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d 2c ** no ELSE term,
21856 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 NULL.. */.
21857 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72 default: asser
21858 74 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 45 20 29 t( op==TK_CASE )
21859 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e ; {. int en
2185a 64 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 dLabel;
2185b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 /* G
2185c 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 65 6e OTO label for en
2185d 64 20 6f 66 20 43 41 53 45 20 73 74 6d 74 20 2a d of CASE stmt *
2185e 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78 74 /. int next
2185f 43 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 Case;
21860 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 /* GOT
21861 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65 78 74 O label for next
21862 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a WHEN clause */.
21863 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 3b int nExpr;
21864 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21865 20 20 20 20 20 20 20 20 2f 2a 20 32 78 20 6e 75 /* 2x nu
21866 6d 62 65 72 20 6f 66 20 57 48 45 4e 20 74 65 72 mber of WHEN ter
21867 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ms */. int
21868 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
21869 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2186a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
2186b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 . ExprList
2186c 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20 *pEList;
2186d 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 /* List
2186e 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a of WHEN terms *
2186f 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 /. struct E
21870 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c xprList_item *aL
21871 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 istelem; /* Arr
21872 61 79 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 ay of WHEN terms
21873 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 6f */. Expr o
21874 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 20 20 pCompare;
21875 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
21876 68 65 20 58 3d 3d 45 69 20 65 78 70 72 65 73 73 he X==Ei express
21877 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 ion */. Exp
21878 72 20 63 61 63 68 65 58 3b 20 20 20 20 20 20 20 r cacheX;
21879 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2187a 2a 20 43 61 63 68 65 64 20 65 78 70 72 65 73 73 * Cached express
2187b 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20 20 45 ion X */. E
2187c 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20 xpr *pX;
2187d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2187e 20 2f 2a 20 54 68 65 20 58 20 65 78 70 72 65 73 /* The X expres
2187f 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 sion */. Ex
21880 70 72 20 2a 70 54 65 73 74 20 3d 20 30 3b 20 20 pr *pTest = 0;
21881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21882 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d 20 41 /* X==Ei (form A
21883 29 20 6f 72 20 6a 75 73 74 20 45 69 20 28 66 6f ) or just Ei (fo
21884 72 6d 20 42 29 20 2a 2f 0a 20 20 20 20 20 20 56 rm B) */. V
21885 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 69 43 61 VA_ONLY( int iCa
21886 63 68 65 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 cheLevel = pPars
21887 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 e->iCacheLevel;
21888 29 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ).. assert(
21889 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 !ExprHasPropert
2188a 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
2188b 65 6c 65 63 74 29 20 26 26 20 70 45 78 70 72 2d elect) && pExpr-
2188c 3e 78 2e 70 4c 69 73 74 20 29 3b 0a 20 20 20 20 >x.pList );.
2188d 20 20 61 73 73 65 72 74 28 28 70 45 78 70 72 2d assert((pExpr-
2188e 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 >x.pList->nExpr
2188f 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 20 % 2) == 0);.
21890 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d 3e assert(pExpr->
21891 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e x.pList->nExpr >
21892 20 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 73 0);. pELis
21893 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 t = pExpr->x.pLi
21894 73 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 65 st;. aListe
21895 6c 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b lem = pEList->a;
21896 0a 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70 . nExpr = p
21897 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 EList->nExpr;.
21898 20 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 endLabel = s
21899 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 qlite3VdbeMakeLa
2189a 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66 bel(v);. if
2189b 28 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 ( (pX = pExpr->p
2189c 4c 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 Left)!=0 ){.
2189d 20 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58 cacheX = *pX
2189e 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
2189f 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 se( pX->op==TK_C
218a0 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 OLUMN );.
218a1 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f testcase( pX->o
218a2 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 p==TK_REGISTER )
218a3 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58 ;. cacheX
218a4 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 .iTable = sqlite
218a5 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
218a6 61 72 73 65 2c 20 70 58 2c 20 26 72 65 67 46 72 arse, pX, ®Fr
218a7 65 65 31 29 3b 0a 20 20 20 20 20 20 20 20 74 65 ee1);. te
218a8 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 stcase( regFree1
218a9 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 63 ==0 );. c
218aa 61 63 68 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 acheX.op = TK_RE
218ab 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20 20 GISTER;.
218ac 6f 70 43 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54 opCompare.op = T
218ad 4b 5f 45 51 3b 0a 20 20 20 20 20 20 20 20 6f 70 K_EQ;. op
218ae 43 6f 6d 70 61 72 65 2e 70 4c 65 66 74 20 3d 20 Compare.pLeft =
218af 26 63 61 63 68 65 58 3b 0a 20 20 20 20 20 20 20 &cacheX;.
218b0 20 70 54 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70 pTest = &opComp
218b1 61 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 are;. }.
218b2 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 for(i=0; i<nE
218b3 78 70 72 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 20 xpr; i=i+2){.
218b4 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
218b5 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 CachePush(pParse
218b6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
218b7 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 X ){. a
218b8 73 73 65 72 74 28 20 70 54 65 73 74 21 3d 30 20 ssert( pTest!=0
218b9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 70 43 );. opC
218ba 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20 3d 20 ompare.pRight =
218bb 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 aListelem[i].pEx
218bc 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 pr;. }els
218bd 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 e{. pTe
218be 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 st = aListelem[i
218bf 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 ].pExpr;.
218c0 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78 74 43 }. nextC
218c1 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ase = sqlite3Vdb
218c2 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 eMakeLabel(v);.
218c3 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
218c4 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 pTest->op==TK_C
218c5 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 OLUMN );.
218c6 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 sqlite3ExprIfFa
218c7 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65 73 lse(pParse, pTes
218c8 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 51 4c t, nextCase, SQL
218c9 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b ITE_JUMPIFNULL);
218ca 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
218cb 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 e( aListelem[i+1
218cc 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ].pExpr->op==TK_
218cd 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 COLUMN );.
218ce 20 20 74 65 73 74 63 61 73 65 28 20 61 4c 69 73 testcase( aLis
218cf 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 telem[i+1].pExpr
218d0 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 ->op==TK_REGISTE
218d1 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c R );. sql
218d2 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 ite3ExprCode(pPa
218d3 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d 5b 69 rse, aListelem[i
218d4 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 65 +1].pExpr, targe
218d5 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 t);. sqli
218d6 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
218d7 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 OP_Goto, 0, end
218d8 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 Label);.
218d9 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
218da 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a Pop(pParse, 1);.
218db 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
218dc 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
218dd 76 2c 20 6e 65 78 74 43 61 73 65 29 3b 0a 20 20 v, nextCase);.
218de 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
218df 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 29 7b pExpr->pRight ){
218e0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
218e1 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 ExprCachePush(pP
218e2 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 arse);. s
218e3 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
218e4 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 Parse, pExpr->pR
218e5 69 67 68 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 ight, target);.
218e6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
218e7 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 prCachePop(pPars
218e8 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c e, 1);. }el
218e9 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
218ea 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
218eb 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 OP_Null, 0, tar
218ec 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 get);. }.
218ed 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
218ee 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 mallocFailed ||
218ef 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 0a pParse->nErr>0 .
218f0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50 || pP
218f1 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 arse->iCacheLeve
218f2 6c 3d 3d 69 43 61 63 68 65 4c 65 76 65 6c 20 29 l==iCacheLevel )
218f3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
218f4 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
218f5 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 v, endLabel);.
218f6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
218f7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
218f8 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 OMIT_TRIGGER.
218f9 20 63 61 73 65 20 54 4b 5f 52 41 49 53 45 3a 20 case TK_RAISE:
218fa 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
218fb 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d pExpr->affinity=
218fc 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 0a 20 20 =OE_Rollback .
218fd 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 || pExp
218fe 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f r->affinity==OE_
218ff 41 62 6f 72 74 0a 20 20 20 20 20 20 20 20 20 20 Abort.
21900 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e || pExpr->affin
21901 69 74 79 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 ity==OE_Fail.
21902 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72 || pExpr
21903 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 ->affinity==OE_I
21904 67 6e 6f 72 65 0a 20 20 20 20 20 20 29 3b 0a 20 gnore. );.
21905 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73 65 if( !pParse
21906 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29 7b ->pTriggerTab ){
21907 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21908 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
21909 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
2190a 20 20 20 20 20 20 20 20 22 52 41 49 53 45 28 29 "RAISE()
2190b 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 may only be use
2190c 64 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 d within a trigg
2190d 65 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 er-program");.
2190e 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
2190f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
21910 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 ( pExpr->affinit
21911 79 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 y==OE_Abort ){.
21912 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 sqlite3Ma
21913 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a yAbort(pParse);.
21914 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 }. as
21915 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 sert( !ExprHasPr
21916 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
21917 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 _IntValue) );.
21918 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 61 if( pExpr->a
21919 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f ffinity==OE_Igno
2191a 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 re ){. sq
2191b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
2191c 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 2c 20 . v,
2191d 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f OP_Halt, SQLITE_
2191e 4f 4b 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 30 OK, OE_Ignore, 0
2191f 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 , pExpr->u.zToke
21920 6e 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 n,0);. }els
21921 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 e{. sqlit
21922 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 e3HaltConstraint
21923 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21924 61 66 66 69 6e 69 74 79 2c 20 70 45 78 70 72 2d affinity, pExpr-
21925 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 >u.zToken, 0);.
21926 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 62 72 }.. br
21927 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 eak;. }.#endi
21928 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 f. }. sqlite3R
21929 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
2192a 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b arse, regFree1);
2192b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 . sqlite3Releas
2192c 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
2192d 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65 regFree2);. re
2192e 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f turn inReg;.}../
2192f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
21930 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 de to evaluate a
21931 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 n expression and
21932 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
21933 74 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 ts.** into a reg
21934 69 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 ister. Return t
21935 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 he register numb
21936 65 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73 er where the res
21937 75 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 ults.** are stor
21938 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ed..**.** If the
21939 20 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74 register is a t
2193a 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 emporary registe
2193b 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65 r that can be de
2193c 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68 allocated,.** th
2193d 65 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d en write its num
2193e 62 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20 ber into *pReg.
2193f 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72 If the result r
21940 65 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a egister is not.*
21941 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74 * a temporary, t
21942 68 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f hen set *pReg to
21943 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 zero..*/.SQLITE
21944 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
21945 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 ite3ExprCodeTemp
21946 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
21947 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 Expr *pExpr, int
21948 20 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72 *pReg){. int r
21949 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 1 = sqlite3GetTe
2194a 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 mpReg(pParse);.
2194b 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 int r2 = sqlite
2194c 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 3ExprCodeTarget(
2194d 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 pParse, pExpr, r
2194e 31 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31 1);. if( r2==r1
2194f 20 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 ){. *pReg =
21950 72 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 r1;. }else{.
21951 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
21952 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
21953 31 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 1);. *pReg =
21954 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
21955 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e r2;.}../*.** Gen
21956 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 erate code that
21957 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78 will evaluate ex
21958 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61 pression pExpr a
21959 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 nd store the.**
2195a 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 results in regis
2195b 74 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65 ter target. The
2195c 20 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61 results are gua
2195d 72 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61 ranteed to appea
2195e 72 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 r.** in register
2195f 20 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 target..*/.SQLI
21960 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
21961 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 qlite3ExprCode(P
21962 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
21963 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 pr *pExpr, int t
21964 61 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e arget){. int in
21965 52 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 Reg;.. assert(
21966 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 target>0 && targ
21967 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d et<=pParse->nMem
21968 20 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 );. inReg = sq
21969 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 lite3ExprCodeTar
2196a 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 get(pParse, pExp
2196b 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 r, target);. as
2196c 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 sert( pParse->pV
2196d 64 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 dbe || pParse->d
2196e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
2196f 29 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d );. if( inReg!=
21970 74 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65 target && pParse
21971 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73 ->pVdbe ){. s
21972 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21973 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 (pParse->pVdbe,
21974 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c OP_SCopy, inReg,
21975 20 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 target);. }.
21976 72 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d return target;.}
21977 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
21978 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 code that evalu
21979 74 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78 tes the given ex
2197a 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74 pression and put
2197b 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 s the result.**
2197c 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 in register targ
2197d 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d et..**.** Also m
2197e 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ake a copy of th
2197f 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73 e expression res
21980 75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65 ults into anothe
21981 72 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74 r "cache" regist
21982 65 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79 er.** and modify
21983 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
21984 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 so that the next
21985 20 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c time it is eval
21986 75 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65 uated,.** the re
21987 73 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f sult is a copy o
21988 66 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69 f the cache regi
21989 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ster..**.** This
2198a 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
2198b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 for expressions
2198c 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d that are used m
2198d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 ultiple .** time
2198e 73 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61 s. They are eva
2198f 6c 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 luated once and
21990 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 the results of t
21991 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a he expression.**
21992 20 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a are reused..*/.
21993 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
21994 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
21995 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65 deAndCache(Parse
21996 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
21997 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 pExpr, int targe
21998 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 t){. Vdbe *v =
21999 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
2199a 20 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e int inReg;. in
2199b 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 Reg = sqlite3Exp
2199c 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 rCode(pParse, pE
2199d 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 xpr, target);.
2199e 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 assert( target>0
2199f 20 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f );. /* This ro
219a0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 utine is called
219a1 66 6f 72 20 74 65 72 6d 73 20 74 6f 20 49 4e 53 for terms to INS
219a2 45 52 54 20 6f 72 20 55 50 44 41 54 45 2e 20 20 ERT or UPDATE.
219a3 41 6e 64 20 74 68 65 20 6f 6e 6c 79 0a 20 20 2a And the only. *
219a4 2a 20 6f 74 68 65 72 20 70 6c 61 63 65 20 77 68 * other place wh
219a5 65 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 ere expressions
219a6 63 61 6e 20 62 65 20 63 6f 6e 76 65 72 74 65 64 can be converted
219a7 20 69 6e 74 6f 20 54 4b 5f 52 45 47 49 53 54 45 into TK_REGISTE
219a8 52 20 69 73 0a 20 20 2a 2a 20 69 6e 20 57 48 45 R is. ** in WHE
219a9 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 RE clause proces
219aa 73 69 6e 67 2e 20 20 53 6f 20 61 73 20 63 75 72 sing. So as cur
219ab 72 65 6e 74 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 rently implement
219ac 65 64 2c 20 74 68 65 72 65 20 69 73 0a 20 20 2a ed, there is. *
219ad 2a 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20 54 * no way for a T
219ae 4b 5f 52 45 47 49 53 54 45 52 20 74 6f 20 65 78 K_REGISTER to ex
219af 69 73 74 20 68 65 72 65 2e 20 20 42 75 74 20 69 ist here. But i
219b0 74 20 73 65 65 6d 73 20 70 72 75 64 65 6e 74 20 t seems prudent
219b1 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 20 74 68 65 to. ** keep the
219b2 20 41 4c 57 41 59 53 28 29 20 69 6e 20 63 61 73 ALWAYS() in cas
219b3 65 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 e the conditions
219b4 20 61 62 6f 76 65 20 63 68 61 6e 67 65 20 77 69 above change wi
219b5 74 68 20 66 75 74 75 72 65 0a 20 20 2a 2a 20 6d th future. ** m
219b6 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 6f 72 20 odifications or
219b7 65 6e 68 61 6e 63 65 6d 65 6e 74 73 2e 20 2a 2f enhancements. */
219b8 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 45 . if( ALWAYS(pE
219b9 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 52 45 47 49 xpr->op!=TK_REGI
219ba 53 54 45 52 29 20 29 7b 20 20 0a 20 20 20 20 69 STER) ){ . i
219bb 6e 74 20 69 4d 65 6d 3b 0a 20 20 20 20 69 4d 65 nt iMem;. iMe
219bc 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d m = ++pParse->nM
219bd 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 em;. sqlite3V
219be 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
219bf 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 69 4d 65 Copy, inReg, iMe
219c0 6d 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 m);. pExpr->i
219c1 54 61 62 6c 65 20 3d 20 69 4d 65 6d 3b 0a 20 20 Table = iMem;.
219c2 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b pExpr->op = TK
219c3 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 7d 0a 20 _REGISTER;. }.
219c4 20 72 65 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d return inReg;.}
219c5 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 ../*.** Return T
219c6 52 55 45 20 69 66 20 70 45 78 70 72 20 69 73 20 RUE if pExpr is
219c7 61 6e 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 an constant expr
219c8 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 ession that is a
219c9 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 66 6f ppropriate.** fo
219ca 72 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 r factoring out
219cb 6f 66 20 61 20 6c 6f 6f 70 2e 20 20 41 70 70 72 of a loop. Appr
219cc 6f 70 72 69 61 74 65 20 65 78 70 72 65 73 73 69 opriate expressi
219cd 6f 6e 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 ons are:.**.**
219ce 20 20 2a 20 20 41 6e 79 20 65 78 70 72 65 73 73 * Any express
219cf 69 6f 6e 20 74 68 61 74 20 65 76 61 6c 75 61 74 ion that evaluat
219d0 65 73 20 74 6f 20 74 77 6f 20 6f 72 20 6d 6f 72 es to two or mor
219d1 65 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a e opcodes..**.**
219d2 20 20 20 20 2a 20 20 41 6e 79 20 4f 50 5f 49 6e * Any OP_In
219d3 74 65 67 65 72 2c 20 4f 50 5f 52 65 61 6c 2c 20 teger, OP_Real,
219d4 4f 50 5f 53 74 72 69 6e 67 2c 20 4f 50 5f 42 6c OP_String, OP_Bl
219d5 6f 62 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 0a 2a 2a ob, OP_Null, .**
219d6 20 20 20 20 20 20 20 6f 72 20 4f 50 5f 56 61 72 or OP_Var
219d7 69 61 62 6c 65 20 74 68 61 74 20 64 6f 65 73 20 iable that does
219d8 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 70 not need to be p
219d9 6c 61 63 65 64 20 69 6e 20 61 20 0a 2a 2a 20 20 laced in a .**
219da 20 20 20 20 20 73 70 65 63 69 66 69 63 20 72 65 specific re
219db 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 gister..**.** Th
219dc 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20 ere is no point
219dd 69 6e 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 in factoring out
219de 20 73 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 single-instruct
219df 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 ion constant.**
219e0 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 expressions that
219e1 20 6e 65 65 64 20 74 6f 20 62 65 20 70 6c 61 63 need to be plac
219e2 65 64 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c ed in a particul
219e3 61 72 20 72 65 67 69 73 74 65 72 2e 20 20 0a 2a ar register. .*
219e4 2a 20 57 65 20 63 6f 75 6c 64 20 66 61 63 74 6f * We could facto
219e5 72 20 74 68 65 6d 20 6f 75 74 2c 20 62 75 74 20 r them out, but
219e6 74 68 65 6e 20 77 65 20 77 6f 75 6c 64 20 65 6e then we would en
219e7 64 20 75 70 20 61 64 64 69 6e 67 20 61 6e 0a 2a d up adding an.*
219e8 2a 20 4f 50 5f 53 43 6f 70 79 20 69 6e 73 74 72 * OP_SCopy instr
219e9 75 63 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 74 uction to move t
219ea 68 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 he value into th
219eb 65 20 63 6f 72 72 65 63 74 20 72 65 67 69 73 74 e correct regist
219ec 65 72 0a 2a 2a 20 6c 61 74 65 72 2e 20 20 57 65 er.** later. We
219ed 20 6d 69 67 68 74 20 61 73 20 77 65 6c 6c 20 6a might as well j
219ee 75 73 74 20 75 73 65 20 74 68 65 20 6f 72 69 67 ust use the orig
219ef 69 6e 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e inal instruction
219f0 20 61 6e 64 0a 2a 2a 20 61 76 6f 69 64 20 74 68 and.** avoid th
219f1 65 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 73 e OP_SCopy..*/.s
219f2 74 61 74 69 63 20 69 6e 74 20 69 73 41 70 70 72 tatic int isAppr
219f3 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72 opriateForFactor
219f4 69 6e 67 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 ing(Expr *p){.
219f5 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 if( !sqlite3Expr
219f6 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 IsConstantNotJoi
219f7 6e 28 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75 n(p) ){. retu
219f8 72 6e 20 30 3b 20 20 2f 2a 20 4f 6e 6c 79 20 63 rn 0; /* Only c
219f9 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 onstant expressi
219fa 6f 6e 73 20 61 72 65 20 61 70 70 72 6f 70 72 69 ons are appropri
219fb 61 74 65 20 66 6f 72 20 66 61 63 74 6f 72 69 6e ate for factorin
219fc 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 28 g */. }. if( (
219fd 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 46 69 p->flags & EP_Fi
219fe 78 65 64 44 65 73 74 29 3d 3d 30 20 29 7b 0a 20 xedDest)==0 ){.
219ff 20 20 20 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a return 1; /*
21a00 20 41 6e 79 20 63 6f 6e 73 74 61 6e 74 20 77 69 Any constant wi
21a01 74 68 6f 75 74 20 61 20 66 69 78 65 64 20 64 65 thout a fixed de
21a02 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 70 70 stination is app
21a03 72 6f 70 72 69 61 74 65 20 2a 2f 0a 20 20 7d 0a ropriate */. }.
21a04 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d while( p->op==
21a05 54 4b 5f 55 50 4c 55 53 20 29 20 70 20 3d 20 70 TK_UPLUS ) p = p
21a06 2d 3e 70 4c 65 66 74 3b 0a 20 20 73 77 69 74 63 ->pLeft;. switc
21a07 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 23 69 66 6e h( p->op ){.#ifn
21a08 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
21a09 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 BLOB_LITERAL.
21a0a 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 0a 23 case TK_BLOB:.#
21a0b 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 endif. case T
21a0c 4b 5f 56 41 52 49 41 42 4c 45 3a 0a 20 20 20 20 K_VARIABLE:.
21a0d 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a case TK_INTEGER:
21a0e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f . case TK_FLO
21a0f 41 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f AT:. case TK_
21a10 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 NULL:. case T
21a11 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 K_STRING: {.
21a12 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f testcase( p->o
21a13 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 3b 0a 20 20 p==TK_BLOB );.
21a14 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d testcase( p-
21a15 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 >op==TK_VARIABLE
21a16 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21a17 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e se( p->op==TK_IN
21a18 54 45 47 45 52 20 29 3b 0a 20 20 20 20 20 20 74 TEGER );. t
21a19 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d estcase( p->op==
21a1a 54 4b 5f 46 4c 4f 41 54 20 29 3b 0a 20 20 20 20 TK_FLOAT );.
21a1b 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f testcase( p->o
21a1c 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 3b 0a 20 20 p==TK_NULL );.
21a1d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d testcase( p-
21a1e 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29 >op==TK_STRING )
21a1f 3b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c ;. /* Singl
21a20 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f e-instruction co
21a21 6e 73 74 61 6e 74 73 20 77 69 74 68 20 61 20 66 nstants with a f
21a22 69 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e ixed destination
21a23 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 are. ** be
21a24 74 74 65 72 20 64 6f 6e 65 20 69 6e 2d 6c 69 6e tter done in-lin
21a25 65 2e 20 20 49 66 20 77 65 20 66 61 63 74 6f 72 e. If we factor
21a26 20 74 68 65 6d 2c 20 74 68 65 79 20 77 69 6c 6c them, they will
21a27 20 6a 75 73 74 20 65 6e 64 0a 20 20 20 20 20 20 just end.
21a28 2a 2a 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67 ** up generating
21a29 20 61 6e 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 an OP_SCopy to
21a2a 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 74 move the value t
21a2b 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
21a2c 6e 0a 20 20 20 20 20 20 2a 2a 20 72 65 67 69 73 n. ** regis
21a2d 74 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 ter. */. re
21a2e 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
21a2f 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 case TK_UMINUS
21a30 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d : {. if( p-
21a31 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 >pLeft->op==TK_F
21a32 4c 4f 41 54 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 LOAT || p->pLeft
21a33 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 ->op==TK_INTEGER
21a34 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
21a35 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 0;. }.
21a36 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21a37 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
21a38 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21a39 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
21a3a 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 1;.}../*.** If p
21a3b 45 78 70 72 20 69 73 20 61 20 63 6f 6e 73 74 61 Expr is a consta
21a3c 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 nt expression th
21a3d 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 at is appropriat
21a3e 65 20 66 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 e for.** factori
21a3f 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 ng out of a loop
21a40 2c 20 74 68 65 6e 20 65 76 61 6c 75 61 74 65 20 , then evaluate
21a41 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a the expression.*
21a42 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 * into a registe
21a43 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 r and convert th
21a44 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 e expression int
21a45 6f 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 0a o a TK_REGISTER.
21a46 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a ** expression..*
21a47 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 /.static int eva
21a48 6c 43 6f 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 lConstExpr(Walke
21a49 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 r *pWalker, Expr
21a4a 20 2a 70 45 78 70 72 29 7b 0a 20 20 50 61 72 73 *pExpr){. Pars
21a4b 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c e *pParse = pWal
21a4c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 ker->pParse;. s
21a4d 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 witch( pExpr->op
21a4e 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
21a4f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 REGISTER: {.
21a50 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 return WRC_Pru
21a51 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ne;. }. ca
21a52 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a se TK_FUNCTION:.
21a53 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f case TK_AGG_
21a54 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 FUNCTION:. ca
21a55 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 se TK_CONST_FUNC
21a56 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 : {. /* The
21a57 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 20 arguments to a
21a58 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20 61 20 function have a
21a59 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f fixed destinatio
21a5a 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 61 72 6b n.. ** Mark
21a5b 20 74 68 65 6d 20 74 68 69 73 20 77 61 79 20 74 them this way t
21a5c 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 61 74 65 o avoid generate
21a5d 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 5f 53 43 d unneeded OP_SC
21a5e 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 opy. ** ins
21a5f 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 20 20 20 tructions. .
21a60 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c */. ExprL
21a61 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 ist *pList = pEx
21a62 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 pr->x.pList;.
21a63 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
21a64 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
21a65 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
21a66 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c );. if( pL
21a67 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ist ){. i
21a68 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 nt i = pList->nE
21a69 78 70 72 3b 0a 20 20 20 20 20 20 20 20 73 74 72 xpr;. str
21a6a 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
21a6b 6d 20 2a 70 49 74 65 6d 20 3d 20 70 4c 69 73 74 m *pItem = pList
21a6c 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 ->a;. for
21a6d 28 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 (; i>0; i--, pIt
21a6e 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 em++){.
21a6f 20 69 66 28 20 41 4c 57 41 59 53 28 70 49 74 65 if( ALWAYS(pIte
21a70 6d 2d 3e 70 45 78 70 72 29 20 29 20 70 49 74 65 m->pExpr) ) pIte
21a71 6d 2d 3e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 m->pExpr->flags
21a72 7c 3d 20 45 50 5f 46 69 78 65 64 44 65 73 74 3b |= EP_FixedDest;
21a73 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
21a74 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
21a75 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
21a76 69 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 isAppropriateFor
21a77 46 61 63 74 6f 72 69 6e 67 28 70 45 78 70 72 29 Factoring(pExpr)
21a78 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d ){. int r1 =
21a79 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
21a7a 0a 20 20 20 20 69 6e 74 20 72 32 3b 0a 20 20 20 . int r2;.
21a7b 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 r2 = sqlite3Exp
21a7c 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 rCodeTarget(pPar
21a7d 73 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a se, pExpr, r1);.
21a7e 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 31 if( NEVER(r1
21a7f 21 3d 72 32 29 20 29 20 73 71 6c 69 74 65 33 52 !=r2) ) sqlite3R
21a80 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
21a81 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 70 arse, r1);. p
21a82 45 78 70 72 2d 3e 6f 70 32 20 3d 20 70 45 78 70 Expr->op2 = pExp
21a83 72 2d 3e 6f 70 3b 0a 20 20 20 20 70 45 78 70 72 r->op;. pExpr
21a84 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 ->op = TK_REGIST
21a85 45 52 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 ER;. pExpr->i
21a86 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20 20 20 20 Table = r2;.
21a87 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 return WRC_Prune
21a88 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 ;. }. return W
21a89 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a RC_Continue;.}..
21a8a 2f 2a 0a 2a 2a 20 50 72 65 65 76 61 6c 75 61 74 /*.** Preevaluat
21a8b 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 e constant subex
21a8c 70 72 65 73 73 69 6f 6e 73 20 77 69 74 68 69 6e pressions within
21a8d 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65 pExpr and store
21a8e 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 the.** results
21a8f 69 6e 20 72 65 67 69 73 74 65 72 73 2e 20 20 4d in registers. M
21a90 6f 64 69 66 79 20 70 45 78 70 72 20 73 6f 20 74 odify pExpr so t
21a91 68 61 74 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 hat the constant
21a92 20 73 75 62 65 78 70 72 65 73 69 6f 6e 73 0a 2a subexpresions.*
21a93 2a 20 61 72 65 20 54 4b 5f 52 45 47 49 53 54 45 * are TK_REGISTE
21a94 52 20 6f 70 63 6f 64 65 73 20 74 68 61 74 20 72 R opcodes that r
21a95 65 66 65 72 20 74 6f 20 74 68 65 20 70 72 65 63 efer to the prec
21a96 6f 6d 70 75 74 65 64 20 76 61 6c 75 65 73 2e 0a omputed values..
21a97 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
21a98 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
21a99 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 prCodeConstants(
21a9a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
21a9b 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 57 xpr *pExpr){. W
21a9c 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 45 78 alker w;. w.xEx
21a9d 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 76 61 prCallback = eva
21a9e 6c 43 6f 6e 73 74 45 78 70 72 3b 0a 20 20 77 2e lConstExpr;. w.
21a9f 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 xSelectCallback
21aa0 3d 20 30 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 = 0;. w.pParse
21aa1 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 = pParse;. sqli
21aa2 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 te3WalkExpr(&w,
21aa3 70 45 78 70 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a pExpr);.}.../*.*
21aa4 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
21aa5 74 68 61 74 20 70 75 73 68 65 73 20 74 68 65 20 that pushes the
21aa6 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79 20 65 value of every e
21aa7 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 67 69 lement of the gi
21aa8 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f ven.** expressio
21aa9 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61 20 73 65 n list into a se
21aaa 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 73 74 quence of regist
21aab 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 ers beginning at
21aac 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 target..**.** R
21aad 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
21aae 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 65 76 61 of elements eva
21aaf 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 luated..*/.SQLIT
21ab0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
21ab1 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 lite3ExprCodeExp
21ab2 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a rList(. Parse *
21ab3 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 pParse, /* P
21ab4 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
21ab5 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c /. ExprList *pL
21ab6 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 65 78 ist, /* The ex
21ab7 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f pression list to
21ab8 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 be coded */. i
21ab9 6e 74 20 74 61 72 67 65 74 2c 20 20 20 20 20 20 nt target,
21aba 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 /* Where to wr
21abb 69 74 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 ite results */.
21abc 20 69 6e 74 20 64 6f 48 61 72 64 43 6f 70 79 20 int doHardCopy
21abd 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61 20 68 61 /* Make a ha
21abe 72 64 20 63 6f 70 79 20 6f 66 20 65 76 65 72 79 rd copy of every
21abf 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 element */.){.
21ac0 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
21ac1 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
21ac2 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61 73 73 65 int i, n;. asse
21ac3 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 0a rt( pList!=0 );.
21ac4 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74 assert( target
21ac5 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 70 4c 69 73 >0 );. n = pLis
21ac6 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 66 6f 72 28 t->nExpr;. for(
21ac7 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 pItem=pList->a,
21ac8 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 i=0; i<n; i++, p
21ac9 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 Item++){. if(
21aca 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 29 pItem->iAlias )
21acb 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65 67 {. int iReg
21acc 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 50 61 = codeAlias(pPa
21acd 72 73 65 2c 20 70 49 74 65 6d 2d 3e 69 41 6c 69 rse, pItem->iAli
21ace 61 73 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 as, pItem->pExpr
21acf 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 , target+i);.
21ad0 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c Vdbe *v = sql
21ad1 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 ite3GetVdbe(pPar
21ad2 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 se);. if( i
21ad3 52 65 67 21 3d 74 61 72 67 65 74 2b 69 20 29 7b Reg!=target+i ){
21ad4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21ad5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21ad6 5f 53 43 6f 70 79 2c 20 69 52 65 67 2c 20 74 61 _SCopy, iReg, ta
21ad7 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 7d rget+i);. }
21ad8 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
21ad9 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 sqlite3ExprCod
21ada 65 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d e(pParse, pItem-
21adb 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 2b 69 >pExpr, target+i
21adc 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
21add 20 64 6f 48 61 72 64 43 6f 70 79 20 26 26 20 21 doHardCopy && !
21ade 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
21adf 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
21ae0 20 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 sqlite3ExprHar
21ae1 64 43 6f 70 79 28 70 50 61 72 73 65 2c 20 74 61 dCopy(pParse, ta
21ae2 72 67 65 74 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a rget, n);. }.
21ae3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a }. return n;.
21ae4 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
21ae5 65 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f e code for a boo
21ae6 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 lean expression
21ae7 73 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 such that a jump
21ae8 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 is made.** to t
21ae9 68 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 he label "dest"
21aea 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f if the expressio
21aeb 6e 20 69 73 20 74 72 75 65 20 62 75 74 20 65 78 n is true but ex
21aec 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 ecution.** conti
21aed 6e 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 nues straight th
21aee 72 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 ru if the expres
21aef 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 2e 0a 2a sion is false..*
21af0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 *.** If the expr
21af1 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 ession evaluates
21af2 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 to NULL (neithe
21af3 72 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 r true nor false
21af4 29 2c 20 74 68 65 6e 0a 2a 2a 20 74 61 6b 65 20 ), then.** take
21af5 74 68 65 20 6a 75 6d 70 20 69 66 20 74 68 65 20 the jump if the
21af6 6a 75 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20 jumpIfNull flag
21af7 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 is SQLITE_JUMPIF
21af8 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 NULL..**.** This
21af9 20 63 6f 64 65 20 64 65 70 65 6e 64 73 20 6f 6e code depends on
21afa 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 63 the fact that c
21afb 65 72 74 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c ertain token val
21afc 75 65 73 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a ues (ex: TK_EQ).
21afd 2a 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 ** are the same
21afe 61 73 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 as opcode values
21aff 20 28 65 78 3a 20 4f 50 5f 45 71 29 20 74 68 61 (ex: OP_Eq) tha
21b00 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 t implement the
21b01 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a corresponding.**
21b02 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 53 70 65 operation. Spe
21b03 63 69 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e cial comments in
21b04 20 76 64 62 65 2e 63 20 61 6e 64 20 74 68 65 20 vdbe.c and the
21b05 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 mkopcodeh.awk sc
21b06 72 69 70 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d ript in.** the m
21b07 61 6b 65 20 70 72 6f 63 65 73 73 20 63 61 75 73 ake process caus
21b08 65 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 74 e these values t
21b09 6f 20 61 6c 69 67 6e 2e 20 20 41 73 73 65 72 74 o align. Assert
21b0a 28 29 73 20 69 6e 20 74 68 65 20 63 6f 64 65 0a ()s in the code.
21b0b 2a 2a 20 62 65 6c 6f 77 20 76 65 72 69 66 79 20 ** below verify
21b0c 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 73 that the numbers
21b0d 20 61 72 65 20 61 6c 69 67 6e 65 64 20 63 6f 72 are aligned cor
21b0e 72 65 63 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 rectly..*/.SQLIT
21b0f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21b10 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 qlite3ExprIfTrue
21b11 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
21b12 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 Expr *pExpr, int
21b13 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49 dest, int jumpI
21b14 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a fNull){. Vdbe *
21b15 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
21b16 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b e;. int op = 0;
21b17 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 . int regFree1
21b18 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72 = 0;. int regFr
21b19 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 ee2 = 0;. int r
21b1a 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74 1, r2;.. assert
21b1b 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51 ( jumpIfNull==SQ
21b1c 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 LITE_JUMPIFNULL
21b1d 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 || jumpIfNull==0
21b1e 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 );. if( NEVER(
21b1f 76 3d 3d 30 29 20 29 20 20 20 20 20 72 65 74 75 v==0) ) retu
21b20 72 6e 3b 20 20 2f 2a 20 45 78 69 73 74 61 6e 63 rn; /* Existanc
21b21 65 20 6f 66 20 56 44 42 45 20 63 68 65 63 6b 65 e of VDBE checke
21b22 64 20 62 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20 d by caller */.
21b23 20 69 66 28 20 4e 45 56 45 52 28 70 45 78 70 72 if( NEVER(pExpr
21b24 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 20 20 ==0) ) return;
21b25 2f 2a 20 4e 6f 20 77 61 79 20 74 68 69 73 20 63 /* No way this c
21b26 61 6e 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 6f an happen */. o
21b27 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 p = pExpr->op;.
21b28 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 switch( op ){.
21b29 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 case TK_AND:
21b2a 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20 3d {. int d2 =
21b2b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
21b2c 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 Label(v);.
21b2d 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 testcase( jumpIf
21b2e 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Null==0 );.
21b2f 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
21b30 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 ePush(pParse);.
21b31 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21b32 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 IfFalse(pParse,
21b33 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 32 pExpr->pLeft, d2
21b34 2c 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c 49 ,jumpIfNull^SQLI
21b35 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a TE_JUMPIFNULL);.
21b36 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
21b37 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 rIfTrue(pParse,
21b38 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 pExpr->pRight, d
21b39 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 est, jumpIfNull)
21b3a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
21b3b 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
21b3c 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 20 73 71 v, d2);. sq
21b3d 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
21b3e 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 p(pParse, 1);.
21b3f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21b40 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a . case TK_OR:
21b41 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 {. testcas
21b42 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 e( jumpIfNull==0
21b43 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
21b44 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72 3ExprIfTrue(pPar
21b45 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
21b46 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 , dest, jumpIfNu
21b47 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 ll);. sqlit
21b48 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 e3ExprIfTrue(pPa
21b49 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 rse, pExpr->pRig
21b4a 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 ht, dest, jumpIf
21b4b 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 Null);. bre
21b4c 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
21b4d 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 se TK_NOT: {.
21b4e 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d testcase( jum
21b4f 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 pIfNull==0 );.
21b50 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 sqlite3ExprI
21b51 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 fFalse(pParse, p
21b52 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 Expr->pLeft, des
21b53 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a t, jumpIfNull);.
21b54 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21b55 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c }. case TK_L
21b56 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c T:. case TK_L
21b57 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 E:. case TK_G
21b58 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 T:. case TK_G
21b59 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e E:. case TK_N
21b5a 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 E:. case TK_E
21b5b 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 Q: {. asser
21b5c 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 t( TK_LT==OP_Lt
21b5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
21b5e 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b TK_LE==OP_Le );
21b5f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21b60 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 K_GT==OP_Gt );.
21b61 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f assert( TK_
21b62 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 GE==OP_Ge );.
21b63 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 assert( TK_EQ
21b64 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 ==OP_Eq );.
21b65 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d assert( TK_NE==
21b66 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 OP_Ne );. t
21b67 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21b68 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LT );. test
21b69 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 case( op==TK_LE
21b6a 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21b6b 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a e( op==TK_GT );.
21b6c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21b6d 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 op==TK_GE );.
21b6e 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21b6f 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 =TK_EQ );.
21b70 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21b71 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 _NE );. tes
21b72 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c tcase( jumpIfNul
21b73 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f l==0 );. co
21b74 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 deCompareOperand
21b75 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d s(pParse, pExpr-
21b76 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 >pLeft, &r1, &re
21b77 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 gFree1,.
21b78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b79 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d pExpr-
21b7a 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 >pRight, &r2, &r
21b7b 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 egFree2);.
21b7c 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 codeCompare(pPar
21b7d 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
21b7e 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c , pExpr->pRight,
21b7f 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 op,.
21b80 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64 r1, r2, d
21b81 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 est, jumpIfNull)
21b82 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21b83 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b ( regFree1==0 );
21b84 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21b85 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a regFree2==0 );.
21b86 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21b87 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 }. case TK_I
21b88 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 SNULL:. case
21b89 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 TK_NOTNULL: {.
21b8a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49 assert( TK_I
21b8b 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c SNULL==OP_IsNull
21b8c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21b8d 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 ( TK_NOTNULL==OP
21b8e 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 _NotNull );.
21b8f 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21b90 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 TK_ISNULL );.
21b91 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21b92 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 =TK_NOTNULL );.
21b93 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 r1 = sqlite
21b94 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
21b95 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
21b96 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a ft, ®Free1);.
21b97 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21b98 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 eAddOp2(v, op, r
21b99 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 1, dest);.
21b9a 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
21b9b 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 e1==0 );. b
21b9c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
21b9d 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a case TK_BETWEEN:
21b9e 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78 {. /* x
21b9f 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a BETWEEN y AND z
21ba0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 . **.
21ba1 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 ** Is equivalent
21ba2 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 to . **.
21ba3 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 ** x>=y A
21ba4 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a ND x<=z. **
21ba5 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 . ** Code i
21ba6 74 20 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e t as such, takin
21ba7 67 20 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65 g care to do the
21ba8 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 common subexpre
21ba9 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 ssion. ** e
21baa 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 lementation of x
21bab 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
21bac 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 Expr exprAnd;.
21bad 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 Expr compLe
21bae 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63 ft;. Expr c
21baf 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 ompRight;.
21bb0 45 78 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 Expr exprX;..
21bb1 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
21bb2 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
21bb3 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
21bb4 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 58 20 );. exprX
21bb5 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b = *pExpr->pLeft;
21bb6 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f . exprAnd.o
21bb7 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 p = TK_AND;.
21bb8 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 exprAnd.pLeft
21bb9 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 = &compLeft;.
21bba 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 exprAnd.pRigh
21bbb 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a t = &compRight;.
21bbc 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f compLeft.o
21bbd 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 p = TK_GE;.
21bbe 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 compLeft.pLeft
21bbf 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 = &exprX;.
21bc0 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 compLeft.pRight
21bc1 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 = pExpr->x.pList
21bc2 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 ->a[0].pExpr;.
21bc3 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 compRight.op
21bc4 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 = TK_LE;.
21bc5 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20 compRight.pLeft
21bc6 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 = &exprX;.
21bc7 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74 compRight.pRight
21bc8 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 = pExpr->x.pLis
21bc9 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 t->a[1].pExpr;.
21bca 20 20 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c exprX.iTabl
21bcb 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 e = sqlite3ExprC
21bcc 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
21bcd 26 65 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 &exprX, ®Free
21bce 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 1);. testca
21bcf 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
21bd0 29 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f );. exprX.o
21bd1 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b p = TK_REGISTER;
21bd2 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21bd3 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 jumpIfNull==0 )
21bd4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
21bd5 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65 xprIfTrue(pParse
21bd6 2c 20 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 , &exprAnd, dest
21bd7 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
21bd8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21bd9 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
21bda 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 . r1 = sqli
21bdb 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
21bdc 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 pParse, pExpr, &
21bdd 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 regFree1);.
21bde 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21bdf 70 33 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c p3(v, OP_If, r1,
21be0 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c dest, jumpIfNul
21be1 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 l!=0);. tes
21be2 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
21be3 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 =0 );. test
21be4 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c case( jumpIfNull
21be5 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
21be6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
21be7 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
21be8 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 mpReg(pParse, re
21be9 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 gFree1);. sqlit
21bea 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
21beb 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 (pParse, regFree
21bec 32 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 2); .}../*.** G
21bed 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 enerate code for
21bee 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 a boolean expre
21bef 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 ssion such that
21bf0 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a a jump is made.*
21bf1 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 * to the label "
21bf2 64 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70 dest" if the exp
21bf3 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 ression is false
21bf4 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a but execution.*
21bf5 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 * continues stra
21bf6 69 67 68 74 20 74 68 72 75 20 69 66 20 74 68 65 ight thru if the
21bf7 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 expression is t
21bf8 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 rue..**.** If th
21bf9 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 e expression eva
21bfa 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 luates to NULL (
21bfb 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 neither true nor
21bfc 20 66 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 false) then.**
21bfd 6a 75 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 jump if jumpIfNu
21bfe 6c 6c 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d ll is SQLITE_JUM
21bff 50 49 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 PIFNULL or fall
21c00 74 68 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49 through if jumpI
21c01 66 4e 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a fNull.** is 0..*
21c02 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21c03 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
21c04 72 49 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a rIfFalse(Parse *
21c05 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 pParse, Expr *pE
21c06 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 xpr, int dest, i
21c07 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a nt jumpIfNull){.
21c08 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
21c09 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 se->pVdbe;. int
21c0a 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 op = 0;. int r
21c0b 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 egFree1 = 0;. i
21c0c 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b nt regFree2 = 0;
21c0d 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a . int r1, r2;..
21c0e 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 assert( jumpIf
21c0f 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d Null==SQLITE_JUM
21c10 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 PIFNULL || jumpI
21c11 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 fNull==0 );. if
21c12 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 ( NEVER(v==0) )
21c13 72 65 74 75 72 6e 3b 20 2f 2a 20 45 78 69 73 74 return; /* Exist
21c14 61 6e 63 65 20 6f 66 20 56 44 42 45 20 63 68 65 ance of VDBE che
21c15 63 6b 65 64 20 62 79 20 63 61 6c 6c 65 72 20 2a cked by caller *
21c16 2f 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 /. if( pExpr==0
21c17 20 29 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a 20 ) return;..
21c18 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 /* The value of
21c19 20 70 45 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f pExpr->op and o
21c1a 70 20 61 72 65 20 72 65 6c 61 74 65 64 20 61 73 p are related as
21c1b 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 follows:. **.
21c1c 20 2a 2a 20 20 20 20 20 20 20 70 45 78 70 72 2d ** pExpr-
21c1d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 6f >op o
21c1e 70 0a 20 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d p. ** ---
21c1f 2d 2d 2d 2d 2d 2d 20 20 20 20 20 20 20 20 20 20 ------
21c20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 ----------. **
21c21 20 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 TK_ISNULL
21c22 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e OP_NotN
21c23 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 ull. ** T
21c24 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 K_NOTNULL
21c25 20 20 4f 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a OP_IsNull. **
21c26 20 20 20 20 20 20 20 54 4b 5f 4e 45 20 20 20 20 TK_NE
21c27 20 20 20 20 20 20 20 20 20 20 4f 50 5f 45 71 0a OP_Eq.
21c28 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 45 51 ** TK_EQ
21c29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 OP
21c2a 5f 4e 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 _Ne. ** T
21c2b 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20 K_GT
21c2c 20 20 4f 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 OP_Le. **
21c2d 20 20 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 TK_LE
21c2e 20 20 20 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a OP_Gt. **
21c2f 20 20 20 20 20 20 20 54 4b 5f 47 45 20 20 20 20 TK_GE
21c30 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a OP_Lt.
21c31 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 ** TK_LT
21c32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50 OP
21c33 5f 47 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f _Ge. **. ** Fo
21c34 72 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 6f r other values o
21c35 66 20 70 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 f pExpr->op, op
21c36 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 is undefined and
21c37 20 75 6e 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68 unused.. ** Th
21c38 65 20 76 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61 e value of TK_ a
21c39 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 nd OP_ constants
21c3a 20 61 72 65 20 61 72 72 61 6e 67 65 64 20 73 75 are arranged su
21c3b 63 68 20 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 ch that we. **
21c3c 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 20 can compute the
21c3d 6d 61 70 70 69 6e 67 20 61 62 6f 76 65 20 75 73 mapping above us
21c3e 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ing the followin
21c3f 67 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 g expression..
21c40 2a 2a 20 41 73 73 65 72 74 28 29 73 20 76 65 72 ** Assert()s ver
21c41 69 66 79 20 74 68 61 74 20 74 68 65 20 63 6f 6d ify that the com
21c42 70 75 74 61 74 69 6f 6e 20 69 73 20 63 6f 72 72 putation is corr
21c43 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d ect.. */. op =
21c44 20 28 28 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b ((pExpr->op+(TK
21c45 5f 49 53 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28 _ISNULL&1))^1)-(
21c46 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 TK_ISNULL&1);..
21c47 20 2f 2a 20 56 65 72 69 66 79 20 63 6f 72 72 65 /* Verify corre
21c48 63 74 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 ct alignment of
21c49 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 TK_ and OP_ cons
21c4a 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 tants. */. ass
21c4b 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d ert( pExpr->op!=
21c4c 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d TK_ISNULL || op=
21c4d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 =OP_NotNull );.
21c4e 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
21c4f 6f 70 21 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c op!=TK_NOTNULL |
21c50 7c 20 6f 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 | op==OP_IsNull
21c51 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 );. assert( pEx
21c52 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c pr->op!=TK_NE ||
21c53 20 6f 70 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 op==OP_Eq );.
21c54 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f assert( pExpr->o
21c55 70 21 3d 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d p!=TK_EQ || op==
21c56 4f 50 5f 4e 65 20 29 3b 0a 20 20 61 73 73 65 72 OP_Ne );. asser
21c57 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b t( pExpr->op!=TK
21c58 5f 4c 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 _LT || op==OP_Ge
21c59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
21c5a 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c xpr->op!=TK_LE |
21c5b 7c 20 6f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 | op==OP_Gt );.
21c5c 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
21c5d 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d op!=TK_GT || op=
21c5e 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65 =OP_Le );. asse
21c5f 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 rt( pExpr->op!=T
21c60 4b 5f 47 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c K_GE || op==OP_L
21c61 74 20 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 t );.. switch(
21c62 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 pExpr->op ){.
21c63 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a case TK_AND: {.
21c64 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21c65 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b jumpIfNull==0 );
21c66 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
21c67 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 prIfFalse(pParse
21c68 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
21c69 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c dest, jumpIfNull
21c6a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21c6b 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 ExprIfFalse(pPar
21c6c 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 se, pExpr->pRigh
21c6d 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e t, dest, jumpIfN
21c6e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 ull);. brea
21c6f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21c70 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 e TK_OR: {.
21c71 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 int d2 = sqlite
21c72 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 3VdbeMakeLabel(v
21c73 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21c74 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 e( jumpIfNull==0
21c75 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
21c76 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 3ExprCachePush(p
21c77 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 Parse);. sq
21c78 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 lite3ExprIfTrue(
21c79 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
21c7a 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70 49 66 Left, d2, jumpIf
21c7b 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 Null^SQLITE_JUMP
21c7c 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 IFNULL);. s
21c7d 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 qlite3ExprIfFals
21c7e 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
21c7f 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a >pRight, dest, j
21c80 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 umpIfNull);.
21c81 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
21c82 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 olveLabel(v, d2)
21c83 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
21c84 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 xprCachePop(pPar
21c85 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 se, 1);. br
21c86 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
21c87 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 ase TK_NOT: {.
21c88 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 sqlite3ExprI
21c89 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 fTrue(pParse, pE
21c8a 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 xpr->pLeft, dest
21c8b 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 , jumpIfNull);.
21c8c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21c8d 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 }. case TK_LT
21c8e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 :. case TK_LE
21c8f 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54 :. case TK_GT
21c90 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45 :. case TK_GE
21c91 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 :. case TK_NE
21c92 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51 :. case TK_EQ
21c93 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 : {. testca
21c94 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b se( op==TK_LT );
21c95 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21c96 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 op==TK_LE );.
21c97 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
21c98 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 ==TK_GT );.
21c99 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21c9a 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 K_GE );. te
21c9b 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 stcase( op==TK_E
21c9c 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 Q );. testc
21c9d 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 ase( op==TK_NE )
21c9e 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21c9f 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 ( jumpIfNull==0
21ca0 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d );. codeCom
21ca1 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 pareOperands(pPa
21ca2 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
21ca3 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 t, &r1, ®Free
21ca4 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1,.
21ca5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21ca6 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 pExpr->pRig
21ca7 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 ht, &r2, ®Fre
21ca8 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 e2);. codeC
21ca9 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 ompare(pParse, p
21caa 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 Expr->pLeft, pEx
21cab 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a pr->pRight, op,.
21cac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21cad 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 r1, r2, dest,
21cae 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 jumpIfNull);.
21caf 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
21cb0 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
21cb1 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
21cb2 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree2==0 );.
21cb3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
21cb4 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c case TK_ISNULL
21cb5 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f :. case TK_NO
21cb6 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 74 TNULL: {. t
21cb7 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21cb8 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 ISNULL );.
21cb9 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21cba 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 _NOTNULL );.
21cbb 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 r1 = sqlite3Ex
21cbc 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
21cbd 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
21cbe 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 ®Free1);.
21cbf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21cc0 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 dOp2(v, op, r1,
21cc1 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73 dest);. tes
21cc2 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
21cc3 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 =0 );. brea
21cc4 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21cc5 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a e TK_BETWEEN: {.
21cc6 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42 45 /* x BE
21cc7 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 TWEEN y AND z.
21cc8 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
21cc9 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f Is equivalent to
21cca 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 . **.
21ccb 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 ** x>=y AND
21ccc 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 x<=z. **.
21ccd 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20 61 ** Code it a
21cce 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 63 s such, taking c
21ccf 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63 6f are to do the co
21cd0 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73 69 mmon subexpressi
21cd1 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d on. ** elem
21cd2 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 entation of x..
21cd3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 */. Ex
21cd4 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20 20 pr exprAnd;.
21cd5 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b Expr compLeft;
21cd6 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 . Expr comp
21cd7 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78 70 Right;. Exp
21cd8 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20 20 r exprX;..
21cd9 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
21cda 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
21cdb 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b EP_xIsSelect) );
21cdc 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20 2a . exprX = *
21cdd 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 pExpr->pLeft;.
21cde 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d exprAnd.op =
21cdf 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 TK_AND;. e
21ce0 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 xprAnd.pLeft = &
21ce1 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 compLeft;.
21ce2 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20 3d exprAnd.pRight =
21ce3 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 &compRight;.
21ce4 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d compLeft.op =
21ce5 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f TK_GE;. co
21ce6 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 mpLeft.pLeft = &
21ce7 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d exprX;. com
21ce8 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20 70 pLeft.pRight = p
21ce9 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 Expr->x.pList->a
21cea 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [0].pExpr;.
21ceb 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 compRight.op =
21cec 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d TK_LE;. com
21ced 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 pRight.pLeft = &
21cee 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d exprX;. com
21cef 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20 pRight.pRight =
21cf0 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e pExpr->x.pList->
21cf1 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 a[1].pExpr;.
21cf2 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d exprX.iTable =
21cf3 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21cf4 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78 Temp(pParse, &ex
21cf5 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b prX, ®Free1);
21cf6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21cf7 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
21cf8 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d exprX.op =
21cf9 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 TK_REGISTER;.
21cfa 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 testcase( ju
21cfb 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 mpIfNull==0 );.
21cfc 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21cfd 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 IfFalse(pParse,
21cfe 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 &exprAnd, dest,
21cff 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 jumpIfNull);.
21d00 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21d01 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
21d02 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 r1 = sqlite
21d03 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 3ExprCodeTemp(pP
21d04 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 arse, pExpr, &re
21d05 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 gFree1);. s
21d06 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
21d07 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 (v, OP_IfNot, r1
21d08 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 , dest, jumpIfNu
21d09 6c 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 ll!=0);. te
21d0a 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 stcase( regFree1
21d0b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 ==0 );. tes
21d0c 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c tcase( jumpIfNul
21d0d 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 l==0 );. br
21d0e 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
21d0f 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
21d10 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
21d11 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 egFree1);. sqli
21d12 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
21d13 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 g(pParse, regFre
21d14 65 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f e2);.}../*.** Do
21d15 20 61 20 64 65 65 70 20 63 6f 6d 70 61 72 69 73 a deep comparis
21d16 6f 6e 20 6f 66 20 74 77 6f 20 65 78 70 72 65 73 on of two expres
21d17 73 69 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 74 sion trees. Ret
21d18 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 urn TRUE (non-ze
21d19 72 6f 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 ro).** if they a
21d1a 72 65 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 re identical and
21d1b 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 return FALSE if
21d1c 20 74 68 65 79 20 64 69 66 66 65 72 20 69 6e 20 they differ in
21d1d 61 6e 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 any way..**.** S
21d1e 6f 6d 65 74 69 6d 65 73 20 74 68 69 73 20 72 6f ometimes this ro
21d1f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 utine will retur
21d20 6e 20 46 41 4c 53 45 20 65 76 65 6e 20 69 66 20 n FALSE even if
21d21 74 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 the two expressi
21d22 6f 6e 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72 ons.** really ar
21d23 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 49 e equivalent. I
21d24 66 20 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76 f we cannot prov
21d25 65 20 74 68 61 74 20 74 68 65 20 65 78 70 72 65 e that the expre
21d26 73 73 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64 ssions are.** id
21d27 65 6e 74 69 63 61 6c 2c 20 77 65 20 72 65 74 75 entical, we retu
21d28 72 6e 20 46 41 4c 53 45 20 6a 75 73 74 20 74 6f rn FALSE just to
21d29 20 62 65 20 73 61 66 65 2e 20 20 53 6f 20 69 66 be safe. So if
21d2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
21d2b 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 2c 20 returns false,
21d2c 74 68 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20 then you do not
21d2d 72 65 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20 really know for
21d2e 63 65 72 74 61 69 6e 20 69 66 20 74 68 65 20 74 certain if the t
21d2f 77 6f 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e wo.** expression
21d30 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 s are the same.
21d31 20 42 75 74 20 69 66 20 79 6f 75 20 67 65 74 20 But if you get
21d32 61 20 54 52 55 45 20 72 65 74 75 72 6e 2c 20 74 a TRUE return, t
21d33 68 65 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62 hen you.** can b
21d34 65 20 73 75 72 65 20 74 68 65 20 65 78 70 72 65 e sure the expre
21d35 73 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73 ssions are the s
21d36 61 6d 65 2e 20 20 49 6e 20 74 68 65 20 70 6c 61 ame. In the pla
21d37 63 65 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 69 ces where.** thi
21d38 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
21d39 64 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 68 d, it does not h
21d3a 75 72 74 20 74 6f 20 67 65 74 20 61 6e 20 65 78 urt to get an ex
21d3b 74 72 61 20 46 41 4c 53 45 20 2d 20 74 68 61 74 tra FALSE - that
21d3c 0a 2a 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72 .** just might r
21d3d 65 73 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c esult in some sl
21d3e 69 67 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f ightly slower co
21d3f 64 65 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69 de. But returni
21d40 6e 67 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 ng.** an incorre
21d41 63 74 20 54 52 55 45 20 63 6f 75 6c 64 20 6c 65 ct TRUE could le
21d42 61 64 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74 ad to a malfunct
21d43 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
21d44 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
21d45 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78 e3ExprCompare(Ex
21d46 70 72 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 42 pr *pA, Expr *pB
21d47 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 ){. int i;. if
21d48 28 20 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29 ( pA==0||pB==0 )
21d49 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 3d {. return pB=
21d4a 3d 70 41 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 =pA;. }. asser
21d4b 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 t( !ExprHasAnyPr
21d4c 6f 70 65 72 74 79 28 70 41 2c 20 45 50 5f 54 6f operty(pA, EP_To
21d4d 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 kenOnly|EP_Reduc
21d4e 65 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ed) );. assert(
21d4f 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 !ExprHasAnyProp
21d50 65 72 74 79 28 70 42 2c 20 45 50 5f 54 6f 6b 65 erty(pB, EP_Toke
21d51 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 nOnly|EP_Reduced
21d52 29 20 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48 ) );. if( ExprH
21d53 61 73 50 72 6f 70 65 72 74 79 28 70 41 2c 20 45 asProperty(pA, E
21d54 50 5f 78 49 73 53 65 6c 65 63 74 29 20 7c 7c 20 P_xIsSelect) ||
21d55 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
21d56 70 42 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pB, EP_xIsSelect
21d57 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
21d58 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 41 0;. }. if( (pA
21d59 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 ->flags & EP_Dis
21d5a 74 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61 tinct)!=(pB->fla
21d5b 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 gs & EP_Distinct
21d5c 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 ) ) return 0;.
21d5d 69 66 28 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e if( pA->op!=pB->
21d5e 6f 70 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 op ) return 0;.
21d5f 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 if( !sqlite3Exp
21d60 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 rCompare(pA->pLe
21d61 66 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 ft, pB->pLeft) )
21d62 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 return 0;. if(
21d63 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d !sqlite3ExprCom
21d64 70 61 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c pare(pA->pRight,
21d65 20 70 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72 pB->pRight) ) r
21d66 65 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 eturn 0;.. if(
21d67 70 41 2d 3e 78 2e 70 4c 69 73 74 20 26 26 20 70 pA->x.pList && p
21d68 42 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 B->x.pList ){.
21d69 20 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 if( pA->x.pLis
21d6a 74 2d 3e 6e 45 78 70 72 21 3d 70 42 2d 3e 78 2e t->nExpr!=pB->x.
21d6b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 20 72 pList->nExpr ) r
21d6c 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 66 6f 72 eturn 0;. for
21d6d 28 69 3d 30 3b 20 69 3c 70 41 2d 3e 78 2e 70 4c (i=0; i<pA->x.pL
21d6e 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 ist->nExpr; i++)
21d6f 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 {. Expr *pE
21d70 78 70 72 41 20 3d 20 70 41 2d 3e 78 2e 70 4c 69 xprA = pA->x.pLi
21d71 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a st->a[i].pExpr;.
21d72 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 Expr *pExp
21d73 72 42 20 3d 20 70 42 2d 3e 78 2e 70 4c 69 73 74 rB = pB->x.pList
21d74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 ->a[i].pExpr;.
21d75 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 if( !sqlite3
21d76 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45 78 70 ExprCompare(pExp
21d77 72 41 2c 20 70 45 78 70 72 42 29 20 29 20 72 65 rA, pExprB) ) re
21d78 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
21d79 7d 65 6c 73 65 20 69 66 28 20 70 41 2d 3e 78 2e }else if( pA->x.
21d7a 70 4c 69 73 74 20 7c 7c 20 70 42 2d 3e 78 2e 70 pList || pB->x.p
21d7b 4c 69 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 List ){. retu
21d7c 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 rn 0;. }.. if(
21d7d 20 70 41 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d pA->iTable!=pB-
21d7e 3e 69 54 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 >iTable || pA->i
21d7f 43 6f 6c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c Column!=pB->iCol
21d80 75 6d 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a umn ) return 0;.
21d81 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
21d82 70 65 72 74 79 28 70 41 2c 20 45 50 5f 49 6e 74 perty(pA, EP_Int
21d83 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 69 66 Value) ){. if
21d84 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21d85 74 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c ty(pB, EP_IntVal
21d86 75 65 29 20 7c 7c 20 70 41 2d 3e 75 2e 69 56 61 ue) || pA->u.iVa
21d87 6c 75 65 21 3d 70 42 2d 3e 75 2e 69 56 61 6c 75 lue!=pB->u.iValu
21d88 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 e ){. retur
21d89 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c n 0;. }. }el
21d8a 73 65 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54 se if( pA->op!=T
21d8b 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e K_COLUMN && pA->
21d8c 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 u.zToken ){.
21d8d 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
21d8e 72 74 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 rty(pB, EP_IntVa
21d8f 6c 75 65 29 20 7c 7c 20 4e 45 56 45 52 28 70 42 lue) || NEVER(pB
21d90 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 29 20 29 ->u.zToken==0) )
21d91 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 return 0;. i
21d92 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
21d93 70 28 70 41 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 70 p(pA->u.zToken,p
21d94 42 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 21 3d 30 20 B->u.zToken)!=0
21d95 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
21d96 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 0;. }. }. r
21d97 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn 1;.}.../*.
21d98 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 ** Add a new ele
21d99 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 ment to the pAgg
21d9a 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 Info->aCol[] arr
21d9b 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 ay. Return the
21d9c 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 index of.** the
21d9d 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 new element. Re
21d9e 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 turn a negative
21d9f 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 number if malloc
21da0 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 fails..*/.stati
21da1 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f c int addAggInfo
21da2 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a Column(sqlite3 *
21da3 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e db, AggInfo *pIn
21da4 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 fo){. int i;.
21da5 70 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 pInfo->aCol = sq
21da6 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 lite3ArrayAlloca
21da7 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 te(. db,.
21da8 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f pInfo->aCo
21da9 6c 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 l,. sizeof
21daa 28 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 (pInfo->aCol[0])
21dab 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 ,. 3,.
21dac 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 &pInfo->nColu
21dad 6d 6e 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 mn,. &pInf
21dae 6f 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c o->nColumnAlloc,
21daf 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a . &i. );.
21db0 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 return i;.}
21db1 20 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e ../*.** Add a n
21db2 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 ew element to th
21db3 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e e pAggInfo->aFun
21db4 63 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 c[] array. Retu
21db5 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a rn the index of.
21db6 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 ** the new eleme
21db7 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 nt. Return a ne
21db8 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 gative number if
21db9 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a malloc fails..*
21dba 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 /.static int add
21dbb 41 67 67 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 AggInfoFunc(sqli
21dbc 74 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f te3 *db, AggInfo
21dbd 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 *pInfo){. int
21dbe 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e i;. pInfo->aFun
21dbf 63 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 c = sqlite3Array
21dc0 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 Allocate(.
21dc1 20 64 62 2c 20 0a 20 20 20 20 20 20 20 70 49 6e db, . pIn
21dc2 66 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 fo->aFunc,.
21dc3 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e sizeof(pInfo->
21dc4 61 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 aFunc[0]),.
21dc5 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 3,. &pIn
21dc6 66 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 fo->nFunc,.
21dc7 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 &pInfo->nFuncA
21dc8 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a lloc,. &i.
21dc9 20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b );. return i;
21dca 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 .} ../*.** Th
21dcb 69 73 20 69 73 20 74 68 65 20 78 45 78 70 72 43 is is the xExprC
21dcc 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72 allback for a tr
21dcd 65 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 ee walker. It i
21dce 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 s used to.** imp
21dcf 6c 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78 lement sqlite3Ex
21dd0 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 prAnalyzeAggrega
21dd1 74 65 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69 tes(). See sqli
21dd2 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 te3ExprAnalyzeAg
21dd3 67 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 gregates.** for
21dd4 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
21dd5 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 mation..*/.stati
21dd6 63 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 c int analyzeAgg
21dd7 72 65 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 regate(Walker *p
21dd8 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 Walker, Expr *pE
21dd9 78 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 xpr){. int i;.
21dda 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e NameContext *pN
21ddb 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 C = pWalker->u.p
21ddc 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 NC;. Parse *pPa
21ddd 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 rse = pNC->pPars
21dde 65 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 e;. SrcList *pS
21ddf 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 rcList = pNC->pS
21de0 72 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 rcList;. AggInf
21de1 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e o *pAggInfo = pN
21de2 43 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 C->pAggInfo;..
21de3 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f switch( pExpr->o
21de4 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b p ){. case TK
21de5 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 _AGG_COLUMN:.
21de6 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a case TK_COLUMN:
21de7 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 {. testcas
21de8 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b e( pExpr->op==TK
21de9 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 _AGG_COLUMN );.
21dea 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
21deb 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c Expr->op==TK_COL
21dec 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 UMN );. /*
21ded 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 Check to see if
21dee 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e the column is in
21def 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c one of the tabl
21df0 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 es in the FROM.
21df1 20 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f ** clause o
21df2 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 f the aggregate
21df3 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 query */. i
21df4 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 4c 69 f( ALWAYS(pSrcLi
21df5 73 74 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 st!=0) ){.
21df6 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
21df7 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 _item *pItem = p
21df8 53 72 63 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 SrcList->a;.
21df9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
21dfa 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 SrcList->nSrc; i
21dfb 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
21dfc 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41 struct A
21dfd 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c ggInfo_col *pCol
21dfe 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 ;. asse
21dff 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 rt( !ExprHasAnyP
21e00 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21e01 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 P_TokenOnly|EP_R
21e02 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20 educed) );.
21e03 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e if( pExpr->
21e04 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 iTable==pItem->i
21e05 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 Cursor ){.
21e06 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72 /* If we r
21e07 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c each this point,
21e08 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 70 it means that p
21e09 45 78 70 72 20 72 65 66 65 72 73 20 74 6f 20 61 Expr refers to a
21e0a 20 74 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 table.
21e0b 20 20 20 2a 2a 20 74 68 61 74 20 69 73 20 69 6e ** that is in
21e0c 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
21e0d 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 of the aggregat
21e0e 65 20 71 75 65 72 79 2e 20 20 0a 20 20 20 20 20 e query. .
21e0f 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
21e10 20 20 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e ** Make an
21e11 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 63 entry for the c
21e12 6f 6c 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 olumn in pAggInf
21e13 6f 2d 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 o->aCol[] if the
21e14 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a re. *
21e15 2a 20 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 * is not an entr
21e16 79 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e y there already.
21e17 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a . */.
21e18 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 int
21e19 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 k;. p
21e1a 43 6f 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e Col = pAggInfo->
21e1b 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 aCol;.
21e1c 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 for(k=0; k<pAg
21e1d 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 gInfo->nColumn;
21e1e 6b 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 k++, pCol++){.
21e1f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
21e20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 pCol->iTable==pE
21e21 78 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 xpr->iTable &&.
21e22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e23 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d pCol->iColumn==
21e24 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 pExpr->iColumn )
21e25 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
21e26 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
21e27 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
21e28 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
21e29 20 20 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 49 if( (k>=pAggI
21e2a 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20 nfo->nColumn).
21e2b 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 6b && (k
21e2c 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c = addAggInfoCol
21e2d 75 6d 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 umn(pParse->db,
21e2e 70 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20 pAggInfo))>=0 .
21e2f 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 ){.
21e30 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c pCol
21e31 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 = &pAggInfo->aC
21e32 6f 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20 ol[k];.
21e33 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20 pCol->pTab
21e34 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 = pExpr->pTab;.
21e35 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f pCo
21e36 6c 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 78 70 l->iTable = pExp
21e37 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 r->iTable;.
21e38 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 pCol->i
21e39 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e Column = pExpr->
21e3a 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 iColumn;.
21e3b 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65 pCol->iMe
21e3c 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d m = ++pParse->nM
21e3d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 em;.
21e3e 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 pCol->iSorterC
21e3f 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 olumn = -1;.
21e40 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e pCol->
21e41 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 pExpr = pExpr;.
21e42 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 if(
21e43 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 pAggInfo->pGrou
21e44 70 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 pBy ){.
21e45 20 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b int j, n;
21e46 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
21e47 20 45 78 70 72 4c 69 73 74 20 2a 70 47 42 20 3d ExprList *pGB =
21e48 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 pAggInfo->pGrou
21e49 70 42 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20 pBy;.
21e4a 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 struct Expr
21e4b 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d List_item *pTerm
21e4c 20 3d 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20 = pGB->a;.
21e4d 20 20 20 20 20 20 20 20 20 20 20 6e 20 3d 20 70 n = p
21e4e 47 42 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 GB->nExpr;.
21e4f 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a for(j
21e50 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 =0; j<n; j++, pT
21e51 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 erm++){.
21e52 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a Expr *
21e53 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 pE = pTerm->pExp
21e54 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 r;.
21e55 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d if( pE->op=
21e56 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 =TK_COLUMN && pE
21e57 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d ->iTable==pExpr-
21e58 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 >iTable &&.
21e59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e5a 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 pE->iColumn==pE
21e5b 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a xpr->iColumn ){.
21e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e5d 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 pCol->iSorte
21e5e 72 43 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20 rColumn = j;.
21e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e60 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
21e61 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
21e62 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
21e63 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
21e64 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
21e65 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c pCol->iSorterCol
21e66 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 umn<0 ){.
21e67 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 pCol->i
21e68 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70 SorterColumn = p
21e69 41 67 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e AggInfo->nSortin
21e6a 67 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 gColumn++;.
21e6b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
21e6c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
21e6d 20 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 /* There is
21e6e 20 6e 6f 77 20 61 6e 20 65 6e 74 72 79 20 66 6f now an entry fo
21e6f 72 20 70 45 78 70 72 20 69 6e 20 70 41 67 67 49 r pExpr in pAggI
21e70 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 nfo->aCol[] (eit
21e71 68 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 her.
21e72 2a 2a 20 62 65 63 61 75 73 65 20 69 74 20 77 61 ** because it wa
21e73 73 20 74 68 65 72 65 20 62 65 66 6f 72 65 20 6f s there before o
21e74 72 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 r because we jus
21e75 74 20 63 72 65 61 74 65 64 20 69 74 29 2e 0a 20 t created it)..
21e76 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 43 6f ** Co
21e77 6e 76 65 72 74 20 74 68 65 20 70 45 78 70 72 20 nvert the pExpr
21e78 74 6f 20 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 to be a TK_AGG_C
21e79 4f 4c 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 OLUMN referring
21e7a 74 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 to that.
21e7b 20 20 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d ** pAggInfo-
21e7c 3e 61 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20 >aCol[] entry..
21e7d 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 */.
21e7e 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53 65 ExprSe
21e7f 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 tIrreducible(pEx
21e80 70 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 pr);.
21e81 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f pExpr->pAggInfo
21e82 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 = pAggInfo;.
21e83 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
21e84 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 op = TK_AGG_COLU
21e85 4d 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 MN;.
21e86 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 28 69 pExpr->